From d9f9989fb546e05000fd2c254bfa6c33906f6532 Mon Sep 17 00:00:00 2001 From: Hosung Kim Date: Thu, 27 Mar 2025 16:40:43 +0900 Subject: [PATCH 1/2] feat: add sendMessageSync API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Hosung Kim hs852.kim@samsung.com --- deps/node/lib/internal/lwnode/setup.js | 9 +++ deps/node/src/lwnode/lwnode-public.cc | 57 ++++++++++++++++++- deps/node/src/node_main_lw_runner-inl.h | 11 ++++ include/lwnode/lwnode-public.h | 46 +++++++++++---- include/lwnode/lwnode.h | 2 + src/lwnode/lwnode.cc | 30 ++++++++++ test/embedding/CMakeLists.txt | 1 + test/embedding/binding.cc | 74 +++++++++++++++++++++++++ test/embedding/test-10-binding-basic.js | 32 +++++++++++ 9 files changed, 248 insertions(+), 14 deletions(-) create mode 100644 test/embedding/binding.cc create mode 100644 test/embedding/test-10-binding-basic.js diff --git a/deps/node/lib/internal/lwnode/setup.js b/deps/node/lib/internal/lwnode/setup.js index 52ccfd407e..59b781be20 100644 --- a/deps/node/lib/internal/lwnode/setup.js +++ b/deps/node/lib/internal/lwnode/setup.js @@ -95,6 +95,15 @@ function wrapLWNodeMethods(binding) { return binding.hasSystemInfo.apply(null, args); } }, + binding: (message) => { + if (typeof message !== "string") { + throw new TypeError("The message argument must be a string"); + } + + if (binding.binding) { + return binding.binding(message); + } + } }; setupMessagePort(object, binding); diff --git a/deps/node/src/lwnode/lwnode-public.cc b/deps/node/src/lwnode/lwnode-public.cc index 1efd417e2d..8450b839e3 100644 --- a/deps/node/src/lwnode/lwnode-public.cc +++ b/deps/node/src/lwnode/lwnode-public.cc @@ -22,17 +22,34 @@ #include "node.h" #include "node_main_lw_runner-inl.h" #include "trace.h" +#include "v8.h" using namespace node; namespace lwnode { +struct Runtime::Configuration::Internal { + Runtime::BindingCallback binding_callback{nullptr}; + void* binding_callback_data{nullptr}; +}; + class Runtime::Internal { friend Runtime; public: std::pair Init(int argc, char** argv) { is_initialized = true; + + // Set binding callback to isolate context embedder data. + runner_.SetOnCreatedContextCallback([this](v8::Local context) { + context->SetAlignedPointerInEmbedderData( + LWNode::ContextEmbedderIndex::kBindingCallback, + reinterpret_cast(config_.internal_->binding_callback)); + context->SetAlignedPointerInEmbedderData( + LWNode::ContextEmbedderIndex::kBindingCallbackData, + config_.internal_->binding_callback_data); + }); + return InitializeNode(argc, argv, &instance_); } @@ -53,11 +70,18 @@ class Runtime::Internal { private: NodeMainInstance* instance_{nullptr}; LWNode::LWNodeMainRunner runner_; + Runtime::Configuration config_; bool is_initialized{false}; }; -Runtime::Runtime() { - internal_ = new Internal(); +/************************************************************************** + * Runtime class + **************************************************************************/ + +Runtime::Runtime() : internal_(new Internal()) {} + +Runtime::Runtime(Configuration&& config) : Runtime() { + internal_->config_ = std::move(config); } Runtime::~Runtime() { @@ -83,6 +107,35 @@ std::shared_ptr Runtime::GetPort() { return internal_->runner_.GetPort(); } +/************************************************************************** + * Runtime::Configuration class + **************************************************************************/ + +Runtime::Configuration::Configuration() + : internal_(new Runtime::Configuration::Internal()) {} + +Runtime::Configuration::~Configuration() { + delete internal_; +} + +Runtime::Configuration& Runtime::Configuration::operator=( + Configuration&& other) { + delete internal_; + internal_ = other.internal_; + other.internal_ = nullptr; + return *this; +} + +void Runtime::Configuration::SetBindingCallback( + Runtime::BindingCallback callback, void* user_data) { + internal_->binding_callback = callback; + internal_->binding_callback_data = user_data; +} + +/************************************************************************** + * Static functions + **************************************************************************/ + bool ParseAULEvent(int argc, char** argv) { bool result = AULEventReceiver::getInstance()->start(argc, argv); if (result) { diff --git a/deps/node/src/node_main_lw_runner-inl.h b/deps/node/src/node_main_lw_runner-inl.h index d8f7741737..482924da74 100644 --- a/deps/node/src/node_main_lw_runner-inl.h +++ b/deps/node/src/node_main_lw_runner-inl.h @@ -127,6 +127,10 @@ class LWNodeMainRunner { Context::Scope context_scope(env_->context()); + if (on_context_created_callback_) { + on_context_created_callback_(env_->context()); + } + if (exit_code == 0) { LoadEnvironment(env_.get()); @@ -193,10 +197,17 @@ class LWNodeMainRunner { promise_ = std::move(promise); } + void SetOnCreatedContextCallback( + const std::function)>& callback) { + on_context_created_callback_ = callback; + } + private: std::unique_ptr array_buffer_allocator_; Environment* environment_ = nullptr; std::promise promise_; + std::function)> on_context_created_callback_{ + nullptr}; }; } // namespace LWNode diff --git a/include/lwnode/lwnode-public.h b/include/lwnode/lwnode-public.h index 014ca0b1bc..60efb5c878 100644 --- a/include/lwnode/lwnode-public.h +++ b/include/lwnode/lwnode-public.h @@ -35,7 +35,7 @@ LWNODE_EXPORT bool ParseAULEvent(int argc, char** argv); * Sets the path of the root directory of the JavaScript. If you do * not put the path argument, the root path is the app's resource path by * default on Tizen AUL mode. Be sure to call this function before lwnode::Start - * function. + * function. **/ LWNODE_EXPORT bool InitScriptRootPath(const std::string path = ""); @@ -43,26 +43,48 @@ LWNODE_EXPORT int Start(int argc, char** argv); /** * Sets the dlog tag id for debugging. This is only used on Tizen when not in - * AUL mode. + * AUL mode. **/ LWNODE_EXPORT void SetDlogID(const std::string& appId); class LWNODE_EXPORT Runtime { public: + using BindingCallback = std::string (*)(const std::string&, void* user_data); + + class Configuration { + public: + friend Runtime; + Configuration(); + ~Configuration(); + + Configuration(Configuration&) = delete; + Configuration(Configuration&&) = delete; + Configuration& operator=(const Configuration& t) = delete; + Configuration& operator=(Configuration&&); + + void SetBindingCallback(BindingCallback callback, void* user_data); + + private: + struct Internal; + Internal* internal_ = nullptr; + }; + Runtime(); + Runtime(Configuration&& config); + ~Runtime(); /** - * Start the runtime and returns the exit code. It initializes the runtime - * and runs it. When the runtime is initialized, the promise object is set. - * - * @param argc - Argument count. - * @param argv - Argument vector. The element should be the starting file - * name of the application. - * @param promise - Promise object. It will be set when the runtime - * initialization is complete. - * @return Returns the exit code of the runtime. - **/ + * Start the runtime and returns the exit code. It initializes the runtime + * and runs it. When the runtime is initialized, the promise object is set. + * + * @param argc - Argument count. + * @param argv - Argument vector. The element should be the starting file + * name of the application. + * @param promise - Promise object. It will be set when the runtime + * initialization is complete. + * @return Returns the exit code of the runtime. + **/ int Start(int argc, char** argv, std::promise&& promise); std::shared_ptr GetPort(); diff --git a/include/lwnode/lwnode.h b/include/lwnode/lwnode.h index 1300248868..b5ca314cb3 100644 --- a/include/lwnode/lwnode.h +++ b/include/lwnode/lwnode.h @@ -40,6 +40,8 @@ enum ContextEmbedderIndex { // Others are listed in deps/node/src/node_context_data.h. kMainMessagePort = 90, kLoopHolder = 91, + kBindingCallback = 92, + kBindingCallbackData = 93, }; void InitializeProcessMethods(v8::Local target, diff --git a/src/lwnode/lwnode.cc b/src/lwnode/lwnode.cc index e614ed541d..048ffbb146 100644 --- a/src/lwnode/lwnode.cc +++ b/src/lwnode/lwnode.cc @@ -28,6 +28,7 @@ #include "api/utils/misc.h" #include "api/utils/smaps.h" #include "base.h" +#include "lwnode-public.h" #include "lwnode/lwnode-gc-strategy.h" #include "lwnode/lwnode-loader.h" @@ -240,6 +241,32 @@ static ValueRef* Unref(ExecutionStateRef* state, return ValueRef::create(loop_holder->ref_count()); } +static ValueRef* Binding(ExecutionStateRef* state, + ValueRef* this_value, + size_t argc, + ValueRef** argv, + bool isConstructCall) { + std::string message; + if (argc > 0 && argv[0]->isString()) { + message = argv[0]->asString()->toStdUTF8String(); + } + + ContextWrap* lwContext = ContextWrap::fromEscargot(state->context()); + lwnode::Runtime::BindingCallback callback = + reinterpret_cast( + lwContext->GetAlignedPointerFromEmbedderData(kBindingCallback)); + + void* data = + lwContext->GetAlignedPointerFromEmbedderData(kBindingCallbackData); + + std::string response; + if (callback) { + response = callback(message, data); + } + + return StringRef::createFromUTF8(response.c_str(), response.length()); +} + void InitMainMessagePort(Local context, MainMessagePort* main_port, LoopHolderUV* loop_holder, @@ -277,6 +304,9 @@ void InitializeProcessMethods(Local target, Local context) { SetMethod(esContext, esTarget, "ref", Ref); SetMethod(esContext, esTarget, "unref", Unref); + + SetMethod(esContext, esTarget, "binding", Binding); + ModuleMessagePortInit(esContext, esTarget); } diff --git a/test/embedding/CMakeLists.txt b/test/embedding/CMakeLists.txt index 90f00e4385..42019751b3 100644 --- a/test/embedding/CMakeLists.txt +++ b/test/embedding/CMakeLists.txt @@ -15,3 +15,4 @@ endif() add_executable(embedtest.x embedtest.cc) add_executable(example.x example.cc) +add_executable(binding.x binding.cc) diff --git a/test/embedding/binding.cc b/test/embedding/binding.cc new file mode 100644 index 0000000000..6a36a5e065 --- /dev/null +++ b/test/embedding/binding.cc @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include +#include + +#define COUNT_OF(array) (sizeof(array) / sizeof((array)[0])) + +class Info { + public: + Info(std::string name, std::string age, std::string gender) + : name_(name), age_(age), gender_(gender) {} + + std::string GetName() const { return name_; } + std::string GetAge() const { return age_; } + std::string GetGender() const { return gender_; } + + private: + std::string name_; + std::string age_; + std::string gender_; +}; + +int main(int argc, char* argv[]) { + std::shared_ptr info = std::make_shared("John", "30", "male"); + + lwnode::Runtime::Configuration configuration; + configuration.SetBindingCallback( + [](const std::string& message, void* user_data) -> std::string { + Info* info = static_cast(user_data); + + if (message == "name") + return info->GetName(); + if (message == "age") + return info->GetAge(); + if (message == "gender") + return info->GetGender(); + + return ""; + }, + (void*)info.get()); + + auto runtime = std::make_shared(std::move(configuration)); + + std::promise promise; + std::future init_future = promise.get_future(); + const char* script = "test/embedding/test-10-binding-basic.js"; + std::string path = (std::filesystem::current_path() / script).string(); + char* args[] = {const_cast(""), const_cast(path.c_str())}; + + std::thread worker = std::thread( + [&](std::promise&& promise) mutable { + // FIXME: Fix Runtime::Init() call to ensure environment initialization + // before running the loop, Runtime::Run(). This workaround passes a + // promise directly to know when that is. + runtime->Start(COUNT_OF(args), args, std::move(promise)); + }, + std::move(promise)); + + init_future.wait(); + + int count1 = 0; + auto port2 = runtime->GetPort(); + port2->OnMessage([&](const MessageEvent* event) { + std::cout << event->data() << std::endl; + count1++; + }); + port2->PostMessage(MessageEvent::New("ping")); + + worker.join(); + return 0; +} diff --git a/test/embedding/test-10-binding-basic.js b/test/embedding/test-10-binding-basic.js new file mode 100644 index 0000000000..6da0a45384 --- /dev/null +++ b/test/embedding/test-10-binding-basic.js @@ -0,0 +1,32 @@ +const lwnode = process.lwnode; +const port = process.lwnode.port; + +lwnode.ref(); + +port.onmessage = (event) => { + console.log(`${event.data}`); + if (event.data == "ping") { + port.postMessage("pong"); + lwnode.unref(); + } +}; + +function printMessage() { + console.log("printMessage called--------------------------------------------"); + const name = lwnode.binding('name'); + console.log(`Hello, ${name}!`); + + const age = lwnode.binding('age'); + console.log(`I am ${age} years old.`); + + const gender = lwnode.binding('gender'); + console.log(`My gender is ${gender}.`); +} + +let count = 10; +let loop = setInterval(() => { + if (count-- <= 0) { + clearInterval(loop); + } + printMessage(); +}, 1000); From 63d6233eb9c7ae3431174c226325240fe672e77d Mon Sep 17 00:00:00 2001 From: Hosung Kim Date: Tue, 1 Apr 2025 14:03:00 +0900 Subject: [PATCH 2/2] apply review comments --- deps/node/lib/internal/lwnode/setup.js | 6 ++-- deps/node/src/lwnode/lwnode-public.cc | 32 ++++++++++--------- deps/node/src/node_main_lw_runner-inl.h | 10 +++--- escargot.gyp | 1 + include/lwnode/lwnode-public.h | 5 +-- include/lwnode/lwnode.h | 4 +-- src/lwnode/lwnode.cc | 31 +++++++++--------- test/embedding/CMakeLists.txt | 2 +- .../{binding.cc => send-message-sync.cc} | 21 ++++++------ ....js => test-10-send-message-sync-basic.js} | 9 ++---- 10 files changed, 60 insertions(+), 61 deletions(-) rename test/embedding/{binding.cc => send-message-sync.cc} (86%) rename test/embedding/{test-10-binding-basic.js => test-10-send-message-sync-basic.js} (77%) diff --git a/deps/node/lib/internal/lwnode/setup.js b/deps/node/lib/internal/lwnode/setup.js index 59b781be20..6a8d2ee6a5 100644 --- a/deps/node/lib/internal/lwnode/setup.js +++ b/deps/node/lib/internal/lwnode/setup.js @@ -95,13 +95,13 @@ function wrapLWNodeMethods(binding) { return binding.hasSystemInfo.apply(null, args); } }, - binding: (message) => { + sendMessageSync: (message) => { if (typeof message !== "string") { throw new TypeError("The message argument must be a string"); } - if (binding.binding) { - return binding.binding(message); + if (binding.sendMessageSync) { + return binding.sendMessageSync(message); } } }; diff --git a/deps/node/src/lwnode/lwnode-public.cc b/deps/node/src/lwnode/lwnode-public.cc index 8450b839e3..8bc27de1be 100644 --- a/deps/node/src/lwnode/lwnode-public.cc +++ b/deps/node/src/lwnode/lwnode-public.cc @@ -29,8 +29,8 @@ using namespace node; namespace lwnode { struct Runtime::Configuration::Internal { - Runtime::BindingCallback binding_callback{nullptr}; - void* binding_callback_data{nullptr}; + Runtime::SendMessageSyncCallback send_message_sync_callback{nullptr}; + void* send_message_sync_callback_data{nullptr}; }; class Runtime::Internal { @@ -40,15 +40,17 @@ class Runtime::Internal { std::pair Init(int argc, char** argv) { is_initialized = true; - // Set binding callback to isolate context embedder data. - runner_.SetOnCreatedContextCallback([this](v8::Local context) { - context->SetAlignedPointerInEmbedderData( - LWNode::ContextEmbedderIndex::kBindingCallback, - reinterpret_cast(config_.internal_->binding_callback)); - context->SetAlignedPointerInEmbedderData( - LWNode::ContextEmbedderIndex::kBindingCallbackData, - config_.internal_->binding_callback_data); - }); + // Set sendMessageSync callback to isolate context embedder data. + runner_.SetOnMainEnvCreationCallback( + [this](v8::Local context) { + context->SetAlignedPointerInEmbedderData( + LWNode::ContextEmbedderIndex::kSendMessageSyncCallback, + reinterpret_cast( + config_.internal_->send_message_sync_callback)); + context->SetAlignedPointerInEmbedderData( + LWNode::ContextEmbedderIndex::kSendMessageSyncCallbackData, + config_.internal_->send_message_sync_callback_data); + }); return InitializeNode(argc, argv, &instance_); } @@ -126,10 +128,10 @@ Runtime::Configuration& Runtime::Configuration::operator=( return *this; } -void Runtime::Configuration::SetBindingCallback( - Runtime::BindingCallback callback, void* user_data) { - internal_->binding_callback = callback; - internal_->binding_callback_data = user_data; +void Runtime::Configuration::OnSendMessageSync( + Runtime::SendMessageSyncCallback callback, void* user_data) { + internal_->send_message_sync_callback = callback; + internal_->send_message_sync_callback_data = user_data; } /************************************************************************** diff --git a/deps/node/src/node_main_lw_runner-inl.h b/deps/node/src/node_main_lw_runner-inl.h index 482924da74..31e22149c8 100644 --- a/deps/node/src/node_main_lw_runner-inl.h +++ b/deps/node/src/node_main_lw_runner-inl.h @@ -127,8 +127,8 @@ class LWNodeMainRunner { Context::Scope context_scope(env_->context()); - if (on_context_created_callback_) { - on_context_created_callback_(env_->context()); + if (on_main_env_creation_callback_) { + on_main_env_creation_callback_(env_->context()); } if (exit_code == 0) { @@ -197,16 +197,16 @@ class LWNodeMainRunner { promise_ = std::move(promise); } - void SetOnCreatedContextCallback( + void SetOnMainEnvCreationCallback( const std::function)>& callback) { - on_context_created_callback_ = callback; + on_main_env_creation_callback_ = callback; } private: std::unique_ptr array_buffer_allocator_; Environment* environment_ = nullptr; std::promise promise_; - std::function)> on_context_created_callback_{ + std::function)> on_main_env_creation_callback_{ nullptr}; }; diff --git a/escargot.gyp b/escargot.gyp index e08b3d681b..13285523ae 100755 --- a/escargot.gyp +++ b/escargot.gyp @@ -54,6 +54,7 @@ '-DESCARGOT_THREADING=<(escargot_threading)', '-DESCARGOT_ASAN=<(asan)', '-DESCARGOT_DEBUGGER=<(escargot_debugger)', + '-DCMAKE_POLICY_VERSION_MINIMUM=3.5', ], }, 'all_dependent_settings': { diff --git a/include/lwnode/lwnode-public.h b/include/lwnode/lwnode-public.h index 60efb5c878..f793878194 100644 --- a/include/lwnode/lwnode-public.h +++ b/include/lwnode/lwnode-public.h @@ -49,7 +49,8 @@ LWNODE_EXPORT void SetDlogID(const std::string& appId); class LWNODE_EXPORT Runtime { public: - using BindingCallback = std::string (*)(const std::string&, void* user_data); + using SendMessageSyncCallback = std::string (*)(const std::string&, + void* user_data); class Configuration { public: @@ -62,7 +63,7 @@ class LWNODE_EXPORT Runtime { Configuration& operator=(const Configuration& t) = delete; Configuration& operator=(Configuration&&); - void SetBindingCallback(BindingCallback callback, void* user_data); + void OnSendMessageSync(SendMessageSyncCallback callback, void* user_data); private: struct Internal; diff --git a/include/lwnode/lwnode.h b/include/lwnode/lwnode.h index b5ca314cb3..cbb942873d 100644 --- a/include/lwnode/lwnode.h +++ b/include/lwnode/lwnode.h @@ -40,8 +40,8 @@ enum ContextEmbedderIndex { // Others are listed in deps/node/src/node_context_data.h. kMainMessagePort = 90, kLoopHolder = 91, - kBindingCallback = 92, - kBindingCallbackData = 93, + kSendMessageSyncCallback = 92, + kSendMessageSyncCallbackData = 93, }; void InitializeProcessMethods(v8::Local target, diff --git a/src/lwnode/lwnode.cc b/src/lwnode/lwnode.cc index 048ffbb146..059c92a2ef 100644 --- a/src/lwnode/lwnode.cc +++ b/src/lwnode/lwnode.cc @@ -241,28 +241,29 @@ static ValueRef* Unref(ExecutionStateRef* state, return ValueRef::create(loop_holder->ref_count()); } -static ValueRef* Binding(ExecutionStateRef* state, - ValueRef* this_value, - size_t argc, - ValueRef** argv, - bool isConstructCall) { +static ValueRef* SendMessageSync(ExecutionStateRef* state, + ValueRef* this_value, + size_t argc, + ValueRef** argv, + bool isConstructCall) { std::string message; if (argc > 0 && argv[0]->isString()) { message = argv[0]->asString()->toStdUTF8String(); } ContextWrap* lwContext = ContextWrap::fromEscargot(state->context()); - lwnode::Runtime::BindingCallback callback = - reinterpret_cast( - lwContext->GetAlignedPointerFromEmbedderData(kBindingCallback)); + lwnode::Runtime::SendMessageSyncCallback callback = + reinterpret_cast( + lwContext->GetAlignedPointerFromEmbedderData( + kSendMessageSyncCallback)); + if (!callback) { + return ValueRef::createUndefined(); + } - void* data = - lwContext->GetAlignedPointerFromEmbedderData(kBindingCallbackData); + void* data = lwContext->GetAlignedPointerFromEmbedderData( + kSendMessageSyncCallbackData); - std::string response; - if (callback) { - response = callback(message, data); - } + std::string response = callback(message, data); return StringRef::createFromUTF8(response.c_str(), response.length()); } @@ -305,7 +306,7 @@ void InitializeProcessMethods(Local target, Local context) { SetMethod(esContext, esTarget, "ref", Ref); SetMethod(esContext, esTarget, "unref", Unref); - SetMethod(esContext, esTarget, "binding", Binding); + SetMethod(esContext, esTarget, "sendMessageSync", SendMessageSync); ModuleMessagePortInit(esContext, esTarget); } diff --git a/test/embedding/CMakeLists.txt b/test/embedding/CMakeLists.txt index 42019751b3..0b0deb33bd 100644 --- a/test/embedding/CMakeLists.txt +++ b/test/embedding/CMakeLists.txt @@ -15,4 +15,4 @@ endif() add_executable(embedtest.x embedtest.cc) add_executable(example.x example.cc) -add_executable(binding.x binding.cc) +add_executable(send-message-sync.x send-message-sync.cc) diff --git a/test/embedding/binding.cc b/test/embedding/send-message-sync.cc similarity index 86% rename from test/embedding/binding.cc rename to test/embedding/send-message-sync.cc index 6a36a5e065..5f2c9096d0 100644 --- a/test/embedding/binding.cc +++ b/test/embedding/send-message-sync.cc @@ -1,10 +1,10 @@ +#include +#include #include #include #include -#include #include -#include -#include +#include #define COUNT_OF(array) (sizeof(array) / sizeof((array)[0])) @@ -27,16 +27,13 @@ int main(int argc, char* argv[]) { std::shared_ptr info = std::make_shared("John", "30", "male"); lwnode::Runtime::Configuration configuration; - configuration.SetBindingCallback( + configuration.OnSendMessageSync( [](const std::string& message, void* user_data) -> std::string { Info* info = static_cast(user_data); - - if (message == "name") - return info->GetName(); - if (message == "age") - return info->GetAge(); - if (message == "gender") - return info->GetGender(); + + if (message == "name") return info->GetName(); + if (message == "age") return info->GetAge(); + if (message == "gender") return info->GetGender(); return ""; }, @@ -46,7 +43,7 @@ int main(int argc, char* argv[]) { std::promise promise; std::future init_future = promise.get_future(); - const char* script = "test/embedding/test-10-binding-basic.js"; + const char* script = "test/embedding/test-10-send-message-sync-basic.js"; std::string path = (std::filesystem::current_path() / script).string(); char* args[] = {const_cast(""), const_cast(path.c_str())}; diff --git a/test/embedding/test-10-binding-basic.js b/test/embedding/test-10-send-message-sync-basic.js similarity index 77% rename from test/embedding/test-10-binding-basic.js rename to test/embedding/test-10-send-message-sync-basic.js index 6da0a45384..9da6946731 100644 --- a/test/embedding/test-10-binding-basic.js +++ b/test/embedding/test-10-send-message-sync-basic.js @@ -1,25 +1,22 @@ const lwnode = process.lwnode; const port = process.lwnode.port; -lwnode.ref(); - port.onmessage = (event) => { console.log(`${event.data}`); if (event.data == "ping") { port.postMessage("pong"); - lwnode.unref(); } }; function printMessage() { console.log("printMessage called--------------------------------------------"); - const name = lwnode.binding('name'); + const name = lwnode.sendMessageSync('name'); console.log(`Hello, ${name}!`); - const age = lwnode.binding('age'); + const age = lwnode.sendMessageSync('age'); console.log(`I am ${age} years old.`); - const gender = lwnode.binding('gender'); + const gender = lwnode.sendMessageSync('gender'); console.log(`My gender is ${gender}.`); }