From d6c38d15ba1ed10f5067b4f668455b4967dfe99e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Wed, 14 Aug 2019 14:59:54 +0300 Subject: [PATCH] Fix calling unknown methods Before, when the type was determined, it was assumed that the method can be resolved. But off course tis is not true, as methods may be defined later in the file. Two solutions for that. One could (and should) define all methods and only then start to compile. Thus having the type safety. Or (as now) make a dynamic call and let it fail at runtime. --- lib/rubyx/rubyx_compiler.rb | 1 - lib/vool/send_statement.rb | 12 +++++------- test/vool/send/test_not_found.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 test/vool/send/test_not_found.rb diff --git a/lib/rubyx/rubyx_compiler.rb b/lib/rubyx/rubyx_compiler.rb index b10f710f..25952c0d 100644 --- a/lib/rubyx/rubyx_compiler.rb +++ b/lib/rubyx/rubyx_compiler.rb @@ -125,7 +125,6 @@ module RubyX parfait = ["object"] parfait.each do |file| path = File.expand_path("../../parfait/#{file}.rb",__FILE__) - puts "Loading #{path}" ruby_to_vool(File.read(path)) end end diff --git a/lib/vool/send_statement.rb b/lib/vool/send_statement.rb index c19bd555..652baa4e 100644 --- a/lib/vool/send_statement.rb +++ b/lib/vool/send_statement.rb @@ -36,12 +36,13 @@ module Vool # - Setting up the next message, with receiver, arguments, and (importantly) return address # - a CachedCall , or a SimpleCall, depending on wether the receiver type can be determined def to_mom( compiler ) + puts "Compiling #{self.to_s}" @receiver = SelfExpression.new(compiler.receiver_type) if @receiver.is_a?(SelfExpression) if(@receiver.ct_type) - simple_call(compiler) - else - cached_call(compiler) + method = @receiver.ct_type.resolve_method(self.name) + return simple_call(compiler, method) if method end + cached_call(compiler) end def message_setup(compiler,called_method) @@ -55,10 +56,7 @@ module Vool setup << Mom::ArgumentTransfer.new(self, mom_receive , args ) end - def simple_call(compiler) - type = @receiver.ct_type - called_method = type.resolve_method(@name) - raise "No method #{@name} for #{type}" unless called_method + def simple_call(compiler, called_method) message_setup(compiler,called_method) << Mom::SimpleCall.new(called_method) end diff --git a/test/vool/send/test_not_found.rb b/test/vool/send/test_not_found.rb new file mode 100644 index 00000000..4fc1736b --- /dev/null +++ b/test/vool/send/test_not_found.rb @@ -0,0 +1,28 @@ +require_relative "../helper" + +module Vool + class TestSendCachedSimpleMom < MiniTest::Test + include VoolCompile + + def setup + Parfait.boot!(Parfait.default_test_options) + @compiler = compile_first_method( "5.div8") + @ins = @compiler.mom_instructions.next + end + def test_check_type + assert_equal NotSameCheck , @ins.class , @ins + end + def test_check_resolve_call + assert_equal ResolveMethod , @ins.next(2).class , @ins + end + def test_dynamic_call_last + assert_equal DynamicCall , @ins.next(6).class , @ins + end + + def test_array + check_array [NotSameCheck, SlotLoad, ResolveMethod, Label, MessageSetup , + ArgumentTransfer, DynamicCall, Label, ReturnSequence, Label] , @ins + end + + end +end