• 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) return "DIRECT";
47    else if (type == FuncType.SYNC) return "SYNC";
48    else if (type == FuncType.ASYNC) return "ASYNC";
49    else if (type == FuncType.PROMISE) return "PROMISE";
50    return "UNKNOW";
51}
52
53class NumberIncrease { }
54NumberIncrease.num = 1
55NumberIncrease.getAndIncrease = function () {
56    return NumberIncrease.num++;
57}
58NumberIncrease.get = function () {
59    return NumberIncrease.num;
60}
61NumberIncrease.reset = function () {
62    NumberIncrease.num = 1
63}
64
65class InterfaceList { }
66InterfaceList.interfacess_ = [];
67InterfaceList.push = function (ifs) {
68    InterfaceList.interfacess_.push(ifs)
69}
70InterfaceList.pop = function () {
71    InterfaceList.interfacess_.pop()
72}
73InterfaceList.getValue = function (name) {
74    let ifs = InterfaceList.interfacess_[InterfaceList.interfacess_.length - 1]
75    for (let i in ifs) {
76        if (ifs[i].name == name) {
77            var hasProperty = Object.prototype.hasOwnProperty.call(ifs[i].body, "allProperties")
78            if (hasProperty) {
79                return ifs[i].body.allProperties.values;
80            }
81            return null;
82        }
83    }
84    return null;
85}
86
87class TypeList { }
88TypeList.types = [];
89TypeList.push = function (ifs) {
90  TypeList.types.push(ifs)
91}
92TypeList.pop = function () {
93  TypeList.types.pop()
94}
95TypeList.getValue = function (name) {
96    let ifs = TypeList.types[TypeList.types.length - 1]
97    for (let i in ifs) {
98        if (ifs[i].name == name) {
99            var hasProperty = Object.prototype.hasOwnProperty.call(ifs[i].body, "allProperties")
100            if (hasProperty) {
101                return ifs[i].body.allProperties.values;
102            } else {
103                return ifs[i].body;
104            }
105            return null;
106        }
107    }
108    return null;
109}
110
111class EnumList { }
112EnumList.enum_ = [];
113EnumList.push = function (ifs) {
114    EnumList.enum_.push(ifs)
115}
116EnumList.pop = function () {
117    EnumList.enum_.pop()
118}
119EnumList.getValue = function (name) {
120    let ifs = EnumList.enum_[EnumList.enum_.length - 1]
121    for (let i in ifs) {
122        if (ifs[i].name == name) {
123            return ifs[i].body.element;
124        }
125    }
126    return null;
127}
128
129function getArrayType(type) {
130    let tt = re.match("Array<([a-zA-Z_0-9]+)>", type)
131    if (tt != null) {
132        return re.getReg(type, tt.regs[1])
133    }
134
135    tt = re.match("Array<{([[a-z:]+)([a-z:]]+)([a-zA-Z_1-9:]+)", type)
136    if (tt != null) {
137        let res = ''
138        let len = tt.regs.length
139        for (let i=1; i<len; i++) {
140            let regs1 = re.getReg(type, tt.regs[i])
141            res += regs1
142        }
143        return res
144    }
145
146    tt = re.match("Array<map<string", type)
147    if (tt != null) {
148        let preStr = 'Array<'
149        let preStrLen = preStr.length
150        let res = type.substring(preStrLen, type.length-1)
151        return res
152    }
153
154    tt = re.match("Array<Map<string", type)
155    if (tt != null) {
156        let preStr = 'Array<'
157        let preStrLen = preStr.length
158        let res = type.substring(preStrLen, type.length-1)
159        return res
160    }
161    return null
162}
163
164function getArrayTypeTwo(type) {
165    let tt = re.match("([a-zA-Z_0-9]+)", type)
166    return re.getReg(type, tt.regs[1])
167}
168
169function jsType2CType(jsTypeName) {
170    if (jsTypeName == "string") {
171        return "std::string"
172    } else if (jsTypeName == "boolean") {
173        return "bool"
174    } else {
175        return jsTypeName
176    }
177}
178
179class EnumValueType { }
180EnumValueType.ENUM_VALUE_TYPE_NUMBER = 0
181EnumValueType.ENUM_VALUE_TYPE_STRING = 1
182
183function isEnum(type, data) {
184    let isEnum = false
185    if (null == data) {
186        return isEnum
187    }
188    for (let i in data.enum) {
189        let enumm = data.enum[i]
190        if (type == enumm.name) {
191            isEnum = true
192        }
193    }
194    return isEnum
195}
196
197function enumIndex(type, data) {
198    let index;
199    if (null == data) {
200        return index
201    }
202    for (let i in data.enum) {
203        let enumm = data.enum[i]
204        if (type == enumm.name) {
205            index = i
206        }
207    }
208    return index
209}
210
211function isType(type, data) {
212  let isType = false
213  if (null == data) {
214    return isType
215  }
216  for (let i in data.type) {
217    let typee = data.type[i]
218    if (type == typee.name) {
219      isType = true
220    }
221  }
222  return isType
223}
224
225function typeIndex(type, data) {
226  let index;
227  if (null == data) {
228      return index
229  }
230  for (let i in data.type) {
231      let typee = data.type[i]
232      if (type == typee.name) {
233          index = i
234      }
235  }
236  return index
237}
238
239function getMapType(type) {
240    type = type.replace(/\s*/g,"")
241    let ttKey = re.search("Map<([a-zA-Z_0-9]+),", type)
242    let ttValue = re.search(",([a-zA-Z_0-9<>]+)>", type)
243    let ttMap = re.search(",([a-zA-Z_0-9]+)>>", type)
244    let ttArray = re.search("Array<([a-zA-Z_0-9]+)>", type)
245
246    if(ttArray == null) {
247        ttArray = re.search("([a-zA-Z_0-9]+)\\[\\]>", type)
248    }
249
250    let valueType
251    let valueMapType
252    let valueArrayType
253    if (ttKey == null && ttValue == null && ttMap == null) {
254        ttKey = re.search("key:([a-zA-Z_0-9]+)", type)
255        ttValue = re.search(":([a-zA-Z_0-9]+)}", type)
256        ttMap = re.search(":([a-zA-Z_0-9]+)}}", type)
257        ttArray = re.search("Array<([a-zA-Z_0-9]+)>", type)
258        if (ttArray == null) {
259            ttArray = re.search(":([a-zA-Z_0-9]+)\\[\\]}", type)
260        }
261    }
262
263    if (ttValue != null) {
264        valueType = re.getReg(type, ttValue.regs[1])
265        if (valueType.indexOf("Array<") == 0) {
266            valueArrayType = re.getReg(valueType, ttArray.regs[1])
267            valueType = undefined
268        } else if (ttMap != undefined) {
269            valueMapType = re.getReg(type, ttMap.regs[1])
270            valueType = undefined
271        }
272    }
273    if (ttMap != null) {
274        valueMapType = re.getReg(type, ttMap.regs[1])
275    }
276    if (ttArray != null) {
277        valueArrayType = re.getReg(type, ttArray.regs[1])
278    }
279    return [re.getReg(type, ttKey.regs[1]), valueType, valueMapType, valueArrayType]
280}
281
282function getUnionType(type) {
283    type = type.replace(/\s*/g,"")
284    var typeArr = new Array()
285    typeArr = type.split("|")
286    return typeArr
287}
288
289
290module.exports = {
291    FuncType,
292    EnumValueType,
293    NumberIncrease,
294    InterfaceList,
295    TypeList,
296    isType,
297    typeIndex,
298    getArrayType,
299    getArrayTypeTwo,
300    checkFileError,
301    isEnum,
302    enumIndex,
303    getMapType,
304    EnumList,
305    jsType2CType,
306    getUnionType
307}