|
| 1 | +#include "node_api.h" |
| 2 | +#include "uv.h" |
| 3 | +#include "../common.h" |
| 4 | + |
| 5 | +namespace { |
| 6 | + |
| 7 | +// the test needs to fake out the async structure, so we need to use |
| 8 | +// the raw structure here and then cast as done behind the scenes |
| 9 | +// in napi calls. |
| 10 | +struct async_context { |
| 11 | + double async_id; |
| 12 | + double trigger_async_id; |
| 13 | +}; |
| 14 | + |
| 15 | + |
| 16 | +napi_value RunInCallbackScope(napi_env env, napi_callback_info info) { |
| 17 | + size_t argc; |
| 18 | + napi_value args[4]; |
| 19 | + |
| 20 | + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr)); |
| 21 | + NAPI_ASSERT(env, argc == 4 , "Wrong number of arguments"); |
| 22 | + |
| 23 | + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); |
| 24 | + |
| 25 | + napi_valuetype valuetype; |
| 26 | + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); |
| 27 | + NAPI_ASSERT(env, valuetype == napi_object, |
| 28 | + "Wrong type of arguments. Expects an object as first argument."); |
| 29 | + |
| 30 | + NAPI_CALL(env, napi_typeof(env, args[1], &valuetype)); |
| 31 | + NAPI_ASSERT(env, valuetype == napi_number, |
| 32 | + "Wrong type of arguments. Expects a number as second argument."); |
| 33 | + |
| 34 | + NAPI_CALL(env, napi_typeof(env, args[2], &valuetype)); |
| 35 | + NAPI_ASSERT(env, valuetype == napi_number, |
| 36 | + "Wrong type of arguments. Expects a number as third argument."); |
| 37 | + |
| 38 | + NAPI_CALL(env, napi_typeof(env, args[3], &valuetype)); |
| 39 | + NAPI_ASSERT(env, valuetype == napi_function, |
| 40 | + "Wrong type of arguments. Expects a function as third argument."); |
| 41 | + |
| 42 | + struct async_context context; |
| 43 | + NAPI_CALL(env, napi_get_value_double(env, args[1], &context.async_id)); |
| 44 | + NAPI_CALL(env, |
| 45 | + napi_get_value_double(env, args[2], &context.trigger_async_id)); |
| 46 | + |
| 47 | + napi_callback_scope scope = nullptr; |
| 48 | + NAPI_CALL( |
| 49 | + env, |
| 50 | + napi_open_callback_scope(env, |
| 51 | + args[0], |
| 52 | + reinterpret_cast<napi_async_context>(&context), |
| 53 | + &scope)); |
| 54 | + |
| 55 | + // if the function has an exception pending after the call that is ok |
| 56 | + // so we don't use NAPI_CALL as we must close the callback scope regardless |
| 57 | + napi_value result = nullptr; |
| 58 | + napi_status function_call_result = |
| 59 | + napi_call_function(env, args[0], args[3], 0, nullptr, &result); |
| 60 | + if (function_call_result != napi_ok) { |
| 61 | + GET_AND_THROW_LAST_ERROR((env)); |
| 62 | + } |
| 63 | + |
| 64 | + NAPI_CALL(env, napi_close_callback_scope(env, scope)); |
| 65 | + |
| 66 | + return result; |
| 67 | +} |
| 68 | + |
| 69 | +static napi_env shared_env = nullptr; |
| 70 | +static napi_deferred deferred = nullptr; |
| 71 | + |
| 72 | +static void Callback(uv_work_t* req, int ignored) { |
| 73 | + napi_env env = shared_env; |
| 74 | + |
| 75 | + napi_handle_scope handle_scope = nullptr; |
| 76 | + NAPI_CALL_RETURN_VOID(env, napi_open_handle_scope(env, &handle_scope)); |
| 77 | + |
| 78 | + napi_value resource_name; |
| 79 | + NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8( |
| 80 | + env, "test", NAPI_AUTO_LENGTH, &resource_name)); |
| 81 | + napi_async_context context; |
| 82 | + NAPI_CALL_RETURN_VOID(env, |
| 83 | + napi_async_init(env, nullptr, resource_name, &context)); |
| 84 | + |
| 85 | + napi_value resource_object; |
| 86 | + NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &resource_object)); |
| 87 | + |
| 88 | + napi_value undefined_value; |
| 89 | + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined_value)); |
| 90 | + |
| 91 | + napi_callback_scope scope = nullptr; |
| 92 | + NAPI_CALL_RETURN_VOID(env, napi_open_callback_scope(env, |
| 93 | + resource_object, |
| 94 | + context, |
| 95 | + &scope)); |
| 96 | + |
| 97 | + NAPI_CALL_RETURN_VOID(env, |
| 98 | + napi_resolve_deferred(env, deferred, undefined_value)); |
| 99 | + |
| 100 | + NAPI_CALL_RETURN_VOID(env, napi_close_callback_scope(env, scope)); |
| 101 | + |
| 102 | + NAPI_CALL_RETURN_VOID(env, napi_close_handle_scope(env, handle_scope)); |
| 103 | + delete req; |
| 104 | +} |
| 105 | + |
| 106 | +napi_value TestResolveAsync(napi_env env, napi_callback_info info) { |
| 107 | + napi_value promise = nullptr; |
| 108 | + if (deferred == nullptr) { |
| 109 | + shared_env = env; |
| 110 | + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); |
| 111 | + |
| 112 | + uv_loop_t* loop = nullptr; |
| 113 | + NAPI_CALL(env, napi_get_uv_event_loop(env, &loop)); |
| 114 | + |
| 115 | + uv_work_t* req = new uv_work_t(); |
| 116 | + uv_queue_work(loop, |
| 117 | + req, |
| 118 | + [](uv_work_t*) {}, |
| 119 | + Callback); |
| 120 | + } |
| 121 | + return promise; |
| 122 | +} |
| 123 | + |
| 124 | +napi_value Init(napi_env env, napi_value exports) { |
| 125 | + napi_property_descriptor descriptors[] = { |
| 126 | + DECLARE_NAPI_PROPERTY("runInCallbackScope", RunInCallbackScope), |
| 127 | + DECLARE_NAPI_PROPERTY("testResolveAsync", TestResolveAsync) |
| 128 | + }; |
| 129 | + |
| 130 | + NAPI_CALL(env, napi_define_properties( |
| 131 | + env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); |
| 132 | + |
| 133 | + return exports; |
| 134 | +} |
| 135 | + |
| 136 | +} // anonymous namespace |
| 137 | + |
| 138 | +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
0 commit comments