From f7f21dea4975f665747e01b27ee2bc615cb22f68 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 15 Nov 2026 18:44:52 +0000 Subject: [PATCH] module: share holder creation context lookup in import.meta getters ImportMetaResolveLazyGetter() and PathHelpersLazyGetter() duplicated the code that retrieves the creation context from the holder object. Move it into a shared GetHolderCreationContext() helper. Assisted-by: Claude Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/67496 Reviewed-By: Vladimir Morozov Reviewed-By: Yagiz Nizipli --- src/module_wrap.cc | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) 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);