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



5307
5308
5309
5310
# File 'lib/syntax_tree/yarv/instructions.rb', line 5307

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

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



5305
5306
5307
# File 'lib/syntax_tree/yarv/instructions.rb', line 5305

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



5305
5306
5307
# File 'lib/syntax_tree/yarv/instructions.rb', line 5305

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



5327
5328
5329
5330
# File 'lib/syntax_tree/yarv/instructions.rb', line 5327

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

#call(vm) ⇒ Object



5348
5349
5350
5351
5352
# File 'lib/syntax_tree/yarv/instructions.rb', line 5348

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

#canonicalObject



5344
5345
5346
# File 'lib/syntax_tree/yarv/instructions.rb', line 5344

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



5323
5324
5325
# File 'lib/syntax_tree/yarv/instructions.rb', line 5323

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

#disasm(fmt) ⇒ Object



5312
5313
5314
5315
5316
5317
# File 'lib/syntax_tree/yarv/instructions.rb', line 5312

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

#lengthObject



5332
5333
5334
# File 'lib/syntax_tree/yarv/instructions.rb', line 5332

def length
  3
end

#popsObject



5336
5337
5338
# File 'lib/syntax_tree/yarv/instructions.rb', line 5336

def pops
  1
end

#pushesObject



5340
5341
5342
# File 'lib/syntax_tree/yarv/instructions.rb', line 5340

def pushes
  0
end

#to_a(_iseq) ⇒ Object



5319
5320
5321
# File 'lib/syntax_tree/yarv/instructions.rb', line 5319

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