-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexample.cc
More file actions
64 lines (54 loc) · 1.97 KB
/
example.cc
File metadata and controls
64 lines (54 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <chrono>
#include <filesystem>
#include <future>
#include <iostream>
#include <thread>
#include <lwnode-public.h>
#include <message-port.h>
template <typename T, size_t N>
constexpr size_t COUNT_OF(T (&)[N]) noexcept {
return N;
}
int main(int argc, char* argv[]) {
lwnode::Runtime::Configuration configuration;
if (!configuration.Set("gc_interval", 10000)) {
std::cerr << "Failed to set gc_interval" << std::endl;
}
auto runtime = std::make_shared<lwnode::Runtime>(std::move(configuration));
const char* script = "test/embedding/test-01-message-port-basic.js";
std::string path = (std::filesystem::current_path() / script).string();
char* args[] = {const_cast<char*>(""), const_cast<char*>(path.c_str())};
std::thread worker = std::thread([&]() 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.
int result = 0;
do {
std::cout << "start runtime" << std::endl;
result = runtime->Start(COUNT_OF(args), args);
std::cout << "result: " << result << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
} while (result == 100);
});
while (true) {
std::future_status status = runtime->WaitForReady(50);
if (status == std::future_status::timeout) {
std::cout << "runtime is not ready: timeout" << std::endl;
} else if (status == std::future_status::deferred) {
std::cout << "runtime is not ready: deferred" << std::endl;
} else if (status == std::future_status::ready) {
std::cout << "runtime is ready" << std::endl;
break;
}
}
int count1 = 0;
auto port2 = runtime->GetPort();
std::cout << "done get port" << std::endl;
port2->OnMessage([&](const MessageEvent* event) {
std::cout << event->data() << std::endl;
count1++;
});
port2->PostMessage(MessageEvent::New("ping"));
worker.join();
return 0;
}