1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

add support for javascript Date as ruby Time, and vice-versa.

This commit is contained in:
Charles Lowell 2010-05-25 09:40:33 +03:00
parent 8f41dfef8d
commit ebd8b8c3cf
6 changed files with 42 additions and 1 deletions

View file

@ -5,6 +5,7 @@
#include "v8_func.h"
#include "v8_array.h"
#include "v8_str.h"
#include "v8_date.h"
#include "v8_msg.h"
using namespace v8;
@ -64,6 +65,9 @@ VALUE rr_v82rb(Handle<Value> value) {
if (value->IsArray()) {
return rr_reflect_v8_array(value);
}
if (value->IsDate()) {
return rr_reflect_v8_date(value);
}
if (value->IsObject()) {
return rr_reflect_v8_object(value);
}

View file

@ -5,6 +5,7 @@
#include "v8_array.h"
#include "v8_msg.h"
#include "v8_func.h"
#include "v8_date.h"
#include "v8_script.h"
#include "v8_template.h"
#include "v8_try_catch.h"
@ -28,6 +29,7 @@ extern "C" {
rr_init_obj();
rr_init_func();
rr_init_v8_array();
rr_init_v8_date();
rr_init_msg();
rr_init_v8_try_catch();
rr_init_v8_callbacks();

26
ext/v8/v8_date.cpp Normal file
View file

@ -0,0 +1,26 @@
#include "rr.h"
#include "v8_date.h"
#include "v8_value.h"
#include "v8_ref.h"
using namespace v8;
namespace {
VALUE DateClass;
VALUE New(VALUE self, VALUE time) {
HandleScope scope;
return rr_v8_ref_create(self, Date::New(NUM2DBL(rb_funcall(time, rb_intern("to_f"), 0))));
}
}
void rr_init_v8_date() {
DateClass = rr_define_class("Date", rr_cV8_C_Value);
rr_define_singleton_method(DateClass, "New", New, 1);
}
VALUE rr_reflect_v8_date(Handle<Value> value) {
Local<Date> date(Date::Cast(*value));
return rr_v8_ref_create(DateClass, date);
}

6
ext/v8/v8_date.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef _RR_V8_DATE_
#define _RR_V8_DATE_
void rr_init_v8_date();
VALUE rr_reflect_v8_date(v8::Handle<v8::Value> value);
#endif

View file

@ -13,6 +13,7 @@ module V8
when V8::C::Array then V8::Array.new(value)
when V8::C::Object then V8::Object.new(value)
when V8::C::String then value.Utf8Value()
when V8::C::Date then Time.at(value.NumberValue())
else
value
end
@ -50,6 +51,8 @@ module V8
o.Set(To.v8(key), To.v8(value))
end
end
when ::Time
C::Date::New(value)
when nil,Numeric
value
else

@ -1 +1 @@
Subproject commit 4cfdb1cd934d27d8e8d38bb99871036884a26d39
Subproject commit b31ad73b452ca016898bed475c896e2f741d8f82