diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2023-02-07 20:22:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-07 20:22:46 +0100 |
commit | b4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch) | |
tree | 3d008912affe8550692183bd2697a386db5e3c79 /core/extensions.rs | |
parent | 65500f36e870b4ada3996b06aa287e30177d21a3 (diff) |
refactor: Use ES modules for internal runtime code (#17648)
This PR refactors all internal js files (except core) to be written as
ES modules.
`__bootstrap`has been mostly replaced with static imports in form in
`internal:[path to file from repo root]`.
To specify if files are ESM, an `esm` method has been added to
`Extension`, similar to the `js` method.
A new ModuleLoader called `InternalModuleLoader` has been added to
enable the loading of internal specifiers, which is used in all
situations except when a snapshot is only loaded, and not a new one is
created from it.
---------
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'core/extensions.rs')
-rw-r--r-- | core/extensions.rs | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/core/extensions.rs b/core/extensions.rs index 129e7b62a..b981e6da2 100644 --- a/core/extensions.rs +++ b/core/extensions.rs @@ -38,6 +38,7 @@ impl OpDecl { #[derive(Default)] pub struct Extension { js_files: Option<Vec<SourcePair>>, + esm_files: Option<Vec<SourcePair>>, ops: Option<Vec<OpDecl>>, opstate_fn: Option<Box<OpStateFn>>, middleware_fn: Option<Box<OpMiddlewareFn>>, @@ -81,13 +82,20 @@ impl Extension { /// returns JS source code to be loaded into the isolate (either at snapshotting, /// or at startup). as a vector of a tuple of the file name, and the source code. - pub fn init_js(&self) -> &[SourcePair] { + pub fn get_js_sources(&self) -> &[SourcePair] { match &self.js_files { Some(files) => files, None => &[], } } + pub fn get_esm_sources(&self) -> &[SourcePair] { + match &self.esm_files { + Some(files) => files, + None => &[], + } + } + /// Called at JsRuntime startup to initialize ops in the isolate. pub fn init_ops(&mut self) -> Option<Vec<OpDecl>> { // TODO(@AaronO): maybe make op registration idempotent @@ -145,6 +153,7 @@ impl Extension { #[derive(Default)] pub struct ExtensionBuilder { js: Vec<SourcePair>, + esm: Vec<SourcePair>, ops: Vec<OpDecl>, state: Option<Box<OpStateFn>>, middleware: Option<Box<OpMiddlewareFn>>, @@ -164,6 +173,11 @@ impl ExtensionBuilder { self } + pub fn esm(&mut self, js_files: Vec<SourcePair>) -> &mut Self { + self.esm.extend(js_files); + self + } + pub fn ops(&mut self, ops: Vec<OpDecl>) -> &mut Self { self.ops.extend(ops); self @@ -195,10 +209,12 @@ impl ExtensionBuilder { pub fn build(&mut self) -> Extension { let js_files = Some(std::mem::take(&mut self.js)); + let esm_files = Some(std::mem::take(&mut self.esm)); let ops = Some(std::mem::take(&mut self.ops)); let deps = Some(std::mem::take(&mut self.deps)); Extension { js_files, + esm_files, ops, opstate_fn: self.state.take(), middleware_fn: self.middleware.take(), |