<commit_before>#include "nan.h"
using namespace v8;

#include "runas.h"

namespace {

NAN_METHOD(Runas) {
  NanScope();

  if (!args[0]->IsString() || !args[1]->IsArray() || !args[2]->IsObject())
    return NanThrowTypeError("Bad argument");

  std::string command(*String::Utf8Value(args[0]));
  std::vector<std::string> c_args;

  Handle<Array> v_args = Handle<Array>::Cast(args[1]);
  uint32_t length = v_args->Length();

  c_args.reserve(length);
  for (uint32_t i = 0; i < length; ++i) {
    std::string arg(*String::Utf8Value(v_args->Get(i)));
    c_args.push_back(arg);
  }

  Handle<Object> v_options = args[2]->ToObject();
  int options = runas::OPTION_NONE;
  if (v_options->Get(NanNew<String>("hide"))->BooleanValue())
    options |= runas::OPTION_HIDE;
  if (v_options->Get(NanNew<String>("admin"))->BooleanValue())
    options |= runas::OPTION_ADMIN;

  std::string std_input;
  Handle<Value> v_stdin = v_options->Get(NanNew<String>("stdin"));
  if (!v_stdin->IsUndefined())
    std_input = *String::Utf8Value(v_stdin);

  std::string std_output;
  bool need_stdout = v_options->Get(NanNew<String>("stdout"))->BooleanValue();

  int code = -1;
  runas::Runas(command, c_args, std_input, need_stdout ? &std_output : NULL, options, &code);

  if (need_stdout && code == 0)
    NanReturnValue(NanNew<String>(std_output.data(), std_output.size()));
  else
    NanReturnValue(NanNew<Integer>(code));
}

void Init(Handle<Object> exports) {
  NODE_SET_METHOD(exports, "runas", Runas);
}

}  // namespace

NODE_MODULE(runas, Init)<commit_msg>Return an object in runas when captureOutput is specified<commit_after>#include "nan.h"
using namespace v8;

#include "runas.h"

namespace {

NAN_METHOD(Runas) {
  NanScope();

  if (!args[0]->IsString() || !args[1]->IsArray() || !args[2]->IsObject())
    return NanThrowTypeError("Bad argument");

  std::string command(*String::Utf8Value(args[0]));
  std::vector<std::string> c_args;

  Handle<Array> v_args = Handle<Array>::Cast(args[1]);
  uint32_t length = v_args->Length();

  c_args.reserve(length);
  for (uint32_t i = 0; i < length; ++i) {
    std::string arg(*String::Utf8Value(v_args->Get(i)));
    c_args.push_back(arg);
  }

  Handle<Object> v_options = args[2]->ToObject();
  int options = runas::OPTION_NONE;
  if (v_options->Get(NanNew<String>("hide"))->BooleanValue())
    options |= runas::OPTION_HIDE;
  if (v_options->Get(NanNew<String>("admin"))->BooleanValue())
    options |= runas::OPTION_ADMIN;

  std::string std_input;
  Handle<Value> v_stdin = v_options->Get(NanNew<String>("stdin"));
  if (!v_stdin->IsUndefined())
    std_input = *String::Utf8Value(v_stdin);

  std::string std_output;
  bool catch_output = v_options->Get(NanNew<String>("catchOutput"))->BooleanValue();

  int code = -1;
  runas::Runas(command, c_args, std_input, catch_output ? &std_output : NULL, options, &code);

  if (catch_output && code == 0) {
    Handle<Object> result = NanNew<Object>();
    result->Set(NanNew<String>("exitCode"), NanNew<Integer>(code));
    result->Set(NanNew<String>("stdout"), NanNew<String>(std_output.data(), std_output.size()));
    NanReturnValue(result);
  } else {
    NanReturnValue(NanNew<Integer>(code));
  }
}

void Init(Handle<Object> exports) {
  NODE_SET_METHOD(exports, "runas", Runas);
}

}  // namespace

NODE_MODULE(runas, Init)
{'fraction_non_alphanumeric': 0.18669075579644687, 'fraction_numerical': 0.010237880156579343, 'mean_word_length': 5.500978473581213, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7094643', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1205, 'n_words': 202}