idlize

Форк
0
/
FileGenerators.ts 
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
 */
15
import * as fs from "fs"
16
import * as path from "path"
17

18
const importTsInteropTypes = `
19
import {
20
    int32,
21
    float32,
22
    KInt,
23
    KBoolean,
24
    KStringPtr,
25
    KPointer,
26
    KNativePointer,
27
    Int32ArrayPtr,
28
    KUint8ArrayPtr,
29
} from "./types"
30
import {
31
    NativeStringBase,
32
    withByteArray,
33
    Access,
34
    providePlatformDefinedData,
35
    nullptr
36
} from "./Interop"
37
`.trim()
38

39
export function nativeModuleDeclaration(methods: string[], nativeBridgePath: string, useEmpty: boolean): string {
40
    // TODO: better NativeBridge loader
41
    return `
42
${importTsInteropTypes}
43
import { NativeModuleEmpty } from "./NativeModuleEmpty"
44

45
let theModule: NativeModule | undefined = undefined
46

47
export function nativeModule(): NativeModule {
48
    if (theModule) return theModule
49
    if (${useEmpty})
50
        theModule = new NativeModuleEmpty()
51
    else
52
        theModule = require("${nativeBridgePath}") as NativeModule
53
    return theModule
54
}
55

56
class NativeString extends NativeStringBase {
57
    constructor(ptr: KPointer) {
58
        super(ptr)
59
    }
60
    protected bytesLength(): int32 {
61
        return nativeModule()._StringLength(this.ptr)
62
    }
63
    protected getData(data: Uint8Array): void {
64
        withByteArray(data, Access.WRITE, (dataPtr: KUint8ArrayPtr) => {
65
            nativeModule()._StringData(this.ptr, dataPtr, data.length)
66
        })
67
    }
68
    close(): void {
69
        nativeModule()._InvokeFinalizer(this.ptr, nativeModule()._GetStringFinalizer())
70
        this.ptr = nullptr
71
    }
72
}
73

74
providePlatformDefinedData({
75
    nativeString(ptr: KPointer): NativeStringBase { return new NativeString(ptr) }
76
})
77

78
export 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

92
export function nativeModuleEmptyDeclaration(methods: string[]): string {
93
    return `
94
${importTsInteropTypes}
95
import { NativeModuleBase } from "./NativeModuleBase"
96
import { NativeModule } from "./NativeModule"
97

98
export class NativeModuleEmpty extends NativeModuleBase implements NativeModule {
99
${methods.join("\n")}
100
}
101
`.trim()
102
}
103

104
export function bridgeCcDeclaration(bridgeCc: string[]): string {
105
    return `#include "Interop.h"
106
#include "Deserializer.h"
107
#include "arkoala_api.h"
108

109
static ArkUIAnyAPI* impls[ArkUIAPIVariantKind::COUNT] = { 0 };
110

111
const ArkUIAnyAPI* GetAnyImpl(ArkUIAPIVariantKind kind, int version, std::string* result) {
112
    return impls[kind];
113
}
114

115
const ArkUIFullNodeAPI* GetFullImpl(std::string* result = nullptr) {
116
    return reinterpret_cast<const ArkUIFullNodeAPI*>(GetAnyImpl(ArkUIAPIVariantKind::FULL, ARKUI_FULL_API_VERSION, result));
117
}
118

119
const ArkUINodeModifiers* GetNodeModifiers() {
120
    // TODO: restore the proper call
121
    // return GetFullImpl()->getNodeModifiers();
122
    extern const ArkUINodeModifiers* GetArkUINodeModifiers();
123
    return GetArkUINodeModifiers();
124
}
125

126
${bridgeCc.join("\n")}
127
`
128
}
129

130
export function dummyImplementations(lines: string[]): string {
131
    return `
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

141
export function dummyModifiers(lines: string[]): string {
142
    return lines.join("\n")
143
}
144

145
export function dummyModifierList(lines: string[]): string {
146
    return `
147
const ArkUINodeModifiers impl = {
148
    1, // version
149
${lines.join("\n")}
150
};
151

152
extern const ArkUINodeModifiers* GetArkUINodeModifiers()
153
{
154
    return &impl;
155
}
156

157
`
158
}
159

160

161
export function makeTSSerializer(lines: string[]): string {
162
    return `
163
import { SerializerBase, runtimeType, Tags, RuntimeType, Function } from "./SerializerBase"
164
import { int32 } from "./types"
165

166
export class Serializer extends SerializerBase {
167
${lines.join("\n")}
168
}
169
`
170
}
171

172
export function makeCDeserializer(structsForward: string[], structs: string[], serializers: string[]): string {
173
    return `
174
#include "Interop.h"
175
#include "ArgDeserializerBase.h"
176
#include <string>
177

178
${structsForward.join("\n")}
179

180
${structs.join("\n")}
181

182
class Deserializer : public ArgDeserializerBase
183
{
184
  public:
185
    Deserializer(uint8_t *data, int32_t length)
186
          : ArgDeserializerBase(data, length) {}
187

188
${serializers.join("\n  ")}
189
};
190
`
191
}
192

193
export function makeApiModifiers(lines: string[]): string {
194
    return `
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
 */
200
struct ArkUINodeModifiers {
201
    KInt version;
202
${lines.join("\n")}
203
};
204

205
struct ArkUIBasicAPI {
206
    KInt version;
207
};
208

209
struct ArkUIAnimation {
210
    KInt version;
211
};
212

213
struct ArkUINavigation {
214
    KInt version;
215
};
216

217
struct ArkUIGraphicsAPI {
218
    KInt 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
 */
226
struct ArkUIFullNodeAPI {
227
    KInt version;
228
    const ArkUIBasicAPI* (*getBasicAPI)();
229
    const ArkUINodeModifiers* (*getNodeModifiers)();
230
    const ArkUIAnimation* (*getAnimation)();
231
    const ArkUINavigation* (*getNavigation)();
232
    const ArkUIGraphicsAPI* (*getGraphicsAPI)();
233
};
234

235
struct ArkUIAnyAPI {
236
    KInt version;
237
};
238
`
239
}
240

241
export function makeApiHeaders(lines: string[]): string {
242
    return `
243
enum ArkUIAPIVariantKind {
244
    BASIC = 1,
245
    FULL = 2,
246
    GRAPHICS = 3,
247
    EXTENDED = 4,
248
    COUNT = EXTENDED + 1,
249
};
250

251
${lines.join("\n")}
252
`
253
}
254

255
export function copyPeerLib(from: string, to: string) {
256
    const tsBase = path.join(from, 'ts')
257
    fs.readdirSync(tsBase).forEach(it => {
258
        fs.copyFileSync(path.join(tsBase, it), path.join(to, it))
259
    })
260
    const cppBase = path.join(from, 'cpp')
261
    fs.readdirSync(cppBase).forEach(it => {
262
        let input = path.join(cppBase, it)
263
        if (fs.lstatSync(input).isFile())
264
            fs.copyFileSync(input, path.join(to, it))
265
    })
266
    const cppBaseNode = path.join(from, 'cpp', 'node')
267
    fs.readdirSync(cppBaseNode).forEach(it => {
268
        fs.copyFileSync(path.join(cppBaseNode, it), path.join(to, it))
269
    })
270
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.