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

Commit 1bd5c3b

Browse files
committed
implement date and related conversions
1 parent 7c293f0 commit 1bd5c3b

11 files changed

Lines changed: 69 additions & 26 deletions

File tree

ext/v8/date.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// -*- mode: c++ -*-
2+
#ifndef RR_DATE_H
3+
#define RR_DATE_H
4+
5+
namespace rr {
6+
class Date : public Ref<v8::Date> {
7+
public:
8+
Date(v8::Isolate* isolate, v8::Local<v8::Date> date) :
9+
Ref<v8::Date>(isolate, date) {}
10+
11+
static void Init() {
12+
ClassBuilder("Date", Object::Class).
13+
store(&Class);
14+
};
15+
};
16+
}
17+
18+
#endif /* RR_DATE_H */

ext/v8/init.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern "C" {
1919
Maybe::Init();
2020
Value::Init();
2121
Object::Init();
22+
Date::Init();
2223
Primitive::Init();
2324
Null::Init();
2425
Undefined::Init();

ext/v8/object.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,15 @@ namespace rr {
105105
if (handle->IsFunction()) {
106106
return Function(isolate, handle.As<v8::Function>());
107107
}
108-
109108
if (handle->IsArray()) {
110109
return Array(isolate, handle.As<v8::Array>());
111110
}
111+
if (handle->IsDate()) {
112+
return Date(isolate, handle.As<v8::Date>());
113+
}
112114

113115
// TODO: Enable this when the methods are implemented
114116
//
115-
// if (handle->IsDate()) {
116-
// // return Date(handle);
117-
// }
118117
//
119118
// if (handle->IsBooleanObject()) {
120119
// // return BooleanObject(handle);

ext/v8/rr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ inline VALUE not_implemented(const char* message) {
5252
#include "function.h"
5353

5454
#include "object.h"
55+
#include "date.h"
5556
#include "return-value.h"
5657
#include "property-callback.h"
5758
#include "property-callback-info.h"

ext/v8/value.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,6 @@ namespace rr {
193193
return Name(isolate, handle.As<v8::Name>());
194194
}
195195

196-
// TODO
197-
// if (handle->IsDate()) {
198-
// return Date((v8::Handle<v8::Date>)v8::Date::Cast(*handle));
199-
// }
200-
201196
if (handle->IsFunction()) {
202197
return Function(isolate, v8::Handle<v8::Function>::Cast(handle));
203198
}

lib/v8.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@
2626
# require 'v8/access/invocation'
2727
# require 'v8/access'
2828
require 'v8/object'
29+
require 'v8/date'
2930
# require 'v8/array'
30-
# require 'v8/function'
31+
require 'v8/function'

lib/v8/conversion.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ def to_ruby
3737
::V8::Object.new self
3838
end
3939
end
40+
41+
class Date
42+
def to_ruby
43+
::V8::Date.new self
44+
end
45+
end
46+
47+
class Function
48+
def to_ruby
49+
::V8::Function.new self
50+
end
51+
end
4052
end
4153

4254
class String
@@ -51,6 +63,13 @@ def to_v8(context)
5163
end
5264
end
5365

66+
class Symbol
67+
def to_v8(context)
68+
isolate = context.isolate.native
69+
V8::C::Symbol::For(context.isolate.native, V8::C::String::NewFromUtf8(isolate, to_s))
70+
end
71+
end
72+
5473
# for type in [TrueClass, FalseClass, NilClass, Float] do
5574
# type.class_eval do
5675
# include V8::Conversion::Primitive

lib/v8/date.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module V8
2+
class Date < V8::Object
3+
def value_of
4+
@context.to_ruby @native.ValueOf()
5+
end
6+
end
7+
end

lib/v8/function.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class V8::Function < V8::Object
2-
include V8::Error::Try
2+
#include V8::Error::Try
33

44
def initialize(native = nil)
55
super do
@@ -10,7 +10,7 @@ def initialize(native = nil)
1010
def methodcall(this, *args)
1111
@context.enter do
1212
this ||= @context.native.Global()
13-
@context.to_ruby try {native.Call(@context.to_v8(this), args.map {|a| @context.to_v8 a})}
13+
@context.to_ruby native.Call(@context.to_v8(this), args.map {|a| @context.to_v8 a})
1414
end
1515
end
1616

@@ -25,4 +25,4 @@ def new(*args)
2525
@context.to_ruby try {native.NewInstance(args.map {|a| @context.to_v8 a})}
2626
end
2727
end
28-
end
28+
end

lib/v8/object.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class V8::Object
22
include Enumerable
33
attr_reader :native
4-
alias_method :to_v8, :native
54

65
def initialize(native = nil)
76
@context = V8::Context.current or fail "tried to initialize a #{self.class} without being in an entered V8::Context"
@@ -11,7 +10,7 @@ def initialize(native = nil)
1110

1211
def [](key)
1312
@context.enter do
14-
@context.to_ruby @native.Get(@context.native, @context.to_v8(key)).FromJust()
13+
@context.to_ruby @native.Get(@context.native, @context.to_v8(key.to_s)).FromJust()
1514
end
1615
end
1716

@@ -52,6 +51,10 @@ def to_s
5251
end
5352
end
5453

54+
def to_v8(context)
55+
native
56+
end
57+
5558
def respond_to?(method)
5659
super or self[method] != nil
5760
end

0 commit comments

Comments
 (0)