|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { |
| 4 | + validateFunction |
| 5 | +} = require('internal/validators'); |
| 6 | +const { |
| 7 | + ERR_NOT_BUILDING_SNAPSHOT |
| 8 | +} = require('internal/errors'); |
| 9 | + |
| 10 | +const { |
| 11 | + setSerializeCallback, |
| 12 | + setDeserializeCallback, |
| 13 | + setDeserializeMainFunction: _setDeserializeMainFunction, |
| 14 | + markBootstrapComplete |
| 15 | +} = internalBinding('mksnapshot'); |
| 16 | + |
| 17 | +function isBuildingSnapshot() { |
| 18 | + // For now this is the only way to build a snapshot. |
| 19 | + return require('internal/options').getOptionValue('--build-snapshot'); |
| 20 | +} |
| 21 | + |
| 22 | +function throwIfNotBuildingSnapshot() { |
| 23 | + if (!isBuildingSnapshot()) { |
| 24 | + throw new ERR_NOT_BUILDING_SNAPSHOT(); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const deserializeCallbacks = []; |
| 29 | +let deserializeCallbackIsSet = false; |
| 30 | +function runDeserializeCallbacks() { |
| 31 | + while (deserializeCallbacks.length > 0) { |
| 32 | + const [callback, data] = deserializeCallbacks.shift(); |
| 33 | + callback(data); |
| 34 | + } |
| 35 | +} |
| 36 | +function addDeserializeCallback(callback, data) { |
| 37 | + throwIfNotBuildingSnapshot(); |
| 38 | + validateFunction(callback, 'callback'); |
| 39 | + if (!deserializeCallbackIsSet) { |
| 40 | + // TODO(joyeecheung): when the main function handling is done in JS, |
| 41 | + // the deserialize callbacks can always be invoked. For now only |
| 42 | + // store it in C++ when it's actually used to avoid unnecessary |
| 43 | + // C++ -> JS costs. |
| 44 | + setDeserializeCallback(runDeserializeCallbacks); |
| 45 | + deserializeCallbackIsSet = true; |
| 46 | + } |
| 47 | + deserializeCallbacks.push([callback, data]); |
| 48 | +} |
| 49 | + |
| 50 | +const serializeCallbacks = []; |
| 51 | +function runSerializeCallbacks() { |
| 52 | + while (serializeCallbacks.length > 0) { |
| 53 | + const [callback, data] = serializeCallbacks.shift(); |
| 54 | + callback(data); |
| 55 | + } |
| 56 | + // Remove the hooks from the snapshot. |
| 57 | + require('v8').startupSnapshot = undefined; |
| 58 | +} |
| 59 | +function addSerializeCallback(callback, data) { |
| 60 | + throwIfNotBuildingSnapshot(); |
| 61 | + validateFunction(callback, 'callback'); |
| 62 | + serializeCallbacks.push([callback, data]); |
| 63 | +} |
| 64 | + |
| 65 | +function initializeCallbacks() { |
| 66 | + // Only run the serialize callbacks in snapshot building mode, otherwise |
| 67 | + // they throw. |
| 68 | + if (isBuildingSnapshot()) { |
| 69 | + setSerializeCallback(runSerializeCallbacks); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +let deserializeMainIsSet = false; |
| 74 | +function setDeserializeMainFunction(callback, data) { |
| 75 | + throwIfNotBuildingSnapshot(); |
| 76 | + // TODO(joyeecheung): In lib/internal/bootstrap/node.js, create a default |
| 77 | + // main function to run the lib/internal/main scripts and make sure that |
| 78 | + // the main function set in the snapshot building process takes precedence. |
| 79 | + validateFunction(callback, 'callback'); |
| 80 | + if (deserializeMainIsSet) { |
| 81 | + throw new Error('Deserialize main function is already configured.'); |
| 82 | + } |
| 83 | + deserializeMainIsSet = true; |
| 84 | + |
| 85 | + _setDeserializeMainFunction(function deserializeMain() { |
| 86 | + const { |
| 87 | + prepareMainThreadExecution |
| 88 | + } = require('internal/bootstrap/pre_execution'); |
| 89 | + |
| 90 | + // This should be in sync with run_main_module.js until we make that |
| 91 | + // a built-in main function. |
| 92 | + prepareMainThreadExecution(true); |
| 93 | + markBootstrapComplete(); |
| 94 | + callback(data); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +module.exports = { |
| 99 | + initializeCallbacks, |
| 100 | + runDeserializeCallbacks, |
| 101 | + // exposed to require('v8').startupSnapshot |
| 102 | + namespace: { |
| 103 | + addDeserializeCallback, |
| 104 | + addSerializeCallback, |
| 105 | + setDeserializeMainFunction, |
| 106 | + isBuildingSnapshot |
| 107 | + } |
| 108 | +}; |
0 commit comments