/* * Copyright (c) 2024 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 "bridge/declarative_frontend/jsview/js_mock.h" #include "bridge/declarative_frontend/engine/jsi/jsi_bindings.h" #include "bridge/declarative_frontend/engine/jsi/modules/jsi_curves_module.h" #include "bridge/js_frontend/engine/jsi/ark_js_value.h" namespace OHOS::Ace::Framework { void JSMockBaseNode::JSBind(BindingTarget globalObj) { JSClass::Declare("__JSBaseNode__"); JSClass::CustomMethod("create", &JSMockBaseNode::Create); JSClass::CustomMethod("finishUpdateFunc", &JSMockBaseNode::FinishUpdateFunc); JSClass::CustomMethod("postTouchEvent", &JSMockBaseNode::PostTouchEvent); JSClass::CustomMethod("disposeNode", &JSMockBaseNode::Dispose); JSClass::CustomMethod("updateStart", &JSMockBaseNode::UpdateStart); JSClass::CustomMethod("updateEnd", &JSMockBaseNode::UpdateEnd); JSClass::Bind(globalObj, JSMockBaseNode::ConstructorCallback, JSMockBaseNode::DestructorCallback); } void JSMockViewPU::JSBind(BindingTarget globalObj, std::string name) { JSClass::Declare(name.c_str()); JSClass::CustomMethod("id__", &JSMockViewPU::Id); JSClass::CustomMethod("updateStateVars", &JSMockViewPU::UpdateStateVars); JSClass::CustomMethod("aboutToReuseInternal", &JSMockViewPU::AboutToReuseInternal); JSClass::CustomMethod("aboutToRecycleInternal", &JSMockViewPU::AboutToRecycleInternal); JSClass::CustomMethod("updateDirtyElements", &JSMockViewPU::UpdateDirtyElements); JSClass::Bind(globalObj); } void JSMockScopeUtil::JSBind(BindingTarget globalObj) { JSClass::Declare("__JSScopeUtil__"); JSClass::StaticMethod("syncInstanceId", &JSMockScopeUtil::SyncInstanceId); JSClass::StaticMethod("restoreInstanceId", &JSMockScopeUtil::RestoreInstanceId); JSClass::Bind(globalObj); } void MockCustomDialogController::JSBind(BindingTarget globalObj) { JSClass::Declare("CustomDialogController"); JSClass::CustomMethod("open", &MockCustomDialogController::Open); JSClass::CustomMethod("close", &MockCustomDialogController::Close); JSClass::Bind(globalObj); } void JSMockNativeCustomSpan::JSBind(BindingTarget globalObj) { JSClass::Declare("NativeCustomSpan"); JSClass::CustomMethod("invalidate", &JSMockNativeCustomSpan::Invalidate); JSClass::Bind(globalObj); } void setAbstractPropertyFunc(Local& objRef, EcmaVM* vm) { objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "get"), panda::FunctionRef::New(const_cast(vm), JSMockAbstractProperty::get)); objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "set"), panda::FunctionRef::New(const_cast(vm), JSMockAbstractProperty::set)); objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "info"), panda::FunctionRef::New(const_cast(vm), JSMockAbstractProperty::info)); } void setSubAbstractPropertyFunc(Local& objRef, EcmaVM* vm) { objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "get"), panda::FunctionRef::New(const_cast(vm), JSMockSubAbstractProperty::get)); objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "set"), panda::FunctionRef::New(const_cast(vm), JSMockSubAbstractProperty::set)); objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "info"), panda::FunctionRef::New(const_cast(vm), JSMockSubAbstractProperty::info)); objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "aboutToBeDeleted"), panda::FunctionRef::New(const_cast(vm), JSMockSubAbstractProperty::aboutToBeDeleted)); } Local JSMockLocalStorage::ref(panda::JsiRuntimeCallInfo* runtimeCallInfo) { EcmaVM* vm = runtimeCallInfo->GetVM(); Local objRef = ObjectRef::New(vm); setAbstractPropertyFunc(objRef, vm); return objRef; } Local JSMockLocalStorage::setAndRef(panda::JsiRuntimeCallInfo* runtimeCallInfo) { EcmaVM* vm = runtimeCallInfo->GetVM(); Local objRef = ObjectRef::New(vm); setAbstractPropertyFunc(objRef, vm); return objRef; } Local JSMockLocalStorage::link(panda::JsiRuntimeCallInfo* runtimeCallInfo) { EcmaVM* vm = runtimeCallInfo->GetVM(); Local objRef = ObjectRef::New(vm); setSubAbstractPropertyFunc(objRef, vm); return objRef; } Local JSMockLocalStorage::setAndLink(panda::JsiRuntimeCallInfo* runtimeCallInfo) { EcmaVM* vm = runtimeCallInfo->GetVM(); Local objRef = ObjectRef::New(vm); setSubAbstractPropertyFunc(objRef, vm); return objRef; } Local JSMockLocalStorage::prop(panda::JsiRuntimeCallInfo* runtimeCallInfo) { EcmaVM* vm = runtimeCallInfo->GetVM(); Local objRef = ObjectRef::New(vm); setSubAbstractPropertyFunc(objRef, vm); return objRef; } Local JSMockLocalStorage::setAndProp(panda::JsiRuntimeCallInfo* runtimeCallInfo) { EcmaVM* vm = runtimeCallInfo->GetVM(); Local objRef = ObjectRef::New(vm); setSubAbstractPropertyFunc(objRef, vm); return objRef; } void JSMockLocalStorage::JSBind(BindingTarget globalObj) { JSClass::Declare("LocalStorage"); JSClass::CustomMethod("has", &JSMockLocalStorage::has); JSClass::CustomMethod("get", &JSMockLocalStorage::get); JSClass::CustomMethod("set", &JSMockLocalStorage::set); JSClass::CustomMethod("setOrCreate", &JSMockLocalStorage::setOrCreate); JSClass::CustomMethod("ref", &JSMockLocalStorage::ref); JSClass::CustomMethod("setAndRef", &JSMockLocalStorage::setAndRef); JSClass::CustomMethod("link", &JSMockLocalStorage::link); JSClass::CustomMethod("setAndLink", &JSMockLocalStorage::setAndLink); JSClass::CustomMethod("prop", &JSMockLocalStorage::prop); JSClass::CustomMethod("setAndProp", &JSMockLocalStorage::setAndProp); JSClass::CustomMethod("delete", &JSMockLocalStorage::deleteKeys); JSClass::CustomMethod("keys", &JSMockLocalStorage::keys); JSClass::CustomMethod("size", &JSMockLocalStorage::size); JSClass::CustomMethod("clear", &JSMockLocalStorage::clear); JSClass::Bind(globalObj); } void JSMockViewBuildNodeBase::JSBind(BindingTarget globalObj) { JSClass::Declare("ViewBuildNodeBase"); JSClass::StaticMethod( "setArkThemeScopeManager", &JSMockViewBuildNodeBase::SetArkThemeScopeManager); JSClass::CustomMethod("id__", &JSMockViewBuildNodeBase::Id); JSClass::CustomMethod("debugInfo__", &JSMockViewBuildNodeBase::DebugInfo); JSClass::CustomMethod("debugInfoElmtId", &JSMockViewBuildNodeBase::DebugInfoElmtId); JSClass::CustomMethod("getChildById", &JSMockViewBuildNodeBase::GetChildById); JSClass::CustomMethod("addChild", &JSMockViewBuildNodeBase::AddChild); JSClass::CustomMethod( "purgeDeletedElmtIds", &JSMockViewBuildNodeBase::PurgeDeletedElmtIds); JSClass::CustomMethod( "updateStateVarsOfChildByElmtId", &JSMockViewBuildNodeBase::UpdateStateVarsOfChildByElmtId); JSClass::CustomMethod("createOrGetNode", &JSMockViewBuildNodeBase::CreateOrGetNode); JSClass::CustomMethod( "ifElseBranchUpdateFunction", &JSMockViewBuildNodeBase::IfElseBranchUpdateFunction); JSClass::CustomMethod( "onWillApplyThemeInternally", &JSMockViewBuildNodeBase::OnWillApplyThemeInternally); JSClass::CustomMethod("onWillApplyTheme", &JSMockViewBuildNodeBase::OnWillApplyTheme); JSClass::CustomMethod( "onGlobalThemeChanged", &JSMockViewBuildNodeBase::OnGlobalThemeChanged); JSClass::Bind( globalObj, JSMockViewBuildNodeBase::ConstructorCallback, JSMockViewBuildNodeBase::DestructorCallback); } void JSMock::JSBind(BindingTarget globalObj) { JSMockBaseNode::JSBind(globalObj); JSMockViewPU::JSBind(globalObj, "ViewPU"); JSMockViewPU::JSBind(globalObj, "ViewV2"); JSMockViewPU::JSBind(globalObj, "PUV2ViewBase"); JSMockScopeUtil::JSBind(globalObj); MockCustomDialogController::JSBind(globalObj); JSMockLocalStorage::JSBind(globalObj); JSMockNativeCustomSpan::JSBind(globalObj); JSMockViewBuildNodeBase::JSBind(globalObj); } void JSLoadV2Decorator(const shared_ptr globalPtr, const shared_ptr runtime) { shared_ptr decoratorPtr = runtime->NewFunction(JsMockFunc::CreatFunction); globalPtr->SetProperty(runtime, "Provider", decoratorPtr); globalPtr->SetProperty(runtime, "Consumer", decoratorPtr); globalPtr->SetProperty(runtime, "Monitor", decoratorPtr); globalPtr->SetProperty(runtime, "__Type__", decoratorPtr); } void JSLoadPersistentStorage(const shared_ptr globalPtr, const shared_ptr runtime) { shared_ptr persistObj = runtime->NewObject(); persistObj->SetProperty(runtime, "persistProp", runtime->NewFunction(JSMockPersistStorage::persistProp)); persistObj->SetProperty(runtime, "deleteProp", runtime->NewFunction(JSMockPersistStorage::deleteProp)); persistObj->SetProperty(runtime, "persistProps", runtime->NewFunction(JSMockPersistStorage::persistProps)); persistObj->SetProperty(runtime, "keys", runtime->NewFunction(JSMockPersistStorage::keys)); globalPtr->SetProperty(runtime, "PersistentStorage", persistObj); } void setAbstractPropertyFunc(shared_ptr& Obj, const shared_ptr& runtime) { Obj->SetProperty(runtime, "get", runtime->NewFunction(JSMockAppStorage::get)); Obj->SetProperty(runtime, "set", runtime->NewFunction(JSMockAppStorage::set)); Obj->SetProperty(runtime, "info", runtime->NewFunction(JSMockAppStorage::info)); } void setSubAbstractPropertyFunc(shared_ptr& Obj, const shared_ptr& runtime) { Obj->SetProperty(runtime, "get", runtime->NewFunction(JSMockAppStorage::get)); Obj->SetProperty(runtime, "set", runtime->NewFunction(JSMockAppStorage::set)); Obj->SetProperty(runtime, "info", runtime->NewFunction(JSMockAppStorage::info)); Obj->SetProperty(runtime, "aboutToBeDeleted", runtime->NewFunction(JSMockAppStorage::aboutToBeDeleted)); } shared_ptr JSMockAppStorage::ref(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) { shared_ptr refImpl_ = runtime->NewObject(); setAbstractPropertyFunc(refImpl_, runtime); return refImpl_; } shared_ptr JSMockAppStorage::setAndRef(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) { shared_ptr setAndRefImpl_ = runtime->NewObject(); setAbstractPropertyFunc(setAndRefImpl_, runtime); return setAndRefImpl_; } shared_ptr JSMockAppStorage::link(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) { shared_ptr linkImpl_ = runtime->NewObject(); setSubAbstractPropertyFunc(linkImpl_, runtime); return linkImpl_; } shared_ptr JSMockAppStorage::setAndLink(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) { shared_ptr setAndLinkImpl_ = runtime->NewObject(); setSubAbstractPropertyFunc(setAndLinkImpl_, runtime); return setAndLinkImpl_; } shared_ptr JSMockAppStorage::prop(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) { shared_ptr propImpl_ = runtime->NewObject(); setSubAbstractPropertyFunc(propImpl_, runtime); return propImpl_; } shared_ptr JSMockAppStorage::setAndProp(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) { shared_ptr setAndPropImpl_ = runtime->NewObject(); setSubAbstractPropertyFunc(setAndPropImpl_, runtime); return setAndPropImpl_; } void JSLoadAppStorage(const shared_ptr globalPtr, const shared_ptr runtime) { shared_ptr appObj = runtime->NewObject(); appObj->SetProperty(runtime, "ref", runtime->NewFunction(JSMockAppStorage::ref)); appObj->SetProperty(runtime, "setAndRef", runtime->NewFunction(JSMockAppStorage::setAndRef)); appObj->SetProperty(runtime, "link", runtime->NewFunction(JSMockAppStorage::link)); appObj->SetProperty(runtime, "setAndLink", runtime->NewFunction(JSMockAppStorage::setAndLink)); appObj->SetProperty(runtime, "prop", runtime->NewFunction(JSMockAppStorage::prop)); appObj->SetProperty(runtime, "setAndProp", runtime->NewFunction(JSMockAppStorage::setAndProp)); appObj->SetProperty(runtime, "has", runtime->NewFunction(JSMockAppStorage::has)); appObj->SetProperty(runtime, "get", runtime->NewFunction(JSMockAppStorage::get)); appObj->SetProperty(runtime, "set", runtime->NewFunction(JSMockAppStorage::set)); appObj->SetProperty(runtime, "setOrCreate", runtime->NewFunction(JSMockAppStorage::setOrCreate)); appObj->SetProperty(runtime, "delete", runtime->NewFunction(JSMockAppStorage::deleteKeys)); appObj->SetProperty(runtime, "keys", runtime->NewFunction(JSMockAppStorage::keys)); appObj->SetProperty(runtime, "clear", runtime->NewFunction(JSMockAppStorage::clear)); appObj->SetProperty(runtime, "size", runtime->NewFunction(JSMockAppStorage::size)); globalPtr->SetProperty(runtime, "AppStorage", appObj); } void JSLoadEnvironment(const shared_ptr globalPtr, const shared_ptr runtime) { shared_ptr envObj = runtime->NewObject(); envObj->SetProperty(runtime, "envProp", runtime->NewFunction(JSMockEnvironment::envProp)); envObj->SetProperty(runtime, "envProps", runtime->NewFunction(JSMockEnvironment::envProps)); envObj->SetProperty(runtime, "keys", runtime->NewFunction(JSMockEnvironment::keys)); globalPtr->SetProperty(runtime, "Environment", envObj); } shared_ptr JSMockPersistenceV2::instanceFunc(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) { shared_ptr persistV2ObjImpl = runtime->NewObject(); persistV2ObjImpl->SetProperty(runtime, "connect", runtime->NewFunction(JSMockPersistenceV2::connect)); persistV2ObjImpl->SetProperty(runtime, "globalConnect", runtime->NewFunction(JSMockPersistenceV2::globalConnect)); persistV2ObjImpl->SetProperty(runtime, "remove", runtime->NewFunction(JSMockPersistenceV2::remove)); persistV2ObjImpl->SetProperty(runtime, "keys", runtime->NewFunction(JSMockPersistenceV2::keys)); persistV2ObjImpl->SetProperty(runtime, "save", runtime->NewFunction(JSMockPersistenceV2::save)); persistV2ObjImpl->SetProperty(runtime, "notifyOnError", runtime->NewFunction(JSMockPersistenceV2::notifyOnError)); return persistV2ObjImpl; } void JSLoadPersistenceV2(const shared_ptr globalPtr, const shared_ptr runtime) { shared_ptr persistV2Obj = runtime->NewObject(); persistV2Obj->SetProperty(runtime, "instance", runtime->NewFunction(JSMockPersistenceV2::instanceFunc)); globalPtr->SetProperty(runtime, "PersistenceV2Impl", persistV2Obj); } shared_ptr JSMockAppStorageV2::instanceFunc(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) { shared_ptr appV2ObjImpl = runtime->NewObject(); appV2ObjImpl->SetProperty(runtime, "connect", runtime->NewFunction(JSMockAppStorageV2::connect)); appV2ObjImpl->SetProperty(runtime, "remove", runtime->NewFunction(JSMockAppStorageV2::remove)); appV2ObjImpl->SetProperty(runtime, "getMemoryKeys", runtime->NewFunction(JSMockAppStorageV2::getMemoryKeys)); return appV2ObjImpl; } void JSLoadAppStorageV2(const shared_ptr globalPtr, const shared_ptr runtime) { shared_ptr appV2Obj = runtime->NewObject(); appV2Obj->SetProperty(runtime, "instance", runtime->NewFunction(JSMockAppStorageV2::instanceFunc)); globalPtr->SetProperty(runtime, "AppStorageV2Impl", appV2Obj); } shared_ptr JSMockUIUtils::instanceFunc(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) { shared_ptr uiUtilsObjImpl = runtime->NewObject(); uiUtilsObjImpl->SetProperty(runtime, "getTarget", runtime->NewFunction(JSMockUIUtils::getTarget)); uiUtilsObjImpl->SetProperty(runtime, "makeObserved", runtime->NewFunction(JSMockUIUtils::makeObserved)); uiUtilsObjImpl->SetProperty(runtime, "makeV1Observed", runtime->NewFunction(JSMockUIUtils::makeV1Observed)); uiUtilsObjImpl->SetProperty(runtime, "enableV2Compatibility", runtime->NewFunction(JSMockUIUtils::enableV2Compatibility)); return uiUtilsObjImpl; } void JSLoadUiUtils(const shared_ptr globalPtr, const shared_ptr runtime) { shared_ptr uiUtilsObj = runtime->NewObject(); uiUtilsObj->SetProperty(runtime, "instance", runtime->NewFunction(JSMockUIUtils::instanceFunc)); globalPtr->SetProperty(runtime, "UIUtilsImpl", uiUtilsObj); } void JSMock::JSBind(BindingTarget globalObj, const shared_ptr runtime, const shared_ptr globalPtr) { auto vm = runtime->GetEcmaVm(); //@Entry globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "registerNamedRoute"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::RegisterNamedRoute)); // V1 Decorator globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Observed"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::Observed)); globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Track"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::Track)); globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Reusable"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::Reusable)); // V2 Decorator globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "ObservedV2"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::ObservedV2)); globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Trace"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::Trace)); globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Local"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::Local)); globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Param"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::Param)); globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Once"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::Once)); globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Event"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::Event)); globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Computed"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::Computed)); globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "ReusableV2"), panda::FunctionRef::New(const_cast(vm), JSMockDecorator::ReusableV2)); JSLoadV2Decorator(globalPtr, runtime); // V1 global instance JSLoadPersistentStorage(globalPtr, runtime); JSLoadAppStorage(globalPtr, runtime); JSLoadEnvironment(globalPtr, runtime); // V2 global instance JSLoadPersistenceV2(globalPtr, runtime); JSLoadAppStorageV2(globalPtr, runtime); JSLoadUiUtils(globalPtr, runtime); } bool JSMock::InitModule( const shared_ptr& runtime, shared_ptr& thisObj, const std::string& moduleName) { static const std::unordered_map& runtime, shared_ptr& thisObj)> MODULE_LIST = { { "ohos.curves", [](const shared_ptr& runtime, shared_ptr& thisObj) { InitCurvesModule(runtime, thisObj); } }, }; auto iter = MODULE_LIST.find(moduleName); if (iter != MODULE_LIST.end()) { iter->second(runtime, thisObj); return true; } else { return false; } } shared_ptr MockRequireNativeModule(const shared_ptr& runtime, const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) { std::string moduleName = argv[0]->ToString(runtime); // has already init module object shared_ptr global = runtime->GetGlobal(); shared_ptr moduleObject = global->GetProperty(runtime, moduleName); if (moduleObject != nullptr && moduleObject->IsObject(runtime)) { return moduleObject; } // init module object first time shared_ptr newObject = runtime->NewObject(); if (JSMock::InitModule(runtime, newObject, moduleName)) { global->SetProperty(runtime, moduleName, newObject); return newObject; } return runtime->NewNull(); } bool JSMock::PreloadWorkerRequireNative(const shared_ptr& runtime, const shared_ptr& global) { return global->SetProperty(runtime, "requireNativeModule", runtime->NewFunction(MockRequireNativeModule)); } } // namespace OHOS::Ace::Framework