From 845b3d97b753d27950b3d4b7653ad7db7150be72 Mon Sep 17 00:00:00 2001 From: liuycag Date: Thu, 8 Sep 2022 17:38:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eany=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuycag --- src/gen/extend/x_napi_tool.js | 27 +++++++++++++++++ src/gen/generate/interface.js | 16 ++++++++--- src/gen/generate/param_generate.js | 35 +++++++++++++++++++---- src/gen/generate/return_generate.js | 33 +++++++++++++++++---- test/storytest/test_any[]/@ohos.test.d.ts | 24 ++++++++++++++++ test/storytest/test_any[]/test.js | 34 ++++++++++++++++++++++ 6 files changed, 154 insertions(+), 15 deletions(-) create mode 100644 test/storytest/test_any[]/@ohos.test.d.ts create mode 100644 test/storytest/test_any[]/test.js diff --git a/src/gen/extend/x_napi_tool.js b/src/gen/extend/x_napi_tool.js index eebaec38..1b4fe0b8 100644 --- a/src/gen/extend/x_napi_tool.js +++ b/src/gen/extend/x_napi_tool.js @@ -91,6 +91,7 @@ public: std::string GetUnionType(napi_value object); std::string GetAnyType(napi_value object); + std::string GetAnyArrayType(napi_value object); void SetAnyValue(std::string &any_type, napi_value argv, std::any &any); void GetAnyValue (std::string any_type, napi_value &result, std::any any); @@ -404,6 +405,32 @@ std::string XNapiTool::GetAnyType(napi_value object){ } } +std::string XNapiTool::GetAnyArrayType(napi_value object){ + napi_valuetype result; + napi_typeof(env_, object, &result); + if (result == napi_object) { + bool is_array; + napi_is_array(env_, object, &is_array); + if (is_array) { + napi_value arr_value_result; + napi_valuetype arr_type_result; + napi_get_element (env_, object, 0, &arr_value_result); + napi_typeof(env_, arr_value_result, &arr_type_result); + if (arr_type_result == napi_string) { + return "arr_string"; + } else if (arr_type_result == napi_number) { + return "arr_number"; + } else if (arr_type_result == napi_boolean) { + return "arr_boolean"; + } else { + return nullptr; + } + } + return nullptr; + } + return nullptr; +} + void XNapiTool::SetAnyValue(std::string &any_type, napi_value argv, std::any &any) { if (any_type == "string") { diff --git a/src/gen/generate/interface.js b/src/gen/generate/interface.js index 59516602..cad8e1dc 100644 --- a/src/gen/generate/interface.js +++ b/src/gen/generate/interface.js @@ -52,14 +52,22 @@ function getHDefineOfVariable(name, type, variable) { else if (EnumList.getValue(type)) variable.hDefine += "\n %s %s;".format(type, name) else if (type.indexOf("Array<") == 0) { let arrayType = getArrayType(type) - let cType = jsType2CType(arrayType) - variable.hDefine += "\n std::vector<%s> %s;".format(cType, name) + if (arrayType == "any") { + variable.hDefine += "\n std::string %s_type; \n std::any %s;".format(name,name) + } else { + let cType = jsType2CType(arrayType) + variable.hDefine += "\n std::vector<%s> %s;".format(cType, name) + } } else if (type == "boolean") { variable.hDefine += "\n bool %s;".format(name) } else if (type.substring(type.length - 2) == "[]") { let arrayType = getArrayTypeTwo(type) - let cType = jsType2CType(arrayType) - variable.hDefine += "\n std::vector<%s> %s;".format(cType, name) + if (arrayType == "any") { + variable.hDefine += "\n std::string %s_type; \n std::any %s;".format(name,name) + } else { + let cType = jsType2CType(arrayType) + variable.hDefine += "\n std::vector<%s> %s;".format(cType, name) + } } else if (type.substring(0, 4) == "Map<" || type.indexOf("{[key:") == 0) { variable.hDefine += mapTypeString(type, name) } else if (type == "any") { diff --git a/src/gen/generate/param_generate.js b/src/gen/generate/param_generate.js index b8bb349b..3f753398 100644 --- a/src/gen/generate/param_generate.js +++ b/src/gen/generate/param_generate.js @@ -65,7 +65,7 @@ function jsToC(dest, napiVn, type, enumType = 0) { } else if (type.substring(0, 4) == "Map<" || type.substring(0, 6) == "{[key:") { return mapTempleteFunc(dest, napiVn, type); } else if (type == "any") { - return anyTempleteFunc(dest, napiVn, type); + return anyTempleteFunc(dest) } else { NapiLog.logError(`do not support to generate jsToC %s,%s,%s`.format(dest, napiVn, type)); } @@ -120,6 +120,8 @@ function getArrayTypeTemplete(type) { arrayType = "std::string" } else if (arrayType == "boolean") { arrayType = "bool" + } else if (arrayType == "any") { + arrayType = "any" } else if (arrayType == "[key:string]:string" || arrayType == "Map") { arrayType = "std::map" } else if (arrayType.substring(0, arrayType.length-1) == "[key:string]:NUMBER_TYPE_" || @@ -142,6 +144,9 @@ function getArrayTypeTemplete(type) { function arrTemplete(dest, napiVn, type) { let lt = LenIncrease.getAndIncrease() let arrayType = getArrayTypeTemplete(type) + if (arrayType == "any") { + return anyArrayTempleteFunc(dest); + } let arrTemplete = `\ uint32_t len[replace_lt]=pxt->GetArrayLength(%s); for(uint32_t i[replace_lt]=0;i[replace_lt]* in%d = nullptr;".format(arrayType, p) : "\n std::vector<%s> in%d;".format(arrayType, p) param.valueCheckout += jsToC(inParamName, "pxt->GetArgv(%d)".format(p), type) @@ -291,13 +299,15 @@ function paramGenerateArray(p, funcValue, param) { let arrayType = getArrayType(type) let strLen = getMapKeyLen(arrayType) let keyType = arrayType.substring(0, strLen) - let suType = arrayType.substring(0,12) if (arrayType == "string") { arrayType = "std::string" } else if (arrayType == "boolean") { arrayType = "bool" - } else if (keyType == "[key:string]:"|| keyType == "Map 0 ? ", " : "", name) } +function paramGenerateAnyArray(p, name, type, param) { + param.valueIn += `\n std::any in%d; + std::string in%d_type;`.format(p, 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::any &%s".format(param.valueDefine.length > 0 ? ", " : "", name) +} + function paramGenerateEnum(data, funcValue, param, p) { let index = enumIndex(funcValue.type, data) if (data.enum[index].body.enumValueType == EnumValueType.ENUM_VALUE_TYPE_NUMBER) { @@ -387,8 +405,7 @@ function mapTempleteFunc(dest, napiVn, type) { return mapTemplete } -function anyTempleteFunc(dest, napiVn, type) { - +function anyTempleteFunc(dest) { let anyTemplete = `%s_type = pxt->GetAnyType(pxt->GetArgv(0)); pxt->SetAnyValue(%s_type, pxt->GetArgv(0), %s);` .format(dest, dest, dest) @@ -396,6 +413,14 @@ function anyTempleteFunc(dest, napiVn, type) { return anyTemplete } +function anyArrayTempleteFunc(dest) { + let anyTemplete = `%s_type = pxt->GetAnyArrayType(pxt->GetArgv(0)); + pxt->SetAnyValue(%s_type, pxt->GetArgv(0), %s);` + .format(dest, dest, dest) + + return anyTemplete +} + let mapValueTemplete = `\ uint32_t len[replace_lt]=pxt->GetMapLength(%s); for(uint32_t i[replace_lt]=0;i[replace_lt]SwapC2JsBool(%s[i]);`.format(lt, value)) } + else if (arrayType == "any") { + return anyArrayTempleteFunc(value) + } else if (InterfaceList.getValue(arrayType)) { ret = tnvdef.replaceAll("[calc_out]", cToJs(value + "[i]", arrayType, "tnv" + lt, deep + 1)) } @@ -189,8 +192,14 @@ function mapTempleteFunc(type, deep, dest, value) { return ret } -function anyTempleteFunc(type, deep, dest, value) { +function anyTempleteFunc(value) { + let anyTemplete = `pxt->GetAnyValue(%s_type, result, %s);` + .format(value, value) + + return anyTemplete +} +function anyArrayTempleteFunc(value) { let anyTemplete = `pxt->GetAnyValue(%s_type, result, %s);` .format(value, value) @@ -466,18 +475,30 @@ function returnGenerate2(returnInfo, param, data){ else if (type.substring(0, 6) == "Array<") { let arrayType = getArrayType(type) arrayType = jsType2CType(arrayType) - param.valueOut = returnInfo.optional ? "std::vector<%s>* out = nullptr;".format(arrayType) + if (arrayType == "any") { + param.valueOut = `std::any out; + std::string out_type;` + param.valueDefine += "%sstd::any &out".format(param.valueDefine.length > 0 ? ", " : "") + } else { + param.valueOut = returnInfo.optional ? "std::vector<%s>* out = nullptr;".format(arrayType) : "std::vector<%s> out;".format(arrayType) - param.valueDefine += "%sstd::vector<%s>%s out".format( + param.valueDefine += "%sstd::vector<%s>%s out".format( param.valueDefine.length > 0 ? ", ": "", arrayType, modifiers) + } } else if (type.substring(type.length - 2) == "[]") { let arrayType = getArrayTypeTwo(type) arrayType = jsType2CType(arrayType) - param.valueOut = returnInfo.optional ? "std::vector<%s>* out = nullptr;".format(arrayType) + if (arrayType == "any") { + param.valueOut = `std::any out; + std::string out_type;` + param.valueDefine += "%sstd::any &out".format(param.valueDefine.length > 0 ? ", " : "") + } else { + param.valueOut = returnInfo.optional ? "std::vector<%s>* out = nullptr;".format(arrayType) : "std::vector<%s> out;".format(arrayType) - param.valueDefine += "%sstd::vector<%s>%s out".format( + param.valueDefine += "%sstd::vector<%s>%s out".format( param.valueDefine.length > 0 ? ", " : "", arrayType, modifiers) + } } else if (type.substring(0, 4) == "Map<" || type.indexOf("{[key:") == 0) { returnGenerateMap(returnInfo, param) diff --git a/test/storytest/test_any[]/@ohos.test.d.ts b/test/storytest/test_any[]/@ohos.test.d.ts new file mode 100644 index 00000000..6d401e0e --- /dev/null +++ b/test/storytest/test_any[]/@ohos.test.d.ts @@ -0,0 +1,24 @@ +/* +* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +declare namespace napitest { + interface TestClass1 { + any1: any[]; + } + + function fun1(v1: any[]): number; + function fun2(v1: Array): number; +} + +export default napitest; diff --git a/test/storytest/test_any[]/test.js b/test/storytest/test_any[]/test.js new file mode 100644 index 00000000..a25bb113 --- /dev/null +++ b/test/storytest/test_any[]/test.js @@ -0,0 +1,34 @@ +/* +* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +const { fun1, fun2, TestClass1 } = require("./out/build/Release/napitest") +var assert = require("assert"); + +describe('any[]', function () { + it('test fun1', function () { + let ret = fun1(['a', 'b', 'c', 'd']); + assert.strictEqual(ret, 0); + }); + + it('test fun1', function () { + let ret = fun1([1, 2, 3, 4]); + assert.strictEqual(ret, 0); + }); + + it('test fun2', function () { + let ret = fun2([true, true, false, false]); + assert.strictEqual(ret, 0); + }); +}); + -- Gitee