|
9 | 9 | #include "async_wrap-inl.h"
|
10 | 10 |
|
11 | 11 | #include <string>
|
| 12 | +#include <vector> |
12 | 13 |
|
| 14 | +using node::options_parser::kDisallowedInEnvironment; |
13 | 15 | using v8::ArrayBuffer;
|
14 | 16 | using v8::Context;
|
15 | 17 | using v8::Function;
|
@@ -67,7 +69,10 @@ void WaitForWorkerInspectorToStop(Environment* child) {}
|
67 | 69 |
|
68 | 70 | } // anonymous namespace
|
69 | 71 |
|
70 |
| -Worker::Worker(Environment* env, Local<Object> wrap, const std::string& url) |
| 72 | +Worker::Worker(Environment* env, |
| 73 | + Local<Object> wrap, |
| 74 | + const std::string& url, |
| 75 | + std::shared_ptr<PerIsolateOptions> per_isolate_opts) |
71 | 76 | : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER), url_(url) {
|
72 | 77 | // Generate a new thread id.
|
73 | 78 | {
|
@@ -112,6 +117,9 @@ Worker::Worker(Environment* env, Local<Object> wrap, const std::string& url)
|
112 | 117 | &loop_,
|
113 | 118 | env->isolate_data()->platform(),
|
114 | 119 | array_buffer_allocator_.get()));
|
| 120 | + if (per_isolate_opts != nullptr) { |
| 121 | + isolate_data_->set_options(per_isolate_opts); |
| 122 | + } |
115 | 123 | CHECK(isolate_data_);
|
116 | 124 |
|
117 | 125 | Local<Context> context = NewContext(isolate_);
|
@@ -390,14 +398,67 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
|
390 | 398 | }
|
391 | 399 |
|
392 | 400 | std::string url;
|
| 401 | + std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr; |
| 402 | + |
393 | 403 | // Argument might be a string or URL
|
394 |
| - if (args.Length() == 1 && !args[0]->IsNullOrUndefined()) { |
| 404 | + if (args.Length() > 0 && !args[0]->IsNullOrUndefined()) { |
395 | 405 | Utf8Value value(
|
396 | 406 | args.GetIsolate(),
|
397 | 407 | args[0]->ToString(env->context()).FromMaybe(v8::Local<v8::String>()));
|
398 | 408 | url.append(value.out(), value.length());
|
| 409 | + |
| 410 | + if (args.Length() > 1 && args[1]->IsArray()) { |
| 411 | + v8::Local<v8::Array> array = args[1].As<v8::Array>(); |
| 412 | + // The first argument is reserved for program name, but we don't need it |
| 413 | + // in workers. |
| 414 | + std::vector<std::string> exec_argv = {""}; |
| 415 | + uint32_t length = array->Length(); |
| 416 | + for (uint32_t i = 0; i < length; i++) { |
| 417 | + v8::Local<v8::Value> arg; |
| 418 | + if (!array->Get(env->context(), i).ToLocal(&arg)) { |
| 419 | + return; |
| 420 | + } |
| 421 | + v8::MaybeLocal<v8::String> arg_v8_string = |
| 422 | + arg->ToString(env->context()); |
| 423 | + if (arg_v8_string.IsEmpty()) { |
| 424 | + return; |
| 425 | + } |
| 426 | + Utf8Value arg_utf8_value( |
| 427 | + args.GetIsolate(), |
| 428 | + arg_v8_string.FromMaybe(v8::Local<v8::String>())); |
| 429 | + std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length()); |
| 430 | + exec_argv.push_back(arg_string); |
| 431 | + } |
| 432 | + |
| 433 | + std::vector<std::string> invalid_args{}; |
| 434 | + std::vector<std::string> errors{}; |
| 435 | + per_isolate_opts.reset(new PerIsolateOptions()); |
| 436 | + |
| 437 | + // Using invalid_args as the v8_args argument as it stores unknown |
| 438 | + // options for the per isolate parser. |
| 439 | + options_parser::PerIsolateOptionsParser::instance.Parse( |
| 440 | + &exec_argv, |
| 441 | + nullptr, |
| 442 | + &invalid_args, |
| 443 | + per_isolate_opts.get(), |
| 444 | + kDisallowedInEnvironment, |
| 445 | + &errors); |
| 446 | + |
| 447 | + // The first argument is program name. |
| 448 | + invalid_args.erase(invalid_args.begin()); |
| 449 | + if (errors.size() > 0 || invalid_args.size() > 0) { |
| 450 | + v8::Local<v8::Value> value = |
| 451 | + ToV8Value(env->context(), |
| 452 | + errors.size() > 0 ? errors : invalid_args) |
| 453 | + .ToLocalChecked(); |
| 454 | + Local<String> key = |
| 455 | + FIXED_ONE_BYTE_STRING(env->isolate(), "invalidExecArgv"); |
| 456 | + args.This()->Set(env->context(), key, value).FromJust(); |
| 457 | + return; |
| 458 | + } |
| 459 | + } |
399 | 460 | }
|
400 |
| - new Worker(env, args.This(), url); |
| 461 | + new Worker(env, args.This(), url, per_isolate_opts); |
401 | 462 | }
|
402 | 463 |
|
403 | 464 | void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
|
|
0 commit comments