Skip to content

Commit 389a098

Browse files
hs0225daeyeon
authored andcommitted
feat: add Runtime::Free
Signed-off-by: Hosung Kim [email protected]
1 parent b85c381 commit 389a098

File tree

6 files changed

+25
-13
lines changed

6 files changed

+25
-13
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ class Runtime::Internal {
3131
friend Runtime;
3232

3333
public:
34-
~Internal() {
35-
if (is_initialized && instance_) {
36-
DisposeNode(instance_);
37-
}
38-
}
39-
4034
std::pair<bool, int> Init(int argc, char** argv) {
4135
is_initialized = true;
4236
return InitializeNode(argc, argv, &instance_);
@@ -48,7 +42,12 @@ class Runtime::Internal {
4842
}
4943

5044
return runner_.Run(*instance_);
51-
;
45+
}
46+
47+
void Free() {
48+
if (is_initialized && instance_) {
49+
DisposeNode(instance_);
50+
}
5251
}
5352

5453
private:
@@ -72,6 +71,10 @@ std::pair<bool, int> Runtime::Init(int argc,
7271
return internal_->Init(argc, argv);
7372
}
7473

74+
void Runtime::Free() {
75+
return internal_->Free();
76+
}
77+
7578
int Runtime::Run() {
7679
return internal_->Run();
7780
}

deps/node/src/node_main_lw.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ int main(int argc, char* argv[]) {
9090
}
9191

9292
if (init_result.first) {
93+
runtime.Free();
9394
return init_result.second;
9495
}
9596

96-
return runtime.Run();
97+
int result = runtime.Run();
98+
runtime.Free();
99+
100+
return result;
97101
}

deps/node/src/node_main_lw_runner-inl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class LoopStrategy : public MainLoopStrategy {
103103

104104
class LWNodeMainRunner {
105105
public:
106-
~LWNodeMainRunner() { v8::V8::ShutdownPlatform(); }
106+
~LWNodeMainRunner() {}
107107

108108
int Run(node::NodeMainInstance& nodeMainInstance) {
109109
// To release array buffer allocator after node is finished,
@@ -178,6 +178,9 @@ class LWNodeMainRunner {
178178
#if defined(LEAK_SANITIZER)
179179
__lsan_do_leak_check();
180180
#endif
181+
182+
v8::V8::ShutdownPlatform();
183+
181184
return exit_code;
182185
}
183186

include/lwnode/lwnode-public.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class LWNODE_EXPORT Runtime {
5959
// initialized.
6060
int Run();
6161

62+
void Free();
63+
6264
std::shared_ptr<Port> GetPort();
6365

6466
private:

test/embedding/embedtest.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ std::string getTimestamp() {
2828
}
2929

3030
TEST0(Embedtest, MessagePort2_Post_Many_JS_First) {
31-
auto runtime = new lwnode::Runtime();
31+
auto runtime = std::make_shared<lwnode::Runtime>();
3232

3333
std::promise<void> promise;
3434
std::future<void> init_future = promise.get_future();
@@ -44,6 +44,7 @@ TEST0(Embedtest, MessagePort2_Post_Many_JS_First) {
4444
[&](std::promise<void>&& promise) mutable {
4545
runtime->Init(COUNT_OF(args), args, std::move(promise));
4646
runtime->Run();
47+
runtime->Free();
4748
},
4849
std::move(promise));
4950

test/embedding/example.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
#define COUNT_OF(array) (sizeof(array) / sizeof((array)[0]))
1010

1111
int main(int argc, char* argv[]) {
12-
// FIXME: Fix to ensure the runtime is deleted without crashing.
13-
// auto runtime = std::make_shared<lwnode::Runtime>();
14-
auto runtime = new lwnode::Runtime();
12+
auto runtime = std::make_shared<lwnode::Runtime>();
1513

1614
std::promise<void> promise;
1715
std::future<void> init_future = promise.get_future();
@@ -26,6 +24,7 @@ int main(int argc, char* argv[]) {
2624
// promise directly to know when that is.
2725
runtime->Init(COUNT_OF(args), args, std::move(promise));
2826
runtime->Run();
27+
runtime->Free();
2928
},
3029
std::move(promise));
3130

0 commit comments

Comments
 (0)