| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7bd909b commit 3803b02
16 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,18 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const { | ||
| 3 | + prepareMainThreadExecution, | ||
| 4 | + markBootstrapComplete, | ||
| 5 | + } = require('internal/process/pre_execution'); | ||
| 6 | + const { isSea } = internalBinding('sea'); | ||
| 7 | + const { emitExperimentalWarning } = require('internal/util'); | ||
| 8 | + const { embedderRequire, embedderRunCjs } = require('internal/util/embedding'); | ||
| 9 | + const { getEmbedderEntryFunction } = internalBinding('mksnapshot'); | ||
| 10 | + | ||
| 11 | + prepareMainThreadExecution(false, true); | ||
| 12 | + markBootstrapComplete(); | ||
| 13 | + | ||
| 14 | + if (isSea()) { | ||
| 15 | + emitExperimentalWarning('Single executable application'); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + return getEmbedderEntryFunction()(embedderRequire, embedderRunCjs); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -119,16 +119,25 @@ function main() { | |||
| 119 | 119 | const { | |
| 120 | 120 | prepareMainThreadExecution | |
| 121 | 121 | } = require('internal/process/pre_execution'); | |
| 122 | + const path = require('path'); | ||
| 122 | 123 | ||
| 123 | 124 | let serializeMainFunction = getEmbedderEntryFunction(); | |
| 124 | 125 | const serializeMainArgs = [requireForUserSnapshot]; | |
| 125 | 126 | ||
| 126 | 127 | if (serializeMainFunction) { // embedded case | |
| 127 | 128 | prepareMainThreadExecution(false, false); | |
| 129 | + // TODO(addaleax): Make this `embedderRunCjs` once require('module') | ||
| 130 | + // is supported in snapshots. | ||
| 131 | + const filename = process.execPath; | ||
| 132 | + const dirname = path.dirname(filename); | ||
| 133 | + function minimalRunCjs(source) { | ||
| 134 | + const fn = compileSerializeMain(filename, source); | ||
| 135 | + return fn(requireForUserSnapshot, filename, dirname); | ||
| 136 | + } | ||
| 137 | + serializeMainArgs.push(minimalRunCjs); | ||
| 128 | 138 | } else { | |
| 129 | 139 | prepareMainThreadExecution(true, false); | |
| 130 | 140 | const file = process.argv[1]; | |
| 131 | - const path = require('path'); | ||
| 132 | 141 | const filename = path.resolve(file); | |
| 133 | 142 | const dirname = path.dirname(filename); | |
| 134 | 143 | const source = readFileSync(file, 'utf-8'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,47 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors'); | ||
| 3 | + const { Module, wrapSafe } = require('internal/modules/cjs/loader'); | ||
| 4 | + | ||
| 5 | + // This is roughly the same as: | ||
| 6 | + // | ||
| 7 | + // const mod = new Module(filename); | ||
| 8 | + // mod._compile(contents, filename); | ||
| 9 | + // | ||
| 10 | + // but the code has been duplicated because currently there is no way to set the | ||
| 11 | + // value of require.main to module. | ||
| 12 | + // | ||
| 13 | + // TODO(RaisinTen): Find a way to deduplicate this. | ||
| 14 | + | ||
| 15 | + function embedderRunCjs(contents) { | ||
| 16 | + const filename = process.execPath; | ||
| 17 | + const compiledWrapper = wrapSafe(filename, contents); | ||
| 18 | + | ||
| 19 | + const customModule = new Module(filename, null); | ||
| 20 | + customModule.filename = filename; | ||
| 21 | + customModule.paths = Module._nodeModulePaths(customModule.path); | ||
| 22 | + | ||
| 23 | + const customExports = customModule.exports; | ||
| 24 | + | ||
| 25 | + embedderRequire.main = customModule; | ||
| 26 | + | ||
| 27 | + const customFilename = customModule.filename; | ||
| 28 | + | ||
| 29 | + const customDirname = customModule.path; | ||
| 30 | + | ||
| 31 | + return compiledWrapper( | ||
| 32 | + customExports, | ||
| 33 | + embedderRequire, | ||
| 34 | + customModule, | ||
| 35 | + customFilename, | ||
| 36 | + customDirname); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + function embedderRequire(path) { | ||
| 40 | + if (!Module.isBuiltin(path)) { | ||
| 41 | + throw new ERR_UNKNOWN_BUILTIN_MODULE(path); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + return require(path); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + module.exports = { embedderRequire, embedderRunCjs }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -528,17 +528,15 @@ MaybeLocal<Value> LoadEnvironment( | |||
| 528 | 528 | return StartExecution(env, cb); | |
| 529 | 529 | } | |
| 530 | 530 | ||
| 531 | - MaybeLocal<Value> LoadEnvironment( | ||
| 532 | - Environment* env, | ||
| 533 | - const char* main_script_source_utf8) { | ||
| 534 | - CHECK_NOT_NULL(main_script_source_utf8); | ||
| 531 | + MaybeLocal<Value> LoadEnvironment(Environment* env, | ||
| 532 | + std::string_view main_script_source_utf8) { | ||
| 533 | + CHECK_NOT_NULL(main_script_source_utf8.data()); | ||
| 535 | 534 | return LoadEnvironment( | |
| 536 | 535 | env, [&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> { | |
| 537 | - std::string name = "embedder_main_" + std::to_string(env->thread_id()); | ||
| 538 | - env->builtin_loader()->Add(name.c_str(), main_script_source_utf8); | ||
| 539 | - Realm* realm = env->principal_realm(); | ||
| 540 | - | ||
| 541 | - return realm->ExecuteBootstrapper(name.c_str()); | ||
| 536 | + Local<Value> main_script = | ||
| 537 | + ToV8Value(env->context(), main_script_source_utf8).ToLocalChecked(); | ||
| 538 | + return info.run_cjs->Call( | ||
| 539 | + env->context(), Null(env->isolate()), 1, &main_script); | ||
| 542 | 540 | }); | |
| 543 | 541 | } | |
| 544 | 542 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -406,14 +406,12 @@ inline builtins::BuiltinLoader* Environment::builtin_loader() { | |||
| 406 | 406 | return &builtin_loader_; | |
| 407 | 407 | } | |
| 408 | 408 | ||
| 409 | - inline const StartExecutionCallback& | ||
| 410 | - Environment::embedder_mksnapshot_entry_point() const { | ||
| 411 | - return embedder_mksnapshot_entry_point_; | ||
| 409 | + inline const StartExecutionCallback& Environment::embedder_entry_point() const { | ||
| 410 | + return embedder_entry_point_; | ||
| 412 | 411 | } | |
| 413 | 412 | ||
| 414 | - inline void Environment::set_embedder_mksnapshot_entry_point( | ||
| 415 | - StartExecutionCallback&& fn) { | ||
| 416 | - embedder_mksnapshot_entry_point_ = std::move(fn); | ||
| 413 | + inline void Environment::set_embedder_entry_point(StartExecutionCallback&& fn) { | ||
| 414 | + embedder_entry_point_ = std::move(fn); | ||
| 417 | 415 | } | |
| 418 | 416 | ||
| 419 | 417 | inline double Environment::new_async_id() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -948,8 +948,8 @@ class Environment : public MemoryRetainer { | |||
| 948 | 948 | ||
| 949 | 949 | #endif // HAVE_INSPECTOR | |
| 950 | 950 | ||
| 951 | - inline const StartExecutionCallback& embedder_mksnapshot_entry_point() const; | ||
| 952 | - inline void set_embedder_mksnapshot_entry_point(StartExecutionCallback&& fn); | ||
| 951 | + inline const StartExecutionCallback& embedder_entry_point() const; | ||
| 952 | + inline void set_embedder_entry_point(StartExecutionCallback&& fn); | ||
| 953 | 953 | ||
| 954 | 954 | inline void set_process_exit_handler( | |
| 955 | 955 | std::function<void(Environment*, ExitCode)>&& handler); | |
@@ -1133,7 +1133,7 @@ class Environment : public MemoryRetainer { | |||
| 1133 | 1133 | std::unique_ptr<Realm> principal_realm_ = nullptr; | |
| 1134 | 1134 | ||
| 1135 | 1135 | builtins::BuiltinLoader builtin_loader_; | |
| 1136 | - StartExecutionCallback embedder_mksnapshot_entry_point_; | ||
| 1136 | + StartExecutionCallback embedder_entry_point_; | ||
| 1137 | 1137 | ||
| 1138 | 1138 | // Used by allocate_managed_buffer() and release_managed_buffer() to keep | |
| 1139 | 1139 | // track of the BackingStore for a given pointer. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -277,22 +277,17 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) { | |||
| 277 | 277 | ||
| 278 | 278 | if (cb != nullptr) { | |
| 279 | 279 | EscapableHandleScope scope(env->isolate()); | |
| 280 | + // TODO(addaleax): pass the callback to the main script more directly, | ||
| 281 | + // e.g. by making StartExecution(env, builtin) parametrizable | ||
| 282 | + env->set_embedder_entry_point(std::move(cb)); | ||
| 283 | + auto reset_entry_point = | ||
| 284 | + OnScopeLeave([&]() { env->set_embedder_entry_point({}); }); | ||
| 280 | 285 | ||
| 281 | - if (env->isolate_data()->options()->build_snapshot) { | ||
| 282 | - // TODO(addaleax): pass the callback to the main script more directly, | ||
| 283 | - // e.g. by making StartExecution(env, builtin) parametrizable | ||
| 284 | - env->set_embedder_mksnapshot_entry_point(std::move(cb)); | ||
| 285 | - auto reset_entry_point = | ||
| 286 | - OnScopeLeave([&]() { env->set_embedder_mksnapshot_entry_point({}); }); | ||
| 286 | + const char* entry = env->isolate_data()->options()->build_snapshot | ||
| 287 | + ? "internal/main/mksnapshot" | ||
| 288 | + : "internal/main/embedding"; | ||
| 287 | 289 | ||
| 288 | - return StartExecution(env, "internal/main/mksnapshot"); | ||
| 289 | - } | ||
| 290 | - | ||
| 291 | - if (StartExecution(env, "internal/main/environment").IsEmpty()) return {}; | ||
| 292 | - return scope.EscapeMaybe(cb({ | ||
| 293 | - env->process_object(), | ||
| 294 | - env->builtin_module_require(), | ||
| 295 | - })); | ||
| 290 | + return scope.EscapeMaybe(StartExecution(env, entry)); | ||
| 296 | 291 | } | |
| 297 | 292 | ||
| 298 | 293 | // TODO(joyeecheung): move these conditions into JS land and let the | |
@@ -312,18 +307,6 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) { | |||
| 312 | 307 | first_argv = env->argv()[1]; | |
| 313 | 308 | } | |
| 314 | 309 | ||
| 315 | - #ifndef DISABLE_SINGLE_EXECUTABLE_APPLICATION | ||
| 316 | - if (sea::IsSingleExecutable()) { | ||
| 317 | - // TODO(addaleax): Find a way to reuse: | ||
| 318 | - // | ||
| 319 | - // LoadEnvironment(Environment*, const char*) | ||
| 320 | - // | ||
| 321 | - // instead and not add yet another main entry point here because this | ||
| 322 | - // already duplicates existing code. | ||
| 323 | - return StartExecution(env, "internal/main/single_executable_application"); | ||
| 324 | - } | ||
| 325 | - #endif | ||
| 326 | - | ||
| 327 | 310 | if (first_argv == "inspect") { | |
| 328 | 311 | return StartExecution(env, "internal/main/inspect"); | |
| 329 | 312 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -682,6 +682,7 @@ NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( | |||
| 682 | 682 | struct StartExecutionCallbackInfo { | |
| 683 | 683 | v8::Local<v8::Object> process_object; | |
| 684 | 684 | v8::Local<v8::Function> native_require; | |
| 685 | + v8::Local<v8::Function> run_cjs; | ||
| 685 | 686 | }; | |
| 686 | 687 | ||
| 687 | 688 | using StartExecutionCallback = | |
@@ -691,8 +692,7 @@ NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment( | |||
| 691 | 692 | Environment* env, | |
| 692 | 693 | StartExecutionCallback cb); | |
| 693 | 694 | NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment( | |
| 694 | - Environment* env, | ||
| 695 | - const char* main_script_source_utf8); | ||
| 695 | + Environment* env, std::string_view main_script_source_utf8); | ||
| 696 | 696 | NODE_EXTERN void FreeEnvironment(Environment* env); | |
| 697 | 697 | ||
| 698 | 698 | // Set a callback that is called when process.exit() is called from JS, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments