94
94
import java .util .Set ;
95
95
import java .util .function .Predicate ;
96
96
97
+ import com .oracle .truffle .api .TruffleLanguage ;
97
98
import com .oracle .truffle .api .test .ReflectionUtils ;
98
99
import com .oracle .truffle .api .test .common .AbstractExecutableTestLanguage ;
99
100
import com .oracle .truffle .api .test .common .TestUtils ;
100
101
import com .oracle .truffle .tck .tests .TruffleTestAssumptions ;
101
102
import org .graalvm .home .Version ;
102
103
import org .graalvm .polyglot .Context ;
104
+ import org .graalvm .polyglot .Engine ;
103
105
import org .graalvm .polyglot .io .FileSystem ;
104
106
import org .graalvm .polyglot .io .IOAccess ;
105
107
import org .junit .AfterClass ;
@@ -196,7 +198,7 @@ public static void createConfigurations() throws IOException, ReflectiveOperatio
196
198
accessibleDir = createContent (Files .createTempDirectory (FileSystemsTest .class .getSimpleName ()),
197
199
fullIO );
198
200
ctx = Context .newBuilder ().allowIO (IOAccess .ALL ).build ();
199
- setCwd (ctx , accessibleDir , null );
201
+ setCwd (ctx , accessibleDir );
200
202
cfgs .put (FULL_IO , new Configuration (FULL_IO , ctx , accessibleDir , fullIO , true , true , true , true ));
201
203
}
202
204
@@ -208,19 +210,24 @@ public static void createConfigurations() throws IOException, ReflectiveOperatio
208
210
209
211
// No IO under language home - public file
210
212
if (TruffleTestAssumptions .isNoClassLoaderEncapsulation ()) { // setCwd not supported
211
- ctx = Context . newBuilder (). allowIO ( IOAccess . NONE ). build ();
213
+ Engine engine = Engine . create ();
212
214
privateDir = createContent (Files .createTempDirectory (FileSystemsTest .class .getSimpleName ()).toRealPath (),
213
215
fullIO );
214
- setCwd (ctx , privateDir , privateDir );
215
- cfgs .put (NO_IO_UNDER_LANGUAGE_HOME_PUBLIC_FILE , new Configuration (NO_IO_UNDER_LANGUAGE_HOME_PUBLIC_FILE , ctx , privateDir , fullIO , true , false , false , false ));
216
+ markAsLanguageHome (engine , SetCurrentWorkingDirectoryLanguage .class , privateDir );
217
+ ctx = Context .newBuilder ().engine (engine ).allowIO (IOAccess .NONE ).build ();
218
+ setCwd (ctx , privateDir );
219
+ cfgs .put (NO_IO_UNDER_LANGUAGE_HOME_PUBLIC_FILE ,
220
+ new Configuration (NO_IO_UNDER_LANGUAGE_HOME_PUBLIC_FILE , ctx , privateDir , privateDir , fullIO , true , false , false , false , false , true , engine ));
216
221
217
222
// No IO under language home - internal file
218
- ctx = Context . newBuilder (). allowIO ( IOAccess . NONE ). build ();
223
+ engine = Engine . create ();
219
224
privateDir = createContent (Files .createTempDirectory (FileSystemsTest .class .getSimpleName ()).toRealPath (),
220
225
fullIO );
221
- setCwd (ctx , privateDir , privateDir );
226
+ markAsLanguageHome (engine , SetCurrentWorkingDirectoryLanguage .class , privateDir );
227
+ ctx = Context .newBuilder ().engine (engine ).allowIO (IOAccess .NONE ).build ();
228
+ setCwd (ctx , privateDir );
222
229
cfgs .put (NO_IO_UNDER_LANGUAGE_HOME_INTERNAL_FILE ,
223
- new Configuration (NO_IO_UNDER_LANGUAGE_HOME_INTERNAL_FILE , ctx , privateDir , privateDir , fullIO , true , true , false , false , true , false ));
230
+ new Configuration (NO_IO_UNDER_LANGUAGE_HOME_INTERNAL_FILE , ctx , privateDir , privateDir , fullIO , true , true , false , false , true , false , engine ));
224
231
}
225
232
226
233
// Read Only
@@ -230,7 +237,7 @@ public static void createConfigurations() throws IOException, ReflectiveOperatio
230
237
fullIO );
231
238
ioAccess = IOAccess .newBuilder ().fileSystem (FileSystem .newReadOnlyFileSystem (fullIO )).build ();
232
239
ctx = Context .newBuilder ().allowIO (ioAccess ).build ();
233
- setCwd (ctx , accessibleDir , null );
240
+ setCwd (ctx , accessibleDir );
234
241
cfgs .put (READ_ONLY , new Configuration (READ_ONLY , ctx , accessibleDir , fullIO , true , true , false , true ));
235
242
}
236
243
@@ -290,11 +297,13 @@ public static void createConfigurations() throws IOException, ReflectiveOperatio
290
297
memDir = mkdirs (fileSystem .toAbsolutePath (fileSystem .parsePath ("work" )), fileSystem );
291
298
fileSystem .setCurrentWorkingDirectory (memDir );
292
299
privateDir = createContent (memDir , fileSystem );
300
+ Engine engine = Engine .create ();
301
+ markAsLanguageHome (engine , SetCurrentWorkingDirectoryLanguage .class , privateDir );
293
302
ioAccess = IOAccess .newBuilder ().fileSystem (fileSystem ).build ();
294
- ctx = Context .newBuilder ().allowIO (ioAccess ).build ();
295
- setCwd (ctx , privateDir , privateDir );
303
+ ctx = Context .newBuilder ().engine ( engine ). allowIO (ioAccess ).build ();
304
+ setCwd (ctx , privateDir );
296
305
cfgs .put (MEMORY_FILE_SYSTEM_WITH_LANGUAGE_HOMES_INTERNAL_FILE ,
297
- new Configuration (MEMORY_FILE_SYSTEM_WITH_LANGUAGE_HOMES_INTERNAL_FILE , ctx , privateDir , privateDir , fileSystem , false , true , true , true ));
306
+ new Configuration (MEMORY_FILE_SYSTEM_WITH_LANGUAGE_HOMES_INTERNAL_FILE , ctx , privateDir , privateDir , fileSystem , false , true , true , true , true , true , engine ));
298
307
299
308
// PreInitializeContextFileSystem in image build time
300
309
fileSystem = createPreInitializeContextFileSystem ();
@@ -324,7 +333,7 @@ private static void createDeprecatedConfigurations(FileSystem fullIO) throws IOE
324
333
Path accessibleDir = createContent (Files .createTempDirectory (FileSystemsTest .class .getSimpleName ()),
325
334
fullIO );
326
335
Context ctx = Context .newBuilder ().allowIO (true ).build ();
327
- setCwd (ctx , accessibleDir , null );
336
+ setCwd (ctx , accessibleDir );
328
337
cfgs .put (FULL_IO_DEPRECATED , new Configuration (FULL_IO , ctx , accessibleDir , fullIO , true , true , true , true ));
329
338
330
339
// No IO using deprecated Context.Builder methods
@@ -2742,6 +2751,7 @@ public static final class Configuration implements Closeable {
2742
2751
private final boolean allowsUserDir ;
2743
2752
private final boolean allowsAbsolutePath ;
2744
2753
private final boolean usePublicFile ;
2754
+ private final Engine engine ;
2745
2755
2746
2756
Configuration (
2747
2757
final String name ,
@@ -2765,11 +2775,11 @@ public static final class Configuration implements Closeable {
2765
2775
final boolean readableFileSystem ,
2766
2776
final boolean writableFileSystem ,
2767
2777
final boolean allowsUserDir ) {
2768
- this (name , context , path , userDir , fileSystem , internalFileSystem , readableFileSystem , writableFileSystem , allowsUserDir , allowsUserDir , true );
2778
+ this (name , context , path , userDir , fileSystem , internalFileSystem , readableFileSystem , writableFileSystem , allowsUserDir , allowsUserDir , true , null );
2769
2779
}
2770
2780
2771
2781
Configuration (String name , Context context , Path path , Path userDir , FileSystem fileSystem , boolean internalFileSystem , boolean readableFileSystem ,
2772
- boolean writableFileSystem , boolean allowsUserDir , boolean allowsAbsolutePath , boolean usePublicFile ) {
2782
+ boolean writableFileSystem , boolean allowsUserDir , boolean allowsAbsolutePath , boolean usePublicFile , Engine engine ) {
2773
2783
Objects .requireNonNull (name , "Name must be non null." );
2774
2784
Objects .requireNonNull (context , "Context must be non null." );
2775
2785
Objects .requireNonNull (path , "Path must be non null." );
@@ -2786,6 +2796,7 @@ public static final class Configuration implements Closeable {
2786
2796
this .allowsUserDir = allowsUserDir ;
2787
2797
this .allowsAbsolutePath = allowsAbsolutePath ;
2788
2798
this .usePublicFile = usePublicFile ;
2799
+ this .engine = engine ;
2789
2800
}
2790
2801
2791
2802
String getName () {
@@ -2864,6 +2875,9 @@ public String toString() {
2864
2875
public void close () throws IOException {
2865
2876
try {
2866
2877
ctx .close ();
2878
+ if (engine != null ) {
2879
+ engine .close ();
2880
+ }
2867
2881
} finally {
2868
2882
deleteRecursively (path , fileSystem );
2869
2883
}
@@ -2977,10 +2991,9 @@ private static void resetLanguageHomes() {
2977
2991
*
2978
2992
* @param ctx the context to set the cwd for
2979
2993
* @param cwd the new current working directory
2980
- * @param langHome language home to set
2981
2994
*/
2982
- private static void setCwd (Context ctx , Path cwd , Path langHome ) {
2983
- AbstractExecutableTestLanguage .evalTestLanguage (ctx , SetCurrentWorkingDirectoryLanguage .class , "" , cwd .toString (), langHome != null ? langHome . toString () : "" );
2995
+ private static void setCwd (Context ctx , Path cwd ) {
2996
+ AbstractExecutableTestLanguage .evalTestLanguage (ctx , SetCurrentWorkingDirectoryLanguage .class , "" , cwd .toString ());
2984
2997
}
2985
2998
2986
2999
@ Registration
@@ -2989,17 +3002,30 @@ public static final class SetCurrentWorkingDirectoryLanguage extends AbstractExe
2989
3002
@ TruffleBoundary
2990
3003
protected Object execute (RootNode node , Env env , Object [] contextArguments , Object [] frameArguments ) throws Exception {
2991
3004
String currentWorkingDirectory = (String ) contextArguments [0 ];
2992
- String langHome = (String ) contextArguments [1 ];
2993
- if (!langHome .isEmpty ()) {
2994
- String languageId = TestUtils .getDefaultLanguageId (getClass ());
2995
- System .setProperty ("org.graalvm.language." + languageId + ".home" , langHome );
2996
- resetLanguageHomes ();
2997
- }
2998
3005
env .setCurrentWorkingDirectory (env .getInternalTruffleFile (currentWorkingDirectory ));
2999
3006
return null ;
3000
3007
}
3001
3008
}
3002
3009
3010
+ static void markAsLanguageHome (Engine engine , Class <? extends TruffleLanguage <?>> language , Path languageHome ) {
3011
+ try (Context ctx = Context .newBuilder ().engine (engine ).build ()) {
3012
+ AbstractExecutableTestLanguage .evalTestLanguage (ctx , MarkAsLanguageHomeLanguage .class , "" , TestUtils .getDefaultLanguageId (language ), languageHome .toString ());
3013
+ }
3014
+ }
3015
+
3016
+ @ Registration
3017
+ public static final class MarkAsLanguageHomeLanguage extends AbstractExecutableTestLanguage {
3018
+ @ Override
3019
+ @ TruffleBoundary
3020
+ protected Object execute (RootNode node , Env env , Object [] contextArguments , Object [] frameArguments ) throws Exception {
3021
+ String languageId = (String ) contextArguments [0 ];
3022
+ String langHome = (String ) contextArguments [1 ];
3023
+ System .setProperty ("org.graalvm.language." + languageId + ".home" , langHome );
3024
+ resetLanguageHomes ();
3025
+ return null ;
3026
+ }
3027
+ }
3028
+
3003
3029
private static FileSystem createPreInitializeContextFileSystem () throws ReflectiveOperationException {
3004
3030
Class <? extends FileSystem > clazz = Class .forName ("com.oracle.truffle.polyglot.FileSystems$PreInitializeContextFileSystem" ).asSubclass (FileSystem .class );
3005
3031
Constructor <? extends FileSystem > init = clazz .getDeclaredConstructor (String .class );
@@ -3009,13 +3035,10 @@ private static FileSystem createPreInitializeContextFileSystem() throws Reflecti
3009
3035
3010
3036
private static void switchToImageExecutionTime (FileSystem fileSystem , Path cwd ) throws ReflectiveOperationException {
3011
3037
String workDir = cwd .toString ();
3012
- Class <? extends FileSystem > clazz = Class .forName ("com.oracle.truffle.polyglot.FileSystems$PreInitializeContextFileSystem" ).asSubclass (FileSystem .class );
3013
- Method preInitClose = clazz .getDeclaredMethod ("onPreInitializeContextEnd" );
3014
- ReflectionUtils .setAccessible (preInitClose , true );
3015
- preInitClose .invoke (fileSystem );
3016
- Method patchStart = clazz .getDeclaredMethod ("onLoadPreinitializedContext" , FileSystem .class );
3017
- ReflectionUtils .setAccessible (patchStart , true );
3018
- patchStart .invoke (fileSystem , newFullIOFileSystem (Paths .get (workDir )));
3038
+ Class <?> internalResourceRootsClass = Class .forName ("com.oracle.truffle.polyglot.InternalResourceRoots" );
3039
+ Object roots = ReflectionUtils .invokeStatic (internalResourceRootsClass , "getInstance" );
3040
+ ReflectionUtils .invoke (fileSystem , "onPreInitializeContextEnd" , new Class <?>[]{internalResourceRootsClass , Map .class }, roots , Map .of ());
3041
+ ReflectionUtils .invoke (fileSystem , "onLoadPreinitializedContext" , new Class <?>[]{FileSystem .class }, newFullIOFileSystem (Paths .get (workDir )));
3019
3042
}
3020
3043
3021
3044
static FileSystem newFullIOFileSystem (final Path currentWorkingDirectory ) {
0 commit comments