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);
});
}
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);
});
}
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)));
});
}
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();
}
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();
}
Aggregations