Skip to content

Commit 2ae8f2e

Browse files
hs0225daeyeon
authored andcommitted
feat: add lwnode_data_path config
Signed-off-by: Hosung Kim [email protected]
1 parent dfae7c2 commit 2ae8f2e

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
namespace node {
3030
namespace native_module {
31-
extern bool initializeLWNodeBuiltinFile();
31+
extern bool initializeLWNodeBuiltinFile(const std::string path = "");
3232
}
3333
} // namespace node
3434

@@ -39,6 +39,7 @@ namespace lwnode {
3939
struct Runtime::Configuration::Internal {
4040
Runtime::SendMessageSyncCallback send_message_sync_callback{nullptr};
4141
void* send_message_sync_callback_data{nullptr};
42+
std::string lwnode_data_path;
4243
};
4344

4445
class Runtime::Internal {
@@ -59,20 +60,16 @@ class Runtime::Internal {
5960
kNoBuiltinFile = 100,
6061
};
6162

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-
}
63+
Internal() { LWNODE_DEV_LOG("[Runtime::Internal::Internal] new"); }
6864

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

75-
if (!native_module::initializeLWNodeBuiltinFile()) {
71+
if (!native_module::initializeLWNodeBuiltinFile(
72+
config_.internal_->lwnode_data_path)) {
7673
LWNODE_DEV_LOG(
7774
"[Runtime::Internal::Init] failed to initialize builtin file");
7875
return std::make_pair(true, ExitCode::kNoBuiltinFile);
@@ -224,8 +221,15 @@ void Runtime::Configuration::OnSendMessageSync(
224221
internal_->send_message_sync_callback_data = user_data;
225222
}
226223

227-
bool Runtime::Configuration::Set(const std::string& key,
228-
const std::string& value) {
224+
bool Runtime::Configuration::Set(const std::string& key, const char* value) {
225+
std::string value_string = value ? value : "";
226+
227+
if (key == "lwnode_data_path") {
228+
LWNODE_DEV_LOGF("[Runtime::Configuration::Set] data path set to %s",
229+
value_string.c_str());
230+
internal_->lwnode_data_path = value_string;
231+
return true;
232+
}
229233
return false;
230234
}
231235

deps/node/src/node_native_module_lwnode-inl.h

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@
2727

2828
using namespace LWNode;
2929

30+
std::string getSelfProcPath() {
31+
char path[PATH_MAX + 1];
32+
ssize_t length = readlink("/proc/self/exe", path, PATH_MAX);
33+
if (length < 0) {
34+
ERROR_AND_ABORT("readlink fails");
35+
}
36+
path[length] = '\0';
37+
return std::string(path);
38+
}
39+
40+
std::string getExternalBuiltinsPath() {
41+
std::string executablePath = getSelfProcPath();
42+
executablePath = executablePath.substr(0, executablePath.rfind('/') + 1);
43+
44+
return executablePath + LWNODE_EXTERNAL_BUILTINS_FILENAME;
45+
}
46+
3047
namespace node {
3148
namespace native_module {
3249
using v8::Isolate;
@@ -76,29 +93,14 @@ static thread_local std::map<std::string, UnzFileCachedInfo>
7693
s_unzFileInfoDictionary;
7794
static thread_local ReaderError s_lastError = ReaderError::NO_ERROR;
7895

79-
std::string getSelfProcPath() {
80-
char path[PATH_MAX + 1];
81-
ssize_t length = readlink("/proc/self/exe", path, PATH_MAX);
82-
if (length < 0) {
83-
ERROR_AND_ABORT("readlink fails");
84-
}
85-
path[length] = '\0';
86-
return std::string(path);
87-
}
96+
bool initializeLWNodeBuiltinFile(const std::string path = "") {
97+
#ifdef LWNODE_EXTERNAL_BUILTINS_FILENAME
8898

89-
static std::string getExternalBuiltinsPath() {
90-
static std::string s_externalBuiltinsPath;
91-
if (s_externalBuiltinsPath.empty()) {
92-
std::string executablePath = getSelfProcPath();
93-
executablePath = executablePath.substr(0, executablePath.rfind('/') + 1);
94-
s_externalBuiltinsPath = executablePath + LWNODE_EXTERNAL_BUILTINS_FILENAME;
95-
}
96-
return s_externalBuiltinsPath;
97-
}
99+
std::string externalBuiltinsPath = path;
98100

99-
bool initializeLWNodeBuiltinFile() {
100-
#ifdef LWNODE_EXTERNAL_BUILTINS_FILENAME
101-
std::string externalBuiltinsPath = getExternalBuiltinsPath();
101+
if (path.empty()) {
102+
externalBuiltinsPath = getExternalBuiltinsPath();
103+
}
102104

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

include/lwnode/lwnode-public.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class LWNODE_EXPORT Runtime {
6565

6666
void OnSendMessageSync(SendMessageSyncCallback callback, void* user_data);
6767

68-
bool Set(const std::string& key, const std::string& value);
68+
bool Set(const std::string& key, const char* value);
6969
bool Set(const std::string& key, int value);
7070
bool Set(const std::string& key, bool value);
7171

@@ -93,8 +93,8 @@ class LWNODE_EXPORT Runtime {
9393
int Start(int argc, char** argv, std::promise<void>&& promise);
9494

9595
/**
96-
* Stop the runtime. You can use this function to stop the runtime from another
97-
* thread.
96+
* Stop the runtime. You can use this function to stop the runtime from
97+
* another thread.
9898
**/
9999
void Stop();
100100

0 commit comments

Comments
 (0)