【发布时间】:2023-03-27 07:01:01
【问题描述】:
一旦我将一个方法存储在 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>
【问题讨论】:
-
我不明白,我的“V8 返回”在上下文 1 和 2 中不同(但在上下文中相同)。
-
@mu 太短:我不明白你的评论。如果你的意思是你得到的结果与我不同,你能分享你的代码和输出吗?
-
与您的代码相同。我得到 V8 返回值,如
A、A、B、B,而你的值类似于A、A、A、A。仍然不完全符合您的预期,但更接近。 -
@mu 太短:感谢您的回复。介意分享您的版本信息吗?我的是:libv8(3.11.8.17 x86_64-darwin-12),ruby 1.9.3p194(2012-04-20 修订版 35410)[x86_64-darwin12.0.0](抱歉无法在此评论中缩进。)
-
我刚刚再次尝试使用 Ruby 2.0.0 和 libv8-3.11.8.17,得到了与您相同的结果。我做的第一个是 1.9.2 和 libv8-3.3.10.4。啊,升级的乐趣。
标签: ruby v8 therubyracer