2012-01-06 12:29:22 -05:00
|
|
|
module Rhino
|
|
|
|
module Ruby
|
|
|
|
module DefaultAccess
|
|
|
|
|
2012-01-07 11:01:38 -05:00
|
|
|
def self.has(object, name, scope)
|
2012-01-06 12:29:22 -05:00
|
|
|
if object.respond_to?(name.to_s) ||
|
2012-01-07 11:01:38 -05:00
|
|
|
object.respond_to?("#{name}=")
|
2012-01-06 12:29:22 -05:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
# try [](name) method :
|
|
|
|
if object.respond_to?(:'[]') && object.method(:'[]').arity == 1
|
|
|
|
return true if object[name]
|
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2012-01-07 11:01:38 -05:00
|
|
|
def self.get(object, name, scope)
|
2012-01-06 12:29:22 -05:00
|
|
|
if object.respond_to?(name_s = name.to_s)
|
|
|
|
method = object.method(name_s)
|
2012-01-07 11:01:38 -05:00
|
|
|
if method.arity == 0
|
2012-01-06 12:29:22 -05:00
|
|
|
begin
|
2012-01-07 11:01:38 -05:00
|
|
|
return Rhino.to_javascript(method.call, scope)
|
2012-01-06 12:29:22 -05:00
|
|
|
rescue => e
|
2012-01-07 11:01:38 -05:00
|
|
|
raise Rhino::Ruby.wrap_error(e)
|
2012-01-06 12:29:22 -05:00
|
|
|
end
|
|
|
|
else
|
2012-01-10 11:24:12 -05:00
|
|
|
return Function.wrap(method.unbind)
|
2012-01-06 12:29:22 -05:00
|
|
|
end
|
|
|
|
elsif object.respond_to?("#{name}=")
|
2012-01-07 11:01:38 -05:00
|
|
|
return nil
|
2012-01-06 12:29:22 -05:00
|
|
|
end
|
|
|
|
# try [](name) method :
|
|
|
|
if object.respond_to?(:'[]') && object.method(:'[]').arity == 1
|
|
|
|
if value = object[name]
|
2012-01-07 11:01:38 -05:00
|
|
|
return Rhino.to_javascript(value, scope)
|
2012-01-06 12:29:22 -05:00
|
|
|
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
|