use of com.oracle.truffle.sl.runtime.SLBigNumber in project graal by oracle.
the class SLInstrumentTest method testEarlyReturn.
/**
* Test that we can forcibly return early from call nodes with an arbitrary value.
*/
@Test
public void testEarlyReturn() throws Exception {
String code = "function main() {\n" + " a = 10;\n" + " b = a;\n" + " // Let fce() warm up and specialize:\n" + " while (a == b && a < 100000) {\n" + " a = fce(a);\n" + " b = b + 1;\n" + " }\n" + " c = a;\n" + " // Run fce() and alter it's return type in an instrument:\n" + " c = fce(c);\n" + " return c;\n" + "}\n" + "function fce(x) {\n" + " return x + 1;\n" + "}\n";
final Source source = Source.newBuilder("sl", code, "testing").build();
ByteArrayOutputStream engineOut = new ByteArrayOutputStream();
Engine engine = Engine.newBuilder().err(engineOut).build();
Context context = Context.newBuilder().engine(engine).build();
// No instrument:
Value ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(100001L, ret.asLong());
EarlyReturnInstrument earlyReturn = context.getEngine().getInstruments().get("testEarlyReturn").lookup(EarlyReturnInstrument.class);
earlyReturn.fceCode = "fce(a)";
earlyReturn.returnValue = 200000L;
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(200001L, ret.asLong());
earlyReturn.returnValue = "Hello!";
ret = context.eval(source);
assertFalse(ret.isNumber());
assertTrue(ret.isString());
assertEquals("Hello!1", ret.asString());
// Specialize to long again:
earlyReturn.fceCode = "<>";
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(100001L, ret.asLong());
earlyReturn.fceCode = "fce(a)";
earlyReturn.returnValue = new BigInteger("-42");
boolean interopFailure;
try {
context.eval(source);
interopFailure = false;
} catch (PolyglotException err) {
interopFailure = true;
}
assertTrue(interopFailure);
earlyReturn.returnValue = new SLBigNumber(new BigInteger("-42"));
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(-41L, ret.asLong());
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = Boolean.TRUE;
ret = context.eval(source);
assertTrue(ret.isBoolean());
assertEquals(Boolean.TRUE, ret.asBoolean());
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = -42.42;
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(-42.42, ret.asDouble(), 1e-8);
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = "Hello!";
ret = context.eval(source);
assertTrue(ret.isString());
assertEquals("Hello!", ret.asString());
}
use of com.oracle.truffle.sl.runtime.SLBigNumber in project graal by oracle.
the class SLException method typeError.
/**
* Provides a user-readable message for run-time type errors. SL is strongly typed, i.e., there
* are no automatic type conversions of values.
*/
@TruffleBoundary
public static SLException typeError(Node operation, Object... values) {
StringBuilder result = new StringBuilder();
result.append("Type error");
if (operation != null) {
SourceSection ss = operation.getEncapsulatingSourceSection();
if (ss != null && ss.isAvailable()) {
result.append(" at ").append(ss.getSource().getName()).append(" line ").append(ss.getStartLine()).append(" col ").append(ss.getStartColumn());
}
}
result.append(": operation");
if (operation != null) {
NodeInfo nodeInfo = SLContext.lookupNodeInfo(operation.getClass());
if (nodeInfo != null) {
result.append(" \"").append(nodeInfo.shortName()).append("\"");
}
}
result.append(" not defined for");
String sep = " ";
for (int i = 0; i < values.length; i++) {
Object value = values[i];
result.append(sep);
sep = ", ";
if (value instanceof Long || value instanceof SLBigNumber) {
result.append("Number ").append(value);
} else if (value instanceof Boolean) {
result.append("Boolean ").append(value);
} else if (value instanceof String) {
result.append("String \"").append(value).append("\"");
} else if (value instanceof SLFunction) {
result.append("Function ").append(value);
} else if (value == SLNull.SINGLETON) {
result.append("NULL");
} else if (value == null) {
// value is not evaluated because of short circuit evaluation
result.append("ANY");
} else {
result.append(value);
}
}
return new SLException(result.toString(), operation);
}
Aggregations