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

Commit 0235696

Browse files
committed
Merge pull request #353 from cowboyd/implement-numbers
Implement numbers
2 parents 75823c1 + 7fc20dc commit 0235696

13 files changed

Lines changed: 200 additions & 34 deletions

File tree

ext/v8/array.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ namespace rr {
2727
Array array(self);
2828
Locker lock(array.getIsolate());
2929

30-
return UInt32(array->Length());
30+
return Uint32_t(array->Length());
3131
}
3232

3333
VALUE Array::CloneElementAt(VALUE self, VALUE index) {
3434
Array array(self);
3535
Locker lock(array.getIsolate());
3636

37-
return Object(array.getIsolate(), array->CloneElementAt(UInt32(index)));
37+
return Object(array.getIsolate(), array->CloneElementAt(Uint32_t(index)));
3838
}
3939

4040
}

ext/v8/init.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ extern "C" {
1717
Value::Init();
1818
Object::Init();
1919
Primitive::Init();
20+
Number::Init();
21+
Integer::Init();
2022
Name::Init();
2123
String::Init();
2224
Symbol::Init();

ext/v8/integer.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// -*- mode: c++ -*-
2+
#ifndef RR_INTEGER_H
3+
#define RR_INTEGER_H
4+
5+
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+
36+
class Integer : public Ref<v8::Integer> {
37+
public:
38+
Integer(v8::Isolate* isolate, v8::Handle<v8::Integer> integer) :
39+
Ref<v8::Integer>(isolate, integer) {}
40+
Integer(VALUE self) :
41+
Ref<v8::Integer>(self) {}
42+
43+
static VALUE New(VALUE self, VALUE r_isolate, VALUE value) {
44+
Isolate isolate(r_isolate);
45+
Locker lock(isolate);
46+
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+
}
55+
}
56+
57+
static VALUE NewFromUnsigned(VALUE self, VALUE r_isolate, VALUE value) {
58+
Isolate isolate(r_isolate);
59+
Locker lock(isolate);
60+
61+
return Uint32(isolate, v8::Integer::NewFromUnsigned(isolate, NUM2UINT(value)));
62+
}
63+
64+
static VALUE Value(VALUE self) {
65+
Integer integer(self);
66+
Locker lock(integer);
67+
68+
return INT2NUM(integer->Value());
69+
}
70+
71+
static void Init() {
72+
ClassBuilder("Integer", Number::Class).
73+
defineSingletonMethod("New", &New).
74+
defineSingletonMethod("NewFromUnsigned", &NewFromUnsigned).
75+
defineMethod("Value", &Value).
76+
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);
85+
}
86+
};
87+
}
88+
89+
#endif /* RR_INTEGER_H */

ext/v8/number.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// -*- mode: c++ -*-
2+
#ifndef RR_NUMBER_H
3+
#define RR_NUMBER_H
4+
5+
namespace rr {
6+
class Number : public Ref<v8::Number> {
7+
public:
8+
Number(v8::Isolate* isolate, v8::Handle<v8::Value> value) :
9+
Ref<v8::Number>(isolate, value.As<v8::Number>()) {}
10+
Number(VALUE self) :
11+
Ref<v8::Number>(self) {}
12+
13+
static VALUE New(VALUE self, VALUE r_isolate, VALUE value) {
14+
Isolate isolate(r_isolate);
15+
Locker lock(isolate);
16+
17+
return Number(isolate, v8::Number::New(isolate, NUM2DBL(value)));
18+
}
19+
20+
static VALUE Value(VALUE self) {
21+
Number number(self);
22+
Locker lock(number);
23+
24+
return DBL2NUM(number->Value());
25+
}
26+
static void Init() {
27+
ClassBuilder("Number", Primitive::Class).
28+
defineSingletonMethod("New", &New).
29+
defineMethod("Value", &Value).
30+
store(&Class);
31+
}
32+
};
33+
}
34+
35+
36+
#endif /* RR_NUMBER_H */

