51
51
import static com .oracle .graal .python .runtime .exception .PythonErrorType .TypeError ;
52
52
53
53
import com .oracle .graal .python .PythonLanguage ;
54
- import com .oracle .graal .python .builtins .Builtin ;
55
54
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
56
55
import com .oracle .graal .python .builtins .modules .BuiltinFunctions .GetAttrNode ;
57
56
import com .oracle .graal .python .builtins .modules .PythonCextBuiltins ;
93
92
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes ;
94
93
import com .oracle .graal .python .builtins .objects .complex .PComplex ;
95
94
import com .oracle .graal .python .builtins .objects .floats .PFloat ;
96
- import com .oracle .graal .python .builtins .objects .function .PArguments ;
97
95
import com .oracle .graal .python .builtins .objects .function .PKeyword ;
98
- import com .oracle .graal .python .builtins .objects .function .Signature ;
99
96
import com .oracle .graal .python .builtins .objects .getsetdescriptor .DescriptorDeleteMarker ;
100
97
import com .oracle .graal .python .builtins .objects .ints .PInt ;
101
98
import com .oracle .graal .python .builtins .objects .module .PythonModule ;
111
108
import com .oracle .graal .python .nodes .PGuards ;
112
109
import com .oracle .graal .python .nodes .PNodeWithContext ;
113
110
import com .oracle .graal .python .nodes .PRaiseNode ;
114
- import com .oracle .graal .python .nodes .PRootNode ;
115
111
import com .oracle .graal .python .nodes .SpecialMethodNames ;
116
112
import com .oracle .graal .python .nodes .attributes .ReadAttributeFromObjectNode ;
117
113
import com .oracle .graal .python .nodes .call .CallNode ;
118
- import com .oracle .graal .python .nodes .call .CallTargetInvokeNode ;
119
114
import com .oracle .graal .python .nodes .call .special .CallBinaryMethodNode ;
120
115
import com .oracle .graal .python .nodes .call .special .CallTernaryMethodNode ;
121
116
import com .oracle .graal .python .nodes .call .special .CallUnaryMethodNode ;
122
117
import com .oracle .graal .python .nodes .call .special .LookupAndCallUnaryNode .LookupAndCallUnaryDynamicNode ;
123
118
import com .oracle .graal .python .nodes .classes .IsSubtypeNode ;
119
+ import com .oracle .graal .python .nodes .expression .ExpressionNode ;
124
120
import com .oracle .graal .python .nodes .frame .GetCurrentFrameRef ;
125
121
import com .oracle .graal .python .nodes .object .GetClassNode ;
126
122
import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
127
123
import com .oracle .graal .python .nodes .truffle .PythonTypes ;
128
124
import com .oracle .graal .python .nodes .util .CannotCastException ;
129
125
import com .oracle .graal .python .nodes .util .CastToJavaLongLossyNode ;
130
- import com .oracle .graal .python .runtime .ExecutionContext .CalleeContext ;
131
126
import com .oracle .graal .python .runtime .PythonContext ;
132
127
import com .oracle .graal .python .runtime .PythonCore ;
133
128
import com .oracle .graal .python .runtime .PythonOptions ;
140
135
import com .oracle .truffle .api .Assumption ;
141
136
import com .oracle .truffle .api .CompilerAsserts ;
142
137
import com .oracle .truffle .api .CompilerDirectives ;
143
- import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
144
138
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
145
- import com .oracle .truffle .api .RootCallTarget ;
146
139
import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
147
140
import com .oracle .truffle .api .TruffleLogger ;
148
141
import com .oracle .truffle .api .dsl .Cached ;
@@ -2475,50 +2468,48 @@ public static PCallCapiFunction getUncached() {
2475
2468
return CExtNodesFactory .PCallCapiFunctionNodeGen .getUncached ();
2476
2469
}
2477
2470
}
2478
-
2471
+
2472
+ /**
2473
+ * Simple enum to abstract over common error indication values used in C extensions. We use this
2474
+ * enum instead of concrete values to be able to safely share them between contexts.
2475
+ */
2479
2476
public enum MayRaiseErrorResult {
2480
- NATIVE_NULL , NONE , INT , FLOAT
2477
+ NATIVE_NULL ,
2478
+ NONE ,
2479
+ INT ,
2480
+ FLOAT
2481
2481
}
2482
2482
2483
- // -----------------------------------------------------------------------------------------------------------------
2484
- @ Builtin (takesVarArgs = true )
2485
- public static class MayRaiseNode extends PRootNode {
2486
- @ Child private CallTargetInvokeNode callTargetInvokeNode ;
2483
+ /**
2484
+ * A fake-expression node that wraps an expression node with a {@code try-catch} and any catched
2485
+ * Python exception will be transformed to native and the pre-defined error result (specified
2486
+ * with enum {@link MayRaiseErrorResult}) will be returned.
2487
+ */
2488
+ public static final class MayRaiseNode extends ExpressionNode {
2489
+ @ Child private ExpressionNode wrappedBody ;
2487
2490
@ Child private TransformExceptionToNativeNode transformExceptionToNativeNode ;
2488
- @ Child private CalleeContext calleeContext ;
2489
-
2491
+
2490
2492
@ Child private GetNativeNullNode getNativeNullNode ;
2491
2493
2492
- private final Signature signature ;
2493
2494
private final MayRaiseErrorResult errorResult ;
2494
2495
2495
- public MayRaiseNode (PythonLanguage lang , Signature sign , RootCallTarget ct , MayRaiseErrorResult errorResult ) {
2496
- super (lang );
2497
- this .signature = sign ;
2498
- this .callTargetInvokeNode = CallTargetInvokeNode .create (ct , false , false );
2499
- this .calleeContext = CalleeContext .create ();
2496
+ MayRaiseNode (ExpressionNode wrappedBody , MayRaiseErrorResult errorResult ) {
2497
+ this .wrappedBody = wrappedBody ;
2500
2498
this .errorResult = errorResult ;
2501
2499
}
2502
2500
2501
+ public static MayRaiseNode create (ExpressionNode nodeToWrap , MayRaiseErrorResult errorResult ) {
2502
+ return new MayRaiseNode (nodeToWrap , errorResult );
2503
+ }
2504
+
2503
2505
@ Override
2504
- public final Object execute (VirtualFrame frame ) {
2505
- Object [] arguments = frame .getArguments ();
2506
- int userArgumentLength = PArguments .getUserArgumentLength (arguments );
2507
- Object [] newArguments = PArguments .create (userArgumentLength );
2508
- // just copy user arguments, varargs and kwargs
2509
- System .arraycopy (arguments , PArguments .USER_ARGUMENTS_OFFSET , newArguments , PArguments .USER_ARGUMENTS_OFFSET , userArgumentLength );
2510
- PArguments .setVariableArguments (newArguments , PArguments .getVariableArguments (arguments ));
2511
- PArguments .setKeywordArguments (newArguments , PArguments .getKeywordArguments (arguments ));
2512
-
2513
- calleeContext .enter (frame );
2506
+ public Object execute (VirtualFrame frame ) {
2514
2507
try {
2515
- return callTargetInvokeNode .execute (frame , null , PArguments . getGlobals ( arguments ), PArguments . getClosure ( arguments ), newArguments );
2508
+ return wrappedBody .execute (frame );
2516
2509
} catch (PException e ) {
2517
2510
// transformExceptionToNativeNode acts as a branch profile
2518
2511
ensureTransformExceptionToNativeNode ().execute (frame , e );
2519
2512
return getErrorResult ();
2520
- } finally {
2521
- calleeContext .exit (frame , this );
2522
2513
}
2523
2514
}
2524
2515
@@ -2529,15 +2520,15 @@ private TransformExceptionToNativeNode ensureTransformExceptionToNativeNode() {
2529
2520
}
2530
2521
return transformExceptionToNativeNode ;
2531
2522
}
2532
-
2523
+
2533
2524
private Object getErrorResult () {
2534
- switch (errorResult ) {
2525
+ switch (errorResult ) {
2535
2526
case INT :
2536
2527
return -1 ;
2537
2528
case FLOAT :
2538
2529
return -1.0 ;
2539
2530
case NONE :
2540
- return PNone .NONE ;
2531
+ return PNone .NONE ;
2541
2532
case NATIVE_NULL :
2542
2533
if (getNativeNullNode == null ) {
2543
2534
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -2547,16 +2538,6 @@ private Object getErrorResult() {
2547
2538
}
2548
2539
throw CompilerDirectives .shouldNotReachHere ();
2549
2540
}
2550
-
2551
- @ Override
2552
- public Signature getSignature () {
2553
- return signature ;
2554
- }
2555
-
2556
- @ Override
2557
- public boolean isPythonInternal () {
2558
- return true ;
2559
- }
2560
2541
}
2561
2542
2562
2543
// -----------------------------------------------------------------------------------------------------------------
0 commit comments