3

test.js

buf = new Buffer(100);
for (var i = 0; i < 100; i++) buf[i] = i
addon.myFync(buf);

addon.cpp

Handle<Value> set(const Arguments& args) {
    char *buf = SOMETHING(args[0]);
    return Undefined();
}

How to get the pointer to a data of the buffer inside the C++ function?

What should I write in place of SOMETHING(args[0])?

I have node_buffer.h opened in my editor, but I cannot figure out.

Node version = v0.10.29

exebook
  • 32,014
  • 33
  • 141
  • 226

2 Answers2

8

You can do:

char* buf = node::Buffer::Data(args[0]);

to directly access the bytes of a Buffer.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
0

According to the node.js node binding documentation the 'arg[0]' value argument can be accessed as:

String::AsciiValue v(args[0]->ToString());
karmasponge
  • 1,169
  • 2
  • 12
  • 26