1 #include <js_native_api.h>
2 #include <string.h>
3 #include "../common.h"
4 #include "../entry_point.h"
5
6 // these tests validate the handle scope functions in the normal
7 // flow. Forcing gc behavior to fully validate they are doing
8 // the right right thing would be quite hard so we keep it
9 // simple for now.
10
NewScope(napi_env env, napi_callback_info info)11 static napi_value NewScope(napi_env env, napi_callback_info info) {
12 napi_handle_scope scope;
13 napi_value output = NULL;
14
15 NODE_API_CALL(env, napi_open_handle_scope(env, &scope));
16 NODE_API_CALL(env, napi_create_object(env, &output));
17 NODE_API_CALL(env, napi_close_handle_scope(env, scope));
18 return NULL;
19 }
20
NewScopeEscape(napi_env env, napi_callback_info info)21 static napi_value NewScopeEscape(napi_env env, napi_callback_info info) {
22 napi_escapable_handle_scope scope;
23 napi_value output = NULL;
24 napi_value escapee = NULL;
25
26 NODE_API_CALL(env, napi_open_escapable_handle_scope(env, &scope));
27 NODE_API_CALL(env, napi_create_object(env, &output));
28 NODE_API_CALL(env, napi_escape_handle(env, scope, output, &escapee));
29 NODE_API_CALL(env, napi_close_escapable_handle_scope(env, scope));
30 return escapee;
31 }
32
NewScopeEscapeTwice(napi_env env, napi_callback_info info)33 static napi_value NewScopeEscapeTwice(napi_env env, napi_callback_info info) {
34 napi_escapable_handle_scope scope;
35 napi_value output = NULL;
36 napi_value escapee = NULL;
37 napi_status status;
38
39 NODE_API_CALL(env, napi_open_escapable_handle_scope(env, &scope));
40 NODE_API_CALL(env, napi_create_object(env, &output));
41 NODE_API_CALL(env, napi_escape_handle(env, scope, output, &escapee));
42 status = napi_escape_handle(env, scope, output, &escapee);
43 NODE_API_ASSERT(env, status == napi_escape_called_twice, "Escaping twice fails");
44 NODE_API_CALL(env, napi_close_escapable_handle_scope(env, scope));
45 return NULL;
46 }
47
NewScopeWithException(napi_env env, napi_callback_info info)48 static napi_value NewScopeWithException(napi_env env, napi_callback_info info) {
49 napi_handle_scope scope;
50 size_t argc;
51 napi_value exception_function;
52 napi_status status;
53 napi_value output = NULL;
54
55 NODE_API_CALL(env, napi_open_handle_scope(env, &scope));
56 NODE_API_CALL(env, napi_create_object(env, &output));
57
58 argc = 1;
59 NODE_API_CALL(env, napi_get_cb_info(
60 env, info, &argc, &exception_function, NULL, NULL));
61
62 status = napi_call_function(
63 env, output, exception_function, 0, NULL, NULL);
64 NODE_API_ASSERT(env, status == napi_pending_exception,
65 "Function should have thrown.");
66
67 // Closing a handle scope should still work while an exception is pending.
68 NODE_API_CALL(env, napi_close_handle_scope(env, scope));
69 return NULL;
70 }
71
72 EXTERN_C_START
Init(napi_env env, napi_value exports)73 napi_value Init(napi_env env, napi_value exports) {
74 napi_property_descriptor properties[] = {
75 DECLARE_NODE_API_PROPERTY("NewScope", NewScope),
76 DECLARE_NODE_API_PROPERTY("NewScopeEscape", NewScopeEscape),
77 DECLARE_NODE_API_PROPERTY("NewScopeEscapeTwice", NewScopeEscapeTwice),
78 DECLARE_NODE_API_PROPERTY("NewScopeWithException", NewScopeWithException),
79 };
80
81 NODE_API_CALL(env, napi_define_properties(
82 env, exports, sizeof(properties) / sizeof(*properties), properties));
83
84 return exports;
85 }
86 EXTERN_C_END
87