38
38
*/
39
39
package com .oracle .graal .python .builtins .objects .referencetype ;
40
40
41
+ import static com .oracle .graal .python .nodes .SpecialAttributeNames .__NAME__ ;
42
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__CALLBACK__ ;
41
43
import static com .oracle .graal .python .nodes .SpecialMethodNames .__CALL__ ;
44
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__EQ__ ;
42
45
import static com .oracle .graal .python .nodes .SpecialMethodNames .__HASH__ ;
46
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__NE__ ;
47
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__REPR__ ;
43
48
44
49
import java .util .List ;
45
50
46
51
import com .oracle .graal .python .builtins .Builtin ;
47
52
import com .oracle .graal .python .builtins .CoreFunctions ;
48
53
import com .oracle .graal .python .builtins .PythonBuiltins ;
54
+ import com .oracle .graal .python .builtins .objects .PNone ;
55
+ import com .oracle .graal .python .nodes .attributes .LookupInheritedAttributeNode ;
56
+ import com .oracle .graal .python .nodes .expression .BinaryComparisonNode ;
49
57
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
58
+ import com .oracle .graal .python .runtime .exception .PythonErrorType ;
59
+ import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
60
+ import com .oracle .truffle .api .dsl .Cached ;
61
+ import com .oracle .truffle .api .dsl .Fallback ;
50
62
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
51
63
import com .oracle .truffle .api .dsl .NodeFactory ;
52
64
import com .oracle .truffle .api .dsl .Specialization ;
@@ -59,12 +71,12 @@ protected List<? extends NodeFactory<? extends PythonBuiltinNode>> getNodeFactor
59
71
}
60
72
61
73
// ref.__callback__
62
- @ Builtin (name = "__callback__" , fixedNumOfArguments = 1 )
74
+ @ Builtin (name = __CALLBACK__ , fixedNumOfArguments = 1 )
63
75
@ GenerateNodeFactory
64
76
public abstract static class RefTypeCallbackPropertyNode extends PythonBuiltinNode {
65
77
@ Specialization
66
- public Object getCallback (PReferenceType referenceType ) {
67
- return referenceType . __callback__ ();
78
+ public Object getCallback (PReferenceType self ) {
79
+ return self . getCallback ();
68
80
}
69
81
}
70
82
@@ -73,18 +85,79 @@ public Object getCallback(PReferenceType referenceType) {
73
85
@ GenerateNodeFactory
74
86
public abstract static class RefTypeCallNode extends PythonBuiltinNode {
75
87
@ Specialization
76
- public Object call (PReferenceType referenceType ) {
77
- return referenceType . __call__ ();
88
+ public Object call (PReferenceType self ) {
89
+ return self . getPyObject ();
78
90
}
79
91
}
80
92
81
93
// ref.__hash__
82
94
@ Builtin (name = __HASH__ , fixedNumOfArguments = 1 )
83
95
@ GenerateNodeFactory
84
96
public abstract static class RefTypeHashNode extends PythonBuiltinNode {
85
- @ Specialization
86
- public Object hash (PReferenceType referenceType ) {
87
- return referenceType .hashCode ();
97
+ @ Specialization (guards = "self.getObject() != null" )
98
+ public int hash (PReferenceType self ) {
99
+ return self .getHash ();
100
+ }
101
+
102
+ @ Fallback
103
+ public int hash (@ SuppressWarnings ("unused" ) Object self ) {
104
+ throw raise (PythonErrorType .TypeError , "weak object has gone away" );
105
+ }
106
+ }
107
+
108
+ // ref.__repr__
109
+ @ Builtin (name = __REPR__ , fixedNumOfArguments = 1 )
110
+ @ GenerateNodeFactory
111
+ public abstract static class RefTypeReprNode extends PythonBuiltinNode {
112
+ @ Specialization (guards = "self.getObject() == null" )
113
+ @ TruffleBoundary
114
+ public String repr (PReferenceType self ) {
115
+ return String .format ("<weakref at %s; dead>" , self .hashCode ());
116
+ }
117
+
118
+ @ Specialization (guards = "self.getObject() != null" )
119
+ @ TruffleBoundary
120
+ public String repr (PReferenceType self ,
121
+ @ Cached ("create()" ) LookupInheritedAttributeNode getNameNode ) {
122
+ Object object = self .getObject ();
123
+ Object name = getNameNode .execute (object , __NAME__ );
124
+ if (name == PNone .NO_VALUE ) {
125
+ return String .format ("<weakref at %s; to '%s' at %s>" , self .hashCode (), object , object .hashCode ());
126
+ } else {
127
+ return String .format ("<weakref at %s; to '%s' at %s (%s)>" , self .hashCode (), object , object .hashCode (), name );
128
+ }
129
+ }
130
+ }
131
+
132
+ // ref.__eq__
133
+ @ Builtin (name = __EQ__ , fixedNumOfArguments = 2 )
134
+ @ GenerateNodeFactory
135
+ public abstract static class RefTypeEqNode extends PythonBuiltinNode {
136
+ @ Specialization (guards = {"self.getObject() != null" , "other.getObject() != null" })
137
+ public boolean eq (PReferenceType self , PReferenceType other ,
138
+ @ Cached ("create(__EQ__, __EQ__, __EQ__)" ) BinaryComparisonNode eqNode ) {
139
+ return eqNode .executeBool (self .getObject (), other .getObject ());
140
+ }
141
+
142
+ @ Specialization (guards = "self.getObject() == null || other.getObject() == null" )
143
+ public boolean eq (PReferenceType self , PReferenceType other ) {
144
+ return self == other ;
145
+ }
146
+ }
147
+
148
+ // ref.__ne__
149
+ @ Builtin (name = __NE__ , fixedNumOfArguments = 2 )
150
+ @ GenerateNodeFactory
151
+ public abstract static class RefTypeNeNode extends PythonBuiltinNode {
152
+ @ Specialization (guards = {"self.getObject() != null" , "other.getObject() != null" })
153
+ public boolean ne (PReferenceType self , PReferenceType other ,
154
+ @ Cached ("create(__NE__, __NE__, __NE__)" ) BinaryComparisonNode neNode ) {
155
+ return neNode .executeBool (self .getObject (), other .getObject ());
156
+ }
157
+
158
+ @ Specialization (guards = "self.getObject() == null || other.getObject() == null" )
159
+ public boolean ne (PReferenceType self , PReferenceType other ) {
160
+ return self != other ;
88
161
}
89
162
}
90
163
}
0 commit comments