44
44
import java .util .regex .Matcher ;
45
45
import java .util .regex .Pattern ;
46
46
47
- import com .oracle .graal .python .runtime .object .PythonObjectSlowPathFactory ;
48
47
import org .graalvm .nativeimage .ImageInfo ;
49
48
50
49
import com .oracle .graal .python .PythonLanguage ;
265
264
import com .oracle .graal .python .runtime .exception .PException ;
266
265
import com .oracle .graal .python .runtime .formatting .ErrorMessageFormatter ;
267
266
import com .oracle .graal .python .runtime .interop .PythonMapScope ;
267
+ import com .oracle .graal .python .runtime .object .PythonObjectSlowPathFactory ;
268
268
import com .oracle .graal .python .util .PythonUtils ;
269
269
import com .oracle .graal .python .util .Supplier ;
270
270
import com .oracle .truffle .api .CallTarget ;
282
282
283
283
/**
284
284
* The core is intended to the immutable part of the interpreter, including most modules and most
285
- * types.
285
+ * types. The core is embedded, using inheritance, into {@link PythonContext} to avoid indirection
286
+ * through an extra field in the context.
286
287
*/
287
- public final class Python3Core implements ParserErrorCallback {
288
+ public abstract class Python3Core implements ParserErrorCallback {
288
289
private static final TruffleLogger LOGGER = PythonLanguage .getLogger (Python3Core .class );
289
290
private final String [] coreFiles ;
290
291
@@ -604,7 +605,6 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
604
605
605
606
private final PythonParser parser ;
606
607
607
- @ CompilationFinal private PythonContext singletonContext ;
608
608
@ CompilationFinal private Object globalScopeObject ;
609
609
610
610
/*
@@ -613,43 +613,45 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
613
613
*/
614
614
private volatile boolean initialized ;
615
615
616
- private PythonObjectSlowPathFactory objectFactory ;
616
+ private final PythonLanguage language ;
617
+ @ CompilationFinal private PythonObjectSlowPathFactory objectFactory ;
617
618
618
- public Python3Core (PythonParser parser , boolean isNativeSupportAllowed ) {
619
+ public Python3Core (PythonLanguage language , PythonParser parser , boolean isNativeSupportAllowed ) {
620
+ this .language = language ;
619
621
this .parser = parser ;
620
622
this .builtins = initializeBuiltins (isNativeSupportAllowed );
621
623
this .coreFiles = initializeCoreFiles ();
622
624
}
623
625
624
- public PythonLanguage getLanguage () {
625
- return singletonContext .getLanguage ();
626
+ @ Override
627
+ public final PythonContext getContext () {
628
+ // Small hack: we know that this is the only implementation of Python3Core
629
+ return (PythonContext ) this ;
626
630
}
627
631
628
- @ Override
629
- public PythonContext getContext () {
630
- return singletonContext ;
632
+ public final PythonLanguage getLanguage () {
633
+ return language ;
631
634
}
632
635
633
- public PythonParser getParser () {
636
+ public final PythonParser getParser () {
634
637
return parser ;
635
638
}
636
639
637
- public PythonCodeSerializer getSerializer () {
640
+ public final PythonCodeSerializer getSerializer () {
638
641
return (PythonCodeSerializer ) parser ;
639
642
}
640
643
641
644
/**
642
645
* Checks whether the core is initialized.
643
646
*/
644
- public boolean isInitialized () {
647
+ public final boolean isCoreInitialized () {
645
648
return initialized ;
646
649
}
647
650
648
651
/**
649
652
* Load the core library and prepare all builtin classes and modules.
650
653
*/
651
- public void initialize (PythonContext context ) {
652
- singletonContext = context ;
654
+ public final void initialize (PythonContext context ) {
653
655
objectFactory = new PythonObjectSlowPathFactory (context .getAllocationReporter ());
654
656
initializeJavaCore ();
655
657
initializePython3Core (context .getCoreHomeOrFail ());
@@ -678,7 +680,7 @@ private void initializePython3Core(String coreHome) {
678
680
* eagerly when the context is initialized on the JVM or a new context is created on SVM, but is
679
681
* omitted when the native image is generated.
680
682
*/
681
- public void postInitialize () {
683
+ public final void postInitialize () {
682
684
if (!ImageInfo .inImageBuildtimeCode () || ImageInfo .inImageRuntimeCode ()) {
683
685
initialized = false ;
684
686
@@ -696,48 +698,48 @@ public void postInitialize() {
696
698
}
697
699
698
700
@ TruffleBoundary
699
- public PythonModule lookupBuiltinModule (String name ) {
701
+ public final PythonModule lookupBuiltinModule (String name ) {
700
702
return builtinModules .get (name );
701
703
}
702
704
703
- public PythonBuiltinClass lookupType (PythonBuiltinClassType type ) {
705
+ public final PythonBuiltinClass lookupType (PythonBuiltinClassType type ) {
704
706
assert builtinTypes [type .ordinal ()] != null ;
705
707
return builtinTypes [type .ordinal ()];
706
708
}
707
709
708
710
@ TruffleBoundary
709
- public String [] builtinModuleNames () {
711
+ public final String [] builtinModuleNames () {
710
712
return builtinModules .keySet ().toArray (PythonUtils .EMPTY_STRING_ARRAY );
711
713
}
712
714
713
- public PythonModule getBuiltins () {
715
+ public final PythonModule getBuiltins () {
714
716
return builtinsModule ;
715
717
}
716
718
717
- public PythonModule getSysModule () {
719
+ public final PythonModule getSysModule () {
718
720
return sysModule ;
719
721
}
720
722
721
- public PDict getSysModules () {
723
+ public final PDict getSysModules () {
722
724
return sysModules ;
723
725
}
724
726
725
- public PythonModule getImportlib () {
727
+ public final PythonModule getImportlib () {
726
728
return importlib ;
727
729
}
728
730
729
- public void registerImportlib (PythonModule mod ) {
731
+ public final void registerImportlib (PythonModule mod ) {
730
732
if (importlib != null ) {
731
733
throw new IllegalStateException ("importlib cannot be registered more than once" );
732
734
}
733
735
importlib = mod ;
734
736
}
735
737
736
- public PMethod getImportFunc () {
738
+ public final PMethod getImportFunc () {
737
739
return importFunc ;
738
740
}
739
741
740
- public void registerImportFunc (PMethod func ) {
742
+ public final void registerImportFunc (PMethod func ) {
741
743
if (importFunc != null ) {
742
744
throw new IllegalStateException ("__import__ func cannot be registered more than once" );
743
745
}
@@ -746,14 +748,14 @@ public void registerImportFunc(PMethod func) {
746
748
747
749
@ Override
748
750
@ TruffleBoundary
749
- public void warn (PythonBuiltinClassType type , String format , Object ... args ) {
751
+ public final void warn (PythonBuiltinClassType type , String format , Object ... args ) {
750
752
WarningsModuleBuiltins .WarnNode .getUncached ().warnFormat (null , null , type , 1 , format , args );
751
753
}
752
754
753
755
/**
754
756
* Returns the stderr object or signals error when stderr is "lost".
755
757
*/
756
- public Object getStderr () {
758
+ public final Object getStderr () {
757
759
try {
758
760
return PyObjectLookupAttr .getUncached ().execute (null , sysModule , "stderr" );
759
761
} catch (PException e ) {
@@ -933,29 +935,24 @@ private void loadFile(String s, String prefix) {
933
935
GenericInvokeNode .getUncached ().execute (callTarget , PArguments .withGlobals (mod ));
934
936
}
935
937
936
- public PythonObjectSlowPathFactory factory () {
938
+ public final PythonObjectSlowPathFactory factory () {
937
939
return objectFactory ;
938
940
}
939
941
940
- public void setContext (PythonContext context ) {
941
- assert singletonContext == null ;
942
- singletonContext = context ;
943
- }
944
-
945
- public PInt getTrue () {
942
+ public final PInt getTrue () {
946
943
return pyTrue ;
947
944
}
948
945
949
- public PInt getFalse () {
946
+ public final PInt getFalse () {
950
947
return pyFalse ;
951
948
}
952
949
953
- public PFloat getNaN () {
950
+ public final PFloat getNaN () {
954
951
return pyNaN ;
955
952
}
956
953
957
954
@ Override
958
- public RuntimeException raiseInvalidSyntax (PythonParser .ErrorType type , Source source , SourceSection section , String message , Object ... arguments ) {
955
+ public final RuntimeException raiseInvalidSyntax (PythonParser .ErrorType type , Source source , SourceSection section , String message , Object ... arguments ) {
959
956
CompilerDirectives .transferToInterpreter ();
960
957
Node location = new Node () {
961
958
@ Override
@@ -968,7 +965,7 @@ public SourceSection getSourceSection() {
968
965
969
966
@ Override
970
967
@ TruffleBoundary
971
- public RuntimeException raiseInvalidSyntax (PythonParser .ErrorType type , Node location , String message , Object ... arguments ) {
968
+ public final RuntimeException raiseInvalidSyntax (PythonParser .ErrorType type , Node location , String message , Object ... arguments ) {
972
969
PBaseException instance ;
973
970
Object cls ;
974
971
switch (type ) {
@@ -1017,15 +1014,15 @@ public RuntimeException raiseInvalidSyntax(PythonParser.ErrorType type, Node loc
1017
1014
throw PException .fromObject (instance , location , PythonOptions .isPExceptionWithJavaStacktrace (getLanguage ()));
1018
1015
}
1019
1016
1020
- public Object getTopScopeObject () {
1017
+ public final Object getTopScopeObject () {
1021
1018
return globalScopeObject ;
1022
1019
}
1023
1020
1024
- public static final void writeInfo (String message ) {
1021
+ public static void writeInfo (String message ) {
1025
1022
PythonLanguage .getLogger (Python3Core .class ).fine (message );
1026
1023
}
1027
1024
1028
- public static final void writeInfo (Supplier <String > messageSupplier ) {
1025
+ public static void writeInfo (Supplier <String > messageSupplier ) {
1029
1026
PythonLanguage .getLogger (Python3Core .class ).fine (messageSupplier );
1030
1027
}
1031
1028
}
0 commit comments