deno.land / x / deno@v1.28.2 / runtime / js / 99_main.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license."use strict";
// Removes the `__proto__` for security reasons.// https://tc39.es/ecma262/#sec-get-object.prototype.__proto__delete Object.prototype.__proto__;
// Remove Intl.v8BreakIterator because it is a non-standard API.delete Intl.v8BreakIterator;
((window) => { const core = Deno.core; const ops = core.ops; const { ArrayPrototypeIndexOf, ArrayPrototypePush, ArrayPrototypeShift, ArrayPrototypeSplice, ArrayPrototypeMap, DateNow, Error, FunctionPrototypeCall, FunctionPrototypeBind, ObjectAssign, ObjectDefineProperty, ObjectDefineProperties, ObjectFreeze, ObjectPrototypeIsPrototypeOf, ObjectSetPrototypeOf, PromiseResolve, Symbol, SymbolFor, SymbolIterator, PromisePrototypeThen, SafeWeakMap, TypeError, WeakMapPrototypeDelete, WeakMapPrototypeGet, WeakMapPrototypeSet, } = window.__bootstrap.primordials; const util = window.__bootstrap.util; const event = window.__bootstrap.event; const eventTarget = window.__bootstrap.eventTarget; const location = window.__bootstrap.location; const build = window.__bootstrap.build; const version = window.__bootstrap.version; const os = window.__bootstrap.os; const timers = window.__bootstrap.timers; const colors = window.__bootstrap.colors; const inspectArgs = window.__bootstrap.console.inspectArgs; const quoteString = window.__bootstrap.console.quoteString; const internals = window.__bootstrap.internals; const performance = window.__bootstrap.performance; const net = window.__bootstrap.net; const url = window.__bootstrap.url; const fetch = window.__bootstrap.fetch; const messagePort = window.__bootstrap.messagePort; const denoNs = window.__bootstrap.denoNs; const denoNsUnstable = window.__bootstrap.denoNsUnstable; const errors = window.__bootstrap.errors.errors; const webidl = window.__bootstrap.webidl; const domException = window.__bootstrap.domException; const { defineEventHandler, reportException } = window.__bootstrap.event; const { deserializeJsMessageData, serializeJsMessageData } = window.__bootstrap.messagePort; const { windowOrWorkerGlobalScope, unstableWindowOrWorkerGlobalScope, workerRuntimeGlobalProperties, mainRuntimeGlobalProperties, setNumCpus, setUserAgent, setLanguage, } = window.__bootstrap.globalScope;
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. PromisePrototypeThen( PromiseResolve(), () => FunctionPrototypeCall(timers.setTimeout, null, () => { // This should be fine, since only Window/MainWorker has .close() os.exit(0); }, 0), ); } }
function workerClose() { if (isClosing) { return; }
isClosing = true; ops.op_worker_close(); }
function postMessage(message, transferOrOptions = {}) { const prefix = "Failed to execute 'postMessage' on 'DedicatedWorkerGlobalScope'"; webidl.requiredArguments(arguments.length, 1, { prefix }); message = webidl.converters.any(message); let options; if ( webidl.type(transferOrOptions) === "Object" && transferOrOptions !== undefined && transferOrOptions[SymbolIterator] !== undefined ) { const transfer = webidl.converters["sequence<object>"]( transferOrOptions, { prefix, context: "Argument 2" }, ); options = { transfer }; } else { options = webidl.converters.StructuredSerializeOptions( transferOrOptions, { prefix, context: "Argument 2", }, ); } const { transfer } = options; const data = serializeJsMessageData(message, transfer); ops.op_worker_post_message(data); }
let isClosing = false; let globalDispatchEvent;
async function pollForMessages() { if (!globalDispatchEvent) { globalDispatchEvent = FunctionPrototypeBind( globalThis.dispatchEvent, globalThis, ); } while (!isClosing) { const data = await core.opAsync("op_worker_recv_message"); if (data === null) break; const v = deserializeJsMessageData(data); const message = v[0]; const transferables = v[1];
const msgEvent = new event.MessageEvent("message", { cancelable: false, data: message, ports: transferables.filter((t) => ObjectPrototypeIsPrototypeOf(messagePort.MessagePortPrototype, t) ), });
try { globalDispatchEvent(msgEvent); } catch (e) { const errorEvent = new event.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: e, });
globalDispatchEvent(errorEvent); if (!errorEvent.defaultPrevented) { throw e; } } } }
let loadedMainWorkerScript = false;
function importScripts(...urls) { if (ops.op_worker_get_type() === "module") { throw new TypeError("Can't import scripts in a module worker."); }
const baseUrl = location.getLocationHref(); const parsedUrls = ArrayPrototypeMap(urls, (scriptUrl) => { try { return new url.URL(scriptUrl, baseUrl ?? undefined).href; } catch { throw new domException.DOMException( "Failed to parse URL.", "SyntaxError", ); } });
// A classic worker's main script has looser MIME type checks than any // imported scripts, so we use `loadedMainWorkerScript` to distinguish them. // TODO(andreubotella) Refactor worker creation so the main script isn't // loaded with `importScripts()`. const scripts = ops.op_worker_sync_fetch( parsedUrls, !loadedMainWorkerScript, ); loadedMainWorkerScript = true;
for (const { url, script } of scripts) { const err = core.evalContext(script, url)[1]; if (err !== null) { throw err.thrown; } } }
function opMainModule() { return ops.op_main_module(); }
function formatException(error) { if (error instanceof Error) { return null; } else if (typeof error == "string") { return `Uncaught ${ inspectArgs([quoteString(error)], { colors: !colors.getNoColor(), }) }`; } else { return `Uncaught ${ inspectArgs([error], { colors: !colors.getNoColor() }) }`; } }
function runtimeStart(runtimeOptions, source) { core.setMacrotaskCallback(timers.handleTimerMacrotask); core.setMacrotaskCallback(promiseRejectMacrotaskCallback); core.setWasmStreamingCallback(fetch.handleWasmStreaming); core.setReportExceptionCallback(reportException); ops.op_set_format_exception_callback(formatException); version.setVersions( runtimeOptions.denoVersion, runtimeOptions.v8Version, runtimeOptions.tsVersion, ); build.setBuildInfo(runtimeOptions.target); util.setLogDebug(runtimeOptions.debugFlag, source); colors.setNoColor(runtimeOptions.noColor || !runtimeOptions.isTty); // deno-lint-ignore prefer-primordials Error.prepareStackTrace = core.prepareStackTrace; registerErrors(); }
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); core.registerErrorClass("NotSupported", errors.NotSupported); core.registerErrorBuilder( "DOMExceptionOperationError", function DOMExceptionOperationError(msg) { return new domException.DOMException(msg, "OperationError"); }, ); core.registerErrorBuilder( "DOMExceptionQuotaExceededError", function DOMExceptionQuotaExceededError(msg) { return new domException.DOMException(msg, "QuotaExceededError"); }, ); core.registerErrorBuilder( "DOMExceptionNotSupportedError", function DOMExceptionNotSupportedError(msg) { return new domException.DOMException(msg, "NotSupported"); }, ); core.registerErrorBuilder( "DOMExceptionNetworkError", function DOMExceptionNetworkError(msg) { return new domException.DOMException(msg, "NetworkError"); }, ); core.registerErrorBuilder( "DOMExceptionAbortError", function DOMExceptionAbortError(msg) { return new domException.DOMException(msg, "AbortError"); }, ); core.registerErrorBuilder( "DOMExceptionInvalidCharacterError", function DOMExceptionInvalidCharacterError(msg) { return new domException.DOMException(msg, "InvalidCharacterError"); }, ); core.registerErrorBuilder( "DOMExceptionDataError", function DOMExceptionDataError(msg) { return new domException.DOMException(msg, "DataError"); }, ); }
const pendingRejections = []; const pendingRejectionsReasons = new SafeWeakMap();
function promiseRejectCallback(type, promise, reason) { switch (type) { case 0: { ops.op_store_pending_promise_exception(promise, reason); ArrayPrototypePush(pendingRejections, promise); WeakMapPrototypeSet(pendingRejectionsReasons, promise, reason); break; } case 1: { ops.op_remove_pending_promise_exception(promise); const index = ArrayPrototypeIndexOf(pendingRejections, promise); if (index > -1) { ArrayPrototypeSplice(pendingRejections, index, 1); WeakMapPrototypeDelete(pendingRejectionsReasons, promise); } break; } default: return false; }
return !!globalThis.onunhandledrejection || eventTarget.listenerCount(globalThis, "unhandledrejection") > 0; }
function promiseRejectMacrotaskCallback() { while (pendingRejections.length > 0) { const promise = ArrayPrototypeShift(pendingRejections); const hasPendingException = ops.op_has_pending_promise_exception( promise, ); const reason = WeakMapPrototypeGet(pendingRejectionsReasons, promise); WeakMapPrototypeDelete(pendingRejectionsReasons, promise);
if (!hasPendingException) { continue; }
const rejectionEvent = new event.PromiseRejectionEvent( "unhandledrejection", { cancelable: true, promise, reason, }, );
const errorEventCb = (event) => { if (event.error === reason) { ops.op_remove_pending_promise_exception(promise); } }; // Add a callback for "error" event - it will be dispatched // if error is thrown during dispatch of "unhandledrejection" // event. globalThis.addEventListener("error", errorEventCb); globalThis.dispatchEvent(rejectionEvent); globalThis.removeEventListener("error", errorEventCb);
// If event was not prevented (or "unhandledrejection" listeners didn't // throw) we will let Rust side handle it. if (rejectionEvent.defaultPrevented) { ops.op_remove_pending_promise_exception(promise); } } return true; }
let hasBootstrapped = false;
function bootstrapMainRuntime(runtimeOptions) { if (hasBootstrapped) { throw new Error("Worker runtime already bootstrapped"); }
core.initializeAsyncOps(); performance.setTimeOrigin(DateNow()); net.setup(runtimeOptions.unstableFlag);
const consoleFromV8 = window.console; const wrapConsole = window.__bootstrap.console.wrapConsole;
// Remove bootstrapping data from the global scope const __bootstrap = globalThis.__bootstrap; delete globalThis.__bootstrap; delete globalThis.bootstrap; util.log("bootstrapMainRuntime"); hasBootstrapped = true;
// If the `--location` flag isn't set, make `globalThis.location` `undefined` and // writable, so that they can mock it themselves if they like. If the flag was // set, define `globalThis.location`, using the provided value. if (runtimeOptions.location == null) { mainRuntimeGlobalProperties.location = { writable: true, }; } else { location.setLocationHref(runtimeOptions.location); }
ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope); if (runtimeOptions.unstableFlag) { ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope); } ObjectDefineProperties(globalThis, mainRuntimeGlobalProperties); ObjectDefineProperties(globalThis, { close: util.writable(windowClose), closed: util.getterOnly(() => windowIsClosing), }); ObjectSetPrototypeOf(globalThis, Window.prototype);
if (runtimeOptions.inspectFlag) { const consoleFromDeno = globalThis.console; wrapConsole(consoleFromDeno, consoleFromV8); }
eventTarget.setEventTargetData(globalThis);
defineEventHandler(window, "error"); defineEventHandler(window, "load"); defineEventHandler(window, "beforeunload"); defineEventHandler(window, "unload"); defineEventHandler(window, "unhandledrejection");
core.setPromiseRejectCallback(promiseRejectCallback);
const isUnloadDispatched = SymbolFor("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; });
runtimeStart(runtimeOptions);
setNumCpus(runtimeOptions.cpuCount); setUserAgent(runtimeOptions.userAgent); setLanguage(runtimeOptions.locale);
const internalSymbol = Symbol("Deno.internal");
// These have to initialized here and not in `90_deno_ns.js` because // the op function that needs to be passed will be invalidated by creating // a snapshot ObjectAssign(internals, { nodeUnstable: { spawnChild: __bootstrap.spawn.createSpawnChild( ops.op_node_unstable_spawn_child, ), spawn: __bootstrap.spawn.createSpawn(ops.op_node_unstable_spawn_child), spawnSync: __bootstrap.spawn.createSpawnSync( ops.op_node_unstable_spawn_sync, ), serve: __bootstrap.flash.createServe(ops.op_node_unstable_flash_serve), upgradeHttpRaw: __bootstrap.flash.upgradeHttpRaw, listenDatagram: __bootstrap.net.createListenDatagram( ops.op_node_unstable_net_listen_udp, ops.op_node_unstable_net_listen_unixpacket, ), }, });
const finalDenoNs = { core, internal: internalSymbol, [internalSymbol]: internals, resources: core.resources, close: core.close, ...denoNs, }; ObjectDefineProperties(finalDenoNs, { pid: util.readOnly(runtimeOptions.pid), ppid: util.readOnly(runtimeOptions.ppid), noColor: util.readOnly(runtimeOptions.noColor), args: util.readOnly(ObjectFreeze(runtimeOptions.args)), mainModule: util.getterOnly(opMainModule), });
if (runtimeOptions.unstableFlag) { ObjectAssign(finalDenoNs, denoNsUnstable); // These have to initialized here and not in `90_deno_ns.js` because // the op function that needs to be passed will be invalidated by creating // a snapshot ObjectAssign(finalDenoNs, { spawnChild: __bootstrap.spawn.createSpawnChild(ops.op_spawn_child), spawn: __bootstrap.spawn.createSpawn(ops.op_spawn_child), spawnSync: __bootstrap.spawn.createSpawnSync(ops.op_spawn_sync), serve: __bootstrap.flash.createServe(ops.op_flash_serve), listenDatagram: __bootstrap.net.createListenDatagram( ops.op_net_listen_udp, ops.op_net_listen_unixpacket, ), }); }
// Setup `Deno` global - we're actually overriding already existing global // `Deno` with `Deno` namespace from "./deno.ts". ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs)); ObjectFreeze(globalThis.Deno.core);
util.log("args", runtimeOptions.args); }
function bootstrapWorkerRuntime( runtimeOptions, name, internalName, ) { if (hasBootstrapped) { throw new Error("Worker runtime already bootstrapped"); }
core.initializeAsyncOps(); performance.setTimeOrigin(DateNow()); net.setup(runtimeOptions.unstableFlag);
const consoleFromV8 = window.console; const wrapConsole = window.__bootstrap.console.wrapConsole;
// Remove bootstrapping data from the global scope const __bootstrap = globalThis.__bootstrap; delete globalThis.__bootstrap; delete globalThis.bootstrap; util.log("bootstrapWorkerRuntime"); hasBootstrapped = true; ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope); if (runtimeOptions.unstableFlag) { ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope); } ObjectDefineProperties(globalThis, workerRuntimeGlobalProperties); ObjectDefineProperties(globalThis, { name: util.writable(name), // TODO(bartlomieju): should be readonly? close: util.nonEnumerable(workerClose), postMessage: util.writable(postMessage), }); if (runtimeOptions.enableTestingFeaturesFlag) { ObjectDefineProperty( globalThis, "importScripts", util.writable(importScripts), ); } ObjectSetPrototypeOf(globalThis, DedicatedWorkerGlobalScope.prototype);
const consoleFromDeno = globalThis.console; wrapConsole(consoleFromDeno, consoleFromV8);
eventTarget.setEventTargetData(globalThis);
defineEventHandler(self, "message"); defineEventHandler(self, "error", undefined, true); defineEventHandler(self, "unhandledrejection");
core.setPromiseRejectCallback(promiseRejectCallback);
// `Deno.exit()` is an alias to `self.close()`. Setting and exit // code using an op in worker context is a no-op. os.setExitHandler((_exitCode) => { workerClose(); });
runtimeStart( runtimeOptions, internalName ?? name, );
location.setLocationHref(runtimeOptions.location);
setNumCpus(runtimeOptions.cpuCount); setLanguage(runtimeOptions.locale);
globalThis.pollForMessages = pollForMessages;
const internalSymbol = Symbol("Deno.internal");
// These have to initialized here and not in `90_deno_ns.js` because // the op function that needs to be passed will be invalidated by creating // a snapshot ObjectAssign(internals, { nodeUnstable: { spawnChild: __bootstrap.spawn.createSpawnChild( ops.op_node_unstable_spawn_child, ), spawn: __bootstrap.spawn.createSpawn(ops.op_node_unstable_spawn_child), spawnSync: __bootstrap.spawn.createSpawnSync( ops.op_node_unstable_spawn_sync, ), serve: __bootstrap.flash.createServe(ops.op_node_unstable_flash_serve), upgradeHttpRaw: __bootstrap.flash.upgradeHttpRaw, listenDatagram: __bootstrap.net.createListenDatagram( ops.op_node_unstable_net_listen_udp, ops.op_node_unstable_net_listen_unixpacket, ), }, });
const finalDenoNs = { core, internal: internalSymbol, [internalSymbol]: internals, resources: core.resources, close: core.close, ...denoNs, }; if (runtimeOptions.unstableFlag) { ObjectAssign(finalDenoNs, denoNsUnstable); // These have to initialized here and not in `90_deno_ns.js` because // the op function that needs to be passed will be invalidated by creating // a snapshot ObjectAssign(finalDenoNs, { spawnChild: __bootstrap.spawn.createSpawnChild(ops.op_spawn_child), spawn: __bootstrap.spawn.createSpawn(ops.op_spawn_child), spawnSync: __bootstrap.spawn.createSpawnSync(ops.op_spawn_sync), serve: __bootstrap.flash.createServe(ops.op_flash_serve), listenDatagram: __bootstrap.net.createListenDatagram( ops.op_net_listen_udp, ops.op_net_listen_unixpacket, ), }); } ObjectDefineProperties(finalDenoNs, { pid: util.readOnly(runtimeOptions.pid), noColor: util.readOnly(runtimeOptions.noColor), args: util.readOnly(ObjectFreeze(runtimeOptions.args)), }); // Setup `Deno` global - we're actually overriding already // existing global `Deno` with `Deno` namespace from "./deno.ts". ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs)); ObjectFreeze(globalThis.Deno.core); }
ObjectDefineProperties(globalThis, { bootstrap: { value: { mainRuntime: bootstrapMainRuntime, workerRuntime: bootstrapWorkerRuntime, }, configurable: true, }, });})(this);
deno

Version Info

Tagged at
a year ago