forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup_snapshot.js
120 lines (106 loc) · 3.25 KB
/
startup_snapshot.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
'use strict';
const {
validateFunction,
}=require('internal/validators');
const {
codes: {
ERR_NOT_BUILDING_SNAPSHOT,
ERR_NOT_SUPPORTED_IN_SNAPSHOT,
ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION,
},
}=require('internal/errors');
const {
setSerializeCallback,
setDeserializeCallback,
setDeserializeMainFunction: _setDeserializeMainFunction,
isBuildingSnapshotBuffer,
}=internalBinding('mksnapshot');
function isBuildingSnapshot() {
return isBuildingSnapshotBuffer[0];
}
function throwIfNotBuildingSnapshot() {
if(!isBuildingSnapshot()) {
throw new ERR_NOT_BUILDING_SNAPSHOT();
}
}
function throwIfBuildingSnapshot(reason) {
if(isBuildingSnapshot()) {
throw new ERR_NOT_SUPPORTED_IN_SNAPSHOT(reason);
}
}
const deserializeCallbacks=[];
let deserializeCallbackIsSet=false;
function runDeserializeCallbacks() {
while(deserializeCallbacks.length>0) {
const {0: callback,1: data}=deserializeCallbacks.shift();
callback(data);
}
}
function addDeserializeCallback(callback,data) {
throwIfNotBuildingSnapshot();
validateFunction(callback,'callback');
if(!deserializeCallbackIsSet) {
// TODO(joyeecheung): when the main function handling is done in JS,
// the deserialize callbacks can always be invoked. For now only
// store it in C++ when it's actually used to avoid unnecessary
// C++ -> JS costs.
setDeserializeCallback(runDeserializeCallbacks);
deserializeCallbackIsSet=true;
}
deserializeCallbacks.push([callback,data]);
}
const serializeCallbacks=[];
function runSerializeCallbacks() {
while(serializeCallbacks.length>0) {
const {0: callback,1: data}=serializeCallbacks.shift();
callback(data);
}
}
function addSerializeCallback(callback,data) {
throwIfNotBuildingSnapshot();
validateFunction(callback,'callback');
serializeCallbacks.push([callback,data]);
}
function initializeCallbacks() {
// Only run the serialize callbacks in snapshot building mode, otherwise
// they throw.
if(isBuildingSnapshot()) {
setSerializeCallback(runSerializeCallbacks);
}
}
let deserializeMainIsSet=false;
function setDeserializeMainFunction(callback,data) {
throwIfNotBuildingSnapshot();
// TODO(joyeecheung): In lib/internal/bootstrap/node.js, create a default
// main function to run the lib/internal/main scripts and make sure that
// the main function set in the snapshot building process takes precedence.
validateFunction(callback,'callback');
if(deserializeMainIsSet) {
throw new ERR_DUPLICATE_STARTUP_SNAPSHOT_MAIN_FUNCTION();
}
deserializeMainIsSet=true;
_setDeserializeMainFunction(function deserializeMain() {
const {
prepareMainThreadExecution,
markBootstrapComplete,
}=require('internal/process/pre_execution');
// This should be in sync with run_main_module.js until we make that
// a built-in main function.
// TODO(joyeecheung): make a copy of argv[0] and insert it as argv[1].
prepareMainThreadExecution(false);
markBootstrapComplete();
callback(data);
});
}
module.exports={
initializeCallbacks,
runDeserializeCallbacks,
throwIfBuildingSnapshot,
// Exposed to require('v8').startupSnapshot
namespace: {
addDeserializeCallback,
addSerializeCallback,
setDeserializeMainFunction,
isBuildingSnapshot,
},
};