1 /* 2 * Copyright (c) 2023 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/bridge/declarative_frontend/jsview/js_scope_util.h" 17 18 #include "frameworks/core/common/container.h" 19 #include "base/memory/referenced.h" 20 21 namespace OHOS::Ace::Framework { 22 int32_t JSScopeUtil::restoreInstanceId_ = -1; 23 JSScopeUtil()24JSScopeUtil::JSScopeUtil() 25 { 26 LOGI("JSScopeUtil constructor"); 27 } 28 JSBind(BindingTarget globalObj)29void JSScopeUtil::JSBind(BindingTarget globalObj) 30 { 31 LOGI("JSScopeUtil JSBind"); 32 JSClass<JSScopeUtil>::Declare("__JSScopeUtil__"); 33 JSClass<JSScopeUtil>::StaticMethod("syncInstanceId", &JSScopeUtil::SyncInstanceId); 34 JSClass<JSScopeUtil>::StaticMethod("restoreInstanceId", &JSScopeUtil::RestoreInstanceId); 35 JSClass<JSScopeUtil>::Bind(globalObj); 36 } 37 SyncInstanceId(const JSCallbackInfo & info)38void JSScopeUtil::SyncInstanceId(const JSCallbackInfo& info) 39 { 40 if (info.Length() < 1) { 41 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments"); 42 return; 43 } 44 45 if (!info[0]->IsNumber()) { 46 LOGE("arg is not Number."); 47 return; 48 } 49 50 int32_t instanceId = info[0]->ToNumber<int32_t>(); 51 52 restoreInstanceId_ = Container::CurrentId(); 53 ContainerScope::UpdateCurrent(instanceId); 54 } 55 RestoreInstanceId(const JSCallbackInfo & info)56void JSScopeUtil::RestoreInstanceId(const JSCallbackInfo& info) 57 { 58 ContainerScope::UpdateCurrent(restoreInstanceId_); 59 } 60 } // namespace OHOS::Ace::Framework