/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/tests/test_helper.h" #include #include "ecmascript/builtins/builtins_function.h" #include "ecmascript/ecma_global_storage.h" #include "ecmascript/ecma_vm.h" #include "ecmascript/global_env.h" #include "ecmascript/js_thread.h" #include "ecmascript/napi/include/jsnapi.h" #include "ecmascript/napi/jsnapi_helper.h" #include "ecmascript/object_factory.h" using namespace panda; using namespace panda::ecmascript; namespace panda::test { using BuiltinsFunction = ecmascript::builtins::BuiltinsFunction; class JSNApiTests : public testing::Test { public: static void SetUpTestCase() { GTEST_LOG_(INFO) << "SetUpTestCase"; } static void TearDownTestCase() { GTEST_LOG_(INFO) << "TearDownCase"; } void SetUp() override { RuntimeOption option; option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); vm_ = JSNApi::CreateJSVM(option); ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime"; thread_ = vm_->GetJSThread(); vm_->SetEnableForceGC(true); } void TearDown() override { vm_->SetEnableForceGC(false); JSNApi::DestroyJSVM(vm_); } protected: JSThread *thread_ = nullptr; EcmaVM *vm_ = nullptr; }; Local FunctionCallback(JsiRuntimeCallInfo* info) { EscapeLocalScope scope(info->GetVM()); return scope.Escape(ArrayRef::New(info->GetVM(), info->GetArgsNumber())); } void WeakRefCallback(EcmaVM* vm) { LocalScope scope(vm); Local object = ObjectRef::New(vm); Global globalObject(vm, object); globalObject.SetWeak(); Local object1 = ObjectRef::New(vm); Global globalObject1(vm, object1); globalObject1.SetWeak(); vm->CollectGarbage(TriggerGCType::YOUNG_GC); vm->CollectGarbage(TriggerGCType::OLD_GC); globalObject.FreeGlobalHandleAddr(); } void ThreadCheck(const EcmaVM *vm) { EXPECT_TRUE(vm->GetJSThread()->GetThreadId() != JSThread::GetCurrentThreadId()); } HWTEST_F_L0(JSNApiTests, GetGlobalObject) { LocalScope scope(vm_); Local globalObject = JSNApi::GetGlobalObject(vm_); ASSERT_FALSE(globalObject.IsEmpty()); ASSERT_TRUE(globalObject->IsObject()); } HWTEST_F_L0(JSNApiTests, ThreadIdCheck) { EXPECT_TRUE(vm_->GetJSThread()->GetThreadId() == JSThread::GetCurrentThreadId()); } HWTEST_F_L0(JSNApiTests, RegisterFunction) { LocalScope scope(vm_); Local callback = FunctionRef::New(vm_, FunctionCallback); ASSERT_TRUE(!callback.IsEmpty()); std::vector> arguments; arguments.emplace_back(JSValueRef::Undefined(vm_)); Local result = callback->Call(vm_, JSValueRef::Undefined(vm_), arguments.data(), arguments.size()); ASSERT_TRUE(result->IsArray(vm_)); Local array(result); ASSERT_EQ(static_cast(array->Length(vm_)), arguments.size()); } HWTEST_F_L0(JSNApiTests, GetProperty) { LocalScope scope(vm_); Local globalObject = JSNApi::GetGlobalObject(vm_); ASSERT_FALSE(globalObject.IsEmpty()); ASSERT_TRUE(globalObject->IsObject()); Local key = StringRef::NewFromUtf8(vm_, "Number"); Local property = globalObject->Get(vm_, key); ASSERT_TRUE(property->IsFunction()); } HWTEST_F_L0(JSNApiTests, SetProperty) { LocalScope scope(vm_); Local globalObject = JSNApi::GetGlobalObject(vm_); ASSERT_FALSE(globalObject.IsEmpty()); ASSERT_TRUE(globalObject->IsObject()); Local property = ArrayRef::New(vm_, 3); // 3 : length ASSERT_TRUE(property->IsArray(vm_)); ASSERT_EQ(property->Length(vm_), 3); // 3 : test case of input Local key = StringRef::NewFromUtf8(vm_, "Test"); bool result = globalObject->Set(vm_, key, property); ASSERT_TRUE(result); Local propertyGet = globalObject->Get(vm_, key); ASSERT_TRUE(propertyGet->IsArray(vm_)); ASSERT_EQ(Local(propertyGet)->Length(vm_), 3); // 3 : test case of input } HWTEST_F_L0(JSNApiTests, JsonParser) { LocalScope scope(vm_); Local globalObject = JSNApi::GetGlobalObject(vm_); ASSERT_FALSE(globalObject.IsEmpty()); ASSERT_TRUE(globalObject->IsObject()); const char *const test{R"({"orientation": "portrait"})"}; Local jsonString = StringRef::NewFromUtf8(vm_, test); Local result = JSON::Parse(vm_, jsonString); ASSERT_TRUE(result->IsObject()); Local keyString = StringRef::NewFromUtf8(vm_, "orientation"); Local property = Local(result)->Get(vm_, keyString); ASSERT_TRUE(property->IsString()); } HWTEST_F_L0(JSNApiTests, StrictEqual) { LocalScope scope(vm_); Local origin = StringRef::NewFromUtf8(vm_, "1"); Local target1 = StringRef::NewFromUtf8(vm_, "1"); Local target = NumberRef::New(vm_, 1); ASSERT_FALSE(origin->IsStrictEquals(vm_, target)); ASSERT_TRUE(origin->IsStrictEquals(vm_, target1)); } HWTEST_F_L0(JSNApiTests, InstanceOf) { LocalScope scope(vm_); Local target = FunctionRef::New(vm_, nullptr); Local origin = ArrayRef::New(vm_, 1); ASSERT_FALSE(origin->InstanceOf(vm_, target)); } HWTEST_F_L0(JSNApiTests, TypeOf) { LocalScope scope(vm_); Local origin = StringRef::NewFromUtf8(vm_, "1"); Local typeString = origin->Typeof(vm_); ASSERT_EQ(typeString->ToString(), "string"); Local target = NumberRef::New(vm_, 1); typeString = target->Typeof(vm_); ASSERT_EQ(typeString->ToString(), "number"); } HWTEST_F_L0(JSNApiTests, Symbol) { LocalScope scope(vm_); Local description = StringRef::NewFromUtf8(vm_, "test"); Local symbol = SymbolRef::New(vm_, description); ASSERT_FALSE(description->IsSymbol()); ASSERT_TRUE(symbol->IsSymbol()); } HWTEST_F_L0(JSNApiTests, StringUtf8_001) { LocalScope scope(vm_); std::string test = "Hello world"; Local testString = StringRef::NewFromUtf8(vm_, test.c_str()); EXPECT_EQ(testString->Utf8Length(), 12); // 12 : length of testString("Hello World") char buffer[12]; // 12 : length of testString EXPECT_EQ(testString->WriteUtf8(buffer, 12), 12); // 12 : length of testString("Hello World") std::string res(buffer); ASSERT_EQ(res, test); } HWTEST_F_L0(JSNApiTests, StringUtf8_002) { LocalScope scope(vm_); std::string test = "年"; Local testString = StringRef::NewFromUtf8(vm_, test.c_str()); EXPECT_EQ(testString->Utf8Length(), 4); // 4 : length of testString("年") char buffer[4]; // 4 : length of testString EXPECT_EQ(testString->WriteUtf8(buffer, 4), 4); // 4 : length of testString("年") std::string res(buffer); ASSERT_EQ(res, test); } HWTEST_F_L0(JSNApiTests, ToType) { LocalScope scope(vm_); Local toString = StringRef::NewFromUtf8(vm_, "-123.3"); Local toValue(toString); ASSERT_EQ(toString->ToNumber(vm_)->Value(), -123.3); // -123 : test case of input ASSERT_EQ(toString->ToBoolean(vm_)->Value(), true); ASSERT_EQ(toValue->ToString(vm_)->ToString(), "-123.3"); ASSERT_TRUE(toValue->ToObject(vm_)->IsObject()); } HWTEST_F_L0(JSNApiTests, TypeValue) { LocalScope scope(vm_); Local toString = StringRef::NewFromUtf8(vm_, "-123"); Local toValue(toString); ASSERT_EQ(toString->Int32Value(vm_), -123); // -123 : test case of input ASSERT_EQ(toString->BooleaValue(), true); ASSERT_EQ(toString->Uint32Value(vm_), 4294967173U); // 4294967173 : test case of input ASSERT_EQ(toString->IntegerValue(vm_), -123); // -123 : test case of input } void* detach() { GTEST_LOG_(INFO) << "detach is running"; return nullptr; } void attach([[maybe_unused]] void* buffer) { GTEST_LOG_(INFO) << "attach is running"; } HWTEST_F_L0(JSNApiTests, CreateNativeObject) { LocalScope scope(vm_); Local object = ObjectRef::New(vm_, reinterpret_cast(detach), reinterpret_cast(attach)); Local key = StringRef::NewFromUtf8(vm_, "TestKey"); Local value = ObjectRef::New(vm_); PropertyAttribute attribute(value, true, true, true); ASSERT_TRUE(object->DefineProperty(vm_, key, attribute)); Local value1 = object->Get(vm_, key); ASSERT_TRUE(value->IsStrictEquals(vm_, value1)); ASSERT_TRUE(object->Has(vm_, key)); ASSERT_TRUE(object->Delete(vm_, key)); ASSERT_FALSE(object->Has(vm_, key)); } HWTEST_F_L0(JSNApiTests, DefineProperty) { LocalScope scope(vm_); Local object = ObjectRef::New(vm_); Local key = StringRef::NewFromUtf8(vm_, "TestKey"); Local value = ObjectRef::New(vm_); PropertyAttribute attribute(value, true, true, true); ASSERT_TRUE(object->DefineProperty(vm_, key, attribute)); Local value1 = object->Get(vm_, key); ASSERT_TRUE(value->IsStrictEquals(vm_, value1)); } HWTEST_F_L0(JSNApiTests, HasProperty) { LocalScope scope(vm_); Local object = ObjectRef::New(vm_); Local key = StringRef::NewFromUtf8(vm_, "TestKey"); Local value = ObjectRef::New(vm_); PropertyAttribute attribute(value, true, true, true); ASSERT_TRUE(object->DefineProperty(vm_, key, attribute)); ASSERT_TRUE(object->Has(vm_, key)); } HWTEST_F_L0(JSNApiTests, DeleteProperty) { LocalScope scope(vm_); Local object = ObjectRef::New(vm_); Local key = StringRef::NewFromUtf8(vm_, "TestKey"); Local value = ObjectRef::New(vm_); PropertyAttribute attribute(value, true, true, true); ASSERT_TRUE(object->DefineProperty(vm_, key, attribute)); ASSERT_TRUE(object->Delete(vm_, key)); ASSERT_FALSE(object->Has(vm_, key)); } HWTEST_F_L0(JSNApiTests, GetProtoType) { LocalScope scope(vm_); Local function = FunctionRef::New(vm_, nullptr); Local protoType = function->GetPrototype(vm_); ASSERT_TRUE(protoType->IsObject()); Local object = ObjectRef::New(vm_); protoType = object->GetPrototype(vm_); ASSERT_TRUE(protoType->IsObject()); Local native = ObjectRef::New(vm_, reinterpret_cast(detach), reinterpret_cast(attach)); protoType = native->GetPrototype(vm_); ASSERT_TRUE(protoType->IsObject()); } void CheckReject(JsiRuntimeCallInfo* info) { ASSERT_EQ(info->GetArgsNumber(), 1U); Local reason = info->GetCallArgRef(0); ASSERT_TRUE(reason->IsString()); ASSERT_EQ(Local(reason)->ToString(), "Reject"); } Local RejectCallback(JsiRuntimeCallInfo* info) { LocalScope scope(info->GetVM()); CheckReject(info); return JSValueRef::Undefined(info->GetVM()); } HWTEST_F_L0(JSNApiTests, PromiseCatch) { LocalScope scope(vm_); Local capability = PromiseCapabilityRef::New(vm_); Local promise = capability->GetPromise(vm_); Local reject = FunctionRef::New(vm_, RejectCallback); Local catchPromise = promise->Catch(vm_, reject); ASSERT_TRUE(promise->IsPromise()); ASSERT_TRUE(catchPromise->IsPromise()); Local reason = StringRef::NewFromUtf8(vm_, "Reject"); ASSERT_TRUE(capability->Reject(vm_, reason)); vm_->ExecutePromisePendingJob(); } void CheckResolve(JsiRuntimeCallInfo* info) { ASSERT_EQ(info->GetArgsNumber(), 1U); Local value = info->GetCallArgRef(0); ASSERT_TRUE(value->IsNumber()); ASSERT_EQ(Local(value)->Value(), 300.3); // 300.3 : test case of input } Local ResolvedCallback(JsiRuntimeCallInfo* info) { LocalScope scope(info->GetVM()); CheckResolve(info); return JSValueRef::Undefined(info->GetVM()); } HWTEST_F_L0(JSNApiTests, PromiseThen) { LocalScope scope(vm_); Local capability = PromiseCapabilityRef::New(vm_); Local promise = capability->GetPromise(vm_); Local resolve = FunctionRef::New(vm_, ResolvedCallback); Local reject = FunctionRef::New(vm_, RejectCallback); Local thenPromise = promise->Then(vm_, resolve, reject); ASSERT_TRUE(promise->IsPromise()); ASSERT_TRUE(thenPromise->IsPromise()); Local value = NumberRef::New(vm_, 300.3); // 300.3 : test case of input ASSERT_TRUE(capability->Resolve(vm_, value)); vm_->ExecutePromisePendingJob(); } HWTEST_F_L0(JSNApiTests, Constructor) { LocalScope scope(vm_); Local object = JSNApi::GetGlobalObject(vm_); Local key = StringRef::NewFromUtf8(vm_, "Number"); Local numberConstructor = object->Get(vm_, key); Local argv[1]; argv[0] = NumberRef::New(vm_, 1.3); // 1.3 : test case of input Local result = numberConstructor->Constructor(vm_, argv, 1); ASSERT_TRUE(result->IsObject()); ASSERT_EQ(result->ToNumber(vm_)->Value(), 1.3); // 1.3 : size of arguments } HWTEST_F_L0(JSNApiTests, ArrayBuffer) { LocalScope scope(vm_); const int32_t length = 15; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); ASSERT_EQ(arrayBuffer->ByteLength(vm_), length); ASSERT_NE(arrayBuffer->GetBuffer(), nullptr); JSNApi::TriggerGC(vm_); } HWTEST_F_L0(JSNApiTests, ArrayBufferWithBuffer) { static bool isFree = false; struct Data { int32_t length; }; const int32_t length = 15; Data *data = new Data(); data->length = length; Deleter deleter = [](void *buffer, void *data) -> void { delete[] reinterpret_cast(buffer); Data *currentData = reinterpret_cast(data); ASSERT_EQ(currentData->length, 15); // 5 : size of arguments delete currentData; isFree = true; }; { LocalScope scope(vm_); uint8_t *buffer = new uint8_t[length](); Local arrayBuffer = ArrayBufferRef::New(vm_, buffer, length, deleter, data); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); ASSERT_EQ(arrayBuffer->ByteLength(vm_), length); ASSERT_EQ(arrayBuffer->GetBuffer(), buffer); } JSNApi::TriggerGC(vm_, JSNApi::TRIGGER_GC_TYPE::FULL_GC); ASSERT_TRUE(isFree); } HWTEST_F_L0(JSNApiTests, DataView) { LocalScope scope(vm_); const int32_t length = 15; Local arrayBuffer = ArrayBufferRef::New(vm_, length); JSNApi::TriggerGC(vm_); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 5 : offset of byte, 7 : length Local dataView = DataViewRef::New(vm_, arrayBuffer, 5, 7); ASSERT_TRUE(dataView->IsDataView()); ASSERT_EQ(dataView->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); ASSERT_EQ(dataView->ByteLength(), 7U); // 7 : size of arguments ASSERT_EQ(dataView->ByteOffset(), 5U); // 5 : size of arguments // 5 : offset of byte, 11 : length dataView = DataViewRef::New(vm_, arrayBuffer, 5, 11); ASSERT_TRUE(dataView->IsUndefined()); } HWTEST_F_L0(JSNApiTests, Int8Array) { LocalScope scope(vm_); const int32_t length = 15; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 5 : offset of byte, 6 : length Local typedArray = Int8ArrayRef::New(vm_, arrayBuffer, 5, 6); ASSERT_TRUE(typedArray->IsInt8Array()); ASSERT_EQ(typedArray->ByteLength(vm_), 6U); // 6 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 5U); // 5 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, Uint8Array) { LocalScope scope(vm_); const int32_t length = 15; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 5 : offset of byte, 6 : length Local typedArray = Uint8ArrayRef::New(vm_, arrayBuffer, 5, 6); ASSERT_TRUE(typedArray->IsUint8Array()); ASSERT_EQ(typedArray->ByteLength(vm_), 6U); // 6 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 5U); // 5 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, Uint8ClampedArray) { LocalScope scope(vm_); const int32_t length = 15; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 5 : offset of byte, 6 : length Local typedArray = Uint8ClampedArrayRef::New(vm_, arrayBuffer, 5, 6); ASSERT_TRUE(typedArray->IsUint8ClampedArray()); ASSERT_EQ(typedArray->ByteLength(vm_), 6U); // 6 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 5U); // 5 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, Int16Array) { LocalScope scope(vm_); const int32_t length = 30; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 4 : offset of byte, 6 : length Local typedArray = Int16ArrayRef::New(vm_, arrayBuffer, 4, 6); ASSERT_TRUE(typedArray->IsInt16Array()); ASSERT_EQ(typedArray->ByteLength(vm_), 12U); // 12 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 4U); // 4 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, Uint16Array) { LocalScope scope(vm_); const int32_t length = 30; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 4 : offset of byte, 6 : length Local typedArray = Uint16ArrayRef::New(vm_, arrayBuffer, 4, 6); ASSERT_TRUE(typedArray->IsUint16Array()); ASSERT_EQ(typedArray->ByteLength(vm_), 12U); // 12 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 4U); // 4 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, Uint32Array) { LocalScope scope(vm_); const int32_t length = 30; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 4 : offset of byte, 6 : length Local typedArray = Uint32ArrayRef::New(vm_, arrayBuffer, 4, 6); ASSERT_TRUE(typedArray->IsUint32Array()); ASSERT_EQ(typedArray->ByteLength(vm_), 24U); // 24 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 4U); // 4 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, Int32Array) { LocalScope scope(vm_); const int32_t length = 30; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 4 : offset of byte, 6 : length Local typedArray = Int32ArrayRef::New(vm_, arrayBuffer, 4, 6); ASSERT_TRUE(typedArray->IsInt32Array()); ASSERT_EQ(typedArray->ByteLength(vm_), 24U); // 24 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 4U); // 4 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, Float32Array) { LocalScope scope(vm_); const int32_t length = 30; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 4 : offset of byte, 6 : length Local typedArray = Float32ArrayRef::New(vm_, arrayBuffer, 4, 6); ASSERT_TRUE(typedArray->IsFloat32Array()); ASSERT_EQ(typedArray->ByteLength(vm_), 24U); // 24 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 4U); // 4 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, Float64Array) { LocalScope scope(vm_); const int32_t length = 57; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 8 : offset of byte, 6 : length Local typedArray = Float64ArrayRef::New(vm_, arrayBuffer, 8, 6); ASSERT_TRUE(typedArray->IsFloat64Array()); ASSERT_EQ(typedArray->ByteLength(vm_), 48U); // 48 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 8U); // 8 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, BigInt64Array) { LocalScope scope(vm_); const int32_t length = 57; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 8 : offset of byte, 6 : length Local typedArray = BigInt64ArrayRef::New(vm_, arrayBuffer, 8, 6); ASSERT_TRUE(typedArray->IsBigInt64Array()); ASSERT_EQ(typedArray->ByteLength(vm_), 48U); // 48 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 8U); // 8 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, BigUint64Array) { LocalScope scope(vm_); const int32_t length = 57; Local arrayBuffer = ArrayBufferRef::New(vm_, length); ASSERT_TRUE(arrayBuffer->IsArrayBuffer()); // 8 : offset of byte, 6 : length Local typedArray = BigUint64ArrayRef::New(vm_, arrayBuffer, 8, 6); ASSERT_TRUE(typedArray->IsBigUint64Array()); ASSERT_EQ(typedArray->ByteLength(vm_), 48U); // 48 : length of bytes ASSERT_EQ(typedArray->ByteOffset(vm_), 8U); // 8 : offset of byte ASSERT_EQ(typedArray->ArrayLength(vm_), 6U); // 6 : length of array ASSERT_EQ(typedArray->GetArrayBuffer(vm_)->GetBuffer(), arrayBuffer->GetBuffer()); } HWTEST_F_L0(JSNApiTests, Error) { LocalScope scope(vm_); Local message = StringRef::NewFromUtf8(vm_, "ErrorTest"); Local error = Exception::Error(vm_, message); ASSERT_TRUE(error->IsError()); JSNApi::ThrowException(vm_, error); ASSERT_TRUE(thread_->HasPendingException()); } HWTEST_F_L0(JSNApiTests, RangeError) { LocalScope scope(vm_); Local message = StringRef::NewFromUtf8(vm_, "ErrorTest"); Local error = Exception::RangeError(vm_, message); ASSERT_TRUE(error->IsError()); JSNApi::ThrowException(vm_, error); ASSERT_TRUE(thread_->HasPendingException()); } HWTEST_F_L0(JSNApiTests, TypeError) { LocalScope scope(vm_); Local message = StringRef::NewFromUtf8(vm_, "ErrorTest"); Local error = Exception::TypeError(vm_, message); ASSERT_TRUE(error->IsError()); JSNApi::ThrowException(vm_, error); ASSERT_TRUE(thread_->HasPendingException()); } HWTEST_F_L0(JSNApiTests, ReferenceError) { LocalScope scope(vm_); Local message = StringRef::NewFromUtf8(vm_, "ErrorTest"); Local error = Exception::ReferenceError(vm_, message); ASSERT_TRUE(error->IsError()); JSNApi::ThrowException(vm_, error); ASSERT_TRUE(thread_->HasPendingException()); } HWTEST_F_L0(JSNApiTests, SyntaxError) { LocalScope scope(vm_); Local message = StringRef::NewFromUtf8(vm_, "ErrorTest"); Local error = Exception::SyntaxError(vm_, message); ASSERT_TRUE(error->IsError()); JSNApi::ThrowException(vm_, error); ASSERT_TRUE(thread_->HasPendingException()); } HWTEST_F_L0(JSNApiTests, OOMError) { LocalScope scope(vm_); Local message = StringRef::NewFromUtf8(vm_, "ErrorTest"); Local error = Exception::OOMError(vm_, message); ASSERT_TRUE(error->IsError()); JSNApi::ThrowException(vm_, error); ASSERT_TRUE(thread_->HasPendingException()); } HWTEST_F_L0(JSNApiTests, InheritPrototype_001) { LocalScope scope(vm_); JSHandle env = vm_->GetGlobalEnv(); // new with Builtins::Set Prototype JSHandle set = env->GetBuiltinsSetFunction(); Local setLocal = JSNApiHelper::ToLocal(set); // new with Builtins::Map Prototype JSHandle map = env->GetBuiltinsMapFunction(); Local mapLocal = JSNApiHelper::ToLocal(map); JSHandle setPrototype(thread_, JSHandle::Cast(set)->GetFunctionPrototype()); JSHandle mapPrototype(thread_, JSHandle::Cast(map)->GetFunctionPrototype()); JSHandle mapPrototypeProto(thread_, JSTaggedValue::GetPrototype(thread_, mapPrototype)); bool same = JSTaggedValue::SameValue(setPrototype, mapPrototypeProto); // before inherit, map.Prototype.__proto__ should be different from set.Prototype ASSERT_FALSE(same); // before inherit, map.__proto__ should be different from set JSHandle mapProto(thread_, JSTaggedValue::GetPrototype(thread_, map)); bool same1 = JSTaggedValue::SameValue(set, mapProto); ASSERT_FALSE(same1); // Set property to Set Function auto factory = vm_->GetFactory(); JSHandle defaultString = thread_->GlobalConstants()->GetHandledDefaultString(); PropertyDescriptor desc = PropertyDescriptor(thread_, defaultString); bool success = JSObject::DefineOwnProperty(thread_, JSHandle::Cast(set), defaultString, desc); ASSERT_TRUE(success); JSHandle property1String(thread_, factory->NewFromASCII("property1").GetTaggedValue()); JSHandle func = env->GetTypedArrayFunction(); PropertyDescriptor desc1 = PropertyDescriptor(thread_, func); bool success1 = JSObject::DefineOwnProperty(thread_, JSHandle::Cast(set), property1String, desc1); ASSERT_TRUE(success1); mapLocal->Inherit(vm_, setLocal); JSHandle sonHandle = JSNApiHelper::ToJSHandle(mapLocal); JSHandle sonPrototype(thread_, JSHandle::Cast(sonHandle)->GetFunctionPrototype()); JSHandle sonPrototypeProto(thread_, JSTaggedValue::GetPrototype(thread_, sonPrototype)); bool same2 = JSTaggedValue::SameValue(setPrototype, sonPrototypeProto); ASSERT_TRUE(same2); JSHandle sonProto(thread_, JSTaggedValue::GetPrototype(thread_, map)); bool same3 = JSTaggedValue::SameValue(set, sonProto); ASSERT_TRUE(same3); // son = new Son(), Son() inherit from Parent(), Test whether son.InstanceOf(Parent) is true JSHandle sonObj = factory->NewJSObjectByConstructor(JSHandle::Cast(sonHandle), sonHandle); bool isInstance = JSObject::InstanceOf(thread_, JSHandle::Cast(sonObj), set); ASSERT_TRUE(isInstance); // Test whether son Function can access to property of parent Function OperationResult res = JSObject::GetProperty(thread_, JSHandle::Cast(sonHandle), defaultString); bool same4 = JSTaggedValue::SameValue(defaultString, res.GetValue()); ASSERT_TRUE(same4); OperationResult res1 = JSObject::GetProperty(thread_, JSHandle::Cast(sonHandle), property1String); bool same5 = JSTaggedValue::SameValue(func, res1.GetValue()); ASSERT_TRUE(same5); // new with empty Function Constructor Local son1 = FunctionRef::New(vm_, FunctionCallback, nullptr); son1->Inherit(vm_, mapLocal); JSHandle son1Handle = JSHandle::Cast(JSNApiHelper::ToJSHandle(son1)); ASSERT_TRUE(son1Handle->HasFunctionPrototype()); } HWTEST_F_L0(JSNApiTests, InheritPrototype_002) { LocalScope scope(vm_); JSHandle env = vm_->GetGlobalEnv(); // new with Builtins::weakSet Prototype JSHandle weakSet = env->GetBuiltinsWeakSetFunction(); Local weakSetLocal = JSNApiHelper::ToLocal(weakSet); // new with Builtins::weakMap Prototype JSHandle weakMap = env->GetBuiltinsWeakMapFunction(); Local weakMapLocal = JSNApiHelper::ToLocal(weakMap); weakMapLocal->Inherit(vm_, weakSetLocal); auto factory = vm_->GetFactory(); JSHandle property1String(thread_, factory->NewFromASCII("property1").GetTaggedValue()); JSHandle func = env->GetArrayFunction(); PropertyDescriptor desc1 = PropertyDescriptor(thread_, func); bool success1 = JSObject::DefineOwnProperty(thread_, JSHandle::Cast(weakMap), property1String, desc1); ASSERT_TRUE(success1); JSHandle sonHandle = JSNApiHelper::ToJSHandle(weakMapLocal); JSHandle sonObj = factory->NewJSObjectByConstructor(JSHandle::Cast(sonHandle), sonHandle); JSHandle fatherHandle = JSNApiHelper::ToJSHandle(weakSetLocal); JSHandle fatherObj = factory->NewJSObjectByConstructor(JSHandle::Cast(fatherHandle), fatherHandle); JSHandle sonMethod = JSObject::GetMethod(thread_, JSHandle(sonObj), property1String); JSHandle fatherMethod = JSObject::GetMethod(thread_, JSHandle(fatherObj), property1String); bool same = JSTaggedValue::SameValue(sonMethod, fatherMethod); ASSERT_TRUE(same); } HWTEST_F_L0(JSNApiTests, InheritPrototype_003) { LocalScope scope(vm_); JSHandle env = vm_->GetGlobalEnv(); auto factory = vm_->GetFactory(); JSHandle invokeSelf = factory->NewMethodForNativeFunction(reinterpret_cast(BuiltinsFunction::FunctionPrototypeInvokeSelf)); // father type JSHandle protoClass = JSHandle::Cast(env->GetFunctionClassWithProto()); JSHandle protoFunc = factory->NewJSFunctionByHClass(invokeSelf, protoClass); Local protoLocal = JSNApiHelper::ToLocal(JSHandle(protoFunc)); // son type JSHandle noProtoClass = JSHandle::Cast(env->GetFunctionClassWithoutProto()); JSHandle noProtoFunc = factory->NewJSFunctionByHClass(invokeSelf, noProtoClass); Local noProtoLocal = JSNApiHelper::ToLocal(JSHandle(noProtoFunc)); JSHandle sonHandle = JSHandle::Cast(JSNApiHelper::ToJSHandle(noProtoLocal)); EXPECT_FALSE(sonHandle->HasFunctionPrototype()); JSHandle defaultString = thread_->GlobalConstants()->GetHandledDefaultString(); PropertyDescriptor desc = PropertyDescriptor(thread_, defaultString); JSObject::DefineOwnProperty(thread_, JSHandle::Cast(protoFunc), defaultString, desc); noProtoLocal->Inherit(vm_, protoLocal); JSHandle son1Handle = JSHandle::Cast(JSNApiHelper::ToJSHandle(noProtoLocal)); EXPECT_TRUE(son1Handle->HasFunctionPrototype()); OperationResult res = JSObject::GetProperty(thread_, JSHandle::Cast(son1Handle), defaultString); EXPECT_EQ(JSTaggedValue::SameValue(defaultString, res.GetValue()), true); JSHandle propertyString(thread_, factory->NewFromASCII("property").GetTaggedValue()); JSHandle func = env->GetArrayFunction(); PropertyDescriptor desc1 = PropertyDescriptor(thread_, func); JSObject::DefineOwnProperty(thread_, JSHandle::Cast(protoFunc), propertyString, desc1); OperationResult res1 = JSObject::GetProperty(thread_, JSHandle::Cast(son1Handle), propertyString); EXPECT_EQ(JSTaggedValue::SameValue(func, res1.GetValue()), true); } HWTEST_F_L0(JSNApiTests, InheritPrototype_004) { LocalScope scope(vm_); JSHandle env = vm_->GetGlobalEnv(); auto factory = vm_->GetFactory(); JSHandle weakSet = env->GetBuiltinsWeakSetFunction(); JSHandle deleteString(factory->NewFromASCII("delete")); JSHandle addString(factory->NewFromASCII("add")); JSHandle defaultString = thread_->GlobalConstants()->GetHandledDefaultString(); JSHandle deleteMethod = JSObject::GetMethod(thread_, weakSet, deleteString); JSHandle addMethod = JSObject::GetMethod(thread_, weakSet, addString); JSHandle invokeSelf = factory->NewMethodForNativeFunction(reinterpret_cast(BuiltinsFunction::FunctionPrototypeInvokeSelf)); JSHandle ctor = factory->NewMethodForNativeFunction(reinterpret_cast(BuiltinsFunction::FunctionConstructor)); JSHandle protoClass = JSHandle::Cast(env->GetFunctionClassWithProto()); JSHandle funcFuncPrototype = factory->NewJSFunctionByHClass(invokeSelf, protoClass); // add method in funcPrototype PropertyDescriptor desc = PropertyDescriptor(thread_, deleteMethod); JSObject::DefineOwnProperty(thread_, JSHandle::Cast(funcFuncPrototype), deleteString, desc); JSHandle funcFuncPrototypeValue(funcFuncPrototype); JSHandle funcFuncProtoIntanceClass = factory->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, funcFuncPrototypeValue); // new with NewJSFunctionByHClass::function Class JSHandle protoFunc = factory->NewJSFunctionByHClass(ctor, funcFuncProtoIntanceClass); EXPECT_TRUE(*protoFunc != nullptr); // add method in funcnction PropertyDescriptor desc1 = PropertyDescriptor(thread_, addMethod); JSObject::DefineOwnProperty(thread_, JSHandle::Cast(protoFunc), addString, desc1); JSObject::DefineOwnProperty(thread_, JSHandle::Cast(protoFunc), deleteString, desc); // father type Local protoLocal = JSNApiHelper::ToLocal(JSHandle(protoFunc)); JSHandle noProtoClass = JSHandle::Cast(env->GetFunctionClassWithoutProto()); JSHandle funcFuncNoProtoPrototype = factory->NewJSFunctionByHClass(invokeSelf, noProtoClass); JSHandle funcFuncNoProtoPrototypeValue(funcFuncNoProtoPrototype); JSHandle funcFuncNoProtoProtoIntanceClass = factory->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, funcFuncNoProtoPrototypeValue); // new with NewJSFunctionByHClass::function Class JSHandle noProtoFunc = factory->NewJSFunctionByHClass(ctor, funcFuncNoProtoProtoIntanceClass); EXPECT_TRUE(*noProtoFunc != nullptr); // set property that has same key with fater type PropertyDescriptor desc2 = PropertyDescriptor(thread_, defaultString); JSObject::DefineOwnProperty(thread_, JSHandle::Cast(noProtoFunc), addString, desc2); // son type Local noProtoLocal = JSNApiHelper::ToLocal(JSHandle(noProtoFunc)); noProtoLocal->Inherit(vm_, protoLocal); JSHandle sonHandle = JSHandle::Cast(JSNApiHelper::ToJSHandle(noProtoLocal)); OperationResult res = JSObject::GetProperty(thread_, JSHandle::Cast(sonHandle), deleteString); EXPECT_EQ(JSTaggedValue::SameValue(deleteMethod, res.GetValue()), true); // test if the property value changed after inherit OperationResult res1 = JSObject::GetProperty(thread_, JSHandle::Cast(sonHandle), addString); EXPECT_EQ(JSTaggedValue::SameValue(defaultString, res1.GetValue()), true); } HWTEST_F_L0(JSNApiTests, ClassFunction) { LocalScope scope(vm_); Local cls = FunctionRef::NewClassFunction(vm_, nullptr, nullptr, nullptr); JSHandle clsObj = JSNApiHelper::ToJSHandle(Local(cls)); ASSERT_TRUE(clsObj->IsClassConstructor()); JSTaggedValue accessor = JSHandle(clsObj)->GetPropertyInlinedProps( JSFunction::CLASS_PROTOTYPE_INLINE_PROPERTY_INDEX); ASSERT_TRUE(accessor.IsInternalAccessor()); } HWTEST_F_L0(JSNApiTests, WeakRefSecondPassCallback) { LocalScope scope(vm_); Local object1 = ObjectRef::New(vm_); Global globalObject1(vm_, object1); globalObject1.SetWeak(); NativeReferenceHelper *temp = nullptr; { LocalScope scope1(vm_); Local object2 = ObjectRef::New(vm_); Global globalObject2(vm_, object2); NativeReferenceHelper *ref1 = new NativeReferenceHelper(vm_, globalObject2, WeakRefCallback); ref1->SetWeakCallback(); temp = ref1; } { LocalScope scope1(vm_); Local object3 = ObjectRef::New(vm_); Global globalObject3(vm_, object3); globalObject3.SetWeak(); } Local object4 = ObjectRef::New(vm_); Global globalObject4(vm_, object4); NativeReferenceHelper *ref2 = new NativeReferenceHelper(vm_, globalObject4, WeakRefCallback); ref2->SetWeakCallback(); vm_->CollectGarbage(TriggerGCType::OLD_GC); delete temp; delete ref2; } HWTEST_F_L0(JSNApiTests, TriggerGC_OLD_GC) { vm_->SetEnableForceGC(false); auto globalEnv = vm_->GetGlobalEnv(); auto factory = vm_->GetFactory(); JSHandle jsFunc = globalEnv->GetArrayFunction(); JSHandle objVal1 = factory->NewJSObjectByConstructor(JSHandle(jsFunc), jsFunc); JSHandle objVal2 = factory->NewJSObjectByConstructor(JSHandle(jsFunc), jsFunc); JSObject *newObj2 = *objVal2; JSTaggedValue canBeGcValue(newObj2); uint32_t arrayLength = 2; JSHandle taggedArray = factory->NewTaggedArray(arrayLength); taggedArray->Set(thread_, 0, objVal1); taggedArray->Set(thread_, 1, canBeGcValue); EXPECT_EQ(taggedArray->GetIdx(objVal1.GetTaggedValue()), 0U); EXPECT_EQ(taggedArray->GetIdx(canBeGcValue), 1U); // trigger gc JSNApi::TRIGGER_GC_TYPE gcType = JSNApi::TRIGGER_GC_TYPE::OLD_GC; JSNApi::TriggerGC(vm_, gcType); EXPECT_EQ(taggedArray->GetIdx(objVal1.GetTaggedValue()), 0U); EXPECT_EQ(taggedArray->GetIdx(canBeGcValue), TaggedArray::MAX_ARRAY_INDEX); vm_->SetEnableForceGC(true); } HWTEST_F_L0(JSNApiTests, ExecuteModuleBuffer) { const char *fileName = "__JSNApiTests_ExecuteModuleBuffer.abc"; const char *data = R"( .language ECMAScript .function any func_main_0(any a0, any a1, any a2) { ldai 1 return } )"; bool executeResult = JSNApi::ExecuteModuleBuffer(vm_, reinterpret_cast(data), sizeof(data), fileName); EXPECT_FALSE(executeResult); } HWTEST_F_L0(JSNApiTests, addWorker_DeleteWorker) { JSRuntimeOptions option; EcmaVM *workerVm = JSNApi::CreateEcmaVM(option); JSNApi::addWorker(vm_, workerVm); bool hasDeleted = JSNApi::DeleteWorker(vm_, workerVm); EXPECT_TRUE(hasDeleted); hasDeleted = JSNApi::DeleteWorker(vm_, nullptr); EXPECT_FALSE(hasDeleted); } HWTEST_F_L0(JSNApiTests, PrimitiveRef_GetValue) { auto factory = vm_->GetFactory(); Local intValue = IntegerRef::New(vm_, 0); EXPECT_EQ(intValue->Value(), 0); Local jsValue = intValue->GetValue(vm_); EXPECT_TRUE(*jsValue == nullptr); JSHandle nullHandle(thread_, JSTaggedValue::Null()); JSHandle jsClassHandle = factory->NewEcmaHClass(JSObject::SIZE, JSType::JS_PRIMITIVE_REF, nullHandle); TaggedObject *taggedObject = factory->NewObject(jsClassHandle); JSHandle jsTaggedValue(thread_, JSTaggedValue(taggedObject)); Local jsValueRef = JSNApiHelper::ToLocal(jsTaggedValue); EXPECT_TRUE(*(jsValueRef->GetValue(vm_)) != nullptr); } HWTEST_F_L0(JSNApiTests, BigIntRef_New_Uint64) { uint64_t maxUint64 = std::numeric_limits::max(); Local maxBigintUint64 = BigIntRef::New(vm_, maxUint64); EXPECT_TRUE(maxBigintUint64->IsBigInt()); JSHandle maxBigintUint64Val(thread_, JSNApiHelper::ToJSTaggedValue(*maxBigintUint64)); EXPECT_EQ(maxBigintUint64Val->GetDigit(0), static_cast(maxUint64 & 0xffffffff)); EXPECT_EQ(maxBigintUint64Val->GetDigit(1), static_cast((maxUint64 >> BigInt::DATEBITS) & 0xffffffff)); uint64_t minUint64 = std::numeric_limits::min(); Local minBigintUint64 = BigIntRef::New(vm_, minUint64); EXPECT_TRUE(minBigintUint64->IsBigInt()); JSHandle minBigintUint64Val(thread_, JSNApiHelper::ToJSTaggedValue(*minBigintUint64)); EXPECT_EQ(minBigintUint64Val->GetDigit(0), static_cast(minUint64 & 0xffffffff)); EXPECT_EQ(minBigintUint64Val->GetLength(), 1U); } HWTEST_F_L0(JSNApiTests, BigIntRef_New_Int64) { int64_t maxInt64 = std::numeric_limits::max(); Local maxBigintInt64 = BigIntRef::New(vm_, maxInt64); EXPECT_TRUE(maxBigintInt64->IsBigInt()); JSHandle maxBigintInt64Val(thread_, JSNApiHelper::ToJSTaggedValue(*maxBigintInt64)); EXPECT_EQ(maxBigintInt64Val->GetDigit(0), static_cast(maxInt64 & 0xffffffff)); EXPECT_EQ(maxBigintInt64Val->GetDigit(1), static_cast((maxInt64 >> BigInt::DATEBITS) & 0xffffffff)); int64_t minInt64 = std::numeric_limits::min(); Local minBigintInt64 = BigIntRef::New(vm_, minInt64); EXPECT_TRUE(minBigintInt64->IsBigInt()); JSHandle minBigintInt64Val(thread_, JSNApiHelper::ToJSTaggedValue(*minBigintInt64)); EXPECT_EQ(minBigintInt64Val->GetSign(), true); EXPECT_EQ(minBigintInt64Val->GetDigit(0), static_cast((-minInt64) & 0xffffffff)); EXPECT_EQ(minBigintInt64Val->GetDigit(1), static_cast(((-minInt64) >> BigInt::DATEBITS) & 0xffffffff)); } HWTEST_F_L0(JSNApiTests, BigIntRef_CreateBigWords_GetWordsArray_GetWordsArraySize) { bool sign = false; uint32_t size = 3; const uint32_t MULTIPLE = 2; const uint64_t words[3] = { std::numeric_limits::min() - 1, std::numeric_limits::min(), std::numeric_limits::max(), }; Local bigWords = BigIntRef::CreateBigWords(vm_, sign, size, words); EXPECT_TRUE(bigWords->IsBigInt()); Local bigWordsRef(bigWords); EXPECT_EQ(bigWordsRef->GetWordsArraySize(), size); JSHandle bigintUint64Val(thread_, JSNApiHelper::ToJSTaggedValue(*bigWords)); EXPECT_EQ(bigintUint64Val->GetSign(), false); EXPECT_EQ(bigintUint64Val->GetLength(), size * MULTIPLE); bool resultSignBit = true; uint64_t *resultWords = new uint64_t[3](); // 3 : length of words array bigWordsRef->GetWordsArray(&resultSignBit, size, resultWords); EXPECT_EQ(resultSignBit, false); EXPECT_EQ(resultWords[0], words[0]); EXPECT_EQ(resultWords[1], words[1]); EXPECT_EQ(resultWords[2], words[2]); delete[] resultWords; } HWTEST_F_L0(JSNApiTests, BigIntToInt64) { LocalScope scope(vm_); uint64_t maxUint64 = std::numeric_limits::max(); Local maxBigintUint64 = BigIntRef::New(vm_, maxUint64); EXPECT_TRUE(maxBigintUint64->IsBigInt()); int64_t num = -11; int64_t num1 = num; bool lossless = true; maxBigintUint64->BigIntToInt64(vm_, &num, &lossless); EXPECT_TRUE(num != num1); } HWTEST_F_L0(JSNApiTests, BigIntToUint64) { LocalScope scope(vm_); uint64_t maxUint64 = std::numeric_limits::max(); Local maxBigintUint64 = BigIntRef::New(vm_, maxUint64); EXPECT_TRUE(maxBigintUint64->IsBigInt()); uint64_t num = -11; uint64_t num1 = num; bool lossless = true; maxBigintUint64->BigIntToUint64(vm_, &num, &lossless); EXPECT_TRUE(num != num1); } HWTEST_F_L0(JSNApiTests, BooleanRef_New) { LocalScope scope(vm_); bool input = true; Local res = BooleanRef::New(vm_, input); EXPECT_TRUE(res->IsBoolean()); EXPECT_TRUE(res->BooleaValue()); } HWTEST_F_L0(JSNApiTests, NewFromUnsigned) { LocalScope scope(vm_); unsigned int input = 1; [[maybe_unused]] Local res = IntegerRef::NewFromUnsigned(vm_, input); EXPECT_TRUE(res->IntegerValue(vm_) == 1); EXPECT_TRUE(res->Value() == 1); } HWTEST_F_L0(JSNApiTests, SetBundleName_GetBundleName) { LocalScope scope(vm_); std::string str = "11"; JSNApi::SetBundleName(vm_, str); std::string res = JSNApi::GetBundleName(vm_); ASSERT_EQ(str, res); } HWTEST_F_L0(JSNApiTests, SetModuleName_GetModuleName) { LocalScope scope(vm_); std::string str = "11"; JSNApi::SetModuleName(vm_, str); std::string res = JSNApi::GetModuleName(vm_); ASSERT_EQ(str, res); } HWTEST_F_L0(JSNApiTests, IsBundle) { LocalScope scope(vm_); bool res = JSNApi::IsBundle(vm_); ASSERT_EQ(res, true); } HWTEST_F_L0(JSNApiTests, MapRef_GetSize_GetTotalElements_Get_GetKey_GetValue_New_Set) { LocalScope scope(vm_); Local map = MapRef::New(vm_); Local key = StringRef::NewFromUtf8(vm_, "TestKey"); Local value = StringRef::NewFromUtf8(vm_, "TestValue"); map->Set(vm_, key, value); Local res = map->Get(vm_, key); ASSERT_EQ(res->ToString(vm_)->ToString(), value->ToString(vm_)->ToString()); int32_t num = map->GetSize(); int32_t num1 = map->GetTotalElements(); ASSERT_EQ(num, 1); ASSERT_EQ(num1, 1); Local res1 = map->GetKey(vm_, 0); ASSERT_EQ(res1->ToString(vm_)->ToString(), key->ToString(vm_)->ToString()); Local res2 = map->GetValue(vm_, 0); ASSERT_EQ(res2->ToString(vm_)->ToString(), value->ToString(vm_)->ToString()); } HWTEST_F_L0(JSNApiTests, GetSourceCode) { LocalScope scope(vm_); Local callback = FunctionRef::New(vm_, FunctionCallback); bool res = callback->IsNative(vm_); EXPECT_TRUE(res); } HWTEST_F_L0(JSNApiTests, ObjectRef_Delete) { LocalScope scope(vm_); Local object = ObjectRef::New(vm_); Local key = StringRef::NewFromUtf8(vm_, "TestKey"); Local value = ObjectRef::New(vm_); PropertyAttribute attribute(value, true, true, true); ASSERT_TRUE(object->DefineProperty(vm_, key, attribute)); ASSERT_TRUE(object->Delete(vm_, key)); ASSERT_FALSE(object->Has(vm_, key)); } HWTEST_F_L0(JSNApiTests, ObjectRef_Set1) { LocalScope scope(vm_); Local object = ObjectRef::New(vm_); Local toString = StringRef::NewFromUtf8(vm_, "-123.3"); Local toValue(toString); bool res = object->Set(vm_, 12, toValue); ASSERT_TRUE(res); Local res1 = object->Get(vm_, 12); ASSERT_EQ(res1->ToString(vm_)->ToString(), toValue->ToString(vm_)->ToString()); } HWTEST_F_L0(JSNApiTests, NativePointerRef_New) { LocalScope scope(vm_); NativePointerCallback callBack = nullptr; void *vp1 = static_cast(new std::string("test")); void *vp2 = static_cast(new std::string("test")); Local res = NativePointerRef::New(vm_, vp1, callBack, vp2, 0); ASSERT_EQ(res->Value(), vp1); } HWTEST_F_L0(JSNApiTests, ObjectRef_Has_Delete) { LocalScope scope(vm_); Local object = ObjectRef::New(vm_); uint32_t num = 10; Local toString = StringRef::NewFromUtf8(vm_, "-123.3"); Local toValue(toString); bool res = object->Set(vm_, num, toValue); ASSERT_TRUE(res); bool res1 = object->Has(vm_, num); ASSERT_TRUE(res1); bool res2 = object->Delete(vm_, num); ASSERT_TRUE(res2); bool res3 = object->Has(vm_, num); ASSERT_FALSE(res3); } HWTEST_F_L0(JSNApiTests, PromiseRejectInfo_GetData) { LocalScope scope(vm_); Local toString = StringRef::NewFromUtf8(vm_, "-123.3"); Local promise(toString); Local toString1 = StringRef::NewFromUtf8(vm_, "123.3"); Local reason(toString1); void *data = static_cast(new std::string("test")); PromiseRejectInfo promisereject(promise, reason, PromiseRejectInfo::PROMISE_REJECTION_EVENT::REJECT, data); Local promise_res = promisereject.GetPromise(); Local reason_res = promisereject.GetReason(); ASSERT_EQ(promise_res->ToString(vm_)->ToString(), promise->ToString(vm_)->ToString()); ASSERT_EQ(reason_res->ToString(vm_)->ToString(), reason->ToString(vm_)->ToString()); void* dataRes = promisereject.GetData(); ASSERT_EQ(dataRes, data); } } // namespace panda::test