Skip to content

Commit 3652c97

Browse files
committed
FEATURE: add support for activesupport dates
1 parent f4745f3 commit 3652c97

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

ext/mini_racer_extension/mini_racer_extension.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,20 @@ static int serialize1(Ser *s, VALUE refs, VALUE v)
604604
case T_STRING:
605605
add_string(s, v);
606606
break;
607+
case T_OBJECT:
608+
// this is somewhat wide, but we have active support which creates
609+
// entirely new objects
610+
if (rb_respond_to(v, rb_intern("to_time"))) {
611+
v = rb_funcall(v, rb_intern("to_time"), 0);
612+
}
613+
if (rb_obj_is_kind_of(v, rb_cTime)) {
614+
struct timeval tv = rb_time_timeval(v);
615+
ser_date(s, tv.tv_sec*1e3 + tv.tv_usec/1e3);
616+
} else {
617+
snprintf(s->err, sizeof(s->err), "unsupported type %s", rb_class2name(CLASS_OF(v)));
618+
return -1;
619+
}
620+
break;
607621
default:
608622
snprintf(s->err, sizeof(s->err), "unsupported type %x", TYPE(v));
609623
return -1;

mini_racer.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Gem::Specification.new do |spec|
3939
spec.add_development_dependency "rake", ">= 12.3.3"
4040
spec.add_development_dependency "minitest", "~> 5.0"
4141
spec.add_development_dependency "rake-compiler"
42+
spec.add_development_dependency "activesupport", "> 6"
4243
spec.add_development_dependency "m"
4344

4445
spec.add_dependency "libv8-node", MiniRacer::LIBV8_NODE_VERSION

test/mini_racer_test.rb

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,7 @@ def test_return_hash
262262
def test_date_nan
263263
# NoMethodError: undefined method `source_location' for "<internal:core>
264264
# core/float.rb:114:in `to_i'":Thread::Backtrace::Location
265-
if RUBY_ENGINE == "truffleruby"
266-
skip "TruffleRuby bug"
267-
end
265+
skip "TruffleRuby bug" if RUBY_ENGINE == "truffleruby"
268266
context = MiniRacer::Context.new
269267
assert_raises(RangeError) { context.eval("new Date(NaN)") } # should not crash process
270268
end
@@ -893,11 +891,14 @@ def test_wasm_ref
893891
end
894892
context = MiniRacer::Context.new
895893
expected = {}
896-
actual = context.eval("
894+
actual =
895+
context.eval(
896+
"
897897
var b = [0,97,115,109,1,0,0,0,1,26,5,80,0,95,0,80,0,95,1,127,0,96,0,1,110,96,1,100,2,1,111,96,0,1,100,3,3,4,3,3,2,4,7,26,2,12,99,114,101,97,116,101,83,116,114,117,99,116,0,1,7,114,101,102,70,117,110,99,0,2,9,5,1,3,0,1,0,10,23,3,8,0,32,0,20,2,251,27,11,7,0,65,12,251,0,1,11,4,0,210,0,11,0,44,4,110,97,109,101,1,37,3,0,11,101,120,112,111,114,116,101,100,65,110,121,1,12,99,114,101,97,116,101,83,116,114,117,99,116,2,7,114,101,102,70,117,110,99]
898898
var o = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array(b))).exports
899899
o.refFunc()(o.createStruct) // exotic object
900-
")
900+
"
901+
)
901902
assert_equal expected, actual
902903
end
903904

@@ -923,7 +924,7 @@ def test_proxy_support
923924

924925
def test_proxy_uncloneable
925926
context = MiniRacer::Context.new()
926-
expected = {"x" => 42}
927+
expected = { "x" => 42 }
927928
assert_equal expected, context.eval(<<~JS)
928929
const o = {x: 42}
929930
const p = new Proxy(o, {})
@@ -1031,9 +1032,7 @@ def test_forking
10311032
skip "TruffleRuby forking is not supported"
10321033
else
10331034
`bundle exec ruby test/test_forking.rb`
1034-
if $?.exitstatus != 0
1035-
assert false, "forking test failed"
1036-
end
1035+
assert false, "forking test failed" if $?.exitstatus != 0
10371036
end
10381037
end
10391038

@@ -1053,19 +1052,22 @@ def test_poison
10531052

10541053
def test_map
10551054
context = MiniRacer::Context.new
1056-
expected = {"x" => 42, "y" => 43}
1055+
expected = { "x" => 42, "y" => 43 }
10571056
assert_equal expected, context.eval("new Map([['x', 42], ['y', 43]])")
10581057
if RUBY_ENGINE == "truffleruby"
10591058
# See https://github.com/rubyjs/mini_racer/pull/325#discussion_r1907187166
10601059
expected = [["x", 42], ["y", 43]]
10611060
else
10621061
expected = ["x", 42, "y", 43]
10631062
end
1064-
assert_equal expected, context.eval("new Map([['x', 42], ['y', 43]]).entries()")
1065-
expected = ["x", "y"]
1066-
assert_equal expected, context.eval("new Map([['x', 42], ['y', 43]]).keys()")
1063+
assert_equal expected,
1064+
context.eval("new Map([['x', 42], ['y', 43]]).entries()")
1065+
expected = %w[x y]
1066+
assert_equal expected,
1067+
context.eval("new Map([['x', 42], ['y', 43]]).keys()")
10671068
expected = [[42], [43]]
1068-
assert_equal expected, context.eval("new Map([['x', [42]], ['y', [43]]]).values()")
1069+
assert_equal expected,
1070+
context.eval("new Map([['x', [42]], ['y', [43]]]).values()")
10691071
end
10701072

10711073
def test_regexp_string_iterator
@@ -1083,17 +1085,9 @@ def test_regexp_string_iterator
10831085
def test_function_property
10841086
context = MiniRacer::Context.new
10851087
if RUBY_ENGINE == "truffleruby"
1086-
expected = {
1087-
"m" => {1 => 2, 3 => 4},
1088-
"s" => {},
1089-
"x" => 42,
1090-
}
1088+
expected = { "m" => { 1 => 2, 3 => 4 }, "s" => {}, "x" => 42 }
10911089
else
1092-
expected = {
1093-
"m" => {1 => 2, 3 => 4},
1094-
"s" => [5, 7, 11, 13],
1095-
"x" => 42,
1096-
}
1090+
expected = { "m" => { 1 => 2, 3 => 4 }, "s" => [5, 7, 11, 13], "x" => 42 }
10971091
end
10981092
script = <<~JS
10991093
({
@@ -1106,6 +1100,16 @@ def test_function_property
11061100
assert_equal expected, context.eval(script)
11071101
end
11081102

1103+
def test_dates_from_active_support
1104+
require "active_support"
1105+
require "active_support/time"
1106+
context = MiniRacer::Context.new
1107+
Time.zone = "UTC"
1108+
time = Time.current
1109+
context.attach("f", proc { time })
1110+
assert_in_delta time.to_f, context.call("f").to_f, 0.001
1111+
end
1112+
11091113
def test_string_encoding
11101114
context = MiniRacer::Context.new
11111115
assert_equal "ä", context.eval("'ä'")
@@ -1124,7 +1128,7 @@ def test_object_ref
11241128
context = MiniRacer::Context.new
11251129
context.eval("function f(o) { return o }")
11261130
expected = {}
1127-
expected["a"] = expected["b"] = {"x" => 42}
1131+
expected["a"] = expected["b"] = { "x" => 42 }
11281132
actual = context.call("f", expected)
11291133
assert_equal actual, expected
11301134
end

0 commit comments

Comments
 (0)