forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_init.js
64 lines (57 loc) · 2.26 KB
/
02_init.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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore-file
const internals = globalThis.__bootstrap.internals;
const requireImpl = internals.requireImpl;
const primordials = globalThis.__bootstrap.primordials;
const { ObjectDefineProperty } = primordials;
import { nodeGlobals, nodeGlobalThis } from "ext:deno_node/00_globals.js";
import "ext:deno_node/01_require.js";
let initialized = false;
function initialize(
nodeGlobalThisName,
usesLocalNodeModulesDir,
argv0,
) {
if (initialized) {
throw Error("Node runtime already initialized");
}
initialized = true;
if (usesLocalNodeModulesDir) {
requireImpl.setUsesLocalNodeModulesDir();
}
const nativeModuleExports = requireImpl.nativeModuleExports;
nodeGlobals.Buffer = nativeModuleExports["buffer"].Buffer;
nodeGlobals.clearImmediate = nativeModuleExports["timers"].clearImmediate;
nodeGlobals.clearInterval = nativeModuleExports["timers"].clearInterval;
nodeGlobals.clearTimeout = nativeModuleExports["timers"].clearTimeout;
nodeGlobals.console = nativeModuleExports["console"];
nodeGlobals.global = nodeGlobalThis;
nodeGlobals.process = nativeModuleExports["process"];
nodeGlobals.setImmediate = nativeModuleExports["timers"].setImmediate;
nodeGlobals.setInterval = nativeModuleExports["timers"].setInterval;
nodeGlobals.setTimeout = nativeModuleExports["timers"].setTimeout;
nodeGlobals.performance = nativeModuleExports["perf_hooks"].performance;
// add a hidden global for the esm code to use in order to reliably
// get node's globalThis
ObjectDefineProperty(globalThis, nodeGlobalThisName, {
enumerable: false,
configurable: true,
value: nodeGlobalThis,
});
// FIXME(bartlomieju): not nice to depend on `Deno` namespace here
// but it's the only way to get `args` and `version` and this point.
internals.__bootstrapNodeProcess(argv0, Deno.args, Deno.version);
internals.__initWorkerThreads();
// `Deno[Deno.internal].requireImpl` will be unreachable after this line.
delete internals.requireImpl;
}
function loadCjsModule(moduleName, isMain, inspectBrk) {
if (inspectBrk) {
requireImpl.setInspectBrk();
}
requireImpl.Module._load(moduleName, null, { main: isMain });
}
internals.node = {
initialize,
loadCjsModule,
};