Pass an int to a node.js addon

243 views
Skip to first unread message

NodeNinja

unread,
Oct 26, 2012, 2:43:26 AM10/26/12
to nod...@googlegroups.com
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??


}

}

James Jiang

unread,
Oct 26, 2012, 5:14:27 AM10/26/12
to nod...@googlegroups.com
Can Cast do that?

NodeNinja

unread,
Oct 26, 2012, 7:27:25 AM10/26/12
to nod...@googlegroups.com


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();

}

Ben Noordhuis

unread,
Oct 26, 2012, 7:35:35 AM10/26/12
to nod...@googlegroups.com
int length = args[0]->Int32Value();
if (length <= 0) {
return ThrowException(Exception::Error(String::New("BAM")));

NodeNinja

unread,
Oct 26, 2012, 8:00:44 AM10/26/12
to nod...@googlegroups.com


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 **)

Dean Mao

unread,
Oct 26, 2012, 9:33:47 AM10/26/12
to nod...@googlegroups.com
If you want some crazy v8 type conversion library, there is v8-juice:

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.



--
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 nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Ben Noordhuis

unread,
Oct 26, 2012, 10:16:44 AM10/26/12
to nod...@googlegroups.com
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"));

NodeNinja

unread,
Oct 27, 2012, 11:52:07 PM10/27/12
to nod...@googlegroups.com

If you want some crazy v8 type conversion library, there is v8-juice:
 
Thats wonderful info Dan!! Many thanks!

--------------------------

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

 

Ben Noordhuis

unread,
Oct 29, 2012, 7:44:11 AM10/29/12
to nod...@googlegroups.com
On Sun, Oct 28, 2012 at 4:52 AM, NodeNinja <aeon...@gmail.com> 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.
Reply all
Reply to author
Forward
0 new messages