mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 08:09:08 -05:00
refactor(core): provide builtins as an Extension (#10449)
This commit is contained in:
parent
684c357136
commit
578f2ba45e
4 changed files with 30 additions and 31 deletions
|
@ -1,18 +1,29 @@
|
|||
use crate::error::bad_resource_id;
|
||||
use crate::error::type_error;
|
||||
use crate::error::AnyError;
|
||||
use crate::include_js_files;
|
||||
use crate::op_sync;
|
||||
use crate::resources::ResourceId;
|
||||
use crate::Extension;
|
||||
use crate::OpState;
|
||||
use crate::ZeroCopyBuf;
|
||||
|
||||
// TODO(@AaronO): provide these ops grouped as a runtime extension
|
||||
// e.g:
|
||||
// pub fn init_builtins() -> Extension { ... }
|
||||
pub(crate) fn init_builtins() -> Extension {
|
||||
Extension::builder()
|
||||
.js(include_js_files!(
|
||||
prefix "deno:core",
|
||||
"core.js",
|
||||
"error.js",
|
||||
))
|
||||
.ops(vec![
|
||||
("op_close", op_sync(op_close)),
|
||||
("op_resources", op_sync(op_resources)),
|
||||
])
|
||||
.build()
|
||||
}
|
||||
|
||||
/// Return map of resources with id as key
|
||||
/// and string representation as value.
|
||||
///
|
||||
/// This op must be wrapped in `op_sync`.
|
||||
pub fn op_resources(
|
||||
state: &mut OpState,
|
||||
_args: (),
|
||||
|
@ -27,8 +38,6 @@ pub fn op_resources(
|
|||
}
|
||||
|
||||
/// Remove a resource from the resource table.
|
||||
///
|
||||
/// This op must be wrapped in `op_sync`.
|
||||
pub fn op_close(
|
||||
state: &mut OpState,
|
||||
rid: Option<ResourceId>,
|
||||
|
|
|
@ -305,6 +305,11 @@ impl JsRuntime {
|
|||
waker: AtomicWaker::new(),
|
||||
})));
|
||||
|
||||
// Add builtins extension
|
||||
options
|
||||
.extensions
|
||||
.insert(0, crate::ops_builtin::init_builtins());
|
||||
|
||||
let mut js_runtime = Self {
|
||||
v8_isolate: Some(isolate),
|
||||
snapshot_creator: maybe_snapshot_creator,
|
||||
|
@ -316,7 +321,6 @@ impl JsRuntime {
|
|||
// TODO(@AaronO): diff extensions inited in snapshot and those provided
|
||||
// for now we assume that snapshot and extensions always match
|
||||
if !has_startup_snapshot {
|
||||
js_runtime.js_init();
|
||||
js_runtime.init_extension_js().unwrap();
|
||||
}
|
||||
// Init extension ops
|
||||
|
@ -360,18 +364,6 @@ impl JsRuntime {
|
|||
s.clone()
|
||||
}
|
||||
|
||||
/// Executes a JavaScript code to provide Deno.core and error reporting.
|
||||
///
|
||||
/// This function can be called during snapshotting.
|
||||
fn js_init(&mut self) {
|
||||
self
|
||||
.execute("deno:core/core.js", include_str!("core.js"))
|
||||
.unwrap();
|
||||
self
|
||||
.execute("deno:core/error.js", include_str!("error.js"))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Initializes JS of provided Extensions
|
||||
fn init_extension_js(&mut self) -> Result<(), AnyError> {
|
||||
// Take extensions to avoid double-borrow
|
||||
|
@ -1592,7 +1584,8 @@ pub mod tests {
|
|||
dispatch_count: dispatch_count.clone(),
|
||||
});
|
||||
|
||||
runtime.register_op("test", dispatch);
|
||||
runtime.register_op("op_test", dispatch);
|
||||
runtime.sync_ops_cache();
|
||||
|
||||
runtime
|
||||
.execute(
|
||||
|
@ -1618,9 +1611,9 @@ pub mod tests {
|
|||
"filename.js",
|
||||
r#"
|
||||
let control = 42;
|
||||
Deno.core.opcall(1, null, control);
|
||||
Deno.core.opAsync("op_test", control);
|
||||
async function main() {
|
||||
Deno.core.opcall(1, null, control);
|
||||
Deno.core.opAsync("op_test", control);
|
||||
}
|
||||
main();
|
||||
"#,
|
||||
|
@ -1636,7 +1629,7 @@ pub mod tests {
|
|||
.execute(
|
||||
"filename.js",
|
||||
r#"
|
||||
Deno.core.opcall(1);
|
||||
Deno.core.opAsync("op_test");
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
@ -1651,7 +1644,7 @@ pub mod tests {
|
|||
"filename.js",
|
||||
r#"
|
||||
let zero_copy_a = new Uint8Array([0]);
|
||||
Deno.core.opcall(1, null, null, zero_copy_a);
|
||||
Deno.core.opAsync("op_test", null, zero_copy_a);
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
@ -1954,7 +1947,8 @@ pub mod tests {
|
|||
module_loader: Some(loader),
|
||||
..Default::default()
|
||||
});
|
||||
runtime.register_op("test", dispatcher);
|
||||
runtime.register_op("op_test", dispatcher);
|
||||
runtime.sync_ops_cache();
|
||||
|
||||
runtime
|
||||
.execute(
|
||||
|
@ -1980,7 +1974,7 @@ pub mod tests {
|
|||
import { b } from './b.js'
|
||||
if (b() != 'b') throw Error();
|
||||
let control = 42;
|
||||
Deno.core.opcall(1, null, control);
|
||||
Deno.core.opAsync("op_test", control);
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
|
|
@ -260,8 +260,6 @@ impl WebWorker {
|
|||
Some(sender),
|
||||
options.create_web_worker_cb.clone(),
|
||||
);
|
||||
ops::reg_sync(js_runtime, "op_close", deno_core::op_close);
|
||||
ops::reg_sync(js_runtime, "op_resources", deno_core::op_resources);
|
||||
ops::io::init(js_runtime);
|
||||
|
||||
if options.use_deno_namespace {
|
||||
|
|
|
@ -146,8 +146,6 @@ impl MainWorker {
|
|||
None,
|
||||
options.create_web_worker_cb.clone(),
|
||||
);
|
||||
ops::reg_sync(js_runtime, "op_close", deno_core::op_close);
|
||||
ops::reg_sync(js_runtime, "op_resources", deno_core::op_resources);
|
||||
ops::fs_events::init(js_runtime);
|
||||
ops::fs::init(js_runtime);
|
||||
ops::http::init(js_runtime);
|
||||
|
|
Loading…
Reference in a new issue