support for JavaScript Date

This commit is contained in:
Charles Lowell 2012-06-07 10:02:10 -05:00
parent c35d1d1de5
commit 2dabbfe5e1
4 changed files with 32 additions and 0 deletions

18
ext/v8/date.cc Normal file
View File

@ -0,0 +1,18 @@
#include "rr.h"
namespace rr {
void Date::Init() {
ClassBuilder("Date", Value::Class).
defineSingletonMethod("New", &New).
defineMethod("NumberValue", &NumberValue).
store(&Class);
}
VALUE Date::New(VALUE self, VALUE time) {
return Value(v8::Date::New(NUM2DBL(time)));
}
VALUE Date::NumberValue(VALUE self) {
return rb_float_new(Date(self)->NumberValue());
}
}

View File

@ -21,6 +21,7 @@ extern "C" {
Object::Init();
Array::Init();
Function::Init();
Date::Init();
External::Init();
Script::Init();
Template::Init();

View File

@ -513,6 +513,16 @@ public:
inline Function(v8::Handle<v8::Function> function) : Ref<v8::Function>(function) {}
};
class Date : public Ref<v8::Date> {
public:
static void Init();
static VALUE New(VALUE self, VALUE time);
static VALUE NumberValue(VALUE self);
inline Date(VALUE value) : Ref<v8::Date>(value) {}
inline Date(v8::Handle<v8::Date> date) : Ref<v8::Date>(date) {}
};
class Signature : public Ref<v8::Signature> {
public:
static void Init();

View File

@ -182,6 +182,9 @@ Value::operator VALUE() {
if (handle->IsString()) {
return String(handle->ToString());
}
if (handle->IsDate()) {
return Date((v8::Handle<v8::Date>)v8::Date::Cast(*handle));
}
if (handle->IsObject()) {
return Object(handle->ToObject());
}