/* * 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 "ark_native_value.h" ArkNativeValue::ArkNativeValue(ArkNativeEngine* engine, Local value) { engine_ = engine; Global globalValue(engine->GetEcmaVm(), value); value_ = globalValue; NativeScopeManager* scopeManager = engine_->GetScopeManager(); if (scopeManager != nullptr) { scopeManager->CreateHandle(this); } } ArkNativeValue::~ArkNativeValue() { // Addr of Global stored in ArkNativeValue should be released. Global oldValue = value_; oldValue.FreeGlobalHandleAddr(); } void* ArkNativeValue::GetInterface(int interfaceId) { return nullptr; } void ArkNativeValue::UpdateValue(Local value) { auto vm = engine_->GetEcmaVm(); Global oldValue = value_; oldValue.FreeGlobalHandleAddr(); Global newValue(vm, value); value_ = newValue; } NativeValueType ArkNativeValue::TypeOf() { Global value = value_; NativeValueType result; if (value->IsNumber()) { result = NATIVE_NUMBER; } else if (value->IsString()) { result = NATIVE_STRING; } else if (value->IsFunction()) { result = NATIVE_FUNCTION; } else if (value->IsNativePointer()) { result = NATIVE_EXTERNAL; } else if (value->IsNull()) { result = NATIVE_NULL; } else if (value->IsBoolean()) { result = NATIVE_BOOLEAN; } else if (value->IsUndefined()) { result = NATIVE_UNDEFINED; } else if (value->IsSymbol()) { result = NATIVE_SYMBOL; } else if (value->IsBigInt()) { result = NATIVE_BIGINT; } else if (value->IsObject()) { result = NATIVE_OBJECT; } else { result = NATIVE_UNDEFINED; } return result; } bool ArkNativeValue::InstanceOf(NativeValue* obj) { auto vm = engine_->GetEcmaVm(); LocalScope scope(vm); Global value = value_; Global object = *obj; return value->InstanceOf(vm, object.ToLocal(vm)); } bool ArkNativeValue::IsArray() { Global value = value_; return value->IsArray(engine_->GetEcmaVm()); } bool ArkNativeValue::IsArrayBuffer() { Global value = value_; return value->IsArrayBuffer(); } bool ArkNativeValue::IsBuffer() { return false; } bool ArkNativeValue::IsDate() { Global value = value_; return value->IsDate(); } bool ArkNativeValue::IsError() { Global value = value_; return value->IsError(); } bool ArkNativeValue::IsTypedArray() { Global value = value_; return value->IsTypedArray(); } bool ArkNativeValue::IsDataView() { Global value = value_; return value->IsDataView(); } bool ArkNativeValue::IsPromise() { Global value = value_; return value->IsPromise(); } bool ArkNativeValue::IsCallable() { Global value = value_; return value->IsFunction(); } bool ArkNativeValue::IsArgumentsObject() { Global value = value_; return value->IsArgumentsObject(); } bool ArkNativeValue::IsAsyncFunction() { Global value = value_; return value->IsAsyncFunction(); } bool ArkNativeValue::IsBooleanObject() { Global value = value_; return value->IsJSPrimitiveBoolean(); } bool ArkNativeValue::IsGeneratorFunction() { Global value = value_; return value->IsGeneratorFunction(); } bool ArkNativeValue::IsGeneratorObject() { Global value = value_; return value->IsGeneratorObject(); } bool ArkNativeValue::IsMap() { Global value = value_; return value->IsMap(); } bool ArkNativeValue::IsMapIterator() { Global value = value_; return value->IsMapIterator(); } bool ArkNativeValue::IsModuleNamespaceObject() { Global value = value_; return value->IsModuleNamespaceObject(); } bool ArkNativeValue::IsNumberObject() { Global value = value_; return value->IsJSPrimitiveNumber(); } bool ArkNativeValue::IsProxy() { Global value = value_; return value->IsProxy(); } bool ArkNativeValue::IsRegExp() { Global value = value_; return value->IsRegExp(); } bool ArkNativeValue::IsSet() { Global value = value_; return value->IsSet(); } bool ArkNativeValue::IsSetIterator() { Global value = value_; return value->IsSetIterator(); } bool ArkNativeValue::IsStringObject() { Global value = value_; return value->IsJSPrimitiveString(); } bool ArkNativeValue::IsSymbolObject() { Global value = value_; return value->IsJSPrimitiveSymbol(); } bool ArkNativeValue::IsWeakMap() { Global value = value_; return value->IsWeakMap(); } bool ArkNativeValue::IsWeakSet() { Global value = value_; return value->IsWeakSet(); } NativeValue* ArkNativeValue::ToBoolean() { auto vm = engine_->GetEcmaVm(); LocalScope scope(vm); Global value = value_; return ArkNativeEngine::ArkValueToNativeValue(engine_, value->ToBoolean(vm)); } NativeValue* ArkNativeValue::ToNumber() { auto vm = engine_->GetEcmaVm(); LocalScope scope(vm); Global value = value_; return ArkNativeEngine::ArkValueToNativeValue(engine_, value->ToNumber(vm)); } NativeValue* ArkNativeValue::ToString() { auto vm = engine_->GetEcmaVm(); LocalScope scope(vm); Global value = value_; return ArkNativeEngine::ArkValueToNativeValue(engine_, value->ToString(vm)); } NativeValue* ArkNativeValue::ToObject() { auto vm = engine_->GetEcmaVm(); LocalScope scope(vm); Global value = value_; return ArkNativeEngine::ArkValueToNativeValue(engine_, value->ToObject(vm)); } bool ArkNativeValue::StrictEquals(NativeValue* value) { auto vm = engine_->GetEcmaVm(); LocalScope scope(vm); Global value1 = value_; Global value2 = *value; return value1->IsStrictEquals(vm, value2.ToLocal(vm)); } bool ArkNativeValue::IsBigInt64Array() { Global value = value_; return value->IsBigInt64Array(); } bool ArkNativeValue::IsBigUint64Array() { Global value = value_; return value->IsBigUint64Array(); } bool ArkNativeValue::IsSharedArrayBuffer() { Global value = value_; return value->IsSharedArrayBuffer(); }