Search in sources :

Example 71 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class PolyglotNativeAPI method polyglot_create_function.

@CEntryPoint(name = "polyglot_create_function")
public static PolyglotStatus polyglot_create_function(IsolateThread isolate_thread, PolyglotContextPointer polyglot_context, PolyglotCallbackPointer callback, VoidPointer data, PolyglotValuePointerPointer value) {
    return withHandledErrors(() -> {
        Context c = ObjectHandles.getGlobal().get(polyglot_context);
        ProxyExecutable executable = (Value... arguments) -> {
            ObjectHandle[] handleArgs = new ObjectHandle[arguments.length];
            for (int i = 0; i < arguments.length; i++) {
                handleArgs[i] = createHandle(arguments[i]);
            }
            PolyglotCallbackInfo cbInfo = (PolyglotCallbackInfo) createHandle(new PolyglotCallbackInfoInternal(handleArgs, data));
            try {
                PolyglotValuePointer result = callback.invoke(CEntryPointContext.getCurrentIsolateThread(), cbInfo);
                CallbackException ce = exceptionsTL.get();
                if (ce != null) {
                    exceptionsTL.remove();
                    throw ce;
                } else {
                    return PolyglotNativeAPI.fetchHandle(result);
                }
            } finally {
                PolyglotCallbackInfoInternal info = fetchHandle(cbInfo);
                for (ObjectHandle arg : info.arguments) {
                    freeHandle(arg);
                }
                freeHandle(cbInfo);
            }
        };
        value.write(createHandle(c.asValue(executable)));
    });
}
Also used : CEntryPointContext(org.graalvm.nativeimage.c.function.CEntryPointContext) Context(org.graalvm.polyglot.Context) PolyglotCallbackInfo(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotCallbackInfo) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) ObjectHandle(org.graalvm.nativeimage.ObjectHandle) Value(org.graalvm.polyglot.Value) PolyglotValuePointer(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotValuePointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 72 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class CPUSamplerSnippets method example.

@SuppressWarnings("unused")
public void example() {
    // @formatter:off
    // BEGIN: CPUSamplerSnippets#example
    Context context = Context.create();
    CPUSampler sampler = CPUSampler.find(context.getEngine());
    sampler.setCollecting(true);
    context.eval("...", "...");
    sampler.setCollecting(false);
    sampler.close();
    // Read information about the roots of the tree.
    for (ProfilerNode<CPUSampler.Payload> node : sampler.getRootNodes()) {
        final String rootName = node.getRootName();
        final int selfHitCount = node.getPayload().getSelfHitCount();
    // ...
    }
// END: CPUSamplerSnippets#example
// @formatter:on
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context)

Example 73 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class CPUTracerSnippets method example.

@SuppressWarnings("unused")
public void example() {
    // @formatter:off
    // BEGIN: CPUTracerSnippets#example
    Context context = Context.create();
    CPUTracer tracer = CPUTracer.find(context.getEngine());
    tracer.setCollecting(true);
    context.eval("...", "...");
    tracer.setCollecting(false);
    tracer.close();
    // Read information about execution counts of elements.
    for (CPUTracer.Payload p : tracer.getPayloads()) {
        final String rootName = p.getRootName();
        final long count = p.getCount();
    }
// END: CPUTracerSnippets#example
// @formatter:on
}
Also used : EventContext(com.oracle.truffle.api.instrumentation.EventContext) Context(org.graalvm.polyglot.Context)

Example 74 with Context

use of org.graalvm.polyglot.Context in project graal by oracle.

the class SLInstrumentTest method testChangeArgumentsOnReenter.

/**
 * Test that we can alter function arguments on reenter.
 */
@Test
public void testChangeArgumentsOnReenter() throws Exception {
    String code = "function main() {\n" + "  y = fce(0, 10000);\n" + "  return y;\n" + "}\n" + "function fce(x, z) {\n" + "  y = 2 * x;\n" + "  if (y < z) {\n" + "    print(\"A bad error.\");\n" + "    return 0 - 1;\n" + "  } else {\n" + "    return y;\n" + "  }\n" + "}\n";
    final Source source = Source.newBuilder("sl", code, "testing").build();
    Context context = Context.create();
    IncreaseArgOnErrorInstrument incOnError = context.getEngine().getInstruments().get("testIncreaseArgumentOnError").lookup(IncreaseArgOnErrorInstrument.class);
    incOnError.attachOn("A bad error");
    Value ret = context.eval(source);
    assertEquals(10000, ret.asInt());
}
Also used : Context(org.graalvm.polyglot.Context) EventContext(com.oracle.truffle.api.instrumentation.EventContext) Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 75 with Context

use of org.graalvm.polyglot.Context 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());
}
Also used : Context(org.graalvm.polyglot.Context) EventContext(com.oracle.truffle.api.instrumentation.EventContext) SLBigNumber(com.oracle.truffle.sl.runtime.SLBigNumber) Value(org.graalvm.polyglot.Value) BigInteger(java.math.BigInteger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Aggregations

Context (org.graalvm.polyglot.Context)185 Test (org.junit.Test)148 Value (org.graalvm.polyglot.Value)58 TruffleContext (com.oracle.truffle.api.TruffleContext)56 Env (com.oracle.truffle.api.TruffleLanguage.Env)41 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)37 Engine (org.graalvm.polyglot.Engine)32 ArrayList (java.util.ArrayList)29 PolyglotException (org.graalvm.polyglot.PolyglotException)24 Source (org.graalvm.polyglot.Source)22 Path (java.nio.file.Path)21 TruffleFile (com.oracle.truffle.api.TruffleFile)20 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)20 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)19 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)19 LanguageContext (com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext)17 IOException (java.io.IOException)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 EventContext (com.oracle.truffle.api.instrumentation.EventContext)13 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)12