Search in sources :

Example 11 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class Xar method create.

public static Xar create(ByteSequence bytes) {
    ObjectFileReader data = new ObjectFileReader(bytes, false);
    // magic
    data.getInt();
    XarHeader header = XarHeader.create(data);
    List<XarFile> files = TocParser.parse(data, header);
    ByteSequence heap = data.slice();
    return new Xar(header, files, heap);
}
Also used : ObjectFileReader(com.oracle.truffle.llvm.parser.filereader.ObjectFileReader) ByteSequence(org.graalvm.polyglot.io.ByteSequence)

Example 12 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class SourceBuilderTest method testBinarySourcesUnavailableSourceSection.

@Test
public void testBinarySourcesUnavailableSourceSection() {
    ByteSequence sequence = ByteSequence.create(new byte[] { 1, 2, 3, 4 });
    Source source = Source.newBuilder("", sequence, null).build();
    SourceSection section = source.createUnavailableSection();
    assertEquals("", section.getCharacters().toString());
    assertEquals(0, section.getCharEndIndex());
    assertEquals(0, section.getCharIndex());
    assertEquals(0, section.getCharLength());
    assertEquals(1, section.getEndColumn());
    assertEquals(1, section.getEndLine());
    assertEquals(1, section.getStartColumn());
    assertEquals(1, section.getStartLine());
    assertSame(source, section.getSource());
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection) ByteSequence(org.graalvm.polyglot.io.ByteSequence) Source(com.oracle.truffle.api.source.Source) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) Test(org.junit.Test)

Example 13 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class SourceBuilderTest method testMimeTypes.

@Test
public void testMimeTypes() {
    setupEnv();
    ByteSequence bytes = ByteSequence.create(new byte[8]);
    assertNotNull(Source.newBuilder("", "", "").mimeType(null).build());
    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").build().getMimeType());
    assertEquals("application/a", Source.newBuilder("", bytes, "").mimeType("application/a").build().getMimeType());
}
Also used : ByteSequence(org.graalvm.polyglot.io.ByteSequence) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) Test(org.junit.Test)

Example 14 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class SourceBuilderTest method testBinarySources.

@Test
public void testBinarySources() {
    ByteSequence sequence = ByteSequence.create(new byte[] { 1, 2, 3, 4 });
    Source source = Source.newBuilder("", sequence, null).build();
    assertTrue(source.hasBytes());
    assertFalse(source.hasCharacters());
    assertEquals(4, source.getLength());
    assertFails(() -> source.createSection(0), UnsupportedOperationException.class);
    assertFails(() -> source.createSection(0, 0), UnsupportedOperationException.class);
    assertFails(() -> source.createSection(0, 0, 0), UnsupportedOperationException.class);
    SourceSection section = source.createUnavailableSection();
    assertFalse(section.isAvailable());
    assertTrue(section.getCharacters().length() == 0);
    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());
    assertEquals(sequence, source.getBytes());
    assertEquals("Unnamed", source.getName());
    assertNull(source.getURL());
    assertEquals("truffle:9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a/Unnamed", source.getURI().toString());
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection) ByteSequence(org.graalvm.polyglot.io.ByteSequence) Source(com.oracle.truffle.api.source.Source) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) Test(org.junit.Test)

Example 15 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class SourceBuilderTest method testBuildBinarySources.

@Test
public void testBuildBinarySources() throws IOException {
    setupEnv();
    ByteSequence bytes = ByteSequence.create(new byte[8]);
    Source source = Source.newBuilder("", bytes, null).build();
    assertTrue(source.hasBytes());
    assertFalse(source.hasCharacters());
    source = Source.newBuilder("", "", null).content(bytes).build();
    assertTrue(source.hasBytes());
    assertFalse(source.hasCharacters());
    source = Source.newBuilder("", bytes, null).content("").build();
    assertFalse(source.hasBytes());
    assertTrue(source.hasCharacters());
    File file = File.createTempFile("Hello", ".bin").getCanonicalFile();
    file.deleteOnExit();
    TruffleFile truffleFile = languageEnv.getPublicTruffleFile(file.getPath());
    // mime-type not specified + invalid langauge -> characters
    source = Source.newBuilder("", truffleFile).build();
    assertFalse(source.hasBytes());
    assertTrue(source.hasCharacters());
    // mime-type not specified + invalid langauge -> characters
    source = Source.newBuilder("", truffleFile).content(bytes).build();
    assertTrue(source.hasBytes());
    assertFalse(source.hasCharacters());
    source = Source.newBuilder("", truffleFile).content("").build();
    assertFalse(source.hasBytes());
    assertTrue(source.hasCharacters());
}
Also used : TruffleFile(com.oracle.truffle.api.TruffleFile) TruffleFile(com.oracle.truffle.api.TruffleFile) File(java.io.File) ByteSequence(org.graalvm.polyglot.io.ByteSequence) Source(com.oracle.truffle.api.source.Source) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) Test(org.junit.Test)

Aggregations

ByteSequence (org.graalvm.polyglot.io.ByteSequence)26 Test (org.junit.Test)20 Source (org.graalvm.polyglot.Source)10 Context (org.graalvm.polyglot.Context)8 Value (org.graalvm.polyglot.Value)8 AbstractPolyglotTest (com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)5 Source (com.oracle.truffle.api.source.Source)4 File (java.io.File)3 WasmContext (org.graalvm.wasm.WasmContext)3 TruffleFile (com.oracle.truffle.api.TruffleFile)2 SourceSection (com.oracle.truffle.api.source.SourceSection)2 ObjectFileReader (com.oracle.truffle.llvm.parser.filereader.ObjectFileReader)2 TruffleTestAssumptions (com.oracle.truffle.tck.tests.TruffleTestAssumptions)2 IOException (java.io.IOException)2 Assert.assertArrayEquals (org.junit.Assert.assertArrayEquals)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Assert.fail (org.junit.Assert.fail)2