-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_cannot_run_js.c
More file actions
66 lines (58 loc) · 2.28 KB
/
test_cannot_run_js.c
File metadata and controls
66 lines (58 loc) · 2.28 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
65
66
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
#include "stdlib.h"
static void Finalize(napi_env env, void* data, void* hint) {
napi_value global, set_timeout;
napi_ref* ref = data;
NODE_API_BASIC_ASSERT_RETURN_VOID(
napi_delete_reference(env, *ref) == napi_ok,
"deleting reference in finalizer should succeed");
NODE_API_BASIC_ASSERT_RETURN_VOID(
napi_get_global(env, &global) == napi_ok,
"getting global reference in finalizer should succeed");
napi_status result =
napi_get_named_property(env, global, "setTimeout", &set_timeout);
// The finalizer could be invoked either from check callbacks (as native
// immediates) if the event loop is still running (where napi_ok is returned)
// or during environment shutdown (where napi_cannot_run_js or
// napi_pending_exception is returned). This is not deterministic from
// the point of view of the addon.
#if NAPI_VERSION > 9
NODE_API_BASIC_ASSERT_RETURN_VOID(
result == napi_cannot_run_js || result == napi_ok,
"getting named property from global in finalizer should succeed "
"or return napi_cannot_run_js");
#else
NODE_API_BASIC_ASSERT_RETURN_VOID(
result == napi_pending_exception || result == napi_ok,
"getting named property from global in finalizer should succeed "
"or return napi_pending_exception");
#endif // NAPI_VERSION > 9
free(ref);
}
static napi_value CreateRef(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value cb;
napi_valuetype value_type;
napi_ref* ref = malloc(sizeof(*ref));
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &cb, NULL, NULL));
NODE_API_ASSERT(env, argc == 1, "Function takes only one argument");
NODE_API_CALL(env, napi_typeof(env, cb, &value_type));
NODE_API_ASSERT(
env, value_type == napi_function, "argument must be function");
NODE_API_CALL(env, napi_add_finalizer(env, cb, ref, Finalize, NULL, ref));
return cb;
}
EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
DECLARE_NODE_API_PROPERTY("createRef", CreateRef),
};
NODE_API_CALL(
env,
napi_define_properties(
env, exports, sizeof(properties) / sizeof(*properties), properties));
return exports;
}
EXTERN_C_END