11cb0ef41Sopenharmony_ci#include <assert.h> 21cb0ef41Sopenharmony_ci#include <js_native_api.h> 31cb0ef41Sopenharmony_ci#include <stdlib.h> 41cb0ef41Sopenharmony_ci#include "../common.h" 51cb0ef41Sopenharmony_ci#include "../entry_point.h" 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_cistatic int test_value = 1; 81cb0ef41Sopenharmony_cistatic int finalize_count = 0; 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_cistatic void FinalizeExternalCallJs(napi_env env, void* data, void* hint) { 111cb0ef41Sopenharmony_ci int* actual_value = data; 121cb0ef41Sopenharmony_ci NODE_API_ASSERT_RETURN_VOID( 131cb0ef41Sopenharmony_ci env, 141cb0ef41Sopenharmony_ci actual_value == &test_value, 151cb0ef41Sopenharmony_ci "The correct pointer was passed to the finalizer"); 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci napi_ref finalizer_ref = (napi_ref)hint; 181cb0ef41Sopenharmony_ci napi_value js_finalizer; 191cb0ef41Sopenharmony_ci napi_value recv; 201cb0ef41Sopenharmony_ci NODE_API_CALL_RETURN_VOID( 211cb0ef41Sopenharmony_ci env, napi_get_reference_value(env, finalizer_ref, &js_finalizer)); 221cb0ef41Sopenharmony_ci NODE_API_CALL_RETURN_VOID(env, napi_get_global(env, &recv)); 231cb0ef41Sopenharmony_ci NODE_API_CALL_RETURN_VOID( 241cb0ef41Sopenharmony_ci env, napi_call_function(env, recv, js_finalizer, 0, NULL, NULL)); 251cb0ef41Sopenharmony_ci NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, finalizer_ref)); 261cb0ef41Sopenharmony_ci} 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_cistatic napi_value CreateExternalWithJsFinalize(napi_env env, 291cb0ef41Sopenharmony_ci napi_callback_info info) { 301cb0ef41Sopenharmony_ci size_t argc = 1; 311cb0ef41Sopenharmony_ci napi_value args[1]; 321cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); 331cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments"); 341cb0ef41Sopenharmony_ci napi_value finalizer = args[0]; 351cb0ef41Sopenharmony_ci napi_valuetype finalizer_valuetype; 361cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_typeof(env, finalizer, &finalizer_valuetype)); 371cb0ef41Sopenharmony_ci NODE_API_ASSERT(env, 381cb0ef41Sopenharmony_ci finalizer_valuetype == napi_function, 391cb0ef41Sopenharmony_ci "Wrong type of first argument"); 401cb0ef41Sopenharmony_ci napi_ref finalizer_ref; 411cb0ef41Sopenharmony_ci NODE_API_CALL(env, napi_create_reference(env, finalizer, 1, &finalizer_ref)); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci napi_value result; 441cb0ef41Sopenharmony_ci NODE_API_CALL(env, 451cb0ef41Sopenharmony_ci napi_create_external(env, 461cb0ef41Sopenharmony_ci &test_value, 471cb0ef41Sopenharmony_ci FinalizeExternalCallJs, 481cb0ef41Sopenharmony_ci finalizer_ref, /* finalize_hint */ 491cb0ef41Sopenharmony_ci &result)); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci finalize_count = 0; 521cb0ef41Sopenharmony_ci return result; 531cb0ef41Sopenharmony_ci} 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ciEXTERN_C_START 561cb0ef41Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) { 571cb0ef41Sopenharmony_ci napi_property_descriptor descriptors[] = { 581cb0ef41Sopenharmony_ci DECLARE_NODE_API_PROPERTY("createExternalWithJsFinalize", 591cb0ef41Sopenharmony_ci CreateExternalWithJsFinalize), 601cb0ef41Sopenharmony_ci }; 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci NODE_API_CALL( 631cb0ef41Sopenharmony_ci env, 641cb0ef41Sopenharmony_ci napi_define_properties(env, 651cb0ef41Sopenharmony_ci exports, 661cb0ef41Sopenharmony_ci sizeof(descriptors) / sizeof(*descriptors), 671cb0ef41Sopenharmony_ci descriptors)); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci return exports; 701cb0ef41Sopenharmony_ci} 711cb0ef41Sopenharmony_ciEXTERN_C_END 72