diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/core.d.ts | 25 | ||||
-rw-r--r-- | core/http_bench.js | 12 | ||||
-rw-r--r-- | core/isolate.rs | 68 | ||||
-rw-r--r-- | core/shared_queue.js | 35 | ||||
-rw-r--r-- | core/shared_queue_test.js | 4 |
5 files changed, 90 insertions, 54 deletions
diff --git a/core/core.d.ts b/core/core.d.ts new file mode 100644 index 000000000..b1d1ac57f --- /dev/null +++ b/core/core.d.ts @@ -0,0 +1,25 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + +// This file contains APIs that are introduced into the global namespace by +// Deno core. These are not intended to be used directly by runtime users of +// Deno and therefore do not flow through to the runtime type library. + +declare interface MessageCallback { + (msg: Uint8Array): void; +} + +declare interface DenoCore { + dispatch( + control: Uint8Array, + zeroCopy?: ArrayBufferView | null + ): Uint8Array | null; + setAsyncHandler(cb: MessageCallback): void; + sharedQueue: { + head(): number; + numRecords(): number; + size(): number; + push(buf: Uint8Array): boolean; + reset(): void; + shift(): Uint8Array | null; + }; +} diff --git a/core/http_bench.js b/core/http_bench.js index d7cffcb75..8eb764b55 100644 --- a/core/http_bench.js +++ b/core/http_bench.js @@ -42,7 +42,7 @@ function send(promiseId, opId, arg, zeroCopy = null) { scratch32[1] = opId; scratch32[2] = arg; scratch32[3] = -1; - return DenoCore.dispatch(scratchBytes, zeroCopy); + return Deno.core.dispatch(scratchBytes, zeroCopy); } /** Returns Promise<number> */ @@ -123,17 +123,17 @@ async function serve(rid) { } async function main() { - DenoCore.setAsyncHandler(handleAsyncMsgFromRust); + Deno.core.setAsyncHandler(handleAsyncMsgFromRust); - libdeno.print("http_bench.js start\n"); + Deno.core.print("http_bench.js start\n"); const listenerRid = listen(); - libdeno.print(`listening http://127.0.0.1:4544/ rid = ${listenerRid}\n`); + Deno.core.print(`listening http://127.0.0.1:4544/ rid = ${listenerRid}\n`); while (true) { const rid = await accept(listenerRid); - // libdeno.print(`accepted ${rid}`); + // Deno.core.print(`accepted ${rid}`); if (rid < 0) { - libdeno.print(`accept error ${rid}`); + Deno.core.print(`accept error ${rid}`); return; } serve(rid); diff --git a/core/isolate.rs b/core/isolate.rs index 74b8a7270..b28730ba6 100644 --- a/core/isolate.rs +++ b/core/isolate.rs @@ -63,8 +63,8 @@ pub trait Behavior { /// Isolate is created. fn startup_data(&mut self) -> Option<StartupData>; - /// Called whenever libdeno.send() is called in JavaScript. zero_copy_buf - /// corresponds to the second argument of libdeno.send(). + /// Called whenever Deno.core.send() is called in JavaScript. zero_copy_buf + /// corresponds to the second argument of Deno.core.send(). fn dispatch( &mut self, control: &[u8], @@ -77,7 +77,7 @@ pub trait Behavior { /// Tokio. The Isolate future complete when there is an error or when all /// pending ops have completed. /// -/// Ops are created in JavaScript by calling libdeno.send(), and in Rust by +/// Ops are created in JavaScript by calling Deno.core.send(), and in Rust by /// implementing Behavior::dispatch. An Op corresponds exactly to a Promise in /// JavaScript. pub struct Isolate<B: Behavior> { @@ -125,7 +125,7 @@ impl<B: Behavior> Isolate<B> { None => libdeno::deno_buf::empty(), }, shared: shared.as_deno_buf(), - recv_cb: Self::pre_dispatch, + recv_cb: Self::predispatch, }; let libdeno_isolate = unsafe { libdeno::deno_new(config) }; @@ -157,7 +157,7 @@ impl<B: Behavior> Isolate<B> { } } - /// Executes a bit of built-in JavaScript to provide Deno._sharedQueue. + /// Executes a bit of built-in JavaScript to provide Deno.sharedQueue. pub fn shared_init(&mut self) { if self.needs_init { self.needs_init = false; @@ -167,7 +167,7 @@ impl<B: Behavior> Isolate<B> { } } - extern "C" fn pre_dispatch( + extern "C" fn predispatch( user_data: *mut c_void, control_argv0: deno_buf, zero_copy_buf: deno_buf, @@ -178,12 +178,12 @@ impl<B: Behavior> Isolate<B> { let control_shared = isolate.shared.shift(); let (is_sync, op) = if control_argv0.len() > 0 { - // The user called libdeno.send(control) + // The user called Deno.core.send(control) isolate .behavior .dispatch(control_argv0.as_ref(), zero_copy_buf) } else if let Some(c) = control_shared { - // The user called Deno._sharedQueue.push(control) + // The user called Deno.sharedQueue.push(control) isolate.behavior.dispatch(&c, zero_copy_buf) } else { // The sharedQueue is empty. The shouldn't happen usually, but it's also @@ -199,7 +199,7 @@ impl<B: Behavior> Isolate<B> { if is_sync { let res_record = op.wait().unwrap(); - // For sync messages, we always return the response via libdeno.send's + // For sync messages, we always return the response via Deno.core.send's // return value. // TODO(ry) check that if JSError thrown during respond(), that it will be // picked up. @@ -614,15 +614,15 @@ mod tests { } #[test] - fn test_dispatch() { + fn testdispatch() { let mut isolate = TestBehavior::setup(TestBehaviorMode::AsyncImmediate); js_check(isolate.execute( "filename.js", r#" let control = new Uint8Array([42]); - libdeno.send(control); + Deno.core.send(control); async function main() { - libdeno.send(control); + Deno.core.send(control); } main(); "#, @@ -641,7 +641,7 @@ mod tests { import { b } from 'b.js' if (b() != 'b') throw Error(); let control = new Uint8Array([42]); - libdeno.send(control); + Deno.core.send(control); "#, ).unwrap(); assert_eq!(isolate.behavior.dispatch_count, 0); @@ -685,7 +685,7 @@ mod tests { "setup2.js", r#" let nrecv = 0; - DenoCore.setAsyncHandler((buf) => { + Deno.core.setAsyncHandler((buf) => { nrecv++; }); "#, @@ -696,7 +696,7 @@ mod tests { r#" assert(nrecv == 0); let control = new Uint8Array([42]); - libdeno.send(control); + Deno.core.send(control); assert(nrecv == 0); "#, )); @@ -707,7 +707,7 @@ mod tests { "check2.js", r#" assert(nrecv == 1); - libdeno.send(control); + Deno.core.send(control); assert(nrecv == 1); "#, )); @@ -727,7 +727,7 @@ mod tests { "setup2.js", r#" let nrecv = 0; - DenoCore.setAsyncHandler((buf) => { + Deno.core.setAsyncHandler((buf) => { assert(buf.byteLength === 1); assert(buf[0] === 43); nrecv++; @@ -740,12 +740,12 @@ mod tests { "send1.js", r#" let control = new Uint8Array([42]); - DenoCore.shared.push(control); - libdeno.send(); + Deno.core.sharedQueue.push(control); + Deno.core.send(); assert(nrecv === 0); - DenoCore.shared.push(control); - libdeno.send(); + Deno.core.sharedQueue.push(control); + Deno.core.send(); assert(nrecv === 0); "#, )); @@ -832,10 +832,10 @@ mod tests { "overflow_req_sync.js", r#" let asyncRecv = 0; - DenoCore.setAsyncHandler((buf) => { asyncRecv++ }); + Deno.core.setAsyncHandler((buf) => { asyncRecv++ }); // Large message that will overflow the shared space. let control = new Uint8Array(100 * 1024 * 1024); - let response = DenoCore.dispatch(control); + let response = Deno.core.dispatch(control); assert(response instanceof Uint8Array); assert(response.length == 1); assert(response[0] == 43); @@ -854,10 +854,10 @@ mod tests { "overflow_res_sync.js", r#" let asyncRecv = 0; - DenoCore.setAsyncHandler((buf) => { asyncRecv++ }); + Deno.core.setAsyncHandler((buf) => { asyncRecv++ }); // Large message that will overflow the shared space. let control = new Uint8Array([42]); - let response = DenoCore.dispatch(control); + let response = Deno.core.dispatch(control); assert(response instanceof Uint8Array); assert(response.length == 100 * 1024 * 1024); assert(response[0] == 99); @@ -874,14 +874,14 @@ mod tests { "overflow_req_async.js", r#" let asyncRecv = 0; - DenoCore.setAsyncHandler((buf) => { + Deno.core.setAsyncHandler((buf) => { assert(buf.byteLength === 1); assert(buf[0] === 43); asyncRecv++; }); // Large message that will overflow the shared space. let control = new Uint8Array(100 * 1024 * 1024); - let response = DenoCore.dispatch(control); + let response = Deno.core.dispatch(control); // Async messages always have null response. assert(response == null); assert(asyncRecv == 0); @@ -901,14 +901,14 @@ mod tests { "overflow_res_async.js", r#" let asyncRecv = 0; - DenoCore.setAsyncHandler((buf) => { + Deno.core.setAsyncHandler((buf) => { assert(buf.byteLength === 100 * 1024 * 1024); assert(buf[0] === 4); asyncRecv++; }); // Large message that will overflow the shared space. let control = new Uint8Array([42]); - let response = DenoCore.dispatch(control); + let response = Deno.core.dispatch(control); assert(response == null); assert(asyncRecv == 0); "#, @@ -919,27 +919,27 @@ mod tests { } #[test] - fn overflow_res_multiple_dispatch_async() { + fn overflow_res_multipledispatch_async() { // TODO(ry) This test is quite slow due to memcpy-ing 100MB into JS. We // should optimize this. let mut isolate = TestBehavior::setup(TestBehaviorMode::OverflowResAsync); js_check(isolate.execute( - "overflow_res_multiple_dispatch_async.js", + "overflow_res_multipledispatch_async.js", r#" let asyncRecv = 0; - DenoCore.setAsyncHandler((buf) => { + Deno.core.setAsyncHandler((buf) => { assert(buf.byteLength === 100 * 1024 * 1024); assert(buf[0] === 4); asyncRecv++; }); // Large message that will overflow the shared space. let control = new Uint8Array([42]); - let response = DenoCore.dispatch(control); + let response = Deno.core.dispatch(control); assert(response == null); assert(asyncRecv == 0); // Dispatch another message to verify that pending ops // are done even if shared space overflows - DenoCore.dispatch(control); + Deno.core.dispatch(control); "#, )); assert_eq!(isolate.behavior.dispatch_count, 2); diff --git a/core/shared_queue.js b/core/shared_queue.js index 6c9a0326f..02aded748 100644 --- a/core/shared_queue.js +++ b/core/shared_queue.js @@ -1,5 +1,8 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. + (window => { + const GLOBAL_NAMESPACE = "Deno"; + const CORE_NAMESPACE = "core"; const MAX_RECORDS = 100; const INDEX_NUM_RECORDS = 0; const INDEX_NUM_SHIFTED_OFF = 1; @@ -8,8 +11,11 @@ const INDEX_RECORDS = 3 + MAX_RECORDS; const HEAD_INIT = 4 * INDEX_RECORDS; - let sharedBytes = null; - let shared32 = null; + const Deno = window[GLOBAL_NAMESPACE]; + const core = Deno[CORE_NAMESPACE]; + + let sharedBytes; + let shared32; function assert(cond) { if (!cond) { @@ -92,10 +98,12 @@ reset(); } + assert(off != null); + assert(end != null); return sharedBytes.subarray(off, end); } - let asyncHandler = null; + let asyncHandler; function setAsyncHandler(cb) { assert(asyncHandler == null); asyncHandler = cb; @@ -117,23 +125,22 @@ assert(shared32 == null); sharedBytes = new Uint8Array(shared); shared32 = new Int32Array(shared); - // Callers should not call libdeno.recv, use setAsyncHandler. - libdeno.recv(handleAsyncMsgFromRust); + // Callers should not call Deno.core.recv, use setAsyncHandler. + window.Deno.core.recv(handleAsyncMsgFromRust); } function dispatch(control, zeroCopy = null) { // First try to push control to shared. const success = push(control); - // If successful, don't use first argument of libdeno.send. + // If successful, don't use first argument of core.send. const arg0 = success ? null : control; - return libdeno.send(arg0, zeroCopy); + return window.Deno.core.send(arg0, zeroCopy); } - assert(!window["DenoCore"]); - window["DenoCore"] = { + const denoCore = { setAsyncHandler, dispatch, - shared: { + sharedQueue: { head, numRecords, size, @@ -143,5 +150,9 @@ } }; - init(libdeno.shared); -})(this); + assert(window[GLOBAL_NAMESPACE] != null); + assert(window[GLOBAL_NAMESPACE][CORE_NAMESPACE] != null); + Object.assign(core, denoCore); + + init(Deno.core.shared); +})(globalThis); diff --git a/core/shared_queue_test.js b/core/shared_queue_test.js index 817bb3bcd..75a877930 100644 --- a/core/shared_queue_test.js +++ b/core/shared_queue_test.js @@ -7,7 +7,7 @@ function assert(cond) { } function main() { - const q = DenoCore.shared; + const q = Deno.core.sharedQueue; let h = q.head(); assert(h > 0); @@ -56,7 +56,7 @@ function main() { assert(q.numRecords() == 0); assert(q.size() == 0); - libdeno.print("shared_queue_test.js ok\n"); + Deno.core.print("shared_queue_test.js ok\n"); } main(); |