Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 0e4d915

Browse files
committed
return specific subtypes of integer in constructor
1 parent 712ad93 commit 0e4d915

5 files changed

Lines changed: 49 additions & 66 deletions

File tree

ext/v8/init.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ extern "C" {
1717
Value::Init();
1818
Object::Init();
1919
Primitive::Init();
20-
Name::Init();
2120
Number::Init();
2221
Integer::Init();
23-
Int32::Init();
24-
Uint32::Init();
22+
Name::Init();
2523
String::Init();
2624
Symbol::Init();
2725
Function::Init();

ext/v8/int32.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

ext/v8/integer.h

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,36 @@
33
#define RR_INTEGER_H
44

55
namespace rr {
6+
class Uint32 : public Ref<v8::Uint32> {
7+
public:
8+
Uint32(VALUE self) :
9+
Ref<v8::Uint32>(self) {}
10+
Uint32(v8::Isolate* isolate, v8::Handle<v8::Value> value) :
11+
Ref<v8::Uint32>(isolate, value.As<v8::Uint32>()) {}
12+
13+
static VALUE Value(VALUE self) {
14+
Uint32 uint32(self);
15+
Locker lock(uint32);
16+
17+
return UINT2NUM(uint32->Value());
18+
}
19+
};
20+
21+
class Int32 : public Ref<v8::Int32> {
22+
public:
23+
Int32(VALUE self) :
24+
Ref<v8::Int32>(self) {}
25+
Int32(v8::Isolate* isolate, v8::Handle<v8::Value> value) :
26+
Ref<v8::Int32>(isolate, value.As<v8::Int32>()) {}
27+
28+
static VALUE Value(VALUE self) {
29+
Int32 int32(self);
30+
Locker lock(int32);
31+
32+
return INT2NUM(int32->Value());
33+
}
34+
};
35+
636
class Integer : public Ref<v8::Integer> {
737
public:
838
Integer(v8::Isolate* isolate, v8::Handle<v8::Integer> integer) :
@@ -14,17 +44,21 @@ namespace rr {
1444
Isolate isolate(r_isolate);
1545
Locker lock(isolate);
1646

17-
v8::Local<v8::Integer> i = v8::Integer::New(isolate, NUM2INT(value));
18-
return Value::handleToRubyObject(isolate, i);
47+
v8::Local<v8::Integer> integer(v8::Integer::New(isolate, NUM2INT(value)));
48+
if (integer->IsUint32()) {
49+
return Uint32(isolate, integer);
50+
} else if (integer->IsInt32()) {
51+
return Int32(isolate, integer);
52+
} else {
53+
return Integer(isolate, integer);
54+
}
1955
}
2056

2157
static VALUE NewFromUnsigned(VALUE self, VALUE r_isolate, VALUE value) {
2258
Isolate isolate(r_isolate);
2359
Locker lock(isolate);
2460

25-
uint32_t uint = NUM2UINT(value);
26-
v8::Local<v8::Integer> i = v8::Integer::NewFromUnsigned(isolate, uint);
27-
return Value::handleToRubyObject(isolate, i);
61+
return Uint32(isolate, v8::Integer::NewFromUnsigned(isolate, NUM2UINT(value)));
2862
}
2963

3064
static VALUE Value(VALUE self) {
@@ -40,6 +74,14 @@ namespace rr {
4074
defineSingletonMethod("NewFromUnsigned", &NewFromUnsigned).
4175
defineMethod("Value", &Value).
4276
store(&Class);
77+
78+
ClassBuilder("Int32", Integer::Class).
79+
defineMethod("Value", &Value).
80+
store(&Int32::Class);
81+
82+
ClassBuilder("Uint32", Integer::Class).
83+
defineMethod("Value", &Value).
84+
store(&Uint32::Class);
4385
}
4486
};
4587
}

ext/v8/rr.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ inline VALUE not_implemented(const char* message) {
2020
#include "maybe.h"
2121
#include "equiv.h"
2222
#include "bool.h"
23+
#include "uint32_t.h"
2324
#include "pointer.h"
2425
#include "isolate.h"
2526

@@ -38,8 +39,6 @@ inline VALUE not_implemented(const char* message) {
3839
#include "primitive.h"
3940
#include "number.h"
4041
#include "integer.h"
41-
#include "int32.h"
42-
#include "uint32.h"
4342

4443

4544
#include "external.h"

ext/v8/uint32.h renamed to ext/v8/uint32_t.h

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,6 @@
44

55

66
namespace rr {
7-
8-
class Uint32 : public Ref<v8::Uint32> {
9-
public:
10-
Uint32(VALUE self) :
11-
Ref<v8::Uint32>(self) {}
12-
Uint32(v8::Isolate* isolate, v8::Handle<v8::Value> value) :
13-
Ref<v8::Uint32>(isolate, value.As<v8::Uint32>()) {}
14-
15-
static VALUE Value(VALUE self) {
16-
Uint32 uint32(self);
17-
Locker lock(uint32);
18-
19-
return UINT2NUM(uint32->Value());
20-
}
21-
22-
static inline void Init() {
23-
ClassBuilder("Uint32", Integer::Class).
24-
defineMethod("Value", &Value).
25-
store(&Class);
26-
}
27-
};
28-
29-
30-
//TODO: remove this at some point. I don't see what this provides
31-
//above and beyond just using UINT2NUM and NUM2UINT.
32-
// --cowboyd Jul 9, 2015
33-
347
/**
358
* Converts between Ruby `Number` and the C/C++ `uint32_t`.
369
*

0 commit comments

Comments
 (0)