Class: SyntaxTree::YARV::SplatArray
- Inherits:
-
Object
- Object
- SyntaxTree::YARV::SplatArray
- Defined in:
- lib/syntax_tree/yarv/instructions.rb
Overview
### Summary
splatarray coerces the array object at the top of the stack into Array by calling to_a. It pushes a duplicate of the array if there is a flag, and the original array if there isn’t one.
### Usage
~~~ruby x = *(5) ~~~
Instance Attribute Summary collapse
-
#flag ⇒ Object
readonly
Returns the value of attribute flag.
Instance Method Summary collapse
- #call(vm) ⇒ Object
- #canonical ⇒ Object
- #disasm(fmt) ⇒ Object
-
#initialize(flag) ⇒ SplatArray
constructor
A new instance of SplatArray.
- #length ⇒ Object
- #pops ⇒ Object
- #pushes ⇒ Object
- #to_a(_iseq) ⇒ Object
Constructor Details
#initialize(flag) ⇒ SplatArray
Returns a new instance of SplatArray.
5083 5084 5085 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 5083 def initialize(flag) @flag = flag end |
Instance Attribute Details
#flag ⇒ Object (readonly)
Returns the value of attribute flag.
5081 5082 5083 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 5081 def flag @flag end |
Instance Method Details
#call(vm) ⇒ Object
5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 5111 def call(vm) value = vm.pop vm.push( if Array === value value.instance_of?(Array) ? value.dup : Array[*value] elsif value.nil? value.to_a else if value.respond_to?(:to_a, true) result = value.to_a if result.nil? [value] elsif !result.is_a?(Array) raise TypeError, "expected to_a to return an Array" end else [value] end end ) end |
#canonical ⇒ Object
5107 5108 5109 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 5107 def canonical self end |
#disasm(fmt) ⇒ Object
5087 5088 5089 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 5087 def disasm(fmt) fmt.instruction("splatarray", [fmt.object(flag)]) end |
#length ⇒ Object
5095 5096 5097 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 5095 def length 2 end |
#pops ⇒ Object
5099 5100 5101 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 5099 def pops 1 end |
#pushes ⇒ Object
5103 5104 5105 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 5103 def pushes 1 end |
#to_a(_iseq) ⇒ Object
5091 5092 5093 |
# File 'lib/syntax_tree/yarv/instructions.rb', line 5091 def to_a(_iseq) [:splatarray, flag] end |