use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.
the class LanguageSPITest method testLookupHostArray.
@Test
public void testLookupHostArray() {
Context context = Context.newBuilder().allowHostAccess(true).build();
Value value = eval(context, new Function<Env, Object>() {
public Object apply(Env t) {
return t.lookupHostSymbol("java.lang.String[]");
}
});
assertTrue(value.isHostObject());
Object map = value.asHostObject();
assertSame(map, String[].class);
context.close();
}
use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.
the class LanguageSPITest method testInnerContext.
@Test
public void testInnerContext() {
Context context = Context.create();
Function<Env, Object> f = new Function<Env, Object>() {
public Object apply(Env env) {
LanguageContext outerLangContext = LanguageSPITestLanguage.getContext();
Object config = new Object();
TruffleContext innerContext = env.newContextBuilder().config("config", config).build();
Object p = innerContext.enter();
LanguageContext innerLangContext = LanguageSPITestLanguage.getContext();
try {
try {
innerContext.close();
fail("context could be closed when entered");
} catch (IllegalStateException e) {
}
assertEquals(0, innerLangContext.disposeCalled);
assertEquals(config, innerLangContext.config.get("config"));
assertNotSame(outerLangContext, innerLangContext);
boolean assertions = false;
assert (assertions = true) == true;
if (assertions) {
boolean leaveFailed = false;
try {
innerContext.leave("foo");
} catch (AssertionError e) {
leaveFailed = true;
}
if (!leaveFailed) {
fail("no assertion error for leaving with the wrong object");
}
}
} finally {
innerContext.leave(p);
}
assertSame(outerLangContext, LanguageSPITestLanguage.getContext());
innerContext.close();
try {
innerContext.enter();
fail("cannot be entered after closing");
} catch (IllegalStateException e) {
}
innerContext.close();
assertEquals(1, innerLangContext.disposeCalled);
return null;
}
};
eval(context, f);
// ensure we are not yet closed
eval(context, f);
context.close();
}
use of com.oracle.truffle.api.TruffleLanguage.Env 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.TruffleLanguage.Env in project graal by oracle.
the class LanguageSPITest method testCancelExecutionWhileSleeping.
@Test
public void testCancelExecutionWhileSleeping() throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(1);
try {
Engine engine = Engine.create();
Context context = Context.newBuilder(LanguageSPITestLanguage.ID).engine(engine).build();
CountDownLatch beforeSleep = new CountDownLatch(1);
CountDownLatch interrupt = new CountDownLatch(1);
AtomicInteger gotInterrupt = new AtomicInteger(0);
Function<Env, Object> f = new Function<Env, Object>() {
public Object apply(Env t) {
try {
beforeSleep.countDown();
Thread.sleep(5000);
} catch (InterruptedException e) {
gotInterrupt.incrementAndGet();
interrupt.countDown();
throw new Interrupted();
}
return null;
}
};
Future<Value> future = service.submit(() -> eval(context, f));
beforeSleep.await(10000, TimeUnit.MILLISECONDS);
context.close(true);
interrupt.await(10000, TimeUnit.MILLISECONDS);
assertEquals(1, gotInterrupt.get());
try {
future.get();
fail();
} catch (ExecutionException e) {
PolyglotException polyglotException = (PolyglotException) e.getCause();
assertTrue(polyglotException.isCancelled());
}
engine.close();
} finally {
service.shutdown();
}
}
use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.
the class LanguageSPITest method testEngineCloseInsideFromSameThread.
@Test
public void testEngineCloseInsideFromSameThread() {
Engine engine = Engine.create();
langContext = null;
Context context = Context.newBuilder(LanguageSPITestLanguage.ID).engine(engine).build();
eval(context, new Function<Env, Object>() {
public Object apply(Env t) {
engine.close();
return null;
}
});
assertEquals(1, langContext.disposeCalled);
engine.close();
}
Aggregations