idlize
199 строк · 10.4 Кб
1
2import {Type, Method, NamedMethodSignature} from "./LanguageWriters"
3import { PeerGeneratorConfig } from "./PeerGeneratorConfig";
4import { capitalize } from "../util";
5
6
7const K_VMCONTEXT_TYPE = new Type("KVMContext");
8const ARK_VMCONTEXT_TYPE = new Type(`Ark_VMContext`);
9
10const K_NATIVE_POINTER_TYPE = new Type("KNativePointerArray");
11const ARK_FLOAT32_ARRAY_PTR_TYPE = new Type(`Ark_Float32*`);
12
13const K_CHAR_PTR_TYPE = new Type("KCharPtr");
14const ARK_CHAR_PTR_TYPE = new Type(`Ark_CharPtr`);
15
16const K_FLOAT_ARRAY_TYPE = new Type("KFloatArray");
17const ARK_FLOAT_ARRAY_TYPE = new Type("Ark_Float32*");
18
19const NODE_TYPE_ENUM = new Type("NodeTypeEnum");
20const ARK_NODE_TYPE = new Type(`${PeerGeneratorConfig.cppPrefix}Ark_NodeType`);
21
22const K_LONG_TYPE = new Type("KLong")
23const K_UINT_TYPE = new Type("KUInt")
24const K_FLOAT_TYPE = new Type("KFloat")
25const K_DOUBLE_TYPE = new Type("KDouble")
26
27const EVENT_SUB_KIND_ENUM = new Type("EventSubKindeEnum");
28const ARK_EVENT_SUB_KIND_TYPE = new Type(`${PeerGeneratorConfig.cppPrefix}Ark_EventSubKind`);
29
30const ARK_NATIVE_POINTER_TYPE = new Type("Ark_NativePointer")
31const RECEIVER_TYPE = new Type(`${PeerGeneratorConfig.cppPrefix}EventReceiver`)
32
33const ARK_NODE_EVENT_TYPE = new Type(`${PeerGeneratorConfig.cppPrefix}Ark_NodeEvent`)
34
35const PIPELINE_CONTEXT_TYPE = new Type(`PipelineContext`)
36const ARK_PIPELINE_CONTEXT_TYPE = new Type(`Ark_PipelineContext`)
37
38export class CustomAPI {
39
40typeMap = new Map<Type, [nativeType: Type, castType: Type, jniType: Type]>()
41
42constructor(public apiName: string, public methods: Method[], public withContext: boolean = false) {
43this.typeMap.set(Type.Void, [Type.Void, Type.Void, Type.Void])
44this.typeMap.set(Type.Int32, [new Type("Ark_Int32"), new Type("Ark_Int32"), new Type("int")])
45this.typeMap.set(Type.Pointer, [ARK_NATIVE_POINTER_TYPE, new Type("Ark_NodeHandle"), new Type("long")])
46this.typeMap.set(Type.Boolean, [new Type("KBoolean"), new Type("Ark_Boolean"), new Type("boolean")])
47this.typeMap.set(K_UINT_TYPE, [K_UINT_TYPE, new Type("Ark_UInt32"), new Type("int")])
48this.typeMap.set(K_LONG_TYPE, [new Type("Ark_Int64"), new Type("Ark_Int64"), new Type("long")])
49this.typeMap.set(K_FLOAT_TYPE, [new Type("Ark_Float32"), new Type("Ark_Float32"), new Type("float")])
50this.typeMap.set(K_DOUBLE_TYPE, [K_DOUBLE_TYPE, new Type("Ark_Float64"), new Type("double")])
51this.typeMap.set(K_VMCONTEXT_TYPE, [K_VMCONTEXT_TYPE, ARK_VMCONTEXT_TYPE, new Type("long")])
52this.typeMap.set(K_NATIVE_POINTER_TYPE, [K_NATIVE_POINTER_TYPE, ARK_FLOAT32_ARRAY_PTR_TYPE, new Type("long")])
53this.typeMap.set(K_CHAR_PTR_TYPE, [K_CHAR_PTR_TYPE, ARK_CHAR_PTR_TYPE, new Type("long")])
54this.typeMap.set(K_FLOAT_ARRAY_TYPE, [K_FLOAT_ARRAY_TYPE, ARK_FLOAT_ARRAY_TYPE, new Type("float[]")])
55this.typeMap.set(NODE_TYPE_ENUM, [new Type("Ark_Int32"), ARK_NODE_TYPE, new Type("int")])
56this.typeMap.set(EVENT_SUB_KIND_ENUM, [new Type("Ark_Int32"), ARK_EVENT_SUB_KIND_TYPE, new Type("int")])
57this.typeMap.set(ARK_NODE_EVENT_TYPE, [ARK_NATIVE_POINTER_TYPE, ARK_NODE_EVENT_TYPE, new Type("long")])
58this.typeMap.set(RECEIVER_TYPE, [ARK_NATIVE_POINTER_TYPE, RECEIVER_TYPE, new Type("long")])
59this.typeMap.set(PIPELINE_CONTEXT_TYPE, [ARK_NATIVE_POINTER_TYPE, ARK_PIPELINE_CONTEXT_TYPE, new Type("long")])
60}
61
62getArgType(type: Type) {
63return this.typeMap.get(type)![0]
64}
65
66getCastType(type: Type) {
67return this.typeMap.get(type)![1]
68}
69
70getJniType(type: Type) {
71return this.typeMap.get(type)![2]
72}
73}
74
75function method(name: string, returnType: Type, args: Type[], argsNames: string[]) {
76return new Method(name, new NamedMethodSignature(returnType, args, argsNames))
77}
78
79export const CUSTOM_API: CustomAPI[] = [
80
81// BasicNodeAPI
82new CustomAPI(
83"BasicNodeAPI", [
84method(`createNode`, Type.Pointer, [NODE_TYPE_ENUM, Type.Int32, Type.Int32], ["type", "id", "flags"]),
85method(`getNodeByViewStack`, Type.Pointer, [], []),
86method(`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
91method(`removeChild`, Type.Void, [Type.Pointer, Type.Pointer], ["parent", "child"]),
92method(`insertChildAfter`, Type.Int32, [Type.Pointer, Type.Pointer, Type.Pointer], ["parent", "child", "sibling"]),
93method(`addChild`, Type.Int32, [Type.Pointer, Type.Pointer], ["parent", "child"]),
94method(`insertChildBefore`, Type.Int32, [Type.Pointer, Type.Pointer, Type.Pointer], ["parent", "child", "sibling"]),
95method(`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
110method(`applyModifierFinish`, Type.Void, [Type.Pointer], ["nodePtr"]),
111method(`markDirty`, Type.Void, [Type.Pointer, K_UINT_TYPE], ["nodePtr", "dirtyFlag"]),
112method(`isBuilderNode`, Type.Boolean, [Type.Pointer], ["nodePtr"]),
113method(`convertLengthMetricsUnit`, K_FLOAT_TYPE, [K_FLOAT_TYPE, Type.Int32, Type.Int32], ["value", "originUnit", "targetUnit"]),
114]),
115
116// ExtendedNodeAPI with context
117new CustomAPI(
118"ExtendedNodeAPI", [
119// measureLayoutAndDraw returns void or Int32?
120method(`measureLayoutAndDraw`, Type.Void, [K_VMCONTEXT_TYPE, Type.Pointer], ["vmContext", "nodePtr"]),
121
122method(`measureNode`, Type.Int32, [K_VMCONTEXT_TYPE, Type.Pointer, K_FLOAT_ARRAY_TYPE], ["vmContext", "nodePtr", "data"]),
123method(`layoutNode`, Type.Int32, [K_VMCONTEXT_TYPE, Type.Pointer, K_FLOAT_ARRAY_TYPE], ["vmContext", "nodePtr", "data"]),
124method(`drawNode`, Type.Int32, [K_VMCONTEXT_TYPE, Type.Pointer, K_FLOAT_ARRAY_TYPE], ["vmContext", "nodePtr", "data"]),
125
126method(`indexerChecker`, Type.Int32, [K_VMCONTEXT_TYPE, Type.Pointer], ["vmContext", "nodePtr"]),
127method(`setLazyItemIndexer`, Type.Void, [K_VMCONTEXT_TYPE, Type.Pointer, Type.Int32], ["vmContext", "nodePtr", "indexerId"]),
128
129method(`setVsyncCallback`, Type.Void, [K_VMCONTEXT_TYPE, PIPELINE_CONTEXT_TYPE, Type.Int32], ["vmContext", "pipelineContext", "callbackId"]),
130method(`unblockVsyncWait`, Type.Void, [K_VMCONTEXT_TYPE, PIPELINE_CONTEXT_TYPE], ["vmContext", "pipelineContext"]),
131method(`setCustomCallback`, Type.Void, [K_VMCONTEXT_TYPE, Type.Pointer, Type.Int32], ["vmContext", "nodePtr", "updaterId"]),
132], true),
133
134// ExtendedNodeAPI
135new CustomAPI(
136"ExtendedNodeAPI", [
137method(`setMeasureWidth`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "value"]),
138method(`getMeasureWidth`, Type.Int32, [Type.Pointer], ["nodePtr"]),
139method(`setMeasureHeight`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "value"]),
140method(`getMeasureHeight`, Type.Int32, [Type.Pointer], ["nodePtr"]),
141method(`setX`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "value"]),
142method(`getX`, Type.Int32, [Type.Pointer], ["nodePtr"]),
143method(`setY`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "value"]),
144method(`getY`, Type.Int32, [Type.Pointer], ["nodePtr"]),
145method(`setAlignment`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "value"]),
146method(`getAlignment`, Type.Int32, [Type.Pointer], ["nodePtr"]),
147
148method(`setRangeUpdater`, Type.Void, [Type.Pointer, Type.Int32], ["nodePtr", "updaterId"]),
149
150method(`getPipelineContext`, Type.Pointer, [Type.Pointer], ["nodePtr"]),
151
152method(`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
159function printCustomApiMethodTS(c: CustomAPI, m: Method) {
160const sig = m.signature as NamedMethodSignature
161const ret = c.getArgType(sig.returnType).name
162let args = sig.args.map((type, index) => `${sig.argsNames[index]}: ${c.getArgType(type).name} `)
163args = c.withContext ? args.slice(1) : args
164const name = `_${capitalize(m.name)}`
165console.log(` ${name}(${args.join(", ")}): ${ret}`)
166}
167
168function printCustomApiMethodJNI(c: CustomAPI, m: Method) {
169const sig = m.signature as NamedMethodSignature
170const ret = c.getJniType(sig.returnType).name
171let args = sig.args.map((type, index) => `${c.getJniType(type).name} ${sig.argsNames[index]}`)
172args = c.withContext ? args.slice(1) : args
173const name = `_${capitalize(m.name)}`
174console.log(` static native ${ret} ${name}(${args.join(", ")});`)
175}
176
177function printCustomApiMethodETS(c: CustomAPI, m: Method) {
178const sig = m.signature as NamedMethodSignature
179const ret = c.getJniType(sig.returnType).name
180let args = sig.args.map((type, index) => `${sig.argsNames[index]}: ${c.getJniType(type).name} `)
181args = c.withContext ? args.slice(1) : args
182const name = `_${capitalize(m.name)}`
183console.log(` static native ${name}(${args.join(", ")}): ${ret}`)
184}
185
186function printCustomAPIMethods(langName: string, printMethod: (c: CustomAPI, m: Method) => void ): void {
187console.log(`// ${langName}`)
188for (const customApi of CUSTOM_API) {
189console.log(`// ${customApi.apiName}`)
190for (const method of customApi.methods) {
191printMethod(customApi, method)
192}
193}
194}
195
196if (false) {
197printCustomAPIMethods("TypeScript", printCustomApiMethodTS)
198printCustomAPIMethods("JNI", printCustomApiMethodJNI)
199printCustomAPIMethods("ETS", printCustomApiMethodETS)
200}
201