idlize
270 строк · 6.7 Кб
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*/
15import * as fs from "fs"16import * as path from "path"17
18const importTsInteropTypes = `19import {
20int32,
21float32,
22KInt,
23KBoolean,
24KStringPtr,
25KPointer,
26KNativePointer,
27Int32ArrayPtr,
28KUint8ArrayPtr,
29} from "./types"
30import {
31NativeStringBase,
32withByteArray,
33Access,
34providePlatformDefinedData,
35nullptr
36} from "./Interop"
37`.trim()38
39export function nativeModuleDeclaration(methods: string[], nativeBridgePath: string, useEmpty: boolean): string {40// TODO: better NativeBridge loader41return `42${importTsInteropTypes}43import { NativeModuleEmpty } from "./NativeModuleEmpty"
44
45let theModule: NativeModule | undefined = undefined
46
47export function nativeModule(): NativeModule {
48if (theModule) return theModule
49if (${useEmpty})50theModule = new NativeModuleEmpty()
51else
52theModule = require("${nativeBridgePath}") as NativeModule53return theModule
54}
55
56class NativeString extends NativeStringBase {
57constructor(ptr: KPointer) {
58super(ptr)
59}
60protected bytesLength(): int32 {
61return nativeModule()._StringLength(this.ptr)
62}
63protected getData(data: Uint8Array): void {
64withByteArray(data, Access.WRITE, (dataPtr: KUint8ArrayPtr) => {
65nativeModule()._StringData(this.ptr, dataPtr, data.length)
66})
67}
68close(): void {
69nativeModule()._InvokeFinalizer(this.ptr, nativeModule()._GetStringFinalizer())
70this.ptr = nullptr
71}
72}
73
74providePlatformDefinedData({
75nativeString(ptr: KPointer): NativeStringBase { return new NativeString(ptr) }
76})
77
78export interface NativeModule {
79_GetGroupedLog(index: KInt): KPointer;
80_ClearGroupedLog(index: KInt): void;
81_GetStringFinalizer(): KPointer;
82_InvokeFinalizer(ptr: KPointer, finalizer: KPointer): void;
83_StringLength(ptr: KPointer): KInt;
84_StringData(ptr: KPointer, buffer: KUint8ArrayPtr, length: KInt): void;
85_StringMake(value: KStringPtr): KPointer;
86
87${methods.map(it => ` ${it}`).join("\n")}88}
89`
90}
91
92export function nativeModuleEmptyDeclaration(methods: string[]): string {93return `94${importTsInteropTypes}95import { NativeModuleBase } from "./NativeModuleBase"
96import { NativeModule } from "./NativeModule"
97
98export class NativeModuleEmpty extends NativeModuleBase implements NativeModule {
99${methods.join("\n")}100}
101`.trim()102}
103
104export function bridgeCcDeclaration(bridgeCc: string[]): string {105return `#include "Interop.h"106#include "Deserializer.h"
107#include "arkoala_api.h"
108
109static ArkUIAnyAPI* impls[ArkUIAPIVariantKind::COUNT] = { 0 };
110
111const ArkUIAnyAPI* GetAnyImpl(ArkUIAPIVariantKind kind, int version, std::string* result) {
112return impls[kind];
113}
114
115const ArkUIFullNodeAPI* GetFullImpl(std::string* result = nullptr) {
116return reinterpret_cast<const ArkUIFullNodeAPI*>(GetAnyImpl(ArkUIAPIVariantKind::FULL, ARKUI_FULL_API_VERSION, result));
117}
118
119const ArkUINodeModifiers* GetNodeModifiers() {
120// TODO: restore the proper call
121// return GetFullImpl()->getNodeModifiers();
122extern const ArkUINodeModifiers* GetArkUINodeModifiers();
123return GetArkUINodeModifiers();
124}
125
126${bridgeCc.join("\n")}127`
128}
129
130export function dummyImplementations(lines: string[]): string {131return `132#include "Interop.h"
133#include "Deserializer.h"
134#include "arkoala_api.h"
135#include "common-interop.h"
136
137${lines.join("\n")}138`
139}
140
141export function dummyModifiers(lines: string[]): string {142return lines.join("\n")143}
144
145export function dummyModifierList(lines: string[]): string {146return `147const ArkUINodeModifiers impl = {
1481, // version
149${lines.join("\n")}150};
151
152extern const ArkUINodeModifiers* GetArkUINodeModifiers()
153{
154return &impl;
155}
156
157`
158}
159
160
161export function makeTSSerializer(lines: string[]): string {162return `163import { SerializerBase, runtimeType, Tags, RuntimeType, Function } from "./SerializerBase"
164import { int32 } from "./types"
165
166export class Serializer extends SerializerBase {
167${lines.join("\n")}168}
169`
170}
171
172export function makeCDeserializer(structsForward: string[], structs: string[], serializers: string[]): string {173return `174#include "Interop.h"
175#include "ArgDeserializerBase.h"
176#include <string>
177
178${structsForward.join("\n")}179
180${structs.join("\n")}181
182class Deserializer : public ArgDeserializerBase
183{
184public:
185Deserializer(uint8_t *data, int32_t length)
186: ArgDeserializerBase(data, length) {}
187
188${serializers.join("\n ")}189};
190`
191}
192
193export function makeApiModifiers(lines: string[]): string {194return `195/**
196* An API to control an implementation. When making changes modifying binary
197* layout, i.e. adding new events - increase ARKUI_API_VERSION above for binary
198* layout checks.
199*/
200struct ArkUINodeModifiers {
201KInt version;
202${lines.join("\n")}203};
204
205struct ArkUIBasicAPI {
206KInt version;
207};
208
209struct ArkUIAnimation {
210KInt version;
211};
212
213struct ArkUINavigation {
214KInt version;
215};
216
217struct ArkUIGraphicsAPI {
218KInt version;
219};
220
221/**
222* An API to control an implementation. When making changes modifying binary
223* layout, i.e. adding new events - increase ARKUI_NODE_API_VERSION above for binary
224* layout checks.
225*/
226struct ArkUIFullNodeAPI {
227KInt version;
228const ArkUIBasicAPI* (*getBasicAPI)();
229const ArkUINodeModifiers* (*getNodeModifiers)();
230const ArkUIAnimation* (*getAnimation)();
231const ArkUINavigation* (*getNavigation)();
232const ArkUIGraphicsAPI* (*getGraphicsAPI)();
233};
234
235struct ArkUIAnyAPI {
236KInt version;
237};
238`
239}
240
241export function makeApiHeaders(lines: string[]): string {242return `243enum ArkUIAPIVariantKind {
244BASIC = 1,
245FULL = 2,
246GRAPHICS = 3,
247EXTENDED = 4,
248COUNT = EXTENDED + 1,
249};
250
251${lines.join("\n")}252`
253}
254
255export function copyPeerLib(from: string, to: string) {256const tsBase = path.join(from, 'ts')257fs.readdirSync(tsBase).forEach(it => {258fs.copyFileSync(path.join(tsBase, it), path.join(to, it))259})260const cppBase = path.join(from, 'cpp')261fs.readdirSync(cppBase).forEach(it => {262let input = path.join(cppBase, it)263if (fs.lstatSync(input).isFile())264fs.copyFileSync(input, path.join(to, it))265})266const cppBaseNode = path.join(from, 'cpp', 'node')267fs.readdirSync(cppBaseNode).forEach(it => {268fs.copyFileSync(path.join(cppBaseNode, it), path.join(to, it))269})270}