mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
1.9 compatibility fixes
This commit is contained in:
parent
98cc3d8e8b
commit
3f68bb00dc
5 changed files with 19 additions and 17 deletions
|
@ -202,7 +202,7 @@ module Rhino
|
|||
def version=(version)
|
||||
const = version.to_s.gsub('.', '_').upcase
|
||||
const = "VERSION_#{const}" if const[0, 7] != 'VERSION'
|
||||
if JS::Context.constants.include?(const)
|
||||
if JS::Context.constants.find { |c| c.to_s == const }
|
||||
const_value = JS::Context.const_get(const)
|
||||
@native.setLanguageVersion(const_value)
|
||||
const_value
|
||||
|
|
|
@ -87,11 +87,11 @@ class Java::OrgMozillaJavascript::ScriptableObject
|
|||
|
||||
# Delegate methods to JS object if possible when called from Ruby.
|
||||
def method_missing(name, *args)
|
||||
s_name = name.to_s
|
||||
if s_name[-1, 1] == '=' && args.size == 1 # writer -> JS put
|
||||
self[ s_name[0...-1] ] = args[0]
|
||||
name_str = name.to_s
|
||||
if name_str[-1, 1] == '=' && args.size == 1 # writer -> JS put
|
||||
self[ name_str[0...-1] ] = args[0]
|
||||
else
|
||||
if property = self[s_name]
|
||||
if property = self[name_str]
|
||||
if property.is_a?(Rhino::JS::Function)
|
||||
begin
|
||||
context = Rhino::JS::Context.enter
|
||||
|
@ -103,7 +103,7 @@ class Java::OrgMozillaJavascript::ScriptableObject
|
|||
end
|
||||
else
|
||||
if args.size > 0
|
||||
raise ArgumentError, "can't #{name}(#{args.join(', ')}) as '#{name}' is a property"
|
||||
raise ArgumentError, "can't call '#{name_str}' with args: #{args.inspect} as it's a property"
|
||||
end
|
||||
Rhino.to_ruby property
|
||||
end
|
||||
|
|
|
@ -32,7 +32,7 @@ module Rhino
|
|||
ids = []
|
||||
unwrap.public_methods(false).each do |name|
|
||||
name = name[0...-1] if name[-1, 1] == '=' # 'foo=' ... 'foo'
|
||||
name = name.to_java # java.lang.String
|
||||
name = name.to_s.to_java # java.lang.String
|
||||
ids << name unless ids.include?(name)
|
||||
end
|
||||
super.each { |id| ids.unshift(id) }
|
||||
|
|
|
@ -4,7 +4,7 @@ module Rhino
|
|||
|
||||
def self.has(object, name, scope)
|
||||
if object.respond_to?(name.to_s) ||
|
||||
object.respond_to?("#{name}=") # might have a writer but no reader
|
||||
object.respond_to?(:"#{name}=") # might have a writer but no reader
|
||||
return true
|
||||
end
|
||||
# try [](name) method :
|
||||
|
@ -15,10 +15,12 @@ module Rhino
|
|||
end
|
||||
|
||||
def self.get(object, name, scope)
|
||||
if object.respond_to?(name_s = name.to_s)
|
||||
method = object.method(name_s)
|
||||
name_sym = name.to_s.to_sym
|
||||
if object.respond_to?(name_sym)
|
||||
method = object.method(name_sym)
|
||||
if method.arity == 0 && # check if it is an attr_reader
|
||||
( object.respond_to?("#{name}=") || object.instance_variables.include?("@#{name}") )
|
||||
( object.respond_to?(:"#{name}=") ||
|
||||
object.instance_variables.find { |var| var.to_sym == :"@#{name}" } )
|
||||
begin
|
||||
return Rhino.to_javascript(method.call, scope)
|
||||
rescue => e
|
||||
|
@ -27,7 +29,7 @@ module Rhino
|
|||
else
|
||||
return Function.wrap(method.unbind)
|
||||
end
|
||||
elsif object.respond_to?("#{name}=")
|
||||
elsif object.respond_to?(:"#{name}=")
|
||||
return nil # it does have the property but is non readable
|
||||
end
|
||||
# try [](name) method :
|
||||
|
@ -40,7 +42,7 @@ module Rhino
|
|||
end
|
||||
|
||||
def self.put(object, name, value)
|
||||
if object.respond_to?(set_name = "#{name}=")
|
||||
if object.respond_to?(set_name = :"#{name}=")
|
||||
return object.send(set_name, Rhino.to_ruby(value))
|
||||
end
|
||||
# try []=(name, value) method :
|
||||
|
|
|
@ -132,12 +132,12 @@ describe Rhino::To do
|
|||
it "converts procs and methods into native functions" do
|
||||
Rhino.to_javascript(lambda {|lhs,rhs| lhs * rhs}).tap do |f|
|
||||
f.should be_kind_of(Rhino::JS::Function)
|
||||
f.call(nil, nil, nil, [7,6]).should be(42)
|
||||
f.call(nil, nil, nil, [7, 6].to_java).should be(42)
|
||||
end
|
||||
|
||||
Rhino.to_javascript("foo,bar,baz".method(:split)).tap do |m|
|
||||
m.should be_kind_of(Rhino::JS::Function)
|
||||
Rhino.to_ruby(m.call(nil, nil, nil, ',')).should == ['foo', 'bar', 'baz']
|
||||
Rhino.to_javascript("foo,bar,baz".method(:split)).tap do |f|
|
||||
f.should be_kind_of(Rhino::JS::Function)
|
||||
Rhino.to_ruby(f.call(nil, nil, nil, [','].to_java)).should == ['foo', 'bar', 'baz']
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue