-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: share common code paths for SEA and embedder script
Since SEA is very similar in principle to embedding functionality, it makes sense to share code paths where possible. This commit does so and addresses a `TODO` while doing so. It also adds a utility to directly run CJS code to the embedder startup callback, which comes in handy for this purpose. Finally, this commit is breaking because it aligns the behavior of `require()`ing internal modules; previously, embedders could use the `require` function that they received to do so. (If this is not considered breaking because accessing internals is not covered by the API, then this would need ABI compatibility patches for becoming fully non-breaking.) PR-URL: #46825 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
- Loading branch information
Showing
16 changed files
with
145 additions
and
184 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
const { | ||
prepareMainThreadExecution, | ||
markBootstrapComplete, | ||
} = require('internal/process/pre_execution'); | ||
const { isSea } = internalBinding('sea'); | ||
const { emitExperimentalWarning } = require('internal/util'); | ||
const { embedderRequire, embedderRunCjs } = require('internal/util/embedding'); | ||
const { getEmbedderEntryFunction } = internalBinding('mksnapshot'); | ||
|
||
prepareMainThreadExecution(false, true); | ||
markBootstrapComplete(); | ||
|
||
if (isSea()) { | ||
emitExperimentalWarning('Single executable application'); | ||
} | ||
|
||
return getEmbedderEntryFunction()(embedderRequire, embedderRunCjs); |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,47 @@ | ||
'use strict'; | ||
const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors'); | ||
const { Module, wrapSafe } = require('internal/modules/cjs/loader'); | ||
|
||
// This is roughly the same as: | ||
// | ||
// const mod = new Module(filename); | ||
// mod._compile(contents, filename); | ||
// | ||
// but the code has been duplicated because currently there is no way to set the | ||
// value of require.main to module. | ||
// | ||
// TODO(RaisinTen): Find a way to deduplicate this. | ||
|
||
function embedderRunCjs(contents) { | ||
const filename = process.execPath; | ||
const compiledWrapper = wrapSafe(filename, contents); | ||
|
||
const customModule = new Module(filename, null); | ||
customModule.filename = filename; | ||
customModule.paths = Module._nodeModulePaths(customModule.path); | ||
|
||
const customExports = customModule.exports; | ||
|
||
embedderRequire.main = customModule; | ||
|
||
const customFilename = customModule.filename; | ||
|
||
const customDirname = customModule.path; | ||
|
||
return compiledWrapper( | ||
customExports, | ||
embedderRequire, | ||
customModule, | ||
customFilename, | ||
customDirname); | ||
} | ||
|
||
function embedderRequire(path) { | ||
if (!Module.isBuiltin(path)) { | ||
throw new ERR_UNKNOWN_BUILTIN_MODULE(path); | ||
} | ||
|
||
return require(path); | ||
} | ||
|
||
module.exports = { embedderRequire, embedderRunCjs }; |
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
Oops, something went wrong.