The callee is the method or function that is being invoked by another method (the caller) at a call site. It’s the target of a method call.
What is a Callee?
def caller_method
callee_method() # ← callee_method is the callee
end
def callee_method
puts "I am the callee!"
endThe callee is the method that receives control when a call site executes.
The Call Relationship
Every method call establishes a caller/callee pair:
def a
b() # 'a' is caller, 'b' is callee
end
def b
c() # 'b' is caller, 'c' is callee
end
def c
# 'c' is callee, has no callees of its own
endA method can be both callee (when being called) and caller (when it calls others).
Callee in the Call Stack
The callee appears above the caller in the call stack:
Call Stack (grows downward):
┌──────────────┐
│ main │ ← Called 'a'
├──────────────┤
│ a │ ← Called 'b' (callee of main)
├──────────────┤
│ b │ ← Called 'c' (callee of a, caller of c)
├──────────────┤
│ c │ ← Current callee (no further calls)
└──────────────┘
Callee Context in YARV
When a call site executes in YARV, a new frame is created for the callee:
def caller_method
x = 10
callee_method(x) # ← New frame created for callee
end
def callee_method(arg)
# New frame contains:
# - arg parameter
# - Fresh stack space
# - New program counter pointing to first instruction
y = arg + 1
y
endThe callee’s frame includes:
- Parameters passed from caller
- Local variables
- program counter starting at the callee’s first instruction
- Stack pointer for the callee’s stack space
Callee Responsibilities
The callee must:
- Accept parameters from the caller
- Execute its instructions using its frame
- Maintain stack discipline (clean up temporary values)
- Return a value to the caller
def callee_method(a, b)
# 1. Parameters received
# 2. Execute logic
result = a + b
# 3. Stack cleaned up automatically
# 4. Return value
result
endIn stack-based VMs, the return value is left on the stack for the caller.
Method Dispatch to Callee
Finding the callee involves method dispatch:
# Ruby code:
object.method_name(arg)
# YARV process:
# 1. Look up 'method_name' on object's class
# 2. Find the method (the callee)
# 3. Create frame for callee
# 4. Jump to callee's instruction sequenceYARV optimizes this through inline caching at each call site.
Callee and Receiver
The callee executes in the context of a receiver:
class MyClass
def callee_method
puts self.class # 'self' is the receiver
end
end
obj = MyClass.new
obj.callee_method # obj is the receiver, callee_method is the calleeThe receiver becomes self inside the callee.
Callee Return
When the callee returns, control flows back to the caller:
def caller_method
puts "before"
result = callee_method() # Control passes to callee
puts "after: #{result}" # Control returns here
end
def callee_method
42 # Return value
endIn YARV:
- Callee’s frame is destroyed
- Return value left on stack
- Caller’s program counter restored
- Execution continues in caller