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

introduce an access module for routing (ruby) Scriptable calls

This commit is contained in:
kares 2012-01-06 18:29:22 +01:00
parent cb2016334e
commit f4b462216e
4 changed files with 82 additions and 45 deletions

View file

@ -25,4 +25,5 @@ require 'rhino/context'
require 'rhino/error'
require 'rhino/rhino_ext'
require 'rhino/ruby'
require 'rhino/ruby/access'
require 'rhino/deprecations'

View file

@ -8,61 +8,19 @@ module Rhino
# override Object Scriptable#get(String name, Scriptable start);
# override Object Scriptable#get(int index, Scriptable start);
def get(name, start)
if name.is_a?(String)
if unwrap.respond_to?(name)
method = unwrap.method(name)
if method.arity == 0 && # check if it is an attr_reader
( unwrap.respond_to?("#{name}=") || unwrap.instance_variables.include?("@#{name}") )
begin
return Rhino.to_javascript(method.call, self)
rescue => e
raise Function.wrap_error(e)
end
else
return Function.wrap(unwrap.method(name))
end
elsif unwrap.respond_to?("#{name}=")
return nil # it does have the property but is non readable
end
end
# try [](name) method :
if unwrap.respond_to?(:'[]') && unwrap.method(:'[]').arity == 1
if value = unwrap[name]
return Rhino.to_javascript(value, self)
end
end
super
access.get(unwrap, name) { super }
end
# override boolean Scriptable#has(String name, Scriptable start);
# override boolean Scriptable#has(int index, Scriptable start);
def has(name, start)
if name.is_a?(String)
if unwrap.respond_to?(name) ||
unwrap.respond_to?("#{name}=") # might have a writer but no reader
return true
end
end
# try [](name) method :
if unwrap.respond_to?(:'[]') && unwrap.method(:'[]').arity == 1
return true if unwrap[name]
end
super
access.has(unwrap, name) { super }
end
# override void Scriptable#put(String name, Scriptable start, Object value);
# override void Scriptable#put(int index, Scriptable start, Object value);
def put(name, start, value)
if name.is_a?(String)
if unwrap.respond_to?(set_name = "#{name}=")
return unwrap.send(set_name, Rhino.to_ruby(value))
end
end
# try []=(name, value) method :
if unwrap.respond_to?(:'[]=') && unwrap.method(:'[]=').arity == 2
return unwrap[name] = Rhino.to_ruby(value)
end
super
access.put(unwrap, name, value) { super }
end
# override Object[] Scriptable#getIds();
@ -77,6 +35,22 @@ module Rhino
ids.to_java
end
@@access = nil
def self.access=(access)
@@access = access
end
def self.access
@@access ||= Rhino::Ruby::DefaultAccess
end
private
def access
Scriptable.access
end
end
class Object < JS::ScriptableObject

7
lib/rhino/ruby/access.rb Normal file
View file

@ -0,0 +1,7 @@
module Rhino
module Ruby
autoload :DefaultAccess, "rhino/ruby/default_access"
end
end

View file

@ -0,0 +1,55 @@
module Rhino
module Ruby
module DefaultAccess
def self.has(object, name)
if object.respond_to?(name.to_s) ||
object.respond_to?("#{name}=") # might have a writer but no reader
return true
end
# try [](name) method :
if object.respond_to?(:'[]') && object.method(:'[]').arity == 1
return true if object[name]
end
yield
end
def self.get(object, name)
if object.respond_to?(name_s = name.to_s)
method = object.method(name_s)
if method.arity == 0 && # check if it is an attr_reader
( object.respond_to?("#{name}=") || object.instance_variables.include?("@#{name}") )
begin
return Rhino.to_javascript(method.call, self)
rescue => e
raise Function.wrap_error(e)
end
else
return Function.wrap(method)
end
elsif object.respond_to?("#{name}=")
return nil # it does have the property but is non readable
end
# try [](name) method :
if object.respond_to?(:'[]') && object.method(:'[]').arity == 1
if value = object[name]
return Rhino.to_javascript(value, self)
end
end
yield
end
def self.put(object, name, value)
if object.respond_to?(set_name = "#{name}=")
return object.send(set_name, Rhino.to_ruby(value))
end
# try []=(name, value) method :
if object.respond_to?(:'[]=') && object.method(:'[]=').arity == 2
return object[name] = Rhino.to_ruby(value)
end
yield
end
end
end
end