javascript - 函数的Setter/Getter在调用函数时将函数转换为字符串?

标签 javascript c++ v8

我正在使用 v8 为我的 C++ 应用程序创建一个 javascript 接口(interface),但遇到了函数回调问题。

我有一个对象模板,它有对象“更新”的 setter 和 getter,它只是设置/获取 setter 和 getter 都可以访问的对象句柄(参见“我试过的东西”。)实例化一个对象在此对象模板中称为“world”的全局上下文中。然后运行脚本,将“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 对象的内部字段,尽管我尝试过使用全局变量和使用普通的旧对象句柄——这里没有指出任何区别。
  • 获取更新功能但不调用,然后再次获取。这证明电话是原因的一部分
  • 获取和调用,然后从内部字段调用(没有区别。)
  • 直接从内部字段调用(worldUpdateFunc.func);第一次调用成功,此后内部字段不再是函数(无法弄清楚它是什么,因为它向所有 Is* 函数返回 false)并且程序在 V8 中的某个地方随机出现段错误??
  • 删除“use strict”没有任何作用

最佳答案

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

解决方案是简单地包装一个 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 !

关于javascript - 函数的Setter/Getter在调用函数时将函数转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19202721/

相关文章:

javascript - 可搜索的选择选项

javascript - 是否有用于 JavaScript 的 native 机器代码编译器?

c++ - 动态向下转换为抽象类 (C++)

eclipse - Node 和 ChromeDevTools 中的 V8 版本?

javascript - v8 对嵌套函数的优化效果如何?

javascript - 如何设计多个段落的单选按钮?

java - Apache HttpClient 4 和 JavaScript

c++ - 用于 C、C++ 或 Fortran 的代码抛光器/重组器

C++ Vector 实现分配新对象

javascript - Chrome V8 错误