MCPcopy Create free account
hub / github.com/nodejs/node / main

Function main

lib/internal/main/mksnapshot.js:133–210  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

131
132
133function main() {
134 prepareMainThreadExecution(false, false);
135
136 // In a context created for building snapshots, V8 does not install Error.stackTraceLimit and as
137 // a result, if an error is created during the snapshot building process, error.stack would be
138 // undefined. To prevent users from tripping over this, install Error.stackTraceLimit based on
139 // --stack-trace-limit by ourselves (which defaults to 10).
140 // See https://chromium-review.googlesource.com/c/v8/v8/+/3319481
141 const initialStackTraceLimitDesc = {
142 value: getOptionValue('--stack-trace-limit'),
143 configurable: true,
144 writable: true,
145 enumerable: true,
146 __proto__: null,
147 };
148 ObjectDefineProperty(Error, 'stackTraceLimit', initialStackTraceLimitDesc);
149
150 let stackTraceLimitDescToRestore;
151 // Error.stackTraceLimit needs to be removed during serialization, because when V8 deserializes
152 // the snapshot, it expects Error.stackTraceLimit to be unset so that it can install it as a new property
153 // using the value of --stack-trace-limit.
154 addAfterUserSerializeCallback(() => {
155 const desc = ObjectGetOwnPropertyDescriptor(Error, 'stackTraceLimit');
156
157 // If it's modified by users, emit a warning.
158 if (desc && (
159 desc.value !== initialStackTraceLimitDesc.value ||
160 desc.configurable !== initialStackTraceLimitDesc.configurable ||
161 desc.writable !== initialStackTraceLimitDesc.writable ||
162 desc.enumerable !== initialStackTraceLimitDesc.enumerable
163 )) {
164 process._rawDebug('Error.stackTraceLimit has been modified by the snapshot builder script.');
165 // We want to use null-prototype objects to not rely on globally mutable
166 // %Object.prototype%.
167 if (desc.configurable) {
168 stackTraceLimitDescToRestore = desc;
169 ObjectSetPrototypeOf(stackTraceLimitDescToRestore, null);
170 process._rawDebug('It will be preserved after snapshot deserialization and override ' +
171 '--stack-trace-limit passed into the deserialized application.\n' +
172 'To allow --stack-trace-limit override in the deserialized application, ' +
173 'delete Error.stackTraceLimit.');
174 } else {
175 process._rawDebug('It is not configurable and will crash the application upon deserialization.\n' +
176 'To fix the error, make Error.stackTraceLimit configurable.');
177 }
178 }
179
180 delete Error.stackTraceLimit;
181 });
182
183 addDeserializeCallback(() => {
184 if (stackTraceLimitDescToRestore) {
185 ObjectDefineProperty(Error, 'stackTraceLimit', stackTraceLimitDescToRestore);
186 }
187 });
188
189 // TODO(addaleax): Make this `embedderRunCjs` once require('module')
190 // is supported in snapshots.

Callers 1

mksnapshot.jsFile · 0.70

Calls 5

getOptionValueFunction · 0.85
addDeserializeCallbackFunction · 0.85
emitExperimentalWarningFunction · 0.85

Tested by

no test coverage detected