Skip to content

Commit

Permalink
namespace reorg: libdeno and DenoCore to Deno.core (denoland#1998)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored and ry committed Mar 26, 2019
1 parent ed2977d commit c43cfed
Show file tree
Hide file tree
Showing 26 changed files with 178 additions and 146 deletions.
1 change: 0 additions & 1 deletion BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ ts_sources = [
"js/globals.ts",
"js/headers.ts",
"js/io.ts",
"js/libdeno.ts",
"js/lib.web_assembly.d.ts",
"js/location.ts",
"js/main.ts",
Expand Down
6 changes: 3 additions & 3 deletions cli/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ fn empty_buf() -> Buf {
}

/// Processes raw messages from JavaScript.
/// This functions invoked every time libdeno.send() is called.
/// control corresponds to the first argument of libdeno.send().
/// data corresponds to the second argument of libdeno.send().
/// This functions invoked every time Deno.core.dispatch() is called.
/// control corresponds to the first argument of Deno.core.dispatch().
/// data corresponds to the second argument of Deno.core.dispatch().
pub fn dispatch_all(
sc: &IsolateStateContainer,
control: &[u8],
Expand Down
25 changes: 25 additions & 0 deletions core/core.d.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}
12 changes: 6 additions & 6 deletions core/http_bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -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> */
Expand Down Expand Up @@ -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:https://127.0.0.1:4544/ rid = ${listenerRid}\n`);
Deno.core.print(`listening http:https://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);
Expand Down
68 changes: 34 additions & 34 deletions core/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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> {
Expand Down Expand Up @@ -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) };

Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand 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();
"#,
Expand All @@ -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);
Expand Down Expand Up @@ -685,7 +685,7 @@ mod tests {
"setup2.js",
r#"
let nrecv = 0;
DenoCore.setAsyncHandler((buf) => {
Deno.core.setAsyncHandler((buf) => {
nrecv++;
});
"#,
Expand All @@ -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);
"#,
));
Expand All @@ -707,7 +707,7 @@ mod tests {
"check2.js",
r#"
assert(nrecv == 1);
libdeno.send(control);
Deno.core.send(control);
assert(nrecv == 1);
"#,
));
Expand All @@ -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++;
Expand All @@ -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);
"#,
));
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
"#,
Expand All @@ -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);
Expand Down
Loading

0 comments on commit c43cfed

Please sign in to comment.