Skip to content

Instantly share code, notes, and snippets.

Last active October 2, 2017 20:33
What would you like to do?
External buffer for nbind
#include <cstddef>
/// A non-owning memory view in a C++ std::vector<double> like object.
class ExternalBuffer {
ExternalBuffer(double* data, size_t length):
data_(data), length_(length) {}
void* data() {
return reinterpret_cast<void*>(data_);
size_t byte_length() {
return length_ * sizeof(double);
size_t length() {
return length_;
double* data_;
/// The actual
size_t length_;
#include "nbind/api.h"
#include <nan.h>
#include <node_version.h>
namespace nbind {
template <> struct BindingType<ExternalBuffer> {
typedef ExternalBuffer Type;
static bool checkType(WireType /*unused*/) {
return false;
static Type fromWireType(WireType /*unused*/) {
NBIND_ERR("can not create a BorrowedBuffer from a JS object");
return Type(nullptr, 0);
static WireType toWireType(Type &&arg) {
auto buffer = v8::ArrayBuffer::New(
v8::Isolate::GetCurrent(),, arg.byte_length()
return v8::Float64Array::New(buffer, 0, arg.length());
#elif defined(__EMSCRIPTEN__)
} // namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment