summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/extensions.rs16
-rw-r--r--core/ops_builtin.rs59
-rw-r--r--core/runtime.rs107
-rw-r--r--core/snapshot_util.rs2
4 files changed, 78 insertions, 106 deletions
diff --git a/core/extensions.rs b/core/extensions.rs
index 3d17db592..2c6e1669c 100644
--- a/core/extensions.rs
+++ b/core/extensions.rs
@@ -104,7 +104,7 @@ impl Extension {
/// Check if dependencies have been loaded, and errors if either:
/// - The extension is depending on itself or an extension with the same name.
/// - A dependency hasn't been loaded yet.
- pub fn check_dependencies(&self, previous_exts: &[&mut Extension]) {
+ pub fn check_dependencies(&self, previous_exts: &[Extension]) {
if let Some(deps) = self.deps {
'dep_loop: for dep in deps {
if dep == &self.name {
@@ -124,18 +124,12 @@ 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 get_js_sources(&self) -> &[ExtensionFileSource] {
- match &self.js_files {
- Some(files) => files,
- None => &[],
- }
+ pub fn get_js_sources(&self) -> Option<&Vec<ExtensionFileSource>> {
+ self.js_files.as_ref()
}
- pub fn get_esm_sources(&self) -> &[ExtensionFileSource] {
- match &self.esm_files {
- Some(files) => files,
- None => &[],
- }
+ pub fn get_esm_sources(&self) -> Option<&Vec<ExtensionFileSource>> {
+ self.esm_files.as_ref()
}
pub fn get_esm_entry_point(&self) -> Option<&'static str> {
diff --git a/core/ops_builtin.rs b/core/ops_builtin.rs
index d225b8624..7b66ffaa4 100644
--- a/core/ops_builtin.rs
+++ b/core/ops_builtin.rs
@@ -1,3 +1,4 @@
+use crate::ExtensionBuilder;
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::error::format_file_name;
use crate::error::type_error;
@@ -18,38 +19,50 @@ use std::io::stdout;
use std::io::Write;
use std::rc::Rc;
-pub(crate) fn init_builtins() -> Extension {
+fn ext() -> ExtensionBuilder {
Extension::builder("core")
+}
+
+fn ops(ext: &mut ExtensionBuilder) -> &mut ExtensionBuilder {
+ let mut ops = vec![
+ op_close::decl(),
+ op_try_close::decl(),
+ op_print::decl(),
+ op_resources::decl(),
+ op_wasm_streaming_feed::decl(),
+ op_wasm_streaming_set_url::decl(),
+ op_void_sync::decl(),
+ op_void_async::decl(),
+ op_add::decl(),
+ // // TODO(@AaronO): track IO metrics for builtin streams
+ op_read::decl(),
+ op_read_all::decl(),
+ op_write::decl(),
+ op_write_all::decl(),
+ op_shutdown::decl(),
+ op_metrics::decl(),
+ op_format_file_name::decl(),
+ op_is_proxy::decl(),
+ op_str_byte_length::decl(),
+ ];
+ ops.extend(crate::ops_builtin_v8::init_builtins_v8());
+ ext.ops(ops)
+}
+
+pub(crate) fn init_builtin_ops_and_esm() -> Extension {
+ ops(&mut ext())
.js(include_js_files!(
"00_primordials.js",
"01_core.js",
"02_error.js",
))
- .ops(vec![
- op_close::decl(),
- op_try_close::decl(),
- op_print::decl(),
- op_resources::decl(),
- op_wasm_streaming_feed::decl(),
- op_wasm_streaming_set_url::decl(),
- op_void_sync::decl(),
- op_void_async::decl(),
- op_add::decl(),
- // // TODO(@AaronO): track IO metrics for builtin streams
- op_read::decl(),
- op_read_all::decl(),
- op_write::decl(),
- op_write_all::decl(),
- op_shutdown::decl(),
- op_metrics::decl(),
- op_format_file_name::decl(),
- op_is_proxy::decl(),
- op_str_byte_length::decl(),
- ])
- .ops(crate::ops_builtin_v8::init_builtins_v8())
.build()
}
+pub(crate) fn init_builtin_ops() -> Extension {
+ ops(&mut ext()).build()
+}
+
/// Return map of resources with id as key
/// and string representation as value.
#[op]
diff --git a/core/runtime.rs b/core/runtime.rs
index b9da06346..75bdb4519 100644
--- a/core/runtime.rs
+++ b/core/runtime.rs
@@ -89,7 +89,6 @@ pub struct JsRuntime {
snapshot_options: SnapshotOptions,
allocations: IsolateAllocations,
extensions: Vec<Extension>,
- extensions_with_js: Vec<Extension>,
event_loop_middlewares: Vec<Box<OpEventLoopFn>>,
// Marks if this is considered the top-level runtime. Used only be inspector.
is_main: bool,
@@ -253,20 +252,12 @@ pub struct RuntimeOptions {
/// JsRuntime extensions, not to be confused with ES modules.
/// Only ops registered by extensions will be initialized. If you need
- /// to execute JS code from extensions, use `extensions_with_js` options
- /// instead.
- pub extensions: Vec<Extension>,
-
- /// JsRuntime extensions, not to be confused with ES modules.
- /// Ops registered by extensions will be initialized and JS code will be
- /// executed. If you don't need to execute JS code from extensions, use
- /// `extensions` option instead.
+ /// to execute JS code from extensions, pass source files in `js` or `esm`
+ /// option on `ExtensionBuilder`.
///
- /// This is useful when creating snapshots, in such case you would pass
- /// extensions using `extensions_with_js`, later when creating a runtime
- /// from the snapshot, you would pass these extensions using `extensions`
- /// option.
- pub extensions_with_js: Vec<Extension>,
+ /// If you are creating a runtime from a snapshot take care not to include
+ /// JavaScript sources in the extensions.
+ pub extensions: Vec<Extension>,
/// V8 snapshot that should be loaded on startup.
pub startup_snapshot: Option<Snapshot>,
@@ -373,18 +364,15 @@ impl JsRuntime {
let has_startup_snapshot = options.startup_snapshot.is_some();
if !has_startup_snapshot {
options
- .extensions_with_js
- .insert(0, crate::ops_builtin::init_builtins());
+ .extensions
+ .insert(0, crate::ops_builtin::init_builtin_ops_and_esm());
} else {
options
.extensions
- .insert(0, crate::ops_builtin::init_builtins());
+ .insert(0, crate::ops_builtin::init_builtin_ops());
}
- let ops = Self::collect_ops(
- &mut options.extensions,
- &mut options.extensions_with_js,
- );
+ let ops = Self::collect_ops(&mut options.extensions);
let mut op_state = OpState::new(ops.len());
if let Some(get_error_class_fn) = options.get_error_class_fn {
@@ -624,9 +612,12 @@ impl JsRuntime {
let loader = if snapshot_options != SnapshotOptions::Load {
let esm_sources = options
- .extensions_with_js
+ .extensions
.iter()
- .flat_map(|ext| ext.get_esm_sources().to_owned())
+ .flat_map(|ext| match ext.get_esm_sources() {
+ Some(s) => s.to_owned(),
+ None => vec![],
+ })
.collect::<Vec<ExtensionFileSource>>();
#[cfg(feature = "include_js_files_for_snapshotting")]
@@ -689,7 +680,6 @@ impl JsRuntime {
allocations: IsolateAllocations::default(),
event_loop_middlewares: Vec::with_capacity(options.extensions.len()),
extensions: options.extensions,
- extensions_with_js: options.extensions_with_js,
state: state_rc,
module_map: Some(module_map_rc),
is_main: options.is_main,
@@ -752,7 +742,7 @@ impl JsRuntime {
/// Creates a new realm (V8 context) in this JS execution context,
/// pre-initialized with all of the extensions that were passed in
- /// [`RuntimeOptions::extensions_with_js`] when the [`JsRuntime`] was
+ /// [`RuntimeOptions::extensions`] when the [`JsRuntime`] was
/// constructed.
pub fn create_realm(&mut self) -> Result<JsRealm, Error> {
let realm = {
@@ -868,50 +858,45 @@ impl JsRuntime {
}
// Take extensions to avoid double-borrow
- let extensions = std::mem::take(&mut self.extensions_with_js);
+ let extensions = std::mem::take(&mut self.extensions);
for ext in &extensions {
{
- let esm_files = ext.get_esm_sources();
- if let Some(entry_point) = ext.get_esm_entry_point() {
- let file_source = esm_files
- .iter()
- .find(|file| file.specifier == entry_point)
- .unwrap();
- load_and_evaluate_module(self, file_source)?;
- } else {
- for file_source in esm_files {
+ if let Some(esm_files) = ext.get_esm_sources() {
+ if let Some(entry_point) = ext.get_esm_entry_point() {
+ let file_source = esm_files
+ .iter()
+ .find(|file| file.specifier == entry_point)
+ .unwrap();
load_and_evaluate_module(self, file_source)?;
+ } else {
+ for file_source in esm_files {
+ load_and_evaluate_module(self, file_source)?;
+ }
}
}
}
{
- let js_files = ext.get_js_sources();
- for file_source in js_files {
- // TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap
- realm.execute_script(
- self.v8_isolate(),
- &file_source.specifier,
- &file_source.code.load()?,
- )?;
+ if let Some(js_files) = ext.get_js_sources() {
+ for file_source in js_files {
+ // TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap
+ realm.execute_script(
+ self.v8_isolate(),
+ &file_source.specifier,
+ &file_source.code.load()?,
+ )?;
+ }
}
}
}
// Restore extensions
- self.extensions_with_js = extensions;
+ self.extensions = extensions;
Ok(())
}
/// Collects ops from extensions & applies middleware
- fn collect_ops(
- extensions: &mut [Extension],
- extensions_with_js: &mut [Extension],
- ) -> Vec<OpDecl> {
- let mut exts = vec![];
- exts.extend(extensions);
- exts.extend(extensions_with_js);
-
+ fn collect_ops(exts: &mut [Extension]) -> Vec<OpDecl> {
for (ext, previous_exts) in
exts.iter().enumerate().map(|(i, ext)| (ext, &exts[..i]))
{
@@ -970,24 +955,6 @@ impl JsRuntime {
// Restore extensions
self.extensions = extensions;
}
- {
- let mut extensions: Vec<Extension> =
- std::mem::take(&mut self.extensions_with_js);
-
- // Setup state
- for e in extensions.iter_mut() {
- // ops are already registered during in bindings::initialize_context();
- e.init_state(&mut op_state.borrow_mut());
-
- // Setup event-loop middleware
- if let Some(middleware) = e.init_event_loop_middleware() {
- self.event_loop_middlewares.push(middleware);
- }
- }
-
- // Restore extensions
- self.extensions_with_js = extensions;
- }
Ok(())
}
diff --git a/core/snapshot_util.rs b/core/snapshot_util.rs
index 8b4fa3fa4..38ab18b4f 100644
--- a/core/snapshot_util.rs
+++ b/core/snapshot_util.rs
@@ -17,7 +17,6 @@ pub struct CreateSnapshotOptions {
pub snapshot_path: PathBuf,
pub startup_snapshot: Option<Snapshot>,
pub extensions: Vec<Extension>,
- pub extensions_with_js: Vec<Extension>,
pub compression_cb: Option<Box<CompressionCb>>,
pub snapshot_module_load_cb: Option<ExtModuleLoaderCb>,
}
@@ -29,7 +28,6 @@ pub fn create_snapshot(create_snapshot_options: CreateSnapshotOptions) {
will_snapshot: true,
startup_snapshot: create_snapshot_options.startup_snapshot,
extensions: create_snapshot_options.extensions,
- extensions_with_js: create_snapshot_options.extensions_with_js,
snapshot_module_load_cb: create_snapshot_options.snapshot_module_load_cb,
..Default::default()
});