modify ObjCallback

Signed-off-by: zhaojunxia <zhaojunxia@kaihong.com>
This commit is contained in:
zhaojunxia 2023-11-08 23:50:37 -08:00
commit 2cfb5ac33f
8 changed files with 136 additions and 28 deletions

View File

@ -205,14 +205,14 @@ function analyzeFunction(data, isStatic, name, values, ret, results, interfaceNa
let funcType
let callbackFunc = null
if (res) {
tmp = analyzeParams(name, res.values)
tmp = analyzeParams(name, res.values, results)
values = tmp[0]
funcType = tmp[1]
if (tmp[2][0] != undefined) {
callbackFunc = tmp[2][0] // 当方法的参数是回调方法,并且回调方法写法为=>函数时s
}
// callbackFunc = tmp[2] // 当方法的参数是回调方法,并且回调方法写法为=>函数
if (results != undefined && callbackFunc) {
if (results != undefined && callbackFunc != null) {
results.callFunction.push(callbackFunc)
}
}

View File

@ -14,8 +14,9 @@
*/
const re = require("../tools/re");
const { checkOutBody, print } = require("../tools/tool");
const { FuncType, NumberIncrease,isFuncType, isArrowFunc } = require("../tools/common");
const { FuncType, InterfaceList, NumberIncrease,isFuncType, isArrowFunc,isRegisterFunc } = require("../tools/common");
const { NapiLog } = require("../tools/NapiLog");
const { interfaces } = require("mocha");
function isSyncFuncType(type, funcType) {
let isSync = false;
@ -38,7 +39,7 @@ function analyzeCallbackFunction(valueType, valueName, rsltCallFunction) {
valueType = re.replaceAll(valueType, ' ', '')
}
let matchs = re.match("\\(([a-zA-Z_0-9:,]+)*\\)=>([a-zA-Z_0-9]+)", valueType)
if (matchs) {
let number = NumberIncrease.getAndIncrease();
let functionTypeName = 'AUTO_CALLFUNCTION_%s_%s'.format(valueName, number)
@ -65,9 +66,41 @@ function analyzeCallbackFunction(valueType, valueName, rsltCallFunction) {
return valueType
}
function analyzeInerfaceCallbackFunction(functionTypeName, functionRet, functionBody, rsltCallFunction) {
// let tmp = analyzeParams(functionTypeName, functionBody)
// let bodyRes = tmp[0]
rsltCallFunction.push({
"name": functionTypeName,
"body": functionBody,
"ret": functionRet // 返回值
})
let valueType = functionTypeName
return valueType
}
// 判断 注册的 type 的是否为interface
function getCallbackInterface(type, results) {
let allInterfaces = results.interface
if (allInterfaces === null || allInterfaces === undefined || results.interface === null) {
NapiLog.logError("Invalid param: allInterfaces")
return;
}
for (let i in allInterfaces) {
if (allInterfaces[i].name == type) {
return allInterfaces[i];
}
}
return null;
}
/**函数参数解析 */
function analyzeParams(funcName, values) {
let result = []
function analyzeParams(funcName, values, results = null) {
let result = []
let rsltCallFunction = []
let funcType = FuncType.DIRECT
let optionalParamCount = 0; // 可选参数的个数
@ -90,7 +123,33 @@ function analyzeParams(funcName, values) {
}
let valueName = re.getReg(v, matchs.regs[1])
type = analyzeCallbackFunction(type, valueName, rsltCallFunction)
let isRegister = isRegisterFunc(funcName);
if (isRegister) {
// 判断 注册的 type 的是否为interface
let inter = getCallbackInterface(type, results)
if (inter != null) {
for(let i=0; i<inter.body.function.length; i++) {
let cbFunc = inter.body.function[i];
let cbTypeName = "AUTO_CALLFUNCTION_" + inter.name + "_" + cbFunc.name;
// 对于包含回调函数集合的interface、class,根据此标记后续不需要进行转换
inter.isCallbackObj = true;
// 对于包含回调函数集合的interface、class,根据此标记直接解析,
// 后续无需对interface、class中的回调进行转换
inter.body.function[i].isObjCbFuncs = true
inter.body.function[i].cbTypeName = cbTypeName
analyzeInerfaceCallbackFunction(cbTypeName, cbFunc.ret, cbFunc.value, rsltCallFunction)
}
} else {
type = analyzeCallbackFunction(type, valueName, rsltCallFunction)
}
} else {
type = analyzeCallbackFunction(type, valueName, rsltCallFunction)
}
let optionalFlag = re.getReg(v, matchs.regs[2]) == '?' ? true : false;
let checkParamOk = true;
if (optionalFlag) {

View File

@ -141,11 +141,11 @@ function addOnOffFunc(data, funcName) {
data.onOffList.push(funcName)
}
function getregistLine(name) {
function getregistLine(func) {
let registLine = ''
if (isRegisterFunc(name)) {
if (isRegisterFunc(func.name)) {
registLine = "pxt->RegistOnOffFunc(vio->eventName, pxt->GetArgv(XNapiTool::ZERO));"
} else if (name == 'on') {
} else if (func.name == 'on') {
registLine = "pxt->RegistOnOffFunc(vio->eventName, pxt->GetArgv(XNapiTool::ONE));"
} else if (isOnObjCallback(name)) {
registLine = "pxt->RegistOnOffFunc(vio->eventName, pxt->GetArgv(XNapiTool::ONE));"
@ -254,7 +254,7 @@ function gennerateEventCallback(codeContext, data, param, className = null, isOn
if (param.params === '') {
callbackFunc = replaceAll(callbackFunc, "&eventName, ", "&eventName")
}
if (param.callback.isArrowFuncFlag) { // 回调是箭头函数
if (param.callback != null && param.callback.isArrowFuncFlag != undefined && param.callback.isArrowFuncFlag) { // 回调是箭头函数
callbackFunc = getArrowCallbackC2JsParam(callbackFunc, param);
} else { // 回调是普通callback
callbackFunc = getCallbackC2JsParam(callbackFunc, param);

View File

@ -377,6 +377,15 @@ function addVirtualKeywords(data, implH, name) {
return implH;
}
function isOnOffReisterFunc(func) {
let flag = false;
if (func.name == 'on' || func.name == 'off' || isRegisterFunc(func.name) || isUnRegisterFunc(func.name) ||
func.isObjCbFuncs) {
flag = true;
}
return flag;
}
function connectResult(data, inNamespace, name) {
let implH = ""
let implCpp = ""

View File

@ -18,6 +18,7 @@ const { InterfaceList, getArrayType, getArrayTypeTwo, NumberIncrease,
const re = require("../tools/re");
const { NapiLog } = require("../tools/NapiLog");
const { getConstNum } = require("../tools/tool");
const { Interface } = require("readline/promises");
class LenIncrease { }
LenIncrease.LEN_TO = 1;
@ -868,20 +869,6 @@ function paramGenerateArrowCallBack(funcValue, param, p, onFlag = false) {
}
}
// function paramGenerateOnObjCallBack(funcValue, param, p, onFlag = false) {
// param.callback = {
// // function类型参数按照空参数、空返回值回调处理 () => void {}
// type: cbParamType,
// offset: p,
// returnType: returnType,
// optional: funcValue.optional,
// isArrowFuncFlag: true,
// arrowFuncParamList:cbParamList,
// onFlag: onFlag,
// isAsync: type.indexOf("AsyncCallback") >= 0
// }
// }
function isArrayType(type) {
if (type.substring(type.length - 2) == "[]" || type.substring(0, 6) == "Array<") {
return true;
@ -1125,6 +1112,12 @@ function eventParamGenerate(p, funcValue, param, data) {
type = type.replaceAll("'", "")
}
let inerBody = InterfaceList.getBody(type)
if (inerBody != null && inerBody.isCallbackObj) {
NapiLog.logInfo("no need to trans!");
return
}
let regName = re.match("([a-zA-Z_0-9]+)", type)
if(isFuncType(type)) {
paramGenerateCallBack(data, funcValue, param, p)
@ -1135,7 +1128,15 @@ function eventParamGenerate(p, funcValue, param, data) {
// callFunction => 函数参数处理
let onFlag = true;
paramGenerateArrowCallBack(funcValue, param, p, onFlag)
} else if (regName) {
}
// else if (funcs != null) {
// // 标记interface中的接口走注册流程不需要进行普通napi接口转换
// for (let i in funcs) {
// let onFlag = true;
// paramGenerateArrowCallBack(funcs[i], param, p, onFlag)
// }
// }
else if (regName) {
// event type参数处理
param.eventName = re.getReg(type, regName.regs[1]) // string类型如何处理
if (param.eventName == "string") {

View File

@ -478,7 +478,7 @@ function getReturnFill(returnInfo, param) {
}
if (type != "void") {
// callback<xxx> 中的xxx不是void时生成的capp代码才需要用户填充out参数
// callback<xxx> 中的xxx不是void时生成的cpp代码才需要用户填充out参数
if (param.callback.isArrowFuncFlag) {
valueFillStr += "%svio->%s".format(valueFillStr.length > 0 ? ", " : "", returnInfo.name)
} else {
@ -611,6 +611,10 @@ function returnGenerateForOnOffMultiPara(paramInfo, param, data) {
}
function returnGenerate(returnInfo, param, data, isOnFuncFlag = false) {
if (returnInfo === undefined) {
NapiLog.logError("returnGenerate: returnInfo is undefined!");
return;
}
let type = returnInfo.type
if (type === undefined) {
NapiLog.logError("returnGenerate: type of %s is undefined!".format(returnInfo));

View File

@ -86,6 +86,32 @@ InterfaceList.getValue = function (name) {
return null;
}
InterfaceList.getBody = function (name) {
let ifs = InterfaceList.interfacess_[InterfaceList.interfacess_.length - 1]
for (let i in ifs) {
if (ifs[i].name == name) {
return ifs[i].body;
}
}
return null;
}
InterfaceList.getFuncs = function (name) {
let ifs = InterfaceList.interfacess_[InterfaceList.interfacess_.length - 1]
for (let i in ifs) {
let vv = ifs[i];
if (ifs[i].name == name) {
var hasProperty = Object.prototype.hasOwnProperty.call(ifs[i].body, "allProperties")
if (hasProperty) {
return ifs[i].body.allProperties.functions;
} else {
return ifs[i].body.function;
}
}
}
return null;
}
class CallFunctionList { }
CallFunctionList.callFuncs = [];
CallFunctionList.push = function (ifs) {

View File

@ -98,8 +98,12 @@ declare namespace napitest {
function unRegisterNamespacefunc21(cb : (wid: number) => string);
function registerNamespacefunc22(cb : Callback<boolean>);
function unRegisterNamespacefunc22(cb : Callback<boolean>);
function unRegisterNamespacefunc22(cb : Callback<boolean>);
function registerNamespacefunc23(cb : (wid: ModelEvent) => string);
function unRegisterNamespacefunc23(cb : (wid: ModelEvent) => string);
export class NodeISayHelloListener
{
onSayHelloStart(info: SayInfo);
@ -108,6 +112,7 @@ declare namespace napitest {
// function registerSayHelloListener(listener: NodeISayHelloListener);
export type SayInfo =
{
from: string;
@ -118,6 +123,10 @@ declare namespace napitest {
saidTime: string;
isEnd: boolean;
}
// function registerSayHelloStart(info: SayInfo);
// function registerNamespacefunc25(cb : onSayHelloStart);
// function registerNamespacefunc24(cb : NodeISayHelloListener);
}
export default napitest;