ext/v8/object.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace rr {
2727
Locker lock(isolate);
2828

2929
if (rb_obj_is_kind_of(key, rb_cNumeric)) {
30-
return Bool::Maybe(object->Set(context, UInt32(key), Value::rubyObjectToHandle(isolate, value)));
30+
return Bool::Maybe(object->Set(context, Uint32_t(key), Value::rubyObjectToHandle(isolate, value)));
3131
} else {
3232
return Bool::Maybe(object->Set(context, *Value(key), Value::rubyObjectToHandle(isolate, value)));
3333
}
@@ -40,7 +40,7 @@ namespace rr {
4040
Locker lock(isolate);
4141

4242
if (rb_obj_is_kind_of(key, rb_cNumeric)) {
43-
return Value::Maybe(isolate, object->Get(context, UInt32(key)));
43+
return Value::Maybe(isolate, object->Get(context, Uint32_t(key)));
4444
} else {
4545
return Value::Maybe(isolate, object->Get(context, *Value(key)));
4646
}

ext/v8/rr.h

Lines changed: 5 additions & 1 deletion
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

@@ -30,13 +31,16 @@ inline VALUE not_implemented(const char* message) {
3031
#include "handles.h"
3132
#include "context.h"
3233

33-
#include "uint32.h"
3434
#include "value.h"
3535
#include "backref.h"
3636

3737
#include "object.h"
3838
#include "array.h"
3939
#include "primitive.h"
40+
#include "number.h"
41+
#include "integer.h"
42+
43+
4044
#include "external.h"
4145
// This one is named v8_string to avoid name collisions with C's string.h
4246
#include "name.h"

ext/v8/script-origin.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "rr.h"
22

33
namespace rr {
4-
VALUE ScriptOrigin::Class;
4+
VALUE ScriptOrigin::Class;
55
void ScriptOrigin::Init() {
66
ClassBuilder("ScriptOrigin").
77
defineSingletonMethod("new", &initialize).

ext/v8/script-origin.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ namespace rr {
3535
VALUE is_opaque; //option
3636

3737
};
38-
struct Integer : public Equiv {
39-
Integer(v8::Handle<v8::Integer> value) :
40-
Equiv(INT2FIX(value->IntegerValue())) {
41-
}
42-
};
38+
4339
public:
4440
static void Init();
4541

@@ -52,10 +48,10 @@ namespace rr {
5248
ScriptOrigin(v8::Isolate* isolate, v8::ScriptOrigin origin) :
5349
ScriptOrigin(new Container(
5450
Value(isolate, origin.ResourceName()),
55-
Integer(origin.ResourceLineOffset()),
56-
Integer(origin.ResourceColumnOffset()),
51+
Integer(isolate, origin.ResourceLineOffset()),
52+
Integer(isolate, origin.ResourceColumnOffset()),
5753
Bool(origin.Options().IsSharedCrossOrigin()),
58-
Integer(origin.ScriptID()),
54+
Integer(isolate, origin.ScriptID()),
5955
Bool(origin.Options().IsEmbedderDebugScript()),
6056
Value(isolate, origin.SourceMapUrl()),
6157
Bool(origin.Options().IsOpaque()))) {

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
// -*- mode: c++ -*-
2-
#ifndef RR_UINT32
3-
#define RR_UINT32
2+
#ifndef RR_UINT32_H
3+
#define RR_UINT32_H
44

5-
namespace rr {
65

6+
namespace rr {
77
/**
88
* Converts between Ruby `Number` and the C/C++ `uint32_t`.
99
*
1010
* This allows you to easily pass in `uint32_t` values whenever a
1111
* Ruby VALUE is expected (such as a method call) E.g.
1212
*
1313
* uint_32_t myInt = 5;
14-
* rb_funcall(UInt32(myInt), rb_intern("to_s")); //=> <String "5">
14+
* rb_funcall(Uint32_t(myInt), rb_intern("to_s")); //=> <String "5">
1515
*
1616
* It also converts a Ruby `VALUE` into its corresponding
1717
* `uint32_t`:
1818
*
19-
* uint_32_t myInt = UInt32(rb_eval_string("5")); //=> 5
19+
* uint_32_t myInt = Uint32_t(rb_eval_string("5")); //=> 5
2020
*
2121
* Like all `Equiv`s, it stores itself internally as a Ruby `VALUE`
2222
*/
23-
class UInt32 : public Equiv {
23+
class Uint32_t : public Equiv {
2424
public:
2525
/**
26-
* Construct a UInt32 from a Ruby `VALUE`
26+
* Construct a Uint32_t from a Ruby `VALUE`
2727
*/
28-
UInt32(VALUE val) : Equiv(val) {}
28+
Uint32_t(VALUE val) : Equiv(val) {}
2929

3030
/**
31-
* Construct a UInt32 from a `uint32_t` by converting it into its
31+
* Construct a Uint32_t from a `uint32_t` by converting it into its
3232
* corresponding `VALUE`.
3333
*/
34-
UInt32(uint32_t ui) : Equiv(UINT2NUM(ui)) {}
34+
Uint32_t(uint32_t ui) : Equiv(UINT2NUM(ui)) {}
3535

3636
/**
3737
* Coerce the Ruby `VALUE` into a `uint32_t`.

ext/v8/value.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace rr {
4545

4646
store(&Class);
4747

48-
rb_gc_register_address(&Empty);
48+
rb_gc_register_address(&Empty);
4949
}
5050

5151
VALUE Value::IsUndefined(VALUE self) {
@@ -171,21 +171,22 @@ namespace rr {
171171
}
172172

173173
if (handle->IsUint32()) {
174-
return UInt32(handle->Uint32Value());
174+
return Uint32(isolate, handle);
175175
}
176176

177177
if (handle->IsInt32()) {
178-
return INT2FIX(handle->Int32Value());
178+
return Int32(isolate, handle);
179+
}
180+
181+
if (handle->IsNumber()) {
182+
return Number(isolate, handle);
179183
}
180184

181185
if (handle->IsBoolean()) {
182186
return handle->BooleanValue() ? Qtrue : Qfalse;
183187
}
184188

185189
// TODO
186-
// if (handle->IsNumber()) {
187-
// return rb_float_new(handle->NumberValue());
188-
// }
189190

190191
if (handle->IsString()) {
191192
return String(isolate, handle->ToString());

0 commit comments

Comments
 (0)