2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
// Removes the `__proto__` for security reasons. This intentionally makes
|
|
|
|
// Deno non compliant with ECMA-262 Annex B.2.2.1
|
|
|
|
//
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
delete Object.prototype.__proto__;
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
const core = Deno.core;
|
|
|
|
const util = window.__bootstrap.util;
|
|
|
|
const eventTarget = window.__bootstrap.eventTarget;
|
2020-10-20 00:05:42 -04:00
|
|
|
const globalInterfaces = window.__bootstrap.globalInterfaces;
|
2021-01-07 13:06:08 -05:00
|
|
|
const location = window.__bootstrap.location;
|
2020-07-19 13:49:44 -04:00
|
|
|
const build = window.__bootstrap.build;
|
|
|
|
const version = window.__bootstrap.version;
|
|
|
|
const errorStack = window.__bootstrap.errorStack;
|
|
|
|
const os = window.__bootstrap.os;
|
|
|
|
const timers = window.__bootstrap.timers;
|
|
|
|
const Console = window.__bootstrap.console.Console;
|
|
|
|
const worker = window.__bootstrap.worker;
|
|
|
|
const signals = window.__bootstrap.signals;
|
2021-03-12 15:23:59 -05:00
|
|
|
const internals = window.__bootstrap.internals;
|
2020-07-19 13:49:44 -04:00
|
|
|
const performance = window.__bootstrap.performance;
|
|
|
|
const crypto = window.__bootstrap.crypto;
|
|
|
|
const url = window.__bootstrap.url;
|
|
|
|
const headers = window.__bootstrap.headers;
|
|
|
|
const streams = window.__bootstrap.streams;
|
2020-08-11 08:00:53 -04:00
|
|
|
const fileReader = window.__bootstrap.fileReader;
|
2021-03-01 05:31:13 -05:00
|
|
|
const webgpu = window.__bootstrap.webgpu;
|
2020-09-05 10:39:25 -04:00
|
|
|
const webSocket = window.__bootstrap.webSocket;
|
2021-02-04 09:05:36 -05:00
|
|
|
const file = window.__bootstrap.file;
|
2020-07-19 13:49:44 -04:00
|
|
|
const fetch = window.__bootstrap.fetch;
|
2020-10-13 09:31:59 -04:00
|
|
|
const prompt = window.__bootstrap.prompt;
|
2020-07-19 13:49:44 -04:00
|
|
|
const denoNs = window.__bootstrap.denoNs;
|
|
|
|
const denoNsUnstable = window.__bootstrap.denoNsUnstable;
|
2020-08-07 16:47:18 -04:00
|
|
|
const errors = window.__bootstrap.errors.errors;
|
2021-03-08 07:27:49 -05:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
2020-12-07 15:22:58 -05:00
|
|
|
const { defineEventHandler } = window.__bootstrap.webUtil;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
let windowIsClosing = false;
|
|
|
|
|
|
|
|
function windowClose() {
|
|
|
|
if (!windowIsClosing) {
|
|
|
|
windowIsClosing = true;
|
|
|
|
// Push a macrotask to exit after a promise resolve.
|
|
|
|
// This is not perfect, but should be fine for first pass.
|
|
|
|
Promise.resolve().then(() =>
|
|
|
|
timers.setTimeout.call(
|
|
|
|
null,
|
|
|
|
() => {
|
|
|
|
// This should be fine, since only Window/MainWorker has .close()
|
|
|
|
os.exit(0);
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
|
|
function workerClose() {
|
|
|
|
if (isClosing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isClosing = true;
|
|
|
|
opCloseWorker();
|
|
|
|
}
|
|
|
|
|
2020-10-23 07:19:37 -04:00
|
|
|
// TODO(bartlomieju): remove these functions
|
2020-07-19 13:49:44 -04:00
|
|
|
// Stuff for workers
|
|
|
|
const onmessage = () => {};
|
|
|
|
const onerror = () => {};
|
|
|
|
|
|
|
|
function postMessage(data) {
|
|
|
|
const dataJson = JSON.stringify(data);
|
|
|
|
const dataIntArray = encoder.encode(dataJson);
|
|
|
|
opPostMessage(dataIntArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
let isClosing = false;
|
|
|
|
async function workerMessageRecvCallback(data) {
|
2020-09-05 10:39:25 -04:00
|
|
|
const msgEvent = new MessageEvent("message", {
|
2020-07-19 13:49:44 -04:00
|
|
|
cancelable: false,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (globalThis["onmessage"]) {
|
|
|
|
const result = globalThis.onmessage(msgEvent);
|
|
|
|
if (result && "then" in result) {
|
|
|
|
await result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
globalThis.dispatchEvent(msgEvent);
|
|
|
|
} catch (e) {
|
|
|
|
let handled = false;
|
|
|
|
|
|
|
|
const errorEvent = new ErrorEvent("error", {
|
|
|
|
cancelable: true,
|
|
|
|
message: e.message,
|
|
|
|
lineno: e.lineNumber ? e.lineNumber + 1 : undefined,
|
|
|
|
colno: e.columnNumber ? e.columnNumber + 1 : undefined,
|
|
|
|
filename: e.fileName,
|
|
|
|
error: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (globalThis["onerror"]) {
|
|
|
|
const ret = globalThis.onerror(
|
|
|
|
e.message,
|
|
|
|
e.fileName,
|
|
|
|
e.lineNumber,
|
|
|
|
e.columnNumber,
|
|
|
|
e,
|
|
|
|
);
|
|
|
|
handled = ret === true;
|
|
|
|
}
|
|
|
|
|
|
|
|
globalThis.dispatchEvent(errorEvent);
|
|
|
|
if (errorEvent.defaultPrevented) {
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function opPostMessage(data) {
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("op_worker_post_message", null, data);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opCloseWorker() {
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("op_worker_close");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opMainModule() {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_main_module");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
function runtimeStart(runtimeOptions, source) {
|
2021-03-18 09:10:27 -04:00
|
|
|
core.ops();
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
core.setMacrotaskCallback(timers.handleTimerMacrotask);
|
|
|
|
version.setVersions(
|
|
|
|
runtimeOptions.denoVersion,
|
|
|
|
runtimeOptions.v8Version,
|
|
|
|
runtimeOptions.tsVersion,
|
|
|
|
);
|
|
|
|
build.setBuildInfo(runtimeOptions.target);
|
|
|
|
util.setLogDebug(runtimeOptions.debugFlag, source);
|
2020-12-01 17:33:44 -05:00
|
|
|
// TODO(bartlomieju): a very crude way to disable
|
|
|
|
// source mapping of errors. This condition is true
|
|
|
|
// only for compiled standalone binaries.
|
2020-12-10 08:45:41 -05:00
|
|
|
let prepareStackTrace;
|
2020-12-11 12:49:26 -05:00
|
|
|
if (runtimeOptions.applySourceMaps) {
|
2020-12-10 08:45:41 -05:00
|
|
|
prepareStackTrace = core.createPrepareStackTrace(
|
|
|
|
errorStack.opApplySourceMap,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
prepareStackTrace = core.createPrepareStackTrace();
|
2020-12-01 17:33:44 -05:00
|
|
|
}
|
2020-12-10 08:45:41 -05:00
|
|
|
Error.prepareStackTrace = prepareStackTrace;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-08-07 16:47:18 -04:00
|
|
|
function registerErrors() {
|
|
|
|
core.registerErrorClass("NotFound", errors.NotFound);
|
|
|
|
core.registerErrorClass("PermissionDenied", errors.PermissionDenied);
|
|
|
|
core.registerErrorClass("ConnectionRefused", errors.ConnectionRefused);
|
|
|
|
core.registerErrorClass("ConnectionReset", errors.ConnectionReset);
|
|
|
|
core.registerErrorClass("ConnectionAborted", errors.ConnectionAborted);
|
|
|
|
core.registerErrorClass("NotConnected", errors.NotConnected);
|
|
|
|
core.registerErrorClass("AddrInUse", errors.AddrInUse);
|
|
|
|
core.registerErrorClass("AddrNotAvailable", errors.AddrNotAvailable);
|
|
|
|
core.registerErrorClass("BrokenPipe", errors.BrokenPipe);
|
|
|
|
core.registerErrorClass("AlreadyExists", errors.AlreadyExists);
|
|
|
|
core.registerErrorClass("InvalidData", errors.InvalidData);
|
|
|
|
core.registerErrorClass("TimedOut", errors.TimedOut);
|
|
|
|
core.registerErrorClass("Interrupted", errors.Interrupted);
|
|
|
|
core.registerErrorClass("WriteZero", errors.WriteZero);
|
|
|
|
core.registerErrorClass("UnexpectedEof", errors.UnexpectedEof);
|
|
|
|
core.registerErrorClass("BadResource", errors.BadResource);
|
|
|
|
core.registerErrorClass("Http", errors.Http);
|
|
|
|
core.registerErrorClass("Busy", errors.Busy);
|
2020-08-25 18:22:15 -04:00
|
|
|
core.registerErrorClass("NotSupported", errors.NotSupported);
|
|
|
|
core.registerErrorClass("Error", Error);
|
|
|
|
core.registerErrorClass("RangeError", RangeError);
|
|
|
|
core.registerErrorClass("ReferenceError", ReferenceError);
|
|
|
|
core.registerErrorClass("SyntaxError", SyntaxError);
|
|
|
|
core.registerErrorClass("TypeError", TypeError);
|
|
|
|
core.registerErrorClass("URIError", URIError);
|
2021-03-01 05:31:13 -05:00
|
|
|
core.registerErrorClass(
|
|
|
|
"DOMExceptionOperationError",
|
|
|
|
DOMException,
|
|
|
|
"OperationError",
|
|
|
|
);
|
2020-08-07 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
2021-03-08 07:27:49 -05:00
|
|
|
class Navigator {
|
|
|
|
constructor() {
|
|
|
|
webidl.illegalConstructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Symbol.for("Deno.customInspect")](inspect) {
|
|
|
|
return `${this.constructor.name} ${inspect({})}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const navigator = webidl.createBranded(Navigator);
|
|
|
|
|
|
|
|
Object.defineProperties(Navigator.prototype, {
|
|
|
|
gpu: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
webidl.assertBranded(this, Navigator);
|
|
|
|
return webgpu.gpu;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
class WorkerNavigator {
|
|
|
|
constructor() {
|
|
|
|
webidl.illegalConstructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Symbol.for("Deno.customInspect")](inspect) {
|
|
|
|
return `${this.constructor.name} ${inspect({})}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const workerNavigator = webidl.createBranded(WorkerNavigator);
|
|
|
|
|
|
|
|
Object.defineProperties(WorkerNavigator.prototype, {
|
|
|
|
gpu: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
webidl.assertBranded(this, WorkerNavigator);
|
|
|
|
return webgpu.gpu;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope
|
2020-09-16 12:41:01 -04:00
|
|
|
const windowOrWorkerGlobalScope = {
|
2021-02-04 09:05:36 -05:00
|
|
|
Blob: util.nonEnumerable(file.Blob),
|
2020-07-19 13:49:44 -04:00
|
|
|
ByteLengthQueuingStrategy: util.nonEnumerable(
|
2020-09-18 09:20:55 -04:00
|
|
|
streams.ByteLengthQueuingStrategy,
|
2020-07-19 13:49:44 -04:00
|
|
|
),
|
2020-09-16 12:41:01 -04:00
|
|
|
CloseEvent: util.nonEnumerable(CloseEvent),
|
2020-07-19 13:49:44 -04:00
|
|
|
CountQueuingStrategy: util.nonEnumerable(
|
2020-09-18 09:20:55 -04:00
|
|
|
streams.CountQueuingStrategy,
|
2020-07-19 13:49:44 -04:00
|
|
|
),
|
|
|
|
CustomEvent: util.nonEnumerable(CustomEvent),
|
|
|
|
DOMException: util.nonEnumerable(DOMException),
|
|
|
|
ErrorEvent: util.nonEnumerable(ErrorEvent),
|
|
|
|
Event: util.nonEnumerable(Event),
|
|
|
|
EventTarget: util.nonEnumerable(EventTarget),
|
2021-02-04 09:05:36 -05:00
|
|
|
File: util.nonEnumerable(file.File),
|
2020-09-16 12:41:01 -04:00
|
|
|
FileReader: util.nonEnumerable(fileReader.FileReader),
|
2020-09-18 09:20:55 -04:00
|
|
|
FormData: util.nonEnumerable(fetch.FormData),
|
2020-09-16 12:41:01 -04:00
|
|
|
Headers: util.nonEnumerable(headers.Headers),
|
|
|
|
MessageEvent: util.nonEnumerable(MessageEvent),
|
2020-07-19 13:49:44 -04:00
|
|
|
Performance: util.nonEnumerable(performance.Performance),
|
|
|
|
PerformanceEntry: util.nonEnumerable(performance.PerformanceEntry),
|
|
|
|
PerformanceMark: util.nonEnumerable(performance.PerformanceMark),
|
|
|
|
PerformanceMeasure: util.nonEnumerable(performance.PerformanceMeasure),
|
2020-09-18 10:01:50 -04:00
|
|
|
ProgressEvent: util.nonEnumerable(ProgressEvent),
|
2020-09-16 12:41:01 -04:00
|
|
|
ReadableStream: util.nonEnumerable(streams.ReadableStream),
|
2021-01-14 16:57:19 -05:00
|
|
|
ReadableStreamDefaultReader: util.nonEnumerable(
|
|
|
|
streams.ReadableStreamDefaultReader,
|
|
|
|
),
|
2020-09-18 09:20:55 -04:00
|
|
|
Request: util.nonEnumerable(fetch.Request),
|
2020-09-16 12:41:01 -04:00
|
|
|
Response: util.nonEnumerable(fetch.Response),
|
2020-07-19 13:49:44 -04:00
|
|
|
TextDecoder: util.nonEnumerable(TextDecoder),
|
|
|
|
TextEncoder: util.nonEnumerable(TextEncoder),
|
|
|
|
TransformStream: util.nonEnumerable(streams.TransformStream),
|
|
|
|
URL: util.nonEnumerable(url.URL),
|
|
|
|
URLSearchParams: util.nonEnumerable(url.URLSearchParams),
|
2020-09-16 12:41:01 -04:00
|
|
|
WebSocket: util.nonEnumerable(webSocket.WebSocket),
|
2020-07-19 13:49:44 -04:00
|
|
|
Worker: util.nonEnumerable(worker.Worker),
|
|
|
|
WritableStream: util.nonEnumerable(streams.WritableStream),
|
2021-01-14 16:57:19 -05:00
|
|
|
WritableStreamDefaultWriter: util.nonEnumerable(
|
|
|
|
streams.WritableStreamDefaultWriter,
|
|
|
|
),
|
2020-09-16 12:41:01 -04:00
|
|
|
atob: util.writable(atob),
|
|
|
|
btoa: util.writable(btoa),
|
|
|
|
clearInterval: util.writable(timers.clearInterval),
|
|
|
|
clearTimeout: util.writable(timers.clearTimeout),
|
2021-03-18 14:25:25 -04:00
|
|
|
console: util.writable(
|
|
|
|
new Console((msg, level) => core.print(msg, level > 1)),
|
|
|
|
),
|
2020-09-16 12:41:01 -04:00
|
|
|
crypto: util.readOnly(crypto),
|
|
|
|
fetch: util.writable(fetch.fetch),
|
2020-09-19 17:30:59 -04:00
|
|
|
performance: util.writable(performance.performance),
|
2020-09-16 12:41:01 -04:00
|
|
|
setInterval: util.writable(timers.setInterval),
|
|
|
|
setTimeout: util.writable(timers.setTimeout),
|
2021-03-01 05:31:13 -05:00
|
|
|
|
|
|
|
GPU: util.nonEnumerable(webgpu.GPU),
|
|
|
|
GPUAdapter: util.nonEnumerable(webgpu.GPUAdapter),
|
|
|
|
GPUAdapterLimits: util.nonEnumerable(webgpu.GPUAdapterLimits),
|
|
|
|
GPUAdapterFeatures: util.nonEnumerable(webgpu.GPUAdapterFeatures),
|
|
|
|
GPUDevice: util.nonEnumerable(webgpu.GPUDevice),
|
|
|
|
GPUQueue: util.nonEnumerable(webgpu.GPUQueue),
|
|
|
|
GPUBuffer: util.nonEnumerable(webgpu.GPUBuffer),
|
|
|
|
GPUBufferUsage: util.nonEnumerable(webgpu.GPUBufferUsage),
|
|
|
|
GPUMapMode: util.nonEnumerable(webgpu.GPUMapMode),
|
|
|
|
GPUTexture: util.nonEnumerable(webgpu.GPUTexture),
|
|
|
|
GPUTextureUsage: util.nonEnumerable(webgpu.GPUTextureUsage),
|
|
|
|
GPUTextureView: util.nonEnumerable(webgpu.GPUTextureView),
|
|
|
|
GPUSampler: util.nonEnumerable(webgpu.GPUSampler),
|
|
|
|
GPUBindGroupLayout: util.nonEnumerable(webgpu.GPUBindGroupLayout),
|
|
|
|
GPUPipelineLayout: util.nonEnumerable(webgpu.GPUPipelineLayout),
|
|
|
|
GPUBindGroup: util.nonEnumerable(webgpu.GPUBindGroup),
|
|
|
|
GPUShaderModule: util.nonEnumerable(webgpu.GPUShaderModule),
|
|
|
|
GPUShaderStage: util.nonEnumerable(webgpu.GPUShaderStage),
|
|
|
|
GPUComputePipeline: util.nonEnumerable(webgpu.GPUComputePipeline),
|
|
|
|
GPURenderPipeline: util.nonEnumerable(webgpu.GPURenderPipeline),
|
|
|
|
GPUColorWrite: util.nonEnumerable(webgpu.GPUColorWrite),
|
|
|
|
GPUCommandEncoder: util.nonEnumerable(webgpu.GPUCommandEncoder),
|
|
|
|
GPURenderPassEncoder: util.nonEnumerable(webgpu.GPURenderPassEncoder),
|
|
|
|
GPUComputePassEncoder: util.nonEnumerable(webgpu.GPUComputePassEncoder),
|
|
|
|
GPUCommandBuffer: util.nonEnumerable(webgpu.GPUCommandBuffer),
|
|
|
|
GPURenderBundleEncoder: util.nonEnumerable(webgpu.GPURenderBundleEncoder),
|
|
|
|
GPURenderBundle: util.nonEnumerable(webgpu.GPURenderBundle),
|
|
|
|
GPUQuerySet: util.nonEnumerable(webgpu.GPUQuerySet),
|
|
|
|
GPUOutOfMemoryError: util.nonEnumerable(webgpu.GPUOutOfMemoryError),
|
|
|
|
GPUValidationError: util.nonEnumerable(webgpu.GPUValidationError),
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
|
2021-01-10 12:49:45 -05:00
|
|
|
// The console seems to be the only one that should be writable and non-enumerable
|
|
|
|
// thus we don't have a unique helper for it. If other properties follow the same
|
|
|
|
// structure, it might be worth it to define a helper in `util`
|
|
|
|
windowOrWorkerGlobalScope.console.enumerable = false;
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
const mainRuntimeGlobalProperties = {
|
2021-01-17 10:28:54 -05:00
|
|
|
Location: location.locationConstructorDescriptor,
|
|
|
|
location: location.locationDescriptor,
|
2020-10-20 00:05:42 -04:00
|
|
|
Window: globalInterfaces.windowConstructorDescriptor,
|
2020-07-19 13:49:44 -04:00
|
|
|
window: util.readOnly(globalThis),
|
|
|
|
self: util.readOnly(globalThis),
|
2021-03-08 07:27:49 -05:00
|
|
|
Navigator: util.nonEnumerable(Navigator),
|
|
|
|
navigator: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: () => navigator,
|
|
|
|
},
|
2020-07-19 13:49:44 -04:00
|
|
|
// TODO(bartlomieju): from MDN docs (https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope)
|
|
|
|
// it seems those two properties should be available to workers as well
|
|
|
|
onload: util.writable(null),
|
|
|
|
onunload: util.writable(null),
|
|
|
|
close: util.writable(windowClose),
|
|
|
|
closed: util.getterOnly(() => windowIsClosing),
|
2020-10-13 09:31:59 -04:00
|
|
|
alert: util.writable(prompt.alert),
|
|
|
|
confirm: util.writable(prompt.confirm),
|
|
|
|
prompt: util.writable(prompt.prompt),
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const workerRuntimeGlobalProperties = {
|
2021-01-17 10:28:54 -05:00
|
|
|
WorkerLocation: location.workerLocationConstructorDescriptor,
|
|
|
|
location: location.workerLocationDescriptor,
|
2020-10-20 00:05:42 -04:00
|
|
|
WorkerGlobalScope: globalInterfaces.workerGlobalScopeConstructorDescriptor,
|
|
|
|
DedicatedWorkerGlobalScope:
|
|
|
|
globalInterfaces.dedicatedWorkerGlobalScopeConstructorDescriptor,
|
2021-03-08 07:27:49 -05:00
|
|
|
WorkerNavigator: util.nonEnumerable(WorkerNavigator),
|
|
|
|
navigator: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: () => workerNavigator,
|
|
|
|
},
|
2020-07-19 13:49:44 -04:00
|
|
|
self: util.readOnly(globalThis),
|
|
|
|
onmessage: util.writable(onmessage),
|
|
|
|
onerror: util.writable(onerror),
|
2021-01-16 18:32:59 -05:00
|
|
|
// TODO(bartlomieju): should be readonly?
|
2020-07-19 13:49:44 -04:00
|
|
|
close: util.nonEnumerable(workerClose),
|
|
|
|
postMessage: util.writable(postMessage),
|
|
|
|
workerMessageRecvCallback: util.nonEnumerable(workerMessageRecvCallback),
|
|
|
|
};
|
|
|
|
|
|
|
|
let hasBootstrapped = false;
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
function bootstrapMainRuntime(runtimeOptions) {
|
2020-07-19 13:49:44 -04:00
|
|
|
if (hasBootstrapped) {
|
|
|
|
throw new Error("Worker runtime already bootstrapped");
|
|
|
|
}
|
2020-09-04 07:52:19 -04:00
|
|
|
// Remove bootstrapping data from the global scope
|
|
|
|
delete globalThis.__bootstrap;
|
|
|
|
delete globalThis.bootstrap;
|
2020-07-19 13:49:44 -04:00
|
|
|
util.log("bootstrapMainRuntime");
|
|
|
|
hasBootstrapped = true;
|
2020-09-16 12:41:01 -04:00
|
|
|
Object.defineProperties(globalThis, windowOrWorkerGlobalScope);
|
2020-07-19 13:49:44 -04:00
|
|
|
Object.defineProperties(globalThis, mainRuntimeGlobalProperties);
|
2020-10-11 18:04:43 -04:00
|
|
|
Object.setPrototypeOf(globalThis, Window.prototype);
|
2020-07-19 13:49:44 -04:00
|
|
|
eventTarget.setEventTargetData(globalThis);
|
2020-11-26 16:27:55 -05:00
|
|
|
|
2020-12-07 15:22:58 -05:00
|
|
|
defineEventHandler(window, "load", null);
|
|
|
|
defineEventHandler(window, "unload", null);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-21 02:44:48 -05:00
|
|
|
const isUnloadDispatched = Symbol.for("isUnloadDispatched");
|
|
|
|
// Stores the flag for checking whether unload is dispatched or not.
|
|
|
|
// This prevents the recursive dispatches of unload events.
|
|
|
|
// See https://github.com/denoland/deno/issues/9201.
|
|
|
|
window[isUnloadDispatched] = false;
|
|
|
|
window.addEventListener("unload", () => {
|
|
|
|
window[isUnloadDispatched] = true;
|
|
|
|
});
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
runtimeStart(runtimeOptions);
|
2021-01-07 13:06:08 -05:00
|
|
|
const {
|
|
|
|
args,
|
|
|
|
location: locationHref,
|
|
|
|
noColor,
|
|
|
|
pid,
|
|
|
|
ppid,
|
|
|
|
unstableFlag,
|
|
|
|
} = runtimeOptions;
|
|
|
|
|
|
|
|
if (locationHref != null) {
|
|
|
|
location.setLocationHref(locationHref);
|
|
|
|
fetch.setBaseUrl(locationHref);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2020-08-07 16:47:18 -04:00
|
|
|
registerErrors();
|
|
|
|
|
2021-03-12 15:23:59 -05:00
|
|
|
const internalSymbol = Symbol("Deno.internal");
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
const finalDenoNs = {
|
|
|
|
core,
|
|
|
|
internal: internalSymbol,
|
2021-03-12 15:23:59 -05:00
|
|
|
[internalSymbol]: internals,
|
2020-09-17 12:09:50 -04:00
|
|
|
resources: core.resources,
|
|
|
|
close: core.close,
|
2021-04-12 04:47:44 -04:00
|
|
|
memoryUsage: core.memoryUsage,
|
2020-07-19 13:49:44 -04:00
|
|
|
...denoNs,
|
|
|
|
};
|
|
|
|
Object.defineProperties(finalDenoNs, {
|
|
|
|
pid: util.readOnly(pid),
|
|
|
|
ppid: util.readOnly(ppid),
|
|
|
|
noColor: util.readOnly(noColor),
|
|
|
|
args: util.readOnly(Object.freeze(args)),
|
2020-08-10 16:41:51 -04:00
|
|
|
mainModule: util.getterOnly(opMainModule),
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (unstableFlag) {
|
|
|
|
Object.assign(finalDenoNs, denoNsUnstable);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup `Deno` global - we're actually overriding already
|
|
|
|
// existing global `Deno` with `Deno` namespace from "./deno.ts".
|
|
|
|
util.immutableDefine(globalThis, "Deno", finalDenoNs);
|
|
|
|
Object.freeze(globalThis.Deno);
|
|
|
|
Object.freeze(globalThis.Deno.core);
|
|
|
|
Object.freeze(globalThis.Deno.core.sharedQueue);
|
|
|
|
signals.setSignals();
|
|
|
|
|
|
|
|
util.log("args", args);
|
|
|
|
}
|
|
|
|
|
2020-12-11 12:49:26 -05:00
|
|
|
function bootstrapWorkerRuntime(
|
|
|
|
runtimeOptions,
|
|
|
|
name,
|
|
|
|
useDenoNamespace,
|
|
|
|
internalName,
|
|
|
|
) {
|
2020-07-19 13:49:44 -04:00
|
|
|
if (hasBootstrapped) {
|
|
|
|
throw new Error("Worker runtime already bootstrapped");
|
|
|
|
}
|
2020-09-04 07:52:19 -04:00
|
|
|
// Remove bootstrapping data from the global scope
|
|
|
|
delete globalThis.__bootstrap;
|
|
|
|
delete globalThis.bootstrap;
|
2020-07-19 13:49:44 -04:00
|
|
|
util.log("bootstrapWorkerRuntime");
|
|
|
|
hasBootstrapped = true;
|
2020-09-16 12:41:01 -04:00
|
|
|
Object.defineProperties(globalThis, windowOrWorkerGlobalScope);
|
2020-07-19 13:49:44 -04:00
|
|
|
Object.defineProperties(globalThis, workerRuntimeGlobalProperties);
|
|
|
|
Object.defineProperties(globalThis, { name: util.readOnly(name) });
|
2020-10-11 18:04:43 -04:00
|
|
|
Object.setPrototypeOf(globalThis, DedicatedWorkerGlobalScope.prototype);
|
2020-07-19 13:49:44 -04:00
|
|
|
eventTarget.setEventTargetData(globalThis);
|
2020-12-11 12:49:26 -05:00
|
|
|
|
|
|
|
runtimeStart(
|
|
|
|
runtimeOptions,
|
2020-07-19 13:49:44 -04:00
|
|
|
internalName ?? name,
|
|
|
|
);
|
2021-01-07 13:06:08 -05:00
|
|
|
const { unstableFlag, pid, noColor, args, location: locationHref } =
|
|
|
|
runtimeOptions;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-07 13:06:08 -05:00
|
|
|
location.setLocationHref(locationHref);
|
|
|
|
fetch.setBaseUrl(locationHref);
|
2020-08-07 16:47:18 -04:00
|
|
|
registerErrors();
|
|
|
|
|
2021-03-12 15:23:59 -05:00
|
|
|
const internalSymbol = Symbol("Deno.internal");
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
const finalDenoNs = {
|
|
|
|
core,
|
|
|
|
internal: internalSymbol,
|
2021-03-12 15:23:59 -05:00
|
|
|
[internalSymbol]: internals,
|
2020-09-17 12:09:50 -04:00
|
|
|
resources: core.resources,
|
|
|
|
close: core.close,
|
2020-07-19 13:49:44 -04:00
|
|
|
...denoNs,
|
|
|
|
};
|
|
|
|
if (useDenoNamespace) {
|
|
|
|
if (unstableFlag) {
|
|
|
|
Object.assign(finalDenoNs, denoNsUnstable);
|
|
|
|
}
|
|
|
|
Object.defineProperties(finalDenoNs, {
|
|
|
|
pid: util.readOnly(pid),
|
|
|
|
noColor: util.readOnly(noColor),
|
|
|
|
args: util.readOnly(Object.freeze(args)),
|
|
|
|
});
|
|
|
|
// Setup `Deno` global - we're actually overriding already
|
|
|
|
// existing global `Deno` with `Deno` namespace from "./deno.ts".
|
|
|
|
util.immutableDefine(globalThis, "Deno", finalDenoNs);
|
|
|
|
Object.freeze(globalThis.Deno);
|
|
|
|
Object.freeze(globalThis.Deno.core);
|
|
|
|
Object.freeze(globalThis.Deno.core.sharedQueue);
|
|
|
|
signals.setSignals();
|
|
|
|
} else {
|
|
|
|
delete globalThis.Deno;
|
|
|
|
util.assert(globalThis.Deno === undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperties(globalThis, {
|
|
|
|
bootstrap: {
|
|
|
|
value: {
|
|
|
|
mainRuntime: bootstrapMainRuntime,
|
|
|
|
workerRuntime: bootstrapWorkerRuntime,
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})(this);
|