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