Search in sources :

Example 1 with ObjectHandle

use of org.graalvm.nativeimage.ObjectHandle in project graal by oracle.

the class PolyglotNativeAPI method polyglot_get_callback_info.

@CEntryPoint(name = "polyglot_get_callback_info")
public static PolyglotStatus polyglot_get_callback_info(IsolateThread isolate_thread, PolyglotCallbackInfo callback_info, SizeTPointer argc, PolyglotValuePointerPointer argv, WordPointer data) {
    return withHandledErrors(() -> {
        PolyglotCallbackInfoInternal callbackInfo = fetchHandle(callback_info);
        UnsignedWord numberOfArguments = WordFactory.unsigned(callbackInfo.arguments.length);
        UnsignedWord bufferSize = argc.read();
        UnsignedWord size = bufferSize.belowThan(numberOfArguments) ? bufferSize : numberOfArguments;
        argc.write(size);
        for (UnsignedWord i = WordFactory.zero(); i.belowThan(size); i = i.add(1)) {
            int index = (int) i.rawValue();
            ObjectHandle argument = callbackInfo.arguments[index];
            argv.write(index, argument);
        }
        data.write(callbackInfo.data);
    });
}
Also used : ObjectHandle(org.graalvm.nativeimage.ObjectHandle) UnsignedWord(org.graalvm.word.UnsignedWord) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 2 with ObjectHandle

use of org.graalvm.nativeimage.ObjectHandle in project graal by oracle.

the class PolyglotNativeAPI method polyglot_create_engine.

@CEntryPoint(name = "polyglot_create_engine", documentation = { "Creates a polyglot engine.", "Engine is a unit that holds configuration, instruments, and compiled code for many contexts", "inside the engine." })
public static PolyglotStatus polyglot_create_engine(IsolateThread isolate_thread, PolyglotEnginePointerPointer engine) {
    return withHandledErrors(() -> {
        ObjectHandle handle = createHandle(Engine.create());
        engine.write(handle);
    });
}
Also used : ObjectHandle(org.graalvm.nativeimage.ObjectHandle) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 3 with ObjectHandle

use of org.graalvm.nativeimage.ObjectHandle 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 4 with ObjectHandle

use of org.graalvm.nativeimage.ObjectHandle in project graal by oracle.

the class PosixThreadsFeature method pthreadStartRoutine.

@CEntryPoint
@CEntryPointOptions(prologue = PthreadStartRoutinePrologue.class, epilogue = LeaveDetachThreadEpilogue.class, publishAs = Publish.NotPublished, include = CEntryPointOptions.NotIncludedAutomatically.class)
static WordBase pthreadStartRoutine(ThreadStartData data) {
    ObjectHandle threadHandle = data.getThreadHandle();
    UnmanagedMemory.free(data);
    Thread thread = ObjectHandles.getGlobal().get(threadHandle);
    boolean status = singleton().assignJavaThread(thread, false);
    VMError.guarantee(status, "currentThread already initialized");
    /*
         * Destroy the handle only after setting currentThread, since the lock used by destroy
         * requires the current thread.
         */
    ObjectHandles.getGlobal().destroy(threadHandle);
    /* Complete the initialization of the thread, now that it is (nearly) running. */
    setPthreadIdentifier(thread, Pthread.pthread_self());
    singleton().setNativeName(thread, thread.getName());
    singleton().noteThreadStart(thread);
    try {
        thread.run();
    } catch (Throwable ex) {
        SnippetRuntime.reportUnhandledExceptionJava(ex);
    } finally {
        exit(thread);
        singleton().noteThreadFinish(thread);
    }
    return WordFactory.nullPointer();
}
Also used : ObjectHandle(org.graalvm.nativeimage.ObjectHandle) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CEntryPointOptions(com.oracle.svm.core.c.function.CEntryPointOptions)

Example 5 with ObjectHandle

use of org.graalvm.nativeimage.ObjectHandle in project graal by oracle.

the class CInterfaceTutorial method releaseData.

/* Java function that can be called directly from C code. */
@CEntryPoint(name = "java_release_data")
protected static void releaseData(@SuppressWarnings("unused") IsolateThread thread, MyData data) {
    dump(data);
    /* Retrieve the object we have stored in a handle. */
    ObjectHandle handle = data.getJavaObject();
    String javaObject = ObjectHandles.getGlobal().get(handle);
    System.out.format("javaObject: %s\n", javaObject);
    /* Free the handle. After this call, the handle must not be used anymore. */
    ObjectHandles.getGlobal().destroy(handle);
    /*
         * Release the pin to the byte[] array. After this call, the field f_cstr of our data object
         * must not be accessed anymore, the memory it points to might no longer be valid.
         */
    pin.close();
}
Also used : ObjectHandle(org.graalvm.nativeimage.ObjectHandle) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Aggregations

ObjectHandle (org.graalvm.nativeimage.ObjectHandle)5 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)5 CEntryPointOptions (com.oracle.svm.core.c.function.CEntryPointOptions)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 UnsignedWord (org.graalvm.word.UnsignedWord)1