Skip to content

fix: hold lwnode.dat file handle when the runtime start #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion deps/node/src/lwnode/lwnode-public.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#include "trace.h"
#include "v8.h"

namespace node {
namespace native_module {
extern bool initializeLWNodeBuiltinFile();
}
} // namespace node

using namespace node;

namespace lwnode {
Expand All @@ -47,10 +53,29 @@ class Runtime::Internal {
kReleased
};

enum ExitCode {
kSuccess = 0,
kFailure = 1,
kNoBuiltinFile = 100,
};

Internal() {
LWNODE_DEV_LOG("[Runtime::Internal::Internal] new");

// Ensure that builtin file is loaded before initializing node.
native_module::initializeLWNodeBuiltinFile();
}

std::pair<bool, int> Init(int argc, char** argv) {
if (state_ != State::kNotInitialized) {
LWNODE_DEV_LOG("[Runtime::Internal::Init] already initialized");
return std::make_pair(false, -1);
return std::make_pair(true, ExitCode::kFailure);
}

if (!native_module::initializeLWNodeBuiltinFile()) {
LWNODE_DEV_LOG(
"[Runtime::Internal::Init] failed to initialize builtin file");
return std::make_pair(true, ExitCode::kNoBuiltinFile);
}

LWNODE_DEV_LOG("[Runtime::Internal::Init]");
Expand Down
1 change: 1 addition & 0 deletions deps/node/src/node_main_lw_runner-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ class LWNodeMainRunner {

v8::V8::ShutdownPlatform();

environment_ = nullptr;
return exit_code;
}

Expand Down
34 changes: 25 additions & 9 deletions deps/node/src/node_native_module_lwnode-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ std::string getSelfProcPath() {
return std::string(path);
}

static std::string getExternalBuiltinsPath() {
static std::string s_externalBuiltinsPath;
if (s_externalBuiltinsPath.empty()) {
std::string executablePath = getSelfProcPath();
executablePath = executablePath.substr(0, executablePath.rfind('/') + 1);
s_externalBuiltinsPath = executablePath + LWNODE_EXTERNAL_BUILTINS_FILENAME;
}
return s_externalBuiltinsPath;
}

bool initializeLWNodeBuiltinFile() {
#ifdef LWNODE_EXTERNAL_BUILTINS_FILENAME
std::string externalBuiltinsPath = getExternalBuiltinsPath();

if (s_archiveFileScope.isFileOpened() == false) {
s_archiveFileScope.open(externalBuiltinsPath.c_str());
}

return s_archiveFileScope.isFileOpened();
#else
return true;
#endif
}

void setError(ReaderError error) {
s_lastError = error;
ERROR_AND_ABORT(s_lastError);
Expand Down Expand Up @@ -197,16 +221,8 @@ FileData readFileFromArchive(std::string filename,
size_t bufferSize = 0;
char* buffer = nullptr;

static std::string s_externalBuiltinsPath;

if (s_externalBuiltinsPath.empty()) {
std::string executablePath = getSelfProcPath();
executablePath = executablePath.substr(0, executablePath.rfind('/') + 1);
s_externalBuiltinsPath = executablePath + LWNODE_EXTERNAL_BUILTINS_FILENAME;
}

if (readFileFromArchive(
s_externalBuiltinsPath, filename, &buffer, &bufferSize) == false) {
getExternalBuiltinsPath(), filename, &buffer, &bufferSize) == false) {
setError(ReaderError::READ_FILE_FROMARCHIVE);
return FileData();
}
Expand Down
4 changes: 2 additions & 2 deletions include/lwnode/lwnode-version.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@

#define LWNODE_VERSION_MAJOR 1
#define LWNODE_VERSION_MINOR 0
#define LWNODE_VERSION_PATCH 10
#define LWNODE_VERSION_TAG "v1.0.10"
#define LWNODE_VERSION_PATCH 11
#define LWNODE_VERSION_TAG "v1.0.11"