diff --git a/deps/node/src/lwnode/lwnode-public.cc b/deps/node/src/lwnode/lwnode-public.cc index 0889f4c567..256b81f6ac 100644 --- a/deps/node/src/lwnode/lwnode-public.cc +++ b/deps/node/src/lwnode/lwnode-public.cc @@ -37,8 +37,22 @@ class Runtime::Internal { friend Runtime; public: + enum class State { + kNotInitialized, + kInitialized, + kRunning, + kStopped, + kReleased + }; + std::pair Init(int argc, char** argv) { - is_initialized = true; + if (state_ != State::kNotInitialized) { + LWNODE_DEV_LOG("[Runtime::Init] already initialized"); + return std::make_pair(false, -1); + } + + LWNODE_DEV_LOG("[Runtime::Init]"); + state_ = State::kInitialized; // Set sendMessageSync callback to isolate context embedder data. runner_.SetOnMainEnvCreationCallback( @@ -56,39 +70,64 @@ class Runtime::Internal { } int Run() { - if (instance_ == nullptr) { + if (state_ != State::kInitialized) { + LWNODE_DEV_LOG("[Runtime::Run] not initialized"); return -1; } - return runner_.Run(*instance_); + CHECK_NOT_NULL(instance_); + LWNODE_DEV_LOG("[Runtime::Run]"); + state_ = State::kRunning; + + int result = runner_.Run(*instance_); + + state_ = State::kStopped; + + return result; } void Stop() { - if (instance_ == nullptr) { + if (state_ != State::kRunning) { + LWNODE_DEV_LOG("[Runtime::Stop] already stopped"); return; } + CHECK_NOT_NULL(instance_); + LWNODE_DEV_LOG("[Runtime::Stop]"); + state_ = State::kStopped; + runner_.Stop(); } void Free() { - if (is_initialized && instance_) { + if (state_ != State::kStopped && state_ != State::kInitialized) { + LWNODE_DEV_LOG("[Runtime::Free] not stopped"); + return; + } + + state_ = State::kReleased; + if (instance_) { + LWNODE_DEV_LOG("[Runtime::Free]"); DisposeNode(instance_); } + + instance_ = nullptr; } private: NodeMainInstance* instance_{nullptr}; LWNode::LWNodeMainRunner runner_; Runtime::Configuration config_; - bool is_initialized{false}; + State state_{State::kNotInitialized}; }; /************************************************************************** * Runtime class **************************************************************************/ -Runtime::Runtime() : internal_(new Internal()) {} +Runtime::Runtime() : internal_(new Internal()) { + LWNODE_DEV_LOG("[Runtime::Runtime]"); +} Runtime::Runtime(Configuration&& config) : Runtime() { internal_->config_ = std::move(config); @@ -96,6 +135,7 @@ Runtime::Runtime(Configuration&& config) : Runtime() { Runtime::~Runtime() { delete internal_; + LWNODE_DEV_LOG("[Runtime::~Runtime]"); } int Runtime::Start(int argc, char** argv, std::promise&& promise) { diff --git a/deps/node/src/node_main_lw_runner-inl.h b/deps/node/src/node_main_lw_runner-inl.h index da206cdf09..0c49728dc6 100644 --- a/deps/node/src/node_main_lw_runner-inl.h +++ b/deps/node/src/node_main_lw_runner-inl.h @@ -103,9 +103,13 @@ class LoopStrategy : public MainLoopStrategy { class LWNodeMainRunner { public: - ~LWNodeMainRunner() {} + ~LWNodeMainRunner() { + LWNODE_DEV_LOG("[LWNodeMainRunner::~LWNodeMainRunner]"); + } int Run(node::NodeMainInstance& nodeMainInstance) { + LWNODE_DEV_LOG("[LWNodeMainRunner::Run]"); + // To release array buffer allocator after node is finished, // this runner should has it. array_buffer_allocator_ = @@ -120,6 +124,7 @@ class LWNodeMainRunner { int exit_code = 0; DeleteFnPtr env_ = nodeMainInstance.CreateMainEnvironment(&exit_code); + LWNODE_DEV_LOG("[LWNodeMainRunner::Run] create main environment"); CHECK_NOT_NULL(env_); @@ -133,10 +138,15 @@ class LWNodeMainRunner { if (exit_code == 0) { LoadEnvironment(env_.get()); + LWNODE_DEV_LOG("[LWNodeMainRunner::Run] load environment"); env_->set_trace_sync_io(env_->options()->trace_sync_io); - promise_.set_value(); + try { + promise_.set_value(); + } catch (const std::exception& e) { + LWNODE_DEV_LOG("[LWNodeMainRunner::Run] promise error:", e.what()); + } { SealHandleScope seal(isolate_); @@ -146,8 +156,10 @@ class LWNodeMainRunner { // Run main loop std::unique_ptr mainLoop; if (GmainLoopNodeBindings::isEnabled()) { + LWNODE_DEV_LOG("[LWNodeMainRunner::Run] use gmain loop"); mainLoop = std::make_unique(); } else { + LWNODE_DEV_LOG("[LWNodeMainRunner::Run] use standard loop"); mainLoop = std::make_unique(); } @@ -189,6 +201,7 @@ class LWNodeMainRunner { } void Stop() { + LWNODE_DEV_LOG("[LWNodeMainRunner::Stop]"); CHECK_NOT_NULL(environment_); if (environment_->is_stopping()) { return; diff --git a/src/api/utils/logger/logger.h b/src/api/utils/logger/logger.h index 70720bf1c7..d2c40b29fd 100644 --- a/src/api/utils/logger/logger.h +++ b/src/api/utils/logger/logger.h @@ -22,7 +22,11 @@ // loggers using LWNodeLogger which supports dlog #define LWNODE_USER_LOG(...) LWNodeLogger(LogKind::user()).log(__VA_ARGS__) +#if defined(HOST_TIZEN) #define LWNODE_DEV_LOG(...) LWNodeLogger(LogKind::lwnode()).log(__VA_ARGS__) +#else +#define LWNODE_DEV_LOG(...) +#endif #define LWNODE_DEV_LOGF(fmt, ...) \ LWNodeLogger(LogKind::lwnode()).print(fmt, ##__VA_ARGS__) diff --git a/src/lwnode/nd-vm-main-message-port.cc b/src/lwnode/nd-vm-main-message-port.cc index 1e79c5c33c..e808736279 100644 --- a/src/lwnode/nd-vm-main-message-port.cc +++ b/src/lwnode/nd-vm-main-message-port.cc @@ -18,6 +18,7 @@ #include #include #include +#include "api/utils/logger/logger.h" #include "es-helper.h" using namespace Escargot; @@ -35,6 +36,7 @@ MainMessagePort::MainMessagePort(std::shared_ptr port, } MainMessagePort::~MainMessagePort() { + LWNODE_DEV_LOG("[MainMessagePort::~MainMessagePort]"); Channel::DeletePendingMessages(); } @@ -47,8 +49,16 @@ Escargot::FunctionObjectRef* MainMessagePort::MessageEventClass() { } void MainMessagePort::Init(ContextRef* context, uv_loop_t* loop) { + LWNODE_DEV_LOG("[MainMessagePort::Init]"); + context_ = context; uv_loop_ = loop; - internal_->uv_promise_.set_value(uv_loop_); + + try { + internal_->uv_promise_.set_value(uv_loop_); + } catch (const std::exception& e) { + LWNODE_DEV_LOG("[MainMessagePort::Init] promise error:", e.what()); + } + Channel::DrainPendingMessages(uv_loop_); }