Class: SyntaxTree::YARV::DefineSMethod

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

Overview

### Summary

‘definesmethod` defines a method on the singleton class of the current value of `self`. It accepts two arguments. The first is the name of the method being defined. The second is the instruction sequence representing the body of the method. It pops the object off the stack that the method should be defined on.

### Usage

~~~ruby def self.value = “value” ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

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

Constructor Details

#initialize(method_name, method_iseq) ⇒ DefineSMethod

Returns a new instance of DefineSMethod.



1140
1141
1142
1143
# File 'lib/syntax_tree/yarv/instructions.rb', line 1140

def initialize(method_name, method_iseq)
  @method_name = method_name
  @method_iseq = method_iseq
end

Instance Attribute Details

#method_iseqObject (readonly)

Returns the value of attribute method_iseq.



1138
1139
1140
# File 'lib/syntax_tree/yarv/instructions.rb', line 1138

def method_iseq
  @method_iseq
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



1138
1139
1140
# File 'lib/syntax_tree/yarv/instructions.rb', line 1138

def method_name
  @method_name
end

Instance Method Details

#==(other) ⇒ Object



1161
1162
1163
1164
# File 'lib/syntax_tree/yarv/instructions.rb', line 1161

def ==(other)
  other.is_a?(DefineSMethod) && other.method_name == method_name &&
    other.method_iseq == method_iseq
end

#call(vm) ⇒ Object



1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
# File 'lib/syntax_tree/yarv/instructions.rb', line 1174

def call(vm)
  name = method_name
  nesting = vm.frame.nesting
  iseq = method_iseq

  vm
    .frame
    ._self
    .__send__(:define_singleton_method, name) do |*args, **kwargs, &block|
      vm.run_method_frame(
        name,
        nesting,
        iseq,
        self,
        *args,
        **kwargs,
        &block
      )
    end
end

#deconstruct_keys(_keys) ⇒ Object



1157
1158
1159
# File 'lib/syntax_tree/yarv/instructions.rb', line 1157

def deconstruct_keys(_keys)
  { method_name: method_name, method_iseq: method_iseq }
end

#disasm(fmt) ⇒ Object



1145
1146
1147
1148
1149
1150
1151
# File 'lib/syntax_tree/yarv/instructions.rb', line 1145

def disasm(fmt)
  fmt.enqueue(method_iseq)
  fmt.instruction(
    "definesmethod",
    [fmt.object(method_name), method_iseq.name]
  )
end

#lengthObject



1166
1167
1168
# File 'lib/syntax_tree/yarv/instructions.rb', line 1166

def length
  3
end

#popsObject



1170
1171
1172
# File 'lib/syntax_tree/yarv/instructions.rb', line 1170

def pops
  1
end

#to_a(_iseq) ⇒ Object



1153
1154
1155
# File 'lib/syntax_tree/yarv/instructions.rb', line 1153

def to_a(_iseq)
  [:definesmethod, method_name, method_iseq.to_a]
end