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

avoid using instance variables with java class

this is due  JRuby 1.7.0 throwing warnings https://github.com/jruby/jruby/wiki/Persistence on org.mozilla.javascript.Context
This commit is contained in:
kares 2012-05-18 16:49:40 +02:00
parent cd93c2b422
commit af4a05c874

View file

@ -198,16 +198,18 @@ end
class Java::OrgMozillaJavascript::Context
CACHE = java.util.WeakHashMap.new
def reset_cache!
@cache = java.util.WeakHashMap.new
CACHE[self] = java.util.WeakHashMap.new
end
def enable_cache!
@cache = nil unless @cache
CACHE[self] = nil unless CACHE[self]
end
def disable_cache!
@cache = false
CACHE[self] = false
end
# Support for caching JS data per context.
@ -217,20 +219,20 @@ class Java::OrgMozillaJavascript::Context
# (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)
return yield if (cache = CACHE[self]) == false
cache = reset_cache! unless cache
fetch(key, cache) || store(key, yield, cache)
end
private
def fetch(key)
ref = @cache.get(key)
def fetch(key, cache = CACHE[self])
ref = cache.get(key)
ref ? ref.get : nil
end
def store(key, value)
@cache.put(key, java.lang.ref.WeakReference.new(value))
def store(key, value, cache = CACHE[self])
cache.put(key, java.lang.ref.WeakReference.new(value))
value
end