| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 347dd99 commit 8cf4170
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -428,7 +428,9 @@ if (isMainThread) { | |||
| 428 | 428 | not automatically be piped through to `process.stderr` in the parent. | |
| 429 | 429 | * `execArgv` {string[]} List of node CLI options passed to the worker. | |
| 430 | 430 | V8 options (such as `--max-old-space-size`) and options that affect the | |
| 431 | - process (such as `--title`) are not supported. | ||
| 431 | + process (such as `--title`) are not supported. If set, this will be provided | ||
| 432 | + as [`process.execArgv`][] inside the worker. By default, options will be | ||
| 433 | + inherited from the parent thread. | ||
| 432 | 434 | ||
| 433 | 435 | ### Event: 'error' | |
| 434 | 436 | <!-- YAML | |
@@ -582,6 +584,7 @@ active handle in the event system. If the worker is already `unref()`ed calling | |||
| 582 | 584 | [`process.abort()`]: process.html#process_process_abort | |
| 583 | 585 | [`process.chdir()`]: process.html#process_process_chdir_directory | |
| 584 | 586 | [`process.env`]: process.html#process_process_env | |
| 587 | + [`process.execArgv`]: process.html#process_process_execargv | ||
| 585 | 588 | [`process.exit()`]: process.html#process_process_exit_code | |
| 586 | 589 | [`process.stderr`]: process.html#process_process_stderr | |
| 587 | 590 | [`process.stdin`]: process.html#process_process_stdin | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -605,6 +605,10 @@ inline std::shared_ptr<EnvironmentOptions> Environment::options() { | |||
| 605 | 605 | return options_; | |
| 606 | 606 | } | |
| 607 | 607 | ||
| 608 | + inline const std::vector<std::string>& Environment::exec_argv() { | ||
| 609 | + return exec_argv_; | ||
| 610 | + } | ||
| 611 | + | ||
| 608 | 612 | inline std::shared_ptr<HostPort> Environment::inspector_host_port() { | |
| 609 | 613 | return inspector_host_port_; | |
| 610 | 614 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -386,6 +386,7 @@ MaybeLocal<Object> Environment::ProcessCliArgs( | |||
| 386 | 386 | std::move(traced_value)); | |
| 387 | 387 | } | |
| 388 | 388 | ||
| 389 | + exec_argv_ = exec_args; | ||
| 389 | 390 | Local<Object> process_object = | |
| 390 | 391 | node::CreateProcessObject(this, args, exec_args) | |
| 391 | 392 | .FromMaybe(Local<Object>()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -674,6 +674,7 @@ class Environment { | |||
| 674 | 674 | v8::MaybeLocal<v8::Object> ProcessCliArgs( | |
| 675 | 675 | const std::vector<std::string>& args, | |
| 676 | 676 | const std::vector<std::string>& exec_args); | |
| 677 | + inline const std::vector<std::string>& exec_argv(); | ||
| 677 | 678 | ||
| 678 | 679 | typedef void (*HandleCleanupCb)(Environment* env, | |
| 679 | 680 | uv_handle_t* handle, | |
@@ -1064,6 +1065,7 @@ class Environment { | |||
| 1064 | 1065 | // the inspector_host_port_->port() will be the actual port being | |
| 1065 | 1066 | // used. | |
| 1066 | 1067 | std::shared_ptr<HostPort> inspector_host_port_; | |
| 1068 | + std::vector<std::string> exec_argv_; | ||
| 1067 | 1069 | ||
| 1068 | 1070 | uint32_t module_id_counter_ = 0; | |
| 1069 | 1071 | uint32_t script_id_counter_ = 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -101,10 +101,12 @@ void AsyncRequest::MemoryInfo(MemoryTracker* tracker) const { | |||
| 101 | 101 | Worker::Worker(Environment* env, | |
| 102 | 102 | Local<Object> wrap, | |
| 103 | 103 | const std::string& url, | |
| 104 | - std::shared_ptr<PerIsolateOptions> per_isolate_opts) | ||
| 104 | + std::shared_ptr<PerIsolateOptions> per_isolate_opts, | ||
| 105 | + std::vector<std::string>&& exec_argv) | ||
| 105 | 106 | : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER), | |
| 106 | 107 | url_(url), | |
| 107 | 108 | per_isolate_opts_(per_isolate_opts), | |
| 109 | + exec_argv_(exec_argv), | ||
| 108 | 110 | platform_(env->isolate_data()->platform()), | |
| 109 | 111 | profiler_idle_notifier_started_(env->profiler_idle_notifier_started()), | |
| 110 | 112 | thread_id_(Environment::AllocateThreadId()) { | |
@@ -284,7 +286,7 @@ void Worker::Run() { | |||
| 284 | 286 | ||
| 285 | 287 | env_->Start(profiler_idle_notifier_started_); | |
| 286 | 288 | env_->ProcessCliArgs(std::vector<std::string>{}, | |
| 287 | - std::vector<std::string>{}); | ||
| 289 | + std::move(exec_argv_)); | ||
| 288 | 290 | } | |
| 289 | 291 | ||
| 290 | 292 | Debug(this, "Created Environment for worker with id %llu", thread_id_); | |
@@ -434,6 +436,9 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 434 | 436 | std::string url; | |
| 435 | 437 | std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr; | |
| 436 | 438 | ||
| 439 | + std::vector<std::string> exec_argv_out; | ||
| 440 | + bool has_explicit_exec_argv = false; | ||
| 441 | + | ||
| 437 | 442 | // Argument might be a string or URL | |
| 438 | 443 | if (args.Length() > 0 && !args[0]->IsNullOrUndefined()) { | |
| 439 | 444 | Utf8Value value( | |
@@ -445,6 +450,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 445 | 450 | v8::Local<v8::Array> array = args[1].As<v8::Array>(); | |
| 446 | 451 | // The first argument is reserved for program name, but we don't need it | |
| 447 | 452 | // in workers. | |
| 453 | + has_explicit_exec_argv = true; | ||
| 448 | 454 | std::vector<std::string> exec_argv = {""}; | |
| 449 | 455 | uint32_t length = array->Length(); | |
| 450 | 456 | for (uint32_t i = 0; i < length; i++) { | |
@@ -472,7 +478,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 472 | 478 | // options for the per isolate parser. | |
| 473 | 479 | options_parser::PerIsolateOptionsParser::instance.Parse( | |
| 474 | 480 | &exec_argv, | |
| 475 | - nullptr, | ||
| 481 | + &exec_argv_out, | ||
| 476 | 482 | &invalid_args, | |
| 477 | 483 | per_isolate_opts.get(), | |
| 478 | 484 | kDisallowedInEnvironment, | |
@@ -492,7 +498,9 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 492 | 498 | } | |
| 493 | 499 | } | |
| 494 | 500 | } | |
| 495 | - new Worker(env, args.This(), url, per_isolate_opts); | ||
| 501 | + if (!has_explicit_exec_argv) | ||
| 502 | + exec_argv_out = env->exec_argv(); | ||
| 503 | + new Worker(env, args.This(), url, per_isolate_opts, std::move(exec_argv_out)); | ||
| 496 | 504 | } | |
| 497 | 505 | ||
| 498 | 506 | void Worker::StartThread(const FunctionCallbackInfo<Value>& args) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,7 +38,8 @@ class Worker : public AsyncWrap { | |||
| 38 | 38 | Worker(Environment* env, | |
| 39 | 39 | v8::Local<v8::Object> wrap, | |
| 40 | 40 | const std::string& url, | |
| 41 | - std::shared_ptr<PerIsolateOptions> per_isolate_opts); | ||
| 41 | + std::shared_ptr<PerIsolateOptions> per_isolate_opts, | ||
| 42 | + std::vector<std::string>&& exec_argv); | ||
| 42 | 43 | ~Worker() override; | |
| 43 | 44 | ||
| 44 | 45 | // Run the worker. This is only called from the worker thread. | |
@@ -74,6 +75,7 @@ class Worker : public AsyncWrap { | |||
| 74 | 75 | const std::string url_; | |
| 75 | 76 | ||
| 76 | 77 | std::shared_ptr<PerIsolateOptions> per_isolate_opts_; | |
| 78 | + std::vector<std::string> exec_argv_; | ||
| 77 | 79 | MultiIsolatePlatform* platform_; | |
| 78 | 80 | v8::Isolate* isolate_ = nullptr; | |
| 79 | 81 | bool profiler_idle_notifier_started_; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,26 +20,46 @@ | |||
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | ||
| 22 | 22 | 'use strict'; | |
| 23 | - require('../common'); | ||
| 23 | + const common = require('../common'); | ||
| 24 | 24 | const assert = require('assert'); | |
| 25 | 25 | const spawn = require('child_process').spawn; | |
| 26 | + const { Worker, isMainThread } = require('worker_threads'); | ||
| 26 | 27 | ||
| 27 | - if (process.argv[2] === 'child') { | ||
| 28 | - process.stdout.write(JSON.stringify(process.execArgv)); | ||
| 28 | + if (process.argv[2] === 'child' || !isMainThread) { | ||
| 29 | + if (process.argv[3] === 'cp+worker') | ||
| 30 | + new Worker(__filename); | ||
| 31 | + else | ||
| 32 | + process.stdout.write(JSON.stringify(process.execArgv)); | ||
| 29 | 33 | } else { | |
| 30 | 34 | for (const extra of [ [], [ '--' ] ]) { | |
| 31 | - const execArgv = ['--stack-size=256']; | ||
| 32 | - const args = [__filename, 'child', 'arg0']; | ||
| 33 | - const child = spawn(process.execPath, [...execArgv, ...extra, ...args]); | ||
| 34 | - let out = ''; | ||
| 35 | + for (const kind of [ 'cp', 'worker', 'cp+worker' ]) { | ||
| 36 | + const execArgv = ['--pending-deprecation']; | ||
| 37 | + const args = [__filename, 'child', kind]; | ||
| 38 | + let child; | ||
| 39 | + switch (kind) { | ||
| 40 | + case 'cp': | ||
| 41 | + child = spawn(process.execPath, [...execArgv, ...extra, ...args]); | ||
| 42 | + break; | ||
| 43 | + case 'worker': | ||
| 44 | + child = new Worker(__filename, { | ||
| 45 | + execArgv: [...execArgv, ...extra], | ||
| 46 | + stdout: true | ||
| 47 | + }); | ||
| 48 | + break; | ||
| 49 | + case 'cp+worker': | ||
| 50 | + child = spawn(process.execPath, [...execArgv, ...args]); | ||
| 51 | + break; | ||
| 52 | + } | ||
| 35 | 53 | ||
| 36 | - child.stdout.setEncoding('utf8'); | ||
| 37 | - child.stdout.on('data', function(chunk) { | ||
| 38 | - out += chunk; | ||
| 39 | - }); | ||
| 54 | + let out = ''; | ||
| 55 | + child.stdout.setEncoding('utf8'); | ||
| 56 | + child.stdout.on('data', (chunk) => { | ||
| 57 | + out += chunk; | ||
| 58 | + }); | ||
| 40 | 59 | ||
| 41 | - child.on('close', function() { | ||
| 42 | - assert.deepStrictEqual(JSON.parse(out), execArgv); | ||
| 43 | - }); | ||
| 60 | + child.stdout.on('end', common.mustCall(() => { | ||
| 61 | + assert.deepStrictEqual(JSON.parse(out), execArgv); | ||
| 62 | + })); | ||
| 63 | + } | ||
| 44 | 64 | } | |
| 45 | 65 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,11 +5,13 @@ const assert = require('assert'); | |||
| 5 | 5 | // This test ensures that Workers have the ability to get | |
| 6 | 6 | // their own command line flags. | |
| 7 | 7 | ||
| 8 | - const { Worker, isMainThread } = require('worker_threads'); | ||
| 8 | + const { Worker } = require('worker_threads'); | ||
| 9 | 9 | const { StringDecoder } = require('string_decoder'); | |
| 10 | 10 | const decoder = new StringDecoder('utf8'); | |
| 11 | 11 | ||
| 12 | - if (isMainThread) { | ||
| 12 | + // Do not use isMainThread so that this test itself can be run inside a Worker. | ||
| 13 | + if (!process.env.HAS_STARTED_WORKER) { | ||
| 14 | + process.env.HAS_STARTED_WORKER = 1; | ||
| 13 | 15 | const w = new Worker(__filename, { execArgv: ['--trace-warnings'] }); | |
| 14 | 16 | w.stderr.on('data', common.mustCall((chunk) => { | |
| 15 | 17 | const error = decoder.write(chunk); | |
@@ -19,4 +21,5 @@ if (isMainThread) { | |||
| 19 | 21 | })); | |
| 20 | 22 | } else { | |
| 21 | 23 | process.emitWarning('some warning'); | |
| 24 | + assert.deepStrictEqual(process.execArgv, ['--trace-warnings']); | ||
| 22 | 25 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments