133eb0b6dSopenharmony_ci/*
233eb0b6dSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License.
533eb0b6dSopenharmony_ci * You may obtain a copy of the License at
633eb0b6dSopenharmony_ci *
733eb0b6dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
833eb0b6dSopenharmony_ci *
933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and
1333eb0b6dSopenharmony_ci * limitations under the License.
1433eb0b6dSopenharmony_ci */
1533eb0b6dSopenharmony_ci
1633eb0b6dSopenharmony_ci#include "napi/native_common.h"
1733eb0b6dSopenharmony_ci#include "napi/native_api.h"
1833eb0b6dSopenharmony_ci#include "napi/native_node_api.h"
1933eb0b6dSopenharmony_ci#include "securec.h"
2033eb0b6dSopenharmony_ci#include "test.h"
2133eb0b6dSopenharmony_ci#include "test_common.h"
2233eb0b6dSopenharmony_ci#include "utils/log.h"
2333eb0b6dSopenharmony_ci#ifdef FOR_JERRYSCRIPT_TEST
2433eb0b6dSopenharmony_ci#include "jerryscript-core.h"
2533eb0b6dSopenharmony_ci#endif
2633eb0b6dSopenharmony_ci
2733eb0b6dSopenharmony_cistatic constexpr int32_t NAPI_UT_BUFFER_SIZE = 64;
2833eb0b6dSopenharmony_ci
2933eb0b6dSopenharmony_ciclass NapiExtTest : public NativeEngineTest {
3033eb0b6dSopenharmony_cipublic:
3133eb0b6dSopenharmony_ci    static void SetUpTestCase()
3233eb0b6dSopenharmony_ci    {
3333eb0b6dSopenharmony_ci        GTEST_LOG_(INFO) << "NapiExtTest SetUpTestCase";
3433eb0b6dSopenharmony_ci    }
3533eb0b6dSopenharmony_ci
3633eb0b6dSopenharmony_ci    static void TearDownTestCase()
3733eb0b6dSopenharmony_ci    {
3833eb0b6dSopenharmony_ci        GTEST_LOG_(INFO) << "NapiExtTest TearDownTestCase";
3933eb0b6dSopenharmony_ci    }
4033eb0b6dSopenharmony_ci
4133eb0b6dSopenharmony_ci    void SetUp() override {}
4233eb0b6dSopenharmony_ci    void TearDown() override {}
4333eb0b6dSopenharmony_ci};
4433eb0b6dSopenharmony_ci
4533eb0b6dSopenharmony_ci/**
4633eb0b6dSopenharmony_ci * @tc.name: UndefinedTest
4733eb0b6dSopenharmony_ci * @tc.desc: Test undefined type.
4833eb0b6dSopenharmony_ci * @tc.type: FUNC
4933eb0b6dSopenharmony_ci */
5033eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, CreateBufferTest001, testing::ext::TestSize.Level1)
5133eb0b6dSopenharmony_ci{
5233eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
5333eb0b6dSopenharmony_ci
5433eb0b6dSopenharmony_ci    napi_value buffer = nullptr;
5533eb0b6dSopenharmony_ci    void* bufferPtr = nullptr;
5633eb0b6dSopenharmony_ci    size_t bufferSize = NAPI_UT_BUFFER_SIZE;
5733eb0b6dSopenharmony_ci    napi_create_buffer(env, bufferSize, &bufferPtr, &buffer);
5833eb0b6dSopenharmony_ci    void* tmpBufferPtr = nullptr;
5933eb0b6dSopenharmony_ci    size_t bufferLength = 0;
6033eb0b6dSopenharmony_ci    napi_get_buffer_info(env, buffer, &tmpBufferPtr, &bufferLength);
6133eb0b6dSopenharmony_ci
6233eb0b6dSopenharmony_ci    ASSERT_EQ(bufferPtr, tmpBufferPtr);
6333eb0b6dSopenharmony_ci    ASSERT_EQ(bufferSize, bufferLength);
6433eb0b6dSopenharmony_ci}
6533eb0b6dSopenharmony_ci
6633eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, CreateBufferTest003, testing::ext::TestSize.Level1)
6733eb0b6dSopenharmony_ci{
6833eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
6933eb0b6dSopenharmony_ci
7033eb0b6dSopenharmony_ci    napi_value buffer = nullptr;
7133eb0b6dSopenharmony_ci    void* bufferPtr = nullptr;
7233eb0b6dSopenharmony_ci    const char bufferdata[] = "for test";
7333eb0b6dSopenharmony_ci    const char* data = bufferdata;
7433eb0b6dSopenharmony_ci    size_t bufferSize = NAPI_UT_BUFFER_SIZE;
7533eb0b6dSopenharmony_ci    napi_create_buffer_copy(env, bufferSize, data, &bufferPtr, &buffer);
7633eb0b6dSopenharmony_ci
7733eb0b6dSopenharmony_ci    void* tmpBufferPtr = nullptr;
7833eb0b6dSopenharmony_ci    size_t bufferLength = 0;
7933eb0b6dSopenharmony_ci    napi_get_buffer_info(env, buffer, &tmpBufferPtr, &bufferLength);
8033eb0b6dSopenharmony_ci
8133eb0b6dSopenharmony_ci    ASSERT_EQ(bufferPtr, tmpBufferPtr);
8233eb0b6dSopenharmony_ci    ASSERT_EQ(bufferSize, bufferLength);
8333eb0b6dSopenharmony_ci    ASSERT_EQ(0, memcmp(bufferdata, bufferPtr, bufferSize));
8433eb0b6dSopenharmony_ci}
8533eb0b6dSopenharmony_ci
8633eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, CreateBufferTest005, testing::ext::TestSize.Level1)
8733eb0b6dSopenharmony_ci{
8833eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
8933eb0b6dSopenharmony_ci
9033eb0b6dSopenharmony_ci    napi_value buffer = nullptr;
9133eb0b6dSopenharmony_ci    char testStr[] = "test";
9233eb0b6dSopenharmony_ci    void* bufferPtr = testStr;
9333eb0b6dSopenharmony_ci
9433eb0b6dSopenharmony_ci    size_t bufferSize = NAPI_UT_BUFFER_SIZE;
9533eb0b6dSopenharmony_ci    napi_create_external_buffer(
9633eb0b6dSopenharmony_ci        env, bufferSize, bufferPtr, [](napi_env env, void* data, void* hint) {}, (void*)testStr, &buffer);
9733eb0b6dSopenharmony_ci
9833eb0b6dSopenharmony_ci    void* tmpBufferPtr = nullptr;
9933eb0b6dSopenharmony_ci    size_t bufferLength = 0;
10033eb0b6dSopenharmony_ci    napi_get_buffer_info(env, buffer, &tmpBufferPtr, &bufferLength);
10133eb0b6dSopenharmony_ci    bool isBuffer = false;
10233eb0b6dSopenharmony_ci    napi_is_buffer(env, buffer, &isBuffer);
10333eb0b6dSopenharmony_ci
10433eb0b6dSopenharmony_ci    ASSERT_EQ(bufferSize, bufferLength);
10533eb0b6dSopenharmony_ci}
10633eb0b6dSopenharmony_ci
10733eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, IsBufferTest001, testing::ext::TestSize.Level1)
10833eb0b6dSopenharmony_ci{
10933eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
11033eb0b6dSopenharmony_ci
11133eb0b6dSopenharmony_ci    napi_value buffer = nullptr;
11233eb0b6dSopenharmony_ci    void* bufferPtr = nullptr;
11333eb0b6dSopenharmony_ci    size_t bufferSize = NAPI_UT_BUFFER_SIZE;
11433eb0b6dSopenharmony_ci    bool isBuffer = false;
11533eb0b6dSopenharmony_ci
11633eb0b6dSopenharmony_ci    napi_create_buffer(env, bufferSize, &bufferPtr, &buffer);
11733eb0b6dSopenharmony_ci
11833eb0b6dSopenharmony_ci    void* tmpBufferPtr = nullptr;
11933eb0b6dSopenharmony_ci    size_t bufferLength = 0;
12033eb0b6dSopenharmony_ci    napi_get_buffer_info(env, buffer, &tmpBufferPtr, &bufferLength);
12133eb0b6dSopenharmony_ci    napi_is_buffer(env, buffer, &isBuffer);
12233eb0b6dSopenharmony_ci
12333eb0b6dSopenharmony_ci    ASSERT_TRUE(isBuffer);
12433eb0b6dSopenharmony_ci}
12533eb0b6dSopenharmony_ci
12633eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, IsBufferTest002, testing::ext::TestSize.Level1)
12733eb0b6dSopenharmony_ci{
12833eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
12933eb0b6dSopenharmony_ci
13033eb0b6dSopenharmony_ci    napi_value buffer = nullptr;
13133eb0b6dSopenharmony_ci    void* bufferPtr = nullptr;
13233eb0b6dSopenharmony_ci    size_t bufferSize = -1;
13333eb0b6dSopenharmony_ci    bool isBuffer = false;
13433eb0b6dSopenharmony_ci
13533eb0b6dSopenharmony_ci    napi_create_buffer(env, bufferSize, &bufferPtr, &buffer);
13633eb0b6dSopenharmony_ci
13733eb0b6dSopenharmony_ci    void* tmpBufferPtr = nullptr;
13833eb0b6dSopenharmony_ci    size_t bufferLength = 0;
13933eb0b6dSopenharmony_ci    napi_get_buffer_info(env, buffer, &tmpBufferPtr, &bufferLength);
14033eb0b6dSopenharmony_ci    napi_is_buffer(env, buffer, &isBuffer);
14133eb0b6dSopenharmony_ci
14233eb0b6dSopenharmony_ci    ASSERT_EQ(isBuffer, false);
14333eb0b6dSopenharmony_ci}
14433eb0b6dSopenharmony_ci
14533eb0b6dSopenharmony_ci/**
14633eb0b6dSopenharmony_ci * @tc.name: StringTestAce
14733eb0b6dSopenharmony_ci * @tc.desc: Test string type.
14833eb0b6dSopenharmony_ci * @tc.type: FUNC
14933eb0b6dSopenharmony_ci */
15033eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, StringTest001, testing::ext::TestSize.Level1)
15133eb0b6dSopenharmony_ci{
15233eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
15333eb0b6dSopenharmony_ci    const char16_t testStr[] = u"中文,English,123456,!@#$%$#^%&12345";
15433eb0b6dSopenharmony_ci    int testStrLength = static_cast<int>(std::char_traits<char16_t>::length(testStr));
15533eb0b6dSopenharmony_ci    napi_value result = nullptr;
15633eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_create_string_utf16(env, testStr, testStrLength, &result));
15733eb0b6dSopenharmony_ci    ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
15833eb0b6dSopenharmony_ci
15933eb0b6dSopenharmony_ci    char16_t* buffer = nullptr;
16033eb0b6dSopenharmony_ci    size_t bufferSize = 0;
16133eb0b6dSopenharmony_ci    size_t strLength = 0;
16233eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, nullptr, 0, &bufferSize));
16333eb0b6dSopenharmony_ci    ASSERT_GT(bufferSize, (size_t)0);
16433eb0b6dSopenharmony_ci    buffer = new char16_t[bufferSize + 1] { 0 };
16533eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, buffer, bufferSize + 1, &strLength));
16633eb0b6dSopenharmony_ci    for (int i = 0; i < testStrLength; i++) {
16733eb0b6dSopenharmony_ci        ASSERT_EQ(testStr[i], buffer[i]);
16833eb0b6dSopenharmony_ci    }
16933eb0b6dSopenharmony_ci    ASSERT_EQ(testStrLength, strLength);
17033eb0b6dSopenharmony_ci    delete[] buffer;
17133eb0b6dSopenharmony_ci    buffer = nullptr;
17233eb0b6dSopenharmony_ci    char16_t* bufferShort = nullptr;
17333eb0b6dSopenharmony_ci    int bufferShortSize = 3;
17433eb0b6dSopenharmony_ci    bufferShort = new char16_t[bufferShortSize] { 0 };
17533eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, bufferShort, bufferShortSize, &strLength));
17633eb0b6dSopenharmony_ci    for (int i = 0; i < bufferShortSize; i++) {
17733eb0b6dSopenharmony_ci        if (i == (bufferShortSize - 1)) {
17833eb0b6dSopenharmony_ci            ASSERT_EQ(0, bufferShort[i]);
17933eb0b6dSopenharmony_ci        } else {
18033eb0b6dSopenharmony_ci            ASSERT_EQ(testStr[i], bufferShort[i]);
18133eb0b6dSopenharmony_ci        }
18233eb0b6dSopenharmony_ci    }
18333eb0b6dSopenharmony_ci    ASSERT_EQ(testStrLength, strLength);
18433eb0b6dSopenharmony_ci    delete[] bufferShort;
18533eb0b6dSopenharmony_ci    bufferShort = nullptr;
18633eb0b6dSopenharmony_ci}
18733eb0b6dSopenharmony_ci
18833eb0b6dSopenharmony_ci#if  (defined(FOR_JERRYSCRIPT_TEST)) &&  (JERRY_API_MINOR_VERSION <= 3)
18933eb0b6dSopenharmony_ci    // jerryscript 2.3 do nothing
19033eb0b6dSopenharmony_ci#else
19133eb0b6dSopenharmony_ci// jerryscript 2.4 or quickjs or V8
19233eb0b6dSopenharmony_ci
19333eb0b6dSopenharmony_ci/**
19433eb0b6dSopenharmony_ci * @tc.name: BigIntTest
19533eb0b6dSopenharmony_ci * @tc.desc: Test number type.
19633eb0b6dSopenharmony_ci * @tc.type: FUNC
19733eb0b6dSopenharmony_ci */
19833eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, BigIntTest001, testing::ext::TestSize.Level1)
19933eb0b6dSopenharmony_ci{
20033eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
20133eb0b6dSopenharmony_ci    // uint64
20233eb0b6dSopenharmony_ci    {
20333eb0b6dSopenharmony_ci        uint64_t testValue = UINT64_MAX;
20433eb0b6dSopenharmony_ci        napi_value result = nullptr;
20533eb0b6dSopenharmony_ci        bool flag = false;
20633eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_create_bigint_uint64(env, testValue, &result));
20733eb0b6dSopenharmony_ci        ASSERT_CHECK_VALUE_TYPE(env, result, napi_bigint);
20833eb0b6dSopenharmony_ci
20933eb0b6dSopenharmony_ci        uint64_t resultValue = 0;
21033eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_get_value_bigint_uint64(env, result, &resultValue, &flag));
21133eb0b6dSopenharmony_ci        ASSERT_EQ(resultValue, UINT64_MAX);
21233eb0b6dSopenharmony_ci        ASSERT_TRUE(flag);
21333eb0b6dSopenharmony_ci    }
21433eb0b6dSopenharmony_ci    {
21533eb0b6dSopenharmony_ci        uint64_t testValue = 0xffffffffffffffff;
21633eb0b6dSopenharmony_ci        napi_value result = nullptr;
21733eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_create_bigint_uint64(env, testValue, &result));
21833eb0b6dSopenharmony_ci        ASSERT_CHECK_VALUE_TYPE(env, result, napi_bigint);
21933eb0b6dSopenharmony_ci        bool flag = false;
22033eb0b6dSopenharmony_ci        uint64_t resultValue = 0;
22133eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_get_value_bigint_uint64(env, result, &resultValue, &flag));
22233eb0b6dSopenharmony_ci        ASSERT_EQ(resultValue, testValue);
22333eb0b6dSopenharmony_ci        ASSERT_TRUE(flag);
22433eb0b6dSopenharmony_ci    }
22533eb0b6dSopenharmony_ci    {
22633eb0b6dSopenharmony_ci        uint64_t testValue = 9007199254740991;
22733eb0b6dSopenharmony_ci        napi_value result = nullptr;
22833eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_create_bigint_uint64(env, testValue, &result));
22933eb0b6dSopenharmony_ci        ASSERT_CHECK_VALUE_TYPE(env, result, napi_bigint);
23033eb0b6dSopenharmony_ci        bool flag = false;
23133eb0b6dSopenharmony_ci        uint64_t resultValue = 0;
23233eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_get_value_bigint_uint64(env, result, &resultValue, &flag));
23333eb0b6dSopenharmony_ci        ASSERT_EQ(resultValue, testValue);
23433eb0b6dSopenharmony_ci        ASSERT_TRUE(flag);
23533eb0b6dSopenharmony_ci    }
23633eb0b6dSopenharmony_ci}
23733eb0b6dSopenharmony_ci
23833eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, BigIntTest002, testing::ext::TestSize.Level1)
23933eb0b6dSopenharmony_ci{
24033eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
24133eb0b6dSopenharmony_ci    // int64
24233eb0b6dSopenharmony_ci    {
24333eb0b6dSopenharmony_ci        int64_t testValue = INT64_MAX;
24433eb0b6dSopenharmony_ci        napi_value result = nullptr;
24533eb0b6dSopenharmony_ci        bool flag = false;
24633eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_create_bigint_int64(env, testValue, &result));
24733eb0b6dSopenharmony_ci        ASSERT_CHECK_VALUE_TYPE(env, result, napi_bigint);
24833eb0b6dSopenharmony_ci
24933eb0b6dSopenharmony_ci        int64_t resultValue = 0;
25033eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_get_value_bigint_int64(env, result, &resultValue, &flag));
25133eb0b6dSopenharmony_ci        ASSERT_EQ(resultValue, INT64_MAX);
25233eb0b6dSopenharmony_ci        ASSERT_TRUE(flag);
25333eb0b6dSopenharmony_ci    }
25433eb0b6dSopenharmony_ci    {
25533eb0b6dSopenharmony_ci        int64_t testValue = 9007199254740991;
25633eb0b6dSopenharmony_ci        napi_value result = nullptr;
25733eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_create_bigint_int64(env, testValue, &result));
25833eb0b6dSopenharmony_ci        ASSERT_CHECK_VALUE_TYPE(env, result, napi_bigint);
25933eb0b6dSopenharmony_ci        bool flag = false;
26033eb0b6dSopenharmony_ci        int64_t resultValue = 0;
26133eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_get_value_bigint_int64(env, result, &resultValue, &flag));
26233eb0b6dSopenharmony_ci        ASSERT_EQ(resultValue, testValue);
26333eb0b6dSopenharmony_ci        ASSERT_TRUE(flag);
26433eb0b6dSopenharmony_ci    }
26533eb0b6dSopenharmony_ci    {
26633eb0b6dSopenharmony_ci        int64_t testValue = -1;
26733eb0b6dSopenharmony_ci        napi_value result = nullptr;
26833eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_create_bigint_int64(env, testValue, &result));
26933eb0b6dSopenharmony_ci        ASSERT_CHECK_VALUE_TYPE(env, result, napi_bigint);
27033eb0b6dSopenharmony_ci        bool flag = false;
27133eb0b6dSopenharmony_ci        int64_t resultValue = 0;
27233eb0b6dSopenharmony_ci        ASSERT_CHECK_CALL(napi_get_value_bigint_int64(env, result, &resultValue, &flag));
27333eb0b6dSopenharmony_ci        ASSERT_EQ(resultValue, testValue);
27433eb0b6dSopenharmony_ci        ASSERT_TRUE(flag);
27533eb0b6dSopenharmony_ci    }
27633eb0b6dSopenharmony_ci}
27733eb0b6dSopenharmony_ci
27833eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, BigIntWordsTest001, testing::ext::TestSize.Level1)
27933eb0b6dSopenharmony_ci{
28033eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
28133eb0b6dSopenharmony_ci    int signBit = 0;
28233eb0b6dSopenharmony_ci    size_t wordCount = 4;
28333eb0b6dSopenharmony_ci    uint64_t words[] = { 0xFFFFFFFFFFFFFFFF, 34ULL, 56ULL, 0xFFFFFFFFFFFFFFFF };
28433eb0b6dSopenharmony_ci    uint64_t wordsOut[] = { 0ULL, 0ULL, 0ULL, 0ULL };
28533eb0b6dSopenharmony_ci    napi_value result = nullptr;
28633eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_create_bigint_words(env, signBit, wordCount, words, &result));
28733eb0b6dSopenharmony_ci
28833eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_get_value_bigint_words(env, result, &signBit, &wordCount, wordsOut));
28933eb0b6dSopenharmony_ci
29033eb0b6dSopenharmony_ci    ASSERT_EQ(signBit, 0);
29133eb0b6dSopenharmony_ci    ASSERT_EQ(wordCount, 4);
29233eb0b6dSopenharmony_ci    ASSERT_EQ(words[0], wordsOut[0]);
29333eb0b6dSopenharmony_ci    ASSERT_EQ(words[1], wordsOut[1]);
29433eb0b6dSopenharmony_ci    ASSERT_EQ(words[2], wordsOut[2]);
29533eb0b6dSopenharmony_ci    ASSERT_EQ(words[3], wordsOut[3]);
29633eb0b6dSopenharmony_ci}
29733eb0b6dSopenharmony_ci
29833eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, BigIntWordsTest002, testing::ext::TestSize.Level1)
29933eb0b6dSopenharmony_ci{
30033eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
30133eb0b6dSopenharmony_ci    int signBit = 0;
30233eb0b6dSopenharmony_ci    size_t wordCount = 5;
30333eb0b6dSopenharmony_ci    uint64_t words[] = { 12ULL, 34ULL, 56ULL, 78ULL, 90ULL };
30433eb0b6dSopenharmony_ci    uint64_t wordsOut[] = { 0ULL, 0ULL, 0ULL, 0ULL, 0ULL };
30533eb0b6dSopenharmony_ci    napi_value result = nullptr;
30633eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_create_bigint_words(env, signBit, wordCount, words, &result));
30733eb0b6dSopenharmony_ci
30833eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_get_value_bigint_words(env, result, &signBit, &wordCount, wordsOut));
30933eb0b6dSopenharmony_ci
31033eb0b6dSopenharmony_ci    ASSERT_EQ(signBit, 0);
31133eb0b6dSopenharmony_ci    ASSERT_EQ(wordCount, 5);
31233eb0b6dSopenharmony_ci    for (size_t i = 0; i < wordCount; i++) {
31333eb0b6dSopenharmony_ci        ASSERT_EQ(words[i], wordsOut[i]);
31433eb0b6dSopenharmony_ci    }
31533eb0b6dSopenharmony_ci}
31633eb0b6dSopenharmony_ci
31733eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, BigIntWordsTest003, testing::ext::TestSize.Level1)
31833eb0b6dSopenharmony_ci{
31933eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
32033eb0b6dSopenharmony_ci    int signBit = 1;
32133eb0b6dSopenharmony_ci    size_t wordCount = 4;
32233eb0b6dSopenharmony_ci    uint64_t words[] = { 0xFFFFFFFFFFFFFFFF, 34ULL, 56ULL, 0xFFFFFFFFFFFFFFFF };
32333eb0b6dSopenharmony_ci    uint64_t wordsOut[] = { 0ULL, 0ULL, 0ULL, 0ULL };
32433eb0b6dSopenharmony_ci    napi_value result = nullptr;
32533eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_create_bigint_words(env, signBit, wordCount, words, &result));
32633eb0b6dSopenharmony_ci
32733eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_get_value_bigint_words(env, result, &signBit, &wordCount, wordsOut));
32833eb0b6dSopenharmony_ci
32933eb0b6dSopenharmony_ci    ASSERT_EQ(signBit, 1);
33033eb0b6dSopenharmony_ci    ASSERT_EQ(wordCount, 4);
33133eb0b6dSopenharmony_ci    ASSERT_EQ(words[0], wordsOut[0]);
33233eb0b6dSopenharmony_ci    ASSERT_EQ(words[1], wordsOut[1]);
33333eb0b6dSopenharmony_ci    ASSERT_EQ(words[2], wordsOut[2]);
33433eb0b6dSopenharmony_ci    ASSERT_EQ(words[3], wordsOut[3]);
33533eb0b6dSopenharmony_ci}
33633eb0b6dSopenharmony_ci
33733eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, BigIntWordsTest004, testing::ext::TestSize.Level1)
33833eb0b6dSopenharmony_ci{
33933eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
34033eb0b6dSopenharmony_ci    int signBit = 1;
34133eb0b6dSopenharmony_ci    size_t wordCount = 5;
34233eb0b6dSopenharmony_ci    uint64_t words[] = { 12ULL, 34ULL, 56ULL, 78ULL, 0x000000FF98765432 };
34333eb0b6dSopenharmony_ci    uint64_t wordsOut[] = { 0ULL, 0ULL, 0ULL, 0ULL, 0ULL };
34433eb0b6dSopenharmony_ci    napi_value result = nullptr;
34533eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_create_bigint_words(env, signBit, wordCount, words, &result));
34633eb0b6dSopenharmony_ci
34733eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_get_value_bigint_words(env, result, &signBit, &wordCount, wordsOut));
34833eb0b6dSopenharmony_ci
34933eb0b6dSopenharmony_ci    ASSERT_EQ(signBit, 1);
35033eb0b6dSopenharmony_ci    ASSERT_EQ(wordCount, 5);
35133eb0b6dSopenharmony_ci    for (size_t i = 0; i < wordCount; i++) {
35233eb0b6dSopenharmony_ci        ASSERT_EQ(words[i], wordsOut[i]);
35333eb0b6dSopenharmony_ci    }
35433eb0b6dSopenharmony_ci}
35533eb0b6dSopenharmony_ci
35633eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, TagObjectTest001, testing::ext::TestSize.Level1)
35733eb0b6dSopenharmony_ci{
35833eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
35933eb0b6dSopenharmony_ci    napi_value object = nullptr;
36033eb0b6dSopenharmony_ci    const napi_type_tag typeTag = { 0xFFFFFFFFFFFFFFFF, 34ULL };
36133eb0b6dSopenharmony_ci
36233eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_create_object(env, &object));
36333eb0b6dSopenharmony_ci
36433eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_type_tag_object(env, object, &typeTag));
36533eb0b6dSopenharmony_ci
36633eb0b6dSopenharmony_ci    bool checkResult = false;
36733eb0b6dSopenharmony_ci
36833eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_check_object_type_tag(env, object, &typeTag, &checkResult));
36933eb0b6dSopenharmony_ci    ASSERT_TRUE(checkResult);
37033eb0b6dSopenharmony_ci}
37133eb0b6dSopenharmony_ci#endif
37233eb0b6dSopenharmony_ci
37333eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, GetDateTest001, testing::ext::TestSize.Level1)
37433eb0b6dSopenharmony_ci{
37533eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
37633eb0b6dSopenharmony_ci    napi_value createResult = nullptr;
37733eb0b6dSopenharmony_ci    double time = 202110181203150;
37833eb0b6dSopenharmony_ci
37933eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_create_date(env, time, &createResult));
38033eb0b6dSopenharmony_ci
38133eb0b6dSopenharmony_ci    double getTime = false;
38233eb0b6dSopenharmony_ci
38333eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_get_date_value(env, createResult, &getTime));
38433eb0b6dSopenharmony_ci    bool result = false;
38533eb0b6dSopenharmony_ci    if (time == getTime) {
38633eb0b6dSopenharmony_ci        result = true;
38733eb0b6dSopenharmony_ci    }
38833eb0b6dSopenharmony_ci    ASSERT_TRUE(result);
38933eb0b6dSopenharmony_ci}
39033eb0b6dSopenharmony_ci
39133eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, IsDateTest001, testing::ext::TestSize.Level1)
39233eb0b6dSopenharmony_ci{
39333eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
39433eb0b6dSopenharmony_ci    napi_value createResult = nullptr;
39533eb0b6dSopenharmony_ci    double time = 202110181203150;
39633eb0b6dSopenharmony_ci
39733eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_create_date(env, time, &createResult));
39833eb0b6dSopenharmony_ci
39933eb0b6dSopenharmony_ci    bool result = false;
40033eb0b6dSopenharmony_ci
40133eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_is_date(env, createResult, &result));
40233eb0b6dSopenharmony_ci
40333eb0b6dSopenharmony_ci    ASSERT_TRUE(result);
40433eb0b6dSopenharmony_ci}
40533eb0b6dSopenharmony_ci
40633eb0b6dSopenharmony_ci/**
40733eb0b6dSopenharmony_ci * @tc.name: ACE_napi_adjust_external_memory_test.
40833eb0b6dSopenharmony_ci * @tc.desc: Test napi_adjust_external_memory.
40933eb0b6dSopenharmony_ci * @tc.type: FUNC
41033eb0b6dSopenharmony_ci */
41133eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, AdjustExternalMemoryTest001, testing::ext::TestSize.Level1)
41233eb0b6dSopenharmony_ci{
41333eb0b6dSopenharmony_ci    HILOG_INFO("%{public}s", "ACE_napi_adjust_external_memory_test start");
41433eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
41533eb0b6dSopenharmony_ci    int64_t changeInBytes = 32;
41633eb0b6dSopenharmony_ci    int64_t adjustedValue = 32;
41733eb0b6dSopenharmony_ci    napi_status ret = napi_adjust_external_memory(env, changeInBytes, &adjustedValue);
41833eb0b6dSopenharmony_ci    ASSERT_EQ(ret, napi_ok);
41933eb0b6dSopenharmony_ci    HILOG_INFO("%{public}s", "ACE_napi_adjust_external_memory_test end");
42033eb0b6dSopenharmony_ci}
42133eb0b6dSopenharmony_ci
42233eb0b6dSopenharmony_ci/**
42333eb0b6dSopenharmony_ci * @tc.name: ACE_napi_async_init_Test.
42433eb0b6dSopenharmony_ci * @tc.desc: Test napi_async_init, napi_async_destroy.
42533eb0b6dSopenharmony_ci * @tc.type: FUNC
42633eb0b6dSopenharmony_ci */
42733eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, AsyncInitTest001, testing::ext::TestSize.Level1)
42833eb0b6dSopenharmony_ci{
42933eb0b6dSopenharmony_ci    HILOG_INFO("ACE_napi_async_init_Test_001 start");
43033eb0b6dSopenharmony_ci
43133eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
43233eb0b6dSopenharmony_ci
43333eb0b6dSopenharmony_ci    napi_value resourceName;
43433eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, "ACE_napi_async_init_Test_001",
43533eb0b6dSopenharmony_ci        NAPI_AUTO_LENGTH, &resourceName));
43633eb0b6dSopenharmony_ci
43733eb0b6dSopenharmony_ci    napi_async_context context = nullptr;
43833eb0b6dSopenharmony_ci    napi_status ret = napi_async_init(env, nullptr, resourceName, &context);
43933eb0b6dSopenharmony_ci    ASSERT_EQ(ret, napi_ok);
44033eb0b6dSopenharmony_ci    EXPECT_NE(context, nullptr);
44133eb0b6dSopenharmony_ci
44233eb0b6dSopenharmony_ci    ret = napi_async_destroy(env, context);
44333eb0b6dSopenharmony_ci    ASSERT_EQ(ret, napi_ok);
44433eb0b6dSopenharmony_ci
44533eb0b6dSopenharmony_ci    HILOG_INFO("ACE_napi_async_init_Test_001 end");
44633eb0b6dSopenharmony_ci}
44733eb0b6dSopenharmony_ci
44833eb0b6dSopenharmony_ci/**
44933eb0b6dSopenharmony_ci * @tc.name: ACE_napi_open_callback_scope_Test
45033eb0b6dSopenharmony_ci * @tc.desc: Test napi_open_callback_scope, napi_close_callback_scope.
45133eb0b6dSopenharmony_ci * @tc.type: FUNC
45233eb0b6dSopenharmony_ci */
45333eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, OpenCallbackScopeTest001, testing::ext::TestSize.Level1)
45433eb0b6dSopenharmony_ci{
45533eb0b6dSopenharmony_ci    HILOG_INFO("ACE_napi_open_callback_scope_Test_001 start");
45633eb0b6dSopenharmony_ci
45733eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
45833eb0b6dSopenharmony_ci
45933eb0b6dSopenharmony_ci    auto callbackScopeManager = engine_->GetCallbackScopeManager();
46033eb0b6dSopenharmony_ci    ASSERT_NE(callbackScopeManager, nullptr);
46133eb0b6dSopenharmony_ci
46233eb0b6dSopenharmony_ci    int openCallbackScopesBefore = callbackScopeManager->GetOpenCallbackScopes();
46333eb0b6dSopenharmony_ci    int asyncCallbackScopeDepthBefore = callbackScopeManager->GetAsyncCallbackScopeDepth();
46433eb0b6dSopenharmony_ci
46533eb0b6dSopenharmony_ci    napi_value resourceName;
46633eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, "test", NAPI_AUTO_LENGTH, &resourceName));
46733eb0b6dSopenharmony_ci
46833eb0b6dSopenharmony_ci    napi_async_context context;
46933eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_async_init(env, nullptr, resourceName, &context));
47033eb0b6dSopenharmony_ci
47133eb0b6dSopenharmony_ci    napi_callback_scope scope = nullptr;
47233eb0b6dSopenharmony_ci    napi_status ret = napi_open_callback_scope(env, NULL, context, &scope);
47333eb0b6dSopenharmony_ci    EXPECT_EQ(ret, napi_ok);
47433eb0b6dSopenharmony_ci    EXPECT_NE(scope, nullptr);
47533eb0b6dSopenharmony_ci
47633eb0b6dSopenharmony_ci    int openCallbackScopes = callbackScopeManager->GetOpenCallbackScopes();
47733eb0b6dSopenharmony_ci    int asyncCallbackScopeDepth = callbackScopeManager->GetAsyncCallbackScopeDepth();
47833eb0b6dSopenharmony_ci    EXPECT_EQ(openCallbackScopes, (openCallbackScopesBefore + 1));
47933eb0b6dSopenharmony_ci    EXPECT_EQ(asyncCallbackScopeDepth, (asyncCallbackScopeDepthBefore + 1));
48033eb0b6dSopenharmony_ci
48133eb0b6dSopenharmony_ci    ret = napi_close_callback_scope(env, scope);
48233eb0b6dSopenharmony_ci    EXPECT_EQ(ret, napi_ok);
48333eb0b6dSopenharmony_ci
48433eb0b6dSopenharmony_ci    int openCallbackScopesAfter = callbackScopeManager->GetOpenCallbackScopes();
48533eb0b6dSopenharmony_ci    int asyncCallbackScopeDepthAfter = callbackScopeManager->GetAsyncCallbackScopeDepth();
48633eb0b6dSopenharmony_ci    EXPECT_EQ(openCallbackScopesAfter, openCallbackScopesBefore);
48733eb0b6dSopenharmony_ci    EXPECT_EQ(asyncCallbackScopeDepthAfter, asyncCallbackScopeDepthBefore);
48833eb0b6dSopenharmony_ci
48933eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_async_destroy(env, context));
49033eb0b6dSopenharmony_ci
49133eb0b6dSopenharmony_ci    HILOG_INFO("ACE_napi_open_callback_scope_Test_001 end");
49233eb0b6dSopenharmony_ci}
49333eb0b6dSopenharmony_ci
49433eb0b6dSopenharmony_ci/**
49533eb0b6dSopenharmony_ci * @tc.name: ACE_napi_open_callback_scope_Test
49633eb0b6dSopenharmony_ci * @tc.desc: Test napi_open_callback_scope, napi_close_callback_scope.
49733eb0b6dSopenharmony_ci * @tc.type: FUNC
49833eb0b6dSopenharmony_ci */
49933eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, OpenCallbackScopeTest002, testing::ext::TestSize.Level1)
50033eb0b6dSopenharmony_ci{
50133eb0b6dSopenharmony_ci    HILOG_INFO("ACE_napi_open_callback_scope_Test_002 start");
50233eb0b6dSopenharmony_ci
50333eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
50433eb0b6dSopenharmony_ci
50533eb0b6dSopenharmony_ci    auto callbackScopeManager = engine_->GetCallbackScopeManager();
50633eb0b6dSopenharmony_ci    ASSERT_NE(callbackScopeManager, nullptr);
50733eb0b6dSopenharmony_ci
50833eb0b6dSopenharmony_ci    int openCallbackScopesBefore = callbackScopeManager->GetOpenCallbackScopes();
50933eb0b6dSopenharmony_ci    int asyncCallbackScopeDepthBefore = callbackScopeManager->GetAsyncCallbackScopeDepth();
51033eb0b6dSopenharmony_ci
51133eb0b6dSopenharmony_ci    napi_value resourceName;
51233eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, "test", NAPI_AUTO_LENGTH, &resourceName));
51333eb0b6dSopenharmony_ci
51433eb0b6dSopenharmony_ci    napi_async_context context;
51533eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_async_init(env, nullptr, resourceName, &context));
51633eb0b6dSopenharmony_ci
51733eb0b6dSopenharmony_ci    napi_callback_scope scope = nullptr;
51833eb0b6dSopenharmony_ci    napi_status ret = napi_open_callback_scope(env, NULL, context, &scope);
51933eb0b6dSopenharmony_ci    EXPECT_EQ(ret, napi_ok);
52033eb0b6dSopenharmony_ci    EXPECT_NE(scope, nullptr);
52133eb0b6dSopenharmony_ci
52233eb0b6dSopenharmony_ci    int openCallbackScopes1 = callbackScopeManager->GetOpenCallbackScopes();
52333eb0b6dSopenharmony_ci    int asyncCallbackScopeDepth1 = callbackScopeManager->GetAsyncCallbackScopeDepth();
52433eb0b6dSopenharmony_ci
52533eb0b6dSopenharmony_ci    // Open a internal callback scope
52633eb0b6dSopenharmony_ci    panda::Local<panda::ObjectRef> obj = panda::ObjectRef::New(env_->GetEcmaVm());
52733eb0b6dSopenharmony_ci    auto scope2 = callbackScopeManager->Open(engine_, obj, {0, 0});
52833eb0b6dSopenharmony_ci    int openCallbackScopes2 = callbackScopeManager->GetOpenCallbackScopes();
52933eb0b6dSopenharmony_ci    int asyncCallbackScopeDepth2 = callbackScopeManager->GetAsyncCallbackScopeDepth();
53033eb0b6dSopenharmony_ci
53133eb0b6dSopenharmony_ci    EXPECT_NE(scope2, nullptr);
53233eb0b6dSopenharmony_ci    EXPECT_EQ(openCallbackScopes2, openCallbackScopes1);
53333eb0b6dSopenharmony_ci    EXPECT_EQ(asyncCallbackScopeDepth2, (asyncCallbackScopeDepth1 + 1));
53433eb0b6dSopenharmony_ci
53533eb0b6dSopenharmony_ci    callbackScopeManager->Close(scope2);
53633eb0b6dSopenharmony_ci    obj->Delete(env_->GetEcmaVm(), obj);
53733eb0b6dSopenharmony_ci    int openCallbackScopes2After = callbackScopeManager->GetOpenCallbackScopes();
53833eb0b6dSopenharmony_ci    int asyncCallbackScopeDepth2After = callbackScopeManager->GetAsyncCallbackScopeDepth();
53933eb0b6dSopenharmony_ci
54033eb0b6dSopenharmony_ci    EXPECT_EQ(openCallbackScopes2After, openCallbackScopes1);
54133eb0b6dSopenharmony_ci    EXPECT_EQ(asyncCallbackScopeDepth2After, asyncCallbackScopeDepth1);
54233eb0b6dSopenharmony_ci
54333eb0b6dSopenharmony_ci    ret = napi_close_callback_scope(env, scope);
54433eb0b6dSopenharmony_ci    EXPECT_EQ(ret, napi_ok);
54533eb0b6dSopenharmony_ci
54633eb0b6dSopenharmony_ci    int openCallbackScopes1After = callbackScopeManager->GetOpenCallbackScopes();
54733eb0b6dSopenharmony_ci    int asyncCallbackScopeDepth1After = callbackScopeManager->GetAsyncCallbackScopeDepth();
54833eb0b6dSopenharmony_ci
54933eb0b6dSopenharmony_ci    EXPECT_EQ(openCallbackScopes1After, openCallbackScopesBefore);
55033eb0b6dSopenharmony_ci    EXPECT_EQ(asyncCallbackScopeDepth1After, asyncCallbackScopeDepthBefore);
55133eb0b6dSopenharmony_ci
55233eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_async_destroy(env, context));
55333eb0b6dSopenharmony_ci
55433eb0b6dSopenharmony_ci    HILOG_INFO("ACE_napi_open_callback_scope_Test_002 end");
55533eb0b6dSopenharmony_ci}
55633eb0b6dSopenharmony_ci
55733eb0b6dSopenharmony_cistatic napi_value TestFatalException(napi_env env, napi_callback_info info)
55833eb0b6dSopenharmony_ci{
55933eb0b6dSopenharmony_ci    napi_value err;
56033eb0b6dSopenharmony_ci    size_t argc = 1;
56133eb0b6dSopenharmony_ci
56233eb0b6dSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &err, nullptr, nullptr));
56333eb0b6dSopenharmony_ci    NAPI_CALL(env, napi_fatal_exception(env, err));
56433eb0b6dSopenharmony_ci    return nullptr;
56533eb0b6dSopenharmony_ci}
56633eb0b6dSopenharmony_ci
56733eb0b6dSopenharmony_ci/**
56833eb0b6dSopenharmony_ci * @tc.name: FatalException
56933eb0b6dSopenharmony_ci * @tc.desc: Test FatalException Func.
57033eb0b6dSopenharmony_ci * @tc.type: FUNC
57133eb0b6dSopenharmony_ci */
57233eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, FatalExceptionTest001, testing::ext::TestSize.Level1)
57333eb0b6dSopenharmony_ci{
57433eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
57533eb0b6dSopenharmony_ci    ASSERT_EQ(TestFatalException(env, nullptr), nullptr);
57633eb0b6dSopenharmony_ci}
57733eb0b6dSopenharmony_ci
57833eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, AddFinalizerTest001, testing::ext::TestSize.Level1)
57933eb0b6dSopenharmony_ci{
58033eb0b6dSopenharmony_ci    HILOG_INFO("add_finalizer_test_0100 start");
58133eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
58233eb0b6dSopenharmony_ci
58333eb0b6dSopenharmony_ci    napi_value object;
58433eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &object));
58533eb0b6dSopenharmony_ci
58633eb0b6dSopenharmony_ci    static bool testValue = false;
58733eb0b6dSopenharmony_ci    const char* testStr = "test";
58833eb0b6dSopenharmony_ci    napi_ref ref = nullptr;
58933eb0b6dSopenharmony_ci    napi_add_finalizer(
59033eb0b6dSopenharmony_ci        env, object, (void*)testStr, [](napi_env env, void* data, void* hint) {
59133eb0b6dSopenharmony_ci            testValue = true;
59233eb0b6dSopenharmony_ci        }, nullptr, &ref);
59333eb0b6dSopenharmony_ci
59433eb0b6dSopenharmony_ci    napi_delete_reference(env, ref);
59533eb0b6dSopenharmony_ci    ASSERT_TRUE(testValue);
59633eb0b6dSopenharmony_ci    HILOG_INFO("add_finalizer_test_0100 end");
59733eb0b6dSopenharmony_ci}
59833eb0b6dSopenharmony_ci
59933eb0b6dSopenharmony_citypedef struct {
60033eb0b6dSopenharmony_ci    size_t value;
60133eb0b6dSopenharmony_ci    bool print;
60233eb0b6dSopenharmony_ci    napi_ref js_cb_ref;
60333eb0b6dSopenharmony_ci} AddonData;
60433eb0b6dSopenharmony_ci
60533eb0b6dSopenharmony_cistatic void DeleteAddonData(napi_env env, void* raw_data, void* hint)
60633eb0b6dSopenharmony_ci{
60733eb0b6dSopenharmony_ci    AddonData* data = (AddonData*)raw_data;
60833eb0b6dSopenharmony_ci    if (data->print) {
60933eb0b6dSopenharmony_ci        printf("deleting addon data\n");
61033eb0b6dSopenharmony_ci    }
61133eb0b6dSopenharmony_ci    if (data->js_cb_ref != NULL) {
61233eb0b6dSopenharmony_ci        NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, data->js_cb_ref));
61333eb0b6dSopenharmony_ci    }
61433eb0b6dSopenharmony_ci    free(data);
61533eb0b6dSopenharmony_ci}
61633eb0b6dSopenharmony_ci
61733eb0b6dSopenharmony_cistatic napi_value SetPrintOnDelete(napi_env env, napi_callback_info info)
61833eb0b6dSopenharmony_ci{
61933eb0b6dSopenharmony_ci    AddonData* data;
62033eb0b6dSopenharmony_ci    NAPI_CALL(env, napi_get_instance_data(env, (void**)&data));
62133eb0b6dSopenharmony_ci    data->print = true;
62233eb0b6dSopenharmony_ci    return NULL;
62333eb0b6dSopenharmony_ci}
62433eb0b6dSopenharmony_ci
62533eb0b6dSopenharmony_cistatic void TestFinalizer(napi_env env, void* raw_data, void* hint)
62633eb0b6dSopenharmony_ci{
62733eb0b6dSopenharmony_ci    (void)raw_data;
62833eb0b6dSopenharmony_ci    (void)hint;
62933eb0b6dSopenharmony_ci
63033eb0b6dSopenharmony_ci    AddonData* data;
63133eb0b6dSopenharmony_ci    napi_value jsResult;
63233eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_get_instance_data(env, (void**)&data));
63333eb0b6dSopenharmony_ci    napi_value js_cb, undefined;
63433eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, data->js_cb_ref, &js_cb));
63533eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined));
63633eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, js_cb, 0, NULL, &jsResult));
63733eb0b6dSopenharmony_ci
63833eb0b6dSopenharmony_ci    NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, data->js_cb_ref));
63933eb0b6dSopenharmony_ci    data->js_cb_ref = NULL;
64033eb0b6dSopenharmony_ci}
64133eb0b6dSopenharmony_ci
64233eb0b6dSopenharmony_cistatic napi_value ObjectWithFinalizer(napi_env env, napi_callback_info info)
64333eb0b6dSopenharmony_ci{
64433eb0b6dSopenharmony_ci    HILOG_INFO("%{public}s", "start.");
64533eb0b6dSopenharmony_ci    AddonData* data;
64633eb0b6dSopenharmony_ci
64733eb0b6dSopenharmony_ci    napi_value result, js_cb;
64833eb0b6dSopenharmony_ci    size_t argc = 1;
64933eb0b6dSopenharmony_ci
65033eb0b6dSopenharmony_ci    auto func = [](napi_env env, napi_callback_info info) -> napi_value {
65133eb0b6dSopenharmony_ci        HILOG_INFO("%{public}s", "function called");
65233eb0b6dSopenharmony_ci        return nullptr;
65333eb0b6dSopenharmony_ci    };
65433eb0b6dSopenharmony_ci
65533eb0b6dSopenharmony_ci    napi_create_function(env, "testFunc", NAPI_AUTO_LENGTH, func, nullptr, &js_cb);
65633eb0b6dSopenharmony_ci
65733eb0b6dSopenharmony_ci    NAPI_CALL(env, napi_get_instance_data(env, (void**)&data));
65833eb0b6dSopenharmony_ci    NAPI_ASSERT(env, data->js_cb_ref == NULL, "reference must be NULL");
65933eb0b6dSopenharmony_ci    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &js_cb, NULL, NULL));
66033eb0b6dSopenharmony_ci    NAPI_CALL(env, napi_create_object(env, &result));
66133eb0b6dSopenharmony_ci    NAPI_CALL(env, napi_add_finalizer(env, result, NULL, TestFinalizer, NULL, NULL));
66233eb0b6dSopenharmony_ci    NAPI_CALL(env, napi_create_reference(env, js_cb, 1, &data->js_cb_ref));
66333eb0b6dSopenharmony_ci    HILOG_INFO("%{public}s", "end.");
66433eb0b6dSopenharmony_ci    return nullptr;
66533eb0b6dSopenharmony_ci}
66633eb0b6dSopenharmony_ci
66733eb0b6dSopenharmony_ciHWTEST_F(NapiExtTest, InstanceDataTest_001, testing::ext::TestSize.Level1)
66833eb0b6dSopenharmony_ci{
66933eb0b6dSopenharmony_ci    napi_env env = (napi_env)engine_;
67033eb0b6dSopenharmony_ci    // Set instance data
67133eb0b6dSopenharmony_ci    AddonData* data = (AddonData*)malloc(sizeof(*data));
67233eb0b6dSopenharmony_ci    data->value = 41;
67333eb0b6dSopenharmony_ci    data->print = false;
67433eb0b6dSopenharmony_ci    data->js_cb_ref = NULL;
67533eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_set_instance_data(env, data, DeleteAddonData, NULL));
67633eb0b6dSopenharmony_ci
67733eb0b6dSopenharmony_ci    // Test get instance data
67833eb0b6dSopenharmony_ci    AddonData* get_data = nullptr;
67933eb0b6dSopenharmony_ci    ASSERT_CHECK_CALL(napi_get_instance_data(env, (void**)&get_data));
68033eb0b6dSopenharmony_ci    ++get_data->value;
68133eb0b6dSopenharmony_ci    const size_t expectValue = 42;
68233eb0b6dSopenharmony_ci    ASSERT_EQ(get_data->value, expectValue);
68333eb0b6dSopenharmony_ci
68433eb0b6dSopenharmony_ci    // Test finalizer
68533eb0b6dSopenharmony_ci    SetPrintOnDelete(env, nullptr);
68633eb0b6dSopenharmony_ci    ObjectWithFinalizer(env, nullptr);
68733eb0b6dSopenharmony_ci}
688