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
Public Class Methods
Source
# File lib/syntax_tree/yarv/instructions.rb, line 837 def initialize(name, class_iseq, flags) @name = name @class_iseq = class_iseq @flags = flags end
Public Instance Methods
Source
# 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
Source
# 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
Source
# File lib/syntax_tree/yarv/instructions.rb, line 855 def deconstruct_keys(_keys) { name: name, class_iseq: class_iseq, flags: flags } end
Source
# 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
Source
# File lib/syntax_tree/yarv/instructions.rb, line 851 def to_a(_iseq) [:defineclass, name, class_iseq.to_a, flags] end