2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-04-21 12:16:55 -04:00
|
|
|
/*
|
|
|
|
SharedQueue Binary Layout
|
|
|
|
+-------------------------------+-------------------------------+
|
|
|
|
| NUM_RECORDS (32) |
|
|
|
|
+---------------------------------------------------------------+
|
|
|
|
| NUM_SHIFTED_OFF (32) |
|
|
|
|
+---------------------------------------------------------------+
|
|
|
|
| HEAD (32) |
|
|
|
|
+---------------------------------------------------------------+
|
|
|
|
| OFFSETS (32) |
|
|
|
|
+---------------------------------------------------------------+
|
|
|
|
| RECORD_ENDS (*MAX_RECORDS) ...
|
|
|
|
+---------------------------------------------------------------+
|
|
|
|
| RECORDS (*MAX_RECORDS) ...
|
|
|
|
+---------------------------------------------------------------+
|
|
|
|
*/
|
2019-03-26 08:22:07 -04:00
|
|
|
|
2019-04-24 21:43:06 -04:00
|
|
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
|
|
|
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;
|
2019-08-07 14:02:29 -04:00
|
|
|
const INDEX_RECORDS = INDEX_OFFSETS + 2 * MAX_RECORDS;
|
2019-03-14 19:17:52 -04:00
|
|
|
const HEAD_INIT = 4 * INDEX_RECORDS;
|
|
|
|
|
2019-08-05 07:23:41 -04:00
|
|
|
// Available on start due to bindings.
|
2019-03-26 08:22:07 -04:00
|
|
|
const Deno = window[GLOBAL_NAMESPACE];
|
|
|
|
const core = Deno[CORE_NAMESPACE];
|
2019-08-05 07:23:41 -04:00
|
|
|
// 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.
|
2019-03-26 08:22:07 -04:00
|
|
|
|
|
|
|
let sharedBytes;
|
|
|
|
let shared32;
|
2019-04-24 21:43:06 -04:00
|
|
|
let initialized = false;
|
|
|
|
|
|
|
|
function maybeInit() {
|
|
|
|
if (!initialized) {
|
|
|
|
init();
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
2019-09-07 12:27:18 -04:00
|
|
|
const shared = Deno.core.shared;
|
2019-04-24 21:43:06 -04:00
|
|
|
assert(shared.byteLength > 0);
|
|
|
|
assert(sharedBytes == null);
|
|
|
|
assert(shared32 == null);
|
|
|
|
sharedBytes = new Uint8Array(shared);
|
|
|
|
shared32 = new Int32Array(shared);
|
|
|
|
// Callers should not call Deno.core.recv, use setAsyncHandler.
|
|
|
|
Deno.core.recv(handleAsyncMsgFromRust);
|
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2019-09-30 14:59:44 -04:00
|
|
|
function ops() {
|
2020-01-02 07:48:46 -05:00
|
|
|
// op id 0 is a special value to retrieve the map of registered ops.
|
2019-09-30 14:59:44 -04:00
|
|
|
const opsMapBytes = Deno.core.send(0, new Uint8Array([]), null);
|
|
|
|
const opsMapJson = String.fromCharCode.apply(null, opsMapBytes);
|
|
|
|
return JSON.parse(opsMapJson);
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
function assert(cond) {
|
|
|
|
if (!cond) {
|
|
|
|
throw Error("assert");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
2019-04-24 21:43:06 -04:00
|
|
|
maybeInit();
|
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() {
|
2019-04-24 21:43:06 -04:00
|
|
|
maybeInit();
|
2019-03-14 19:17:52 -04:00
|
|
|
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-08-07 14:02:29 -04:00
|
|
|
function setMeta(index, end, opId) {
|
|
|
|
shared32[INDEX_OFFSETS + 2 * index] = end;
|
|
|
|
shared32[INDEX_OFFSETS + 2 * index + 1] = opId;
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
function getMeta(index) {
|
2019-03-14 19:17:52 -04:00
|
|
|
if (index < numRecords()) {
|
2019-08-07 14:02:29 -04:00
|
|
|
const buf = shared32[INDEX_OFFSETS + 2 * index];
|
|
|
|
const opId = shared32[INDEX_OFFSETS + 2 * index + 1];
|
|
|
|
return [opId, buf];
|
2019-03-14 19:17:52 -04:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getOffset(index) {
|
|
|
|
if (index < numRecords()) {
|
|
|
|
if (index == 0) {
|
|
|
|
return HEAD_INIT;
|
|
|
|
} else {
|
2019-08-07 14:02:29 -04:00
|
|
|
return shared32[INDEX_OFFSETS + 2 * (index - 1)];
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
function push(opId, buf) {
|
2019-09-07 12:27:18 -04:00
|
|
|
const off = head();
|
|
|
|
const end = off + buf.byteLength;
|
|
|
|
const index = numRecords();
|
2019-04-21 12:16:55 -04:00
|
|
|
if (end > shared32.byteLength || index >= MAX_RECORDS) {
|
2019-05-20 12:06:57 -04:00
|
|
|
// console.log("shared_queue.js push fail");
|
2019-03-14 19:17:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-07 14:02:29 -04:00
|
|
|
setMeta(index, end, opId);
|
2019-03-14 19:17:52 -04:00
|
|
|
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() {
|
2019-09-07 12:27:18 -04:00
|
|
|
const 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-08-07 14:02:29 -04:00
|
|
|
const off = getOffset(i);
|
|
|
|
const [opId, end] = getMeta(i);
|
2019-03-14 19:17:52 -04:00
|
|
|
|
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-08-07 14:02:29 -04:00
|
|
|
const buf = sharedBytes.subarray(off, end);
|
|
|
|
return [opId, buf];
|
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) {
|
2019-04-24 21:43:06 -04:00
|
|
|
maybeInit();
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(asyncHandler == null);
|
|
|
|
asyncHandler = cb;
|
|
|
|
}
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
function handleAsyncMsgFromRust(opId, buf) {
|
2019-03-14 19:17:52 -04:00
|
|
|
if (buf) {
|
2019-08-07 14:02:29 -04:00
|
|
|
// This is the overflow_response case of deno::Isolate::poll().
|
|
|
|
asyncHandler(opId, buf);
|
2019-03-14 19:17:52 -04:00
|
|
|
} else {
|
2019-08-07 14:02:29 -04:00
|
|
|
while (true) {
|
2019-09-07 12:27:18 -04:00
|
|
|
const opIdBuf = shift();
|
2019-08-07 14:02:29 -04:00
|
|
|
if (opIdBuf == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
asyncHandler(...opIdBuf);
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
function dispatch(opId, control, zeroCopy = null) {
|
2019-04-24 21:43:06 -04:00
|
|
|
maybeInit();
|
2019-08-07 14:02:29 -04:00
|
|
|
return Deno.core.send(opId, control, 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-04-21 12:16:55 -04:00
|
|
|
MAX_RECORDS,
|
2019-03-14 19:17:52 -04:00
|
|
|
head,
|
|
|
|
numRecords,
|
|
|
|
size,
|
|
|
|
push,
|
|
|
|
reset,
|
|
|
|
shift
|
2019-09-30 14:59:44 -04:00
|
|
|
},
|
|
|
|
ops
|
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);
|
2019-04-24 21:43:06 -04:00
|
|
|
})(this);
|