Class: Peruby::Op::Push

Inherits:
Base
  • Object
show all
Defined in:
lib/peruby/op/call.rb

Overview

Array push with autovivifying dereference support.

Instance Method Summary collapse

Methods inherited from Base

#argument_cells, #local_slot, #lvalue, #run_subroutine, #to_callable, #warning_directive?

Constructor Details

#initialize(array, values) ⇒ Push

Returns a new instance of Push.



168
169
170
171
# File 'lib/peruby/op/call.rb', line 168

def initialize(array, values)
  @array = array
  @values = values
end

Instance Method Details

#run(env, context) ⇒ Object

Raises:



173
174
175
176
177
178
179
180
181
# File 'lib/peruby/op/call.rb', line 173

def run(env, context)
  array = @array.lvalue(env, :scalar)
  raise PerlError, 'Type of arg 1 to push must be array' unless array.is_a?(PerlArray)

  values = @values.flat_map { |value| value.argument_cells(env) }.map(&:copy)
  array.cells.concat(values)
  result = array.size
  context == :list ? [Scalar.new(result)] : result
end