Skip to content

Commit 884fe88

Browse files
legendecastargos
authored andcommitted
vm: hint module identifier in instantiate errors
PR-URL: #60199 Fixes: #60157 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 05d7509 commit 884fe88

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

src/module_wrap.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ ModuleWrap::ModuleWrap(Realm* realm,
144144
Local<Object> context_object,
145145
Local<Value> synthetic_evaluation_step)
146146
: BaseObject(realm, object),
147+
url_(Utf8Value(realm->isolate(), url).ToString()),
147148
module_(realm->isolate(), module),
148149
module_hash_(module->GetIdentityHash()) {
149150
realm->env()->hash_to_module_map.emplace(module_hash_, this);
150151

151152
object->SetInternalField(kModuleSlot, module);
152-
object->SetInternalField(kURLSlot, url);
153153
object->SetInternalField(kModuleSourceObjectSlot,
154154
v8::Undefined(realm->isolate()));
155155
object->SetInternalField(kSyntheticEvaluationStepsSlot,
@@ -969,8 +969,7 @@ void ModuleWrap::GetModuleSourceObject(
969969
obj->object()->GetInternalField(kModuleSourceObjectSlot).As<Value>();
970970

971971
if (module_source_object->IsUndefined()) {
972-
Local<String> url = obj->object()->GetInternalField(kURLSlot).As<String>();
973-
THROW_ERR_SOURCE_PHASE_NOT_DEFINED(isolate, url);
972+
THROW_ERR_SOURCE_PHASE_NOT_DEFINED(isolate, obj->url_);
974973
return;
975974
}
976975

@@ -1044,10 +1043,8 @@ MaybeLocal<Object> ModuleWrap::ResolveSourceCallback(
10441043
->GetInternalField(ModuleWrap::kModuleSourceObjectSlot)
10451044
.As<Value>();
10461045
if (module_source_object->IsUndefined()) {
1047-
Local<String> url = resolved_module->object()
1048-
->GetInternalField(ModuleWrap::kURLSlot)
1049-
.As<String>();
1050-
THROW_ERR_SOURCE_PHASE_NOT_DEFINED(context->GetIsolate(), url);
1046+
THROW_ERR_SOURCE_PHASE_NOT_DEFINED(context->GetIsolate(),
1047+
resolved_module->url_);
10511048
return {};
10521049
}
10531050
CHECK(module_source_object->IsObject());
@@ -1079,17 +1076,21 @@ Maybe<ModuleWrap*> ModuleWrap::ResolveModule(
10791076
return Nothing<ModuleWrap*>();
10801077
}
10811078
if (!dependent->IsLinked()) {
1082-
THROW_ERR_VM_MODULE_LINK_FAILURE(
1083-
env,
1084-
"request for '%s' is from a module not been linked",
1085-
cache_key.specifier);
1079+
THROW_ERR_VM_MODULE_LINK_FAILURE(env,
1080+
"request for '%s' can not be resolved on "
1081+
"module '%s' that is not linked",
1082+
cache_key.specifier,
1083+
dependent->url_);
10861084
return Nothing<ModuleWrap*>();
10871085
}
10881086

10891087
auto it = dependent->resolve_cache_.find(cache_key);
10901088
if (it == dependent->resolve_cache_.end()) {
10911089
THROW_ERR_VM_MODULE_LINK_FAILURE(
1092-
env, "request for '%s' is not in cache", cache_key.specifier);
1090+
env,
1091+
"request for '%s' is not cached on module '%s'",
1092+
cache_key.specifier,
1093+
dependent->url_);
10931094
return Nothing<ModuleWrap*>();
10941095
}
10951096

src/module_wrap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ class ModuleWrap : public BaseObject {
9999
public:
100100
enum InternalFields {
101101
kModuleSlot = BaseObject::kInternalFieldCount,
102-
kURLSlot,
103102
kModuleSourceObjectSlot,
104103
kSyntheticEvaluationStepsSlot,
105104
kContextObjectSlot, // Object whose creation context is the target Context
@@ -215,6 +214,7 @@ class ModuleWrap : public BaseObject {
215214
v8::Local<v8::FixedArray> import_attributes,
216215
v8::Local<v8::Module> referrer);
217216

217+
std::string url_;
218218
v8::Global<v8::Module> module_;
219219
ResolveCache resolve_cache_;
220220
contextify::ContextifyContext* contextify_context_ = nullptr;

src/node_errors.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,11 @@ inline v8::Local<v8::Object> ERR_BUFFER_TOO_LARGE(v8::Isolate* isolate) {
295295
}
296296

297297
inline void THROW_ERR_SOURCE_PHASE_NOT_DEFINED(v8::Isolate* isolate,
298-
v8::Local<v8::String> url) {
299-
std::string message = std::string(*v8::String::Utf8Value(isolate, url));
298+
const std::string& url) {
300299
return THROW_ERR_SOURCE_PHASE_NOT_DEFINED(
301300
isolate,
302-
"Source phase import object is not defined for module %s",
303-
message.c_str());
301+
"Source phase import object is not defined for module '%s'",
302+
url);
304303
}
305304

306305
inline v8::Local<v8::Object> ERR_STRING_TOO_LONG(v8::Isolate* isolate) {

test/parallel/test-vm-module-linkmodulerequests.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,14 @@ test('mismatch linkage', () => {
6565
code: 'ERR_MODULE_LINK_MISMATCH',
6666
});
6767
});
68+
69+
test('instantiate error should hint about module identifier', () => {
70+
const foo = new SourceTextModule('import bar from "bar"', { identifier: 'file://foo' });
71+
const bar = new SourceTextModule('import "unknown"', { identifier: 'file://bar' });
72+
73+
foo.linkRequests([bar]);
74+
assert.throws(() => foo.instantiate(), {
75+
message: `request for 'unknown' can not be resolved on module 'file://bar' that is not linked`,
76+
code: 'ERR_VM_MODULE_LINK_FAILURE',
77+
});
78+
});

0 commit comments

Comments
 (0)