Skip to content

Commit 397f384

Browse files
committed
sea: fix entry point file name
Refs: nodejs#48191 (comment) Signed-off-by: Darshan Sen <[email protected]>
1 parent d17be31 commit 397f384

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

lib/internal/util/embedding.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { BuiltinModule: { normalizeRequirableId } } = require('internal/bootstrap/realm');
33
const { Module, wrapSafe } = require('internal/modules/cjs/loader');
44
const { codes: { ERR_UNKNOWN_BUILTIN_MODULE } } = require('internal/errors');
5-
const { getCodeCache } = internalBinding('sea');
5+
const { getCodeCache, getCodePath } = internalBinding('sea');
66

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

1717
function embedderRunCjs(contents) {
1818
const filename = process.execPath;
19-
const compiledWrapper = wrapSafe(filename, contents, undefined, getCodeCache());
19+
const codeCache = getCodeCache();
20+
const compiledWrapper = wrapSafe(codeCache ? getCodePath() : filename, contents, undefined, codeCache);
2021

2122
const customModule = new Module(filename, null);
2223
customModule.filename = filename;

src/node_sea.cc

+35-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ size_t SeaSerializer::Write(const SeaResource& sea) {
8383
written_total += WriteArithmetic<uint32_t>(flags);
8484
DCHECK_EQ(written_total, SeaResource::kHeaderSize);
8585

86+
Debug("Write SEA code path %p, size=%zu\n",
87+
sea.code_path.data(),
88+
sea.code_path.size());
89+
written_total +=
90+
WriteStringView(sea.code_path, StringLogMode::kAddressAndContent);
91+
8692
Debug("Write SEA resource code %p, size=%zu\n",
8793
sea.code.data(),
8894
sea.code.size());
@@ -117,14 +123,19 @@ SeaResource SeaDeserializer::Read() {
117123
Debug("Read SEA flags %x\n", static_cast<uint32_t>(flags));
118124
CHECK_EQ(read_total, SeaResource::kHeaderSize);
119125

126+
std::string_view code_path =
127+
ReadStringView(StringLogMode::kAddressAndContent);
128+
Debug(
129+
"Read SEA code path %p, size=%zu\n", code_path.data(), code_path.size());
130+
120131
std::string_view code = ReadStringView(StringLogMode::kAddressAndContent);
121132
Debug("Read SEA resource code %p, size=%zu\n", code.data(), code.size());
122133

123134
std::string_view code_cache = ReadStringView(StringLogMode::kAddressOnly);
124135
Debug("Read SEA resource code cache %p, size=%zu\n",
125136
code_cache.data(),
126137
code_cache.size());
127-
return {flags, code, code_cache};
138+
return {flags, code_path, code, code_cache};
128139
}
129140

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

222+
void GetCodePath(const FunctionCallbackInfo<Value>& args) {
223+
if (!IsSingleExecutable()) {
224+
return;
225+
}
226+
227+
Environment* env = Environment::GetCurrent(args);
228+
Isolate* isolate = env->isolate();
229+
HandleScope scope(isolate);
230+
231+
SeaResource sea_resource = FindSingleExecutableResource();
232+
233+
Local<String> code_path;
234+
if (!String::NewFromUtf8(isolate, sea_resource.code_path.data())
235+
.ToLocal(&code_path)) {
236+
return;
237+
}
238+
239+
args.GetReturnValue().Set(code_path);
240+
}
241+
211242
std::tuple<int, char**> FixupArgsForSEA(int argc, char** argv) {
212243
// Repeats argv[0] at position 1 on argv as a replacement for the missing
213244
// entry point file path.
@@ -346,7 +377,7 @@ ExitCode GenerateSingleExecutableBlob(const SeaConfig& config) {
346377
}
347378
std::string code_cache = optional_code_cache.value();
348379

349-
SeaResource sea{config.flags, main_script, code_cache};
380+
SeaResource sea{config.flags, config.main_path, main_script, code_cache};
350381

351382
SeaSerializer serializer;
352383
serializer.Write(sea);
@@ -386,11 +417,13 @@ void Initialize(Local<Object> target,
386417
target,
387418
"isExperimentalSeaWarningNeeded",
388419
IsExperimentalSeaWarningNeeded);
420+
SetMethod(context, target, "getCodePath", GetCodePath);
389421
SetMethod(context, target, "getCodeCache", GetCodeCache);
390422
}
391423

392424
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
393425
registry->Register(IsExperimentalSeaWarningNeeded);
426+
registry->Register(GetCodePath);
394427
registry->Register(GetCodeCache);
395428
}
396429

src/node_sea.h

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ enum class SeaFlags : uint32_t {
2525

2626
struct SeaResource {
2727
SeaFlags flags = SeaFlags::kDefault;
28+
std::string_view code_path;
2829
std::string_view code;
2930
std::string_view code_cache;
3031

test/fixtures/sea.js

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ if (createdRequire('./sea-config.json').disableExperimentalSEAWarning) {
1818
const { deepStrictEqual, strictEqual, throws } = require('assert');
1919
const { dirname } = require('node:path');
2020

21+
// Checks that the source filename is used in the error stack trace.
22+
strictEqual(new Error('lol').stack.split('\n')[1], ' at sea.js:22:13');
23+
2124
// Should be possible to require a core module that requires using the "node:"
2225
// scheme.
2326
{

0 commit comments

Comments
 (0)