Skip to content

Commit

Permalink
Clean up core/shared_queue.js (denoland#5237)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 12, 2020
1 parent 35e8bc8 commit 593c3aa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
30 changes: 11 additions & 19 deletions core/shared_queue.js → core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ SharedQueue Binary Layout
/* eslint-disable @typescript-eslint/no-use-before-define */

((window) => {
const GLOBAL_NAMESPACE = "Deno";
const CORE_NAMESPACE = "core";
const MAX_RECORDS = 100;
const INDEX_NUM_RECORDS = 0;
const INDEX_NUM_SHIFTED_OFF = 1;
Expand All @@ -30,11 +28,8 @@ SharedQueue Binary Layout
const HEAD_INIT = 4 * INDEX_RECORDS;

// Available on start due to bindings.
const Deno = window[GLOBAL_NAMESPACE];
const core = Deno[CORE_NAMESPACE];
// Warning: DO NOT use window.Deno after this point.
// It is possible that the Deno namespace has been deleted.
// Use the above local Deno and core variable instead.
const core = window.Deno.core;
const { recv, send } = core;

let sharedBytes;
let shared32;
Expand All @@ -51,20 +46,20 @@ SharedQueue Binary Layout
}

function init() {
const shared = Deno.core.shared;
const shared = core.shared;
assert(shared.byteLength > 0);
assert(sharedBytes == null);
assert(shared32 == null);
sharedBytes = new Uint8Array(shared);
shared32 = new Int32Array(shared);
asyncHandlers = [];
// Callers should not call Deno.core.recv, use setAsyncHandler.
Deno.core.recv(handleAsyncMsgFromRust);
// Callers should not call core.recv, use setAsyncHandler.
recv(handleAsyncMsgFromRust);
}

function ops() {
// op id 0 is a special value to retrieve the map of registered ops.
const opsMapBytes = Deno.core.send(0, new Uint8Array([]), null);
const opsMapBytes = send(0, new Uint8Array([]), null);
const opsMapJson = String.fromCharCode.apply(null, opsMapBytes);
return JSON.parse(opsMapJson);
}
Expand Down Expand Up @@ -187,12 +182,14 @@ SharedQueue Binary Layout
}

function dispatch(opId, control, zeroCopy = null) {
return Deno.core.send(opId, control, zeroCopy);
return send(opId, control, zeroCopy);
}

const denoCore = {
Object.assign(window.Deno.core, {
setAsyncHandler,
dispatch,
ops,
// sharedQueue is private but exposed for testing.
sharedQueue: {
MAX_RECORDS,
head,
Expand All @@ -202,10 +199,5 @@ SharedQueue Binary Layout
reset,
shift,
},
ops,
};

assert(window[GLOBAL_NAMESPACE] != null);
assert(window[GLOBAL_NAMESPACE][CORE_NAMESPACE] != null);
Object.assign(core, denoCore);
});
})(this);
File renamed without changes.
13 changes: 3 additions & 10 deletions core/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,7 @@ impl CoreIsolate {
pub(crate) fn shared_init(&mut self) {
if self.needs_init {
self.needs_init = false;
js_check(
self.execute("shared_queue.js", include_str!("shared_queue.js")),
);
js_check(self.execute("core.js", include_str!("core.js")));
// Maybe execute the startup script.
if let Some(s) = self.startup_script.take() {
self.execute(&s.filename, &s.source).unwrap()
Expand Down Expand Up @@ -1126,15 +1124,10 @@ pub mod tests {
}

#[test]
fn test_js() {
fn core_test_js() {
run_in_task(|mut cx| {
let (mut isolate, _dispatch_count) = setup(Mode::Async);
js_check(
isolate.execute(
"shared_queue_test.js",
include_str!("shared_queue_test.js"),
),
);
js_check(isolate.execute("core_test.js", include_str!("core_test.js")));
if let Poll::Ready(Err(_)) = isolate.poll_unpin(&mut cx) {
unreachable!();
}
Expand Down

0 comments on commit 593c3aa

Please sign in to comment.