Skip to content
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

Merged
merged 2 commits into from Oct 11, 2022
Merged

jsg: dynamic imports #88

merged 2 commits into from Oct 11, 2022

Conversation

mikea
Copy link
Collaborator

@mikea mikea commented Oct 10, 2022

Modules registered with addOnDemand method will be parsed and instantiated when all other imports failed.

Modules registered with addOnDemand method will be parsed and
instantiated when all other imports failed.
@mikea
Copy link
Collaborator Author

mikea commented Oct 10, 2022

@jasnell @harrishancock please take a look.

@@ -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;
Copy link
Member

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 a jsg::Lock& js instead of the isolate

Copy link
Collaborator Author

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

Copy link
Member

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 the jsg::Lock for the current isolate without passing it through everywhere using auto& js jsg::Lock::from(isolate).

Copy link
Collaborator Author

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.

return moduleInfo;
}
KJ_CASE_ONEOF(src, kj::ArrayPtr<const char>) {
info = ModuleInfo(isolate, specifier.basename()[0], src);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious because it's not obvious...Why is this taking only the basename of the specifier and not the whole specifier?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(to be clear, just saying that a comment here would be good :-) ...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious because it's not obvious...Why is this taking only the basename of the specifier and not the whole specifier?

I actually don't know what's right: should module name contain full path or only basename? E.g. for import "node/path" what should the name be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To import node core modules it should be require('node:... ') where ... is the name of the core module. E.g. require('node:assert')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this name is relate to how you import. This name goes to ScriptOrigin.

I don't know if it should be the same or not.

PS nit: only ESM import will be supported, not require.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldn't require be supported? This should work for that also

Copy link
Collaborator Author

@mikea mikea Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldn't require be supported?

Because then we'll have to compile two versions of the module? One for ESM and one for commonjs?

Or can ESM modules be imported safely using require too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way we handle imports and require it should just work. Worth testing though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(to be clear, it might not just work also)

@@ -198,17 +198,25 @@ class ModuleRegistryImpl final: public ModuleRegistry {
entries.insert(Entry(specifier, kj::fwd<ModuleInfo>(info)));
}

kj::Maybe<ModuleInfo&> resolve(const kj::Path& specifier) override {
void addOnDemand(const kj::Path& specifier, kj::ArrayPtr<const char> sourceCode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addOnDemand(...) seems like an odd name for this. I would expect something more descriptive like addBuiltinModule(...)

Also, where and when would this be called? And by whom? I know this is part of a large piece of work but expanding the comment here to provide some additional detail would help me as a reviewer evaluate the correctness of this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created a demo in EW PR 4479. Will rename the function shortly.

@mikea
Copy link
Collaborator Author

mikea commented Oct 11, 2022

Method renamed, comments added. A change to replace Isolate with Lock in modules is underway. PTAL @jasnell

@mikea mikea requested a review from jasnell October 11, 2022 15:47
@mikea
Copy link
Collaborator Author

mikea commented Oct 11, 2022

Method renamed, comments added. A change to replace Isolate with Lock in modules is underway. PTAL @jasnell

I think I messed up my various git clients. Hold on please while I fix this.

@mikea
Copy link
Collaborator Author

mikea commented Oct 11, 2022

pushed the update. To recap:

  • renamed to addBuiltinModule
  • added comments
  • removed basename. Using specifier.toString() as module name.
  • lock change is incoming. Will update it after this one lands.

ptal. @jasnell

@mikea mikea merged commit 395ce36 into main Oct 11, 2022
@mikea mikea deleted the mikea/dynamic-imports branch October 11, 2022 16:54
@@ -276,19 +301,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);
Copy link
Member

Choose a reason for hiding this comment

The 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.

return moduleInfo.hash;
}
KJ_CASE_ONEOF(src, kj::ArrayPtr<const char>) {
return kj::hashCode(src);
Copy link
Member

Choose a reason for hiding this comment

The 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants