• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_xcomponent_node_bridge.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_xcomponent.h"
19 #include "bridge/declarative_frontend/jsview/js_xcomponent_controller.h"
20 
21 namespace OHOS::Ace::NG {
22 
SetXComponentNodeParams(ArkUIRuntimeCallInfo * runtimeCallInfo,EcmaVM * vm)23 Framework::XComponentParams XComponentNodeBridge::SetXComponentNodeParams(
24     ArkUIRuntimeCallInfo* runtimeCallInfo, EcmaVM* vm)
25 {
26     Framework::XComponentParams params;
27 
28     // elmtId
29     Local<JSValueRef> arg = runtimeCallInfo->GetCallArgRef(0);
30     if (arg->IsNumber()) {
31         params.elmtId = arg->Int32Value(vm);
32     }
33     // xcomponent id
34     arg = runtimeCallInfo->GetCallArgRef(1);
35     if (arg->IsString(vm)) {
36         params.xcomponentId = arg->ToString(vm)->ToString(vm);
37     }
38     // xcomponentType
39     arg = runtimeCallInfo->GetCallArgRef(2);
40     if (arg->IsNumber()) {
41         params.xcomponentType = arg->Int32Value(vm);
42     }
43     // renderType
44     arg = runtimeCallInfo->GetCallArgRef(3);
45     if (arg->IsNumber()) {
46         params.renderType = arg->Int32Value(vm);
47     }
48     // surfaceId
49     arg = runtimeCallInfo->GetCallArgRef(4);
50     if (arg->IsString(vm)) {
51         params.surfaceId = arg->ToString(vm)->ToString(vm);
52     }
53     // selfIdealWidth
54     arg = runtimeCallInfo->GetCallArgRef(5);
55     if (arg->IsNumber()) {
56         params.width = arg->Int32Value(vm);
57     }
58     // selfIdealHeight
59     arg = runtimeCallInfo->GetCallArgRef(6);
60     if (arg->IsNumber()) {
61         params.height = arg->Int32Value(vm);
62     }
63     // libraryname
64     arg = runtimeCallInfo->GetCallArgRef(7);
65     if (arg->IsString(vm)) {
66         params.libraryName = arg->ToString(vm)->ToString(vm);
67     }
68     // xComponentController
69     arg = runtimeCallInfo->GetCallArgRef(8);
70     if (!arg->IsUndefined()) {
71         params.controller =
72             static_cast<Framework::JSXComponentController*>(Local<panda::ObjectRef>(arg)->GetNativePointerField(
73                 vm, 0));
74     }
75 
76     return params;
77 }
78 
Create(ArkUIRuntimeCallInfo * runtimeCallInfo)79 ArkUINativeModuleValue XComponentNodeBridge::Create(ArkUIRuntimeCallInfo* runtimeCallInfo)
80 {
81     EcmaVM* vm = runtimeCallInfo->GetVM();
82     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
83     Framework::XComponentParams params = SetXComponentNodeParams(runtimeCallInfo, vm);
84     void* jsXComponent = Framework::JSXComponent::Create(params);
85     auto nativeModule = panda::NativePointerRef::New(
86         vm, reinterpret_cast<void*>(jsXComponent),
87         [](void *env, void* data, [[maybe_unused]] void* hint) {
88             auto* jsXComponent = reinterpret_cast<Framework::JSXComponent*>(data);
89             if (jsXComponent) {
90                 delete jsXComponent;
91                 jsXComponent = nullptr;
92             }
93         },
94         reinterpret_cast<void*>(jsXComponent), sizeof(void*));
95     return nativeModule;
96 }
97 
GetFrameNode(ArkUIRuntimeCallInfo * runtimeCallInfo)98 ArkUINativeModuleValue XComponentNodeBridge::GetFrameNode(ArkUIRuntimeCallInfo* runtimeCallInfo)
99 {
100     EcmaVM* vm = runtimeCallInfo->GetVM();
101     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
102     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
103     if (!firstArg->IsNativePointer(vm)) {
104         return panda::JSValueRef::Undefined(vm);
105     }
106     Framework::JSXComponent* jsXComponent =
107         reinterpret_cast<Framework::JSXComponent*>(firstArg->ToNativePointer(vm)->Value());
108     if (jsXComponent) {
109         auto frameNode = jsXComponent->GetFrameNode();
110         auto nativeModule = panda::NativePointerRef::New(vm, reinterpret_cast<void*>(AceType::RawPtr(frameNode)));
111         return nativeModule;
112     }
113     return panda::JSValueRef::Undefined(vm);
114 }
115 
RegisterOnCreateCallback(ArkUIRuntimeCallInfo * runtimeCallInfo)116 ArkUINativeModuleValue XComponentNodeBridge::RegisterOnCreateCallback(ArkUIRuntimeCallInfo* runtimeCallInfo)
117 {
118     EcmaVM* vm = runtimeCallInfo->GetVM();
119     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
120     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
121     if (!firstArg->IsNativePointer(vm)) {
122         return panda::JSValueRef::Undefined(vm);
123     }
124     Framework::JSXComponent* jsXComponent =
125         reinterpret_cast<Framework::JSXComponent*>(firstArg->ToNativePointer(vm)->Value());
126     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
127     if (jsXComponent && secondArg->IsFunction(vm)) {
128         Framework::JsiExecutionContext execCtx = { vm };
129         jsXComponent->RegisterOnCreate(execCtx, secondArg);
130     }
131     return panda::JSValueRef::Undefined(vm);
132 }
133 
RegisterOnDestroyCallback(ArkUIRuntimeCallInfo * runtimeCallInfo)134 ArkUINativeModuleValue XComponentNodeBridge::RegisterOnDestroyCallback(ArkUIRuntimeCallInfo* runtimeCallInfo)
135 {
136     EcmaVM* vm = runtimeCallInfo->GetVM();
137     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
138     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
139     if (!firstArg->IsNativePointer(vm)) {
140         return panda::JSValueRef::Undefined(vm);
141     }
142     Framework::JSXComponent* jsXComponent =
143         reinterpret_cast<Framework::JSXComponent*>(firstArg->ToNativePointer(vm)->Value());
144     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
145     if (jsXComponent && secondArg->IsFunction(vm)) {
146         Framework::JsiExecutionContext execCtx = { vm };
147         jsXComponent->RegisterOnDestroy(execCtx, secondArg);
148     }
149     return panda::JSValueRef::Undefined(vm);
150 }
151 
ChangeRenderType(ArkUIRuntimeCallInfo * runtimeCallInfo)152 ArkUINativeModuleValue XComponentNodeBridge::ChangeRenderType(ArkUIRuntimeCallInfo* runtimeCallInfo)
153 {
154     EcmaVM* vm = runtimeCallInfo->GetVM();
155     auto defaultNativeModule = panda::BooleanRef::New(vm, false);
156     CHECK_NULL_RETURN(vm, defaultNativeModule);
157     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
158     if (!firstArg->IsNativePointer(vm)) {
159         return defaultNativeModule;
160     }
161     Framework::JSXComponent* jsXComponent =
162         reinterpret_cast<Framework::JSXComponent*>(firstArg->ToNativePointer(vm)->Value());
163     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
164     if (jsXComponent && secondArg->IsNumber()) {
165         auto ret = jsXComponent->ChangeRenderType(secondArg->Int32Value(vm));
166         auto nativeModule = panda::BooleanRef::New(vm, ret);
167         return nativeModule;
168     }
169     return defaultNativeModule;
170 }
171 } // namespace OHOS::Ace::NG
172