2013-10-05 36 views
3

我正在使用v8爲我的C++應用創建一個JavaScript接口,但遇到了函數回調的問題。Setter/Getter函數調用函數時將函數轉換爲字符串?

我有一個對象模板,它有一個setter和getter對象「update」,它簡單地設置/獲取setter和getter都可訪問的對象句柄(請參閱「我試過的東西」。)在這個對象模板中被稱爲「世界」的全局上下文中實例化。然後運行一個腳本,它將「world.update」設置爲具有基本輸出消息的函數。然後程序獲取更新函數並調用它,它完全按照預期工作 - 打印一些輸出。然後程序再次獲取更新函數,但更新函數現在是一個字符串 - 來自原始調用的輸出。嘗試調用它會導致異常。

代碼:

#include <iostream> 
#include <fstream> 
#include <string> 

#include <v8.h> 

using namespace v8; 

std::string readFile(std::string fname) { 
    std::ifstream finput(fname); 
    std::string filestr((std::istreambuf_iterator<char>(finput)), 
         std::istreambuf_iterator<char>()); 

    return filestr; 
} 

Handle<Value> print(const Arguments& args) { 
    String::AsciiValue str(args[0]); 
    std::cout << *str; 

    HandleScope scope; 
    return scope.Close(Undefined()); 
} 

class FuncWrapper { 
public: 
    Handle<Function> func; 

}; 

Handle<Value> getWorldUpdate(Local<String> property, const AccessorInfo &info) { 
    std::cout << "Get update() [" << *String::AsciiValue(property) << "]\n"; 

    Local<Object> self = info.Holder(); 
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0)); 
    FuncWrapper *fw = static_cast<FuncWrapper*>(wrap->Value()); 

    return fw->func; 
} 

void setWorldUpdate(Local<String> property, Local<Value> value, const AccessorInfo& info) { 
    std::cout << "Set update() [" << *String::AsciiValue(property) << "]\n"; 

    Local<Object> self = info.Holder(); 
    Local<External> wrap = Local<External>::Cast(self->GetInternalField(0)); 
    FuncWrapper *fw = static_cast<FuncWrapper*>(wrap->Value()); 

    //Accessor info could be used to get the class here 
    fw->func = Handle<Function>::Cast(value); 
} 

int main() { 
    // Create a stack-allocated handle scope. 
    HandleScope handle_scope; 

    //Add stuff 
    Handle<ObjectTemplate> globalScope = ObjectTemplate::New(); 
    globalScope->Set(String::New("print"), FunctionTemplate::New(print)); 

    Handle<ObjectTemplate> worldTmpl = ObjectTemplate::New(); 
    worldTmpl->SetInternalFieldCount(1); 
    worldTmpl->SetAccessor(String::New("update"), getWorldUpdate, setWorldUpdate); 

    // Create a new context. 
    Handle<Context> context = Context::New(NULL, globalScope); 

    // Enter the created context for compiling 
    Context::Scope context_scope(context); 

    Handle<Object> global = context->Global(); 

    Handle<Object> world = worldTmpl->NewInstance(); 
    FuncWrapper worldUpdateFunc; 
    world->SetInternalField(0, External::New((void*)&worldUpdateFunc)); 
    global->Set(String::New("world"), world); 

    // Compile the source code. 
    Handle<Script> script = Script::Compile(String::New(readFile("main.js").c_str())); 

    // Run the script to get the result. 
    script->Run(); 

    v8::TryCatch try_catch; 
    Handle<Function> updateFunc = Handle<Function>::Cast(world->Get(String::New("update"))); 
    updateFunc->Call(updateFunc, 0, NULL); 

    if (try_catch.HasCaught()) { 
     String::AsciiValue asciistr(try_catch.Message()->Get()); 
     std::cout << "Caught1: " << *asciistr << "\n"; 
     return -1; 
    } 

    //Re-calling. Has the same effect as calling worldUpdateFunc.func 
    updateFunc = Handle<Function>::Cast(world->Get(String::New("update"))); 
    updateFunc->Call(updateFunc, 0, NULL); 

    if (try_catch.HasCaught()) { 
     String::AsciiValue asciistr(try_catch.Message()->Get()); 
     std::cout << "Caught2: " << *asciistr << "\n"; 
     return -1; 
    } 

    return 0; 
} 

腳本(main.js):

"use strict"; 

world.update = function() { 
    print("Did a world.update()\n"); 
} 

輸出:

Set update() [update] 
Get update() [update] 
Did a world.update() 
Get update() [update] 
Caught2: Uncaught TypeError: Did a world.update() 
is not a function 

沒有對象模板(即只是一個普通的對象在沒有getter/setter組合的JavaScript)程序功能正常,但我想能夠使用它來哈腳本管理回調。

爲什麼會發生這種情況,我做錯了什麼?

事情我已經嘗試:

  • 在代碼中我使用一個內部字段指向Handle對象,雖然我一直在使用一個全局變量,使用普通的舊對象處理嘗試 - 沒有差異,這裏要注意。
  • Get'ing更新功能,但沒有調用,然後再次get'ing。這證明該呼叫是原因的一部分
  • Get'ing和呼叫,然後從內部字段調用(沒有差異。)
  • 直接從內部字段調用(worldUpdateFunc.func);第一次調用是成功的,在此之後內部字段不再是一個函數(不知道它是什麼,因爲它返回所有Is *函數的假),程序段錯誤在V8隨機某個地方?
  • 刪除「使用嚴格的」不執行任何操作

回答

0

的問題是,該對象指向功能手柄被刪除。

解決方案只是簡單地將Persistent而不是句柄換行,然後調用Persistent :: New()來創建函數的新實例。

class FuncWrapper { 
public: 
    Persistent<Function> func; 

}; 

... 
void setWorldUpdate(Local<String> property, Local<Value> value, const AccessorInfo& info) { 
    ... 
    fw->func = Persistent<Function>::New(Local<Function>::Cast(value)); 
} 

由於在v8-users group的人對我的幫助solve this