Merge branch 'master' of gitee.com:openharmony/napi_generator into master

Signed-off-by: zhaojunxia <zhaojunxia@kaihong.com>
This commit is contained in:
zhaojunxia 2023-11-09 07:54:29 +00:00 committed by Gitee
commit caa6999216
7 changed files with 187 additions and 46 deletions

View File

@ -96,8 +96,9 @@ function generateClass(name, data, inNamespace, functiontType) {
selfNs = ", " + nsl[nsl.length - 1]
}
}
middleInit += `\n pxt->DefineClass("%s", %s%s_middle::constructor, valueList, funcList%s);\n}\n`
.format(name, inNamespace, name, selfNs)
let toolNamespace = getToolNamespace(inNamespace)
middleInit += `\n pxt->DefineClass("%s", %s%s%s_middle::constructor, valueList, funcList%s);\n}\n`
.format(name, inNamespace, toolNamespace, name, selfNs)
let result = {
implH: `
class %s {
@ -123,14 +124,15 @@ function connectResult(data, inNamespace, name) {
hDefine: "",
middleValue: "",
}
let toolNamespace = getToolNamespace(inNamespace)
middleInit = `{\n std::map<const char *, std::map<const char *, napi_callback>> valueList;`
for (let i in data.value) {
let v = data.value[i]
generateVariable(v.name, v.type, variable, name)
middleInit += `
valueList["%s"]["getvalue"] = %s%s_middle::getvalue_%s;
valueList["%s"]["setvalue"] = %s%s_middle::setvalue_%s;`
.format(v.name, inNamespace, name, v.name, v.name, inNamespace, name, v.name)
valueList["%s"]["getvalue"] = %s%s%s_middle::getvalue_%s;
valueList["%s"]["setvalue"] = %s%s%s_middle::setvalue_%s;`
.format(v.name, inNamespace, toolNamespace, name, v.name, v.name, inNamespace, toolNamespace, name, v.name)
}
implH += variable.hDefine
middleFunc += variable.middleValue
@ -156,11 +158,29 @@ function connectResult(data, inNamespace, name) {
implH += tmp[1]
implCpp += tmp[2]
middleH += tmp[3]
middleInit += `\n funcList["%s"] = %s%s_middle::%s_middle;`.format(func.name, inNamespace, name, func.name)
middleInit += `\n funcList["%s"] = %s%s%s_middle::%s_middle;`
.format(func.name, inNamespace, toolNamespace, name, func.name)
}
return [middleFunc, implH, implCpp, middleInit, middleH]
}
function getToolNamespace(inNamespace) {
let index = inNamespace.lastIndexOf("::");
let toolNamespace;
if (index > 0) {
let bodyTmp = inNamespace.substring(0, index)
let index2 = bodyTmp.lastIndexOf('::')
if (index2 > 0 && index2 < index) {
toolNamespace = inNamespace.substring(index2 + 2, index) + '_interface::'
} else {
toolNamespace = bodyTmp + "_interface::";
}
} else {
toolNamespace = inNamespace + "_interface::";
}
return toolNamespace;
}
module.exports = {
generateClass
}

View File

