Class: CodeTools::AST::SingleBlockArg

Inherits:
SendWithArguments show all
Defined in:
lib/rubinius/code/ast/transforms.rb

Overview

Handles Rubinius.single_block_arg

Given the following code:

m { |x| ... }

In Ruby 1.8, this has the following semantics:

* x == nil if no values are yielded
* x == val if one value is yielded
* x == [p, q, r, ...] if more than one value is yielded
* x == [a, b, c, ...] if one Array is yielded

In Ruby 1.9, this has the following semantics:

* x == nil if no values are yielded
* x == val if one value is yielded
* x == p if yield(p, q, r, ...)
* x == [a, b, c, ...] if one Array is yielded

However, in Ruby 1.9, the Enumerator code manually implements the 1.8 block argument semantics. This transform exposes the VM instruction we use in 1.8 mode (cast_for_single_block_arg) so we can use it in 1.9 mode for Enumerator.

Instance Attribute Summary

Attributes inherited from SendWithArguments

#arguments

Attributes inherited from Send

#block, #name, #privately, #receiver, #variable, #vcall_style

Attributes inherited from Node

#line

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SendWithArguments

#arguments_sexp, #initialize

Methods inherited from Send

#arguments_sexp, #check_local_reference, #defined, #initialize, #receiver_sexp, #sexp_name, #to_sexp, #value_defined

Methods inherited from Node

#ascii_graph, #attributes, #children, #defined, #initialize, match_arguments?, match_send?, #new_block_generator, #new_generator, #node_name, #or_bytecode, #pos, #set_child, #to_sexp, #transform, transform, transform_comment, transform_kind, transform_kind=, transform_name, #value_defined, #visit, #walk

Constructor Details

This class inherits a constructor from CodeTools::AST::SendWithArguments

Class Method Details

.match?(line, receiver, name, arguments, privately) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/rubinius/code/ast/transforms.rb', line 179

def self.match?(line, receiver, name, arguments, privately)
  match_send? receiver, :Rubinius, name, :single_block_arg
end

Instance Method Details

#bytecode(g) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/rubinius/code/ast/transforms.rb', line 183

def bytecode(g)
  if @arguments.splat?
    raise CompileError, "splat argument passed to single_block_arg"
  elsif @block
    raise CompileError, "block passed to single_block_arg"
  end

  pos(g)
  g.cast_for_single_block_arg
end