• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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*/
15const re = require('./re');
16const tsc = require('../../node_modules/typescript');
17
18function checkFileError(ifname) {
19    let program = tsc.createProgram([ifname], { target: tsc.ScriptTarget.Latest, });
20    let emitResult = program.emit();
21    let allDiagnostics = tsc.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
22
23    let errorMsg = '';
24    allDiagnostics.forEach(diagnostic => {
25        if (diagnostic.file) {
26            let { line, character } = tsc.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
27            let message = tsc.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
28            errorMsg += `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}\n`;
29        } else {
30            errorMsg += tsc.flattenDiagnosticMessageText(diagnostic.messageText, '\n') + '\n';
31        }
32    });
33
34    if (allDiagnostics.length > 0) {
35        return [false, errorMsg];
36    }
37    return [true, ''];
38}
39
40class FuncType { }
41FuncType.DIRECT = 1;
42FuncType.SYNC = 2;
43FuncType.ASYNC = 4;
44FuncType.PROMISE = 8;
45FuncType.ToString = function (type) {
46    if (type === FuncType.DIRECT) {
47        return 'DIRECT';
48    } else if (type === FuncType.SYNC) {
49        return 'SYNC';
50    } else if (type === FuncType.ASYNC) {
51        return 'ASYNC';
52    } else if (type === FuncType.PROMISE) {
53        return 'PROMISE';
54    }
55    return 'UNKNOW';
56};
57
58class NumberIncrease { }
59NumberIncrease.num = 1;
60NumberIncrease.getAndIncrease = function () {
61    return NumberIncrease.num++;
62};
63NumberIncrease.get = function () {
64    return NumberIncrease.num;
65};
66NumberIncrease.reset = function () {
67    NumberIncrease.num = 1;
68};
69
70class InterfaceList { }
71InterfaceList.interfacess_ = [];
72InterfaceList.push = function (ifs) {
73    InterfaceList.interfacess_.push(ifs);
74};
75InterfaceList.pop = function () {
76    InterfaceList.interfacess_.pop();
77};
78InterfaceList.getValue = function (name) {
79    let ifs = InterfaceList.interfacess_[InterfaceList.interfacess_.length - 1];
80    for (let i in ifs) {
81        let vv = ifs[i];
82        if (ifs[i].name === name) {
83            let hasProperty = Object.prototype.hasOwnProperty.call(ifs[i].body, 'allProperties');
84            if (hasProperty) {
85                return ifs[i].body.allProperties.values;
86            } else {
87                return ifs[i].body.value;
88            }
89        }
90    }
91    return null;
92};
93
94InterfaceList.getBody = function (name) {
95    let ifs = InterfaceList.interfacess_[InterfaceList.interfacess_.length - 1];
96    for (let i in ifs) {
97        if (ifs[i].name === name) {
98            return ifs[i].body;
99        }
100    }
101    return null;
102};
103
104class CallFunctionList { }
105CallFunctionList.callFuncs = [];
106CallFunctionList.push = function (ifs) {
107    CallFunctionList.callFuncs.push(ifs);
108};
109CallFunctionList.pop = function () {
110    CallFunctionList.callFuncs.pop();
111};
112CallFunctionList.getValue = function (name) {
113    if (CallFunctionList.callFuncs.length === 0) {
114        return null;
115    }
116
117    let cfs = CallFunctionList.callFuncs[CallFunctionList.callFuncs.length - 1];
118    if (cfs === undefined) {
119        return null;
120    }
121
122    for (let i = 0; i < cfs.length; i++) {
123        if (cfs[i].name === name) {
124            return [cfs[i].body, cfs[i].ret];
125        }
126    }
127    return null;
128};
129
130CallFunctionList.getObjOnFuncName = function (interfaceName) {
131    let cfs = CallFunctionList.callFuncs[CallFunctionList.callFuncs.length - 1];
132    let funNames = [];
133    for (let i = 0; i < cfs.length; i++) {
134        if (cfs[i].name.indexOf(interfaceName) === 0) {
135            let funName = cfs[i].name.substring(interfaceName.length + 1, cfs[i].name.length);
136            funNames.push(funName);
137        }
138    }
139    return funNames;
140};
141
142class TypeList { }
143TypeList.types = [];
144TypeList.push = function (ifs) {
145    TypeList.types.push(ifs);
146};
147TypeList.pop = function () {
148    TypeList.types.pop();
149};
150TypeList.getValue = function (name) {
151    let ifs = TypeList.types[TypeList.types.length - 1];
152    for (let i in ifs) {
153        if (ifs[i].name === name) {
154            let hasProperty = Object.prototype.hasOwnProperty.call(ifs[i].body, 'allProperties');
155            if (hasProperty) {
156                return ifs[i].body.allProperties.values;
157            } else {
158                return ifs[i].body;
159            }
160        }
161    }
162    return null;
163};
164
165class EnumList { }
166EnumList.enum_ = [];
167EnumList.push = function (ifs) {
168    EnumList.enum_.push(ifs);
169};
170EnumList.pop = function () {
171    EnumList.enum_.pop();
172};
173EnumList.getValue = function (name) {
174    let ifs = EnumList.enum_[EnumList.enum_.length - 1];
175    for (let i in ifs) {
176        if (ifs[i].name === name) {
177            return ifs[i].body.element;
178        }
179    }
180    return null;
181};
182
183function getArrayType(type) {
184    let tt = re.match('Array<([a-zA-Z_0-9]+)>', type);
185    if (tt !== null && tt !== undefined) {
186        return re.getReg(type, tt.regs[1]);
187    }
188
189    tt = re.match('Array<{([[a-z:]+)([a-z:]]+)([a-zA-Z_1-9:]+)', type);
190    if (tt !== null && tt !== undefined) {
191        let res = '';
192        let len = tt.regs.length;
193        for (let i = 1; i < len; i++) {
194            let regs1 = re.getReg(type, tt.regs[i]);
195            res += regs1;
196        }
197        return res;
198    }
199
200    tt = re.match('Array<map<string', type);
201    if (tt !== null && tt !== undefined) {
202        let preStr = 'Array<';
203        let preStrLen = preStr.length;
204        let res = type.substring(preStrLen, type.length - 1);
205        return res;
206    }
207
208    tt = re.match('Array<Map<string', type);
209    if (tt !== null && tt !== undefined) {
210        let preStr = 'Array<';
211        let preStrLen = preStr.length;
212        let res = type.substring(preStrLen, type.length - 1);
213        return res;
214    }
215    return null;
216}
217
218function getArrayTypeTwo(type) {
219    let tt = re.match('([a-zA-Z_0-9]+)', type);
220    return re.getReg(type, tt.regs[1]);
221}
222
223function jsType2CType(jsTypeName) {
224    if (jsTypeName === 'string') {
225        return 'std::string';
226    } else if (jsTypeName === 'boolean') {
227        return 'bool';
228    } else {
229        return jsTypeName;
230    }
231}
232
233class EnumValueType { }
234EnumValueType.ENUM_VALUE_TYPE_NUMBER = 0;
235EnumValueType.ENUM_VALUE_TYPE_STRING = 1;
236
237function isEnum(type, data) {
238    let isEnum = false;
239    if (null === data || undefined === data) {
240        return isEnum;
241    }
242    for (let i in data.enum) {
243        let enumm = data.enum[i];
244        if (type === enumm.name) {
245            isEnum = true;
246        }
247    }
248    return isEnum;
249}
250
251function enumIndex(type, data) {
252    let index;
253    if (null === data) {
254        return index;
255    }
256    for (let i in data.enum) {
257        let enumm = data.enum[i];
258        if (type === enumm.name) {
259            index = i;
260        }
261    }
262    return index;
263}
264
265function isType(type, data) {
266    let isType = false;
267    if (null === data) {
268        return isType;
269    }
270    for (let i in data.type) {
271        let typee = data.type[i];
272        if (type === typee.name) {
273            isType = true;
274        }
275    }
276    return isType;
277}
278
279function typeIndex(type, data) {
280    let index;
281    if (null === data) {
282        return index;
283    }
284    for (let i in data.type) {
285        let typee = data.type[i];
286        if (type === typee.name) {
287            index = i;
288        }
289    }
290    return index;
291}
292
293function getMapType(type) {
294    type = type.replace(/\s*/g, '');
295    let ttKey = re.search('Map<([a-zA-Z_0-9]+),', type);
296    let ttValue = re.search(',([a-zA-Z_0-9<>]+)>', type);
297    let ttMap = re.search(',([a-zA-Z_0-9]+)>>', type);
298    let ttArray = re.search('Array<([a-zA-Z_0-9]+)>', type);
299
300    if (ttArray === null) {
301        ttArray = re.search('([a-zA-Z_0-9]+)\\[\\]>', type);
302    }
303
304    let valueType;
305    let valueMapType;
306    let valueArrayType;
307    if (ttKey === null && ttValue === null && ttMap === null) {
308        ttKey = re.search('key:([a-zA-Z_0-9]+)', type);
309        ttValue = re.search(':([a-zA-Z_0-9]+)}', type);
310        ttMap = re.search(':([a-zA-Z_0-9]+)}}', type);
311        ttArray = re.search('Array<([a-zA-Z_0-9]+)>', type);
312        if (ttArray === null) {
313            ttArray = re.search(':([a-zA-Z_0-9]+)\\[\\]}', type);
314        }
315    }
316
317    if (ttValue !== null && ttValue !== undefined) {
318        valueType = re.getReg(type, ttValue.regs[1]);
319        if (valueType.indexOf('Array<') === 0) {
320            valueArrayType = re.getReg(valueType, ttArray.regs[1]);
321            valueType = undefined;
322        } else if (ttMap !== undefined && ttMap !== null) {
323            valueMapType = re.getReg(type, ttMap.regs[1]);
324            valueType = undefined;
325        }
326    }
327    if (ttMap !== null && ttMap !== undefined) {
328        valueMapType = re.getReg(type, ttMap.regs[1]);
329    }
330    if (ttArray !== null && ttArray !== undefined) {
331        valueArrayType = re.getReg(type, ttArray.regs[1]);
332    }
333    return [re.getReg(type, ttKey.regs[1]), valueType, valueMapType, valueArrayType];
334}
335
336function getUnionType(type) {
337    type = type.replace(/\s*/g, '');
338    let typeArr = new Array();
339    typeArr = type.split('|');
340    return typeArr;
341}
342
343function isFuncType(type) {
344    let isFunction = false;
345    if (type === null || type === undefined) {
346        isFunction = true;
347    }
348
349    if (type === 'function' || type === 'Function') {
350        isFunction = true;
351    }
352    return isFunction;
353}
354
355function isRegisterFunc(name) {
356    let regIndex = name.indexOf('register');
357    let isRegister = false;
358    if (regIndex === 0) {
359        isRegister = true;
360    }
361    return isRegister;
362}
363
364function isUnRegisterFunc(name) {
365    let unRegIndex = name.indexOf('unRegister');
366    let isRegister = false;
367    if (unRegIndex === 0) {
368        isRegister = true;
369    }
370    return isRegister;
371}
372
373function isOnObjCallback(name) {
374    let regIndex = name.indexOf('on');
375    let flag = false;
376    let onLen = 2;
377    if (regIndex === 0 && name.length > onLen) {
378        flag = true;
379    }
380    return flag;
381}
382
383// 箭头函数,如funTest(cb: (wid: boolean) => void): string;
384function isArrowFunc(type) {
385    let arrowFunc = false;
386    if (type.indexOf('AUTO_CALLFUNCTION') >= 0 || type.indexOf('=>') > 0) {
387        arrowFunc = true;
388    }
389    return arrowFunc;
390}
391
392function isOnOffRegisterFunc(name) {
393    let flag = false;
394    if (name === 'on' || name === 'off' || isRegisterFunc(name) || isUnRegisterFunc(name) ||
395        isOnObjCallback(name)) {
396        flag = true;
397    }
398    return flag;
399}
400
401function isCreateThreadsafeFunc(name) {
402    let index = name.indexOf('createThreadSafeFunc');
403    let isTdSafeFunc = false;
404    if (index === 0) {
405        isTdSafeFunc = true;
406    }
407    return isTdSafeFunc;
408}
409
410function getOnObjCallbackType(funcName, interName) {
411    let onObjCbType = '';
412    if (interName !== '') {
413        onObjCbType = interName + '_' + funcName;
414    } else {
415        onObjCbType = funcName;
416    }
417    return 'AUTO_CALLFUNCTION_' + onObjCbType;
418}
419
420function getOnCallbackFunAndInterName(CallbackType) {
421    CallbackType = CallbackType.replaceAll('AUTO_CALLFUNCTION_', '');
422    let CallbackTypes = CallbackType.split('_');
423    let funcName = CallbackTypes[1];
424    let interName = CallbackTypes[0];
425    return [interName, funcName];
426}
427
428class JsonCfgList { }
429JsonCfgList.jsonCfg = [];
430JsonCfgList.push = function (ifs) {
431    JsonCfgList.jsonCfg.push(ifs);
432};
433JsonCfgList.pop = function () {
434    JsonCfgList.jsonCfg.pop();
435};
436JsonCfgList.getValue = function (className, inter) {
437    let ifs = JsonCfgList.jsonCfg[JsonCfgList.jsonCfg.length - 1];
438    for (let i in ifs) {
439        if (ifs[i].interfaceName.className === className && ifs[i].interfaceName.funcName === inter) {
440            return ifs[i].serviceCode;
441        }
442    }
443    return null;
444};
445
446function getLogErrInfo() {
447    let errInfo = ' Please refer to for support capacity';
448    return errInfo;
449}
450
451module.exports = {
452    FuncType,
453    EnumValueType,
454    NumberIncrease,
455    InterfaceList,
456    TypeList,
457    CallFunctionList,
458    isType,
459    typeIndex,
460    getArrayType,
461    getArrayTypeTwo,
462    checkFileError,
463    isEnum,
464    enumIndex,
465    getMapType,
466    EnumList,
467    jsType2CType,
468    getUnionType,
469    isFuncType,
470    isArrowFunc,
471    jsonCfgList: JsonCfgList,
472    isRegisterFunc,
473    isUnRegisterFunc,
474    isOnObjCallback,
475    isOnOffRegisterFunc,
476    getOnObjCallbackType,
477    getLogErrInfo,
478    isCreateThreadsafeFunc,
479};