From 282b92d6e1a293bcefa0ff9549121f789ca0818d Mon Sep 17 00:00:00 2001 From: 190n Date: Mon, 9 Sep 2024 16:08:40 -0800 Subject: [PATCH] Fix issues with NAPI tests (#13831) Co-authored-by: 190n <190n@users.noreply.github.com> --- test/napi/napi-app/binding.gyp | 39 +++++++++++++++++++--------------- test/napi/napi-app/main.cpp | 28 ++++++++++++++++++++---- test/napi/napi.test.ts | 6 ++++++ 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/test/napi/napi-app/binding.gyp b/test/napi/napi-app/binding.gyp index 0cc549a69a..aebdebb7ef 100644 --- a/test/napi/napi-app/binding.gyp +++ b/test/napi/napi-app/binding.gyp @@ -1,18 +1,23 @@ { - "targets": [{ - "target_name": "napitests", - "cflags!": [ "-fno-exceptions" ], - "cflags_cc!": [ "-fno-exceptions" ], - "sources": [ - "main.cpp" - ], - 'include_dirs': [ - " -#include -#include #include -#include -#include +#include #include +#include +#include +#include +#include napi_value fail(napi_env env, const char *msg) { napi_value result; @@ -431,6 +432,24 @@ napi_value create_promise(const Napi::CallbackInfo &info) { return promise; } +napi_value test_napi_ref(const Napi::CallbackInfo &info) { + napi_env env = info.Env(); + + napi_value object; + assert(napi_create_object(env, &object) == napi_ok); + + napi_ref ref; + assert(napi_create_reference(env, object, 0, &ref) == napi_ok); + + napi_value from_ref; + assert(napi_get_reference_value(env, ref, &from_ref) == napi_ok); + assert(from_ref != nullptr); + napi_valuetype typeof_result; + assert(napi_typeof(env, from_ref, &typeof_result) == napi_ok); + assert(typeof_result == napi_object); + return ok(env); +} + Napi::Value RunCallback(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Function cb = info[0].As(); @@ -472,6 +491,7 @@ Napi::Object InitAll(Napi::Env env, Napi::Object exports1) { exports.Set("get_class_with_constructor", Napi::Function::New(env, get_class_with_constructor)); exports.Set("create_promise", Napi::Function::New(env, create_promise)); + exports.Set("test_napi_ref", Napi::Function::New(env, test_napi_ref)); return exports; } diff --git a/test/napi/napi.test.ts b/test/napi/napi.test.ts index 17828b23c1..876c7cbc77 100644 --- a/test/napi/napi.test.ts +++ b/test/napi/napi.test.ts @@ -116,6 +116,12 @@ describe("napi", () => { ); }); }); + + describe("napi_ref", () => { + it("can recover the value from a weak ref", () => { + checkSameOutput("test_napi_ref", []); + }); + }); }); function checkSameOutput(test: string, args: any[] | string) {