idlize

Форк
0
/
CustomAPI.ts 
199 строк · 10.4 Кб
1

2
import {Type, Method, NamedMethodSignature} from "./LanguageWriters"
3
import { PeerGeneratorConfig } from "./PeerGeneratorConfig";
4
import { capitalize } from "../util";
5

6

7
const K_VMCONTEXT_TYPE = new Type("KVMContext");
8
const ARK_VMCONTEXT_TYPE = new Type(`Ark_VMContext`);
9

10
const K_NATIVE_POINTER_TYPE = new Type("KNativePointerArray");
11
const ARK_FLOAT32_ARRAY_PTR_TYPE = new Type(`Ark_Float32*`);
12

13
const K_CHAR_PTR_TYPE = new Type("KCharPtr");
14
const ARK_CHAR_PTR_TYPE = new Type(`Ark_CharPtr`);
15

16
const K_FLOAT_ARRAY_TYPE = new Type("KFloatArray");
17
const ARK_FLOAT_ARRAY_TYPE = new Type("Ark_Float32*");
18

19
const NODE_TYPE_ENUM = new Type("NodeTypeEnum");
20
const ARK_NODE_TYPE = new Type(`${PeerGeneratorConfig.cppPrefix}Ark_NodeType`);
21

22
const K_LONG_TYPE = new Type("KLong")
23
const K_UINT_TYPE = new Type("KUInt")
24
const K_FLOAT_TYPE = new Type("KFloat")
25
const K_DOUBLE_TYPE = new Type("KDouble")
26

27
const EVENT_SUB_KIND_ENUM = new Type("EventSubKindeEnum");
28
const ARK_EVENT_SUB_KIND_TYPE = new Type(`${PeerGeneratorConfig.cppPrefix}Ark_EventSubKind`);
29

30
const ARK_NATIVE_POINTER_TYPE = new Type("Ark_NativePointer")
31
const RECEIVER_TYPE = new Type(`${PeerGeneratorConfig.cppPrefix}EventReceiver`)
32

33
const ARK_NODE_EVENT_TYPE = new Type(`${PeerGeneratorConfig.cppPrefix}Ark_NodeEvent`)
34

35
const PIPELINE_CONTEXT_TYPE = new Type(`PipelineContext`)
36
const ARK_PIPELINE_CONTEXT_TYPE = new Type(`Ark_PipelineContext`)
37

38
export class CustomAPI {
39

40
    typeMap = new Map<Type, [nativeType: Type, castType: Type, jniType: Type]>()
41

42
    constructor(public apiName: string, public methods: Method[], public withContext: boolean = false) {
43
        this.typeMap.set(Type.Void, [Type.Void, Type.Void, Type.Void])
44
        this.typeMap.set(Type.Int32, [new Type("Ark_Int32"), new Type("Ark_Int32"), new Type("int")])
45
        this.typeMap.set(Type.Pointer, [ARK_NATIVE_POINTER_TYPE, new Type("Ark_NodeHandle"), new Type("long")])
46
        this.typeMap.set(Type.Boolean, [new Type("KBoolean"), new Type("Ark_Boolean"), new Type("boolean")])
47
        this.typeMap.set(K_UINT_TYPE, [K_UINT_TYPE, new Type("Ark_UInt32"), new Type("int")])
48
        this.typeMap.set(K_LONG_TYPE, [new Type("Ark_Int64"), new Type("Ark_Int64"), new Type("long")])
49
        this.typeMap.set(K_FLOAT_TYPE, [new Type("Ark_Float32"), new Type("Ark_Float32"), new Type("float")])
50
        this.typeMap.set(K_DOUBLE_TYPE, [K_DOUBLE_TYPE, new Type("Ark_Float64"), new Type("double")])
51
        this.typeMap.set(K_VMCONTEXT_TYPE, [K_VMCONTEXT_TYPE, ARK_VMCONTEXT_TYPE, new Type("long")])
52
        this.typeMap.set(K_NATIVE_POINTER_TYPE, [K_NATIVE_POINTER_TYPE, ARK_FLOAT32_ARRAY_PTR_TYPE, new Type("long")])
53
        this.typeMap.set(K_CHAR_PTR_TYPE, [K_CHAR_PTR_TYPE, ARK_CHAR_PTR_TYPE, new Type("long")])
54
        this.typeMap.set(K_FLOAT_ARRAY_TYPE, [K_FLOAT_ARRAY_TYPE, ARK_FLOAT_ARRAY_TYPE, new Type("float[]")])
55
        this.typeMap.set(NODE_TYPE_ENUM, [new Type("Ark_Int32"), ARK_NODE_TYPE, new Type("int")])
56
        this.typeMap.set(EVENT_SUB_KIND_ENUM, [new Type("Ark_Int32"), ARK_EVENT_SUB_KIND_TYPE, new Type("int")])
57
        this.typeMap.set(ARK_NODE_EVENT_TYPE, [ARK_NATIVE_POINTER_TYPE, ARK_NODE_EVENT_TYPE, new Type("long")])
58
        this.typeMap.set(RECEIVER_TYPE, [ARK_NATIVE_POINTER_TYPE, RECEIVER_TYPE, new Type("long")])
59
        this.typeMap.set(PIPELINE_CONTEXT_TYPE, [ARK_NATIVE_POINTER_TYPE, ARK_PIPELINE_CONTEXT_TYPE, new Type("long")])
60
    }
61

62
    getArgType(type: Type) {
63
        return this.typeMap.get(type)![0]
64
    }
65

66
    getCastType(type: Type) {
67
        return this.typeMap.get(type)![1]
68
    }
69

70
    getJniType(type: Type) {
71
        return this.typeMap.get(type)![2]
72
    }
73
}
74

