Skip to content
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
54 changes: 47 additions & 7 deletions deps/node/src/lwnode/lwnode-public.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,22 @@ class Runtime::Internal {
friend Runtime;

public:
enum class State {
kNotInitialized,
kInitialized,
kRunning,
kStopped,
kReleased
};

std::pair<bool, int> 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(
Expand All @@ -56,46 +70,72 @@ 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);
}

Runtime::~Runtime() {
delete internal_;
LWNODE_DEV_LOG("[Runtime::~Runtime]");
}

int Runtime::Start(int argc, char** argv, std::promise<void>&& promise) {
Expand Down
17 changes: 15 additions & 2 deletions deps/node/src/node_main_lw_runner-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ =
Expand All @@ -120,6 +124,7 @@ class LWNodeMainRunner {
int exit_code = 0;
DeleteFnPtr<Environment, FreeEnvironment> env_ =
nodeMainInstance.CreateMainEnvironment(&exit_code);
LWNODE_DEV_LOG("[LWNodeMainRunner::Run] create main environment");

CHECK_NOT_NULL(env_);

Expand All @@ -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_);
Expand All @@ -146,8 +156,10 @@ class LWNodeMainRunner {
// Run main loop
std::unique_ptr<MainLoopStrategy> mainLoop;
if (GmainLoopNodeBindings::isEnabled()) {
LWNODE_DEV_LOG("[LWNodeMainRunner::Run] use gmain loop");
mainLoop = std::make_unique<GmainLoopStrategy>();
} else {
LWNODE_DEV_LOG("[LWNodeMainRunner::Run] use standard loop");
mainLoop = std::make_unique<LoopStrategy>();
}

Expand Down Expand Up @@ -189,6 +201,7 @@ class LWNodeMainRunner {
}

void Stop() {
LWNODE_DEV_LOG("[LWNodeMainRunner::Stop]");
CHECK_NOT_NULL(environment_);
if (environment_->is_stopping()) {
return;
Expand Down
4 changes: 4 additions & 0 deletions src/api/utils/logger/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
12 changes: 11 additions & 1 deletion src/lwnode/nd-vm-main-message-port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <async-uv.h>
#include <channel.h>
#include <future>
#include "api/utils/logger/logger.h"
#include "es-helper.h"

using namespace Escargot;
Expand All @@ -35,6 +36,7 @@ MainMessagePort::MainMessagePort(std::shared_ptr<Port> port,
}

MainMessagePort::~MainMessagePort() {
LWNODE_DEV_LOG("[MainMessagePort::~MainMessagePort]");
Channel::DeletePendingMessages();
}

Expand All @@ -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_);
}