Skip to content

Commit 250b6d4

Browse files
committed
apply review comments
1 parent d9f9989 commit 250b6d4

File tree

9 files changed

+59
-61
lines changed

9 files changed

+59
-61
lines changed

deps/node/lib/internal/lwnode/setup.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ function wrapLWNodeMethods(binding) {
9595
return binding.hasSystemInfo.apply(null, args);
9696
}
9797
},
98-
binding: (message) => {
98+
sendMessageSync: (message) => {
9999
if (typeof message !== "string") {
100100
throw new TypeError("The message argument must be a string");
101101
}
102102

103-
if (binding.binding) {
104-
return binding.binding(message);
103+
if (binding.sendMessageSync) {
104+
return binding.sendMessageSync(message);
105105
}
106106
}
107107
};

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ using namespace node;
2929
namespace lwnode {
3030

3131
struct Runtime::Configuration::Internal {
32-
Runtime::BindingCallback binding_callback{nullptr};
33-
void* binding_callback_data{nullptr};
32+
Runtime::SendMessageSyncCallback send_message_sync_callback{nullptr};
33+
void* send_message_sync_callback_data{nullptr};
3434
};
3535

3636
class Runtime::Internal {
@@ -40,15 +40,17 @@ class Runtime::Internal {
4040
std::pair<bool, int> Init(int argc, char** argv) {
4141
is_initialized = true;
4242

43-
// Set binding callback to isolate context embedder data.
44-
runner_.SetOnCreatedContextCallback([this](v8::Local<v8::Context> context) {
45-
context->SetAlignedPointerInEmbedderData(
46-
LWNode::ContextEmbedderIndex::kBindingCallback,
47-
reinterpret_cast<void*>(config_.internal_->binding_callback));
48-
context->SetAlignedPointerInEmbedderData(
49-
LWNode::ContextEmbedderIndex::kBindingCallbackData,
50-
config_.internal_->binding_callback_data);
51-
});
43+
// Set sendMessageSync callback to isolate context embedder data.
44+
runner_.SetOnMainEnvCreationCallback(
45+
[this](v8::Local<v8::Context> context) {
46+
context->SetAlignedPointerInEmbedderData(
47+
LWNode::ContextEmbedderIndex::kSendMessageSyncCallback,
48+
reinterpret_cast<void*>(
49+
config_.internal_->send_message_sync_callback));
50+
context->SetAlignedPointerInEmbedderData(
51+
LWNode::ContextEmbedderIndex::kSendMessageSyncCallbackData,
52+
config_.internal_->send_message_sync_callback_data);
53+
});
5254

5355
return InitializeNode(argc, argv, &instance_);
5456
}
@@ -126,10 +128,10 @@ Runtime::Configuration& Runtime::Configuration::operator=(
126128
return *this;
127129
}
128130

129-
void Runtime::Configuration::SetBindingCallback(
130-
Runtime::BindingCallback callback, void* user_data) {
131-
internal_->binding_callback = callback;
132-
internal_->binding_callback_data = user_data;
131+
void Runtime::Configuration::OnSendMessageSync(
132+
Runtime::SendMessageSyncCallback callback, void* user_data) {
133+
internal_->send_message_sync_callback = callback;
134+
internal_->send_message_sync_callback_data = user_data;
133135
}
134136

135137
/**************************************************************************

deps/node/src/node_main_lw_runner-inl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ class LWNodeMainRunner {
127127

128128
Context::Scope context_scope(env_->context());
129129

130-
if (on_context_created_callback_) {
131-
on_context_created_callback_(env_->context());
130+
if (on_main_env_creation_callback_) {
131+
on_main_env_creation_callback_(env_->context());
132132
}
133133

134134
if (exit_code == 0) {
@@ -197,16 +197,16 @@ class LWNodeMainRunner {
197197
promise_ = std::move(promise);
198198
}
199199

200-
void SetOnCreatedContextCallback(
200+
void SetOnMainEnvCreationCallback(
201201
const std::function<void(v8::Local<v8::Context>)>& callback) {
202-
on_context_created_callback_ = callback;
202+
on_main_env_creation_callback_ = callback;
203203
}
204204

205205
private:
206206
std::unique_ptr<node::ArrayBufferAllocator> array_buffer_allocator_;
207207
Environment* environment_ = nullptr;
208208
std::promise<void> promise_;
209-
std::function<void(v8::Local<v8::Context>)> on_context_created_callback_{
209+
std::function<void(v8::Local<v8::Context>)> on_main_env_creation_callback_{
210210
nullptr};
211211
};
212212

include/lwnode/lwnode-public.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ LWNODE_EXPORT void SetDlogID(const std::string& appId);
4949

5050
class LWNODE_EXPORT Runtime {
5151
public:
52-
using BindingCallback = std::string (*)(const std::string&, void* user_data);
52+
using SendMessageSyncCallback = std::string (*)(const std::string&,
53+
void* user_data);
5354

5455
class Configuration {
5556
public:
@@ -62,7 +63,7 @@ class LWNODE_EXPORT Runtime {
6263
Configuration& operator=(const Configuration& t) = delete;
6364
Configuration& operator=(Configuration&&);
6465

65-
void SetBindingCallback(BindingCallback callback, void* user_data);
66+
void OnSendMessageSync(SendMessageSyncCallback callback, void* user_data);
6667

6768
private:
6869
struct Internal;

include/lwnode/lwnode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ enum ContextEmbedderIndex {
4040
// Others are listed in deps/node/src/node_context_data.h.
4141
kMainMessagePort = 90,
4242
kLoopHolder = 91,
43-
kBindingCallback = 92,
44-
kBindingCallbackData = 93,
43+
kSendMessageSyncCallback = 92,
44+
kSendMessageSyncCallbackData = 93,
4545
};
4646

4747
void InitializeProcessMethods(v8::Local<v8::Object> target,

src/lwnode/lwnode.cc

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,28 +241,29 @@ static ValueRef* Unref(ExecutionStateRef* state,
241241
return ValueRef::create(loop_holder->ref_count());
242242
}
243243

244-
static ValueRef* Binding(ExecutionStateRef* state,
245-
ValueRef* this_value,
246-
size_t argc,
247-
ValueRef** argv,
248-
bool isConstructCall) {
244+
static ValueRef* SendMessageSync(ExecutionStateRef* state,
245+
ValueRef* this_value,
246+
size_t argc,
247+
ValueRef** argv,
248+
bool isConstructCall) {
249249
std::string message;
250250
if (argc > 0 && argv[0]->isString()) {
251251
message = argv[0]->asString()->toStdUTF8String();
252252
}
253253

254254
ContextWrap* lwContext = ContextWrap::fromEscargot(state->context());
255-
lwnode::Runtime::BindingCallback callback =
256-
reinterpret_cast<lwnode::Runtime::BindingCallback>(
257-
lwContext->GetAlignedPointerFromEmbedderData(kBindingCallback));
255+
lwnode::Runtime::SendMessageSyncCallback callback =
256+
reinterpret_cast<lwnode::Runtime::SendMessageSyncCallback>(
257+
lwContext->GetAlignedPointerFromEmbedderData(
258+
kSendMessageSyncCallback));
259+
if (!callback) {
260+
return ValueRef::createUndefined();
261+
}
258262

259-
void* data =
260-
lwContext->GetAlignedPointerFromEmbedderData(kBindingCallbackData);
263+
void* data = lwContext->GetAlignedPointerFromEmbedderData(
264+
kSendMessageSyncCallbackData);
261265

262-
std::string response;
263-
if (callback) {
264-
response = callback(message, data);
265-
}
266+
std::string response = callback(message, data);
266267

267268
return StringRef::createFromUTF8(response.c_str(), response.length());
268269
}
@@ -305,7 +306,7 @@ void InitializeProcessMethods(Local<Object> target, Local<Context> context) {
305306
SetMethod(esContext, esTarget, "ref", Ref);
306307
SetMethod(esContext, esTarget, "unref", Unref);
307308

308-
SetMethod(esContext, esTarget, "binding", Binding);
309+
SetMethod(esContext, esTarget, "sendMessageSync", SendMessageSync);
309310

310311
ModuleMessagePortInit(esContext, esTarget);
311312
}

test/embedding/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ endif()
1515

1616
add_executable(embedtest.x embedtest.cc)
1717
add_executable(example.x example.cc)
18-
add_executable(binding.x binding.cc)
18+
add_executable(send-message-sync.x send-message-sync.cc)

test/embedding/binding.cc renamed to test/embedding/send-message-sync.cc

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
#include <lwnode-public.h>
2+
#include <message-port.h>
13
#include <filesystem>
24
#include <future>
35
#include <iostream>
4-
#include <thread>
56
#include <memory>
6-
#include <lwnode-public.h>
7-
#include <message-port.h>
7+
#include <thread>
88

99
#define COUNT_OF(array) (sizeof(array) / sizeof((array)[0]))
1010

@@ -27,16 +27,13 @@ int main(int argc, char* argv[]) {
2727
std::shared_ptr<Info> info = std::make_shared<Info>("John", "30", "male");
2828

2929
lwnode::Runtime::Configuration configuration;
30-
configuration.SetBindingCallback(
30+
configuration.OnSendMessageSync(
3131
[](const std::string& message, void* user_data) -> std::string {
3232
Info* info = static_cast<Info*>(user_data);
33-
34-
if (message == "name")
35-
return info->GetName();
36-
if (message == "age")
37-
return info->GetAge();
38-
if (message == "gender")
39-
return info->GetGender();
33+
34+
if (message == "name") return info->GetName();
35+
if (message == "age") return info->GetAge();
36+
if (message == "gender") return info->GetGender();
4037

4138
return "";
4239
},
@@ -46,7 +43,7 @@ int main(int argc, char* argv[]) {
4643

4744
std::promise<void> promise;
4845
std::future<void> init_future = promise.get_future();
49-
const char* script = "test/embedding/test-10-binding-basic.js";
46+
const char* script = "test/embedding/test-10-send-message-sync-basic.js";
5047
std::string path = (std::filesystem::current_path() / script).string();
5148
char* args[] = {const_cast<char*>(""), const_cast<char*>(path.c_str())};
5249

test/embedding/test-10-binding-basic.js renamed to test/embedding/test-10-send-message-sync-basic.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
const lwnode = process.lwnode;
22
const port = process.lwnode.port;
33

4-
lwnode.ref();
5-
64
port.onmessage = (event) => {
75
console.log(`${event.data}`);
86
if (event.data == "ping") {
97
port.postMessage("pong");
10-
lwnode.unref();
118
}
129
};
1310

1411
function printMessage() {
1512
console.log("printMessage called--------------------------------------------");
16-
const name = lwnode.binding('name');
13+
const name = lwnode.sendMessageSync('name');
1714
console.log(`Hello, ${name}!`);
1815

19-
const age = lwnode.binding('age');
16+
const age = lwnode.sendMessageSync('age');
2017
console.log(`I am ${age} years old.`);
2118

22-
const gender = lwnode.binding('gender');
19+
const gender = lwnode.sendMessageSync('gender');
2320
console.log(`My gender is ${gender}.`);
2421
}
2522

0 commit comments

Comments
 (0)