我正在编写一个 Node.js 原生插件(带有nan
帮助模块)。我想创建和导出一个 ES6 类值,其类型为"function"
和。即,我想知道本机等价物:.toString()
"class ... { ... }"
module.exports = class CLASS_NAME {};
.
我在V8文档中唯一能找到的就是.SetName()
.
#include <nan.h>
#define LOCAL_STRING(c_string) (Nan::New(c_string).ToLocalChecked())
#define LOCAL_FUNCTION(c_function) \
(Nan::New<v8::FunctionTemplate>(c_function)->GetFunction())
void method(const Nan::FunctionCallbackInfo<v8::Value>& info)
{
info.GetReturnValue().Set(LOCAL_STRING("world"));
}
void initialize_module(
v8::Local<v8::Object> exports,
v8::Local<v8::Object> module
)
{
v8::Local<v8::Function> f = LOCAL_FUNCTION(method);
f->SetName(LOCAL_STRING("FUNCTION_NAME")); // The only thing I could do
module->Set(
LOCAL_STRING("exports"),
f
); // module.exports = f;
}
NODE_MODULE(blahblah, initialize_module)
但它只会改变函数的名称:
module.exports = function FUNCTION_NAME { ...... };
,它根本不会创建 ES6 类。
我该怎么做呢?