36
36
import static com .oracle .graal .python .nodes .BuiltinNames .DIR ;
37
37
import static com .oracle .graal .python .nodes .BuiltinNames .DIVMOD ;
38
38
import static com .oracle .graal .python .nodes .BuiltinNames .EVAL ;
39
+ import static com .oracle .graal .python .nodes .BuiltinNames .EXEC ;
39
40
import static com .oracle .graal .python .nodes .BuiltinNames .GETATTR ;
40
41
import static com .oracle .graal .python .nodes .BuiltinNames .HASH ;
41
42
import static com .oracle .graal .python .nodes .BuiltinNames .ID ;
96
97
import com .oracle .graal .python .builtins .objects .common .SequenceNodes ;
97
98
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes ;
98
99
import com .oracle .graal .python .builtins .objects .dict .PDict ;
100
+ import com .oracle .graal .python .builtins .objects .frame .PFrame ;
99
101
import com .oracle .graal .python .builtins .objects .function .Arity ;
100
102
import com .oracle .graal .python .builtins .objects .function .PArguments ;
101
103
import com .oracle .graal .python .builtins .objects .function .PFunction ;
172
174
import com .oracle .truffle .api .frame .FrameSlot ;
173
175
import com .oracle .truffle .api .frame .VirtualFrame ;
174
176
import com .oracle .truffle .api .nodes .ExplodeLoop ;
177
+ import com .oracle .truffle .api .nodes .IndirectCallNode ;
175
178
import com .oracle .truffle .api .nodes .Node ;
176
179
import com .oracle .truffle .api .nodes .NodeUtil ;
177
180
import com .oracle .truffle .api .nodes .NodeVisitor ;
@@ -595,31 +598,61 @@ private static Object evalNode(RootCallTarget callTarget, PythonObject globals,
595
598
}
596
599
}
597
600
601
+ @ Builtin (name = EXEC , fixedNumOfPositionalArgs = 1 , parameterNames = {"source" }, keywordArguments = {"globals" , "locals" })
602
+ @ GenerateNodeFactory
603
+ abstract static class ExecNode extends PythonBuiltinNode {
604
+ @ Child CompileNode compileNode = CompileNode .create ();
605
+ @ Child IndirectCallNode indirectCallNode = IndirectCallNode .create ();
606
+
607
+ @ Specialization (guards = {"isNoValue(globals)" , "isNoValue(locals)" })
608
+ PNone execDefault (VirtualFrame frame , Object source , @ SuppressWarnings ("unused" ) PNone globals , @ SuppressWarnings ("unused" ) PNone locals ,
609
+ @ Cached ("create()" ) ReadCallerFrameNode readCallerFrameNode ) {
610
+ PCode code = compileNode .execute (source , "exec" , "exec" , 0 , false , -1 );
611
+ Frame callerFrame = readCallerFrameNode .executeWith (frame );
612
+ Object [] args = PArguments .create ();
613
+ PArguments .setGlobals (args , PArguments .getGlobals (callerFrame ));
614
+ PArguments .setClosure (args , PArguments .getClosure (callerFrame ));
615
+ indirectCallNode .call (code .getRootCallTarget (), args );
616
+ return PNone .NO_VALUE ;
617
+ }
618
+
619
+ @ Specialization (guards = {"isNoValue(locals)" })
620
+ PNone execDefault (Object source , PDict globals , @ SuppressWarnings ("unused" ) PNone locals ) {
621
+ PCode code = compileNode .execute (source , "exec" , "exec" , 0 , false , -1 );
622
+ Object [] args = PArguments .create ();
623
+ PArguments .setGlobals (args , globals );
624
+ // If locals are not given, they default to the globals, so we don't need the caller
625
+ // frame's closure at all
626
+ indirectCallNode .call (code .getRootCallTarget (), args );
627
+ return PNone .NO_VALUE ;
628
+ }
629
+ }
630
+
598
631
// compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
599
632
@ Builtin (name = COMPILE , fixedNumOfPositionalArgs = 3 , keywordArguments = {"flags" , "dont_inherit" , "optimize" })
600
633
@ GenerateNodeFactory
601
634
@ TypeSystemReference (PythonArithmeticTypes .class )
602
635
public abstract static class CompileNode extends PythonBuiltinNode {
603
636
604
- public abstract Object execute (Object source , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize );
637
+ public abstract PCode execute (Object source , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize );
605
638
606
639
@ Specialization
607
640
@ TruffleBoundary
608
- Object compile (PBytes source , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize ,
641
+ PCode compile (PBytes source , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize ,
609
642
@ Cached ("create()" ) BytesNodes .ToBytesNode toBytesNode ) {
610
643
return compile (new String (toBytesNode .execute (source )), filename , mode , kwFlags , kwDontInherit , kwOptimize );
611
644
}
612
645
613
646
@ Specialization
614
647
@ TruffleBoundary
615
- Object compile (OpaqueBytes source , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize ) {
648
+ PCode compile (OpaqueBytes source , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize ) {
616
649
return compile (new String (source .getBytes ()), filename , mode , kwFlags , kwDontInherit , kwOptimize );
617
650
}
618
651
619
652
@ SuppressWarnings ("unused" )
620
653
@ Specialization
621
654
@ TruffleBoundary
622
- Object compile (String expression , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize ) {
655
+ PCode compile (String expression , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize ) {
623
656
Source source = PythonLanguage .newSource (getContext (), expression , filename );
624
657
ParserMode pm ;
625
658
if (mode .equals ("exec" )) {
@@ -641,7 +674,7 @@ Object compile(String expression, String filename, String mode, Object kwFlags,
641
674
642
675
@ SuppressWarnings ("unused" )
643
676
@ Specialization
644
- Object compile (PCode code , String filename , String mode , Object flags , Object dontInherit , Object optimize ) {
677
+ PCode compile (PCode code , String filename , String mode , Object flags , Object dontInherit , Object optimize ) {
645
678
return code ;
646
679
}
647
680
@@ -1587,4 +1620,40 @@ String inputPrompt(String prompt) {
1587
1620
return input (null );
1588
1621
}
1589
1622
}
1623
+
1624
+ @ Builtin (name = "globals" , fixedNumOfPositionalArgs = 0 )
1625
+ @ GenerateNodeFactory
1626
+ abstract static class GlobalsNode extends PythonBuiltinNode {
1627
+ @ Child ReadCallerFrameNode readCallerFrameNode = ReadCallerFrameNode .create ();
1628
+ private final ConditionProfile condProfile = ConditionProfile .createBinaryProfile ();
1629
+
1630
+ @ Specialization
1631
+ public Object globals (VirtualFrame frame ) {
1632
+ Frame callerFrame = readCallerFrameNode .executeWith (frame );
1633
+ PythonObject globals = PArguments .getGlobals (callerFrame );
1634
+ if (condProfile .profile (globals instanceof PythonModule )) {
1635
+ return factory ().createDictFixedStorage (globals );
1636
+ } else {
1637
+ return globals ;
1638
+ }
1639
+ }
1640
+ }
1641
+
1642
+ @ Builtin (name = "locals" , fixedNumOfPositionalArgs = 0 )
1643
+ @ GenerateNodeFactory
1644
+ abstract static class LocalsNode extends PythonBuiltinNode {
1645
+ @ Child ReadCallerFrameNode readCallerFrameNode = ReadCallerFrameNode .create ();
1646
+ private final ConditionProfile condProfile = ConditionProfile .createBinaryProfile ();
1647
+
1648
+ @ Specialization
1649
+ public Object locals (VirtualFrame frame ) {
1650
+ Frame callerFrame = readCallerFrameNode .executeWith (frame );
1651
+ PFrame pFrame = PArguments .getPFrame (callerFrame );
1652
+ if (condProfile .profile (pFrame == null )) {
1653
+ pFrame = factory ().createPFrame (callerFrame );
1654
+ PArguments .setPFrame (callerFrame , pFrame );
1655
+ }
1656
+ return pFrame .getLocals (factory ());
1657
+ }
1658
+ }
1590
1659
}
0 commit comments