arthurakay arthurakay - 1 year ago 136
Node.js Question

Native Node.JS module - parsing an int[] from arguments

I am trying to write a native C++ module to include in a Node.js project -- I followed the guide here and have things setup pretty well.

The general idea is that I want to pass an array of integers to my C++ module to be sorted; the module then returns the sorted array.

However, I cannot compile using

node-gyp build
because I hit the following error:

error: no viable conversion from 'Local' to 'int *'

It is complaining about this code in my C++:

void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();

int* inputArray = args[0]; // <-- ERROR!

sort(inputArray, 0, sizeof(inputArray) - 1);


This all makes conceptual sense to me -- the compiler can't magically cast
(presumably of type
) to an
. Having said that, I cannot seem to find any way to get my argument successfully cast into a C++ integer array.

It should be known that my C++ is rather rusty, and I know next-to-nothing about V8. Can anyone point me in the right direction?

Answer Source

It's not trivial: you first need to unpack the JS array (internally represented as a v8::Array) into something sortable (like a std::vector), sort it, and convert it back to a JS array.

Here's an example:

void Method(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = args.GetIsolate();

    // Make sure there is an argument.
    if (args.Length() != 1) {
            String::NewFromUtf8(isolate, "Need an argument")));

    // Make sure it's an array.
    if (! args[0]->IsArray()) {
            String::NewFromUtf8(isolate, "First argument needs to be an array")));

    // Unpack JS array into a std::vector
    std::vector<int> values;
    Local<Array> input = Local<Array>::Cast(args[0]);
    unsigned int numValues = input->Length();
    for (unsigned int i = 0; i < numValues; i++) {

    // Sort the vector.
    std::sort(values.begin(), values.end());

    // Create a new JS array from the vector.
    Local<Array> result = Array::New(isolate);
    for (unsigned int i = 0; i < numValues; i++ ) {
        result->Set(i, Number::New(isolate, values[i]));

    // Return it.

Disclaimer: I'm not a v8 wizard, nor a C++ one, so there may be better ways to do this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download