@ -288,9 +288,10 @@ function generateInterface(name, data, inNamespace) {
selfNs = ", " + nsl[nsl.length - 1]
}
}
middleInit += `\n pxt->DefineClass("%s", %s%s_middle::constructor,
let toolNamespace = getToolNamespace(inNamespace);
middleInit += `\n pxt->DefineClass("%s", %s%s%s_middle::constructor,
valueList, funcList%s);\n}\n`
.format(name, inNamespace, name, selfNs)
.format(name, inNamespace, toolNamespace, name, selfNs)
let extendsStr = (data.parentNameList && data.parentNameList.length > 0) ?
" : public %s".format(data.parentNameList.join(", public ")) : ""
let result = {
@ -429,30 +430,54 @@ function connectResult(data, inNamespace, name) {
implH += tmp[1]
implCpp += tmp[2]
middleH += tmp[3]
if (func.name != "constructor") {
middleInit += `\n funcList["%s"] = %s%s_middle::%s_middle;`.format(func.name,
inNamespace, name, func.name)
}
middleInit = generateMiddleInitFunc(func, inNamespace, middleInit, name);
}
implH = addVirtualKeywords(data, implH, name);
return [middleFunc, implH, implCpp, middleInit, middleH]
}
function generateMiddleInitFunc(func, inNamespace, middleInit, name) {
if (func.name != "constructor") {
let toolNamespace = getToolNamespace(inNamespace);
middleInit += `\n funcList["%s"] = %s%s%s_middle::%s_middle;`.format(func.name,
inNamespace, toolNamespace, name, func.name);
}
return middleInit;
}
function getMiddleInitFunc(middleInit, data, variable, name, inNamespace) {
middleInit = `{\n std::map<const char *, std::map<const char *, napi_callback>> valueList;`;
data.allProperties = { values: [], functions: [] };
getAllPropties(data, data.allProperties, false);
let toolNamespace = getToolNamespace(inNamespace);
for (let i in data.allProperties.values) {
let v = data.allProperties.values[i];
generateVariable(v, variable, name);
middleInit += `
valueList["%s"]["getvalue"] = %s%s_middle::getvalue_%s;
valueList["%s"]["setvalue"] = %s%s_middle::setvalue_%s;`
.format(v.name, inNamespace, name, v.name, v.name, inNamespace, name, v.name);
valueList["%s"]["getvalue"] = %s%s%s_middle::getvalue_%s;
valueList["%s"]["setvalue"] = %s%s%s_middle::setvalue_%s;`
.format(v.name, inNamespace, toolNamespace, name, v.name, v.name, inNamespace, toolNamespace, name, v.name);
}
return middleInit;
}
function getToolNamespace(inNamespace) {
let index = inNamespace.lastIndexOf("::");
let toolNamespace;
if (index > 0) {
let bodyTmp = inNamespace.substring(0, index)
let index2 = bodyTmp.lastIndexOf('::')
if (index2 > 0 && index2 < index) {
toolNamespace = inNamespace.substring(index2 + 2, index) + '_interface::'
} else {
toolNamespace = bodyTmp + "_interface::";
}
} else {
toolNamespace = inNamespace + "_interface::";
}
return toolNamespace;
}
module.exports = {
generateInterface,
connectResult,

View File

@ -125,6 +125,10 @@ function generateNamespace(name, data, inNamespace = "") {
for (let i in data.function) {
genNamespaceFunc(data, i, namespaceResult, inNamespace, name);
}
let flag = true;
if (data.namespace.length !== 0) {
flag = false;
}
for (let i in data.namespace) {
let ns = data.namespace[i]
let result = generateNamespace(ns.name, ns.body, inNamespace + name + "::")
@ -136,9 +140,10 @@ function generateNamespace(name, data, inNamespace = "") {
CallFunctionList.pop();
if (inNamespace.length > 0) {
namespaceResult.middleInit += "}"
flag = true;
}
return generateResult(name, namespaceResult.implH, namespaceResult.implCpp, namespaceResult.middleFunc,
namespaceResult.middleInit, namespaceResult.middleH)
namespaceResult.middleInit, namespaceResult.middleH, flag)
}
function genNamespaceFunc(data, i, namespaceResult, inNamespace, name) {
@ -148,13 +153,31 @@ function genNamespaceFunc(data, i, namespaceResult, inNamespace, name) {
namespaceResult.implH += tmp[1];
namespaceResult.implCpp += tmp[2];
namespaceResult.middleH += tmp[3];
let middleTmp = ' pxt->DefineFunction("%s", %s%s::%s_middle%s);\n'
.format(func.name, inNamespace, name, func.name, inNamespace.length > 0 ? ", " + name : "");
let toolNamespace = getToolNamespaceFunc(inNamespace, name);
let middleTmp = ' pxt->DefineFunction("%s", %s%s::%s%s_middle%s);\n'
.format(func.name, inNamespace, name, toolNamespace, func.name, inNamespace.length > 0 ? ", " + name : "");
if (namespaceResult.middleInit.indexOf(middleTmp) < 0) { // on方法不需要重复定义
namespaceResult.middleInit += middleTmp;
}
}
function getToolNamespaceFunc(inNamespace, name) {
let toolNamespace;
if (inNamespace != '') {
let index = inNamespace.lastIndexOf('::');
let bodyTmp = inNamespace.substring(0, index);
let index2 = bodyTmp.lastIndexOf('::');
if (index2 > 0 && index2 < index) {
toolNamespace = inNamespace.substring(index2 + 2, index) + '_interface::';
} else {
toolNamespace = name + '_interface::';
}
} else {
toolNamespace = name + '_interface::';
}
return toolNamespace;
}
function enumNamespaceFunction(data, namespaceResult) {
let result = generateEnumResult(data);
namespaceResult.implH += result.implH;
@ -190,15 +213,26 @@ function generateEnumResult(data) {
return resultEnum
}
function generateResult(name, implH, implCpp, middleFunc, middleInit, middleH) {
let result = {
function generateResult(name, implH, implCpp, middleFunc, middleInit, middleH, flag) {
let result
if (flag) {
result = {
implH: `\nnamespace %s {\nnamespace %s_interface {%s\n}\n}`.format(name, name, implH),
implCpp: `\nnamespace %s {\nnamespace %s_interface {%s\n}\n}`.format(name, name, implCpp),
middleBody: `\nnamespace %s {\nnamespace %s_interface {%s\n}\n}`.format(name, name, middleFunc),
middleInit: middleInit,
middleH: `\nnamespace %s {\nnamespace %s_interface {%s\n}\n}`.format(name, name, middleH)
}
} else {
result = {
implH: `\nnamespace %s {%s\n}`.format(name, implH),
implCpp: `\nnamespace %s {%s}`.format(name, implCpp),
middleBody: `\nnamespace %s {%s}`.format(name, middleFunc),
middleInit: middleInit,
middleH: `\nnamespace %s {%s\n}`.format(name, middleH)
}
return result;
}
return result;
}
function generateFunction(func, data) {

View File

@ -45,14 +45,21 @@ function cToJsForType(value, type, dest, deep) {
for (let i in ifl) {
let name2 = ifl[i].name
let type2 = ifl[i].type
let optional2 = ifl[i].optional
let isSubEnum = EnumList.getValue(type2) ? true : false;
let subDest = isSubEnum ? dest : "tnv%d".format(lt)
let typeType = cToJs("%s.%s".format(value, name2), type2, subDest, deep + 1)
let typeType = null
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)
} else {
typeType = cToJs("%s.%s".format(value, name2), type2, subDest, deep + 1)
}
if (isSubEnum) {
result += typeType
} else {
result += "{\nnapi_value tnv%d = nullptr;\n".format(lt) +
result += "%s{\nnapi_value tnv%d = nullptr;\n".format(ifOptional, lt) +
typeType + `\npxt->SetValueProperty(%s, "%s", tnv%d);\n}\n`
.format(dest, name2, lt)
}
@ -74,7 +81,9 @@ function cToJsForInterface(value, type, dest, deep) {
let isSubEnum = EnumList.getValue(type2) ? true : false;
let subDest = isSubEnum ? dest : "tnv%d".format(lt)
let interfaceType = null;
let ifOptional = ''
if (optional2) {
ifOptional = 'if (%s.%s.has_value())\n'.format(value, name2)
interfaceType = cToJs("%s.%s".format(value, "%s.value()".format(name2)), type2, subDest, deep + 1)
} else {
interfaceType = cToJs("%s.%s".format(value, name2), type2, subDest, deep + 1)
@ -84,7 +93,7 @@ function cToJsForInterface(value, type, dest, deep) {
// interface include enum properties
result += interfaceType
} else {
result += "{\nnapi_value tnv%d = nullptr;\n".format(lt) +
result += "%s{\nnapi_value tnv%d = nullptr;\n".format(ifOptional, lt) +
interfaceType + `\npxt->SetValueProperty(%s, "%s", tnv%d);\n}\n`
.format(dest, name2, lt)
}

View File

@ -114,20 +114,24 @@ function removeExplains(data) {
}
// 去除 namespace 域外 // 类型的注释
// 如果换行格式是\r\n, 去除\r, 统一成\n格式
while (data.indexOf("\r") >= 0) {
data = data.replace("\r", "")
}
while (data.indexOf("//") >= 0) {
let i1 = data.indexOf("//")
let i2 = data.indexOf("\r\n")
let i2 = data.indexOf("\n")
let end = data.indexOf("declare namespace ")
while (i2 < end && i1 < end) {
while (i1 > i2) {
data = data.substring(0, i2) + data.substring(i2 + 2, data.length)
i2 = data.indexOf("\r\n")
i2 = data.indexOf("\n")
i1 = data.indexOf("//")
}
data = data.substring(0, i1) + data.substring(i2 + 2, data.length)
data = data.substring(0, i1) + data.substring(i2 + 1, data.length)
i1 = data.indexOf("//")
i2 = data.indexOf("\r\n")
end = data.indexOf("declare namespace ")
i2 = data.indexOf("\n")
end = data.indexOf("declare namespace ")
}
if (i2 > end || i1 > end) {
break;

View File

@ -101,8 +101,8 @@ declare namespace napitest {
function unRegisterNamespacefunc22(cb : Callback<boolean>);
function registerNamespacefunc23(cb : (wid: ModelEvent) => string);
function unRegisterNamespacefunc23(cb : (wid: ModelEvent) => string);
function registerNamespacefunc24(cb : (wid: ModelEvent) => string);
function unRegisterNamespacefunc24(cb : (wid: ModelEvent) => string);
export class NodeISayHelloListener
{
@ -112,21 +112,30 @@ declare namespace napitest {
// function registerSayHelloListener(listener: NodeISayHelloListener);
export type SayInfo =
interface TestClass12 {
registerTestfunc14(cb: (wid: SayInfo, test: TestOptional) => void);
unRegisterTestfunc14(cb: (wid: SayInfo, test: TestOptional) => void);
}
function registerNamespacefunc23(cb: (wid: SayInfo, test: TestOptional) => void);
function unRegisterNamespacefunc23(cb: (wid: SayInfo, test: TestOptional) => void);
export type SayInfo =
{
from: string;
// fromId?: number;
to: string;
// toId?: number;
fromId?: number;
content: string;
saidTime: string;
isEnd: boolean;
saidTime?: string;
isEnd?: boolean;
}
// function registerSayHelloStart(info: SayInfo);
// function registerNamespacefunc25(cb : onSayHelloStart);
// function registerNamespacefunc24(cb : NodeISayHelloListener);
export interface TestOptional
{
v1: string;
v2?: boolean;
v3: number;
v4?: string;
v5?: number;
}
}
export default napitest;

View File

@ -12,8 +12,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { TestClass1, TestClass2, ModelEvent, TestClass3, on, off, TestClass4,
TestClass5, TestClass6, TestClass7, TestClass10, TestClass11 } = require("./out/build/Release/napitest")
const { TestClass1, TestClass2, ModelEvent, TestClass3, on, off, TestClass4, TestClass5,
TestClass6, TestClass7, TestClass10, TestClass11, TestClass12 } = require("./out/build/Release/napitest")
const testObj = require("./out/build/Release/napitest")
var assert = require("assert");
@ -224,7 +224,7 @@ describe('test register/unRegister', function () {
return 'fun10nm'
}
it('test registerNamespacefunc20', function () {
it('test registerNamespacefunc21', function () {
testObj.registerNamespacefunc21(onCallbackfun10nm);
});
@ -248,4 +248,44 @@ describe('test register/unRegister', function () {
// registerTestfunc13(cb : Callback<boolean>);
// unRegisterTestfunc13(cb : Callback<boolean>);
// }
// }
function callbackTest14(ret1, ret2) {
console.info("SayInfo.from = " + ret1.from)
console.info("SayInfo.fromId = " + ret1.fromId)
console.info("SayInfo.content = " + ret1.content)
console.info("SayInfo.saidTime = " + ret1.saidTime)
console.info("SayInfo.isEnd = " + ret1.isEnd)
console.info("TestOptional.v1 = " + ret2.v1)
console.info("TestOptional.v2 = " + ret2.v2)
console.info("TestOptional.v3 = " + ret2.v3)
console.info("TestOptional.v4 = " + ret2.v4)
console.info("TestOptional.v5 = " + ret2.v5)
}
// interface TestClass12 {
// registerTestfunc14(cb: (wid: SayInfo, test: TestOptional) => void);
// unRegisterTestfunc14(cb: (wid: SayInfo, test: TestOptional) => void);
// }
// function registerNamespacefunc23(cb: (wid: SayInfo, test: TestOptional) => void);
// function unRegisterNamespacefunc23(cb: (wid: SayInfo, test: TestOptional) => void);
describe('test register/unRegister callback interface/type param is optional', function () {
let tc12 = new TestClass12();
it('test TestClass12 registerTestfunc14', function () {
tc12.registerTestfunc14(callbackTest14);
});
it('test TestClass12 unRegisterTestfunc14', function () {
tc12.unRegisterTestfunc14(callbackTest14);
});
it('test function registerNamespacefunc23', function () {
testObj.registerNamespacefunc23(callbackTest14);
});
it('test function unRegisterNamespacefunc23', function () {
testObj.unRegisterNamespacefunc23(callbackTest14);
});
});