From 099417b231b89cf22354f9663ccea3b25ef7a9b7 Mon Sep 17 00:00:00 2001 From: liuycag Date: Tue, 14 Jun 2022 00:23:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0map<>=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuycag --- package.json | 4 +++- src/gen/analyze/params.js | 2 +- src/gen/generate/param_generate.js | 2 +- src/gen/generate/return_generate.js | 10 +++++----- src/gen/tools/common.js | 15 ++++++++++----- test/storytest/test_map/@ohos.test.d.ts | 4 +++- test/storytest/test_map/test.js | 9 +++++++-- 7 files changed, 30 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 61ab6523..9f041be8 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,9 @@ "stdio": "^2.1.1", "typescript": "^4.5.5" }, - "devDependencies": {}, + "devDependencies": { + "@types/node": "^17.0.42" + }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/src/gen/analyze/params.js b/src/gen/analyze/params.js index 7bb09779..937e062e 100644 --- a/src/gen/analyze/params.js +++ b/src/gen/analyze/params.js @@ -26,7 +26,7 @@ function analyzeParams(values) { if (v == null) v = values values = values.substring(v.length, values.length) - let matchs = re.match("([a-zA-Z_0-9\\.]+)\\?*: *([a-zA-Z<>_0-9\\[\\]\\(\\):='{}]+)", v) + let matchs = re.match("([a-zA-Z_0-9\\.]+)\\?*: *([a-zA-Z<,>_0-9\\[\\]\\(\\):='{}]+)", v) if (matchs != null) { let type = re.getReg(v, matchs.regs[2]) result.push({ "name": re.getReg(v, matchs.regs[1]), "type": type }) diff --git a/src/gen/generate/param_generate.js b/src/gen/generate/param_generate.js index 7566cce8..f13c1414 100644 --- a/src/gen/generate/param_generate.js +++ b/src/gen/generate/param_generate.js @@ -369,7 +369,7 @@ function mapArray(mapType, napiVn, dest, lt) { NUMBER_JS_2_C(pxt->GetArrayElement(pxt->GetMapElementValue(%s,tt%d.c_str()),i%d), %s, tt%d); tt%d.push_back(tt%d); }`.format(napiVn, lt, lt, lt + 1, napiVn, lt, lt + 1, lt + 1, lt + 1, - lt + 1, mapTypeString, lt + 2, napiVn, lt, lt, mapTypeString, lt + 2, lt + 1, lt + 2)) + lt + 1, mapTypeString, lt + 2, napiVn, lt, lt + 1, mapTypeString, lt + 2, lt + 1, lt + 2)) } return mapTemplete } diff --git a/src/gen/generate/return_generate.js b/src/gen/generate/return_generate.js index ecfad193..9d328fe5 100644 --- a/src/gen/generate/return_generate.js +++ b/src/gen/generate/return_generate.js @@ -43,7 +43,7 @@ function cToJs(value, type, dest, deep = 1) { let arrayType = checkArrayParamType(type) return arrayTempleteFunc(arrayType, deep, dest, value) } - else if (type.substring(0, 1) == "{") { + else if (type.substring(0, 4) == "Map<" || type.indexOf("{") == 0) { return mapTempleteFunc(type, deep, dest, value) } else @@ -94,7 +94,7 @@ function mapTempleteFunc(type, deep, dest, value) { napi_value tnv%d = nullptr; [calc_out] pxt->SetMapElement(%s, tnv%d, tnv%d); - }`.format(value, value, lt, lt + 1, tnv, lt, lt + 2) + }`.format(value, value, lt, lt + 1, tnv, lt, lt + 1) let ret = "" if (mapType[1] != undefined && mapType[2] == undefined) { ret = mapTempleteValue(mapType, tnvdef, lt, value, tnv) @@ -222,7 +222,7 @@ function mapTempleteArray(mapType, tnvdef, lt) { for(uint32_t j=0;jSwapC2JsUtf8(i->second[j].c_str()); pxt->SetArrayElement(tnv%d, j, tnv%d); - }`.format(lt + 2, lt, lt, lt, lt + 1, lt + 2, lt + 1)) + }`.format(lt + 2, lt, lt, lt, lt + 2, lt + 1, lt + 2)) } else if (mapType[3] == "boolean") { ret = tnvdef.replaceAll("[calc_out]", `napi_value tnv%d = nullptr; tnv%d = (i -> first).c_str(); @@ -230,7 +230,7 @@ function mapTempleteArray(mapType, tnvdef, lt) { for(uint32_t j=0;jSwapC2JsBool(i->second[j]); pxt->SetArrayElement(tnv%d, j, tnv%d); - }`.format(lt + 2, lt, lt, lt, lt + 1, lt + 2, lt + 1)) + }`.format(lt + 2, lt, lt, lt, lt + 2, lt + 1, lt + 2)) } else if (mapType[3].substring(0, 12) == "NUMBER_TYPE_") { ret = tnvdef.replaceAll("[calc_out]", `napi_value tnv%d = nullptr; tnv%d = (i -> first).c_str(); @@ -238,7 +238,7 @@ function mapTempleteArray(mapType, tnvdef, lt) { for(uint32_t j=0;jsecond[j]); pxt->SetArrayElement(tnv%d, j, tnv%d); - }`.format(lt + 2, lt, lt, lt, lt + 1, lt + 2, lt + 1)) + }`.format(lt + 2, lt, lt, lt, lt + 2, lt + 1, lt + 2)) } return ret } diff --git a/src/gen/tools/common.js b/src/gen/tools/common.js index b0c57211..953f6727 100644 --- a/src/gen/tools/common.js +++ b/src/gen/tools/common.js @@ -125,26 +125,31 @@ 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 tt2 = re.search(",([a-zA-Z_0-9<>]+)>", type) let tt3 let tt4 let valueType let valueMapType let valueArrayType - if(tt1 == null && tt2 == null){ + 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(tt2 != null){ + if (tt2 != null) { valueType = re.getReg(type, tt2.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]) + valueType = undefined + } } - if(tt3 != null){ + if (tt3 != null) { valueMapType = re.getReg(type, tt3.regs[1]) } - if(tt4 != null){ + if (tt4 != null) { valueArrayType = re.getReg(type, tt4.regs[1]) } return [re.getReg(type, tt1.regs[1]), valueType, valueMapType, valueArrayType] diff --git a/test/storytest/test_map/@ohos.test.d.ts b/test/storytest/test_map/@ohos.test.d.ts index 9c98f3b6..be80db64 100644 --- a/test/storytest/test_map/@ohos.test.d.ts +++ b/test/storytest/test_map/@ohos.test.d.ts @@ -14,7 +14,8 @@ */ declare namespace napitest { interface TestClass1 { - map1 : {[key: string]: Array} ; + map1 : {[key: string]: Array} ; + map2 : Map> } interface TestClass2 { @@ -24,6 +25,7 @@ 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; } } 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 dd19f368..cc04b0a7 100644 --- a/test/storytest/test_map/test.js +++ b/test/storytest/test_map/test.js @@ -43,7 +43,7 @@ describe('Map', function () { it('test TestClass2 fun5', function () { let tc2 = new TestClass2(); - let ret = tc2.fun5({"age":[1,2,3],"name":[4,5,6]}); + let ret = tc2.fun5({"age":[122,222,322],"name":[422,522,622]}); assert.strictEqual(ret, 0); }); @@ -52,5 +52,10 @@ describe('Map', function () { let ret = tc2.fun6({"age":[true,true,true],"name":[false,false,false]}); assert.strictEqual(ret, 0); }); - + + it('test TestClass2 fun7', function () { + let tc2 = new TestClass2(); + let ret = tc2.fun7({"age":"ageValue","name":"nameValue"}); + assert.strictEqual(ret, 0); + }); }); \ No newline at end of file -- Gitee