use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class TruffleLanguageSnippets method parseWithParams.
// BEGIN: TruffleLanguageSnippets#parseWithParams
public void parseWithParams(Env env) {
Source multiply = Source.newBuilder("a * b").mimeType("text/javascript").name("mul.js").build();
CallTarget method = env.parse(multiply, "a", "b");
Number fortyTwo = (Number) method.call(6, 7);
assert 42 == fortyTwo.intValue();
Number ten = (Number) method.call(2, 5);
assert 10 == ten.intValue();
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class LanguageSPITest method testExceptionGetSourceLocation.
@Test
public void testExceptionGetSourceLocation() {
try (final Context context = Context.create(LanguageSPITestLanguage.ID)) {
final String text = "0123456789";
LanguageSPITestLanguage.runinside = (env) -> {
Source src = Source.newBuilder(text).mimeType(LanguageSPITestLanguage.ID).name("test.txt").build();
throw new ParseException(src, 1, 2);
};
try {
context.eval(LanguageSPITestLanguage.ID, text);
Assert.fail("PolyglotException expected.");
} catch (PolyglotException pe) {
Assert.assertTrue(pe.isSyntaxError());
Assert.assertEquals("12", pe.getSourceLocation().getCharacters().toString());
} finally {
LanguageSPITestLanguage.runinside = null;
}
}
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class LanguageSPITest method testTruffleContext.
@Test
public void testTruffleContext() {
Context context = Context.create();
Function<Env, Object> f = new Function<Env, Object>() {
public Object apply(Env env) {
boolean assertions = false;
assert (assertions = true) == true;
if (!assertions) {
fail("Tests must be run with assertions on");
}
// No more recursive runs inside
LanguageSPITestLanguage.runinside = null;
Throwable[] error = new Throwable[1];
Thread thread = new Thread(() -> {
try {
Source source = Source.newBuilder("").language(LanguageSPITestLanguage.ID).name("s").build();
boolean parsingFailed = false;
try {
// execute Truffle code in a fresh thread fails
env.parse(source).call();
} catch (AssertionError e) {
// No current context available.
parsingFailed = true;
}
if (!parsingFailed) {
fail("no assertion error \"No current context available.\"");
}
TruffleContext truffleContext = env.getContext();
// attach the Thread
Object prev = truffleContext.enter();
try {
// execute Truffle code
env.parse(source).call();
} finally {
// detach the Thread
truffleContext.leave(prev);
}
} catch (Throwable t) {
error[0] = t;
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
if (error[0] != null) {
throw new AssertionError(error[0]);
}
boolean leaveFailed = false;
try {
TruffleContext truffleContext = env.getContext();
truffleContext.leave(null);
} catch (AssertionError e) {
leaveFailed = true;
}
if (!leaveFailed) {
fail("no assertion error for leaving without enter");
}
return null;
}
};
eval(context, f);
context.close();
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class SourceBuilderDocumentationTest method readerSample.
@Test
public void readerSample() throws Exception {
if (!loadedOK) {
return;
}
Source source = (Source) invokeStatic(SOURCE_SNIPPETS, "fromReader", SourceBuilderDocumentationTest.class);
assertNotNull("Every source must have URI", source.getURI());
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class SourceBuilderDocumentationTest method relativeFile.
@Test
public void relativeFile() throws Exception {
if (!loadedOK) {
return;
}
File relative = null;
for (File f : new File(".").listFiles()) {
if (f.isFile() && f.canRead()) {
relative = f;
break;
}
}
if (relative == null) {
// skip the test
return;
}
Source direct = Source.newBuilder(relative).name(relative.getPath()).build();
Source fromBuilder = (Source) invokeStatic(SOURCE_SNIPPETS, "likeFileName", relative.getPath());
assertEquals("Both sources are equal", direct, fromBuilder);
}
Aggregations