一旦我将方法存储在V8 :: Context中,该方法的所有后续实例都存储在任何名称的任何上下文中,就像初始实例一样(即好像原始实例已再次存储一样)。
我尝试用以下方法隔离/演示问题:
require 'V8'
class TestClass
def test_method
self.inspect
end
end
(1..2).each do |cxt_i|
cxt = V8::Context.new
[:test_method, :test_method2].each_with_index do |method_name, method_i|
method = TestClass.new.method(:test_method)
cxt[method_name.to_s] = method
script = method_name.to_s+'()'
puts "Context #{cxt_i}, method #{method_i+1} result is #{method.call}, V8 returns #{cxt.eval(script)}"
end
end
产生以下输出:
Context 1, method 1 result is #<TestClass:0x007fce2419cdb0>, V8 returns #<TestClass:0x007fce2419cdb0>
Context 1, method 2 result is #<TestClass:0x007fce2419b780>, V8 returns #<TestClass:0x007fce2419cdb0>
Context 2, method 1 result is #<TestClass:0x007fce2419abc8>, V8 returns #<TestClass:0x007fce2419cdb0>
Context 2, method 2 result is #<TestClass:0x007fce24199a98>, V8 returns #<TestClass:0x007fce2419cdb0>
答案 0 :(得分:1)
这是一个弱参考问题。通过在内部循环中插入GC.start
,我们强制将V8上下文中的弱引用进行垃圾回收。尽管如此,它的速度却大大降低了。
require 'v8'
class TestClass
def test_method
self.inspect
end
end
(1..2).each do |cxt_i|
cxt = V8::Context.new
[:test_method, :test_method2].each_with_index do |method_name, method_i|
method = TestClass.new.method(:test_method)
cxt[method_name.to_s] = method
script = method_name.to_s+'()'
puts "Context #{cxt_i}, method #{method_i+1} result is #{method.call}, V8 returns #{cxt.eval(script)}"
GC.start # <<<<=========
end
end
输出:
Context 1, method 1 result is #<TestClass:0x007f8f13a26cd8>, V8 returns #<TestClass:0x007f8f13a26cd8>
Context 1, method 2 result is #<TestClass:0x007f8f135cca48>, V8 returns #<TestClass:0x007f8f135cca48>
Context 2, method 1 result is #<TestClass:0x007f8f135ceac8>, V8 returns #<TestClass:0x007f8f135ceac8>
Context 2, method 2 result is #<TestClass:0x007f8f135cdbf0>, V8 returns #<TestClass:0x007f8f135cdbf0>
ruby -v是ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-linux]
,libv8是3.11.8.17 x86_64-linux