Skip to content

Commit 88a0c21

Browse files
committed
feat: create Runtime public class
Signed-off-by: Hosung Kim [email protected]
1 parent c73473d commit 88a0c21

File tree

5 files changed

+129
-10
lines changed

5 files changed

+129
-10
lines changed

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

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,59 @@
2020
#include "lwnode.h"
2121
#include "lwnode/aul-event-receiver.h"
2222
#include "node.h"
23+
#include "node_main_lw_runner-inl.h"
2324
#include "trace.h"
2425

26+
using namespace node;
27+
2528
namespace lwnode {
2629

30+
class Runtime::Internal {
31+
friend Runtime;
32+
33+
public:
34+
~Internal() {
35+
if (is_initialized && instance_) {
36+
DisposeNode(instance_);
37+
}
38+
}
39+
40+
std::pair<bool, int> Init(int argc, char** argv) {
41+
is_initialized = true;
42+
return InitializeNode(argc, argv, &instance_);
43+
}
44+
45+
int Run() {
46+
if (instance_ == nullptr) {
47+
return -1;
48+
}
49+
50+
return runner_.Run(*instance_);
51+
;
52+
}
53+
54+
private:
55+
NodeMainInstance* instance_{nullptr};
56+
LWNode::LWNodeMainRunner runner_;
57+
bool is_initialized{false};
58+
};
59+
60+
Runtime::Runtime() {
61+
internal_ = new Internal();
62+
}
63+
64+
Runtime::~Runtime() {
65+
delete internal_;
66+
}
67+
68+
std::pair<bool, int> Runtime::Init(int argc, char** argv) {
69+
return internal_->Init(argc, argv);
70+
}
71+
72+
int Runtime::Run() {
73+
return internal_->Run();
74+
}
75+
2776
bool ParseAULEvent(int argc, char** argv) {
2877
bool result = AULEventReceiver::getInstance()->start(argc, argv);
2978
if (result) {
@@ -73,8 +122,4 @@ void SetDlogID(const std::string& tag) {
73122
#endif
74123
}
75124

76-
int Start(int argc, char** argv) {
77-
return node::Start(argc, argv);
78-
}
79-
80125
} // namespace lwnode

deps/node/src/node.cc

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,13 +1069,55 @@ void TearDownOncePerProcess() {
10691069
per_process::v8_platform.Dispose();
10701070
}
10711071

1072+
#if defined(LWNODE)
1073+
std::pair<bool, int> InitializeNode(int argc, char** argv, NodeMainInstance** main_instance) {
1074+
InitializationResult result = InitializeOncePerProcess(argc, argv);
1075+
if (result.early_return) {
1076+
return std::make_pair(true, result.exit_code);
1077+
}
1078+
1079+
Isolate::CreateParams params;
1080+
const std::vector<size_t>* indexes = nullptr;
1081+
std::vector<intptr_t> external_references;
1082+
1083+
bool force_no_snapshot =
1084+
per_process::cli_options->per_isolate->no_node_snapshot;
1085+
if (!force_no_snapshot) {
1086+
v8::StartupData* blob = NodeMainInstance::GetEmbeddedSnapshotBlob();
1087+
if (blob != nullptr) {
1088+
// TODO(joyeecheung): collect external references and set it in
1089+
// params.external_references.
1090+
external_references.push_back(reinterpret_cast<intptr_t>(nullptr));
1091+
params.external_references = external_references.data();
1092+
params.snapshot_blob = blob;
1093+
indexes = NodeMainInstance::GetIsolateDataIndexes();
1094+
}
1095+
}
1096+
uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME);
1097+
1098+
*main_instance = new NodeMainInstance(&params,
1099+
uv_default_loop(),
1100+
per_process::v8_platform.Platform(),
1101+
result.args,
1102+
result.exec_args,
1103+
indexes);
1104+
return std::make_pair(false, result.exit_code);
1105+
}
1106+
1107+
void DisposeNode(NodeMainInstance* main_instance) {
1108+
delete main_instance;
1109+
1110+
TearDownOncePerProcess();
1111+
}
1112+
#endif
1113+
10721114
int Start(int argc, char** argv) {
10731115
InitializationResult result = InitializeOncePerProcess(argc, argv);
10741116
if (result.early_return) {
10751117
return result.exit_code;
10761118
}
10771119

1078-
#if defined(LWNODE)
1120+
#if defined(LWNODE)
10791121
LWNode::LWNodeMainRunner nodeMainRunner; // @lwnode
10801122
{
10811123
Isolate::CreateParams params;

deps/node/src/node.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,12 @@ void RegisterSignalHandler(int signal,
10691069
bool reset_handler = false);
10701070
#endif // _WIN32
10711071

1072+
#ifdef LWNODE // @lwnode
1073+
class NodeMainInstance;
1074+
std::pair<bool, int> InitializeNode(int argc, char** argv, NodeMainInstance** main_instance);
1075+
void DisposeNode(NodeMainInstance* main_instance);
1076+
#endif
1077+
10721078
} // namespace node
10731079

10741080
#endif // SRC_NODE_H_

deps/node/src/node_main_lw.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,24 @@ int main(int argc, char* argv[]) {
7171
setvbuf(stdout, nullptr, _IONBF, 0);
7272
setvbuf(stderr, nullptr, _IONBF, 0);
7373

74+
lwnode::Runtime runtime;
75+
std::pair<bool, int> init_result; // early_return, exit_code
76+
7477
if (lwnode::ParseAULEvent(argc, argv)) {
7578
if (!lwnode::InitScriptRootPath()) {
7679
exit(-errno);
7780
}
7881

7982
char* args[] = {"", "index.js", nullptr};
80-
return lwnode::Start(2, args);
83+
init_result = runtime.Init(2, args);
84+
} else {
85+
// started by command line
86+
init_result = runtime.Init(argc, argv);
87+
}
88+
89+
if (init_result.first) {
90+
return init_result.second;
8191
}
8292

83-
// started by command line
84-
return lwnode::Start(argc, argv);
93+
return runtime.Run();
8594
}

include/lwnode/lwnode-public.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,26 @@ LWNODE_EXPORT bool ParseAULEvent(int argc, char** argv);
3232
// function.
3333
LWNODE_EXPORT bool InitScriptRootPath(const std::string path = "");
3434

35-
// Sets the dlog tag id for debugging. This is only used on Tizen when not in AUL mode.
35+
// Sets the dlog tag id for debugging. This is only used on Tizen when not in
36+
// AUL mode.
3637
LWNODE_EXPORT void SetDlogID(const std::string& appId);
3738

38-
LWNODE_EXPORT int Start(int argc, char** argv);
39+
class LWNODE_EXPORT Runtime {
40+
public:
41+
Runtime();
42+
~Runtime();
43+
44+
// Initializes the runtime and returns true if it early termination and sets
45+
// the exit code to the second return value. Otherwise, it returns false
46+
// and you need call Run() to run the runtime.
47+
std::pair<bool, int> Init(int argc, char** argv);
48+
49+
// Runs the runtime and returns the exit code. The runtime must be initialized.
50+
int Run();
51+
52+
private:
53+
class Internal;
54+
Internal* internal_ = nullptr;
55+
};
3956

4057
} // namespace lwnode

0 commit comments

Comments
 (0)