/* * 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 JSMock::JSBind(BindingTarget globalObj) { JSMockBaseNode::JSBind(globalObj); JSMockViewPU::JSBind(globalObj, "ViewPU"); JSMockViewPU::JSBind(globalObj, "PUV2ViewBase"); JSMockScopeUtil::JSBind(globalObj); MockCustomDialogController::JSBind(globalObj); } 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