diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/benches/op_baseline.rs | 11 | ||||
-rw-r--r-- | core/core.js | 9 | ||||
-rw-r--r-- | core/examples/hello_world.rs | 7 | ||||
-rw-r--r-- | core/examples/http_bench_json_ops.js | 2 | ||||
-rw-r--r-- | core/examples/http_bench_json_ops.rs | 1 | ||||
-rw-r--r-- | core/ops_json.rs | 12 | ||||
-rw-r--r-- | core/runtime.rs | 8 |
7 files changed, 21 insertions, 29 deletions
diff --git a/core/benches/op_baseline.rs b/core/benches/op_baseline.rs index a7f3bda1d..c0c988eb6 100644 --- a/core/benches/op_baseline.rs +++ b/core/benches/op_baseline.rs @@ -20,16 +20,7 @@ fn create_js_runtime() -> JsRuntime { runtime.register_op("nop", |state, _, _| { Op::Sync(serialize_op_result(Ok(9), state)) }); - - // Init ops - runtime - .execute( - "init", - r#" - Deno.core.ops(); - "#, - ) - .unwrap(); + runtime.sync_ops_cache(); runtime } diff --git a/core/core.js b/core/core.js index a11f281f1..fd9b2c3ea 100644 --- a/core/core.js +++ b/core/core.js @@ -60,12 +60,14 @@ } function ops() { - // op id 0 is a special value to retrieve the map of registered ops. - const newOpsCache = Object.fromEntries(opcall(0)); - opsCache = Object.freeze(newOpsCache); return opsCache; } + function syncOpsCache() { + // op id 0 is a special value to retrieve the map of registered ops. + opsCache = Object.freeze(Object.fromEntries(opcall(0))); + } + function handleAsyncMsgFromRust() { for (let i = 0; i < arguments.length; i += 2) { const promiseId = arguments[i]; @@ -130,5 +132,6 @@ resources, registerErrorClass, handleAsyncMsgFromRust, + syncOpsCache, }); })(this); diff --git a/core/examples/hello_world.rs b/core/examples/hello_world.rs index 62d268340..a9d2934f6 100644 --- a/core/examples/hello_world.rs +++ b/core/examples/hello_world.rs @@ -56,6 +56,7 @@ fn main() { Ok(sum) }), ); + runtime.sync_ops_cache(); // Now we see how to invoke the ops we just defined. The runtime automatically // contains a Deno.core object with several functions for interacting with it. @@ -64,11 +65,7 @@ fn main() { .execute( "<init>", r#" -// First we initialize the ops cache. -// This maps op names to their id's. -Deno.core.ops(); - -// Then we define a print function that uses +// Define a print function that uses // our op_print op to display the stringified argument. const _newline = new Uint8Array([10]); function print(value) { diff --git a/core/examples/http_bench_json_ops.js b/core/examples/http_bench_json_ops.js index 672747196..ad36dd674 100644 --- a/core/examples/http_bench_json_ops.js +++ b/core/examples/http_bench_json_ops.js @@ -54,8 +54,6 @@ async function serve(rid) { } async function main() { - Deno.core.ops(); - const listenerRid = listen(); Deno.core.print(`http_bench_ops listening on http://127.0.0.1:4544/\n`); diff --git a/core/examples/http_bench_json_ops.rs b/core/examples/http_bench_json_ops.rs index e1b435e4c..f891caa9e 100644 --- a/core/examples/http_bench_json_ops.rs +++ b/core/examples/http_bench_json_ops.rs @@ -116,6 +116,7 @@ fn create_js_runtime() -> JsRuntime { runtime.register_op("accept", deno_core::op_async(op_accept)); runtime.register_op("read", deno_core::op_async(op_read)); runtime.register_op("write", deno_core::op_async(op_write)); + runtime.sync_ops_cache(); runtime } diff --git a/core/ops_json.rs b/core/ops_json.rs index 309fac12d..a04bdac60 100644 --- a/core/ops_json.rs +++ b/core/ops_json.rs @@ -25,15 +25,15 @@ use std::rc::Rc; /// ```ignore /// let mut runtime = JsRuntime::new(...); /// runtime.register_op("hello", deno_core::op_sync(Self::hello_op)); +/// runtime.sync_ops_cache(); /// ``` /// /// ...it can be invoked from JS using the provided name, for example: /// ```js -/// Deno.core.ops(); /// let result = Deno.core.opSync("function_name", args); /// ``` /// -/// The `Deno.core.ops()` statement is needed once before any op calls, for initialization. +/// `runtime.sync_ops_cache()` must be called after registering new ops /// A more complete example is available in the examples directory. pub fn op_sync<F, V, R>(op_fn: F) -> Box<OpFn> where @@ -63,15 +63,15 @@ where /// ```ignore /// let mut runtime = JsRuntime::new(...); /// runtime.register_op("hello", deno_core::op_async(Self::hello_op)); +/// runtime.sync_ops_cache(); /// ``` /// /// ...it can be invoked from JS using the provided name, for example: /// ```js -/// Deno.core.ops(); /// let future = Deno.core.opAsync("function_name", args); /// ``` /// -/// The `Deno.core.ops()` statement is needed once before any op calls, for initialization. +/// `runtime.sync_ops_cache()` must be called after registering new ops /// A more complete example is available in the examples directory. pub fn op_async<F, V, R, RV>(op_fn: F) -> Box<OpFn> where @@ -116,13 +116,11 @@ mod tests { } runtime.register_op("op_throw", op_async(op_throw)); + runtime.sync_ops_cache(); runtime .execute( "<init>", r#" - // First we initialize the ops cache. This maps op names to their id's. - Deno.core.ops(); - async function f1() { await Deno.core.opAsync('op_throw', 'hello'); } diff --git a/core/runtime.rs b/core/runtime.rs index 34a83e3f3..7475c7020 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -367,6 +367,11 @@ impl JsRuntime { state.js_recv_cb.replace(v8::Global::new(scope, cb)); } + /// Ensures core.js has the latest op-name to op-id mappings + pub fn sync_ops_cache(&mut self) { + self.execute("<anon>", "Deno.core.syncOpsCache()").unwrap() + } + /// Returns the runtime's op state, which can be used to maintain ops /// and access resources between op calls. pub fn op_state(&mut self) -> Rc<RefCell<OpState>> { @@ -2140,6 +2145,7 @@ pub mod tests { module_loader: Some(loader), ..Default::default() }); + runtime.sync_ops_cache(); runtime .execute( "file:///dyn_import3.js", @@ -2149,8 +2155,6 @@ pub mod tests { if (mod.b() !== 'b') { throw Error("bad"); } - // Now do any op - Deno.core.ops(); })(); "#, ) |