Class: SyntaxTree::YARV::SetClassVariable

Inherits:
Instruction
  • 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

Methods inherited from Instruction

#branch_targets, #canonical, #falls_through?, #leaves?, #pushes, #side_effects?

Constructor Details

#initialize(name, cache) ⇒ SetClassVariable

Returns a new instance of SetClassVariable.



4961
4962
4963
4964
# File 'lib/syntax_tree/yarv/instructions.rb', line 4961

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



4959
4960
4961
# File 'lib/syntax_tree/yarv/instructions.rb', line 4959

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



4959
4960
4961
# File 'lib/syntax_tree/yarv/instructions.rb', line 4959

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



4981
4982
4983
4984
# File 'lib/syntax_tree/yarv/instructions.rb', line 4981

def ==(other)
  other.is_a?(SetClassVariable) && other.name == name &&
    other.cache == cache
end

#call(vm) ⇒ Object



4994
4995
4996
4997
4998
# File 'lib/syntax_tree/yarv/instructions.rb', line 4994

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

#deconstruct_keys(_keys) ⇒ Object



4977
4978
4979
# File 'lib/syntax_tree/yarv/instructions.rb', line 4977

def deconstruct_keys(_keys)
  { name: name, cache: cache }
end

#disasm(fmt) ⇒ Object



4966
4967
4968
4969
4970
4971
# File 'lib/syntax_tree/yarv/instructions.rb', line 4966

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

#lengthObject



4986
4987
4988
# File 'lib/syntax_tree/yarv/instructions.rb', line 4986

def length
  3
end

#popsObject



4990
4991
4992
# File 'lib/syntax_tree/yarv/instructions.rb', line 4990

def pops
  1
end

#to_a(_iseq) ⇒ Object



4973
4974
4975
# File 'lib/syntax_tree/yarv/instructions.rb', line 4973

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