1
0
Fork 0
mirror of https://github.com/rubyjs/therubyrhino synced 2023-03-27 23:21:34 -04:00

delegate (Ruby wrapper) caching to JS::Context - to behave somehow 'more' correctly with multiple context sessions

This commit is contained in:
kares 2012-01-10 10:52:53 +01:00
parent 07f7d5a5b2
commit a6acb32a5b
2 changed files with 44 additions and 20 deletions

View file

@ -173,3 +173,43 @@ class Java::OrgMozillaJavascript::BaseFunction
end
end
class Java::OrgMozillaJavascript::Context
def reset_cache!
@cache = java.util.WeakHashMap.new
end
def enable_cache!
@cache = nil unless @cache
end
def disable_cache!
@cache = false
end
# Support for caching JS data per context.
# e.g. to get === comparison's working ...
#
# NOTE: the cache only works correctly for keys following Java identity !
# (implementing #equals & #hashCode e.g. RubyStrings will work ...)
#
def cache(key)
return yield if @cache == false
reset_cache! unless @cache
fetch(key) || store(key, yield)
end
private
def fetch(key)
ref = @cache.get(key)
ref ? ref.get : nil
end
def store(key, value)
@cache.put(key, java.lang.ref.WeakReference.new(value))
value
end
end

View file

@ -207,28 +207,12 @@ module Rhino
end
end
def self.cache(key)
return yield unless @@cache
fetch(key) || store(key, yield)
def self.cache(key, &block)
context = JS::Context.getCurrentContext
context ? context.cache(key, &block) : yield
end
private
# NOTE: just to get === comparison's working ...
# if == is enough might be disabled by setting to nil
@@cache = java.util.WeakHashMap.new
def self.fetch(key)
ref = @@cache.get(key)
ref ? ref.get : nil
end
def self.store(key, value)
@@cache.put(key, java.lang.ref.WeakReference.new(value))
value
end
end
RubyObject = Ruby::Object