diff --git a/src/gen/generate/interface.js b/src/gen/generate/interface.js index 78129a25f1f5f161f5aa003e623ed78b558532b1..fa37f3314652837b343496de4787b12447b30e27 100644 --- a/src/gen/generate/interface.js +++ b/src/gen/generate/interface.js @@ -55,7 +55,7 @@ function generateVariable(name, type, variable, className) { variable.hDefine += "\n bool %s;".format(name) } else if (type.indexOf("[]") == 0) { variable.hDefine += "\n std::vector<%s> %s;".format(type, name) - } else if (type.indexOf("{") == 0) { + } else if (type.substring(0, 4) == "Map<" || type.indexOf("{") == 0) { variable.hDefine += mapTypeString(type, name) } else diff --git a/src/gen/generate/param_generate.js b/src/gen/generate/param_generate.js index 7ad20e146cc070a6e57eefab5e5062c6ddeca60b..4acb1c95f779e95863635a329b9e30f2fcb8ddd9 100644 --- a/src/gen/generate/param_generate.js +++ b/src/gen/generate/param_generate.js @@ -69,7 +69,7 @@ function jsToC(dest, napiVn, type) { else if (type == "boolean") { return `BOOLEAN_JS_2_C(%s,%s,%s);`.format(napiVn, "bool", dest) } - else if (type.indexOf("{") == 0) { + else if (type.substring(0, 4) == "Map<" || type.indexOf("{") == 0) { return mapTempleteFunc(dest, napiVn, type); } else @@ -347,18 +347,18 @@ function mapArray(mapType, napiVn, dest, lt){ pxt->SwapJs2CUtf8(pxt->GetArrayElement(pxt->GetMapElementValue(%s,tt%d.c_str()),i%d), tt%d); tt%d.push_back(tt%d); }`.format(napiVn, lt, lt, lt+1, napiVn, lt, lt+1, lt+1, lt+1, - lt+1, lt+2, napiVn, lt, lt, lt+2 ,lt+1, lt+2)) + lt+1, lt+2, napiVn, lt, lt+1, lt+2 ,lt+1, lt+2)) } else if(mapType[3] == "boolean"){ mapTemplete = mapTemplete.replaceAll("[replace_swap]", `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%s), tt%d); uint32_t len%s=pxt->GetArrayLength(pxt->GetMapElementValue(%s,tt%d.c_str())); for(uint32_t i%d=0;i%dSwapJs2CBool(pxt->GetArrayElement(pxt->GetMapElementValue(%s,tt%d.c_str()),i%d), tt%d); + bool tt%d; + tt%d = pxt->SwapJs2CBool(pxt->GetArrayElement(pxt->GetMapElementValue(%s,tt%d.c_str()),i%d)); tt%d.push_back(tt%d); }`.format(napiVn, lt, lt, lt+1, napiVn, lt, lt+1, lt+1, lt+1, - lt+1, lt+2, napiVn, lt, lt, lt+2 ,lt+1, lt+2)) + lt+1, lt+2, lt+2, napiVn, lt, lt ,lt+1, lt+2)) } else if(mapType[3].substring(0, 12) == "NUMBER_TYPE_"){ mapTemplete = mapTemplete.replaceAll("[replace_swap]", @@ -410,7 +410,7 @@ function paramGenerate(p, name, type, param, data) { else if (isEnum(type, data)) { paramGenerateEnum(data, type, param, name, p) } - else if (type.indexOf("{") == 0) { + else if (type.substring(0, 4) == "Map<" || type.indexOf("{") == 0) { paramGenerateMap(type, param, p, name) } else { diff --git a/src/gen/generate/return_generate.js b/src/gen/generate/return_generate.js index 3e6398beae733cb6d7989e6c26525f44a5616f2a..ecfad193b4e83ab10e21c00de1e299279a00f6c0 100644 --- a/src/gen/generate/return_generate.js +++ b/src/gen/generate/return_generate.js @@ -87,13 +87,14 @@ function mapTempleteFunc(type, deep, dest, value) { let mapType = getMapType(type) let lt = deep let tnv = dest - let tnvdef = `for (auto i = %s.begin(); i != %s.end(); i++) + let tnvdef = `result = nullptr; + for (auto i = %s.begin(); i != %s.end(); i++) { const char * tnv%d; napi_value tnv%d = nullptr; [calc_out] pxt->SetMapElement(%s, tnv%d, tnv%d); - }`.format(value, value, lt, lt + 1, tnv, lt, lt + 1) + }`.format(value, value, lt, lt + 1, tnv, lt, lt + 2) let ret = "" if (mapType[1] != undefined && mapType[2] == undefined) { ret = mapTempleteValue(mapType, tnvdef, lt, value, tnv) @@ -284,7 +285,34 @@ function returnGenerate(type, param, data) { param.valueOut = type + " out;" param.valueDefine += "%s%s &out".format(param.valueDefine.length > 0 ? ", " : "", type) } - else if (InterfaceList.getValue(type)) { + else if(generateType(type)){ + returnGenerate2(type, param, data) + } + else { + NapiLog.logError("The current version do not support this type return %s`.format(type)"); + } +} + +function generateType(type){ + if (InterfaceList.getValue(type)) { + return true + } + else if (type.substring(0, 6) == "Array<") { + return true + } + else if (type.substring(type.length - 2) == "[]") { + return true + } + else if (type.substring(0, 4) == "Map<" || type.indexOf("{") == 0) { + return true + } + else { + return false + } +} + +function returnGenerate2(type, param, data){ + if (InterfaceList.getValue(type)) { param.valueOut = type + " out;" param.valueDefine += "%s%s &out".format(param.valueDefine.length > 0 ? ", " : "", type) } @@ -300,12 +328,9 @@ function returnGenerate(type, param, data) { param.valueOut = "std::vector<%s> out;".format(arrayType) param.valueDefine += "%sstd::vector<%s> &out".format(param.valueDefine.length > 0 ? ", " : "", arrayType) } - else if (type.substring(0, 1) == "{") { + else if (type.substring(0, 4) == "Map<" || type.indexOf("{") == 0) { returnGenerateMap(type, param) } - else { - NapiLog.logError("The current version do not support this type return %s`.format(type)"); - } } function returnGenerateEnum(data, type, param) { diff --git a/test/storytest/test_map/@ohos.test.d.ts b/test/storytest/test_map/@ohos.test.d.ts index 9eaddd85e1e9ad39d3a576358b4070b8efa096cf..9c98f3b69d019e6d0a01872857d4f6ba2541ee5f 100644 --- a/test/storytest/test_map/@ohos.test.d.ts +++ b/test/storytest/test_map/@ohos.test.d.ts @@ -14,11 +14,16 @@ */ declare namespace napitest { interface TestClass1 { - map1 : {[key: string]: string} ; + map1 : {[key: string]: Array} ; } interface TestClass2 { fun1(v: {[key: string]: string}): number; + fun2(v: {[key: string]: number}): number; + fun3(v: {[key: string]: boolean}): number; + fun4(v: {[key: string]: Array}): number; + fun5(v: {[key: string]: Array}): number; + fun6(v: {[key: string]: Array}): number; } } export default napitest; \ No newline at end of file diff --git a/test/storytest/test_map/test.js b/test/storytest/test_map/test.js index b97c3e0454523ea3323d218853170750e84b3aa5..dd19f368702d51981b8470f2f8b1a5e6d016b82d 100644 --- a/test/storytest/test_map/test.js +++ b/test/storytest/test_map/test.js @@ -23,4 +23,34 @@ describe('Map', function () { assert.strictEqual(ret, 0); }); + it('test TestClass2 fun2', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun2({"age":1234,"name":5678}); + assert.strictEqual(ret, 0); + }); + + it('test TestClass2 fun3', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun3({"age":true,"name":false}); + assert.strictEqual(ret, 0); + }); + + it('test TestClass2 fun4', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun4({"age":["1","12","145"],"name":["21","22","23"]}); + assert.strictEqual(ret, 0); + }); + + it('test TestClass2 fun5', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun5({"age":[1,2,3],"name":[4,5,6]}); + assert.strictEqual(ret, 0); + }); + + it('test TestClass2 fun6', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun6({"age":[true,true,true],"name":[false,false,false]}); + assert.strictEqual(ret, 0); + }); + }); \ No newline at end of file