fix: resolve the c++ code check warnnings

Signed-off-by: zhangzhicheng007 <zhangzhicheng@kaihong.com>
This commit is contained in:
zhangzhicheng007 2023-01-05 11:05:51 +08:00
parent 46227fa28a
commit 7bad7107de
10 changed files with 176 additions and 103 deletions

View File

@ -27,6 +27,8 @@ let xNapiToolH = `\
#include <vector>
#include <cmath>
using DataPtr = struct DumyData*;
struct AsyncFunc {
napi_env env_;
napi_ref funcRef_;
@ -35,12 +37,22 @@ struct AsyncFunc {
class XNapiTool {
public:
static const int ZERO = 0;
static const int ONE = 1;
static const int TWO = 2;
static const int THREE = 3;
static const int FOUE = 4;
static const int FIVE = 5;
static const int SIX = 6;
static const int SEVEN = 7;
static const int EIGHT = 8;
static const int NINE = 9;
void RegistAsyncFunc(std::string name, napi_value func);
void UnregistAsyncFunc(std::string name);
static void CallAsyncFunc(AsyncFunc *pAsyncFuncs, napi_value ret);
using CallbackFunction = void (*)(XNapiTool *pxt, void *data);
using RELEASE_INSTANCE = void (*)(void *p);
using CallbackFunction = void (*)(XNapiTool *pxt, DataPtr data);
using RELEASE_INSTANCE = void (*)(DataPtr p);
static napi_value UndefinedValue(napi_env env);
const uint32_t DEFAULT_ARG_COUNT = 8;
napi_value UndefinedValue();
@ -98,7 +110,7 @@ public:
napi_value SyncCallBack(napi_value func, size_t argc, napi_value *args);
napi_value StartAsync(CallbackFunction pe, void *data, CallbackFunction pc, napi_value func = nullptr);
napi_value StartAsync(CallbackFunction pe, DataPtr data, CallbackFunction pc, napi_value func = nullptr);
void FinishAsync(size_t argc, napi_value *args);
bool IsFailed()
@ -118,13 +130,13 @@ public:
// create code related class
public:
static void WrapFinalize(napi_env env, void *data, void *hint);
static void WrapFinalize(napi_env env, XNapiTool *data, DataPtr hint);
void ReleaseInstance();
napi_value WrapInstance(void *instance, RELEASE_INSTANCE ri);
void *UnWarpInstance();
napi_value WrapInstance(DataPtr instance, RELEASE_INSTANCE ri);
DataPtr UnWarpInstance();
void SetAsyncInstance(void *p);
void *GetAsyncInstance();
void SetAsyncInstance(DataPtr p);
void* GetAsyncInstance();
private:
napi_env env_;
@ -145,9 +157,9 @@ private:
bool CheckValueType(napi_value value, napi_valuetype type);
// asynchronous call related code
static void AsyncExecute(napi_env env, void *p);
static void AsyncExecute(napi_env env, XNapiTool *p);
void AsyncExecuteFunction();
static void AsyncComplete(napi_env env, napi_status status, void *p);
static void AsyncComplete(napi_env env, napi_status status, XNapiTool *p);
void AsyncCompleteFunction();
napi_ref callbackFunc_;
napi_ref asyncThisVar_;
@ -155,7 +167,7 @@ private:
bool asyncNeedRelease_;
CallbackFunction executeFunction_;
CallbackFunction completeFunction_;
void *valueData_;
DataPtr valueData_;
napi_deferred deferred_;
enum class AsyncMode {
NONE,
@ -166,9 +178,9 @@ private:
private:
napi_ref wrapper_;
void *pInstance_;
DataPtr pInstance_;
RELEASE_INSTANCE releaseInstance_;
void *asyncInstance_;
DataPtr asyncInstance_;
};
#endif
@ -820,9 +832,9 @@ void XNapiTool::AsyncExecuteFunction()
executeFunction_(this, valueData_);
}
}
void XNapiTool::AsyncExecute(napi_env env, void *p)
void XNapiTool::AsyncExecute(napi_env env, XNapiTool *p)
{
XNapiTool *pxt = (XNapiTool *)p;
XNapiTool *pxt = p;
pxt->AsyncExecuteFunction();
}
void XNapiTool::AsyncCompleteFunction()
@ -831,14 +843,14 @@ void XNapiTool::AsyncCompleteFunction()
completeFunction_(this, valueData_);
}
}
void XNapiTool::AsyncComplete(napi_env env, napi_status status, void *p)
void XNapiTool::AsyncComplete(napi_env env, napi_status status, XNapiTool *p)
{
XNapiTool *pxt = (XNapiTool *)p;
XNapiTool *pxt = p;
pxt->AsyncCompleteFunction();
delete pxt;
}
napi_value XNapiTool::StartAsync(CallbackFunction pe, void *data, CallbackFunction pc, napi_value func)
napi_value XNapiTool::StartAsync(CallbackFunction pe, DataPtr data, CallbackFunction pc, napi_value func)
{
napi_value result;
napi_status result_status;
@ -865,8 +877,9 @@ napi_value XNapiTool::StartAsync(CallbackFunction pe, void *data, CallbackFuncti
napi_value resourceName = nullptr;
result_status = napi_create_string_utf8(env_, "tool_utility", NAPI_AUTO_LENGTH, &resourceName);
CC_ASSERT(result_status == napi_ok);
result_status = napi_create_async_work(env_, nullptr, resourceName, XNapiTool::AsyncExecute,
XNapiTool::AsyncComplete, this, &work_);
result_status = napi_create_async_work(env_, nullptr, resourceName,
(napi_async_execute_callback)XNapiTool::AsyncExecute,
(napi_async_complete_callback)XNapiTool::AsyncComplete, this, &work_);
CC_ASSERT(result_status == napi_ok);
result_status = napi_queue_async_work(env_, work_);
CC_ASSERT(result_status == napi_ok);
@ -995,9 +1008,9 @@ void XNapiTool::DefineClass(const char *className, napi_callback constructorFunc
CC_ASSERT(result_status == napi_ok);
}
void XNapiTool::WrapFinalize(napi_env env, void *data, void *hint)
void XNapiTool::WrapFinalize(napi_env env, XNapiTool *data, DataPtr hint)
{
XNapiTool *pxt = (XNapiTool *)data;
XNapiTool *pxt = data;
pxt->ReleaseInstance();
delete pxt;
}
@ -1009,16 +1022,16 @@ void XNapiTool::ReleaseInstance()
}
}
napi_value XNapiTool::WrapInstance(void *instance, RELEASE_INSTANCE ri)
napi_value XNapiTool::WrapInstance(DataPtr instance, RELEASE_INSTANCE ri)
{
pInstance_ = instance;
releaseInstance_ = ri;
napi_status result_status = napi_wrap(env_, thisVar_, this, WrapFinalize, nullptr, &wrapper_);
napi_status result_status = napi_wrap(env_, thisVar_, this, (napi_finalize)WrapFinalize, nullptr, &wrapper_);
CC_ASSERT(result_status == napi_ok);
return thisVar_;
}
void *XNapiTool::UnWarpInstance()
DataPtr XNapiTool::UnWarpInstance()
{
XNapiTool *p;
napi_status result_status = napi_unwrap(env_, thisVar_, (void **)&p);
@ -1026,12 +1039,12 @@ void *XNapiTool::UnWarpInstance()
return p->pInstance_;
}
void XNapiTool::SetAsyncInstance(void *p)
void XNapiTool::SetAsyncInstance(DataPtr p)
{
asyncInstance_ = p;
}
void *XNapiTool::GetAsyncInstance()
void* XNapiTool::GetAsyncInstance()
{
return asyncInstance_;
}

View File

@ -66,12 +66,12 @@ let moduleCppTmplete = `\
dest = pxt->SwapC2JsBool(napi_v); \\
}
#define C_DELETE((p)) \\
#define C_DELETE(p) \\
if ((p)) { \\
delete (p); \\
}
__attribute__((unused)) static napi_value number_c_to_js(XNapiTool *pxt, const std::type_info &n, void *num)
__attribute__((unused)) static napi_value number_c_to_js(XNapiTool *pxt, const std::type_info &n, DataPtr num)
{
if (n == typeid(int32_t))
return pxt->SwapC2JsInt32(*(int32_t *)num);
@ -83,8 +83,8 @@ __attribute__((unused)) static napi_value number_c_to_js(XNapiTool *pxt, const s
return pxt->SwapC2JsDouble(*(double_t *)num);
return nullptr;
}
#define NUMBER_C_2_JS(pxt, (n)) \\
number_c_to_js(pxt, typeid((n)), &(n))
#define NUMBER_C_2_JS(pxt, n) \\
number_c_to_js(pxt, typeid((n)), reinterpret_cast<DataPtr>(&(n)))
[body_replace]
static napi_value init(napi_env env, napi_value exports)
{

View File

@ -28,12 +28,13 @@ public:
{
XNapiTool *pxt = new XNapiTool(env, info);
[className] *p = new [className]();
napi_value thisvar = pxt->WrapInstance(p, release);
napi_value thisvar = pxt->WrapInstance(reinterpret_cast<DataPtr>(p), release);
return thisvar;
}
static void release(void *p)
static void release(DataPtr p)
{
[className] *p2 = ([className] *)p;
void *dataPtr = p;
[className] *p2 = static_cast<[className] *>(dataPtr);
delete p2;
}
[static_funcs]
@ -61,7 +62,8 @@ function generateVariable(name, type, variable, className) {
static napi_value getvalue_%s(napi_env env, napi_callback_info info)
{
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
%s *p = (%s *)pxt->UnWarpInstance();
void *instPtr = pxt->UnWarpInstance();
%s *p = static_cast<%s *>(instPtr);
napi_value result = nullptr;
`.format(name, className, className) + cToJs("p->" + name, type, "result") + `
delete pxt;
@ -70,8 +72,9 @@ function generateVariable(name, type, variable, className) {
static napi_value setvalue_%s(napi_env env, napi_callback_info info)
{
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
%s *p = (%s *)pxt->UnWarpInstance();
`.format(name, className, className) + jsToC("p->" + name, "pxt->GetArgv(0)", type) + `
void *instPtr = pxt->UnWarpInstance();
%s *p = static_cast<%s *>(instPtr);
`.format(name, className, className) + jsToC("p->" + name, "pxt->GetArgv(XNapiTool::ZERO)", type) + `
return nullptr;
}
`

View File

@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { replaceAll, getPrefix } = require("../tools/tool");
const { replaceAll, getPrefix, getConstNum } = require("../tools/tool");
const { paramGenerate } = require("./param_generate");
const { returnGenerate } = require("./return_generate");
@ -24,16 +24,18 @@ struct [funcName]_value_struct {[valueIn]
uint32_t outErrCode = 0;[valueOut]
};
[static_define]void [funcName]_execute(XNapiTool *pxt, void *data)
[static_define]void [funcName]_execute(XNapiTool *pxt, DataPtr data)
{
[funcName]_value_struct *vio = ([funcName]_value_struct *)data;
void *data_ptr = data;
[funcName]_value_struct *vio = static_cast<[funcName]_value_struct *>(data_ptr);
[checkout_async_instance]
[callFunc]
}
[static_define]void [funcName]_complete(XNapiTool *pxt, void *data)
[static_define]void [funcName]_complete(XNapiTool *pxt, DataPtr data)
{
[funcName]_value_struct *vio = ([funcName]_value_struct *)data;
void *data_ptr = data;
[funcName]_value_struct *vio = static_cast<[funcName]_value_struct *>(data_ptr);
napi_value result = nullptr;
[valuePackage]
napi_value errCodeResult = nullptr;
@ -41,8 +43,8 @@ struct [funcName]_value_struct {[valueIn]
napiErrCode = NUMBER_C_2_JS(pxt, vio->outErrCode);
pxt->SetValueProperty(errCodeResult, "code", napiErrCode);
{
napi_value args[2] = {errCodeResult, result};
pxt->FinishAsync(2, args);
napi_value args[XNapiTool::TWO] = {errCodeResult, result};
pxt->FinishAsync(XNapiTool::TWO, args);
}
[optionalParamDestory]
delete vio;
@ -140,21 +142,22 @@ function generateFunctionAsync(func, data, className) {
middleFunc = replaceAll(middleFunc, "[optionalCallbackInit]", optionalCallback + "\n ") // 可选callback参数初始化
}
middleFunc = replaceAll(middleFunc, "[start_async]", `
napi_value result = pxt->StartAsync(%s_execute, vio, %s_complete,
napi_value result = pxt->StartAsync(%s_execute, reinterpret_cast<DataPtr>(vio), %s_complete,
pxt->GetArgc() == %s? pxt->GetArgv(%d) : nullptr);`
.format(func.name, func.name, parseInt(param.callback.offset) + 1, param.callback.offset)) // 注册异步调用
.format(func.name, func.name, getConstNum(parseInt(param.callback.offset) + 1),
getConstNum(param.callback.offset))) // 注册异步调用
let callFunc = "%s%s(%s);".format(className == null ? "" : "pInstance->", func.name, param.valueFill)
middleFunc = replaceAll(middleFunc, "[callFunc]", callFunc) // 执行
middleFunc = replaceAll(middleFunc, "[valuePackage]", param.valuePackage) // 输出参数打包
middleFunc = replaceAll(middleFunc, "[optionalParamDestory]", param.optionalParamDestory) // 可选参数内存释放
let prefixArr = getPrefix(data, func.isStatic)
let prefixArr = getPrefix(data, func)
let implH = ""
let implCpp = ""
if (!func.isParentMember) {
// 只有类/接口自己的成员方法需要在.h.cpp中生成父类/父接口不需要
implH = "\n%s%s%sbool %s(%s);".format(
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.valueDefine)
implH = "\n%s%s%sbool %s(%s)%s;".format(
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.valueDefine, prefixArr[3])
implCpp = cppTemplate.format(className == null ? "" : className + "::", func.name, param.valueDefine)
}
return [middleFunc, implH, implCpp]

View File

@ -74,7 +74,8 @@ function generateFunctionDirect(func, data, className) {
else {
middleFunc = middleFunc.replaceAll("[static_define]", "static ")
middleFunc = middleFunc.replaceAll("[unwarp_instance]",
"%s *pInstance = (%s *)pxt->UnWarpInstance();".format(className, className))
`void *instPtr = pxt->UnWarpInstance();
%s *pInstance = static_cast<%s *>(instPtr);`.format(className, className))
}
// 定义输入,定义输出,解析,填充到函数内,输出参数打包,impl参数定义,可选参数内存释放
let param = { valueIn: "", valueOut: "", valueCheckout: "", valueFill: "",
@ -105,13 +106,13 @@ function generateFunctionDirect(func, data, className) {
} else {
middleFunc = replaceAll(middleFunc, "[optionalParamDestory]", "\n " + param.optionalParamDestory) // 可选参数内存释放
}
let prefixArr = getPrefix(data, func.isStatic)
let prefixArr = getPrefix(data, func)
let implH = ""
let implCpp = ""
if (!func.isParentMember) {
// 只有类/接口自己的成员方法需要在.h.cpp中生成父类/父接口不需要
implH = "\n%s%s%sbool %s(%s);".format(
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.valueDefine)
implH = "\n%s%s%sbool %s(%s)%s;".format(
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.valueDefine, prefixArr[3])
implCpp = cppTemplate.format(className == null ? "" : className + "::", func.name, param.valueDefine)
}
return [middleFunc, implH, implCpp]

View File

@ -35,7 +35,7 @@ struct [funcName]_value_struct {
}
[unwarp_instance]
struct [funcName]_value_struct *vio = new [funcName]_value_struct();
pxt->SwapJs2CUtf8(pxt->GetArgv(0), vio->eventName);
pxt->SwapJs2CUtf8(pxt->GetArgv(XNapiTool::ZERO), vio->eventName);
[handleRegist]
[instance][funcName](vio->eventName);
napi_value result = pxt->UndefinedValue();
@ -113,12 +113,13 @@ function gennerateOnOffContext(codeContext, func, data, className, param) {
else {
codeContext.middleFunc = codeContext.middleFunc.replaceAll("[static_define]", "static ")
codeContext.middleFunc = codeContext.middleFunc.replaceAll("[unwarp_instance]",
"%s *pInstance = (%s *)pxt->UnWarpInstance();".format(className, className))
`void *instPtr = pxt->UnWarpInstance();
%s *pInstance = static_cast<%s *>(instPtr);`.format(className, className))
}
let instancePtr = "%s".format(className == null ? "" : "pInstance->")
codeContext.middleFunc = replaceAll(codeContext.middleFunc, "[instance]", instancePtr) //执行
let registLine = func.name == 'on' ? "pxt->RegistAsyncFunc(vio->eventName, pxt->GetArgv(1));"
let registLine = func.name == 'on' ? "pxt->RegistAsyncFunc(vio->eventName, pxt->GetArgv(XNapiTool::ONE));"
: "pxt->UnregistAsyncFunc(vio->eventName);"
codeContext.middleFunc = replaceAll(codeContext.middleFunc, "[handleRegist]", registLine) //注册/去注册event

View File

@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { replaceAll, getPrefix } = require("../tools/tool");
const { replaceAll, getPrefix, getConstNum } = require("../tools/tool");
const { paramGenerate } = require("./param_generate");
const { returnGenerate } = require("./return_generate");
@ -40,7 +40,7 @@ struct [funcName]_value_struct {[valueIn][valueOut]
[valuePackage]
{
napi_value args[1] = {result};
pxt->SyncCallBack(pxt->GetArgv([callback_param_offset]), 1, args);
pxt->SyncCallBack(pxt->GetArgv([callback_param_offset]), XNapiTool::ONE, args);
}
}
result = pxt->UndefinedValue();
@ -78,7 +78,8 @@ function getOptionalCallbackInit(param) {
return ""
}
let cType = param.valueOut.substr(0, param.valueOut.indexOf("*"))
return "if (pxt->GetArgc() > %s) {\n vio->out = new %s;\n }".format(param.callback.offset, cType)
return "if (pxt->GetArgc() > %s) {\n vio->out = new %s;\n }"
.format(getConstNum(param.callback.offset), cType)
}
function generateFunctionSync(func, data, className) {
@ -90,7 +91,8 @@ function generateFunctionSync(func, data, className) {
else {
middleFunc = middleFunc.replaceAll("[static_define]", "static ")
middleFunc = middleFunc.replaceAll("[unwarp_instance]",
"%s *pInstance = (%s *)pxt->UnWarpInstance();".format(className, className))
`void *instPtr = pxt->UnWarpInstance();
%s *pInstance = static_cast<%s *>(instPtr);`.format(className, className))
}
// 定义输入,定义输出,解析,填充到函数内,输出参数打包,impl参数定义,可选参数内存释放
let param = { valueIn: "", valueOut: "", valueCheckout: "", valueFill: "",
@ -121,13 +123,13 @@ function generateFunctionSync(func, data, className) {
middleFunc = replaceAll(middleFunc, "[optionalParamDestory]", param.optionalParamDestory) // 可选参数内存释放
middleFunc = middleFunc.replaceAll("[callback_param_offset]", param.callback.offset); // 呼叫回调
let prefixArr = getPrefix(data, func.isStatic)
let prefixArr = getPrefix(data, func)
let implH = ""
let implCpp = ""
if (!func.isParentMember) {
// 只有类/接口自己的成员方法需要在.h.cpp中生成父类/父接口不需要
implH = "\n%s%s%sbool %s(%s);".format(
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.valueDefine)
implH = "\n%s%s%sbool %s(%s)%s;".format(
prefixArr[0], prefixArr[1], prefixArr[2], func.name, param.valueDefine, prefixArr[3])
implCpp = cppTemplate.format(className == null ? "" : className + "::", func.name, param.valueDefine)
}
return [middleFunc, implH, implCpp]

View File

@ -21,7 +21,7 @@ const { jsToC } = require("./param_generate");
const { cToJs } = require("./return_generate");
const re = require("../tools/re");
const { NapiLog } = require("../tools/NapiLog");
const { addUniqFunc2List, addUniqObj2List } = require("../tools/tool");
const { addUniqFunc2List, addUniqObj2List, setOverrideFunc } = require("../tools/tool");
let middleBodyTmplete = `
class [className]_middle {
@ -30,12 +30,13 @@ public:
{
XNapiTool *pxt = new XNapiTool(env, info);
[className] *p = new [className]();
napi_value thisvar = pxt->WrapInstance(p, release);
napi_value thisvar = pxt->WrapInstance(reinterpret_cast<DataPtr>(p), release);
return thisvar;
}
static void release(void *p)
static void release(DataPtr p)
{
[className] *p2 = ([className] *)p;
void *dataPtr = p;
[className] *p2 = static_cast<[className] *>(dataPtr);
delete p2;
}
[static_funcs]
@ -93,7 +94,8 @@ function generateVariable(value, variable, className) {
static napi_value getvalue_%s(napi_env env, napi_callback_info info)
{
XNapiTool *pxt = std::make_unique<XNapiTool>(env, info).release();
%s *p = (%s *)pxt->UnWarpInstance();
void *instPtr = pxt->UnWarpInstance();
%s *p = static_cast<%s *>(instPtr);
napi_value result = nullptr;
`.format(name, className, className) + cToJs("p->" + name, type, "result") + `
delete pxt;
@ -102,8 +104,9 @@ function generateVariable(value, variable, className) {
static napi_value setvalue_%s(napi_env env, napi_callback_info info)
{
std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, info);
%s *p = (%s *)pxt->UnWarpInstance();
`.format(name, className, className) + jsToC("p->" + name, "pxt->GetArgv(0)", type) + `
void *instPtr = pxt->UnWarpInstance();
%s *p = static_cast<%s *>(instPtr);
`.format(name, className, className) + jsToC("p->" + name, "pxt->GetArgv(XNapiTool::ZERO)", type) + `
return nullptr;
}`
}
@ -192,13 +195,31 @@ function getAllPropties(interfaceBody, properties, isParentClass) {
}
for (let i in interfaceBody.function) {
interfaceBody.function[i].isParentMember = isParentClass
addUniqFunc2List(interfaceBody.function[i], properties.functions)
if(!addUniqFunc2List(interfaceBody.function[i], properties.functions)) {
if (isParentClass) {
// 没添加到列表说明子类方法properties.functions重写了父类方法interfaceBody.function[i]
// 找到该子类方法并将其设置为override (生成的重写函数如果没有override关键字会触发门禁告警)
setOverrideFunc(interfaceBody.function[i], properties.functions)
}
}
}
if (!isParentClass && interfaceBody.parentNameList && interfaceBody.parentNameList.length > 0) {
getAllPropties(interfaceBody.parentBody, properties, true)
}
}
function addVirtualKeywords(data, implH, name) {
if (data.childList && data.childList.length > 0) {
// 如果该类是其它类的父类,增加虚析构函数使其具备泛型特征 (基类必须有虚函数才能正确使用dynamic_cast和typeinfo等方法)
// 如果该类自己也有父类虚析构函数需要加上override关键字(否则C++门禁会有告警)
let ovrrideStr = (data.parentList && data.parentList.length > 0) ? " override" : "";
// 如果虚析构函数已经有override关键字就不需要再加virtual关键字了(否则C++门禁会有告警)
let virtualStr = (data.parentList && data.parentList.length > 0) ? "" : "virtual ";
implH += "\n %s~%s()%s {};".format(virtualStr, name, ovrrideStr);
}
return implH;
}
function connectResult(data, inNamespace, name) {
let implH = ""
let implCpp = ""
@ -244,10 +265,7 @@ function connectResult(data, inNamespace, name) {
implCpp += tmp[2]
middleInit += `\n funcList["%s"] = %s%s_middle::%s_middle;`.format(func.name, inNamespace, name, func.name)
}
if (data.childList && data.childList.length > 0) {
// 如果是父类,增加虚析构函数使其具备泛型特征 (基类必须有虚函数才能正确使用dynamic_cast和typeinfo等方法)
implH += "\n virtual ~%s() {};".format(name)
}
implH = addVirtualKeywords(data, implH, name);
return [middleFunc, implH, implCpp, middleInit]
}
@ -256,4 +274,6 @@ module.exports = {
connectResult,
generateVariable,
mapTypeString
}
}

View File

@ -17,7 +17,7 @@ const { InterfaceList, getArrayType, getArrayTypeTwo, NumberIncrease,
EnumList, getUnionType } = require("../tools/common");
const re = require("../tools/re");
const { NapiLog } = require("../tools/NapiLog");
const { print } = require("../tools/tool");
const { getConstNum } = require("../tools/tool");
class LenIncrease { }
LenIncrease.LEN_TO = 1;
@ -126,10 +126,10 @@ function getArrayTypeTemplete(type) {
arrayType = "any"
} else if (arrayType == "[key:string]:string" || arrayType == "Map<string,string>") {
arrayType = "std::map<std::string, std::string>"
} else if (arrayType.substring(0, arrayType.length-1) == "[key:string]:NUMBER_TYPE_" ||
arrayType.substring(0, arrayType.length-1) == "Map<string,NUMBER_TYPE_>") {
} else if (arrayType.substring(0, arrayType.lastIndexOf("_") + 1) == "[key:string]:NUMBER_TYPE_" ||
arrayType.substring(0, arrayType.lastIndexOf("_") + 1) == "Map<string,NUMBER_TYPE_>") {
let len = arrayType.length
let num = arrayType.substring(len-1, len)
let num = arrayType.substring(arrayType.lastIndexOf("_") + 1, len)
arrayType = "std::map<std::string, NUMBER_TYPE_%s>".format(num)
} else if (arrayType == "[key:string]:boolean" || arrayType == "Map<string,boolean>") {
arrayType = "std::map<std::string, bool>"
@ -161,7 +161,7 @@ function arrTemplete(dest, napiVn, type) {
uint32_t len[replace_lt] = pxt->GetArrayLength(%s);
for (uint32_t i[replace_lt] = 0; i[replace_lt] < len[replace_lt]; i[replace_lt]++) {
%s tt[replace_lt];
napi_value mapPara = pxt->GetArrayElement(pxt->GetArgv(0), i[replace_lt]);
napi_value mapPara = pxt->GetArrayElement(pxt->GetArgv(XNapiTool::ZERO), i[replace_lt]);
uint32_t len2 = pxt->GetMapLength(mapPara);
for (uint32_t i2 = 0; i2 < len2; i2++) {
std::string ttName;
@ -289,7 +289,7 @@ function paramGenerateArray(p, funcValue, param) {
}
param.valueIn += funcValue.optional ? "\n std::vector<%s>* in%d = nullptr;".format(arrayType, p)
: "\n std::vector<%s> in%d;".format(arrayType, p)
param.valueCheckout += jsToC(inParamName, "pxt->GetArgv(%d)".format(p), type)
param.valueCheckout += jsToC(inParamName, "pxt->GetArgv(%d)".format(getConstNum(p)), type)
param.valueFill += "%svio->in%d".format(param.valueFill.length > 0 ? ", " : "", p)
param.valueDefine += "%sstd::vector<%s>%s%s".format(param.valueDefine.length > 0 ? ", "
: "", arrayType, modifiers, name)
@ -311,10 +311,10 @@ function paramGenerateArray(p, funcValue, param) {
}
param.valueIn += funcValue.optional ? "\n std::vector<%s>* in%d = nullptr;".format(arrayType, p)
: "\n std::vector<%s> in%d;".format(arrayType, p)
let arrValueCheckout = jsToC(inParamName, "pxt->GetArgv(%d)".format(p), type)
let arrValueCheckout = jsToC(inParamName, "pxt->GetArgv(%d)".format(getConstNum(p)), type)
if (funcValue.optional) {
arrValueCheckout = "if (pxt->GetArgc() > %s) {\n vio->in%d = new std::vector<%s>;\n"
.format(p, p, arrayType) + arrValueCheckout + " }\n"
.format(getConstNum(p), p, arrayType) + arrValueCheckout + " }\n"
param.optionalParamDestory += "C_DELETE(vio->in%d);\n ".format(p)
}
param.valueCheckout += arrValueCheckout
@ -329,7 +329,7 @@ function paramGenerateArray(p, funcValue, param) {
function paramGenerateAny(p, name, type, param) {
param.valueIn += `\n std::any in%d;
std::string in%d_type;`.format(p, p)
param.valueCheckout += jsToC("vio->in" + p, "pxt->GetArgv(%d)".format(p), type)
param.valueCheckout += jsToC("vio->in" + p, "pxt->GetArgv(%d)".format(getConstNum(p)), type)
param.valueFill += "%svio->in%d".format(param.valueFill.length > 0 ? ", " : "", p)
param.valueDefine += "%sstd::any &%s".format(param.valueDefine.length > 0 ? ", " : "", name)
}
@ -337,7 +337,7 @@ function paramGenerateAny(p, name, type, param) {
function paramGenerateAnyArray(p, name, type, param) {
param.valueIn += `\n std::any in%d;
std::string in%d_type;`.format(p, p)
param.valueCheckout += jsToC("vio->in" + p, "pxt->GetArgv(%d)".format(p), type)
param.valueCheckout += jsToC("vio->in" + p, "pxt->GetArgv(%d)".format(getConstNum(p)), type)
param.valueFill += "%svio->in%d".format(param.valueFill.length > 0 ? ", " : "", p)
param.valueDefine += "%sstd::any &%s".format(param.valueDefine.length > 0 ? ", " : "", name)
}
@ -417,8 +417,8 @@ function mapTempleteFunc(dest, napiVn, type) {
}
function anyTempleteFunc(dest) {
let anyTemplete = `%s_type = pxt->GetAnyType(pxt->GetArgv(0));
pxt->SetAnyValue(%s_type, pxt->GetArgv(0), %s);\n`
let anyTemplete = `%s_type = pxt->GetAnyType(pxt->GetArgv(XNapiTool::ZERO));
pxt->SetAnyValue(%s_type, pxt->GetArgv(XNapiTool::ZERO), %s);\n`
.format(dest, dest, dest)
return anyTemplete
@ -449,7 +449,7 @@ function mapInterface(mapTypeString, mapTemplete, napiVn, lt) {
if (interfaceValue[i].type == 'string') {
interfaceVarName += `std::string %dName = "%d";\n`.format(interfaceValue[i].name, interfaceValue[i].name)
interfaceFun +=
`pxt->%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(0),
`pxt->%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(XNapiTool::ZERO),
tt%d.c_str()), %sName.c_str()), tt%d.%s);\n`
.format("SwapJs2CUtf8", "GetMapElementValue",
lt, interfaceValue[i].name, lt+1, interfaceValue[i].name)
@ -457,7 +457,7 @@ function mapInterface(mapTypeString, mapTemplete, napiVn, lt) {
else if (interfaceValue[i].type.substring(0, 12) == "NUMBER_TYPE_") {
interfaceVarName += `std::string %dName = "%d";\n`.format(interfaceValue[i].name, interfaceValue[i].name)
interfaceFun +=
`%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(0),
`%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(XNapiTool::ZERO),
tt%d.c_str()), %sName.c_str()), %s, tt%d.%s);\n`
.format("NUMBER_JS_2_C", "GetMapElementValue", lt, interfaceValue[i].name,
interfaceValue[i].type, lt + 1, interfaceValue[i].name)
@ -465,7 +465,7 @@ function mapInterface(mapTypeString, mapTemplete, napiVn, lt) {
else if (interfaceValue[i].type == 'boolean') {
interfaceVarName += `std::string %dName = "%d";\n`.format(interfaceValue[i].name, interfaceValue[i].name)
interfaceFun +=
`tt%d.%s = pxt->%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(0),
`tt%d.%s = pxt->%s(pxt->%s(pxt->GetMapElementValue(pxt->GetArgv(XNapiTool::ZERO),
tt%d.c_str()), %sName.c_str()));\n`
.format(lt + 1, interfaceValue[i].name, "SwapJs2CBool", "GetMapElementValue",
lt, interfaceValue[i].name)
@ -700,10 +700,10 @@ function isArrayType(type) {
}
function getValueCheckout(funcValue, param, inParamName, p, cType) {
let valueCheckout = jsToC(inParamName, "pxt->GetArgv(%d)".format(p), funcValue.type) + "\n "
let valueCheckout = jsToC(inParamName, "pxt->GetArgv(%d)".format(getConstNum(p)), funcValue.type) + "\n "
if (funcValue.optional) {
valueCheckout = "if (pxt->GetArgc() > %d) {\n vio->in%d = new %s;\n ".format(p, p, cType)
+ valueCheckout + "}\n "
valueCheckout = "if (pxt->GetArgc() > %d) {\n vio->in%d = new %s;\n "
.format(getConstNum(p), p, cType) + valueCheckout + "}\n "
param.optionalParamDestory += "C_DELETE(vio->in%d);\n ".format(p)
}
return valueCheckout;
@ -712,7 +712,7 @@ function getValueCheckout(funcValue, param, inParamName, p, cType) {
function paramGenerateUnion(type, param, p, name) {
param.valueIn += `\n std::any in%d;
std::string in%d_type;`.format(p, p)
param.valueCheckout += jsToC("vio->in" + p, "pxt->GetArgv(%d)".format(p), type)
param.valueCheckout += jsToC("vio->in" + p, "pxt->GetArgv(%d)".format(getConstNum(p)), type)
param.valueFill += "%svio->in%d".format(param.valueFill.length > 0 ? ", " : "", p)
param.valueDefine += "%sstd::any &%s".format(param.valueDefine.length > 0 ? ", " : "", name)
}
@ -771,7 +771,7 @@ function paramGenerateObject(p, funcValue, param) {
param.valueIn += funcValue.optional ? "\n %s* in%d = nullptr;".format(arrayType, p)
: "\n %s in%d;".format(arrayType, p)
let arrValueCheckout = jsToC(inParamName, "pxt->GetArgv(%d)".format(p), type)
let arrValueCheckout = jsToC(inParamName, "pxt->GetArgv(%d)".format(getConstNum(p)), type)
param.valueCheckout += arrValueCheckout
param.valueFill += "%svio->in%d".format(param.valueFill.length > 0 ? ", " : "", p)
param.valueDefine += "%s%s %s%s".format(param.valueDefine.length > 0 ? ", "

View File

@ -21,6 +21,12 @@ catch (err) {
vscode = null;
}
const NUM_CONST_MAP = new Map([
[0, "XNapiTool::ZERO"], [1, "XNapiTool::ONE"], [2, "XNapiTool::TWO"], [3, "XNapiTool::THREE"],
[4, "XNapiTool::FOUE"], [5, "XNapiTool::FIVE"], [6, "XNapiTool::SIX"], [7, "XNapiTool::SEVEN"],
[8, "XNapiTool::EIGHT"], [9, "XNapiTool::NINE"]
]);
function print(...args) {
if (vscode) {
vscode.window.showInformationMessage(...args);
@ -204,15 +210,31 @@ function replaceAll(s, sfrom, sto) {
* 将方法对象插入列表重复的方法对象不插入
* @param obj 待插入的方法对象
* @param list 目标列表
* @returns void
* @returns 是否成功插入列表
*/
function addUniqFunc2List(obj, list) {
for (let i in list) {
if (isSameFunc(obj, list[i])) {
return
return false
}
}
list.push(obj)
return true
}
/**
* 找到子类中重写父类的方法并将它设置为override
* @param parentFunc 父类被重写的方法名
* @param childFunclist 子类全部方法列表
* @returns void
*/
function setOverrideFunc(parentFunc, childFunclist) {
for (let i in childFunclist) {
if (isSameFunc(parentFunc, childFunclist[i])) {
childFunclist[i].isOverride = true
return
}
}
}
/**
@ -233,10 +255,11 @@ function replaceAll(s, sfrom, sto) {
/**
* 如果方法所在的类为基类生成的c++函数定义为虚函数
* @param data 方法所在的类信息
* @param isStatic ts方法是否定义为静态方法
* return tabStr 缩进staticStr 静态函数关键词virtualStr 虚函数关键词
* @param funcInfo 方法信息
* return tabStr 缩进staticStr 静态函数关键词virtualStr 虚函数关键词, overrideStr 重写关键词
*/
function getPrefix(data, isStatic) {
function getPrefix(data, funcInfo) {
let isStatic = funcInfo.isStatic
let tabStr = ""
let virtualStr = ""
let staticStr = isStatic ? "static " : ""
@ -244,7 +267,12 @@ function replaceAll(s, sfrom, sto) {
tabStr = " " // 类中的方法增加一个缩进
virtualStr = (data.childList.length > 0 && !isStatic) ? "virtual " : "" //如果是基类中的非静态方法,定义为虚函数
}
return [tabStr, staticStr, virtualStr]
let overrideStr = funcInfo.isOverride ? " override" : "" // 重写了父类方法需要加上override关键字否则触发c++门禁告警
return [tabStr, staticStr, virtualStr, overrideStr]
}
function getConstNum(num) {
return NUM_CONST_MAP.get(parseInt(num));
}
module.exports = {
@ -258,5 +286,7 @@ module.exports = {
replaceTab,
addUniqObj2List,
addUniqFunc2List,
getPrefix
getPrefix,
getConstNum,
setOverrideFunc
}