Class: SyntaxTree::YARV::Send

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/instructions.rb

Overview

### Summary

send invokes a method with an optional block. It pops its receiver and the arguments for the method off the stack and pushes the return value onto the stack. It has two arguments: the calldata for the call site and the optional block instruction sequence.

### Usage

~~~ruby “hello”.tap { |i| p i } ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(calldata, block_iseq) ⇒ Send

Returns a new instance of Send.



5158
5159
5160
5161
# File 'lib/syntax_tree/yarv/instructions.rb', line 5158

def initialize(calldata, block_iseq)
  @calldata = calldata
  @block_iseq = block_iseq
end

Instance Attribute Details

#block_iseqObject (readonly)

Returns the value of attribute block_iseq.



5156
5157
5158
# File 'lib/syntax_tree/yarv/instructions.rb', line 5156

def block_iseq
  @block_iseq
end

#calldataObject (readonly)

Returns the value of attribute calldata.



5156
5157
5158
# File 'lib/syntax_tree/yarv/instructions.rb', line 5156

def calldata
  @calldata
end

Instance Method Details

#==(other) ⇒ Object



5179
5180
5181
5182
# File 'lib/syntax_tree/yarv/instructions.rb', line 5179

def ==(other)
  other.is_a?(Send) && other.calldata == calldata &&
    other.block_iseq == block_iseq
end

#call(vm) ⇒ Object



5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
# File 'lib/syntax_tree/yarv/instructions.rb', line 5201

def call(vm)
  block =
    if (iseq = block_iseq)
      frame = vm.frame
      ->(*args, **kwargs, &blk) do
        vm.run_block_frame(iseq, frame, *args, **kwargs, &blk)
      end
    elsif calldata.flag?(CallData::CALL_ARGS_BLOCKARG)
      vm.pop
    end

  keywords =
    if calldata.kw_arg
      calldata.kw_arg.zip(vm.pop(calldata.kw_arg.length)).to_h
    else
      {}
    end

  arguments = vm.pop(calldata.argc)
  receiver = vm.pop

  vm.push(
    receiver.__send__(calldata.method, *arguments, **keywords, &block)
  )
end

#canonicalObject



5197
5198
5199
# File 'lib/syntax_tree/yarv/instructions.rb', line 5197

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



5175
5176
5177
# File 'lib/syntax_tree/yarv/instructions.rb', line 5175

def deconstruct_keys(_keys)
  { calldata: calldata, block_iseq: block_iseq }
end

#disasm(fmt) ⇒ Object



5163
5164
5165
5166
5167
5168
5169
# File 'lib/syntax_tree/yarv/instructions.rb', line 5163

def disasm(fmt)
  fmt.enqueue(block_iseq) if block_iseq
  fmt.instruction(
    "send",
    [fmt.calldata(calldata), block_iseq&.name || "nil"]
  )
end

#lengthObject



5184
5185
5186
# File 'lib/syntax_tree/yarv/instructions.rb', line 5184

def length
  3
end

#popsObject



5188
5189
5190
5191
# File 'lib/syntax_tree/yarv/instructions.rb', line 5188

def pops
  argb = (calldata.flag?(CallData::CALL_ARGS_BLOCKARG) ? 1 : 0)
  argb + calldata.argc + 1
end

#pushesObject



5193
5194
5195
# File 'lib/syntax_tree/yarv/instructions.rb', line 5193

def pushes
  1
end

#to_a(_iseq) ⇒ Object



5171
5172
5173
# File 'lib/syntax_tree/yarv/instructions.rb', line 5171

def to_a(_iseq)
  [:send, calldata.to_h, block_iseq&.to_a]
end