75
function method(name: string, returnType: Type, args: Type[], argsNames: string[]) {
76
    return new Method(name, new NamedMethodSignature(returnType, args, argsNames))
77
}
78

79
export const CUSTOM_API: CustomAPI[] = [
80

81
    // BasicNodeAPI
82
    new CustomAPI(
83
        "BasicNodeAPI", [
84
        method(`createNode`, Type.Pointer, [NODE_TYPE_ENUM, Type.Int32, Type.Int32], ["type", "id", "flags"]),
85
        method(`getNodeByViewStack`, Type.Pointer, [], []),
86
        method(`disposeNode`, Type.Void, [Type.Pointer], ["nodePtr"]),
87
        // TBD: Returns string
88
        // method(`getName`, K_CHAR_PTR_TYPE, [Type.Pointer], ["nodePtr"]),
89
        // method(`dump`, Type.Void, [Type.Pointer], ["nodePtr"]),
90

91
        method(`removeChild`, Type.Void, [Type.Pointer, Type.Pointer], ["parent", "child"]),
92
        method(`insertChildAfter`, Type.Int32, [Type.Pointer, Type.Pointer, Type.Pointer], ["parent", "child", "sibling"]),
93
        method(`addChild`, Type.Int32, [Type.Pointer, Type.Pointer], ["parent", "child"]),
94
        method(`insertChildBefore`, Type.Int32, [Type.Pointer, Type.Pointer, Type.Pointer], ["parent", "child", "sibling"]),
95
        method(`insertChildAt`, Type.Int32, [Type.Pointer, Type.Pointer, Type.Int32], ["parent", "child", "position"]),
96

97
        // May be obsolete api, returns string
98
        // method(`getAttribute`, K_CHAR_PTR_TYPE, [Type.Pointer, K_CHAR_PTR_TYPE], ["nodePtr", "attribute"]),
99
        // method(`setAttribute`, Type.Void, [Type.Pointer, K_CHAR_PTR_TYPE, K_CHAR_PTR_TYPE], ["nodePtr", "attribute", "value"]),
100
        // method(`resetAttribute`, Type.Void, [Type.Pointer, K_CHAR_PTR_TYPE], ["nodePtr", "attribute"]),
101

102
        // TBD: Fix Java Int64
103
        // method(`registerNodeAsyncEvent`, Type.Void, [Type.Pointer, EVENT_SUB_KIND_ENUM, K_LONG_TYPE], ["nodePtr", "kind", "extraParam"]),
104
        // method(`unRegisterNodeAsyncEvent`, Type.Void, [Type.Pointer, EVENT_SUB_KIND_ENUM], ["nodePtr", "kind"]),
105
        // method(`registerNodeAsyncEventReceiver`, Type.Void, [RECEIVER_TYPE], ["eventReceiver"]),
106
        // method(`unRegisterNodeAsyncEventReceiver`, Type.Void, [], []),
107
        // TBD: convert NodeEvent struct
108
        // method(`checkAsyncEvent`, Type.Int32, [ARK_NODE_EVENT_TYPE], ["event"]),
109

110
        method(`applyModifierFinish`, Type.Void, [Type.Pointer], ["nodePtr"]),
111
        method(`markDirty`, Type.Void, [Type.Pointer, K_UINT_TYPE], ["nodePtr", "dirtyFlag"]),
112
        method(`isBuilderNode`, Type.Boolean, [Type.Pointer], ["nodePtr"]),
113
        method(`convertLengthMetricsUnit`, K_FLOAT_TYPE, [K_FLOAT_TYPE, Type.Int32, Type.Int32], ["value", "originUnit", "targetUnit"]),
114
    ]),
115

116
    // ExtendedNodeAPI with context
117
    new CustomAPI(
118
        "ExtendedNodeAPI", [
119
        // measureLayoutAndDraw returns void or Int32?
120
        method(`measureLayoutAndDraw`, Type.Void, [K_VMCONTEXT_TYPE, Type.Pointer], ["vmContext", "nodePtr"]),
121

122
        method(`measureNode`, Type.Int32, [K_VMCONTEXT_TYPE, Type.Pointer, K_FLOAT_ARRAY_TYPE], ["vmContext", "nodePtr", "data"]),
123
        method(`layoutNode`, Type.Int32, [K_VMCONTEXT_TYPE, Type.Pointer, K_FLOAT_ARRAY_TYPE], ["vmContext", "nodePtr", "data"]),
124
        method(`drawNode`, Type.Int32, [K_VMCONTEXT_TYPE, Type.Pointer, K_FLOAT_ARRAY_TYPE], ["vmContext", "nodePtr", "data"]),
125

126
        method(`indexerChecker`, Type.Int32, [K_VMCONTEXT_TYPE, Type.Pointer], ["vmContext", "nodePtr"]),
127
        method(`setLazyItemIndexer`, Type.Void, [K_VMCONTEXT_TYPE, Type.Pointer, Type.Int32], ["vmContext", "nodePtr", "indexerId"]),
128

129
        method(`setVsyncCallback`, Type.Void, [K_VMCONTEXT_TYPE, PIPELINE_CONTEXT_TYPE, Type.Int32], ["vmContext", "pipelineContext", "callbackId"]),
130
        method(`unblockVsyncWait`, Type.Void, [K_VMCONTEXT_TYPE, PIPELINE_CONTEXT_TYPE], ["vmContext", "pipelineContext"]),
131
        method(`setCustomCallback`, Type.Void, [K_VMCONTEXT_TYPE, Type.Pointer, Type.Int32], ["vmContext", "nodePtr", "updaterId"]),
132
    ], true),
133

134
    // ExtendedNodeAPI
135
    new CustomAPI(
136
        "ExtendedNodeAPI", [
137
        method(`setMeasureWidth`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "value"]),
138
        method(`getMeasureWidth`, Type.Int32, [Type.Pointer], ["nodePtr"]),
139
        method(`setMeasureHeight`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "value"]),
140
        method(`getMeasureHeight`, Type.Int32, [Type.Pointer], ["nodePtr"]),
141
        method(`setX`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "value"]),
