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.



837
838
839
840
841
# File 'lib/syntax_tree/yarv/instructions.rb', line 837

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.



835
836
837
# File 'lib/syntax_tree/yarv/instructions.rb', line 835

def class_iseq
  @class_iseq
end

#flagsObject (readonly)

Returns the value of attribute flags.



835
836
837
# File 'lib/syntax_tree/yarv/instructions.rb', line 835

def flags
  @flags
end

#nameObject (readonly)

Returns the value of attribute name.



835
836
837
# File 'lib/syntax_tree/yarv/instructions.rb', line 835

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



859
860
861
862
# File 'lib/syntax_tree/yarv/instructions.rb', line 859

def ==(other)
  other.is_a?(DefineClass) && other.name == name &&
    other.class_iseq == class_iseq && other.flags == flags
end

#call(vm) ⇒ Object



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'lib/syntax_tree/yarv/instructions.rb', line 876

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



855
856
857
# File 'lib/syntax_tree/yarv/instructions.rb', line 855

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

#disasm(fmt) ⇒ Object



843
844
845
846
847
848
849
# File 'lib/syntax_tree/yarv/instructions.rb', line 843

def disasm(fmt)
  fmt.enqueue(class_iseq)
  fmt.instruction(
    "defineclass",
    [fmt.object(name), class_iseq.name, fmt.object(flags)]
  )
end

#lengthObject



864
865
866
# File 'lib/syntax_tree/yarv/instructions.rb', line 864

def length
  4
end

#popsObject



868
869
870
# File 'lib/syntax_tree/yarv/instructions.rb', line 868

def pops
  2
end

#pushesObject



872
873
874
# File 'lib/syntax_tree/yarv/instructions.rb', line 872

def pushes
  1
end

#to_a(_iseq) ⇒ Object



851
852
853
# File 'lib/syntax_tree/yarv/instructions.rb', line 851

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