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)
|
def version=(version)
|
||||||
const = version.to_s.gsub('.', '_').upcase
|
const = version.to_s.gsub('.', '_').upcase
|
||||||
const = "VERSION_#{const}" if const[0, 7] != 'VERSION'
|
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)
|
const_value = JS::Context.const_get(const)
|
||||||
@native.setLanguageVersion(const_value)
|
@native.setLanguageVersion(const_value)
|
||||||
const_value
|
const_value
|
||||||
|
|
|
@ -87,11 +87,11 @@ class Java::OrgMozillaJavascript::ScriptableObject
|
||||||
|
|
||||||
# Delegate methods to JS object if possible when called from Ruby.
|
# Delegate methods to JS object if possible when called from Ruby.
|
||||||
def method_missing(name, *args)
|
def method_missing(name, *args)
|
||||||
s_name = name.to_s
|
name_str = name.to_s
|
||||||
if s_name[-1, 1] == '=' && args.size == 1 # writer -> JS put
|
if name_str[-1, 1] == '=' && args.size == 1 # writer -> JS put
|
||||||
self[ s_name[0...-1] ] = args[0]
|
self[ name_str[0...-1] ] = args[0]
|
||||||
else
|
else
|
||||||
if property = self[s_name]
|
if property = self[name_str]
|
||||||
if property.is_a?(Rhino::JS::Function)
|
if property.is_a?(Rhino::JS::Function)
|
||||||
begin
|
begin
|
||||||
context = Rhino::JS::Context.enter
|
context = Rhino::JS::Context.enter
|
||||||
|
@ -103,7 +103,7 @@ class Java::OrgMozillaJavascript::ScriptableObject
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if args.size > 0
|
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
|
end
|
||||||
Rhino.to_ruby property
|
Rhino.to_ruby property
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,7 +32,7 @@ module Rhino
|
||||||
ids = []
|
ids = []
|
||||||
unwrap.public_methods(false).each do |name|
|
unwrap.public_methods(false).each do |name|
|
||||||
name = name[0...-1] if name[-1, 1] == '=' # 'foo=' ... 'foo'
|
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)
|
ids << name unless ids.include?(name)
|
||||||
end
|
end
|
||||||
super.each { |id| ids.unshift(id) }
|
super.each { |id| ids.unshift(id) }
|
||||||
|
|
|
@ -4,7 +4,7 @@ module Rhino
|
||||||
|
|
||||||
def self.has(object, name, scope)
|
def self.has(object, name, scope)
|
||||||
if object.respond_to?(name.to_s) ||
|
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
|
return true
|
||||||
end
|
end
|
||||||
# try [](name) method :
|
# try [](name) method :
|
||||||
|
@ -15,10 +15,12 @@ module Rhino
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get(object, name, scope)
|
def self.get(object, name, scope)
|
||||||
if object.respond_to?(name_s = name.to_s)
|
name_sym = name.to_s.to_sym
|
||||||
method = object.method(name_s)
|
if object.respond_to?(name_sym)
|
||||||
|
method = object.method(name_sym)
|
||||||
if method.arity == 0 && # check if it is an attr_reader
|
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
|
begin
|
||||||
return Rhino.to_javascript(method.call, scope)
|
return Rhino.to_javascript(method.call, scope)
|
||||||
rescue => e
|
rescue => e
|
||||||
|
@ -27,7 +29,7 @@ module Rhino
|
||||||
else
|
else
|
||||||
return Function.wrap(method.unbind)
|
return Function.wrap(method.unbind)
|
||||||
end
|
end
|
||||||
elsif object.respond_to?("#{name}=")
|
elsif object.respond_to?(:"#{name}=")
|
||||||
return nil # it does have the property but is non readable
|
return nil # it does have the property but is non readable
|
||||||
end
|
end
|
||||||
# try [](name) method :
|
# try [](name) method :
|
||||||
|
@ -40,7 +42,7 @@ module Rhino
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.put(object, name, value)
|
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))
|
return object.send(set_name, Rhino.to_ruby(value))
|
||||||
end
|
end
|
||||||
# try []=(name, value) method :
|
# try []=(name, value) method :
|
||||||
|
|
|
@ -132,12 +132,12 @@ describe Rhino::To do
|
||||||
it "converts procs and methods into native functions" do
|
it "converts procs and methods into native functions" do
|
||||||
Rhino.to_javascript(lambda {|lhs,rhs| lhs * rhs}).tap do |f|
|
Rhino.to_javascript(lambda {|lhs,rhs| lhs * rhs}).tap do |f|
|
||||||
f.should be_kind_of(Rhino::JS::Function)
|
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
|
end
|
||||||
|
|
||||||
Rhino.to_javascript("foo,bar,baz".method(:split)).tap do |m|
|
Rhino.to_javascript("foo,bar,baz".method(:split)).tap do |f|
|
||||||
m.should be_kind_of(Rhino::JS::Function)
|
f.should be_kind_of(Rhino::JS::Function)
|
||||||
Rhino.to_ruby(m.call(nil, nil, nil, ',')).should == ['foo', 'bar', 'baz']
|
Rhino.to_ruby(f.call(nil, nil, nil, [','].to_java)).should == ['foo', 'bar', 'baz']
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue