2010-05-26 12:54:56 -04:00
|
|
|
require 'set'
|
2010-05-22 05:12:42 -04:00
|
|
|
module V8
|
2010-06-08 17:32:31 -04:00
|
|
|
class Access
|
2010-08-28 11:46:30 -04:00
|
|
|
def initialize(portal)
|
|
|
|
@classes = Hash.new do |h, cls|
|
|
|
|
h[cls] = template(cls).tap do |t|
|
|
|
|
portal.setuptemplate(t.InstanceTemplate())
|
2010-06-08 17:32:31 -04:00
|
|
|
if cls.name && cls.name =~ /(::)?(\w+?)$/
|
2010-06-08 17:59:22 -04:00
|
|
|
t.SetClassName(C::String::NewSymbol("rb::" + $2))
|
2010-06-08 17:32:31 -04:00
|
|
|
else
|
|
|
|
t.SetClassName("Ruby")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-08-28 11:46:30 -04:00
|
|
|
@impl = RubyAccess.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](cls)
|
|
|
|
@classes[cls]
|
2010-06-08 17:32:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def template(cls)
|
|
|
|
C::FunctionTemplate::New() do |arguments|
|
|
|
|
unless arguments.Length() == 1 && arguments[0].kind_of?(C::External)
|
|
|
|
C::ThrowException(C::Exception::Error(C::String::New("cannot call native constructor from javascript")))
|
|
|
|
else
|
|
|
|
arguments.This().tap do |this|
|
|
|
|
this.SetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyObject"), arguments[0])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-28 12:22:33 -04:00
|
|
|
def method_missing(name, *args, &blk)
|
|
|
|
@impl.send(name, *args, &blk)
|
2010-08-06 11:23:13 -04:00
|
|
|
end
|
2010-07-08 11:10:19 -04:00
|
|
|
end
|
|
|
|
|
2010-08-07 01:53:08 -04:00
|
|
|
class RubyAccess
|
|
|
|
def get(obj, name, &dontintercept)
|
2010-06-28 12:02:54 -04:00
|
|
|
methods = accessible_methods(obj)
|
2010-06-28 09:36:41 -04:00
|
|
|
if methods.include?(name)
|
|
|
|
method = obj.method(name)
|
2010-08-06 11:23:13 -04:00
|
|
|
method.arity == 0 ? method.call : method
|
2010-06-29 01:22:41 -04:00
|
|
|
elsif obj.respond_to?(:[])
|
2010-08-06 11:23:13 -04:00
|
|
|
obj.send(:[], name, &dontintercept)
|
2010-05-22 05:12:42 -04:00
|
|
|
else
|
2010-08-06 11:23:13 -04:00
|
|
|
yield
|
2010-05-22 05:12:42 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-07 01:53:08 -04:00
|
|
|
|
|
|
|
def iget(obj, index, &dontintercept)
|
|
|
|
if obj.respond_to?(:[])
|
|
|
|
obj.send(:[], index, &dontintercept)
|
|
|
|
else
|
|
|
|
yield
|
2010-08-06 11:23:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-07 01:53:08 -04:00
|
|
|
def set(obj, name, value, &dontintercept)
|
2010-06-29 01:22:41 -04:00
|
|
|
setter = name + "="
|
2010-08-28 15:04:51 -04:00
|
|
|
methods = accessible_methods(obj, true)
|
2010-06-28 12:02:54 -04:00
|
|
|
if methods.include?(setter)
|
2010-08-06 11:23:13 -04:00
|
|
|
obj.send(setter, value)
|
2010-06-29 01:22:41 -04:00
|
|
|
elsif obj.respond_to?(:[]=)
|
2010-08-06 11:23:13 -04:00
|
|
|
obj.send(:[]=, name, value, &dontintercept)
|
2010-05-22 05:12:42 -04:00
|
|
|
else
|
2010-08-06 11:23:13 -04:00
|
|
|
yield
|
2010-05-22 05:12:42 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-07 01:53:08 -04:00
|
|
|
|
|
|
|
def iset(obj, index, value, &dontintercept)
|
|
|
|
if obj.respond_to?(:[]=)
|
|
|
|
obj.send(:[]=, index, value, &dontintercept)
|
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-16 16:20:04 -04:00
|
|
|
def query(obj, name, attributes)
|
|
|
|
if obj.respond_to?(name)
|
|
|
|
attributes.dont_delete
|
|
|
|
unless obj.respond_to?(name + "=")
|
|
|
|
attributes.read_only
|
|
|
|
end
|
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def iquery(obj, index, attributes)
|
|
|
|
if obj.respond_to?(:[])
|
|
|
|
attributes.dont_delete
|
|
|
|
unless obj.respond_to?(:[]=)
|
|
|
|
attributes.read_only
|
|
|
|
end
|
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def names(obj)
|
2010-08-28 15:04:51 -04:00
|
|
|
accessible_methods(obj)
|
2010-08-16 16:20:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def indices(obj)
|
|
|
|
obj.respond_to?(:length) ? (0..obj.length).to_a : yield
|
|
|
|
end
|
|
|
|
|
2010-08-28 11:46:30 -04:00
|
|
|
private
|
|
|
|
|
2010-08-28 15:04:51 -04:00
|
|
|
def accessible_methods(obj, special_methods = false)
|
2010-08-07 01:53:08 -04:00
|
|
|
obj.public_methods(false).map {|m| m.to_s}.to_set.tap do |methods|
|
|
|
|
ancestors = obj.class.ancestors.dup
|
|
|
|
while ancestor = ancestors.shift
|
|
|
|
break if ancestor == ::Object
|
|
|
|
methods.merge(ancestor.public_instance_methods(false).map {|m| m.to_s})
|
|
|
|
end
|
2010-08-28 15:04:51 -04:00
|
|
|
methods.reject! {|m| m == "[]" || m == "[]=" || m =~ /=$/} unless special_methods
|
2010-08-07 01:53:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-08-06 11:23:13 -04:00
|
|
|
end
|