FAQ
My original c++ function had the following signature

unsigned char* random(int length);

To use this as an add-on I modified it to

Handle<Value> random(const Arguments& args) {


if(args[0]->IsInt32()){

Local<Int32> length = args[0]->ToInt32();

// Now here I want to create an array like this "unsigned char* buffer =
new unsigned char[length];" using the length variable
// but it gives me an error saying length is undefined

//1. how do I convert Local<Int32> to int ???
// 2. How to return the array from this function??


}

}

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Search Discussions

  • James Jiang at Oct 26, 2012 at 10:21 am
    Can Cast do that?

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • NodeNinja at Oct 26, 2012 at 12:34 pm

    On Friday, October 26, 2012 2:44:27 PM UTC+5:30, James Jiang wrote:
    Can Cast do that?

    I found on the web that something like this can do the trick

    if(args[0]->IsInt32() ){
    int32_t intVal = args[0]->ToInt32()->Value();

    }

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Ben Noordhuis at Oct 26, 2012 at 1:16 pm

    On Fri, Oct 26, 2012 at 8:43 AM, NodeNinja wrote:
    My original c++ function had the following signature

    unsigned char* random(int length);

    To use this as an add-on I modified it to

    Handle<Value> random(const Arguments& args) {


    if(args[0]->IsInt32()){

    Local<Int32> length = args[0]->ToInt32();

    // Now here I want to create an array like this "unsigned char* buffer = new
    unsigned char[length];" using the length variable
    // but it gives me an error saying length is undefined

    //1. how do I convert Local<Int32> to int ???
    // 2. How to return the array from this function??


    }

    }
    int length = args[0]->Int32Value();
    if (length <= 0) {
    return ThrowException(Exception::Error(String::New("BAM")));
    }
    unsigned char* buffer = new unsigned char[length];

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • NodeNinja at Oct 26, 2012 at 12:00 pm

    int length = args[0]->Int32Value();
    if (length <= 0) {
    return ThrowException(Exception::Error(String::New("BAM")));
    }
    unsigned char* buffer = new unsigned char[length];
    Thanks for that info Ben,

    Further to that is there a way to pass and return array of pointers I have
    something like (unsigned int **)

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Ben Noordhuis at Oct 26, 2012 at 2:16 pm

    On Fri, Oct 26, 2012 at 2:00 PM, NodeNinja wrote:
    int length = args[0]->Int32Value();
    if (length <= 0) {
    return ThrowException(Exception::Error(String::New("BAM")));
    }
    unsigned char* buffer = new unsigned char[length];

    Thanks for that info Ben,

    Further to that is there a way to pass and return array of pointers I have
    something like (unsigned int **)
    Yep. Here is how you accept one:

    if (!args[0]->IsArray()) {
    return ThrowException(Exception::Error(String::New("BAM")));
    }
    Local<Array> array = Local<Array>::Cast(args[0]);
    uint32_t len = array->Length();
    Local<Value> first = array->Get(0);

    And this is how you create one:

    Local<Array> array = Array::New(32); // 32=initial length, optional
    array->Set(0, String::New("first"));

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Dean Mao at Oct 26, 2012 at 2:34 pm
    If you want some crazy v8 type conversion library, there is v8-juice:
    http://code.google.com/p/v8-juice/wiki/ConvertingTypes

    I've not used it before, but it came highly recommended from others. It's
    not too useful for converting one type, but if you have composite types, it
    might make things easier.

    On Fri, Oct 26, 2012 at 5:00 AM, NodeNinja wrote:


    int length = args[0]->Int32Value();
    if (length <= 0) {
    return ThrowException(Exception::**Error(String::New("BAM")));
    }
    unsigned char* buffer = new unsigned char[length];
    Thanks for that info Ben,

    Further to that is there a way to pass and return array of pointers I have
    something like (unsigned int **)

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines:
    https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • NodeNinja at Oct 28, 2012 at 3:52 am

    If you want some crazy v8 type conversion library, there is v8-juice:
    http://code.google.com/p/v8-juice/wiki/ConvertingTypes
    Thats wonderful info Dan!! Many thanks!
    --------------------------

    Ben, Thanks for the code snippet is an array of pointers also created in
    the same style?



    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en
  • Ben Noordhuis at Oct 29, 2012 at 11:44 am

    On Sun, Oct 28, 2012 at 4:52 AM, NodeNinja wrote:
    Ben, Thanks for the code snippet is an array of pointers also created in the
    same style?
    Depends on what you mean by 'pointer'. JS the language doesn't have a
    concept of pointers. V8 lets you store pointers with
    v8::Object::SetPointerInInternalField() but those are visible only to
    C++, not JS.

    --
    Job Board: http://jobs.nodejs.org/
    Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
    You received this message because you are subscribed to the Google
    Groups "nodejs" group.
    To post to this group, send email to nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/nodejs?hl=en?hl=en

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedOct 26, '12 at 7:43a
activeOct 29, '12 at 11:44a
posts9
users4
websitenodejs.org
irc#node.js

People

Translate

site design / logo © 2022 Grokbase