142
        method(`getX`, Type.Int32, [Type.Pointer], ["nodePtr"]),
143
        method(`setY`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "value"]),
144
        method(`getY`, Type.Int32, [Type.Pointer], ["nodePtr"]),
145
        method(`setAlignment`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "value"]),
146
        method(`getAlignment`, Type.Int32, [Type.Pointer], ["nodePtr"]),
147

148
        method(`setRangeUpdater`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "updaterId"]),
149

150
        method(`getPipelineContext`, Type.Pointer, [Type.Pointer], ["nodePtr"]),
151

152
        method(`setChildTotalCount`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "totalCount"]),
153

154
        // custom realization is added to the bridge_epilogue
155
        //method(`showCrash`, Type.Void, [K_STRING_PTR_TYPE], ["messagePtr"]),
156
    ]),
157
]
158

159
function printCustomApiMethodTS(c: CustomAPI, m: Method) {
160
    const sig = m.signature as NamedMethodSignature
161
    const ret = c.getArgType(sig.returnType).name
162
    let args = sig.args.map((type, index) => `${sig.argsNames[index]}: ${c.getArgType(type).name} `)
163
    args = c.withContext ? args.slice(1) : args
164
    const name = `_${capitalize(m.name)}`
165
    console.log(`  ${name}(${args.join(", ")}): ${ret}`)
166
}
167

168
function printCustomApiMethodJNI(c: CustomAPI, m: Method) {
169
    const sig = m.signature as NamedMethodSignature
170
    const ret = c.getJniType(sig.returnType).name
171
    let args = sig.args.map((type, index) => `${c.getJniType(type).name} ${sig.argsNames[index]}`)
172
    args = c.withContext ? args.slice(1) : args
173
    const name = `_${capitalize(m.name)}`
174
    console.log(`  static native ${ret} ${name}(${args.join(", ")});`)
175
}
176

177
function printCustomApiMethodETS(c: CustomAPI, m: Method) {
178
    const sig = m.signature as NamedMethodSignature
179
    const ret = c.getJniType(sig.returnType).name
180
    let args = sig.args.map((type, index) => `${sig.argsNames[index]}: ${c.getJniType(type).name} `)
181
    args = c.withContext ? args.slice(1) : args
182
    const name = `_${capitalize(m.name)}`
183
    console.log(`  static native ${name}(${args.join(", ")}): ${ret}`)
184
}
185

186
function printCustomAPIMethods(langName: string, printMethod: (c: CustomAPI, m: Method) => void ): void {
187
    console.log(`// ${langName}`)
188
    for (const customApi of CUSTOM_API) {
189
        console.log(`// ${customApi.apiName}`)
190
        for (const method of customApi.methods) {
191
            printMethod(customApi, method)
192
        }
193
    }
194
}
195

196
if (false) {
197
    printCustomAPIMethods("TypeScript", printCustomApiMethodTS)
198
    printCustomAPIMethods("JNI", printCustomApiMethodJNI)
199
    printCustomAPIMethods("ETS", printCustomApiMethodETS)
200
}
201

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

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

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

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