From dd156e886b0d0f7d67538539bf7f3624b817d80a Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 23 Apr 2021 17:50:45 +0200 Subject: 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) --- cli/tests/unit/dispatch_test.ts | 48 ----------------------------------------- cli/tests/unit/opcall_test.ts | 48 +++++++++++++++++++++++++++++++++++++++++ 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 ++++++++--------- 8 files changed, 71 insertions(+), 72 deletions(-) delete mode 100644 cli/tests/unit/dispatch_test.ts create mode 100644 cli/tests/unit/opcall_test.ts diff --git a/cli/tests/unit/dispatch_test.ts b/cli/tests/unit/dispatch_test.ts deleted file mode 100644 index 6805d1af7..000000000 --- a/cli/tests/unit/dispatch_test.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { assertStringIncludes, unitTest, unreachable } from "./test_util.ts"; - -unitTest(async function sendAsyncStackTrace() { - const buf = new Uint8Array(10); - const rid = 10; - try { - await Deno.read(rid, buf); - unreachable(); - } catch (error) { - const s = error.stack.toString(); - console.log(s); - assertStringIncludes(s, "dispatch_test.ts"); - assertStringIncludes(s, "read"); - } -}); - -declare global { - namespace Deno { - // deno-lint-ignore no-explicit-any - var core: any; // eslint-disable-line no-var - } -} - -unitTest(async function opsAsyncBadResource(): Promise { - try { - const nonExistingRid = 9999; - await Deno.core.opAsync( - "op_read_async", - nonExistingRid, - new Uint8Array(0), - ); - } catch (e) { - if (!(e instanceof Deno.errors.BadResource)) { - throw e; - } - } -}); - -unitTest(function opsSyncBadResource(): void { - try { - const nonExistingRid = 9999; - Deno.core.opSync("op_read_sync", nonExistingRid, new Uint8Array(0)); - } catch (e) { - if (!(e instanceof Deno.errors.BadResource)) { - throw e; - } - } -}); diff --git a/cli/tests/unit/opcall_test.ts b/cli/tests/unit/opcall_test.ts new file mode 100644 index 000000000..6bade6545 --- /dev/null +++ b/cli/tests/unit/opcall_test.ts @@ -0,0 +1,48 @@ +import { assertStringIncludes, unitTest, unreachable } from "./test_util.ts"; + +unitTest(async function sendAsyncStackTrace() { + const buf = new Uint8Array(10); + const rid = 10; + try { + await Deno.read(rid, buf); + unreachable(); + } catch (error) { + const s = error.stack.toString(); + console.log(s); + assertStringIncludes(s, "opcall_test.ts"); + assertStringIncludes(s, "read"); + } +}); + +declare global { + namespace Deno { + // deno-lint-ignore no-explicit-any + var core: any; // eslint-disable-line no-var + } +} + +unitTest(async function opsAsyncBadResource(): Promise { + try { + const nonExistingRid = 9999; + await Deno.core.opAsync( + "op_read_async", + nonExistingRid, + new Uint8Array(0), + ); + } catch (e) { + if (!(e instanceof Deno.errors.BadResource)) { + throw e; + } + } +}); + +unitTest(function opsSyncBadResource(): void { + try { + const nonExistingRid = 9999; + Deno.core.opSync("op_read_sync", nonExistingRid, new Uint8Array(0)); + } catch (e) { + if (!(e instanceof Deno.errors.BadResource)) { + throw e; + } + } +}); diff --git a/cli/tests/unit/unit_tests.ts b/cli/tests/unit/unit_tests.ts index 28924165f..f538ab9b6 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 04b72959c..a7f3bda1d 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 7467fe0a8..4574e2d4e 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 f90dbcc18..a11f281f1 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 3a40ee29a..62d268340 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 917588720..da8a6d93a 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(); -- cgit v1.2.3