From 91c79038486e159d472b629979fc657d280dd84c Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 2 Jun 2018 16:34:44 +0300 Subject: [PATCH] start simple with code listner --- .../{code_position.rb => code_listener.rb} | 14 ++++++++- lib/risc/position/position.rb | 2 +- test/risc/position/test_code_listener.rb | 31 +++++++++++++++++++ test/risc/position/test_object_listener.rb | 12 +++---- 4 files changed, 51 insertions(+), 8 deletions(-) rename lib/risc/position/{code_position.rb => code_listener.rb} (82%) create mode 100644 test/risc/position/test_code_listener.rb diff --git a/lib/risc/position/code_position.rb b/lib/risc/position/code_listener.rb similarity index 82% rename from lib/risc/position/code_position.rb rename to lib/risc/position/code_listener.rb index d68e9ec3..da64fd38 100644 --- a/lib/risc/position/code_position.rb +++ b/lib/risc/position/code_listener.rb @@ -8,7 +8,7 @@ module Risc # At the end of the list the propagation spills into the next methods # binary and so on # - class CodePosition < ObjectPosition + class CodeListener attr_reader :code , :method @@ -61,6 +61,18 @@ module Risc return nekst if nekst.methods return next_type(nekst) end + def self.init( code , at = -1) + while code + position = ObjectPosition.new(code , at) + Position.set_to(position , at) + if code.next + listener = ObjectListener.new(code.next) + position.register_event(:position_changed , listener) + end + at += code.padded_length unless at < 0 + code = code.next + end + end end end end diff --git a/lib/risc/position/position.rb b/lib/risc/position/position.rb index 2b2eec58..8120e9f3 100644 --- a/lib/risc/position/position.rb +++ b/lib/risc/position/position.rb @@ -74,4 +74,4 @@ end require_relative "object_position" require_relative "object_listener" require_relative "instruction_position" -require_relative "code_position" +require_relative "code_listener" diff --git a/test/risc/position/test_code_listener.rb b/test/risc/position/test_code_listener.rb new file mode 100644 index 00000000..010055e6 --- /dev/null +++ b/test/risc/position/test_code_listener.rb @@ -0,0 +1,31 @@ +require_relative "helper" + +module Risc + module Position + # tests that require a boot and test propagation + class TestcodeListener < MiniTest::Test + def setup + Risc.machine.boot + @binary = Parfait::BinaryCode.new(1) + @method = Parfait.object_space.types.values.first.methods + @label = Risc.label("hi","ho") + end + + def test_has_init + CodeListener.init(@binary) + assert Position.get(@binary) + end + def test_init_listner + @binary.extend_one + CodeListener.init(@binary) + pos = Position.get(@binary) + assert !pos.event_table[:position_changed].empty? + end + def test_not_init_listner + CodeListener.init(@binary) + pos = Position.get(@binary) + assert pos.event_table[:position_changed].empty? + end + end + end +end diff --git a/test/risc/position/test_object_listener.rb b/test/risc/position/test_object_listener.rb index 915b812d..fc2312ef 100644 --- a/test/risc/position/test_object_listener.rb +++ b/test/risc/position/test_object_listener.rb @@ -7,8 +7,8 @@ module Risc def setup @object = Dummy.new @dependent = Dummy.new - @pos = Position.set(@object,0) - Position.set(@dependent,0) + @pos = ObjectPosition.new(@object,0) + ObjectPosition.new(@dependent,0) @listener = ObjectListener.new(@dependent) end def test_register @@ -16,13 +16,13 @@ module Risc end def test_no_fire @pos.register_event(:position_changed , self) - @pos = Position.set(@object,0) - assert_equal 0 , Position.get(@dependent).at + Position.set_to(@pos,0) + assert_equal 0 , Position.get(@object).at end def test_reset @pos.register_event(:position_changed , @listener) - @pos = Position.set(@object,4) - assert_equal 4 , Position.get(@dependent).at + Position.set_to(@pos,4) + assert_equal 0 , Position.at(4).at end end end