一旦我将一个方法存储在 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>