1
0
Fork 0
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:
Charles Lowell 2010-08-30 13:58:59 -05:00
parent 30d2ef4147
commit 88754089f5
3 changed files with 49 additions and 10 deletions

View file

@ -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'

View file

@ -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]

View 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