2011-04-26 19:08:34 -04:00
|
|
|
|
|
|
|
module V8
|
|
|
|
class Portal
|
|
|
|
class Templates
|
|
|
|
|
|
|
|
attr_reader :portal
|
|
|
|
|
|
|
|
def initialize(portal)
|
|
|
|
@portal = portal
|
|
|
|
@constructors = {}
|
2011-04-27 00:39:32 -04:00
|
|
|
@methods = {}
|
|
|
|
@procs = {}
|
2011-05-02 16:40:07 -04:00
|
|
|
@releases = {}
|
2011-04-26 19:08:34 -04:00
|
|
|
end
|
2011-05-02 16:40:07 -04:00
|
|
|
|
2011-04-26 19:08:34 -04:00
|
|
|
def to_constructor(ruby_class)
|
|
|
|
class_id = ruby_class.object_id
|
|
|
|
if constructor = @constructors[class_id]
|
|
|
|
return constructor
|
|
|
|
else
|
2011-04-27 01:08:53 -04:00
|
|
|
constructor = @constructors[class_id] = ConstructorAdapter.new(self, class_id)
|
2011-05-02 16:40:07 -04:00
|
|
|
ObjectSpace.define_finalizer(ruby_class, release(@constructors, class_id))
|
2011-04-26 19:08:34 -04:00
|
|
|
return constructor
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-27 00:39:32 -04:00
|
|
|
def to_function(code)
|
|
|
|
case code
|
|
|
|
when Method, UnboundMethod
|
|
|
|
if fn = @methods[code.to_s]
|
|
|
|
return fn
|
|
|
|
else
|
2011-04-27 01:08:53 -04:00
|
|
|
function = @methods[code.to_s] = FunctionAdapter.new(@portal, code)
|
2011-04-27 00:39:32 -04:00
|
|
|
#TODO: test this weak behavior
|
2011-05-02 16:40:07 -04:00
|
|
|
function.template.MakeWeak(0, release(@methods, code.to_s))
|
2011-04-27 00:39:32 -04:00
|
|
|
return function
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if fn = @procs[code]
|
|
|
|
return fn
|
|
|
|
else
|
2011-05-02 16:40:07 -04:00
|
|
|
function = @procs[code] = FunctionAdapter.new(@portal, code)
|
2011-04-27 00:39:32 -04:00
|
|
|
#TODO: test this weak behavior
|
2011-05-02 16:40:07 -04:00
|
|
|
function.template.MakeWeak(0, release(@procs, code))
|
2011-04-27 00:39:32 -04:00
|
|
|
return function
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-26 19:08:34 -04:00
|
|
|
def proxies
|
|
|
|
@portal.proxies
|
|
|
|
end
|
|
|
|
|
2011-05-02 16:40:07 -04:00
|
|
|
def release(refs, id)
|
|
|
|
release = Release.new(@releases, refs, id)
|
|
|
|
@releases[release] = true
|
|
|
|
return release
|
|
|
|
end
|
|
|
|
|
|
|
|
class Release
|
|
|
|
def initialize(releases, refs, id)
|
|
|
|
@releases, @refs, @id = releases, refs, id
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(*args)
|
|
|
|
@refs.delete(@id)
|
|
|
|
@releases.delete(self)
|
|
|
|
end
|
|
|
|
|
2011-04-26 19:08:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|