Skip to content

Commit

Permalink
sea: fix entry point file name
Browse files Browse the repository at this point in the history
Refs: nodejs#48191 (comment)
Signed-off-by: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
RaisinTen committed May 29, 2023
1 parent d17be31 commit 397f384
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/internal/util/embedding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm');
const { Module, wrapSafe } = require('internal/modules/cjs/loader');
const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors');
const { getCodeCache } = internalBinding('sea');
const { getCodeCache, getCodePath } = internalBinding('sea');

// This is roughly the same as:
//
Expand All @@ -16,7 +16,8 @@ const { getCodeCache } = internalBinding('sea');

function embedderRunCjs(contents) {
const filename = process.execPath;
const compiledWrapper = wrapSafe(filename, contents, undefined, getCodeCache());
const codeCache = getCodeCache();
const compiledWrapper = wrapSafe(codeCache ? getCodePath() : filename, contents, undefined, codeCache);

const customModule = new Module(filename, null);
customModule.filename = filename;
Expand Down
37 changes: 35 additions & 2 deletions src/node_sea.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ size_t SeaSerializer::Write(const SeaResource& sea) {
written_total += WriteArithmetic<uint32_t>(flags);
DCHECK_EQ(written_total, SeaResource::kHeaderSize);

Debug("Write SEA code path %p, size=%zu\n",
sea.code_path.data(),
sea.code_path.size());
written_total +=
WriteStringView(sea.code_path, StringLogMode::kAddressAndContent);

Debug("Write SEA resource code %p, size=%zu\n",
sea.code.data(),
sea.code.size());
Expand Down Expand Up @@ -117,14 +123,19 @@ SeaResource SeaDeserializer::Read() {
Debug("Read SEA flags %x\n", static_cast<uint32_t>(flags));
CHECK_EQ(read_total, SeaResource::kHeaderSize);

std::string_view code_path =
ReadStringView(StringLogMode::kAddressAndContent);
Debug(
"Read SEA code path %p, size=%zu\n", code_path.data(), code_path.size());

std::string_view code = ReadStringView(StringLogMode::kAddressAndContent);
Debug("Read SEA resource code %p, size=%zu\n", code.data(), code.size());

std::string_view code_cache = ReadStringView(StringLogMode::kAddressOnly);
Debug("Read SEA resource code cache %p, size=%zu\n",
code_cache.data(),
code_cache.size());
return {flags, code, code_cache};
return {flags, code_path, code, code_cache};
}

std::string_view FindSingleExecutableBlob() {
Expand Down Expand Up @@ -208,6 +219,26 @@ void GetCodeCache(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(buf);
}

void GetCodePath(const FunctionCallbackInfo<Value>& args) {
if (!IsSingleExecutable()) {
return;
}

Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
HandleScope scope(isolate);

SeaResource sea_resource = FindSingleExecutableResource();

Local<String> code_path;
if (!String::NewFromUtf8(isolate, sea_resource.code_path.data())
.ToLocal(&code_path)) {
return;
}

args.GetReturnValue().Set(code_path);
}

std::tuple<int, char**> FixupArgsForSEA(int argc, char** argv) {
// Repeats argv[0] at position 1 on argv as a replacement for the missing
// entry point file path.
Expand Down Expand Up @@ -346,7 +377,7 @@ ExitCode GenerateSingleExecutableBlob(const SeaConfig& config) {
}
std::string code_cache = optional_code_cache.value();

SeaResource sea{config.flags, main_script, code_cache};
SeaResource sea{config.flags, config.main_path, main_script, code_cache};

SeaSerializer serializer;
serializer.Write(sea);
Expand Down Expand Up @@ -386,11 +417,13 @@ void Initialize(Local<Object> target,
target,
"isExperimentalSeaWarningNeeded",
IsExperimentalSeaWarningNeeded);
SetMethod(context, target, "getCodePath", GetCodePath);
SetMethod(context, target, "getCodeCache", GetCodeCache);
}

void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(IsExperimentalSeaWarningNeeded);
registry->Register(GetCodePath);
registry->Register(GetCodeCache);
}

Expand Down
1 change: 1 addition & 0 deletions src/node_sea.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum class SeaFlags : uint32_t {

struct SeaResource {
SeaFlags flags = SeaFlags::kDefault;
std::string_view code_path;
std::string_view code;
std::string_view code_cache;

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/sea.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ if (createdRequire('./sea-config.json').disableExperimentalSEAWarning) {
const { deepStrictEqual, strictEqual, throws } = require('assert');
const { dirname } = require('node:path');

// Checks that the source filename is used in the error stack trace.
strictEqual(new Error('lol').stack.split('\n')[1], ' at sea.js:22:13');

// Should be possible to require a core module that requires using the "node:"
// scheme.
{
Expand Down

0 comments on commit 397f384

Please sign in to comment.