class SyntaxTree::YARV::DefineClass

### Summary

‘defineclass` defines a class. First it pops the superclass off the stack, then it pops the object off the stack that the class should be defined under. It has three arguments: the name of the constant, the instruction sequence associated with the class, and various flags that indicate if it is a singleton class, a module, or a regular class.

### Usage

~~~ruby class Foo end ~~~

Constants

FLAG_HAS_SUPERCLASS
FLAG_SCOPED
TYPE_CLASS
TYPE_MODULE
TYPE_SINGLETON_CLASS

Attributes

class_iseq[R]
flags[R]
name[R]

Public Class Methods

new(name, class_iseq, flags) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 784
def initialize(name, class_iseq, flags)
  @name = name
  @class_iseq = class_iseq
  @flags = flags
end

Public Instance Methods

==(other) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 806
def ==(other)
  other.is_a?(DefineClass) && other.name == name &&
    other.class_iseq == class_iseq && other.flags == flags
end
call(vm) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 823
def call(vm)
  object, superclass = vm.pop(2)

  if name == :singletonclass
    vm.push(vm.run_class_frame(class_iseq, object.singleton_class))
  elsif object.const_defined?(name)
    vm.push(vm.run_class_frame(class_iseq, object.const_get(name)))
  elsif flags & TYPE_MODULE > 0
    clazz = Module.new
    object.const_set(name, clazz)
    vm.push(vm.run_class_frame(class_iseq, clazz))
  else
    clazz =
      if flags & FLAG_HAS_SUPERCLASS > 0
        Class.new(superclass)
      else
        Class.new
      end

    object.const_set(name, clazz)
    vm.push(vm.run_class_frame(class_iseq, clazz))
  end
end
deconstruct_keys(_keys) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 802
def deconstruct_keys(_keys)
  { name: name, class_iseq: class_iseq, flags: flags }
end
disasm(fmt) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 790
def disasm(fmt)
  fmt.enqueue(class_iseq)
  fmt.instruction(
    "defineclass",
    [fmt.object(name), class_iseq.name, fmt.object(flags)]
  )
end
length() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 811
def length
  4
end
pops() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 815
def pops
  2
end
pushes() click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 819
def pushes
  1
end
to_a(_iseq) click to toggle source
# File lib/syntax_tree/yarv/instructions.rb, line 798
def to_a(_iseq)
  [:defineclass, name, class_iseq.to_a, flags]
end