From 70fa975102824cf796a867352874ddb689c3641d Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Tue, 16 Jul 2024 15:34:49 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix=20bug:=20js=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=97=B6=E8=AF=AF=E6=94=B9=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gou-jingjing --- src/cli/dts2cpp/src/gen/analyze/namespace.js | 2 +- src/cli/dts2cpp/src/gen/analyze/type.js | 2 +- src/cli/dts2cpp/src/gen/generate/function_onoff.js | 2 +- src/cli/dts2cpp/src/gen/generate/param_generate.js | 3 ++- src/cli/dts2cpp/src/gen/generate/return_generate.js | 4 ++-- src/cli/dts2cpp/src/gen/generate/type.js | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/cli/dts2cpp/src/gen/analyze/namespace.js b/src/cli/dts2cpp/src/gen/analyze/namespace.js index 2de91620..0e7c0958 100644 --- a/src/cli/dts2cpp/src/gen/analyze/namespace.js +++ b/src/cli/dts2cpp/src/gen/analyze/namespace.js @@ -229,7 +229,7 @@ function parseType(matchs, data, result) { } } - matchs = re.match('(export )*type ([a-zA-Z]+) *= *([\\(\\):=a-zA-Z<> |\n\']+); ', data); + matchs = re.match('(export )*type ([a-zA-Z]+) *= *([\\(\\):=a-zA-Z<> |\n\']+);', data); if (matchs) { let typeName = re.getReg(data, matchs.regs[2]); let typeBody = re.getReg(data, matchs.regs[3]); diff --git a/src/cli/dts2cpp/src/gen/analyze/type.js b/src/cli/dts2cpp/src/gen/analyze/type.js index 4669a881..5787d588 100644 --- a/src/cli/dts2cpp/src/gen/analyze/type.js +++ b/src/cli/dts2cpp/src/gen/analyze/type.js @@ -75,7 +75,7 @@ function analyzeTypeVariable(t, tt, result) { } function analyzeType2(data) { - let body = re.replaceAll(data, ' ', '').split('"|"'); + let body = re.replaceAll(data, ' ', '').split('\'|\''); let result = { element: [], function: [], diff --git a/src/cli/dts2cpp/src/gen/generate/function_onoff.js b/src/cli/dts2cpp/src/gen/generate/function_onoff.js index 65294505..805e04f2 100644 --- a/src/cli/dts2cpp/src/gen/generate/function_onoff.js +++ b/src/cli/dts2cpp/src/gen/generate/function_onoff.js @@ -326,7 +326,7 @@ function genCallbackMethodH(param, codeContext) { } function genCallbackMethod(param, className, middleClassName, codeContext) { - let isStrType = param.eventNameIsStr ? '' : `std::string eventName = '[fixed_eventName]';`; + let isStrType = param.eventNameIsStr ? '' : `std::string eventName = "[fixed_eventName]";`; isStrType = replaceAll(isStrType, '[fixed_eventName]', param.eventName); let implCppCallBack = replaceAll(implCppEventCallbakTemplate, '[eventName]', param.eventName); implCppCallBack = replaceAll(implCppCallBack, '[callback_param_type]', param.params); diff --git a/src/cli/dts2cpp/src/gen/generate/param_generate.js b/src/cli/dts2cpp/src/gen/generate/param_generate.js index f827c6e1..e32ee37f 100644 --- a/src/cli/dts2cpp/src/gen/generate/param_generate.js +++ b/src/cli/dts2cpp/src/gen/generate/param_generate.js @@ -878,6 +878,7 @@ function paramGenerateCallBack(data, funcValue, param, p) { } } + param.valueIn += '\n uint32_t outErrCode = 0;'; param.callback = { // function类型参数,按照空参数、空返回值回调处理 () => void {} type: cbParamType, @@ -1170,7 +1171,7 @@ function eventParamGenerate(p, funcValue, param, data) { return; } if (type.indexOf("'") >= 0) { - type = type.replaceAll(''', ''); + type = type.replaceAll('\'', ''); } let regName = re.match('([a-zA-Z_0-9]+)', type); if (isFuncType(type)) { diff --git a/src/cli/dts2cpp/src/gen/generate/return_generate.js b/src/cli/dts2cpp/src/gen/generate/return_generate.js index 431c39d2..23d573a1 100644 --- a/src/cli/dts2cpp/src/gen/generate/return_generate.js +++ b/src/cli/dts2cpp/src/gen/generate/return_generate.js @@ -671,12 +671,12 @@ function returnGenerateForOnOffMultiPara(paramInfo, param, data) { NapiLog.logInfo('The current void type don\'t need generate'); } else if (type === 'boolean') { - param.valueOut += paramInfo.optional ? 'bool* %s = nullptr;' : 'bool %s;\n'; + param.valueOut += paramInfo.optional ? 'bool* %s = nullptr;' : 'bool %s;\n' .format(paramInfo.name, paramInfo.name); param.params += '%sbool%s %s'.format(param.params.length > 0 ? ', ' : '', modifiers, paramInfo.name); } else if (type.substring(0, 12) === 'NUMBER_TYPE_') { - param.valueOut += type + (paramInfo.optional ? '* %s = nullptr;' : ' %s;\n'); + param.valueOut += type + (paramInfo.optional ? '* %s = nullptr;' : ' %s;\n') .format(paramInfo.name, paramInfo.name); param.params += '%s%s%s %s'.format(param.params.length > 0 ? ', ' : '', type, modifiers, paramInfo.name) diff --git a/src/cli/dts2cpp/src/gen/generate/type.js b/src/cli/dts2cpp/src/gen/generate/type.js index aef7251b..3a5f0ed1 100644 --- a/src/cli/dts2cpp/src/gen/generate/type.js +++ b/src/cli/dts2cpp/src/gen/generate/type.js @@ -282,7 +282,7 @@ function generateType(name, data, inNamespace, inNameSpaceEnum, nameSpaceName, t implCpp: implCpp, middleBody: '', middleInit: middleInit, - declarationH: `, + declarationH: ` struct %s;\r`.format(name), middleH: '', midInitEnumDefine: '', -- Gitee From 66a4f7d678b080ba2b8e4e0f134487e53b295597 Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Tue, 16 Jul 2024 15:59:00 +0800 Subject: [PATCH 2/4] fix codecheck Signed-off-by: gou-jingjing --- .../src/gen/generate/param_generate.js | 11 ++++++---- .../src/gen/generate/return_generate.js | 20 +++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/cli/dts2cpp/src/gen/generate/param_generate.js b/src/cli/dts2cpp/src/gen/generate/param_generate.js index e32ee37f..305467fc 100644 --- a/src/cli/dts2cpp/src/gen/generate/param_generate.js +++ b/src/cli/dts2cpp/src/gen/generate/param_generate.js @@ -57,7 +57,9 @@ function getMapCType(type) { else if (mapType[1] === 'any') { mapTypeString = 'std::any' } else if (mapType[1] !== null) { mapTypeString = mapType[1] } } else if (mapType[2] !== undefined) { - if (mapType[2] === 'string') { mapTypeString = 'std::map' } + if (mapType[2] === 'string') { + mapTypeString = 'std::map'; + } else if (mapType[2].substring(0, 12) === 'NUMBER_TYPE_') { mapTypeString = 'std::map'.format(mapType[2]); } @@ -108,6 +110,7 @@ function jsToC(dest, napiVn, type, enumType = 0, optional) { } else { NapiLog.logError(`do not support to generate jsToC %s,%s,%s` .format(dest, napiVn, type), getLogErrInfo()); + return null; } } @@ -827,7 +830,7 @@ function getCBparaTypeForArrow(type) { if (callbackParams.indexOf(',') >= 0) { // 多个参数,进行分割 callbackParams = callbackParams.split(','); - for(let i = 0; i < callbackParams.length; i++) { + for (let i = 0; i < callbackParams.length; i++) { NapiLog.logInfo('muilti paramets'); } } else { // 一个参数 @@ -878,7 +881,6 @@ function paramGenerateCallBack(data, funcValue, param, p) { } } - param.valueIn += '\n uint32_t outErrCode = 0;'; param.callback = { // function类型参数,按照空参数、空返回值回调处理 () => void {} type: cbParamType, @@ -1132,7 +1134,8 @@ function paramGenerate(p, funcValue, param, data) { let inParamName = funcValue.optional ? '(*vio->in' + p + ')' : 'vio->in' + p; let modifiers = funcValue.optional ? '*' : '&'; if (type.indexOf('|') >= 0) { - return paramGenerateUnion(type, param, p, name); + paramGenerateUnion(type, param, p, name); + return; } else if (type === 'string') { paramGenerateCommon(p, 'std::string', funcValue, param, modifiers, inParamName); } else if (type.substring(0, 12) === 'NUMBER_TYPE_' && type.indexOf('[]') < 0) { diff --git a/src/cli/dts2cpp/src/gen/generate/return_generate.js b/src/cli/dts2cpp/src/gen/generate/return_generate.js index 23d573a1..a15e0f55 100644 --- a/src/cli/dts2cpp/src/gen/generate/return_generate.js +++ b/src/cli/dts2cpp/src/gen/generate/return_generate.js @@ -49,7 +49,7 @@ function cToJsForType(value, type, dest, deep) { let isSubEnum = EnumList.getValue(type2) ? true : false; let subDest = isSubEnum ? dest : 'tnv%d'.format(lt); let typeType = null; - let ifOptional = ''; // 如果是可选参数则需要增加可选参数是否有值的判断 + let ifOptional = ''; // 如果是可选参数则需要增加可选参数是否有值的判断 if (optional2) { ifOptional = 'if (%s.%s.has_value())\n'.format(value, name2); typeType = cToJs('%s.%s'.format(value, '%s.value()'.format(name2)), type2, subDest, deep + 1); @@ -57,7 +57,7 @@ function cToJsForType(value, type, dest, deep) { typeType = cToJs('%s.%s'.format(value, name2), type2, subDest, deep + 1); } if (isSubEnum) { - result += typeType + result += typeType; } else { result += '%s{\nnapi_value tnv%d = nullptr;\n'.format(ifOptional, lt) + typeType + `\npxt->SetValueProperty(%s, "%s", tnv%d);\n}\n` @@ -91,7 +91,7 @@ function cToJsForInterface(value, type, dest, deep) { if (isSubEnum) { // interface include enum properties - result += interfaceType + result += interfaceType; } else { result += '%s{\nnapi_value tnv%d = nullptr;\n'.format(ifOptional, lt) + interfaceType + `\npxt->SetValueProperty(%s, "%s", tnv%d);\n}\n` @@ -493,7 +493,7 @@ function mapTempleteArray(mapType, tnvdef, lt) { pxt->SetArrayElement(tnv%d, j, tnv%d); }`.format(lt + 2, lt + 2, lt, lt, lt, lt + 2, lt + 1, lt + 2)); } - return ret + return ret; } function returnGenerateMap(returnInfo, param) { @@ -564,7 +564,7 @@ function getReturnFill(returnInfo, param) { valueFillStr += '%svio->out'.format(valueFillStr.length > 0 ? ', ' : ''); } } - } else { // 普通方法的返回参数处理 + } else { // 普通方法的返回参数处理 valueFillStr = 'vio->out'; } return valueFillStr; @@ -590,7 +590,7 @@ function generateOptionalAndUnion(returnInfo, param, data, outParam, c2JsresultN // 判断callback是否有效,若无效,则为普通函数 let paramCallbackFlag = param.callback !== undefined ? true : false; - let paramCallbackIsArrow + let paramCallbackIsArrow; if (paramCallbackFlag) { // 若callback有效, 判断是否是箭头函数 paramCallbackIsArrow = param.callback.isArrowFuncFlag; } @@ -630,7 +630,7 @@ function returnGenerateForArrowCbMultiPara(paramInfo, param, data, i) { else if (type === 'boolean') { param.valueOut += paramInfo.optional ? 'bool* %s = nullptr;' : 'bool %s;\n' .format(paramInfo.name, paramInfo.name); - param.valueDefine += '%sbool%s %s'.format(param.valueDefine.length > 0 ? ', ' : '', modifiers, paramInfo.name) + param.valueDefine += '%sbool%s %s'.format(param.valueDefine.length > 0 ? ', ' : '', modifiers, paramInfo.name); } else if (type.substring(0, 12) === 'NUMBER_TYPE_') { param.valueOut += type + (paramInfo.optional ? '* %s = nullptr;' : ' %s;\n') @@ -679,7 +679,7 @@ function returnGenerateForOnOffMultiPara(paramInfo, param, data) { param.valueOut += type + (paramInfo.optional ? '* %s = nullptr;' : ' %s;\n') .format(paramInfo.name, paramInfo.name); param.params += '%s%s%s %s'.format(param.params.length > 0 ? ', ' : '', type, modifiers, - paramInfo.name) + paramInfo.name); } else if (generateType(type)) { returnGenerate2(paramInfo, param, data); @@ -812,7 +812,7 @@ function returnGenerate2(returnInfo, param, data) { arrayType = jsType2CType(arrayType); if (arrayType === 'any') { param.valueOut = `std::any out;\n - std::string out_type;` + std::string out_type;`; param.valueDefine += '%sstd::any &out'.format(param.valueDefine.length > 0 ? ', ' : ''); } else { param.valueOut = returnInfo.optional ? 'std::vector<%s>* out = nullptr;\n'.format(arrayType) @@ -879,4 +879,4 @@ module.exports = { returnGenerateEnum, objectTempleteFuncReturn, cToJsForType, -} +}; -- Gitee From 394e92f5bc9fce88fc904493665b95f79faefa41 Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Tue, 16 Jul 2024 19:22:10 +0800 Subject: [PATCH 3/4] fix bugs Signed-off-by: gou-jingjing --- src/cli/dts2cpp/src/gen/analyze/params.js | 45 +++++++++++-------- .../src/gen/generate/param_generate.js | 1 + .../src/gen/generate/return_generate.js | 16 +++---- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/cli/dts2cpp/src/gen/analyze/params.js b/src/cli/dts2cpp/src/gen/analyze/params.js index 3a1cfd01..694ab7e9 100644 --- a/src/cli/dts2cpp/src/gen/analyze/params.js +++ b/src/cli/dts2cpp/src/gen/analyze/params.js @@ -94,24 +94,10 @@ function analyzeParams(funcName, values) { let optionalFlag = re.getReg(v, matchs.regs[2]) === '?' ? true : false; let checkParamOk = true; - if (optionalFlag) { - optionalParamCount++; - } else if (optionalParamCount > 0) { - // 可选参数之后不能再有必选参数,须是可选参数。 - NapiLog.logError('Invalid parameter [%s] of function [%s],'.format(v, funcName) + - ' the required parameter cannot follow an optional parameter.'); - checkParamOk = false; - } - if (checkParamOk) { - result.push({ 'name': re.getReg(v, matchs.regs[1]), 'type': type, 'optional': optionalFlag, 'realType': type }); - if (type.indexOf('AsyncCallback') >= 0) { - funcType = FuncType.ASYNC; - } - - if (isSyncFuncType(type, funcType)) { - funcType = FuncType.SYNC; - } - } + let retVal = analyzaParamsFunc(optionalFlag, optionalParamCount, v, funcName, checkParamOk, + result, matchs, type, funcType); + result = retVal[0]; + funcType = retVal[1]; } else { NapiLog.logError('Failed to analyse parameter [%s] of function [%s].'.format(v, funcName)); @@ -120,6 +106,29 @@ function analyzeParams(funcName, values) { return [result, funcType, rsltCallFunction]; } +function analyzaParamsFunc(optionalFlag, optionalParamCount, v, funcName, checkParamOk, + result, matchs, type, funcType) { + if (optionalFlag) { + optionalParamCount++; + } else if (optionalParamCount > 0) { + // 可选参数之后不能再有必选参数,须是可选参数。 + NapiLog.logError('Invalid parameter [%s] of function [%s],'.format(v, funcName) + + ' the required parameter cannot follow an optional parameter.'); + checkParamOk = false; + } + if (checkParamOk) { + result.push({ 'name': re.getReg(v, matchs.regs[1]), 'type': type, 'optional': optionalFlag, 'realType': type }); + if (type.indexOf('AsyncCallback') >= 0) { + funcType = FuncType.ASYNC; + } + + if (isSyncFuncType(type, funcType)) { + funcType = FuncType.SYNC; + } + } + return [result, funcType]; +} + module.exports = { analyzeParams, }; diff --git a/src/cli/dts2cpp/src/gen/generate/param_generate.js b/src/cli/dts2cpp/src/gen/generate/param_generate.js index 305467fc..867151df 100644 --- a/src/cli/dts2cpp/src/gen/generate/param_generate.js +++ b/src/cli/dts2cpp/src/gen/generate/param_generate.js @@ -472,6 +472,7 @@ function paramGenerateArray(p, funcValue, param) { } else { NapiLog.logError('The current version do not support to this param to generate :', name, 'type :', type, getLogErrInfo()); + return null; } } diff --git a/src/cli/dts2cpp/src/gen/generate/return_generate.js b/src/cli/dts2cpp/src/gen/generate/return_generate.js index a15e0f55..b50f0650 100644 --- a/src/cli/dts2cpp/src/gen/generate/return_generate.js +++ b/src/cli/dts2cpp/src/gen/generate/return_generate.js @@ -27,20 +27,18 @@ const specialPrefixArr = ['p->', 'vio->out.']; * @returns the real value without prefix, example: xxx, yyy */ function delPrefix(valueName) { - for (var i in specialPrefixArr) { - if (valueName.indexOf(specialPrefixArr[i]) === 0) { - // Find special prefix and delete it. - return valueName.substring(specialPrefixArr[i].length, valueName.length); - } - } - // Without special prefix, nothing is changed. - return valueName; + return specialPrefixArr.reduce((result, prefix) => { + if (valueName.startsWith(prefix)) { + return valueName.substring(prefix.length); + } + return result; + }, valueName); } function cToJsForType(value, type, dest, deep) { let lt = deep; let result = ''; - let ifl = TypeList.getValue(type) + let ifl = TypeList.getValue(type); if (typeof (ifl) === 'object') { for (let i in ifl) { let name2 = ifl[i].name; -- Gitee From d3210ad95075be64e3170c907d4219a3a19857ca Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Tue, 16 Jul 2024 20:13:13 +0800 Subject: [PATCH 4/4] fix codecheck Signed-off-by: gou-jingjing --- src/cli/dts2cpp/src/gen/generate/param_generate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/dts2cpp/src/gen/generate/param_generate.js b/src/cli/dts2cpp/src/gen/generate/param_generate.js index 867151df..24ab33c3 100644 --- a/src/cli/dts2cpp/src/gen/generate/param_generate.js +++ b/src/cli/dts2cpp/src/gen/generate/param_generate.js @@ -472,7 +472,7 @@ function paramGenerateArray(p, funcValue, param) { } else { NapiLog.logError('The current version do not support to this param to generate :', name, 'type :', type, getLogErrInfo()); - return null; + return; } } -- Gitee