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