mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
create a context with a Ruby object as its scope
This commit is contained in:
parent
a39114a6bd
commit
d8579acf28
4 changed files with 59 additions and 8 deletions
32
lib/v8/util/weakcell.rb
Normal file
32
lib/v8/util/weakcell.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
require 'weakref'
|
||||
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.__getobj__
|
||||
else
|
||||
populate(block)
|
||||
end
|
||||
rescue RefError
|
||||
populate(block)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def populate(block)
|
||||
occupant = block.call()
|
||||
@ref = WeakRef.new(occupant)
|
||||
return occupant
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue