Type conversions from JavaScript to C++ in V8
blog.scottfrees.com
blog.scottfrees.com
// js
var x = new Float64Array([1, 2, 3]);
binding(x);
// c++
void binding(const v8::FunctionCallbackInfo<v8::Value>& info) {
void *data = info[0].As<v8::Float64Array>()->Buffer()->GetContents().Data();
double *x = reinterpret_cast<double*>(data);
// do whatever, e.g.
x[0] = 0;
}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.
https://github.com/nodejs/nan/blob/master/doc/v8_misc.md#api...
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.
converting a V8 string to an std string and back by first casting to a null terminated char array seems very cumbersome and error prone.
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());
Question on the book (if author sees this): will the book cover the npm V8 wrapper Nan (https://www.npmjs.com/package/nan)?
Does anybody know if NodeJS uses SWIG?