Class: SyntaxTree::YARV::DefineClass

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

Overview

### 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 ~~~

Constant Summary collapse

TYPE_CLASS =
0
TYPE_SINGLETON_CLASS =
1
TYPE_MODULE =
2
FLAG_SCOPED =
8
FLAG_HAS_SUPERCLASS =
16

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

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

Constructor Details

#initialize(name, class_iseq, flags) ⇒ DefineClass

Returns a new instance of DefineClass.



784
785
786
787
788
# File 'lib/syntax_tree/yarv/instructions.rb', line 784

def initialize(name, class_iseq, flags)
  @name = name
  @class_iseq = class_iseq
  @flags = flags
end

Instance Attribute Details

#class_iseqObject (readonly)

Returns the value of attribute class_iseq.



782
783
784
# File 'lib/syntax_tree/yarv/instructions.rb', line 782

def class_iseq
  @class_iseq
end

#flagsObject (readonly)

Returns the value of attribute flags.



782
783
784
# File 'lib/syntax_tree/yarv/instructions.rb', line 782

def flags
  @flags
end

#nameObject (readonly)

Returns the value of attribute name.



782
783
784
# File 'lib/syntax_tree/yarv/instructions.rb', line 782

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



806
807
808
809
# 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) ⇒ Object



823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# 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) ⇒ Object



802
803
804
# File 'lib/syntax_tree/yarv/instructions.rb', line 802

def deconstruct_keys(_keys)
  { name: name, class_iseq: class_iseq, flags: flags }
end

#disasm(fmt) ⇒ Object



790
791
792
793
794
795
796
# 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

#lengthObject



811
812
813
# File 'lib/syntax_tree/yarv/instructions.rb', line 811

def length
  4
end

#popsObject



815
816
817
# File 'lib/syntax_tree/yarv/instructions.rb', line 815

def pops
  2
end

#pushesObject



819
820
821
# File 'lib/syntax_tree/yarv/instructions.rb', line 819

def pushes
  1
end

#to_a(_iseq) ⇒ Object



798
799
800
# File 'lib/syntax_tree/yarv/instructions.rb', line 798

def to_a(_iseq)
  [:defineclass, name, class_iseq.to_a, flags]
end