1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "frameworks/base/utils/string_utils.h" 17#include "frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h" 18#include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_ref.h" 19#include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.h" 20 21namespace OHOS::Ace::Framework { 22 23template<typename T> 24JsiType<T>::JsiType(panda::Local<T> val) 25{ 26 if (!val.IsEmpty()) { 27 auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime()); 28 handle_ = panda::CopyableGlobal(runtime->GetEcmaVm(), val); 29 } 30} 31 32template<typename T> 33template<typename S> 34JsiType<T>::JsiType(panda::Local<S> val) 35{ 36 if (!val.IsEmpty()) { 37 auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime()); 38 handle_ = panda::CopyableGlobal(runtime->GetEcmaVm(), val); 39 } 40} 41 42template<typename T> 43JsiType<T>::JsiType(const panda::CopyableGlobal<T>& other) : handle_(other) 44{ 45} 46 47template<typename T> 48JsiType<T>::JsiType(const JsiType<T>& rhs) : handle_(rhs.handle_) 49{ 50} 51 52template<typename T> 53JsiType<T>::JsiType(JsiType<T>&& rhs) : handle_(std::move(rhs.handle_)) 54{ 55} 56 57template<typename T> 58JsiType<T>& JsiType<T>::operator=(const JsiType<T>& rhs) 59{ 60 handle_ = rhs.handle_; 61 return *this; 62} 63 64template<typename T> 65JsiType<T>& JsiType<T>::operator=(JsiType<T>&& rhs) 66{ 67 handle_ = std::move(rhs.handle_); 68 return *this; 69} 70 71template<typename T> 72void JsiType<T>::SetWeakCallback(void *ref, panda::WeakRefClearCallBack callback) 73{ 74 if (!handle_.IsEmpty()) { 75 handle_.SetWeakCallback(ref, callback, nullptr); 76 } 77} 78 79template<typename T> 80const EcmaVM* JsiType<T>::GetEcmaVM() const 81{ 82 return handle_.GetEcmaVM(); 83} 84 85template<typename T> 86const panda::CopyableGlobal<T>& JsiType<T>::GetHandle() const 87{ 88 return handle_; 89} 90 91template<typename T> 92panda::Local<T> JsiType<T>::GetLocalHandle() const 93{ 94 return handle_.ToLocal(); 95} 96 97template<typename T> 98bool JsiType<T>::IsEmpty() const 99{ 100 return handle_.IsEmpty(); 101} 102 103template<typename T> 104bool JsiType<T>::IsWeak() const 105{ 106 return handle_.IsWeak(); 107} 108 109template<typename T> 110void JsiType<T>::Reset() 111{ 112 handle_.Reset(); 113} 114 115template<typename T> 116const panda::CopyableGlobal<T>& JsiType<T>::operator->() const 117{ 118 return handle_; 119} 120 121template<typename T> 122JsiType<T>::operator panda::CopyableGlobal<T>() const 123{ 124 return handle_; 125} 126 127template<typename T> 128template<class... Args> 129JsiType<T> JsiType<T>::New(Args&&... args) 130{ 131 auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime()); 132 return JsiType<T>(T::New(runtime->GetEcmaVm(), std::forward<Args>(args)...)); 133} 134 135template<typename T> 136T JsiValue::ToNumber() const 137{ 138 return JsiValueConvertor::fromJsiValue<T>(GetEcmaVM(), GetLocalHandle()); 139} 140 141template<typename T> 142T* JsiObject::Unwrap() const 143{ 144 if (GetHandle()->GetNativePointerFieldCount() < 1) { 145 return nullptr; 146 } 147 return static_cast<T*>(GetHandle()->GetNativePointerField(INSTANCE)); 148} 149 150template<typename T> 151void JsiObject::Wrap(T* data) const 152{ 153 GetHandle()->SetNativePointerField(INSTANCE, static_cast<void*>(data)); 154} 155 156template<typename T> 157void JsiObject::SetProperty(const char* prop, T value) const 158{ 159 auto stringRef = panda::StringRef::NewFromUtf8(GetEcmaVM(), prop); 160 GetHandle()->Set(GetEcmaVM(), stringRef, JsiValueConvertor::toJsiValueWithVM<T>(GetEcmaVM(), value)); 161} 162 163template<typename T> 164void JsiCallbackInfo::SetReturnValue(T* instance) const 165{ 166 retVal_ = instance; 167} 168 169template<typename T> 170void JsiCallbackInfo::SetReturnValue(JsiRef<T> val) const 171{ 172 retVal_ = panda::CopyableGlobal<panda::JSValueRef>(val.Get().GetHandle()); 173} 174 175template<typename... Args> 176void JsiException::Throw(const char* format, Args... args) 177{ 178 const std::string str = StringUtils::FormatString(format, args...); 179 auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime()); 180 auto vm = runtime->GetEcmaVm(); 181 panda::JSNApi::ThrowException(vm, panda::Exception::Error(vm, panda::StringRef::NewFromUtf8(vm, str.c_str()))); 182} 183 184template<typename... Args> 185void JsiException::ThrowRangeError(const char* format, Args... args) 186{ 187 const std::string str = StringUtils::FormatString(format, args...); 188 auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime()); 189 auto vm = runtime->GetEcmaVm(); 190 panda::JSNApi::ThrowException(vm, panda::Exception::RangeError(vm, panda::StringRef::NewFromUtf8(vm, str.c_str()))); 191} 192 193template<typename... Args> 194void JsiException::ThrowReferenceError(const char* format, Args... args) 195{ 196 const std::string str = StringUtils::FormatString(format, args...); 197 auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime()); 198 auto vm = runtime->GetEcmaVm(); 199 panda::JSNApi::ThrowException( 200 vm, panda::Exception::ReferenceError(vm, panda::StringRef::NewFromUtf8(vm, str.c_str()))); 201} 202 203template<typename... Args> 204void JsiException::ThrowSyntaxError(const char* format, Args... args) 205{ 206 const std::string str = StringUtils::FormatString(format, args...); 207 auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime()); 208 auto vm = runtime->GetEcmaVm(); 209 panda::JSNApi::ThrowException( 210 vm, panda::Exception::SyntaxError(vm, panda::StringRef::NewFromUtf8(vm, str.c_str()))); 211} 212 213template<typename... Args> 214void JsiException::ThrowTypeError(const char* format, Args... args) 215{ 216 const std::string str = StringUtils::FormatString(format, args...); 217 auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime()); 218 auto vm = runtime->GetEcmaVm(); 219 panda::JSNApi::ThrowException(vm, panda::Exception::TypeError(vm, panda::StringRef::NewFromUtf8(vm, str.c_str()))); 220} 221 222template<typename... Args> 223void JsiException::ThrowEvalError(const char* format, Args... args) 224{ 225 const std::string str = StringUtils::FormatString(format, args...); 226 auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime()); 227 auto vm = runtime->GetEcmaVm(); 228 panda::JSNApi::ThrowException(vm, panda::Exception::EvalError(vm, panda::StringRef::NewFromUtf8(vm, str.c_str()))); 229} 230} // namespace OHOS::Ace::Framework 231