|
| 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 */ |
0 commit comments