2013-03-03 26 views
5

我正在使用Node模塊,並試圖傳遞將ObjectWrap作爲參數的子類的實例傳遞給JavaScript回調。如何在V8中將對象傳遞給JavaScript回調

在其他地方,我已經能夠成功地展開JavaScript對象同一類,使用:

GitCommit *commit = ObjectWrap::Unwrap<GitCommit>(args[0]->ToObject()); 

我怎麼可能做相反?我想的GitCommit一個實例傳遞到JavaScript回調,如:

Local<Value> argv[] = { 
    // Error code 
    Local<Value>::New(Integer::New(0)), 
    // The commit 
    commit // Instance of GitCommit : ObjectWrap 
}; 

// Both error code and the commit are passed, JS equiv: callback(error, commit)  
ar->callback->Call(Context::GetCurrent()->Global(), 1, argv); 

這可能嗎?如果有的話,請給我一個例子,或鏈接到相關文件?

回答

3

所以你正在編寫一個節點插件。試試:

Handle<Value> argv[] = { 
    // Error code 
    Integer::New(0), 
    // The commit 
    commit->handle_ // Instance of GitCommit : ObjectWrap 
}; 

// Both error code and the commit are passed, JS equiv: callback(error, commit)  
ar->callback->Call(Context::GetCurrent()->Global(), 1, argv); 
+0

謝謝*非常*這個! – 2013-03-04 09:28:52

相關問題