1
0
Fork 0
mirror of https://github.com/rubyjs/therubyrhino synced 2023-03-27 23:21:34 -04:00

JS === for wrapped object (now unbound) methods; a better optimization_level writer and adding a reader as well

This commit is contained in:
kares 2012-01-10 17:24:12 +01:00
parent a6acb32a5b
commit 7e3c2e8e1d
4 changed files with 31 additions and 22 deletions

View file

@ -125,12 +125,22 @@ module Rhino
@factory.instruction_limit = limit
end
def optimization_level
@native.getOptimizationLevel
end
# Set the optimization level that this context will use. This is sometimes necessary
# in Rhino, if the bytecode size of the compiled javascript exceeds the 64KB limit.
# By using the -1 optimization level, you tell Rhino to run in interpretative mode,
# taking a hit to performance but escaping the Java bytecode limit.
def optimization_level=(level)
@native.setOptimizationLevel(level)
if @native.class.isValidOptimizationLevel(level)
@native.setOptimizationLevel(level)
level
else
@native.setOptimizationLevel(0)
nil
end
end
# Get the JS interpreter version.
@ -191,7 +201,7 @@ module Rhino
begin
str = @io.read(length)
rescue StandardError => e
raise java.io.IOException.new, "Failed reading from ruby IO object"
raise java.io.IOException.new("failed reading from ruby IO object")
end
if str.nil?
return -1

View file

@ -46,7 +46,7 @@ module Rhino
end
def self.access
@@access ||= Rhino::Ruby::DefaultAccess
@@access ||= Ruby::DefaultAccess
end
private
@ -63,7 +63,7 @@ module Rhino
# wrap an arbitrary (ruby) object
def self.wrap(object, scope = nil)
Rhino::Ruby.cache(object) { new(object, scope) }
Ruby.cache(object) { new(object, scope) }
end
TYPE = JS::TopLevel::Builtins::Object
@ -97,22 +97,14 @@ module Rhino
end
class Function < JS::BaseFunction
include JS::Wrapper
include Scriptable
# wrap a callable (Method/Proc)
def self.wrap(callable, scope = nil)
# NOTE: === seems 'correctly' impossible without having multiple
# instances of the 'same' wrapper function (even with an UnboundMethod),
# suppose :
#
# var foo1 = one.foo;
# var foo2 = two.foo;
# foo1 === foo2; // expect 'same' reference
# foo1(); foo2(); // one ref but different implicit 'this' objects
#
# returns different instances as obj1.method(:foo) != obj2.method(:foo)
#
new(callable, scope)
# NOTE: include JS::Wrapper & Ruby.cache(callable.to_s) guarantees ===
# in Rhino although if a bind Method gets passed it might get confusing
Ruby.cache(callable.to_s) { new(callable, scope) }
end
def initialize(callable, scope)
@ -166,10 +158,15 @@ module Rhino
end
rb_args = Rhino.args_to_ruby(args)
begin
result = @callable.call(*rb_args)
callable =
if @callable.is_a?(UnboundMethod)
@callable.bind(Rhino.to_ruby(this))
else
@callable
end
result = callable.call(*rb_args)
rescue => e
# ... correct wrapping thus it's try { } catch (e) works in JS :
raise Rhino::Ruby.wrap_error(e)
raise Ruby.wrap_error(e) # thus `try { } catch (e)` works in JS
end
Rhino.to_javascript(result, scope)
end
@ -181,7 +178,9 @@ module Rhino
# wrap a ruby class as as constructor function
def self.wrap(klass, scope = nil)
new(klass, scope)
# NOTE: caching here seems redundant since we implemented JS::Wrapper
# and a ruby class objects seems always the same ref under JRuby ...
Ruby.cache(klass) { new(klass, scope) }
end
def initialize(klass, scope)

View file

@ -25,7 +25,7 @@ module Rhino
raise Rhino::Ruby.wrap_error(e)
end
else
return Function.wrap(method)
return Function.wrap(method.unbind)
end
elsif object.respond_to?("#{name}=")
return nil # it does have the property but is non readable

View file

@ -24,7 +24,7 @@ module Rhino
raise Rhino::Ruby.wrap_error(e)
end
else
return Function.wrap(method)
return Function.wrap(method.unbind)
end
elsif object.respond_to?("#{name}=")
return nil