diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 8a122aeb65f..66500b7c6ae 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -1244,18 +1244,26 @@ void ModuleWrap::SetImportMetaResolveInitializer( realm->set_host_import_meta_resolve_initializer(initializer); } +// The import.meta lazy getters have no JavaScript function representation and +// are not invoked in the creation context. When they are invoked in a vm +// context, `Realm::GetCurrent(info)` returns nullptr, so retrieve the creation +// context via the holder object instead. Throws and returns false on failure. +static bool GetHolderCreationContext(const PropertyCallbackInfo& info, + Local* context) { + Local receiver_val = info.Holder(); + if (!receiver_val->IsObject() || + !receiver_val.As()->GetCreationContext().ToLocal(context)) { + THROW_ERR_INVALID_INVOCATION(info.GetIsolate()); + return false; + } + return true; +} + static void ImportMetaResolveLazyGetter( Local name, const PropertyCallbackInfo& info) { Isolate* isolate = info.GetIsolate(); - Local receiver_val = info.Holder(); - if (!receiver_val->IsObject()) { - THROW_ERR_INVALID_INVOCATION(isolate); - return; - } - Local receiver = receiver_val.As(); Local context; - if (!receiver->GetCreationContext().ToLocal(&context)) { - THROW_ERR_INVALID_INVOCATION(isolate); + if (!GetHolderCreationContext(info, &context)) { return; } Realm* realm = Realm::GetCurrent(context); @@ -1284,20 +1292,8 @@ static void ImportMetaResolveLazyGetter( static void PathHelpersLazyGetter(Local name, const PropertyCallbackInfo& info) { Isolate* isolate = info.GetIsolate(); - // This getter has no JavaScript function representation and is not - // invoked in the creation context. - // When this getter is invoked in a vm context, the `Realm::GetCurrent(info)` - // returns a nullptr and retrieve the creation context via `this` object and - // get the creation Realm. - Local receiver_val = info.Holder(); - if (!receiver_val->IsObject()) { - THROW_ERR_INVALID_INVOCATION(isolate); - return; - } - Local receiver = receiver_val.As(); Local context; - if (!receiver->GetCreationContext().ToLocal(&context)) { - THROW_ERR_INVALID_INVOCATION(isolate); + if (!GetHolderCreationContext(info, &context)) { return; } Environment* env = Environment::GetCurrent(context);