Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数的Setter/Getter在调用函数时将函数转换为字符串? 我使用V8来创建一个JavaScript接口到我的C++应用程序,但是遇到了一个函数回调问题。_Javascript_C++_V8 - Fatal编程技术网

函数的Setter/Getter在调用函数时将函数转换为字符串? 我使用V8来创建一个JavaScript接口到我的C++应用程序,但是遇到了一个函数回调问题。

函数的Setter/Getter在调用函数时将函数转换为字符串? 我使用V8来创建一个JavaScript接口到我的C++应用程序,但是遇到了一个函数回调问题。,javascript,c++,v8,Javascript,C++,V8,我有一个对象模板,它有一个setter和getter,用于对象“update”,它只是设置/获取一个setter和getter都可以访问的对象句柄(请参阅“我尝试过的事情”)。对象在此对象模板中在名为“world”的全局上下文中实例化。然后运行一个脚本,将“world.update”设置为具有基本输出消息的函数。然后程序获取update函数并调用它,它完全按照预期的方式工作-打印一些输出。然后,程序再次获得update函数,但update函数现在是一个字符串——原始调用的输出。试图调用它会导致异

我有一个对象模板,它有一个setter和getter,用于对象“update”,它只是设置/获取一个setter和getter都可以访问的对象句柄(请参阅“我尝试过的事情”)。对象在此对象模板中在名为“world”的全局上下文中实例化。然后运行一个脚本,将“world.update”设置为具有基本输出消息的函数。然后程序获取update函数并调用它,它完全按照预期的方式工作-打印一些输出。然后,程序再次获得update函数,但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;
}
输出:

Set update() [update]
Get update() [update]
Did a world.update()
Get update() [update]
Caught2: Uncaught TypeError: Did a world.update()
 is not a function
如果没有对象模板(即javascript中只有一个常规对象,没有getter/setter组合),程序可以正常运行,但我希望能够使用它让脚本管理回调

为什么会发生这种情况?我做错了什么

我尝试过的事情:

  • 在代码中,我使用了一个指向句柄对象的内部字段,尽管我尝试过使用一个全局变量和普通的旧对象句柄——这里没有注意到任何差异
  • 获取更新函数但不调用,然后再次获取。这证明电话是原因之一
  • 获取和调用,然后从内部字段调用(没有区别。)
  • 直接从内部字段调用(worldUpdateFunc.func);第一次调用成功后,内部字段不再是一个函数(无法确定它是什么,因为它向所有is*函数返回false),并且程序在V8?中随机出现故障
  • 删除“严格使用”并没有任何作用

问题在于函数句柄指向的对象正在被删除

解决方案是简单地包装一个持久性而不是句柄,然后调用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));
}
class-FuncWrapper{
公众:
持久功能;
};
...
void setWorldUpdate(本地属性、本地值、常量附件信息和信息){
...
fw->func=Persistent::New(Local::Cast(value));
}
感谢现场的人对我的帮助

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));
}