From dd156e886b0d0f7d67538539bf7f3624b817d80a Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 23 Apr 2021 17:50:45 +0200 Subject: [PATCH] refactor(core): rename send() to opcall() (#10307) I think it's a better fit since recv() was killed and opcall <> syscall (send/recv was too reminiscent of request/response and custom payloads) --- .../unit/{dispatch_test.ts => opcall_test.ts} | 2 +- cli/tests/unit/unit_tests.ts | 2 +- core/benches/op_baseline.rs | 2 +- core/bindings.rs | 8 ++++---- core/core.js | 9 ++++----- core/examples/hello_world.rs | 6 +++--- core/runtime.rs | 20 +++++++++---------- 7 files changed, 24 insertions(+), 25 deletions(-) rename cli/tests/unit/{dispatch_test.ts => opcall_test.ts} (95%) diff --git a/cli/tests/unit/dispatch_test.ts b/cli/tests/unit/opcall_test.ts similarity index 95% rename from cli/tests/unit/dispatch_test.ts rename to cli/tests/unit/opcall_test.ts index 6805d1af78..6bade65457 100644 --- a/cli/tests/unit/dispatch_test.ts +++ b/cli/tests/unit/opcall_test.ts @@ -9,7 +9,7 @@ unitTest(async function sendAsyncStackTrace() { } catch (error) { const s = error.stack.toString(); console.log(s); - assertStringIncludes(s, "dispatch_test.ts"); + assertStringIncludes(s, "opcall_test.ts"); assertStringIncludes(s, "read"); } }); diff --git a/cli/tests/unit/unit_tests.ts b/cli/tests/unit/unit_tests.ts index 28924165fc..f538ab9b69 100644 --- a/cli/tests/unit/unit_tests.ts +++ b/cli/tests/unit/unit_tests.ts @@ -15,7 +15,7 @@ import "./console_test.ts"; import "./copy_file_test.ts"; import "./custom_event_test.ts"; import "./dir_test.ts"; -import "./dispatch_test.ts"; +import "./opcall_test.ts"; import "./error_stack_test.ts"; import "./event_test.ts"; import "./event_target_test.ts"; diff --git a/core/benches/op_baseline.rs b/core/benches/op_baseline.rs index 04b72959cb..a7f3bda1df 100644 --- a/core/benches/op_baseline.rs +++ b/core/benches/op_baseline.rs @@ -81,7 +81,7 @@ fn bench_op_nop(b: &mut Bencher) { bench_runtime_js( b, r#"for(let i=0; i < 1e3; i++) { - Deno.core.dispatchByName("nop", null, null, null); + Deno.core.opSync("nop", null, null, null); }"#, ); } diff --git a/core/bindings.rs b/core/bindings.rs index 7467fe0a8e..4574e2d4e3 100644 --- a/core/bindings.rs +++ b/core/bindings.rs @@ -26,7 +26,7 @@ lazy_static::lazy_static! { function: print.map_fn_to() }, v8::ExternalReference { - function: send.map_fn_to() + function: opcall.map_fn_to() }, v8::ExternalReference { function: set_macrotask_callback.map_fn_to() @@ -119,7 +119,7 @@ pub fn initialize_context<'s>( // Bind functions to Deno.core.* set_func(scope, core_val, "print", print); - set_func(scope, core_val, "send", send); + set_func(scope, core_val, "opcall", opcall); set_func( scope, core_val, @@ -317,7 +317,7 @@ fn print( } } -fn send<'s>( +fn opcall<'s>( scope: &mut v8::HandleScope<'s>, args: v8::FunctionCallbackArguments, mut rv: v8::ReturnValue, @@ -336,7 +336,7 @@ fn send<'s>( } }; - // send(0) returns obj of all ops, handle as special case + // opcall(0) returns obj of all ops, handle as special case if op_id == 0 { // TODO: Serialize as HashMap when serde_v8 supports maps ... let ops = OpTable::op_entries(state.op_state.clone()); diff --git a/core/core.js b/core/core.js index f90dbcc18f..a11f281f1a 100644 --- a/core/core.js +++ b/core/core.js @@ -3,7 +3,7 @@ ((window) => { // Available on start due to bindings. - const { send } = window.Deno.core; + const { opcall } = window.Deno.core; let opsCache = {}; const errorMap = { @@ -61,7 +61,7 @@ function ops() { // op id 0 is a special value to retrieve the map of registered ops. - const newOpsCache = Object.fromEntries(send(0)); + const newOpsCache = Object.fromEntries(opcall(0)); opsCache = Object.freeze(newOpsCache); return opsCache; } @@ -76,7 +76,8 @@ } function dispatch(opName, promiseId, control, zeroCopy) { - return send(opsCache[opName], promiseId, control, zeroCopy); + const opId = typeof opName === "string" ? opsCache[opName] : opName; + return opcall(opId, promiseId, control, zeroCopy); } function registerErrorClass(className, errorClass) { @@ -124,8 +125,6 @@ Object.assign(window.Deno.core, { opAsync, opSync, - dispatch: send, - dispatchByName: dispatch, ops, close, resources, diff --git a/core/examples/hello_world.rs b/core/examples/hello_world.rs index 3a40ee29ac..62d2683401 100644 --- a/core/examples/hello_world.rs +++ b/core/examples/hello_world.rs @@ -19,8 +19,8 @@ fn main() { // The second one just transforms some input and returns it to JavaScript. // Register the op for outputting a string to stdout. - // It can be invoked with Deno.core.dispatch and the id this method returns - // or Deno.core.dispatchByName and the name provided. + // It can be invoked with Deno.core.opcall and the id this method returns + // or Deno.core.opSync and the name provided. runtime.register_op( "op_print", // The op_fn callback takes a state object OpState, @@ -72,7 +72,7 @@ Deno.core.ops(); // our op_print op to display the stringified argument. const _newline = new Uint8Array([10]); function print(value) { - Deno.core.dispatchByName('op_print', 0, value.toString(), _newline); + Deno.core.opSync('op_print', value.toString(), _newline); } "#, ) diff --git a/core/runtime.rs b/core/runtime.rs index 917588720c..da8a6d93a2 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -73,9 +73,9 @@ struct IsolateAllocations { /// The JsRuntime future completes when there is an error or when all /// pending ops have completed. /// -/// Ops are created in JavaScript by calling Deno.core.dispatch(), and in Rust -/// by implementing dispatcher function that takes control buffer and optional zero copy buffer -/// as arguments. An async Op corresponds exactly to a Promise in JavaScript. +/// Pending ops are created in JavaScript by calling Deno.core.opAsync(), and in Rust +/// by implementing an async function that takes a serde::Deserialize "control argument" +/// and an optional zero copy buffer, each async Op is tied to a Promise in JavaScript. pub struct JsRuntime { // This is an Option instead of just OwnedIsolate to workaround // an safety issue with SnapshotCreator. See JsRuntime::drop. @@ -1572,9 +1572,9 @@ pub mod tests { "filename.js", r#" let control = 42; - Deno.core.send(1, null, control); + Deno.core.opcall(1, null, control); async function main() { - Deno.core.send(1, null, control); + Deno.core.opcall(1, null, control); } main(); "#, @@ -1590,7 +1590,7 @@ pub mod tests { .execute( "filename.js", r#" - Deno.core.send(1); + Deno.core.opcall(1); "#, ) .unwrap(); @@ -1605,7 +1605,7 @@ pub mod tests { "filename.js", r#" let zero_copy_a = new Uint8Array([0]); - Deno.core.send(1, null, null, zero_copy_a); + Deno.core.opcall(1, null, null, zero_copy_a); "#, ) .unwrap(); @@ -1673,7 +1673,7 @@ pub mod tests { r#" let thrown; try { - Deno.core.dispatch(100); + Deno.core.opSync(100); } catch (e) { thrown = e; } @@ -1934,7 +1934,7 @@ pub mod tests { import { b } from './b.js' if (b() != 'b') throw Error(); let control = 42; - Deno.core.send(1, null, control); + Deno.core.opcall(1, null, control); "#, ) .unwrap(); @@ -2304,7 +2304,7 @@ main(); let error = runtime .execute( "core_js_stack_frame.js", - "Deno.core.dispatchByName('non_existent');", + "Deno.core.opSync('non_existent');", ) .unwrap_err(); let error_string = error.to_string();