From f4b462216e1d305567e08f17c2d9930832b5a2f3 Mon Sep 17 00:00:00 2001 From: kares Date: Fri, 6 Jan 2012 18:29:22 +0100 Subject: [PATCH] introduce an access module for routing (ruby) Scriptable calls --- lib/rhino.rb | 1 + lib/rhino/ruby.rb | 64 ++++++++++---------------------- lib/rhino/ruby/access.rb | 7 ++++ lib/rhino/ruby/default_access.rb | 55 +++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 45 deletions(-) create mode 100644 lib/rhino/ruby/access.rb create mode 100644 lib/rhino/ruby/default_access.rb diff --git a/lib/rhino.rb b/lib/rhino.rb index c65212f..63af807 100644 --- a/lib/rhino.rb +++ b/lib/rhino.rb @@ -25,4 +25,5 @@ require 'rhino/context' require 'rhino/error' require 'rhino/rhino_ext' require 'rhino/ruby' +require 'rhino/ruby/access' require 'rhino/deprecations' diff --git a/lib/rhino/ruby.rb b/lib/rhino/ruby.rb index c1f882c..7c47a4b 100644 --- a/lib/rhino/ruby.rb +++ b/lib/rhino/ruby.rb @@ -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 diff --git a/lib/rhino/ruby/access.rb b/lib/rhino/ruby/access.rb new file mode 100644 index 0000000..c5e5734 --- /dev/null +++ b/lib/rhino/ruby/access.rb @@ -0,0 +1,7 @@ +module Rhino + module Ruby + + autoload :DefaultAccess, "rhino/ruby/default_access" + + end +end \ No newline at end of file diff --git a/lib/rhino/ruby/default_access.rb b/lib/rhino/ruby/default_access.rb new file mode 100644 index 0000000..e29f574 --- /dev/null +++ b/lib/rhino/ruby/default_access.rb @@ -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 \ No newline at end of file