0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

Clean up core/shared_queue.js (#5237)

This commit is contained in:
Ryan Dahl 2020-05-12 11:09:28 -04:00 committed by GitHub
parent 35e8bc8de6
commit 593c3aa857
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 29 deletions

View file

@ -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;
@ -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;
@ -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);
}
@ -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,
@ -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);

View file

@ -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()
@ -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!();
}