Skip to content

Refactor Worker::Api module loading#4082

Merged
kentonv merged 7 commits intomainfrom
kenton/refactor-modules
May 9, 2025
Merged

Refactor Worker::Api module loading#4082
kentonv merged 7 commits intomainfrom
kenton/refactor-modules

Conversation

@kentonv
Copy link
Member

@kentonv kentonv commented May 4, 2025

My goal is to be able to construct a Worker::Script from code that does not originate from a workerd config file.

To that end, I've defined an intermediate representation of workerd modules as a OneOf of a bunch of structs (see Worker::Script::Module). Worker::Script::ModulesSoruce now returns this representation, instead of returning a callback used to compile the modules later.

Wroker::Api now has a virtual compileModules() method which replaces the old lambda callback, taking the intermediate representation as input.

Note: I am a little confused about how the "new module registry" stuff works, since it appears that the code is passed directly to it, bypassing the Worker::Script constructor. Is the code passed to the constructor not used anymore when using the new module registry? Well, with this change, the code in WorkerdApi::initializeBundleModuleRegistry() is no longer workerd-specific, so perhaps in a future change it could move somewhere else and become reusable in the internal codebase.

@kentonv kentonv requested a review from jasnell May 4, 2025 02:14
@kentonv kentonv requested review from a team as code owners May 4, 2025 02:14
@kentonv kentonv force-pushed the kenton/refactor-modules branch 2 times, most recently from 2ae43f0 to 47b563c Compare May 9, 2025 17:15
@jasnell
Copy link
Collaborator

jasnell commented May 9, 2025

... I am a little confused about how the "new module registry" stuff works, since it appears that the code is passed directly to it, ...

Yeah, the new module registry set up works quite a bit differently. Really happy to see these cleanups as I think it'll simplify a number of things. Should be able to do some review here this afternoon

Copy link
Collaborator

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

Overall change LGTM. I'll look at the new module registry details to see if any of that needs to be updated or can be updated to benefit here.

kentonv added 7 commits May 9, 2025 15:03
Previously, they were passed to `WorkerdApi::extractSource()` (and from there to `compileModules()`).

This is a step towards making `compileModules()` a virtual method on `Worker::Api`.
Honestly this `bool isPython;` member variable declaration hidden between two type declarations in the public area looked like a mistake, but when I deleted it I discovered it was actually used! This is obviously a gross C++ style violation, so let's clean in up.
Instead, it contains a representation of the source code.

This refactoring is needed to support dynamic code loading, which won't necessarily originate from a workerd config.

This diff is large, but it's mostly just changing code which previosuly switched on config::Worker::Module::Reader (a capnp union) to instead switch on Worker::Script::Module (a OneOf). The details are straightforward.
It isn't needed in workerd, but it will be needed in the internal codebase to get the LimitEnforcer.
This isn't actually needed (at present) in workerd, but the internal codebase needs to implement the same interface, so will need to pass this in somehow.
These aren't currently supported in workerd, but again, the internal runtime needs these to be included in the input to compileModules().
@kentonv kentonv force-pushed the kenton/refactor-modules branch from 93f800b to f2858bc Compare May 9, 2025 20:04
@kentonv kentonv merged commit ea0e68e into main May 9, 2025
18 checks passed
@kentonv kentonv deleted the kenton/refactor-modules branch May 9, 2025 20:44
kentonv added a commit that referenced this pull request May 11, 2025
…Source.

This is a continuation of #4082. I had left `ScriptSource` still containing a weird callback because I didn't think I needed to update it. Turns out I do actually need to update it to avoid some ugliness later.

Luckily this turned out to be a much smaller refactor. I would have done it in the first place had I realized.
kentonv added a commit that referenced this pull request May 19, 2025
Similar to #4082, we want the representation of the worker definition passed to `makeWorker()` to not have to come from a workerd config file. This introduces an intermediate representation, `WorkerDef`, to represent this.

No logic has changed here, code has just moved around.
kentonv added a commit that referenced this pull request May 23, 2025
…Source.

This is a continuation of #4082. I had left `ScriptSource` still containing a weird callback because I didn't think I needed to update it. Turns out I do actually need to update it to avoid some ugliness later.

Luckily this turned out to be a much smaller refactor. I would have done it in the first place had I realized.
kentonv added a commit that referenced this pull request Jun 15, 2025
Similar to #4082, we want the representation of the worker definition passed to `makeWorker()` to not have to come from a workerd config file. This introduces an intermediate representation, `WorkerDef`, to represent this.

No logic has changed here, code has just moved around.
kentonv added a commit that referenced this pull request Jun 17, 2025
Similar to #4082, we want the representation of the worker definition passed to `makeWorker()` to not have to come from a workerd config file. This introduces an intermediate representation, `WorkerDef`, to represent this.

No logic has changed here, code has just moved around.
kentonv added a commit that referenced this pull request Jun 17, 2025
Similar to #4082, we want the representation of the worker definition passed to `makeWorker()` to not have to come from a workerd config file. This introduces an intermediate representation, `WorkerDef`, to represent this.

No logic has changed here, code has just moved around.
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.

2 participants