我有一个本机 NodeJS 插件,它接受一个Buffer
实例作为它的参数之一。
我可以使用以下代码将char
数组转换为 a Buffer
,但寻找另一种方式。
static v8::Local<v8::Object> create_buffer(char *data, unsigned long length) {
node::Buffer *slow_buffer = node::Buffer::New(length);
memcpy(node::Buffer::Data(slow_buffer), data, length);
v8::Handle<v8::Value> constructor_arguments[3] = {
slow_buffer->handle_,
v8::Integer::New(length),
v8::Integer::New(0)
};
v8::Local<v8::Object> global_object = v8::Context::GetCurrent()->Global();
v8::Local<v8::Function> buffer_constructor = v8::Local<v8::Function>::Cast(global_object->Get(v8::String::New("Buffer")));
return buffer_constructor->NewInstance(3, constructor_arguments);
}