add interface register feat

Signed-off-by: zhaojunxia <zhaojunxia@kaihong.com>
This commit is contained in:
zhaojunxia 2023-11-13 01:08:34 -08:00
parent 4ccfa5d84b
commit 2d94fdae6a
6 changed files with 82 additions and 11 deletions

View File

@ -39,6 +39,7 @@ napi_value [middleClassName][funcName]_middle(napi_env env, napi_callback_info i
[unwarp_instance]
struct [funcName]_value_struct *vio = new [funcName]_value_struct();
[valueCheckout]
[addListener]
[callFunc]
napi_value result = nullptr;
[valuePackage][optionalParamDestory]
@ -51,6 +52,7 @@ napi_value [middleClassName][funcName]_middle(napi_env env, napi_callback_info i
}`
let cppTemplate = `
%s
bool %s%s(%s)
{
%s
@ -71,12 +73,38 @@ function removeEndlineEnter(value) {
return value
}
function isAddFunc(name) {
let regIndex = name.indexOf('add');
let flag = false
if (regIndex === 0) {
flag = true
}
return flag
}
function generateFunctionDirect(func, data, className, implHVariable) {
let middleFunc = replaceAll(funcDirectTemplete, "[funcName]", func.name)
let middleH = ""
if (func.name != "constructor") {
middleH = replaceAll(funcDirectMiddleHTemplete, "[funcName]", func.name)
}
let addListenerCont = ''
let isAddReg = isAddFunc(func.name)
if (isAddReg) {
addListenerCont = `napi_value para = pxt->GetArgv(XNapiTool::ZERO);
napi_valuetype valueType = napi_undefined;
napi_status status = napi_typeof(env, para, &valueType);
if (status != napi_ok) {
return nullptr;
}
if (valueType != napi_object) {
printf("valueType is Err, not napi_object !");
return nullptr;
}
napi_create_reference(env, para, 1, &NodeISayHelloListener_middle::ref_); `
}
middleFunc = replaceAll(middleFunc, "[addListener]", addListenerCont)
let isClassresult = isClassFunc(className, middleFunc, middleH);
middleFunc = isClassresult[0]
@ -109,6 +137,10 @@ function generateFunctionDirect(func, data, className, implHVariable) {
let prefixArr = getPrefix(data, func)
let implH = ""
let implCpp = ""
let initListener = ''
if (!func.isParentMember) {
if (func.name == 'constructor') {
// 构造函数去掉&或* (在内部去掉较麻烦,生成后统一去除)
@ -119,7 +151,13 @@ function generateFunctionDirect(func, data, className, implHVariable) {
implH = "\n%s%s%sbool %s(%s)%s;".format(
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.valueDefine, prefixArr[3])
let callStatement = jsonCfgList.getValue(className == null? "": className, func.name);
implCpp = cppTemplate.format(className == null ? "" : className + "::", func.name, param.valueDefine,
if (isAddReg) {
initListener = 'NodeISayHelloListener NodeISayHello::listener_ = {};'
callStatement = 'NodeISayHello::listener_ = listener;'
}
implCpp = cppTemplate.format(initListener, className == null ? "" : className + "::", func.name, param.valueDefine,
callStatement == null? "": callStatement)
}
}

View File

@ -35,6 +35,7 @@ let middleHCallbackTemplate = `
* on和off接口生成模板
*/
let funcOnOffTemplete = `
[ref_define]
napi_value [middleClassName][funcName]_middle(napi_env env, napi_callback_info info)
{
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
@ -44,7 +45,8 @@ napi_value [middleClassName][funcName]_middle(napi_env env, napi_callback_info i
return err;
}
struct [funcName]_value_struct *vio = new [funcName]_value_struct();
[getEventName]
[getEventName]
[getRefCallback]
[handleRegist]
napi_value result = pxt->UndefinedValue();
@ -148,7 +150,7 @@ function getregistLine(name) {
} else if (name == 'on') {
registLine = "pxt->RegistOnOffFunc(vio->eventName, pxt->GetArgv(XNapiTool::ONE));"
} else if (isOnObjCallback(name)) {
registLine = "pxt->RegistOnOffFunc(vio->eventName, pxt->GetArgv(XNapiTool::ONE));"
registLine = "pxt->RegistOnOffFunc(vio->eventName, cbFunc);"
// registLine = "pxt->RegistOnOffFunc(vio->eventName, %s);".format(name)
}else { // off/unRegister处理
registLine = "pxt->UnregistOnOffFunc(vio->eventName);"
@ -172,7 +174,9 @@ function gennerateOnOffContext(codeContext, func, data, className, param) {
let getEventName = ''
let registLine = getregistLine(func.name)
let onObjFlag = isOnObjCallback(func.name)
let refCb = ''
let refDefineCont = ''
if (isRegister || isUnRegister) {
let prefix = getPrefix(isRegister)
param.eventName = func.name.replaceAll(prefix, "") // 去掉注册、注销关键字前缀
@ -180,6 +184,18 @@ function gennerateOnOffContext(codeContext, func, data, className, param) {
} else if (onObjFlag) {
param.eventName = className + '_' +func.name
getEventName = 'vio->eventName = "%s";\n'.format(param.eventName)
refDefineCont = 'napi_ref NodeISayHelloListener_middle::ref_ = nullptr;'
refCb = ` std::string proName = "onSayHelloStart";
bool hasProperty = false;
napi_value cbFunc = nullptr;
napi_value callbackObj = pxt->GetArgv(XNapiTool::ZERO);
napi_has_named_property(env, callbackObj, proName.c_str(), &hasProperty);
if (hasProperty) {
napi_value propKey = nullptr;
napi_create_string_utf8(env, proName.c_str(), proName.length(), &propKey);
napi_get_property(env, callbackObj, propKey, &cbFunc);
}`
} else {
getEventName = 'pxt->SwapJs2CUtf8(pxt->GetArgv(XNapiTool::ZERO), vio->eventName);\n'
}
@ -189,7 +205,7 @@ function gennerateOnOffContext(codeContext, func, data, className, param) {
codeContext.middleH = replaceAll(middleHOnOffTemplate, "[funcName]", func.name)
}
codeContext.middleFunc = codeContext.middleFunc.replaceAll("[getEventName]", getEventName)
codeContext.middleFunc = codeContext.middleFunc.replaceAll("[ref_define]", refDefineCont)
let middleClassName = ""
if (className == null) {
codeContext.middleH = codeContext.middleH.replaceAll("[static_define]", "")
@ -202,7 +218,8 @@ function gennerateOnOffContext(codeContext, func, data, className, param) {
}
let instancePtr = "%s".format(className == null ? "" : "pInstance->")
codeContext.middleFunc = replaceAll(codeContext.middleFunc, "[instance]", instancePtr) //执行
codeContext.middleFunc = replaceAll(codeContext.middleFunc, "[getRefCallback]", refCb)
codeContext.middleFunc = replaceAll(codeContext.middleFunc, "[handleRegist]", registLine) //注册/去注册event
if (isRegister) {

View File

@ -32,6 +32,7 @@ public:
static napi_value constructor(napi_env env, napi_callback_info info);
static void release(DataPtr p);
[static_funcs]
static napi_ref ref_;
};
`
@ -297,8 +298,8 @@ function generateInterface(name, data, inNamespace) {
let result = {
implH: `
class %s%s {
public:%s
};\n`.format(name, extendsStr, implH),
public:%s\n
static NodeISayHelloListener listener_;\n};\n`.format(name, extendsStr, implH),
implCpp: implCpp,
middleBody: middleBodyTmplete.replaceAll("[className]", name).replaceAll("[static_funcs]", middleFunc)
.replaceAll("[getConstructorParam]", getConParam)

View File

@ -1121,6 +1121,8 @@ function eventParamGenerate(p, funcValue, param, data) {
// callFunction => 函数参数处理
let onFlag = true;
paramGenerateArrowCallBack(funcValue, param, p, onFlag)
} else if (InterfaceList.getValue(type)) {
let aaOnFlag = true;
}
else if (regName) {
// event type参数处理

21
test/storytest/test_on/@ohos.test.d.ts vendored Normal file → Executable file
View File

@ -104,13 +104,26 @@ declare namespace napitest {
function registerNamespacefunc24(cb : (wid: ModelEvent) => string);
function unRegisterNamespacefunc24(cb : (wid: ModelEvent) => string);
export class NodeISayHelloListener
export class NodeISayHello
{
onSayHelloStart(info: SayInfo);
// registerSayHelloEnd(info: SayInfo);
addSayHelloListener(listener: NodeISayHelloListener);
sayHello(from: string, to: string, sayType: SayType);
}
// function registerSayHelloListener(listener: NodeISayHelloListener);
export class NodeISayHelloListener
{
onSayHelloStart(info: SayInfo);
}
export enum SayType
{
/** 0 主动说话 */
kInitiative,
/** 1 回应对方 */
kResponse,
}
interface TestClass12 {
registerTestfunc14(cb: (wid: SayInfo, test: TestOptional) => void);

0
test/storytest/test_on/test.js Normal file → Executable file
View File