我正在尝试对 NodeJS C++ 插件使用V8::AddMemoryAllocationCallback方法。我想调用该方法并返回该大小值。我想出了以下代码。似乎它没有调用回调方法。代码取自gonative npm 模块。
但是该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)
node-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