Skip to content

Commit

Permalink
src: share common code paths for SEA and embedder script
Browse files Browse the repository at this point in the history
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 <raisinten@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
addaleax authored Mar 2, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 7bd909b commit 3803b02
Showing 16 changed files with 145 additions and 184 deletions.
18 changes: 18 additions & 0 deletions lib/internal/main/embedding.js
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);
13 changes: 0 additions & 13 deletions lib/internal/main/environment.js

This file was deleted.

11 changes: 10 additions & 1 deletion lib/internal/main/mksnapshot.js
Original file line number Diff line number Diff line change
@@ -119,16 +119,25 @@ function main() {
const {
prepareMainThreadExecution
} = require('internal/process/pre_execution');
const path = require('path');

let serializeMainFunction = getEmbedderEntryFunction();
const serializeMainArgs = [requireForUserSnapshot];

if (serializeMainFunction) { // embedded case
prepareMainThreadExecution(false, false);
// TODO(addaleax): Make this `embedderRunCjs` once require('module')
// is supported in snapshots.
const filename = process.execPath;
const dirname = path.dirname(filename);
function minimalRunCjs(source) {
const fn = compileSerializeMain(filename, source);
return fn(requireForUserSnapshot, filename, dirname);
}
serializeMainArgs.push(minimalRunCjs);
} else {
prepareMainThreadExecution(true, false);
const file = process.argv[1];
const path = require('path');
const filename = path.resolve(file);
const dirname = path.dirname(filename);
const source = readFileSync(file, 'utf-8');
55 changes: 0 additions & 55 deletions lib/internal/main/single_executable_application.js

This file was deleted.

47 changes: 47 additions & 0 deletions lib/internal/util/embedding.js
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 };
16 changes: 7 additions & 9 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
@@ -528,17 +528,15 @@ MaybeLocal<Value> LoadEnvironment(
return StartExecution(env, cb);
}

MaybeLocal<Value> LoadEnvironment(
Environment* env,
const char* main_script_source_utf8) {
CHECK_NOT_NULL(main_script_source_utf8);
MaybeLocal<Value> LoadEnvironment(Environment* env,
std::string_view main_script_source_utf8) {
CHECK_NOT_NULL(main_script_source_utf8.data());
return LoadEnvironment(
env, [&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> {
std::string name = "embedder_main_" + std::to_string(env->thread_id());
env->builtin_loader()->Add(name.c_str(), main_script_source_utf8);
Realm* realm = env->principal_realm();

return realm->ExecuteBootstrapper(name.c_str());
Local<Value> main_script =
ToV8Value(env->context(), main_script_source_utf8).ToLocalChecked();
return info.run_cjs->Call(
env->context(), Null(env->isolate()), 1, &main_script);
});
}

10 changes: 4 additions & 6 deletions src/env-inl.h
Original file line number Diff line number Diff line change
@@ -406,14 +406,12 @@ inline builtins::BuiltinLoader* Environment::builtin_loader() {
return &builtin_loader_;
}

inline const StartExecutionCallback&
Environment::embedder_mksnapshot_entry_point() const {
return embedder_mksnapshot_entry_point_;
inline const StartExecutionCallback& Environment::embedder_entry_point() const {
return embedder_entry_point_;
}

inline void Environment::set_embedder_mksnapshot_entry_point(
StartExecutionCallback&& fn) {
embedder_mksnapshot_entry_point_ = std::move(fn);
inline void Environment::set_embedder_entry_point(StartExecutionCallback&& fn) {
embedder_entry_point_ = std::move(fn);
}

inline double Environment::new_async_id() {
6 changes: 3 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
@@ -948,8 +948,8 @@ class Environment : public MemoryRetainer {

#endif // HAVE_INSPECTOR

inline const StartExecutionCallback& embedder_mksnapshot_entry_point() const;
inline void set_embedder_mksnapshot_entry_point(StartExecutionCallback&& fn);
inline const StartExecutionCallback& embedder_entry_point() const;
inline void set_embedder_entry_point(StartExecutionCallback&& fn);

inline void set_process_exit_handler(
std::function<void(Environment*, ExitCode)>&& handler);
@@ -1133,7 +1133,7 @@ class Environment : public MemoryRetainer {
std::unique_ptr<Realm> principal_realm_ = nullptr;

builtins::BuiltinLoader builtin_loader_;
StartExecutionCallback embedder_mksnapshot_entry_point_;
StartExecutionCallback embedder_entry_point_;

// Used by allocate_managed_buffer() and release_managed_buffer() to keep
// track of the BackingStore for a given pointer.
35 changes: 9 additions & 26 deletions src/node.cc
Original file line number Diff line number Diff line change
@@ -277,22 +277,17 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {

if (cb != nullptr) {
EscapableHandleScope scope(env->isolate());
// TODO(addaleax): pass the callback to the main script more directly,
// e.g. by making StartExecution(env, builtin) parametrizable
env->set_embedder_entry_point(std::move(cb));
auto reset_entry_point =
OnScopeLeave([&]() { env->set_embedder_entry_point({}); });

if (env->isolate_data()->options()->build_snapshot) {
// TODO(addaleax): pass the callback to the main script more directly,
// e.g. by making StartExecution(env, builtin) parametrizable
env->set_embedder_mksnapshot_entry_point(std::move(cb));
auto reset_entry_point =
OnScopeLeave([&]() { env->set_embedder_mksnapshot_entry_point({}); });
const char* entry = env->isolate_data()->options()->build_snapshot
? "internal/main/mksnapshot"
: "internal/main/embedding";

return StartExecution(env, "internal/main/mksnapshot");
}

if (StartExecution(env, "internal/main/environment").IsEmpty()) return {};
return scope.EscapeMaybe(cb({
env->process_object(),
env->builtin_module_require(),
}));
return scope.EscapeMaybe(StartExecution(env, entry));
}

// TODO(joyeecheung): move these conditions into JS land and let the
@@ -312,18 +307,6 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
first_argv = env->argv()[1];
}

#ifndef DISABLE_SINGLE_EXECUTABLE_APPLICATION
if (sea::IsSingleExecutable()) {
// TODO(addaleax): Find a way to reuse:
//
// LoadEnvironment(Environment*, const char*)
//
// instead and not add yet another main entry point here because this
// already duplicates existing code.
return StartExecution(env, "internal/main/single_executable_application");
}
#endif

if (first_argv == "inspect") {
return StartExecution(env, "internal/main/inspect");
}
4 changes: 2 additions & 2 deletions src/node.h
Original file line number Diff line number Diff line change
@@ -682,6 +682,7 @@ NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
struct StartExecutionCallbackInfo {
v8::Local<v8::Object> process_object;
v8::Local<v8::Function> native_require;
v8::Local<v8::Function> run_cjs;
};

using StartExecutionCallback =
@@ -691,8 +692,7 @@ NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
Environment* env,
StartExecutionCallback cb);
NODE_EXTERN v8::MaybeLocal<v8::Value> LoadEnvironment(
Environment* env,
const char* main_script_source_utf8);
Environment* env, std::string_view main_script_source_utf8);
NODE_EXTERN void FreeEnvironment(Environment* env);

// Set a callback that is called when process.exit() is called from JS,
29 changes: 7 additions & 22 deletions src/node_builtins.cc
Original file line number Diff line number Diff line change
@@ -185,17 +185,14 @@ static std::string OnDiskFileName(const char* id) {
MaybeLocal<String> BuiltinLoader::LoadBuiltinSource(Isolate* isolate,
const char* id) const {
auto source = source_.read();
#ifdef NODE_BUILTIN_MODULES_PATH
if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
#endif // NODE_BUILTIN_MODULES_PATH
const auto source_it = source->find(id);
if (UNLIKELY(source_it == source->end())) {
fprintf(stderr, "Cannot find native builtin: \"%s\".\n", id);
ABORT();
}
return source_it->second.ToStringChecked(isolate);
#ifdef NODE_BUILTIN_MODULES_PATH
#ifndef NODE_BUILTIN_MODULES_PATH
const auto source_it = source->find(id);
if (UNLIKELY(source_it == source->end())) {
fprintf(stderr, "Cannot find native builtin: \"%s\".\n", id);
ABORT();
}
return source_it->second.ToStringChecked(isolate);
#else // !NODE_BUILTIN_MODULES_PATH
std::string filename = OnDiskFileName(id);

std::string contents;
@@ -395,12 +392,6 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(Local<Context> context,
FIXED_ONE_BYTE_STRING(isolate, "internalBinding"),
FIXED_ONE_BYTE_STRING(isolate, "primordials"),
};
} else if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
// Synthetic embedder main scripts from LoadEnvironment(): process, require
parameters = {
FIXED_ONE_BYTE_STRING(isolate, "process"),
FIXED_ONE_BYTE_STRING(isolate, "require"),
};
} else {
// others: exports, require, module, process, internalBinding, primordials
parameters = {
@@ -457,12 +448,6 @@ MaybeLocal<Value> BuiltinLoader::CompileAndCall(Local<Context> context,
realm->builtin_module_require(),
realm->internal_binding_loader(),
realm->primordials()};
} else if (strncmp(id, "embedder_main_", strlen("embedder_main_")) == 0) {
// Synthetic embedder main scripts from LoadEnvironment(): process, require
arguments = {
realm->process_object(),
realm->builtin_module_require(),
};
} else {
// This should be invoked with the other CompileAndCall() methods, as
// we are unable to generate the arguments.
12 changes: 11 additions & 1 deletion src/node_main_instance.cc
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
#include "node_internals.h"
#include "node_options-inl.h"
#include "node_realm.h"
#include "node_sea.h"
#include "node_snapshot_builder.h"
#include "node_snapshotable.h"
#include "node_v8_platform-inl.h"
@@ -86,7 +87,16 @@ ExitCode NodeMainInstance::Run() {

void NodeMainInstance::Run(ExitCode* exit_code, Environment* env) {
if (*exit_code == ExitCode::kNoFailure) {
LoadEnvironment(env, StartExecutionCallback{});
bool is_sea = false;
#ifndef DISABLE_SINGLE_EXECUTABLE_APPLICATION
if (sea::IsSingleExecutable()) {
is_sea = true;
LoadEnvironment(env, sea::FindSingleExecutableCode());
}
#endif
if (!is_sea) {
LoadEnvironment(env, StartExecutionCallback{});
}

*exit_code =
SpinEventLoopInternal(env).FromMaybe(ExitCode::kGenericUserError);
Loading

0 comments on commit 3803b02

Please sign in to comment.