mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
1a93c34b07
It turns out that the Ruby stdlib WeakRef class is completely broken. http://bugs.ruby-lang.org/issues/4168 While it is fixed in trunk, it is not useable, so as a temporary measure, use the `ref` gem as a dependency. It appears to be both faster and more correct.
29 lines
No EOL
628 B
Ruby
29 lines
No EOL
628 B
Ruby
module V8
|
|
module Util
|
|
module Weakcell
|
|
def weakcell(name, &block)
|
|
unless storage = instance_variable_get("@#{name}")
|
|
storage = instance_variable_set("@#{name}", Storage.new)
|
|
end
|
|
storage.access(&block)
|
|
end
|
|
class Storage
|
|
def access(&block)
|
|
if @ref
|
|
@ref.object || populate(block)
|
|
else
|
|
populate(block)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def populate(block)
|
|
occupant = block.call()
|
|
@ref = Ref::WeakReference.new(occupant)
|
|
return occupant
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end |