Class: SyntaxTree::YARV::Send

Inherits:
Instruction 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

Methods inherited from Instruction

#branch_targets, #canonical, #falls_through?, #leaves?, #side_effects?

Constructor Details

#initialize(calldata, block_iseq) ⇒ Send

Returns a new instance of Send.



4812
4813
4814
4815
# File 'lib/syntax_tree/yarv/instructions.rb', line 4812

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.



4810
4811
4812
# File 'lib/syntax_tree/yarv/instructions.rb', line 4810

def block_iseq
  @block_iseq
end

#calldataObject (readonly)

Returns the value of attribute calldata.



4810
4811
4812
# File 'lib/syntax_tree/yarv/instructions.rb', line 4810

def calldata
  @calldata
end

Instance Method Details

#==(other) ⇒ Object



4833
4834
4835
4836
# File 'lib/syntax_tree/yarv/instructions.rb', line 4833

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

#call(vm) ⇒ Object



4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
# File 'lib/syntax_tree/yarv/instructions.rb', line 4851

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

#deconstruct_keys(_keys) ⇒ Object



4829
4830
4831
# File 'lib/syntax_tree/yarv/instructions.rb', line 4829

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

#disasm(fmt) ⇒ Object



4817
4818
4819
4820
4821
4822
4823
# File 'lib/syntax_tree/yarv/instructions.rb', line 4817

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

#lengthObject



4838
4839
4840
# File 'lib/syntax_tree/yarv/instructions.rb', line 4838

def length
  3
end

#popsObject



4842
4843
4844
4845
# File 'lib/syntax_tree/yarv/instructions.rb', line 4842

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

#pushesObject



4847
4848
4849
# File 'lib/syntax_tree/yarv/instructions.rb', line 4847

def pushes
  1
end

#to_a(_iseq) ⇒ Object



4825
4826
4827
# File 'lib/syntax_tree/yarv/instructions.rb', line 4825

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