45
45
46
46
import com .oracle .graal .python .PythonLanguage ;
47
47
import com .oracle .graal .python .builtins .Builtin ;
48
- import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
49
48
import com .oracle .graal .python .builtins .objects .PNone ;
50
49
import com .oracle .graal .python .builtins .objects .cext .hpy .GraalHPyNodes .PCallHPyFunction ;
51
50
import com .oracle .graal .python .builtins .objects .cext .hpy .GraalHPyNodesFactory .PCallHPyFunctionNodeGen ;
52
51
import com .oracle .graal .python .builtins .objects .cext .hpy .GraalHPyObjectBuiltinsFactory .HPyObjectNewNodeGen ;
53
52
import com .oracle .graal .python .builtins .objects .function .PBuiltinFunction ;
54
53
import com .oracle .graal .python .builtins .objects .function .PKeyword ;
55
54
import com .oracle .graal .python .builtins .objects .type .PythonClass ;
56
- import com .oracle .graal .python .builtins .objects .type .SpecialMethodSlot ;
57
- import com .oracle .graal .python .builtins .objects .type .TypeNodes .GetSuperClassNode ;
58
- import com .oracle .graal .python .builtins .objects .type .TypeNodesFactory .GetSuperClassNodeGen ;
59
55
import com .oracle .graal .python .nodes .SpecialMethodNames ;
60
- import com .oracle .graal .python .nodes .attributes .LookupCallableSlotInMRONode ;
61
56
import com .oracle .graal .python .nodes .attributes .WriteAttributeToObjectNode ;
62
57
import com .oracle .graal .python .nodes .call .special .CallVarargsMethodNode ;
63
58
import com .oracle .graal .python .nodes .function .BuiltinFunctionRootNode ;
64
59
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
65
60
import com .oracle .graal .python .nodes .function .builtins .PythonVarargsBuiltinNode ;
66
- import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
67
61
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
68
62
import com .oracle .graal .python .util .PythonUtils ;
69
63
import com .oracle .truffle .api .CompilerAsserts ;
@@ -122,15 +116,22 @@ public List<Class<? extends Node>> getExecutionSignature() {
122
116
123
117
@ Builtin (name = SpecialMethodNames .__NEW__ , minNumOfPositionalArgs = 1 , takesVarArgs = true , takesVarKeywordArgs = true )
124
118
abstract static class HPyObjectNewNode extends PythonVarargsBuiltinNode {
119
+ private static final String KW_SUPER_CONSTRUCTOR = "$supercons" ;
120
+ private static final String [] KEYWORDS_HIDDEN_CALLABLE = {KW_SUPER_CONSTRUCTOR };
121
+
122
+ private static PKeyword [] createKwDefaults (Object superConstructor ) {
123
+ if (superConstructor != null ) {
124
+ return new PKeyword []{new PKeyword (KW_SUPER_CONSTRUCTOR , superConstructor )};
125
+ }
126
+ return PKeyword .EMPTY_KEYWORDS ;
127
+ }
128
+
125
129
private static final Builtin BUILTIN = HPyObjectNewNode .class .getAnnotation (Builtin .class );
126
130
private static final TruffleLogger LOGGER = PythonLanguage .getLogger (HPyObjectNewNode .class );
127
131
128
132
@ Child private PCallHPyFunction callHPyFunctionNode ;
129
- @ Child private LookupCallableSlotInMRONode lookupNewNode ;
130
133
@ Child private CallVarargsMethodNode callNewNode ;
131
- @ Child private GetSuperClassNode getBaseClassNode ;
132
134
@ Child private WriteAttributeToObjectNode writeNativeSpaceNode ;
133
- @ Child private IsBuiltinClassProfile isObjectProfile ;
134
135
135
136
@ Override
136
137
public Object varArgExecute (VirtualFrame frame , Object self , Object [] arguments , PKeyword [] keywords ) throws VarargsBuiltinDirectInvocationNotSupported {
@@ -178,14 +179,13 @@ Object doGeneric(VirtualFrame frame, Object explicitSelf, Object[] arguments, PK
178
179
}
179
180
}
180
181
181
- Object baseClass = ensureGetBaseClassNode (). execute ( self );
182
+ Object inheritedConstructor = extractInheritedConstructor ( arguments , keywords );
182
183
Object pythonObject ;
183
- if (ensureIsObjectProfile (). profileClass ( baseClass , PythonBuiltinClassType . PythonObject ) ) {
184
+ if (inheritedConstructor == null ) {
184
185
// fast-path if the super class is 'object'
185
186
pythonObject = factory ().createPythonHPyObject (self , dataPtr );
186
187
} else {
187
- Object superNew = ensureLookupNewNode ().execute (baseClass );
188
- pythonObject = ensureCallNewNode ().execute (frame , superNew , argsWithSelf , keywords );
188
+ pythonObject = ensureCallNewNode ().execute (frame , inheritedConstructor , argsWithSelf , keywords );
189
189
190
190
/*
191
191
* Since we are creating an object with an unknown constructor, the Java type may be
@@ -196,13 +196,15 @@ Object doGeneric(VirtualFrame frame, Object explicitSelf, Object[] arguments, PK
196
196
ensureWriteNativeSpaceNode ().execute (pythonObject , GraalHPyDef .OBJECT_HPY_NATIVE_SPACE , dataPtr );
197
197
}
198
198
}
199
-
200
- if (pythonObject == null ) {
201
- pythonObject = factory ().createPythonObject (self );
202
- }
203
199
return pythonObject ;
204
200
}
205
201
202
+ @ SuppressWarnings ("unused" )
203
+ private Object extractInheritedConstructor (Object [] arguments , PKeyword [] keywords ) {
204
+ // TODO(fa): not yet implemented
205
+ return null ;
206
+ }
207
+
206
208
private PCallHPyFunction ensureCallHPyFunctionNode () {
207
209
if (callHPyFunctionNode == null ) {
208
210
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -219,22 +221,6 @@ private CallVarargsMethodNode ensureCallNewNode() {
219
221
return callNewNode ;
220
222
}
221
223
222
- private LookupCallableSlotInMRONode ensureLookupNewNode () {
223
- if (lookupNewNode == null ) {
224
- CompilerDirectives .transferToInterpreterAndInvalidate ();
225
- lookupNewNode = insert (LookupCallableSlotInMRONode .create (SpecialMethodSlot .New ));
226
- }
227
- return lookupNewNode ;
228
- }
229
-
230
- private GetSuperClassNode ensureGetBaseClassNode () {
231
- if (getBaseClassNode == null ) {
232
- CompilerDirectives .transferToInterpreterAndInvalidate ();
233
- getBaseClassNode = insert (GetSuperClassNodeGen .create ());
234
- }
235
- return getBaseClassNode ;
236
- }
237
-
238
224
private WriteAttributeToObjectNode ensureWriteNativeSpaceNode () {
239
225
if (writeNativeSpaceNode == null ) {
240
226
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -243,20 +229,12 @@ private WriteAttributeToObjectNode ensureWriteNativeSpaceNode() {
243
229
return writeNativeSpaceNode ;
244
230
}
245
231
246
- private IsBuiltinClassProfile ensureIsObjectProfile () {
247
- if (isObjectProfile == null ) {
248
- CompilerDirectives .transferToInterpreterAndInvalidate ();
249
- isObjectProfile = insert (IsBuiltinClassProfile .create ());
250
- }
251
- return isObjectProfile ;
252
- }
253
-
254
232
@ TruffleBoundary
255
- public static PBuiltinFunction createBuiltinFunction (PythonLanguage language ) {
233
+ public static PBuiltinFunction createBuiltinFunction (PythonLanguage language , Object superConstructor ) {
256
234
RootCallTarget callTarget = language .createCachedCallTarget (l -> new BuiltinFunctionRootNode (l , BUILTIN , new HPyObjectNewNodeFactory <>(HPyObjectNewNodeGen .create ()), true ),
257
235
HPyObjectNewNode .class , BUILTIN .name ());
258
236
int flags = PBuiltinFunction .getFlags (BUILTIN , callTarget );
259
- return PythonObjectFactory .getUncached ().createBuiltinFunction (SpecialMethodNames .__NEW__ , null , 0 , flags , callTarget );
237
+ return PythonObjectFactory .getUncached ().createBuiltinFunction (SpecialMethodNames .__NEW__ , null , PythonUtils . EMPTY_OBJECT_ARRAY , createKwDefaults ( superConstructor ) , flags , callTarget );
260
238
}
261
239
}
262
240
}
0 commit comments