全部博文(512)
分类: JavaScript
2013-07-27 17:00:10
#define BUILDING_NODE_EXTENSION #includeusing namespace v8; Handle RunCallback(const Arguments& args) { HandleScope scope; Local cb = Local ::Cast(args[0]); const unsigned argc = 1; Local argv[argc] = { Local ::New(String::New("hello world")) }; cb->Call(Context::GetCurrent()->Global(), argc, argv); return scope.Close(Undefined()); } void Init(Handle
var addon = require('./build/Release/addon'); addon.runCallback(function(msg){ console.log(msg); // 'hello world' });
#include#include struct Baton { uv_work_t request; Persistent callback; bool error; int value; // 可另加自定義變量 }; void AsyncWork(uv_work_t* req) { Baton* baton = static_cast (req->data); // ... 需時處理的非同步部分 } void AsyncAfter(uv_work_t* req) { Baton* baton = static_cast (req->data); // ... 完成後處理及傳回 node.js,argc 及 argv 為傳入 callback 的資料 baton->callback->Call(Context::GetCurrent()->Global(), argc, argv); } Handle RunCallback(const Arguments& args) { // ... node.js 的進入點,args 為傳入資料,應放到 Baton 內 Baton* baton = new Baton(); baton->request.data = baton; baton->callback = Persistent ::New(Local ::Cast(args[1])); baton->error = false; // ... 準備工作 int status = uv_queue_work(uv_default_loop(), &baton->request, AsyncWork, AsyncAfter); return Undefined(); } static void Init(Handle
var addon = require('./build/Release/addon'); addon.runCallback(function(msg){ console.log(msg); });
var memwatch = require('memwatch'); memwatch.on('stats', function(stats) { console.log(stats); });
memwatch.gc();
var weak = require('weak'); function test(){ var a = {"i": 0}; var b = weak(a, function(){ console.log("data will be garbage collected"); } }
var testObj = {a: {b: "a"}}; weak(testObj, function(){console.log("testObj will be garbage collected");}); weak(testObj.a, function(){console.log("testObj.a will be garbage collected");}); function doSth(testObj){ var intervalFunction = function(){ var b = testObj; b = null; } var interval = setInterval(intervalFunction, 2000); setInterval(function(){ clearInterval(interval); },5000); } doSth(testObj);
var testObj = {a: {b: "a"}};
weak(testObj, function(){console.log("testObj will be garbage collected");});
weak(testObj.a, function(){console.log("testObj.a will be garbage collected");});
function doSth(testObj){
var intervalFunction = function(){
var b = testObj;
b = null;
}
var interval = setInterval(intervalFunction, 2000);
setInterval(function(){
clearInterval(interval); testObj = null; },5000);
}
doSth(testObj);