Commit f7f21dea497 for nodejs
commit f7f21dea4975f665747e01b27ee2bc615cb22f68
Author: James M Snell <jasnell@gmail.com>
Date: Sun Nov 15 18:44:52 2026 +0000
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 <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67496
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
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<Value>& info,
+ Local<Context>* context) {
+ Local<Value> receiver_val = info.Holder();
+ if (!receiver_val->IsObject() ||
+ !receiver_val.As<Object>()->GetCreationContext().ToLocal(context)) {
+ THROW_ERR_INVALID_INVOCATION(info.GetIsolate());
+ return false;
+ }
+ return true;
+}
+
static void ImportMetaResolveLazyGetter(
Local<v8::Name> name, const PropertyCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
- Local<Value> receiver_val = info.Holder();
- if (!receiver_val->IsObject()) {
- THROW_ERR_INVALID_INVOCATION(isolate);
- return;
- }
- Local<Object> receiver = receiver_val.As<Object>();
Local<Context> 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<v8::Name> name,
const PropertyCallbackInfo<Value>& 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<Value> receiver_val = info.Holder();
- if (!receiver_val->IsObject()) {
- THROW_ERR_INVALID_INVOCATION(isolate);
- return;
- }
- Local<Object> receiver = receiver_val.As<Object>();
Local<Context> context;
- if (!receiver->GetCreationContext().ToLocal(&context)) {
- THROW_ERR_INVALID_INVOCATION(isolate);
+ if (!GetHolderCreationContext(info, &context)) {
return;
}
Environment* env = Environment::GetCurrent(context);