1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

refactor(core): provide builtins as an Extension (#10449)

This commit is contained in:
Aaron O'Mullan 2021-05-01 03:08:29 +02:00 committed by GitHub
parent 684c357136
commit 578f2ba45e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 31 deletions

View file

@ -1,18 +1,29 @@
use crate::error::bad_resource_id; use crate::error::bad_resource_id;
use crate::error::type_error; use crate::error::type_error;
use crate::error::AnyError; use crate::error::AnyError;
use crate::include_js_files;
use crate::op_sync;
use crate::resources::ResourceId; use crate::resources::ResourceId;
use crate::Extension;
use crate::OpState; use crate::OpState;
use crate::ZeroCopyBuf; use crate::ZeroCopyBuf;
// TODO(@AaronO): provide these ops grouped as a runtime extension pub(crate) fn init_builtins() -> Extension {
// e.g: Extension::builder()
// pub fn init_builtins() -> Extension { ... } .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 /// Return map of resources with id as key
/// and string representation as value. /// and string representation as value.
///
/// This op must be wrapped in `op_sync`.
pub fn op_resources( pub fn op_resources(
state: &mut OpState, state: &mut OpState,
_args: (), _args: (),
@ -27,8 +38,6 @@ pub fn op_resources(
} }
/// Remove a resource from the resource table. /// Remove a resource from the resource table.
///
/// This op must be wrapped in `op_sync`.
pub fn op_close( pub fn op_close(
state: &mut OpState, state: &mut OpState,
rid: Option<ResourceId>, rid: Option<ResourceId>,

View file

@ -305,6 +305,11 @@ impl JsRuntime {
waker: AtomicWaker::new(), waker: AtomicWaker::new(),
}))); })));
// Add builtins extension
options
.extensions
.insert(0, crate::ops_builtin::init_builtins());
let mut js_runtime = Self { let mut js_runtime = Self {
v8_isolate: Some(isolate), v8_isolate: Some(isolate),
snapshot_creator: maybe_snapshot_creator, snapshot_creator: maybe_snapshot_creator,
@ -316,7 +321,6 @@ impl JsRuntime {
// TODO(@AaronO): diff extensions inited in snapshot and those provided // TODO(@AaronO): diff extensions inited in snapshot and those provided
// for now we assume that snapshot and extensions always match // for now we assume that snapshot and extensions always match
if !has_startup_snapshot { if !has_startup_snapshot {
js_runtime.js_init();
js_runtime.init_extension_js().unwrap(); js_runtime.init_extension_js().unwrap();
} }
// Init extension ops // Init extension ops
@ -360,18 +364,6 @@ impl JsRuntime {
s.clone() 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 /// Initializes JS of provided Extensions
fn init_extension_js(&mut self) -> Result<(), AnyError> { fn init_extension_js(&mut self) -> Result<(), AnyError> {
// Take extensions to avoid double-borrow // Take extensions to avoid double-borrow
@ -1592,7 +1584,8 @@ pub mod tests {
dispatch_count: dispatch_count.clone(), dispatch_count: dispatch_count.clone(),
}); });
runtime.register_op("test", dispatch); runtime.register_op("op_test", dispatch);
runtime.sync_ops_cache();
runtime runtime
.execute( .execute(
@ -1618,9 +1611,9 @@ pub mod tests {
"filename.js", "filename.js",
r#" r#"
let control = 42; let control = 42;
Deno.core.opcall(1, null, control); Deno.core.opAsync("op_test", control);
async function main() { async function main() {
Deno.core.opcall(1, null, control); Deno.core.opAsync("op_test", control);
} }
main(); main();
"#, "#,
@ -1636,7 +1629,7 @@ pub mod tests {
.execute( .execute(
"filename.js", "filename.js",
r#" r#"
Deno.core.opcall(1); Deno.core.opAsync("op_test");
"#, "#,
) )
.unwrap(); .unwrap();
@ -1651,7 +1644,7 @@ pub mod tests {
"filename.js", "filename.js",
r#" r#"
let zero_copy_a = new Uint8Array([0]); 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(); .unwrap();
@ -1954,7 +1947,8 @@ pub mod tests {
module_loader: Some(loader), module_loader: Some(loader),
..Default::default() ..Default::default()
}); });
runtime.register_op("test", dispatcher); runtime.register_op("op_test", dispatcher);
runtime.sync_ops_cache();
runtime runtime
.execute( .execute(
@ -1980,7 +1974,7 @@ pub mod tests {
import { b } from './b.js' import { b } from './b.js'
if (b() != 'b') throw Error(); if (b() != 'b') throw Error();
let control = 42; let control = 42;
Deno.core.opcall(1, null, control); Deno.core.opAsync("op_test", control);
"#, "#,
) )
.unwrap(); .unwrap();

View file

@ -260,8 +260,6 @@ impl WebWorker {
Some(sender), Some(sender),
options.create_web_worker_cb.clone(), 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); ops::io::init(js_runtime);
if options.use_deno_namespace { if options.use_deno_namespace {

View file

@ -146,8 +146,6 @@ impl MainWorker {
None, None,
options.create_web_worker_cb.clone(), 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_events::init(js_runtime);
ops::fs::init(js_runtime); ops::fs::init(js_runtime);
ops::http::init(js_runtime); ops::http::init(js_runtime);