From 4c7c47eed3499a523174d13addc3d3341a47db52 Mon Sep 17 00:00:00 2001 From: liuycag Date: Wed, 12 Oct 2022 15:49:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81map=5Fany=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuycag --- src/gen/generate/interface.js | 5 ++++ src/gen/generate/param_generate.js | 24 +++++++++++++++++- src/gen/generate/return_generate.js | 4 +++ test/storytest/test_map/@ohos.test.d.ts | 1 + test/storytest/test_map/test.js | 33 +++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/gen/generate/interface.js b/src/gen/generate/interface.js index 1f9e7ea6..dfd1d361 100644 --- a/src/gen/generate/interface.js +++ b/src/gen/generate/interface.js @@ -126,6 +126,11 @@ function mapTypeString(type, name) { else if (mapType[1].substring(0, 12) == "NUMBER_TYPE_") { mapTypeString = "std::string,%s".format(mapType[1]) } + else if (mapType[1].substring(0, 12) == "any") { + mapTypeString = `std::string,std::any`.format(mapType[1]) + return `\n std::map<%s> %s; + std::string %s_type;`.format(mapTypeString, name, name) + } else if (InterfaceList.getValue(mapType[1])) mapTypeString = "std::string,%s".format(mapType[1]) } if (mapType[2] != undefined) { diff --git a/src/gen/generate/param_generate.js b/src/gen/generate/param_generate.js index b10f6ce6..4c5e5d54 100644 --- a/src/gen/generate/param_generate.js +++ b/src/gen/generate/param_generate.js @@ -368,6 +368,7 @@ function paramGenerateMap(funcValue, param, p) { 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] == "any") { mapTypeString = "std::any" } else { mapTypeString = mapType[1] } } else if (mapType[2] != undefined) { @@ -380,10 +381,21 @@ function paramGenerateMap(funcValue, param, p) { else if (mapType[3].substring(0, 12) == "NUMBER_TYPE_") { mapTypeString = "std::vector<"+mapType[3]+">" } else if (mapType[3] == "boolean") { mapTypeString = "std::vector" } } + paramGenerateMap2(funcValue, param, p, mapType, mapTypeString, name) +} + +function paramGenerateMap2(funcValue, param, p, mapType, mapTypeString, 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) + if (mapType[1] == "any") { + param.valueIn += funcValue.optional ? `\n std::map* in%d = nullptr; + std::string in%d_type;`.format(mapTypeString, p, p) + : `\n std::map in%d; + std::string in%d_type;`.format(mapTypeString, p, p) + } else { + 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) @@ -471,6 +483,7 @@ function mapValue(mapType, napiVn, dest, lt) { 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] == "any") { mapTypeString = "std::any" } else if (mapType[1] != null) { mapTypeString = mapType[1] } let mapTemplete = mapValueTemplete.format(napiVn, mapTypeString, dest) mapTemplete = mapTemplete.replaceAll("[replace_lt]", lt) @@ -493,6 +506,15 @@ function mapValue(mapType, napiVn, dest, lt) { tt%d = pxt->SwapJs2CBool(pxt->GetMapElementValue(%s,tt%d.c_str()));` .format(napiVn, lt, lt, lt + 1, napiVn, lt)) } + if (mapTypeString == "std::any") { + mapTemplete = mapTemplete.replaceAll("[replace_swap]", + `pxt->SwapJs2CUtf8(pxt->GetMapElementName(%s,i%d), tt%d); + if (i%d == 0){ + %s_type = pxt->GetAnyType(pxt->GetMapElementValue(%s,tt%d.c_str())); + } + pxt->SetAnyValue(%s_type, pxt->GetMapElementValue(%s,tt%d.c_str()), tt%d);` + .format(napiVn, lt, lt, lt, dest, napiVn, lt, dest, napiVn, lt, lt + 1)) + } else if (InterfaceList.getValue(mapTypeString)) { mapTemplete = mapInterface(mapTypeString, mapTemplete, napiVn, lt) } diff --git a/src/gen/generate/return_generate.js b/src/gen/generate/return_generate.js index 405a5eb1..48e7790b 100644 --- a/src/gen/generate/return_generate.js +++ b/src/gen/generate/return_generate.js @@ -278,6 +278,9 @@ function mapTempleteValue(mapType, tnvdef, lt, value, tnv) { } else if (mapType[1].substring(0, 12) == "NUMBER_TYPE_") { ret = tnvdef.replaceAll("[calc_out]", `tnv%d = (i -> first).c_str(); tnv%d = NUMBER_C_2_JS(pxt,i->second);`.format(lt, lt + 1)) + } else if (mapType[1] == "any") { + ret = tnvdef.replaceAll("[calc_out]", `tnv%d = (i -> first).c_str(); + pxt->GetAnyValue(%s_type, tnv%d, i->second);`.format(lt, value, lt + 1)) } else if (InterfaceList.getValue(mapType[1])) { ret = mapInterface(value, lt, tnv, mapType) @@ -363,6 +366,7 @@ function returnGenerateMap(returnInfo, param) { 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] == "any") { mapTypeString = "std::any" } else { mapTypeString = mapType[1] } } else if (mapType[2] != undefined) { diff --git a/test/storytest/test_map/@ohos.test.d.ts b/test/storytest/test_map/@ohos.test.d.ts index b4afc8f0..0218d4fc 100644 --- a/test/storytest/test_map/@ohos.test.d.ts +++ b/test/storytest/test_map/@ohos.test.d.ts @@ -37,6 +37,7 @@ declare namespace napitest { fun8(v: Map>): number; fun9(v: {[key: string]: Human}): number; fun10(v: Map): number; + fun11(v: Map): number; } function fun13(v: {[key: string]: string[]}): number; diff --git a/test/storytest/test_map/test.js b/test/storytest/test_map/test.js index 14b469c5..2d64578c 100644 --- a/test/storytest/test_map/test.js +++ b/test/storytest/test_map/test.js @@ -83,4 +83,37 @@ describe('Map', function () { "jane":{"age":666,"name":"jane","isTrue":false}}); assert.strictEqual(ret, 0); }); +}); + +describe('Map_Any', function () { + it('test TestClass2 fun11', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun11({"age":"ageValue","name":"nameValue"}); + assert.strictEqual(ret, 0); + }); + + it('test TestClass2 fun11', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun11({"age":1234,"name":5678}); + assert.strictEqual(ret, 0); + }); + + it('test TestClass2 fun11', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun11({"age":true,"name":false}); + assert.strictEqual(ret, 0); + }); + + it('test TestClass2 fun11', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun11({"age":[1,23,456],"name":[7,89,101112]}); + assert.strictEqual(ret, 0); + }); + + it('test TestClass2 fun11', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun11({"peter":{"age":"ageValue","name":"nameValue"}, + "jane":{"age":"ageValue","name":"nameValue"}}); + assert.strictEqual(ret, 0); + }); }); \ No newline at end of file