2019-03-14 19:17:52 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-26 08:22:07 -04:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
(window => {
|
2019-03-26 08:22:07 -04:00
|
|
|
const GLOBAL_NAMESPACE = "Deno";
|
|
|
|
const CORE_NAMESPACE = "core";
|
2019-03-14 19:17:52 -04:00
|
|
|
const MAX_RECORDS = 100;
|
|
|
|
const INDEX_NUM_RECORDS = 0;
|
|
|
|
const INDEX_NUM_SHIFTED_OFF = 1;
|
|
|
|
const INDEX_HEAD = 2;
|
|
|
|
const INDEX_OFFSETS = 3;
|
|
|
|
const INDEX_RECORDS = 3 + MAX_RECORDS;
|
|
|
|
const HEAD_INIT = 4 * INDEX_RECORDS;
|
|
|
|
|
2019-03-26 08:22:07 -04:00
|
|
|
const Deno = window[GLOBAL_NAMESPACE];
|
|
|
|
const core = Deno[CORE_NAMESPACE];
|
|
|
|
|
|
|
|
let sharedBytes;
|
|
|
|
let shared32;
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
function assert(cond) {
|
|
|
|
if (!cond) {
|
|
|
|
throw Error("assert");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
2019-03-15 15:49:41 -04:00
|
|
|
shared32[INDEX_NUM_RECORDS] = 0;
|
|
|
|
shared32[INDEX_NUM_SHIFTED_OFF] = 0;
|
2019-03-14 19:17:52 -04:00
|
|
|
shared32[INDEX_HEAD] = HEAD_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
function head() {
|
|
|
|
return shared32[INDEX_HEAD];
|
|
|
|
}
|
|
|
|
|
|
|
|
function numRecords() {
|
|
|
|
return shared32[INDEX_NUM_RECORDS];
|
|
|
|
}
|
|
|
|
|
2019-03-15 15:49:41 -04:00
|
|
|
function size() {
|
|
|
|
return shared32[INDEX_NUM_RECORDS] - shared32[INDEX_NUM_SHIFTED_OFF];
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
function setEnd(index, end) {
|
|
|
|
shared32[INDEX_OFFSETS + index] = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEnd(index) {
|
|
|
|
if (index < numRecords()) {
|
|
|
|
return shared32[INDEX_OFFSETS + index];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOffset(index) {
|
|
|
|
if (index < numRecords()) {
|
|
|
|
if (index == 0) {
|
|
|
|
return HEAD_INIT;
|
|
|
|
} else {
|
|
|
|
return shared32[INDEX_OFFSETS + index - 1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function push(buf) {
|
|
|
|
let off = head();
|
|
|
|
let end = off + buf.byteLength;
|
|
|
|
let index = numRecords();
|
|
|
|
if (end > shared32.byteLength) {
|
|
|
|
console.log("shared_queue.ts push fail");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
setEnd(index, end);
|
|
|
|
assert(end - off == buf.byteLength);
|
|
|
|
sharedBytes.set(buf, off);
|
|
|
|
shared32[INDEX_NUM_RECORDS] += 1;
|
|
|
|
shared32[INDEX_HEAD] = end;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns null if empty.
|
|
|
|
function shift() {
|
|
|
|
let i = shared32[INDEX_NUM_SHIFTED_OFF];
|
2019-03-15 15:49:41 -04:00
|
|
|
if (size() == 0) {
|
|
|
|
assert(i == 0);
|
2019-03-14 19:17:52 -04:00
|
|
|
return null;
|
|
|
|
}
|
2019-03-15 15:49:41 -04:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
let off = getOffset(i);
|
|
|
|
let end = getEnd(i);
|
|
|
|
|
2019-03-15 15:49:41 -04:00
|
|
|
if (size() > 1) {
|
|
|
|
shared32[INDEX_NUM_SHIFTED_OFF] += 1;
|
|
|
|
} else {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2019-03-26 08:22:07 -04:00
|
|
|
assert(off != null);
|
|
|
|
assert(end != null);
|
2019-03-15 15:49:41 -04:00
|
|
|
return sharedBytes.subarray(off, end);
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-03-26 08:22:07 -04:00
|
|
|
let asyncHandler;
|
2019-03-14 19:17:52 -04:00
|
|
|
function setAsyncHandler(cb) {
|
|
|
|
assert(asyncHandler == null);
|
|
|
|
asyncHandler = cb;
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
function handleAsyncMsgFromRust(buf) {
|
|
|
|
if (buf) {
|
2019-03-14 19:17:52 -04:00
|
|
|
asyncHandler(buf);
|
2019-03-14 19:17:52 -04:00
|
|
|
} else {
|
|
|
|
while ((buf = shift()) != null) {
|
|
|
|
asyncHandler(buf);
|
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function init(shared) {
|
|
|
|
assert(shared.byteLength > 0);
|
|
|
|
assert(sharedBytes == null);
|
|
|
|
assert(shared32 == null);
|
|
|
|
sharedBytes = new Uint8Array(shared);
|
|
|
|
shared32 = new Int32Array(shared);
|
2019-03-26 08:22:07 -04:00
|
|
|
// Callers should not call Deno.core.recv, use setAsyncHandler.
|
|
|
|
window.Deno.core.recv(handleAsyncMsgFromRust);
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
function dispatch(control, zeroCopy = null) {
|
|
|
|
// First try to push control to shared.
|
|
|
|
const success = push(control);
|
2019-03-26 08:22:07 -04:00
|
|
|
// If successful, don't use first argument of core.send.
|
2019-03-14 19:17:52 -04:00
|
|
|
const arg0 = success ? null : control;
|
2019-03-26 08:22:07 -04:00
|
|
|
return window.Deno.core.send(arg0, zeroCopy);
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-03-26 08:22:07 -04:00
|
|
|
const denoCore = {
|
2019-03-14 19:17:52 -04:00
|
|
|
setAsyncHandler,
|
|
|
|
dispatch,
|
2019-03-26 08:22:07 -04:00
|
|
|
sharedQueue: {
|
2019-03-14 19:17:52 -04:00
|
|
|
head,
|
|
|
|
numRecords,
|
|
|
|
size,
|
|
|
|
push,
|
|
|
|
reset,
|
|
|
|
shift
|
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
};
|
|
|
|
|
2019-03-26 08:22:07 -04:00
|
|
|
assert(window[GLOBAL_NAMESPACE] != null);
|
|
|
|
assert(window[GLOBAL_NAMESPACE][CORE_NAMESPACE] != null);
|
|
|
|
Object.assign(core, denoCore);
|
|
|
|
|
|
|
|
init(Deno.core.shared);
|
|
|
|
})(globalThis);
|