mirror of
https://github.com/nodejs/node.git
synced 2025-05-09 15:46:27 +00:00

As per discussion in abi-stable-node: https://github.com/nodejs/abi-stable-node/issues/256, take a refactor to napi_addon_register_func such that the result from the register function is assigned to the module exports property. By making this change, native module can be agnostic about which type of module the environment supports. PR-URL: https://github.com/nodejs/node/pull/15088 Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Hitesh Kanwathirtha <digitalinfinity@gmail.com>
63 lines
1.6 KiB
C
63 lines
1.6 KiB
C
#include <node_api.h>
|
|
#include "../common.h"
|
|
|
|
napi_deferred deferred = NULL;
|
|
|
|
napi_value createPromise(napi_env env, napi_callback_info info) {
|
|
napi_value promise;
|
|
|
|
// We do not overwrite an existing deferred.
|
|
if (deferred != NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));
|
|
|
|
return promise;
|
|
}
|
|
|
|
napi_value concludeCurrentPromise(napi_env env, napi_callback_info info) {
|
|
napi_value argv[2];
|
|
size_t argc = 2;
|
|
bool resolution;
|
|
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
|
|
NAPI_CALL(env, napi_get_value_bool(env, argv[1], &resolution));
|
|
if (resolution) {
|
|
NAPI_CALL(env, napi_resolve_deferred(env, deferred, argv[0]));
|
|
} else {
|
|
NAPI_CALL(env, napi_reject_deferred(env, deferred, argv[0]));
|
|
}
|
|
|
|
deferred = NULL;
|
|
|
|
return NULL;
|
|
}
|
|
|
|
napi_value isPromise(napi_env env, napi_callback_info info) {
|
|
napi_value promise, result;
|
|
size_t argc = 1;
|
|
bool is_promise;
|
|
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &promise, NULL, NULL));
|
|
NAPI_CALL(env, napi_is_promise(env, promise, &is_promise));
|
|
NAPI_CALL(env, napi_get_boolean(env, is_promise, &result));
|
|
|
|
return result;
|
|
}
|
|
|
|
napi_value Init(napi_env env, napi_value exports) {
|
|
napi_property_descriptor descriptors[] = {
|
|
DECLARE_NAPI_PROPERTY("createPromise", createPromise),
|
|
DECLARE_NAPI_PROPERTY("concludeCurrentPromise", concludeCurrentPromise),
|
|
DECLARE_NAPI_PROPERTY("isPromise", isPromise),
|
|
};
|
|
|
|
NAPI_CALL(env, napi_define_properties(
|
|
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
|
|
|
|
return exports;
|
|
}
|
|
|
|
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
|