Skip to content

Commit 43d6b6b

Browse files
committed
vm: hint module identifier in instantiate errors
1 parent 75a6fff commit 43d6b6b

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,
@@ -968,8 +968,7 @@ void ModuleWrap::GetModuleSourceObject(
968968
obj->object()->GetInternalField(kModuleSourceObjectSlot).As<Value>();
969969

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

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

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

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)