diff --git a/src/gen/analyze/class.js b/src/gen/analyze/class.js index 62521d033f7a4bd6965cb43110bbb7096445b73c..a570e92e6ccaed4c906387321fbdfb8ee73fb5fd 100644 --- a/src/gen/analyze/class.js +++ b/src/gen/analyze/class.js @@ -52,7 +52,7 @@ function analyzeClass(data) { let rules = "(static)? *([A-Za-z0-9_]+)\\(([\n a-zA-Z:;=,_0-9?<>{}|]*)\\) *: *([A-Za-z0-9_<>{}:, .]+)"; matchNameAndType = re.match(rules, classBody) if (matchNameAndType) { - let funcDetail = analyzeFunction(re.getReg(classBody, matchNameAndType.regs[2]), + let funcDetail = analyzeFunction(data, re.getReg(classBody, matchNameAndType.regs[2]), re.getReg(classBody, matchNameAndType.regs[3]), re.getReg(classBody, matchNameAndType.regs[4])) if (funcDetail != null) result.function.push(funcDetail) diff --git a/src/gen/analyze/enum.js b/src/gen/analyze/enum.js index 0495762396e80ac41aef85a5f6606a533f0c6459..a4ab2fa4aa587d5c8ff26fa26a37bf4b827569c9 100644 --- a/src/gen/analyze/enum.js +++ b/src/gen/analyze/enum.js @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +const { NumberIncrease } = require("../tools/common"); const re = require("../tools/re"); /** Enum解析 */ @@ -39,8 +40,8 @@ function analyzeEnum(data) { } function analyzeEnumResult(result, bodyContent, index) { - let regString = re.match(" *([a-zA-Z0-9_]+) * = *\"([a-zA-Z_0-9<>-]+)\"", bodyContent) - let regSingleQuotes = re.match(" *([a-zA-Z0-9_]+) * = *'([a-zA-Z_0-9<>-]+)'", bodyContent) + let regString = re.match(" *([a-zA-Z0-9_]+) * = *\"([a-zA-Z_0-9<>-]+)*\"", bodyContent) + let regSingleQuotes = re.match(" *([a-zA-Z0-9_]+) * = *'([a-zA-Z_0-9<>-]+)*'", bodyContent) let regNumber = re.match(" *([a-zA-Z0-9_]+) * = *([a-zA-Z_0-9<>-]+)", bodyContent) let reg = re.match(" *([a-zA-Z0-9_]+) *", bodyContent) if (regString) { @@ -48,7 +49,8 @@ function analyzeEnumResult(result, bodyContent, index) { let elementValue = re.getReg(bodyContent, regString.regs[2]) result.element.push({ name: elementName, - value: elementValue + value: elementValue, + type: 'string' }) result.enumValueType = 1 } else if (regSingleQuotes) { @@ -56,7 +58,8 @@ function analyzeEnumResult(result, bodyContent, index) { let elementValue = re.getReg(bodyContent, regSingleQuotes.regs[2]) result.element.push({ name: elementName, - value: elementValue + value: elementValue, + type: 'string' }) result.enumValueType = 1 } else if (regNumber) { @@ -65,7 +68,8 @@ function analyzeEnumResult(result, bodyContent, index) { typeof (elementValue) result.element.push({ name: elementName, - value: elementValue + value: elementValue, + type: "NUMBER_TYPE_" + NumberIncrease.getAndIncrease() }) result.enumValueType = 0 } else if (reg) { @@ -73,7 +77,8 @@ function analyzeEnumResult(result, bodyContent, index) { let elementValue = index result.element.push({ name: elementName, - value: elementValue + value: elementValue, + type: "NUMBER_TYPE_" + NumberIncrease.getAndIncrease() }) result.enumValueType = 0 } diff --git a/src/gen/generate/interface.js b/src/gen/generate/interface.js index fa37f3314652837b343496de4787b12447b30e27..fca9f1528395fc9e53cf80fdc76a1244b6b34f6f 100644 --- a/src/gen/generate/interface.js +++ b/src/gen/generate/interface.js @@ -15,7 +15,7 @@ const { generateFunctionDirect } = require("./function_direct"); const { generateFunctionSync } = require("./function_sync"); const { generateFunctionAsync } = require("./function_async"); -const { FuncType, InterfaceList, getArrayType, getMapType } = require("../tools/common"); +const { FuncType, InterfaceList, getArrayType, getMapType, EnumList } = require("../tools/common"); const { jsToC } = require("./param_generate"); const { cToJs } = require("./return_generate"); const re = require("../tools/re"); @@ -46,6 +46,7 @@ function generateVariable(name, type, variable, className) { if (type == "string") variable.hDefine += "\n std::string %s;".format(name) else if (type.substring(0, 12) == "NUMBER_TYPE_") variable.hDefine += "\n %s %s;".format(type, name) else if (InterfaceList.getValue(type)) variable.hDefine += "\n %s %s;".format(type, name) + else if (EnumList.getValue(type)) variable.hDefine += "\n %s %s;".format(type, name) else if (type.indexOf("Array<") == 0) { let type2 = getArrayType(type) if (type2 == "string") type2 = "std::string" diff --git a/src/gen/generate/namespace.js b/src/gen/generate/namespace.js index cab3c08abb827afeed61a2b02d404fa370f91ce5..69c49da923f8acfe5c9fe89ea4243e3ca6caa8ad 100644 --- a/src/gen/generate/namespace.js +++ b/src/gen/generate/namespace.js @@ -17,7 +17,7 @@ const { generateFunctionSync } = require("./function_sync"); const { generateFunctionAsync } = require("./function_async"); const { generateInterface } = require("./interface"); const { generateClass } = require("./class"); -const { FuncType, InterfaceList } = require("../tools/common"); +const { FuncType, InterfaceList, EnumList } = require("../tools/common"); const { generateEnum } = require("./enum"); //生成module_middle.cpp、module.h、module.cpp @@ -28,6 +28,7 @@ function generateNamespace(name, data, inNamespace = "") { let middleInit = "" middleInit += formatMiddleInit(inNamespace, name) InterfaceList.push(data.interface) + EnumList.push(data.enum) for (let i in data.interface) { let ii = data.interface[i] let result = generateInterface(ii.name, ii.body, inNamespace + name + "::") @@ -63,6 +64,7 @@ function generateNamespace(name, data, inNamespace = "") { middleInit += result.middleInit } InterfaceList.pop(); + EnumList.pop(); if (inNamespace.length > 0) { middleInit += "}" } diff --git a/src/gen/generate/param_generate.js b/src/gen/generate/param_generate.js index 7566cce8043caca3d2744407268508cbda535e83..cb89cbde7d0b9224077853b95c5939165169c276 100644 --- a/src/gen/generate/param_generate.js +++ b/src/gen/generate/param_generate.js @@ -13,7 +13,7 @@ * limitations under the License. */ const { InterfaceList, getArrayType, getArrayTypeTwo, NumberIncrease, - enumIndex, isEnum, EnumValueType, getMapType } = require("../tools/common"); + enumIndex, isEnum, EnumValueType, getMapType, EnumList } = require("../tools/common"); const re = require("../tools/re"); const { NapiLog } = require("../tools/NapiLog"); @@ -63,6 +63,9 @@ function jsToC(dest, napiVn, type) { } return tt } + else if (EnumList.getValue(type)) { + return jsToCEnum(type, dest, napiVn) + } else if (type.indexOf("Array<") == 0) { return arrTemplete(dest, napiVn, type); } @@ -76,6 +79,17 @@ function jsToC(dest, napiVn, type) { NapiLog.logError(`do not support to generate jsToC %s,%s,%s`.format(dest, napiVn, type)); } +function jsToCEnum(type, dest, napiVn) { + let tt = "" + let ifl = EnumList.getValue(type) + for (let i in ifl) { + let name2 = ifl[i].name + let type2 = ifl[i].type + tt += jsToC("%s.%s".format(dest, name2), getValueProperty(napiVn, name2), type2) + } + return tt +} + function arrTemplete(dest, napiVn, type) { let arrayType if (type.substring(type.length - 2) == "[]") { diff --git a/src/gen/generate/return_generate.js b/src/gen/generate/return_generate.js index ecfad193b4e83ab10e21c00de1e299279a00f6c0..5392cdf6059edda72c64fbc2d2c993afdfe31195 100644 --- a/src/gen/generate/return_generate.js +++ b/src/gen/generate/return_generate.js @@ -13,7 +13,7 @@ * limitations under the License. */ const { InterfaceList, getArrayType, NumberIncrease, enumIndex, - isEnum, EnumValueType, getArrayTypeTwo, getMapType } = require("../tools/common"); + isEnum, EnumValueType, getArrayTypeTwo, getMapType, EnumList } = require("../tools/common"); const { NapiLog } = require("../tools/NapiLog"); function cToJs(value, type, dest, deep = 1) { @@ -39,6 +39,20 @@ function cToJs(value, type, dest, deep = 1) { } return result } + else if(EnumList.getValue(type)){ + let lt = deep + let result = "" + let ifl = EnumList.getValue(type) + for (let i in ifl) { + let name2 = ifl[i].name + let type2 = ifl[i].type + let interfaceType = cToJs("%s.%s".format(value, name2), type2, "tnv%d".format(lt), deep + 1) + result += "{\nnapi_value tnv%d = nullptr;\n".format(lt) + + interfaceType + `\npxt->SetValueProperty(%s,"%s",tnv%d);\n}` + .format(dest, name2, lt) + } + return result + } else if (type.substring(0, 6) == "Array<" || type.substring(type.length - 2) == "[]") { let arrayType = checkArrayParamType(type) return arrayTempleteFunc(arrayType, deep, dest, value) diff --git a/src/gen/tools/common.js b/src/gen/tools/common.js index b0c5721187576344b1edf285ae2802dda7fea21b..185554c4a92b31ca8eab5a7fa0bbd5a7833b5192 100644 --- a/src/gen/tools/common.js +++ b/src/gen/tools/common.js @@ -80,6 +80,24 @@ InterfaceList.getValue = function (name) { return null; } +class EnumList { } +EnumList.enum_ = []; +EnumList.push = function (ifs) { + EnumList.enum_.push(ifs) +} +EnumList.pop = function () { + EnumList.enum_.pop() +} +EnumList.getValue = function (name) { + let ifs = EnumList.enum_[EnumList.enum_.length - 1] + for (let i in ifs) { + if (ifs[i].name == name) { + return ifs[i].body.element; + } + } + return null; +} + function getArrayType(type) { let tt = re.match("Array<([a-zA-Z_0-9]+)>", type) return re.getReg(type, tt.regs[1]) @@ -123,7 +141,7 @@ function enumIndex(type, data) { } function getMapType(type) { - type = type.replace(/\s*/g,"") + type = type.replace(/\s*/g, "") let tt1 = re.search("Map<([a-zA-Z_0-9]+),", type) let tt2 = re.search(",([a-zA-Z_0-9]+)>", type) let tt3 @@ -132,19 +150,19 @@ function getMapType(type) { let valueType let valueMapType let valueArrayType - if(tt1 == null && tt2 == null){ + if (tt1 == null && tt2 == null) { tt1 = re.search("key:([a-zA-Z_0-9]+)", type) tt2 = re.search(":([a-zA-Z_0-9]+)}", type) tt3 = re.search(":([a-zA-Z_0-9]+)}}", type) tt4 = re.search("Array<([a-zA-Z_0-9]+)>", type) } - if(tt2 != null){ + if (tt2 != null) { valueType = re.getReg(type, tt2.regs[1]) } - if(tt3 != null){ + if (tt3 != null) { valueMapType = re.getReg(type, tt3.regs[1]) } - if(tt4 != null){ + if (tt4 != null) { valueArrayType = re.getReg(type, tt4.regs[1]) } return [re.getReg(type, tt1.regs[1]), valueType, valueMapType, valueArrayType] @@ -160,5 +178,6 @@ module.exports = { checkFileError, isEnum, enumIndex, - getMapType + getMapType, + EnumList } \ No newline at end of file