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 d5cb15e9cd699780ebc744033e7d8b70c97779b6..f68331d122ae5c7204649e51b71fa2399e3d0832 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenAkiCppFile.java @@ -662,6 +662,96 @@ public class GenAkiCppFile extends GeneratorBase { System.out.println("genFuncList : " + resContent); }; + private String genCppStructContent(StructObj so) { + String structName = so.getName(); + structName = !structName.isEmpty() ? structName : so.getAlias(); + + String templateStr = !so.getTemplateList().isEmpty() ? + AKI_TEMPLATE_TOKEN + AKI_BLANK_SPACE + AKI_LEFT_ANGLE_BRACKET : ""; + for (String teStr : so.getTemplateList()) { + templateStr += AKI_TYPE_NAME_TOKEN + AKI_BLANK_SPACE + teStr + AKI_COMMA + AKI_BLANK_SPACE; + } + templateStr = templateStr.length() > 1 ? + StringUtils.removeLastCharacter(templateStr, 2) + AKI_RIGHT_ANGLE_BRACKET + AKI_BLANK_SPACE : ""; + + List paList = so.getMemberList(); + String resContent = ""; + resContent += AKI_NEW_LINE + templateStr + AKI_STRUCT_TOKEN + + AKI_BLANK_SPACE + structName + AKI_BLANK_SPACE + AKI_LEFT_BRACE; + + for (ParamObj paItem : paList) { + String paType = paItem.getType().isEmpty() ? AKI_AUTO_TOKEN : paItem.getType(); + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + ts2CppKey(paType) + + AKI_BLANK_SPACE + paItem.getName(); + ; + List initVList = paItem.getvList(); + if (initVList.size() > 0) { + resContent += AKI_EQUAL + initVList.get(0) + AKI_SEMICOLON; + } else { + resContent += AKI_SEMICOLON; + } + } + + List funcList = so.getFuncList(); + for (FuncObj funcItem : funcList) { + String retValue = ts2CppKey(funcItem.getRetValue()).isEmpty() ? "" : + ts2CppKey(funcItem.getRetValue()) + AKI_BLANK_SPACE; + resContent += AKI_NEW_LINE + AKI_TAB_SPACE + retValue + + replaceTsToken(funcItem.getName()) + AKI_LEFT_PARENTHESES; + List pol = funcItem.getParamList(); + for (ParamObj poItem : pol) { + String retType = ts2CppKey(poItem.getType()).isEmpty() ? + AKI_AUTO_TOKEN : ts2CppKey(poItem.getType()); + resContent += retType + AKI_BLANK_SPACE + replaceTsToken(poItem.getName()) + + AKI_COMMA + AKI_BLANK_SPACE; + } + resContent = !pol.isEmpty() ? StringUtils.removeLastCharacter(resContent, 2) : resContent; + resContent += AKI_RIGHT_PARENTHESES + AKI_SEMICOLON; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; + return resContent; + } + + private String genAkiCppStructContent(StructObj so) { + String structName = so.getName(); + structName = !structName.isEmpty() ? structName : so.getAlias(); + + String structDeclare = AKI_CLASS_DECLARE.replace(AKI_CLASS_NAME, structName); + String classPropertyDeclare = ""; + + List paList = so.getMemberList(); + for (ParamObj paItem : paList) { + classPropertyDeclare += AKI_PROPERTY_DECLARE.replace(AKI_PROPERTY_NAME, paItem.getName()); + } + structDeclare = structDeclare.replace(AKI_PROPERTY_EXPRESSION, classPropertyDeclare); + + List foList = so.getFuncList(); + String classFunctionDeclare = ""; + String classConstructDeclare = ""; + for (FuncObj foItem : foList) { + if (foItem.getName().equals("constructor")) { + String paramStr = ""; + for (ParamObj poItem : foItem.getParamList()) { + paramStr += poItem.getType() + AKI_COMMA + AKI_BLANK_SPACE; + } + paramStr = StringUtils.removeLastCharacter(paramStr, 2); + classConstructDeclare += AKI_CONSTRUCTOR_DECLARE.replace( + AKI_CONSTRUCTOR_PARAMS, paramStr); + } else { + classFunctionDeclare += AKI_METHOD_DECLARE.replace(AKI_METHOD_NAME, foItem.getName()); + } + } + + String classPFunctionDeclare = ""; + structDeclare = structDeclare.replace(AKI_METHOD_EXPRESSION, classFunctionDeclare); + structDeclare = structDeclare.replace(AKI_CONSTRUCTOR_EXPRESSION, classConstructDeclare); + structDeclare = structDeclare.replace(AKI_PMETHOD_EXPRESSION, classPFunctionDeclare); + + return structDeclare; + } + /** * 生成输出内容 * @@ -673,53 +763,8 @@ public class GenAkiCppFile extends GeneratorBase { String resContent = ""; for (StructObj so : sol) { - String structName = so.getName(); - structName = !structName.isEmpty() ? structName : so.getAlias(); - - String templateStr = !so.getTemplateList().isEmpty() ? - AKI_TEMPLATE_TOKEN + AKI_BLANK_SPACE + AKI_LEFT_ANGLE_BRACKET : ""; - for (String teStr : so.getTemplateList()) { - templateStr += AKI_TYPE_NAME_TOKEN + AKI_BLANK_SPACE + teStr + AKI_COMMA + AKI_BLANK_SPACE; - } - templateStr = templateStr.length() > 1 ? - StringUtils.removeLastCharacter(templateStr, 2) + AKI_RIGHT_ANGLE_BRACKET + AKI_BLANK_SPACE : ""; - - List paList = so.getMemberList(); - resContent += AKI_NEW_LINE + templateStr + AKI_STRUCT_TOKEN + - AKI_BLANK_SPACE + structName + AKI_BLANK_SPACE + AKI_LEFT_BRACE; - - for (ParamObj paItem : paList) { - String paType = paItem.getType().isEmpty() ? AKI_AUTO_TOKEN : paItem.getType(); - resContent += AKI_NEW_LINE + AKI_TAB_SPACE + ts2CppKey(paType) + - AKI_BLANK_SPACE + paItem.getName(); - ; - List initVList = paItem.getvList(); - if (initVList.size() > 0) { - resContent += AKI_EQUAL + initVList.get(0) + AKI_SEMICOLON; - } else { - resContent += AKI_SEMICOLON; - } - } - - List funcList = so.getFuncList(); - for (FuncObj funcItem : funcList) { - String retValue = ts2CppKey(funcItem.getRetValue()).isEmpty() ? "" : - ts2CppKey(funcItem.getRetValue()) + AKI_BLANK_SPACE; - resContent += AKI_NEW_LINE + AKI_TAB_SPACE + retValue + - replaceTsToken(funcItem.getName()) + AKI_LEFT_PARENTHESES; - List pol = funcItem.getParamList(); - for (ParamObj poItem : pol) { - String retType = ts2CppKey(poItem.getType()).isEmpty() ? - AKI_AUTO_TOKEN : ts2CppKey(poItem.getType()); - resContent += retType + AKI_BLANK_SPACE + replaceTsToken(poItem.getName()) + - AKI_COMMA + AKI_BLANK_SPACE; - } - resContent = !pol.isEmpty() ? StringUtils.removeLastCharacter(resContent, 2) : resContent; - resContent += AKI_RIGHT_PARENTHESES + AKI_SEMICOLON; - } - - resContent = StringUtils.removeLastSpace(resContent); - resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; + resContent += genCppStructContent(so); + resContent += genAkiCppStructContent(so); } this.structContent = resContent; }; 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 76b29b06e29bdfefeb6de9736b7be20710e69f62..c13e9ac28c179cf9a3652084dd6b041b226e4ce0 100644 --- a/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java +++ b/src/intellij_plugin/ohosgen/src/main/java/gen/GenNapiCppFile.java @@ -190,7 +190,7 @@ public class GenNapiCppFile extends GeneratorBase { "\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" + + "\nnapi_value GetNAPI_CLASS_ATTRIBUTE_NAMENAPI_CLASS_NAME(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -208,7 +208,7 @@ public class GenNapiCppFile extends GeneratorBase { "\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" + + "\nnapi_value SetNAPI_CLASS_ATTRIBUTE_NAMENAPI_CLASS_NAME(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -241,11 +241,11 @@ public class GenNapiCppFile extends GeneratorBase { "\tNAPI_CLASS_NAME *obj;\n" + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + - "\t// 获取参数" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + - "\t// 调用原始类方法" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE" + + "\t// 调用原始类方法\n" + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + - "\t// 创建返回参数" + + "\t// 创建返回参数\n" + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + "\t}\n" + "\treturn result;\n" + @@ -253,10 +253,10 @@ public class GenNapiCppFile extends GeneratorBase { 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, " + + "\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, " + + "\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" + @@ -283,11 +283,11 @@ public class GenNapiCppFile extends GeneratorBase { private static final String NAPI_CLASS_CALL_METHOD_DECLARE = "NAPI_CLASS_CALL_METHOD_DECLARE"; private static final String NAPI_CLASS_RETURN_VALUE_DECLARE = "NAPI_CLASS_RETURN_VALUE_DECLARE"; - private static final String NAPI_FUNCTION_CALL_EXPRESSION = "\n\tNAPI_FUNCTION_NAME(NAPI_PARAM_EXPRESSION);"; + private static final String NAPI_FUNCTION_CALL_EXPRESSION = "NAPI_FUNCTION_NAME(NAPI_PARAM_EXPRESSION);"; private static final String NAPI_PARAM_CNT = "NAPI_PARAM_CNT"; private static final String NAPI_PARAM_CHECK = - "\n\tsize_t argc = NAPI_PARAM_CNT;" + + "size_t argc = NAPI_PARAM_CNT;" + "\n\tnapi_value args[NAPI_PARAM_CNT] = {nullptr};" + "\n\tnapi_value this_arg;" + "\n\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);" + @@ -295,7 +295,7 @@ public class GenNapiCppFile extends GeneratorBase { "\n\tif (argc < NAPI_PARAM_CNT) {" + "\n\t\tnapi_throw_error(env, \"EINVAL\", \"需要NAPI_PARAM_CNT个参数\");" + "\n\t\treturn nullptr;" + - "\n\t};\n"; + "\n\t};\n\n"; private static final String NAPI_FUNCTION_DECLARE = "\nnapi_value NAPI_FUNCTION_NAMENapi(napi_env env, napi_callback_info info)\n" + @@ -304,11 +304,11 @@ public class GenNapiCppFile extends GeneratorBase { "\tnapi_value jsthis;\n" + "\tnapi_status status;\n" + "\tnapi_get_undefined(env, &result);\n" + - "\t// 获取参数" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + - "\t// 调用原始类方法" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE" + + "\t// 调用原始类方法\n" + "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + - "\t// 创建返回参数" + + "\t// 创建返回参数\n" + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + "\treturn result;\n" + "};\n"; @@ -322,6 +322,41 @@ public class GenNapiCppFile extends GeneratorBase { "};\n" + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private static final String NAPI_VAL_GET_DECLARE = + "\nnapi_value GetNAPI_CLASS_ATTRIBUTE_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" + + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n"; + private static final String NAPI_VAL_SET_DECLARE = + "\nnapi_value SetNAPI_CLASS_ATTRIBUTE_NAMENAPI_CLASS_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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tNAPI_CLASS_ATTRIBUTE_NAME = msg;\n" + + "\treturn nullptr;\n" + + "};\n"; + private String interfaceContent = ""; private String enumContent = ""; private String classContent = ""; @@ -349,7 +384,7 @@ public class GenNapiCppFile extends GeneratorBase { ); private final Map getArguMap = Map.ofEntries( - Map.entry("bool", "\n\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + + Map.entry("bool", "\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + "\n\tif (napi_typeof(env, args[NAPI_PARAM_CNT], &valuetypeNAPI_PARAM_CNT) != napi_ok) {" + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");" + @@ -367,8 +402,8 @@ public class GenNapiCppFile extends GeneratorBase { "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_double error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + "\n\t\treturn result;" + - "\n\t};\n"), - Map.entry("string", "\n\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + + "\n\t};\n\n"), + Map.entry("string", "\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + "\n\tif (napi_typeof(env, args[NAPI_PARAM_CNT], &valuetypeNAPI_PARAM_CNT) != napi_ok) {" + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");" + @@ -390,8 +425,8 @@ public class GenNapiCppFile extends GeneratorBase { "\"napi_get_value_string_utf8 error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + "\n\t\treturn result;" + - "\n\t};\n"), - Map.entry("number", "\n\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + + "\n\t};\n\n"), + Map.entry("number", "\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + "\n\tif (napi_typeof(env, args[NAPI_PARAM_CNT], &valuetypeNAPI_PARAM_CNT) != napi_ok) {" + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");" + @@ -413,8 +448,8 @@ public class GenNapiCppFile extends GeneratorBase { "\"napi_get_value_int32 error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + "\n\t\treturn result;" + - "\n\t};\n"), - Map.entry("double", "\n\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + + "\n\t};\n\n"), + Map.entry("double", "\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + "\n\tif (napi_typeof(env, args[NAPI_PARAM_CNT], &valuetypeNAPI_PARAM_CNT) != napi_ok) {" + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");" + @@ -436,8 +471,8 @@ public class GenNapiCppFile extends GeneratorBase { "\"napi_get_value_double error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + "\n\t\treturn result;" + - "\n\t};\n"), - Map.entry("object", "\n\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + + "\n\t};\n\n"), + Map.entry("object", "\tnapi_valuetype valuetypeNAPI_PARAM_CNT;" + "\n\tif (napi_typeof(env, args[NAPI_PARAM_CNT], &valuetypeNAPI_PARAM_CNT) != napi_ok) {" + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");" + @@ -456,21 +491,21 @@ public class GenNapiCppFile extends GeneratorBase { "\"napi_unwrap error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + "\n\t\treturn result;" + - "\n\t};\n") + "\n\t};\n\n") ); private final Map setArguMap = Map.ofEntries( Map.entry("void", ""), Map.entry("bool", - "\n\tnapi_value valueRetNAPI_PARAM_CNT;\n" + + "napi_value valueRetNAPI_PARAM_CNT;" + "\n\tif (napi_create_uint32(env, args[NAPI_PARAM_CNT], &valueNAPI_PARAM_CNT) != napi_ok) {" + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_double error\");" + "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + "\n\t\treturn result;" + "\n\t};" + - "\n\treturn valueRetNAPI_PARAM_CNT"), + "\n\treturn valueRetNAPI_PARAM_CNT;\n"), Map.entry("string", - "\n\tnapi_value valueRetNAPI_PARAM_CNT;\n" + + "napi_value valueRetNAPI_PARAM_CNT;" + "\n\tif (napi_create_string_utf8(env, args[NAPI_PARAM_CNT], " + "realSize, &valueRetNAPI_PARAM_CNT) != napi_ok) {" + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + @@ -478,9 +513,9 @@ public class GenNapiCppFile extends GeneratorBase { "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + "\n\t\treturn result;" + "\n\t};" + - "\n\treturn valueRetNAPI_PARAM_CNT"), + "\n\treturn valueRetNAPI_PARAM_CNT;\n"), Map.entry("number", - "\n\tnapi_value valueRetNAPI_PARAM_CNT;\n" + + "napi_value valueRetNAPI_PARAM_CNT;" + "\n\tif (napi_create_int32(env, args[NAPI_PARAM_CNT], " + "&valueRetNAPI_PARAM_CNT) != napi_ok) {" + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + @@ -488,9 +523,9 @@ public class GenNapiCppFile extends GeneratorBase { "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + "\n\t\treturn result;" + "\n\t};" + - "\n\treturn valueRetNAPI_PARAM_CNT"), + "\n\treturn valueRetNAPI_PARAM_CNT;\n"), Map.entry("double", - "\n\tnapi_value valueRetNAPI_PARAM_CNT;\n" + + "napi_value valueRetNAPI_PARAM_CNT;" + "\n\tif (napi_create_double(env, args[NAPI_PARAM_CNT], " + "&valueRetNAPI_PARAM_CNT) != napi_ok) {" + "\n\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", " + @@ -498,9 +533,9 @@ public class GenNapiCppFile extends GeneratorBase { "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");" + "\n\t\treturn result;" + "\n\t};" + - "\n\treturn valueRetNAPI_PARAM_CNT"), + "\n\treturn valueRetNAPI_PARAM_CNT;\n"), Map.entry("object", - "\n\tNAPI_PARAM_TYPE *reference = new NAPI_PARAM_TYPE();\n" + + "NAPI_PARAM_TYPE *reference = new NAPI_PARAM_TYPE();" + "\n\tif (napi_wrap(env, thisVar, reinterpret_cast(reference), " + "DesNAPI_PARAM_TYPENAPI_FUNCTION_NAMENAPI_PARAM_CNT, nullptr, " + "nullptr) != napi_ok) {" + @@ -509,7 +544,7 @@ public class GenNapiCppFile extends GeneratorBase { "\n\t\tnapi_throw_error(env, \"EINTYPE\", \"error wrap value\");" + "\n\t\treturn result;" + "\n\t};" + - "\n\treturn thisVar;") + "\n\treturn thisVar;\n") ); /** @@ -857,6 +892,8 @@ public class GenNapiCppFile extends GeneratorBase { NAPI_CLASS_NAME, className); classMethodStr = classMethodStr.replace( NAPI_CLASS_METHOD_NAME, funcItem.getName()); + + classMethodStr = genNapiFuncConvert(funcItem, classMethodStr); classMethodContent += classMethodStr; String classMethodPropertyStr = NAPI_CLASS_METHOD_PROPERTY_DECLARE.replace( @@ -1030,6 +1067,140 @@ public class GenNapiCppFile extends GeneratorBase { System.out.println("genFuncList : " + resContent); }; + private String genCppStructContent(StructObj so) { + String structName = so.getName(); + structName = !structName.isEmpty() ? structName : so.getAlias(); + + String templateStr = !so.getTemplateList().isEmpty() ? + NAPI_TEMPLATE_TOKEN + NAPI_BLANK_SPACE + NAPI_LEFT_ANGLE_BRACKET : ""; + for (String teStr : so.getTemplateList()) { + 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 paList = so.getMemberList(); + String resContent = ""; + 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() ? 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 += NAPI_EQUAL + initVList.get(0) + NAPI_SEMICOLON; + } else { + resContent += NAPI_SEMICOLON; + } + } + + List funcList = so.getFuncList(); + for (FuncObj funcItem : funcList) { + String retValue = ts2CppKey(funcItem.getRetValue()).isEmpty() ? "" : + 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() ? + 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 += NAPI_RIGHT_PARENTHESES + NAPI_SEMICOLON; + } + + resContent = StringUtils.removeLastSpace(resContent); + resContent += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; + return resContent; + } + + private String genNapiFuncConvert(FuncObj funcItem, String structMethodStr) { + String funcName = funcItem.getName(); + funcName = funcName.isEmpty() ? funcItem.getAlias() : funcName; + funcName = StringUtils.unCapitalFirst(funcName); + + String funcGetParamStr = ""; + String funcCallStr = ""; + String funcRetStr = ""; + int i = 0; + for (ParamObj pa : funcItem.getParamList()) { + funcGetParamStr += genGetParam(pa, i); + i++; + } + funcCallStr += genFuncCall(funcItem); + funcRetStr += genFuncRet(funcItem.getRetValue()); + String paCheckStr = NAPI_PARAM_CHECK.replace(NAPI_PARAM_CNT, + Integer.toString(funcItem.getParamList().size())); + String tempStr = structMethodStr; + tempStr = tempStr.replace(NAPI_GET_ARGUMENTS_DECLARE, paCheckStr + funcGetParamStr); + tempStr = tempStr.replace(NAPI_CLASS_CALL_METHOD_DECLARE, funcCallStr); + tempStr = tempStr.replace(NAPI_CLASS_RETURN_VALUE_DECLARE, funcRetStr); + return tempStr; + } + + private String genNapiStructContent(StructObj so) { + String structName = so.getName(); + structName = !structName.isEmpty() ? structName : so.getAlias(); + List funcList = so.getFuncList(); + String structNameList = ""; + String structMethodContent = ""; + String structMethodProperty = ""; + for (FuncObj funcItem : funcList) { + String structMethodStr = NAPI_CLASS_METHOD_DECLARE.replace( + NAPI_CLASS_NAME, structName); + structMethodStr = structMethodStr.replace( + NAPI_CLASS_METHOD_NAME, funcItem.getName()); + + structMethodStr = genNapiFuncConvert(funcItem, structMethodStr); + structMethodContent += structMethodStr; + + String structMethodPropertyStr = NAPI_CLASS_METHOD_PROPERTY_DECLARE.replace( + NAPI_CLASS_METHOD_NAME, funcItem.getName()); + structMethodPropertyStr = structMethodPropertyStr.replace(NAPI_CLASS_NAME, structName); + structMethodProperty += structMethodPropertyStr; + } + + String classAttributeContent = ""; + String classAttributeProperty = ""; + List paList = so.getMemberList(); + for (ParamObj paItem : paList) { + String getAttributeContent = NAPI_CLASS_GET_ATTRIBUTE_DECLARE.replace( + NAPI_CLASS_ATTRIBUTE_NAME, paItem.getName()); + getAttributeContent = getAttributeContent.replace(NAPI_CLASS_NAME, structName); + String setAttributeContent = NAPI_CLASS_SET_ATTRIBUTE_DECLARE.replace( + NAPI_CLASS_ATTRIBUTE_NAME, paItem.getName()); + setAttributeContent = setAttributeContent.replace(NAPI_CLASS_NAME, structName); + classAttributeContent += getAttributeContent + setAttributeContent; + + String classAttributeStr = NAPI_CLASS_ATTRIBUTE_PROPERTY_DECLARE.replace( + NAPI_CLASS_ATTRIBUTE_NAME, paItem.getName()); + classAttributeStr = classAttributeStr.replace(NAPI_CLASS_NAME, structName); + classAttributeProperty += classAttributeStr; + } + + String classDeclare = NAPI_CLASS_CONSTRUCTURE.replace(NAPI_CLASS_NAME, structName); + classDeclare += NAPI_CLASS_DESTRUCTURE.replace(NAPI_CLASS_NAME, structName); + classDeclare += structMethodContent + classAttributeContent; + + String classPropertyStr = NAPI_CLASS_PROPERTY_DECLARE.replace(NAPI_CLASS_NAME, structName); + classPropertyStr = classPropertyStr.replace(NAPI_CLASS_METHOD_PROPERTY, structMethodProperty); + classPropertyStr = classPropertyStr.replace(NAPI_CLASS_ATTRIBUTE_PROPERTY, classAttributeProperty); + classDeclare += classPropertyStr; + + String classInitStr = NAPI_CLASS_INIT.replace(NAPI_CLASS_NAME, structName); + classDeclare += classInitStr; + + String resContent = ""; + resContent = classDeclare; + return resContent; + } + /** * 生成输出内容 * @@ -1041,54 +1212,8 @@ public class GenNapiCppFile extends GeneratorBase { String resContent = ""; for (StructObj so : sol) { - String structName = so.getName(); - structName = !structName.isEmpty() ? structName : so.getAlias(); - - String templateStr = !so.getTemplateList().isEmpty() ? - NAPI_TEMPLATE_TOKEN + NAPI_BLANK_SPACE + NAPI_LEFT_ANGLE_BRACKET : ""; - for (String teStr : so.getTemplateList()) { - 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 paList = so.getMemberList(); - 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() ? 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 += NAPI_EQUAL + initVList.get(0) + NAPI_SEMICOLON; - } else { - resContent += NAPI_SEMICOLON; - } - } - - List funcList = so.getFuncList(); - for (FuncObj funcItem : funcList) { - String retValue = ts2CppKey(funcItem.getRetValue()).isEmpty() ? "" : - 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() ? - 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 += NAPI_RIGHT_PARENTHESES + NAPI_SEMICOLON; - } - - resContent = StringUtils.removeLastSpace(resContent); - resContent += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; + resContent += genCppStructContent(so); + resContent += genNapiStructContent(so); } this.structContent = resContent; }; @@ -1174,6 +1299,69 @@ public class GenNapiCppFile extends GeneratorBase { return resContent; }; + private String genCppValContent(ParamObj po) { + String paName = po.getName(); + String paType = ts2CppKey(po.getType()).isEmpty() ? NAPI_AUTO_TOKEN : ts2CppKey(po.getType()); + String paValue = po.getStrValue(0); + List paList = po.getPaList(); + String resContent = ""; + if (paList.isEmpty()) { + 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 += NAPI_SEMICOLON + NAPI_NEW_LINE; + } else if (paList.get(0).getPaList().isEmpty()) { + String valType = StringUtils.isAllDigits(paList.get(0).getStrValue(0)) ? + 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 += 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 += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; + } else if (!(paList.get(0).getPaList().isEmpty())) { + resContent = genVarArrayList(resContent, paName, paList); + } + return resContent; + }; + + private String genNapiValContent(ParamObj po) { + String paramAttributeContent = ""; + String paramAttributeProperty = ""; + String structName = "GNAPI"; + { + String getAttributeContent = NAPI_VAL_GET_DECLARE.replace( + NAPI_CLASS_ATTRIBUTE_NAME, po.getName()); + getAttributeContent = getAttributeContent.replace(NAPI_CLASS_NAME, structName); + String setAttributeContent = NAPI_VAL_SET_DECLARE.replace( + NAPI_CLASS_ATTRIBUTE_NAME, po.getName()); + setAttributeContent = setAttributeContent.replace(NAPI_CLASS_NAME, structName); + paramAttributeContent += getAttributeContent + setAttributeContent; + + String classAttributeStr = NAPI_CLASS_ATTRIBUTE_PROPERTY_DECLARE.replace( + NAPI_CLASS_ATTRIBUTE_NAME, po.getName()); + classAttributeStr = classAttributeStr.replace(NAPI_CLASS_NAME, structName); + paramAttributeProperty += classAttributeStr; + } + + String classDeclare = ""; + classDeclare += paramAttributeContent; + + String classPropertyStr = NAPI_CLASS_PROPERTY_DECLARE.replace(NAPI_CLASS_NAME, structName); + classPropertyStr = classPropertyStr.replace(NAPI_CLASS_ATTRIBUTE_PROPERTY, paramAttributeProperty); + classPropertyStr = classPropertyStr.replace(NAPI_CLASS_METHOD_PROPERTY, ""); + classDeclare += classPropertyStr; + + return classDeclare; + } + /** * 生成输出内容 * @@ -1185,35 +1373,8 @@ public class GenNapiCppFile extends GeneratorBase { String resContent = ""; for (ParamObj po : pol) { - String paName = po.getName(); - 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 += NAPI_NEW_LINE + NAPI_EXTENDS_TOKEN + NAPI_BLANK_SPACE + NAPI_CONST_TOKEN + - NAPI_BLANK_SPACE + paType + NAPI_BLANK_SPACE + paName + NAPI_EQUAL + paValue; - - resContent += NAPI_SEMICOLON + NAPI_NEW_LINE; - } else if (paList.get(0).getPaList().isEmpty()) { - String valType = StringUtils.isAllDigits(paList.get(0).getStrValue(0)) ? - 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 += 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 += NAPI_NEW_LINE + NAPI_RIGHT_BRACE + NAPI_SEMICOLON + NAPI_NEW_LINE; - } else if (!(paList.get(0).getPaList().isEmpty())) { - resContent = genVarArrayList(resContent, paName, paList); - } - + resContent += genCppValContent(po); + resContent += genNapiValContent(po); } this.constContent = resContent; System.out.println("genVarList : " + resContent); diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java index 741327a374c4b3c386c686324b520c105df833f1..5c66433a2958b395f8908fcdfd40adeb30835010 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenAkiCppFileTest.java @@ -968,6 +968,14 @@ class GenAkiCppFileTest { "\tchar* name;\n" + "\tbool age;\n" + "\tint add(bool a, bool b);\n" + + "};\n" + + "\n" + + "JSBIND_CLASS(TestStruct)\n" + + "{\n" + + "\tJSBIND_METHOD(add, \"add\");\n" + + "\tJSBIND_PMETHOD(add, \"addPromise\");\n" + + "\tJSBIND_PROPERTY(name);\n" + + "\tJSBIND_PROPERTY(age);\n" + "};\n"; assertEquals(expect, structContent); } @@ -1009,6 +1017,14 @@ class GenAkiCppFileTest { "\tT name;\n" + "\tU age;\n" + "\tint add(T a, U b);\n" + + "};\n" + + "\n" + + "JSBIND_CLASS(TestStruct)\n" + + "{\n" + + "\tJSBIND_METHOD(add, \"add\");\n" + + "\tJSBIND_PMETHOD(add, \"addPromise\");\n" + + "\tJSBIND_PROPERTY(name);\n" + + "\tJSBIND_PROPERTY(age);\n" + "};\n"; assertEquals(expect, structContent); } @@ -1049,6 +1065,14 @@ class GenAkiCppFileTest { "\tauto name;\n" + "\tauto age;\n" + "\tadd(auto a, auto b);\n" + + "};\n" + + "\n" + + "JSBIND_CLASS(TestStruct)\n" + + "{\n" + + "\tJSBIND_METHOD(add, \"add\");\n" + + "\tJSBIND_PMETHOD(add, \"addPromise\");\n" + + "\tJSBIND_PROPERTY(name);\n" + + "\tJSBIND_PROPERTY(age);\n" + "};\n"; assertEquals(expect, structContent); } @@ -1071,6 +1095,10 @@ class GenAkiCppFileTest { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); String expect = "\nstruct TestStruct {\n" + + "};\n" + + "\n" + + "JSBIND_CLASS(TestStruct)\n" + + "{\n" + "};\n"; assertEquals(expect, structContent); } 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 3da10be07edd16ef100fac6e36452d54e9a859b6..437a8f9932b28556458209960819afd0fc17507f 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest.java @@ -76,8 +76,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" + @@ -107,16 +107,76 @@ class GenNapiCppFileTest { "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + "\t// 获取参数\n" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype0;\n" + + "\tif (napi_typeof(env, args[0], &valuetype0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_number) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第valuetype0个参数必须是数字\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tint value0 = 0;\n" + + "\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_int32(env, args[0], &value0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype1;\n" + + "\tif (napi_typeof(env, args[1], &valuetype1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_number) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第valuetype1个参数必须是数字\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tint value1 = 0;\n" + + "\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_int32(env, args[1], &value1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + "\t// 调用原始类方法\n" + - "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\tadd(NAPI_PARAM_EXPRESSION);\n" + "\t// 创建返回参数\n" + - "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\tnapi_value valueRet0;\n" + + "\tif (napi_create_int32(env, args[0], &valueRet0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\treturn valueRet0;\n" + + "\n" + "\t}\n" + "\treturn result;\n" + "};\n" + "\n" + - "napi_value Getname(napi_env env, napi_callback_info info)\n" + + "napi_value GetnameTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -134,7 +194,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setname(napi_env env, napi_callback_info info)\n" + + "napi_value SetnameTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -154,7 +214,7 @@ class GenNapiCppFileTest { "\treturn nullptr;\n" + "};\n" + "\n" + - "napi_value Getage(napi_env env, napi_callback_info info)\n" + + "napi_value GetageTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -172,7 +232,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setage(napi_env env, napi_callback_info info)\n" + + "napi_value SetageTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -193,9 +253,9 @@ class GenNapiCppFileTest { "};\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" + + "\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" + @@ -251,16 +311,25 @@ class GenNapiCppFileTest { "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + "\t// 获取参数\n" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tsize_t argc = 0;\n" + + "\tnapi_value args[0] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 0) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要0个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + "\t// 调用原始类方法\n" + - "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\tconstructor(NAPI_PARAM_EXPRESSION);\n" + "\t// 创建返回参数\n" + - "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t\n" + "\t}\n" + "\treturn result;\n" + "};\n" + "\n" + - "napi_value Getname(napi_env env, napi_callback_info info)\n" + + "napi_value GetnameTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -278,7 +347,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setname(napi_env env, napi_callback_info info)\n" + + "napi_value SetnameTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -298,7 +367,7 @@ class GenNapiCppFileTest { "\treturn nullptr;\n" + "};\n" + "\n" + - "napi_value Getage(napi_env env, napi_callback_info info)\n" + + "napi_value GetageTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -316,7 +385,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setage(napi_env env, napi_callback_info info)\n" + + "napi_value SetageTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -336,7 +405,7 @@ class GenNapiCppFileTest { "\treturn nullptr;\n" + "};\n" + "\n" + - "napi_value Getno(napi_env env, napi_callback_info info)\n" + + "napi_value GetnoTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -354,7 +423,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setno(napi_env env, napi_callback_info info)\n" + + "napi_value SetnoTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -374,7 +443,7 @@ class GenNapiCppFileTest { "\treturn nullptr;\n" + "};\n" + "\n" + - "napi_value Getaddr(napi_env env, napi_callback_info info)\n" + + "napi_value GetaddrTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -392,7 +461,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setaddr(napi_env env, napi_callback_info info)\n" + + "napi_value SetaddrTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -413,17 +482,16 @@ class GenNapiCppFileTest { "};\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" + + "\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" + + "\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,8 +511,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" + @@ -474,11 +542,20 @@ class GenNapiCppFileTest { "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + "\t// 获取参数\n" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tsize_t argc = 0;\n" + + "\tnapi_value args[0] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 0) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要0个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + "\t// 调用原始类方法\n" + - "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\tconstructor(NAPI_PARAM_EXPRESSION);\n" + "\t// 创建返回参数\n" + - "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t\n" + "\t}\n" + "\treturn result;\n" + "};\n" + @@ -497,16 +574,25 @@ class GenNapiCppFileTest { "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + "\t// 获取参数\n" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tsize_t argc = 0;\n" + + "\tnapi_value args[0] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 0) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要0个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + "\t// 调用原始类方法\n" + - "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\tdisplayName(NAPI_PARAM_EXPRESSION);\n" + "\t// 创建返回参数\n" + - "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t\n" + "\t}\n" + "\treturn result;\n" + "};\n" + "\n" + - "napi_value GetempCode(napi_env env, napi_callback_info info)\n" + + "napi_value GetempCodeEmployee(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -524,7 +610,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value SetempCode(napi_env env, napi_callback_info info)\n" + + "napi_value SetempCodeEmployee(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -544,7 +630,7 @@ class GenNapiCppFileTest { "\treturn nullptr;\n" + "};\n" + "\n" + - "napi_value GetcurrentUser(napi_env env, napi_callback_info info)\n" + + "napi_value GetcurrentUserEmployee(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -562,7 +648,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value SetcurrentUser(napi_env env, napi_callback_info info)\n" + + "napi_value SetcurrentUserEmployee(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -582,7 +668,7 @@ class GenNapiCppFileTest { "\treturn nullptr;\n" + "};\n" + "\n" + - "napi_value Getpi(napi_env env, napi_callback_info info)\n" + + "napi_value GetpiEmployee(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -600,7 +686,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setpi(napi_env env, napi_callback_info info)\n" + + "napi_value SetpiEmployee(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -621,12 +707,13 @@ class GenNapiCppFileTest { "};\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, " + + "\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" + + "\t{\"pi\", nullptr, nullptr, GetpiEmployee, SetpiEmployee, nullptr, napi_default, nullptr},\n" + "};\n" + "\n" + "napi_value EmployeeIns = nullptr;\n" + @@ -647,8 +734,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" + @@ -678,17 +765,26 @@ class GenNapiCppFileTest { "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + "\t// 获取参数\n" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tsize_t argc = 0;\n" + + "\tnapi_value args[0] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 0) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要0个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + "\t// 调用原始类方法\n" + - "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\tfoo(NAPI_PARAM_EXPRESSION);\n" + "\t// 创建返回参数\n" + - "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t\n" + "\t}\n" + "\treturn result;\n" + "};\n" + "\n" + "napi_property_descriptor myClassProps[] = {\n" + - "\t{foo, nullptr, foomyClass, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{\"foo\", nullptr, foomyClass, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + "};\n" + "\n" + "napi_value myClassIns = nullptr;\n" + @@ -711,8 +807,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" + @@ -742,16 +838,25 @@ class GenNapiCppFileTest { "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + "\t// 获取参数\n" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + "\t// 调用原始类方法\n" + - "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\tsetKeyValue(NAPI_PARAM_EXPRESSION);\n" + "\t// 创建返回参数\n" + - "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t\n" + "\t}\n" + "\treturn result;\n" + "};\n" + "\n" + - "napi_value Getkey(napi_env env, napi_callback_info info)\n" + + "napi_value GetkeyKeyValuePair(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -769,7 +874,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setkey(napi_env env, napi_callback_info info)\n" + + "napi_value SetkeyKeyValuePair(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -789,7 +894,7 @@ class GenNapiCppFileTest { "\treturn nullptr;\n" + "};\n" + "\n" + - "napi_value Getval(napi_env env, napi_callback_info info)\n" + + "napi_value GetvalKeyValuePair(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -807,7 +912,7 @@ class GenNapiCppFileTest { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setval(napi_env env, napi_callback_info info)\n" + + "napi_value SetvalKeyValuePair(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -828,15 +933,18 @@ class GenNapiCppFileTest { "};\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" + + "\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" + + "\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" + @@ -854,7 +962,7 @@ class GenNapiCppFileTest { "\tnapi_get_undefined(env, &undefineVar);\n" + "\n" + "\tif (napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr) == napi_ok && " + - "thisVar != nullptr) {\n" + + "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" + @@ -884,23 +992,32 @@ class GenNapiCppFileTest { "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + "\t// 获取参数\n" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + "\t// 调用原始类方法\n" + - "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\tprocess(NAPI_PARAM_EXPRESSION);\n" + "\t// 创建返回参数\n" + - "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t\n" + "\t}\n" + "\treturn result;\n" + "};\n" + "\n" + "napi_property_descriptor kvProcessorProps[] = {\n" + - "\t{process, nullptr, processkvProcessor, nullptr, nullptr, nullptr, napi_default, nullptr},\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" + + "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" + @@ -916,8 +1033,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" + @@ -947,17 +1064,26 @@ class GenNapiCppFileTest { "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + "\t// 获取参数\n" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + "\t// 调用原始类方法\n" + - "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\tprocess(NAPI_PARAM_EXPRESSION);\n" + "\t// 创建返回参数\n" + - "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\t\n" + "\t}\n" + "\treturn result;\n" + "};\n" + "\n" + "napi_property_descriptor ShapeProps[] = {\n" + - "\t{process, nullptr, processShape, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{\"process\", nullptr, processShape, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + "};\n" + "\n" + "napi_value ShapeIns = nullptr;\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 3c2c3d5ac8a48a5077d15e22eee1e6bf0591030c..58d43d382474993942ec1b308cf59bff3fcf6790 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest2.java @@ -47,8 +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" + @@ -78,11 +78,71 @@ class GenNapiCppFileTest2 { "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + "\t// 获取参数\n" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype0;\n" + + "\tif (napi_typeof(env, args[0], &valuetype0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_number) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第valuetype0个参数必须是数字\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tint value0 = 0;\n" + + "\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_int32(env, args[0], &value0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype1;\n" + + "\tif (napi_typeof(env, args[1], &valuetype1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_number) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第valuetype1个参数必须是数字\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tint value1 = 0;\n" + + "\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_int32(env, args[1], &value1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + "\t// 调用原始类方法\n" + - "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\tadd(NAPI_PARAM_EXPRESSION);\n" + "\t// 创建返回参数\n" + - "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\tnapi_value valueRet0;\n" + + "\tif (napi_create_int32(env, args[0], &valueRet0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\treturn valueRet0;\n" + + "\n" + "\t}\n" + "\treturn result;\n" + "};\n" + @@ -101,16 +161,54 @@ class GenNapiCppFileTest2 { "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + "\t\n" + "\t// 获取参数\n" + - "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tsize_t argc = 1;\n" + + "\tnapi_value args[1] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 1) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要1个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype0;\n" + + "\tif (napi_typeof(env, args[0], &valuetype0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_number) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第valuetype0个参数必须是数字\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tint value0 = 0;\n" + + "\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_int32(env, args[0], &value0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + "\t// 调用原始类方法\n" + - "\tNAPI_CLASS_CALL_METHOD_DECLARE\n" + + "\tdelete(NAPI_PARAM_EXPRESSION);\n" + "\t// 创建返回参数\n" + - "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\tnapi_value valueRet0;\n" + + "\tif (napi_create_int32(env, args[0], &valueRet0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\treturn valueRet0;\n" + + "\n" + "\t}\n" + "\treturn result;\n" + "};\n" + "\n" + - "napi_value Getname(napi_env env, napi_callback_info info)\n" + + "napi_value GetnameTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -128,7 +226,7 @@ class GenNapiCppFileTest2 { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setname(napi_env env, napi_callback_info info)\n" + + "napi_value SetnameTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -148,7 +246,7 @@ class GenNapiCppFileTest2 { "\treturn nullptr;\n" + "};\n" + "\n" + - "napi_value Getage(napi_env env, napi_callback_info info)\n" + + "napi_value GetageTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_value jsthis;\n" + @@ -166,7 +264,7 @@ class GenNapiCppFileTest2 { "\treturn result;\n" + "};\n" + "\n" + - "napi_value Setage(napi_env env, napi_callback_info info)\n" + + "napi_value SetageTestClass(napi_env env, napi_callback_info info)\n" + "{\n" + "\tnapi_value result = nullptr;\n" + "\tnapi_get_undefined(env, &result);\n" + @@ -187,15 +285,15 @@ class GenNapiCppFileTest2 { "};\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" + + "\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, " + + "\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" + @@ -203,6 +301,352 @@ class GenNapiCppFileTest2 { "\t\treturn nullptr;\n" + "\t}"; + private String testGenContent = "\nextends const int TestParam = 100;\n" + + "\n" + + "napi_value GetTestParamGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value SetTestParamGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tTestParam = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor GNAPIProps[] = {\n" + + "\t{\"TestParam\", nullptr, nullptr, GetTestParamGNAPI, SetTestParamGNAPI, " + + "nullptr, napi_default, nullptr},\n" + + "};\n"; + + private String testGenFile = "\nextends const int TestParam = 100;\n" + + "\n" + + "napi_value GetTestParamGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value SetTestParamGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tTestParam = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor GNAPIProps[] = {\n" + + "\t{\"TestParam\", nullptr, nullptr, GetTestParamGNAPI, SetTestParamGNAPI, " + + "nullptr, napi_default, nullptr},\n" + + "};\n"; + + private String testGenVarList = "\nextends const int TestParam = 100;\n" + + "\n" + + "napi_value GetTestParamGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value SetTestParamGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tTestParam = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor GNAPIProps[] = {\n" + + "\t{\"TestParam\", nullptr, nullptr, GetTestParamGNAPI, SetTestParamGNAPI, nullptr, " + + "napi_default, nullptr},\n" + + "};\n"; + + private String testGenStructList = "\nstruct TestStruct {\n" + + "\tstd::string name;\n" + + "\tint age;\n" + + "\tint add(int a, int b);\n" + + "};\n" + + "\n" + + "napi_value ConstructorTestStruct(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\tTestStruct *reference = new TestStruct();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorTestStruct, 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 DestructorTestStruct(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value addTestStruct(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" + + "\tTestStruct *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\n" + + "\tadd(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\n" + + "\t\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value GetnameTestStruct(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" + + "\tTestStruct *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 SetnameTestStruct(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" + + "\tTestStruct *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 GetageTestStruct(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" + + "\tTestStruct *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 SetageTestStruct(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" + + "\tTestStruct *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 TestStructProps[] = {\n" + + "\t{\"add\", nullptr, addTestStruct, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{\"name\", nullptr, nullptr, GetnameTestStruct, SetnameTestStruct, nullptr, napi_default, nullptr},\n" + + "\t{\"age\", nullptr, nullptr, GetageTestStruct, SetageTestStruct, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value TestStructIns = nullptr;\n" + + "\tif (napi_define_class(env, \"TestStruct\", NAPI_AUTO_LENGTH, ConstructorTestStruct, " + + "nullptr, sizeof(TestStructProps) / sizeof(TestStructProps[0]), " + + "TestStructProps, &TestStructIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"TestStruct\", TestStructIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String testGenFuncList = "\nvoid TestFunc(std::string name, int age);\n" + + "napi_value testFuncNapi(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// 获取参数\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype0;\n" + + "\tif (napi_typeof(env, args[0], &valuetype0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_string) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_string error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第0个参数必须是字符串\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tchar* value0[MAX_BUFFER_SIZE];\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_string_utf8(env, args[0], &value0, bufferSize, &realSize) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_string_utf8 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + + "\tnapi_valuetype valuetype1;\n" + + "\tif (napi_typeof(env, args[1], &valuetype1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error value type\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\tif (type != napi_number) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_number error\");\n" + + "\t\tapi_throw_type_error(env, \"ERR_INVALID_ARG_TYPE\", \"第valuetype1个参数必须是数字\");\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\tint value1 = 0;\n" + + "\n" + + "\tsize_t bufferSize = MAX_BUFFER_SIZE;\n" + + "\tsize_t realSize = 0;\n" + + "\tif (napi_get_value_int32(env, args[1], &value1) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\n" + + "\tTestFunc(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\n\t\n" + + "\treturn result;\n" + + "};\n" + + "napi_property_descriptor funcDesc[] = {\n" + + "\t{ \"testFunc\", nullptr, testFuncNapi, nullptr, nullptr, nullptr, napi_default, nullptr },\n" + + "};\n" + + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + @Test void getInterfaceContent() { } @@ -265,199 +709,6 @@ class GenNapiCppFileTest2 { } } - @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 std::string 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(); @@ -475,7 +726,7 @@ class GenNapiCppFileTest2 { if (gb instanceof GenNapiCppFile gdf) { String varContent = gdf.getConstContent(); System.out.println("genVar: " + varContent); - String expect = "\nextends const int TestParam = 100;\n"; + String expect = testGenContent; assertEquals(expect, varContent); } } @@ -510,7 +761,7 @@ class GenNapiCppFileTest2 { if (gb instanceof GenNapiCppFile gdf) { String varContent = gdf.getConstContent(); System.out.println("genVar: " + varContent); - String expect = "\nextends const int TestParam = 100;\n"; + String expect = testGenFile; assertEquals(expect, varContent); } } @@ -556,7 +807,7 @@ class GenNapiCppFileTest2 { "\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_create_int32(env, values[i], &value);\n" + "\t\tnapi_set_named_property(env, enum_obj, members[i], value);\n" + "\t}\n" + "\n" + @@ -629,7 +880,7 @@ class GenNapiCppFileTest2 { if (gb instanceof GenNapiCppFile gdf) { String funcContent = gdf.getFuncContent(); System.out.println("genFunc: " + funcContent); - String expect = "\nvoid TestFunc(std::string name, int age);"; + String expect = testGenFuncList; assertEquals(expect, funcContent); } } @@ -665,11 +916,7 @@ class GenNapiCppFileTest2 { if (gb instanceof GenNapiCppFile gdf) { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); - String expect = "\nstruct TestStruct {\n" + - "\tstd::string name;\n" + - "\tint age;\n" + - "\tint add(int a, int b);\n" + - "};\n"; + String expect = testGenStructList; assertEquals(expect, structContent); } } @@ -722,7 +969,7 @@ class GenNapiCppFileTest2 { if (gb instanceof GenNapiCppFile gdf) { String varContent = gdf.getConstContent(); System.out.println("genVar: " + varContent); - String expect = "\nextends const int TestParam = 100;\n"; + String expect = testGenVarList; assertEquals(expect, varContent); } } diff --git a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java index 6f694974006c5ed93afe5a2b608862a93c4793de..337e7a4b532f0ef42b74c942045a638c986d5ea2 100644 --- a/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java +++ b/src/intellij_plugin/ohosgen/src/test/java/gen/GenNapiCppFileTest3.java @@ -40,7 +40,7 @@ class GenNapiCppFileTest3 { "\tnapi_value jsthis;\n" + "\tnapi_status status;\n" + "\tnapi_get_undefined(env, &result);\n" + - "\t// 获取参数\t\n" + + "\t// 获取参数\n" + "\tsize_t argc = 2;\n" + "\tnapi_value args[2] = {nullptr};\n" + "\tnapi_value this_arg;\n" + @@ -49,8 +49,7 @@ class GenNapiCppFileTest3 { "\tif (argc < 2) {\n" + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + "\t\treturn nullptr;\n" + - "\t};\n" + - "\n" + + "\t};\n\n" + "\tnapi_valuetype valuetype0;\n" + "\tif (napi_typeof(env, args[0], &valuetype0) != napi_ok) {\n" + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + @@ -70,8 +69,7 @@ class GenNapiCppFileTest3 { "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_string_utf8 error\");\n" + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + "\t\treturn result;\n" + - "\t};\n" + - "\n" + + "\t};\n\n" + "\tnapi_valuetype valuetype1;\n" + "\tif (napi_typeof(env, args[1], &valuetype1) != napi_ok) {\n" + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + @@ -94,9 +92,9 @@ class GenNapiCppFileTest3 { "\t\treturn result;\n" + "\t};\n" + "\n" + - "\t// 调用原始类方法\t\n" + + "\t// 调用原始类方法\n" + "\tTestFunc(NAPI_PARAM_EXPRESSION);\n" + - "\t// 创建返回参数\t\n" + + "\t// 创建返回参数\n\t\n" + "\treturn result;\n" + "};\n" + "napi_property_descriptor funcDesc[] = {\n" + @@ -111,7 +109,7 @@ class GenNapiCppFileTest3 { "\tnapi_value jsthis;\n" + "\tnapi_status status;\n" + "\tnapi_get_undefined(env, &result);\n" + - "\t// 获取参数\t\n" + + "\t// 获取参数\n" + "\tsize_t argc = 2;\n" + "\tnapi_value args[2] = {nullptr};\n" + "\tnapi_value this_arg;\n" + @@ -120,8 +118,7 @@ class GenNapiCppFileTest3 { "\tif (argc < 2) {\n" + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + "\t\treturn nullptr;\n" + - "\t};\n" + - "\n" + + "\t};\n\n" + "\tnapi_valuetype valuetype0;\n" + "\tif (napi_typeof(env, args[0], &valuetype0) != napi_ok) {\n" + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + @@ -141,8 +138,7 @@ class GenNapiCppFileTest3 { "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_string_utf8 error\");\n" + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + "\t\treturn result;\n" + - "\t};\n" + - "\n" + + "\t};\n\n" + "\tnapi_valuetype valuetype1;\n" + "\tif (napi_typeof(env, args[1], &valuetype1) != napi_ok) {\n" + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + @@ -165,17 +161,16 @@ class GenNapiCppFileTest3 { "\t\treturn result;\n" + "\t};\n" + "\n" + - "\t// 调用原始类方法\t\n" + + "\t// 调用原始类方法\n" + "\tToCapital(NAPI_PARAM_EXPRESSION);\n" + - "\t// 创建返回参数\t\n" + - "\tnapi_value valueRet0;\n" + + "\t// 创建返回参数\n\tnapi_value valueRet0;" + "\n" + "\tif (napi_create_string_utf8(env, args[0], realSize, &valueRet0) != napi_ok) {\n" + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_string_utf8 error\");\n" + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + "\t\treturn result;\n" + "\t};\n" + - "\treturn valueRet0\n" + + "\treturn valueRet0;\n\n" + "\treturn result;\n" + "};\n" + "napi_property_descriptor funcDesc[] = {\n" + @@ -190,7 +185,7 @@ class GenNapiCppFileTest3 { "\tnapi_value jsthis;\n" + "\tnapi_status status;\n" + "\tnapi_get_undefined(env, &result);\n" + - "\t// 获取参数\t\n" + + "\t// 获取参数\n" + "\tsize_t argc = 2;\n" + "\tnapi_value args[2] = {nullptr};\n" + "\tnapi_value this_arg;\n" + @@ -199,8 +194,7 @@ class GenNapiCppFileTest3 { "\tif (argc < 2) {\n" + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + "\t\treturn nullptr;\n" + - "\t};\n" + - "\n" + + "\t};\n\n" + "\tnapi_valuetype valuetype0;\n" + "\tif (napi_typeof(env, args[0], &valuetype0) != napi_ok) {\n" + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + @@ -220,8 +214,7 @@ class GenNapiCppFileTest3 { "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_get_value_string_utf8 error\");\n" + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + "\t\treturn result;\n" + - "\t};\n" + - "\n" + + "\t};\n\n" + "\tnapi_valuetype valuetype1;\n" + "\tif (napi_typeof(env, args[1], &valuetype1) != napi_ok) {\n" + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_typeof error\");\n" + @@ -244,17 +237,16 @@ class GenNapiCppFileTest3 { "\t\treturn result;\n" + "\t};\n" + "\n" + - "\t// 调用原始类方法\t\n" + + "\t// 调用原始类方法\n" + "\tNemw(NAPI_PARAM_EXPRESSION);\n" + - "\t// 创建返回参数\t\n" + - "\tnapi_value valueRet0;\n" + + "\t// 创建返回参数\n\tnapi_value valueRet0;" + "\n" + "\tif (napi_create_string_utf8(env, args[0], realSize, &valueRet0) != napi_ok) {\n" + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_string_utf8 error\");\n" + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + "\t\treturn result;\n" + "\t};\n" + - "\treturn valueRet0\n" + + "\treturn valueRet0;\n\n" + "\treturn result;\n" + "};\n" + "napi_property_descriptor funcDesc[] = {\n" + @@ -269,7 +261,7 @@ class GenNapiCppFileTest3 { "\tnapi_value jsthis;\n" + "\tnapi_status status;\n" + "\tnapi_get_undefined(env, &result);\n" + - "\t// 获取参数\t\n" + + "\t// 获取参数\n" + "\tsize_t argc = 2;\n" + "\tnapi_value args[2] = {nullptr};\n" + "\tnapi_value this_arg;\n" + @@ -280,17 +272,16 @@ class GenNapiCppFileTest3 { "\t\treturn nullptr;\n" + "\t};\n" + "\n" + - "\t// 调用原始类方法\t\n" + + "\t// 调用原始类方法\n" + "\tNemw(NAPI_PARAM_EXPRESSION);\n" + - "\t// 创建返回参数\t\n" + - "\tnapi_value valueRet0;\n" + + "\t// 创建返回参数\n\tnapi_value valueRet0;" + "\n" + "\tif (napi_create_string_utf8(env, args[0], realSize, &valueRet0) != napi_ok) {\n" + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_string_utf8 error\");\n" + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + "\t\treturn result;\n" + "\t};\n" + - "\treturn valueRet0\n" + + "\treturn valueRet0;\n\n" + "\treturn result;\n" + "};\n" + "napi_property_descriptor funcDesc[] = {\n" + @@ -305,7 +296,7 @@ class GenNapiCppFileTest3 { "\tnapi_value jsthis;\n" + "\tnapi_status status;\n" + "\tnapi_get_undefined(env, &result);\n" + - "\t// 获取参数\t\n" + + "\t// 获取参数\n" + "\tsize_t argc = 2;\n" + "\tnapi_value args[2] = {nullptr};\n" + "\tnapi_value this_arg;\n" + @@ -316,9 +307,9 @@ class GenNapiCppFileTest3 { "\t\treturn nullptr;\n" + "\t};\n" + "\n" + - "\t// 调用原始类方法\t\n" + + "\t// 调用原始类方法\n" + "\tNemw(NAPI_PARAM_EXPRESSION);\n" + - "\t// 创建返回参数\t\n" + + "\t// 创建返回参数\n\t\n" + "\treturn result;\n" + "};\n" + "napi_property_descriptor funcDesc[] = {\n" + @@ -333,7 +324,7 @@ class GenNapiCppFileTest3 { "\tnapi_value jsthis;\n" + "\tnapi_status status;\n" + "\tnapi_get_undefined(env, &result);\n" + - "\t// 获取参数\t\n" + + "\t// 获取参数\n" + "\tsize_t argc = 1;\n" + "\tnapi_value args[1] = {nullptr};\n" + "\tnapi_value this_arg;\n" + @@ -344,9 +335,9 @@ class GenNapiCppFileTest3 { "\t\treturn nullptr;\n" + "\t};\n" + "\n" + - "\t// 调用原始类方法\t\n" + + "\t// 调用原始类方法\n" + "\tgetArray(NAPI_PARAM_EXPRESSION);\n" + - "\t// 创建返回参数\t\n" + + "\t// 创建返回参数\n\t\n" + "\treturn result;\n" + "};\n" + "napi_property_descriptor funcDesc[] = {\n" + @@ -361,7 +352,7 @@ class GenNapiCppFileTest3 { "\tnapi_value jsthis;\n" + "\tnapi_status status;\n" + "\tnapi_get_undefined(env, &result);\n" + - "\t// 获取参数\t\n" + + "\t// 获取参数\n" + "\tsize_t argc = 2;\n" + "\tnapi_value args[2] = {nullptr};\n" + "\tnapi_value this_arg;\n" + @@ -372,9 +363,9 @@ class GenNapiCppFileTest3 { "\t\treturn nullptr;\n" + "\t};\n" + "\n" + - "\t// 调用原始类方法\t\n" + + "\t// 调用原始类方法\n" + "\tdisplayType(NAPI_PARAM_EXPRESSION);\n" + - "\t// 创建返回参数\t\n" + + "\t// 创建返回参数\n\t\n" + "\treturn result;\n" + "};\n" + "napi_property_descriptor funcDesc[] = {\n" + @@ -382,6 +373,805 @@ class GenNapiCppFileTest3 { "};\n" + "napi_define_properties(env, exports, sizeof(funcDesc) / sizeof(funcDesc[0]), funcDesc);"; + private String testStructContent1 = "\nstruct TestStruct {\n" + + "\tstd::string name;\n" + + "\tbool age;\n" + + "\tint add(bool a, bool b);\n" + + "};\n" + + "\n" + + "napi_value ConstructorTestStruct(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\tTestStruct *reference = new TestStruct();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorTestStruct, 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 DestructorTestStruct(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value addTestStruct(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" + + "\tTestStruct *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\n" + + "\tadd(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\n" + + "\tnapi_value valueRet0;\n" + + "\tif (napi_create_int32(env, args[0], &valueRet0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\treturn valueRet0;\n\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value GetnameTestStruct(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" + + "\tTestStruct *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 SetnameTestStruct(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" + + "\tTestStruct *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 GetageTestStruct(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" + + "\tTestStruct *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 SetageTestStruct(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" + + "\tTestStruct *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 TestStructProps[] = {\n" + + "\t{\"add\", nullptr, addTestStruct, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{\"name\", nullptr, nullptr, GetnameTestStruct, SetnameTestStruct, nullptr, napi_default, nullptr},\n" + + "\t{\"age\", nullptr, nullptr, GetageTestStruct, SetageTestStruct, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value TestStructIns = nullptr;\n" + + "\tif (napi_define_class(env, \"TestStruct\", NAPI_AUTO_LENGTH, ConstructorTestStruct, nullptr, " + + "sizeof(TestStructProps) / sizeof(TestStructProps[0]), TestStructProps, &TestStructIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"TestStruct\", TestStructIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String testStructContent2 = "\ntemplate struct TestStruct {\n" + + "\tT name;\n" + + "\tU age;\n" + + "\tint add(T a, U b);\n" + + "};\n" + + "\n" + + "napi_value ConstructorTestStruct(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\tTestStruct *reference = new TestStruct();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorTestStruct, 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 DestructorTestStruct(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value addTestStruct(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" + + "\tTestStruct *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法" + + "\n" + + "\tadd(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\n" + + "\tnapi_value valueRet0;\n" + + "\tif (napi_create_int32(env, args[0], &valueRet0) != napi_ok) {\n" + + "\t\tOH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, \"Log\", \"napi_create_int32 error\");\n" + + "\t\tnapi_throw_error(env, \"EINTYPE\", \"error get value\");\n" + + "\t\treturn result;\n" + + "\t};\n" + + "\treturn valueRet0;\n\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value GetnameTestStruct(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" + + "\tTestStruct *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 SetnameTestStruct(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" + + "\tTestStruct *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 GetageTestStruct(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" + + "\tTestStruct *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 SetageTestStruct(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" + + "\tTestStruct *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 TestStructProps[] = {\n" + + "\t{\"add\", nullptr, addTestStruct, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{\"name\", nullptr, nullptr, GetnameTestStruct, SetnameTestStruct, nullptr, napi_default, nullptr},\n" + + "\t{\"age\", nullptr, nullptr, GetageTestStruct, SetageTestStruct, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value TestStructIns = nullptr;\n" + + "\tif (napi_define_class(env, \"TestStruct\", NAPI_AUTO_LENGTH, ConstructorTestStruct, nullptr, " + + "sizeof(TestStructProps) / sizeof(TestStructProps[0]), TestStructProps, &TestStructIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"TestStruct\", TestStructIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String testStructContent3 = "\nstruct TestStruct {\n" + + "\tauto name;\n" + + "\tauto age;\n" + + "\tadd(auto a, auto b);\n" + + "};\n" + + "\n" + + "napi_value ConstructorTestStruct(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\tTestStruct *reference = new TestStruct();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorTestStruct, 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 DestructorTestStruct(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_value addTestStruct(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" + + "\tTestStruct *obj;\n" + + "\tstatus = napi_unwrap(env, jsthis, (void **)&obj);\n" + + "\t\n" + + "\t// 获取参数\n" + + "\tsize_t argc = 2;\n" + + "\tnapi_value args[2] = {nullptr};\n" + + "\tnapi_value this_arg;\n" + + "\tnapi_get_cb_info(env, info, &argc, args, &this_arg, nullptr);\n" + + "\t// 参数校验\n" + + "\tif (argc < 2) {\n" + + "\t\tnapi_throw_error(env, \"EINVAL\", \"需要2个参数\");\n" + + "\t\treturn nullptr;\n" + + "\t};\n" + + "\n" + + "\t// 调用原始类方法\n" + + "\tadd(NAPI_PARAM_EXPRESSION);\n" + + "\t// 创建返回参数\n" + + "\t\n" + + "\t}\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value GetnameTestStruct(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" + + "\tTestStruct *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 SetnameTestStruct(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" + + "\tTestStruct *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 GetageTestStruct(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" + + "\tTestStruct *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 SetageTestStruct(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" + + "\tTestStruct *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 TestStructProps[] = {\n" + + "\t{\"add\", nullptr, addTestStruct, nullptr, nullptr, nullptr, napi_default, nullptr},\n" + + "\t{\"name\", nullptr, nullptr, GetnameTestStruct, SetnameTestStruct, nullptr, napi_default, nullptr},\n" + + "\t{\"age\", nullptr, nullptr, GetageTestStruct, SetageTestStruct, nullptr, napi_default, nullptr},\n" + + "};\n" + + "\n" + + "napi_value TestStructIns = nullptr;\n" + + "\tif (napi_define_class(env, \"TestStruct\", NAPI_AUTO_LENGTH, ConstructorTestStruct, nullptr, " + + "sizeof(TestStructProps) / sizeof(TestStructProps[0]), TestStructProps, &TestStructIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"TestStruct\", TestStructIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String testStructContent4 = "\nstruct TestStruct {\n" + + "};\n" + + "\n" + + "napi_value ConstructorTestStruct(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\tTestStruct *reference = new TestStruct();\n" + + "\t\tif (napi_wrap(env, thisVar,\n" + + "\t\t\treinterpret_cast(reference), DestructorTestStruct, 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 DestructorTestStruct(napi_env env, void *nativeObject, void *finalize)\n" + + "{\n" + + "\tdelete reinterpret_cast(nativeObject);\n" + + "};\n" + + "\n" + + "napi_property_descriptor TestStructProps[] = {\n" + + "};\n" + + "\n" + + "napi_value TestStructIns = nullptr;\n" + + "\tif (napi_define_class(env, \"TestStruct\", NAPI_AUTO_LENGTH, ConstructorTestStruct, nullptr, " + + "sizeof(TestStructProps) / sizeof(TestStructProps[0]), TestStructProps, &TestStructIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}\n" + + "\tif (napi_set_named_property(env, exports, \"TestStruct\", TestStructIns) != napi_ok) {\n" + + "\t\treturn nullptr;\n" + + "\t}"; + + private String testValContent1 = "\nextends const auto employeeName = \"John\";\n" + + "\n" + + "napi_value GetemployeeNameGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value SetemployeeNameGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\temployeeName = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor GNAPIProps[] = {\n" + + "\t{\"employeeName\", nullptr, nullptr, GetemployeeNameGNAPI, SetemployeeNameGNAPI, nullptr, " + + "napi_default, nullptr},\n" + + "};\n"; + + private String testValContent2 = "\nextends const std::string employeeName = \"John\";\n" + + "\n" + + "napi_value GetemployeeNameGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value SetemployeeNameGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\temployeeName = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor GNAPIProps[] = {\n" + + "\t{\"employeeName\", nullptr, nullptr, GetemployeeNameGNAPI, SetemployeeNameGNAPI, nullptr, " + + "napi_default, nullptr},\n" + + "};\n"; + + private String testValContent3 = "\nextends const int num1 = 1;\n" + + "\n" + + "napi_value Getnum1GNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value Setnum1GNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tnum1 = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor GNAPIProps[] = {\n" + + "\t{\"num1\", nullptr, nullptr, Getnum1GNAPI, Setnum1GNAPI, nullptr, napi_default, nullptr},\n" + + "};\n"; + + private String testValContent4 = "\nextends const std::map ROUTES = {\n" + + "\t{'/dashboard', false},\n" + + "\t{'/deals', true},\n" + + "};\n" + + "\n" + + "napi_value GetROUTESGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value SetROUTESGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tROUTES = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor GNAPIProps[] = {\n" + + "\t{\"ROUTES\", nullptr, nullptr, GetROUTESGNAPI, SetROUTESGNAPI, nullptr, napi_default, nullptr},\n" + + "};\n"; + + private String testGenConstContent = "\nextends const int TestParam = 100;\n" + + "\n" + + "napi_value GetTestParamGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\n" + + "\t// 创建返回对象\n" + + "\tNAPI_CLASS_RETURN_VALUE_DECLARE\n" + + "\n" + + "\treturn result;\n" + + "};\n" + + "\n" + + "napi_value SetTestParamGNAPI(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, nullptr, nullptr) != napi_ok) {\n" + + "\t\treturn result;\n" + + "\t}\n" + + "\t// 获取参数\n" + + "\tNAPI_GET_ARGUMENTS_DECLARE\n" + + "\tTestParam = msg;\n" + + "\treturn nullptr;\n" + + "};\n" + + "\n" + + "napi_property_descriptor GNAPIProps[] = {\n" + + "\t{\"TestParam\", nullptr, nullptr, GetTestParamGNAPI, " + + "SetTestParamGNAPI, nullptr, napi_default, nullptr},\n" + + "};\n"; + @Test void getInterfaceContent() { } @@ -610,11 +1400,7 @@ class GenNapiCppFileTest3 { if (gb instanceof GenNapiCppFile gdf) { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); - String expect = "\nstruct TestStruct {\n" + - "\tstd::string name;\n" + - "\tbool age;\n" + - "\tint add(bool a, bool b);\n" + - "};\n"; + String expect = testStructContent1; assertEquals(expect, structContent); } } @@ -651,11 +1437,7 @@ class GenNapiCppFileTest3 { if (gb instanceof GenNapiCppFile gdf) { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); - String expect = "\ntemplate struct TestStruct {\n" + - "\tT name;\n" + - "\tU age;\n" + - "\tint add(T a, U b);\n" + - "};\n"; + String expect = testStructContent2; assertEquals(expect, structContent); } } @@ -691,11 +1473,7 @@ class GenNapiCppFileTest3 { if (gb instanceof GenNapiCppFile gdf) { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); - String expect = "\nstruct TestStruct {\n" + - "\tauto name;\n" + - "\tauto age;\n" + - "\tadd(auto a, auto b);\n" + - "};\n"; + String expect = testStructContent3; assertEquals(expect, structContent); } } @@ -716,8 +1494,7 @@ class GenNapiCppFileTest3 { if (gb instanceof GenNapiCppFile gdf) { String structContent = gdf.getStructContent(); System.out.println("genStruct: " + structContent); - String expect = "\nstruct TestStruct {\n" + - "};\n"; + String expect = testStructContent4; assertEquals(expect, structContent); } } @@ -796,7 +1573,7 @@ class GenNapiCppFileTest3 { if (gb instanceof GenNapiCppFile gdf) { String constContent = gdf.getConstContent(); System.out.println("getVar: " + constContent); - String expect = "\nextends const auto employeeName = \"John\";\n"; + String expect = testValContent1; assertEquals(expect, constContent); } } @@ -818,7 +1595,7 @@ class GenNapiCppFileTest3 { if (gb instanceof GenNapiCppFile gdf) { String constContent = gdf.getConstContent(); System.out.println("getVar: " + constContent); - String expect = "\nextends const std::string employeeName = \"John\";\n"; + String expect = testValContent2; assertEquals(expect, constContent); } } @@ -840,7 +1617,7 @@ class GenNapiCppFileTest3 { if (gb instanceof GenNapiCppFile gdf) { String constContent = gdf.getConstContent(); System.out.println("getVar: " + constContent); - String expect = "\nextends const int num1 = 1;\n"; + String expect = testValContent3; assertEquals(expect, constContent); } } @@ -869,10 +1646,7 @@ class GenNapiCppFileTest3 { if (gb instanceof GenNapiCppFile gdf) { String constContent = gdf.getConstContent(); System.out.println("getVar: " + constContent); - String expect = "\nextends const std::map ROUTES = {\n" + - "\t{'/dashboard', false},\n" + - "\t{'/deals', true},\n" + - "};\n"; + String expect = testValContent6; assertEquals(expect, constContent); } } @@ -968,7 +1734,7 @@ class GenNapiCppFileTest3 { if (gb instanceof GenNapiCppFile gdf) { String varContent = gdf.getConstContent(); System.out.println("genVar: " + varContent); - String expect = "\nextends const int TestParam = 100;\n"; + String expect = testGenConstContent; assertEquals(expect, varContent); } }