New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jsg: dynamic imports #88
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,7 +161,7 @@ class ModuleRegistry { | |
v8::Local<v8::Module> module, | ||
kj::Maybe<SyntheticModuleInfo> maybeSynthetic = nullptr); | ||
|
||
ModuleInfo(v8::Isolate* isolate, kj::StringPtr name, kj::StringPtr content); | ||
ModuleInfo(v8::Isolate* isolate, kj::StringPtr name, kj::ArrayPtr<const char> content); | ||
|
||
ModuleInfo(v8::Isolate* isolate, kj::StringPtr name, | ||
kj::Maybe<kj::ArrayPtr<kj::StringPtr>> maybeExports, | ||
|
@@ -171,9 +171,9 @@ class ModuleRegistry { | |
ModuleInfo& operator=(ModuleInfo&&) = default; | ||
}; | ||
|
||
virtual kj::Maybe<ModuleInfo&> resolve(const kj::Path& specifier) = 0; | ||
virtual kj::Maybe<ModuleInfo&> resolve(v8::Isolate* isolate, const kj::Path& specifier) = 0; | ||
|
||
virtual kj::Maybe<ModuleInfo&> resolve(v8::Local<v8::Module> module) = 0; | ||
virtual kj::Maybe<ModuleInfo&> resolve(v8::Isolate* isolate, v8::Local<v8::Module> module) = 0; | ||
|
||
virtual kj::Maybe<const kj::Path&> resolvePath(v8::Local<v8::Module> referrer)= 0; | ||
|
||
|
@@ -198,17 +198,26 @@ class ModuleRegistryImpl final: public ModuleRegistry { | |
entries.insert(Entry(specifier, kj::fwd<ModuleInfo>(info))); | ||
} | ||
|
||
kj::Maybe<ModuleInfo&> resolve(const kj::Path& specifier) override { | ||
void addBuiltinModule(const kj::Path& specifier, kj::ArrayPtr<const char> sourceCode) { | ||
// Register new module accessible by a given importPath. The module is instantiated | ||
// after first resolve attempt within application has failed, i.e. it is possible for | ||
// application to override the module. | ||
// sourceCode has to exist while this ModuleRegistry exists. | ||
// The expectation is for this method to be called during the assembly of worker global context. | ||
entries.insert(Entry(specifier, sourceCode)); | ||
} | ||
|
||
kj::Maybe<ModuleInfo&> resolve(v8::Isolate* isolate, const kj::Path& specifier) override { | ||
// TODO(soon): Soon we will support prefixed imports of Workers built in types. | ||
KJ_IF_MAYBE(entry, entries.find(specifier)) { | ||
return entry->info; | ||
return entry->module(isolate); | ||
} | ||
return nullptr; | ||
} | ||
|
||
kj::Maybe<ModuleInfo&> resolve(v8::Local<v8::Module> module) override { | ||
kj::Maybe<ModuleInfo&> resolve(v8::Isolate* isolate, v8::Local<v8::Module> module) override { | ||
KJ_IF_MAYBE(entry, entries.template find<1>(module)) { | ||
return entry->info; | ||
return entry->module(isolate); | ||
} | ||
return nullptr; | ||
} | ||
|
@@ -223,7 +232,7 @@ class ModuleRegistryImpl final: public ModuleRegistry { | |
size_t size() const { return entries.size(); } | ||
|
||
Promise<Value> resolveDynamicImport(v8::Isolate* isolate, kj::Path specifier) override { | ||
KJ_IF_MAYBE(info, resolve(specifier)) { | ||
KJ_IF_MAYBE(info, resolve(isolate, specifier)) { | ||
KJ_IF_MAYBE(func, dynamicImportHandler) { | ||
auto handler = [&info = *info, isolate]() -> Value { | ||
auto module = info.module.Get(isolate); | ||
|
@@ -252,12 +261,32 @@ class ModuleRegistryImpl final: public ModuleRegistry { | |
// object by identity. We use a kj::Table! | ||
struct Entry { | ||
kj::Path specifier; | ||
ModuleInfo info; | ||
kj::OneOf<ModuleInfo, kj::ArrayPtr<const char>> info; | ||
// Either instantiated module or module source code. | ||
|
||
Entry(kj::Path& specifier, ModuleInfo info) | ||
Entry(const kj::Path& specifier, ModuleInfo info) | ||
: specifier(specifier.clone()), info(kj::mv(info)) {} | ||
|
||
Entry(const kj::Path& specifier, kj::ArrayPtr<const char> src) | ||
: specifier(specifier.clone()), info(src) {} | ||
|
||
Entry(Entry&&) = default; | ||
Entry& operator=(Entry&&) = default; | ||
|
||
ModuleInfo& module(v8::Isolate* isolate) { | ||
// Lazily instantiate module from source code if needed | ||
|
||
KJ_SWITCH_ONEOF(info) { | ||
KJ_CASE_ONEOF(moduleInfo, ModuleInfo) { | ||
return moduleInfo; | ||
} | ||
KJ_CASE_ONEOF(src, kj::ArrayPtr<const char>) { | ||
info = ModuleInfo(isolate, specifier.toString(), src); | ||
return KJ_ASSERT_NONNULL(info.tryGet<ModuleInfo>()); | ||
} | ||
} | ||
KJ_UNREACHABLE; | ||
} | ||
}; | ||
|
||
struct SpecifierHashCallbacks { | ||
|
@@ -276,19 +305,28 @@ class ModuleRegistryImpl final: public ModuleRegistry { | |
const Entry& keyForRow(const Entry& row) const { return row; } | ||
|
||
bool matches(const Entry& entry, const Entry& other) const { | ||
return entry.info.hash == other.info.hash; | ||
return hashCode(entry) == hashCode(other); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it has problems with hash collisions. It looks like previously hash codes were based strictly on V8 identity hashes which might plausibly be unique (I'm not sure), but now it's based on string hashes in some cases which definitely could collide. This function needs to change to actually perform a proper comparison. |
||
} | ||
|
||
bool matches(const Entry& entry, v8::Local<v8::Module>& module) const { | ||
return entry.info.hash == module->GetIdentityHash(); | ||
return entry.info.template is<ModuleInfo>() && | ||
entry.info.template get<ModuleInfo>().hash == module->GetIdentityHash(); | ||
} | ||
|
||
uint hashCode(v8::Local<v8::Module>& module) const { | ||
return kj::hashCode(module->GetIdentityHash()); | ||
} | ||
|
||
uint hashCode(const Entry& entry) const { | ||
return entry.info.hash; | ||
KJ_SWITCH_ONEOF(entry.info) { | ||
KJ_CASE_ONEOF(moduleInfo, ModuleInfo) { | ||
return moduleInfo.hash; | ||
} | ||
KJ_CASE_ONEOF(src, kj::ArrayPtr<const char>) { | ||
return kj::hashCode(src); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this is hashing the whole source code, which sounds kind of expensive. Maybe we could key off a pointer to the code instead? This always points to a static constant, right? |
||
} | ||
} | ||
KJ_UNREACHABLE; | ||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's have
resolve
take ajsg::Lock& js
instead of the isolateThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately that would mean changing very long call chains, since they don't have lock wired through.
E.g.:
resolve <- evaluateSyntheticModuleCallback <- createSyntheticModule <- ModuleInfo with no lock in sight yet.
Let me know if you want to create locks before calling resolve or tackle passing jsg::Lock everywhere in modules.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do eventually want to update modules.h to use
jsg::Lock&
throughout so if you don't want to do this now that's fine. That said, it's simple to grab thejsg::Lock
for the current isolate without passing it through everywhere usingauto& js jsg::Lock::from(isolate)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make separate PR with this cleanup today if you don't mind.