Class: SyntaxTree::YARV::Defined
Overview
Summary
defined checks if the top value of the stack is defined. If it is, it
pushes its value onto the stack. Otherwise it pushes nil.
Usage
defined?(x)
Constant Summary
collapse
- TYPE_NIL =
1
- TYPE_IVAR =
2
- TYPE_LVAR =
3
- TYPE_GVAR =
4
- TYPE_CVAR =
5
- TYPE_CONST =
6
- TYPE_METHOD =
7
- TYPE_YIELD =
8
- TYPE_ZSUPER =
9
- TYPE_SELF =
10
- TYPE_TRUE =
11
- TYPE_FALSE =
12
- TYPE_ASGN =
13
- TYPE_EXPR =
14
- TYPE_REF =
15
- TYPE_FUNC =
16
- TYPE_CONST_FROM =
17
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Instruction
#branch_targets, #canonical, #falls_through?, #leaves?, #side_effects?
Constructor Details
#initialize(type, name, message) ⇒ Defined
Returns a new instance of Defined.
933
934
935
936
937
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 933
def initialize(type, name, message)
@type = type
@name = name
@message = message
end
|
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
931
932
933
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 931
def message
@message
end
|
#name ⇒ Object
Returns the value of attribute name.
931
932
933
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 931
def name
@name
end
|
#type ⇒ Object
Returns the value of attribute type.
931
932
933
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 931
def type
@type
end
|
Instance Method Details
#==(other) ⇒ Object
992
993
994
995
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 992
def ==(other)
other.is_a?(Defined) && other.type == type && other.name == name &&
other.message == message
end
|
#call(vm) ⇒ Object
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 1009
def call(vm)
object = vm.pop
result =
case type
when TYPE_NIL, TYPE_SELF, TYPE_TRUE, TYPE_FALSE, TYPE_ASGN, TYPE_EXPR
message
when TYPE_IVAR
message if vm.frame._self.instance_variable_defined?(name)
when TYPE_LVAR
raise NotImplementedError, "defined TYPE_LVAR"
when TYPE_GVAR
message if global_variables.include?(name)
when TYPE_CVAR
clazz = vm.frame._self
clazz = clazz.singleton_class unless clazz.is_a?(Module)
message if clazz.class_variable_defined?(name)
when TYPE_CONST
clazz = vm.frame._self
clazz = clazz.singleton_class unless clazz.is_a?(Module)
message if clazz.const_defined?(name)
when TYPE_METHOD
raise NotImplementedError, "defined TYPE_METHOD"
when TYPE_YIELD
raise NotImplementedError, "defined TYPE_YIELD"
when TYPE_ZSUPER
raise NotImplementedError, "defined TYPE_ZSUPER"
when TYPE_REF
raise NotImplementedError, "defined TYPE_REF"
when TYPE_FUNC
message if object.respond_to?(name, true)
when TYPE_CONST_FROM
defined =
vm.frame.nesting.any? { |scope| scope.const_defined?(name, true) }
message if defined
end
vm.push(result)
end
|
#deconstruct_keys(_keys) ⇒ Object
988
989
990
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 988
def deconstruct_keys(_keys)
{ type: type, name: name, message: message }
end
|
#length ⇒ Object
997
998
999
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 997
def length
4
end
|
#pops ⇒ Object
1001
1002
1003
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 1001
def pops
1
end
|
#pushes ⇒ Object
1005
1006
1007
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 1005
def pushes
1
end
|
#to_a(_iseq) ⇒ Object
984
985
986
|
# File 'lib/syntax_tree/yarv/instructions.rb', line 984
def to_a(_iseq)
[:defined, type, name, message]
end
|