简体   繁体   中英

Conversion array in node.js with node-gyp

I have source library on C++ and I want to use it on my Node.js application. For learning how it work, I'm using node-gyp for compile C++ code, which have 2 simple functions for calculate checksum (crc16 and crc16Two).

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

    if (args.Length() < 2) {
        // Throw an Error that is passed back to JavaScript

        isolate->ThrowException(Exception::TypeError(
            String::NewFromUtf8(isolate, "Wrong number of arguments")));
        return;
    }

    uint32_t size = args[1]->Uint32Value();
    Local<Array> *data;
    Local<Array> jsArray = Local<Array>::Cast(args[0]);
    *data = jsArray;

    uint16_t crc = 0xFFFF;
    uint8_t i = 0;

    while (size--)
    {
        crc ^= data++ << 8;

        for (i = 0; i < 8; i++)
            crc = crc & 0x8000 ? (crc << 1) ^ 0x1021 : crc << 1;
    }

    Local<Number> num = Number::New(isolate, crc);
    args.GetReturnValue().Set(num);
}

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

    // Check the number of arguments passed.

    if (args.Length() < 2) {
        // Throw an Error that is passed back to JavaScript

        isolate->ThrowException(Exception::TypeError(
            String::NewFromUtf8(isolate, "Wrong number of arguments")));
        return;
    }
    // Check the argument types

    if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
        isolate->ThrowException(Exception::TypeError(
            String::NewFromUtf8(isolate, "Wrong arguments")));
        return;
    }
    // Perform the operation

    uint16_t v1 = args[0]->Uint32Value();
    uint16_t v2 = args[1]->Uint32Value();
    uint16_t crc = 0xFFFF;
    uint8_t i = 0;
    crc ^= v1 << 8;
    for (i = 0; i < 8; i++)
        crc = crc & 0x8000 ? (crc << 1) ^ 0x1021 : crc << 1;

    crc ^= v2 << 8;
    for (i = 0; i < 8; i++)
        crc = crc & 0x8000 ? (crc << 1) ^ 0x1021 : crc << 1;

    Local<Number> num = Number::New(isolate, crc);
    args.GetReturnValue().Set(num);
}

Crc16Two works fine, I have the correct result, but when I compile crc16 with node-gyp rebuild I have errors:

..\Crc.c (49): error C2296: <<: inadmissible, the left operand is "v8::Local <v8 :: Array> *" [C:\Users\dns\Desktop\Crc\build\addon.vcxproj]

Here binding.gyp:

{ "targets": [{"target_name": "addon", "sources": [ "crc.cc" ], "include_dirs" : ["<!(node -e \"require('nan')\")"]}]}

And code on Node.js:

var addon = require('bindings')('addon');
var buf = new Buffer([0,1,2,3,4,5,6,7,8,9]);
console.log(addon.crc16(buf,10));
console.log(addon.crc16Two(32145, 1470));

Could you tell how to fix it? I think I'm doing something wrong.

I am very confused about this line :

 crc ^= data++ << 8;

data is effectively a :

v8::Local <v8 :: Array> *

But from the documentation , there is no mention of a << operator implementation on it. The error is justified to me. and bit shifting on it seems strange, are you really sure you wanted to do it ?

Also , just upper of that you do :

 Local<Array> *data;
 Local<Array> jsArray = Local<Array>::Cast(args[0]);
 *data = jsArray;

At the moment you write *data = jsArray , data is an unitialized pointer , writing *data will make your program crash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM