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:
parent
712ad9318b
commit
0e4d915faf
5 changed files with 49 additions and 66 deletions
|
@ -17,11 +17,9 @@ extern "C" {
|
||||||
Value::Init();
|
Value::Init();
|
||||||
Object::Init();
|
Object::Init();
|
||||||
Primitive::Init();
|
Primitive::Init();
|
||||||
Name::Init();
|
|
||||||
Number::Init();
|
Number::Init();
|
||||||
Integer::Init();
|
Integer::Init();
|
||||||
Int32::Init();
|
Name::Init();
|
||||||
Uint32::Init();
|
|
||||||
String::Init();
|
String::Init();
|
||||||
Symbol::Init();
|
Symbol::Init();
|
||||||
Function::Init();
|
Function::Init();
|
||||||
|
|
|
@ -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 */
|
|
|
@ -3,6 +3,36 @@
|
||||||
#define RR_INTEGER_H
|
#define RR_INTEGER_H
|
||||||
|
|
||||||
namespace rr {
|
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> {
|
class Integer : public Ref<v8::Integer> {
|
||||||
public:
|
public:
|
||||||
Integer(v8::Isolate* isolate, v8::Handle<v8::Integer> integer) :
|
Integer(v8::Isolate* isolate, v8::Handle<v8::Integer> integer) :
|
||||||
|
@ -14,17 +44,21 @@ namespace rr {
|
||||||
Isolate isolate(r_isolate);
|
Isolate isolate(r_isolate);
|
||||||
Locker lock(isolate);
|
Locker lock(isolate);
|
||||||
|
|
||||||
v8::Local<v8::Integer> i = v8::Integer::New(isolate, NUM2INT(value));
|
v8::Local<v8::Integer> integer(v8::Integer::New(isolate, NUM2INT(value)));
|
||||||
return Value::handleToRubyObject(isolate, i);
|
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) {
|
static VALUE NewFromUnsigned(VALUE self, VALUE r_isolate, VALUE value) {
|
||||||
Isolate isolate(r_isolate);
|
Isolate isolate(r_isolate);
|
||||||
Locker lock(isolate);
|
Locker lock(isolate);
|
||||||
|
|
||||||
uint32_t uint = NUM2UINT(value);
|
return Uint32(isolate, v8::Integer::NewFromUnsigned(isolate, NUM2UINT(value)));
|
||||||
v8::Local<v8::Integer> i = v8::Integer::NewFromUnsigned(isolate, uint);
|
|
||||||
return Value::handleToRubyObject(isolate, i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE Value(VALUE self) {
|
static VALUE Value(VALUE self) {
|
||||||
|
@ -40,6 +74,14 @@ namespace rr {
|
||||||
defineSingletonMethod("NewFromUnsigned", &NewFromUnsigned).
|
defineSingletonMethod("NewFromUnsigned", &NewFromUnsigned).
|
||||||
defineMethod("Value", &Value).
|
defineMethod("Value", &Value).
|
||||||
store(&Class);
|
store(&Class);
|
||||||
|
|
||||||
|
ClassBuilder("Int32", Integer::Class).
|
||||||
|
defineMethod("Value", &Value).
|
||||||
|
store(&Int32::Class);
|
||||||
|
|
||||||
|
ClassBuilder("Uint32", Integer::Class).
|
||||||
|
defineMethod("Value", &Value).
|
||||||
|
store(&Uint32::Class);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ inline VALUE not_implemented(const char* message) {
|
||||||
#include "maybe.h"
|
#include "maybe.h"
|
||||||
#include "equiv.h"
|
#include "equiv.h"
|
||||||
#include "bool.h"
|
#include "bool.h"
|
||||||
|
#include "uint32_t.h"
|
||||||
#include "pointer.h"
|
#include "pointer.h"
|
||||||
#include "isolate.h"
|
#include "isolate.h"
|
||||||
|
|
||||||
|
@ -38,8 +39,6 @@ inline VALUE not_implemented(const char* message) {
|
||||||
#include "primitive.h"
|
#include "primitive.h"
|
||||||
#include "number.h"
|
#include "number.h"
|
||||||
#include "integer.h"
|
#include "integer.h"
|
||||||
#include "int32.h"
|
|
||||||
#include "uint32.h"
|
|
||||||
|
|
||||||
|
|
||||||
#include "external.h"
|
#include "external.h"
|
||||||
|
|
|
@ -4,33 +4,6 @@
|
||||||
|
|
||||||
|
|
||||||
namespace rr {
|
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`.
|
* Converts between Ruby `Number` and the C/C++ `uint32_t`.
|
||||||
*
|
*
|
Loading…
Add table
Add a link
Reference in a new issue