메서드를 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>
인 상황 1과 2에 다른 (그러나 문맥 내의 동일한 것). –
@mu가 너무 짧습니다. 귀하의 의견을 이해할 수 없습니다. 내가하는 것보다 다른 결과가 나오면 코드와 산출물을 공유 할 수 있습니까? –
당신과 같은 코드입니다. V8은 A, A, A, A와 같이 A, A, B, B와 같은 값을 반환합니다. 아직도 당신이 기대하는 바가 아니지만 더 가까이에 있습니다. –