I'm trying to transfer a function to node.js project

45 Views Asked by At

I'm trying to transfer a function to node.js project. I've succeeded in creating the function itself so far. The JavaScript code is accepting it and everything works as it should. I'm not exactly certain on how to pass in any parameters nor return any value. I'm using nan.h for this.

The Code:

#include <iostream>
#include <string>
#include <bitset>
#include <math.h>
#include <nan.h>
// using namespace v8 ;
// using namespace std ;

NAN_METHOD( encript ) {
  const float stringLn = 11 ;

  std::string data = "Hello World" ;
  int bit8Data[ sizeof( data ) / sizeof( data[0] ) ];
  for ( std::size_t i = 0; i < data.size(); ++i )
  {
    bit8Data[i] = int( long( std::bitset<8>( data.c_str()[i] ).to_ulong()));
  }

  int bit6Data[ 15 ];
  int remainder = 0, remainderLn = 0;
  int j = 0 ;
  for ( std::size_t i = 0; i < data.size(); ++i )
  {
    if ( remainderLn == 6 ) {
      bit6Data[j] = remainder ;
      j++ ;
      remainder = 0 ;
      remainderLn = 0 ;
    };

    bit6Data[j] = int( remainder * pow( 2, 8 ) + bit8Data[i] ) / int( pow( 2, remainderLn + 2 ));
    remainder   = int( remainder * pow( 2, 8 ) + bit8Data[i] ) % int( pow( 2, remainderLn + 2 ));
    remainderLn += 2 ;

    j++ ;
  }
  if ( remainderLn > 0 ) bit6Data[j] = remainder * ( pow( 2, 6 - remainderLn ));

  for ( std::size_t i = 0; i < sizeof( bit6Data ) / sizeof( bit6Data[0] ); ++i )
  {
    std::cout << i << ": " << bit6Data[i] << std::endl ;
  }
}


NAN_MODULE_INIT( encriptInit ) {
  Nan::SetMethod( target, "encript", encript );
}

NODE_MODULE( encript, encriptInit );
1

There are 1 best solutions below

0
Criminal_Affair_At_SO On

NAN_METHOD creates a definition with a single info parameter that is an array of Local<Value>. info[0] will be the first one, info.This() will be the this and you can call info.Length() to get the count. Call info.GetReturnValue().Set(value) to return a value.