11cb0ef41Sopenharmony_ci#include <js_native_api.h> 21cb0ef41Sopenharmony_ci#include "../common.h" 31cb0ef41Sopenharmony_ci#include "../entry_point.h" 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_cinapi_deferred deferred = NULL; 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_cistatic napi_value createPromise(napi_env env, napi_callback_info info) { 81cb0ef41Sopenharmony_ci napi_value promise; 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci // We do not overwrite an existing deferred. 111cb0ef41Sopenharmony_ci if (deferred != NULL) { 121cb0ef41Sopenharmony_ci return NULL; 131cb0ef41Sopenharmony_ci } 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_create_promise(env, &deferred, &promise)); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci return promise; 181cb0ef41Sopenharmony_ci} 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cistatic napi_value 211cb0ef41Sopenharmony_ciconcludeCurrentPromise(napi_env env, napi_callback_info info) { 221cb0ef41Sopenharmony_ci napi_value argv[2]; 231cb0ef41Sopenharmony_ci size_t argc = 2; 241cb0ef41Sopenharmony_ci bool resolution; 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); 271cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_value_bool(env, argv[1], &resolution)); 281cb0ef41Sopenharmony_ci if (resolution) { 291cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_resolve_deferred(env, deferred, argv[0])); 301cb0ef41Sopenharmony_ci } else { 311cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_reject_deferred(env, deferred, argv[0])); 321cb0ef41Sopenharmony_ci } 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci deferred = NULL; 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci return NULL; 371cb0ef41Sopenharmony_ci} 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cistatic napi_value isPromise(napi_env env, napi_callback_info info) { 401cb0ef41Sopenharmony_ci napi_value promise, result; 411cb0ef41Sopenharmony_ci size_t argc = 1; 421cb0ef41Sopenharmony_ci bool is_promise; 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, &promise, NULL, NULL)); 451cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_is_promise(env, promise, &is_promise)); 461cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_boolean(env, is_promise, &result)); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci return result; 491cb0ef41Sopenharmony_ci} 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ciEXTERN_C_START 521cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) { 531cb0ef41Sopenharmony_ci napi_property_descriptor descriptors[] = { 541cb0ef41Sopenharmony_ci DECLARE_NODE_API_PROPERTY("createPromise", createPromise), 551cb0ef41Sopenharmony_ci DECLARE_NODE_API_PROPERTY("concludeCurrentPromise", concludeCurrentPromise), 561cb0ef41Sopenharmony_ci DECLARE_NODE_API_PROPERTY("isPromise", isPromise), 571cb0ef41Sopenharmony_ci }; 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_define_properties( 601cb0ef41Sopenharmony_ci env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci return exports; 631cb0ef41Sopenharmony_ci} 641cb0ef41Sopenharmony_ciEXTERN_C_END 65