2017-04-01 20:28:57 +02:00
|
|
|
module Vool
|
2017-04-02 11:59:07 +02:00
|
|
|
class SendStatement < Statement
|
2017-04-06 15:06:51 +02:00
|
|
|
attr_reader :name , :receiver , :arguments
|
|
|
|
|
2017-04-15 19:58:39 +02:00
|
|
|
def initialize(name , receiver , arguments )
|
2017-04-06 15:06:51 +02:00
|
|
|
@name , @receiver , @arguments = name , receiver , arguments
|
2017-04-15 19:58:39 +02:00
|
|
|
@arguments ||= []
|
2017-04-02 17:25:30 +02:00
|
|
|
end
|
2017-04-08 11:10:42 +02:00
|
|
|
|
|
|
|
def collect(arr)
|
|
|
|
@receiver.collect(arr)
|
2017-04-15 19:58:39 +02:00
|
|
|
@arguments.each do |arg|
|
|
|
|
arg.collect(arr)
|
|
|
|
end
|
2017-04-08 11:10:42 +02:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2017-04-15 19:58:39 +02:00
|
|
|
# Sending in a dynamic language is off course not as simple as just calling.
|
|
|
|
# The function that needs to be called depends after all on the receiver,
|
|
|
|
# and no guarantees can be made on what that is.
|
|
|
|
#
|
|
|
|
# It helps to know that usually (>99%) the class of the receiver does not change.
|
|
|
|
# Our stategy then is to cache the functions and only dynamically determine it in
|
|
|
|
# case of a miss (the 1%, and first invocation)
|
|
|
|
#
|
|
|
|
# As cache key we must use the type of the object (which is the first word of _every_ object)
|
|
|
|
# as that is constant, and function implementations depend on the type (not class)
|
|
|
|
#
|
|
|
|
# A Send breaks down to 2 steps:
|
|
|
|
# - Setting up the next message, with receiver, arguments, and (importantly) return address
|
|
|
|
# - a CachedCall , or a SimpleCall, depending on weather the receiver type can be determined
|
|
|
|
#
|
|
|
|
# FIXME: we now presume direct (assignable) values for the arguments and receiver.
|
|
|
|
# in a not so distant future, temporary variables will have to be created
|
|
|
|
# and complex statements hoisted to assign to them. pps: same as in conditions
|
|
|
|
def to_mom( method )
|
2017-09-06 11:51:24 +02:00
|
|
|
Mom::Statements.new( message_setup + call_instruction )
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def message_setup
|
2017-09-10 12:14:51 +02:00
|
|
|
pops = [@receiver.slot_class.new([:message , :next_message , :receiver] , @receiver) ]
|
2017-04-15 19:58:39 +02:00
|
|
|
@arguments.each_with_index do |arg , index|
|
|
|
|
arg_target = [:message , :next_message , :arguments]
|
2017-09-10 12:14:51 +02:00
|
|
|
pops << arg.slot_class.new( arg_target + [index] , arg)
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
|
|
|
pops
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_instruction
|
2017-04-19 19:59:13 +02:00
|
|
|
if(@receiver.ct_type)
|
2017-04-15 19:58:39 +02:00
|
|
|
simple_call
|
|
|
|
else
|
|
|
|
cached_call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def simple_call
|
2017-04-19 19:59:13 +02:00
|
|
|
type = @receiver.ct_type
|
2017-04-25 08:40:09 +02:00
|
|
|
method = type.resolve_method(@name)
|
|
|
|
raise "No method #{@name} for #{type}" unless method
|
2017-04-15 19:58:39 +02:00
|
|
|
[Mom::SimpleCall.new( method) ]
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_call
|
2017-04-23 16:50:06 +02:00
|
|
|
raise "Not implemented"
|
2017-09-10 12:14:51 +02:00
|
|
|
[@receiver.slot_class.new([:message , :next_message , :receiver] , @receiver) ]
|
2017-04-15 19:58:39 +02:00
|
|
|
end
|
|
|
|
|
2017-04-01 20:28:57 +02:00
|
|
|
end
|
|
|
|
end
|