@pwaos.net/runtime-client-api 0.0.0-stage → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +137 -2
- package/binding.gyp +83 -0
- package/package.json +74 -4
- package/runtime.cjs +69 -0
- package/runtime.js +17 -0
- package/scripts/resolve-sdk.cjs +129 -0
- package/scripts/runtime-resolver.cjs +135 -0
- package/scripts/sdk-dir.cjs +37 -0
- package/scripts/setup-sdk.js +161 -0
- package/src/addon_main.cc +370 -0
- package/src/app_obj.cc +832 -0
- package/src/common.h +291 -0
- package/src/instance_obj.cc +734 -0
- package/src/instance_obj.h +10 -0
- package/src/interface_obj.cc +374 -0
- package/test/test.cjs +747 -0
package/src/common.h
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
#ifndef CHROMIUM_PWAOS_THIRDPARTY_NODEJS_ADDON_COMMON_H_
|
|
2
|
+
#define CHROMIUM_PWAOS_THIRDPARTY_NODEJS_ADDON_COMMON_H_
|
|
3
|
+
|
|
4
|
+
#include <js_native_api.h>
|
|
5
|
+
#include <stdlib.h> // abort()
|
|
6
|
+
|
|
7
|
+
// Empty value so that macros here are able to return NULL or void
|
|
8
|
+
#define NODE_API_RETVAL_NOTHING // Intentionally blank #define
|
|
9
|
+
|
|
10
|
+
#define GET_AND_THROW_LAST_ERROR(env) \
|
|
11
|
+
do { \
|
|
12
|
+
const napi_extended_error_info *error_info; \
|
|
13
|
+
napi_get_last_error_info((env), &error_info); \
|
|
14
|
+
bool is_pending; \
|
|
15
|
+
const char* err_message = error_info->error_message; \
|
|
16
|
+
napi_is_exception_pending((env), &is_pending); \
|
|
17
|
+
/* If an exception is already pending, don't rethrow it */ \
|
|
18
|
+
if (!is_pending) { \
|
|
19
|
+
const char* error_message = err_message != NULL ? \
|
|
20
|
+
err_message : \
|
|
21
|
+
"empty error message"; \
|
|
22
|
+
napi_throw_error((env), NULL, error_message); \
|
|
23
|
+
} \
|
|
24
|
+
} while (0)
|
|
25
|
+
|
|
26
|
+
// The basic version of GET_AND_THROW_LAST_ERROR. We cannot access any
|
|
27
|
+
// exceptions and we cannot fail by way of JS exception, so we abort.
|
|
28
|
+
#define FATALLY_FAIL_WITH_LAST_ERROR(env) \
|
|
29
|
+
do { \
|
|
30
|
+
const napi_extended_error_info* error_info; \
|
|
31
|
+
napi_get_last_error_info((env), &error_info); \
|
|
32
|
+
const char* err_message = error_info->error_message; \
|
|
33
|
+
const char* error_message = \
|
|
34
|
+
err_message != NULL ? err_message : "empty error message"; \
|
|
35
|
+
fprintf(stderr, "%s\n", error_message); \
|
|
36
|
+
abort(); \
|
|
37
|
+
} while (0)
|
|
38
|
+
|
|
39
|
+
#define NODE_API_ASSERT_BASE(env, assertion, message, ret_val) \
|
|
40
|
+
do { \
|
|
41
|
+
if (!(assertion)) { \
|
|
42
|
+
napi_throw_error( \
|
|
43
|
+
(env), \
|
|
44
|
+
NULL, \
|
|
45
|
+
"assertion (" #assertion ") failed: " message); \
|
|
46
|
+
return ret_val; \
|
|
47
|
+
} \
|
|
48
|
+
} while (0)
|
|
49
|
+
|
|
50
|
+
#define NODE_API_BASIC_ASSERT_BASE(assertion, message, ret_val) \
|
|
51
|
+
do { \
|
|
52
|
+
if (!(assertion)) { \
|
|
53
|
+
fprintf(stderr, "assertion (" #assertion ") failed: " message); \
|
|
54
|
+
abort(); \
|
|
55
|
+
return ret_val; \
|
|
56
|
+
} \
|
|
57
|
+
} while (0)
|
|
58
|
+
|
|
59
|
+
// Returns NULL on failed assertion.
|
|
60
|
+
// This is meant to be used inside napi_callback methods.
|
|
61
|
+
#define NODE_API_ASSERT(env, assertion, message) \
|
|
62
|
+
NODE_API_ASSERT_BASE(env, assertion, message, NULL)
|
|
63
|
+
|
|
64
|
+
// Returns empty on failed assertion.
|
|
65
|
+
// This is meant to be used inside functions with void return type.
|
|
66
|
+
#define NODE_API_ASSERT_RETURN_VOID(env, assertion, message) \
|
|
67
|
+
NODE_API_ASSERT_BASE(env, assertion, message, NODE_API_RETVAL_NOTHING)
|
|
68
|
+
|
|
69
|
+
#define NODE_API_BASIC_ASSERT_RETURN_VOID(assertion, message) \
|
|
70
|
+
NODE_API_BASIC_ASSERT_BASE(assertion, message, NODE_API_RETVAL_NOTHING)
|
|
71
|
+
|
|
72
|
+
#define NODE_API_CALL_BASE(env, the_call, ret_val) \
|
|
73
|
+
do { \
|
|
74
|
+
if ((the_call) != napi_ok) { \
|
|
75
|
+
GET_AND_THROW_LAST_ERROR((env)); \
|
|
76
|
+
return ret_val; \
|
|
77
|
+
} \
|
|
78
|
+
} while (0)
|
|
79
|
+
|
|
80
|
+
#define NODE_API_BASIC_CALL_BASE(env, the_call, ret_val) \
|
|
81
|
+
do { \
|
|
82
|
+
if ((the_call) != napi_ok) { \
|
|
83
|
+
FATALLY_FAIL_WITH_LAST_ERROR((env)); \
|
|
84
|
+
return ret_val; \
|
|
85
|
+
} \
|
|
86
|
+
} while (0)
|
|
87
|
+
|
|
88
|
+
// Returns NULL if the_call doesn't return napi_ok.
|
|
89
|
+
#define NODE_API_CALL(env, the_call) \
|
|
90
|
+
NODE_API_CALL_BASE(env, the_call, NULL)
|
|
91
|
+
|
|
92
|
+
// Returns empty if the_call doesn't return napi_ok.
|
|
93
|
+
#define NODE_API_CALL_RETURN_VOID(env, the_call) \
|
|
94
|
+
NODE_API_CALL_BASE(env, the_call, NODE_API_RETVAL_NOTHING)
|
|
95
|
+
|
|
96
|
+
#define NODE_API_BASIC_CALL_RETURN_VOID(env, the_call) \
|
|
97
|
+
NODE_API_BASIC_CALL_BASE(env, the_call, NODE_API_RETVAL_NOTHING)
|
|
98
|
+
|
|
99
|
+
#define NODE_API_CHECK_STATUS(the_call) \
|
|
100
|
+
do { \
|
|
101
|
+
napi_status status = (the_call); \
|
|
102
|
+
if (status != napi_ok) { \
|
|
103
|
+
return status; \
|
|
104
|
+
} \
|
|
105
|
+
} while (0)
|
|
106
|
+
|
|
107
|
+
#define NODE_API_ASSERT_STATUS(env, assertion, message) \
|
|
108
|
+
NODE_API_ASSERT_BASE(env, assertion, message, napi_generic_failure)
|
|
109
|
+
|
|
110
|
+
#define DECLARE_NODE_API_PROPERTY(name, func) \
|
|
111
|
+
{ (name), NULL, (func), NULL, NULL, NULL, napi_default, NULL }
|
|
112
|
+
|
|
113
|
+
#define DECLARE_NODE_API_GETTER(name, func) \
|
|
114
|
+
{ (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL }
|
|
115
|
+
|
|
116
|
+
#define DECLARE_NODE_API_PROPERTY_VALUE(name, value) \
|
|
117
|
+
{ (name), NULL, NULL, NULL, NULL, (value), napi_default, NULL }
|
|
118
|
+
|
|
119
|
+
inline void DefineStaticInt32Property(napi_env env, napi_value target, const char* name, int32_t value) {
|
|
120
|
+
napi_value js_value;
|
|
121
|
+
napi_create_int32(env, value, &js_value);
|
|
122
|
+
napi_property_descriptor desc = {
|
|
123
|
+
name,
|
|
124
|
+
nullptr,
|
|
125
|
+
nullptr,
|
|
126
|
+
nullptr,
|
|
127
|
+
nullptr,
|
|
128
|
+
js_value,
|
|
129
|
+
(napi_property_attributes)(
|
|
130
|
+
napi_static | napi_enumerable),
|
|
131
|
+
nullptr
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
napi_define_properties(env, target, 1, &desc);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
#include "runtime_client_api.h"
|
|
138
|
+
#include <node_api.h>
|
|
139
|
+
#include <ostream>
|
|
140
|
+
#include <functional>
|
|
141
|
+
|
|
142
|
+
extern napi_value GetOrCreateAppObj(napi_env env, PWAOS_APP_HANDLE app_handle);
|
|
143
|
+
extern napi_value GetOrCreateInstanceObj(napi_env env, PWAOS_INSTANCE_HANDLE instance_handle);
|
|
144
|
+
extern napi_value GetOrCreateInterfaceObj(napi_env env, PWAOS_OBJECT_INTERFACE_HANDLE interface_handle);
|
|
145
|
+
inline void throw_pwaos_error(napi_env env, pwaosResult result) {
|
|
146
|
+
char msg[PWAOS_MAX_RESULT_NAME_SIZE];
|
|
147
|
+
pwaos_resultToString(result, msg);
|
|
148
|
+
napi_throw_error(env, nullptr, msg);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
#define PWAOS_API_CALL(env, the_call) \
|
|
152
|
+
do { \
|
|
153
|
+
pwaosResult result = the_call; \
|
|
154
|
+
if (result != PWAOS_RESULT_SUCCESS) { \
|
|
155
|
+
throw_pwaos_error(env, result); \
|
|
156
|
+
return nullptr; \
|
|
157
|
+
} \
|
|
158
|
+
} while (0)
|
|
159
|
+
|
|
160
|
+
#define PWAOS_API_CALL_AND_RETURN(env, the_call) \
|
|
161
|
+
do { \
|
|
162
|
+
napi_value result_val; \
|
|
163
|
+
napi_create_int32(env, the_call, &result_val); \
|
|
164
|
+
return result_val; \
|
|
165
|
+
} while (0)
|
|
166
|
+
|
|
167
|
+
struct pwaosNodeResult {
|
|
168
|
+
napi_value node_val;
|
|
169
|
+
pwaosResult val;
|
|
170
|
+
bool have_node_result;
|
|
171
|
+
|
|
172
|
+
pwaosNodeResult(pwaosResult result)
|
|
173
|
+
: val(result), have_node_result(false) {}
|
|
174
|
+
pwaosNodeResult(pwaosResult result, napi_value node_result)
|
|
175
|
+
: node_val(node_result), val(result), have_node_result(true) {}
|
|
176
|
+
};
|
|
177
|
+
struct pwaosPromiseData {
|
|
178
|
+
napi_deferred deferred;
|
|
179
|
+
pwaosNodeResult result;
|
|
180
|
+
std::function<pwaosNodeResult()> work;
|
|
181
|
+
|
|
182
|
+
pwaosPromiseData(napi_deferred d, std::function<pwaosNodeResult()> t)
|
|
183
|
+
: deferred(d), result(PWAOS_RESULT_SUCCESS), work(std::move(t)) {}
|
|
184
|
+
|
|
185
|
+
static void CompleteCallback(napi_env env, napi_value js_cb, void* context, void* data) {
|
|
186
|
+
pwaosPromiseData* p = static_cast<pwaosPromiseData*>(context);
|
|
187
|
+
// assert(p->result.have_node_result == false);
|
|
188
|
+
|
|
189
|
+
if (p->result.val == PWAOS_RESULT_SUCCESS ||
|
|
190
|
+
p->result.val == PWAOS_RESULT_PENDING) {
|
|
191
|
+
napi_value result_val;
|
|
192
|
+
napi_create_int32(env, p->result.val, &result_val);
|
|
193
|
+
napi_resolve_deferred(env, p->deferred, result_val);
|
|
194
|
+
} else {
|
|
195
|
+
char msg[PWAOS_MAX_RESULT_NAME_SIZE];
|
|
196
|
+
pwaos_resultToString(p->result.have_node_result ? p->result.val : PWAOS_RESULT_ERROR_INTERNAL, msg);
|
|
197
|
+
napi_value error;
|
|
198
|
+
napi_create_string_utf8(env, msg, NAPI_AUTO_LENGTH, &error);
|
|
199
|
+
napi_reject_deferred(env, p->deferred, error);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
delete p;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
#define PWAOS_API_CALL_AND_RETURN_PROMISE(env, task) \
|
|
207
|
+
do { \
|
|
208
|
+
napi_value promise; \
|
|
209
|
+
napi_deferred deferred; \
|
|
210
|
+
NODE_API_CALL(env, napi_create_promise(env, &deferred, &promise)); \
|
|
211
|
+
\
|
|
212
|
+
auto data = std::make_unique<pwaosPromiseData>(deferred, task); \
|
|
213
|
+
auto execute = [](napi_env env, void* d) { \
|
|
214
|
+
pwaosPromiseData* p = static_cast<pwaosPromiseData*>(d); \
|
|
215
|
+
p->result = p->work(); \
|
|
216
|
+
}; \
|
|
217
|
+
auto complete = [](napi_env env, napi_status status, void* d) { \
|
|
218
|
+
pwaosPromiseData* p = static_cast<pwaosPromiseData*>(d); \
|
|
219
|
+
if (p->result.val == PWAOS_RESULT_SUCCESS || \
|
|
220
|
+
p->result.val == PWAOS_RESULT_PENDING) { \
|
|
221
|
+
napi_value result_val; \
|
|
222
|
+
if (!p->result.have_node_result) \
|
|
223
|
+
napi_create_int32(env, p->result.val, &result_val); \
|
|
224
|
+
else \
|
|
225
|
+
result_val = p->result.node_val; \
|
|
226
|
+
napi_resolve_deferred(env, p->deferred, result_val); \
|
|
227
|
+
} else { \
|
|
228
|
+
char msg[PWAOS_MAX_RESULT_NAME_SIZE]; \
|
|
229
|
+
pwaos_resultToString(p->result.val, msg); \
|
|
230
|
+
napi_value error; \
|
|
231
|
+
napi_create_string_utf8(env, msg, NAPI_AUTO_LENGTH, &error); \
|
|
232
|
+
napi_reject_deferred(env, p->deferred, error); \
|
|
233
|
+
} \
|
|
234
|
+
delete p; \
|
|
235
|
+
}; \
|
|
236
|
+
\
|
|
237
|
+
napi_value work_name; \
|
|
238
|
+
napi_create_string_utf8(env, #task, NAPI_AUTO_LENGTH, &work_name); \
|
|
239
|
+
napi_async_work work; \
|
|
240
|
+
NODE_API_CALL(env, napi_create_async_work(env, nullptr, work_name, \
|
|
241
|
+
execute, complete, data.get(), &work)); \
|
|
242
|
+
NODE_API_CALL(env, napi_queue_async_work(env, work)); \
|
|
243
|
+
data.release(); \
|
|
244
|
+
return promise; \
|
|
245
|
+
} while (0)
|
|
246
|
+
|
|
247
|
+
struct pwaosPromiseData_tsfn : public pwaosPromiseData {
|
|
248
|
+
napi_threadsafe_function tsfn;
|
|
249
|
+
napi_value promise;
|
|
250
|
+
void* data;
|
|
251
|
+
|
|
252
|
+
pwaosPromiseData_tsfn(napi_deferred d,
|
|
253
|
+
std::function<pwaosNodeResult()> t,
|
|
254
|
+
napi_threadsafe_function f)
|
|
255
|
+
: pwaosPromiseData(d, std::move(t)), tsfn(f), data(nullptr) {}
|
|
256
|
+
~pwaosPromiseData_tsfn() {
|
|
257
|
+
// assert(data == nullptr);
|
|
258
|
+
napi_release_threadsafe_function(tsfn, napi_tsfn_release);
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
#define PWAOS_API_MAKE_TSFN_PROMISE(env, task, func_val, js_cb) \
|
|
262
|
+
[&]() -> std::unique_ptr<pwaosPromiseData_tsfn> { \
|
|
263
|
+
napi_value promise; \
|
|
264
|
+
napi_deferred deferred; \
|
|
265
|
+
NODE_API_CALL(env, napi_create_promise(env, &deferred, &promise)); \
|
|
266
|
+
auto data = std::make_unique<pwaosPromiseData_tsfn>(deferred, task, nullptr); \
|
|
267
|
+
if (!data) return nullptr; \
|
|
268
|
+
napi_value async_name; \
|
|
269
|
+
napi_create_string_utf8(env, #js_cb, NAPI_AUTO_LENGTH, &async_name); \
|
|
270
|
+
napi_status status = napi_create_threadsafe_function( \
|
|
271
|
+
env, func_val, nullptr, async_name, 0, 1, \
|
|
272
|
+
nullptr, nullptr, data.get(), js_cb, &data->tsfn); \
|
|
273
|
+
if (status != napi_ok) { \
|
|
274
|
+
napi_throw_error(env, nullptr, "Failed to create threadsafe function"); \
|
|
275
|
+
return nullptr; \
|
|
276
|
+
} \
|
|
277
|
+
data->promise = promise; \
|
|
278
|
+
return data; \
|
|
279
|
+
}()
|
|
280
|
+
|
|
281
|
+
#define PWAOS_API_CLEAR_CALLBACK(env, obj, method_name) \
|
|
282
|
+
do { \
|
|
283
|
+
napi_value _method; \
|
|
284
|
+
napi_get_named_property((env), (obj), method_name, &_method); \
|
|
285
|
+
napi_value js_null; \
|
|
286
|
+
napi_get_null(env, &js_null); \
|
|
287
|
+
napi_value _args[2] = { js_null, js_null }; \
|
|
288
|
+
napi_call_function(env, obj, _method, 2, _args, nullptr); \
|
|
289
|
+
} while (0)
|
|
290
|
+
|
|
291
|
+
#endif // CHROMIUM_PWAOS_THIRDPARTY_NODEJS_ADDON_COMMON_H_
|