将数据复制到 v8::ArrayBuffer [英] Copy data into v8::ArrayBuffer

查看:66
本文介绍了将数据复制到 v8::ArrayBuffer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 v8 用 C++ 编写一个 Javascript 解释器.我需要将一个字符缓冲区传递给一个 ArrayBuffer 以便它被垃圾收集.这是我的代码:

I'm writing a Javascript interpreter in C++ using v8. I need to pass a char buffer into an ArrayBuffer so that it gets garbage collected. Here is my code:

QByteArray data_buffer(file.readAll().data(), file.size());

v8::Handle<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(args.GetIsolate(), data_buffer.size());
//insert code to copy data from data_buffer to ab
args.GetReturnValue().Set(ab);

如果我使用文档中传递指向数据的指针的构造函数,我将不得不自己处理内存,而我不想要那样.

If I use the constructor from the documentation in which I pass a pointer to the data, I'll have to deal with the memory myself and I don't want that.

我想避免分配内存,让 v8 自己做内存管理.找不到使用 Set() 或任何其他函数的方法.

I want to avoid allocating memory and let v8 do it's own memory management. Couldn't find a way using Set() or any other function.

关于如何将数据复制到数组缓冲区的任何建议?或者如何使用 2 个参数构造函数让 v8 处理我的数据使用的内存?

Any suggestions on how to copy data to the arraybuffer? Or how can I use the 2 parameters constructor to let v8 deal with the memory my data uses?

此处的文档:http://bespin.cz/~ondras/html/classv8_1_1ArrayBuffer.html 谢谢.

推荐答案

找到方法:

memcpy(ab->GetContents().Data(), data_buffer.data(), data_buffer.size());

现在我不需要分配内存,一切都被垃圾收集了.

Now I don't need to allocate memory and everything is garbage collected.

这篇关于将数据复制到 v8::ArrayBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