forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_lib.js
324 lines (284 loc) · 9.09 KB
/
core_lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
/*
SharedQueue Binary Layout
+-------------------------------+-------------------------------+
| NUM_RECORDS (32) |
+---------------------------------------------------------------+
| NUM_SHIFTED_OFF (32) |
+---------------------------------------------------------------+
| HEAD (32) |
+---------------------------------------------------------------+
| OFFSETS (32) |
+---------------------------------------------------------------+
| RECORD_ENDS (*MAX_RECORDS) ...
+---------------------------------------------------------------+
| RECORDS (*MAX_RECORDS) ...
+---------------------------------------------------------------+
*/
/* eslint-disable @typescript-eslint/no-use-before-define */
(window => {
if (Deno && Deno.core.maybeInit === undefined) {
const GLOBAL_NAMESPACE = "Deno";
const OPS_NAMESPACE = "ops";
const CORE_NAMESPACE = "core";
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 = INDEX_OFFSETS + 2 * MAX_RECORDS;
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.
// Async handler registry
const asyncHandlerMap = [];
// SharedQueue state
let sharedBytes;
let shared32;
// Op registry state
let opRecords = {};
const opListeners = {};
let initialized = false;
function maybeInit() {
if (!initialized) {
init();
initialized = true;
}
}
function init() {
const shared = Deno.core.shared;
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);
Deno.core.recvOpReg(handleOpUpdate);
}
function assert(cond) {
if (!cond) {
throw Error("assert");
}
}
function reset() {
maybeInit();
shared32[INDEX_NUM_RECORDS] = 0;
shared32[INDEX_NUM_SHIFTED_OFF] = 0;
shared32[INDEX_HEAD] = HEAD_INIT;
}
function head() {
maybeInit();
return shared32[INDEX_HEAD];
}
function numRecords() {
return shared32[INDEX_NUM_RECORDS];
}
function size() {
return shared32[INDEX_NUM_RECORDS] - shared32[INDEX_NUM_SHIFTED_OFF];
}
// TODO(ry) rename to setMeta
function setMeta(index, end, opId) {
shared32[INDEX_OFFSETS + 2 * index] = end;
shared32[INDEX_OFFSETS + 2 * index + 1] = opId;
}
function getMeta(index) {
if (index < numRecords()) {
const buf = shared32[INDEX_OFFSETS + 2 * index];
const opId = shared32[INDEX_OFFSETS + 2 * index + 1];
return [opId, buf];
} else {
return null;
}
}
function getOffset(index) {
if (index < numRecords()) {
if (index == 0) {
return HEAD_INIT;
} else {
return shared32[INDEX_OFFSETS + 2 * (index - 1)];
}
} else {
return null;
}
}
function push(opId, buf) {
const off = head();
const end = off + buf.byteLength;
const index = numRecords();
if (end > shared32.byteLength || index >= MAX_RECORDS) {
// console.log("shared_queue.js push fail");
return false;
}
setMeta(index, end, opId);
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() {
const i = shared32[INDEX_NUM_SHIFTED_OFF];
if (size() == 0) {
assert(i == 0);
return null;
}
const off = getOffset(i);
const [opId, end] = getMeta(i);
if (size() > 1) {
shared32[INDEX_NUM_SHIFTED_OFF] += 1;
} else {
reset();
}
assert(off != null);
assert(end != null);
const buf = sharedBytes.subarray(off, end);
return [opId, buf];
}
function setAsyncHandler(opId, cb) {
maybeInit();
if (typeof opId === "number") {
asyncHandlerMap[opId] = cb;
}
}
function handleAsyncMsgFromRust(opId, buf) {
if (buf) {
// This is the overflow_response case of deno::Isolate::poll().
asyncHandlerMap[opId](buf);
} else {
while (true) {
const opIdBuf = shift();
if (opIdBuf == null) {
break;
}
asyncHandlerMap[opIdBuf[0]](opIdBuf[1]);
}
}
}
function dispatch(opId, control, zeroCopy = null) {
maybeInit();
return Deno.core.send(opId, control, zeroCopy);
}
// Op registry handlers
function handleOpUpdate(opId, namespace, name) {
// If we recieve a call with no params reset opRecords
if (opId === undefined) {
resetOps();
return;
}
registerOp(opId, namespace, name);
}
function resetOps() {
// Reset records
opRecords = {};
// Call all listeners with undefined
for (const space in opListeners) {
for (const name in opListeners[space]) {
for (const listener of opListeners[space][name]) {
listener(undefined);
}
}
}
}
function registerOp(opId, namespace, name) {
// Ensure namespace exists in object
if (opRecords[namespace] === undefined) {
opRecords[namespace] = {};
}
// Set record to opId
opRecords[namespace][name] = opId;
// Check for listeners
if (opListeners[namespace] !== undefined) {
if (opListeners[namespace][name] !== undefined) {
// Call all listeners with new id
for (const listener of opListeners[namespace][name]) {
listener(opId);
}
}
}
}
// Op registry external backplane
// (stuff that makes the external interface work)
// This part relies on Proxy for custom index handling. I would normally
// avoid Proxy, but I don't think there is a preferable way to do this.
// TODO(afinch7) maybe implement enumerablity, and some others functions?
const namespaceHandler = {
get: (target, prop, _receiver) => {
if (typeof prop === "symbol") {
throw new TypeError("symbol isn't a valid index");
}
// If namespace exists return value of op from namespace (maybe undefined)
if (target.root.ops[target.namespace]) {
return target.root.ops[target.namespace][prop];
}
// Otherwise return undefined
return undefined;
},
set: (target, prop, value, _receiver) => {
if (typeof prop === "symbol") {
throw new TypeError("symbol isn't a valid index");
}
// Init namespace if not present
if (target.root.listeners[target.namespace] === undefined) {
target.root.listeners[target.namespace] = {};
}
// Init op in namespace if not present
if (target.root.listeners[target.namespace][prop] === undefined) {
target.root.listeners[target.namespace][prop] = [];
}
// Notify the listener of the current value.
if (target.root.ops[target.namespace]) {
value(target.root.ops[target.namespace][prop]);
}
// Push our new listener
target.root.listeners[target.namespace][prop].push(value);
return true;
}
};
const rootHandler = {
get: (target, prop, _receiver) => {
const namespaceObject = {
root: target,
namespace: prop.toString()
};
const namespaceProxy = new Proxy(namespaceObject, namespaceHandler);
return namespaceProxy;
}
};
const registryRootObject = {
// This needs to be a accessor since opRecords is let
get ops() {
return opRecords;
},
get listeners() {
return opListeners;
}
};
const registryProxy = new Proxy(registryRootObject, rootHandler);
Object.seal(registryProxy);
const denoCore = {
setAsyncHandler,
dispatch,
maybeInit,
sharedQueue: {
MAX_RECORDS,
head,
numRecords,
size,
push,
reset,
shift
}
};
assert(window[GLOBAL_NAMESPACE] != null);
assert(window[GLOBAL_NAMESPACE][CORE_NAMESPACE] != null);
assert(window[GLOBAL_NAMESPACE][OPS_NAMESPACE] != null);
Object.assign(core, denoCore);
Deno[OPS_NAMESPACE] = registryProxy;
}
})(this);