我在vscode下建了一個c/c++ addon專案,學習插件的開發。
我用到了兩個幫助類:
1,AsyncProgressQueueWorker
2,ObjectWrap
第一個類,我用來做一些長計算
第二個類,用于包裝第一個類,這樣,在 js 里呼叫時,可以用 new 的方式創建
介面暴露的方式如下:
Napi::Value Echo(const Napi::CallbackInfo &info)
{
if (info.Length() != 8)
{
Napi::TypeError::New(info.Env(), "引數不足").ThrowAsJavaScriptException();
return Napi::Boolean::New(info.Env(), false);
}
std::string strCom = info[0].As<Napi::String>();
int nBaundRate = info[1].As<Napi::Number>().Int32Value();
std::string strParity = info[2].As<Napi::String>();
unsigned char nDataSize = info[3].As<Napi::Number>().Int32Value();
std::string strStopBits = info[4].As<Napi::String>();
Napi::Function okcb = info[5].As<Napi::Function>();
Napi::Function errcb = info[6].As<Napi::Function>();
Napi::Function progresscb = info[7].As<Napi::Function>();
CZLCom *zlcom = new CZLCom(okcb, errcb, progresscb);
zlcom->Queue();
if (zlcom->Open(strCom, nBaundRate, strParity, nDataSize, strStopBits))
{
/* code */
return Napi::Boolean::New(info.Env(), false);
}
else
{
return Napi::Boolean::New(info.Env(), true);
}
}
。。。。。。
Napi::Object CZLComWrapper::Init(Napi::Env env, Napi::Object exports)
{
Napi::Function func = DefineClass(
env, "ZLComWrapper",
{InstanceMethod("open", &CZLComWrapper::Open),
InstanceMethod("close", &CZLComWrapper::Close),
InstanceMethod("getValue", &CZLComWrapper::GetValue),
StaticMethod<&CZLComWrapper::CreateNewItem>("CreateNewItem")});
Napi::FunctionReference *constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);
exports.Set("ZLComWrapper", func);
return exports;
}
。。。。。。
Napi::Value CZLComWrapper::Open(const Napi::CallbackInfo &info)
{
if (info.Length() != 8)
{
Napi::TypeError::New(info.Env(), "引數不足").ThrowAsJavaScriptException();
return Napi::Boolean::New(info.Env(), false);
}
std::string strCom = info[0].As<Napi::String>();
int nBaundRate = info[1].As<Napi::Number>().Int32Value();
std::string strParity = info[2].As<Napi::String>();
unsigned char nDataSize = info[3].As<Napi::Number>().Int32Value();
std::string strStopBits = info[4].As<Napi::String>();
Napi::Function okcb = info[5].As<Napi::Function>();
Napi::Function errcb = info[6].As<Napi::Function>();
Napi::Function progresscb = info[7].As<Napi::Function>();
m_pCom = new CZLCom(okcb, errcb, progresscb);
m_pCom->Queue();
if (m_pCom->Open(strCom, nBaundRate, strParity, nDataSize, strStopBits))
{
return info.Env().Undefined();
}
return Napi::Boolean::New(info.Env(), false);
}
。。。。。。
Napi::Object Init(Napi::Env env, Napi::Object exports)
{
exports.Set(
Napi::String::New(env, "echo"),
Napi::Function::New(env, Echo));
return CZLComWrapper::Init(env, exports);
}
js 的呼叫方式如下:
const addon = require('./build/Debug/greet.node')
const onOk = (msg) => {
console.log(msg);
};
const onErr = (msg) => {
console.log(msg);
};
const onProgress = (msg) => {
console.log(msg);
};
// 類方式
var com = new addon.ZLComWrapper();
var ret = com.open("com3", 9600, "n", 8, "1", onOk, onErr, onProgress);
console.log(ret);
// 函式方式
// var ret = addon.echo("com3", 9600, "n", 8, "1", onOk, onErr, onProgress);
// console.log(ret);
然后我發現采用類方式的話,過了一小會兒,CZLComWrapper內部就收到了垃圾回收的訊息,而函式方式就不會,
但是如果我在命令列測驗(node inde.js),而不是在vscode下除錯的話,就不會出現上述問題。
請問有沒有人知道這是什么原因?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/253293.html
標籤:JavaScript
下一篇:使用前端開發工控軟體怎么樣?
