14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2024 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 "ecmascript/builtins/builtins_boolean.h" 174514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_function.h" 184514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_shared_async_function.h" 194514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_shared_function.h" 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h" 224514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h" 234514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h" 244514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 254514f5e3Sopenharmony_ci#include "ecmascript/js_array.h" 264514f5e3Sopenharmony_ci#include "ecmascript/js_function.h" 274514f5e3Sopenharmony_ci#include "ecmascript/js_object-inl.h" 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h" 304514f5e3Sopenharmony_ci#include "ecmascript/tagged_array-inl.h" 314514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 324514f5e3Sopenharmony_ci 334514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 344514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins; 354514f5e3Sopenharmony_ciusing BuiltinsBase = panda::ecmascript::base::BuiltinsBase; 364514f5e3Sopenharmony_ciusing JSArray = panda::ecmascript::JSArray; 374514f5e3Sopenharmony_ci 384514f5e3Sopenharmony_cinamespace panda::test { 394514f5e3Sopenharmony_ciclass BuiltinsSharedFunctionTest : public BaseTestWithScope<false> { 404514f5e3Sopenharmony_ci}; 414514f5e3Sopenharmony_ci 424514f5e3Sopenharmony_ci// native function for test apply and call 434514f5e3Sopenharmony_ciJSTaggedValue TestFunctionApplyAndCall(EcmaRuntimeCallInfo *argv) 444514f5e3Sopenharmony_ci{ 454514f5e3Sopenharmony_ci JSThread *thread = argv->GetThread(); 464514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 474514f5e3Sopenharmony_ci 484514f5e3Sopenharmony_ci int result = 0; 494514f5e3Sopenharmony_ci for (uint32_t index = 0; index < argv->GetArgsNumber(); ++index) { 504514f5e3Sopenharmony_ci result += BuiltinsBase::GetCallArg(argv, index)->GetInt(); 514514f5e3Sopenharmony_ci } 524514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> thisValue(BuiltinsBase::GetThis(argv)); 534514f5e3Sopenharmony_ci 544514f5e3Sopenharmony_ci JSTaggedValue testA = JSObject::GetProperty(thread, thisValue, 554514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_a"))).GetValue().GetTaggedValue(); 564514f5e3Sopenharmony_ci JSTaggedValue testB = JSObject::GetProperty(thread, thisValue, 574514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_b"))).GetValue().GetTaggedValue(); 584514f5e3Sopenharmony_ci 594514f5e3Sopenharmony_ci result = result + testA.GetInt() + testB.GetInt(); 604514f5e3Sopenharmony_ci return BuiltinsBase::GetTaggedInt(result); 614514f5e3Sopenharmony_ci} 624514f5e3Sopenharmony_ci 634514f5e3Sopenharmony_cienum class AlgorithmType { 644514f5e3Sopenharmony_ci PROTOTYPE_APPLY, 654514f5e3Sopenharmony_ci PROTOTYPE_BIND, 664514f5e3Sopenharmony_ci PROTOTYPE_CALL, 674514f5e3Sopenharmony_ci}; 684514f5e3Sopenharmony_ci 694514f5e3Sopenharmony_cistatic JSTaggedValue FunctionAlgorithm(JSThread *thread, JSHandle<JSFunction> &thisArg, 704514f5e3Sopenharmony_ci std::vector<JSTaggedValue> &args, uint32_t argLen, 714514f5e3Sopenharmony_ci AlgorithmType type = AlgorithmType::PROTOTYPE_APPLY) 724514f5e3Sopenharmony_ci{ 734514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfos = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), argLen); 744514f5e3Sopenharmony_ci ecmaRuntimeCallInfos->SetFunction(JSTaggedValue::Undefined()); 754514f5e3Sopenharmony_ci ecmaRuntimeCallInfos->SetThis(thisArg.GetTaggedValue()); 764514f5e3Sopenharmony_ci for (size_t i = 0; i < args.size(); i++) { 774514f5e3Sopenharmony_ci ecmaRuntimeCallInfos->SetCallArg(i, args[i]); 784514f5e3Sopenharmony_ci } 794514f5e3Sopenharmony_ci auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfos); 804514f5e3Sopenharmony_ci JSTaggedValue result; 814514f5e3Sopenharmony_ci switch (type) { 824514f5e3Sopenharmony_ci case AlgorithmType::PROTOTYPE_BIND: 834514f5e3Sopenharmony_ci result = BuiltinsFunction::FunctionPrototypeBind(ecmaRuntimeCallInfos); 844514f5e3Sopenharmony_ci break; 854514f5e3Sopenharmony_ci case AlgorithmType::PROTOTYPE_APPLY: 864514f5e3Sopenharmony_ci result = BuiltinsFunction::FunctionPrototypeApply(ecmaRuntimeCallInfos); 874514f5e3Sopenharmony_ci break; 884514f5e3Sopenharmony_ci case AlgorithmType::PROTOTYPE_CALL: 894514f5e3Sopenharmony_ci result = BuiltinsFunction::FunctionPrototypeCall(ecmaRuntimeCallInfos); 904514f5e3Sopenharmony_ci break; 914514f5e3Sopenharmony_ci default: 924514f5e3Sopenharmony_ci break; 934514f5e3Sopenharmony_ci } 944514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 954514f5e3Sopenharmony_ci return result; 964514f5e3Sopenharmony_ci} 974514f5e3Sopenharmony_ci 984514f5e3Sopenharmony_ci// func Constructor 994514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, FunctionConstructor) 1004514f5e3Sopenharmony_ci{ 1014514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1024514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSharedFunction::SharedFunctionConstructor(ecmaRuntimeCallInfo); 1034514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::Exception().GetRawData()); 1044514f5e3Sopenharmony_ci} 1054514f5e3Sopenharmony_ci 1064514f5e3Sopenharmony_ci// async func Constructor 1074514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, AsyncFunctionConstructor) 1084514f5e3Sopenharmony_ci{ 1094514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1104514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSharedAsyncFunction::SharedAsyncFunctionConstructor(ecmaRuntimeCallInfo); 1114514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::Exception().GetRawData()); 1124514f5e3Sopenharmony_ci} 1134514f5e3Sopenharmony_ci 1144514f5e3Sopenharmony_ci// func.apply(thisArg) 1154514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, FunctionPrototypeApply) 1164514f5e3Sopenharmony_ci{ 1174514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 1184514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1194514f5e3Sopenharmony_ci // ecma 19.2.3.1: func 1204514f5e3Sopenharmony_ci JSHandle<JSFunction> func = factory->NewSFunction(env, reinterpret_cast<void *>(TestFunctionApplyAndCall)); 1214514f5e3Sopenharmony_ci 1224514f5e3Sopenharmony_ci // ecma 19.2.3.1: thisArg 1234514f5e3Sopenharmony_ci JSHandle<JSObject> thisArg(thread, env->GetGlobalObject()); 1244514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(thisArg), 1254514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_a")), 1264514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(thread, JSTaggedValue(1))); 1274514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(thisArg), 1284514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_b")), 1294514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(thread, JSTaggedValue(2))); 1304514f5e3Sopenharmony_ci 1314514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{thisArg.GetTaggedValue()}; 1324514f5e3Sopenharmony_ci auto result = FunctionAlgorithm(thread, func, args, 6, AlgorithmType::PROTOTYPE_APPLY); 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue(3).GetRawData()); 1354514f5e3Sopenharmony_ci 1364514f5e3Sopenharmony_ci JSObject::DeleteProperty(thread, (thisArg), 1374514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_a"))); 1384514f5e3Sopenharmony_ci JSObject::DeleteProperty(thread, (thisArg), 1394514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_b"))); 1404514f5e3Sopenharmony_ci} 1414514f5e3Sopenharmony_ci 1424514f5e3Sopenharmony_ci// func.apply(thisArg, argArray) 1434514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, FunctionPrototypeApply1) 1444514f5e3Sopenharmony_ci{ 1454514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 1464514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1474514f5e3Sopenharmony_ci 1484514f5e3Sopenharmony_ci // ecma 19.2.3.1: func 1494514f5e3Sopenharmony_ci JSHandle<JSFunction> func = factory->NewSFunction(env, reinterpret_cast<void *>(TestFunctionApplyAndCall)); 1504514f5e3Sopenharmony_ci 1514514f5e3Sopenharmony_ci // ecma 19.2.3.1: thisArg 1524514f5e3Sopenharmony_ci JSHandle<JSObject> thisArg(thread, env->GetGlobalObject()); 1534514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(thisArg), 1544514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_a")), 1554514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(thread, JSTaggedValue(10))); 1564514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(thisArg), 1574514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_b")), 1584514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(thread, JSTaggedValue(20))); 1594514f5e3Sopenharmony_ci 1604514f5e3Sopenharmony_ci // ecma 19.2.3.1: argArray 1614514f5e3Sopenharmony_ci JSHandle<JSObject> array(JSArray::ArrayCreate(thread, JSTaggedNumber(2))); 1624514f5e3Sopenharmony_ci PropertyDescriptor desc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(30))); 1634514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, array, JSHandle<JSTaggedValue>(thread, JSTaggedValue(0)), desc); 1644514f5e3Sopenharmony_ci 1654514f5e3Sopenharmony_ci PropertyDescriptor desc1(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(40))); 1664514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, array, JSHandle<JSTaggedValue>(thread, JSTaggedValue(1)), desc1); 1674514f5e3Sopenharmony_ci 1684514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{thisArg.GetTaggedValue(), array.GetTaggedValue()}; 1694514f5e3Sopenharmony_ci auto result = FunctionAlgorithm(thread, func, args, 8, AlgorithmType::PROTOTYPE_APPLY); 1704514f5e3Sopenharmony_ci 1714514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue(100).GetRawData()); 1724514f5e3Sopenharmony_ci 1734514f5e3Sopenharmony_ci JSObject::DeleteProperty(thread, (thisArg), 1744514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_a"))); 1754514f5e3Sopenharmony_ci JSObject::DeleteProperty(thread, (thisArg), 1764514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_b"))); 1774514f5e3Sopenharmony_ci} 1784514f5e3Sopenharmony_ci 1794514f5e3Sopenharmony_ci// target.bind(thisArg) 1804514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, FunctionPrototypeBind) 1814514f5e3Sopenharmony_ci{ 1824514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 1834514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1844514f5e3Sopenharmony_ci 1854514f5e3Sopenharmony_ci JSHandle<JSFunction> target = factory->NewSFunction(env); 1864514f5e3Sopenharmony_ci JSHandle<JSObject> thisArg(thread, env->GetGlobalObject()); 1874514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{thisArg.GetTaggedValue()}; 1884514f5e3Sopenharmony_ci auto result = FunctionAlgorithm(thread, target, args, 6, AlgorithmType::PROTOTYPE_BIND); 1894514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsECMAObject()); 1904514f5e3Sopenharmony_ci 1914514f5e3Sopenharmony_ci JSHandle<JSBoundFunction> resultFunc(thread, reinterpret_cast<TaggedObject *>(result.GetRawData())); 1924514f5e3Sopenharmony_ci // test BoundTarget 1934514f5e3Sopenharmony_ci ASSERT_EQ(resultFunc->GetBoundTarget(), target.GetTaggedValue()); 1944514f5e3Sopenharmony_ci // test BoundThis 1954514f5e3Sopenharmony_ci ASSERT_EQ(resultFunc->GetBoundThis(), thisArg.GetTaggedValue()); 1964514f5e3Sopenharmony_ci // test BoundArguments 1974514f5e3Sopenharmony_ci JSHandle<TaggedArray> array(thread, resultFunc->GetBoundArguments()); 1984514f5e3Sopenharmony_ci ASSERT_EQ(array->GetLength(), 0U); 1994514f5e3Sopenharmony_ci} 2004514f5e3Sopenharmony_ci 2014514f5e3Sopenharmony_ci// target.bind(thisArg, 123, "helloworld") 2024514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, FunctionPrototypeBind1) 2034514f5e3Sopenharmony_ci{ 2044514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2054514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2064514f5e3Sopenharmony_ci 2074514f5e3Sopenharmony_ci JSHandle<JSFunction> target = factory->NewSFunction(env); 2084514f5e3Sopenharmony_ci JSHandle<JSObject> thisArg(thread, env->GetGlobalObject()); 2094514f5e3Sopenharmony_ci JSHandle<EcmaString> str = factory->NewFromASCII("helloworld"); 2104514f5e3Sopenharmony_ci 2114514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{thisArg.GetTaggedValue(), JSTaggedValue(static_cast<int32_t>(123)), 2124514f5e3Sopenharmony_ci str.GetTaggedValue()}; 2134514f5e3Sopenharmony_ci auto result = FunctionAlgorithm(thread, target, args, 10, AlgorithmType::PROTOTYPE_BIND); 2144514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsECMAObject()); 2154514f5e3Sopenharmony_ci 2164514f5e3Sopenharmony_ci JSHandle<JSBoundFunction> resultFunc(thread, reinterpret_cast<TaggedObject *>(result.GetRawData())); 2174514f5e3Sopenharmony_ci // test BoundTarget 2184514f5e3Sopenharmony_ci ASSERT_EQ(resultFunc->GetBoundTarget(), target.GetTaggedValue()); 2194514f5e3Sopenharmony_ci // test BoundThis 2204514f5e3Sopenharmony_ci ASSERT_EQ(resultFunc->GetBoundThis(), thisArg.GetTaggedValue()); 2214514f5e3Sopenharmony_ci // test BoundArguments 2224514f5e3Sopenharmony_ci JSHandle<TaggedArray> array(thread, resultFunc->GetBoundArguments()); 2234514f5e3Sopenharmony_ci ASSERT_EQ(array->GetLength(), 2U); 2244514f5e3Sopenharmony_ci JSTaggedValue elem = array->Get(0); 2254514f5e3Sopenharmony_ci JSTaggedValue elem1 = array->Get(1); 2264514f5e3Sopenharmony_ci ASSERT_EQ(elem.GetRawData(), JSTaggedValue(123).GetRawData()); 2274514f5e3Sopenharmony_ci 2284514f5e3Sopenharmony_ci ASSERT_EQ(elem1.GetRawData(), str.GetTaggedType()); 2294514f5e3Sopenharmony_ci ASSERT_TRUE(elem1.IsString()); 2304514f5e3Sopenharmony_ci} 2314514f5e3Sopenharmony_ci 2324514f5e3Sopenharmony_ci// target.bind(thisArg, 123, "helloworld") set target_name = EmptyString() 2334514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, FunctionPrototypeBind2) 2344514f5e3Sopenharmony_ci{ 2354514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2364514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2374514f5e3Sopenharmony_ci 2384514f5e3Sopenharmony_ci JSHandle<JSFunction> target = factory->NewJSFunction(env); 2394514f5e3Sopenharmony_ci PropertyDescriptor nameDesc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(123)), false, false, true); 2404514f5e3Sopenharmony_ci JSTaggedValue::DefinePropertyOrThrow(thread, JSHandle<JSTaggedValue>(target), 2414514f5e3Sopenharmony_ci thread->GlobalConstants()->GetHandledNameString(), nameDesc); 2424514f5e3Sopenharmony_ci JSFunction::SetFunctionLength(thread, target, JSTaggedValue(5)); 2434514f5e3Sopenharmony_ci 2444514f5e3Sopenharmony_ci JSHandle<JSObject> thisArg(thread, env->GetGlobalObject()); 2454514f5e3Sopenharmony_ci JSHandle<EcmaString> str = factory->NewFromASCII("helloworld"); 2464514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{thisArg.GetTaggedValue(), JSTaggedValue(static_cast<int32_t>(123)), 2474514f5e3Sopenharmony_ci str.GetTaggedValue()}; 2484514f5e3Sopenharmony_ci auto result = FunctionAlgorithm(thread, target, args, 10, AlgorithmType::PROTOTYPE_BIND); 2494514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsECMAObject()); 2504514f5e3Sopenharmony_ci 2514514f5e3Sopenharmony_ci JSHandle<JSBoundFunction> resultFunc(thread, reinterpret_cast<TaggedObject *>(result.GetRawData())); 2524514f5e3Sopenharmony_ci // test BoundThis 2534514f5e3Sopenharmony_ci ASSERT_EQ(resultFunc->GetBoundThis(), thisArg.GetTaggedValue()); 2544514f5e3Sopenharmony_ci // test BoundTarget 2554514f5e3Sopenharmony_ci ASSERT_EQ(resultFunc->GetBoundTarget(), target.GetTaggedValue()); 2564514f5e3Sopenharmony_ci // test BoundArguments 2574514f5e3Sopenharmony_ci JSHandle<TaggedArray> array(thread, resultFunc->GetBoundArguments()); 2584514f5e3Sopenharmony_ci ASSERT_EQ(array->GetLength(), 2U); 2594514f5e3Sopenharmony_ci JSTaggedValue elem = array->Get(0); 2604514f5e3Sopenharmony_ci JSTaggedValue elem1 = array->Get(1); 2614514f5e3Sopenharmony_ci ASSERT_EQ(elem.GetRawData(), JSTaggedValue(123).GetRawData()); 2624514f5e3Sopenharmony_ci 2634514f5e3Sopenharmony_ci ASSERT_TRUE(elem1.IsString()); 2644514f5e3Sopenharmony_ci ASSERT_EQ(elem1.GetRawData(), str.GetTaggedType()); 2654514f5e3Sopenharmony_ci} 2664514f5e3Sopenharmony_ci 2674514f5e3Sopenharmony_ci// func.call(thisArg) 2684514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, FunctionPrototypeCall) 2694514f5e3Sopenharmony_ci{ 2704514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2714514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2724514f5e3Sopenharmony_ci 2734514f5e3Sopenharmony_ci // ecma 19.2.3.3: func 2744514f5e3Sopenharmony_ci JSHandle<JSFunction> func = factory->NewSFunction(env, reinterpret_cast<void *>(TestFunctionApplyAndCall)); 2754514f5e3Sopenharmony_ci // ecma 19.2.3.3: thisArg 2764514f5e3Sopenharmony_ci JSHandle<JSObject> thisArg(thread, env->GetGlobalObject()); 2774514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(thisArg), 2784514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_a")), 2794514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(thread, JSTaggedValue(1))); 2804514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(thisArg), 2814514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_b")), 2824514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(thread, JSTaggedValue(2))); 2834514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{thisArg.GetTaggedValue()}; 2844514f5e3Sopenharmony_ci auto result = FunctionAlgorithm(thread, func, args, 6, AlgorithmType::PROTOTYPE_CALL); 2854514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue(3).GetRawData()); 2864514f5e3Sopenharmony_ci 2874514f5e3Sopenharmony_ci JSObject::DeleteProperty(thread, (thisArg), 2884514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_a"))); 2894514f5e3Sopenharmony_ci JSObject::DeleteProperty(thread, (thisArg), 2904514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_b"))); 2914514f5e3Sopenharmony_ci} 2924514f5e3Sopenharmony_ci 2934514f5e3Sopenharmony_ci// func.call(thisArg, 123, 456, 789) 2944514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, FunctionPrototypeCall1) 2954514f5e3Sopenharmony_ci{ 2964514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2974514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2984514f5e3Sopenharmony_ci 2994514f5e3Sopenharmony_ci // ecma 19.2.3.3: func 3004514f5e3Sopenharmony_ci JSHandle<JSFunction> func = factory->NewSFunction(env, reinterpret_cast<void *>(TestFunctionApplyAndCall)); 3014514f5e3Sopenharmony_ci // ecma 19.2.3.3: thisArg 3024514f5e3Sopenharmony_ci JSHandle<JSObject> thisArg(thread, env->GetGlobalObject()); 3034514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(thisArg), 3044514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_a")), 3054514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(thread, JSTaggedValue(1))); 3064514f5e3Sopenharmony_ci JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(thisArg), 3074514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_b")), 3084514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(thread, JSTaggedValue(2))); 3094514f5e3Sopenharmony_ci 3104514f5e3Sopenharmony_ci // func thisArg ...args 3114514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{thisArg.GetTaggedValue(), JSTaggedValue(static_cast<int32_t>(123)), 3124514f5e3Sopenharmony_ci JSTaggedValue(static_cast<int32_t>(456)), JSTaggedValue(static_cast<int32_t>(789))}; 3134514f5e3Sopenharmony_ci auto result = FunctionAlgorithm(thread, func, args, 12, AlgorithmType::PROTOTYPE_CALL); 3144514f5e3Sopenharmony_ci 3154514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue(1371).GetRawData()); 3164514f5e3Sopenharmony_ci 3174514f5e3Sopenharmony_ci JSObject::DeleteProperty(thread, (thisArg), 3184514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_a"))); 3194514f5e3Sopenharmony_ci JSObject::DeleteProperty(thread, (thisArg), 3204514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>(factory->NewFromASCII("test_builtins_function_b"))); 3214514f5e3Sopenharmony_ci} 3224514f5e3Sopenharmony_ci 3234514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, FunctionPrototypeHasInstance) 3244514f5e3Sopenharmony_ci{ 3254514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 3264514f5e3Sopenharmony_ci JSHandle<JSFunction> booleanCtor(env->GetBooleanFunction()); 3274514f5e3Sopenharmony_ci 3284514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*booleanCtor), 6); 3294514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(booleanCtor.GetTaggedValue()); 3304514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(JSTaggedValue::Undefined()); 3314514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(123))); 3324514f5e3Sopenharmony_ci 3334514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 3344514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsBoolean::BooleanConstructor(ecmaRuntimeCallInfo1); 3354514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3364514f5e3Sopenharmony_ci 3374514f5e3Sopenharmony_ci JSHandle<JSObject> booleanInstance(thread, result); 3384514f5e3Sopenharmony_ci 3394514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 3404514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined()); 3414514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetThis(booleanCtor.GetTaggedValue()); 3424514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetCallArg(0, booleanInstance.GetTaggedValue()); 3434514f5e3Sopenharmony_ci 3444514f5e3Sopenharmony_ci prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2); 3454514f5e3Sopenharmony_ci EXPECT_TRUE(BuiltinsFunction::FunctionPrototypeHasInstance(ecmaRuntimeCallInfo2).GetRawData()); 3464514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3474514f5e3Sopenharmony_ci} 3484514f5e3Sopenharmony_ci 3494514f5e3Sopenharmony_ci/** 3504514f5e3Sopenharmony_ci * @tc.name: FunctionPrototypeToString 3514514f5e3Sopenharmony_ci * @tc.desc: Create msgs through "CreateEcmaRuntimeCallInfo" function, Set ArgsNumber and CallArg, then call 3524514f5e3Sopenharmony_ci * the "FunctionPrototypeToString" function to get the result of Function.prototype.call.toString(). 3534514f5e3Sopenharmony_ci * @tc.type: FUNC 3544514f5e3Sopenharmony_ci * @tc.require: issueI5INW1 3554514f5e3Sopenharmony_ci */ 3564514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedFunctionTest, FunctionPrototypeToString) 3574514f5e3Sopenharmony_ci{ 3584514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 3594514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3604514f5e3Sopenharmony_ci JSHandle<JSFunction> func = factory->NewSFunction( 3614514f5e3Sopenharmony_ci env, reinterpret_cast<void *>(BuiltinsFunction::FunctionPrototypeCall)); 3624514f5e3Sopenharmony_ci 3634514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); 3644514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3654514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(func.GetTaggedValue()); 3664514f5e3Sopenharmony_ci 3674514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3684514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsFunction::FunctionPrototypeToString(ecmaRuntimeCallInfo); 3694514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 3704514f5e3Sopenharmony_ci JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData())); 3714514f5e3Sopenharmony_ci JSHandle<EcmaString> test = factory->NewFromASCII("function undefined() { [native code] }"); 3724514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0); 3734514f5e3Sopenharmony_ci} 3744514f5e3Sopenharmony_ci} // namespace panda::test 375