src: make sure pass the argv to worker threads

PR-URL: https://github.com/nodejs/node/pull/52827
Fixes: https://github.com/nodejs/node/issues/52825
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
theanarkh 2024-05-12 15:46:28 +08:00 committed by GitHub
parent c75675c726
commit aba4a00c8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 18 deletions

View File

@ -569,26 +569,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;
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);
}
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);
} 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{};
@ -606,9 +610,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 =

View File

@ -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'] });