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.



4491
4492
4493
4494
# File 'lib/syntax_tree/yarv/instructions.rb', line 4491

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.



4489
4490
4491
# File 'lib/syntax_tree/yarv/instructions.rb', line 4489

def block_iseq
  @block_iseq
end

#calldataObject (readonly)

Returns the value of attribute calldata.



4489
4490
4491
# File 'lib/syntax_tree/yarv/instructions.rb', line 4489

def calldata
  @calldata
end

Instance Method Details

#call(vm) ⇒ Object



4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
# File 'lib/syntax_tree/yarv/instructions.rb', line 4525

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



4521
4522
4523
# File 'lib/syntax_tree/yarv/instructions.rb', line 4521

def canonical
  self
end

#disasm(fmt) ⇒ Object



4496
4497
4498
4499
4500
4501
4502
# File 'lib/syntax_tree/yarv/instructions.rb', line 4496

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

#lengthObject



4508
4509
4510
# File 'lib/syntax_tree/yarv/instructions.rb', line 4508

def length
  3
end

#popsObject



4512
4513
4514
4515
# File 'lib/syntax_tree/yarv/instructions.rb', line 4512

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

#pushesObject



4517
4518
4519
# File 'lib/syntax_tree/yarv/instructions.rb', line 4517

def pushes
  1
end

#to_a(_iseq) ⇒ Object



4504
4505
4506
# File 'lib/syntax_tree/yarv/instructions.rb', line 4504

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