News for hackers. Stuff that matters.
Re:Type conversions from JavaScript to C++ in V8 by megalodon on Sunday April 03, 2016 @02:22PM (#11417025)
Somewhat related: You can also read and manipulate the contents of JavaScript typed arrays in V8:

    // 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;
    }
Re:Type conversions from JavaScript to C++ in V8 by dkopi on Sunday April 03, 2016 @03:00PM (#11417165)
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.

Re:Type conversions from JavaScript to C++ in V8 by zbjornson on Sunday April 03, 2016 @07:24PM (#11418332)
Or use the relatively new NaN helper TypedArrayContents:

https://github.com/nodejs/nan/blob/master/doc/v8_misc.md#api...

Re:Type conversions from JavaScript to C++ in V8 by wasdfan on Sunday April 03, 2016 @08:25PM (#11418610)
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.

Re:Type conversions from JavaScript to C++ in V8 by dkopi on Sunday April 03, 2016 @03:04PM (#11417179)
Does V8 have easy conversion between V8 classes and std classes?

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.

Re:Type conversions from JavaScript to C++ in V8 by tibiapejagala on Sunday April 03, 2016 @04:19PM (#11417455)
It would make sense to provide some.

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());

Re:Type conversions from JavaScript to C++ in V8 by wasdfan on Sunday April 03, 2016 @08:21PM (#11418588)
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.

Question on the book (if author sees this): will the book cover the npm V8 wrapper Nan (https://www.npmjs.com/package/nan)?

Re:Type conversions from JavaScript to C++ in V8 by freezer333 on Sunday April 03, 2016 @09:20PM (#11418815)
Author here... yes! Full table of contents is here http://scottfrees.com/ebooks/nodecpp/ - Chapter 7 will cover using nan.
Re:Type conversions from JavaScript to C++ in V8 by amelius on Sunday April 03, 2016 @03:02PM (#11417171)
http://www.swig.org also supports V8.

Does anybody know if NodeJS uses SWIG?

Re:Type conversions from JavaScript to C++ in V8 by wasdfan on Sunday April 03, 2016 @08:22PM (#11418599)
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).
Re:Type conversions from JavaScript to C++ in V8 by catnaroek on Sunday April 03, 2016 @09:32PM (#11418845)
Can Node addons be used to finally provide a workable integer datatype for JavaScript?