use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class SourceSnippets method buildSource.
static Source buildSource(String language, Object origin, String name, String path, boolean canonicalizePath, String mimeType, Object content, URL url, URI uri, Charset encoding, boolean internal, boolean interactive, boolean cached, Object fileSystemContext, boolean embedderSource) throws IOException {
String useName = name;
URI useUri = uri;
Object useContent = content;
String useMimeType = mimeType;
String usePath = path;
URL useUrl = url;
Object useOrigin = origin;
Charset useEncoding = encoding;
TruffleFile useTruffleFile = null;
Object useFileSystemContext = fileSystemContext;
if (useOrigin instanceof File) {
final File file = (File) useOrigin;
assert useFileSystemContext != null : "file system context must be provided by polyglot embedding API";
TruffleFile truffleFile = SourceAccessor.getTruffleFile(file.toPath().toString(), useFileSystemContext);
useOrigin = truffleFile;
}
if (useOrigin == CONTENT_UNSET) {
useContent = useContent == CONTENT_UNSET ? null : useContent;
} else if (useOrigin instanceof TruffleFile) {
useTruffleFile = (TruffleFile) useOrigin;
if (!canonicalizePath || useContent == CONTENT_NONE) {
// Do not canonicalize the file, and use a relative URI if the file is relative
if (useUri == null) {
useUri = useTruffleFile.isAbsolute() ? useTruffleFile.toUri() : useTruffleFile.toRelativeUri();
}
} else {
// Canonicalize the file if it exists
useTruffleFile = useTruffleFile.exists() ? useTruffleFile.getCanonicalFile() : useTruffleFile;
}
useFileSystemContext = SourceAccessor.LANGUAGE.getFileSystemContext(useTruffleFile);
useName = useName == null ? useTruffleFile.getName() : useName;
usePath = usePath == null ? useTruffleFile.getPath() : usePath;
useUri = useUri == null ? useTruffleFile.toUri() : useUri;
useMimeType = useMimeType == null ? SourceAccessor.detectMimeType(useTruffleFile, getValidMimeTypes(useFileSystemContext, language)) : useMimeType;
if (useContent == CONTENT_UNSET) {
if (isCharacterBased(useFileSystemContext, language, useMimeType)) {
useEncoding = useEncoding == null ? findEncoding(useTruffleFile, useMimeType) : useEncoding;
useContent = read(useTruffleFile, useEncoding);
} else {
useContent = ByteSequence.create(useTruffleFile.readAllBytes());
}
}
} else if (useOrigin instanceof URL) {
useUrl = (URL) useOrigin;
String urlPath = useUrl.getPath();
int lastIndex = urlPath.lastIndexOf('/');
useName = useName == null && lastIndex != -1 ? useUrl.getPath().substring(lastIndex + 1) : useName;
URI tmpUri;
try {
tmpUri = useUrl.toURI();
} catch (URISyntaxException ex) {
throw new IOException("Bad URL: " + useUrl, ex);
}
useUri = useUri == null ? tmpUri : useUri;
usePath = usePath == null ? useUrl.getPath() : usePath;
useFileSystemContext = useFileSystemContext == null ? SourceAccessor.ACCESSOR.engineSupport().getCurrentFileSystemContext() : useFileSystemContext;
try {
useTruffleFile = SourceAccessor.getTruffleFile(tmpUri, useFileSystemContext);
useTruffleFile = useTruffleFile.exists() ? useTruffleFile.getCanonicalFile() : useTruffleFile;
if (useContent == CONTENT_UNSET) {
if (isCharacterBased(useFileSystemContext, language, useMimeType)) {
String fileMimeType = useMimeType == null ? SourceAccessor.detectMimeType(useTruffleFile, getValidMimeTypes(useFileSystemContext, language)) : useMimeType;
useEncoding = useEncoding == null ? findEncoding(useTruffleFile, fileMimeType) : useEncoding;
useContent = read(useTruffleFile, useEncoding);
} else {
useContent = ByteSequence.create(useTruffleFile.readAllBytes());
}
}
} catch (UnsupportedOperationException uoe) {
if (ALLOW_IO && SourceAccessor.hasAllAccess(useFileSystemContext)) {
// Not a recognized by FileSystem, fall back to URLConnection only for allowed
// IO without a custom FileSystem
URLConnection connection = useUrl.openConnection();
useEncoding = useEncoding == null ? StandardCharsets.UTF_8 : useEncoding;
if (useContent == CONTENT_UNSET) {
if (isCharacterBased(useFileSystemContext, language, useMimeType)) {
useContent = read(new InputStreamReader(connection.getInputStream(), useEncoding));
} else {
useContent = ByteSequence.create(readBytes(connection));
}
}
} else {
throw new SecurityException("Reading of URL " + useUrl + " is not allowed.");
}
}
} else if (useOrigin instanceof Reader) {
final Reader r = (Reader) useOrigin;
useContent = useContent == CONTENT_UNSET ? read(r) : useContent;
} else if (useOrigin instanceof ByteSequence) {
useContent = useContent == CONTENT_UNSET ? useOrigin : useContent;
} else {
assert useOrigin instanceof CharSequence;
useContent = useContent == CONTENT_UNSET ? useOrigin : useContent;
}
if (useName == null) {
useName = "Unnamed";
}
useContent = enforceInterfaceContracts(useContent);
String relativePathInLanguageHome = null;
if (useTruffleFile != null) {
// The relativePathInLanguageHome has to be calculated also for Sources created in the
// image execution time. They have to have the same hash code as sources created during
// the context pre-initialization.
relativePathInLanguageHome = SourceAccessor.ACCESSOR.engineSupport().getRelativePathInLanguageHome(useTruffleFile);
if (relativePathInLanguageHome != null) {
Object fsEngineObject = SourceAccessor.ACCESSOR.languageSupport().getFileSystemEngineObject(SourceAccessor.ACCESSOR.languageSupport().getFileSystemContext(useTruffleFile));
if (SourceAccessor.ACCESSOR.engineSupport().inContextPreInitialization(fsEngineObject)) {
SourceImpl.Key key = new SourceImpl.ReinitializableKey(useTruffleFile, useContent, useMimeType, language, useUrl, useUri, useName, usePath, internal, interactive, cached, relativePathInLanguageHome, embedderSource);
Source source = SOURCES.intern(key);
SourceAccessor.onSourceCreated(source);
return source;
}
}
}
SourceImpl.Key key = new SourceImpl.ImmutableKey(useContent, useMimeType, language, useUrl, useUri, useName, usePath, internal, interactive, cached, relativePathInLanguageHome, embedderSource);
return SOURCES.intern(key);
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class SourceAPITest method testMimeTypes.
@Test
public void testMimeTypes() {
ByteSequence bytes = ByteSequence.create(new byte[8]);
assertNotNull(Source.newBuilder("", "", "").mimeType(null).buildLiteral());
assertFails(() -> Source.newBuilder("", "", "").mimeType(""), IllegalArgumentException.class);
assertFails(() -> Source.newBuilder("", "", "").mimeType("/"), IllegalArgumentException.class);
assertFails(() -> Source.newBuilder("", "", "").mimeType("a/"), IllegalArgumentException.class);
assertFails(() -> Source.newBuilder("", "", "").mimeType("/a"), IllegalArgumentException.class);
assertEquals("text/a", Source.newBuilder("", "", "").mimeType("text/a").buildLiteral().getMimeType());
assertEquals("application/a", Source.newBuilder("", bytes, "").mimeType("application/a").buildLiteral().getMimeType());
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class SourceAPITest method testBinarySources.
@Test
public void testBinarySources() {
ByteSequence sequence = ByteSequence.create(new byte[] { 1, 2, 3, 4 });
Source source = Source.newBuilder("", sequence, null).cached(false).buildLiteral();
assertTrue(source.hasBytes());
assertFalse(source.hasCharacters());
assertFails(() -> source.getCharacters(), UnsupportedOperationException.class);
assertFails(() -> source.getCharacters(0), UnsupportedOperationException.class);
assertFails(() -> source.getColumnNumber(0), UnsupportedOperationException.class);
assertFails(() -> source.getLineCount(), UnsupportedOperationException.class);
assertFails(() -> source.getLineLength(0), UnsupportedOperationException.class);
assertFails(() -> source.getLineNumber(0), UnsupportedOperationException.class);
assertFails(() -> source.getLineStartOffset(0), UnsupportedOperationException.class);
assertFails(() -> source.getReader(), UnsupportedOperationException.class);
assertNull(source.getMimeType());
assertEquals("", source.getLanguage());
assertSame(sequence, source.getBytes());
assertEquals("Unnamed", source.getName());
assertNull(source.getURL());
assertEquals("truffle:9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a/Unnamed", source.getURI().toString());
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class WasmLateLinkingSuite method linkingFailureInvertedOnlyAux.
@Test
public void linkingFailureInvertedOnlyAux() throws IOException, InterruptedException {
final Context context = Context.newBuilder(WasmLanguage.ID).build();
final ByteSequence binaryAux = ByteSequence.create(compileWat("file1", "(import \"non_existing\" \"f\" (func))"));
final ByteSequence binaryMain = ByteSequence.create(compileWat("file2", "(func (export \"g\") (result i32) (i32.const 42))"));
final Source sourceAux = Source.newBuilder(WasmLanguage.ID, binaryAux, "module1").build();
final Source sourceMain = Source.newBuilder(WasmLanguage.ID, binaryMain, "module2").build();
context.eval(sourceAux);
final Value module2Instance = context.eval(sourceMain);
// does not exist."
try {
final Value g = module2Instance.getMember("g");
g.execute();
Assert.assertFalse("Should not reach here.", true);
} catch (Throwable e) {
// We can later refine these semantics, and avoid a failure here.
// To do this, we should only lazily link exactly the required modules, instead of
// linking all of them.
Assert.assertTrue("Should fail due to unresolved import in the other module, got: " + e.getMessage(), e.getMessage().contains("module 'non_existing', referenced by the import 'f' in the module 'main', does not exist"));
}
try {
final Value g2 = module2Instance.getMember("g");
final Value result2 = g2.execute();
Assert.assertEquals(42, result2.asInt());
} catch (Throwable e) {
Assert.assertFalse("Should not reach here, got: " + e.getMessage(), true);
}
}
use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.
the class WasmLateLinkingSuite method linkingTooLate.
@Test
public void linkingTooLate() throws IOException, InterruptedException {
final Context context = Context.newBuilder(WasmLanguage.ID).build();
final ByteSequence binaryAux = ByteSequence.create(compileWat("file1", textWithExportFun));
final ByteSequence binaryMain = ByteSequence.create(compileWat("file1", textWithImportFunExportFun));
final Source sourceAux = Source.newBuilder(WasmLanguage.ID, binaryAux, "m1").build();
final Source sourceMain = Source.newBuilder(WasmLanguage.ID, binaryMain, "m2").build();
// main
context.parse(sourceMain);
// m1
context.parse(sourceAux);
final Value g = context.getBindings(WasmLanguage.ID).getMember("main").getMember("g");
Assert.assertEquals(42, g.execute().asInt());
}
Aggregations