14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include <cstddef>
174514f5e3Sopenharmony_ci#include <ctime>
184514f5e3Sopenharmony_ci#include <sys/time.h>
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_ci#include "gtest/gtest.h"
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_ci#include "assembler/assembly-parser.h"
234514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins.h"
244514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_function.h"
254514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_locale.h"
264514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/an_file_data_manager.h"
274514f5e3Sopenharmony_ci#include "ecmascript/compiler/aot_file/aot_file_manager.h"
284514f5e3Sopenharmony_ci#include "ecmascript/ecma_global_storage.h"
294514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
304514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
314514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_deque.h"
324514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_queue.h"
334514f5e3Sopenharmony_ci#include "ecmascript/js_bigint.h"
344514f5e3Sopenharmony_ci#include "ecmascript/js_generator_object.h"
354514f5e3Sopenharmony_ci#include "ecmascript/js_list_format.h"
364514f5e3Sopenharmony_ci#include "ecmascript/js_runtime_options.h"
374514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h"
384514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/js_pandafile_manager.h"
394514f5e3Sopenharmony_ci#include "ecmascript/napi/include/jsnapi.h"
404514f5e3Sopenharmony_ci#include "ecmascript/napi/jsnapi_helper.h"
414514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
424514f5e3Sopenharmony_ci#include "ecmascript/tagged_array.h"
434514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ciusing namespace panda;
464514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
474514f5e3Sopenharmony_ciusing namespace panda::pandasm;
484514f5e3Sopenharmony_ciusing namespace panda::panda_file;
494514f5e3Sopenharmony_ci
504514f5e3Sopenharmony_cinamespace panda::test {
514514f5e3Sopenharmony_ciclass JSNApiSampleTest : public testing::Test {
524514f5e3Sopenharmony_cipublic:
534514f5e3Sopenharmony_ci    void SetUp() override
544514f5e3Sopenharmony_ci    {
554514f5e3Sopenharmony_ci        RuntimeOption option;
564514f5e3Sopenharmony_ci        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
574514f5e3Sopenharmony_ci        vm_ = JSNApi::CreateJSVM(option);
584514f5e3Sopenharmony_ci        thread_ = vm_->GetJSThread();
594514f5e3Sopenharmony_ci        vm_->SetEnableForceGC(true);
604514f5e3Sopenharmony_ci    }
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ci    void TearDown() override
634514f5e3Sopenharmony_ci    {
644514f5e3Sopenharmony_ci        vm_->SetEnableForceGC(false);
654514f5e3Sopenharmony_ci        JSNApi::DestroyJSVM(vm_);
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ciprotected:
694514f5e3Sopenharmony_ci    JSThread *thread_ = nullptr;
704514f5e3Sopenharmony_ci    EcmaVM *vm_ = nullptr;
714514f5e3Sopenharmony_ci};
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_ci/* demo1 原始类型的使用。 */
744514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, Sample_PrimitiveRef_IntegerRef)
754514f5e3Sopenharmony_ci{
764514f5e3Sopenharmony_ci    LocalScope scope(vm_);
774514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_IntegerRef_int=============================================================";
784514f5e3Sopenharmony_ci    int inputInt = 123; // int value = 123
794514f5e3Sopenharmony_ci    Local<IntegerRef> intObject = IntegerRef::New(vm_, inputInt);
804514f5e3Sopenharmony_ci    int intValue = intObject->Value();
814514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_IntegerRef_intValue : " << intValue;
824514f5e3Sopenharmony_ci    Local<PrimitiveRef> pintObject = intObject;
834514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_IntegerRef_pintObject_IsInt : " << pintObject->IsInt();
844514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_IntegerRef_uint============================================================";
854514f5e3Sopenharmony_ci    uint inputUint = 123; // uint value = 123
864514f5e3Sopenharmony_ci    Local<IntegerRef> uintObject = IntegerRef::NewFromUnsigned(vm_, inputUint);
874514f5e3Sopenharmony_ci    int uintValue = uintObject->Value();
884514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_uintValue : " << uintValue;
894514f5e3Sopenharmony_ci}
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, Sample_PrimitiveRef_NumberRef)
924514f5e3Sopenharmony_ci{
934514f5e3Sopenharmony_ci    LocalScope scope(vm_);
944514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_double===========================================================";
954514f5e3Sopenharmony_ci    double inputDouble = 3.14; // double value = 3.14
964514f5e3Sopenharmony_ci    Local<NumberRef> doubleObject = NumberRef::New(vm_, inputDouble);
974514f5e3Sopenharmony_ci    double doubleValue = doubleObject->Value();
984514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_doubleValue : " << doubleValue;
994514f5e3Sopenharmony_ci    Local<PrimitiveRef> pdoubleObject = doubleObject;
1004514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_pdoubleObject_IsNumber : " << pdoubleObject->IsNumber();
1014514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_int32_t==========================================================";
1024514f5e3Sopenharmony_ci    int32_t inputInt32t = 666; // int32-t value = 666
1034514f5e3Sopenharmony_ci    Local<NumberRef> int32tObject = NumberRef::New(vm_, inputInt32t);
1044514f5e3Sopenharmony_ci    double int32tValue = int32tObject->Value();
1054514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_int32tValue : " << int32tValue;
1064514f5e3Sopenharmony_ci    Local<PrimitiveRef> pint32tObject = int32tObject;
1074514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_pint32tObject_Int32Value : " << pint32tObject->Int32Value(vm_);
1084514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_uint32_t=========================================================";
1094514f5e3Sopenharmony_ci    uint32_t inputUint32t = 666; // uint32_t value = 666
1104514f5e3Sopenharmony_ci    Local<NumberRef> uint32tObject = NumberRef::New(vm_, inputUint32t);
1114514f5e3Sopenharmony_ci    double uint32tValue = uint32tObject->Value();
1124514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_uint32tValue : " << uint32tValue;
1134514f5e3Sopenharmony_ci    Local<PrimitiveRef> puint32tObject = uint32tObject;
1144514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_puint32tObject_Uint32Value : " << puint32tObject->Uint32Value(vm_);
1154514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_int64_t==========================================================";
1164514f5e3Sopenharmony_ci    int64_t inputInt64t = 666; // int64_t value = 666
1174514f5e3Sopenharmony_ci    Local<NumberRef> int64tObject = NumberRef::New(vm_, inputInt64t);
1184514f5e3Sopenharmony_ci    double int64tValue = int64tObject->Value();
1194514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_NumberRef_int64tValue : " << int64tValue;
1204514f5e3Sopenharmony_ci}
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, Sample_PrimitiveRef_BigIntRef_Uint64)
1234514f5e3Sopenharmony_ci{
1244514f5e3Sopenharmony_ci    LocalScope scope(vm_);
1254514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_uint64_t=========================================================";
1264514f5e3Sopenharmony_ci    uint64_t inputUint64t = 999; // uint64_t value = 999
1274514f5e3Sopenharmony_ci    Local<BigIntRef> uint64tObject = BigIntRef::New(vm_, inputUint64t);
1284514f5e3Sopenharmony_ci    int64_t ucValue = 0;
1294514f5e3Sopenharmony_ci    bool uLossless = true;
1304514f5e3Sopenharmony_ci    uint64_t ucValue1 = 0;
1314514f5e3Sopenharmony_ci    uint64tObject->BigIntToInt64(vm_, &ucValue, &uLossless);
1324514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_BigIntToInt64 : " << ucValue;
1334514f5e3Sopenharmony_ci    uint64tObject->BigIntToUint64(vm_, &ucValue1, &uLossless);
1344514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_BigIntToUint64 : " << ucValue1;
1354514f5e3Sopenharmony_ci    bool uSign = false;
1364514f5e3Sopenharmony_ci    uint32_t uSize = 3;
1374514f5e3Sopenharmony_ci    int uarraySize = 3;
1384514f5e3Sopenharmony_ci    const uint64_t uWords[3] = {666, 777, 888};
1394514f5e3Sopenharmony_ci    Local<JSValueRef> ubigWords = BigIntRef::CreateBigWords(vm_, uSign, uSize, uWords);
1404514f5e3Sopenharmony_ci    for (int i = 0; i < uarraySize; i++) {
1414514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_UwordsArrayindex"
1424514f5e3Sopenharmony_ci                         << " [ " << i << " ] : " << uWords[i];
1434514f5e3Sopenharmony_ci    }
1444514f5e3Sopenharmony_ci    Local<BigIntRef> ubigWordsRef(ubigWords);
1454514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_UArraySize : " << ubigWordsRef->GetWordsArraySize(vm_);
1464514f5e3Sopenharmony_ci    bool uresultSignBit = true;
1474514f5e3Sopenharmony_ci    int unewarraySize = 3;
1484514f5e3Sopenharmony_ci    uint64_t *uresultWords = new uint64_t[unewarraySize]();
1494514f5e3Sopenharmony_ci    ubigWordsRef->GetWordsArray(vm_, &uresultSignBit, uSize, uresultWords);
1504514f5e3Sopenharmony_ci    for (int i = 0; i < unewarraySize; i++) {
1514514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_uresultWords"
1524514f5e3Sopenharmony_ci                         << " [ " << i << " ] : " << uresultWords[i];
1534514f5e3Sopenharmony_ci    }
1544514f5e3Sopenharmony_ci}
1554514f5e3Sopenharmony_ci
1564514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, Sample_PrimitiveRef_BigIntRef_Int64)
1574514f5e3Sopenharmony_ci{
1584514f5e3Sopenharmony_ci    LocalScope scope(vm_);
1594514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_int64_t==========================================================";
1604514f5e3Sopenharmony_ci    int64_t inputInt64t = 111; // int64_t value = 111
1614514f5e3Sopenharmony_ci    Local<BigIntRef> int64tObject = BigIntRef::New(vm_, inputInt64t);
1624514f5e3Sopenharmony_ci    int64_t icValue = 0;
1634514f5e3Sopenharmony_ci    bool iLossless = true;
1644514f5e3Sopenharmony_ci    uint64_t icValue1 = 0;
1654514f5e3Sopenharmony_ci    int64tObject->BigIntToInt64(vm_, &icValue, &iLossless);
1664514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_BigIntToInt64 : " << icValue;
1674514f5e3Sopenharmony_ci    int64tObject->BigIntToUint64(vm_, &icValue1, &iLossless);
1684514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_BigIntToUint64 : " << icValue1;
1694514f5e3Sopenharmony_ci    bool iSign = false;
1704514f5e3Sopenharmony_ci    uint32_t iSize = 3;
1714514f5e3Sopenharmony_ci    int iarraySize = 3;
1724514f5e3Sopenharmony_ci    const uint64_t iWords[3] = {222, 333, 444};
1734514f5e3Sopenharmony_ci    Local<JSValueRef> ibigWords = BigIntRef::CreateBigWords(vm_, iSign, iSize, iWords);
1744514f5e3Sopenharmony_ci    for (int i = 0; i < iarraySize; i++) {
1754514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_iWordsArrayindex"
1764514f5e3Sopenharmony_ci                         << " [ " << i << " ] : " << iWords[i];
1774514f5e3Sopenharmony_ci    }
1784514f5e3Sopenharmony_ci    Local<BigIntRef> ibigWordsRef(ibigWords);
1794514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_iarraySize : " << ibigWordsRef->GetWordsArraySize(vm_);
1804514f5e3Sopenharmony_ci    bool iresultSignBit = true;
1814514f5e3Sopenharmony_ci    int inewarraySize = 3;
1824514f5e3Sopenharmony_ci    uint64_t *iresultWords = new uint64_t[inewarraySize]();
1834514f5e3Sopenharmony_ci    ibigWordsRef->GetWordsArray(vm_, &iresultSignBit, iSize, iresultWords);
1844514f5e3Sopenharmony_ci    for (int i = 0; i < inewarraySize; i++) {
1854514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_iresultWords"
1864514f5e3Sopenharmony_ci                         << " [ " << i << " ] : " << iresultWords[i];
1874514f5e3Sopenharmony_ci    }
1884514f5e3Sopenharmony_ci    Local<PrimitiveRef> pint64tObject = int64tObject;
1894514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BigIntRef_pint64tObject_IsBigInt : " << pint64tObject->IsBigInt(vm_);
1904514f5e3Sopenharmony_ci}
1914514f5e3Sopenharmony_ci
1924514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, Sample_PrimitiveRef_BooleanRef)
1934514f5e3Sopenharmony_ci{
1944514f5e3Sopenharmony_ci    LocalScope scope(vm_);
1954514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BooleanRef_true============================================================";
1964514f5e3Sopenharmony_ci    Local<BooleanRef> trueObject = BooleanRef::New(vm_, true);
1974514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BooleanRef_trueObject_Value : " << trueObject->Value();
1984514f5e3Sopenharmony_ci    Local<PrimitiveRef> ptrueObject = trueObject;
1994514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BooleanRef_ptrueObject_IsBoolean : " << ptrueObject->IsBoolean();
2004514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BooleanRef_false===========================================================";
2014514f5e3Sopenharmony_ci    Local<BooleanRef> falseObject = BooleanRef::New(vm_, false);
2024514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BooleanRef_falseObject_Value : " << falseObject->Value();
2034514f5e3Sopenharmony_ci    Local<PrimitiveRef> pfalseObject = falseObject;
2044514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_BooleanRef_pfalseObject_IsBoolean : " << pfalseObject->IsBoolean();
2054514f5e3Sopenharmony_ci}
2064514f5e3Sopenharmony_ci
2074514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, Sample_PrimitiveRef_StringRef_Char)
2084514f5e3Sopenharmony_ci{
2094514f5e3Sopenharmony_ci    LocalScope scope(vm_);
2104514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_char=============================================================";
2114514f5e3Sopenharmony_ci    char utf8[12] = "Hello world";
2124514f5e3Sopenharmony_ci    Local<JSValueRef> local = StringRef::NewFromUtf8(vm_, utf8);
2134514f5e3Sopenharmony_ci    JSValueRef *jsValue = (*local);
2144514f5e3Sopenharmony_ci    StringRef *charObject = StringRef::Cast(jsValue);
2154514f5e3Sopenharmony_ci    EXPECT_EQ(charObject->Utf8Length(vm_), 12);
2164514f5e3Sopenharmony_ci    char buffer[12];
2174514f5e3Sopenharmony_ci    EXPECT_EQ(charObject->WriteUtf8(vm_, buffer, 12), 12);
2184514f5e3Sopenharmony_ci    std::string res(buffer);
2194514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_charObject : " << res;
2204514f5e3Sopenharmony_ci    std::string charObjectStr = charObject->ToString(vm_);
2214514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_charObject_ToString : " << charObjectStr;
2224514f5e3Sopenharmony_ci    uint32_t charSize = charObject->Length(vm_);
2234514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_charObject_Length : " << charSize;
2244514f5e3Sopenharmony_ci    char cs[16] = {0};
2254514f5e3Sopenharmony_ci    uint32_t length = charObject->WriteLatin1(vm_, cs, 12);
2264514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_charObject_WriteLatin1 : " << length;
2274514f5e3Sopenharmony_ci    Local<StringRef> napiWrapperString = charObject->GetNapiWrapperString(vm_);
2284514f5e3Sopenharmony_ci    EXPECT_EQ(napiWrapperString->Utf8Length(vm_), 13);
2294514f5e3Sopenharmony_ci    char buffer1[12];
2304514f5e3Sopenharmony_ci    EXPECT_EQ(charObject->WriteUtf8(vm_, buffer1, 12), 12);
2314514f5e3Sopenharmony_ci    std::string res1(buffer1);
2324514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_charObject_GetNapiWrapperString : " << res1;
2334514f5e3Sopenharmony_ci    PrimitiveRef *pcharObject = charObject;
2344514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_pcharObject_IsString : " << pcharObject->IsString(vm_);
2354514f5e3Sopenharmony_ci}
2364514f5e3Sopenharmony_ci
2374514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, Sample_PrimitiveRef_StringRef_Char16)
2384514f5e3Sopenharmony_ci{
2394514f5e3Sopenharmony_ci    LocalScope scope(vm_);
2404514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_char16_t=========================================================";
2414514f5e3Sopenharmony_ci    const char16_t *utf16 = u"Hi,ABCDEF";
2424514f5e3Sopenharmony_ci    Local<JSValueRef> local = StringRef::NewFromUtf16(vm_, utf16);
2434514f5e3Sopenharmony_ci    JSValueRef *jsValue = (*local);
2444514f5e3Sopenharmony_ci    StringRef *char16tObject = StringRef::Cast(jsValue);
2454514f5e3Sopenharmony_ci    EXPECT_EQ(char16tObject->Utf8Length(vm_), 10);
2464514f5e3Sopenharmony_ci    char16_t buffer2[10];
2474514f5e3Sopenharmony_ci    EXPECT_EQ(char16tObject->WriteUtf16(vm_, buffer2, 9), 9);
2484514f5e3Sopenharmony_ci    std::string res2(buffer2, buffer2 + sizeof(buffer2) / sizeof(char16_t));
2494514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_char16tObject : " << res2 << std::endl;
2504514f5e3Sopenharmony_ci    std::string char16tObjectStr = char16tObject->ToString(vm_);
2514514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_char16tObject_ToString : " << char16tObjectStr;
2524514f5e3Sopenharmony_ci    uint32_t charSize = char16tObject->Length(vm_);
2534514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_char16tObject_Length : " << charSize;
2544514f5e3Sopenharmony_ci    char cs1[10] = {0};
2554514f5e3Sopenharmony_ci    uint32_t length = char16tObject->WriteLatin1(vm_, cs1, 10);
2564514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_char16tObject_WriteLatin1 : " << length;
2574514f5e3Sopenharmony_ci    Local<StringRef> napiWrapperString = char16tObject->GetNapiWrapperString(vm_);
2584514f5e3Sopenharmony_ci    EXPECT_EQ(napiWrapperString->Utf8Length(vm_), 13);
2594514f5e3Sopenharmony_ci    char16_t buffer3[10];
2604514f5e3Sopenharmony_ci    EXPECT_EQ(char16tObject->WriteUtf16(vm_, buffer3, 9), 9);
2614514f5e3Sopenharmony_ci    std::string res3(buffer3, buffer3 + sizeof(buffer3) / sizeof(char16_t));
2624514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_char16tObject : " << res3;
2634514f5e3Sopenharmony_ci    PrimitiveRef *pchar16tObject = char16tObject;
2644514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_StringRef_pchar16tObject_IsString : " << pchar16tObject->IsString(vm_);
2654514f5e3Sopenharmony_ci}
2664514f5e3Sopenharmony_ci
2674514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, Sample_PrimitiveRef_SymbolRef)
2684514f5e3Sopenharmony_ci{
2694514f5e3Sopenharmony_ci    LocalScope scope(vm_);
2704514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_SymbolRef==================================================================";
2714514f5e3Sopenharmony_ci    Local<SymbolRef> symbolObject = SymbolRef::New(vm_, StringRef::NewFromUtf8(vm_, "description"));
2724514f5e3Sopenharmony_ci    Local<StringRef> symbolDescription = symbolObject->GetDescription(vm_);
2734514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_symbol_description : " << symbolDescription->ToString(vm_);
2744514f5e3Sopenharmony_ci    Local<PrimitiveRef> psymbolObject = symbolObject;
2754514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_primitive_SymbolRef_Psym_IsSymbol : " << psymbolObject->IsSymbol(vm_);
2764514f5e3Sopenharmony_ci}
2774514f5e3Sopenharmony_ci
2784514f5e3Sopenharmony_ci/* demo2 本地指针包装的使用。 */
2794514f5e3Sopenharmony_ciclass NativePointerTestClass {
2804514f5e3Sopenharmony_cipublic:
2814514f5e3Sopenharmony_ci    NativePointerTestClass() : someProperty(0)
2824514f5e3Sopenharmony_ci    {
2834514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "NativePointerTestClass Construction";
2844514f5e3Sopenharmony_ci    }
2854514f5e3Sopenharmony_ci
2864514f5e3Sopenharmony_ci    ~NativePointerTestClass()
2874514f5e3Sopenharmony_ci    {
2884514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "NativePointerTestClass Destruction";
2894514f5e3Sopenharmony_ci    }
2904514f5e3Sopenharmony_ci
2914514f5e3Sopenharmony_ci    int GetsomeProperty() const
2924514f5e3Sopenharmony_ci    {
2934514f5e3Sopenharmony_ci        return someProperty;
2944514f5e3Sopenharmony_ci    }
2954514f5e3Sopenharmony_ci
2964514f5e3Sopenharmony_ci    void SetsomeProperty(int value)
2974514f5e3Sopenharmony_ci    {
2984514f5e3Sopenharmony_ci        someProperty = value;
2994514f5e3Sopenharmony_ci    }
3004514f5e3Sopenharmony_ci
3014514f5e3Sopenharmony_ciprivate:
3024514f5e3Sopenharmony_ci    int someProperty;
3034514f5e3Sopenharmony_ci};
3044514f5e3Sopenharmony_ci
3054514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_NativePointerRef)
3064514f5e3Sopenharmony_ci{
3074514f5e3Sopenharmony_ci    LocalScope scope(vm_);
3084514f5e3Sopenharmony_ci    NativePointerTestClass *nativePointerTestClass = new NativePointerTestClass();
3094514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointer adress: " << nativePointerTestClass;
3104514f5e3Sopenharmony_ci    Local<NativePointerRef> nativePoint = NativePointerRef::New(
3114514f5e3Sopenharmony_ci        vm_, nativePointerTestClass,
3124514f5e3Sopenharmony_ci        [](void *env, void *value, void *hint) {
3134514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "NativePointerCb value : " << value;
3144514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "NativePointerCb hint  : " << hint;
3154514f5e3Sopenharmony_ci            NativePointerTestClass *ptr = static_cast<NativePointerTestClass *>(value);
3164514f5e3Sopenharmony_ci            delete ptr;
3174514f5e3Sopenharmony_ci        },
3184514f5e3Sopenharmony_ci        (void *)0x123);
3194514f5e3Sopenharmony_ci    NativePointerTestClass *yourClassPtr = static_cast<NativePointerTestClass *>(nativePoint->Value());
3204514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointer GetValue : " << yourClassPtr->GetsomeProperty();
3214514f5e3Sopenharmony_ci    yourClassPtr->SetsomeProperty(99);
3224514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointer SetValue : " << yourClassPtr->GetsomeProperty();
3234514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointer Value : " << nativePoint->Value();
3244514f5e3Sopenharmony_ci}
3254514f5e3Sopenharmony_ci
3264514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_NativePointerRef_String)
3274514f5e3Sopenharmony_ci{
3284514f5e3Sopenharmony_ci    LocalScope scope(vm_);
3294514f5e3Sopenharmony_ci    void *vps = new std::string("123456");
3304514f5e3Sopenharmony_ci    Local<NativePointerRef> nativePoint = NativePointerRef::New(vm_, vps);
3314514f5e3Sopenharmony_ci    std::string *p = (std::string *)nativePoint->Value();
3324514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointerRef_String Value : " << *p;
3334514f5e3Sopenharmony_ci    std::string value = "78910";
3344514f5e3Sopenharmony_ci    *p = value;
3354514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointerRef_String ChangeValue : " << *((std::string *)nativePoint->Value());
3364514f5e3Sopenharmony_ci}
3374514f5e3Sopenharmony_ci
3384514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_NativePointerRef_Double)
3394514f5e3Sopenharmony_ci{
3404514f5e3Sopenharmony_ci    LocalScope scope(vm_);
3414514f5e3Sopenharmony_ci    void *vpd = new double(123.456);
3424514f5e3Sopenharmony_ci    Local<NativePointerRef> nativePoint = NativePointerRef::New(vm_, vpd);
3434514f5e3Sopenharmony_ci    double *p = (double *)nativePoint->Value();
3444514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointerRef_Double Value : " << *p;
3454514f5e3Sopenharmony_ci    double value = 66.66;
3464514f5e3Sopenharmony_ci    *p = value;
3474514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointerRef_Double ChangeValue: " << *((double *)nativePoint->Value());
3484514f5e3Sopenharmony_ci}
3494514f5e3Sopenharmony_ci
3504514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_NativePointerRef_Char)
3514514f5e3Sopenharmony_ci{
3524514f5e3Sopenharmony_ci    LocalScope scope(vm_);
3534514f5e3Sopenharmony_ci    void *vpc = new char('a');
3544514f5e3Sopenharmony_ci    Local<NativePointerRef> nativePoint = NativePointerRef::New(vm_, vpc);
3554514f5e3Sopenharmony_ci    char *p = (char *)nativePoint->Value();
3564514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointerRef_Char Value : " << *p;
3574514f5e3Sopenharmony_ci    *p = 'A';
3584514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointerRef_Char ChangeValue: " << *((char *)nativePoint->Value());
3594514f5e3Sopenharmony_ci}
3604514f5e3Sopenharmony_ci
3614514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_NativePointerRef_Long)
3624514f5e3Sopenharmony_ci{
3634514f5e3Sopenharmony_ci    LocalScope scope(vm_);
3644514f5e3Sopenharmony_ci    void *vpl = new long(123456);
3654514f5e3Sopenharmony_ci    Local<NativePointerRef> nativePoint = NativePointerRef::New(vm_, vpl);
3664514f5e3Sopenharmony_ci    long *p = (long *)nativePoint->Value();
3674514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointerRef_Long Value : " << *p;
3684514f5e3Sopenharmony_ci    long value = 2147483647;
3694514f5e3Sopenharmony_ci    *p = value;
3704514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointerRef_Long ChangeValue: " << *((long *)nativePoint->Value());
3714514f5e3Sopenharmony_ci}
3724514f5e3Sopenharmony_ci
3734514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_NativePointerRef_Int)
3744514f5e3Sopenharmony_ci{
3754514f5e3Sopenharmony_ci    LocalScope scope(vm_);
3764514f5e3Sopenharmony_ci    void *vpi = new int(123);
3774514f5e3Sopenharmony_ci    Local<NativePointerRef> nativePoint = NativePointerRef::New(vm_, vpi);
3784514f5e3Sopenharmony_ci    int *p = (int *)nativePoint->Value();
3794514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointerRef_Int Value : " << *p;
3804514f5e3Sopenharmony_ci    int value = 147483647;
3814514f5e3Sopenharmony_ci    *p = value;
3824514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_NativePointerRef_Int ChangeValue: " << *((int *)nativePoint->Value());
3834514f5e3Sopenharmony_ci}
3844514f5e3Sopenharmony_ci
3854514f5e3Sopenharmony_ci/* demo3 对object的操作 */
3864514f5e3Sopenharmony_cistatic std::vector<Local<SymbolRef>> GetSymbolRef(EcmaVM *vm)
3874514f5e3Sopenharmony_ci{
3884514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "GetSymbolRef";
3894514f5e3Sopenharmony_ci    static std::vector<Local<SymbolRef>> value = { SymbolRef::New(vm, StringRef::NewFromUtf8(vm, "symbolKey1")),
3904514f5e3Sopenharmony_ci                                                   SymbolRef::New(vm, StringRef::NewFromUtf8(vm, "symbolKey2")),
3914514f5e3Sopenharmony_ci                                                   SymbolRef::New(vm, StringRef::NewFromUtf8(vm, "symbolKey3")) };
3924514f5e3Sopenharmony_ci    return value;
3934514f5e3Sopenharmony_ci}
3944514f5e3Sopenharmony_ci
3954514f5e3Sopenharmony_ciLocal<JSValueRef> Setter1(JsiRuntimeCallInfo *info)
3964514f5e3Sopenharmony_ci{
3974514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Setter is running";
3984514f5e3Sopenharmony_ci    int cnt = 0; // 0 = first element
3994514f5e3Sopenharmony_ci    EscapeLocalScope scope(info->GetVM());
4004514f5e3Sopenharmony_ci    Local<JSValueRef> arg = info->GetCallArgRef(0);
4014514f5e3Sopenharmony_ci    Local<JSValueRef> value = info->GetThisRef();
4024514f5e3Sopenharmony_ci    Local<ObjectRef> obj = value->ToObject(info->GetVM());
4034514f5e3Sopenharmony_ci    if (value->IsNumber()) {
4044514f5e3Sopenharmony_ci        double num = value->ToNumber(info->GetVM())->Value();
4054514f5e3Sopenharmony_ci        int max = 100; // 100 = maxValue
4064514f5e3Sopenharmony_ci        int min = 0;   // 0 = minValue
4074514f5e3Sopenharmony_ci        if (num < min || num > max) {
4084514f5e3Sopenharmony_ci            return JSValueRef::Undefined(info->GetVM());
4094514f5e3Sopenharmony_ci        }
4104514f5e3Sopenharmony_ci    }
4114514f5e3Sopenharmony_ci    obj->Set(info->GetVM(), GetSymbolRef(info->GetVM())[cnt], arg);
4124514f5e3Sopenharmony_ci    return JSValueRef::Undefined(info->GetVM());
4134514f5e3Sopenharmony_ci}
4144514f5e3Sopenharmony_ci
4154514f5e3Sopenharmony_ciLocal<JSValueRef> Setter2(JsiRuntimeCallInfo *info)
4164514f5e3Sopenharmony_ci{
4174514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Setter is running";
4184514f5e3Sopenharmony_ci    int cnt = 1; // 1 = second element
4194514f5e3Sopenharmony_ci    EscapeLocalScope scope(info->GetVM());
4204514f5e3Sopenharmony_ci    Local<JSValueRef> arg = info->GetCallArgRef(0);
4214514f5e3Sopenharmony_ci    Local<JSValueRef> value = info->GetThisRef();
4224514f5e3Sopenharmony_ci    Local<ObjectRef> obj = value->ToObject(info->GetVM());
4234514f5e3Sopenharmony_ci    if (!arg->IsString(info->GetVM())) {
4244514f5e3Sopenharmony_ci        return JSValueRef::Undefined(info->GetVM());
4254514f5e3Sopenharmony_ci    }
4264514f5e3Sopenharmony_ci    obj->Set(info->GetVM(), GetSymbolRef(info->GetVM())[cnt], arg);
4274514f5e3Sopenharmony_ci    return JSValueRef::Undefined(info->GetVM());
4284514f5e3Sopenharmony_ci}
4294514f5e3Sopenharmony_ci
4304514f5e3Sopenharmony_ciLocal<JSValueRef> Setter3(JsiRuntimeCallInfo *info)
4314514f5e3Sopenharmony_ci{
4324514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Setter is running";
4334514f5e3Sopenharmony_ci    int cnt = 2; // 2 = third element
4344514f5e3Sopenharmony_ci    EscapeLocalScope scope(info->GetVM());
4354514f5e3Sopenharmony_ci    Local<JSValueRef> arg = info->GetCallArgRef(0);
4364514f5e3Sopenharmony_ci    Local<JSValueRef> value = info->GetThisRef();
4374514f5e3Sopenharmony_ci    Local<ObjectRef> obj = value->ToObject(info->GetVM());
4384514f5e3Sopenharmony_ci    if (!arg->IsSymbol(info->GetVM())) {
4394514f5e3Sopenharmony_ci        return JSValueRef::Undefined(info->GetVM());
4404514f5e3Sopenharmony_ci    }
4414514f5e3Sopenharmony_ci    obj->Set(info->GetVM(), GetSymbolRef(info->GetVM())[cnt], arg);
4424514f5e3Sopenharmony_ci    return JSValueRef::Undefined(info->GetVM());
4434514f5e3Sopenharmony_ci}
4444514f5e3Sopenharmony_ci
4454514f5e3Sopenharmony_ciLocal<JSValueRef> Getter1(JsiRuntimeCallInfo *info)
4464514f5e3Sopenharmony_ci{
4474514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Getter is running";
4484514f5e3Sopenharmony_ci    int cnt = 0; // 0 = first element
4494514f5e3Sopenharmony_ci    Local<JSValueRef> value = info->GetThisRef();
4504514f5e3Sopenharmony_ci    Local<ObjectRef> obj = value->ToObject(info->GetVM());
4514514f5e3Sopenharmony_ci    Local<JSValueRef> temp = obj->Get(info->GetVM(), GetSymbolRef(info->GetVM())[cnt]);
4524514f5e3Sopenharmony_ci    return temp->ToString(info->GetVM());
4534514f5e3Sopenharmony_ci}
4544514f5e3Sopenharmony_ci
4554514f5e3Sopenharmony_ciLocal<JSValueRef> Getter2(JsiRuntimeCallInfo *info)
4564514f5e3Sopenharmony_ci{
4574514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Getter is running";
4584514f5e3Sopenharmony_ci    int cnt = 1; // 1 = second element
4594514f5e3Sopenharmony_ci    Local<JSValueRef> value = info->GetThisRef();
4604514f5e3Sopenharmony_ci    Local<ObjectRef> obj = value->ToObject(info->GetVM());
4614514f5e3Sopenharmony_ci    Local<JSValueRef> temp = obj->Get(info->GetVM(), GetSymbolRef(info->GetVM())[cnt]);
4624514f5e3Sopenharmony_ci    return temp->ToString(info->GetVM());
4634514f5e3Sopenharmony_ci}
4644514f5e3Sopenharmony_ci
4654514f5e3Sopenharmony_ciLocal<JSValueRef> Getter3(JsiRuntimeCallInfo *info)
4664514f5e3Sopenharmony_ci{
4674514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Getter is running";
4684514f5e3Sopenharmony_ci    int cnt = 2; // 2 = third element
4694514f5e3Sopenharmony_ci    Local<JSValueRef> value = info->GetThisRef();
4704514f5e3Sopenharmony_ci    Local<ObjectRef> obj = value->ToObject(info->GetVM());
4714514f5e3Sopenharmony_ci    Local<JSValueRef> temp = obj->Get(info->GetVM(), GetSymbolRef(info->GetVM())[cnt]);
4724514f5e3Sopenharmony_ci    if (!temp->IsSymbol(info->GetVM())) {
4734514f5e3Sopenharmony_ci        JSValueRef::Undefined(info->GetVM());
4744514f5e3Sopenharmony_ci    }
4754514f5e3Sopenharmony_ci    Local<SymbolRef> str = temp;
4764514f5e3Sopenharmony_ci    return str->GetDescription(info->GetVM());
4774514f5e3Sopenharmony_ci}
4784514f5e3Sopenharmony_ci
4794514f5e3Sopenharmony_civoid ObjectRefSet(Local<ObjectRef> object, EcmaVM *vm, Local<SymbolRef> symbol)
4804514f5e3Sopenharmony_ci{
4814514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "ObjectRefSet";
4824514f5e3Sopenharmony_ci    int cnt = 1; // 1 = key
4834514f5e3Sopenharmony_ci    bool b = object->Set(vm, cnt, StringRef::NewFromUtf8(vm, "TestValue1"));
4844514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
4854514f5e3Sopenharmony_ci    b = object->Set(vm, StringRef::NewFromUtf8(vm, "Test2"), StringRef::NewFromUtf8(vm, "TestValue2"));
4864514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
4874514f5e3Sopenharmony_ci    b = object->Set(vm, StringRef::NewFromUtf8(vm, "Test3"), StringRef::NewFromUtf8(vm, "TestValue3"));
4884514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
4894514f5e3Sopenharmony_ci    b = object->Set(vm, symbol, StringRef::NewFromUtf8(vm, "symbolValue"));
4904514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
4914514f5e3Sopenharmony_ci
4924514f5e3Sopenharmony_ci    Local<FunctionRef> getter1 = FunctionRef::New(vm, Getter1);
4934514f5e3Sopenharmony_ci    Local<FunctionRef> setter1 = FunctionRef::New(vm, Setter1);
4944514f5e3Sopenharmony_ci
4954514f5e3Sopenharmony_ci    Local<FunctionRef> getter2 = FunctionRef::New(vm, Getter2);
4964514f5e3Sopenharmony_ci    Local<FunctionRef> setter2 = FunctionRef::New(vm, Setter2);
4974514f5e3Sopenharmony_ci
4984514f5e3Sopenharmony_ci    Local<FunctionRef> getter3 = FunctionRef::New(vm, Getter3);
4994514f5e3Sopenharmony_ci    Local<FunctionRef> setter3 = FunctionRef::New(vm, Setter3);
5004514f5e3Sopenharmony_ci
5014514f5e3Sopenharmony_ci    PropertyAttribute attribute1(StringRef::NewFromUtf8(vm, "AttributeValue1"), true, true, false);
5024514f5e3Sopenharmony_ci    PropertyAttribute attribute2(StringRef::NewFromUtf8(vm, "AttributeValue2"), false, true, true);
5034514f5e3Sopenharmony_ci    PropertyAttribute attribute3(StringRef::NewFromUtf8(vm, "AttributeValue3"), true, false, true);
5044514f5e3Sopenharmony_ci    attribute1.SetGetter(getter1);
5054514f5e3Sopenharmony_ci    attribute1.SetSetter(setter1);
5064514f5e3Sopenharmony_ci    b = object->DefineProperty(vm, StringRef::NewFromUtf8(vm, "AttributeKey1"), attribute1);
5074514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5084514f5e3Sopenharmony_ci    b = object->DefineProperty(vm, StringRef::NewFromUtf8(vm, "Test2"), attribute2);
5094514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5104514f5e3Sopenharmony_ci    b = object->SetAccessorProperty(vm, StringRef::NewFromUtf8(vm, "Accessor1"), getter1, setter1);
5114514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5124514f5e3Sopenharmony_ci    b = object->SetAccessorProperty(vm, StringRef::NewFromUtf8(vm, "Test3"), getter2, setter2);
5134514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5144514f5e3Sopenharmony_ci    b = object->DefineProperty(vm, StringRef::NewFromUtf8(vm, "AttributeKey3"), attribute3);
5154514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5164514f5e3Sopenharmony_ci    b = object->SetAccessorProperty(vm, StringRef::NewFromUtf8(vm, "AttributeKey3"), getter3, setter3, attribute3);
5174514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5184514f5e3Sopenharmony_ci}
5194514f5e3Sopenharmony_ci
5204514f5e3Sopenharmony_civoid GetProperty(Local<ObjectRef> object, EcmaVM *vm)
5214514f5e3Sopenharmony_ci{
5224514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "GetProperty";
5234514f5e3Sopenharmony_ci    Local<ArrayRef> names = object->GetOwnPropertyNames(vm);
5244514f5e3Sopenharmony_ci    int cnt = names->Length(vm);
5254514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "GetOwnPropertyNames cnt: " << cnt;
5264514f5e3Sopenharmony_ci    for (int i = 0; i < cnt; i++) {
5274514f5e3Sopenharmony_ci        Local<JSValueRef> value = ArrayRef::GetValueAt(vm, names, i);
5284514f5e3Sopenharmony_ci        if (value->IsSymbol(vm)) {
5294514f5e3Sopenharmony_ci            Local<SymbolRef> symbol = value;
5304514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "PropertyNames: " << symbol->GetDescription(vm)->ToString(vm);
5314514f5e3Sopenharmony_ci        } else {
5324514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "PropertyNames: " << value->ToString(vm)->ToString(vm);
5334514f5e3Sopenharmony_ci        }
5344514f5e3Sopenharmony_ci    }
5354514f5e3Sopenharmony_ci}
5364514f5e3Sopenharmony_ci
5374514f5e3Sopenharmony_civoid Get(Local<ObjectRef> object, EcmaVM *vm)
5384514f5e3Sopenharmony_ci{
5394514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Get";
5404514f5e3Sopenharmony_ci    int cnt = 1; // 1 = key
5414514f5e3Sopenharmony_ci    Local<JSValueRef> value = object->Get(vm, cnt);
5424514f5e3Sopenharmony_ci    if (value->IsString(vm)) {
5434514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "Key:1  Value:" << value->ToString(vm)->ToString(vm);
5444514f5e3Sopenharmony_ci    }
5454514f5e3Sopenharmony_ci    value = object->Get(vm, StringRef::NewFromUtf8(vm, "Test2"));
5464514f5e3Sopenharmony_ci    if (value->IsString(vm)) {
5474514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "Key:Test2  Value:" << value->ToString(vm)->ToString(vm);
5484514f5e3Sopenharmony_ci    }
5494514f5e3Sopenharmony_ci    value = object->Get(vm, StringRef::NewFromUtf8(vm, "AttributeKey1"));
5504514f5e3Sopenharmony_ci    if (value->IsString(vm)) {
5514514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "Key:AttributeKey1  Value:" << value->ToString(vm)->ToString(vm);
5524514f5e3Sopenharmony_ci    }
5534514f5e3Sopenharmony_ci    int num = 10; // 10 = randomness
5544514f5e3Sopenharmony_ci    object->Set(vm, StringRef::NewFromUtf8(vm, "Accessor1"), NumberRef::New(vm, num));
5554514f5e3Sopenharmony_ci    object->Set(vm, StringRef::NewFromUtf8(vm, "Test3"), StringRef::NewFromUtf8(vm, "Test3Value"));
5564514f5e3Sopenharmony_ci    object->Set(vm, StringRef::NewFromUtf8(vm, "AttributeKey3"),
5574514f5e3Sopenharmony_ci        SymbolRef::New(vm, StringRef::NewFromUtf8(vm, "AttributeKey3Value")));
5584514f5e3Sopenharmony_ci    Local<StringRef> str = object->Get(vm, StringRef::NewFromUtf8(vm, "AttributeKey3"));
5594514f5e3Sopenharmony_ci    if (str->IsString(vm)) {
5604514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "Key:AttributeKey3  Value:" << str->ToString(vm);
5614514f5e3Sopenharmony_ci    }
5624514f5e3Sopenharmony_ci    str = object->Get(vm, StringRef::NewFromUtf8(vm, "Accessor1"));
5634514f5e3Sopenharmony_ci    if (str->IsString(vm)) {
5644514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "Key:Accessor1  Value:" << str->ToString(vm);
5654514f5e3Sopenharmony_ci    }
5664514f5e3Sopenharmony_ci    str = object->Get(vm, StringRef::NewFromUtf8(vm, "Test3"));
5674514f5e3Sopenharmony_ci    if (str->IsString(vm)) {
5684514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "Key:Test3  Value:" << str->ToString(vm);
5694514f5e3Sopenharmony_ci    }
5704514f5e3Sopenharmony_ci}
5714514f5e3Sopenharmony_ci
5724514f5e3Sopenharmony_civoid SetValueAgain(Local<ObjectRef> object, EcmaVM *vm)
5734514f5e3Sopenharmony_ci{
5744514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "SetValueAgain";
5754514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "SetValueAgain";
5764514f5e3Sopenharmony_ci    int cnt = 1; // 1 = key
5774514f5e3Sopenharmony_ci    bool b = object->Set(vm, cnt, StringRef::NewFromUtf8(vm, "TestValueAgain1"));
5784514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5794514f5e3Sopenharmony_ci    b = object->Set(vm, StringRef::NewFromUtf8(vm, "Test3"), StringRef::NewFromUtf8(vm, "TestValueAgain3"));
5804514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5814514f5e3Sopenharmony_ci    b = object->Set(vm, StringRef::NewFromUtf8(vm, "AttributeKey1"),
5824514f5e3Sopenharmony_ci        StringRef::NewFromUtf8(vm, "AttributeValueAgain1"));
5834514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5844514f5e3Sopenharmony_ci    b = object->Set(vm, StringRef::NewFromUtf8(vm, "AttributeKey3"),
5854514f5e3Sopenharmony_ci        StringRef::NewFromUtf8(vm, "AttributeValueAgain3"));
5864514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5874514f5e3Sopenharmony_ci    b = object->Set(vm, StringRef::NewFromUtf8(vm, "Accessor1"), StringRef::NewFromUtf8(vm, "AttributeValueAgain2"));
5884514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
5894514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "GetValueAgain";
5904514f5e3Sopenharmony_ci    Get(object, vm);
5914514f5e3Sopenharmony_ci    GetProperty(object, vm);
5924514f5e3Sopenharmony_ci}
5934514f5e3Sopenharmony_ci
5944514f5e3Sopenharmony_civoid GetOwnEnumerablePropertyNames(Local<ObjectRef> object, EcmaVM *vm)
5954514f5e3Sopenharmony_ci{
5964514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "GetOwnEnumerablePropertyNames";
5974514f5e3Sopenharmony_ci    Local<ArrayRef> names = object->GetOwnEnumerablePropertyNames(vm);
5984514f5e3Sopenharmony_ci    int cnt = names->Length(vm);
5994514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "GetOwnEnumerablePropertyNames cnt: " << cnt;
6004514f5e3Sopenharmony_ci    for (int i = 0; i < cnt; i++) {
6014514f5e3Sopenharmony_ci        Local<JSValueRef> value = ArrayRef::GetValueAt(vm, names, i);
6024514f5e3Sopenharmony_ci        if (value->IsSymbol(vm)) {
6034514f5e3Sopenharmony_ci            Local<SymbolRef> symbol = value;
6044514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "PropertyNames: " << symbol->GetDescription(vm)->ToString(vm);
6054514f5e3Sopenharmony_ci        } else {
6064514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "PropertyNames: " << value->ToString(vm)->ToString(vm);
6074514f5e3Sopenharmony_ci        }
6084514f5e3Sopenharmony_ci    }
6094514f5e3Sopenharmony_ci}
6104514f5e3Sopenharmony_ci
6114514f5e3Sopenharmony_civoid PrintAllProperty(Local<ObjectRef> object, EcmaVM *vm, int flag)
6124514f5e3Sopenharmony_ci{
6134514f5e3Sopenharmony_ci    Local<ArrayRef> names = object->GetAllPropertyNames(vm, flag);
6144514f5e3Sopenharmony_ci    int cnt = names->Length(vm);
6154514f5e3Sopenharmony_ci    switch (flag) {
6164514f5e3Sopenharmony_ci        case 0: // 0 = NATIVE_DEFAULT
6174514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "GetOwnPropertyNames NATIVE_DEFAULT: " << cnt;
6184514f5e3Sopenharmony_ci            break;
6194514f5e3Sopenharmony_ci        case 1: // 1 = NATIVE_WRITABLE
6204514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "GetOwnPropertyNames NATIVE_WRITABLE: " << cnt;
6214514f5e3Sopenharmony_ci            break;
6224514f5e3Sopenharmony_ci        case 2: // 2 = NATIVE_ENUMERABLE
6234514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "GetOwnPropertyNames NATIVE_ENUMERABLE: " << cnt;
6244514f5e3Sopenharmony_ci            break;
6254514f5e3Sopenharmony_ci        case 3: // 3 = NATIVE_CONFIGURABLE
6264514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "GetOwnPropertyNames NATIVE_CONFIGURABLE: " << cnt;
6274514f5e3Sopenharmony_ci            break;
6284514f5e3Sopenharmony_ci        default:
6294514f5e3Sopenharmony_ci            break;
6304514f5e3Sopenharmony_ci    }
6314514f5e3Sopenharmony_ci    for (int i = 0; i < cnt; i++) {
6324514f5e3Sopenharmony_ci        Local<JSValueRef> value = ArrayRef::GetValueAt(vm, names, i);
6334514f5e3Sopenharmony_ci        if (value->IsSymbol(vm)) {
6344514f5e3Sopenharmony_ci            Local<SymbolRef> symbol = value;
6354514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "PropertyNames: " << symbol->GetDescription(vm)->ToString(vm);
6364514f5e3Sopenharmony_ci        } else {
6374514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "PropertyNames: " << value->ToString(vm)->ToString(vm);
6384514f5e3Sopenharmony_ci        }
6394514f5e3Sopenharmony_ci    }
6404514f5e3Sopenharmony_ci}
6414514f5e3Sopenharmony_ci
6424514f5e3Sopenharmony_civoid GetAllPropertyNames(Local<ObjectRef> object, EcmaVM *vm)
6434514f5e3Sopenharmony_ci{
6444514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "GetAllPropertyNames";
6454514f5e3Sopenharmony_ci    for (int i = 0; i < 4; i++) { // 4 show Go through all the properties
6464514f5e3Sopenharmony_ci        PrintAllProperty(object, vm, i);
6474514f5e3Sopenharmony_ci    }
6484514f5e3Sopenharmony_ci}
6494514f5e3Sopenharmony_ci
6504514f5e3Sopenharmony_civoid HasAndDelete(Local<ObjectRef> object, EcmaVM *vm)
6514514f5e3Sopenharmony_ci{
6524514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "HasAndDelete";
6534514f5e3Sopenharmony_ci    bool b = object->Has(vm, 1); // 1 = key
6544514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
6554514f5e3Sopenharmony_ci    b = object->Has(vm, StringRef::NewFromUtf8(vm, "Test2"));
6564514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
6574514f5e3Sopenharmony_ci    b = object->Delete(vm, StringRef::NewFromUtf8(vm, "Test2"));
6584514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
6594514f5e3Sopenharmony_ci    b = object->Has(vm, StringRef::NewFromUtf8(vm, "Test2"));
6604514f5e3Sopenharmony_ci    ASSERT_FALSE(b);
6614514f5e3Sopenharmony_ci}
6624514f5e3Sopenharmony_ci
6634514f5e3Sopenharmony_civoid FreezeAndSeal(Local<ObjectRef> object, EcmaVM *vm)
6644514f5e3Sopenharmony_ci{
6654514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "FreezeAndSeal";
6664514f5e3Sopenharmony_ci    object->Seal(vm);
6674514f5e3Sopenharmony_ci    int num1 = 1; // 1 = key
6684514f5e3Sopenharmony_ci    int num2 = 2; // 2 = key
6694514f5e3Sopenharmony_ci    bool b = object->Set(vm, num1, StringRef::NewFromUtf8(vm, "Seal1"));
6704514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
6714514f5e3Sopenharmony_ci    b = object->Delete(vm, num1);
6724514f5e3Sopenharmony_ci    ASSERT_FALSE(b);
6734514f5e3Sopenharmony_ci    b = object->Set(vm, num2, StringRef::NewFromUtf8(vm, "2"));
6744514f5e3Sopenharmony_ci    ASSERT_FALSE(b);
6754514f5e3Sopenharmony_ci    object->Freeze(vm);
6764514f5e3Sopenharmony_ci    PropertyAttribute attribute(StringRef::NewFromUtf8(vm, "FreezeValue"), true, true, false);
6774514f5e3Sopenharmony_ci    b = object->DefineProperty(vm, StringRef::NewFromUtf8(vm, "Freeze"), attribute);
6784514f5e3Sopenharmony_ci    ASSERT_FALSE(b);
6794514f5e3Sopenharmony_ci    b = object->Has(vm, num2);
6804514f5e3Sopenharmony_ci    ASSERT_FALSE(b);
6814514f5e3Sopenharmony_ci    b = object->Has(vm, StringRef::NewFromUtf8(vm, "Freeze"));
6824514f5e3Sopenharmony_ci    ASSERT_FALSE(b);
6834514f5e3Sopenharmony_ci    b = object->Delete(vm, num2);
6844514f5e3Sopenharmony_ci    ASSERT_FALSE(b);
6854514f5e3Sopenharmony_ci    b = object->Delete(vm, StringRef::NewFromUtf8(vm, "Freeze"));
6864514f5e3Sopenharmony_ci    ASSERT_FALSE(b);
6874514f5e3Sopenharmony_ci}
6884514f5e3Sopenharmony_ci
6894514f5e3Sopenharmony_civoid GetOwnProperty(Local<ObjectRef> object, EcmaVM *vm)
6904514f5e3Sopenharmony_ci{
6914514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "GetOwnProperty";
6924514f5e3Sopenharmony_ci    PropertyAttribute value1;
6934514f5e3Sopenharmony_ci    bool b = object->GetOwnProperty(vm, NumberRef::New(vm, 1), value1);
6944514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
6954514f5e3Sopenharmony_ci    PropertyAttribute value2;
6964514f5e3Sopenharmony_ci    b = object->GetOwnProperty(vm, StringRef::NewFromUtf8(vm, "AttributeKey1"), value2);
6974514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
6984514f5e3Sopenharmony_ci    ASSERT_EQ(true, value2.IsWritable());
6994514f5e3Sopenharmony_ci    ASSERT_EQ(true, value2.IsEnumerable());
7004514f5e3Sopenharmony_ci    ASSERT_EQ(false, value2.IsConfigurable());
7014514f5e3Sopenharmony_ci    ASSERT_EQ(true, value2.HasWritable());
7024514f5e3Sopenharmony_ci    ASSERT_EQ(true, value2.HasConfigurable());
7034514f5e3Sopenharmony_ci    ASSERT_EQ(true, value2.HasEnumerable());
7044514f5e3Sopenharmony_ci    Local<JSValueRef> value = value2.GetValue(vm);
7054514f5e3Sopenharmony_ci    ASSERT_EQ("AttributeValue1", value->ToString(vm)->ToString(vm));
7064514f5e3Sopenharmony_ci}
7074514f5e3Sopenharmony_ci
7084514f5e3Sopenharmony_ciclass A {
7094514f5e3Sopenharmony_cipublic:
7104514f5e3Sopenharmony_ci    void Test() const
7114514f5e3Sopenharmony_ci    {
7124514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "Class A Test()";
7134514f5e3Sopenharmony_ci    }
7144514f5e3Sopenharmony_ci};
7154514f5e3Sopenharmony_ci
7164514f5e3Sopenharmony_civoid NativePointer(Local<ObjectRef> object, EcmaVM *vm)
7174514f5e3Sopenharmony_ci{
7184514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "NativePointer";
7194514f5e3Sopenharmony_ci    int cnt = 10; // 10 = accommodate quantity
7204514f5e3Sopenharmony_ci    object->SetNativePointerFieldCount(vm, cnt);
7214514f5e3Sopenharmony_ci    ASSERT_EQ(cnt, object->GetNativePointerFieldCount(vm));
7224514f5e3Sopenharmony_ci    A *a = new A();
7234514f5e3Sopenharmony_ci    int cnt2 = 1;  // 11 = random Numbers
7244514f5e3Sopenharmony_ci    int cnt3 = 11; // 1 = random Numbers
7254514f5e3Sopenharmony_ci    object->SetNativePointerField(vm, cnt2, static_cast<void *>(a), nullptr, nullptr);
7264514f5e3Sopenharmony_ci    object->SetNativePointerField(vm, cnt3, static_cast<void *>(a), nullptr, nullptr);
7274514f5e3Sopenharmony_ci    A *value1 = static_cast<A *>(object->GetNativePointerField(vm, cnt2));
7284514f5e3Sopenharmony_ci    A *value2 = static_cast<A *>(object->GetNativePointerField(vm, cnt3));
7294514f5e3Sopenharmony_ci    if (value1 == nullptr) {
7304514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "SetNativePointerField is Error";
7314514f5e3Sopenharmony_ci    } else {
7324514f5e3Sopenharmony_ci        value1->Test();
7334514f5e3Sopenharmony_ci    }
7344514f5e3Sopenharmony_ci    if (value2 == nullptr) {
7354514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "SetNativePointerField is OK";
7364514f5e3Sopenharmony_ci    }
7374514f5e3Sopenharmony_ci    Local<NativePointerRef> native = NativePointerRef::New(vm, static_cast<void *>(a));
7384514f5e3Sopenharmony_ci    bool b = object->ConvertToNativeBindingObject(vm, native);
7394514f5e3Sopenharmony_ci    ASSERT_TRUE(b);
7404514f5e3Sopenharmony_ci}
7414514f5e3Sopenharmony_ci
7424514f5e3Sopenharmony_civoid SetPrototype(Local<ObjectRef> object, EcmaVM *vm)
7434514f5e3Sopenharmony_ci{
7444514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "SetPrototype";
7454514f5e3Sopenharmony_ci    int cnt = 111; // 111 = random Numbers
7464514f5e3Sopenharmony_ci    Local<NumberRef> obj = NumberRef::New(vm, cnt);
7474514f5e3Sopenharmony_ci    object->SetPrototype(vm, obj);
7484514f5e3Sopenharmony_ci    Local<JSValueRef> type = object->GetPrototype(vm);
7494514f5e3Sopenharmony_ci    if (type->IsUndefined() || type->IsNull()) {
7504514f5e3Sopenharmony_ci        return;
7514514f5e3Sopenharmony_ci    }
7524514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Prototype: " << type->Typeof(vm)->ToString(vm);
7534514f5e3Sopenharmony_ci}
7544514f5e3Sopenharmony_ci
7554514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, Sample_Demo3_ObjectRef)
7564514f5e3Sopenharmony_ci{
7574514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "SetPrototype";
7584514f5e3Sopenharmony_ci    LocalScope scope(vm_);
7594514f5e3Sopenharmony_ci    GetSymbolRef(vm_);
7604514f5e3Sopenharmony_ci    Local<SymbolRef> symbol = SymbolRef::New(vm_, StringRef::NewFromUtf8(vm_, "Symbol"));
7614514f5e3Sopenharmony_ci    Local<ObjectRef> object = ObjectRef::New(vm_);
7624514f5e3Sopenharmony_ci    SetPrototype(object, vm_);
7634514f5e3Sopenharmony_ci    ObjectRefSet(object, vm_, symbol);
7644514f5e3Sopenharmony_ci    GetProperty(object, vm_);
7654514f5e3Sopenharmony_ci    GetOwnProperty(object, vm_);
7664514f5e3Sopenharmony_ci    GetAllPropertyNames(object, vm_);
7674514f5e3Sopenharmony_ci    GetOwnEnumerablePropertyNames(object, vm_);
7684514f5e3Sopenharmony_ci    Get(object, vm_);
7694514f5e3Sopenharmony_ci    NativePointer(object, vm_);
7704514f5e3Sopenharmony_ci    SetValueAgain(object, vm_);
7714514f5e3Sopenharmony_ci    HasAndDelete(object, vm_);
7724514f5e3Sopenharmony_ci    FreezeAndSeal(object, vm_);
7734514f5e3Sopenharmony_ci}
7744514f5e3Sopenharmony_ci
7754514f5e3Sopenharmony_ci/* demo4 无参无返回值的函数的调用。 ts:
7764514f5e3Sopenharmony_ci * function FuncTest(): void {
7774514f5e3Sopenharmony_ci * console.log("func test log ...");
7784514f5e3Sopenharmony_ci * }
7794514f5e3Sopenharmony_ci * FuncTest();
7804514f5e3Sopenharmony_ci */
7814514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo4_function_test_1)
7824514f5e3Sopenharmony_ci{
7834514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo4_function_test_1 =======================================";
7844514f5e3Sopenharmony_ci    LocalScope scope(vm_);
7854514f5e3Sopenharmony_ci
7864514f5e3Sopenharmony_ci    Local<FunctionRef> FuncTest = FunctionRef::New(vm_, [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
7874514f5e3Sopenharmony_ci        EcmaVM *vm = runtimeInfo->GetVM();
7884514f5e3Sopenharmony_ci        LocalScope scope(vm);
7894514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "func test log ...";
7904514f5e3Sopenharmony_ci        return JSValueRef::Undefined(vm);
7914514f5e3Sopenharmony_ci    });
7924514f5e3Sopenharmony_ci    FuncTest->Call(vm_, JSValueRef::Undefined(vm_), nullptr, 0);
7934514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo4_function_test_1 ==========================================";
7944514f5e3Sopenharmony_ci}
7954514f5e3Sopenharmony_ci
7964514f5e3Sopenharmony_ci/* demo4 有参有返回值的函数的调用。 ts:
7974514f5e3Sopenharmony_ci * function Add(x: number, y: number): number {
7984514f5e3Sopenharmony_ci * return x + y;
7994514f5e3Sopenharmony_ci * }
8004514f5e3Sopenharmony_ci * let sum = Add(12, 34);
8014514f5e3Sopenharmony_ci * console.log(sum);
8024514f5e3Sopenharmony_ci */
8034514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo4_function_test_2)
8044514f5e3Sopenharmony_ci{
8054514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo4_function_test_2 =======================================";
8064514f5e3Sopenharmony_ci    LocalScope scope(vm_);
8074514f5e3Sopenharmony_ci    Local<FunctionRef> Add = FunctionRef::New(vm_, [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
8084514f5e3Sopenharmony_ci        EcmaVM *vm = runtimeInfo->GetVM();
8094514f5e3Sopenharmony_ci        LocalScope scope(vm);
8104514f5e3Sopenharmony_ci        // 参数的个数。
8114514f5e3Sopenharmony_ci        uint32_t argsCount = runtimeInfo->GetArgsNumber();
8124514f5e3Sopenharmony_ci        // 遍历参数。
8134514f5e3Sopenharmony_ci        for (uint32_t i = 0; i < argsCount; ++i) {
8144514f5e3Sopenharmony_ci            Local<JSValueRef> arg = runtimeInfo->GetCallArgRef(i);
8154514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "func test arg " << i << "   " << arg->Int32Value(vm);
8164514f5e3Sopenharmony_ci        }
8174514f5e3Sopenharmony_ci        // 获取前两个参数。
8184514f5e3Sopenharmony_ci        Local<JSValueRef> jsArg0 = runtimeInfo->GetCallArgRef(0);
8194514f5e3Sopenharmony_ci        Local<JSValueRef> jsArg1 = runtimeInfo->GetCallArgRef(1);
8204514f5e3Sopenharmony_ci        int arg0 = jsArg0->Int32Value(vm);
8214514f5e3Sopenharmony_ci        int arg1 = jsArg1->Int32Value(vm);
8224514f5e3Sopenharmony_ci        int sum = arg0 + arg1;
8234514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "func test sum " << sum;
8244514f5e3Sopenharmony_ci        // 参数返回值
8254514f5e3Sopenharmony_ci        return NumberRef::New(vm, sum);
8264514f5e3Sopenharmony_ci    });
8274514f5e3Sopenharmony_ci    int argv0 = 12; // random number
8284514f5e3Sopenharmony_ci    int argv1 = 34; // random number
8294514f5e3Sopenharmony_ci    Local<JSValueRef> *argv = new Local<JSValueRef>[2];
8304514f5e3Sopenharmony_ci    argv[0] = NumberRef::New(vm_, argv0);
8314514f5e3Sopenharmony_ci    argv[1] = NumberRef::New(vm_, argv1);
8324514f5e3Sopenharmony_ci    Local<JSValueRef> jsSum = Add->Call(vm_, JSValueRef::Undefined(vm_), argv, 2);
8334514f5e3Sopenharmony_ci    int sum = jsSum->Int32Value(vm_);
8344514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "func test call sum " << sum;
8354514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo4_function_test_2 ==========================================";
8364514f5e3Sopenharmony_ci}
8374514f5e3Sopenharmony_ci
8384514f5e3Sopenharmony_ciLocal<JSValueRef> AddFunc(JsiRuntimeCallInfo *runtimeInfo)
8394514f5e3Sopenharmony_ci{
8404514f5e3Sopenharmony_ci    EcmaVM *vm = runtimeInfo->GetVM();
8414514f5e3Sopenharmony_ci    LocalScope scope(vm);
8424514f5e3Sopenharmony_ci    // 参数的个数。
8434514f5e3Sopenharmony_ci    uint32_t argsCount = runtimeInfo->GetArgsNumber();
8444514f5e3Sopenharmony_ci    // 遍历参数。
8454514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < argsCount; ++i) {
8464514f5e3Sopenharmony_ci        Local<JSValueRef> arg = runtimeInfo->GetCallArgRef(i);
8474514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "func test arg " << i << "   " << arg->Int32Value(vm);
8484514f5e3Sopenharmony_ci    }
8494514f5e3Sopenharmony_ci    // 获取前两个参数。
8504514f5e3Sopenharmony_ci    Local<JSValueRef> jsArg0 = runtimeInfo->GetCallArgRef(0);
8514514f5e3Sopenharmony_ci    Local<JSValueRef> jsArg1 = runtimeInfo->GetCallArgRef(1);
8524514f5e3Sopenharmony_ci    int arg0 = jsArg0->Int32Value(vm);
8534514f5e3Sopenharmony_ci    int arg1 = jsArg1->Int32Value(vm);
8544514f5e3Sopenharmony_ci    int sum = arg0 + arg1;
8554514f5e3Sopenharmony_ci    // 参数返回值
8564514f5e3Sopenharmony_ci    return NumberRef::New(vm, sum);
8574514f5e3Sopenharmony_ci}
8584514f5e3Sopenharmony_ci
8594514f5e3Sopenharmony_ciLocal<JSValueRef> AddProxyFunc(JsiRuntimeCallInfo *runtimeInfo)
8604514f5e3Sopenharmony_ci{
8614514f5e3Sopenharmony_ci    EcmaVM *vm = runtimeInfo->GetVM();
8624514f5e3Sopenharmony_ci    LocalScope scope(vm);
8634514f5e3Sopenharmony_ci    // 参数的个数。
8644514f5e3Sopenharmony_ci    uint32_t argsCount = runtimeInfo->GetArgsNumber();
8654514f5e3Sopenharmony_ci    // 函数调用的时候的传参个数,如果不能等于这个值说明函数调用有问题。
8664514f5e3Sopenharmony_ci    uint32_t defaultArgsCount = 3;
8674514f5e3Sopenharmony_ci    if (argsCount != defaultArgsCount) {
8684514f5e3Sopenharmony_ci        return NumberRef::New(vm, 0);
8694514f5e3Sopenharmony_ci    }
8704514f5e3Sopenharmony_ci    // 函数
8714514f5e3Sopenharmony_ci    int index = 0; // 获取参数的索引。
8724514f5e3Sopenharmony_ci    Local<JSValueRef> jsArg0 = runtimeInfo->GetCallArgRef(index);
8734514f5e3Sopenharmony_ci    // 获取前两个参数。
8744514f5e3Sopenharmony_ci    index++;
8754514f5e3Sopenharmony_ci    Local<JSValueRef> jsArg1 = runtimeInfo->GetCallArgRef(index);
8764514f5e3Sopenharmony_ci    index++;
8774514f5e3Sopenharmony_ci    Local<JSValueRef> jsArg2 = runtimeInfo->GetCallArgRef(index);
8784514f5e3Sopenharmony_ci    Local<FunctionRef> addFunc = jsArg0;
8794514f5e3Sopenharmony_ci    const int addFuncArgCount = 2; // 内部调用的函数的参数个数。
8804514f5e3Sopenharmony_ci    Local<JSValueRef> argv[addFuncArgCount] = { jsArg1, jsArg2 };
8814514f5e3Sopenharmony_ci    Local<JSValueRef> jsSum = addFunc->Call(vm, JSValueRef::Undefined(vm), argv, addFuncArgCount);
8824514f5e3Sopenharmony_ci    int sum = jsSum->Int32Value(vm);
8834514f5e3Sopenharmony_ci    int multiple = 2; // 代理的功能,原函数的2倍返回。
8844514f5e3Sopenharmony_ci    sum *= multiple;
8854514f5e3Sopenharmony_ci    // 参数返回值
8864514f5e3Sopenharmony_ci    return NumberRef::New(vm, sum);
8874514f5e3Sopenharmony_ci}
8884514f5e3Sopenharmony_ci
8894514f5e3Sopenharmony_ci/* demo4 参数是函数的调用。 ts:
8904514f5e3Sopenharmony_ci * function Add(x: number, y: number): number {
8914514f5e3Sopenharmony_ci * return x + y;
8924514f5e3Sopenharmony_ci * }
8934514f5e3Sopenharmony_ci * function AddProxy(add: (x: number, y: number) => number, x: number, y: number) {
8944514f5e3Sopenharmony_ci * let sum = add(x, y);
8954514f5e3Sopenharmony_ci * return sum * 2
8964514f5e3Sopenharmony_ci * }
8974514f5e3Sopenharmony_ci * let sum1 = Add(12, 34);
8984514f5e3Sopenharmony_ci * let sum2 = AddProxy(Add,12, 34);
8994514f5e3Sopenharmony_ci * console.log(sum1);
9004514f5e3Sopenharmony_ci * console.log(sum2);
9014514f5e3Sopenharmony_ci */
9024514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo4_function_test_3)
9034514f5e3Sopenharmony_ci{
9044514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo4_function_test_3 =======================================";
9054514f5e3Sopenharmony_ci    LocalScope scope(vm_);
9064514f5e3Sopenharmony_ci    Local<FunctionRef> Add = FunctionRef::New(vm_, AddFunc);
9074514f5e3Sopenharmony_ci    Local<FunctionRef> AddProxy = FunctionRef::New(vm_, AddProxyFunc);
9084514f5e3Sopenharmony_ci    int num1 = 12; // random number
9094514f5e3Sopenharmony_ci    int num2 = 34; // random number
9104514f5e3Sopenharmony_ci    Local<JSValueRef> addArgv[2];
9114514f5e3Sopenharmony_ci    addArgv[0] = NumberRef::New(vm_, num1);
9124514f5e3Sopenharmony_ci    addArgv[1] = NumberRef::New(vm_, num2);
9134514f5e3Sopenharmony_ci    Local<JSValueRef> addProxyArgv[3];
9144514f5e3Sopenharmony_ci    addProxyArgv[0] = Add;
9154514f5e3Sopenharmony_ci    addProxyArgv[1] = NumberRef::New(vm_, num1);
9164514f5e3Sopenharmony_ci    addProxyArgv[2] = NumberRef::New(vm_, num2);
9174514f5e3Sopenharmony_ci    Local<JSValueRef> jsSum1 = Add->Call(vm_, JSValueRef::Undefined(vm_), addArgv, 2);
9184514f5e3Sopenharmony_ci    Local<JSValueRef> jsSum2 = AddProxy->Call(vm_, JSValueRef::Undefined(vm_), addProxyArgv, 3);
9194514f5e3Sopenharmony_ci    int sum1 = jsSum1->Int32Value(vm_);
9204514f5e3Sopenharmony_ci    int sum2 = jsSum2->Int32Value(vm_);
9214514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "func test call Add , sum = " << sum1;
9224514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "func test call AddProxy , sum = " << sum2;
9234514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo4_function_test_3 ==========================================";
9244514f5e3Sopenharmony_ci}
9254514f5e3Sopenharmony_ci
9264514f5e3Sopenharmony_ci/* demo5 类的静态函数和非静态函数的测试,静态变量变量 和 非静态变量的测试。 ts:
9274514f5e3Sopenharmony_ci * class Greeter {
9284514f5e3Sopenharmony_ci * // 静态变量。
9294514f5e3Sopenharmony_ci * static position:string = "door";
9304514f5e3Sopenharmony_ci * // 私有静态变量。
9314514f5e3Sopenharmony_ci * private static standardGreetingStr:string = "Hello, there";
9324514f5e3Sopenharmony_ci *
9334514f5e3Sopenharmony_ci * // 私有非静态变量。
9344514f5e3Sopenharmony_ci * private privateGreeting: string;
9354514f5e3Sopenharmony_ci * // 非静态变量。
9364514f5e3Sopenharmony_ci * greeting: string;
9374514f5e3Sopenharmony_ci *
9384514f5e3Sopenharmony_ci * // 构造函数。
9394514f5e3Sopenharmony_ci * constructor(greet: string) {
9404514f5e3Sopenharmony_ci * this.greeting = greet;
9414514f5e3Sopenharmony_ci * }
9424514f5e3Sopenharmony_ci *
9434514f5e3Sopenharmony_ci * // 非静态函数。
9444514f5e3Sopenharmony_ci * SetPrivateGreeting(priGreeting: string):void
9454514f5e3Sopenharmony_ci * {
9464514f5e3Sopenharmony_ci * this.privateGreeting = priGreeting;
9474514f5e3Sopenharmony_ci * }
9484514f5e3Sopenharmony_ci *
9494514f5e3Sopenharmony_ci * // 非静态函数调用。
9504514f5e3Sopenharmony_ci * Greet(): string {
9514514f5e3Sopenharmony_ci * if (this.privateGreeting) {
9524514f5e3Sopenharmony_ci * return "Hello, " + this.privateGreeting;
9534514f5e3Sopenharmony_ci * }else if (this.greeting) {
9544514f5e3Sopenharmony_ci * return "Hello, " + this.greeting;
9554514f5e3Sopenharmony_ci * }
9564514f5e3Sopenharmony_ci * else {
9574514f5e3Sopenharmony_ci * return Greeter.standardGreetingStr;
9584514f5e3Sopenharmony_ci * }
9594514f5e3Sopenharmony_ci * }
9604514f5e3Sopenharmony_ci *
9614514f5e3Sopenharmony_ci * // 静态函数调用。
9624514f5e3Sopenharmony_ci * static StandardGreeting(): string {
9634514f5e3Sopenharmony_ci * return Greeter.standardGreetingStr;
9644514f5e3Sopenharmony_ci * }
9654514f5e3Sopenharmony_ci *
9664514f5e3Sopenharmony_ci * // 静态函数调用。
9674514f5e3Sopenharmony_ci * static StandardPosition(): string {
9684514f5e3Sopenharmony_ci * return Greeter.position;
9694514f5e3Sopenharmony_ci * }
9704514f5e3Sopenharmony_ci *
9714514f5e3Sopenharmony_ci * }
9724514f5e3Sopenharmony_ci *
9734514f5e3Sopenharmony_ci * let greeter1: Greeter = new Greeter("everyone");
9744514f5e3Sopenharmony_ci *
9754514f5e3Sopenharmony_ci * // 非静态函数调用
9764514f5e3Sopenharmony_ci * console.log(greeter1.Greet());
9774514f5e3Sopenharmony_ci *
9784514f5e3Sopenharmony_ci * greeter1.SetPrivateGreeting("vip");
9794514f5e3Sopenharmony_ci * console.log(greeter1.Greet());
9804514f5e3Sopenharmony_ci *
9814514f5e3Sopenharmony_ci * greeter1.SetPrivateGreeting("");
9824514f5e3Sopenharmony_ci * console.log(greeter1.Greet());
9834514f5e3Sopenharmony_ci *
9844514f5e3Sopenharmony_ci * // 修改变量
9854514f5e3Sopenharmony_ci * greeter1.greeting = "";
9864514f5e3Sopenharmony_ci * console.log(greeter1.Greet());
9874514f5e3Sopenharmony_ci *
9884514f5e3Sopenharmony_ci * // 静态函数调用。
9894514f5e3Sopenharmony_ci * console.log(Greeter.StandardGreeting());
9904514f5e3Sopenharmony_ci * console.log(Greeter.StandardPosition());
9914514f5e3Sopenharmony_ci *
9924514f5e3Sopenharmony_ci * // 修改静态变量。
9934514f5e3Sopenharmony_ci * Greeter.position = "house";
9944514f5e3Sopenharmony_ci * console.log(Greeter.StandardPosition());
9954514f5e3Sopenharmony_ci */
9964514f5e3Sopenharmony_ciclass Tag {
9974514f5e3Sopenharmony_cipublic:
9984514f5e3Sopenharmony_ci    explicit Tag(const std::string name) : name_(name)
9994514f5e3Sopenharmony_ci    {
10004514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "tag construction , name is " << name_;
10014514f5e3Sopenharmony_ci    }
10024514f5e3Sopenharmony_ci    ~Tag()
10034514f5e3Sopenharmony_ci    {
10044514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "tag deconstruction , name is " << name_;
10054514f5e3Sopenharmony_ci    }
10064514f5e3Sopenharmony_ci
10074514f5e3Sopenharmony_ciprivate:
10084514f5e3Sopenharmony_ci    std::string name_;
10094514f5e3Sopenharmony_ci};
10104514f5e3Sopenharmony_ci
10114514f5e3Sopenharmony_ciclass Greeter {
10124514f5e3Sopenharmony_ciprivate:
10134514f5e3Sopenharmony_ci    // 新建ClassFunction
10144514f5e3Sopenharmony_ci    static Local<FunctionRef> NewClassFunction(EcmaVM *vm);
10154514f5e3Sopenharmony_ci
10164514f5e3Sopenharmony_ci    // 添加静态变量。
10174514f5e3Sopenharmony_ci    static void AddStaticVariable(EcmaVM *vm, Local<FunctionRef> &claFunc);
10184514f5e3Sopenharmony_ci    // 添加静态函数。
10194514f5e3Sopenharmony_ci    static void AddStaticFunction(EcmaVM *vm, Local<FunctionRef> &claFunc);
10204514f5e3Sopenharmony_ci
10214514f5e3Sopenharmony_ci    // 添加非静态变量。
10224514f5e3Sopenharmony_ci    static void AddVariable(EcmaVM *vm, Local<ObjectRef> &proto);
10234514f5e3Sopenharmony_ci    // 添加非静态函数。
10244514f5e3Sopenharmony_ci    static void AddFunction(EcmaVM *vm, Local<ObjectRef> &proto);
10254514f5e3Sopenharmony_ci
10264514f5e3Sopenharmony_cipublic:
10274514f5e3Sopenharmony_ci    // 获取ClassFunction
10284514f5e3Sopenharmony_ci    static Local<FunctionRef> GetClassFunction(EcmaVM *vm);
10294514f5e3Sopenharmony_ci    static Local<Greeter> New(EcmaVM *vm, Local<StringRef> greet);
10304514f5e3Sopenharmony_ci
10314514f5e3Sopenharmony_ci    // 非静态函数调用。
10324514f5e3Sopenharmony_ci    static void SetPrivateGreeting(EcmaVM *vm, Local<Greeter> thisRef, Local<StringRef> priGreeting);
10334514f5e3Sopenharmony_ci    static Local<StringRef> Greet(EcmaVM *vm, Local<Greeter> thisRef);
10344514f5e3Sopenharmony_ci
10354514f5e3Sopenharmony_ci    // 静态函数的调用。
10364514f5e3Sopenharmony_ci    static Local<StringRef> StandardGreeting(EcmaVM *vm);
10374514f5e3Sopenharmony_ci    static Local<StringRef> StandardPosition(EcmaVM *vm);
10384514f5e3Sopenharmony_ci
10394514f5e3Sopenharmony_ciprivate:
10404514f5e3Sopenharmony_ci    static Local<SymbolRef> standardGreetingStrKey;
10414514f5e3Sopenharmony_ci    static Local<SymbolRef> privateGreetingKey;
10424514f5e3Sopenharmony_ci
10434514f5e3Sopenharmony_ci    // 类名
10444514f5e3Sopenharmony_ci    const static std::string CLASS_NAME;
10454514f5e3Sopenharmony_ci};
10464514f5e3Sopenharmony_ci
10474514f5e3Sopenharmony_ciLocal<SymbolRef> Greeter::standardGreetingStrKey;
10484514f5e3Sopenharmony_ciLocal<SymbolRef> Greeter::privateGreetingKey;
10494514f5e3Sopenharmony_ci
10504514f5e3Sopenharmony_ciconst std::string Greeter::CLASS_NAME = "GreeterClass";
10514514f5e3Sopenharmony_ci
10524514f5e3Sopenharmony_ciLocal<FunctionRef> Greeter::NewClassFunction(EcmaVM *vm)
10534514f5e3Sopenharmony_ci{
10544514f5e3Sopenharmony_ci    // 初始化私有静态变量的key。
10554514f5e3Sopenharmony_ci    Greeter::standardGreetingStrKey = SymbolRef::New(vm, StringRef::NewFromUtf8(vm, "standardGreetingStr"));
10564514f5e3Sopenharmony_ci    Greeter::privateGreetingKey = SymbolRef::New(vm, StringRef::NewFromUtf8(vm, "privateGreeting"));
10574514f5e3Sopenharmony_ci
10584514f5e3Sopenharmony_ci    Tag *tag = new Tag("ClassFunctionTag");
10594514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = FunctionRef::NewClassFunction(
10604514f5e3Sopenharmony_ci        vm,
10614514f5e3Sopenharmony_ci        // 构造函数调用
10624514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
10634514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
10644514f5e3Sopenharmony_ci            LocalScope scope(vm);
10654514f5e3Sopenharmony_ci            // 获取this
10664514f5e3Sopenharmony_ci            Local<JSValueRef> jsThisRef = runtimeInfo->GetThisRef();
10674514f5e3Sopenharmony_ci            Local<ObjectRef> thisRef = jsThisRef->ToObject(vm);
10684514f5e3Sopenharmony_ci            // 获取参数。
10694514f5e3Sopenharmony_ci            Local<JSValueRef> greet = runtimeInfo->GetCallArgRef(0);
10704514f5e3Sopenharmony_ci            // ts: this.greeting = greet;
10714514f5e3Sopenharmony_ci            thisRef->Set(vm, StringRef::NewFromUtf8(vm, "greeting"), greet);
10724514f5e3Sopenharmony_ci            return thisRef;
10734514f5e3Sopenharmony_ci        },
10744514f5e3Sopenharmony_ci        [](void *env, void *nativePointer, void *data) {
10754514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "NewClassFunction, nativePointer is " << nativePointer;
10764514f5e3Sopenharmony_ci            Tag *t = static_cast<Tag *>(data);
10774514f5e3Sopenharmony_ci            delete t;
10784514f5e3Sopenharmony_ci        },
10794514f5e3Sopenharmony_ci        tag);
10804514f5e3Sopenharmony_ci    // static 添加 到  claFunc。
10814514f5e3Sopenharmony_ci    // 添加静态变量。
10824514f5e3Sopenharmony_ci    AddStaticVariable(vm, claFunc);
10834514f5e3Sopenharmony_ci    // 添加静态函数
10844514f5e3Sopenharmony_ci    AddStaticFunction(vm, claFunc);
10854514f5e3Sopenharmony_ci    Local<JSValueRef> jsProto = claFunc->GetFunctionPrototype(vm);
10864514f5e3Sopenharmony_ci    Local<ObjectRef> proto = jsProto->ToObject(vm);
10874514f5e3Sopenharmony_ci    // 非static 添加到 proto。
10884514f5e3Sopenharmony_ci    // 添加非静态变量
10894514f5e3Sopenharmony_ci    AddVariable(vm, proto);
10904514f5e3Sopenharmony_ci    // 添加非静态函数
10914514f5e3Sopenharmony_ci    AddFunction(vm, proto);
10924514f5e3Sopenharmony_ci    // 设置类名。
10934514f5e3Sopenharmony_ci    claFunc->SetName(vm, StringRef::NewFromUtf8(vm, Greeter::CLASS_NAME.c_str()));
10944514f5e3Sopenharmony_ci    return claFunc;
10954514f5e3Sopenharmony_ci}
10964514f5e3Sopenharmony_ci
10974514f5e3Sopenharmony_ciLocal<FunctionRef> Greeter::GetClassFunction(EcmaVM *vm)
10984514f5e3Sopenharmony_ci{
10994514f5e3Sopenharmony_ci    Local<ObjectRef> globalObj = JSNApi::GetGlobalObject(vm);
11004514f5e3Sopenharmony_ci    Local<JSValueRef> jsClaFunc = globalObj->Get(vm, StringRef::NewFromUtf8(vm, Greeter::CLASS_NAME.c_str()));
11014514f5e3Sopenharmony_ci    if (jsClaFunc->IsFunction(vm)) {
11024514f5e3Sopenharmony_ci        return jsClaFunc;
11034514f5e3Sopenharmony_ci    }
11044514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = Greeter::NewClassFunction(vm);
11054514f5e3Sopenharmony_ci    // 添加到全局。
11064514f5e3Sopenharmony_ci    globalObj->Set(vm, claFunc->GetName(vm), claFunc);
11074514f5e3Sopenharmony_ci    return claFunc;
11084514f5e3Sopenharmony_ci}
11094514f5e3Sopenharmony_ci
11104514f5e3Sopenharmony_ci// 添加静态变量。
11114514f5e3Sopenharmony_civoid Greeter::AddStaticVariable(EcmaVM *vm, Local<FunctionRef> &claFunc)
11124514f5e3Sopenharmony_ci{
11134514f5e3Sopenharmony_ci    // 静态变量。
11144514f5e3Sopenharmony_ci    // static position:string = "door";
11154514f5e3Sopenharmony_ci    claFunc->Set(vm, StringRef::NewFromUtf8(vm, "position"), StringRef::NewFromUtf8(vm, "door"));
11164514f5e3Sopenharmony_ci    // 私有静态变量。
11174514f5e3Sopenharmony_ci    // private static standardGreetingStr:string = "Hello, there";
11184514f5e3Sopenharmony_ci    claFunc->Set(vm, Greeter::standardGreetingStrKey, StringRef::NewFromUtf8(vm, "Hello, there"));
11194514f5e3Sopenharmony_ci}
11204514f5e3Sopenharmony_ci
11214514f5e3Sopenharmony_ci// 添加静态函数。
11224514f5e3Sopenharmony_civoid Greeter::AddStaticFunction(EcmaVM *vm, Local<FunctionRef> &claFunc)
11234514f5e3Sopenharmony_ci{
11244514f5e3Sopenharmony_ci    // 静态函数调用。
11254514f5e3Sopenharmony_ci    claFunc->Set(vm, StringRef::NewFromUtf8(vm, "StandardGreeting"),
11264514f5e3Sopenharmony_ci        FunctionRef::New(vm,
11274514f5e3Sopenharmony_ci        // 函数体
11284514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
11294514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
11304514f5e3Sopenharmony_ci            LocalScope scope(vm);
11314514f5e3Sopenharmony_ci            Local<FunctionRef> claFunc = Greeter::GetClassFunction(vm);
11324514f5e3Sopenharmony_ci            Local<JSValueRef> jsStandardGreetingStr = claFunc->Get(vm, Greeter::standardGreetingStrKey);
11334514f5e3Sopenharmony_ci            return jsStandardGreetingStr;
11344514f5e3Sopenharmony_ci        }));
11354514f5e3Sopenharmony_ci    // 静态函数调用。
11364514f5e3Sopenharmony_ci    claFunc->Set(vm, StringRef::NewFromUtf8(vm, "StandardPosition"),
11374514f5e3Sopenharmony_ci        FunctionRef::New(vm,
11384514f5e3Sopenharmony_ci        // 函数体
11394514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
11404514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
11414514f5e3Sopenharmony_ci            LocalScope scope(vm);
11424514f5e3Sopenharmony_ci            Local<FunctionRef> claFunc = Greeter::GetClassFunction(vm);
11434514f5e3Sopenharmony_ci            Local<JSValueRef> jsPosition = claFunc->Get(vm, StringRef::NewFromUtf8(vm, "position"));
11444514f5e3Sopenharmony_ci            return jsPosition;
11454514f5e3Sopenharmony_ci        }));
11464514f5e3Sopenharmony_ci}
11474514f5e3Sopenharmony_ci
11484514f5e3Sopenharmony_ci// 添加非静态变量。
11494514f5e3Sopenharmony_civoid Greeter::AddVariable(EcmaVM *vm, Local<ObjectRef> &proto)
11504514f5e3Sopenharmony_ci{
11514514f5e3Sopenharmony_ci    // 私有非静态变量。
11524514f5e3Sopenharmony_ci    proto->Set(vm, Greeter::privateGreetingKey, JSValueRef::Undefined(vm));
11534514f5e3Sopenharmony_ci    // 非静态变量。
11544514f5e3Sopenharmony_ci    proto->Set(vm, StringRef::NewFromUtf8(vm, "greeting"), JSValueRef::Undefined(vm));
11554514f5e3Sopenharmony_ci}
11564514f5e3Sopenharmony_ci
11574514f5e3Sopenharmony_ci// 添加非静态函数。
11584514f5e3Sopenharmony_civoid Greeter::AddFunction(EcmaVM *vm, Local<ObjectRef> &proto)
11594514f5e3Sopenharmony_ci{
11604514f5e3Sopenharmony_ci    // 非静态函数。
11614514f5e3Sopenharmony_ci    proto->Set(vm, StringRef::NewFromUtf8(vm, "SetPrivateGreeting"),
11624514f5e3Sopenharmony_ci        FunctionRef::New(vm,
11634514f5e3Sopenharmony_ci        // 函数体
11644514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
11654514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
11664514f5e3Sopenharmony_ci            LocalScope scope(vm);
11674514f5e3Sopenharmony_ci            // 获取this
11684514f5e3Sopenharmony_ci            Local<JSValueRef> jsThisRef = runtimeInfo->GetThisRef();
11694514f5e3Sopenharmony_ci            Local<ObjectRef> thisRef = jsThisRef->ToObject(vm);
11704514f5e3Sopenharmony_ci            // 获取参数。
11714514f5e3Sopenharmony_ci            Local<JSValueRef> priGreeting = runtimeInfo->GetCallArgRef(0);
11724514f5e3Sopenharmony_ci            thisRef->Set(vm, Greeter::privateGreetingKey, priGreeting);
11734514f5e3Sopenharmony_ci            return JSValueRef::Undefined(vm);
11744514f5e3Sopenharmony_ci        }));
11754514f5e3Sopenharmony_ci    // 非静态函数。
11764514f5e3Sopenharmony_ci    proto->Set(vm, StringRef::NewFromUtf8(vm, "Greet"),
11774514f5e3Sopenharmony_ci        FunctionRef::New(vm,
11784514f5e3Sopenharmony_ci        // 函数体
11794514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
11804514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
11814514f5e3Sopenharmony_ci            LocalScope scope(vm);
11824514f5e3Sopenharmony_ci            // 获取类
11834514f5e3Sopenharmony_ci            Local<FunctionRef> claFunc = Greeter::GetClassFunction(vm);
11844514f5e3Sopenharmony_ci            // 获取this
11854514f5e3Sopenharmony_ci            Local<JSValueRef> jsThisRef = runtimeInfo->GetThisRef();
11864514f5e3Sopenharmony_ci            Local<ObjectRef> thisRef = jsThisRef->ToObject(vm);
11874514f5e3Sopenharmony_ci            Local<JSValueRef> jsPrivateGreeting = thisRef->Get(vm, Greeter::privateGreetingKey);
11884514f5e3Sopenharmony_ci            Local<JSValueRef> jsGreeting = thisRef->Get(vm, StringRef::NewFromUtf8(vm, "greeting"));
11894514f5e3Sopenharmony_ci            Local<JSValueRef> jsStandardGreetingStr = claFunc->Get(vm, Greeter::standardGreetingStrKey);
11904514f5e3Sopenharmony_ci            std::string ret;
11914514f5e3Sopenharmony_ci            if (jsPrivateGreeting->IsString(vm)) {
11924514f5e3Sopenharmony_ci                ret.append("Hello, ").append(jsPrivateGreeting->ToString(vm)->ToString(vm));
11934514f5e3Sopenharmony_ci            } else if (jsGreeting->IsString(vm)) {
11944514f5e3Sopenharmony_ci                ret.append("Hello, ").append(jsGreeting->ToString(vm)->ToString(vm));
11954514f5e3Sopenharmony_ci            } else {
11964514f5e3Sopenharmony_ci                ret.append(jsStandardGreetingStr->ToString(vm)->ToString(vm));
11974514f5e3Sopenharmony_ci            }
11984514f5e3Sopenharmony_ci            return StringRef::NewFromUtf8(vm, ret.c_str(), ret.size());
11994514f5e3Sopenharmony_ci        }));
12004514f5e3Sopenharmony_ci}
12014514f5e3Sopenharmony_ci
12024514f5e3Sopenharmony_ciLocal<Greeter> Greeter::New(EcmaVM *vm, Local<StringRef> greet)
12034514f5e3Sopenharmony_ci{
12044514f5e3Sopenharmony_ci    // 获取类函数。
12054514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = Greeter::GetClassFunction(vm);
12064514f5e3Sopenharmony_ci    // 定义参数。
12074514f5e3Sopenharmony_ci    Local<JSValueRef> argv[1] = {greet};
12084514f5e3Sopenharmony_ci    // 构造函数,构造对象。
12094514f5e3Sopenharmony_ci    Local<JSValueRef> jsObj = claFunc->Constructor(vm, argv, 1);
12104514f5e3Sopenharmony_ci    Local<ObjectRef> obj = jsObj->ToObject(vm);
12114514f5e3Sopenharmony_ci    return obj;
12124514f5e3Sopenharmony_ci}
12134514f5e3Sopenharmony_ci
12144514f5e3Sopenharmony_ci/* // 非静态函数调用。 ts:
12154514f5e3Sopenharmony_ci * SetPrivateGreeting(priGreeting: string):void
12164514f5e3Sopenharmony_ci */
12174514f5e3Sopenharmony_civoid Greeter::SetPrivateGreeting(EcmaVM *vm, Local<Greeter> thisRef, Local<StringRef> priGreeting)
12184514f5e3Sopenharmony_ci{
12194514f5e3Sopenharmony_ci    Local<ObjectRef> objRef = thisRef;
12204514f5e3Sopenharmony_ci    Local<FunctionRef> func = objRef->Get(vm, StringRef::NewFromUtf8(vm, "SetPrivateGreeting"));
12214514f5e3Sopenharmony_ci    Local<JSValueRef> argv [1] = {priGreeting};
12224514f5e3Sopenharmony_ci    func->Call(vm, objRef, argv, 1);
12234514f5e3Sopenharmony_ci}
12244514f5e3Sopenharmony_ci
12254514f5e3Sopenharmony_ci/* // 非静态函数调用。 ts:
12264514f5e3Sopenharmony_ci * Greet(): string
12274514f5e3Sopenharmony_ci */
12284514f5e3Sopenharmony_ciLocal<StringRef> Greeter::Greet(EcmaVM *vm, Local<Greeter> thisRef)
12294514f5e3Sopenharmony_ci{
12304514f5e3Sopenharmony_ci    Local<ObjectRef> objRef = thisRef;
12314514f5e3Sopenharmony_ci    Local<FunctionRef> func = objRef->Get(vm, StringRef::NewFromUtf8(vm, "Greet"));
12324514f5e3Sopenharmony_ci    return func->Call(vm, objRef, nullptr, 0);
12334514f5e3Sopenharmony_ci}
12344514f5e3Sopenharmony_ci
12354514f5e3Sopenharmony_ci/* // 静态函数的调用。 ts:
12364514f5e3Sopenharmony_ci * static StandardGreeting(): string
12374514f5e3Sopenharmony_ci */
12384514f5e3Sopenharmony_ciLocal<StringRef> Greeter::StandardGreeting(EcmaVM *vm)
12394514f5e3Sopenharmony_ci{
12404514f5e3Sopenharmony_ci    // 获取类函数。
12414514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = Greeter::GetClassFunction(vm);
12424514f5e3Sopenharmony_ci    // 获取函数
12434514f5e3Sopenharmony_ci    Local<FunctionRef> func = claFunc->Get(vm, StringRef::NewFromUtf8(vm, "StandardGreeting"));
12444514f5e3Sopenharmony_ci    // 调用函数
12454514f5e3Sopenharmony_ci    return func->Call(vm, JSValueRef::Undefined(vm), nullptr, 0);
12464514f5e3Sopenharmony_ci}
12474514f5e3Sopenharmony_ci
12484514f5e3Sopenharmony_ci/* // 静态函数的调用。ts:
12494514f5e3Sopenharmony_ci * static StandardPosition(): string
12504514f5e3Sopenharmony_ci */
12514514f5e3Sopenharmony_ciLocal<StringRef> Greeter::StandardPosition(EcmaVM *vm)
12524514f5e3Sopenharmony_ci{
12534514f5e3Sopenharmony_ci    // 获取类函数。
12544514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = Greeter::GetClassFunction(vm);
12554514f5e3Sopenharmony_ci    // 获取函数
12564514f5e3Sopenharmony_ci    Local<FunctionRef> func = claFunc->Get(vm, StringRef::NewFromUtf8(vm, "StandardPosition"));
12574514f5e3Sopenharmony_ci    // 调用函数
12584514f5e3Sopenharmony_ci    return func->Call(vm, JSValueRef::Undefined(vm), nullptr, 0);
12594514f5e3Sopenharmony_ci}
12604514f5e3Sopenharmony_ci
12614514f5e3Sopenharmony_ci/* 类的调用测试: ts:
12624514f5e3Sopenharmony_ci * let greeter1: Greeter = new Greeter("everyone");
12634514f5e3Sopenharmony_ci *
12644514f5e3Sopenharmony_ci * // 非静态函数调用
12654514f5e3Sopenharmony_ci * console.log(greeter1.Greet());
12664514f5e3Sopenharmony_ci *
12674514f5e3Sopenharmony_ci * greeter1.SetPrivateGreeting("vip");
12684514f5e3Sopenharmony_ci * console.log(greeter1.Greet());
12694514f5e3Sopenharmony_ci *
12704514f5e3Sopenharmony_ci * greeter1.SetPrivateGreeting("");
12714514f5e3Sopenharmony_ci * console.log(greeter1.Greet());
12724514f5e3Sopenharmony_ci *
12734514f5e3Sopenharmony_ci * // 修改变量
12744514f5e3Sopenharmony_ci * greeter1.greeting = "";
12754514f5e3Sopenharmony_ci * console.log(greeter1.Greet());
12764514f5e3Sopenharmony_ci *
12774514f5e3Sopenharmony_ci * // 静态函数调用。
12784514f5e3Sopenharmony_ci * console.log(Greeter.StandardGreeting());
12794514f5e3Sopenharmony_ci * console.log(Greeter.StandardPosition());
12804514f5e3Sopenharmony_ci *
12814514f5e3Sopenharmony_ci * // 修改静态变量。
12824514f5e3Sopenharmony_ci * Greeter.position = "house";
12834514f5e3Sopenharmony_ci * console.log(Greeter.StandardPosition());
12844514f5e3Sopenharmony_ci */
12854514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo5_class_test)
12864514f5e3Sopenharmony_ci{
12874514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo5_class_test =======================================";
12884514f5e3Sopenharmony_ci    LocalScope scope(vm_);
12894514f5e3Sopenharmony_ci    Local<Greeter> greeter1 = Greeter::New(vm_, StringRef::NewFromUtf8(vm_, "everyone"));
12904514f5e3Sopenharmony_ci    // 非静态函数调用
12914514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << Greeter::Greet(vm_, greeter1)->ToString(vm_);
12924514f5e3Sopenharmony_ci    Greeter::SetPrivateGreeting(vm_, greeter1, StringRef::NewFromUtf8(vm_, "vip"));
12934514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << Greeter::Greet(vm_, greeter1)->ToString(vm_);
12944514f5e3Sopenharmony_ci    Greeter::SetPrivateGreeting(vm_, greeter1, JSValueRef::Undefined(vm_));
12954514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << Greeter::Greet(vm_, greeter1)->ToString(vm_);
12964514f5e3Sopenharmony_ci    // 修改变量
12974514f5e3Sopenharmony_ci    Local<ObjectRef> objRef1 = greeter1;
12984514f5e3Sopenharmony_ci    objRef1->Set(vm_, StringRef::NewFromUtf8(vm_, "greeting"), JSValueRef::Undefined(vm_));
12994514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << Greeter::Greet(vm_, greeter1)->ToString(vm_);
13004514f5e3Sopenharmony_ci
13014514f5e3Sopenharmony_ci    // 静态函数调用。
13024514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << Greeter::StandardGreeting(vm_)->ToString(vm_);
13034514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << Greeter::StandardPosition(vm_)->ToString(vm_);
13044514f5e3Sopenharmony_ci
13054514f5e3Sopenharmony_ci    // 修改静态变量。
13064514f5e3Sopenharmony_ci    Local<FunctionRef> classFunc = Greeter::GetClassFunction(vm_);
13074514f5e3Sopenharmony_ci    classFunc->Set(vm_, StringRef::NewFromUtf8(vm_, "position"), StringRef::NewFromUtf8(vm_, "house"));
13084514f5e3Sopenharmony_ci
13094514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << Greeter::StandardPosition(vm_)->ToString(vm_);
13104514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo5_class_test =======================================";
13114514f5e3Sopenharmony_ci}
13124514f5e3Sopenharmony_ci
13134514f5e3Sopenharmony_ci/* demo6 多态 ts:
13144514f5e3Sopenharmony_ci * // 基类
13154514f5e3Sopenharmony_ci * class Derive {
13164514f5e3Sopenharmony_ci * baseNum: number = 1
13174514f5e3Sopenharmony_ci * constructor(num: number){
13184514f5e3Sopenharmony_ci * this.baseNum = num
13194514f5e3Sopenharmony_ci * }
13204514f5e3Sopenharmony_ci * Compute(): number {
13214514f5e3Sopenharmony_ci * return this.baseNum
13224514f5e3Sopenharmony_ci * }
13234514f5e3Sopenharmony_ci * }
13244514f5e3Sopenharmony_ci * // 子类1
13254514f5e3Sopenharmony_ci * class DeriveDouble extends Derive {
13264514f5e3Sopenharmony_ci * constructor(num: number){
13274514f5e3Sopenharmony_ci * super(num);
13284514f5e3Sopenharmony_ci * }
13294514f5e3Sopenharmony_ci * Compute() : number {
13304514f5e3Sopenharmony_ci * return this.baseNum * 2
13314514f5e3Sopenharmony_ci * }
13324514f5e3Sopenharmony_ci * }
13334514f5e3Sopenharmony_ci * // 子类2
13344514f5e3Sopenharmony_ci * class DerivedTriple extends Derive {
13354514f5e3Sopenharmony_ci * constructor(num: number){
13364514f5e3Sopenharmony_ci * super(num);
13374514f5e3Sopenharmony_ci * }
13384514f5e3Sopenharmony_ci * Compute() : number {
13394514f5e3Sopenharmony_ci * return this.baseNum * 3
13404514f5e3Sopenharmony_ci * }
13414514f5e3Sopenharmony_ci * }
13424514f5e3Sopenharmony_ci *
13434514f5e3Sopenharmony_ci * // 测试:
13444514f5e3Sopenharmony_ci * let d1: Derive;
13454514f5e3Sopenharmony_ci * let d2: Derive;
13464514f5e3Sopenharmony_ci * let d3: Derive;
13474514f5e3Sopenharmony_ci * d1 = new Derive(5);//新建基类。
13484514f5e3Sopenharmony_ci * d2 = new DeriveDouble(5);//新建子类。
13494514f5e3Sopenharmony_ci * d3 = new DerivedTriple(5);//新建子类。
13504514f5e3Sopenharmony_ci * let i1:number = d1.Compute();
13514514f5e3Sopenharmony_ci * let i2:number = d2.Compute();
13524514f5e3Sopenharmony_ci * let i3:number = d3.Compute();
13534514f5e3Sopenharmony_ci * console.log(i1);
13544514f5e3Sopenharmony_ci * console.log(i2);
13554514f5e3Sopenharmony_ci * console.log(i3);
13564514f5e3Sopenharmony_ci */
13574514f5e3Sopenharmony_ci
13584514f5e3Sopenharmony_ci/* 基类 ts:
13594514f5e3Sopenharmony_ci * class Derive {
13604514f5e3Sopenharmony_ci * baseNum: number = 1
13614514f5e3Sopenharmony_ci * constructor(num: number){
13624514f5e3Sopenharmony_ci * this.baseNum = num
13634514f5e3Sopenharmony_ci * }
13644514f5e3Sopenharmony_ci * Compute(): number {
13654514f5e3Sopenharmony_ci * return this.baseNum
13664514f5e3Sopenharmony_ci * }
13674514f5e3Sopenharmony_ci * }
13684514f5e3Sopenharmony_ci */
13694514f5e3Sopenharmony_ciclass Derive {
13704514f5e3Sopenharmony_ciprivate:
13714514f5e3Sopenharmony_ci    // 新建ClassFunction
13724514f5e3Sopenharmony_ci    static Local<FunctionRef> NewClassFunction(EcmaVM *vm);
13734514f5e3Sopenharmony_ci
13744514f5e3Sopenharmony_cipublic:
13754514f5e3Sopenharmony_ci    // 获取ClassFunction
13764514f5e3Sopenharmony_ci    static Local<FunctionRef> GetClassFunction(EcmaVM *vm);
13774514f5e3Sopenharmony_ci    static Local<Derive> New(EcmaVM *vm, Local<NumberRef> num);
13784514f5e3Sopenharmony_ci
13794514f5e3Sopenharmony_ci    static Local<NumberRef> Compute(EcmaVM *vm, Local<Derive> thisRef);
13804514f5e3Sopenharmony_ci
13814514f5e3Sopenharmony_ciprivate:
13824514f5e3Sopenharmony_ci    const static std::string CLASS_NAME;
13834514f5e3Sopenharmony_ci};
13844514f5e3Sopenharmony_ci
13854514f5e3Sopenharmony_ciconst std::string Derive::CLASS_NAME = "DeriveClass";
13864514f5e3Sopenharmony_ci
13874514f5e3Sopenharmony_ciLocal<FunctionRef> Derive::NewClassFunction(EcmaVM *vm)
13884514f5e3Sopenharmony_ci{
13894514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = FunctionRef::NewClassFunction(
13904514f5e3Sopenharmony_ci        vm,
13914514f5e3Sopenharmony_ci        // 构造函数调用
13924514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
13934514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
13944514f5e3Sopenharmony_ci            LocalScope scope(vm);
13954514f5e3Sopenharmony_ci            // 获取this
13964514f5e3Sopenharmony_ci            Local<JSValueRef> jsThisRef = runtimeInfo->GetThisRef();
13974514f5e3Sopenharmony_ci            Local<ObjectRef> thisRef = jsThisRef->ToObject(vm);
13984514f5e3Sopenharmony_ci            // 获取参数。
13994514f5e3Sopenharmony_ci            Local<JSValueRef> num = runtimeInfo->GetCallArgRef(0);
14004514f5e3Sopenharmony_ci            // this.baseNum = num
14014514f5e3Sopenharmony_ci            thisRef->Set(vm, StringRef::NewFromUtf8(vm, "baseNum"), num);
14024514f5e3Sopenharmony_ci            return thisRef;
14034514f5e3Sopenharmony_ci        },
14044514f5e3Sopenharmony_ci        nullptr, nullptr);
14054514f5e3Sopenharmony_ci    Local<JSValueRef> jsProto = claFunc->GetFunctionPrototype(vm);
14064514f5e3Sopenharmony_ci    Local<ObjectRef> proto = jsProto->ToObject(vm);
14074514f5e3Sopenharmony_ci    // 添加非静态变量
14084514f5e3Sopenharmony_ci    proto->Set(vm, StringRef::NewFromUtf8(vm, "baseNum"), NumberRef::New(vm, 1));
14094514f5e3Sopenharmony_ci    // 添加非静态函数
14104514f5e3Sopenharmony_ci    proto->Set(vm, StringRef::NewFromUtf8(vm, "Compute"),
14114514f5e3Sopenharmony_ci        FunctionRef::New(vm,
14124514f5e3Sopenharmony_ci        // 函数体
14134514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
14144514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
14154514f5e3Sopenharmony_ci            LocalScope scope(vm);
14164514f5e3Sopenharmony_ci
14174514f5e3Sopenharmony_ci            // 获取this
14184514f5e3Sopenharmony_ci            Local<JSValueRef> jsThisRef = runtimeInfo->GetThisRef();
14194514f5e3Sopenharmony_ci            Local<ObjectRef> thisRef = jsThisRef->ToObject(vm);
14204514f5e3Sopenharmony_ci
14214514f5e3Sopenharmony_ci            Local<JSValueRef> jsNum = thisRef->Get(vm, StringRef::NewFromUtf8(vm, "baseNum"));
14224514f5e3Sopenharmony_ci            int num = jsNum->Int32Value(vm);
14234514f5e3Sopenharmony_ci            return NumberRef::New(vm, num);
14244514f5e3Sopenharmony_ci        }));
14254514f5e3Sopenharmony_ci    // 设置类名。
14264514f5e3Sopenharmony_ci    claFunc->SetName(vm, StringRef::NewFromUtf8(vm, Derive::CLASS_NAME.c_str()));
14274514f5e3Sopenharmony_ci    return claFunc;
14284514f5e3Sopenharmony_ci}
14294514f5e3Sopenharmony_ci
14304514f5e3Sopenharmony_ciLocal<FunctionRef> Derive::GetClassFunction(EcmaVM *vm)
14314514f5e3Sopenharmony_ci{
14324514f5e3Sopenharmony_ci    Local<ObjectRef> globalObj = JSNApi::GetGlobalObject(vm);
14334514f5e3Sopenharmony_ci    Local<JSValueRef> jsClaFunc = globalObj->Get(vm, StringRef::NewFromUtf8(vm, Derive::CLASS_NAME.c_str()));
14344514f5e3Sopenharmony_ci    if (jsClaFunc->IsFunction(vm)) {
14354514f5e3Sopenharmony_ci        return jsClaFunc;
14364514f5e3Sopenharmony_ci    }
14374514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = Derive::NewClassFunction(vm);
14384514f5e3Sopenharmony_ci    // 添加到全局。
14394514f5e3Sopenharmony_ci    globalObj->Set(vm, claFunc->GetName(vm), claFunc);
14404514f5e3Sopenharmony_ci    return claFunc;
14414514f5e3Sopenharmony_ci}
14424514f5e3Sopenharmony_ci
14434514f5e3Sopenharmony_ciLocal<Derive> Derive::New(EcmaVM *vm, Local<NumberRef> num)
14444514f5e3Sopenharmony_ci{
14454514f5e3Sopenharmony_ci    // 获取类函数。
14464514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = Derive::GetClassFunction(vm);
14474514f5e3Sopenharmony_ci    // 定义参数。
14484514f5e3Sopenharmony_ci    Local<JSValueRef> argv[1] = {num};
14494514f5e3Sopenharmony_ci    // 构造函数,构造对象。
14504514f5e3Sopenharmony_ci    Local<JSValueRef> jsObj = claFunc->Constructor(vm, argv, 1);
14514514f5e3Sopenharmony_ci    Local<ObjectRef> obj = jsObj->ToObject(vm);
14524514f5e3Sopenharmony_ci    return obj;
14534514f5e3Sopenharmony_ci}
14544514f5e3Sopenharmony_ci
14554514f5e3Sopenharmony_ciLocal<NumberRef> Derive::Compute(EcmaVM *vm, Local<Derive> thisRef)
14564514f5e3Sopenharmony_ci{
14574514f5e3Sopenharmony_ci    Local<ObjectRef> objRef = thisRef;
14584514f5e3Sopenharmony_ci    Local<FunctionRef> func = objRef->Get(vm, StringRef::NewFromUtf8(vm, "Compute"));
14594514f5e3Sopenharmony_ci    return func->Call(vm, objRef, nullptr, 0);
14604514f5e3Sopenharmony_ci}
14614514f5e3Sopenharmony_ci
14624514f5e3Sopenharmony_ci/* 子类1 ts:
14634514f5e3Sopenharmony_ci * class DeriveDouble extends Base {
14644514f5e3Sopenharmony_ci * constructor(num: number){
14654514f5e3Sopenharmony_ci * super(num);
14664514f5e3Sopenharmony_ci * }
14674514f5e3Sopenharmony_ci * Compute() : number {
14684514f5e3Sopenharmony_ci * return this.baseNum * 2
14694514f5e3Sopenharmony_ci * }
14704514f5e3Sopenharmony_ci * }
14714514f5e3Sopenharmony_ci */
14724514f5e3Sopenharmony_ciclass DeriveDouble : public Derive {
14734514f5e3Sopenharmony_ciprivate:
14744514f5e3Sopenharmony_ci    DeriveDouble() = default;
14754514f5e3Sopenharmony_ci    // 新建ClassFunction
14764514f5e3Sopenharmony_ci    static Local<FunctionRef> NewClassFunction(EcmaVM *vm);
14774514f5e3Sopenharmony_ci
14784514f5e3Sopenharmony_cipublic:
14794514f5e3Sopenharmony_ci    // 获取ClassFunction
14804514f5e3Sopenharmony_ci    static Local<FunctionRef> GetClassFunction(EcmaVM *vm);
14814514f5e3Sopenharmony_ci    static Local<DeriveDouble> New(EcmaVM *vm, Local<NumberRef> num);
14824514f5e3Sopenharmony_ci    ~DeriveDouble() = default;
14834514f5e3Sopenharmony_ci
14844514f5e3Sopenharmony_cipublic:
14854514f5e3Sopenharmony_ci    // 设置类名
14864514f5e3Sopenharmony_ci    const static std::string CLASS_NAME;
14874514f5e3Sopenharmony_ci};
14884514f5e3Sopenharmony_ci
14894514f5e3Sopenharmony_ciconst std::string DeriveDouble::CLASS_NAME = "DeriveDoubleClass";
14904514f5e3Sopenharmony_ci
14914514f5e3Sopenharmony_ciLocal<FunctionRef> DeriveDouble::NewClassFunction(EcmaVM *vm)
14924514f5e3Sopenharmony_ci{
14934514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = FunctionRef::NewClassFunction(
14944514f5e3Sopenharmony_ci        vm,
14954514f5e3Sopenharmony_ci        // 构造函数调用
14964514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
14974514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
14984514f5e3Sopenharmony_ci            LocalScope scope(vm);
14994514f5e3Sopenharmony_ci            // 获取参数。
15004514f5e3Sopenharmony_ci            Local<JSValueRef> num = runtimeInfo->GetCallArgRef(0);
15014514f5e3Sopenharmony_ci            // 获取this
15024514f5e3Sopenharmony_ci            Local<JSValueRef> jsThisRef = runtimeInfo->GetThisRef();
15034514f5e3Sopenharmony_ci            Local<ObjectRef> thisRef = jsThisRef->ToObject(vm);
15044514f5e3Sopenharmony_ci            // 修改父类。
15054514f5e3Sopenharmony_ci            thisRef->Set(vm, StringRef::NewFromUtf8(vm, "baseNum"), num);
15064514f5e3Sopenharmony_ci            return thisRef;
15074514f5e3Sopenharmony_ci        },
15084514f5e3Sopenharmony_ci        nullptr, nullptr);
15094514f5e3Sopenharmony_ci    // 设置类名。
15104514f5e3Sopenharmony_ci    claFunc->SetName(vm, StringRef::NewFromUtf8(vm, DeriveDouble::CLASS_NAME.c_str()));
15114514f5e3Sopenharmony_ci
15124514f5e3Sopenharmony_ci    // 添加非静态函数
15134514f5e3Sopenharmony_ci    Local<ObjectRef> proto = claFunc->GetFunctionPrototype(vm)->ToObject(vm);
15144514f5e3Sopenharmony_ci    proto->Set(vm, StringRef::NewFromUtf8(vm, "Compute"),
15154514f5e3Sopenharmony_ci        FunctionRef::New(vm,
15164514f5e3Sopenharmony_ci        // 函数体
15174514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
15184514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
15194514f5e3Sopenharmony_ci            LocalScope scope(vm);
15204514f5e3Sopenharmony_ci            // 获取this
15214514f5e3Sopenharmony_ci            Local<JSValueRef> jsThisRef = runtimeInfo->GetThisRef();
15224514f5e3Sopenharmony_ci            Local<ObjectRef> thisRef = jsThisRef->ToObject(vm);
15234514f5e3Sopenharmony_ci            Local<JSValueRef> jsNum = thisRef->Get(vm, StringRef::NewFromUtf8(vm, "baseNum"));
15244514f5e3Sopenharmony_ci            int num = jsNum->Int32Value(vm);
15254514f5e3Sopenharmony_ci            int multiple = 2; // 函数功能,2倍返回。
15264514f5e3Sopenharmony_ci            num *= multiple;
15274514f5e3Sopenharmony_ci            return NumberRef::New(vm, num);
15284514f5e3Sopenharmony_ci        }));
15294514f5e3Sopenharmony_ci    // 父类。
15304514f5e3Sopenharmony_ci    Local<FunctionRef> claFuncBase = Derive::GetClassFunction(vm);
15314514f5e3Sopenharmony_ci    // 继承。
15324514f5e3Sopenharmony_ci    claFunc->Inherit(vm, claFuncBase);
15334514f5e3Sopenharmony_ci    return claFunc;
15344514f5e3Sopenharmony_ci}
15354514f5e3Sopenharmony_ci
15364514f5e3Sopenharmony_ciLocal<FunctionRef> DeriveDouble::GetClassFunction(EcmaVM *vm)
15374514f5e3Sopenharmony_ci{
15384514f5e3Sopenharmony_ci    Local<ObjectRef> globalObj = JSNApi::GetGlobalObject(vm);
15394514f5e3Sopenharmony_ci    Local<JSValueRef> jsClaFunc = globalObj->Get(vm, StringRef::NewFromUtf8(vm, DeriveDouble::CLASS_NAME.c_str()));
15404514f5e3Sopenharmony_ci    if (jsClaFunc->IsFunction(vm)) {
15414514f5e3Sopenharmony_ci        return jsClaFunc;
15424514f5e3Sopenharmony_ci    }
15434514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = DeriveDouble::NewClassFunction(vm);
15444514f5e3Sopenharmony_ci    globalObj->Set(vm, claFunc->GetName(vm), claFunc);
15454514f5e3Sopenharmony_ci    return claFunc;
15464514f5e3Sopenharmony_ci}
15474514f5e3Sopenharmony_ci
15484514f5e3Sopenharmony_ciLocal<DeriveDouble> DeriveDouble::New(EcmaVM *vm, Local<NumberRef> num)
15494514f5e3Sopenharmony_ci{
15504514f5e3Sopenharmony_ci    // 获取类函数。
15514514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = DeriveDouble::GetClassFunction(vm);
15524514f5e3Sopenharmony_ci    // 定义参数。
15534514f5e3Sopenharmony_ci    Local<JSValueRef> argv[1] = {num};
15544514f5e3Sopenharmony_ci    // 构造函数,构造对象。
15554514f5e3Sopenharmony_ci    Local<JSValueRef> jsObj = claFunc->Constructor(vm, argv, 1);
15564514f5e3Sopenharmony_ci    Local<ObjectRef> obj = jsObj->ToObject(vm);
15574514f5e3Sopenharmony_ci    return obj;
15584514f5e3Sopenharmony_ci}
15594514f5e3Sopenharmony_ci
15604514f5e3Sopenharmony_ci/* 子类2 ts:
15614514f5e3Sopenharmony_ci * class DerivedTriple extends Derive {
15624514f5e3Sopenharmony_ci * constructor(num: number){
15634514f5e3Sopenharmony_ci * super(num);
15644514f5e3Sopenharmony_ci * }
15654514f5e3Sopenharmony_ci * Compute() : number {
15664514f5e3Sopenharmony_ci * return this.baseNum * 3
15674514f5e3Sopenharmony_ci * }
15684514f5e3Sopenharmony_ci * }
15694514f5e3Sopenharmony_ci */
15704514f5e3Sopenharmony_ciclass DerivedTriple : public Derive {
15714514f5e3Sopenharmony_ciprivate:
15724514f5e3Sopenharmony_ci    DerivedTriple() = default;
15734514f5e3Sopenharmony_ci    // 新建ClassFunction
15744514f5e3Sopenharmony_ci    static Local<FunctionRef> NewClassFunction(EcmaVM *vm);
15754514f5e3Sopenharmony_ci
15764514f5e3Sopenharmony_cipublic:
15774514f5e3Sopenharmony_ci    // 获取ClassFunction
15784514f5e3Sopenharmony_ci    static Local<FunctionRef> GetClassFunction(EcmaVM *vm);
15794514f5e3Sopenharmony_ci    static Local<DerivedTriple> New(EcmaVM *vm, Local<NumberRef> num);
15804514f5e3Sopenharmony_ci    ~DerivedTriple() = default;
15814514f5e3Sopenharmony_ci
15824514f5e3Sopenharmony_cipublic:
15834514f5e3Sopenharmony_ci    // 设置类名
15844514f5e3Sopenharmony_ci    const static std::string CLASS_NAME;
15854514f5e3Sopenharmony_ci};
15864514f5e3Sopenharmony_ci
15874514f5e3Sopenharmony_ciconst std::string DerivedTriple::CLASS_NAME = "DerivedTripleClass";
15884514f5e3Sopenharmony_ci
15894514f5e3Sopenharmony_ciLocal<FunctionRef> DerivedTriple::NewClassFunction(EcmaVM *vm)
15904514f5e3Sopenharmony_ci{
15914514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = FunctionRef::NewClassFunction(
15924514f5e3Sopenharmony_ci        vm,
15934514f5e3Sopenharmony_ci        // 构造函数调用
15944514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
15954514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
15964514f5e3Sopenharmony_ci            LocalScope scope(vm);
15974514f5e3Sopenharmony_ci            // 获取参数。
15984514f5e3Sopenharmony_ci            Local<JSValueRef> num = runtimeInfo->GetCallArgRef(0);
15994514f5e3Sopenharmony_ci            // 获取this
16004514f5e3Sopenharmony_ci            Local<JSValueRef> jsThisRef = runtimeInfo->GetThisRef();
16014514f5e3Sopenharmony_ci            Local<ObjectRef> thisRef = jsThisRef->ToObject(vm);
16024514f5e3Sopenharmony_ci            // 修改父类。
16034514f5e3Sopenharmony_ci            thisRef->Set(vm, StringRef::NewFromUtf8(vm, "baseNum"), num);
16044514f5e3Sopenharmony_ci            return thisRef;
16054514f5e3Sopenharmony_ci        },
16064514f5e3Sopenharmony_ci        nullptr, nullptr);
16074514f5e3Sopenharmony_ci    // 设置类名。
16084514f5e3Sopenharmony_ci    claFunc->SetName(vm, StringRef::NewFromUtf8(vm, DerivedTriple::CLASS_NAME.c_str()));
16094514f5e3Sopenharmony_ci
16104514f5e3Sopenharmony_ci    // 添加非静态函数
16114514f5e3Sopenharmony_ci    Local<ObjectRef> proto = claFunc->GetFunctionPrototype(vm)->ToObject(vm);
16124514f5e3Sopenharmony_ci    proto->Set(vm, StringRef::NewFromUtf8(vm, "Compute"),
16134514f5e3Sopenharmony_ci        FunctionRef::New(vm,
16144514f5e3Sopenharmony_ci        // 函数体
16154514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
16164514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
16174514f5e3Sopenharmony_ci            LocalScope scope(vm);
16184514f5e3Sopenharmony_ci            // 获取this
16194514f5e3Sopenharmony_ci            Local<JSValueRef> jsThisRef = runtimeInfo->GetThisRef();
16204514f5e3Sopenharmony_ci            Local<ObjectRef> thisRef = jsThisRef->ToObject(vm);
16214514f5e3Sopenharmony_ci            Local<JSValueRef> jsNum = thisRef->Get(vm, StringRef::NewFromUtf8(vm, "baseNum"));
16224514f5e3Sopenharmony_ci            int num = jsNum->Int32Value(vm);
16234514f5e3Sopenharmony_ci            int multiple = 3; // 函数功能,3倍返回。
16244514f5e3Sopenharmony_ci            num *= multiple;
16254514f5e3Sopenharmony_ci            return NumberRef::New(vm, num);
16264514f5e3Sopenharmony_ci        }));
16274514f5e3Sopenharmony_ci    // 父类。
16284514f5e3Sopenharmony_ci    Local<FunctionRef> claFuncBase = Derive::GetClassFunction(vm);
16294514f5e3Sopenharmony_ci    // 继承。
16304514f5e3Sopenharmony_ci    claFunc->Inherit(vm, claFuncBase);
16314514f5e3Sopenharmony_ci    return claFunc;
16324514f5e3Sopenharmony_ci}
16334514f5e3Sopenharmony_ci
16344514f5e3Sopenharmony_ciLocal<FunctionRef> DerivedTriple::GetClassFunction(EcmaVM *vm)
16354514f5e3Sopenharmony_ci{
16364514f5e3Sopenharmony_ci    Local<ObjectRef> globalObj = JSNApi::GetGlobalObject(vm);
16374514f5e3Sopenharmony_ci    Local<JSValueRef> jsClaFunc = globalObj->Get(vm, StringRef::NewFromUtf8(vm, DerivedTriple::CLASS_NAME.c_str()));
16384514f5e3Sopenharmony_ci    if (jsClaFunc->IsFunction(vm)) {
16394514f5e3Sopenharmony_ci        return jsClaFunc;
16404514f5e3Sopenharmony_ci    }
16414514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = DerivedTriple::NewClassFunction(vm);
16424514f5e3Sopenharmony_ci    globalObj->Set(vm, claFunc->GetName(vm), claFunc);
16434514f5e3Sopenharmony_ci    return claFunc;
16444514f5e3Sopenharmony_ci}
16454514f5e3Sopenharmony_ci
16464514f5e3Sopenharmony_ciLocal<DerivedTriple> DerivedTriple::New(EcmaVM *vm, Local<NumberRef> num)
16474514f5e3Sopenharmony_ci{
16484514f5e3Sopenharmony_ci    // 获取类函数。
16494514f5e3Sopenharmony_ci    Local<FunctionRef> claFunc = DerivedTriple::GetClassFunction(vm);
16504514f5e3Sopenharmony_ci    // 定义参数。
16514514f5e3Sopenharmony_ci    Local<JSValueRef> argv[1] = {num};
16524514f5e3Sopenharmony_ci    // 构造函数,构造对象。
16534514f5e3Sopenharmony_ci    Local<JSValueRef> jsObj = claFunc->Constructor(vm, argv, 1);
16544514f5e3Sopenharmony_ci    Local<ObjectRef> obj = jsObj->ToObject(vm);
16554514f5e3Sopenharmony_ci    return obj;
16564514f5e3Sopenharmony_ci}
16574514f5e3Sopenharmony_ci
16584514f5e3Sopenharmony_ci/* 测试。ts:
16594514f5e3Sopenharmony_ci * let d1: Derive;
16604514f5e3Sopenharmony_ci * let d2: Derive;
16614514f5e3Sopenharmony_ci * let d3: Derive;
16624514f5e3Sopenharmony_ci * d1 = new Derive(5);//新建基类。
16634514f5e3Sopenharmony_ci * d2 = new DeriveDouble(5);//新建子类。
16644514f5e3Sopenharmony_ci * d3 = new DerivedTriple(5);//新建子类。
16654514f5e3Sopenharmony_ci * let i1:number = d1.Compute();
16664514f5e3Sopenharmony_ci * let i2:number = d2.Compute();
16674514f5e3Sopenharmony_ci * let i3:number = d3.Compute();
16684514f5e3Sopenharmony_ci * console.log(i1);
16694514f5e3Sopenharmony_ci * console.log(i2);
16704514f5e3Sopenharmony_ci * console.log(i3);
16714514f5e3Sopenharmony_ci */
16724514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo6_polymorphic_test)
16734514f5e3Sopenharmony_ci{
16744514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo6_polymorphic_test =======================================";
16754514f5e3Sopenharmony_ci    LocalScope scope(vm_);
16764514f5e3Sopenharmony_ci
16774514f5e3Sopenharmony_ci    int num = 5;
16784514f5e3Sopenharmony_ci
16794514f5e3Sopenharmony_ci    Local<Derive> d1 = Derive::New(vm_, NumberRef::New(vm_, num));
16804514f5e3Sopenharmony_ci    Local<Derive> d2 = DeriveDouble::New(vm_, NumberRef::New(vm_, num));
16814514f5e3Sopenharmony_ci    Local<Derive> d3 = DerivedTriple::New(vm_, NumberRef::New(vm_, num));
16824514f5e3Sopenharmony_ci
16834514f5e3Sopenharmony_ci    Local<NumberRef> i1 = Derive::Compute(vm_, d1);
16844514f5e3Sopenharmony_ci    Local<NumberRef> i2 = Derive::Compute(vm_, d2);
16854514f5e3Sopenharmony_ci    Local<NumberRef> i3 = Derive::Compute(vm_, d3);
16864514f5e3Sopenharmony_ci
16874514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "i1 = " << i1->Int32Value(vm_);
16884514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "i2 = " << i2->Int32Value(vm_);
16894514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "i3 = " << i3->Int32Value(vm_);
16904514f5e3Sopenharmony_ci
16914514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo6_polymorphic_test =======================================";
16924514f5e3Sopenharmony_ci}
16934514f5e3Sopenharmony_ci
16944514f5e3Sopenharmony_ci/* demo7 数组的使用 */
16954514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_ArrayRef_Int)
16964514f5e3Sopenharmony_ci{
16974514f5e3Sopenharmony_ci    LocalScope scope(vm_);
16984514f5e3Sopenharmony_ci    uint32_t length = 5; // array length = 5
16994514f5e3Sopenharmony_ci    Local<ArrayRef> arrayObject = ArrayRef::New(vm_, length);
17004514f5e3Sopenharmony_ci    uint32_t arrayLength = arrayObject->Length(vm_);
17014514f5e3Sopenharmony_ci    uint32_t index = 1; // array index = 0
17024514f5e3Sopenharmony_ci    for (int i = 0; i < (int)arrayLength; i++) {
17034514f5e3Sopenharmony_ci        Local<IntegerRef> intValue = IntegerRef::New(vm_, i * 10);
17044514f5e3Sopenharmony_ci        ArrayRef::SetValueAt(vm_, arrayObject, i, intValue);
17054514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_setIntValue_index_" << i << ": " << intValue->Value();
17064514f5e3Sopenharmony_ci    }
17074514f5e3Sopenharmony_ci    Local<IntegerRef> resultInt = ArrayRef::GetValueAt(vm_, arrayObject, index);
17084514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_getIntValue_index_1: " << resultInt->Value();
17094514f5e3Sopenharmony_ci    int inputInt = 99; // int data = 99
17104514f5e3Sopenharmony_ci    Local<IntegerRef> intValue = IntegerRef::New(vm_, inputInt);
17114514f5e3Sopenharmony_ci    bool setResult = ArrayRef::SetValueAt(vm_, arrayObject, arrayLength, intValue);
17124514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_setIntValue_index_5: " << intValue->Value() << " setResult: " << setResult;
17134514f5e3Sopenharmony_ci    int changedInt = 66; // change data = 66
17144514f5e3Sopenharmony_ci    Local<IntegerRef> changedIntValue = IntegerRef::New(vm_, changedInt);
17154514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arrayObject, index, changedIntValue);
17164514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_changedIntValue_index_1: " << changedIntValue->Value();
17174514f5e3Sopenharmony_ci    Local<IntegerRef> resultChangedInt = ArrayRef::GetValueAt(vm_, arrayObject, index);
17184514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_getChangedIntValue_index_1: " << resultChangedInt->Value();
17194514f5e3Sopenharmony_ci    for (int i = 0; i < (int)arrayLength; i++) {
17204514f5e3Sopenharmony_ci        Local<IntegerRef> printInt = ArrayRef::GetValueAt(vm_, arrayObject, i);
17214514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_printIntValue_index_" << i << ": " << printInt->Value();
17224514f5e3Sopenharmony_ci    }
17234514f5e3Sopenharmony_ci}
17244514f5e3Sopenharmony_ci
17254514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_ArrayRef_Bool)
17264514f5e3Sopenharmony_ci{
17274514f5e3Sopenharmony_ci    LocalScope scope(vm_);
17284514f5e3Sopenharmony_ci    uint32_t length = 5; // array length = 5
17294514f5e3Sopenharmony_ci    Local<ArrayRef> arrayObject = ArrayRef::New(vm_, length);
17304514f5e3Sopenharmony_ci    uint32_t arrayLength = arrayObject->Length(vm_);
17314514f5e3Sopenharmony_ci    bool inputBool = true; // bool data = true
17324514f5e3Sopenharmony_ci    uint32_t index = 0;    // array index = 0
17334514f5e3Sopenharmony_ci    Local<BooleanRef> boolValue = BooleanRef::New(vm_, inputBool);
17344514f5e3Sopenharmony_ci    for (int i = 0; i < (int)arrayLength; i++) {
17354514f5e3Sopenharmony_ci        ArrayRef::SetValueAt(vm_, arrayObject, i, boolValue);
17364514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_setBoolValue_index_" << i << ": " << boolValue->Value();
17374514f5e3Sopenharmony_ci    }
17384514f5e3Sopenharmony_ci    Local<BooleanRef> resultBool = ArrayRef::GetValueAt(vm_, arrayObject, index);
17394514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_getBoolValue_index_0: " << resultBool->Value();
17404514f5e3Sopenharmony_ci    bool setResult = ArrayRef::SetValueAt(vm_, arrayObject, arrayLength, boolValue);
17414514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_setBoolValue_index_5: " << boolValue->Value() << " setResult: " << setResult;
17424514f5e3Sopenharmony_ci    bool changedBool = false; // change data = false
17434514f5e3Sopenharmony_ci    Local<BooleanRef> changedBoolValue = BooleanRef::New(vm_, changedBool);
17444514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arrayObject, index, changedBoolValue);
17454514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_changedBoolValue_index_0: " << changedBoolValue->Value();
17464514f5e3Sopenharmony_ci    Local<BooleanRef> resultChangedBool = ArrayRef::GetValueAt(vm_, arrayObject, index);
17474514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_getChangedBoolValue_index_0: " << resultChangedBool->Value();
17484514f5e3Sopenharmony_ci    for (int i = 0; i < (int)arrayLength; i++) {
17494514f5e3Sopenharmony_ci        Local<BooleanRef> printBool = ArrayRef::GetValueAt(vm_, arrayObject, i);
17504514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_printBoolValue_index_" << i << ": " << printBool->Value();
17514514f5e3Sopenharmony_ci    }
17524514f5e3Sopenharmony_ci}
17534514f5e3Sopenharmony_ci
17544514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_ArrayRef_Number)
17554514f5e3Sopenharmony_ci{
17564514f5e3Sopenharmony_ci    LocalScope scope(vm_);
17574514f5e3Sopenharmony_ci    uint32_t length = 5; // array length = 5
17584514f5e3Sopenharmony_ci    Local<ArrayRef> arrayObject = ArrayRef::New(vm_, length);
17594514f5e3Sopenharmony_ci    uint32_t arrayLength = arrayObject->Length(vm_);
17604514f5e3Sopenharmony_ci    uint32_t inputNumber = 40; // number data = 40
17614514f5e3Sopenharmony_ci    uint32_t index = 0;        // array index = 0
17624514f5e3Sopenharmony_ci    Local<NumberRef> numberValue = NumberRef::New(vm_, inputNumber);
17634514f5e3Sopenharmony_ci    for (int i = 0; i < (int)arrayLength; i++) {
17644514f5e3Sopenharmony_ci        ArrayRef::SetValueAt(vm_, arrayObject, i, numberValue);
17654514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_setNumberValue_index_" << i << ": " << numberValue->Value();
17664514f5e3Sopenharmony_ci    }
17674514f5e3Sopenharmony_ci    Local<NumberRef> resultNumber = ArrayRef::GetValueAt(vm_, arrayObject, index);
17684514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_getNumberValue_index_0: " << resultNumber->Value();
17694514f5e3Sopenharmony_ci    bool setResult = ArrayRef::SetValueAt(vm_, arrayObject, arrayLength, numberValue);
17704514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_setNumberValue_index_5: " << numberValue->Value() << " setResult: " << setResult;
17714514f5e3Sopenharmony_ci    uint32_t changedNumber = 50; // change data = 50
17724514f5e3Sopenharmony_ci    Local<NumberRef> changedNumberValue = NumberRef::New(vm_, changedNumber);
17734514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arrayObject, index, changedNumberValue);
17744514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_changedNumberValue_index_0: " << changedNumberValue->Value();
17754514f5e3Sopenharmony_ci    Local<NumberRef> resultChangedNumber = ArrayRef::GetValueAt(vm_, arrayObject, index);
17764514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_getChangedNumberValue_index_0: " << resultChangedNumber->Value();
17774514f5e3Sopenharmony_ci    for (int i = 0; i < (int)arrayLength; i++) {
17784514f5e3Sopenharmony_ci        Local<NumberRef> printNumber = ArrayRef::GetValueAt(vm_, arrayObject, i);
17794514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_printNumberValue_index_" << i << ": " << printNumber->Value();
17804514f5e3Sopenharmony_ci    }
17814514f5e3Sopenharmony_ci}
17824514f5e3Sopenharmony_ci
17834514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_ArrayRef_String)
17844514f5e3Sopenharmony_ci{
17854514f5e3Sopenharmony_ci    LocalScope scope(vm_);
17864514f5e3Sopenharmony_ci    uint32_t length = 5; // array length = 5
17874514f5e3Sopenharmony_ci    Local<ArrayRef> arrayObject = ArrayRef::New(vm_, length);
17884514f5e3Sopenharmony_ci    uint32_t arrayLength = arrayObject->Length(vm_);
17894514f5e3Sopenharmony_ci    std::string inputString = "Sample Test";
17904514f5e3Sopenharmony_ci    uint32_t index = 0; // array index = 0
17914514f5e3Sopenharmony_ci    Local<StringRef> stringValue = StringRef::NewFromUtf8(vm_, inputString.c_str());
17924514f5e3Sopenharmony_ci    for (int i = 0; i < (int)arrayLength; i++) {
17934514f5e3Sopenharmony_ci        ArrayRef::SetValueAt(vm_, arrayObject, i, stringValue);
17944514f5e3Sopenharmony_ci        char setBuffer[20];
17954514f5e3Sopenharmony_ci        stringValue->WriteUtf8(vm_, setBuffer, inputString.length());
17964514f5e3Sopenharmony_ci        std::string result(setBuffer);
17974514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_setStringValue_index_" << i << ": " << result;
17984514f5e3Sopenharmony_ci        memset_s(setBuffer, sizeof(setBuffer), 0, sizeof(setBuffer));
17994514f5e3Sopenharmony_ci    }
18004514f5e3Sopenharmony_ci    Local<StringRef> resultString = ArrayRef::GetValueAt(vm_, arrayObject, index);
18014514f5e3Sopenharmony_ci    char getBuffer[20];
18024514f5e3Sopenharmony_ci    resultString->WriteUtf8(vm_, getBuffer, inputString.length());
18034514f5e3Sopenharmony_ci    std::string getResult(getBuffer);
18044514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_getStringValue_index_0: " << getResult;
18054514f5e3Sopenharmony_ci    bool setResult = ArrayRef::SetValueAt(vm_, arrayObject, arrayLength, stringValue);
18064514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_setStringValue_index_5"
18074514f5e3Sopenharmony_ci                     << " setResult: " << setResult;
18084514f5e3Sopenharmony_ci    std::string changedString = "Change Test";
18094514f5e3Sopenharmony_ci    Local<StringRef> changedStringValue = StringRef::NewFromUtf8(vm_, changedString.c_str());
18104514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arrayObject, index, changedStringValue);
18114514f5e3Sopenharmony_ci    Local<StringRef> resultChangedString = ArrayRef::GetValueAt(vm_, arrayObject, index);
18124514f5e3Sopenharmony_ci    char changedBuffer[20];
18134514f5e3Sopenharmony_ci    resultChangedString->WriteUtf8(vm_, changedBuffer, changedString.length());
18144514f5e3Sopenharmony_ci    std::string changedResult(changedBuffer);
18154514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_getChangedStringValue_index_0: " << changedResult;
18164514f5e3Sopenharmony_ci    for (int i = 0; i < (int)arrayLength; i++) {
18174514f5e3Sopenharmony_ci        Local<StringRef> printString = ArrayRef::GetValueAt(vm_, arrayObject, i);
18184514f5e3Sopenharmony_ci        char printBuffer[20];
18194514f5e3Sopenharmony_ci        printString->WriteUtf8(vm_, printBuffer, inputString.length());
18204514f5e3Sopenharmony_ci        std::string printResult(printBuffer);
18214514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_printStringValue_index_" << i << ": " << printResult;
18224514f5e3Sopenharmony_ci        memset_s(printBuffer, sizeof(printBuffer), 0, sizeof(printBuffer));
18234514f5e3Sopenharmony_ci    }
18244514f5e3Sopenharmony_ci}
18254514f5e3Sopenharmony_ci
18264514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_Int8Array)
18274514f5e3Sopenharmony_ci{
18284514f5e3Sopenharmony_ci    LocalScope scope(vm_);
18294514f5e3Sopenharmony_ci    const int32_t length = 15; // arraybuffer length = 15
18304514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
18314514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
18324514f5e3Sopenharmony_ci    int8_t *ptr = (int8_t *)arrayBuffer->GetBuffer(vm_);
18334514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength; i++) {
18344514f5e3Sopenharmony_ci        *ptr = int8_t(i + 10);
18354514f5e3Sopenharmony_ci        ptr++;
18364514f5e3Sopenharmony_ci    }
18374514f5e3Sopenharmony_ci    int32_t byteOffset = 5;      // byte offset = 5
18384514f5e3Sopenharmony_ci    int32_t int8ArrayLength = 6; // array length = 6
18394514f5e3Sopenharmony_ci    Local<Int8ArrayRef> typedArray = Int8ArrayRef::New(vm_, arrayBuffer, byteOffset, int8ArrayLength);
18404514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Int8Array_byteLength : " << typedArray->ByteLength(vm_);
18414514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Int8Array_byteOffset : " << typedArray->ByteOffset(vm_);
18424514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Int8Array_arrayLength : " << typedArray->ArrayLength(vm_);
18434514f5e3Sopenharmony_ci    int8_t *result = (int8_t *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
18444514f5e3Sopenharmony_ci    for (int i = 0; i < int8ArrayLength; i++) {
18454514f5e3Sopenharmony_ci        int value = int8_t(*result);
18464514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_Int8Array_getInt8ArrayValue : " << value;
18474514f5e3Sopenharmony_ci        result++;
18484514f5e3Sopenharmony_ci    }
18494514f5e3Sopenharmony_ci}
18504514f5e3Sopenharmony_ci
18514514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_Uint8Array)
18524514f5e3Sopenharmony_ci{
18534514f5e3Sopenharmony_ci    LocalScope scope(vm_);
18544514f5e3Sopenharmony_ci    const int32_t length = 15; // arraybuffer length = 15
18554514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
18564514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
18574514f5e3Sopenharmony_ci    uint8_t *ptr = (uint8_t *)arrayBuffer->GetBuffer(vm_);
18584514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength; i++) {
18594514f5e3Sopenharmony_ci        *ptr = uint8_t(i + 10);
18604514f5e3Sopenharmony_ci        ptr++;
18614514f5e3Sopenharmony_ci    }
18624514f5e3Sopenharmony_ci    int32_t byteOffset = 5;       // byte offset = 5
18634514f5e3Sopenharmony_ci    int32_t Uint8ArrayLength = 6; // array length = 6
18644514f5e3Sopenharmony_ci    Local<Uint8ArrayRef> typedArray = Uint8ArrayRef::New(vm_, arrayBuffer, byteOffset, Uint8ArrayLength);
18654514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint8Array_byteLength : " << typedArray->ByteLength(vm_);
18664514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint8Array_byteOffset : " << typedArray->ByteOffset(vm_);
18674514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint8Array_arrayLength : " << typedArray->ArrayLength(vm_);
18684514f5e3Sopenharmony_ci    uint8_t *result = (uint8_t *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
18694514f5e3Sopenharmony_ci    for (int i = 0; i < Uint8ArrayLength; i++) {
18704514f5e3Sopenharmony_ci        int value = uint8_t(*result);
18714514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_Uint8Array_getUint8ArrayValue : " << value;
18724514f5e3Sopenharmony_ci        result++;
18734514f5e3Sopenharmony_ci    }
18744514f5e3Sopenharmony_ci}
18754514f5e3Sopenharmony_ci
18764514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_Uint8ClampedArray)
18774514f5e3Sopenharmony_ci{
18784514f5e3Sopenharmony_ci    LocalScope scope(vm_);
18794514f5e3Sopenharmony_ci    const int32_t length = 15; // arraybuffer length = 15
18804514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
18814514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
18824514f5e3Sopenharmony_ci    uint8_t *ptr = (uint8_t *)arrayBuffer->GetBuffer(vm_);
18834514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength; i++) {
18844514f5e3Sopenharmony_ci        *ptr = uint8_t(i + 10);
18854514f5e3Sopenharmony_ci        ptr++;
18864514f5e3Sopenharmony_ci    }
18874514f5e3Sopenharmony_ci    int32_t byteOffset = 5;     // byte offset = 5
18884514f5e3Sopenharmony_ci    int32_t uint8ArrLength = 6; // array length = 6
18894514f5e3Sopenharmony_ci    Local<Uint8ClampedArrayRef> typedArray = Uint8ClampedArrayRef::New(vm_, arrayBuffer, byteOffset, uint8ArrLength);
18904514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint8ClampedArray_byteLength : " << typedArray->ByteLength(vm_);
18914514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint8ClampedArray_byteOffset : " << typedArray->ByteOffset(vm_);
18924514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint8ClampedArray_arrayLength : " << typedArray->ArrayLength(vm_);
18934514f5e3Sopenharmony_ci    uint8_t *result = (uint8_t *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
18944514f5e3Sopenharmony_ci    for (int i = 0; i < uint8ArrLength; i++) {
18954514f5e3Sopenharmony_ci        int value = uint8_t(*result);
18964514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_Uint8ClampedArray_getUint8ClampedArrayValue : " << value;
18974514f5e3Sopenharmony_ci        result++;
18984514f5e3Sopenharmony_ci    }
18994514f5e3Sopenharmony_ci}
19004514f5e3Sopenharmony_ci
19014514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_Int16Array)
19024514f5e3Sopenharmony_ci{
19034514f5e3Sopenharmony_ci    LocalScope scope(vm_);
19044514f5e3Sopenharmony_ci    const int32_t length = 30; // arraybuffer length = 30
19054514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
19064514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
19074514f5e3Sopenharmony_ci    int16_t *ptr = (int16_t *)arrayBuffer->GetBuffer(vm_);
19084514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength / 2; i++) {
19094514f5e3Sopenharmony_ci        *ptr = int16_t(i + 10);
19104514f5e3Sopenharmony_ci        ptr++;
19114514f5e3Sopenharmony_ci    }
19124514f5e3Sopenharmony_ci    int32_t byteOffset = 4;       // byte offset = 4
19134514f5e3Sopenharmony_ci    int32_t int16ArrayLength = 6; // array length = 6
19144514f5e3Sopenharmony_ci    Local<Int16ArrayRef> typedArray = Int16ArrayRef::New(vm_, arrayBuffer, byteOffset, int16ArrayLength);
19154514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Int16Array_byteLength : " << typedArray->ByteLength(vm_);
19164514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Int16Array_byteOffset : " << typedArray->ByteOffset(vm_);
19174514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Int16Array_arrayLength : " << typedArray->ArrayLength(vm_);
19184514f5e3Sopenharmony_ci    int16_t *result = (int16_t *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
19194514f5e3Sopenharmony_ci    for (int i = 0; i < int16ArrayLength; i++) {
19204514f5e3Sopenharmony_ci        int value = int16_t(*result);
19214514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_Int16Array_getInt16ArrayValue : " << value;
19224514f5e3Sopenharmony_ci        result++;
19234514f5e3Sopenharmony_ci    }
19244514f5e3Sopenharmony_ci}
19254514f5e3Sopenharmony_ci
19264514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_Uint16Array)
19274514f5e3Sopenharmony_ci{
19284514f5e3Sopenharmony_ci    LocalScope scope(vm_);
19294514f5e3Sopenharmony_ci    const int32_t length = 30; // arraybuffer length = 30
19304514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
19314514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
19324514f5e3Sopenharmony_ci    uint16_t *ptr = (uint16_t *)arrayBuffer->GetBuffer(vm_);
19334514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength / 2; i++) {
19344514f5e3Sopenharmony_ci        *ptr = uint16_t(i + 10);
19354514f5e3Sopenharmony_ci        ptr++;
19364514f5e3Sopenharmony_ci    }
19374514f5e3Sopenharmony_ci    int32_t byteOffset = 4;        // byte offset = 4
19384514f5e3Sopenharmony_ci    int32_t uint16ArrayLength = 6; // array length = 6
19394514f5e3Sopenharmony_ci    Local<Uint16ArrayRef> typedArray = Uint16ArrayRef::New(vm_, arrayBuffer, byteOffset, uint16ArrayLength);
19404514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint16Array_byteLength : " << typedArray->ByteLength(vm_);
19414514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint16Array_byteOffset : " << typedArray->ByteOffset(vm_);
19424514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint16Array_arrayLength : " << typedArray->ArrayLength(vm_);
19434514f5e3Sopenharmony_ci    uint16_t *result = (uint16_t *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
19444514f5e3Sopenharmony_ci    for (int i = 0; i < uint16ArrayLength; i++) {
19454514f5e3Sopenharmony_ci        int value = uint16_t(*result);
19464514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_Uint16Array_getUint16ArrayValue : " << value;
19474514f5e3Sopenharmony_ci        result++;
19484514f5e3Sopenharmony_ci    }
19494514f5e3Sopenharmony_ci}
19504514f5e3Sopenharmony_ci
19514514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_Int32Array)
19524514f5e3Sopenharmony_ci{
19534514f5e3Sopenharmony_ci    LocalScope scope(vm_);
19544514f5e3Sopenharmony_ci    const int32_t length = 32; // arraybuffer length = 32
19554514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
19564514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
19574514f5e3Sopenharmony_ci    int32_t *ptr = (int32_t *)arrayBuffer->GetBuffer(vm_);
19584514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength / 4; i++) {
19594514f5e3Sopenharmony_ci        *ptr = int32_t(i + 10);
19604514f5e3Sopenharmony_ci        ptr++;
19614514f5e3Sopenharmony_ci    }
19624514f5e3Sopenharmony_ci    int32_t byteOffset = 4;       // byte offset = 4
19634514f5e3Sopenharmony_ci    int32_t int32ArrayLength = 6; // array length = 6
19644514f5e3Sopenharmony_ci    Local<Int32ArrayRef> typedArray = Int32ArrayRef::New(vm_, arrayBuffer, byteOffset, int32ArrayLength);
19654514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Int32Array_byteLength : " << typedArray->ByteLength(vm_);
19664514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Int32Array_byteOffset : " << typedArray->ByteOffset(vm_);
19674514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Int32Array_arrayLength : " << typedArray->ArrayLength(vm_);
19684514f5e3Sopenharmony_ci    int32_t *result = (int32_t *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
19694514f5e3Sopenharmony_ci    for (int i = 0; i < int32ArrayLength; i++) {
19704514f5e3Sopenharmony_ci        int value = int32_t(*result);
19714514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_Int32Array_getInt32ArrayValue : " << value;
19724514f5e3Sopenharmony_ci        result++;
19734514f5e3Sopenharmony_ci    }
19744514f5e3Sopenharmony_ci}
19754514f5e3Sopenharmony_ci
19764514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_Uint32Array)
19774514f5e3Sopenharmony_ci{
19784514f5e3Sopenharmony_ci    LocalScope scope(vm_);
19794514f5e3Sopenharmony_ci    const int32_t length = 32; // arraybuffer length = 32
19804514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
19814514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
19824514f5e3Sopenharmony_ci    uint32_t *ptr = (uint32_t *)arrayBuffer->GetBuffer(vm_);
19834514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength / 4; i++) {
19844514f5e3Sopenharmony_ci        *ptr = uint32_t(i + 10);
19854514f5e3Sopenharmony_ci        ptr++;
19864514f5e3Sopenharmony_ci    }
19874514f5e3Sopenharmony_ci    int32_t byteOffset = 4;        // byte offset = 4
19884514f5e3Sopenharmony_ci    int32_t uint32ArrayLength = 6; // array length = 6
19894514f5e3Sopenharmony_ci    Local<Uint32ArrayRef> typedArray = Uint32ArrayRef::New(vm_, arrayBuffer, byteOffset, uint32ArrayLength);
19904514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint32Array_byteLength : " << typedArray->ByteLength(vm_);
19914514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint32Array_byteOffset : " << typedArray->ByteOffset(vm_);
19924514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Uint32Array_arrayLength : " << typedArray->ArrayLength(vm_);
19934514f5e3Sopenharmony_ci    uint32_t *result = (uint32_t *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
19944514f5e3Sopenharmony_ci    for (int i = 0; i < uint32ArrayLength; i++) {
19954514f5e3Sopenharmony_ci        int value = uint32_t(*result);
19964514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_Uint32Array_getUint32ArrayValue : " << value;
19974514f5e3Sopenharmony_ci        result++;
19984514f5e3Sopenharmony_ci    }
19994514f5e3Sopenharmony_ci}
20004514f5e3Sopenharmony_ci
20014514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_Float32Array)
20024514f5e3Sopenharmony_ci{
20034514f5e3Sopenharmony_ci    LocalScope scope(vm_);
20044514f5e3Sopenharmony_ci    const int32_t length = 32; // arraybuffer length = 32
20054514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
20064514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
20074514f5e3Sopenharmony_ci    float *ptr = (float *)arrayBuffer->GetBuffer(vm_);
20084514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength / 4; i++) {
20094514f5e3Sopenharmony_ci        *ptr = float(i + 10);
20104514f5e3Sopenharmony_ci        ptr++;
20114514f5e3Sopenharmony_ci    }
20124514f5e3Sopenharmony_ci    int32_t byteOffset = 4;         // byte offset = 4
20134514f5e3Sopenharmony_ci    int32_t float32ArrayLength = 6; // array length = 6
20144514f5e3Sopenharmony_ci    Local<Float32ArrayRef> typedArray = Float32ArrayRef::New(vm_, arrayBuffer, byteOffset, float32ArrayLength);
20154514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Float32Array_byteLength : " << typedArray->ByteLength(vm_);
20164514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Float32Array_byteOffset : " << typedArray->ByteOffset(vm_);
20174514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Float32Array_arrayLength : " << typedArray->ArrayLength(vm_);
20184514f5e3Sopenharmony_ci    float *result = (float *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
20194514f5e3Sopenharmony_ci    for (int i = 0; i < float32ArrayLength; i++) {
20204514f5e3Sopenharmony_ci        int value = float(*result);
20214514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_Float32Array_getFloat32ArrayValue : " << value;
20224514f5e3Sopenharmony_ci        result++;
20234514f5e3Sopenharmony_ci    }
20244514f5e3Sopenharmony_ci}
20254514f5e3Sopenharmony_ci
20264514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_Float64Array)
20274514f5e3Sopenharmony_ci{
20284514f5e3Sopenharmony_ci    LocalScope scope(vm_);
20294514f5e3Sopenharmony_ci    const int32_t length = 64; // arraybuffer length = 64
20304514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
20314514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
20324514f5e3Sopenharmony_ci    double *ptr = (double *)arrayBuffer->GetBuffer(vm_);
20334514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength / 8; i++) {
20344514f5e3Sopenharmony_ci        *ptr = double(i + 10);
20354514f5e3Sopenharmony_ci        ptr++;
20364514f5e3Sopenharmony_ci    }
20374514f5e3Sopenharmony_ci    int32_t byteOffset = 8;         // byte offset = 8
20384514f5e3Sopenharmony_ci    int32_t float64ArrayLength = 6; // array length = 6
20394514f5e3Sopenharmony_ci    Local<Float64ArrayRef> typedArray = Float64ArrayRef::New(vm_, arrayBuffer, byteOffset, float64ArrayLength);
20404514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Float64Array_byteLength : " << typedArray->ByteLength(vm_);
20414514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Float64Array_byteOffset : " << typedArray->ByteOffset(vm_);
20424514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Float64Array_arrayLength : " << typedArray->ArrayLength(vm_);
20434514f5e3Sopenharmony_ci    double *result = (double *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
20444514f5e3Sopenharmony_ci    for (int i = 0; i < float64ArrayLength; i++) {
20454514f5e3Sopenharmony_ci        int value = double(*result);
20464514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_Float64Array_getFloat64ArrayValue : " << value;
20474514f5e3Sopenharmony_ci        result++;
20484514f5e3Sopenharmony_ci    }
20494514f5e3Sopenharmony_ci}
20504514f5e3Sopenharmony_ci
20514514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_BigInt64Array)
20524514f5e3Sopenharmony_ci{
20534514f5e3Sopenharmony_ci    LocalScope scope(vm_);
20544514f5e3Sopenharmony_ci    const int32_t length = 64; // arraybuffer length = 64
20554514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
20564514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
20574514f5e3Sopenharmony_ci    int64_t *ptr = (int64_t *)arrayBuffer->GetBuffer(vm_);
20584514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength / 8; i++) {
20594514f5e3Sopenharmony_ci        *ptr = int64_t(i * 100);
20604514f5e3Sopenharmony_ci        ptr++;
20614514f5e3Sopenharmony_ci    }
20624514f5e3Sopenharmony_ci    int32_t byteOffset = 8;          // byte offset = 8
20634514f5e3Sopenharmony_ci    int32_t bigInt64ArrayLength = 6; // array length = 6
20644514f5e3Sopenharmony_ci    Local<BigInt64ArrayRef> typedArray = BigInt64ArrayRef::New(vm_, arrayBuffer, byteOffset, bigInt64ArrayLength);
20654514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_BigInt64Array_byteLength : " << typedArray->ByteLength(vm_);
20664514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_BigInt64Array_byteOffset : " << typedArray->ByteOffset(vm_);
20674514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_BigInt64Array_arrayLength : " << typedArray->ArrayLength(vm_);
20684514f5e3Sopenharmony_ci    int64_t *result = (int64_t *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
20694514f5e3Sopenharmony_ci    for (int i = 0; i < bigInt64ArrayLength; i++) {
20704514f5e3Sopenharmony_ci        int value = int64_t(*result);
20714514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_BigInt64Array_getBigInt64ArrayValue : " << value;
20724514f5e3Sopenharmony_ci        result++;
20734514f5e3Sopenharmony_ci    }
20744514f5e3Sopenharmony_ci}
20754514f5e3Sopenharmony_ci
20764514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_TypedArrayRef_BigUint64Array)
20774514f5e3Sopenharmony_ci{
20784514f5e3Sopenharmony_ci    LocalScope scope(vm_);
20794514f5e3Sopenharmony_ci    const int32_t length = 64; // arraybuffer length = 64
20804514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
20814514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
20824514f5e3Sopenharmony_ci    uint64_t *ptr = (uint64_t *)arrayBuffer->GetBuffer(vm_);
20834514f5e3Sopenharmony_ci    for (int i = 0; i < arrayLength / 8; i++) {
20844514f5e3Sopenharmony_ci        *ptr = int64_t(i * 100);
20854514f5e3Sopenharmony_ci        ptr++;
20864514f5e3Sopenharmony_ci    }
20874514f5e3Sopenharmony_ci    int32_t byteOffset = 8;           // byte offset = 8
20884514f5e3Sopenharmony_ci    int32_t bigUint64ArrayLength = 6; // array length = 6
20894514f5e3Sopenharmony_ci    Local<BigUint64ArrayRef> typedArray = BigUint64ArrayRef::New(vm_, arrayBuffer, byteOffset, bigUint64ArrayLength);
20904514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_BigUint64Array_byteLength : " << typedArray->ByteLength(vm_);
20914514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_BigUint64Array_byteOffset : " << typedArray->ByteOffset(vm_);
20924514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_BigUint64Array_arrayLength : " << typedArray->ArrayLength(vm_);
20934514f5e3Sopenharmony_ci    uint64_t *result = (uint64_t *)typedArray->GetArrayBuffer(vm_)->GetBuffer(vm_);
20944514f5e3Sopenharmony_ci    for (int i = 0; i < bigUint64ArrayLength; i++) {
20954514f5e3Sopenharmony_ci        int value = uint64_t(*result);
20964514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "sample_BigUint64Array_getBigUint64ArrayValue : " << value;
20974514f5e3Sopenharmony_ci        result++;
20984514f5e3Sopenharmony_ci    }
20994514f5e3Sopenharmony_ci}
21004514f5e3Sopenharmony_ci
21014514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_DataViewRef)
21024514f5e3Sopenharmony_ci{
21034514f5e3Sopenharmony_ci    LocalScope scope(vm_);
21044514f5e3Sopenharmony_ci    const int32_t length = 15; // arraybuffer length = 15
21054514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
21064514f5e3Sopenharmony_ci    int32_t byteOffset = 5;     // byte offset = 5
21074514f5e3Sopenharmony_ci    int32_t dataViewLength = 7; // dataview length = 7
21084514f5e3Sopenharmony_ci    Local<DataViewRef> dataView = DataViewRef::New(vm_, arrayBuffer, byteOffset, dataViewLength);
21094514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_DataView_byteLength : " << dataView->ByteLength();
21104514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_DataView_byteOffset : " << dataView->ByteOffset();
21114514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_DataView_getArrayBuffer : " << dataView->GetArrayBuffer(vm_)->GetBuffer(vm_);
21124514f5e3Sopenharmony_ci}
21134514f5e3Sopenharmony_ci
21144514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_ArrayBuffer_New_Detach)
21154514f5e3Sopenharmony_ci{
21164514f5e3Sopenharmony_ci    LocalScope scope(vm_);
21174514f5e3Sopenharmony_ci    const int32_t length = 32; // arraybuffer length = 32
21184514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, length);
21194514f5e3Sopenharmony_ci    int32_t arrayLength = arrayBuffer->ByteLength(vm_);
21204514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_ArrayBuffer_byteLength : " << arrayLength;
21214514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_ArrayBuffer_getArrayBuffer : " << arrayBuffer->GetBuffer(vm_);
21224514f5e3Sopenharmony_ci    arrayBuffer->Detach(vm_);
21234514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_ArrayBuffer_getDetachArrayBuffer : " << arrayBuffer->GetBuffer(vm_);
21244514f5e3Sopenharmony_ci    bool result = arrayBuffer->IsDetach(vm_);
21254514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_ArrayBuffer_getIsDetach : " << result;
21264514f5e3Sopenharmony_ci}
21274514f5e3Sopenharmony_ci
21284514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_ArrayBufferWithBuffer_New_Detach)
21294514f5e3Sopenharmony_ci{
21304514f5e3Sopenharmony_ci    static bool isFree = false;
21314514f5e3Sopenharmony_ci    struct Data {
21324514f5e3Sopenharmony_ci        int32_t length;
21334514f5e3Sopenharmony_ci    };
21344514f5e3Sopenharmony_ci    const int32_t length = 5; // arraybuffer length = 5
21354514f5e3Sopenharmony_ci    Data *data = new Data();
21364514f5e3Sopenharmony_ci    data->length = length;
21374514f5e3Sopenharmony_ci    NativePointerCallback deleter = [](void *env, void *buffer, void *data) -> void {
21384514f5e3Sopenharmony_ci        delete[] reinterpret_cast<uint8_t *>(buffer);
21394514f5e3Sopenharmony_ci        Data *currentData = reinterpret_cast<Data *>(data);
21404514f5e3Sopenharmony_ci        delete currentData;
21414514f5e3Sopenharmony_ci        isFree = true;
21424514f5e3Sopenharmony_ci    };
21434514f5e3Sopenharmony_ci    LocalScope scope(vm_);
21444514f5e3Sopenharmony_ci    uint8_t *buffer = new uint8_t[length]();
21454514f5e3Sopenharmony_ci    Local<ArrayBufferRef> arrayBuffer = ArrayBufferRef::New(vm_, buffer, length, deleter, data);
21464514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_ArrayBufferWithBuffer_byteLength : " << arrayBuffer->ByteLength(vm_);
21474514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_ArrayBufferWithBuffer_getArrayBuffer : " << arrayBuffer->GetBuffer(vm_);
21484514f5e3Sopenharmony_ci    arrayBuffer->Detach(vm_);
21494514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_ArrayBufferWithBuffer_getDetachArrayBuffer : " << arrayBuffer->GetBuffer(vm_);
21504514f5e3Sopenharmony_ci    bool result = arrayBuffer->IsDetach(vm_);
21514514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_ArrayBufferWithBuffer_getIsDetach : " << result;
21524514f5e3Sopenharmony_ci}
21534514f5e3Sopenharmony_ci
21544514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_Buffer_New_GetBuffer)
21554514f5e3Sopenharmony_ci{
21564514f5e3Sopenharmony_ci    LocalScope scope(vm_);
21574514f5e3Sopenharmony_ci    const int32_t length = 5; // buffer length = 5
21584514f5e3Sopenharmony_ci    Local<BufferRef> buffer = BufferRef::New(vm_, length);
21594514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Buffer_byteLength : " << buffer->ByteLength(vm_);
21604514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_Buffer_getBuffer : " << buffer->GetBuffer(vm_);
21614514f5e3Sopenharmony_ci}
21624514f5e3Sopenharmony_ci
21634514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_BufferWithBuffer_New_GetBuffer)
21644514f5e3Sopenharmony_ci{
21654514f5e3Sopenharmony_ci    static bool isFree = false;
21664514f5e3Sopenharmony_ci    struct Data {
21674514f5e3Sopenharmony_ci        int32_t length;
21684514f5e3Sopenharmony_ci    };
21694514f5e3Sopenharmony_ci    const int32_t length = 5; // buffer length = 5
21704514f5e3Sopenharmony_ci    Data *data = new Data();
21714514f5e3Sopenharmony_ci    data->length = length;
21724514f5e3Sopenharmony_ci    NativePointerCallback deleter = [](void *env, void *buffer, void *data) -> void {
21734514f5e3Sopenharmony_ci        delete[] reinterpret_cast<uint8_t *>(buffer);
21744514f5e3Sopenharmony_ci        Data *currentData = reinterpret_cast<Data *>(data);
21754514f5e3Sopenharmony_ci        delete currentData;
21764514f5e3Sopenharmony_ci        isFree = true;
21774514f5e3Sopenharmony_ci    };
21784514f5e3Sopenharmony_ci    LocalScope scope(vm_);
21794514f5e3Sopenharmony_ci    uint8_t *buffer = new uint8_t[length]();
21804514f5e3Sopenharmony_ci    Local<BufferRef> bufferObj = BufferRef::New(vm_, buffer, length, deleter, data);
21814514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_bufferWithBuffer_byteLength : " << bufferObj->ByteLength(vm_);
21824514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_bufferWithBuffer_getBuffer : " << bufferObj->GetBuffer(vm_);
21834514f5e3Sopenharmony_ci}
21844514f5e3Sopenharmony_ci
21854514f5e3Sopenharmony_ci/* domo8 异步操作。 ts:
21864514f5e3Sopenharmony_ci * new Promise(function (resolve, reject) {
21874514f5e3Sopenharmony_ci * var a = 0;
21884514f5e3Sopenharmony_ci * var b = 1;
21894514f5e3Sopenharmony_ci * if (b == 0) reject("Divide zero");
21904514f5e3Sopenharmony_ci * else resolve(a / b);
21914514f5e3Sopenharmony_ci * }).then(function (value) {
21924514f5e3Sopenharmony_ci * console.log("a / b = " + value);
21934514f5e3Sopenharmony_ci * }).catch(function (err) {
21944514f5e3Sopenharmony_ci * console.log(err);
21954514f5e3Sopenharmony_ci * }).finally(function () {
21964514f5e3Sopenharmony_ci * console.log("End");
21974514f5e3Sopenharmony_ci * });
21984514f5e3Sopenharmony_ci */
21994514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo8_async_test_1)
22004514f5e3Sopenharmony_ci{
22014514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo8_async_test_1 =======================================";
22024514f5e3Sopenharmony_ci    LocalScope scope(vm_);
22034514f5e3Sopenharmony_ci
22044514f5e3Sopenharmony_ci    Local<PromiseCapabilityRef> capability = PromiseCapabilityRef::New(vm_);
22054514f5e3Sopenharmony_ci    capability->GetPromise(vm_)
22064514f5e3Sopenharmony_ci        ->Then(vm_, FunctionRef::New(vm_,
22074514f5e3Sopenharmony_ci        // 函数体
22084514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
22094514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
22104514f5e3Sopenharmony_ci            LocalScope scope(vm);
22114514f5e3Sopenharmony_ci            Local<JSValueRef> jsNum = runtimeInfo->GetCallArgRef(0);
22124514f5e3Sopenharmony_ci            int num = jsNum->Int32Value(vm);
22134514f5e3Sopenharmony_ci            // ts: console.log("a / b = " + value);
22144514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "a / b = " << num;
22154514f5e3Sopenharmony_ci            return JSValueRef::Undefined(vm);
22164514f5e3Sopenharmony_ci        }))
22174514f5e3Sopenharmony_ci        ->Catch(vm_, FunctionRef::New(vm_,
22184514f5e3Sopenharmony_ci        // 函数体
22194514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
22204514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
22214514f5e3Sopenharmony_ci            LocalScope scope(vm);
22224514f5e3Sopenharmony_ci            Local<JSValueRef> jsStr = runtimeInfo->GetCallArgRef(0);
22234514f5e3Sopenharmony_ci            std::string err = jsStr->ToString(vm)->ToString(vm);
22244514f5e3Sopenharmony_ci            // ts: console.log(err);
22254514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << err;
22264514f5e3Sopenharmony_ci            return JSValueRef::Undefined(vm);
22274514f5e3Sopenharmony_ci        }))
22284514f5e3Sopenharmony_ci        ->Finally(vm_, FunctionRef::New(vm_,
22294514f5e3Sopenharmony_ci        // 函数体
22304514f5e3Sopenharmony_ci        [](JsiRuntimeCallInfo *runtimeInfo) -> Local<JSValueRef> {
22314514f5e3Sopenharmony_ci            EcmaVM *vm = runtimeInfo->GetVM();
22324514f5e3Sopenharmony_ci            LocalScope scope(vm);
22334514f5e3Sopenharmony_ci            // ts: console.log("End");
22344514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "End";
22354514f5e3Sopenharmony_ci            return JSValueRef::Undefined(vm);
22364514f5e3Sopenharmony_ci        }));
22374514f5e3Sopenharmony_ci
22384514f5e3Sopenharmony_ci    int a = 0;
22394514f5e3Sopenharmony_ci    int b = 0;
22404514f5e3Sopenharmony_ci    if (b == 0) {
22414514f5e3Sopenharmony_ci        capability->Reject(vm_, StringRef::NewFromUtf8(vm_, "Divide zero"));
22424514f5e3Sopenharmony_ci    } else {
22434514f5e3Sopenharmony_ci        capability->Resolve(vm_, NumberRef::New(vm_, int(a / b)));
22444514f5e3Sopenharmony_ci    }
22454514f5e3Sopenharmony_ci
22464514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo8_async_test_1 =======================================";
22474514f5e3Sopenharmony_ci}
22484514f5e3Sopenharmony_ci
22494514f5e3Sopenharmony_ci// JSValueRef转为字符串输出。
22504514f5e3Sopenharmony_cistd::string jsValue2String(EcmaVM *vm, Local<JSValueRef> &jsVal)
22514514f5e3Sopenharmony_ci{
22524514f5e3Sopenharmony_ci    if (jsVal->IsString(vm)) {
22534514f5e3Sopenharmony_ci        return "type string, val : " + jsVal->ToString(vm)->ToString(vm);
22544514f5e3Sopenharmony_ci    } else if (jsVal->IsNumber()) {
22554514f5e3Sopenharmony_ci        return "type number, val : " + std::to_string(jsVal->Int32Value(vm));
22564514f5e3Sopenharmony_ci    } else if (jsVal->IsBoolean()) {
22574514f5e3Sopenharmony_ci        return "type bool, val : " + std::to_string(jsVal->BooleaValue(vm));
22584514f5e3Sopenharmony_ci    } else if (jsVal->IsSymbol(vm)) {
22594514f5e3Sopenharmony_ci        Local<SymbolRef> symbol = jsVal;
22604514f5e3Sopenharmony_ci        return "type symbol, val : " + symbol->GetDescription(vm)->ToString(vm);
22614514f5e3Sopenharmony_ci    } else {
22624514f5e3Sopenharmony_ci        return "type other : " + jsVal->ToString(vm)->ToString(vm);
22634514f5e3Sopenharmony_ci    }
22644514f5e3Sopenharmony_ci}
22654514f5e3Sopenharmony_ci
22664514f5e3Sopenharmony_civoid MapSetValue(EcmaVM *vm, Local<MapRef> &map, Local<JSValueRef> symbolKey)
22674514f5e3Sopenharmony_ci{
22684514f5e3Sopenharmony_ci    map->Set(vm, StringRef::NewFromUtf8(vm, "key1"), StringRef::NewFromUtf8(vm, "val1"));
22694514f5e3Sopenharmony_ci    int num2 = 222; // random number
22704514f5e3Sopenharmony_ci    map->Set(vm, StringRef::NewFromUtf8(vm, "key2"), NumberRef::New(vm, num2));
22714514f5e3Sopenharmony_ci    map->Set(vm, StringRef::NewFromUtf8(vm, "key3"), BooleanRef::New(vm, true));
22724514f5e3Sopenharmony_ci    map->Set(vm, StringRef::NewFromUtf8(vm, "key4"), SymbolRef::New(vm, StringRef::NewFromUtf8(vm, "val4")));
22734514f5e3Sopenharmony_ci    int num5 = 55; // random number
22744514f5e3Sopenharmony_ci    map->Set(vm, IntegerRef::New(vm, num5), StringRef::NewFromUtf8(vm, "val5"));
22754514f5e3Sopenharmony_ci    int num61 = 66;  // random number
22764514f5e3Sopenharmony_ci    int num62 = 666; // random number
22774514f5e3Sopenharmony_ci    map->Set(vm, IntegerRef::New(vm, num61), IntegerRef::New(vm, num62));
22784514f5e3Sopenharmony_ci    map->Set(vm, BooleanRef::New(vm, true), StringRef::NewFromUtf8(vm, "val7"));
22794514f5e3Sopenharmony_ci    map->Set(vm, symbolKey, StringRef::NewFromUtf8(vm, "val8"));
22804514f5e3Sopenharmony_ci}
22814514f5e3Sopenharmony_ci
22824514f5e3Sopenharmony_civoid MapGetValue(EcmaVM *vm, Local<MapRef> &map, Local<JSValueRef> symbolKey)
22834514f5e3Sopenharmony_ci{
22844514f5e3Sopenharmony_ci    Local<JSValueRef> val1 = map->Get(vm, StringRef::NewFromUtf8(vm, "key1"));
22854514f5e3Sopenharmony_ci    bool val1IsString = val1->IsString(vm);
22864514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "key1 : IsString:" << val1IsString << "    val:" << val1->ToString(vm)->ToString(vm);
22874514f5e3Sopenharmony_ci
22884514f5e3Sopenharmony_ci    Local<JSValueRef> val2 = map->Get(vm, StringRef::NewFromUtf8(vm, "key2"));
22894514f5e3Sopenharmony_ci    bool val2IsNumber = val2->IsNumber();
22904514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "key2 : IsNumber:" << val2IsNumber << "    val:" << val2->Int32Value(vm);
22914514f5e3Sopenharmony_ci
22924514f5e3Sopenharmony_ci    Local<JSValueRef> val3 = map->Get(vm, StringRef::NewFromUtf8(vm, "key3"));
22934514f5e3Sopenharmony_ci    bool val3IsBoolean = val3->IsBoolean();
22944514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "key3 : IsBoolean:" << val3IsBoolean << "    val:" << val3->BooleaValue(vm);
22954514f5e3Sopenharmony_ci
22964514f5e3Sopenharmony_ci    Local<JSValueRef> val4 = map->Get(vm, StringRef::NewFromUtf8(vm, "key4"));
22974514f5e3Sopenharmony_ci    bool val4IsSymbol = val4->IsSymbol(vm);
22984514f5e3Sopenharmony_ci    Local<SymbolRef> val4Symbol = val4;
22994514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "key4 : IsSymbol:" << val4IsSymbol << "    val:"
23004514f5e3Sopenharmony_ci                     << val4Symbol->GetDescription(vm)->ToString(vm);
23014514f5e3Sopenharmony_ci
23024514f5e3Sopenharmony_ci    int num5 = 55; // random number
23034514f5e3Sopenharmony_ci    Local<JSValueRef> val5 = map->Get(vm, IntegerRef::New(vm, num5));
23044514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "55 : " << val5->ToString(vm)->ToString(vm);
23054514f5e3Sopenharmony_ci
23064514f5e3Sopenharmony_ci    int num6 = 66; // random number
23074514f5e3Sopenharmony_ci    Local<JSValueRef> val6 = map->Get(vm, IntegerRef::New(vm, num6));
23084514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "66 : " << val6->Int32Value(vm);
23094514f5e3Sopenharmony_ci    Local<JSValueRef> val7 = map->Get(vm, BooleanRef::New(vm, true));
23104514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "true : " << val7->ToString(vm)->ToString(vm);
23114514f5e3Sopenharmony_ci
23124514f5e3Sopenharmony_ci    Local<JSValueRef> val8 = map->Get(vm, symbolKey);
23134514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "SymbolRef(key8)  : " << val8->ToString(vm)->ToString(vm);
23144514f5e3Sopenharmony_ci
23154514f5e3Sopenharmony_ci    Local<JSValueRef> val82 = map->Get(vm, SymbolRef::New(vm, StringRef::NewFromUtf8(vm, "key8")));
23164514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "new SymbolRef(key8) is Undefined : " << val82->IsUndefined();
23174514f5e3Sopenharmony_ci
23184514f5e3Sopenharmony_ci    int32_t size = map->GetSize(vm);
23194514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "size : " << size;
23204514f5e3Sopenharmony_ci    int32_t totalElement = map->GetTotalElements(vm);
23214514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "total element : " << totalElement;
23224514f5e3Sopenharmony_ci
23234514f5e3Sopenharmony_ci    for (int i = 0; i < size; ++i) {
23244514f5e3Sopenharmony_ci        Local<JSValueRef> jsKey = map->GetKey(vm, i);
23254514f5e3Sopenharmony_ci        Local<JSValueRef> jsVal = map->GetValue(vm, i);
23264514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "for map index : " << i << "    key : " << jsValue2String(vm, jsKey) << "    val : " <<
23274514f5e3Sopenharmony_ci            jsValue2String(vm, jsVal);
23284514f5e3Sopenharmony_ci    }
23294514f5e3Sopenharmony_ci}
23304514f5e3Sopenharmony_ci
23314514f5e3Sopenharmony_civoid MapIteratorGetValue(EcmaVM *vm, Local<MapRef> &map)
23324514f5e3Sopenharmony_ci{
23334514f5e3Sopenharmony_ci    Local<MapIteratorRef> mapIter = MapIteratorRef::New(vm, map);
23344514f5e3Sopenharmony_ci    ecmascript::EcmaRuntimeCallInfo *mapIterInfo = mapIter->GetEcmaRuntimeCallInfo(vm);
23354514f5e3Sopenharmony_ci
23364514f5e3Sopenharmony_ci    Local<StringRef> kind = mapIter->GetKind(vm);
23374514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Map Iterator kind : " << kind->ToString(vm);
23384514f5e3Sopenharmony_ci
23394514f5e3Sopenharmony_ci    for (Local<ArrayRef> array = MapIteratorRef::Next(vm, mapIterInfo); array->IsArray(vm);
23404514f5e3Sopenharmony_ci        array = MapIteratorRef::Next(vm, mapIterInfo)) {
23414514f5e3Sopenharmony_ci        int index = mapIter->GetIndex() - 1;
23424514f5e3Sopenharmony_ci        Local<JSValueRef> jsKey = ArrayRef::GetValueAt(vm, array, 0);
23434514f5e3Sopenharmony_ci        Local<JSValueRef> jsVal = ArrayRef::GetValueAt(vm, array, 1);
23444514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "for map iterator index : " << index << "    key : " << jsValue2String(vm, jsKey) <<
23454514f5e3Sopenharmony_ci            "    val : " << jsValue2String(vm, jsVal);
23464514f5e3Sopenharmony_ci    }
23474514f5e3Sopenharmony_ci}
23484514f5e3Sopenharmony_ci
23494514f5e3Sopenharmony_ci/* demo9 Map,MapIterator 的操作。 */
23504514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo9_map_test_1_MapRef_MapIteratorRef)
23514514f5e3Sopenharmony_ci{
23524514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo9_map_test_1_MapRef_MapIteratorRef =======================================";
23534514f5e3Sopenharmony_ci    LocalScope scope(vm_);
23544514f5e3Sopenharmony_ci
23554514f5e3Sopenharmony_ci    Local<MapRef> map = MapRef::New(vm_);
23564514f5e3Sopenharmony_ci    Local<JSValueRef> symbolKey = SymbolRef::New(vm_, StringRef::NewFromUtf8(vm_, "key8"));
23574514f5e3Sopenharmony_ci    MapSetValue(vm_, map, symbolKey);
23584514f5e3Sopenharmony_ci    MapGetValue(vm_, map, symbolKey);
23594514f5e3Sopenharmony_ci    MapIteratorGetValue(vm_, map);
23604514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo9_map_test_1_MapRef_MapIteratorRef =======================================";
23614514f5e3Sopenharmony_ci}
23624514f5e3Sopenharmony_ci
23634514f5e3Sopenharmony_ci/* demo9 WeakMap 的操作。 */
23644514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo9_map_test_2_WeakMapref)
23654514f5e3Sopenharmony_ci{
23664514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo9_map_test_2_WeakMapref =======================================";
23674514f5e3Sopenharmony_ci    LocalScope scope(vm_);
23684514f5e3Sopenharmony_ci
23694514f5e3Sopenharmony_ci    Local<WeakMapRef> weakMap = WeakMapRef::New(vm_);
23704514f5e3Sopenharmony_ci    weakMap->Set(vm_, StringRef::NewFromUtf8(vm_, "key1"), StringRef::NewFromUtf8(vm_, "val1"));
23714514f5e3Sopenharmony_ci    int num2 = 222; // random number
23724514f5e3Sopenharmony_ci    weakMap->Set(vm_, StringRef::NewFromUtf8(vm_, "key2"), NumberRef::New(vm_, num2));
23734514f5e3Sopenharmony_ci    weakMap->Set(vm_, StringRef::NewFromUtf8(vm_, "key3"), BooleanRef::New(vm_, true));
23744514f5e3Sopenharmony_ci    weakMap->Set(vm_, StringRef::NewFromUtf8(vm_, "key4"), SymbolRef::New(vm_, StringRef::NewFromUtf8(vm_, "val4")));
23754514f5e3Sopenharmony_ci    weakMap->Set(vm_, IntegerRef::New(vm_, 55), StringRef::NewFromUtf8(vm_, "val5"));
23764514f5e3Sopenharmony_ci    int num6 = 666; // random number
23774514f5e3Sopenharmony_ci    weakMap->Set(vm_, IntegerRef::New(vm_, 66), IntegerRef::New(vm_, num6));
23784514f5e3Sopenharmony_ci    weakMap->Set(vm_, BooleanRef::New(vm_, true), StringRef::NewFromUtf8(vm_, "val7"));
23794514f5e3Sopenharmony_ci    Local<JSValueRef> key8 = SymbolRef::New(vm_, StringRef::NewFromUtf8(vm_, "key8"));
23804514f5e3Sopenharmony_ci    weakMap->Set(vm_, key8, StringRef::NewFromUtf8(vm_, "val8"));
23814514f5e3Sopenharmony_ci
23824514f5e3Sopenharmony_ci    int size = weakMap->GetSize(vm_);
23834514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "size : " << size;
23844514f5e3Sopenharmony_ci    int32_t totalElements = weakMap->GetTotalElements(vm_);
23854514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "total elements : " << totalElements;
23864514f5e3Sopenharmony_ci
23874514f5e3Sopenharmony_ci    for (int i = 0; i < size; ++i) {
23884514f5e3Sopenharmony_ci        Local<JSValueRef> jsKey = weakMap->GetKey(vm_, i);
23894514f5e3Sopenharmony_ci        Local<JSValueRef> jsVal = weakMap->GetValue(vm_, i);
23904514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "for WeakMap index : " << i << "    key : " << jsValue2String(vm_, jsKey) << "    val : " <<
23914514f5e3Sopenharmony_ci            jsValue2String(vm_, jsVal);
23924514f5e3Sopenharmony_ci    }
23934514f5e3Sopenharmony_ci
23944514f5e3Sopenharmony_ci    bool hasKey2 = weakMap->Has(vm_, StringRef::NewFromUtf8(vm_, "key2"));
23954514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Has Key2 : " << hasKey2;
23964514f5e3Sopenharmony_ci    bool hasKey222 = weakMap->Has(vm_, StringRef::NewFromUtf8(vm_, "key222"));
23974514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Has key222 : " << hasKey222;
23984514f5e3Sopenharmony_ci
23994514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo9_map_test_2_WeakMapref =======================================";
24004514f5e3Sopenharmony_ci}
24014514f5e3Sopenharmony_ci
24024514f5e3Sopenharmony_ci/* demo10 set 的使用 */
24034514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo10_set)
24044514f5e3Sopenharmony_ci{
24054514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo10_set =======================================";
24064514f5e3Sopenharmony_ci    LocalScope scope(vm_);
24074514f5e3Sopenharmony_ci    Local<SetRef> set = SetRef::New(vm_);
24084514f5e3Sopenharmony_ci
24094514f5e3Sopenharmony_ci    set->Add(vm_, StringRef::NewFromUtf8(vm_, "val1"));
24104514f5e3Sopenharmony_ci    int num2 = 222; // random number
24114514f5e3Sopenharmony_ci    set->Add(vm_, NumberRef::New(vm_, num2));
24124514f5e3Sopenharmony_ci    set->Add(vm_, BooleanRef::New(vm_, true));
24134514f5e3Sopenharmony_ci    set->Add(vm_, SymbolRef::New(vm_, StringRef::NewFromUtf8(vm_, "val4")));
24144514f5e3Sopenharmony_ci    set->Add(vm_, StringRef::NewFromUtf8(vm_, "val5"));
24154514f5e3Sopenharmony_ci    int num6 = 666; // random number
24164514f5e3Sopenharmony_ci    set->Add(vm_, IntegerRef::New(vm_, num6));
24174514f5e3Sopenharmony_ci
24184514f5e3Sopenharmony_ci    int32_t size = set->GetSize(vm_);
24194514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "size : " << size;
24204514f5e3Sopenharmony_ci    int32_t totalElement = set->GetTotalElements(vm_);
24214514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "total element : " << totalElement;
24224514f5e3Sopenharmony_ci
24234514f5e3Sopenharmony_ci    for (int i = 0; i < size; ++i) {
24244514f5e3Sopenharmony_ci        Local<JSValueRef> jsVal = set->GetValue(vm_, i);
24254514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "for set index : " << i << "    val : " << jsValue2String(vm_, jsVal);
24264514f5e3Sopenharmony_ci    }
24274514f5e3Sopenharmony_ci
24284514f5e3Sopenharmony_ci    // Iterator
24294514f5e3Sopenharmony_ci    Local<SetIteratorRef> setIter = SetIteratorRef::New(vm_, set);
24304514f5e3Sopenharmony_ci    ecmascript::EcmaRuntimeCallInfo *info = setIter->GetEcmaRuntimeCallInfo(vm_);
24314514f5e3Sopenharmony_ci
24324514f5e3Sopenharmony_ci    Local<StringRef> kind = setIter->GetKind(vm_);
24334514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "Set Iterator kind : " << kind->ToString(vm_);
24344514f5e3Sopenharmony_ci
24354514f5e3Sopenharmony_ci    for (Local<ArrayRef> array = SetIteratorRef::Next(vm_, info); array->IsArray(vm_);
24364514f5e3Sopenharmony_ci        array = SetIteratorRef::Next(vm_, info)) {
24374514f5e3Sopenharmony_ci        int index = setIter->GetIndex() - 1;
24384514f5e3Sopenharmony_ci        Local<JSValueRef> jsVal = ArrayRef::GetValueAt(vm_, array, 1);
24394514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "for set iterator index : " << index << "    val : " << jsValue2String(vm_, jsVal);
24404514f5e3Sopenharmony_ci    }
24414514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo10_set =======================================";
24424514f5e3Sopenharmony_ci}
24434514f5e3Sopenharmony_ci
24444514f5e3Sopenharmony_ci/* demo10 WeakSet 的使用 */
24454514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo10_weakset)
24464514f5e3Sopenharmony_ci{
24474514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo10_weakset =======================================";
24484514f5e3Sopenharmony_ci    LocalScope scope(vm_);
24494514f5e3Sopenharmony_ci    Local<WeakSetRef> weakSet = WeakSetRef::New(vm_);
24504514f5e3Sopenharmony_ci
24514514f5e3Sopenharmony_ci    weakSet->Add(vm_, StringRef::NewFromUtf8(vm_, "val1"));
24524514f5e3Sopenharmony_ci    int num2 = 666; // random number
24534514f5e3Sopenharmony_ci    weakSet->Add(vm_, NumberRef::New(vm_, num2));
24544514f5e3Sopenharmony_ci    weakSet->Add(vm_, BooleanRef::New(vm_, true));
24554514f5e3Sopenharmony_ci    weakSet->Add(vm_, SymbolRef::New(vm_, StringRef::NewFromUtf8(vm_, "val4")));
24564514f5e3Sopenharmony_ci    weakSet->Add(vm_, StringRef::NewFromUtf8(vm_, "val5"));
24574514f5e3Sopenharmony_ci    int num6 = 666; // random number
24584514f5e3Sopenharmony_ci    weakSet->Add(vm_, IntegerRef::New(vm_, num6));
24594514f5e3Sopenharmony_ci
24604514f5e3Sopenharmony_ci    int32_t size = weakSet->GetSize(vm_);
24614514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "size : " << size;
24624514f5e3Sopenharmony_ci    int32_t totalElement = weakSet->GetTotalElements(vm_);
24634514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "total element : " << totalElement;
24644514f5e3Sopenharmony_ci
24654514f5e3Sopenharmony_ci    for (int i = 0; i < size; ++i) {
24664514f5e3Sopenharmony_ci        Local<JSValueRef> jsVal = weakSet->GetValue(vm_, i);
24674514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "for weakset index : " << i << "    val : " << jsValue2String(vm_, jsVal);
24684514f5e3Sopenharmony_ci    }
24694514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo10_weakset =======================================";
24704514f5e3Sopenharmony_ci}
24714514f5e3Sopenharmony_ci
24724514f5e3Sopenharmony_ci// json对象获取值。
24734514f5e3Sopenharmony_civoid JsonObjGetValue(EcmaVM *vm, Local<ObjectRef> obj)
24744514f5e3Sopenharmony_ci{
24754514f5e3Sopenharmony_ci    Local<JSValueRef> jsVal1 = obj->Get(vm, StringRef::NewFromUtf8(vm, "str1"));
24764514f5e3Sopenharmony_ci    bool jsVal1IsString = jsVal1->IsString(vm);
24774514f5e3Sopenharmony_ci    Local<StringRef> val1 = jsVal1->ToString(vm);
24784514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "str1 : is string : " << jsVal1IsString << "  value : " << val1->ToString(vm);
24794514f5e3Sopenharmony_ci    Local<JSValueRef> jsVal2 = obj->Get(vm, StringRef::NewFromUtf8(vm, "str2"));
24804514f5e3Sopenharmony_ci    bool jsVal2IsNumber = jsVal2->IsNumber();
24814514f5e3Sopenharmony_ci    int val2 = jsVal2->Int32Value(vm);
24824514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "str2 : is number : " << jsVal2IsNumber << "  value : " << val2;
24834514f5e3Sopenharmony_ci    Local<JSValueRef> jsVal3 = obj->Get(vm, StringRef::NewFromUtf8(vm, "333"));
24844514f5e3Sopenharmony_ci    int val3 = jsVal3->Int32Value(vm);
24854514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "333 : " << val3;
24864514f5e3Sopenharmony_ci    Local<JSValueRef> jsVal4 = obj->Get(vm, StringRef::NewFromUtf8(vm, "444"));
24874514f5e3Sopenharmony_ci    Local<StringRef> val4 = jsVal4->ToString(vm);
24884514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "str4 : " << val4->ToString(vm);
24894514f5e3Sopenharmony_ci
24904514f5e3Sopenharmony_ci    Local<JSValueRef> jsVal8 = obj->Get(vm, StringRef::NewFromUtf8(vm, "b8"));
24914514f5e3Sopenharmony_ci    bool jsVal8IsBool = jsVal8->IsBoolean();
24924514f5e3Sopenharmony_ci    Local<BooleanRef> val8Ref = jsVal8;
24934514f5e3Sopenharmony_ci    bool val8 = val8Ref->Value();
24944514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "b8  is bool : " << jsVal8IsBool << "    val : " << val8;
24954514f5e3Sopenharmony_ci}
24964514f5e3Sopenharmony_ci
24974514f5e3Sopenharmony_ci// json对象获取数组。
24984514f5e3Sopenharmony_civoid JsonObjGetArray(EcmaVM *vm, Local<ObjectRef> obj)
24994514f5e3Sopenharmony_ci{
25004514f5e3Sopenharmony_ci    Local<JSValueRef> jsVal5 = obj->Get(vm, StringRef::NewFromUtf8(vm, "arr5"));
25014514f5e3Sopenharmony_ci    Local<ObjectRef> val5Obj = jsVal5;
25024514f5e3Sopenharmony_ci    int length = val5Obj->Get(vm, StringRef::NewFromUtf8(vm, "length"))->Int32Value(vm);
25034514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "arr5 length : " << length;
25044514f5e3Sopenharmony_ci    for (int i = 0; i < length; ++i) {
25054514f5e3Sopenharmony_ci        Local<JSValueRef> val5Item = val5Obj->Get(vm, NumberRef::New(vm, i));
25064514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "arr5 : " << i << "    " << val5Item->Int32Value(vm);
25074514f5e3Sopenharmony_ci    }
25084514f5e3Sopenharmony_ci    Local<ArrayRef> val5Arr = jsVal5;
25094514f5e3Sopenharmony_ci    uint32_t length2 = val5Arr->Length(vm);
25104514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "arr5 length2 : " << length2;
25114514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < length2; ++i) {
25124514f5e3Sopenharmony_ci        Local<JSValueRef> val5Item = ArrayRef::GetValueAt(vm, val5Arr, i);
25134514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "arr5 : " << i << "    " << val5Item->Int32Value(vm);
25144514f5e3Sopenharmony_ci    }
25154514f5e3Sopenharmony_ci    Local<JSValueRef> jsVal6 = obj->Get(vm, StringRef::NewFromUtf8(vm, "arr6"));
25164514f5e3Sopenharmony_ci    Local<ObjectRef> val6Obj = jsVal6;
25174514f5e3Sopenharmony_ci    int length3 = val6Obj->Get(vm, StringRef::NewFromUtf8(vm, "length"))->Int32Value(vm);
25184514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "arr6 length : " << length3;
25194514f5e3Sopenharmony_ci    for (int i = 0; i < length3; ++i) {
25204514f5e3Sopenharmony_ci        Local<JSValueRef> val6Item = val6Obj->Get(vm, NumberRef::New(vm, i));
25214514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "arr6 : " << i << "    " << val6Item->ToString(vm)->ToString(vm);
25224514f5e3Sopenharmony_ci    }
25234514f5e3Sopenharmony_ci}
25244514f5e3Sopenharmony_ci
25254514f5e3Sopenharmony_ci// json对象获取对象。
25264514f5e3Sopenharmony_civoid JsonObjGetObject(EcmaVM *vm, Local<ObjectRef> obj)
25274514f5e3Sopenharmony_ci{
25284514f5e3Sopenharmony_ci    Local<JSValueRef> jsVal7 = obj->Get(vm, StringRef::NewFromUtf8(vm, "data7"));
25294514f5e3Sopenharmony_ci    Local<ObjectRef> val7Obj = jsVal7->ToObject(vm);
25304514f5e3Sopenharmony_ci    Local<ArrayRef> val7ObjKeys = val7Obj->GetOwnPropertyNames(vm);
25314514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < val7ObjKeys->Length(vm); ++i) {
25324514f5e3Sopenharmony_ci        Local<JSValueRef> itemKey = ArrayRef::GetValueAt(vm, val7ObjKeys, i);
25334514f5e3Sopenharmony_ci        Local<JSValueRef> itemVal = val7Obj->Get(vm, itemKey);
25344514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "data7 :   item index:" << i << "    Key:" << itemKey->ToString(vm)->ToString(vm) <<
25354514f5e3Sopenharmony_ci            "    val:" << itemVal->ToString(vm)->ToString(vm);
25364514f5e3Sopenharmony_ci    }
25374514f5e3Sopenharmony_ci}
25384514f5e3Sopenharmony_ci
25394514f5e3Sopenharmony_ci/* demo11 json 测试,json字符串 转 obj。 注意key不能是纯数字。 json:
25404514f5e3Sopenharmony_ci * {
25414514f5e3Sopenharmony_ci * "str1": "val1",
25424514f5e3Sopenharmony_ci * "str2": 222,
25434514f5e3Sopenharmony_ci * "333": 456,
25444514f5e3Sopenharmony_ci * "444": "val4",
25454514f5e3Sopenharmony_ci * "arr5": [
25464514f5e3Sopenharmony_ci * 51,
25474514f5e3Sopenharmony_ci * 52,
25484514f5e3Sopenharmony_ci * 53,
25494514f5e3Sopenharmony_ci * 54,
25504514f5e3Sopenharmony_ci * 55
25514514f5e3Sopenharmony_ci * ],
25524514f5e3Sopenharmony_ci * "arr6": [
25534514f5e3Sopenharmony_ci * "str61",
25544514f5e3Sopenharmony_ci * "str62",
25554514f5e3Sopenharmony_ci * "str63",
25564514f5e3Sopenharmony_ci * "str64"
25574514f5e3Sopenharmony_ci * ],
25584514f5e3Sopenharmony_ci * "data7": {
25594514f5e3Sopenharmony_ci * "key71": "val71",
25604514f5e3Sopenharmony_ci * "key72": "val72"
25614514f5e3Sopenharmony_ci * },
25624514f5e3Sopenharmony_ci * "b8": true
25634514f5e3Sopenharmony_ci * }
25644514f5e3Sopenharmony_ci */
25654514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo11_json_test_1_parse_object)
25664514f5e3Sopenharmony_ci{
25674514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_1_parse_object =======================================";
25684514f5e3Sopenharmony_ci    LocalScope scope(vm_);
25694514f5e3Sopenharmony_ci    std::string jsonStr =
25704514f5e3Sopenharmony_ci        "{\"str1\":\"val1\",\"str2\":222,\"333\":456,\"444\":\"val4\",\"arr5\":[51,52,53,54,55],\"arr6\":[\"str61\","
25714514f5e3Sopenharmony_ci        "\"str62\",\"str63\",\"str64\"],\"data7\":{\"key71\":\"val71\",\"key72\":\"val72\"},\"b8\":true}";
25724514f5e3Sopenharmony_ci    Local<JSValueRef> jsObj = JSON::Parse(vm_, StringRef::NewFromUtf8(vm_, jsonStr.c_str()));
25734514f5e3Sopenharmony_ci    Local<ObjectRef> obj = jsObj->ToObject(vm_);
25744514f5e3Sopenharmony_ci
25754514f5e3Sopenharmony_ci    JsonObjGetValue(vm_, obj);
25764514f5e3Sopenharmony_ci    JsonObjGetArray(vm_, obj);
25774514f5e3Sopenharmony_ci    JsonObjGetObject(vm_, obj);
25784514f5e3Sopenharmony_ci
25794514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_1_parse_object =======================================";
25804514f5e3Sopenharmony_ci}
25814514f5e3Sopenharmony_ci
25824514f5e3Sopenharmony_ci/* demo11 json 测试,obj转json字符串。 json:
25834514f5e3Sopenharmony_ci * {
25844514f5e3Sopenharmony_ci * "key1": "val1",
25854514f5e3Sopenharmony_ci * "key2": 123,
25864514f5e3Sopenharmony_ci * "333": "val3",
25874514f5e3Sopenharmony_ci * "arr4": [
25884514f5e3Sopenharmony_ci * "val40",
25894514f5e3Sopenharmony_ci * "val41",
25904514f5e3Sopenharmony_ci * "val42",
25914514f5e3Sopenharmony_ci * "val43"
25924514f5e3Sopenharmony_ci * ],
25934514f5e3Sopenharmony_ci * "arr5": [
25944514f5e3Sopenharmony_ci * 50,
25954514f5e3Sopenharmony_ci * 51,
25964514f5e3Sopenharmony_ci * 52,
25974514f5e3Sopenharmony_ci * 53
25984514f5e3Sopenharmony_ci * ],
25994514f5e3Sopenharmony_ci * "b6": true,
26004514f5e3Sopenharmony_ci * "obj7": {
26014514f5e3Sopenharmony_ci * "key71": "val71",
26024514f5e3Sopenharmony_ci * "key72": "val72",
26034514f5e3Sopenharmony_ci * "key73": "val73"
26044514f5e3Sopenharmony_ci * }
26054514f5e3Sopenharmony_ci * }
26064514f5e3Sopenharmony_ci */
26074514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo11_json_test_2_stringify_object)
26084514f5e3Sopenharmony_ci{
26094514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_2_stringify_object =======================================";
26104514f5e3Sopenharmony_ci    LocalScope scope(vm_);
26114514f5e3Sopenharmony_ci
26124514f5e3Sopenharmony_ci    Local<ObjectRef> obj = ObjectRef::New(vm_);
26134514f5e3Sopenharmony_ci    obj->Set(vm_, StringRef::NewFromUtf8(vm_, "key1"), StringRef::NewFromUtf8(vm_, "val1"));
26144514f5e3Sopenharmony_ci    int num2 = 123; // random number
26154514f5e3Sopenharmony_ci    obj->Set(vm_, StringRef::NewFromUtf8(vm_, "key2"), NumberRef::New(vm_, num2));
26164514f5e3Sopenharmony_ci    int num3 = 333; // random number
26174514f5e3Sopenharmony_ci    obj->Set(vm_, NumberRef::New(vm_, num3), StringRef::NewFromUtf8(vm_, "val3"));
26184514f5e3Sopenharmony_ci
26194514f5e3Sopenharmony_ci    Local<ArrayRef> arr4 = ArrayRef::New(vm_);
26204514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr4, 0, StringRef::NewFromUtf8(vm_, "val40"));
26214514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr4, 1, StringRef::NewFromUtf8(vm_, "val41"));
26224514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr4, 2, StringRef::NewFromUtf8(vm_, "val42"));
26234514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr4, 3, StringRef::NewFromUtf8(vm_, "val43"));
26244514f5e3Sopenharmony_ci    obj->Set(vm_, StringRef::NewFromUtf8(vm_, "arr4"), arr4);
26254514f5e3Sopenharmony_ci
26264514f5e3Sopenharmony_ci    Local<ArrayRef> arr5 = ArrayRef::New(vm_);
26274514f5e3Sopenharmony_ci    int num50 = 50; // random number
26284514f5e3Sopenharmony_ci    int num51 = 51; // random number
26294514f5e3Sopenharmony_ci    int num52 = 52; // random number
26304514f5e3Sopenharmony_ci    int num53 = 53; // random number
26314514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr5, 0, IntegerRef::New(vm_, num50));
26324514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr5, 1, IntegerRef::New(vm_, num51));
26334514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr5, 2, IntegerRef::New(vm_, num52));
26344514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr5, 3, IntegerRef::New(vm_, num53));
26354514f5e3Sopenharmony_ci    obj->Set(vm_, StringRef::NewFromUtf8(vm_, "arr5"), arr5);
26364514f5e3Sopenharmony_ci
26374514f5e3Sopenharmony_ci    obj->Set(vm_, StringRef::NewFromUtf8(vm_, "b6"), BooleanRef::New(vm_, true));
26384514f5e3Sopenharmony_ci
26394514f5e3Sopenharmony_ci    Local<ObjectRef> obj7 = ObjectRef::New(vm_);
26404514f5e3Sopenharmony_ci    obj7->Set(vm_, StringRef::NewFromUtf8(vm_, "key71"), StringRef::NewFromUtf8(vm_, "val71"));
26414514f5e3Sopenharmony_ci    obj7->Set(vm_, StringRef::NewFromUtf8(vm_, "key72"), StringRef::NewFromUtf8(vm_, "val72"));
26424514f5e3Sopenharmony_ci    obj7->Set(vm_, StringRef::NewFromUtf8(vm_, "key73"), StringRef::NewFromUtf8(vm_, "val73"));
26434514f5e3Sopenharmony_ci
26444514f5e3Sopenharmony_ci    obj->Set(vm_, StringRef::NewFromUtf8(vm_, "obj7"), obj7);
26454514f5e3Sopenharmony_ci
26464514f5e3Sopenharmony_ci    Local<JSValueRef> jsStr = JSON::Stringify(vm_, obj);
26474514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "jsStr is String : " << jsStr->IsString(vm_);
26484514f5e3Sopenharmony_ci    Local<StringRef> strRef = jsStr->ToString(vm_);
26494514f5e3Sopenharmony_ci    std::string str = strRef->ToString(vm_);
26504514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "json : " << str;
26514514f5e3Sopenharmony_ci
26524514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_2_stringify_object =======================================";
26534514f5e3Sopenharmony_ci}
26544514f5e3Sopenharmony_ci
26554514f5e3Sopenharmony_ci/* demo11 json 测试,json字符串转为数组。 json:
26564514f5e3Sopenharmony_ci * [51,52,53,54,55]
26574514f5e3Sopenharmony_ci */
26584514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo11_json_test_3_parse_array1)
26594514f5e3Sopenharmony_ci{
26604514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_3_parse_array1 =======================================";
26614514f5e3Sopenharmony_ci    LocalScope scope(vm_);
26624514f5e3Sopenharmony_ci
26634514f5e3Sopenharmony_ci    // 随机的一个 json 数字 数组。  [51,52,53,54,55]
26644514f5e3Sopenharmony_ci    Local<StringRef> arrStr1 = StringRef::NewFromUtf8(vm_, "[51,52,53,54,55]");
26654514f5e3Sopenharmony_ci    Local<JSValueRef> jsArr1 = JSON::Parse(vm_, arrStr1);
26664514f5e3Sopenharmony_ci    bool jsArr1IsArr = jsArr1->IsArray(vm_);
26674514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "jsArr1 is array : " << jsArr1IsArr;
26684514f5e3Sopenharmony_ci    Local<ArrayRef> arr1 = jsArr1;
26694514f5e3Sopenharmony_ci    uint32_t arr1Length = arr1->Length(vm_);
26704514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "arr1 length : " << arr1Length;
26714514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < arr1Length; ++i) {
26724514f5e3Sopenharmony_ci        Local<JSValueRef> arr1Item = ArrayRef::GetValueAt(vm_, arr1, i);
26734514f5e3Sopenharmony_ci        if (arr1Item->IsNumber()) {
26744514f5e3Sopenharmony_ci            int num = arr1Item->Int32Value(vm_);
26754514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "arr1 index : " << i << "  value : " << num;
26764514f5e3Sopenharmony_ci        } else {
26774514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "arr1 index : " << i << "  not number !";
26784514f5e3Sopenharmony_ci        }
26794514f5e3Sopenharmony_ci    }
26804514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_3_parse_array1 =======================================";
26814514f5e3Sopenharmony_ci}
26824514f5e3Sopenharmony_ci
26834514f5e3Sopenharmony_ci/* demo11 json 测试,json字符串转为数组。 json:
26844514f5e3Sopenharmony_ci * ["a1","a2","a3","a4","a5","a6"]
26854514f5e3Sopenharmony_ci */
26864514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo11_json_test_3_parse_array2)
26874514f5e3Sopenharmony_ci{
26884514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_3_parse_array2 =======================================";
26894514f5e3Sopenharmony_ci    LocalScope scope(vm_);
26904514f5e3Sopenharmony_ci
26914514f5e3Sopenharmony_ci    // 随机的一个json字符串数组。 ["a1","a2","a3","a4","a5","a6"]
26924514f5e3Sopenharmony_ci    Local<StringRef> arrStr2 = StringRef::NewFromUtf8(vm_, "[\"a1\",\"a2\",\"a3\",\"a4\",\"a5\",\"a6\"]");
26934514f5e3Sopenharmony_ci    Local<JSValueRef> jsArr2 = JSON::Parse(vm_, arrStr2);
26944514f5e3Sopenharmony_ci    bool jsArr2IsArr = jsArr2->IsArray(vm_);
26954514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "jsArr2 is array : " << jsArr2IsArr;
26964514f5e3Sopenharmony_ci    Local<ArrayRef> arr2 = jsArr2;
26974514f5e3Sopenharmony_ci    uint32_t arr2Length = arr2->Length(vm_);
26984514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "arr2 length : " << arr2Length;
26994514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < arr2Length; ++i) {
27004514f5e3Sopenharmony_ci        Local<JSValueRef> arr2Item = ArrayRef::GetValueAt(vm_, arr2, i);
27014514f5e3Sopenharmony_ci        std::string str = arr2Item->ToString(vm_)->ToString(vm_);
27024514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "arr2 index : " << i << "  value : " << str;
27034514f5e3Sopenharmony_ci    }
27044514f5e3Sopenharmony_ci
27054514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_3_parse_array2 =======================================";
27064514f5e3Sopenharmony_ci}
27074514f5e3Sopenharmony_ci
27084514f5e3Sopenharmony_ci/* demo11 json 测试,json字符串转为数组。 json:
27094514f5e3Sopenharmony_ci * [true,true,false,false,true]
27104514f5e3Sopenharmony_ci */
27114514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo11_json_test_3_parse_array3)
27124514f5e3Sopenharmony_ci{
27134514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_3_parse_array3 =======================================";
27144514f5e3Sopenharmony_ci    LocalScope scope(vm_);
27154514f5e3Sopenharmony_ci    Local<StringRef> arrStr3 = StringRef::NewFromUtf8(vm_, "[true,true,false,false,true]");
27164514f5e3Sopenharmony_ci    Local<JSValueRef> jsArr3 = JSON::Parse(vm_, arrStr3);
27174514f5e3Sopenharmony_ci    bool jsArr3IsArr = jsArr3->IsArray(vm_);
27184514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "jsArr3 is array : " << jsArr3IsArr;
27194514f5e3Sopenharmony_ci    Local<ArrayRef> arr3 = jsArr3;
27204514f5e3Sopenharmony_ci    uint32_t arr3Length = arr3->Length(vm_);
27214514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "arr3 length : " << arr3Length;
27224514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < arr3Length; ++i) {
27234514f5e3Sopenharmony_ci        Local<JSValueRef> arr3Item = ArrayRef::GetValueAt(vm_, arr3, i);
27244514f5e3Sopenharmony_ci        int b = arr3Item->BooleaValue(vm_);
27254514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "arr3 index : " << i << "  value : " << b;
27264514f5e3Sopenharmony_ci    }
27274514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_3_parse_array3 =======================================";
27284514f5e3Sopenharmony_ci}
27294514f5e3Sopenharmony_ci
27304514f5e3Sopenharmony_ci/* demo11 json 测试,json字符串转为数组。 json:
27314514f5e3Sopenharmony_ci * [
27324514f5e3Sopenharmony_ci * {
27334514f5e3Sopenharmony_ci * "key1": "val11",
27344514f5e3Sopenharmony_ci * "key2": "val12"
27354514f5e3Sopenharmony_ci * },
27364514f5e3Sopenharmony_ci * {
27374514f5e3Sopenharmony_ci * "key1": "val21",
27384514f5e3Sopenharmony_ci * "key2": "val22"
27394514f5e3Sopenharmony_ci * },
27404514f5e3Sopenharmony_ci * {
27414514f5e3Sopenharmony_ci * "key1": "val31",
27424514f5e3Sopenharmony_ci * "key2": "val32"
27434514f5e3Sopenharmony_ci * }
27444514f5e3Sopenharmony_ci * ]
27454514f5e3Sopenharmony_ci */
27464514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo11_json_test_3_parse_array4)
27474514f5e3Sopenharmony_ci{
27484514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_3_parse_array4 =======================================";
27494514f5e3Sopenharmony_ci    LocalScope scope(vm_);
27504514f5e3Sopenharmony_ci
27514514f5e3Sopenharmony_ci    // json 对象数组。
27524514f5e3Sopenharmony_ci    // json: [{"key1": "val11","key2": "val12"},{"key1": "val21","key2": "val22"},{"key1": "val31","key2": "val32"}]
27534514f5e3Sopenharmony_ci    Local<StringRef> arrStr4 =
27544514f5e3Sopenharmony_ci        StringRef::NewFromUtf8(vm_, "[{\"key1\":\"val11\",\"key2\":\"val12\"},{\"key1\":\"val21\",\"key2\":\"val22\"},{"
27554514f5e3Sopenharmony_ci        "\"key1\":\"val31\",\"key2\":\"val32\"}]");
27564514f5e3Sopenharmony_ci    Local<JSValueRef> jsArr4 = JSON::Parse(vm_, arrStr4);
27574514f5e3Sopenharmony_ci    bool jsArr4IsArr = jsArr4->IsArray(vm_);
27584514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "jsArr4 is array : " << jsArr4IsArr;
27594514f5e3Sopenharmony_ci    Local<ArrayRef> arr4 = jsArr4;
27604514f5e3Sopenharmony_ci    uint32_t arr4Length = arr4->Length(vm_);
27614514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "arr4 length : " << arr4Length;
27624514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < arr4Length; ++i) {
27634514f5e3Sopenharmony_ci        Local<JSValueRef> jsArr4Item = ArrayRef::GetValueAt(vm_, arr4, i);
27644514f5e3Sopenharmony_ci        Local<ObjectRef> obj = jsArr4Item->ToObject(vm_);
27654514f5e3Sopenharmony_ci
27664514f5e3Sopenharmony_ci        Local<JSValueRef> objVal1 = obj->Get(vm_, StringRef::NewFromUtf8(vm_, "key1"));
27674514f5e3Sopenharmony_ci        Local<JSValueRef> objVal2 = obj->Get(vm_, StringRef::NewFromUtf8(vm_, "key2"));
27684514f5e3Sopenharmony_ci
27694514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "arr4 index : " << i << "  key1 : " << objVal1->ToString(vm_)->ToString(vm_);
27704514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "arr4 index : " << i << "  key2 : " << objVal2->ToString(vm_)->ToString(vm_);
27714514f5e3Sopenharmony_ci    }
27724514f5e3Sopenharmony_ci
27734514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_3_parse_array4 =======================================";
27744514f5e3Sopenharmony_ci}
27754514f5e3Sopenharmony_ci
27764514f5e3Sopenharmony_ci/* demo11 json 测试,json字符串转为数组。 json:
27774514f5e3Sopenharmony_ci * ["val1",222,true,{"key1": "val1","key2": "val2"}]
27784514f5e3Sopenharmony_ci */
27794514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo11_json_test_3_parse_array5)
27804514f5e3Sopenharmony_ci{
27814514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_3_parse_array5 =======================================";
27824514f5e3Sopenharmony_ci    LocalScope scope(vm_);
27834514f5e3Sopenharmony_ci
27844514f5e3Sopenharmony_ci    // json 数组: ["val1",222,true,{"key1": "val1","key2": "val2"}]
27854514f5e3Sopenharmony_ci    Local<StringRef> arrStr5 = StringRef::NewFromUtf8(vm_, "[\"val1\",222,true,{\"key1\":\"val1\",\"key2\":\"val1\"}]");
27864514f5e3Sopenharmony_ci    Local<JSValueRef> jsArr5 = JSON::Parse(vm_, arrStr5);
27874514f5e3Sopenharmony_ci    bool jsArr5IsArr = jsArr5->IsArray(vm_);
27884514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "jsArr5 is array : " << jsArr5IsArr;
27894514f5e3Sopenharmony_ci    Local<ArrayRef> arr5 = jsArr5;
27904514f5e3Sopenharmony_ci    uint32_t arr5Length = arr5->Length(vm_);
27914514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "arr5 length : " << arr5Length;
27924514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < arr5Length; ++i) {
27934514f5e3Sopenharmony_ci        Local<JSValueRef> arr5Item = ArrayRef::GetValueAt(vm_, arr5, i);
27944514f5e3Sopenharmony_ci        if (arr5Item->IsString(vm_)) {
27954514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "arr5 index : " << i << "  value : " << arr5Item->ToString(vm_)->ToString(vm_);
27964514f5e3Sopenharmony_ci        } else if (arr5Item->IsNumber()) {
27974514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "arr5 index : " << i << "  value : " << arr5Item->Int32Value(vm_);
27984514f5e3Sopenharmony_ci        } else if (arr5Item->IsBoolean()) {
27994514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "arr5 index : " << i << "  value : " << arr5Item->ToBoolean(vm_)->Value();
28004514f5e3Sopenharmony_ci        } else if (arr5Item->IsObject(vm_)) {
28014514f5e3Sopenharmony_ci            Local<ObjectRef> obj = arr5Item->ToObject(vm_);
28024514f5e3Sopenharmony_ci            Local<ObjectRef> val1 = obj->Get(vm_, StringRef::NewFromUtf8(vm_, "key1"));
28034514f5e3Sopenharmony_ci            Local<ObjectRef> val2 = obj->Get(vm_, StringRef::NewFromUtf8(vm_, "key2"));
28044514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "arr5 index : " << i << "  key1 : " << val1->ToString(vm_)->ToString(vm_);
28054514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "arr5 index : " << i << "  key2 : " << val2->ToString(vm_)->ToString(vm_);
28064514f5e3Sopenharmony_ci        } else {
28074514f5e3Sopenharmony_ci            GTEST_LOG_(INFO) << "arr5 index : " << i << "  not type !";
28084514f5e3Sopenharmony_ci        }
28094514f5e3Sopenharmony_ci    }
28104514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_3_parse_array5 =======================================";
28114514f5e3Sopenharmony_ci}
28124514f5e3Sopenharmony_ci
28134514f5e3Sopenharmony_ci/* demo11 json 测试,数组转json字符串。 json:
28144514f5e3Sopenharmony_ci * ["val0","val1","val2","val3"]
28154514f5e3Sopenharmony_ci * [
28164514f5e3Sopenharmony_ci * {
28174514f5e3Sopenharmony_ci * "key1": "val11",
28184514f5e3Sopenharmony_ci * "key2": "val12",
28194514f5e3Sopenharmony_ci * "key3": "val13"
28204514f5e3Sopenharmony_ci * },
28214514f5e3Sopenharmony_ci * {
28224514f5e3Sopenharmony_ci * "key1": "val21",
28234514f5e3Sopenharmony_ci * "key2": "val22",
28244514f5e3Sopenharmony_ci * "key3": "val23"
28254514f5e3Sopenharmony_ci * }
28264514f5e3Sopenharmony_ci * ]
28274514f5e3Sopenharmony_ci * [
28284514f5e3Sopenharmony_ci * "val1",
28294514f5e3Sopenharmony_ci * 222,
28304514f5e3Sopenharmony_ci * true,
28314514f5e3Sopenharmony_ci * {
28324514f5e3Sopenharmony_ci * "key1": "val1",
28334514f5e3Sopenharmony_ci * "key2": "val2"
28344514f5e3Sopenharmony_ci * }
28354514f5e3Sopenharmony_ci * ]
28364514f5e3Sopenharmony_ci */
28374514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo11_json_test_4_stringify_array1)
28384514f5e3Sopenharmony_ci{
28394514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_4_stringify_array1 =======================================";
28404514f5e3Sopenharmony_ci    LocalScope scope(vm_);
28414514f5e3Sopenharmony_ci    Local<ArrayRef> arr = ArrayRef::New(vm_);
28424514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr, 0, StringRef::NewFromUtf8(vm_, "val0"));
28434514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr, 1, StringRef::NewFromUtf8(vm_, "val1"));
28444514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr, 2, StringRef::NewFromUtf8(vm_, "val2"));
28454514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr, 3, StringRef::NewFromUtf8(vm_, "val3"));
28464514f5e3Sopenharmony_ci    Local<JSValueRef> json1 = JSON::Stringify(vm_, arr);
28474514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << " js arr 1 json : " << json1->ToString(vm_)->ToString(vm_);
28484514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_4_stringify_array1 =======================================";
28494514f5e3Sopenharmony_ci}
28504514f5e3Sopenharmony_ci
28514514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo11_json_test_4_stringify_array2)
28524514f5e3Sopenharmony_ci{
28534514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_4_stringify_array2 =======================================";
28544514f5e3Sopenharmony_ci    Local<ObjectRef> obj1 = ObjectRef::New(vm_);
28554514f5e3Sopenharmony_ci    obj1->Set(vm_, StringRef::NewFromUtf8(vm_, "key1"), StringRef::NewFromUtf8(vm_, "val11"));
28564514f5e3Sopenharmony_ci    obj1->Set(vm_, StringRef::NewFromUtf8(vm_, "key2"), StringRef::NewFromUtf8(vm_, "val12"));
28574514f5e3Sopenharmony_ci    obj1->Set(vm_, StringRef::NewFromUtf8(vm_, "key3"), StringRef::NewFromUtf8(vm_, "val13"));
28584514f5e3Sopenharmony_ci    Local<ObjectRef> obj2 = ObjectRef::New(vm_);
28594514f5e3Sopenharmony_ci    obj2->Set(vm_, StringRef::NewFromUtf8(vm_, "key1"), StringRef::NewFromUtf8(vm_, "val21"));
28604514f5e3Sopenharmony_ci    obj2->Set(vm_, StringRef::NewFromUtf8(vm_, "key2"), StringRef::NewFromUtf8(vm_, "val22"));
28614514f5e3Sopenharmony_ci    obj2->Set(vm_, StringRef::NewFromUtf8(vm_, "key3"), StringRef::NewFromUtf8(vm_, "val23"));
28624514f5e3Sopenharmony_ci    Local<ArrayRef> arr = ArrayRef::New(vm_);
28634514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr, 0, obj1);
28644514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr, 1, obj2);
28654514f5e3Sopenharmony_ci    Local<JSValueRef> json2 = JSON::Stringify(vm_, arr);
28664514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << " js arr 2 json : " << json2->ToString(vm_)->ToString(vm_);
28674514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_4_stringify_array2 =======================================";
28684514f5e3Sopenharmony_ci}
28694514f5e3Sopenharmony_ci
28704514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo11_json_test_4_stringify_array3)
28714514f5e3Sopenharmony_ci{
28724514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_4_stringify_array3 =======================================";
28734514f5e3Sopenharmony_ci    Local<ObjectRef> obj = ObjectRef::New(vm_);
28744514f5e3Sopenharmony_ci    obj->Set(vm_, StringRef::NewFromUtf8(vm_, "key1"), StringRef::NewFromUtf8(vm_, "val1"));
28754514f5e3Sopenharmony_ci    obj->Set(vm_, StringRef::NewFromUtf8(vm_, "key2"), StringRef::NewFromUtf8(vm_, "val2"));
28764514f5e3Sopenharmony_ci    Local<ArrayRef> arr = ArrayRef::New(vm_);
28774514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr, 0, StringRef::NewFromUtf8(vm_, "val1"));
28784514f5e3Sopenharmony_ci    int num2 = 222; // random number
28794514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr, 1, NumberRef::New(vm_, num2));
28804514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr, 2, BooleanRef::New(vm_, true));
28814514f5e3Sopenharmony_ci    ArrayRef::SetValueAt(vm_, arr, 3, obj);
28824514f5e3Sopenharmony_ci    Local<JSValueRef> json3 = JSON::Stringify(vm_, arr);
28834514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << " js arr 3 json : " << json3->ToString(vm_)->ToString(vm_);
28844514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo11_json_test_4_stringify_array3 =======================================";
28854514f5e3Sopenharmony_ci}
28864514f5e3Sopenharmony_ci
28874514f5e3Sopenharmony_ci/* demo12 异常的抛出和处理 */
28884514f5e3Sopenharmony_ci// 抛出异常的测试函数
28894514f5e3Sopenharmony_ciLocal<JSValueRef> ThrowErrorFuncTest(const EcmaVM *vm, Local<NumberRef> par)
28904514f5e3Sopenharmony_ci{
28914514f5e3Sopenharmony_ci    int i = par->Int32Value(vm);
28924514f5e3Sopenharmony_ci    if (i == 0) {
28934514f5e3Sopenharmony_ci        std::string errStr = std::string("function:").append(__func__).append("err:Error");
28944514f5e3Sopenharmony_ci        Local<JSValueRef> error = Exception::Error(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
28954514f5e3Sopenharmony_ci        JSNApi::ThrowException(vm, error);
28964514f5e3Sopenharmony_ci        return JSValueRef::Undefined(vm);
28974514f5e3Sopenharmony_ci    }
28984514f5e3Sopenharmony_ci    int num = 2; // 函数的功能,原数字的2倍。
28994514f5e3Sopenharmony_ci    return NumberRef::New(vm, i * num);
29004514f5e3Sopenharmony_ci}
29014514f5e3Sopenharmony_ci
29024514f5e3Sopenharmony_ciLocal<JSValueRef> ThrowRangeErrorFuncTest(const EcmaVM *vm, Local<NumberRef> par)
29034514f5e3Sopenharmony_ci{
29044514f5e3Sopenharmony_ci    int i = par->Int32Value(vm);
29054514f5e3Sopenharmony_ci    if (i == 0) {
29064514f5e3Sopenharmony_ci        std::string errStr = std::string("function:").append(__func__).append("err:RangeError");
29074514f5e3Sopenharmony_ci        Local<JSValueRef> error = Exception::RangeError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
29084514f5e3Sopenharmony_ci        JSNApi::ThrowException(vm, error);
29094514f5e3Sopenharmony_ci        return JSValueRef::Undefined(vm);
29104514f5e3Sopenharmony_ci    }
29114514f5e3Sopenharmony_ci    int num = 2; // 函数的功能,原数字的2倍。
29124514f5e3Sopenharmony_ci    return NumberRef::New(vm, i * num);
29134514f5e3Sopenharmony_ci}
29144514f5e3Sopenharmony_ci
29154514f5e3Sopenharmony_ciLocal<JSValueRef> ThrowReferenceErrorFuncTest(const EcmaVM *vm, Local<NumberRef> par)
29164514f5e3Sopenharmony_ci{
29174514f5e3Sopenharmony_ci    int i = par->Int32Value(vm);
29184514f5e3Sopenharmony_ci    if (i == 0) {
29194514f5e3Sopenharmony_ci        std::string errStr = std::string("function:").append(__func__).append("err:ReferenceError");
29204514f5e3Sopenharmony_ci        Local<JSValueRef> error = Exception::ReferenceError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
29214514f5e3Sopenharmony_ci        JSNApi::ThrowException(vm, error);
29224514f5e3Sopenharmony_ci        return JSValueRef::Undefined(vm);
29234514f5e3Sopenharmony_ci    }
29244514f5e3Sopenharmony_ci    int num = 2; // 函数的功能,原数字的2倍。
29254514f5e3Sopenharmony_ci    return NumberRef::New(vm, i * num);
29264514f5e3Sopenharmony_ci}
29274514f5e3Sopenharmony_ci
29284514f5e3Sopenharmony_ciLocal<JSValueRef> ThrowSyntaxErrorFuncTest(const EcmaVM *vm, Local<NumberRef> par)
29294514f5e3Sopenharmony_ci{
29304514f5e3Sopenharmony_ci    int i = par->Int32Value(vm);
29314514f5e3Sopenharmony_ci    if (i == 0) {
29324514f5e3Sopenharmony_ci        std::string errStr = std::string("function:").append(__func__).append("err:SyntaxError");
29334514f5e3Sopenharmony_ci        Local<JSValueRef> error = Exception::SyntaxError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
29344514f5e3Sopenharmony_ci        JSNApi::ThrowException(vm, error);
29354514f5e3Sopenharmony_ci        return JSValueRef::Undefined(vm);
29364514f5e3Sopenharmony_ci    }
29374514f5e3Sopenharmony_ci    int num = 2; // 函数的功能,原数字的2倍。
29384514f5e3Sopenharmony_ci    return NumberRef::New(vm, i * num);
29394514f5e3Sopenharmony_ci}
29404514f5e3Sopenharmony_ci
29414514f5e3Sopenharmony_ciLocal<JSValueRef> ThrowTypeErrorFuncTest(const EcmaVM *vm, Local<NumberRef> par)
29424514f5e3Sopenharmony_ci{
29434514f5e3Sopenharmony_ci    int i = par->Int32Value(vm);
29444514f5e3Sopenharmony_ci    if (i == 0) {
29454514f5e3Sopenharmony_ci        std::string errStr = std::string("function:").append(__func__).append("err:TypeError");
29464514f5e3Sopenharmony_ci        Local<JSValueRef> error = Exception::TypeError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
29474514f5e3Sopenharmony_ci        JSNApi::ThrowException(vm, error);
29484514f5e3Sopenharmony_ci        return JSValueRef::Undefined(vm);
29494514f5e3Sopenharmony_ci    }
29504514f5e3Sopenharmony_ci    int num = 2; // 函数的功能,原数字的2倍。
29514514f5e3Sopenharmony_ci    return NumberRef::New(vm, i * num);
29524514f5e3Sopenharmony_ci}
29534514f5e3Sopenharmony_ci
29544514f5e3Sopenharmony_ciLocal<JSValueRef> ThrowAggregateErrorFuncTest(const EcmaVM *vm, Local<NumberRef> par)
29554514f5e3Sopenharmony_ci{
29564514f5e3Sopenharmony_ci    int i = par->Int32Value(vm);
29574514f5e3Sopenharmony_ci    if (i == 0) {
29584514f5e3Sopenharmony_ci        std::string errStr = std::string("function:").append(__func__).append("err:AggregateError");
29594514f5e3Sopenharmony_ci        Local<JSValueRef> error = Exception::AggregateError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
29604514f5e3Sopenharmony_ci        JSNApi::ThrowException(vm, error);
29614514f5e3Sopenharmony_ci        return JSValueRef::Undefined(vm);
29624514f5e3Sopenharmony_ci    }
29634514f5e3Sopenharmony_ci    int num = 2; // 函数的功能,原数字的2倍。
29644514f5e3Sopenharmony_ci    return NumberRef::New(vm, i * num);
29654514f5e3Sopenharmony_ci}
29664514f5e3Sopenharmony_ci
29674514f5e3Sopenharmony_ciLocal<JSValueRef> ThrowEvalErrorFuncTest(const EcmaVM *vm, Local<NumberRef> par)
29684514f5e3Sopenharmony_ci{
29694514f5e3Sopenharmony_ci    int i = par->Int32Value(vm);
29704514f5e3Sopenharmony_ci    if (i == 0) {
29714514f5e3Sopenharmony_ci        std::string errStr = std::string("function:").append(__func__).append("err:EvalError");
29724514f5e3Sopenharmony_ci        Local<JSValueRef> error = Exception::EvalError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
29734514f5e3Sopenharmony_ci        JSNApi::ThrowException(vm, error);
29744514f5e3Sopenharmony_ci        return JSValueRef::Undefined(vm);
29754514f5e3Sopenharmony_ci    }
29764514f5e3Sopenharmony_ci    int num = 2; // 函数的功能,原数字的2倍。
29774514f5e3Sopenharmony_ci    return NumberRef::New(vm, i * num);
29784514f5e3Sopenharmony_ci}
29794514f5e3Sopenharmony_ci
29804514f5e3Sopenharmony_ciLocal<JSValueRef> ThrowOOMErrorFuncTest(const EcmaVM *vm, Local<NumberRef> par)
29814514f5e3Sopenharmony_ci{
29824514f5e3Sopenharmony_ci    int i = par->Int32Value(vm);
29834514f5e3Sopenharmony_ci    if (i == 0) {
29844514f5e3Sopenharmony_ci        std::string errStr = std::string("function:").append(__func__).append("err:OOMError");
29854514f5e3Sopenharmony_ci        Local<JSValueRef> error = Exception::OOMError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
29864514f5e3Sopenharmony_ci        JSNApi::ThrowException(vm, error);
29874514f5e3Sopenharmony_ci        return JSValueRef::Undefined(vm);
29884514f5e3Sopenharmony_ci    }
29894514f5e3Sopenharmony_ci    int num = 2; // 函数的功能,原数字的2倍。
29904514f5e3Sopenharmony_ci    return NumberRef::New(vm, i * num);
29914514f5e3Sopenharmony_ci}
29924514f5e3Sopenharmony_ci
29934514f5e3Sopenharmony_ciLocal<JSValueRef> ThrowTerminationErrorFuncTest(const EcmaVM *vm, Local<NumberRef> par)
29944514f5e3Sopenharmony_ci{
29954514f5e3Sopenharmony_ci    int i = par->Int32Value(vm);
29964514f5e3Sopenharmony_ci    if (i == 0) {
29974514f5e3Sopenharmony_ci        std::string errStr = std::string("function:").append(__func__).append("err:TerminationError");
29984514f5e3Sopenharmony_ci        Local<JSValueRef> error = Exception::TerminationError(vm, StringRef::NewFromUtf8(vm, errStr.c_str()));
29994514f5e3Sopenharmony_ci        JSNApi::ThrowException(vm, error);
30004514f5e3Sopenharmony_ci        return JSValueRef::Undefined(vm);
30014514f5e3Sopenharmony_ci    }
30024514f5e3Sopenharmony_ci    int num = 2; // 函数的功能,原数字的2倍。
30034514f5e3Sopenharmony_ci    return NumberRef::New(vm, i * num);
30044514f5e3Sopenharmony_ci}
30054514f5e3Sopenharmony_ci
30064514f5e3Sopenharmony_ci// 处理异常,用 JSNApi 类中的函数。
30074514f5e3Sopenharmony_civoid ClearAndPrintException4JSNApi(const EcmaVM *vm, const std::string log)
30084514f5e3Sopenharmony_ci{
30094514f5e3Sopenharmony_ci    if (!JSNApi::HasPendingException(vm)) {
30104514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << log << " no exception .";
30114514f5e3Sopenharmony_ci        return;
30124514f5e3Sopenharmony_ci    }
30134514f5e3Sopenharmony_ci
30144514f5e3Sopenharmony_ci    JSNApi::PrintExceptionInfo(vm);
30154514f5e3Sopenharmony_ci    Local<ObjectRef> exception = JSNApi::GetAndClearUncaughtException(vm);
30164514f5e3Sopenharmony_ci    std::string exceptionName = exception->Get(vm, StringRef::NewFromUtf8(vm, "name"))->ToString(vm)->ToString(vm);
30174514f5e3Sopenharmony_ci    std::string exceptionMessage = exception->Get(vm, StringRef::NewFromUtf8(vm,
30184514f5e3Sopenharmony_ci        "message"))->ToString(vm)->ToString(vm);
30194514f5e3Sopenharmony_ci    std::string exceptionStack = exception->Get(vm,
30204514f5e3Sopenharmony_ci        StringRef::NewFromUtf8(vm, "stack"))->ToString(vm)->ToString(vm);
30214514f5e3Sopenharmony_ci
30224514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception name : " << exceptionName;
30234514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception message : " << exceptionMessage;
30244514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception stack : " << exceptionStack;
30254514f5e3Sopenharmony_ci};
30264514f5e3Sopenharmony_ci
30274514f5e3Sopenharmony_ci// 处理异常,用 TryCatch 类中的函数。
30284514f5e3Sopenharmony_civoid ClearAndPrintException4TryCatch1(const EcmaVM *vm, const std::string log)
30294514f5e3Sopenharmony_ci{
30304514f5e3Sopenharmony_ci    TryCatch tryCatch(vm);
30314514f5e3Sopenharmony_ci    if (!tryCatch.HasCaught()) {
30324514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << log << " no exception .";
30334514f5e3Sopenharmony_ci        return;
30344514f5e3Sopenharmony_ci    }
30354514f5e3Sopenharmony_ci    Local<ObjectRef> exception = tryCatch.GetAndClearException();
30364514f5e3Sopenharmony_ci    std::string exceptionName = exception->Get(vm, StringRef::NewFromUtf8(vm, "name"))->ToString(vm)->ToString(vm);
30374514f5e3Sopenharmony_ci    std::string exceptionMessage = exception->Get(vm,
30384514f5e3Sopenharmony_ci        StringRef::NewFromUtf8(vm, "message"))->ToString(vm)->ToString(vm);
30394514f5e3Sopenharmony_ci    std::string exceptionStack = exception->Get(vm, StringRef::NewFromUtf8(vm, "stack"))->ToString(vm)->ToString(vm);
30404514f5e3Sopenharmony_ci
30414514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception name : " << exceptionName;
30424514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception message : " << exceptionMessage;
30434514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception stack : " << exceptionStack;
30444514f5e3Sopenharmony_ci};
30454514f5e3Sopenharmony_ci
30464514f5e3Sopenharmony_ci// 处理异常,用 TryCatch 类中的函数。
30474514f5e3Sopenharmony_civoid ClearAndPrintException4TryCatch2(const EcmaVM *vm, const std::string log)
30484514f5e3Sopenharmony_ci{
30494514f5e3Sopenharmony_ci    TryCatch tryCatch(vm);
30504514f5e3Sopenharmony_ci    if (!tryCatch.HasCaught()) {
30514514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << log << " no exception .";
30524514f5e3Sopenharmony_ci        return;
30534514f5e3Sopenharmony_ci    }
30544514f5e3Sopenharmony_ci
30554514f5e3Sopenharmony_ci    Local<ObjectRef> exception = tryCatch.GetException();
30564514f5e3Sopenharmony_ci    tryCatch.ClearException();
30574514f5e3Sopenharmony_ci    std::string exceptionName = exception->Get(vm, StringRef::NewFromUtf8(vm, "name"))->ToString(vm)->ToString(vm);
30584514f5e3Sopenharmony_ci    std::string exceptionMessage = exception->Get(vm,
30594514f5e3Sopenharmony_ci        StringRef::NewFromUtf8(vm, "message"))->ToString(vm)->ToString(vm);
30604514f5e3Sopenharmony_ci    std::string exceptionStack = exception->Get(vm, StringRef::NewFromUtf8(vm, "stack"))->ToString(vm)->ToString(vm);
30614514f5e3Sopenharmony_ci
30624514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception name : " << exceptionName;
30634514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception message : " << exceptionMessage;
30644514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception stack : " << exceptionStack;
30654514f5e3Sopenharmony_ci    tryCatch.Rethrow();
30664514f5e3Sopenharmony_ci};
30674514f5e3Sopenharmony_ci
30684514f5e3Sopenharmony_ci// 异常没有处理重新抛出异常,用 TryCatch 类中的函数。
30694514f5e3Sopenharmony_civoid PrintAndRethrowException4TryCatch3(const EcmaVM *vm, const std::string log)
30704514f5e3Sopenharmony_ci{
30714514f5e3Sopenharmony_ci    TryCatch tryCatch(vm);
30724514f5e3Sopenharmony_ci    if (!tryCatch.HasCaught()) {
30734514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << log << " no exception .";
30744514f5e3Sopenharmony_ci        return;
30754514f5e3Sopenharmony_ci    }
30764514f5e3Sopenharmony_ci
30774514f5e3Sopenharmony_ci    Local<ObjectRef> exception = tryCatch.GetAndClearException();
30784514f5e3Sopenharmony_ci    std::string exceptionName = exception->Get(vm, StringRef::NewFromUtf8(vm, "name"))->ToString(vm)->ToString(vm);
30794514f5e3Sopenharmony_ci    std::string exceptionMessage = exception->Get(vm,
30804514f5e3Sopenharmony_ci        StringRef::NewFromUtf8(vm, "message"))->ToString(vm)->ToString(vm);
30814514f5e3Sopenharmony_ci    std::string exceptionStack = exception->Get(vm, StringRef::NewFromUtf8(vm, "stack"))->ToString(vm)->ToString(vm);
30824514f5e3Sopenharmony_ci
30834514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception name : " << exceptionName;
30844514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception message : " << exceptionMessage;
30854514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << log << " exception stack : " << exceptionStack;
30864514f5e3Sopenharmony_ci    tryCatch.Rethrow();
30874514f5e3Sopenharmony_ci};
30884514f5e3Sopenharmony_ci
30894514f5e3Sopenharmony_ci/* demo12 异常的抛出和处理 */
30904514f5e3Sopenharmony_ciHWTEST_F_L0(JSNApiSampleTest, sample_demo12_exception_test)
30914514f5e3Sopenharmony_ci{
30924514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo12_exception_test =======================================";
30934514f5e3Sopenharmony_ci    LocalScope scope(vm_);
30944514f5e3Sopenharmony_ci    // Error
30954514f5e3Sopenharmony_ci    Local<JSValueRef> exception1 = ThrowErrorFuncTest(vm_, NumberRef::New(vm_, 0));
30964514f5e3Sopenharmony_ci    if (exception1->IsUndefined()) {
30974514f5e3Sopenharmony_ci        ClearAndPrintException4JSNApi(vm_, "ThrowErrorFuncTest");
30984514f5e3Sopenharmony_ci    }
30994514f5e3Sopenharmony_ci    // RangeError
31004514f5e3Sopenharmony_ci    Local<JSValueRef> exception2 = ThrowRangeErrorFuncTest(vm_, NumberRef::New(vm_, 0));
31014514f5e3Sopenharmony_ci    if (exception2->IsUndefined()) {
31024514f5e3Sopenharmony_ci        ClearAndPrintException4TryCatch1(vm_, "ThrowRangeErrorFuncTest");
31034514f5e3Sopenharmony_ci    }
31044514f5e3Sopenharmony_ci    // ReferenceError
31054514f5e3Sopenharmony_ci    Local<JSValueRef> exception3 = ThrowReferenceErrorFuncTest(vm_, NumberRef::New(vm_, 0));
31064514f5e3Sopenharmony_ci    if (exception3->IsUndefined()) {
31074514f5e3Sopenharmony_ci        ClearAndPrintException4TryCatch2(vm_, "ThrowReferenceErrorFuncTest");
31084514f5e3Sopenharmony_ci    }
31094514f5e3Sopenharmony_ci    // SyntaxError
31104514f5e3Sopenharmony_ci    Local<JSValueRef> exception4 = ThrowSyntaxErrorFuncTest(vm_, NumberRef::New(vm_, 0));
31114514f5e3Sopenharmony_ci    if (exception4->IsUndefined()) {
31124514f5e3Sopenharmony_ci        PrintAndRethrowException4TryCatch3(vm_, "ThrowSyntaxErrorFuncTest");
31134514f5e3Sopenharmony_ci        ClearAndPrintException4TryCatch1(vm_, "ThrowSyntaxErrorFuncTest");
31144514f5e3Sopenharmony_ci    }
31154514f5e3Sopenharmony_ci    // TypeError
31164514f5e3Sopenharmony_ci    Local<JSValueRef> exception5 = ThrowTypeErrorFuncTest(vm_, NumberRef::New(vm_, 0));
31174514f5e3Sopenharmony_ci    if (exception5->IsUndefined()) {
31184514f5e3Sopenharmony_ci        ClearAndPrintException4TryCatch1(vm_, "ThrowTypeErrorFuncTest");
31194514f5e3Sopenharmony_ci    }
31204514f5e3Sopenharmony_ci    // AggregateError
31214514f5e3Sopenharmony_ci    Local<JSValueRef> exception6 = ThrowAggregateErrorFuncTest(vm_, NumberRef::New(vm_, 0));
31224514f5e3Sopenharmony_ci    if (exception6->IsUndefined()) {
31234514f5e3Sopenharmony_ci        ClearAndPrintException4TryCatch1(vm_, "ThrowAggregateErrorFuncTest");
31244514f5e3Sopenharmony_ci    }
31254514f5e3Sopenharmony_ci    // EvalError
31264514f5e3Sopenharmony_ci    Local<JSValueRef> exception7 = ThrowEvalErrorFuncTest(vm_, NumberRef::New(vm_, 0));
31274514f5e3Sopenharmony_ci    if (exception7->IsUndefined()) {
31284514f5e3Sopenharmony_ci        ClearAndPrintException4TryCatch1(vm_, "ThrowEvalErrorFuncTest");
31294514f5e3Sopenharmony_ci    }
31304514f5e3Sopenharmony_ci    // OOMError
31314514f5e3Sopenharmony_ci    Local<JSValueRef> exception8 = ThrowOOMErrorFuncTest(vm_, NumberRef::New(vm_, 0));
31324514f5e3Sopenharmony_ci    if (exception8->IsUndefined()) {
31334514f5e3Sopenharmony_ci        ClearAndPrintException4TryCatch1(vm_, "ThrowOOMErrorFuncTest");
31344514f5e3Sopenharmony_ci    }
31354514f5e3Sopenharmony_ci    // TerminationError
31364514f5e3Sopenharmony_ci    Local<JSValueRef> exception9 = ThrowTerminationErrorFuncTest(vm_, NumberRef::New(vm_, 0));
31374514f5e3Sopenharmony_ci    if (exception9->IsUndefined()) {
31384514f5e3Sopenharmony_ci        ClearAndPrintException4TryCatch1(vm_, "ThrowTerminationErrorFuncTest");
31394514f5e3Sopenharmony_ci    }
31404514f5e3Sopenharmony_ci    GTEST_LOG_(INFO) << "sample_demo12_exception_test =======================================";
31414514f5e3Sopenharmony_ci}
31424514f5e3Sopenharmony_ci}