summaryrefslogtreecommitdiff
path: root/core/runtime.rs
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-02-07 20:22:46 +0100
committerGitHub <noreply@github.com>2023-02-07 20:22:46 +0100
commitb4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch)
tree3d008912affe8550692183bd2697a386db5e3c79 /core/runtime.rs
parent65500f36e870b4ada3996b06aa287e30177d21a3 (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/runtime.rs')
-rw-r--r--core/runtime.rs44
1 files changed, 36 insertions, 8 deletions
diff --git a/core/runtime.rs b/core/runtime.rs
index 1418e5791..096d26ca3 100644
--- a/core/runtime.rs
+++ b/core/runtime.rs
@@ -13,17 +13,18 @@ use crate::modules::ModuleId;
use crate::modules::ModuleLoadId;
use crate::modules::ModuleLoader;
use crate::modules::ModuleMap;
-use crate::modules::NoopModuleLoader;
use crate::op_void_async;
use crate::op_void_sync;
use crate::ops::*;
use crate::source_map::SourceMapCache;
use crate::source_map::SourceMapGetter;
use crate::Extension;
+use crate::NoopModuleLoader;
use crate::OpMiddlewareFn;
use crate::OpResult;
use crate::OpState;
use crate::PromiseId;
+use anyhow::Context as AnyhowContext;
use anyhow::Error;
use futures::channel::oneshot;
use futures::future::poll_fn;
@@ -605,9 +606,16 @@ impl JsRuntime {
None
};
- let loader = options
- .module_loader
- .unwrap_or_else(|| Rc::new(NoopModuleLoader));
+ let loader = if snapshot_options != SnapshotOptions::Load {
+ Rc::new(crate::modules::InternalModuleLoader::new(
+ options.module_loader,
+ ))
+ } else {
+ options
+ .module_loader
+ .unwrap_or_else(|| Rc::new(NoopModuleLoader))
+ };
+
{
let mut state = state_rc.borrow_mut();
state.global_realm = Some(JsRealm(global_context.clone()));
@@ -805,10 +813,30 @@ impl JsRuntime {
// Take extensions to avoid double-borrow
let extensions = std::mem::take(&mut self.extensions_with_js);
for ext in &extensions {
- let js_files = ext.init_js();
- for (filename, source) in js_files {
- // TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap
- realm.execute_script(self.v8_isolate(), filename, source)?;
+ {
+ let js_files = ext.get_esm_sources();
+ for (filename, source) in js_files {
+ futures::executor::block_on(async {
+ let id = self
+ .load_side_module(
+ &ModuleSpecifier::parse(filename)?,
+ Some(source.to_string()),
+ )
+ .await?;
+ let receiver = self.mod_evaluate(id);
+ self.run_event_loop(false).await?;
+ receiver.await?
+ })
+ .with_context(|| format!("Couldn't execute '{filename}'"))?;
+ }
+ }
+
+ {
+ let js_files = ext.get_js_sources();
+ for (filename, source) in js_files {
+ // TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap
+ realm.execute_script(self.v8_isolate(), filename, source)?;
+ }
}
}
// Restore extensions