2014-08-23 09:25:55 +02:00
|
|
|
module Virtual
|
2014-08-21 16:46:12 +02:00
|
|
|
# This implements the send logic
|
|
|
|
# Send is so complicated that we actually code it in ruby and stick it in
|
2014-09-23 18:06:10 +02:00
|
|
|
# That off course opens up an endless loop possibility that we stop by
|
|
|
|
# implementing Class and Module methods
|
|
|
|
|
2015-05-30 11:20:39 +02:00
|
|
|
# Note: I find it slightly unsymmetrical that the NewMessage object needs to be created
|
|
|
|
# before this instruction.
|
|
|
|
# This is because all expressions create a (return) value and that return value is
|
|
|
|
# overwritten by the next expression unless saved.
|
|
|
|
# And since the message is the place to save it it needs to exist. qed
|
2014-08-23 09:25:55 +02:00
|
|
|
class SendImplementation
|
2014-08-21 16:46:12 +02:00
|
|
|
def run block
|
|
|
|
block.codes.dup.each do |code|
|
2014-08-23 09:25:55 +02:00
|
|
|
next unless code.is_a? MessageSend
|
2014-09-23 18:06:10 +02:00
|
|
|
new_codes = [ ]
|
2014-09-15 11:08:37 +02:00
|
|
|
ref = code.me
|
2015-05-24 12:55:05 +02:00
|
|
|
raise "only refs implemented #{ref.type}" unless ( ref.type.is_a? Reference)
|
2015-05-16 11:53:10 +02:00
|
|
|
# value known at compile time, got do something with it
|
2014-09-15 11:08:37 +02:00
|
|
|
if(ref.value)
|
|
|
|
me = ref.value
|
2015-05-16 11:53:10 +02:00
|
|
|
if( me.is_a? Parfait::Class )
|
2014-09-14 20:26:30 +02:00
|
|
|
raise "unimplemented #{code}"
|
2015-05-16 11:53:10 +02:00
|
|
|
elsif( me.is_a? Parfait::Object )
|
2014-09-15 11:08:37 +02:00
|
|
|
# get the function from my class. easy peasy
|
2015-05-16 11:53:10 +02:00
|
|
|
puts "Me is #{me.class}"
|
2015-05-31 13:44:26 +02:00
|
|
|
method = me.get_class.get_instance_method(code.name)
|
2015-05-23 11:15:06 +02:00
|
|
|
raise "Method not implemented #{me.class}.#{code.name}" unless method
|
2014-10-05 00:10:10 +02:00
|
|
|
new_codes << MethodCall.new( method )
|
2014-08-22 09:21:12 +02:00
|
|
|
else
|
2014-09-23 19:16:05 +02:00
|
|
|
# note: this is the current view: call internal send, even the method name says else
|
|
|
|
# but send is "special" and accesses the internal method name and resolves.
|
2015-06-01 07:40:17 +02:00
|
|
|
kernel = Virtual.machine.space.get_class_by_name("Kernel")
|
2014-09-24 17:25:18 +02:00
|
|
|
method = kernel.get_instance_method(:__send)
|
2014-10-05 00:10:10 +02:00
|
|
|
new_codes << MethodCall.new( method )
|
2015-05-16 11:53:10 +02:00
|
|
|
raise "unimplemented: \n#{code}"
|
2014-08-22 09:21:12 +02:00
|
|
|
end
|
2014-09-14 20:26:30 +02:00
|
|
|
else
|
2015-05-24 12:55:05 +02:00
|
|
|
if ref.type.is_a?(Reference) and ref.type.of_class
|
|
|
|
#find method and call
|
|
|
|
clazz = ref.type.of_class
|
2015-05-31 17:34:18 +02:00
|
|
|
begin
|
2015-06-30 17:38:56 +02:00
|
|
|
method = clazz.resolve_method code.name
|
2015-05-31 17:34:18 +02:00
|
|
|
rescue
|
2015-06-30 17:38:56 +02:00
|
|
|
raise "No method found #{code.name} for #{clazz.name} in #{clazz.method_names}" unless method
|
2015-05-31 17:34:18 +02:00
|
|
|
end
|
2015-06-30 17:38:56 +02:00
|
|
|
#puts "CALL is #{method.name}"
|
2015-05-24 12:55:05 +02:00
|
|
|
new_codes << MethodCall.new( method )
|
|
|
|
else
|
|
|
|
# must defer send to run-time
|
|
|
|
# So inlining the code from message.send (in the future)
|
|
|
|
raise "not constant/ known object for send #{ref.inspect}"
|
|
|
|
end
|
2014-08-21 21:57:20 +02:00
|
|
|
end
|
2014-09-23 18:06:10 +02:00
|
|
|
block.replace(code , new_codes )
|
2014-08-21 16:46:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|