Class: SyntaxTree::YARV::SetClassVariable

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/instructions.rb

Overview

### Summary

setclassvariable looks for a class variable in the current class and sets its value to the value it pops off the top of the stack. It uses an inline cache to reduce the need to lookup the class variable in the class hierarchy every time.

### Usage

~~~ruby @@class_variable = 1 ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cache) ⇒ SetClassVariable



4622
4623
4624
4625
# File 'lib/syntax_tree/yarv/instructions.rb', line 4622

def initialize(name, cache)
  @name = name
  @cache = cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



4620
4621
4622
# File 'lib/syntax_tree/yarv/instructions.rb', line 4620

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



4620
4621
4622
# File 'lib/syntax_tree/yarv/instructions.rb', line 4620

def name
  @name
end

Instance Method Details

#call(vm) ⇒ Object



4654
4655
4656
4657
4658
# File 'lib/syntax_tree/yarv/instructions.rb', line 4654

def call(vm)
  clazz = vm.frame._self
  clazz = clazz.class unless clazz.is_a?(Class)
  clazz.class_variable_set(name, vm.pop)
end

#canonicalObject



4650
4651
4652
# File 'lib/syntax_tree/yarv/instructions.rb', line 4650

def canonical
  self
end

#disasm(fmt) ⇒ Object



4627
4628
4629
4630
4631
4632
# File 'lib/syntax_tree/yarv/instructions.rb', line 4627

def disasm(fmt)
  fmt.instruction(
    "setclassvariable",
    [fmt.object(name), fmt.inline_storage(cache)]
  )
end

#lengthObject



4638
4639
4640
# File 'lib/syntax_tree/yarv/instructions.rb', line 4638

def length
  3
end

#popsObject



4642
4643
4644
# File 'lib/syntax_tree/yarv/instructions.rb', line 4642

def pops
  1
end

#pushesObject



4646
4647
4648
# File 'lib/syntax_tree/yarv/instructions.rb', line 4646

def pushes
  0
end

#to_a(_iseq) ⇒ Object



4634
4635
4636
# File 'lib/syntax_tree/yarv/instructions.rb', line 4634

def to_a(_iseq)
  [:setclassvariable, name, cache]
end