Author Topic: The NodeJS node.js node js Thread  (Read 8665 times)

0 Members and 1 Guest are viewing this topic.






adarqui

  • Administrator
  • Hero Member
  • *****
  • Posts: 30211
  • who run it.
  • Respect: +7366
    • View Profile
    • Email
Re: The NodeJS node.js node js Thread
« Reply #20 on: April 21, 2013, 05:10:18 pm »
0
http://stackoverflow.com/questions/8371003/how-to-parse-arguments-object-in-c-v8

v8 arguments

For v8::Value instances (such as those contained in Arguments), you
can do just like I did on line 16.

For v8::Number instances, use v8::Number::Value() to return a double.
For v8::Integer instances, use v8::Integer::Value() to return a 64-bit
signed integer.
For v8::Int32 instances, use v8::Int32::Value() to return a 32-bit
signed integer.
For v8::Uint32 instances, use v8::Uint32::Value() to return a 32-bit
unsigned integer.

If you have a v8::Number instance and you want to cast it as a signed
32-bit integer: int num = v8num.Int32Value();
You can do this because v8::Number is a v8::Value.

For v8::String::AsciiValue or v8::String::Utf8Value instances, you can
do as noted in the comment in the gist: char *cstr = (char *) *str;

The v8 API docs detail the class inheritance as well as all of the
classes' available member functions and operators: http://izs.me/v8-docs/annotated.html


Code: [Select]
#include <node.h>
#include <v8.h>
 
#include "point.h"
 
using namespace v8;
using namespace node;
 
void Point::Initialize(Handle<Object> target){
    HandleScope scope;
 
    Local<FunctionTemplate> t = FunctionTemplate::New(New);
 
    t->InstanceTemplate()->SetInternalFieldCount(1);
 
    NODE_SET_PROTOTYPE_METHOD(t, "getX", Point::getX);
    NODE_SET_PROTOTYPE_METHOD(t, "get", Point::get);
    NODE_SET_PROTOTYPE_METHOD(t, "getY", Point::getY);
    NODE_SET_PROTOTYPE_METHOD(t, "setX", Point::setX);
    NODE_SET_PROTOTYPE_METHOD(t, "setY", Point::setY);
 
    target->Set(String::NewSymbol("Point"), t->GetFunction());
}
 
Handle<Value> Point::getX(const Arguments &args){
    HandleScope scope;
    Point *p = ObjectWrap::Unwrap<Point>(args.This());
    Local<Integer> result = Integer::New(p->x);
    return scope.Close(result);
}
 
Handle<Value> Point::getY(const Arguments &args){
    HandleScope scope;
    Point *p = ObjectWrap::Unwrap<Point>(args.This());
    Local<Integer> result = Integer::New(p->y);
    return scope.Close(result);
}
 
Handle<Value> Point::setX(const Arguments &args){
    HandleScope scope;
    if(args.Length() != 1 || !args[0]->IsInt32()){
        return ThrowException(Exception::TypeError(String::New("Argument must be an integer")));
    }
    Local<Integer> i = Local<Integer>::Cast(args[0]);
    Point *p = ObjectWrap::Unwrap<Point>(args.This());
    p->x = (int)(i->Int32Value());
 
    return Undefined();
}
 
Handle<Value> Point::setY(const Arguments &args){
    HandleScope scope;
    if(args.Length() != 1 || !args[0]->IsInt32()){
        return ThrowException(Exception::TypeError(String::New("Argument must be an integer")));
    }
    Local<Integer> i = Local<Integer>::Cast(args[0]);
    Point *p = ObjectWrap::Unwrap<Point>(args.This());
    p->y = (int)(i->Int32Value());
    return Undefined();
}
 
Handle<Value> Point::get(const Arguments &args){
    HandleScope scope;
    Point *p = ObjectWrap::Unwrap<Point>(args.This());
    Local<Object> result =  Object::New();
    result->Set(v8::String::New("x"), v8::Integer::New(p->x));
    result->Set(v8::String::New("y"), v8::Integer::New(p->y));
    return scope.Close(result);
}
 
Handle<Value> Point::New(const Arguments &args){
    HandleScope scope;
 
    Point *point = new Point();
    point->Wrap(args.This());
 
    return scope.Close(args.This());
}
 
extern "C" void init(Handle<Object> target){
    HandleScope scope;
    Point::Initialize(target);
};