我正在嘗試爲NodeJS C++ Addon使用V8::AddMemoryAllocationCallback方法。我想調用該方法並返回該大小值。我想出了以下代碼。似乎不是調用回調方法。 代碼取自goingnative npm模塊。如何在C++ NodeJS插件中使用V8 :: AddMemoryAllocationCallback方法
但memCallback
方法沒有得到觸發器。爲什麼?如何解決它?
它將使用以下C++代碼來訪問V8庫。
//myaddon.cc
#include <nan.h>
# include <unistd.h>
#include <iostream>
#include <fstream>
using namespace std;
using namespace v8;
static int x = 0;
static int y = 0;
void memCallback(ObjectSpace space, AllocationAction action, int size) {
ofstream myfile;
myfile.open ("/tmp/example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
x = size;
}
NAN_METHOD(Delay) {
NanScope();
int delay = args[0].As<Number>()->IntegerValue();
Local<Function> callback = args[1].As<Function>();
V8::AddMemoryAllocationCallback(&memCallback, kObjectSpaceNewSpace, kAllocationActionAllocate);
for(int i = 0; i < 10; i++) {
y = i;
Local<Value> argv[] = {NanNew(x), NanNew(y)};
NanMakeCallback(NanGetCurrentContext()->Global(), callback, 2, argv);
usleep(delay * 1000);
}
NanReturnUndefined();
}
void Init(Handle<Object> exports) {
exports->Set(NanNew("delay"), NanNew<FunctionTemplate>(Delay)->GetFunction());
}
NODE_MODULE(myaddon, Init)
節點GYP具有用於構建和運行的代碼(嘗試node-gyp rebuild && node index.js 1000 && ls /tmp/
從當前文件夾)
//binding.gyp
{
"targets": [
{
"target_name": "myaddon",
"sources": [ "myaddon.cc" ],
"include_dirs": [ "<!(node -e \"require('nan')\")" ]
}
]
}
以下是JavaScript代碼。我創建了一些變量來分配內存。
//index.js
var addon = require('bindings')('myaddon')
addon.delay(process.argv[2], function(x, y) {
console.log("X: ", x, " Y:", y);
var arr = [], obj = {};
for (var i = 0; i < 100; i++) {
arr.push("Text " + i);
}
for (var i = 0; i < 100; i++) {
obj[i] = arr[i];
delete arr[i];
}
console.log('Done!');
})
console.log("The End");
電流輸出是;
X: 0 Y: 0
Done!
X: 0 Y: 1
Done!
X: 0 Y: 2
Done!
X: 0 Y: 3
Done!
X: 0 Y: 4
Done!
X: 0 Y: 5
Done!
X: 0 Y: 6
Done!
X: 0 Y: 7
Done!
X: 0 Y: 8
Done!
X: 0 Y: 9
Done!
The End
'AddMemoryAllocationCallback()'回調可能會在主線程中調用,所以目前您通過在for循環中休眠來阻止該線程。如果你刪除了循環,你會看到正在寫入的'example.txt'嗎? – mscdex
@mscdex我刪除了'for loop'和'usleep'。但是我看不到'example.txt'。你是否認爲我的代碼編寫得很好(任何語法錯誤)?我不確定'V8 :: AddMemoryAllocationCallback(&memCallback,kObjectSpaceNewSpace,kAllocationActionAllocate);''或'V8 :: AddMemoryAllocationCallback(memCallback,kObjectSpaceNewSpace,kAllocationActionAllocate);'。如何添加函數引用? – gihanchanuka
您是否嘗試過其他參數,如'kObjectSpaceAll'而不是'kObjectSpaceNewSpace'和/或'kAllocationActionAll'而不是'kAllocationActionAllocate'? – mscdex