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

implement date and related conversions

This commit is contained in:
Charles Lowell 2015-07-30 21:15:10 +03:00
parent 7c293f0b68
commit 1bd5c3b26e
11 changed files with 69 additions and 26 deletions

18
ext/v8/date.h Normal file
View file

@ -0,0 +1,18 @@
// -*- mode: c++ -*-
#ifndef RR_DATE_H
#define RR_DATE_H
namespace rr {
class Date : public Ref<v8::Date> {
public:
Date(v8::Isolate* isolate, v8::Local<v8::Date> date) :
Ref<v8::Date>(isolate, date) {}
static void Init() {
ClassBuilder("Date", Object::Class).
store(&Class);
};
};
}
#endif /* RR_DATE_H */

View file

@ -19,6 +19,7 @@ extern "C" {
Maybe::Init();
Value::Init();
Object::Init();
Date::Init();
Primitive::Init();
Null::Init();
Undefined::Init();

View file

@ -105,16 +105,15 @@ namespace rr {
if (handle->IsFunction()) {
return Function(isolate, handle.As<v8::Function>());
}
if (handle->IsArray()) {
return Array(isolate, handle.As<v8::Array>());
}
if (handle->IsDate()) {
return Date(isolate, handle.As<v8::Date>());
}
// TODO: Enable this when the methods are implemented
//
// if (handle->IsDate()) {
// // return Date(handle);
// }
//
// if (handle->IsBooleanObject()) {
// // return BooleanObject(handle);

View file

@ -52,6 +52,7 @@ inline VALUE not_implemented(const char* message) {
#include "function.h"
#include "object.h"
#include "date.h"
#include "return-value.h"
#include "property-callback.h"
#include "property-callback-info.h"

View file

@ -193,11 +193,6 @@ namespace rr {
return Name(isolate, handle.As<v8::Name>());
}
// TODO
// if (handle->IsDate()) {
// return Date((v8::Handle<v8::Date>)v8::Date::Cast(*handle));
// }
if (handle->IsFunction()) {
return Function(isolate, v8::Handle<v8::Function>::Cast(handle));
}

View file

@ -26,5 +26,6 @@ require 'v8/conversion'
# require 'v8/access/invocation'
# require 'v8/access'
require 'v8/object'
require 'v8/date'
# require 'v8/array'
# require 'v8/function'
require 'v8/function'

View file

@ -37,6 +37,18 @@ module V8::C
::V8::Object.new self
end
end
class Date
def to_ruby
::V8::Date.new self
end
end
class Function
def to_ruby
::V8::Function.new self
end
end
end
class String
@ -51,6 +63,13 @@ class Fixnum
end
end
class Symbol
def to_v8(context)
isolate = context.isolate.native
V8::C::Symbol::For(context.isolate.native, V8::C::String::NewFromUtf8(isolate, to_s))
end
end
# for type in [TrueClass, FalseClass, NilClass, Float] do
# type.class_eval do
# include V8::Conversion::Primitive

7
lib/v8/date.rb Normal file
View file

@ -0,0 +1,7 @@
module V8
class Date < V8::Object
def value_of
@context.to_ruby @native.ValueOf()
end
end
end

View file

@ -1,5 +1,5 @@
class V8::Function < V8::Object
include V8::Error::Try
#include V8::Error::Try
def initialize(native = nil)
super do
@ -10,7 +10,7 @@ class V8::Function < V8::Object
def methodcall(this, *args)
@context.enter do
this ||= @context.native.Global()
@context.to_ruby try {native.Call(@context.to_v8(this), args.map {|a| @context.to_v8 a})}
@context.to_ruby native.Call(@context.to_v8(this), args.map {|a| @context.to_v8 a})
end
end
@ -25,4 +25,4 @@ class V8::Function < V8::Object
@context.to_ruby try {native.NewInstance(args.map {|a| @context.to_v8 a})}
end
end
end
end

View file

@ -1,7 +1,6 @@
class V8::Object
include Enumerable
attr_reader :native
alias_method :to_v8, :native
def initialize(native = nil)
@context = V8::Context.current or fail "tried to initialize a #{self.class} without being in an entered V8::Context"
@ -11,7 +10,7 @@ class V8::Object
def [](key)
@context.enter do
@context.to_ruby @native.Get(@context.native, @context.to_v8(key)).FromJust()
@context.to_ruby @native.Get(@context.native, @context.to_v8(key.to_s)).FromJust()
end
end
@ -52,6 +51,10 @@ class V8::Object
end
end
def to_v8(context)
native
end
def respond_to?(method)
super or self[method] != nil
end

View file

@ -59,17 +59,16 @@ describe "V8::Context" do
@cxt['nativeUtf8'].should == "Σὲ γνωρίζω ἀπὸ τὴν κόψη"
end
# xit "translates JavaScript dates properly into ruby Time objects" do
# now = Time.now
# @cxt.eval('new Date()').tap do |time|
# time.should be_kind_of(Time)
# time.year.should == now.year
# time.day.should == now.day
# time.month.should == now.month
# time.min.should == now.min
# time.sec.should == now.sec
# end
# end
it "translates JavaScript dates objects" do
now = Time.now
@cxt.eval('new Date()').tap do |time|
expect(time.getDate().to_i).to eql now.day
expect(time.getFullYear().to_i).to eql now.year
expect(time.getMonth().to_i).to eql now.month - 1
expect(time.getMinutes().to_i).to eql now.min
expect(time.getSeconds().to_i).to eql now.sec
end
end
it "can pass objects back to ruby" do
@cxt.eval("({foo: 'bar', baz: 'bang', '5': 5, embedded: {badda: 'bing'}})").tap do |object|