函数的 setter/Getter 在调用函数时将函数转换为字符串

Setter/Getter for function turns function into a string when the function is called?

本文关键字:函数 转换 字符串 setter Getter 调用      更新时间:2023-09-26

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

我有一个对象模板,其中包含用于对象"更新"的 setter 和 getter,它只是设置/获取一个可供 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;
}

脚本(主脚本.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
没有对象

模板(即在javascript中只有一个常规对象而没有getter/setter组合(,程序运行良好,但我希望能够使用它来让脚本管理回调。

为什么会这样,我做错了什么?

我尝试过的事情:

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

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

解决方案只是包装一个 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 用户组的人帮助我解决这个问题!