Method: CodeTools::AST::OpAssignAttribute#bytecode

Defined in:
lib/rubinius/code/ast/operators.rb

#bytecode(g, anddot = false) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/rubinius/code/ast/operators.rb', line 293

def bytecode(g, anddot=false)
  pos(g)

  # X: h.a += 3, given h.a == 2
  @receiver.bytecode(g) unless anddot
  # X: TOS = h
  g.dup
  g.send @name, 0
  # X: TOS = 2

  if @op == :or or @op == :and
    fnd = g.new_label
    fin = g.new_label
    assign = g.new_label

    g.dup
    if @op == :or
      g.goto_if_true fnd
    else
      g.goto_if_false fnd
    end

    # Remove the copy of 2 and push @value on the stack
    g.pop

    old_break = g.break
    new_break = g.new_label
    g.break = new_break

    @value.bytecode(g)

    g.goto assign

    new_break.set!
    if old_break
      g.pop_many 2
      g.push_tagged_nil 0
      g.goto old_break
    end

    g.break = old_break

    assign.set!

    # Retain the this value to use as the expression value
    g.dup
    g.move_down 2

    # Call the assignement method, passing @value as the argument
    g.send @assign, 1
    g.pop

    g.goto fin

    fnd.set!

    # Clean up the stack
    g.swap
    g.pop

    fin.set!
  else
    assign = g.new_label

    old_break = g.break
    new_break = g.new_label
    g.break = new_break

    @value.bytecode(g)

    g.goto assign

    new_break.set!
    if old_break
      g.pop_many 3
      g.push_tagged_nil 0
      g.goto old_break
    end

    g.break = old_break

    assign.set!

    # X: TOS = 3
    # X: 2 + 3
    g.send @op, 1

    # Retain the this value to use as the expression value
    g.dup
    g.move_down 2
    # X: TOS = 5
    g.send @assign, 1
    # X: TOS = 5 (or whatever a=() returns)

    # Discard the methods return value
    g.pop
  end
end