Search in sources :

Example 1 with PolyglotCallbackInfo

use of org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotCallbackInfo 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)

Aggregations

ObjectHandle (org.graalvm.nativeimage.ObjectHandle)1 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)1 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)1 Context (org.graalvm.polyglot.Context)1 Value (org.graalvm.polyglot.Value)1 PolyglotCallbackInfo (org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotCallbackInfo)1 PolyglotValuePointer (org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotValuePointer)1 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)1