forked from tmikov/hermes-jsi-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevloop.cpp
203 lines (175 loc) · 5.83 KB
/
evloop.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* This is free and unencumbered software released into the public domain.
* For more information, please refer to the LICENSE file in the root directory
* or at <http://unlicense.org/>
*/
#include <chrono>
#include <fstream>
#include <iostream>
#include <optional>
#include <sstream>
#include <thread>
#ifndef _WIN32
#include <dlfcn.h>
#else
#include <windows.h>
#endif
#include <hermes/hermes.h>
using namespace ::facebook;
/// The JS library that implements the event loop.
static const char *s_jslib =
#include "jslib.js.inc"
;
/// Read the contents of a file into a string.
static std::optional<std::string> readFile(const char *path) {
std::ifstream fileStream(path);
std::stringstream stringStream;
if (fileStream) {
stringStream << fileStream.rdbuf();
fileStream.close();
} else {
// Handle error - file opening failed
std::cerr << path << ": error opening file" << std::endl;
return std::nullopt;
}
return stringStream.str();
}
/// The signature of the function that initializes the library.
typedef void (*RegisterNativesFN)(jsi::Runtime &rt);
#ifndef _WIN32
/// Load the library and return the "registerNatives()" function.
static RegisterNativesFN loadRegisterNatives(const char *libraryPath) {
// Open the library.
void *handle = dlopen(libraryPath, RTLD_LAZY);
if (!handle) {
std::cerr << "*** Cannot open library: " << dlerror() << '\n';
return nullptr;
}
// Clear any existing error.
dlerror();
// Load the symbol (function).
auto func = (RegisterNativesFN)dlsym(handle, "registerNatives");
if (const char *dlsym_error = dlerror()) {
std::cerr << "Cannot load symbol 'registerNatives': " << dlsym_error
<< '\n';
dlclose(handle);
return nullptr;
}
return func;
}
#else
/// Load the library and return the "registerNatives()" function.
static RegisterNativesFN loadRegisterNatives(const char *libraryPath) {
// Load the library
HMODULE hModule = LoadLibraryA(libraryPath);
if (!hModule) {
std::cerr << "Cannot open library: " << GetLastError() << '\n';
return nullptr;
}
// Get the function address
auto func = (RegisterNativesFN)GetProcAddress(hModule, "registerNatives");
if (!func) {
std::cerr << "Cannot load symbol 'registerNatives': " << GetLastError()
<< '\n';
FreeLibrary(hModule);
return nullptr;
}
return func;
}
#endif
/// Load all the libraries and call their "registerNatives()" function.
/// \return true if all libraries were loaded successfully.
static bool loadNativeLibraries(jsi::Runtime &rt, int argc, char **argv) {
try {
for (int i = 2; i < argc; i++) {
auto func = loadRegisterNatives(argv[i]);
if (!func)
return false;
func(rt);
}
} catch (jsi::JSIException &e) {
// Handle JSI exceptions here.
std::cerr << "JSI Exception: " << e.what() << std::endl;
return false;
}
return true;
}
int main(int argc, char **argv) {
// If no argument is provided, print usage and exit.
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <path-to-js-file> [<shared-lib>...]"
<< std::endl;
return 1;
}
const char *jsPath = argv[1];
// Read the file.
auto optCode = readFile(jsPath);
if (!optCode)
return 1;
// You can Customize the runtime config here.
auto runtimeConfig = ::hermes::vm::RuntimeConfig::Builder()
.withIntl(false)
.withMicrotaskQueue(true)
.build();
// Create the Hermes runtime.
auto runtime = facebook::hermes::makeHermesRuntime(runtimeConfig);
try {
// Register event loop functions and obtain the runMicroTask() helper
// function.
jsi::Object helpers =
runtime
->evaluateJavaScript(
std::make_unique<jsi::StringBuffer>(s_jslib), "jslib.js.inc")
.asObject(*runtime);
jsi::Function peekMacroTask =
helpers.getPropertyAsFunction(*runtime, "peek");
jsi::Function runMacroTask = helpers.getPropertyAsFunction(*runtime, "run");
// There are no pending tasks, but we want to initialize the event loop
// current time.
{
double curTimeMs =
(double)std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
runMacroTask.call(*runtime, curTimeMs);
}
// Register host functions.
if (!loadNativeLibraries(*runtime, argc, argv))
return 1;
runtime->evaluateJavaScript(
std::make_unique<jsi::StringBuffer>(std::move(*optCode)), jsPath);
runtime->drainMicrotasks();
double nextTimeMs;
// This is the event loop. Loop while there are pending tasks.
while ((nextTimeMs = peekMacroTask.call(*runtime).getNumber()) >= 0) {
auto now = std::chrono::steady_clock::now();
double curTimeMs =
(double)std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch())
.count();
// If we have to, sleep until the next task is ready.
if (nextTimeMs > curTimeMs) {
std::this_thread::sleep_until(
now +
std::chrono::milliseconds((int_least64_t)(nextTimeMs - curTimeMs)));
// Update the current time, since we slept.
curTimeMs =
(double)std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}
// Run the next task.
runMacroTask.call(*runtime, curTimeMs);
runtime->drainMicrotasks();
}
} catch (jsi::JSError &e) {
// Handle JS exceptions here.
std::cerr << "JS Exception: " << e.getStack() << std::endl;
return 1;
} catch (jsi::JSIException &e) {
// Handle JSI exceptions here.
std::cerr << "JSI Exception: " << e.what() << std::endl;
return 1;
}
return 0;
}