-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
module: use compileFunction over Module.wrap #21573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
113d0b7
7cf60fb
c352906
3146e19
15db986
ed4af45
8008e59
687d82c
9a152a3
cb604f2
9fbbd27
09cc845
35e209e
9284715
ba89f2d
b35b5ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -286,6 +286,14 @@ void ContextifyContext::WeakCallback( | |||||||||
| delete context; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| void ContextifyContext::WeakCallbackCompileFn( | ||||||||||
| const WeakCallbackInfo<CompileFnEntry>& data) { | ||||||||||
| CompileFnEntry* entry = data.GetParameter(); | ||||||||||
| entry->env->id_to_function_map[entry->id].Reset(); | ||||||||||
|
||||||||||
| entry->env->id_to_function_map.erase(entry->id); | ||||||||||
| delete entry; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // static | ||||||||||
| ContextifyContext* ContextifyContext::ContextFromContextifiedSandbox( | ||||||||||
| Environment* env, | ||||||||||
|
|
@@ -1007,7 +1015,30 @@ void ContextifyContext::CompileFunction( | |||||||||
| data + cached_data_buf->ByteOffset(), cached_data_buf->ByteLength()); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| ScriptOrigin origin(filename, line_offset, column_offset, True(isolate)); | ||||||||||
| // Get the function id | ||||||||||
| uint32_t id = env->get_next_function_id(); | ||||||||||
|
|
||||||||||
| // Set host_defined_options | ||||||||||
| Local<PrimitiveArray> host_defined_options = | ||||||||||
| PrimitiveArray::New(isolate, loader::HostDefinedOptions::kLength); | ||||||||||
| host_defined_options->Set( | ||||||||||
| isolate, | ||||||||||
| loader::HostDefinedOptions::kType, | ||||||||||
| Number::New(isolate, loader::ScriptType::kFunction)); | ||||||||||
| host_defined_options->Set( | ||||||||||
| isolate, loader::HostDefinedOptions::kID, Number::New(isolate, id)); | ||||||||||
|
|
||||||||||
guybedford marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| ScriptOrigin origin(filename, | ||||||||||
| line_offset, // line offset | ||||||||||
| column_offset, // column offset | ||||||||||
| True(isolate), // is cross origin | ||||||||||
| Local<Integer>(), // script id | ||||||||||
| Local<Value>(), // source map URL | ||||||||||
| False(isolate), // is opaque (?) | ||||||||||
| False(isolate), // is WASM | ||||||||||
| False(isolate), // is ES Module | ||||||||||
| host_defined_options); | ||||||||||
|
|
||||||||||
| ScriptCompiler::Source source(code, origin, cached_data); | ||||||||||
| ScriptCompiler::CompileOptions options; | ||||||||||
| if (source.GetCachedData() == nullptr) { | ||||||||||
|
|
@@ -1041,38 +1072,43 @@ void ContextifyContext::CompileFunction( | |||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| MaybeLocal<Function> maybe_fun = ScriptCompiler::CompileFunctionInContext( | ||||||||||
|
||||||||||
| MaybeLocal<Function> maybe_fn = ScriptCompiler::CompileFunctionInContext( | ||||||||||
| parsing_context, &source, params.size(), params.data(), | ||||||||||
| context_extensions.size(), context_extensions.data(), options); | ||||||||||
|
|
||||||||||
| Local<Function> fun; | ||||||||||
| if (maybe_fun.IsEmpty() || !maybe_fun.ToLocal(&fun)) { | ||||||||||
| if (maybe_fn.IsEmpty()) { | ||||||||||
| DecorateErrorStack(env, try_catch); | ||||||||||
| try_catch.ReThrow(); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| Local<Function> fn = maybe_fn.ToLocalChecked(); | ||||||||||
|
||||||||||
| env->id_to_function_map[id].Reset(isolate, fn); | ||||||||||
|
||||||||||
| env->id_to_function_map[id].Reset(isolate, fn); | |
| env->id_to_function_map.emplace(std::piecewise_construct, | |
| std::make_tuple(id), | |
| std::make_tuple(isolate, fn)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,12 +55,19 @@ class ContextifyContext { | |
| static ContextifyContext* Get(const v8::PropertyCallbackInfo<T>& args); | ||
|
|
||
| private: | ||
| struct CompileFnEntry { | ||
|
||
| Environment* env; | ||
| uint32_t id; | ||
| CompileFnEntry(Environment* env, uint32_t id): env(env), id(id) {} | ||
| }; | ||
| static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| static void CompileFunction( | ||
| const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| static void WeakCallback( | ||
| const v8::WeakCallbackInfo<ContextifyContext>& data); | ||
| static void WeakCallbackCompileFn( | ||
guybedford marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const v8::WeakCallbackInfo<CompileFnEntry>& data); | ||
| static void PropertyGetterCallback( | ||
| v8::Local<v8::Name> property, | ||
| const v8::PropertyCallbackInfo<v8::Value>& args); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| const assert = require('assert'); | ||
| const m = require('module'); | ||
|
|
||
| global.mwc = 0; | ||
| m.wrapper[0] += 'global.mwc = (global.mwc || 0 ) + 1;'; | ||
|
|
||
| require('./not-main-module.js'); | ||
| assert.strictEqual(mwc, 1); | ||
| delete global.mwc; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part would still be important in case
wraporwrapperget monkey patched since the line would then not start at an offset of zero. It won't be a big issue though as the assertion will just fall back to the messagefalse != true.