From 1789a74d8846763d1a9b76e11ebe96c08ef79863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E7=91=9E=E6=B6=9B?= Date: Tue, 30 Aug 2022 01:12:25 -0700 Subject: [PATCH] add std::map MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡瑞涛 --- src/gen/generate/param_generate.js | 179 +++++++++++++++++------- test/storytest/test_map/@ohos.test.d.ts | 1 + test/storytest/test_map/test.js | 5 + 3 files changed, 134 insertions(+), 51 deletions(-) diff --git a/src/gen/generate/param_generate.js b/src/gen/generate/param_generate.js index 5ba6c1f1..65c7d57d 100644 --- a/src/gen/generate/param_generate.js +++ b/src/gen/generate/param_generate.js @@ -300,31 +300,64 @@ function paramGenerateMap(funcValue, param, p) { let name = funcValue.name let mapType = getMapType(type) let mapTypeString - if (mapType[1] != undefined && mapType[2] == undefined) { - 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" } + if(mapType[0].substring(0, 12) == "NUMBER_TYPE_") + { + let numbertepy = mapType[0] + if (mapType[1] != undefined && mapType[2] == undefined) { + 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" } + } + let inParamName = funcValue.optional ? "(*vio->in" + p + ")" : "vio->in" + p + let modifiers = funcValue.optional ? "*" : "&" + param.valueIn += funcValue.optional ? "\n std::map<%s,%s>* in%d = nullptr;".format(numbertepy,mapTypeString, p) + : "\n std::map<%s,%s> in%d;".format(numbertepy,mapTypeString, p) + param.valueCheckout += getValueCheckout(funcValue, param, inParamName, p, + "std::map<%s,%s>".format(numbertepy,mapTypeString)) + param.valueFill += "%svio->in%d".format(param.valueFill.length > 0 ? ", " : "", p) + param.valueDefine += "%sstd::map<%s,%s>%s %s" + .format(param.valueDefine.length > 0 ? ", " : "", numbertepy,mapTypeString, modifiers, name) + } + else + { + if (mapType[1] != undefined && mapType[2] == undefined) { + 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" } + } + let inParamName = funcValue.optional ? "(*vio->in" + p + ")" : "vio->in" + p + let modifiers = funcValue.optional ? "*" : "&" + param.valueIn += funcValue.optional ? "\n std::map* in%d = nullptr;".format(mapTypeString, p) + : "\n std::map in%d;".format(mapTypeString, p) + param.valueCheckout += getValueCheckout(funcValue, param, inParamName, p, + "std::map".format(mapTypeString)) + param.valueFill += "%svio->in%d".format(param.valueFill.length > 0 ? ", " : "", p) + param.valueDefine += "%sstd::map%s %s" + .format(param.valueDefine.length > 0 ? ", " : "", mapTypeString, modifiers, name) } - let inParamName = funcValue.optional ? "(*vio->in" + p + ")" : "vio->in" + p - let modifiers = funcValue.optional ? "*" : "&" - param.valueIn += funcValue.optional ? "\n std::map* in%d = nullptr;".format(mapTypeString, p) - : "\n std::map in%d;".format(mapTypeString, p) - param.valueCheckout += getValueCheckout(funcValue, param, inParamName, p, - "std::map".format(mapTypeString)) - param.valueFill += "%svio->in%d".format(param.valueFill.length > 0 ? ", " : "", p) - param.valueDefine += "%sstd::map%s %s" - .format(param.valueDefine.length > 0 ? ", " : "", mapTypeString, modifiers, name) + } function mapTempleteFunc(dest, napiVn, type) { @@ -351,7 +384,14 @@ for(uint32_t i[replace_lt]=0;i[replace_lt]GetMapLength(%s); +for(uint32_t i[replace_lt]=0;i[replace_lt]SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); - pxt->SwapJs2CUtf8(pxt->GetMapElementValue(%s,tt%d.c_str()), tt%d);` - .format(napiVn, lt, lt, napiVn, lt, lt + 1)) - } - else if (mapTypeString.substring(0, 12) == "NUMBER_TYPE_") { - mapTemplete = mapTemplete.replaceAll("[replace_swap]", - `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); - NUMBER_JS_2_C(pxt->GetMapElementValue(%s,tt%d.c_str()),%s,tt%d);` - .format(napiVn, lt, lt, napiVn, lt, mapTypeString, lt + 1)) - } - else if (mapTypeString == "bool") { - mapTemplete = mapTemplete.replaceAll("[replace_swap]", - `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); - tt%d = pxt->SwapJs2CBool(pxt->GetMapElementValue(%s,tt%d.c_str()));` - .format(napiVn, lt, lt, lt + 1, napiVn, lt)) + let mapfirstType + let mapTemplete + if(mapType[0].substring(0, 12) == "NUMBER_TYPE_") + { + mapfirstType = mapType[0] + 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 if (mapType[1] != null) { mapTypeString = mapType[1] } + mapTemplete = mapValueTempleteEx.format(napiVn,mapfirstType, mapTypeString, dest) + mapTemplete = mapTemplete.replaceAll("[replace_lt]", lt) + mapTemplete = mapTemplete.replaceAll("[replace_lt+1]", lt + 1) + if (mapTypeString == "std::string") { + mapTemplete = mapTemplete.replaceAll("[replace_swap]", + `pxt->SwapC2JsUint32(tt%d); + pxt->SwapJs2CUtf8(pxt->GetMapElementValue(%s,(const char*)(tt%d)), tt%d);` + .format(lt, napiVn, lt, lt + 1)) + } + else if (mapTypeString.substring(0, 12) == "NUMBER_TYPE_") { + mapTemplete = mapTemplete.replaceAll("[replace_swap]", + `pxt->SwapC2JsUint32(pxt->GetMapElementName(%s,i%d), tt%d); + NUMBER_JS_2_C(pxt->GetMapElementValue(%s,tt%d),%s,tt%d);` + .format(napiVn, lt, lt, napiVn, lt, mapTypeString, lt + 1)) + } + else if (mapTypeString == "bool") { + mapTemplete = mapTemplete.replaceAll("[replace_swap]", + `pxt->SwapC2JsUint32(pxt->GetMapElementName(%s,i%d), tt%d); + tt%d = pxt->SwapJs2CBool(pxt->GetMapElementValue(%s,tt%d));` + .format(napiVn, lt, lt, lt + 1, napiVn, lt)) + } + else if (InterfaceList.getValue(mapTypeString)) { + mapTemplete = mapInterface(mapTypeString, mapTemplete, napiVn, lt) + } } - else if (InterfaceList.getValue(mapTypeString)) { - mapTemplete = mapInterface(mapTypeString, mapTemplete, napiVn, lt) + else + { + 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 if (mapType[1] != null) { mapTypeString = mapType[1] } + mapTemplete = mapValueTemplete.format(napiVn, mapTypeString, dest) + mapTemplete = mapTemplete.replaceAll("[replace_lt]", lt) + mapTemplete = mapTemplete.replaceAll("[replace_lt+1]", lt + 1) + if (mapTypeString == "std::string") { + mapTemplete = mapTemplete.replaceAll("[replace_swap]", + `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); + pxt->SwapJs2CUtf8(pxt->GetMapElementValue(%s,tt%d.c_str()), tt%d);` + .format(napiVn, lt, lt, napiVn, lt, lt + 1)) + } + else if (mapTypeString.substring(0, 12) == "NUMBER_TYPE_") { + mapTemplete = mapTemplete.replaceAll("[replace_swap]", + `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); + NUMBER_JS_2_C(pxt->GetMapElementValue(%s,tt%d.c_str()),%s,tt%d);` + .format(napiVn, lt, lt, napiVn, lt, mapTypeString, lt + 1)) + } + else if (mapTypeString == "bool") { + mapTemplete = mapTemplete.replaceAll("[replace_swap]", + `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); + tt%d = pxt->SwapJs2CBool(pxt->GetMapElementValue(%s,tt%d.c_str()));` + .format(napiVn, lt, lt, lt + 1, napiVn, lt)) + } + else if (InterfaceList.getValue(mapTypeString)) { + mapTemplete = mapInterface(mapTypeString, mapTemplete, napiVn, lt) + } } return mapTemplete } diff --git a/test/storytest/test_map/@ohos.test.d.ts b/test/storytest/test_map/@ohos.test.d.ts index d6f51195..834bedb4 100644 --- a/test/storytest/test_map/@ohos.test.d.ts +++ b/test/storytest/test_map/@ohos.test.d.ts @@ -35,6 +35,7 @@ declare namespace napitest { fun8(v: Map>): number; fun9(v: {[key: string]: Human}): number; fun10(v: Map): number; + fun11(v: {[key: number]: string}): 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 52a195f5..c45f4aa3 100644 --- a/test/storytest/test_map/test.js +++ b/test/storytest/test_map/test.js @@ -85,4 +85,9 @@ describe('Map', function () { ); assert.strictEqual(ret, 0); }); + it('test TestClass2 fun11', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun11({"name":1234,"age":5678}); + assert.strictEqual(ret, 0); + }); }); \ No newline at end of file -- Gitee