mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
cache methods and procs so that the same function is used for the same method.
This commit is contained in:
parent
30d2ef4147
commit
88754089f5
3 changed files with 49 additions and 10 deletions
|
@ -5,6 +5,7 @@ module V8
|
|||
VERSION = '0.7.6.pre'
|
||||
require 'v8/v8' #native glue
|
||||
require 'v8/portal'
|
||||
require 'v8/portal/functions'
|
||||
require 'v8/context'
|
||||
require 'v8/object'
|
||||
require 'v8/array'
|
||||
|
|
|
@ -43,16 +43,7 @@ module V8
|
|||
h[obj] = @constructors[obj.class].GetFunction().NewInstance(args)
|
||||
end
|
||||
|
||||
@functions = Hash.new do |h, code|
|
||||
template = C::FunctionTemplate::New() do |arguments|
|
||||
rbargs = []
|
||||
for i in 0..arguments.Length() - 1
|
||||
rbargs << rb(arguments[i])
|
||||
end
|
||||
rubycall(code, *rbargs)
|
||||
end
|
||||
h[code] = template.GetFunction()
|
||||
end
|
||||
@functions = Functions.new(self)
|
||||
|
||||
@embedded_constructors = Hash.new do |h, cls|
|
||||
template = @constructors[cls]
|
||||
|
|
47
lib/v8/portal/functions.rb
Normal file
47
lib/v8/portal/functions.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
module V8
|
||||
class Portal
|
||||
class Functions
|
||||
def initialize(portal)
|
||||
@portal = portal
|
||||
@procs, @methods = {},{}
|
||||
end
|
||||
|
||||
def [](code)
|
||||
case code
|
||||
when Proc
|
||||
@procs[code] ||= begin
|
||||
template = C::FunctionTemplate::New() do |arguments|
|
||||
rbargs = []
|
||||
for i in 0..arguments.Length() - 1
|
||||
rbargs << @portal.rb(arguments[i])
|
||||
end
|
||||
@portal.rubycall(code, *rbargs)
|
||||
end
|
||||
template.GetFunction()
|
||||
end
|
||||
when Method
|
||||
get_method(code)
|
||||
end
|
||||
end
|
||||
|
||||
def get_method(method)
|
||||
unbound = method.unbind
|
||||
@methods[unbound.to_s] ||= begin
|
||||
template = C::FunctionTemplate::New() do |arguments|
|
||||
rbargs = []
|
||||
for i in 0..arguments.Length() - 1
|
||||
rbargs << @portal.rb(arguments[i])
|
||||
end
|
||||
this = @portal.rb(arguments.This())
|
||||
begin
|
||||
@portal.rubycall(unbound.bind(this), *rbargs)
|
||||
rescue Exception => e
|
||||
raise e
|
||||
end
|
||||
end
|
||||
template.GetFunction()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue