2010-08-30 14:58:59 -04:00
|
|
|
module V8
|
|
|
|
class Portal
|
|
|
|
class Functions
|
|
|
|
def initialize(portal)
|
|
|
|
@portal = portal
|
|
|
|
@procs, @methods = {},{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](code)
|
2010-08-30 15:23:50 -04:00
|
|
|
self.send(code.class.name, code)
|
|
|
|
end
|
|
|
|
|
|
|
|
def Proc(p)
|
2011-04-25 22:24:05 -04:00
|
|
|
#TODO: check this for memory leaks
|
2010-08-30 15:23:50 -04:00
|
|
|
@procs[p] ||= begin
|
2011-04-25 22:24:05 -04:00
|
|
|
template = C::FunctionTemplate::New(method(:callproc), p)
|
2010-08-30 15:23:50 -04:00
|
|
|
template.GetFunction()
|
2010-08-30 14:58:59 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-30 15:23:50 -04:00
|
|
|
|
|
|
|
def UnboundMethod(method)
|
2011-04-25 22:24:05 -04:00
|
|
|
#TODO: check this for memory leaks.
|
2010-08-30 15:23:50 -04:00
|
|
|
@methods[method.to_s] ||= begin
|
2011-04-25 22:24:05 -04:00
|
|
|
template = C::FunctionTemplate::New(method(:callmethod), method)
|
2010-08-30 14:58:59 -04:00
|
|
|
template.GetFunction()
|
|
|
|
end
|
|
|
|
end
|
2010-08-30 15:23:50 -04:00
|
|
|
|
|
|
|
alias_method :Method, :Proc
|
2011-04-25 22:24:05 -04:00
|
|
|
|
|
|
|
def callproc(arguments)
|
|
|
|
proc = arguments.Data()
|
|
|
|
rbargs = []
|
|
|
|
for i in 0..arguments.Length() - 1
|
|
|
|
rbargs << @portal.rb(arguments[i])
|
|
|
|
end
|
|
|
|
@portal.rubycall(proc, *rbargs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def callmethod(arguments)
|
|
|
|
method = arguments.Data()
|
|
|
|
rbargs = []
|
|
|
|
for i in 0..arguments.Length() - 1
|
|
|
|
rbargs << @portal.rb(arguments[i])
|
|
|
|
end
|
|
|
|
this = @portal.rb(arguments.This())
|
|
|
|
@portal.rubyprotect do
|
|
|
|
method.bind(this).call(*rbargs)
|
|
|
|
end
|
|
|
|
end
|
2010-08-30 14:58:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|