1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

return specific subtypes of integer in constructor

This commit is contained in:
Charles Lowell 2015-07-17 12:17:14 -05:00
parent 712ad9318b
commit 0e4d915faf
5 changed files with 49 additions and 66 deletions

View file

@ -17,11 +17,9 @@ extern "C" {
Value::Init();
Object::Init();
Primitive::Init();
Name::Init();
Number::Init();
Integer::Init();
Int32::Init();
Uint32::Init();
Name::Init();
String::Init();
Symbol::Init();
Function::Init();

View file

@ -1,29 +0,0 @@
// -*- mode: c++ -*-
#ifndef RR_INT32_H
#define RR_INT32_H
namespace rr {
class Int32 : public Ref<v8::Int32> {
public:
Int32(VALUE self) :
Ref<v8::Int32>(self) {}
Int32(v8::Isolate* isolate, v8::Handle<v8::Value> value) :
Ref<v8::Int32>(isolate, value.As<v8::Int32>()) {}
static VALUE Value(VALUE self) {
Int32 int32(self);
Locker lock(int32);
return INT2NUM(int32->Value());
}
static void Init() {
ClassBuilder("Int32", Integer::Class).
defineMethod("Value", &Value).
store(&Int32::Class);
}
};
}
#endif /* RR_INT32_H */

View file

@ -3,6 +3,36 @@
#define RR_INTEGER_H
namespace rr {
class Uint32 : public Ref<v8::Uint32> {
public:
Uint32(VALUE self) :
Ref<v8::Uint32>(self) {}
Uint32(v8::Isolate* isolate, v8::Handle<v8::Value> value) :
Ref<v8::Uint32>(isolate, value.As<v8::Uint32>()) {}
static VALUE Value(VALUE self) {
Uint32 uint32(self);
Locker lock(uint32);
return UINT2NUM(uint32->Value());
}
};
class Int32 : public Ref<v8::Int32> {
public:
Int32(VALUE self) :
Ref<v8::Int32>(self) {}
Int32(v8::Isolate* isolate, v8::Handle<v8::Value> value) :
Ref<v8::Int32>(isolate, value.As<v8::Int32>()) {}
static VALUE Value(VALUE self) {
Int32 int32(self);
Locker lock(int32);
return INT2NUM(int32->Value());
}
};
class Integer : public Ref<v8::Integer> {
public:
Integer(v8::Isolate* isolate, v8::Handle<v8::Integer> integer) :
@ -14,17 +44,21 @@ namespace rr {
Isolate isolate(r_isolate);
Locker lock(isolate);
v8::Local<v8::Integer> i = v8::Integer::New(isolate, NUM2INT(value));
return Value::handleToRubyObject(isolate, i);
v8::Local<v8::Integer> integer(v8::Integer::New(isolate, NUM2INT(value)));
if (integer->IsUint32()) {
return Uint32(isolate, integer);
} else if (integer->IsInt32()) {
return Int32(isolate, integer);
} else {
return Integer(isolate, integer);
}
}
static VALUE NewFromUnsigned(VALUE self, VALUE r_isolate, VALUE value) {
Isolate isolate(r_isolate);
Locker lock(isolate);
uint32_t uint = NUM2UINT(value);
v8::Local<v8::Integer> i = v8::Integer::NewFromUnsigned(isolate, uint);
return Value::handleToRubyObject(isolate, i);
return Uint32(isolate, v8::Integer::NewFromUnsigned(isolate, NUM2UINT(value)));
}
static VALUE Value(VALUE self) {
@ -40,6 +74,14 @@ namespace rr {
defineSingletonMethod("NewFromUnsigned", &NewFromUnsigned).
defineMethod("Value", &Value).
store(&Class);
ClassBuilder("Int32", Integer::Class).
defineMethod("Value", &Value).
store(&Int32::Class);
ClassBuilder("Uint32", Integer::Class).
defineMethod("Value", &Value).
store(&Uint32::Class);
}
};
}

View file

@ -20,6 +20,7 @@ inline VALUE not_implemented(const char* message) {
#include "maybe.h"
#include "equiv.h"
#include "bool.h"
#include "uint32_t.h"
#include "pointer.h"
#include "isolate.h"
@ -38,8 +39,6 @@ inline VALUE not_implemented(const char* message) {
#include "primitive.h"
#include "number.h"
#include "integer.h"
#include "int32.h"
#include "uint32.h"
#include "external.h"

View file

@ -4,33 +4,6 @@
namespace rr {
class Uint32 : public Ref<v8::Uint32> {
public:
Uint32(VALUE self) :
Ref<v8::Uint32>(self) {}
Uint32(v8::Isolate* isolate, v8::Handle<v8::Value> value) :
Ref<v8::Uint32>(isolate, value.As<v8::Uint32>()) {}
static VALUE Value(VALUE self) {
Uint32 uint32(self);
Locker lock(uint32);
return UINT2NUM(uint32->Value());
}
static inline void Init() {
ClassBuilder("Uint32", Integer::Class).
defineMethod("Value", &Value).
store(&Class);
}
};
//TODO: remove this at some point. I don't see what this provides
//above and beyond just using UINT2NUM and NUM2UINT.
// --cowboyd Jul 9, 2015
/**
* Converts between Ruby `Number` and the C/C++ `uint32_t`.
*