From 1d1c7105b4028116ef88fc9d06240a1551a69a54 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 2 Jun 2018 23:02:59 +0300 Subject: [PATCH] introduce some helper methods --- lib/risc/interpreter.rb | 8 +++---- lib/risc/machine.rb | 8 +++---- lib/risc/position/code_listener.rb | 2 +- lib/risc/position/instruction_listener.rb | 2 +- lib/risc/position/position.rb | 14 +++++++++++ test/arm/helper.rb | 2 +- test/risc/position/test_object_listener.rb | 6 ++--- test/risc/position/test_position.rb | 27 +++++++++++++++------- test/risc/test_machine.rb | 6 ++--- 9 files changed, 50 insertions(+), 25 deletions(-) diff --git a/lib/risc/interpreter.rb b/lib/risc/interpreter.rb index e60f4977..2c0a5e6c 100644 --- a/lib/risc/interpreter.rb +++ b/lib/risc/interpreter.rb @@ -49,13 +49,13 @@ module Risc raise "Not int #{pos}" unless pos.is_a? Numeric position = Position.at(pos) raise "No position #{pos.to_s(16)}" unless position - if position.is_a?(CodePosition) + if position.is_a?(CodeListener) raise "Setting Code #{clock}-#{position}, #{position.method}" #return set_pc(position.at + Parfait::BinaryCode.byte_offset) end - log.debug "Setting Position #{clock}-#{position}, #{position.binary}" - raise "not instruction position #{position}-#{position.class}-#{position.object.class}" unless position.is_a?(InstructionPosition) - set_instruction( position.instruction ) + log.debug "Setting Position #{clock}-#{position}, " + #raise "not instruction position #{position}-#{position.class}-#{position.object.class}" unless position.is_a?(InstructionPosition) + set_instruction( position.object ) @clock += 1 @pc = position.at end diff --git a/lib/risc/machine.rb b/lib/risc/machine.rb index a1b04bb9..040da7b0 100644 --- a/lib/risc/machine.rb +++ b/lib/risc/machine.rb @@ -85,7 +85,7 @@ module Risc def position_all raise "Not translated " unless @translated #need the initial jump at 0 and then functions - Position.init(cpu_init , -1) + Position.new(cpu_init , 0) code_start = position_objects( @platform.padding ) # and then everything code position_code(code_start) @@ -101,8 +101,8 @@ module Risc sorted.each do | objekt| next if objekt.is_a?( Parfait::BinaryCode) or objekt.is_a?( Risc::Label ) before = at - position = Position.init(objekt,at) - previous.register_event(:position_changed , PositionListener.new(objekt)) if previous + position = Position.new(objekt , at) + previous.position_listener(objekt) if previous previous = position at += objekt.padded_length log.debug "Object #{objekt.class}:#{before.to_s(16)} len: #{(at - before).to_s(16)}" @@ -123,7 +123,7 @@ module Risc type.methods.each_method do |method| last_code = CodeListener.init(method.binary , code_start) InstructionListener.init(method.cpu_instructions, method.binary) - last_code.register_event(:position_changed , prev_code.object) if prev_code + last_code.position_listener( prev_code.object) if prev_code prev_code = last_code code_start = last_code.next_slot end diff --git a/lib/risc/position/code_listener.rb b/lib/risc/position/code_listener.rb index 69154218..c3a07d1f 100644 --- a/lib/risc/position/code_listener.rb +++ b/lib/risc/position/code_listener.rb @@ -48,7 +48,7 @@ module Risc Position.set_to(position , at) if code.next listener = PositionListener.new(code.next) - position.register_event(:position_changed , listener) + position.position_listener( listener) end at += code.padded_length unless at < 0 code = code.next diff --git a/lib/risc/position/instruction_listener.rb b/lib/risc/position/instruction_listener.rb index 17bd9d5c..60d1064b 100644 --- a/lib/risc/position/instruction_listener.rb +++ b/lib/risc/position/instruction_listener.rb @@ -62,7 +62,7 @@ module Risc nekst = instruction.next if nekst listener = InstructionListener.new( nekst , code ) - position.register_event(:position_changed , listener) + position.position_listener(listener) end instruction = nekst end diff --git a/lib/risc/position/position.rb b/lib/risc/position/position.rb index e2cfee10..137d8d0b 100644 --- a/lib/risc/position/position.rb +++ b/lib/risc/position/position.rb @@ -34,6 +34,20 @@ module Risc Position.set_to(self , pos) end + # utility to register events of type :position_changed + # can give an object and a PositionListener will be created for it + def position_listener(listener) + unless listener.class.name.include?("Listener") + listener = PositionListener.new(listener) + end + register_event(:position_changed , listener) + end + + # utility to get all registered listeners to the :position_changed event + # returns an array + def position_listeners + event_table[:position_changed] + end #look for InstructionListener and return its code if found def get_code listener = event_table.find{|one| one.class == InstructionListener} diff --git a/test/arm/helper.rb b/test/arm/helper.rb index c374d890..d2145a8b 100644 --- a/test/arm/helper.rb +++ b/test/arm/helper.rb @@ -18,7 +18,7 @@ module Arm @machine = Arm::ArmMachine @binary = FakeBin.new Risc::Position.clear_positions - Risc::Position.set(@binary , 0) + Risc::Position.new(@binary , 0) end # code is what the generator spits out, at least one instruction worth (.first) diff --git a/test/risc/position/test_object_listener.rb b/test/risc/position/test_object_listener.rb index d21aabc0..7b369102 100644 --- a/test/risc/position/test_object_listener.rb +++ b/test/risc/position/test_object_listener.rb @@ -11,15 +11,15 @@ module Risc @listener = PositionListener.new(@dependent) end def test_register - assert @pos.register_event(:position_changed , @listener) + assert @pos.position_listener(@listener) end def test_no_fire - @pos.register_event(:position_changed , self) + @pos.position_listener(self) Position.set_to(@pos,0) assert_equal 0 , Position.get(@object).at end def test_reset - @pos.register_event(:position_changed , @listener) + @pos.position_listener( @listener) Position.set_to(@pos,4) assert_equal 0 , Position.at(4).at end diff --git a/test/risc/position/test_position.rb b/test/risc/position/test_position.rb index 3641e6bc..b46ccab7 100644 --- a/test/risc/position/test_position.rb +++ b/test/risc/position/test_position.rb @@ -15,9 +15,18 @@ module Risc def test_has_get_code assert_nil Position.new(self , -1).get_code end - def pest_creation_ok - assert Position.new(self,0) + def test_has_listeners_helper + assert_equal Array , Position.new(self,-1).position_listeners.class end + def test_listeners_empty + assert Position.new(self,-1).position_listeners.empty? + end + def test_has_listener_helper + pos = Position.new(self,-1) + pos.position_listener( self ) + assert_equal 1 , pos.position_listeners.length + end + def pest_creation_fail assert_raises {Position.new("0")} end @@ -63,20 +72,22 @@ module Risc @position = Position.new(self) end def pest_has_register - assert @position.register_event(:position_changed , self) + assert @position.position_listener( self) end def pest_can_unregister - assert @position.register_event(:position_changed ,self) - assert @position.unregister_event(:position_changed ,self) + listener = PositionListener.new(self) + assert @position.position_listener(listener) + assert @position.unregister_event(:position_changed ,listener) end def pest_fires - @position.register_event(:position_changed ,self) + @position.position_listener(self) @position.trigger(:position_changed , @position) assert_equal @position , @trigger end def pest_no_fire_after_unregister - assert @position.register_event(:position_changed ,self) - assert @position.unregister_event(:position_changed ,self) + listener = PositionListener.new(self) + assert @position.position_listener( listener) + assert @position.unregister_event(:position_changed ,listener) @position.trigger(:position_changed , @position) assert_nil @trigger end diff --git a/test/risc/test_machine.rb b/test/risc/test_machine.rb index 57e37732..137e1deb 100644 --- a/test/risc/test_machine.rb +++ b/test/risc/test_machine.rb @@ -31,7 +31,7 @@ module Risc count += 1 addr = addr.next_integer end - assert_equal 5, count + assert_equal 5, count end end class TestMachineInit < MiniTest::Test @@ -48,10 +48,10 @@ module Risc assert_equal "0x5ed4" , Position.get(@machine.cpu_init.first).to_s end def test_cpu_bin - assert_equal "0x5ecc" , Position.get(Position.get(@machine.cpu_init.first).binary).to_s + assert_equal "0x5ecc" , Position.get(@machine.cpu_init).to_s end def test_cpu_label - assert_equal InstructionPosition , Position.get(@machine.cpu_init.first).class + assert_equal Position , Position.get(@machine.cpu_init.first).class end def test_first_binary_jump bin = Parfait.object_space.get_init.binary