mirror of
https://gitee.com/openharmony/napi_generator
synced 2024-11-27 02:30:36 +00:00
Merge branch 'master' of gitee.com:openharmony/napi_generator into master
Signed-off-by: 苟晶晶 <goujingjing@kaihong.com>
This commit is contained in:
commit
c97dc05b1d
@ -288,7 +288,7 @@ function parseFunction(matchs, data, result) {
|
||||
funcRet = funcRet.substring(0, funcRet.length-1)
|
||||
}
|
||||
let funcDetail = analyzeFunction(
|
||||
result, false, funcName, funcValue.substring(1, funcValue.length - 1), funcRet)
|
||||
result, false, funcName, funcValue.substring(1, funcValue.length - 1), funcRet, result)
|
||||
if (funcDetail != null) {
|
||||
// 完全一样的方法不重复添加 (如同名同参的AsyncCallback和Promise方法)
|
||||
addUniqFunc2List(funcDetail, result.function)
|
||||
|
@ -14,9 +14,18 @@
|
||||
*/
|
||||
const re = require("../tools/re");
|
||||
const { checkOutBody, print } = require("../tools/tool");
|
||||
const { FuncType, NumberIncrease, isArrowFunc } = require("../tools/common");
|
||||
const { FuncType, NumberIncrease,isFuncType, isArrowFunc } = require("../tools/common");
|
||||
const { NapiLog } = require("../tools/NapiLog");
|
||||
|
||||
function isSyncFuncType(type, funcType) {
|
||||
let isSync = false;
|
||||
if (funcType == FuncType.DIRECT && type.indexOf("Callback") >= 0 && type.indexOf("AsyncCallback") < 0 ||
|
||||
isFuncType(type) || isArrowFunc(type)) {
|
||||
isSync = true;
|
||||
}
|
||||
return isSync;
|
||||
}
|
||||
|
||||
/**
|
||||
* on方法中回调方法的解析
|
||||
* @param {*} valueType 回调方法体
|
||||
@ -93,12 +102,14 @@ function analyzeParams(funcName, values) {
|
||||
checkParamOk = false;
|
||||
}
|
||||
if (checkParamOk) {
|
||||
result.push({ "name": re.getReg(v, matchs.regs[1]), "type": type , "optional": optionalFlag})
|
||||
if (type.indexOf("AsyncCallback") >= 0)
|
||||
result.push({ "name": re.getReg(v, matchs.regs[1]), "type": type, "optional": optionalFlag})
|
||||
if (type.indexOf("AsyncCallback") >= 0) {
|
||||
funcType = FuncType.ASYNC
|
||||
if (funcType == FuncType.DIRECT && type.indexOf("Callback") >= 0 && type.indexOf("AsyncCallback") < 0 ||
|
||||
isArrowFunc(type))
|
||||
}
|
||||
|
||||
if (isSyncFuncType(type, funcType)) {
|
||||
funcType = FuncType.SYNC
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -163,7 +163,7 @@ function gennerateEventCallback(codeContext, data, param) {
|
||||
for (let i = 0; i < param.callback.length; i++) {
|
||||
paramIsAsync = param.callback[i].isAsync
|
||||
returnGenerate(param.callback[i], param, data, i)
|
||||
let paramType = param.valueOut.substring(0, param.valueOut.length - "out;".length)
|
||||
let paramType = param.valueOut.substring(0, param.valueOut.length - "out;\n".length)
|
||||
paramType = re.replaceAll(paramType, " ", "")
|
||||
let realParamType = paramType.substring(0, 12) == "NUMBER_TYPE_" ? "uint32_t" : paramType
|
||||
let tag = i == param.callback.length - 1? '' : ', '
|
||||
|
@ -15,6 +15,7 @@
|
||||
const { replaceAll, getPrefix, getConstNum } = require("../tools/tool");
|
||||
const { paramGenerate } = require("./param_generate");
|
||||
const { returnGenerate } = require("./return_generate");
|
||||
const { NapiLog } = require("../tools/NapiLog");
|
||||
|
||||
/**
|
||||
* 结果通过同步回调(CallBack)返回
|
||||
@ -36,14 +37,20 @@ struct [funcName]_value_struct {[valueIn][valueOut]
|
||||
[valueCheckout][optionalCallbackInit]
|
||||
[callFunc]
|
||||
napi_value result = nullptr;
|
||||
napi_value retVal = nullptr;
|
||||
if (pxt->GetArgc() > [callback_param_offset]) {
|
||||
[valuePackage]
|
||||
{
|
||||
napi_value args[1] = {result};
|
||||
pxt->SyncCallBack(pxt->GetArgv([callback_param_offset]), XNapiTool::ONE, args);
|
||||
retVal = pxt->SyncCallBack(pxt->GetArgv([callback_param_offset]), XNapiTool::ONE, args);
|
||||
}
|
||||
}
|
||||
result = pxt->UndefinedValue();
|
||||
|
||||
if (retVal != nullptr) {
|
||||
[cbRetValJs2C]
|
||||
[funcRetC2Js]
|
||||
}
|
||||
|
||||
[optionalParamDestory]
|
||||
delete vio;
|
||||
if (pxt->IsFailed()) {
|
||||
@ -59,6 +66,12 @@ bool %s%s(%s)
|
||||
return true;
|
||||
}
|
||||
`
|
||||
let cppFuncReturnTemplate = `
|
||||
bool %s%sReturn(%s)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
`
|
||||
|
||||
function removeEndlineEnter(value) {
|
||||
for (var i = value.length; i > 0; i--) {
|
||||
@ -82,6 +95,130 @@ function getOptionalCallbackInit(param) {
|
||||
.format(getConstNum(param.callback.offset), cType)
|
||||
}
|
||||
|
||||
function callBackReturnValJs2C(funcName, callbackRetType) {
|
||||
let cbRetJs2CTrans = ''
|
||||
if (callbackRetType === 'void') {
|
||||
cbRetJs2CTrans = '';
|
||||
} else if (callbackRetType === 'string') {
|
||||
cbRetJs2CTrans = 'pxt->SwapJs2CUtf8(retVal, vio->cbOut);\n' +
|
||||
'%sReturn(vio->cbOut, vio->retOut);\n'.format(funcName);
|
||||
} else if (callbackRetType === 'boolean') {
|
||||
cbRetJs2CTrans = 'vio->in0 = pxt->SwapJs2CBool(retVal);\n';
|
||||
} else if (callbackRetType === 'number') {
|
||||
cbRetJs2CTrans = 'vio->in0 = pxt->SwapJs2CInt32(retVal);\n';
|
||||
} else {
|
||||
|
||||
}
|
||||
return cbRetJs2CTrans;
|
||||
}
|
||||
|
||||
function returnProcRetC2Js(funRetType) {
|
||||
let retC2JsCode = '';
|
||||
if (funRetType === 'void') {
|
||||
|
||||
} else if (funRetType === 'string') {
|
||||
retC2JsCode = 'result = pxt->SwapC2JsUtf8(vio->retOut.c_str());'
|
||||
} else if (funRetType === 'boolean') {
|
||||
retC2JsCode = 'result = pxt->SwapC2JsUtf8(vio->retOut);'
|
||||
} else {
|
||||
|
||||
}
|
||||
return retC2JsCode;
|
||||
}
|
||||
|
||||
function fillCbRetValueStruct(type, param, outName) {
|
||||
if (type === null || param === null || param.valueOut === null || param.valueDefine === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === 'void') {
|
||||
NapiLog.logInfo("The current void type don't need generate");
|
||||
} else if (type === 'string') {
|
||||
param.cbRetvalueDefine += "%sstd::string& %s".format(param.cbRetvalueDefine.length > 0 ? ", " : "", outName)
|
||||
} else if (type === 'boolean') {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function fillValueStruct(type, param, outName) {
|
||||
if (type === null || param === null || param.valueOut === null || param.valueDefine === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === 'void') {
|
||||
NapiLog.logInfo("The current void type don't need generate");
|
||||
} else if (type === 'string') {
|
||||
param.valueOut += 'std::string %s;\n'.format(outName)
|
||||
if (param.callback.returnType === 'void') {
|
||||
param.valueDefine += "%sstd::string& %s".format(param.valueDefine.length > 0 ? ", " : "", outName)
|
||||
}
|
||||
} else if (type === 'boolean') {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function callbackReturnProc(param, func) {
|
||||
fillValueStruct(param.callback.returnType, param, 'cbOut')
|
||||
fillValueStruct(func.ret, param, 'retOut')
|
||||
|
||||
// 回调返回值非空,业务代码分两部分,一部分填写JS回调需要的参数(对应funcname函数),一部分根据回调返回值进行后续业务处理(对应funcnameReturn函数),
|
||||
// 回调返回值为空,则业务代码处理是一个整体,对应funcname函数,统一处理填写参数、函数返回值赋值处理。
|
||||
if (param.callback.returnType === 'void') {
|
||||
if (func.ret == "string" || func.ret == "boolean") {
|
||||
param.valueFill += param.valueFill.length > 0 ? ", vio->retOut" : "vio->retOut"
|
||||
}
|
||||
else if (func.ret == "void") {
|
||||
NapiLog.logInfo("The current void type don't need generate");
|
||||
}
|
||||
else{
|
||||
NapiLog.logError("not support returnType:%s!".format(param.callback.returnType))
|
||||
}
|
||||
} else{
|
||||
// param.cbRetvalueDefine赋值,传递给funcnameReturn函数
|
||||
fillCbRetValueStruct(param.callback.returnType, param, 'in')
|
||||
fillCbRetValueStruct(func.ret, param, 'out')
|
||||
}
|
||||
}
|
||||
|
||||
function getImplHAndCpp(className, param, func, implH, implCpp, prefixArr) {
|
||||
if (!func.isParentMember) {
|
||||
// 只有类/接口自己的成员方法需要在.h.cpp中生成,父类/父接口不需要
|
||||
implH = "\n%s%s%sbool %s(%s)%s;".format(
|
||||
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.valueDefine, prefixArr[3])
|
||||
implCpp = cppTemplate.format(className == null ? "" : className + "::", func.name, param.valueDefine)
|
||||
|
||||
if (param.callback.returnType != 'void' && param.callback.returnType != undefined) {
|
||||
implH += "\n%s%s%sbool %sReturn(%s)%s;".format(
|
||||
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.cbRetvalueDefine, prefixArr[3])
|
||||
implCpp += cppFuncReturnTemplate.format(className == null ? "" : className + "::",
|
||||
func.name, param.cbRetvalueDefine)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function replaceValueOut(param, middleFunc) {
|
||||
if (param.valueOut == "") {
|
||||
middleFunc = replaceAll(middleFunc, "[valueOut]", param.valueOut) // # 输出参数定义
|
||||
} else {
|
||||
middleFunc = replaceAll(middleFunc, "[valueOut]", "\n " + param.valueOut) // # 输出参数定义
|
||||
}
|
||||
return middleFunc
|
||||
}
|
||||
|
||||
function replaceValueCheckout(param, middleFunc) {
|
||||
if (param.valueCheckout == "") {
|
||||
middleFunc = replaceAll(middleFunc, "[valueCheckout]", param.valueCheckout) // # 输入参数解析
|
||||
} else {
|
||||
param.valueCheckout = removeEndlineEnter(param.valueCheckout)
|
||||
middleFunc = replaceAll(middleFunc, "[valueCheckout]", param.valueCheckout) // # 输入参数解析
|
||||
}
|
||||
return middleFunc
|
||||
}
|
||||
|
||||
function generateFunctionSync(func, data, className) {
|
||||
let middleFunc = replaceAll(funcSyncTemplete, "[funcName]", func.name)
|
||||
if (className == null) {
|
||||
@ -96,25 +233,18 @@ function generateFunctionSync(func, data, className) {
|
||||
}
|
||||
// 定义输入,定义输出,解析,填充到函数内,输出参数打包,impl参数定义,可选参数内存释放
|
||||
let param = { valueIn: "", valueOut: "", valueCheckout: "", valueFill: "",
|
||||
valuePackage: "", valueDefine: "", optionalParamDestory: "" }
|
||||
valuePackage: "", valueDefine: "", optionalParamDestory: "", cbRetvalueDefine: ""}
|
||||
|
||||
for (let i in func.value) {
|
||||
paramGenerate(i, func.value[i], param, data)
|
||||
}
|
||||
returnGenerate(param.callback, param)
|
||||
callbackReturnProc(param, func);
|
||||
|
||||
middleFunc = replaceAll(middleFunc, "[valueIn]", param.valueIn) // # 输入参数定义
|
||||
if (param.valueOut == "") {
|
||||
middleFunc = replaceAll(middleFunc, "[valueOut]", param.valueOut) // # 输出参数定义
|
||||
} else {
|
||||
middleFunc = replaceAll(middleFunc, "[valueOut]", "\n " + param.valueOut) // # 输出参数定义
|
||||
}
|
||||
if (param.valueCheckout == "") {
|
||||
middleFunc = replaceAll(middleFunc, "[valueCheckout]", param.valueCheckout) // # 输入参数解析
|
||||
} else {
|
||||
param.valueCheckout = removeEndlineEnter(param.valueCheckout)
|
||||
middleFunc = replaceAll(middleFunc, "[valueCheckout]", param.valueCheckout) // # 输入参数解析
|
||||
}
|
||||
middleFunc = replaceValueOut(param, middleFunc)
|
||||
middleFunc = replaceValueCheckout(param, middleFunc)
|
||||
|
||||
let callFunc = "%s%s(%s);".format(className == null ? "" : "pInstance->", func.name, param.valueFill)
|
||||
middleFunc = replaceAll(middleFunc, "[callFunc]", callFunc) // 执行
|
||||
let optionalCallback = getOptionalCallbackInit(param)
|
||||
@ -123,14 +253,31 @@ function generateFunctionSync(func, data, className) {
|
||||
middleFunc = replaceAll(middleFunc, "[optionalParamDestory]", param.optionalParamDestory) // 可选参数内存释放
|
||||
middleFunc = middleFunc.replaceAll("[callback_param_offset]", param.callback.offset); // 呼叫回调
|
||||
|
||||
// callback返回值处理,回调成功后根据js返回值,业务进行后续处理
|
||||
let callBackReturnProc = callBackReturnValJs2C(func.name, param.callback.returnType)
|
||||
middleFunc = middleFunc.replaceAll("[cbRetValJs2C]", callBackReturnProc);
|
||||
|
||||
// 同步函数返回值处理
|
||||
let retTrans = returnProcRetC2Js(func.ret)
|
||||
middleFunc = middleFunc.replaceAll("[funcRetC2Js]", retTrans);
|
||||
|
||||
let prefixArr = getPrefix(data, func)
|
||||
let implH = ""
|
||||
let implCpp = ""
|
||||
|
||||
// getImplHAndCpp(className, param, func, implH, implCpp, prefixArr)
|
||||
if (!func.isParentMember) {
|
||||
// 只有类/接口自己的成员方法需要在.h.cpp中生成,父类/父接口不需要
|
||||
implH = "\n%s%s%sbool %s(%s)%s;".format(
|
||||
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.valueDefine, prefixArr[3])
|
||||
implCpp = cppTemplate.format(className == null ? "" : className + "::", func.name, param.valueDefine)
|
||||
|
||||
if (param.callback.returnType != 'void' && param.callback.returnType != undefined) {
|
||||
implH += "\n%s%s%sbool %sReturn(%s)%s;".format(
|
||||
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.cbRetvalueDefine, prefixArr[3])
|
||||
implCpp += cppFuncReturnTemplate.format(className == null ? "" : className + "::",
|
||||
func.name, param.cbRetvalueDefine)
|
||||
}
|
||||
}
|
||||
return [middleFunc, implH, implCpp]
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
*/
|
||||
const { InterfaceList, getArrayType, getArrayTypeTwo, NumberIncrease,
|
||||
enumIndex, isEnum, EnumValueType, getMapType,
|
||||
EnumList, getUnionType, TypeList, CallFunctionList } = require("../tools/common");
|
||||
EnumList, getUnionType, TypeList, CallFunctionList, isFuncType, isArrowFunc } = require("../tools/common");
|
||||
const re = require("../tools/re");
|
||||
const { NapiLog } = require("../tools/NapiLog");
|
||||
const { getConstNum } = require("../tools/tool");
|
||||
@ -761,36 +761,75 @@ function mapArray(mapType, napiVn, dest, lt) {
|
||||
return mapTemplete
|
||||
}
|
||||
|
||||
function paramGenerateCallBack(data, funcValue, param, p, isArrowType) {
|
||||
let type = funcValue.type
|
||||
let regType
|
||||
if (isArrowType) {
|
||||
regType = type;
|
||||
}
|
||||
if (isFuncType(type)) {
|
||||
regType = 'void';
|
||||
function getCBparaTypeForArrow(type) {
|
||||
let cbParamType
|
||||
const typeSplits = type.split("=>", 2);
|
||||
let callbackParams = typeSplits[0];
|
||||
let returnType = typeSplits[1];
|
||||
callbackParams = callbackParams.substring(1, callbackParams.length-1); // 去掉参数列表两侧的小括号
|
||||
if (callbackParams.length <= 0) { //无参
|
||||
cbParamType = 'void';
|
||||
}
|
||||
|
||||
if (callbackParams.indexOf(",") >= 0) { // 多个参数,进行分割
|
||||
callbackParams = callbackParams.split(",")
|
||||
for(let i=0; i<callbackParams.length; i++) {
|
||||
NapiLog.logInfo("muilti paramets")
|
||||
}
|
||||
} else { // 一个参数
|
||||
let params = callbackParams.split(':', 2);
|
||||
cbParamType = params[1]
|
||||
}
|
||||
return [cbParamType, returnType]
|
||||
}
|
||||
|
||||
function matchCBParamType(cbParamType, type) {
|
||||
let arrayType = re.match("(Async)*Callback<(Array<([a-zA-Z_0-9]+)>)>", type)
|
||||
if (arrayType) {
|
||||
regType = re.getReg(type, arrayType.regs[2])
|
||||
cbParamType = re.getReg(type, arrayType.regs[2])
|
||||
}
|
||||
|
||||
let arrayType2 = re.match("(Async)*Callback<(([a-zA-Z_0-9]+)\\[\\])>", type)
|
||||
if (arrayType2) {
|
||||
regType = re.getReg(type, arrayType2.regs[2])
|
||||
cbParamType = re.getReg(type, arrayType2.regs[2])
|
||||
}
|
||||
|
||||
let tt = re.match("(Async)*Callback<([a-zA-Z_0-9]+)>", type)
|
||||
if (tt) {
|
||||
regType = re.getReg(type, tt.regs[2])
|
||||
cbParamType = re.getReg(type, tt.regs[2])
|
||||
}
|
||||
if (isEnum(regType, data)) {
|
||||
let index = enumIndex(regType, data)
|
||||
return cbParamType
|
||||
}
|
||||
function paramGenerateCallBack(data, funcValue, param, p, isArrowFuncFlag = false) {
|
||||
let cbParamType
|
||||
let returnType = 'void'
|
||||
|
||||
let type = funcValue.type
|
||||
|
||||
if (isArrowFuncFlag) {
|
||||
cbParamType = type;
|
||||
}
|
||||
|
||||
if (isArrowFunc(type)) {
|
||||
if (CallFunctionList.getValue(type)) {
|
||||
// callFunction => 函数参数处理
|
||||
let funcBody = CallFunctionList.getValue(type)[0] // 取出回调方法参数
|
||||
cbParamType = funcBody[0].type
|
||||
returnType = CallFunctionList.getValue(type)[1]
|
||||
}
|
||||
}
|
||||
|
||||
if (isFuncType(type)) {
|
||||
cbParamType = 'void';
|
||||
}
|
||||
cbParamType = matchCBParamType(cbParamType, type)
|
||||
|
||||
if (isEnum(cbParamType, data)) {
|
||||
let index = enumIndex(cbParamType, data)
|
||||
if (data.enum[index].body.enumValueType == EnumValueType.ENUM_VALUE_TYPE_NUMBER) {
|
||||
regType = "NUMBER_TYPE_" + NumberIncrease.getAndIncrease()
|
||||
cbParamType = "NUMBER_TYPE_" + NumberIncrease.getAndIncrease()
|
||||
} else if (data.enum[index].body.enumValueType == EnumValueType.ENUM_VALUE_TYPE_STRING) {
|
||||
regType = "string"
|
||||
cbParamType = "string"
|
||||
} else {
|
||||
NapiLog.logError(`paramGenerate is not support`);
|
||||
return
|
||||
@ -798,10 +837,10 @@ function paramGenerateCallBack(data, funcValue, param, p, isArrowType) {
|
||||
}
|
||||
|
||||
let paramCallback = {
|
||||
|
||||
// function类型参数,按照空参数、空返回值回调处理 () => void {}
|
||||
type: regType,
|
||||
// function类型参数,按照空参数、空返回值回调处理 () => void {}
|
||||
type: cbParamType,
|
||||
offset: p,
|
||||
returnType: returnType,
|
||||
optional: funcValue.optional,
|
||||
isAsync: type.indexOf("AsyncCallback") >= 0
|
||||
}
|
||||
@ -986,18 +1025,16 @@ function paramGenerateObject(p, funcValue, param) {
|
||||
: "", arrayType, modifiers, name)
|
||||
}
|
||||
|
||||
function isFuncType(type) {
|
||||
let isFunction = false;
|
||||
if (type === null || type === undefined) {
|
||||
return isFunction;
|
||||
function isCallbackFunc(type) {
|
||||
let callbackFunc = false;
|
||||
if (type.substring(0, 9) == "Callback<" ||
|
||||
type.substring(0, 14) == "AsyncCallback<" ||
|
||||
isFuncType(type) || isArrowFunc(type)) {
|
||||
callbackFunc = true;
|
||||
}
|
||||
|
||||
if (type === 'function' || type === 'Function') {
|
||||
isFunction = true;
|
||||
return isFunction;
|
||||
}
|
||||
|
||||
return callbackFunc;
|
||||
}
|
||||
|
||||
// 函数的参数处理
|
||||
function paramGenerate(p, funcValue, param, data) {
|
||||
let type = funcValue.type
|
||||
@ -1019,7 +1056,7 @@ function paramGenerate(p, funcValue, param, data) {
|
||||
else if (TypeList.getValue(type)) {
|
||||
paramGenerateCommon(p, funcValue.type, funcValue, param, modifiers, inParamName)
|
||||
}
|
||||
else if (type.substring(0, 9) == "Callback<" || type.substring(0, 14) == "AsyncCallback<" || isFuncType(type)) {
|
||||
else if (isCallbackFunc(type)) {
|
||||
paramGenerateCallBack(data, funcValue, param, p)
|
||||
}
|
||||
else if (type == "boolean") {
|
||||
|
@ -92,8 +92,24 @@ function cToJsForInterface(value, type, dest, deep) {
|
||||
return result
|
||||
}
|
||||
|
||||
function c2JsForEnum(deep, type, value, dest, propertyName) {
|
||||
let lt = deep
|
||||
let result = ""
|
||||
let ifl = EnumList.getValue(type)
|
||||
let type2 = ifl[0].type
|
||||
let enumCtoJsStr = cToJs("enumInt%d".format(lt), type2, "tnv%d".format(lt), deep + 1)
|
||||
result += "{\nnapi_value tnv%d = nullptr;\n".format(lt) + "int enumInt%d = (int)(%s);\n".format(lt, value) +
|
||||
enumCtoJsStr + `\npxt->SetValueProperty(%s, "%s", tnv%d);\n}\n`
|
||||
.format(dest, propertyName, lt)
|
||||
return result
|
||||
}
|
||||
|
||||
function cToJs(value, type, dest, deep = 1, optional) {
|
||||
var propertyName = delPrefix(value);
|
||||
if (type == null || type == undefined) {
|
||||
NapiLog.logError("type is invalid!")
|
||||
return
|
||||
}
|
||||
if (type.indexOf("|") >= 0) {
|
||||
return unionTempleteFunc(value, type, dest, optional);
|
||||
} else if (type == "void")
|
||||
@ -109,15 +125,7 @@ function cToJs(value, type, dest, deep = 1, optional) {
|
||||
return cToJsForType(value, type, dest, deep);
|
||||
}
|
||||
else if(EnumList.getValue(type)){
|
||||
let lt = deep
|
||||
let result = ""
|
||||
let ifl = EnumList.getValue(type)
|
||||
let type2 = ifl[0].type
|
||||
let enumCtoJsStr = cToJs("enumInt%d".format(lt), type2, "tnv%d".format(lt), deep + 1)
|
||||
result += "{\nnapi_value tnv%d = nullptr;\n".format(lt) + "int enumInt%d = (int)(%s);\n".format(lt, value) +
|
||||
enumCtoJsStr + `\npxt->SetValueProperty(%s, "%s", tnv%d);\n}\n`
|
||||
.format(dest, propertyName, lt)
|
||||
return result
|
||||
return c2JsForEnum(deep, type, value, dest, propertyName);
|
||||
}
|
||||
else if (type.substring(0, 6) == "Array<" || type.substring(type.length - 2) == "[]") {
|
||||
let arrayType = checkArrayParamType(type)
|
||||
@ -439,12 +447,12 @@ function returnGenerateMap(returnInfo, param) {
|
||||
|
||||
function returnGenerateUnion (param) {
|
||||
param.valueOut = `std::any out;
|
||||
std::string out_type;`
|
||||
std::string out_type;\n`
|
||||
param.valueDefine += "%sstd::any &out".format(param.valueDefine.length > 0 ? ", " : "")
|
||||
}
|
||||
|
||||
function returnGenerateObject(returnInfo, param, data) {
|
||||
param.valueOut = `std::map<std::string, std::any> out;`
|
||||
param.valueOut = `std::map<std::string, std::any> out;\n`
|
||||
param.valueDefine += "%sstd::map<std::string, std::any> &out".format(param.valueDefine.length > 0 ? ", " : "")
|
||||
|
||||
}
|
||||
@ -509,7 +517,7 @@ function generateOptionalAndUnion(returnInfo, param, data, outParam, i) {
|
||||
function returnGenerate(returnInfo, param, data, i) {
|
||||
let type = returnInfo.type
|
||||
if (type === undefined) {
|
||||
NapiLog.logError("returnGenerate: type of returnInfo is undefined!");
|
||||
NapiLog.logError("returnGenerate: type of %s is undefined!".format(returnInfo));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -520,14 +528,14 @@ function returnGenerate(returnInfo, param, data, i) {
|
||||
generateOptionalAndUnion(returnInfo, param, data, outParam, i);
|
||||
|
||||
if (type == "string") {
|
||||
param.valueOut = returnInfo.optional ? "std::string* out = nullptr;" : "std::string out;"
|
||||
param.valueOut = returnInfo.optional ? "std::string* out = nullptr;" : "std::string out;\n"
|
||||
param.valueDefine += "%sstd::string%s out".format(param.valueDefine.length > 0 ? ", " : "", modifiers)
|
||||
}
|
||||
else if (type == "void") {
|
||||
NapiLog.logInfo("The current void type don't need generate");
|
||||
}
|
||||
else if (type == "boolean") {
|
||||
param.valueOut = returnInfo.optional ? "bool* out = nullptr;" : "bool out;"
|
||||
param.valueOut = returnInfo.optional ? "bool* out = nullptr;" : "bool out;\n"
|
||||
param.valueDefine += "%sbool%s out".format(param.valueDefine.length > 0 ? ", " : "", modifiers)
|
||||
}
|
||||
else if (isEnum(type, data)) {
|
||||
@ -537,7 +545,7 @@ function returnGenerate(returnInfo, param, data, i) {
|
||||
returnGenerate2(returnInfo, param, data)
|
||||
}
|
||||
else if (type.substring(0, 12) == "NUMBER_TYPE_") {
|
||||
param.valueOut = type + (returnInfo.optional ? "* out = nullptr;" : " out;")
|
||||
param.valueOut = type + (returnInfo.optional ? "* out = nullptr;" : " out;\n")
|
||||
param.valueDefine += "%s%s%s out".format(param.valueDefine.length > 0 ? ", " : "", type, modifiers)
|
||||
}
|
||||
else if (isObjectType(type)) {
|
||||
@ -546,6 +554,7 @@ function returnGenerate(returnInfo, param, data, i) {
|
||||
else {
|
||||
NapiLog.logError("Do not support returning the type [%s].".format(type));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function generateType(type){
|
||||
@ -584,7 +593,7 @@ function returnGenerate2(returnInfo, param, data){
|
||||
|
||||
let flag = InterfaceList.getValue(type) || TypeList.getValue(type)
|
||||
if (flag) {
|
||||
param.valueOut = type + (returnInfo.optional ? "* out = nullptr;" : " out;")
|
||||
param.valueOut = type + (returnInfo.optional ? "* out = nullptr;" : " out;\n")
|
||||
param.valueDefine += "%s%s%s out".format(param.valueDefine.length > 0 ? ", " : "", type, modifiers)
|
||||
} else if (type.substring(0, 6) == "Array<") {
|
||||
returnArrayGen(type, param, returnInfo, modifiers);
|
||||
@ -592,11 +601,11 @@ function returnGenerate2(returnInfo, param, data){
|
||||
let arrayType = getArrayTypeTwo(type)
|
||||
arrayType = jsType2CType(arrayType)
|
||||
if (arrayType == "any") {
|
||||
param.valueOut = `std::any out;
|
||||
param.valueOut = `std::any out;\n
|
||||
std::string out_type;`
|
||||
param.valueDefine += "%sstd::any &out".format(param.valueDefine.length > 0 ? ", " : "")
|
||||
} else {
|
||||
param.valueOut = returnInfo.optional ? "std::vector<%s>* out = nullptr;".format(arrayType)
|
||||
param.valueOut = returnInfo.optional ? "std::vector<%s>* out = nullptr;\n".format(arrayType)
|
||||
: "std::vector<%s> out;".format(arrayType)
|
||||
param.valueDefine += "%sstd::vector<%s>%s out".format(
|
||||
param.valueDefine.length > 0 ? ", " : "", arrayType, modifiers)
|
||||
@ -608,7 +617,7 @@ function returnGenerate2(returnInfo, param, data){
|
||||
std::string out_type;`
|
||||
param.valueDefine += "%sstd::any &out".format(param.valueDefine.length > 0 ? ", " : "")
|
||||
} else if (isObjectType(type)) {
|
||||
param.valueOut = `std::map<std::string, std::any> out;`
|
||||
param.valueOut = `std::map<std::string, std::any> out;\n`
|
||||
param.valueDefine += "%sstd::map<std::string, std::any> &out".format(param.valueDefine.length > 0 ? ", " : "")
|
||||
}
|
||||
}
|
||||
@ -642,11 +651,11 @@ function returnGenerateEnum(data, returnInfo, param) {
|
||||
}
|
||||
param.valuePackage = cToJs("vio->out", type, "result")
|
||||
if (type == "string") {
|
||||
param.valueOut = returnInfo.optional ? "std::string* out = nullptr;" : "std::string out;"
|
||||
param.valueOut = returnInfo.optional ? "std::string* out = nullptr;" : "std::string out;\n"
|
||||
param.valueDefine += "%sstd::string%s out".format(param.valueDefine.length > 0 ? ", " : "", modifiers)
|
||||
}
|
||||
else if (type.substring(0, 12) == "NUMBER_TYPE_") {
|
||||
param.valueOut = type + " out;"
|
||||
param.valueOut = type + " out;\n"
|
||||
param.valueDefine += "%s%s%s out".format(param.valueDefine.length > 0 ? ", " : "", type, modifiers)
|
||||
}
|
||||
}
|
||||
|
@ -308,12 +308,25 @@ function getUnionType(type) {
|
||||
return typeArr
|
||||
}
|
||||
|
||||
function isFuncType(type) {
|
||||
let isFunction = false;
|
||||
if (type === null || type === undefined) {
|
||||
return isFunction;
|
||||
}
|
||||
|
||||
if (type === 'function' || type === 'Function') {
|
||||
isFunction = true;
|
||||
return isFunction;
|
||||
}
|
||||
}
|
||||
|
||||
// 箭头函数,如funTest(cb: (wid: boolean) => void): string;
|
||||
function isArrowFunc(type) {
|
||||
let arrowFunc = false;
|
||||
if (type.indexOf('=>') >= 0) {
|
||||
arrowFunc = true;
|
||||
}
|
||||
return arrowFunc
|
||||
let arrowFunc = false;
|
||||
if (type.indexOf('AUTO_CALLFUNCTION') >= 0 || type.indexOf('=>') > 0) {
|
||||
arrowFunc = true;
|
||||
}
|
||||
return arrowFunc
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
@ -334,5 +347,6 @@ module.exports = {
|
||||
EnumList,
|
||||
jsType2CType,
|
||||
getUnionType,
|
||||
isFuncType,
|
||||
isArrowFunc
|
||||
}
|
9
test/storytest/test_callback/@ohos.test.d.ts
vendored
9
test/storytest/test_callback/@ohos.test.d.ts
vendored
@ -33,14 +33,15 @@ declare namespace napitest {
|
||||
function fun2(cb: Callback<void>): void;
|
||||
function fun3(cb: AsyncCallback<number>): void;
|
||||
function fun4(cb: AsyncCallback<void>): void;
|
||||
function fun5(cb: Callback<number>): string; // to add testcase for return type is not void
|
||||
function fun6(tt: Function): void;
|
||||
function fun7(tt: Function): string;
|
||||
|
||||
// 以下测试用例待支持
|
||||
function fun5(cb: Callback<number>): string; // to add testcase for return type is not void
|
||||
// function fun6(cb: (wid: boolean) => void): string; // to add testcase cb type is arrow function
|
||||
// function fun7(cb: (wid: boolean) => string): string; // to support return type of cbfunction is not void
|
||||
// function fun8(cb: (wid: boolean) => string): string; // to support return type of cbfunction is not void
|
||||
|
||||
function fun8(cb: (wid: boolean) => void): string; // to add testcase cb type is arrow function
|
||||
// function fun9(cb: (wid: boolean, str: string, tc2:number) => string): string;
|
||||
function fun10(cb: (wid: boolean) => string): string; // to support return type of cbfunction is not void
|
||||
// function fun9(cb: (wid: boolean, str: string, tc2:TestClass2) => string): string; // 回调函数参数个数大于1,待支持
|
||||
|
||||
// function registerCbFunc7(cb: Function): void; // 待支持
|
||||
|
Loading…
Reference in New Issue
Block a user