Skip to content

defined_ivar instruction #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/syntax_tree/yarv/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,7 @@ def visit_defined(node)
when Ident
iseq.putobject("local-variable")
when IVar
iseq.putnil
iseq.defined(Defined::TYPE_IVAR, name, "instance-variable")
iseq.defined_ivar(name, iseq.inline_storage, "instance-variable")
when Kw
case name
when :false
Expand Down
15 changes: 13 additions & 2 deletions lib/syntax_tree/yarv/instruction_sequence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -673,12 +673,21 @@ def concatstrings(number)
push(ConcatStrings.new(number))
end

def defineclass(name, class_iseq, flags)
push(DefineClass.new(name, class_iseq, flags))
end

def defined(type, name, message)
push(Defined.new(type, name, message))
end

def defineclass(name, class_iseq, flags)
push(DefineClass.new(name, class_iseq, flags))
def defined_ivar(name, cache, message)
if RUBY_VERSION < "3.3"
push(PutNil.new)
push(Defined.new(Defined::TYPE_IVAR, name, message))
else
push(DefinedIVar.new(name, cache, message))
end
end

def definemethod(name, method_iseq)
Expand Down Expand Up @@ -1058,6 +1067,8 @@ def self.from(source, options = Compiler::Options.new, parent_iseq = nil)
iseq.defineclass(opnds[0], from(opnds[1], options, iseq), opnds[2])
when :defined
iseq.defined(opnds[0], opnds[1], opnds[2])
when :defined_ivar
iseq.defined_ivar(opnds[0], opnds[1], opnds[2])
when :definemethod
iseq.definemethod(opnds[0], from(opnds[1], options, iseq))
when :definesmethod
Expand Down
58 changes: 58 additions & 0 deletions lib/syntax_tree/yarv/instructions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,64 @@ def call(vm)
end
end

# ### Summary
#
# `defined_ivar` checks if an instance variable is defined. It is a
# specialization of the `defined` instruction. It accepts three arguments:
# the name of the instance variable, an inline cache, and the string that
# should be pushed onto the stack in the event that the instance variable
# is defined.
#
# ### Usage
#
# ~~~ruby
# defined?(@value)
# ~~~
#
class DefinedIVar < Instruction
attr_reader :name, :cache, :message

def initialize(name, cache, message)
@name = name
@cache = cache
@message = message
end

def disasm(fmt)
fmt.instruction(
"defined_ivar",
[fmt.object(name), fmt.inline_storage(cache), fmt.object(message)]
)
end

def to_a(_iseq)
[:defined_ivar, name, cache, message]
end

def deconstruct_keys(_keys)
{ name: name, cache: cache, message: message }
end

def ==(other)
other.is_a?(DefinedIVar) && other.name == name &&
other.cache == cache && other.message == message
end

def length
4
end

def pushes
1
end

def call(vm)
result = (message if vm.frame._self.instance_variable_defined?(name))

vm.push(result)
end
end

# ### Summary
#
# `definemethod` defines a method on the class of the current value of
Expand Down