|
13 | 13 | namespace node {
|
14 | 14 | namespace loader {
|
15 | 15 |
|
| 16 | +using node::contextify::ContextifyContext; |
16 | 17 | using node::url::URL;
|
17 | 18 | using node::url::URL_FLAGS_FAILED;
|
18 | 19 | using v8::Array;
|
@@ -77,54 +78,58 @@ ModuleWrap* ModuleWrap::GetFromModule(Environment* env,
|
77 | 78 |
|
78 | 79 | void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
|
79 | 80 | Environment* env = Environment::GetCurrent(args);
|
| 81 | + Isolate* isolate = env->isolate(); |
80 | 82 |
|
81 |
| - Isolate* isolate = args.GetIsolate(); |
| 83 | + CHECK(args.IsConstructCall()); |
| 84 | + Local<Object> that = args.This(); |
82 | 85 |
|
83 |
| - if (!args.IsConstructCall()) { |
84 |
| - env->ThrowError("constructor must be called using new"); |
85 |
| - return; |
86 |
| - } |
87 |
| - |
88 |
| - if (!args[0]->IsString()) { |
89 |
| - env->ThrowError("first argument is not a string"); |
90 |
| - return; |
91 |
| - } |
| 86 | + const int argc = args.Length(); |
| 87 | + CHECK_GE(argc, 2); |
92 | 88 |
|
| 89 | + CHECK(args[0]->IsString()); |
93 | 90 | Local<String> source_text = args[0].As<String>();
|
94 | 91 |
|
95 |
| - if (!args[1]->IsString()) { |
96 |
| - env->ThrowError("second argument is not a string"); |
97 |
| - return; |
98 |
| - } |
99 |
| - |
| 92 | + CHECK(args[1]->IsString()); |
100 | 93 | Local<String> url = args[1].As<String>();
|
101 | 94 |
|
102 |
| - Local<Object> that = args.This(); |
| 95 | + Local<Context> context; |
| 96 | + Local<Integer> line_offset; |
| 97 | + Local<Integer> column_offset; |
103 | 98 |
|
104 |
| - Environment::ShouldNotAbortOnUncaughtScope no_abort_scope(env); |
105 |
| - TryCatch try_catch(isolate); |
106 |
| - |
107 |
| - Local<Value> options = args[2]; |
108 |
| - MaybeLocal<Integer> line_offset = contextify::GetLineOffsetArg(env, options); |
109 |
| - MaybeLocal<Integer> column_offset = |
110 |
| - contextify::GetColumnOffsetArg(env, options); |
111 |
| - MaybeLocal<Context> maybe_context = contextify::GetContextArg(env, options); |
| 99 | + if (argc == 5) { |
| 100 | + // new ModuleWrap(source, url, context?, lineOffset, columnOffset) |
| 101 | + if (args[2]->IsUndefined()) { |
| 102 | + context = that->CreationContext(); |
| 103 | + } else { |
| 104 | + CHECK(args[2]->IsObject()); |
| 105 | + ContextifyContext* sandbox = |
| 106 | + ContextifyContext::ContextFromContextifiedSandbox( |
| 107 | + env, args[2].As<Object>()); |
| 108 | + CHECK_NE(sandbox, nullptr); |
| 109 | + context = sandbox->context(); |
| 110 | + } |
112 | 111 |
|
| 112 | + CHECK(args[3]->IsNumber()); |
| 113 | + line_offset = args[3].As<Integer>(); |
113 | 114 |
|
114 |
| - if (try_catch.HasCaught()) { |
115 |
| - no_abort_scope.Close(); |
116 |
| - try_catch.ReThrow(); |
117 |
| - return; |
| 115 | + CHECK(args[4]->IsNumber()); |
| 116 | + column_offset = args[4].As<Integer>(); |
| 117 | + } else { |
| 118 | + // new ModuleWrap(source, url) |
| 119 | + context = that->CreationContext(); |
| 120 | + line_offset = Integer::New(isolate, 0); |
| 121 | + column_offset = Integer::New(isolate, 0); |
118 | 122 | }
|
119 | 123 |
|
120 |
| - Local<Context> context = maybe_context.FromMaybe(that->CreationContext()); |
| 124 | + Environment::ShouldNotAbortOnUncaughtScope no_abort_scope(env); |
| 125 | + TryCatch try_catch(isolate); |
121 | 126 | Local<Module> module;
|
122 | 127 |
|
123 | 128 | // compile
|
124 | 129 | {
|
125 | 130 | ScriptOrigin origin(url,
|
126 |
| - line_offset.ToLocalChecked(), // line offset |
127 |
| - column_offset.ToLocalChecked(), // column offset |
| 131 | + line_offset, // line offset |
| 132 | + column_offset, // column offset |
128 | 133 | False(isolate), // is cross origin
|
129 | 134 | Local<Integer>(), // script id
|
130 | 135 | Local<Value>(), // source map URL
|
@@ -161,10 +166,9 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
|
161 | 166 | void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
|
162 | 167 | Environment* env = Environment::GetCurrent(args);
|
163 | 168 | Isolate* isolate = args.GetIsolate();
|
164 |
| - if (!args[0]->IsFunction()) { |
165 |
| - env->ThrowError("first argument is not a function"); |
166 |
| - return; |
167 |
| - } |
| 169 | + |
| 170 | + CHECK_EQ(args.Length(), 1); |
| 171 | + CHECK(args[0]->IsFunction()); |
168 | 172 |
|
169 | 173 | Local<Object> that = args.This();
|
170 | 174 |
|
@@ -239,27 +243,23 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
|
239 | 243 |
|
240 | 244 | void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
|
241 | 245 | Environment* env = Environment::GetCurrent(args);
|
242 |
| - Isolate* isolate = args.GetIsolate(); |
| 246 | + Isolate* isolate = env->isolate(); |
243 | 247 | ModuleWrap* obj;
|
244 | 248 | ASSIGN_OR_RETURN_UNWRAP(&obj, args.This());
|
245 | 249 | Local<Context> context = obj->context_.Get(isolate);
|
246 | 250 | Local<Module> module = obj->module_.Get(isolate);
|
247 | 251 |
|
248 |
| - Environment::ShouldNotAbortOnUncaughtScope no_abort_scope(env); |
249 |
| - TryCatch try_catch(isolate); |
250 |
| - Maybe<int64_t> maybe_timeout = |
251 |
| - contextify::GetTimeoutArg(env, args[0]); |
252 |
| - Maybe<bool> maybe_break_on_sigint = |
253 |
| - contextify::GetBreakOnSigintArg(env, args[0]); |
| 252 | + // module.evaluate(timeout, breakOnSigint) |
| 253 | + CHECK_EQ(args.Length(), 2); |
254 | 254 |
|
255 |
| - if (try_catch.HasCaught()) { |
256 |
| - no_abort_scope.Close(); |
257 |
| - try_catch.ReThrow(); |
258 |
| - return; |
259 |
| - } |
| 255 | + CHECK(args[0]->IsNumber()); |
| 256 | + int64_t timeout = args[0]->IntegerValue(env->context()).FromJust(); |
260 | 257 |
|
261 |
| - int64_t timeout = maybe_timeout.ToChecked(); |
262 |
| - bool break_on_sigint = maybe_break_on_sigint.ToChecked(); |
| 258 | + CHECK(args[1]->IsBoolean()); |
| 259 | + bool break_on_sigint = args[1]->IsTrue(); |
| 260 | + |
| 261 | + Environment::ShouldNotAbortOnUncaughtScope no_abort_scope(env); |
| 262 | + TryCatch try_catch(isolate); |
263 | 263 |
|
264 | 264 | bool timed_out = false;
|
265 | 265 | bool received_signal = false;
|
@@ -665,26 +665,14 @@ Maybe<URL> Resolve(Environment* env,
|
665 | 665 | void ModuleWrap::Resolve(const FunctionCallbackInfo<Value>& args) {
|
666 | 666 | Environment* env = Environment::GetCurrent(args);
|
667 | 667 |
|
668 |
| - if (args.IsConstructCall()) { |
669 |
| - env->ThrowError("resolve() must not be called as a constructor"); |
670 |
| - return; |
671 |
| - } |
672 |
| - if (args.Length() != 2) { |
673 |
| - env->ThrowError("resolve must have exactly 2 arguments (string, string)"); |
674 |
| - return; |
675 |
| - } |
| 668 | + // module.resolve(specifier, url) |
| 669 | + CHECK_EQ(args.Length(), 2); |
676 | 670 |
|
677 |
| - if (!args[0]->IsString()) { |
678 |
| - env->ThrowError("first argument is not a string"); |
679 |
| - return; |
680 |
| - } |
| 671 | + CHECK(args[0]->IsString()); |
681 | 672 | Utf8Value specifier_utf8(env->isolate(), args[0]);
|
682 | 673 | std::string specifier_std(*specifier_utf8, specifier_utf8.length());
|
683 | 674 |
|
684 |
| - if (!args[1]->IsString()) { |
685 |
| - env->ThrowError("second argument is not a string"); |
686 |
| - return; |
687 |
| - } |
| 675 | + CHECK(args[1]->IsString()); |
688 | 676 | Utf8Value url_utf8(env->isolate(), args[1]);
|
689 | 677 | URL url(*url_utf8, url_utf8.length());
|
690 | 678 |
|
@@ -748,11 +736,9 @@ void ModuleWrap::SetImportModuleDynamicallyCallback(
|
748 | 736 | Isolate* iso = args.GetIsolate();
|
749 | 737 | Environment* env = Environment::GetCurrent(args);
|
750 | 738 | HandleScope handle_scope(iso);
|
751 |
| - if (!args[0]->IsFunction()) { |
752 |
| - env->ThrowError("first argument is not a function"); |
753 |
| - return; |
754 |
| - } |
755 | 739 |
|
| 740 | + CHECK_EQ(args.Length(), 1); |
| 741 | + CHECK(args[0]->IsFunction()); |
756 | 742 | Local<Function> import_callback = args[0].As<Function>();
|
757 | 743 | env->set_host_import_module_dynamically_callback(import_callback);
|
758 | 744 |
|
@@ -781,11 +767,9 @@ void ModuleWrap::SetInitializeImportMetaObjectCallback(
|
781 | 767 | const FunctionCallbackInfo<Value>& args) {
|
782 | 768 | Environment* env = Environment::GetCurrent(args);
|
783 | 769 | Isolate* isolate = env->isolate();
|
784 |
| - if (!args[0]->IsFunction()) { |
785 |
| - env->ThrowError("first argument is not a function"); |
786 |
| - return; |
787 |
| - } |
788 | 770 |
|
| 771 | + CHECK_EQ(args.Length(), 1); |
| 772 | + CHECK(args[0]->IsFunction()); |
789 | 773 | Local<Function> import_meta_callback = args[0].As<Function>();
|
790 | 774 | env->set_host_initialize_import_meta_object_callback(import_meta_callback);
|
791 | 775 |
|
|
0 commit comments