Skip to content

Commit

Permalink
src: make sure pass the argv to worker threads
Browse files Browse the repository at this point in the history
PR-URL: #52827
Fixes: #52825
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
theanarkh authored and targos committed Sep 21, 2024
1 parent e2ea7b2 commit 21776a3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -604,26 +604,30 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
}
}
#endif // NODE_WITHOUT_NODE_OPTIONS
}

if (args[2]->IsArray()) {
Local<Array> array = args[2].As<Array>();
// The first argument is reserved for program name, but we don't need it
// in workers.
std::vector<std::string> exec_argv = {""};
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; i++) {
Local<Value> arg;
if (!array->Get(env->context(), i).ToLocal(&arg)) {
return;
}
Local<String> arg_v8;
if (!arg->ToString(env->context()).ToLocal(&arg_v8)) {
return;
if (args[2]->IsArray()) {
Local<Array> array = args[2].As<Array>();
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; i++) {
Local<Value> arg;
if (!array->Get(env->context(), i).ToLocal(&arg)) {
return;
}
Local<String> arg_v8;
if (!arg->ToString(env->context()).ToLocal(&arg_v8)) {
return;
}
Utf8Value arg_utf8_value(args.GetIsolate(), arg_v8);
std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length());
exec_argv.push_back(arg_string);
}
Utf8Value arg_utf8_value(args.GetIsolate(), arg_v8);
std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length());
exec_argv.push_back(arg_string);
} else {
exec_argv_out = env->exec_argv();
exec_argv.insert(
exec_argv.end(), exec_argv_out.begin(), exec_argv_out.end());
}

std::vector<std::string> invalid_args{};
Expand All @@ -641,9 +645,8 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
invalid_args.erase(invalid_args.begin());
if (errors.size() > 0 || invalid_args.size() > 0) {
Local<Value> error;
if (!ToV8Value(env->context(),
errors.size() > 0 ? errors : invalid_args)
.ToLocal(&error)) {
if (!ToV8Value(env->context(), errors.size() > 0 ? errors : invalid_args)
.ToLocal(&error)) {
return;
}
Local<String> key =
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-worker-cli-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Flags: --expose-internals
'use strict';
require('../common');
const { Worker } = require('worker_threads');

const CODE = `
// If the --expose-internals flag does not pass to worker
// require function will throw an error
require('internal/options');
`;
// Test if the flags is passed to worker threads
// See https://github.com/nodejs/node/issues/52825
new Worker(CODE, { eval: true });
new Worker(CODE, { eval: true, env: process.env, execArgv: ['--expose-internals'] });
new Worker(CODE, { eval: true, env: process.env });
new Worker(CODE, { eval: true, execArgv: ['--expose-internals'] });

0 comments on commit 21776a3

Please sign in to comment.