@@ -55,9 +55,13 @@ ModuleWrap::ModuleWrap(Environment* env,
55
55
Local<String> url)
56
56
: BaseObject(env, object),
57
57
module_ (env->isolate (), module),
58
- url_(env->isolate (), url),
59
58
id_(env->get_next_module_id ()) {
60
59
env->id_to_module_map .emplace (id_, this );
60
+
61
+ Local<Value> undefined = Undefined (env->isolate ());
62
+ object->SetInternalField (kURLSlot , url);
63
+ object->SetInternalField (kSyntheticEvaluationStepsSlot , undefined);
64
+ object->SetInternalField (kContextObjectSlot , undefined);
61
65
}
62
66
63
67
ModuleWrap::~ModuleWrap () {
@@ -73,6 +77,12 @@ ModuleWrap::~ModuleWrap() {
73
77
}
74
78
}
75
79
80
+ Local<Context> ModuleWrap::context () const {
81
+ Local<Value> obj = object ()->GetInternalField (kContextObjectSlot );
82
+ if (obj.IsEmpty ()) return {};
83
+ return obj.As <Object>()->CreationContext ();
84
+ }
85
+
76
86
ModuleWrap* ModuleWrap::GetFromModule (Environment* env,
77
87
Local<Module> module) {
78
88
auto range = env->hash_to_module_map .equal_range (module->GetIdentityHash ());
@@ -220,11 +230,14 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {
220
230
221
231
if (synthetic) {
222
232
obj->synthetic_ = true ;
223
- obj->synthetic_evaluation_steps_ .Reset (
224
- env->isolate (), args[3 ].As <Function>());
233
+ obj->object ()->SetInternalField (kSyntheticEvaluationStepsSlot , args[3 ]);
225
234
}
226
235
227
- obj->context_ .Reset (isolate, context);
236
+ // Use the extras object as an object whose CreationContext() will be the
237
+ // original `context`, since the `Context` itself strictly speaking cannot
238
+ // be stored in an internal field.
239
+ obj->object ()->SetInternalField (kContextObjectSlot ,
240
+ context->GetExtrasBindingObject ());
228
241
obj->contextify_context_ = contextify_context;
229
242
230
243
env->hash_to_module_map .emplace (module->GetIdentityHash (), obj);
@@ -254,7 +267,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
254
267
255
268
Local<Function> resolver_arg = args[0 ].As <Function>();
256
269
257
- Local<Context> mod_context = obj->context_ . Get (isolate );
270
+ Local<Context> mod_context = obj->context ( );
258
271
Local<Module> module = obj->module_ .Get (isolate);
259
272
260
273
const int module_requests_length = module->GetModuleRequestsLength ();
@@ -295,7 +308,7 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
295
308
Isolate* isolate = args.GetIsolate ();
296
309
ModuleWrap* obj;
297
310
ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
298
- Local<Context> context = obj->context_ . Get (isolate );
311
+ Local<Context> context = obj->context ( );
299
312
Local<Module> module = obj->module_ .Get (isolate);
300
313
TryCatchScope try_catch (env);
301
314
USE (module->InstantiateModule (context, ResolveCallback));
@@ -318,7 +331,7 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
318
331
Isolate* isolate = env->isolate ();
319
332
ModuleWrap* obj;
320
333
ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
321
- Local<Context> context = obj->context_ . Get (isolate );
334
+ Local<Context> context = obj->context ( );
322
335
Local<Module> module = obj->module_ .Get (isolate);
323
336
324
337
ContextifyContext* contextify_context = obj->contextify_context_ ;
@@ -636,8 +649,10 @@ MaybeLocal<Value> ModuleWrap::SyntheticModuleEvaluationStepsCallback(
636
649
637
650
TryCatchScope try_catch (env);
638
651
Local<Function> synthetic_evaluation_steps =
639
- obj->synthetic_evaluation_steps_ .Get (isolate);
640
- obj->synthetic_evaluation_steps_ .Reset ();
652
+ obj->object ()->GetInternalField (kSyntheticEvaluationStepsSlot )
653
+ .As <Function>();
654
+ obj->object ()->SetInternalField (
655
+ kSyntheticEvaluationStepsSlot , Undefined (isolate));
641
656
MaybeLocal<Value> ret = synthetic_evaluation_steps->Call (context,
642
657
obj->object (), 0 , nullptr );
643
658
if (ret.IsEmpty ()) {
0 commit comments