From 312c6e546328aa7bdbe62fc470669f6173528148 Mon Sep 17 00:00:00 2001 From: sunlian Date: Fri, 18 Apr 2025 16:24:44 +0800 Subject: [PATCH 1/6] add napi enum, class gen Signed-off-by: sunlian --- .../src/main/java/gen/GenAkiCppFile.java | 1 - .../src/main/java/gen/GenNapiCppFile.java | 767 +++++++---- .../src/test/java/gen/GenNapiCppFileTest.java | 1144 +++++++++++++++-- 3 files changed, 1547 insertions(+), 365 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java index 7e6676c9..d5cb15e9 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java @@ -125,7 +125,6 @@ public class GenAkiCppFile extends GeneratorBase { private static final String AKI_FILE_C_SUFFIX = ".c"; private static final String AKI_STRUCT_SUFFIX = "ST"; - private static final String AKI_ENUM_NAME = "AKI_ENUM_NAME"; private static final String AKI_ENUM_ITEM_NANE = "AKI_ENUM_ITEM_NANE"; private static final String AKI_ENUM_ITEM_DECLARE = "\n\tJSBIND_ENUM_VALUE(AKI_ENUM_ITEM_NANE);"; diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java index c707a9c2..631e0071 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java @@ -34,92 +34,244 @@ import java.util.Map; * @version 1.0 */ public class GenNapiCppFile extends GeneratorBase { - private static final String CPP_ENUM_TOKEN = "enum"; - private static final String CPP_CLASS_TOKEN = "class"; - private static final String CPP_STRUCT_TOKEN = "struct"; - private static final String CPP_UNION_TOKEN = "union"; - private static final String CPP_TEMPLATE_TOKEN = "template"; - private static final String CPP_TYPE_NAME_TOKEN = "typename"; - private static final String CPP_STAR_TOKEN = "*"; - private static final String CPP_CHAR_START_TOKEN = "char*"; - private static final String CPP_AUTO_TOKEN = "auto"; - private static final String CPP_EXPORT_TOKEN = "export"; - private static final String CPP_IMPLEMENCPP_TOKEN = "implements"; - private static final String CPP_EXTENDS_TOKEN = "extends"; - private static final String CPP_CONST_TOKEN = "const"; - private static final String CPP_PRIVATE_TOKEN = "private"; - private static final String CPP_PUBLIC_TOKEN = "public"; - private static final String CPP_INTERFACE_TOKEN = "interface"; - private static final String CPP_PROTECTED_TOKEN = "protected"; - private static final String CPP_STATIC_TOKEN = "static"; - private static final String CPP_ANY_TOKEN = "any"; - private static final String CPP_NUMBER_TOKEN = "number"; - private static final String CPP_NEVER_TOKEN = "never"; - private static final String CPP_BOOLEAN_TOKEN = "boolean"; - private static final String CPP_STRING_TOKEN = "string"; - private static final String CPP_UNIQUE_TOKEN = "unique"; - private static final String CPP_SYMBOL_TOKEN = "symbol"; - private static final String CPP_UNDEFINED_TOKEN = "undefined"; - private static final String CPP_OBJECT_TOKEN = "object"; - private static final String CPP_OF_TOKEN = "of"; - private static final String CPP_KEYOF_TOKEN = "keyof"; - private static final String CPP_TYPE_TOKEN = "type"; - private static final String CPP_CONSTRUCTOR_TOKEN = "constructor"; - private static final String CPP_NAMESPACE_TOKEN = "namespace"; - private static final String CPP_REQUIRE_TOKEN = "require"; - private static final String CPP_MODULE_TOKEN = "module"; - private static final String CPP_DECLARE_TOKEN = "declare"; - private static final String CPP_ABSTRACT_TOKEN = "abstract"; - private static final String CPP_DEBUGGER_TOKEN = "debugger"; - private static final String CPP_FUNCTION_TOKEN = "function"; - private static final String CPP_THIS_TOKEN = "this"; - private static final String CPP_WITH_TOKEN = "with"; - private static final String CPP_DEFAULT_TOKEN = "default"; - private static final String CPP_READONLY_TOKEN = "readonly"; - private static final String CPP_ASYNC_TOKEN = "async"; - private static final String CPP_AWAIT_TOKEN = "await"; - private static final String CPP_YIELD_TOKEN = "yield"; - private static final String CPP_NEW_LINE = "\n"; - private static final String CPP_TAB_SPACE = "\t"; - private static final String CPP_BLANK_SPACE = " "; - private static final String CPP_SPLIT = " | "; - private static final String CPP_EQUAL = " = "; - private static final String CPP_COMMA = ","; - private static final String CPP_DOUBLE_QUOTATION = "\""; - private static final String CPP_UNDER_LINE = "_"; - private static final String CPP_SEMICOLON = ";"; - private static final String CPP_COLON = ":"; - private static final String CPP_ELLIPSIS = "..."; - private static final String CPP_DOT = "."; - private static final String CPP_LEFT_BRACE = "{"; - private static final String CPP_RIGHT_BRACE = "}"; - private static final String CPP_LEFT_PARENTHESES = "("; - private static final String CPP_RIGHT_PARENTHESES = ")"; - private static final String CPP_LEFT_SQUARE_BRACKET = "["; - private static final String CPP_RIGHT_SQUARE_BRACKET = "]"; - private static final String CPP_LEFT_ANGLE_BRACKET = "<"; - private static final String CPP_RIGHT_ANGLE_BRACKET = ">"; - - private static final String CPP_STD_STRING = "std::string"; - private static final String CPP_STD_VECTOR = "std::vector"; - private static final String CPP_STD_LIST = "std::list"; - private static final String CPP_STD_ARRAY = "std::array"; - private static final String CPP_STD_STACK = "std::stack"; - private static final String CPP_STD_QUEUE = "std::queue"; - private static final String CPP_STD_PAIR = "std::pair"; - private static final String CPP_STD_MAP = "std::map"; - private static final String CPP_STD_SET = "std::set"; - private static final String CPP_STD_DEQUE = "std::deque"; - private static final String CPP_STD_MULTIMAP = "std::multimap"; - private static final String CPP_STD_MULTISET = "std::multiset"; - - private static final String CPP_STR_SUFFIX = "STR"; - private static final String CPP_FILE_PREFIX = "ag_"; - private static final String CPP_FILE_H_SUFFIX = ".h"; - private static final String CPP_FILE_CPP_SUFFIX = ".cpp"; - private static final String CPP_FILE_C_SUFFIX = ".c"; - private static final String CPP_STRUCT_SUFFIX = "ST"; - + private static final String NAPI_ENUM_TOKEN = "enum"; + private static final String NAPI_CLASS_TOKEN = "class"; + private static final String NAPI_STRUCT_TOKEN = "struct"; + private static final String NAPI_UNION_TOKEN = "union"; + private static final String NAPI_TEMPLATE_TOKEN = "template"; + private static final String NAPI_TYPE_NAME_TOKEN = "typename"; + private static final String NAPI_STAR_TOKEN = "*"; + private static final String NAPI_CHAR_START_TOKEN = "char*"; + private static final String NAPI_AUTO_TOKEN = "auto"; + private static final String NAPI_EXPORT_TOKEN = "export"; + private static final String NAPI_IMPLEMENNAPI_TOKEN = "implements"; + private static final String NAPI_EXTENDS_TOKEN = "extends"; + private static final String NAPI_CONST_TOKEN = "const"; + private static final String NAPI_PRIVATE_TOKEN = "private"; + private static final String NAPI_PUBLIC_TOKEN = "public"; + private static final String NAPI_INTERFACE_TOKEN = "interface"; + private static final String NAPI_PROTECTED_TOKEN = "protected"; + private static final String NAPI_STATIC_TOKEN = "static"; + private static final String NAPI_ANY_TOKEN = "any"; + private static final String NAPI_NUMBER_TOKEN = "number"; + private static final String NAPI_NEVER_TOKEN = "never"; + private static final String NAPI_BOOLEAN_TOKEN = "boolean"; + private static final String NAPI_STRING_TOKEN = "string"; + private static final String NAPI_UNIQUE_TOKEN = "unique"; + private static final String NAPI_SYMBOL_TOKEN = "symbol"; + private static final String NAPI_UNDEFINED_TOKEN = "undefined"; + private static final String NAPI_OBJECT_TOKEN = "object"; + private static final String NAPI_OF_TOKEN = "of"; + private static final String NAPI_KEYOF_TOKEN = "keyof"; + private static final String NAPI_TYPE_TOKEN = "type"; + private static final String NAPI_CONSTRUCTOR_TOKEN = "constructor"; + private static final String NAPI_NAMESPACE_TOKEN = "namespace"; + private static final String NAPI_REQUIRE_TOKEN = "require"; + private static final String NAPI_MODULE_TOKEN = "module"; + private static final String NAPI_DECLARE_TOKEN = "declare"; + private static final String NAPI_ABSTRACT_TOKEN = "abstract"; + private static final String NAPI_DEBUGGER_TOKEN = "debugger"; + private static final String NAPI_FUNCTION_TOKEN = "function"; + private static final String NAPI_THIS_TOKEN = "this"; + private static final String NAPI_WITH_TOKEN = "with"; + private static final String NAPI_DEFAULT_TOKEN = "default"; + private static final String NAPI_READONLY_TOKEN = "readonly"; + private static final String NAPI_ASYNC_TOKEN = "async"; + private static final String NAPI_AWAIT_TOKEN = "await"; + private static final String NAPI_YIELD_TOKEN = "yield"; + private static final String NAPI_NEW_LINE = "\n"; + private static final String NAPI_TAB_SPACE = "\t"; + private static final String NAPI_BLANK_SPACE = " "; + private static final String NAPI_SPLIT = " | "; + private static final String NAPI_EQUAL = " = "; + private static final String NAPI_COMMA = ","; + private static final String NAPI_DOUBLE_QUOTATION = "\""; + private static final String NAPI_UNDER_LINE = "_"; + private static final String NAPI_SEMICOLON = ";"; + private static final String NAPI_COLON = ":"; + private static final String NAPI_ELLIPSIS = "..."; + private static final String NAPI_DOT = "."; + private static final String NAPI_LEFT_BRACE = "{"; + private static final String NAPI_RIGHT_BRACE = "}"; + private static final String NAPI_LEFT_PARENTHESES = "("; + private static final String NAPI_RIGHT_PARENTHESES = ")"; + private static final String NAPI_LEFT_SQUARE_BRACKET = "["; + private static final String NAPI_RIGHT_SQUARE_BRACKET = "]"; + private static final String NAPI_LEFT_ANGLE_BRACKET = "<"; + private static final String NAPI_RIGHT_ANGLE_BRACKET = ">"; + + private static final String NAPI_STD_STRING = "std::string"; + private static final String NAPI_STD_VECTOR = "std::vector"; + private static final String NAPI_STD_LIST = "std::list"; + private static final String NAPI_STD_ARRAY = "std::array"; + private static final String NAPI_STD_STACK = "std::stack"; + private static final String NAPI_STD_QUEUE = "std::queue"; + private static final String NAPI_STD_PAIR = "std::pair"; + private static final String NAPI_STD_MAP = "std::map"; + private static final String NAPI_STD_SET = "std::set"; + private static final String NAPI_STD_DEQUE = "std::deque"; + private static final String NAPI_STD_MULTIMAP = "std::multimap"; + private static final String NAPI_STD_MULTISET = "std::multiset"; + + private static final String NAPI_STR_SUFFIX = "STR"; + private static final String NAPI_FILE_PREFIX = "ag_"; + private static final String NAPI_FILE_H_SUFFIX = ".h"; + private static final String NAPI_FILE_NAPI_SUFFIX = ".cpp"; + private static final String NAPI_FILE_C_SUFFIX = ".c"; + private static final String NAPI_STRUCT_SUFFIX = "ST"; + + private static final String NAPI_ENUM_NAME = "NAPI_ENUM_NAME"; + private static final String NAPI_ENUM_MEM_LIST = "NAPI_ENUM_MEM_LIST"; + private static final String NAPI_ENUM_VAL_LIST = "NAPI_ENUM_VAL_LIST"; + private static final String NAPI_ENUM_CNT = "NAPI_ENUM_CNT"; + private static final String NAPI_ENUM_ITEM_VALUE = "NAPI_ENUM_ITEM_VALUE"; + private static final String NAPI_ENUM_VALUE_INDEX = "i"; + private static final String NAPI_ENUM_VALUE_ITER = "value[i]"; + private static final String NAPI_CREATE_ENUM_DECLARE = "\n// 创建枚举对象\n" + + "napi_value CreateNAPI_ENUM_NAMEEnum(napi_env env) {\n" + + "\tnapi_value enum_obj;\n" + + "\tnapi_create_object(env, &enum_obj);\n" + + "\n" + + "\t// 添加枚举成员\n" + + "\tconst char* members[] = {NAPI_ENUM_MEM_LIST};\n" + + "\tconst int values[] = {NAPI_ENUM_VAL_LIST};\n" + + "\tfor (int32_t i = 0; i < NAPI_ENUM_CNT; ++i) {\n" + + "\t\tnapi_value value;\n" + + "\t\tnapi_create_int32(env, NAPI_ENUM_ITEM_VALUE, &value);\n" + + "\t\tnapi_set_named_property(env, enum_obj, members[i], value);\n" + + "\t}\n" + + "\n" + + "\treturn enum_obj;\n" + + "}\n"; + + private static final String NAPI_EXPORT_ENUM = "\t// 创建并绑定枚举\n" + + "\tnapi_value NAPI_ENUM_NAME_enum = CreateNAPI_ENUM_NAMEEnum(env);\n" + + "\tnapi_set_named_property(env, exports, \"NAPI_ENUM_NAME\", NAPI_ENUM_NAME_enum);\n"; + + private static final String NAPI_CLASS_NAME = "NAPI_CLASS_NAME"; + private static final String NAPI_CLASS_METHOD_NAME = "NAPI_CLASS_METHOD_NAME"; + private static final String NAPI_CLASS_ATTRIBUTE_NAME = "NAPI_CLASS_ATTRIBUTE_NAME"; + private static final String NAPI_CONSTRUCTOR_EXPRESSION = "NAPI_CONSTRUCTOR_EXPRESSION"; + private static final String NAPI_CONSTRUCTOR_DECLARE = "\n\tJSBIND_CONSTRUCTOR();"; + private static final String NAPI_CONSTRUCTOR_PARAMS = "NAPI_CONSTRUCTOR_PARAMS"; + private static final String NAPI_METHOD_DECLARE = "\n\tJSBIND_METHOD(NAPI_METHOD_NAME, \"NAPI_METHOD_NAME\");" + + "\n\tJSBIND_PMETHOD(NAPI_METHOD_NAME, \"NAPI_METHOD_NAMEPromise\");"; + private static final String NAPI_METHOD_EXPRESSION = "NAPI_METHOD_EXPRESSION"; + private static final String NAPI_METHOD_NAME = "NAPI_METHOD_NAME"; + private static final String NAPI_PMETHOD_DECLARE = "\n\tJSBIND_PMETHOD(NAPI_PMETHOD_NAME);"; + private static final String NAPI_PMETHOD_EXPRESSION = "NAPI_PMETHOD_EXPRESSION"; + private static final String NAPI_PMETHOD_NAME = "NAPI_PMETHOD_NAME"; + private static final String NAPI_PROPERTY_DECLARE = "\n\tJSBIND_PROPERTY(NAPI_PROPERTY_NAME);"; + private static final String NAPI_PROPERTY_EXPRESSION = "NAPI_PROPERTY_EXPRESSION"; + private static final String NAPI_PROPERTY_NAME = "NAPI_PROPERTY_NAME"; + + + private static final String NAPI_CLASS_CONSTRUCTURE = + "\nnapi_value ConstructorNAPI_CLASS_NAME(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + + "\tnapi_get_undefined(env, &undefineVar);\n" + + "\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\t\tNAPI_CLASS_NAME *reference = new NAPI_CLASS_NAME();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorNAPI_CLASS_NAME, nullptr, nullptr) == napi_ok) {\n" + + "\t\t\treturn thisVar;\n" + + "\t\t}\n" + + "\t\treturn thisVar;\n" + + "\t}\n" + + "\treturn undefineVar;\n" + + "};\n"; + private static final String NAPI_CLASS_DESTRUCTURE = + "\nvoid DestructorNAPI_CLASS_NAME(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n"; + private static final String NAPI_CLASS_GET_ATTRIBUTE_DECLARE = + "\nnapi_value GetNAPI_CLASS_ATTRIBUTE_NAME(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tNAPI_CLASS_NAME *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n"; + private static final String NAPI_CLASS_SET_ATTRIBUTE_DECLARE = + "\nnapi_value SetNAPI_CLASS_ATTRIBUTE_NAME(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tNAPI_CLASS_NAME *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->NAPI_CLASS_ATTRIBUTE_NAME = msg;\n" + + "\treturn nullptr;\n" + + "};\n"; + private static final String NAPI_CLASS_METHOD_DECLARE = + "\nnapi_value NAPI_CLASS_METHOD_NAMENAPI_CLASS_NAME(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tNAPI_CLASS_NAME *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n"; + private static final String NAPI_CLASS_METHOD_PROPERTY = "NAPI_CLASS_METHOD_PROPERTY"; + private static final String NAPI_CLASS_ATTRIBUTE_PROPERTY = "NAPI_CLASS_ATTRIBUTE_PROPERTY"; + private static final String NAPI_CLASS_METHOD_PROPERTY_DECLARE = + "\t{NAPI_CLASS_METHOD_NAME, nullptr, NAPI_CLASS_METHOD_NAMENAPI_CLASS_NAME, " + + "nullptr, nullptr, nullptr, napi_default, nullptr},\n"; + private static final String NAPI_CLASS_ATTRIBUTE_PROPERTY_DECLARE = + "\t{NAPI_CLASS_ATTRIBUTE_NAME, nullptr, nullptr, GetNAPI_CLASS_ATTRIBUTE_NAMENAPI_CLASS_NAME, " + + "SetNAPI_CLASS_ATTRIBUTE_NAMENAPI_CLASS_NAME, nullptr, napi_default, nullptr},\n"; + private static final String NAPI_CLASS_PROPERTY_DECLARE = + "\nnapi_property_descriptor NAPI_CLASS_NAMEProps[] = {\n" + + "NAPI_CLASS_METHOD_PROPERTY" + + "NAPI_CLASS_ATTRIBUTE_PROPERTY" + + "};\n"; + private static final String NAPI_CLASS_INIT = "\nnapi_value NAPI_CLASS_NAMEIns = nullptr;\n" + + "\tif (napi_define_class(env, \"NAPI_CLASS_NAME\", NAPI_AUTO_LENGTH, " + + "ConstructorNAPI_CLASS_NAME, nullptr, sizeof(NAPI_CLASS_NAMEProps) / " + + "sizeof(NAPI_CLASS_NAMEProps[0]), NAPI_CLASS_NAMEProps, " + + "&NAPI_CLASS_NAMEIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"NAPI_CLASS_NAME\", NAPI_CLASS_NAMEIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + private String interfaceContent = ""; private String enumContent = ""; private String classContent = ""; @@ -130,20 +282,35 @@ public class GenNapiCppFile extends GeneratorBase { private String constContent = ""; private final Map ts2cppMap = Map.ofEntries( - Map.entry("any", "auto"), - Map.entry("boolean", "bool"), - Map.entry("string", "char*"), - Map.entry("number", "int"), - Map.entry("void", "void"), - Map.entry("[]", "*") + Map.entry("any", "auto"), + Map.entry("boolean", "bool"), + Map.entry("string", "std::string"), + Map.entry("number", "int"), + Map.entry("void", "void"), + Map.entry("[]", "*") ); private final Map tsTokenMap = Map.ofEntries( - Map.entry("\"", ""), - Map.entry("*", ""), - Map.entry("&", ""), - Map.entry("(", ""), - Map.entry(")", "") + Map.entry("\"", ""), + Map.entry("*", ""), + Map.entry("&", ""), + Map.entry("(", ""), + Map.entry(")", "") + ); + + private final Map getArguMap = Map.ofEntries( + Map.entry("bool", "auto"), + Map.entry("string", "napi_get_value_string_utf8"), + Map.entry("int", "api_get_value_int32"), + Map.entry("uint", "api_get_value_int32"), + Map.entry("object", "auto") + ); + + private final Map setArguMap = Map.ofEntries( + Map.entry("bool", "auto"), + Map.entry("string", "auto"), + Map.entry("int", "auto"), + Map.entry("object", "auto") ); /** @@ -168,7 +335,7 @@ public class GenNapiCppFile extends GeneratorBase { String key = entry.getKey(); String value = entry.getValue(); int ret = cppKey.indexOf(key); - if (ret >= 0 && value.contains(CPP_STAR_TOKEN)) { + if (ret >= 0 && value.contains(NAPI_STAR_TOKEN)) { return cppKey.substring(0, ret) + value; } else if (ret >= 0) { return value; @@ -294,8 +461,8 @@ public class GenNapiCppFile extends GeneratorBase { @Override public void genFile(String filePath, String fileName) { System.out.println("genFile : " + filePath + fileName); - String outFileName = filePath + File.separator + CPP_FILE_PREFIX + - fileName.replace(".", "_") + CPP_FILE_H_SUFFIX; + String outFileName = filePath + File.separator + NAPI_FILE_PREFIX + + fileName.replace(".", "_") + NAPI_FILE_H_SUFFIX; System.out.println("outFileName : " + outFileName); FileUtils.createFile(outFileName); @@ -322,6 +489,74 @@ public class GenNapiCppFile extends GeneratorBase { System.out.println("genInterfaceList" + iol.toString()); }; + private String genCppEnumContent(EnumObj eo) { + String resContent = ""; + String enumName = eo.getName(); + enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); + List memList = eo.getMemberList(); + List vaList = eo.getValueList(); + int i = 0; + resContent += NAPI_NEW_LINE + NAPI_ENUM_TOKEN + + NAPI_BLANK_SPACE + enumName + NAPI_BLANK_SPACE + NAPI_LEFT_BRACE; + for (String memItem : memList) { + resContent += NAPI_NEW_LINE + NAPI_TAB_SPACE + memItem; + if (vaList.size() > i && !vaList.get(i).isEmpty()) { + resContent += NAPI_EQUAL + replaceTsToken(vaList.get(i)) + NAPI_COMMA; + } else { + resContent += NAPI_COMMA; + } + i++; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; + + i = 0; + if (vaList.size() > i && !vaList.get(i).isEmpty() && + vaList.get(i).contains("\"")) { + resContent += NAPI_NEW_LINE + NAPI_CHAR_START_TOKEN + NAPI_BLANK_SPACE + + enumName.toLowerCase(Locale.ROOT) + NAPI_UNDER_LINE + NAPI_STR_SUFFIX + + NAPI_LEFT_SQUARE_BRACKET + NAPI_RIGHT_SQUARE_BRACKET + NAPI_EQUAL + NAPI_LEFT_BRACE; + for (String val : vaList) { + resContent += NAPI_NEW_LINE + NAPI_TAB_SPACE + NAPI_LEFT_SQUARE_BRACKET + + memList.get(i) + NAPI_RIGHT_SQUARE_BRACKET + NAPI_EQUAL + val + NAPI_COMMA; + i++; + } + resContent = StringUtils.removeLastCharacter(resContent, 1); + resContent += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; + } + return resContent; + } + + private String genNapiEnumContent(EnumObj eo) { + String resContent = ""; + String enumName = eo.getName(); + enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); + List memList = eo.getMemberList(); + String enumNameList = ""; + for (String memItem : memList) { + enumNameList += NAPI_DOUBLE_QUOTATION + memItem + NAPI_DOUBLE_QUOTATION + NAPI_COMMA + NAPI_BLANK_SPACE; + } + enumNameList = StringUtils.removeLastCharacter(enumNameList, 2); + + String enumValueList = ""; + List valueList = eo.getValueList(); + for (String valItem : valueList) { + enumValueList += valItem + NAPI_COMMA + NAPI_BLANK_SPACE; + } + enumValueList = StringUtils.removeLastCharacter(enumValueList, 2); + + String enumDeclare = NAPI_CREATE_ENUM_DECLARE.replace(NAPI_ENUM_NAME, enumName); + enumDeclare = enumDeclare.replace(NAPI_ENUM_MEM_LIST, enumNameList); + enumDeclare = enumDeclare.replace(NAPI_ENUM_VAL_LIST, enumValueList); + enumDeclare = enumDeclare.replace(NAPI_ENUM_ITEM_VALUE, valueList.isEmpty() ? + NAPI_ENUM_VALUE_INDEX : NAPI_ENUM_VALUE_ITER); + enumDeclare = enumDeclare.replace(NAPI_ENUM_CNT, Integer.toString(memList.size())); + + resContent = enumDeclare + NAPI_EXPORT_ENUM.replace(NAPI_ENUM_NAME, enumName); + return resContent; + } + /** * 生成输出内容 * @@ -333,41 +568,8 @@ public class GenNapiCppFile extends GeneratorBase { String resContent = ""; for (EnumObj eo : eol) { - String enumName = eo.getName(); - enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); - List memList = eo.getMemberList(); - List vaList = eo.getValueList(); - int i = 0; - resContent += CPP_NEW_LINE + CPP_ENUM_TOKEN + - CPP_BLANK_SPACE + enumName + CPP_BLANK_SPACE + CPP_LEFT_BRACE; - for (String memItem : memList) { - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + memItem; - if (vaList.size() > i && !vaList.get(i).isEmpty()) { - resContent += CPP_EQUAL + replaceTsToken(vaList.get(i)) + CPP_COMMA; - } else { - resContent += CPP_COMMA; - } - i++; - } - - resContent = StringUtils.removeLastSpace(resContent); - resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; - - i = 0; - if (vaList.size() > i && !vaList.get(i).isEmpty() && - vaList.get(i).contains("\"")) { - resContent += CPP_NEW_LINE + CPP_CHAR_START_TOKEN + CPP_BLANK_SPACE + - enumName.toLowerCase(Locale.ROOT) + CPP_UNDER_LINE + CPP_STR_SUFFIX + - CPP_LEFT_SQUARE_BRACKET + CPP_RIGHT_SQUARE_BRACKET + CPP_EQUAL + CPP_LEFT_BRACE; - for (String val : vaList) { - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_SQUARE_BRACKET + - memList.get(i) + CPP_RIGHT_SQUARE_BRACKET + CPP_EQUAL + val + CPP_COMMA; - i++; - } - resContent = StringUtils.removeLastCharacter(resContent, 1); - resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; - - } + resContent += genCppEnumContent(eo); + resContent += genNapiEnumContent(eo); } this.enumContent = resContent; @@ -377,24 +579,130 @@ public class GenNapiCppFile extends GeneratorBase { String tempResContent = content; for (FuncObj funcItem : funcList) { String retValue = funcItem.getRetValue(); - retValue = retValue.isEmpty() ? "" : ts2CppKey(retValue) + CPP_BLANK_SPACE; - tempResContent += CPP_NEW_LINE + CPP_TAB_SPACE + retValue + - replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; + retValue = retValue.isEmpty() ? "" : ts2CppKey(retValue) + NAPI_BLANK_SPACE; + tempResContent += NAPI_NEW_LINE + NAPI_TAB_SPACE + retValue + + replaceTsToken(funcItem.getName()) + NAPI_LEFT_PARENTHESES; List pol = funcItem.getParamList(); for (ParamObj poItem : pol) { String retType = ts2CppKey(poItem.getType()).isEmpty() ? - CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); - tempResContent += (poItem.getName() == null) ? retType + CPP_COMMA + CPP_BLANK_SPACE : - retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + CPP_COMMA + CPP_BLANK_SPACE; + NAPI_AUTO_TOKEN : ts2CppKey(poItem.getType()); + tempResContent += (poItem.getName() == null) ? retType + NAPI_COMMA + NAPI_BLANK_SPACE : + retType + NAPI_BLANK_SPACE + replaceTsToken(poItem.getName()) + NAPI_COMMA + NAPI_BLANK_SPACE; } if (!pol.isEmpty()) { tempResContent = StringUtils.removeLastCharacter(tempResContent, 2); } - tempResContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + tempResContent += NAPI_RIGHT_PARENTHESES + NAPI_SEMICOLON; } return tempResContent; } + private String genCppClassContent(ClassObj co) { + String className = co.getName(); + className = !className.isEmpty() ? className : co.getAlias(); + + String templateStr = !co.getTempList().isEmpty() ? + NAPI_TEMPLATE_TOKEN + NAPI_BLANK_SPACE + NAPI_LEFT_ANGLE_BRACKET : ""; + for (String teStr : co.getTempList()) { + templateStr += NAPI_TYPE_NAME_TOKEN + NAPI_BLANK_SPACE + teStr + NAPI_COMMA + NAPI_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + NAPI_RIGHT_ANGLE_BRACKET + NAPI_BLANK_SPACE : ""; + + List hnList = co.getHeritageNameList(); + String htStr = hnList.size() > 0 ? NAPI_BLANK_SPACE + NAPI_COLON + NAPI_BLANK_SPACE : ""; + for (String hName : hnList) { + htStr += NAPI_PUBLIC_TOKEN + NAPI_BLANK_SPACE + hName + NAPI_COMMA + NAPI_BLANK_SPACE; + } + htStr = htStr.length() > 1 ? StringUtils.removeLastCharacter(htStr, 2) : htStr; + + List htempList = co.getHeritageTemplateList(); + String htempStr = htempList.size() > 0 ? NAPI_LEFT_ANGLE_BRACKET : ""; + for (String tempStr : htempList) { + htempStr += tempStr + NAPI_COMMA + NAPI_BLANK_SPACE; + } + htempStr = htempList.size() > 0 ? + StringUtils.removeLastCharacter(htempStr, 2) + NAPI_RIGHT_ANGLE_BRACKET : ""; + String resContent = ""; + resContent += NAPI_NEW_LINE + templateStr + NAPI_CLASS_TOKEN + + NAPI_BLANK_SPACE + className + htStr + htempStr + NAPI_BLANK_SPACE + NAPI_LEFT_BRACE; + List paList = co.getParamList(); + for (ParamObj paItem : paList) { + String paType = paItem.getType(); + String qualifyStr = paItem.getQualifier() == null || paItem.getQualifier().isEmpty() ? + "" : paItem.getQualifier() + NAPI_BLANK_SPACE; + resContent += NAPI_NEW_LINE + NAPI_TAB_SPACE + qualifyStr + ts2CppKey(paType) + + NAPI_BLANK_SPACE + replaceTsToken(paItem.getName()); + List initVList = paItem.getvList(); + if (!initVList.isEmpty()) { + resContent += NAPI_EQUAL + initVList.get(0) + NAPI_SEMICOLON; + } else { + resContent += NAPI_SEMICOLON; + } + } + + resContent = setClassFunc(co.getFuncList(), resContent); + + resContent += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; + return resContent; + } + + private String genNapiClassContent(ClassObj co) { + String resContent = ""; + String className = co.getName(); + className = !className.isEmpty() ? className : co.getAlias(); + List funcList = co.getFuncList(); + String classNameList = ""; + String classMethodContent = ""; + String classMethodProperty = ""; + for (FuncObj funcItem : funcList) { + String classMethodStr = NAPI_CLASS_METHOD_DECLARE.replace( + NAPI_CLASS_NAME, className); + classMethodStr = classMethodStr.replace( + NAPI_CLASS_METHOD_NAME, funcItem.getName()); + classMethodContent += classMethodStr; + + String classMethodPropertyStr = NAPI_CLASS_METHOD_PROPERTY_DECLARE.replace( + NAPI_CLASS_METHOD_NAME, funcItem.getName()); + classMethodPropertyStr = classMethodPropertyStr.replace(NAPI_CLASS_NAME, className); + classMethodProperty += classMethodPropertyStr; + } + + String classAttributeContent = ""; + String classAttributeProperty = ""; + List paList = co.getParamList(); + for (ParamObj paItem : paList) { + String getAttributeContent = NAPI_CLASS_GET_ATTRIBUTE_DECLARE.replace( + NAPI_CLASS_ATTRIBUTE_NAME, paItem.getName()); + getAttributeContent = getAttributeContent.replace(NAPI_CLASS_NAME, className); + String setAttributeContent = NAPI_CLASS_SET_ATTRIBUTE_DECLARE.replace( + NAPI_CLASS_ATTRIBUTE_NAME, paItem.getName()); + setAttributeContent = setAttributeContent.replace(NAPI_CLASS_NAME, className); + classAttributeContent += getAttributeContent + setAttributeContent; + + String classAttributeStr = NAPI_CLASS_ATTRIBUTE_PROPERTY_DECLARE.replace( + NAPI_CLASS_ATTRIBUTE_NAME, paItem.getName()); + classAttributeStr = classAttributeStr.replace(NAPI_CLASS_NAME, className); + classAttributeProperty += classAttributeStr; + } + + + String classDeclare = NAPI_CLASS_CONSTRUCTURE.replace(NAPI_CLASS_NAME, className); + classDeclare += NAPI_CLASS_DESTRUCTURE.replace(NAPI_CLASS_NAME, className); + classDeclare += classMethodContent + classAttributeContent; + + String classPropertyStr = NAPI_CLASS_PROPERTY_DECLARE.replace(NAPI_CLASS_NAME, className); + classPropertyStr = classPropertyStr.replace(NAPI_CLASS_METHOD_PROPERTY, classMethodProperty); + classPropertyStr = classPropertyStr.replace(NAPI_CLASS_ATTRIBUTE_PROPERTY, classAttributeProperty); + classDeclare += classPropertyStr; + + String classInitStr = NAPI_CLASS_INIT.replace(NAPI_CLASS_NAME, className); + classDeclare += classInitStr; + + resContent = classDeclare; + return resContent; + } + /** * 生成输出内容 * @@ -406,51 +714,8 @@ public class GenNapiCppFile extends GeneratorBase { String resContent = ""; for (ClassObj co : col) { - String className = co.getName(); - className = !className.isEmpty() ? className : co.getAlias(); - - String templateStr = !co.getTempList().isEmpty() ? - CPP_TEMPLATE_TOKEN + CPP_BLANK_SPACE + CPP_LEFT_ANGLE_BRACKET : ""; - for (String teStr : co.getTempList()) { - templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; - } - templateStr = templateStr.length() > 1 ? - StringUtils.removeLastCharacter(templateStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE : ""; - - List hnList = co.getHeritageNameList(); - String htStr = hnList.size() > 0 ? CPP_BLANK_SPACE + CPP_COLON + CPP_BLANK_SPACE : ""; - for (String hName : hnList) { - htStr += CPP_PUBLIC_TOKEN + CPP_BLANK_SPACE + hName + CPP_COMMA + CPP_BLANK_SPACE; - } - htStr = htStr.length() > 1 ? StringUtils.removeLastCharacter(htStr, 2) : htStr; - - List htempList = co.getHeritageTemplateList(); - String htempStr = htempList.size() > 0 ? CPP_LEFT_ANGLE_BRACKET : ""; - for (String tempStr : htempList) { - htempStr += tempStr + CPP_COMMA + CPP_BLANK_SPACE; - } - htempStr = htempList.size() > 0 ? - StringUtils.removeLastCharacter(htempStr, 2) + CPP_RIGHT_ANGLE_BRACKET : ""; - resContent += CPP_NEW_LINE + templateStr + CPP_CLASS_TOKEN + - CPP_BLANK_SPACE + className + htStr + htempStr + CPP_BLANK_SPACE + CPP_LEFT_BRACE; - List paList = co.getParamList(); - for (ParamObj paItem : paList) { - String paType = paItem.getType(); - String qualifyStr = paItem.getQualifier() == null || paItem.getQualifier().isEmpty() ? - "" : paItem.getQualifier() + CPP_BLANK_SPACE; - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + qualifyStr + ts2CppKey(paType) + - CPP_BLANK_SPACE + replaceTsToken(paItem.getName()); - List initVList = paItem.getvList(); - if (!initVList.isEmpty()) { - resContent += CPP_EQUAL + initVList.get(0) + CPP_SEMICOLON; - } else { - resContent += CPP_SEMICOLON; - } - } - - resContent = setClassFunc(co.getFuncList(), resContent); - - resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + resContent += genCppClassContent(co); + resContent += genNapiClassContent(co); } this.classContent = resContent; }; @@ -468,32 +733,32 @@ public class GenNapiCppFile extends GeneratorBase { String funcName = fo.getName(); funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); List tempList = fo.getTempList(); - String tempStr = tempList.isEmpty() ? "" : CPP_TEMPLATE_TOKEN + CPP_LEFT_ANGLE_BRACKET; + String tempStr = tempList.isEmpty() ? "" : NAPI_TEMPLATE_TOKEN + NAPI_LEFT_ANGLE_BRACKET; for (String teStr : tempList) { - tempStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; + tempStr += NAPI_TYPE_NAME_TOKEN + NAPI_BLANK_SPACE + teStr + NAPI_COMMA + NAPI_BLANK_SPACE; } tempStr = tempList.isEmpty() ? "" : - StringUtils.removeLastCharacter(tempStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE; + StringUtils.removeLastCharacter(tempStr, 2) + NAPI_RIGHT_ANGLE_BRACKET + NAPI_BLANK_SPACE; List paList = fo.getParamList(); String retValue = ts2CppKey(fo.getRetValue()).isEmpty() ? - "" : ts2CppKey(fo.getRetValue()) + CPP_BLANK_SPACE; - resContent += CPP_NEW_LINE + tempStr + retValue + - replaceTsToken(funcName) + CPP_LEFT_PARENTHESES; + "" : ts2CppKey(fo.getRetValue()) + NAPI_BLANK_SPACE; + resContent += NAPI_NEW_LINE + tempStr + retValue + + replaceTsToken(funcName) + NAPI_LEFT_PARENTHESES; for (ParamObj poItem : paList) { String paType = ts2CppKey(poItem.getType()).isEmpty() ? - CPP_AUTO_TOKEN + CPP_BLANK_SPACE : ts2CppKey(poItem.getType()) + CPP_BLANK_SPACE; + NAPI_AUTO_TOKEN + NAPI_BLANK_SPACE : ts2CppKey(poItem.getType()) + NAPI_BLANK_SPACE; String paName = poItem.getName(); String defaultVal = poItem.getStrValue(0); - defaultVal = defaultVal.isEmpty() ? "" : CPP_EQUAL + defaultVal; + defaultVal = defaultVal.isEmpty() ? "" : NAPI_EQUAL + defaultVal; resContent += !paName.isEmpty() ? paType + replaceTsToken(paName) + - defaultVal + CPP_COMMA + CPP_BLANK_SPACE : - paType + CPP_COMMA + CPP_BLANK_SPACE; + defaultVal + NAPI_COMMA + NAPI_BLANK_SPACE : + paType + NAPI_COMMA + NAPI_BLANK_SPACE; } if (!paList.isEmpty()) { resContent = StringUtils.removeLastCharacter(resContent, 2); } - resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + resContent += NAPI_RIGHT_PARENTHESES + NAPI_SEMICOLON; } this.funcContent = resContent; System.out.println("genFuncList : " + resContent); @@ -514,49 +779,49 @@ public class GenNapiCppFile extends GeneratorBase { structName = !structName.isEmpty() ? structName : so.getAlias(); String templateStr = !so.getTemplateList().isEmpty() ? - CPP_TEMPLATE_TOKEN + CPP_BLANK_SPACE + CPP_LEFT_ANGLE_BRACKET : ""; + NAPI_TEMPLATE_TOKEN + NAPI_BLANK_SPACE + NAPI_LEFT_ANGLE_BRACKET : ""; for (String teStr : so.getTemplateList()) { - templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; + templateStr += NAPI_TYPE_NAME_TOKEN + NAPI_BLANK_SPACE + teStr + NAPI_COMMA + NAPI_BLANK_SPACE; } templateStr = templateStr.length() > 1 ? - StringUtils.removeLastCharacter(templateStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE : ""; + StringUtils.removeLastCharacter(templateStr, 2) + NAPI_RIGHT_ANGLE_BRACKET + NAPI_BLANK_SPACE : ""; List paList = so.getMemberList(); - resContent += CPP_NEW_LINE + templateStr + CPP_STRUCT_TOKEN + - CPP_BLANK_SPACE + structName + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + resContent += NAPI_NEW_LINE + templateStr + NAPI_STRUCT_TOKEN + + NAPI_BLANK_SPACE + structName + NAPI_BLANK_SPACE + NAPI_LEFT_BRACE; for (ParamObj paItem : paList) { - String paType = paItem.getType().isEmpty() ? CPP_AUTO_TOKEN : paItem.getType(); - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + ts2CppKey(paType) + - CPP_BLANK_SPACE + paItem.getName(); + String paType = paItem.getType().isEmpty() ? NAPI_AUTO_TOKEN : paItem.getType(); + resContent += NAPI_NEW_LINE + NAPI_TAB_SPACE + ts2CppKey(paType) + + NAPI_BLANK_SPACE + paItem.getName(); ; List initVList = paItem.getvList(); if (initVList.size() > 0) { - resContent += CPP_EQUAL + initVList.get(0) + CPP_SEMICOLON; + resContent += NAPI_EQUAL + initVList.get(0) + NAPI_SEMICOLON; } else { - resContent += CPP_SEMICOLON; + resContent += NAPI_SEMICOLON; } } List funcList = so.getFuncList(); for (FuncObj funcItem : funcList) { String retValue = ts2CppKey(funcItem.getRetValue()).isEmpty() ? "" : - ts2CppKey(funcItem.getRetValue()) + CPP_BLANK_SPACE; - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + retValue + - replaceTsToken(funcItem.getName()) + CPP_LEFT_PARENTHESES; + ts2CppKey(funcItem.getRetValue()) + NAPI_BLANK_SPACE; + resContent += NAPI_NEW_LINE + NAPI_TAB_SPACE + retValue + + replaceTsToken(funcItem.getName()) + NAPI_LEFT_PARENTHESES; List pol = funcItem.getParamList(); for (ParamObj poItem : pol) { String retType = ts2CppKey(poItem.getType()).isEmpty() ? - CPP_AUTO_TOKEN : ts2CppKey(poItem.getType()); - resContent += retType + CPP_BLANK_SPACE + replaceTsToken(poItem.getName()) + - CPP_COMMA + CPP_BLANK_SPACE; + NAPI_AUTO_TOKEN : ts2CppKey(poItem.getType()); + resContent += retType + NAPI_BLANK_SPACE + replaceTsToken(poItem.getName()) + + NAPI_COMMA + NAPI_BLANK_SPACE; } resContent = !pol.isEmpty() ? StringUtils.removeLastCharacter(resContent, 2) : resContent; - resContent += CPP_RIGHT_PARENTHESES + CPP_SEMICOLON; + resContent += NAPI_RIGHT_PARENTHESES + NAPI_SEMICOLON; } resContent = StringUtils.removeLastSpace(resContent); - resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + resContent += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; } this.structContent = resContent; }; @@ -586,58 +851,58 @@ public class GenNapiCppFile extends GeneratorBase { unionName = !unionName.isEmpty() ? unionName : uo.getAlias(); String templateStr = !uo.getTemplateList().isEmpty() ? - CPP_TEMPLATE_TOKEN + CPP_BLANK_SPACE + CPP_LEFT_ANGLE_BRACKET : ""; + NAPI_TEMPLATE_TOKEN + NAPI_BLANK_SPACE + NAPI_LEFT_ANGLE_BRACKET : ""; for (String teStr : uo.getTemplateList()) { - templateStr += CPP_TYPE_NAME_TOKEN + CPP_BLANK_SPACE + teStr + CPP_COMMA + CPP_BLANK_SPACE; + templateStr += NAPI_TYPE_NAME_TOKEN + NAPI_BLANK_SPACE + teStr + NAPI_COMMA + NAPI_BLANK_SPACE; } templateStr = templateStr.length() > 1 ? - StringUtils.removeLastCharacter(templateStr, 2) + CPP_RIGHT_ANGLE_BRACKET + CPP_BLANK_SPACE : ""; + StringUtils.removeLastCharacter(templateStr, 2) + NAPI_RIGHT_ANGLE_BRACKET + NAPI_BLANK_SPACE : ""; - resContent += CPP_NEW_LINE + templateStr + CPP_UNION_TOKEN + - CPP_BLANK_SPACE + unionName + CPP_LEFT_BRACE; + resContent += NAPI_NEW_LINE + templateStr + NAPI_UNION_TOKEN + + NAPI_BLANK_SPACE + unionName + NAPI_LEFT_BRACE; List paList = uo.getMemList(); for (ParamObj paItem : paList) { String paType = paItem.getType(); String paName = paItem.getName(); - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + ts2CppKey(paType) - + CPP_BLANK_SPACE + paName + CPP_SEMICOLON; + resContent += NAPI_NEW_LINE + NAPI_TAB_SPACE + ts2CppKey(paType) + + NAPI_BLANK_SPACE + paName + NAPI_SEMICOLON; } - resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE; - resContent += CPP_SEMICOLON + CPP_NEW_LINE; + resContent += NAPI_NEW_LINE + NAPI_RIGHT_BRACE; + resContent += NAPI_SEMICOLON + NAPI_NEW_LINE; } this.unionContent = resContent; }; private String genVarArrayList(String tmpContent, String paName, List paList) { String resContent = tmpContent; - resContent += CPP_NEW_LINE + CPP_STRUCT_TOKEN + CPP_BLANK_SPACE + paName + - CPP_STRUCT_SUFFIX + CPP_BLANK_SPACE + CPP_LEFT_BRACE; + resContent += NAPI_NEW_LINE + NAPI_STRUCT_TOKEN + NAPI_BLANK_SPACE + paName + + NAPI_STRUCT_SUFFIX + NAPI_BLANK_SPACE + NAPI_LEFT_BRACE; List paramList = paList.get(0).getPaList(); for (ParamObj paItem : paramList) { String paStr = paItem.getName(); String paVal = paItem.getStrValue(0); String typeStr = StringUtils.isAllDigits(paVal) ? - CPP_NUMBER_TOKEN : CPP_STD_STRING; - typeStr = StringUtils.isBoolean(paVal) ? CPP_BOOLEAN_TOKEN : typeStr; - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + typeStr + CPP_BLANK_SPACE + paStr + CPP_SEMICOLON; + NAPI_NUMBER_TOKEN : NAPI_STD_STRING; + typeStr = StringUtils.isBoolean(paVal) ? NAPI_BOOLEAN_TOKEN : typeStr; + resContent += NAPI_NEW_LINE + NAPI_TAB_SPACE + typeStr + NAPI_BLANK_SPACE + paStr + NAPI_SEMICOLON; } - resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + resContent += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; - resContent += CPP_NEW_LINE + CPP_CONST_TOKEN + CPP_BLANK_SPACE + CPP_STD_VECTOR + - CPP_LEFT_ANGLE_BRACKET + paName + CPP_STRUCT_SUFFIX + CPP_RIGHT_ANGLE_BRACKET + - CPP_BLANK_SPACE + paName + CPP_EQUAL + CPP_LEFT_BRACE; + resContent += NAPI_NEW_LINE + NAPI_CONST_TOKEN + NAPI_BLANK_SPACE + NAPI_STD_VECTOR + + NAPI_LEFT_ANGLE_BRACKET + paName + NAPI_STRUCT_SUFFIX + NAPI_RIGHT_ANGLE_BRACKET + + NAPI_BLANK_SPACE + paName + NAPI_EQUAL + NAPI_LEFT_BRACE; for (ParamObj paramListItem : paList) { List subParamList = paramListItem.getPaList(); - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_BRACE; + resContent += NAPI_NEW_LINE + NAPI_TAB_SPACE + NAPI_LEFT_BRACE; for (ParamObj paItem : subParamList) { String paVal = paItem.getStrValue(0); - resContent += paVal + CPP_COMMA + CPP_BLANK_SPACE; + resContent += paVal + NAPI_COMMA + NAPI_BLANK_SPACE; } resContent = StringUtils.removeLastCharacter(resContent, 2); - resContent += CPP_RIGHT_BRACE + CPP_COMMA; + resContent += NAPI_RIGHT_BRACE + NAPI_COMMA; } - resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + resContent += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; return resContent; }; @@ -653,30 +918,30 @@ public class GenNapiCppFile extends GeneratorBase { String resContent = ""; for (ParamObj po : pol) { String paName = po.getName(); - String paType = ts2CppKey(po.getType()).isEmpty() ? CPP_AUTO_TOKEN : ts2CppKey(po.getType()); + String paType = ts2CppKey(po.getType()).isEmpty() ? NAPI_AUTO_TOKEN : ts2CppKey(po.getType()); String paValue = po.getStrValue(0); List paList = po.getPaList(); if (paList.isEmpty()) { - resContent += CPP_NEW_LINE + CPP_EXTENDS_TOKEN + CPP_BLANK_SPACE + CPP_CONST_TOKEN + - CPP_BLANK_SPACE + paType + CPP_BLANK_SPACE + paName + CPP_EQUAL + paValue; + resContent += NAPI_NEW_LINE + NAPI_EXTENDS_TOKEN + NAPI_BLANK_SPACE + NAPI_CONST_TOKEN + + NAPI_BLANK_SPACE + paType + NAPI_BLANK_SPACE + paName + NAPI_EQUAL + paValue; - resContent += CPP_SEMICOLON + CPP_NEW_LINE; + resContent += NAPI_SEMICOLON + NAPI_NEW_LINE; } else if (paList.get(0).getPaList().isEmpty()) { String valType = StringUtils.isAllDigits(paList.get(0).getStrValue(0)) ? - CPP_NUMBER_TOKEN : CPP_STD_STRING; - resContent += CPP_NEW_LINE + CPP_EXTENDS_TOKEN + CPP_BLANK_SPACE + CPP_CONST_TOKEN + - CPP_BLANK_SPACE + CPP_STD_MAP + CPP_LEFT_ANGLE_BRACKET + CPP_STD_STRING + - CPP_COMMA + CPP_BLANK_SPACE + valType + CPP_RIGHT_BRACE + CPP_BLANK_SPACE + - paName + CPP_EQUAL + CPP_LEFT_BRACE; + NAPI_NUMBER_TOKEN : NAPI_STD_STRING; + resContent += NAPI_NEW_LINE + NAPI_EXTENDS_TOKEN + NAPI_BLANK_SPACE + NAPI_CONST_TOKEN + + NAPI_BLANK_SPACE + NAPI_STD_MAP + NAPI_LEFT_ANGLE_BRACKET + NAPI_STD_STRING + + NAPI_COMMA + NAPI_BLANK_SPACE + valType + NAPI_RIGHT_BRACE + NAPI_BLANK_SPACE + + paName + NAPI_EQUAL + NAPI_LEFT_BRACE; for (ParamObj paItem : paList) { String pName = paItem.getName(); String pVal = paItem.getStrValue(0); - resContent += CPP_NEW_LINE + CPP_TAB_SPACE + CPP_LEFT_BRACE + CPP_DOUBLE_QUOTATION + - pName + CPP_DOUBLE_QUOTATION + CPP_COMMA + CPP_BLANK_SPACE + pVal + - CPP_RIGHT_BRACE + CPP_COMMA; + resContent += NAPI_NEW_LINE + NAPI_TAB_SPACE + NAPI_LEFT_BRACE + NAPI_DOUBLE_QUOTATION + + pName + NAPI_DOUBLE_QUOTATION + NAPI_COMMA + NAPI_BLANK_SPACE + pVal + + NAPI_RIGHT_BRACE + NAPI_COMMA; } resContent = StringUtils.removeLastCharacter(resContent, 1); - resContent += CPP_NEW_LINE + CPP_RIGHT_BRACE + CPP_SEMICOLON + CPP_NEW_LINE; + resContent += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; } else if (!(paList.get(0).getPaList().isEmpty())) { resContent = genVarArrayList(resContent, paName, paList); } diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java index 4b23a2b8..13cfa038 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java @@ -36,6 +36,891 @@ import static utils.FileUtils.readText; */ class GenNapiCppFileTest { + private String classContTest1 = "\nclass TestClass {\n" + + "\tstd::string name;\n" + + "\tint age;\n" + + "\tint add(int a, int b);\n" + + "};\n" + + "\n" + + "napi_value ConstructorTestClass(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + + "\tnapi_get_undefined(env, &undefineVar);\n" + + "\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\t\tTestClass *reference = new TestClass();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorTestClass, nullptr, nullptr) == napi_ok) {\n" + + "\t\t\treturn thisVar;\n" + + "\t\t}\n" + + "\t\treturn thisVar;\n" + + "\t}\n" + + "\treturn undefineVar;\n" + + "};\n" + + "\n" + + "void DestructorTestClass(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value addTestClass(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Getname(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setname(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->name = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_value Getage(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setage(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->age = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor TestClassProps[] = {\n" + + "\t{add, nullptr, addTestClass, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{name, nullptr, nullptr, GetnameTestClass, SetnameTestClass, nullptr, napi_default, nullptr},\n" + + "\t{age, nullptr, nullptr, GetageTestClass, SetageTestClass, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value TestClassIns = nullptr;\n" + + "\tif (napi_define_class(env, \"TestClass\", NAPI_AUTO_LENGTH, ConstructorTestClass, nullptr, sizeof(TestClassProps) / sizeof(TestClassProps[0]), TestClassProps, &TestClassIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"TestClass\", TestClassIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String classContTest2 = "\nclass TestClass : public IPerson {\n" + + "\tpublic std::string name;\n" + + "\tprivate int age;\n" + + "\tprotected std::string no;\n" + + "\treadonly std::string addr;\n" + + "\tconstructor();\n" + + "};\n" + + "\n" + + "napi_value ConstructorTestClass(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + + "\tnapi_get_undefined(env, &undefineVar);\n" + + "\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\t\tTestClass *reference = new TestClass();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorTestClass, nullptr, nullptr) == napi_ok) {\n" + + "\t\t\treturn thisVar;\n" + + "\t\t}\n" + + "\t\treturn thisVar;\n" + + "\t}\n" + + "\treturn undefineVar;\n" + + "};\n" + + "\n" + + "void DestructorTestClass(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value constructorTestClass(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Getname(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setname(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->name = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_value Getage(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setage(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->age = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_value Getno(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setno(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->no = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_value Getaddr(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setaddr(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->addr = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor TestClassProps[] = {\n" + + "\t{constructor, nullptr, constructorTestClass, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{name, nullptr, nullptr, GetnameTestClass, SetnameTestClass, nullptr, napi_default, nullptr},\n" + + "\t{age, nullptr, nullptr, GetageTestClass, SetageTestClass, nullptr, napi_default, nullptr},\n" + + "\t{no, nullptr, nullptr, GetnoTestClass, SetnoTestClass, nullptr, napi_default, nullptr},\n" + + "\t{addr, nullptr, nullptr, GetaddrTestClass, SetaddrTestClass, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value TestClassIns = nullptr;\n" + + "\tif (napi_define_class(env, \"TestClass\", NAPI_AUTO_LENGTH, ConstructorTestClass, nullptr, sizeof(TestClassProps) / sizeof(TestClassProps[0]), TestClassProps, &TestClassIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"TestClass\", TestClassIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String classContTest3 = "\nclass Employee : public Person {\n" + + "\tint empCode;\n" + + "\tauto currentUser;\n" + + "\tstatic int pi = 3.14;\n" + + "\tconstructor();\n" + + "\tvoid displayName();\n" + + "};\n" + + "\n" + + "napi_value ConstructorEmployee(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + + "\tnapi_get_undefined(env, &undefineVar);\n" + + "\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\t\tEmployee *reference = new Employee();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorEmployee, nullptr, nullptr) == napi_ok) {\n" + + "\t\t\treturn thisVar;\n" + + "\t\t}\n" + + "\t\treturn thisVar;\n" + + "\t}\n" + + "\treturn undefineVar;\n" + + "};\n" + + "\n" + + "void DestructorEmployee(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value constructorEmployee(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tEmployee *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value displayNameEmployee(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tEmployee *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value GetempCode(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tEmployee *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value SetempCode(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tEmployee *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->empCode = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_value GetcurrentUser(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tEmployee *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value SetcurrentUser(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tEmployee *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->currentUser = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_value Getpi(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tEmployee *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setpi(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tEmployee *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->pi = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor EmployeeProps[] = {\n" + + "\t{constructor, nullptr, constructorEmployee, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{displayName, nullptr, displayNameEmployee, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{empCode, nullptr, nullptr, GetempCodeEmployee, SetempCodeEmployee, nullptr, napi_default, nullptr},\n" + + "\t{currentUser, nullptr, nullptr, GetcurrentUserEmployee, SetcurrentUserEmployee, nullptr, napi_default, nullptr},\n" + + "\t{pi, nullptr, nullptr, GetpiEmployee, SetpiEmployee, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value EmployeeIns = nullptr;\n" + + "\tif (napi_define_class(env, \"Employee\", NAPI_AUTO_LENGTH, ConstructorEmployee, nullptr, sizeof(EmployeeProps) / sizeof(EmployeeProps[0]), EmployeeProps, &EmployeeIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"Employee\", EmployeeIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String classContTest4 = "\nclass myClass {\n" + + "\tauto foo();\n" + + "};\n" + + "\n" + + "napi_value ConstructormyClass(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + + "\tnapi_get_undefined(env, &undefineVar);\n" + + "\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\t\tmyClass *reference = new myClass();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructormyClass, nullptr, nullptr) == napi_ok) {\n" + + "\t\t\treturn thisVar;\n" + + "\t\t}\n" + + "\t\treturn thisVar;\n" + + "\t}\n" + + "\treturn undefineVar;\n" + + "};\n" + + "\n" + + "void DestructormyClass(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value foomyClass(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tmyClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_property_descriptor myClassProps[] = {\n" + + "\t{foo, nullptr, foomyClass, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value myClassIns = nullptr;\n" + + "\tif (napi_define_class(env, \"myClass\", NAPI_AUTO_LENGTH, ConstructormyClass, nullptr, sizeof(myClassProps) / sizeof(myClassProps[0]), myClassProps, &myClassIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"myClass\", myClassIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String classContTest5 = "\ntemplate class KeyValuePair {\n" + + "\tprivate T key;\n" + + "\tprivate U val;\n" + + "\tvoid setKeyValue(T key, U val);\n" + + "};\n" + + "\n" + + "napi_value ConstructorKeyValuePair(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + + "\tnapi_get_undefined(env, &undefineVar);\n" + + "\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\t\tKeyValuePair *reference = new KeyValuePair();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorKeyValuePair, nullptr, nullptr) == napi_ok) {\n" + + "\t\t\treturn thisVar;\n" + + "\t\t}\n" + + "\t\treturn thisVar;\n" + + "\t}\n" + + "\treturn undefineVar;\n" + + "};\n" + + "\n" + + "void DestructorKeyValuePair(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value setKeyValueKeyValuePair(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tKeyValuePair *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Getkey(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tKeyValuePair *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setkey(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tKeyValuePair *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->key = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_value Getval(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tKeyValuePair *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setval(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tKeyValuePair *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->val = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor KeyValuePairProps[] = {\n" + + "\t{setKeyValue, nullptr, setKeyValueKeyValuePair, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{key, nullptr, nullptr, GetkeyKeyValuePair, SetkeyKeyValuePair, nullptr, napi_default, nullptr},\n" + + "\t{val, nullptr, nullptr, GetvalKeyValuePair, SetvalKeyValuePair, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value KeyValuePairIns = nullptr;\n" + + "\tif (napi_define_class(env, \"KeyValuePair\", NAPI_AUTO_LENGTH, ConstructorKeyValuePair, nullptr, sizeof(KeyValuePairProps) / sizeof(KeyValuePairProps[0]), KeyValuePairProps, &KeyValuePairIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"KeyValuePair\", KeyValuePairIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String classContTest6 = "\ntemplate class kvProcessor : public IKeyValueProcessor {\n" + + "\tvoid process(T key, U val);\n" + + "};\n" + + "\n" + + "napi_value ConstructorkvProcessor(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + + "\tnapi_get_undefined(env, &undefineVar);\n" + + "\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\t\tkvProcessor *reference = new kvProcessor();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorkvProcessor, nullptr, nullptr) == napi_ok) {\n" + + "\t\t\treturn thisVar;\n" + + "\t\t}\n" + + "\t\treturn thisVar;\n" + + "\t}\n" + + "\treturn undefineVar;\n" + + "};\n" + + "\n" + + "void DestructorkvProcessor(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value processkvProcessor(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tkvProcessor *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_property_descriptor kvProcessorProps[] = {\n" + + "\t{process, nullptr, processkvProcessor, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value kvProcessorIns = nullptr;\n" + + "\tif (napi_define_class(env, \"kvProcessor\", NAPI_AUTO_LENGTH, ConstructorkvProcessor, nullptr, sizeof(kvProcessorProps) / sizeof(kvProcessorProps[0]), kvProcessorProps, &kvProcessorIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"kvProcessor\", kvProcessorIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String classContTest7 = "\nclass Shape {\n" + + "\tvoid process(auto key, auto val);\n" + + "};\n" + + "\n" + + "napi_value ConstructorShape(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + + "\tnapi_get_undefined(env, &undefineVar);\n" + + "\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\t\tShape *reference = new Shape();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorShape, nullptr, nullptr) == napi_ok) {\n" + + "\t\t\treturn thisVar;\n" + + "\t\t}\n" + + "\t\treturn thisVar;\n" + + "\t}\n" + + "\treturn undefineVar;\n" + + "};\n" + + "\n" + + "void DestructorShape(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value processShape(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tShape *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_property_descriptor ShapeProps[] = {\n" + + "\t{process, nullptr, processShape, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value ShapeIns = nullptr;\n" + + "\tif (napi_define_class(env, \"Shape\", NAPI_AUTO_LENGTH, ConstructorShape, nullptr, sizeof(ShapeProps) / sizeof(ShapeProps[0]), ShapeProps, &ShapeIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"Shape\", ShapeIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + @Test void getInterfaceContent() { } @@ -52,16 +937,36 @@ class GenNapiCppFileTest { eol.add(eo); ParseObj po = new ParseObj(); po.setEnumList(eol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genEnumList(po.getEnumList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String enumContent = gdf.getEnumContent(); System.out.println("genEnum: " + enumContent); String expect = "\nenum TestEnum {\n" + "\tONE,\n" + "\tTWO,\n" + - "};\n"; + "};\n" + + "\n" + + "// 创建枚举对象\n" + + "napi_value CreateTestEnumEnum(napi_env env) {\n" + + "\tnapi_value enum_obj;\n" + + "\tnapi_create_object(env, &enum_obj);\n" + + "\n" + + "\t// 添加枚举成员\n" + + "\tconst char* members[] = {\"ONE\", \"TWO\"};\n" + + "\tconst int values[] = {};\n" + + "\tfor (int32_t i = 0; i < 2; ++i) {\n" + + "\t\tnapi_value value;\n" + + "\t\tnapi_create_int32(env, i, &value);\n" + + "\t\tnapi_set_named_property(env, enum_obj, members[i], value);\n" + + "\t}\n" + + "\n" + + "\treturn enum_obj;\n" + + "}\n" + + "\t// 创建并绑定枚举\n" + + "\tnapi_value TestEnum_enum = CreateTestEnumEnum(env);\n" + + "\tnapi_set_named_property(env, exports, \"TestEnum\", TestEnum_enum);\n"; assertEquals(expect, enumContent); } } @@ -84,17 +989,37 @@ class GenNapiCppFileTest { eol.add(eo); ParseObj po = new ParseObj(); po.setEnumList(eol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genEnumList(po.getEnumList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String enumContent = gdf.getEnumContent(); System.out.println("genEnum: " + enumContent); String expect = "\nenum Colors {\n" + "\tRed = RED,\n" + "\tGreen = GREEN,\n" + "\tBlue = BLUE,\n" + - "};\n"; + "};\n" + + "\n" + + "// 创建枚举对象\n" + + "napi_value CreateColorsEnum(napi_env env) {\n" + + "\tnapi_value enum_obj;\n" + + "\tnapi_create_object(env, &enum_obj);\n" + + "\n" + + "\t// 添加枚举成员\n" + + "\tconst char* members[] = {\"Red\", \"Green\", \"Blue\"};\n" + + "\tconst int values[] = {RED, GREEN, BLUE};\n" + + "\tfor (int32_t i = 0; i < 3; ++i) {\n" + + "\t\tnapi_value value;\n" + + "\t\tnapi_create_int32(env, values[i], &value);\n" + + "\t\tnapi_set_named_property(env, enum_obj, members[i], value);\n" + + "\t}\n" + + "\n" + + "\treturn enum_obj;\n" + + "}\n" + + "\t// 创建并绑定枚举\n" + + "\tnapi_value Colors_enum = CreateColorsEnum(env);\n" + + "\tnapi_set_named_property(env, exports, \"Colors\", Colors_enum);\n"; assertEquals(expect, enumContent); } } @@ -117,10 +1042,10 @@ class GenNapiCppFileTest { eol.add(eo); ParseObj po = new ParseObj(); po.setEnumList(eol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genEnumList(po.getEnumList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String enumContent = gdf.getEnumContent(); System.out.println("genEnum: " + enumContent); String expect = "\nenum Colors {\n" + @@ -133,7 +1058,27 @@ class GenNapiCppFileTest { "\t[Red] = \"RED\",\n" + "\t[Green] = \"GREEN\",\n" + "\t[Blue] = \"BLUE\"\n" + - "};\n"; + "};\n" + + "\n" + + "// 创建枚举对象\n" + + "napi_value CreateColorsEnum(napi_env env) {\n" + + "\tnapi_value enum_obj;\n" + + "\tnapi_create_object(env, &enum_obj);\n" + + "\n" + + "\t// 添加枚举成员\n" + + "\tconst char* members[] = {\"Red\", \"Green\", \"Blue\"};\n" + + "\tconst int values[] = {\"RED\", \"GREEN\", \"BLUE\"};\n" + + "\tfor (int32_t i = 0; i < 3; ++i) {\n" + + "\t\tnapi_value value;\n" + + "\t\tnapi_create_int32(env, value[i], &value);\n" + + "\t\tnapi_set_named_property(env, enum_obj, members[i], value);\n" + + "\t}\n" + + "\n" + + "\treturn enum_obj;\n" + + "}\n" + + "\t// 创建并绑定枚举\n" + + "\tnapi_value Colors_enum = CreateColorsEnum(env);\n" + + "\tnapi_set_named_property(env, exports, \"Colors\", Colors_enum);\n"; assertEquals(expect, enumContent); } } @@ -163,17 +1108,13 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setClassList(col); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genClassList(po.getClassList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String classContent = gdf.getClassContent(); System.out.println("genClass: " + classContent); - String expect = "\nclass TestClass {\n" + - "\tchar* name;\n" + - "\tint age;\n" + - "\tint add(int a, int b);\n" + - "};\n"; + String expect = classContTest1; assertEquals(expect, classContent); } } @@ -215,19 +1156,13 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setClassList(col); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genClassList(po.getClassList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String classContent = gdf.getClassContent(); System.out.println("genClass: " + classContent); - String expect = "\nclass TestClass : public IPerson {\n" + - "\tpublic char* name;\n" + - "\tprivate int age;\n" + - "\tprotected char* no;\n" + - "\treadonly char* addr;\n" + - "\tconstructor();\n" + - "};\n"; + String expect = classContTest2; assertEquals(expect, classContent); } } @@ -273,19 +1208,13 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setClassList(col); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genClassList(po.getClassList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String classContent = gdf.getClassContent(); System.out.println("genClass: " + classContent); - String expect = "\nclass Employee : public Person {\n" + - "\tint empCode;\n" + - "\tauto currentUser;\n" + - "\tstatic int pi = 3.14;\n" + - "\tconstructor();\n" + - "\tvoid displayName();\n" + - "};\n"; + String expect = classContTest3; assertEquals(expect, classContent); } } @@ -308,15 +1237,13 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setClassList(col); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genClassList(po.getClassList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String classContent = gdf.getClassContent(); System.out.println("genClass: " + classContent); - String expect = "\nclass myClass {\n" + - "\tauto foo();\n" + - "};\n"; + String expect = classContTest4; assertEquals(expect, classContent); } } @@ -355,17 +1282,13 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setClassList(col); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genClassList(po.getClassList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String classContent = gdf.getClassContent(); System.out.println("genClass: " + classContent); - String expect = "\ntemplate class KeyValuePair {\n" + - "\tprivate T key;\n" + - "\tprivate U val;\n" + - "\tvoid setKeyValue(T key, U val);\n" + - "};\n"; + String expect = classContTest5; assertEquals(expect, classContent); } } @@ -401,16 +1324,13 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setClassList(col); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genClassList(po.getClassList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String classContent = gdf.getClassContent(); System.out.println("genClass: " + classContent); - String expect = "\ntemplate class kvProcessor : " + - "public IKeyValueProcessor {\n" + - "\tvoid process(T key, U val);\n" + - "};\n"; + String expect = classContTest6; assertEquals(expect, classContent); } } @@ -431,15 +1351,13 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setClassList(col); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genClassList(po.getClassList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String classContent = gdf.getClassContent(); System.out.println("genClass: " + classContent); - String expect = "\nclass Shape {\n" + - "\tvoid process(auto key, auto val);\n" + - "};\n"; + String expect = classContTest7; assertEquals(expect, classContent); } } @@ -455,10 +1373,10 @@ class GenNapiCppFileTest { fol.add(fo); ParseObj po = new ParseObj(); po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genFuncList(po.getFuncList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String funcContent = gdf.getFuncContent(); System.out.println("genFunc: " + funcContent); String expect = "\nvoid TestFunc(char* name, int age);"; @@ -481,10 +1399,10 @@ class GenNapiCppFileTest { fol.add(fo); ParseObj po = new ParseObj(); po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genFuncList(po.getFuncList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String funcContent = gdf.getFuncContent(); System.out.println("genFunc: " + funcContent); String expect = "\nchar* ToCapital(char* str, int length = 0);"; @@ -511,10 +1429,10 @@ class GenNapiCppFileTest { fol.add(fo); ParseObj po = new ParseObj(); po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genFuncList(po.getFuncList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String funcContent = gdf.getFuncContent(); System.out.println("genFunc: " + funcContent); String expect = "\nchar* Nemw(char* str = \"joke\", int length = 0);"; @@ -537,10 +1455,10 @@ class GenNapiCppFileTest { fol.add(fo); ParseObj po = new ParseObj(); po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genFuncList(po.getFuncList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String funcContent = gdf.getFuncContent(); System.out.println("genFunc: " + funcContent); String expect = "\nchar* Nemw(auto str, auto length);"; @@ -563,10 +1481,10 @@ class GenNapiCppFileTest { fol.add(fo); ParseObj po = new ParseObj(); po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genFuncList(po.getFuncList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String funcContent = gdf.getFuncContent(); System.out.println("genFunc: " + funcContent); String expect = "\nNemw(auto str, auto length);"; @@ -592,10 +1510,10 @@ class GenNapiCppFileTest { fol.add(fo); ParseObj po = new ParseObj(); po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genFuncList(po.getFuncList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String funcContent = gdf.getFuncContent(); System.out.println("genFunc: " + funcContent); String expect = "\ntemplate T* getArray(T* items);"; @@ -626,10 +1544,10 @@ class GenNapiCppFileTest { fol.add(fo); ParseObj po = new ParseObj(); po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genFuncList(po.getFuncList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String funcContent = gdf.getFuncContent(); System.out.println("genFunc: " + funcContent); String expect = "\ntemplate void displayType(T id, U name);"; @@ -662,10 +1580,10 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setStructList(sol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genStructList(po.getStructList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); String expect = "\nstruct TestStruct {\n" + @@ -703,10 +1621,10 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setStructList(sol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genStructList(po.getStructList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); String expect = "\ntemplate struct TestStruct {\n" + @@ -743,10 +1661,10 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setStructList(sol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genStructList(po.getStructList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); String expect = "\nstruct TestStruct {\n" + @@ -768,10 +1686,10 @@ class GenNapiCppFileTest { ParseObj po = new ParseObj(); po.setStructList(sol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genStructList(po.getStructList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); String expect = "\nstruct TestStruct {\n" + @@ -796,10 +1714,10 @@ class GenNapiCppFileTest { uol.add(uo); ParseObj po = new ParseObj(); po.setUnionList(uol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genUnionList(po.getUnionList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String unionContent = gdf.getUnionContent(); System.out.println("genUnion: " + unionContent); String expect = "\nunion TestUnion{\n" + @@ -824,10 +1742,10 @@ class GenNapiCppFileTest { uol.add(uo); ParseObj po = new ParseObj(); po.setUnionList(uol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genUnionList(po.getUnionList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String unionContent = gdf.getUnionContent(); System.out.println("genUnion: " + unionContent); String expect = "\ntemplate union TestUnion{\n" + @@ -848,10 +1766,10 @@ class GenNapiCppFileTest { pol.add(paObj); ParseObj po = new ParseObj(); po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genVarList(po.getVarList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String constContent = gdf.getConstContent(); System.out.println("getVar: " + constContent); String expect = "\nextends const auto employeeName = \"John\";\n"; @@ -870,10 +1788,10 @@ class GenNapiCppFileTest { pol.add(paObj); ParseObj po = new ParseObj(); po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genVarList(po.getVarList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String constContent = gdf.getConstContent(); System.out.println("getVar: " + constContent); String expect = "\nextends const char* employeeName = \"John\";\n"; @@ -892,10 +1810,10 @@ class GenNapiCppFileTest { pol.add(paObj); ParseObj po = new ParseObj(); po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genVarList(po.getVarList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String constContent = gdf.getConstContent(); System.out.println("getVar: " + constContent); String expect = "\nextends const int num1 = 1;\n"; @@ -921,10 +1839,10 @@ class GenNapiCppFileTest { pol.add(paObj); ParseObj po = new ParseObj(); po.setVarList(pol); - GeneratorBase gb = GenerateFactory.getGenerator("CPP"); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); gb.genVarList(po.getVarList()); - if (gb instanceof GenCppFile gdf) { + if (gb instanceof GenNapiCppFile gdf) { String constContent = gdf.getConstContent(); System.out.println("getVar: " + constContent); String expect = "\nextends const std::map Date: Fri, 18 Apr 2025 17:27:46 +0800 Subject: [PATCH 2/6] fix code check Signed-off-by: sunlian --- .../src/main/java/gen/GenNapiCppFile.java | 7 +- .../src/test/java/gen/GenNapiCppFileTest.java | 88 +++++++++++-------- 2 files changed, 52 insertions(+), 43 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java index 631e0071..0817caf1 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java @@ -490,12 +490,12 @@ public class GenNapiCppFile extends GeneratorBase { }; private String genCppEnumContent(EnumObj eo) { - String resContent = ""; String enumName = eo.getName(); enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); List memList = eo.getMemberList(); List vaList = eo.getValueList(); int i = 0; + String resContent = ""; resContent += NAPI_NEW_LINE + NAPI_ENUM_TOKEN + NAPI_BLANK_SPACE + enumName + NAPI_BLANK_SPACE + NAPI_LEFT_BRACE; for (String memItem : memList) { @@ -529,7 +529,6 @@ public class GenNapiCppFile extends GeneratorBase { } private String genNapiEnumContent(EnumObj eo) { - String resContent = ""; String enumName = eo.getName(); enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); List memList = eo.getMemberList(); @@ -552,7 +551,7 @@ public class GenNapiCppFile extends GeneratorBase { enumDeclare = enumDeclare.replace(NAPI_ENUM_ITEM_VALUE, valueList.isEmpty() ? NAPI_ENUM_VALUE_INDEX : NAPI_ENUM_VALUE_ITER); enumDeclare = enumDeclare.replace(NAPI_ENUM_CNT, Integer.toString(memList.size())); - + String resContent = ""; resContent = enumDeclare + NAPI_EXPORT_ENUM.replace(NAPI_ENUM_NAME, enumName); return resContent; } @@ -648,7 +647,6 @@ public class GenNapiCppFile extends GeneratorBase { } private String genNapiClassContent(ClassObj co) { - String resContent = ""; String className = co.getName(); className = !className.isEmpty() ? className : co.getAlias(); List funcList = co.getFuncList(); @@ -699,6 +697,7 @@ public class GenNapiCppFile extends GeneratorBase { String classInitStr = NAPI_CLASS_INIT.replace(NAPI_CLASS_NAME, className); classDeclare += classInitStr; + String resContent = ""; resContent = classDeclare; return resContent; } diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java index 13cfa038..4fe452d7 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java @@ -35,6 +35,37 @@ import static utils.FileUtils.readText; * @since 2025-02-28 */ class GenNapiCppFileTest { + private String enumContTest3 = "\nenum Colors {\n" + + "\tRed = RED,\n" + + "\tGreen = GREEN,\n" + + "\tBlue = BLUE,\n" + + "};\n" + + "\n" + + "char* colors_STR[] = {\n" + + "\t[Red] = \"RED\",\n" + + "\t[Green] = \"GREEN\",\n" + + "\t[Blue] = \"BLUE\"\n" + + "};\n" + + "\n" + + "// 创建枚举对象\n" + + "napi_value CreateColorsEnum(napi_env env) {\n" + + "\tnapi_value enum_obj;\n" + + "\tnapi_create_object(env, &enum_obj);\n" + + "\n" + + "\t// 添加枚举成员\n" + + "\tconst char* members[] = {\"Red\", \"Green\", \"Blue\"};\n" + + "\tconst int values[] = {\"RED\", \"GREEN\", \"BLUE\"};\n" + + "\tfor (int32_t i = 0; i < 3; ++i) {\n" + + "\t\tnapi_value value;\n" + + "\t\tnapi_create_int32(env, value[i], &value);\n" + + "\t\tnapi_set_named_property(env, enum_obj, members[i], value);\n" + + "\t}\n" + + "\n" + + "\treturn enum_obj;\n" + + "}\n" + + "\t// 创建并绑定枚举\n" + + "\tnapi_value Colors_enum = CreateColorsEnum(env);\n" + + "\tnapi_set_named_property(env, exports, \"Colors\", Colors_enum);\n"; private String classContTest1 = "\nclass TestClass {\n" + "\tstd::string name;\n" + @@ -47,7 +78,8 @@ class GenNapiCppFileTest { "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + "\tnapi_get_undefined(env, &undefineVar);\n" + "\n" + - "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == " + + "napi_ok && thisVar != nullptr) {\n" + "\t\tTestClass *reference = new TestClass();\n" + "\t\tif (napi_wrap(env, thisVar,\n" + "\t\t\treinterpret_cast(reference), DestructorTestClass, nullptr, nullptr) == napi_ok) {\n" + @@ -169,7 +201,8 @@ class GenNapiCppFileTest { "};\n" + "\n" + "napi_value TestClassIns = nullptr;\n" + - "\tif (napi_define_class(env, \"TestClass\", NAPI_AUTO_LENGTH, ConstructorTestClass, nullptr, sizeof(TestClassProps) / sizeof(TestClassProps[0]), TestClassProps, &TestClassIns) != napi_ok) {\n" + + "\tif (napi_define_class(env, \"TestClass\", NAPI_AUTO_LENGTH, ConstructorTestClass, nullptr, " + + "sizeof(TestClassProps) / sizeof(TestClassProps[0]), TestClassProps, &TestClassIns) != napi_ok) {\n" + "\t\treturn nullptr;\n" + "\t}\n" + "\tif (napi_set_named_property(env, exports, \"TestClass\", TestClassIns) != napi_ok) {\n" + @@ -189,7 +222,8 @@ class GenNapiCppFileTest { "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + "\tnapi_get_undefined(env, &undefineVar);\n" + "\n" + - "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && " + + "thisVar != nullptr) {\n" + "\t\tTestClass *reference = new TestClass();\n" + "\t\tif (napi_wrap(env, thisVar,\n" + "\t\t\treinterpret_cast(reference), DestructorTestClass, nullptr, nullptr) == napi_ok) {\n" + @@ -801,7 +835,8 @@ class GenNapiCppFileTest { "\t\treturn nullptr;\n" + "\t}"; - private String classContTest6 = "\ntemplate class kvProcessor : public IKeyValueProcessor {\n" + + private String classContTest6 = + "\ntemplate class kvProcessor : public IKeyValueProcessor {\n" + "\tvoid process(T key, U val);\n" + "};\n" + "\n" + @@ -810,7 +845,8 @@ class GenNapiCppFileTest { "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + "\tnapi_get_undefined(env, &undefineVar);\n" + "\n" + - "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && " + + "thisVar != nullptr) {\n" + "\t\tkvProcessor *reference = new kvProcessor();\n" + "\t\tif (napi_wrap(env, thisVar,\n" + "\t\t\treinterpret_cast(reference), DestructorkvProcessor, nullptr, nullptr) == napi_ok) {\n" + @@ -854,7 +890,9 @@ class GenNapiCppFileTest { "};\n" + "\n" + "napi_value kvProcessorIns = nullptr;\n" + - "\tif (napi_define_class(env, \"kvProcessor\", NAPI_AUTO_LENGTH, ConstructorkvProcessor, nullptr, sizeof(kvProcessorProps) / sizeof(kvProcessorProps[0]), kvProcessorProps, &kvProcessorIns) != napi_ok) {\n" + + "\tif (napi_define_class(env, \"kvProcessor\", NAPI_AUTO_LENGTH, ConstructorkvProcessor, nullptr, " + + "sizeof(kvProcessorProps) / sizeof(kvProcessorProps[0]), " + + "kvProcessorProps, &kvProcessorIns) != napi_ok) {\n" + "\t\treturn nullptr;\n" + "\t}\n" + "\tif (napi_set_named_property(env, exports, \"kvProcessor\", kvProcessorIns) != napi_ok) {\n" + @@ -870,7 +908,8 @@ class GenNapiCppFileTest { "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + "\tnapi_get_undefined(env, &undefineVar);\n" + "\n" + - "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == " + + "napi_ok && thisVar != nullptr) {\n" + "\t\tShape *reference = new Shape();\n" + "\t\tif (napi_wrap(env, thisVar,\n" + "\t\t\treinterpret_cast(reference), DestructorShape, nullptr, nullptr) == napi_ok) {\n" + @@ -914,7 +953,8 @@ class GenNapiCppFileTest { "};\n" + "\n" + "napi_value ShapeIns = nullptr;\n" + - "\tif (napi_define_class(env, \"Shape\", NAPI_AUTO_LENGTH, ConstructorShape, nullptr, sizeof(ShapeProps) / sizeof(ShapeProps[0]), ShapeProps, &ShapeIns) != napi_ok) {\n" + + "\tif (napi_define_class(env, \"Shape\", NAPI_AUTO_LENGTH, ConstructorShape, nullptr, " + + "sizeof(ShapeProps) / sizeof(ShapeProps[0]), ShapeProps, &ShapeIns) != napi_ok) {\n" + "\t\treturn nullptr;\n" + "\t}\n" + "\tif (napi_set_named_property(env, exports, \"Shape\", ShapeIns) != napi_ok) {\n" + @@ -1048,37 +1088,7 @@ class GenNapiCppFileTest { if (gb instanceof GenNapiCppFile gdf) { String enumContent = gdf.getEnumContent(); System.out.println("genEnum: " + enumContent); - String expect = "\nenum Colors {\n" + - "\tRed = RED,\n" + - "\tGreen = GREEN,\n" + - "\tBlue = BLUE,\n" + - "};\n" + - "\n" + - "char* colors_STR[] = {\n" + - "\t[Red] = \"RED\",\n" + - "\t[Green] = \"GREEN\",\n" + - "\t[Blue] = \"BLUE\"\n" + - "};\n" + - "\n" + - "// 创建枚举对象\n" + - "napi_value CreateColorsEnum(napi_env env) {\n" + - "\tnapi_value enum_obj;\n" + - "\tnapi_create_object(env, &enum_obj);\n" + - "\n" + - "\t// 添加枚举成员\n" + - "\tconst char* members[] = {\"Red\", \"Green\", \"Blue\"};\n" + - "\tconst int values[] = {\"RED\", \"GREEN\", \"BLUE\"};\n" + - "\tfor (int32_t i = 0; i < 3; ++i) {\n" + - "\t\tnapi_value value;\n" + - "\t\tnapi_create_int32(env, value[i], &value);\n" + - "\t\tnapi_set_named_property(env, enum_obj, members[i], value);\n" + - "\t}\n" + - "\n" + - "\treturn enum_obj;\n" + - "}\n" + - "\t// 创建并绑定枚举\n" + - "\tnapi_value Colors_enum = CreateColorsEnum(env);\n" + - "\tnapi_set_named_property(env, exports, \"Colors\", Colors_enum);\n"; + String expect = enumContTest3; assertEquals(expect, enumContent); } } -- Gitee From 2698e4ce6994e9ab8c7012b58ac64d2a77b3e3d9 Mon Sep 17 00:00:00 2001 From: sunlian Date: Fri, 18 Apr 2025 17:47:38 +0800 Subject: [PATCH 3/6] fix code check Signed-off-by: sunlian --- .../src/main/java/gen/GenNapiCppFile.java | 2 +- .../src/test/java/gen/GenNapiCppFileTest.java | 272 +----------------- 2 files changed, 14 insertions(+), 260 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java index 0817caf1..bef9ea97 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java @@ -271,7 +271,7 @@ public class GenNapiCppFile extends GeneratorBase { "\tif (napi_set_named_property(env, exports, \"NAPI_CLASS_NAME\", NAPI_CLASS_NAMEIns) != napi_ok) {\n" + "\t\treturn nullptr;\n" + "\t}"; - + private String interfaceContent = ""; private String enumContent = ""; private String classContent = ""; diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java index 4fe452d7..d4501f7d 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java @@ -423,7 +423,9 @@ class GenNapiCppFileTest { "};\n" + "\n" + "napi_value TestClassIns = nullptr;\n" + - "\tif (napi_define_class(env, \"TestClass\", NAPI_AUTO_LENGTH, ConstructorTestClass, nullptr, sizeof(TestClassProps) / sizeof(TestClassProps[0]), TestClassProps, &TestClassIns) != napi_ok) {\n" + + "\tif (napi_define_class(env, \"TestClass\", NAPI_AUTO_LENGTH, ConstructorTestClass, " + + "nullptr, sizeof(TestClassProps) / sizeof(TestClassProps[0]), TestClassProps, " + + "&TestClassIns) != napi_ok) {\n" + "\t\treturn nullptr;\n" + "\t}\n" + "\tif (napi_set_named_property(env, exports, \"TestClass\", TestClassIns) != napi_ok) {\n" + @@ -443,7 +445,8 @@ class GenNapiCppFileTest { "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + "\tnapi_get_undefined(env, &undefineVar);\n" + "\n" + - "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) ==" + + " napi_ok && thisVar != nullptr) {\n" + "\t\tEmployee *reference = new Employee();\n" + "\t\tif (napi_wrap(env, thisVar,\n" + "\t\t\treinterpret_cast(reference), DestructorEmployee, nullptr, nullptr) == napi_ok) {\n" + @@ -623,12 +626,14 @@ class GenNapiCppFileTest { "\t{constructor, nullptr, constructorEmployee, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + "\t{displayName, nullptr, displayNameEmployee, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + "\t{empCode, nullptr, nullptr, GetempCodeEmployee, SetempCodeEmployee, nullptr, napi_default, nullptr},\n" + - "\t{currentUser, nullptr, nullptr, GetcurrentUserEmployee, SetcurrentUserEmployee, nullptr, napi_default, nullptr},\n" + + "\t{currentUser, nullptr, nullptr, GetcurrentUserEmployee, SetcurrentUserEmployee, nullptr, " + + "napi_default, nullptr},\n" + "\t{pi, nullptr, nullptr, GetpiEmployee, SetpiEmployee, nullptr, napi_default, nullptr},\n" + "};\n" + "\n" + "napi_value EmployeeIns = nullptr;\n" + - "\tif (napi_define_class(env, \"Employee\", NAPI_AUTO_LENGTH, ConstructorEmployee, nullptr, sizeof(EmployeeProps) / sizeof(EmployeeProps[0]), EmployeeProps, &EmployeeIns) != napi_ok) {\n" + + "\tif (napi_define_class(env, \"Employee\", NAPI_AUTO_LENGTH, ConstructorEmployee, nullptr, " + + "sizeof(EmployeeProps) / sizeof(EmployeeProps[0]), EmployeeProps, &EmployeeIns) != napi_ok) {\n" + "\t\treturn nullptr;\n" + "\t}\n" + "\tif (napi_set_named_property(env, exports, \"Employee\", EmployeeIns) != napi_ok) {\n" + @@ -644,7 +649,8 @@ class GenNapiCppFileTest { "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + "\tnapi_get_undefined(env, &undefineVar);\n" + "\n" + - "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == " + + "napi_ok && thisVar != nullptr) {\n" + "\t\tmyClass *reference = new myClass();\n" + "\t\tif (napi_wrap(env, thisVar,\n" + "\t\t\treinterpret_cast(reference), DestructormyClass, nullptr, nullptr) == napi_ok) {\n" + @@ -688,7 +694,8 @@ class GenNapiCppFileTest { "};\n" + "\n" + "napi_value myClassIns = nullptr;\n" + - "\tif (napi_define_class(env, \"myClass\", NAPI_AUTO_LENGTH, ConstructormyClass, nullptr, sizeof(myClassProps) / sizeof(myClassProps[0]), myClassProps, &myClassIns) != napi_ok) {\n" + + "\tif (napi_define_class(env, \"myClass\", NAPI_AUTO_LENGTH, ConstructormyClass, nullptr, " + + "sizeof(myClassProps) / sizeof(myClassProps[0]), myClassProps, &myClassIns) != napi_ok) {\n" + "\t\treturn nullptr;\n" + "\t}\n" + "\tif (napi_set_named_property(env, exports, \"myClass\", myClassIns) != napi_ok) {\n" + @@ -1959,257 +1966,4 @@ class GenNapiCppFileTest { } } - @Test - void genContent() { - ParseObj po = new ParseObj(); - ParamObj pao = new ParamObj(); - pao.setName("TestParam"); - pao.setType("int"); - pao.setStrValue("100"); - List pol = new CopyOnWriteArrayList<>(); - pol.add(pao); - po.setVarList(pol); - - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genContent(po); - - if (gb instanceof GenNapiCppFile gdf) { - String varContent = gdf.getConstContent(); - System.out.println("genVar: " + varContent); - String expect = "\nextends const int TestParam = 100;\n"; - assertEquals(expect, varContent); - } - } - - @Test - void genFile() { - ParamObj pao = new ParamObj(); - pao.setName("TestParam"); - pao.setType("int"); - pao.setStrValue("100"); - List pol = new CopyOnWriteArrayList<>(); - pol.add(pao); - - ParseObj po = new ParseObj(); - po.setVarList(pol); - - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genContent(po); - gb.genFile("./", "testGenFile.d.ts"); - - File file = new File("./ag_testGenFile_d_ts.h"); - assertEquals(true, file.exists()); - assertEquals(false, file.isDirectory()); - - List fcList = readText("./ag_testGenFile_d_ts.h"); - - assertEquals("// Generated from ./\\testGenFile.d.ts by KaiHong ohgen 1.0.0-PLUGIN", - fcList.get(0)); - assertEquals("const int TestParam = 100;", - fcList.get(1)); - - if (gb instanceof GenNapiCppFile gdf) { - String varContent = gdf.getConstContent(); - System.out.println("genVar: " + varContent); - String expect = "\nextends const int TestParam = 100;\n"; - assertEquals(expect, varContent); - } - } - - @Test - void genInterfaceList() { - } - - @Test - void genEnumList() { - EnumObj eo = new EnumObj(); - eo.setName("TestEnum"); - List ml = new CopyOnWriteArrayList<>(); - ml.add("ONE"); - ml.add("TWO"); - eo.setMemberList(ml); - List vl = new CopyOnWriteArrayList<>(); - vl.add("1"); - vl.add("2"); - eo.setValueList(vl); - List eol = new CopyOnWriteArrayList<>(); - eol.add(eo); - ParseObj po = new ParseObj(); - po.setEnumList(eol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genEnumList(po.getEnumList()); - - if (gb instanceof GenNapiCppFile gdf) { - String enumContent = gdf.getEnumContent(); - System.out.println("genEnum: " + enumContent); - String expect = "\nenum TestEnum {\n" + - "\tONE = 1,\n" + - "\tTWO = 2,\n" + - "};\n"; - assertEquals(expect, enumContent); - } - } - - @Test - void genClassList() { - ClassObj co = new ClassObj(); - co.setName("TestClass"); - - co.addParam("name", "string"); - co.addParam("age", "number"); - - List poList = new CopyOnWriteArrayList<>(); - ParamObj poItem = new ParamObj(); - poItem.setName("a"); - poItem.setType("number"); - poList.add(poItem); - ParamObj poItem2 = new ParamObj(); - poItem2.setName("b"); - poItem2.setType("number"); - poList.add(poItem2); - - co.addFunc("add", "number", poList); - - poList = new CopyOnWriteArrayList<>(); - poItem = new ParamObj(); - poItem.setType("number"); - poList.add(poItem); - - co.addFunc("delete", "number", poList); - - List col = new CopyOnWriteArrayList<>(); - col.add(co); - - ParseObj po = new ParseObj(); - po.setClassList(col); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genClassList(po.getClassList()); - - if (gb instanceof GenNapiCppFile gdf) { - String classContent = gdf.getClassContent(); - System.out.println("genClass: " + classContent); - String expect = "\nclass TestClass {\n" + - "\tchar* name;\n" + - "\tint age;\n" + - "\tint add(int a, int b);\n" + - "\tint delete(int);\n" + - "};\n"; - assertEquals(expect, classContent); - } - } - - @Test - void genFuncList() { - FuncObj fo = new FuncObj(); - fo.setName("TestFunc"); - fo.setRetValue("void"); - fo.addParam("name", "string"); - fo.addParam("age", "number"); - List fol = new CopyOnWriteArrayList<>(); - fol.add(fo); - ParseObj po = new ParseObj(); - po.setFuncList(fol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genFuncList(po.getFuncList()); - - if (gb instanceof GenNapiCppFile gdf) { - String funcContent = gdf.getFuncContent(); - System.out.println("genFunc: " + funcContent); - String expect = "\nvoid TestFunc(char* name, int age);"; - assertEquals(expect, funcContent); - } - } - - @Test - void genStructList() { - StructObj so = new StructObj(); - so.setName("TestStruct"); - - so.addMember("name", "string"); - so.addMember("age", "number"); - - List poList = new CopyOnWriteArrayList<>(); - ParamObj poItem = new ParamObj(); - poItem.setName("a"); - poItem.setType("int"); - poList.add(poItem); - ParamObj poItem2 = new ParamObj(); - poItem2.setName("b"); - poItem2.setType("int"); - poList.add(poItem2); - - so.addFunc("add", "int", poList); - - List sol = new CopyOnWriteArrayList<>(); - sol.add(so); - ParseObj po = new ParseObj(); - po.setStructList(sol); - - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genStructList(po.getStructList()); - - if (gb instanceof GenNapiCppFile gdf) { - String structContent = gdf.getStructContent(); - System.out.println("genStruct: " + structContent); - String expect = "\nstruct TestStruct {\n" + - "\tchar* name;\n" + - "\tint age;\n" + - "\tint add(int a, int b);\n" + - "};\n"; - assertEquals(expect, structContent); - } - } - - @Test - void genTypeList() { - TypeObj to = new TypeObj(); - } - - @Test - void genUnionList() { - UnionObj uo = new UnionObj(); - uo.setName("TestUnion"); - - uo.addMember("name", "any"); - uo.addMember("age", "number"); - - List uol = new CopyOnWriteArrayList<>(); - uol.add(uo); - ParseObj po = new ParseObj(); - po.setUnionList(uol); - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genUnionList(po.getUnionList()); - - if (gb instanceof GenNapiCppFile gdf) { - String unionContent = gdf.getUnionContent(); - System.out.println("genUnion: " + unionContent); - String expect = "\nunion TestUnion{\n" + - "\tauto name;\n" + - "\tint age;\n" + - "};\n"; - assertEquals(expect, unionContent); - } - } - - @Test - void genVarList() { - ParseObj po = new ParseObj(); - ParamObj pao = new ParamObj(); - pao.setName("TestParam"); - pao.setType("number"); - pao.setStrValue("100"); - List pol = new CopyOnWriteArrayList<>(); - pol.add(pao); - po.setVarList(pol); - - GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); - gb.genVarList(pol); - - if (gb instanceof GenNapiCppFile gdf) { - String varContent = gdf.getConstContent(); - System.out.println("genVar: " + varContent); - String expect = "\nextends const int TestParam = 100;\n"; - assertEquals(expect, varContent); - } - } } \ No newline at end of file -- Gitee From b5704c8ae5f631e03cdc5b53e294b3c70ed2f42c Mon Sep 17 00:00:00 2001 From: sunlian Date: Fri, 18 Apr 2025 17:48:07 +0800 Subject: [PATCH 4/6] fix code check Signed-off-by: sunlian --- .../test/java/gen/GenNapiCppFileTest2.java | 547 ++++++++++++++++++ 1 file changed, 547 insertions(+) create mode 100644 src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java new file mode 100644 index 00000000..06e0e311 --- /dev/null +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java @@ -0,0 +1,547 @@ +/* + * Copyright (c) 2025 Shenzhen Kaihong Digital. + * 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. + */ + +package gen; + +import grammar.*; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static utils.FileUtils.readText; + +/** + *

类名:该类用于xxx

+ * description + * + * @author Administrator + * date 2025-02-28 + * @version 1.0 + * @since 2025-02-28 + */ +class GenNapiCppFileTest2 { + + @Test + void getInterfaceContent() { + } + + @Test + void getTypeContent() { + } + + @Test + void getUnionContent1() { + UnionObj uo = new UnionObj(); + uo.setName("TestUnion"); + + uo.addMember("name", "string"); + uo.addMember("age", "number"); + + List uol = new CopyOnWriteArrayList<>(); + uol.add(uo); + ParseObj po = new ParseObj(); + po.setUnionList(uol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenNapiCppFile gdf) { + String unionContent = gdf.getUnionContent(); + System.out.println("genUnion: " + unionContent); + String expect = "\nunion TestUnion{\n" + + "\tchar* name;\n" + + "\tint age;\n" + + "};\n"; + assertEquals(expect, unionContent); + } + } + + @Test + void getUnionContent2() { + UnionObj uo = new UnionObj(); + uo.setName("TestUnion"); + uo.addMember("name", "T"); + uo.addMember("age", "U"); + + uo.addTemplate("T"); + uo.addTemplate("U"); + + List uol = new CopyOnWriteArrayList<>(); + uol.add(uo); + ParseObj po = new ParseObj(); + po.setUnionList(uol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenNapiCppFile gdf) { + String unionContent = gdf.getUnionContent(); + System.out.println("genUnion: " + unionContent); + String expect = "\ntemplate union TestUnion{\n" + + "\tT name;\n" + + "\tU age;\n" + + "};\n"; + assertEquals(expect, unionContent); + } + } + + @Test + void getVarContent1() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent2() { + ParamObj paObj = new ParamObj(); + paObj.setName("employeeName"); + paObj.setType("string"); + paObj.setStrValue("\"John\""); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const char* employeeName = \"John\";\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent3() { + ParamObj paObj = new ParamObj(); + paObj.setName("num1"); + paObj.setType("number"); + paObj.setStrValue("1"); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const int num1 = 1;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent4() { + ParamObj paObj = new ParamObj(); + paObj.setName("playerCodes"); + + ParamObj paItem1 = new ParamObj(); + paItem1.setName("player1"); + paItem1.setStrValue("9"); + paObj.addParam(paItem1); + ParamObj paItem2 = new ParamObj(); + paItem2.setName("player2"); + paItem2.setStrValue("10"); + paObj.addParam(paItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const std::map pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nextends const auto playerCodes.player2 = 11;\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getVarContent6() { + ParamObj paObj = new ParamObj(); + paObj.setName("ROUTES"); + paObj.setType("any[]"); + + ParamObj paListItem1 = new ParamObj(); + ParamObj paItem1 = new ParamObj(); + paItem1.setName("path"); + paItem1.setStrValue("'/dashboard'"); + paListItem1.addParam(paItem1); + + ParamObj paItem3 = new ParamObj(); + paItem3.setName("allowAnonymous"); + paItem3.setStrValue("false"); + paListItem1.addParam(paItem3); + paObj.addParam(paListItem1); + + ParamObj paListItem2 = new ParamObj(); + ParamObj paItem21 = new ParamObj(); + paItem21.setName("path"); + paItem21.setStrValue("'/deals'"); + paListItem2.addParam(paItem21); + + ParamObj paItem23 = new ParamObj(); + paItem23.setName("allowAnonymous"); + paItem23.setStrValue("true"); + paListItem2.addParam(paItem23); + paObj.addParam(paListItem2); + + List pol = new CopyOnWriteArrayList<>(); + pol.add(paObj); + ParseObj po = new ParseObj(); + po.setVarList(pol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(po.getVarList()); + + if (gb instanceof GenNapiCppFile gdf) { + String constContent = gdf.getConstContent(); + System.out.println("getVar: " + constContent); + String expect = "\nstruct ROUTESST {\n" + + "\tstd::string path;\n" + + "\tboolean allowAnonymous;\n" + + "};\n" + + "\n" + + "const std::vector ROUTES = {\n" + + "\t{'/dashboard', false},\n" + + "\t{'/deals', true},\n" + + "};\n"; + assertEquals(expect, constContent); + } + } + + @Test + void getConstContent() { + ParseObj po = new ParseObj(); + ParamObj pao = new ParamObj(); + pao.setName("TestParam"); + pao.setType("int"); + pao.setStrValue("100"); + List pol = new CopyOnWriteArrayList<>(); + pol.add(pao); + po.setVarList(pol); + + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(pol); + + if (gb instanceof GenNapiCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } + + @Test + void genContent() { + ParseObj po = new ParseObj(); + ParamObj pao = new ParamObj(); + pao.setName("TestParam"); + pao.setType("int"); + pao.setStrValue("100"); + List pol = new CopyOnWriteArrayList<>(); + pol.add(pao); + po.setVarList(pol); + + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genContent(po); + + if (gb instanceof GenNapiCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } + + @Test + void genFile() { + ParamObj pao = new ParamObj(); + pao.setName("TestParam"); + pao.setType("int"); + pao.setStrValue("100"); + List pol = new CopyOnWriteArrayList<>(); + pol.add(pao); + + ParseObj po = new ParseObj(); + po.setVarList(pol); + + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genContent(po); + gb.genFile("./", "testGenFile.d.ts"); + + File file = new File("./ag_testGenFile_d_ts.h"); + assertEquals(true, file.exists()); + assertEquals(false, file.isDirectory()); + + List fcList = readText("./ag_testGenFile_d_ts.h"); + + assertEquals("// Generated from ./\\testGenFile.d.ts by KaiHong ohgen 1.0.0-PLUGIN", + fcList.get(0)); + assertEquals("const int TestParam = 100;", + fcList.get(1)); + + if (gb instanceof GenNapiCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } + + @Test + void genInterfaceList() { + } + + @Test + void genEnumList() { + EnumObj eo = new EnumObj(); + eo.setName("TestEnum"); + List ml = new CopyOnWriteArrayList<>(); + ml.add("ONE"); + ml.add("TWO"); + eo.setMemberList(ml); + List vl = new CopyOnWriteArrayList<>(); + vl.add("1"); + vl.add("2"); + eo.setValueList(vl); + List eol = new CopyOnWriteArrayList<>(); + eol.add(eo); + ParseObj po = new ParseObj(); + po.setEnumList(eol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genEnumList(po.getEnumList()); + + if (gb instanceof GenNapiCppFile gdf) { + String enumContent = gdf.getEnumContent(); + System.out.println("genEnum: " + enumContent); + String expect = "\nenum TestEnum {\n" + + "\tONE = 1,\n" + + "\tTWO = 2,\n" + + "};\n"; + assertEquals(expect, enumContent); + } + } + + @Test + void genClassList() { + ClassObj co = new ClassObj(); + co.setName("TestClass"); + + co.addParam("name", "string"); + co.addParam("age", "number"); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType("number"); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType("number"); + poList.add(poItem2); + + co.addFunc("add", "number", poList); + + poList = new CopyOnWriteArrayList<>(); + poItem = new ParamObj(); + poItem.setType("number"); + poList.add(poItem); + + co.addFunc("delete", "number", poList); + + List col = new CopyOnWriteArrayList<>(); + col.add(co); + + ParseObj po = new ParseObj(); + po.setClassList(col); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genClassList(po.getClassList()); + + if (gb instanceof GenNapiCppFile gdf) { + String classContent = gdf.getClassContent(); + System.out.println("genClass: " + classContent); + String expect = "\nclass TestClass {\n" + + "\tchar* name;\n" + + "\tint age;\n" + + "\tint add(int a, int b);\n" + + "\tint delete(int);\n" + + "};\n"; + assertEquals(expect, classContent); + } + } + + @Test + void genFuncList() { + FuncObj fo = new FuncObj(); + fo.setName("TestFunc"); + fo.setRetValue("void"); + fo.addParam("name", "string"); + fo.addParam("age", "number"); + List fol = new CopyOnWriteArrayList<>(); + fol.add(fo); + ParseObj po = new ParseObj(); + po.setFuncList(fol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genFuncList(po.getFuncList()); + + if (gb instanceof GenNapiCppFile gdf) { + String funcContent = gdf.getFuncContent(); + System.out.println("genFunc: " + funcContent); + String expect = "\nvoid TestFunc(char* name, int age);"; + assertEquals(expect, funcContent); + } + } + + @Test + void genStructList() { + StructObj so = new StructObj(); + so.setName("TestStruct"); + + so.addMember("name", "string"); + so.addMember("age", "number"); + + List poList = new CopyOnWriteArrayList<>(); + ParamObj poItem = new ParamObj(); + poItem.setName("a"); + poItem.setType("int"); + poList.add(poItem); + ParamObj poItem2 = new ParamObj(); + poItem2.setName("b"); + poItem2.setType("int"); + poList.add(poItem2); + + so.addFunc("add", "int", poList); + + List sol = new CopyOnWriteArrayList<>(); + sol.add(so); + ParseObj po = new ParseObj(); + po.setStructList(sol); + + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genStructList(po.getStructList()); + + if (gb instanceof GenNapiCppFile gdf) { + String structContent = gdf.getStructContent(); + System.out.println("genStruct: " + structContent); + String expect = "\nstruct TestStruct {\n" + + "\tchar* name;\n" + + "\tint age;\n" + + "\tint add(int a, int b);\n" + + "};\n"; + assertEquals(expect, structContent); + } + } + + @Test + void genTypeList() { + TypeObj to = new TypeObj(); + } + + @Test + void genUnionList() { + UnionObj uo = new UnionObj(); + uo.setName("TestUnion"); + + uo.addMember("name", "any"); + uo.addMember("age", "number"); + + List uol = new CopyOnWriteArrayList<>(); + uol.add(uo); + ParseObj po = new ParseObj(); + po.setUnionList(uol); + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genUnionList(po.getUnionList()); + + if (gb instanceof GenNapiCppFile gdf) { + String unionContent = gdf.getUnionContent(); + System.out.println("genUnion: " + unionContent); + String expect = "\nunion TestUnion{\n" + + "\tauto name;\n" + + "\tint age;\n" + + "};\n"; + assertEquals(expect, unionContent); + } + } + + @Test + void genVarList() { + ParseObj po = new ParseObj(); + ParamObj pao = new ParamObj(); + pao.setName("TestParam"); + pao.setType("number"); + pao.setStrValue("100"); + List pol = new CopyOnWriteArrayList<>(); + pol.add(pao); + po.setVarList(pol); + + GeneratorBase gb = GenerateFactory.getGenerator("NAPICPP"); + gb.genVarList(pol); + + if (gb instanceof GenNapiCppFile gdf) { + String varContent = gdf.getConstContent(); + System.out.println("genVar: " + varContent); + String expect = "\nextends const int TestParam = 100;\n"; + assertEquals(expect, varContent); + } + } +} \ No newline at end of file -- Gitee From 63877019082fccf4af69f54d380a9141f2ad9883 Mon Sep 17 00:00:00 2001 From: sunlian Date: Fri, 18 Apr 2025 17:56:51 +0800 Subject: [PATCH 5/6] fix code check Signed-off-by: sunlian --- .../src/main/java/gen/GenNapiCppFile.java | 9 +- .../src/test/java/gen/GenNapiCppFileTest.java | 2 - .../test/java/gen/GenNapiCppFileTest2.java | 201 +++++++++++++++++- 3 files changed, 196 insertions(+), 16 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java index bef9ea97..7bcb3daa 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java @@ -172,7 +172,8 @@ public class GenNapiCppFile extends GeneratorBase { "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + "\tnapi_get_undefined(env, &undefineVar);\n" + "\n" + - "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok &&" + + " thisVar != nullptr) {\n" + "\t\tNAPI_CLASS_NAME *reference = new NAPI_CLASS_NAME();\n" + "\t\tif (napi_wrap(env, thisVar,\n" + "\t\t\treinterpret_cast(reference), DestructorNAPI_CLASS_NAME, nullptr, nullptr) == napi_ok) {\n" + @@ -783,7 +784,8 @@ public class GenNapiCppFile extends GeneratorBase { templateStr += NAPI_TYPE_NAME_TOKEN + NAPI_BLANK_SPACE + teStr + NAPI_COMMA + NAPI_BLANK_SPACE; } templateStr = templateStr.length() > 1 ? - StringUtils.removeLastCharacter(templateStr, 2) + NAPI_RIGHT_ANGLE_BRACKET + NAPI_BLANK_SPACE : ""; + StringUtils.removeLastCharacter(templateStr, 2) + + NAPI_RIGHT_ANGLE_BRACKET + NAPI_BLANK_SPACE : ""; List paList = so.getMemberList(); resContent += NAPI_NEW_LINE + templateStr + NAPI_STRUCT_TOKEN + @@ -855,7 +857,8 @@ public class GenNapiCppFile extends GeneratorBase { templateStr += NAPI_TYPE_NAME_TOKEN + NAPI_BLANK_SPACE + teStr + NAPI_COMMA + NAPI_BLANK_SPACE; } templateStr = templateStr.length() > 1 ? - StringUtils.removeLastCharacter(templateStr, 2) + NAPI_RIGHT_ANGLE_BRACKET + NAPI_BLANK_SPACE : ""; + StringUtils.removeLastCharacter(templateStr, 2) + + NAPI_RIGHT_ANGLE_BRACKET + NAPI_BLANK_SPACE : ""; resContent += NAPI_NEW_LINE + templateStr + NAPI_UNION_TOKEN + NAPI_BLANK_SPACE + unionName + NAPI_LEFT_BRACE; diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java index d4501f7d..5d20b86e 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java @@ -18,12 +18,10 @@ package gen; import grammar.*; import org.junit.jupiter.api.Test; -import java.io.File; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import static org.junit.jupiter.api.Assertions.assertEquals; -import static utils.FileUtils.readText; /** *

类名:该类用于xxx

diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java index 06e0e311..2c421a00 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java @@ -35,6 +35,170 @@ import static utils.FileUtils.readText; * @since 2025-02-28 */ class GenNapiCppFileTest2 { + private String classContTest1 = "\nclass TestClass {\n" + + "\tstd::string name;\n" + + "\tint age;\n" + + "\tint add(int a, int b);\n" + + "\tint delete(int);\n" + + "};\n" + + "\n" + + "napi_value ConstructorTestClass(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + + "\tnapi_get_undefined(env, &undefineVar);\n" + + "\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\t\tTestClass *reference = new TestClass();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorTestClass, nullptr, nullptr) == napi_ok) {\n" + + "\t\t\treturn thisVar;\n" + + "\t\t}\n" + + "\t\treturn thisVar;\n" + + "\t}\n" + + "\treturn undefineVar;\n" + + "};\n" + + "\n" + + "void DestructorTestClass(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value addTestClass(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value deleteTestClass(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\t// 获取napi对象\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\t// 调用原始类方法\n" + + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\t// 创建返回参数\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Getname(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setname(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->name = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_value Getage(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_status status;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setage(napi_env env, napi_callback_info info)\n" + + "{\n" + + "\tnapi_value result = nullptr;\n" + + "\tnapi_get_undefined(env, &result);\n" + + "\tchar msg[128] = {0};\n" + + "\tnapi_value jsthis;\n" + + "\tnapi_value msgvalue;\n" + + "\tnapi_status status;\n" + + "\tsize_t argc = 1, size = 0;\n" + + "\tif (napi_get_cb_info(env, info, &argc, &msgvalue, &jsthis, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\tTestClass *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tobj->age = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor TestClassProps[] = {\n" + + "\t{add, nullptr, addTestClass, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{delete, nullptr, deleteTestClass, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{name, nullptr, nullptr, GetnameTestClass, SetnameTestClass, nullptr, napi_default, nullptr},\n" + + "\t{age, nullptr, nullptr, GetageTestClass, SetageTestClass, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value TestClassIns = nullptr;\n" + + "\tif (napi_define_class(env, \"TestClass\", NAPI_AUTO_LENGTH, ConstructorTestClass, nullptr, sizeof(TestClassProps) / sizeof(TestClassProps[0]), TestClassProps, &TestClassIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"TestClass\", TestClassIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; @Test void getInterfaceContent() { @@ -63,7 +227,7 @@ class GenNapiCppFileTest2 { String unionContent = gdf.getUnionContent(); System.out.println("genUnion: " + unionContent); String expect = "\nunion TestUnion{\n" + - "\tchar* name;\n" + + "\tstd::string name;\n" + "\tint age;\n" + "};\n"; assertEquals(expect, unionContent); @@ -136,7 +300,7 @@ class GenNapiCppFileTest2 { if (gb instanceof GenNapiCppFile gdf) { String constContent = gdf.getConstContent(); System.out.println("getVar: " + constContent); - String expect = "\nextends const char* employeeName = \"John\";\n"; + String expect = "\nextends const std::string employeeName = \"John\";\n"; assertEquals(expect, constContent); } } @@ -377,7 +541,27 @@ class GenNapiCppFileTest2 { String expect = "\nenum TestEnum {\n" + "\tONE = 1,\n" + "\tTWO = 2,\n" + - "};\n"; + "};\n" + + "\n" + + "// 创建枚举对象\n" + + "napi_value CreateTestEnumEnum(napi_env env) {\n" + + "\tnapi_value enum_obj;\n" + + "\tnapi_create_object(env, &enum_obj);\n" + + "\n" + + "\t// 添加枚举成员\n" + + "\tconst char* members[] = {\"ONE\", \"TWO\"};\n" + + "\tconst int values[] = {1, 2};\n" + + "\tfor (int32_t i = 0; i < 2; ++i) {\n" + + "\t\tnapi_value value;\n" + + "\t\tnapi_create_int32(env, value[i], &value);\n" + + "\t\tnapi_set_named_property(env, enum_obj, members[i], value);\n" + + "\t}\n" + + "\n" + + "\treturn enum_obj;\n" + + "}\n" + + "\t// 创建并绑定枚举\n" + + "\tnapi_value TestEnum_enum = CreateTestEnumEnum(env);\n" + + "\tnapi_set_named_property(env, exports, \"TestEnum\", TestEnum_enum);\n"; assertEquals(expect, enumContent); } } @@ -420,12 +604,7 @@ class GenNapiCppFileTest2 { if (gb instanceof GenNapiCppFile gdf) { String classContent = gdf.getClassContent(); System.out.println("genClass: " + classContent); - String expect = "\nclass TestClass {\n" + - "\tchar* name;\n" + - "\tint age;\n" + - "\tint add(int a, int b);\n" + - "\tint delete(int);\n" + - "};\n"; + String expect = classContTest1; assertEquals(expect, classContent); } } @@ -447,7 +626,7 @@ class GenNapiCppFileTest2 { if (gb instanceof GenNapiCppFile gdf) { String funcContent = gdf.getFuncContent(); System.out.println("genFunc: " + funcContent); - String expect = "\nvoid TestFunc(char* name, int age);"; + String expect = "\nvoid TestFunc(std::string name, int age);"; assertEquals(expect, funcContent); } } @@ -484,7 +663,7 @@ class GenNapiCppFileTest2 { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); String expect = "\nstruct TestStruct {\n" + - "\tchar* name;\n" + + "\tstd::string name;\n" + "\tint age;\n" + "\tint add(int a, int b);\n" + "};\n"; -- Gitee From 98bbe42004ff5f797b909c8c21df2f6973cbd13c Mon Sep 17 00:00:00 2001 From: sunlian Date: Fri, 18 Apr 2025 18:13:10 +0800 Subject: [PATCH 6/6] fix code check Signed-off-by: sunlian --- .../ohosgen/src/test/java/gen/GenNapiCppFileTest.java | 7 +++++-- .../ohosgen/src/test/java/gen/GenNapiCppFileTest2.java | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java index 5d20b86e..9834e6d7 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java @@ -711,7 +711,8 @@ class GenNapiCppFileTest { "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + "\tnapi_get_undefined(env, &undefineVar);\n" + "\n" + - "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == " + + "napi_ok && thisVar != nullptr) {\n" + "\t\tKeyValuePair *reference = new KeyValuePair();\n" + "\t\tif (napi_wrap(env, thisVar,\n" + "\t\t\treinterpret_cast(reference), DestructorKeyValuePair, nullptr, nullptr) == napi_ok) {\n" + @@ -833,7 +834,9 @@ class GenNapiCppFileTest { "};\n" + "\n" + "napi_value KeyValuePairIns = nullptr;\n" + - "\tif (napi_define_class(env, \"KeyValuePair\", NAPI_AUTO_LENGTH, ConstructorKeyValuePair, nullptr, sizeof(KeyValuePairProps) / sizeof(KeyValuePairProps[0]), KeyValuePairProps, &KeyValuePairIns) != napi_ok) {\n" + + "\tif (napi_define_class(env, \"KeyValuePair\", NAPI_AUTO_LENGTH, ConstructorKeyValuePair, " + + "nullptr, sizeof(KeyValuePairProps) / sizeof(KeyValuePairProps[0]), KeyValuePairProps, " + + "&KeyValuePairIns) != napi_ok) {\n" + "\t\treturn nullptr;\n" + "\t}\n" + "\tif (napi_set_named_property(env, exports, \"KeyValuePair\", KeyValuePairIns) != napi_ok) {\n" + diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java index 2c421a00..3c2c3d5a 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java @@ -47,7 +47,8 @@ class GenNapiCppFileTest2 { "\tnapi_value undefineVar = nullptr, thisVar = nullptr;\n" + "\tnapi_get_undefined(env, &undefineVar);\n" + "\n" + - "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && thisVar != nullptr) {\n" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == " + + "napi_ok && thisVar != nullptr) {\n" + "\t\tTestClass *reference = new TestClass();\n" + "\t\tif (napi_wrap(env, thisVar,\n" + "\t\t\treinterpret_cast(reference), DestructorTestClass, nullptr, nullptr) == napi_ok) {\n" + @@ -193,7 +194,9 @@ class GenNapiCppFileTest2 { "};\n" + "\n" + "napi_value TestClassIns = nullptr;\n" + - "\tif (napi_define_class(env, \"TestClass\", NAPI_AUTO_LENGTH, ConstructorTestClass, nullptr, sizeof(TestClassProps) / sizeof(TestClassProps[0]), TestClassProps, &TestClassIns) != napi_ok) {\n" + + "\tif (napi_define_class(env, \"TestClass\", NAPI_AUTO_LENGTH, ConstructorTestClass, nullptr, " + + "sizeof(TestClassProps) / sizeof(TestClassProps[0]), TestClassProps, " + + "&TestClassIns) != napi_ok) {\n" + "\t\treturn nullptr;\n" + "\t}\n" + "\tif (napi_set_named_property(env, exports, \"TestClass\", TestClassIns) != napi_ok) {\n" + -- Gitee