From 733a831edb5cff0194c9d59911db4f4a98a284f9 Mon Sep 17 00:00:00 2001 From: liuycag Date: Wed, 15 Jun 2022 15:45:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmap=E5=B5=8C=E5=A5=97?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=92=8Cinterface=E7=B1=BB=E5=9E=8B=E7=94=9F?= =?UTF-8?q?=E6=88=90=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuycag --- src/gen/generate/param_generate.js | 110 +++++++++++++++--------- src/gen/generate/return_generate.js | 40 +++++---- src/gen/tools/common.js | 42 ++++----- test/storytest/test_map/@ohos.test.d.ts | 15 +++- test/storytest/test_map/test.js | 27 ++++++ 5 files changed, 151 insertions(+), 83 deletions(-) diff --git a/src/gen/generate/param_generate.js b/src/gen/generate/param_generate.js index 5e384f58..14e4249d 100644 --- a/src/gen/generate/param_generate.js +++ b/src/gen/generate/param_generate.js @@ -165,17 +165,28 @@ function paramGenerateEnum(data, type, param, name, p) { function paramGenerateMap(type, param, p, name) { let mapType = getMapType(type) + let mapTypeString if (mapType[1] != undefined && mapType[2] == undefined) { - let mapTypeString if (mapType[1] == "string") { mapTypeString = "std::string" } else if (mapType[1].substring(0, 12) == "NUMBER_TYPE_") { mapTypeString = mapType[1] } else if (mapType[1] == "boolean") { mapTypeString = "bool" } - param.valueIn += "\n std::map in%d;".format(mapTypeString, p) + else { mapTypeString = mapType[1] } + } + else if (mapType[2] != undefined) { + if (mapType[2] == "string") { mapTypeString = "std::map" } + else if (mapType[2].substring(0, 12) == "NUMBER_TYPE_") { "std::map" } + else if (mapType[2] == "boolean") { mapTypeString = "std::map" } + } + else if (mapType[3] != undefined) { + if (mapType[3] == "string") { mapTypeString = "std::vector" } + else if (mapType[3].substring(0, 12) == "NUMBER_TYPE_") { mapTypeString = "std::vector<"+mapType[3]+">" } + else if (mapType[3] == "boolean") { mapTypeString = "std::vector" } + } + param.valueIn += "\n std::map in%d;".format(mapTypeString, p) param.valueCheckout += jsToC("vio->in" + p, "pxt->GetArgv(%d)".format(p), type) param.valueFill += "%svio->in%d".format(param.valueFill.length > 0 ? ", " : "", p) param.valueDefine += "%sstd::map &%s" .format(param.valueDefine.length > 0 ? ", " : "", mapTypeString, name) - } } function mapTempleteFunc(dest, napiVn, type) { @@ -213,15 +224,15 @@ function mapInterface(mapTypeString, mapTemplete, napiVn, lt) { interfaceVarName += `std::string %dName = "%d";\n`.format(interfaceValue[i].name, interfaceValue[i].name) interfaceVar += `std::string %s;`.format(interfaceValue[i].name) interfaceFun += - "tt%d.%s = pxt->%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(0),tt%d.c_str()),%sName.c_str()),%s);" - .format(lt + 1, interfaceValue[i].name, "SwapJs2CUtf8", "GetMapElementValue", - lt, interfaceValue[i].name, interfaceValue[i].name) + `pxt->%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(0),tt%d.c_str()),%sName.c_str()),tt%d.%s);` + .format("SwapJs2CUtf8", "GetMapElementValue", + lt, interfaceValue[i].name, lt+1, interfaceValue[i].name) } else if (interfaceValue[i].type.substring(0, 12) == "NUMBER_TYPE_") { interfaceVarName += `std::string %dName = "%d";\n`.format(interfaceValue[i].name, interfaceValue[i].name) interfaceVar += `std::string %s;\n`.format(interfaceValue[i].name) interfaceFun += - "%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(0),tt%d.c_str()),%sName.c_str()),%s,tt%d.%s);\n" + `%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(0),tt%d.c_str()),%sName.c_str()),%s,tt%d.%s);\n` .format("NUMBER_JS_2_C", "GetMapElementValue", lt, interfaceValue[i].name, interfaceValue[i].type, lt + 1, interfaceValue[i].name) } @@ -229,9 +240,9 @@ function mapInterface(mapTypeString, mapTemplete, napiVn, lt) { interfaceVarName += `std::string %dName = "%d";\n`.format(interfaceValue[i].name, interfaceValue[i].name) interfaceVar += `std::string %s;\n`.format(interfaceValue[i].name) interfaceFun += - "tt%d.%s = pxt->%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(0),tt%d.c_str()),%sName.c_str()),%s);\n" + `tt%d.%s = pxt->%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(0),tt%d.c_str()),%sName.c_str()));\n` .format(lt + 1, interfaceValue[i].name, "SwapJs2CBool", "GetMapElementValue", - lt, interfaceValue[i].name, interfaceValue[i].name) + lt, interfaceValue[i].name) } } mapTemplete = mapTemplete.replaceAll("[replace_swap]", @@ -284,6 +295,50 @@ for(uint32_t i[replace_lt]=0;i[replace_lt]SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); + uint32_t len%d=pxt->GetMapLength(pxt->GetMapElementValue(%s,tt%d.c_str())); + for(uint32_t i%d=0;i%dSwapJs2CUtf8(pxt->GetMapElementName(pxt->GetMapElementValue(%s,tt%d.c_str()), i%d),tt%d); + pxt->SwapJs2CUtf8(pxt->GetMapElementValue(pxt->GetMapElementValue(%s,tt%d.c_str()),tt%d.c_str()),tt%d); + tt%d.insert(std::make_pair(tt%d, tt%d)); + }`.format(napiVn, lt, lt, lt + 1, napiVn, lt, lt + 1, lt + 1, lt + 1, lt + 1, + lt + 2, lt + 3, napiVn, lt, lt + 1, lt + 2, napiVn, lt, lt + 2, lt + 3, lt + 1, lt + 2, lt + 3)) +} + +function mapMapBoolean (mapTemplete, napiVn, lt) { + return mapTemplete.replaceAll("[replace_swap]", + `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); + uint32_t len%d=pxt->GetMapLength(pxt->GetMapElementValue(%s,tt%d.c_str())); + for(uint32_t i%d=0;i%dSwapJs2CUtf8(pxt->GetMapElementName(pxt->GetMapElementValue(%s,tt%d.c_str()), i%d),tt%d); + tt%d = pxt->%s(pxt->GetMapElementValue(pxt->GetMapElementValue(%s,tt%d.c_str()),tt%d.c_str())); + tt%d.insert(std::make_pair(tt%d, tt%d)); + }`.format(napiVn, lt, lt, lt + 1, napiVn, lt, lt + 1, lt + 1, lt + 1, lt + 1, + lt + 2, lt + 3, napiVn, lt, lt + 1, lt + 2, lt + 3, "SwapJs2CBool" + ,napiVn, lt, lt + 2, lt + 1, lt + 2, lt + 3)) +} + +function mapMapNumber (mapTemplete, napiVn, lt, mapTypeString) { + return mapTemplete.replaceAll("[replace_swap]", + `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); + uint32_t len%d=pxt->GetMapLength(pxt->GetMapElementValue(%s,tt%d.c_str())); + for(uint32_t i%d=0;i%dSwapJs2CUtf8(pxt->GetMapElementName(pxt->GetMapElementValue(%s,tt%d.c_str()), i%d),tt%d); + NUMBER_JS_2_C(pxt->GetMapElementValue(pxt->GetMapElementValue(%s,tt%d.c_str()),tt%d.c_str()),%s,tt%d); + tt%d.insert(std::make_pair(tt%d, tt%d)); + }`.format(napiVn, lt, lt, lt + 1, napiVn, lt, lt + 1, lt + 1, lt + 1, lt + 1, + lt + 2, mapTypeString, lt + 3, napiVn, lt, lt + 1, lt + 2, napiVn, lt, lt + 2, + mapTypeString, lt + 3, lt + 1, lt + 2, lt + 3)) +} + function mapMap(mapType, napiVn, dest, lt) { let mapTypeString if (mapType[2] == "string") { mapTypeString = "std::string" } @@ -293,44 +348,13 @@ function mapMap(mapType, napiVn, dest, lt) { mapTemplete = mapTemplete.replaceAll("[replace_lt]", lt) mapTemplete = mapTemplete.replaceAll("[replace_lt+1]", lt + 1) if (mapType[2] == "string") { - mapTemplete = mapTemplete.replaceAll("[replace_swap]", - `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); - uint32_t len%d=pxt->GetMapLength(pxt->GetMapElementValue(%s,tt%d.c_str())); - for(uint32_t i%d=0;i%dSwapJs2CUtf8(pxt->GetMapElementName(pxt->GetMapElementValue(%s,tt%d.c_str()), i%d),tt%d); - pxt->SwapJs2CUtf8(pxt->GetMapElementValue(pxt->GetMapElementValue(%s,tt%d.c_str()),tt%d.c_str()),tt%d); - tt%d.insert(std::make_pair(tt%d, tt%d)); - }`.format(napiVn, lt, lt, lt + 1, napiVn, lt, lt + 1, lt + 1, lt + 1, lt + 1, - lt + 2, lt + 3, napiVn, lt, lt + 1, lt + 2, napiVn, lt, lt + 2, lt + 3, lt + 1, lt + 2, lt + 3)) + mapTemplete = mapMapString (mapTemplete, napiVn, lt) } else if (mapType[2] == "boolean") { - mapTemplete = mapTemplete.replaceAll("[replace_swap]", - `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); - uint32_t len%d=pxt->GetMapLength(pxt->GetMapElementValue(%s,tt%d.c_str())); - for(uint32_t i%d=0;i%dSwapJs2CUtf8(pxt->GetMapElementName(pxt->GetMapElementValue(%s,tt%d.c_str()), i%d),tt%d); - pxt->SwapJs2CBool(pxt->GetMapElementValue(pxt->GetMapElementValue(%s,tt%d.c_str()),tt%d.c_str()),tt%d); - tt%d.insert(std::make_pair(tt%d, tt%d)); - }`.format(napiVn, lt, lt, lt + 1, napiVn, lt, lt + 1, lt + 1, lt + 1, lt + 1, - lt + 2, lt + 3, napiVn, lt, lt + 1, lt + 2, napiVn, lt, lt + 2, lt + 3, lt + 1, lt + 2, lt + 3)) + mapTemplete = mapMapBoolean (mapTemplete, napiVn, lt) } else if (mapType[2].substring(0, 12) == "NUMBER_TYPE_") { - mapTemplete = mapTemplete.replaceAll("[replace_swap]", - `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); - uint32_t len%d=pxt->GetMapLength(pxt->GetMapElementValue(%s,tt%d.c_str())); - for(uint32_t i%d=0;i%dSwapJs2CUtf8(pxt->GetMapElementName(pxt->GetMapElementValue(%s,tt%d.c_str()), i%d),tt%d); - NUMBER_JS_2_C(pxt->GetMapElementValue(pxt->GetMapElementValue(%s,tt%d.c_str()),tt%d.c_str()),%s,tt%d); - tt%d.insert(std::make_pair(tt%d, tt%d)); - }`.format(napiVn, lt, lt, lt + 1, napiVn, lt, lt + 1, lt + 1, lt + 1, lt + 1, - lt + 2, mapTypeString, lt + 3, napiVn, lt, lt + 1, lt + 2, napiVn, lt, lt + 2, - mapTypeString, lt + 3, lt + 1, lt + 2, lt + 3)) + mapTemplete = mapMapNumber (mapTemplete, napiVn, lt, mapTypeString) } return mapTemplete } diff --git a/src/gen/generate/return_generate.js b/src/gen/generate/return_generate.js index 6b75718f..855a27f6 100644 --- a/src/gen/generate/return_generate.js +++ b/src/gen/generate/return_generate.js @@ -124,7 +124,8 @@ function mapTempleteFunc(type, deep, dest, value) { function mapInterface(value, lt, tnv, mapType) { let ret - let tnvdefInterface = `for (auto i = %s.begin(); i != %s.end(); i++) + let tnvdefInterface = `result = nullptr; + for (auto i = %s.begin(); i != %s.end(); i++) { const char *tnv%d; [calc_out] @@ -163,7 +164,7 @@ function mapInterface(value, lt, tnv, mapType) { } } ret = tnvdefInterface.replaceAll("[calc_out]", `tnv%d = (i -> first).c_str(); - napi_value result_obj; + napi_value result_obj = nullptr; %s %s %s @@ -195,7 +196,7 @@ function mapTempleteValue(mapType, tnvdef, lt, value, tnv) { function mapTempleteMap(mapType, tnvdef, lt) { let ret if (mapType[2] == "string") { - ret = tnvdef.replaceAll("[calc_out]", `std::string tt%d = i->first; + ret = tnvdef.replaceAll("[calc_out]", `tnv%d = i->first.c_str(); for(auto j = i->second.begin(); j != i->second.end(); j++){ const char * tt%d; napi_value tt%d; @@ -205,7 +206,7 @@ function mapTempleteMap(mapType, tnvdef, lt) { }`.format(lt, lt + 2, lt + 3, lt + 2, lt + 3, lt + 1, lt + 2, lt + 3)) } else if (mapType[2] == "boolean") { - ret = tnvdef.replaceAll("[calc_out]", `std::string tt%d = i->first; + ret = tnvdef.replaceAll("[calc_out]", `tnv%d = i->first.c_str(); for(auto j = i->second.begin(); j != i->second.end(); j++){ const char * tt%d; napi_value tt%d; @@ -215,7 +216,7 @@ function mapTempleteMap(mapType, tnvdef, lt) { }`.format(lt, lt + 2, lt + 3, lt + 2, lt + 3, lt + 1, lt + 2, lt + 3)) } if (mapType[2].substring(0, 12) == "NUMBER_TYPE_") { - ret = tnvdef.replaceAll("[calc_out]", `std::string tt%d = i->first; + ret = tnvdef.replaceAll("[calc_out]", `tnv%d = i->first.c_str(); for(auto j = i->second.begin(); j != i->second.end(); j++){ const char * tt%d; napi_value tt%d; @@ -259,21 +260,26 @@ function mapTempleteArray(mapType, tnvdef, lt) { function returnGenerateMap(type, param) { let mapType = getMapType(type) + let mapTypeString if (mapType[1] != undefined && mapType[2] == undefined) { - let mapTypeString - if (mapType[1] == "string") { - mapTypeString = "std::string" - } - else if (mapType[1].substring(0, 12) == "NUMBER_TYPE_") { - mapTypeString = mapType[1] - } - else if (mapType[1] == "boolean") { - mapTypeString = "bool" - } - param.valueOut = "std::map out;".format(mapTypeString) + if (mapType[1] == "string") { mapTypeString = "std::string" } + else if (mapType[1].substring(0, 12) == "NUMBER_TYPE_") { mapTypeString = mapType[1] } + else if (mapType[1] == "boolean") { mapTypeString = "bool" } + else { mapTypeString = mapType[1] } + } + else if (mapType[2] != undefined) { + if (mapType[2] == "string") { mapTypeString = "std::map" } + else if (mapType[2].substring(0, 12) == "NUMBER_TYPE_") { "std::map" } + else if (mapType[2] == "boolean") { mapTypeString = "std::map" } + } + else if (mapType[3] != undefined) { + if (mapType[3] == "string") { mapTypeString = "std::vector" } + else if (mapType[3].substring(0, 12) == "NUMBER_TYPE_") { mapTypeString = "std::vector<"+mapType[3]+">" } + else if (mapType[3] == "boolean") { mapTypeString = "std::vector" } + } + param.valueOut = "std::map out;".format(mapTypeString) param.valueDefine += "%sstd::map &out" .format(param.valueDefine.length > 0 ? ", " : "", mapTypeString) - } } function returnGenerate(type, param, data) { diff --git a/src/gen/tools/common.js b/src/gen/tools/common.js index 54c4f727..4331ba8b 100644 --- a/src/gen/tools/common.js +++ b/src/gen/tools/common.js @@ -141,36 +141,38 @@ function enumIndex(type, data) { } function getMapType(type) { - type = type.replace(/\s*/g, "") - let tt1 = re.search("Map<([a-zA-Z_0-9]+),", type) - let tt2 = re.search(",([a-zA-Z_0-9<>]+)>", type) - let tt3 - let tt4 - + type = type.replace(/\s*/g,"") + let ttKey = re.search("Map<([a-zA-Z_0-9]+),", type) + let ttValue = re.search(",([a-zA-Z_0-9<>]+)>", type) + let ttMap = re.search(",([a-zA-Z_0-9]+)>>", type) + let ttArray = re.search("Array<([a-zA-Z_0-9]+)>", type) + let valueType let valueMapType let valueArrayType - if (tt1 == null && tt2 == null) { - tt1 = re.search("key:([a-zA-Z_0-9]+)", type) - tt2 = re.search(":([a-zA-Z_0-9]+)}", type) - tt3 = re.search(":([a-zA-Z_0-9]+)}}", type) - tt4 = re.search("Array<([a-zA-Z_0-9]+)>", type) + if (ttKey == null && ttValue == null && ttMap == null) { + ttKey = re.search("key:([a-zA-Z_0-9]+)", type) + ttValue = re.search(":([a-zA-Z_0-9]+)}", type) + ttMap = re.search(":([a-zA-Z_0-9]+)}}", type) + ttArray = re.search("Array<([a-zA-Z_0-9]+)>", type) } - if (tt2 != null) { - valueType = re.getReg(type, tt2.regs[1]) + if (ttValue != null) { + valueType = re.getReg(type, ttValue.regs[1]) if (valueType.indexOf("Array<") == 0) { - let tt5 = re.search("Array<([a-zA-Z_0-9]+)>", valueType) - valueArrayType = re.getReg(valueType, tt5.regs[1]) + valueArrayType = re.getReg(valueType, ttArray.regs[1]) + valueType = undefined + } else if (ttMap != undefined) { + valueMapType = re.getReg(type, ttMap.regs[1]) valueType = undefined } } - if (tt3 != null) { - valueMapType = re.getReg(type, tt3.regs[1]) + if (ttMap != null) { + valueMapType = re.getReg(type, ttMap.regs[1]) } - if (tt4 != null) { - valueArrayType = re.getReg(type, tt4.regs[1]) + if (ttArray != null) { + valueArrayType = re.getReg(type, ttArray.regs[1]) } - return [re.getReg(type, tt1.regs[1]), valueType, valueMapType, valueArrayType] + return [re.getReg(type, ttKey.regs[1]), valueType, valueMapType, valueArrayType] } module.exports = { diff --git a/test/storytest/test_map/@ohos.test.d.ts b/test/storytest/test_map/@ohos.test.d.ts index be80db64..d6f51195 100644 --- a/test/storytest/test_map/@ohos.test.d.ts +++ b/test/storytest/test_map/@ohos.test.d.ts @@ -13,9 +13,15 @@ * limitations under the License. */ declare namespace napitest { + interface Human { + name: string; + age: number; + isTrue: boolean; + } + interface TestClass1 { - map1 : {[key: string]: Array} ; - map2 : Map> + map1 : {[key: string]: {[key: string]: string}} ; + map2 : Map ; } interface TestClass2 { @@ -25,7 +31,10 @@ declare namespace napitest { fun4(v: {[key: string]: Array}): number; fun5(v: {[key: string]: Array}): number; fun6(v: {[key: string]: Array}): number; - fun7(v: Map>): number; + fun7(v: {[key: string]: {[key: string]: string}}): number; + fun8(v: Map>): number; + fun9(v: {[key: string]: Human}): number; + fun10(v: Map): 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 cc04b0a7..52a195f5 100644 --- a/test/storytest/test_map/test.js +++ b/test/storytest/test_map/test.js @@ -46,6 +46,9 @@ describe('Map', function () { let ret = tc2.fun5({"age":[122,222,322],"name":[422,522,622]}); assert.strictEqual(ret, 0); }); +}); + +describe('Map', function () { it('test TestClass2 fun6', function () { let tc2 = new TestClass2(); @@ -58,4 +61,28 @@ describe('Map', function () { let ret = tc2.fun7({"age":"ageValue","name":"nameValue"}); assert.strictEqual(ret, 0); }); + + it('test TestClass2 fun8', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun8( + {"peter":{"age":"ageValue","name":"nameValue"},"jane":{"age":"ageValue","name":"nameValue"}} + ); + assert.strictEqual(ret, 0); + }); + + it('test TestClass2 fun9', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun9( + {"peter":{"age":666,"name":"peter","isTrue":true},"jane":{"age":666,"name":"jane","isTrue":false}} + ); + assert.strictEqual(ret, 0); + }); + + it('test TestClass2 fun10', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun10( + {"peter":{"age":666,"name":"peter","isTrue":true},"jane":{"age":666,"name":"jane","isTrue":false}} + ); + assert.strictEqual(ret, 0); + }); }); \ No newline at end of file -- Gitee