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-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 )
|
2015-08-04 20:46:33 +02:00
|
|
|
raise "unimplemented #{code} me is #{me}"
|
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 )
|
2015-07-02 08:49:52 +02:00
|
|
|
elsif( me.is_a? Symbol )
|
|
|
|
# get the function from my class. easy peasy
|
|
|
|
method = Virtual.machine.space.get_class_by_name(:Word).get_instance_method(code.name)
|
|
|
|
raise "Method not implemented #{me.class}.#{code.name}" unless method
|
|
|
|
new_codes << MethodCall.new( method )
|
2015-08-04 20:46:33 +02:00
|
|
|
elsif( me.is_a? Fixnum )
|
2015-08-05 17:49:37 +02:00
|
|
|
name = code.name
|
|
|
|
name = :plus if name == :+
|
|
|
|
method = Virtual.machine.space.get_class_by_name(:Integer).get_instance_method(name)
|
2015-08-06 17:27:25 +02:00
|
|
|
#puts Virtual.machine.space.get_class_by_name(:Integer).method_names.to_a
|
2015-08-05 17:49:37 +02:00
|
|
|
raise "Method not implemented Integer.#{name}" unless method
|
2015-08-04 20:46:33 +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-07-02 08:49:52 +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-07-02 08:49:52 +02:00
|
|
|
raise "unimplemented: \n#{code} \nfor #{ref.inspect}"
|
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
|