1
0
Fork 0
mirror of https://github.com/rails/execjs synced 2023-03-27 23:21:20 -04:00

Merge branch 'master' into encoded-utf8

This commit is contained in:
Joshua Peek 2011-08-03 11:31:39 -05:00
commit b290d70b75
4 changed files with 15 additions and 5 deletions

View file

@ -1,4 +1,5 @@
require File.expand_path("../lib/execjs/version.rb", __FILE__)
$:.unshift File.expand_path('..', __FILE__)
require 'execjs/version'
Gem::Specification.new do |s|
s.name = "execjs"

View file

@ -7,7 +7,7 @@ module ExecJS
end
def exec(source, options = {})
souce = source.encode('UTF-8') if source.respond_to?(:encode)
source = source.encode('UTF-8') if source.respond_to?(:encode)
if /\S/ =~ source
eval "(function(){#{source}})()", options
@ -15,7 +15,7 @@ module ExecJS
end
def eval(source, options = {})
souce = source.encode('UTF-8') if source.respond_to?(:encode)
source = source.encode('UTF-8') if source.respond_to?(:encode)
if /\S/ =~ source
unbox @runtime.evaluate("(#{source})")

View file

@ -42,14 +42,21 @@ module ExecJS
end
def unbox(value)
case value
case value = ::Rhino::To.ruby(value)
when ::Rhino::NativeFunction
nil
when ::Rhino::NativeObject
value.inject({}) do |vs, (k, v)|
vs[k] = unbox(v) unless v.is_a?(::Rhino::NativeFunction)
case v
when ::Rhino::NativeFunction, ::Rhino::J::Function
nil
else
vs[k] = unbox(v)
end
vs
end
when Array
value.map { |v| unbox(v) }
else
value
end

View file

@ -30,8 +30,10 @@ class TestRuntime < Test::Unit::TestCase
assert_equal 0, @runtime.eval("0")
assert_equal true, @runtime.eval("true")
assert_equal [1, 2], @runtime.eval("[1, 2]")
assert_equal [1, nil], @runtime.eval("[1, function() {}]")
assert_equal "hello", @runtime.eval("'hello'")
assert_equal({"a"=>1,"b"=>2}, @runtime.eval("{a:1,b:2}"))
assert_equal({"a"=>true}, @runtime.eval("{a:true,b:function (){}}"))
assert_equal "café", @runtime.eval("'café'")
assert_equal "", @runtime.eval('"☃"')
assert_equal "", @runtime.eval('"\u2603"')