Skip to content

Commit 3efba59

Browse files
committed
fix: hold lwnode.dat file handle when the runtime start
Signed-off-by: Hosung Kim [email protected]
1 parent 6738e53 commit 3efba59

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

deps/node/src/lwnode/lwnode-public.cc

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
#include "trace.h"
2727
#include "v8.h"
2828

29+
namespace node {
30+
namespace native_module {
31+
extern bool initializeLWNodeBuiltinFile();
32+
}
33+
} // namespace node
34+
2935
using namespace node;
3036

3137
namespace lwnode {
@@ -47,10 +53,29 @@ class Runtime::Internal {
4753
kReleased
4854
};
4955

56+
enum ExitCode {
57+
kSuccess = 0,
58+
kFailure = 1,
59+
kNoBuiltinFile = 100,
60+
};
61+
62+
Internal() {
63+
LWNODE_DEV_LOG("[Runtime::Internal::Internal] new");
64+
65+
// Ensure that builtin file is loaded before initializing node.
66+
native_module::initializeLWNodeBuiltinFile();
67+
}
68+
5069
std::pair<bool, int> Init(int argc, char** argv) {
5170
if (state_ != State::kNotInitialized) {
5271
LWNODE_DEV_LOG("[Runtime::Internal::Init] already initialized");
53-
return std::make_pair(false, -1);
72+
return std::make_pair(true, ExitCode::kFailure);
73+
}
74+
75+
if (!native_module::initializeLWNodeBuiltinFile()) {
76+
LWNODE_DEV_LOG(
77+
"[Runtime::Internal::Init] failed to initialize builtin file");
78+
return std::make_pair(true, ExitCode::kNoBuiltinFile);
5479
}
5580

5681
LWNODE_DEV_LOG("[Runtime::Internal::Init]");

deps/node/src/node_main_lw_runner-inl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ class LWNodeMainRunner {
214214

215215
v8::V8::ShutdownPlatform();
216216

217+
environment_ = nullptr;
217218
return exit_code;
218219
}
219220

deps/node/src/node_native_module_lwnode-inl.h

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ enum class ReaderError {
7171
READ_FILE_FROMARCHIVE
7272
};
7373

74-
static thread_local ArchiveFileScope s_archiveFileScope;
74+
static ArchiveFileScope s_archiveFileScope;
75+
7576
static thread_local std::map<std::string, UnzFileCachedInfo>
7677
s_unzFileInfoDictionary;
7778
static thread_local ReaderError s_lastError = ReaderError::NO_ERROR;
@@ -86,6 +87,30 @@ std::string getSelfProcPath() {
8687
return std::string(path);
8788
}
8889

90+
static std::string getExternalBuiltinsPath() {
91+
static std::string s_externalBuiltinsPath;
92+
if (s_externalBuiltinsPath.empty()) {
93+
std::string executablePath = getSelfProcPath();
94+
executablePath = executablePath.substr(0, executablePath.rfind('/') + 1);
95+
s_externalBuiltinsPath = executablePath + LWNODE_EXTERNAL_BUILTINS_FILENAME;
96+
}
97+
return s_externalBuiltinsPath;
98+
}
99+
100+
bool initializeLWNodeBuiltinFile() {
101+
#ifdef LWNODE_EXTERNAL_BUILTINS_FILENAME
102+
std::string externalBuiltinsPath = getExternalBuiltinsPath();
103+
104+
if (s_archiveFileScope.isFileOpened() == false) {
105+
s_archiveFileScope.open(externalBuiltinsPath.c_str());
106+
}
107+
108+
return s_archiveFileScope.isFileOpened();
109+
#else
110+
return true;
111+
#endif
112+
}
113+
89114
void setError(ReaderError error) {
90115
s_lastError = error;
91116
ERROR_AND_ABORT(s_lastError);
@@ -197,16 +222,8 @@ FileData readFileFromArchive(std::string filename,
197222
size_t bufferSize = 0;
198223
char* buffer = nullptr;
199224

200-
static std::string s_externalBuiltinsPath;
201-
202-
if (s_externalBuiltinsPath.empty()) {
203-
std::string executablePath = getSelfProcPath();
204-
executablePath = executablePath.substr(0, executablePath.rfind('/') + 1);
205-
s_externalBuiltinsPath = executablePath + LWNODE_EXTERNAL_BUILTINS_FILENAME;
206-
}
207-
208225
if (readFileFromArchive(
209-
s_externalBuiltinsPath, filename, &buffer, &bufferSize) == false) {
226+
getExternalBuiltinsPath(), filename, &buffer, &bufferSize) == false) {
210227
setError(ReaderError::READ_FILE_FROMARCHIVE);
211228
return FileData();
212229
}

0 commit comments

Comments
 (0)