2012-01-06 12:29:22 -05:00
|
|
|
module Rhino
|
|
|
|
module Ruby
|
2012-01-07 11:01:38 -05:00
|
|
|
|
2012-01-06 12:29:22 -05:00
|
|
|
autoload :DefaultAccess, "rhino/ruby/default_access"
|
2012-01-07 11:01:38 -05:00
|
|
|
autoload :AttributeAccess, "rhino/ruby/attribute_access"
|
2012-01-06 12:29:22 -05:00
|
|
|
|
2012-04-20 14:24:43 -04:00
|
|
|
class AccessBase
|
|
|
|
|
|
|
|
def has(object, name, scope)
|
|
|
|
# try [](name) method :
|
|
|
|
if object.respond_to?(:'[]') && object.method(:'[]').arity == 1
|
2012-04-21 13:56:05 -04:00
|
|
|
unless internal?(name)
|
|
|
|
value = object.[](name) { return true }
|
|
|
|
return true unless value.nil?
|
|
|
|
end
|
2012-04-20 14:24:43 -04:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(object, name, scope)
|
|
|
|
# try [](name) method :
|
|
|
|
if object.respond_to?(:'[]') && object.method(:'[]').arity == 1
|
2012-04-21 13:51:47 -04:00
|
|
|
value = begin
|
|
|
|
object[name]
|
|
|
|
rescue LocalJumpError
|
|
|
|
nil
|
2012-04-21 13:56:05 -04:00
|
|
|
end unless internal?(name)
|
2012-04-21 13:51:47 -04:00
|
|
|
return Rhino.to_javascript(value, scope) unless value.nil?
|
2012-04-20 14:24:43 -04:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
def put(object, name, value)
|
|
|
|
# try []=(name, value) method :
|
|
|
|
if object.respond_to?(:'[]=') && object.method(:'[]=').arity == 2
|
2012-04-21 13:51:47 -04:00
|
|
|
rb_value = Rhino.to_ruby(value)
|
|
|
|
begin
|
|
|
|
return object[name] = rb_value
|
|
|
|
rescue LocalJumpError
|
2012-04-21 13:56:05 -04:00
|
|
|
end unless internal?(name)
|
2012-04-20 14:24:43 -04:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2012-04-21 13:56:05 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
UNDERSCORES = '__'.freeze
|
|
|
|
|
|
|
|
def internal?(name) # e.g. '__iterator__', '__proto__'
|
|
|
|
name.is_a?(String) &&
|
|
|
|
name[0..1] == UNDERSCORES && name[-2..-1] == UNDERSCORES
|
|
|
|
end
|
|
|
|
|
2012-04-20 14:24:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module DeprecatedAccess
|
|
|
|
|
|
|
|
def has(object, name, scope, &block)
|
|
|
|
Rhino.warn "[DEPRECATION] `#{self.name}.has` is deprecated, please sub-class #{self.name} instead."
|
|
|
|
instance.has(object, name, scope, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(object, name, scope, &block)
|
|
|
|
Rhino.warn "[DEPRECATION] `#{self.name}.get` is deprecated, please sub-class #{self.name} instead."
|
|
|
|
instance.get(object, name, scope, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def put(object, name, value, &block)
|
|
|
|
Rhino.warn "[DEPRECATION] `#{self.name}.put` is deprecated, please sub-class #{self.name} instead."
|
|
|
|
instance.put(object, name, value, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def instance
|
|
|
|
@instance ||= self.new
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-01-06 12:29:22 -05:00
|
|
|
end
|
|
|
|
end
|