Be ultra careful when doing this.
First of all - make sure to get, validate and store the buffer's length (ByteLength())
Secondly - GetContents() is only for temporary access to a buffer. if you store that pointer and use it later (//do whatever), there's a great chance it will get garbage collected.
Make sure to Externalize() the array if you intend on keeping the pointer for later.
Does anybody know of any good articles that discuss the actual performance merits of using npm addons? Aside from raw computation heavy functions (whereby dropping down to C++ would seem to be a good idea a priori), are there are other scenarios where using npm addons is a good idea in the node context?
For example, one thing I have found is that nodejs seems to be horrendously very slow at file system manipulation. Dropping down to a C++ addon thus might be a good idea if you're making a nodejs app that has a lot of file system manipulation involved.
Example problem could be that both JavaScript strings and std::string can have '\0' bytes in them. Converting them like in the post would lead to string truncation if a null byte was present somewhere in the middle of a string.
So it should be something like (error checking aside):
v8::String::Utf8Value s(args[0]);
std::string str(*s, s.length());
This is eerily timely (recently have been dealing with npm addons). Glad to see a good article about this, and am looking forward to the author's ebook.
Swig is amazing at quickly converting simple functions (i.e., functions that return an int and other simple types). But if you want to make a C++ function that returns an array or something like a JS object, SWIG doesn't seem to be able to automatically convert it into its javascript equivalent (AFAIK -- could be wrong here).