Native Node.JS模块 - 从参数解析int []

时间:2022-09-04 12:08:08

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.

我正在尝试编写一个本地C ++模块以包含在Node.js项目中 - 我在这里遵循指南并且设置得很好。

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.

一般的想法是我想将一个整数数组传递给我的C ++模块进行排序;然后,模块返回已排序的数组。

However, I cannot compile using node-gyp build because I hit the following error:

但是,我无法使用node-gyp build进行编译,因为我遇到以下错误:

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

错误:没有可行的从“Local”到“int *”的转换

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

它在我的C ++中抱怨这段代码:

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

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

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

    args.GetReturnValue().Set(inputArray);
}

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

这对我来说都是概念上的意义 - 编译器不能神奇地将arg [0](可能是类型v8 :: Local)转换为int *。话虽如此,我似乎无法找到任何方法让我的论证成功地转换为C ++整数数组。

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?

应该知道我的C ++相当生疏,而且我对V8几乎一无所知。谁能指出我正确的方向?

1 个解决方案

#1


2  

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.

这并非易事:首先需要将JS数组(内部表示为v8 :: Array)解压缩为可排序的东西(如std :: vector),对其进行排序,然后将其转换回JS数组。

Here's an example:

这是一个例子:

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

    // Make sure there is an argument.
    if (args.Length() != 1) {
        isolate->ThrowException(Exception::TypeError(
            String::NewFromUtf8(isolate, "Need an argument")));
        return;
    }

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

    // 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++) {
        values.push_back(input->Get(i)->NumberValue());
    }

    // 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.
    args.GetReturnValue().Set(result);
}

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

免责声明:我不是v8向导,也不是C ++向导,所以可能有更好的方法来做到这一点。

#1


2  

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.

这并非易事:首先需要将JS数组(内部表示为v8 :: Array)解压缩为可排序的东西(如std :: vector),对其进行排序,然后将其转换回JS数组。

Here's an example:

这是一个例子:

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

    // Make sure there is an argument.
    if (args.Length() != 1) {
        isolate->ThrowException(Exception::TypeError(
            String::NewFromUtf8(isolate, "Need an argument")));
        return;
    }

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

    // 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++) {
        values.push_back(input->Get(i)->NumberValue());
    }

    // 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.
    args.GetReturnValue().Set(result);
}

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

免责声明:我不是v8向导,也不是C ++向导,所以可能有更好的方法来做到这一点。