Search in sources :

Example 1 with PolyglotValuePointer

use of org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotValuePointer in project graal by oracle.

the class PolyglotNativeAPI method polyglot_execute.

@CEntryPoint(name = "polyglot_execute")
public static PolyglotStatus polyglot_execute(IsolateThread isolate_thread, PolyglotValuePointer value_handle, PolyglotValuePointerPointer args, int args_size, PolyglotValuePointerPointer return_value) {
    return withHandledErrors(() -> {
        Value function = fetchHandle(value_handle);
        Object[] jArgs = new Object[args_size];
        for (int i = 0; i < args_size; i++) {
            PolyglotValuePointer handle = args.read(i);
            jArgs[i] = fetchHandle(handle);
        }
        Value result = function.execute(jArgs);
        return_value.write(createHandle(result));
    });
}
Also used : Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) PolyglotValuePointer(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotValuePointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 2 with PolyglotValuePointer

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

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