-
Notifications
You must be signed in to change notification settings - Fork 29.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds several APIs under the `v8.startupSnapshot` namespace for specifying hooks into the startup snapshot serialization and deserialization. - isBuildingSnapshot() - addSerializeCallback() - addDeserializeCallback() - setDeserializeMainFunction()
- Loading branch information
1 parent
7a71ae8
commit 42718e2
Showing
15 changed files
with
388 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"use strict"; | ||
|
||
const { | ||
validateFunction | ||
} = require('internal/validators'); | ||
const { | ||
ERR_NOT_BUILDING_SNAPSHOT | ||
} = require('internal/errors'); | ||
|
||
const { | ||
setSerializeCallback, | ||
setDeserializeCallback, | ||
setDeserializeMainFunction: _setDeserializeMainFunction, | ||
markBootstrapComplete | ||
} = internalBinding('mksnapshot'); | ||
|
||
function isBuildingSnapshot() { | ||
// For now this is the only way to build a snapshot. | ||
return require('internal/options').getOptionValue('--build-snapshot'); | ||
} | ||
|
||
function throwIfNotBuildingSnapshot() { | ||
if (!isBuildingSnapshot()) { | ||
throw new ERR_NOT_BUILDING_SNAPSHOT(); | ||
} | ||
} | ||
|
||
const deserializeCallbacks = []; | ||
let deserializeCallbackIsSet = false; | ||
function runDeserializeCallbacks() { | ||
while (deserializeCallbacks.length > 0) { | ||
const [callback, 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 [callback, data] = serializeCallbacks.shift(); | ||
callback(data); | ||
} | ||
// Remove the hooks from the snapshot. | ||
require('v8').startupSnapshot = undefined; | ||
} | ||
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 Error('Deserialize main function is already configured.'); | ||
} | ||
deserializeMainIsSet = true; | ||
|
||
_setDeserializeMainFunction(function deserializeMain() { | ||
const { | ||
prepareMainThreadExecution | ||
} = require('internal/bootstrap/pre_execution'); | ||
|
||
// This should be in sync with run_main_module.js until we make that | ||
// a built-in main function. | ||
prepareMainThreadExecution(true); | ||
markBootstrapComplete(); | ||
callback(data); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
initializeCallbacks, | ||
runDeserializeCallbacks, | ||
// exposed to require('v8').startupSnapshot | ||
namespace: { | ||
addDeserializeCallback, | ||
addSerializeCallback, | ||
setDeserializeMainFunction, | ||
isBuildingSnapshot | ||
} | ||
}; |
Oops, something went wrong.