1 /*
2 * Copyright (c) 2024 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 #include "bridge/declarative_frontend/jsview/js_mock.h"
16 #include "bridge/declarative_frontend/engine/jsi/jsi_bindings.h"
17 #include "bridge/declarative_frontend/engine/jsi/modules/jsi_curves_module.h"
18 #include "bridge/js_frontend/engine/jsi/ark_js_value.h"
19
20 namespace OHOS::Ace::Framework {
JSBind(BindingTarget globalObj)21 void JSMockBaseNode::JSBind(BindingTarget globalObj)
22 {
23 JSClass<JSMockBaseNode>::Declare("__JSBaseNode__");
24
25 JSClass<JSMockBaseNode>::CustomMethod("create", &JSMockBaseNode::Create);
26 JSClass<JSMockBaseNode>::CustomMethod("finishUpdateFunc", &JSMockBaseNode::FinishUpdateFunc);
27 JSClass<JSMockBaseNode>::CustomMethod("postTouchEvent", &JSMockBaseNode::PostTouchEvent);
28 JSClass<JSMockBaseNode>::CustomMethod("disposeNode", &JSMockBaseNode::Dispose);
29 JSClass<JSMockBaseNode>::CustomMethod("updateStart", &JSMockBaseNode::UpdateStart);
30 JSClass<JSMockBaseNode>::CustomMethod("updateEnd", &JSMockBaseNode::UpdateEnd);
31
32 JSClass<JSMockBaseNode>::Bind(globalObj, JSMockBaseNode::ConstructorCallback, JSMockBaseNode::DestructorCallback);
33 }
34
JSBind(BindingTarget globalObj,std::string name)35 void JSMockViewPU::JSBind(BindingTarget globalObj, std::string name)
36 {
37 JSClass<JSMockViewPU>::Declare(name.c_str());
38
39 JSClass<JSMockViewPU>::CustomMethod("id__", &JSMockViewPU::Id);
40 JSClass<JSMockViewPU>::CustomMethod("updateStateVars", &JSMockViewPU::UpdateStateVars);
41 JSClass<JSMockViewPU>::CustomMethod("aboutToReuseInternal", &JSMockViewPU::AboutToReuseInternal);
42 JSClass<JSMockViewPU>::CustomMethod("aboutToRecycleInternal", &JSMockViewPU::AboutToRecycleInternal);
43 JSClass<JSMockViewPU>::CustomMethod("updateDirtyElements", &JSMockViewPU::UpdateDirtyElements);
44
45 JSClass<JSMockViewPU>::Bind(globalObj);
46 }
47
JSBind(BindingTarget globalObj)48 void JSMockScopeUtil::JSBind(BindingTarget globalObj)
49 {
50 JSClass<JSMockScopeUtil>::Declare("__JSScopeUtil__");
51 JSClass<JSMockScopeUtil>::StaticMethod("syncInstanceId", &JSMockScopeUtil::SyncInstanceId);
52 JSClass<JSMockScopeUtil>::StaticMethod("restoreInstanceId", &JSMockScopeUtil::RestoreInstanceId);
53
54 JSClass<JSMockScopeUtil>::Bind(globalObj);
55 }
56
JSBind(BindingTarget globalObj)57 void MockCustomDialogController::JSBind(BindingTarget globalObj)
58 {
59 JSClass<MockCustomDialogController>::Declare("CustomDialogController");
60
61 JSClass<MockCustomDialogController>::CustomMethod("open", &MockCustomDialogController::Open);
62 JSClass<MockCustomDialogController>::CustomMethod("close", &MockCustomDialogController::Close);
63
64 JSClass<JSMockBaseNode>::Bind(globalObj);
65 }
66
JSBind(BindingTarget globalObj)67 void JSMockNativeCustomSpan::JSBind(BindingTarget globalObj)
68 {
69 JSClass<JSMockNativeCustomSpan>::Declare("NativeCustomSpan");
70 JSClass<JSMockNativeCustomSpan>::CustomMethod("invalidate", &JSMockNativeCustomSpan::Invalidate);
71 JSClass<JSMockNativeCustomSpan>::Bind(globalObj);
72 }
73
setAbstractPropertyFunc(Local<ObjectRef> & objRef,EcmaVM * vm)74 void setAbstractPropertyFunc(Local<ObjectRef>& objRef, EcmaVM* vm)
75 {
76 objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "get"),
77 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockAbstractProperty::get));
78 objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "set"),
79 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockAbstractProperty::set));
80 objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "info"),
81 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockAbstractProperty::info));
82 }
83
setSubAbstractPropertyFunc(Local<ObjectRef> & objRef,EcmaVM * vm)84 void setSubAbstractPropertyFunc(Local<ObjectRef>& objRef, EcmaVM* vm)
85 {
86 objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "get"),
87 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockSubAbstractProperty::get));
88 objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "set"),
89 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockSubAbstractProperty::set));
90 objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "info"),
91 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockSubAbstractProperty::info));
92 objRef->Set(vm, panda::StringRef::NewFromUtf8(vm, "aboutToBeDeleted"),
93 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockSubAbstractProperty::aboutToBeDeleted));
94 }
95
ref(panda::JsiRuntimeCallInfo * runtimeCallInfo)96 Local<JSValueRef> JSMockLocalStorage::ref(panda::JsiRuntimeCallInfo* runtimeCallInfo)
97 {
98 EcmaVM* vm = runtimeCallInfo->GetVM();
99 Local<ObjectRef> objRef = ObjectRef::New(vm);
100 setAbstractPropertyFunc(objRef, vm);
101 return objRef;
102 }
103
setAndRef(panda::JsiRuntimeCallInfo * runtimeCallInfo)104 Local<JSValueRef> JSMockLocalStorage::setAndRef(panda::JsiRuntimeCallInfo* runtimeCallInfo)
105 {
106 EcmaVM* vm = runtimeCallInfo->GetVM();
107 Local<ObjectRef> objRef = ObjectRef::New(vm);
108 setAbstractPropertyFunc(objRef, vm);
109 return objRef;
110 }
111
link(panda::JsiRuntimeCallInfo * runtimeCallInfo)112 Local<JSValueRef> JSMockLocalStorage::link(panda::JsiRuntimeCallInfo* runtimeCallInfo)
113 {
114 EcmaVM* vm = runtimeCallInfo->GetVM();
115 Local<ObjectRef> objRef = ObjectRef::New(vm);
116 setSubAbstractPropertyFunc(objRef, vm);
117 return objRef;
118 }
119
setAndLink(panda::JsiRuntimeCallInfo * runtimeCallInfo)120 Local<JSValueRef> JSMockLocalStorage::setAndLink(panda::JsiRuntimeCallInfo* runtimeCallInfo)
121 {
122 EcmaVM* vm = runtimeCallInfo->GetVM();
123 Local<ObjectRef> objRef = ObjectRef::New(vm);
124 setSubAbstractPropertyFunc(objRef, vm);
125 return objRef;
126 }
127
prop(panda::JsiRuntimeCallInfo * runtimeCallInfo)128 Local<JSValueRef> JSMockLocalStorage::prop(panda::JsiRuntimeCallInfo* runtimeCallInfo)
129 {
130 EcmaVM* vm = runtimeCallInfo->GetVM();
131 Local<ObjectRef> objRef = ObjectRef::New(vm);
132 setSubAbstractPropertyFunc(objRef, vm);
133 return objRef;
134 }
135
setAndProp(panda::JsiRuntimeCallInfo * runtimeCallInfo)136 Local<JSValueRef> JSMockLocalStorage::setAndProp(panda::JsiRuntimeCallInfo* runtimeCallInfo)
137 {
138 EcmaVM* vm = runtimeCallInfo->GetVM();
139 Local<ObjectRef> objRef = ObjectRef::New(vm);
140 setSubAbstractPropertyFunc(objRef, vm);
141 return objRef;
142 }
143
JSBind(BindingTarget globalObj)144 void JSMockLocalStorage::JSBind(BindingTarget globalObj)
145 {
146 JSClass<JSMockLocalStorage>::Declare("LocalStorage");
147
148 JSClass<JSMockLocalStorage>::CustomMethod("has", &JSMockLocalStorage::has);
149 JSClass<JSMockLocalStorage>::CustomMethod("get", &JSMockLocalStorage::get);
150 JSClass<JSMockLocalStorage>::CustomMethod("set", &JSMockLocalStorage::set);
151 JSClass<JSMockLocalStorage>::CustomMethod("setOrCreate", &JSMockLocalStorage::setOrCreate);
152 JSClass<JSMockLocalStorage>::CustomMethod("ref", &JSMockLocalStorage::ref);
153 JSClass<JSMockLocalStorage>::CustomMethod("setAndRef", &JSMockLocalStorage::setAndRef);
154 JSClass<JSMockLocalStorage>::CustomMethod("link", &JSMockLocalStorage::link);
155 JSClass<JSMockLocalStorage>::CustomMethod("setAndLink", &JSMockLocalStorage::setAndLink);
156 JSClass<JSMockLocalStorage>::CustomMethod("prop", &JSMockLocalStorage::prop);
157 JSClass<JSMockLocalStorage>::CustomMethod("setAndProp", &JSMockLocalStorage::setAndProp);
158 JSClass<JSMockLocalStorage>::CustomMethod("delete", &JSMockLocalStorage::deleteKeys);
159 JSClass<JSMockLocalStorage>::CustomMethod("keys", &JSMockLocalStorage::keys);
160 JSClass<JSMockLocalStorage>::CustomMethod("size", &JSMockLocalStorage::size);
161 JSClass<JSMockLocalStorage>::CustomMethod("clear", &JSMockLocalStorage::clear);
162
163 JSClass<JSMockLocalStorage>::Bind(globalObj);
164 }
165
JSBind(BindingTarget globalObj)166 void JSMockViewBuildNodeBase::JSBind(BindingTarget globalObj)
167 {
168 JSClass<JSMockViewBuildNodeBase>::Declare("ViewBuildNodeBase");
169
170 JSClass<JSMockViewBuildNodeBase>::StaticMethod(
171 "setArkThemeScopeManager", &JSMockViewBuildNodeBase::SetArkThemeScopeManager);
172 JSClass<JSMockViewBuildNodeBase>::CustomMethod("id__", &JSMockViewBuildNodeBase::Id);
173 JSClass<JSMockViewBuildNodeBase>::CustomMethod("debugInfo__", &JSMockViewBuildNodeBase::DebugInfo);
174 JSClass<JSMockViewBuildNodeBase>::CustomMethod("debugInfoElmtId", &JSMockViewBuildNodeBase::DebugInfoElmtId);
175 JSClass<JSMockViewBuildNodeBase>::CustomMethod("getChildById", &JSMockViewBuildNodeBase::GetChildById);
176 JSClass<JSMockViewBuildNodeBase>::CustomMethod("addChild", &JSMockViewBuildNodeBase::AddChild);
177 JSClass<JSMockViewBuildNodeBase>::CustomMethod(
178 "purgeDeletedElmtIds", &JSMockViewBuildNodeBase::PurgeDeletedElmtIds);
179 JSClass<JSMockViewBuildNodeBase>::CustomMethod(
180 "updateStateVarsOfChildByElmtId", &JSMockViewBuildNodeBase::UpdateStateVarsOfChildByElmtId);
181 JSClass<JSMockViewBuildNodeBase>::CustomMethod("createOrGetNode", &JSMockViewBuildNodeBase::CreateOrGetNode);
182 JSClass<JSMockViewBuildNodeBase>::CustomMethod(
183 "ifElseBranchUpdateFunction", &JSMockViewBuildNodeBase::IfElseBranchUpdateFunction);
184 JSClass<JSMockViewBuildNodeBase>::CustomMethod(
185 "onWillApplyThemeInternally", &JSMockViewBuildNodeBase::OnWillApplyThemeInternally);
186 JSClass<JSMockViewBuildNodeBase>::CustomMethod("onWillApplyTheme", &JSMockViewBuildNodeBase::OnWillApplyTheme);
187 JSClass<JSMockViewBuildNodeBase>::CustomMethod(
188 "onGlobalThemeChanged", &JSMockViewBuildNodeBase::OnGlobalThemeChanged);
189
190 JSClass<JSMockViewBuildNodeBase>::Bind(
191 globalObj, JSMockViewBuildNodeBase::ConstructorCallback, JSMockViewBuildNodeBase::DestructorCallback);
192 }
193
JSBind(BindingTarget globalObj)194 void JSMock::JSBind(BindingTarget globalObj)
195 {
196 JSMockBaseNode::JSBind(globalObj);
197 JSMockViewPU::JSBind(globalObj, "ViewPU");
198 JSMockViewPU::JSBind(globalObj, "ViewV2");
199 JSMockViewPU::JSBind(globalObj, "PUV2ViewBase");
200 JSMockScopeUtil::JSBind(globalObj);
201 MockCustomDialogController::JSBind(globalObj);
202 JSMockLocalStorage::JSBind(globalObj);
203 JSMockNativeCustomSpan::JSBind(globalObj);
204 JSMockViewBuildNodeBase::JSBind(globalObj);
205 }
206
JSLoadV2Decorator(const shared_ptr<JsValue> globalPtr,const shared_ptr<JsRuntime> runtime)207 void JSLoadV2Decorator(const shared_ptr<JsValue> globalPtr, const shared_ptr<JsRuntime> runtime)
208 {
209 shared_ptr<JsValue> decoratorPtr = runtime->NewFunction(JsMockFunc::CreatFunction);
210 globalPtr->SetProperty(runtime, "Provider", decoratorPtr);
211 globalPtr->SetProperty(runtime, "Consumer", decoratorPtr);
212 globalPtr->SetProperty(runtime, "Monitor", decoratorPtr);
213 globalPtr->SetProperty(runtime, "__Type__", decoratorPtr);
214 }
215
JSLoadPersistentStorage(const shared_ptr<JsValue> globalPtr,const shared_ptr<JsRuntime> runtime)216 void JSLoadPersistentStorage(const shared_ptr<JsValue> globalPtr, const shared_ptr<JsRuntime> runtime)
217 {
218 shared_ptr<JsValue> persistObj = runtime->NewObject();
219 persistObj->SetProperty(runtime, "persistProp", runtime->NewFunction(JSMockPersistStorage::persistProp));
220 persistObj->SetProperty(runtime, "deleteProp", runtime->NewFunction(JSMockPersistStorage::deleteProp));
221 persistObj->SetProperty(runtime, "persistProps", runtime->NewFunction(JSMockPersistStorage::persistProps));
222 persistObj->SetProperty(runtime, "keys", runtime->NewFunction(JSMockPersistStorage::keys));
223 globalPtr->SetProperty(runtime, "PersistentStorage", persistObj);
224 }
225
setAbstractPropertyFunc(shared_ptr<JsValue> & Obj,const shared_ptr<JsRuntime> & runtime)226 void setAbstractPropertyFunc(shared_ptr<JsValue>& Obj, const shared_ptr<JsRuntime>& runtime)
227 {
228 Obj->SetProperty(runtime, "get", runtime->NewFunction(JSMockAppStorage::get));
229 Obj->SetProperty(runtime, "set", runtime->NewFunction(JSMockAppStorage::set));
230 Obj->SetProperty(runtime, "info", runtime->NewFunction(JSMockAppStorage::info));
231 }
232
setSubAbstractPropertyFunc(shared_ptr<JsValue> & Obj,const shared_ptr<JsRuntime> & runtime)233 void setSubAbstractPropertyFunc(shared_ptr<JsValue>& Obj, const shared_ptr<JsRuntime>& runtime)
234 {
235 Obj->SetProperty(runtime, "get", runtime->NewFunction(JSMockAppStorage::get));
236 Obj->SetProperty(runtime, "set", runtime->NewFunction(JSMockAppStorage::set));
237 Obj->SetProperty(runtime, "info", runtime->NewFunction(JSMockAppStorage::info));
238 Obj->SetProperty(runtime, "aboutToBeDeleted", runtime->NewFunction(JSMockAppStorage::aboutToBeDeleted));
239 }
240
ref(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)241 shared_ptr<JsValue> JSMockAppStorage::ref(const shared_ptr<JsRuntime>& runtime,
242 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
243 {
244 shared_ptr<JsValue> refImpl_ = runtime->NewObject();
245 setAbstractPropertyFunc(refImpl_, runtime);
246 return refImpl_;
247 }
248
setAndRef(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)249 shared_ptr<JsValue> JSMockAppStorage::setAndRef(const shared_ptr<JsRuntime>& runtime,
250 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
251 {
252 shared_ptr<JsValue> setAndRefImpl_ = runtime->NewObject();
253 setAbstractPropertyFunc(setAndRefImpl_, runtime);
254 return setAndRefImpl_;
255 }
256
link(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)257 shared_ptr<JsValue> JSMockAppStorage::link(const shared_ptr<JsRuntime>& runtime,
258 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
259 {
260 shared_ptr<JsValue> linkImpl_ = runtime->NewObject();
261 setSubAbstractPropertyFunc(linkImpl_, runtime);
262 return linkImpl_;
263 }
264
setAndLink(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)265 shared_ptr<JsValue> JSMockAppStorage::setAndLink(const shared_ptr<JsRuntime>& runtime,
266 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
267 {
268 shared_ptr<JsValue> setAndLinkImpl_ = runtime->NewObject();
269 setSubAbstractPropertyFunc(setAndLinkImpl_, runtime);
270 return setAndLinkImpl_;
271 }
272
prop(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)273 shared_ptr<JsValue> JSMockAppStorage::prop(const shared_ptr<JsRuntime>& runtime,
274 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
275 {
276 shared_ptr<JsValue> propImpl_ = runtime->NewObject();
277 setSubAbstractPropertyFunc(propImpl_, runtime);
278 return propImpl_;
279 }
280
setAndProp(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)281 shared_ptr<JsValue> JSMockAppStorage::setAndProp(const shared_ptr<JsRuntime>& runtime,
282 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
283 {
284 shared_ptr<JsValue> setAndPropImpl_ = runtime->NewObject();
285 setSubAbstractPropertyFunc(setAndPropImpl_, runtime);
286 return setAndPropImpl_;
287 }
288
JSLoadAppStorage(const shared_ptr<JsValue> globalPtr,const shared_ptr<JsRuntime> runtime)289 void JSLoadAppStorage(const shared_ptr<JsValue> globalPtr, const shared_ptr<JsRuntime> runtime)
290 {
291 shared_ptr<JsValue> appObj = runtime->NewObject();
292 appObj->SetProperty(runtime, "ref", runtime->NewFunction(JSMockAppStorage::ref));
293 appObj->SetProperty(runtime, "setAndRef", runtime->NewFunction(JSMockAppStorage::setAndRef));
294 appObj->SetProperty(runtime, "link", runtime->NewFunction(JSMockAppStorage::link));
295 appObj->SetProperty(runtime, "setAndLink", runtime->NewFunction(JSMockAppStorage::setAndLink));
296 appObj->SetProperty(runtime, "prop", runtime->NewFunction(JSMockAppStorage::prop));
297 appObj->SetProperty(runtime, "setAndProp", runtime->NewFunction(JSMockAppStorage::setAndProp));
298 appObj->SetProperty(runtime, "has", runtime->NewFunction(JSMockAppStorage::has));
299 appObj->SetProperty(runtime, "get", runtime->NewFunction(JSMockAppStorage::get));
300 appObj->SetProperty(runtime, "set", runtime->NewFunction(JSMockAppStorage::set));
301 appObj->SetProperty(runtime, "setOrCreate", runtime->NewFunction(JSMockAppStorage::setOrCreate));
302 appObj->SetProperty(runtime, "delete", runtime->NewFunction(JSMockAppStorage::deleteKeys));
303 appObj->SetProperty(runtime, "keys", runtime->NewFunction(JSMockAppStorage::keys));
304 appObj->SetProperty(runtime, "clear", runtime->NewFunction(JSMockAppStorage::clear));
305 appObj->SetProperty(runtime, "size", runtime->NewFunction(JSMockAppStorage::size));
306 globalPtr->SetProperty(runtime, "AppStorage", appObj);
307 }
308
JSLoadEnvironment(const shared_ptr<JsValue> globalPtr,const shared_ptr<JsRuntime> runtime)309 void JSLoadEnvironment(const shared_ptr<JsValue> globalPtr, const shared_ptr<JsRuntime> runtime)
310 {
311 shared_ptr<JsValue> envObj = runtime->NewObject();
312 envObj->SetProperty(runtime, "envProp", runtime->NewFunction(JSMockEnvironment::envProp));
313 envObj->SetProperty(runtime, "envProps", runtime->NewFunction(JSMockEnvironment::envProps));
314 envObj->SetProperty(runtime, "keys", runtime->NewFunction(JSMockEnvironment::keys));
315 globalPtr->SetProperty(runtime, "Environment", envObj);
316 }
317
instanceFunc(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)318 shared_ptr<JsValue> JSMockPersistenceV2::instanceFunc(const shared_ptr<JsRuntime>& runtime,
319 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
320 {
321 shared_ptr<JsValue> persistV2ObjImpl = runtime->NewObject();
322 persistV2ObjImpl->SetProperty(runtime, "connect", runtime->NewFunction(JSMockPersistenceV2::connect));
323 persistV2ObjImpl->SetProperty(runtime, "globalConnect", runtime->NewFunction(JSMockPersistenceV2::globalConnect));
324 persistV2ObjImpl->SetProperty(runtime, "remove", runtime->NewFunction(JSMockPersistenceV2::remove));
325 persistV2ObjImpl->SetProperty(runtime, "keys", runtime->NewFunction(JSMockPersistenceV2::keys));
326 persistV2ObjImpl->SetProperty(runtime, "save", runtime->NewFunction(JSMockPersistenceV2::save));
327 persistV2ObjImpl->SetProperty(runtime, "notifyOnError", runtime->NewFunction(JSMockPersistenceV2::notifyOnError));
328 return persistV2ObjImpl;
329 }
330
JSLoadPersistenceV2(const shared_ptr<JsValue> globalPtr,const shared_ptr<JsRuntime> runtime)331 void JSLoadPersistenceV2(const shared_ptr<JsValue> globalPtr, const shared_ptr<JsRuntime> runtime)
332 {
333 shared_ptr<JsValue> persistV2Obj = runtime->NewObject();
334 persistV2Obj->SetProperty(runtime, "instance", runtime->NewFunction(JSMockPersistenceV2::instanceFunc));
335 globalPtr->SetProperty(runtime, "PersistenceV2Impl", persistV2Obj);
336 }
337
instanceFunc(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)338 shared_ptr<JsValue> JSMockAppStorageV2::instanceFunc(const shared_ptr<JsRuntime>& runtime,
339 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
340 {
341 shared_ptr<JsValue> appV2ObjImpl = runtime->NewObject();
342 appV2ObjImpl->SetProperty(runtime, "connect", runtime->NewFunction(JSMockAppStorageV2::connect));
343 appV2ObjImpl->SetProperty(runtime, "remove", runtime->NewFunction(JSMockAppStorageV2::remove));
344 appV2ObjImpl->SetProperty(runtime, "getMemoryKeys", runtime->NewFunction(JSMockAppStorageV2::getMemoryKeys));
345 return appV2ObjImpl;
346 }
347
JSLoadAppStorageV2(const shared_ptr<JsValue> globalPtr,const shared_ptr<JsRuntime> runtime)348 void JSLoadAppStorageV2(const shared_ptr<JsValue> globalPtr, const shared_ptr<JsRuntime> runtime)
349 {
350 shared_ptr<JsValue> appV2Obj = runtime->NewObject();
351 appV2Obj->SetProperty(runtime, "instance", runtime->NewFunction(JSMockAppStorageV2::instanceFunc));
352 globalPtr->SetProperty(runtime, "AppStorageV2Impl", appV2Obj);
353 }
354
instanceFunc(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)355 shared_ptr<JsValue> JSMockUIUtils::instanceFunc(const shared_ptr<JsRuntime>& runtime,
356 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
357 {
358 shared_ptr<JsValue> uiUtilsObjImpl = runtime->NewObject();
359 uiUtilsObjImpl->SetProperty(runtime, "getTarget", runtime->NewFunction(JSMockUIUtils::getTarget));
360 uiUtilsObjImpl->SetProperty(runtime, "makeObserved", runtime->NewFunction(JSMockUIUtils::makeObserved));
361 uiUtilsObjImpl->SetProperty(runtime, "makeV1Observed", runtime->NewFunction(JSMockUIUtils::makeV1Observed));
362 uiUtilsObjImpl->SetProperty(runtime, "enableV2Compatibility",
363 runtime->NewFunction(JSMockUIUtils::enableV2Compatibility));
364 return uiUtilsObjImpl;
365 }
366
JSLoadUiUtils(const shared_ptr<JsValue> globalPtr,const shared_ptr<JsRuntime> runtime)367 void JSLoadUiUtils(const shared_ptr<JsValue> globalPtr, const shared_ptr<JsRuntime> runtime)
368 {
369 shared_ptr<JsValue> uiUtilsObj = runtime->NewObject();
370 uiUtilsObj->SetProperty(runtime, "instance", runtime->NewFunction(JSMockUIUtils::instanceFunc));
371 globalPtr->SetProperty(runtime, "UIUtilsImpl", uiUtilsObj);
372 }
373
JSBind(BindingTarget globalObj,const shared_ptr<JsRuntime> runtime,const shared_ptr<JsValue> globalPtr)374 void JSMock::JSBind(BindingTarget globalObj, const shared_ptr<JsRuntime> runtime,
375 const shared_ptr<JsValue> globalPtr)
376 {
377 auto vm = runtime->GetEcmaVm();
378
379 //@Entry
380 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "registerNamedRoute"),
381 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::RegisterNamedRoute));
382
383 // V1 Decorator
384 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Observed"),
385 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::Observed));
386 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Track"),
387 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::Track));
388 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Reusable"),
389 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::Reusable));
390
391 // V2 Decorator
392 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "ObservedV2"),
393 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::ObservedV2));
394 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Trace"),
395 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::Trace));
396 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Local"),
397 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::Local));
398 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Param"),
399 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::Param));
400 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Once"),
401 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::Once));
402 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Event"),
403 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::Event));
404 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "Computed"),
405 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::Computed));
406 globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "ReusableV2"),
407 panda::FunctionRef::New(const_cast<panda::EcmaVM*>(vm), JSMockDecorator::ReusableV2));
408 JSLoadV2Decorator(globalPtr, runtime);
409
410 // V1 global instance
411 JSLoadPersistentStorage(globalPtr, runtime);
412 JSLoadAppStorage(globalPtr, runtime);
413 JSLoadEnvironment(globalPtr, runtime);
414
415 // V2 global instance
416 JSLoadPersistenceV2(globalPtr, runtime);
417 JSLoadAppStorageV2(globalPtr, runtime);
418 JSLoadUiUtils(globalPtr, runtime);
419 }
420
InitModule(const shared_ptr<JsRuntime> & runtime,shared_ptr<JsValue> & thisObj,const std::string & moduleName)421 bool JSMock::InitModule(
422 const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& thisObj, const std::string& moduleName)
423 {
424 static const std::unordered_map<std::string,
425 void (*)(const shared_ptr<JsRuntime>& runtime, shared_ptr<JsValue>& thisObj)>
426 MODULE_LIST = {
427 { "ohos.curves", [](const shared_ptr<JsRuntime>& runtime,
428 shared_ptr<JsValue>& thisObj) { InitCurvesModule(runtime, thisObj); } },
429 };
430 auto iter = MODULE_LIST.find(moduleName);
431 if (iter != MODULE_LIST.end()) {
432 iter->second(runtime, thisObj);
433 return true;
434 } else {
435 return false;
436 }
437 }
438
MockRequireNativeModule(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & thisObj,const std::vector<shared_ptr<JsValue>> & argv,int32_t argc)439 shared_ptr<JsValue> MockRequireNativeModule(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj,
440 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc)
441 {
442 std::string moduleName = argv[0]->ToString(runtime);
443
444 // has already init module object
445 shared_ptr<JsValue> global = runtime->GetGlobal();
446 shared_ptr<JsValue> moduleObject = global->GetProperty(runtime, moduleName);
447 if (moduleObject != nullptr && moduleObject->IsObject(runtime)) {
448 return moduleObject;
449 }
450
451 // init module object first time
452 shared_ptr<JsValue> newObject = runtime->NewObject();
453 if (JSMock::InitModule(runtime, newObject, moduleName)) {
454 global->SetProperty(runtime, moduleName, newObject);
455 return newObject;
456 }
457
458 return runtime->NewNull();
459 }
460
PreloadWorkerRequireNative(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & global)461 bool JSMock::PreloadWorkerRequireNative(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& global)
462 {
463 return global->SetProperty(runtime, "requireNativeModule", runtime->NewFunction(MockRequireNativeModule));
464 }
465 } // namespace OHOS::Ace::Framework
466