use of org.graalvm.nativeimage.c.function.CEntryPoint 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();
}
use of org.graalvm.nativeimage.c.function.CEntryPoint in project graal by oracle.
the class CInterfaceTutorial method javaEntryPoint.
/* Java function that can be called directly from C code. */
@CEntryPoint(name = "java_entry_point")
protected static void javaEntryPoint(@SuppressWarnings("unused") IsolateThread thread, MyData data) {
/* Allocate a C structure in our stack frame. */
MyData copy = StackValue.get(SizeOf.get(MyData.class));
/* Get the size of a C structure. */
int dataSize = SizeOf.get(MyData.class);
/* Call a function from the standard C library. */
memcpy(copy, data, WordFactory.unsigned(dataSize));
dump(copy);
/* Modify primitive data of a C structure. */
data.setPrimitive(99);
data.addressOfArray().write(1, 101);
/* Pass out a pointer into a Java byte[] array that is pinned. */
String javaString = CTypeConversion.toJavaString(data.getCString()) + " at " + new Date();
pin = CTypeConversion.toCString(javaString);
CCharPointer cString = pin.get();
data.setCString(cString);
/* Install a function pointer to a Java function. */
data.setPrintFunction(javaPrintFunction.getFunctionPointer());
/* Create a handle to a Java object. */
data.setJavaObject(ObjectHandles.getGlobal().create(javaString));
}
use of org.graalvm.nativeimage.c.function.CEntryPoint in project graal by oracle.
the class PolyglotNativeAPI method polyglot_value_is_number.
@CEntryPoint(name = "polyglot_value_is_number")
public static PolyglotStatus polyglot_value_is_number(IsolateThread isolate_thread, PolyglotValuePointer value, CIntPointer result) {
return withHandledErrors(() -> {
Value jValue = fetchHandle(value);
result.write(jValue.isNumber() ? 1 : 0);
});
}
use of org.graalvm.nativeimage.c.function.CEntryPoint in project graal by oracle.
the class PolyglotNativeAPI method polyglot_create_double.
@SuppressWarnings("UnnecessaryBoxing")
@CEntryPoint(name = "polyglot_create_double")
public static PolyglotStatus polyglot_create_double(IsolateThread isolate_thread, PolyglotContextPointer polyglot_context, double value, PolyglotValuePointerPointer result) {
return withHandledErrors(() -> {
Context ctx = ObjectHandles.getGlobal().get(polyglot_context);
result.write(createHandle(ctx.asValue(Double.valueOf(value))));
});
}
use of org.graalvm.nativeimage.c.function.CEntryPoint in project graal by oracle.
the class PolyglotNativeAPI method polyglot_value_as_uint32.
@CEntryPoint(name = "polyglot_value_as_uint32")
public static PolyglotStatus polyglot_value_as_uint32(IsolateThread isolate_thread, PolyglotValuePointer value, CIntPointer result) {
return withHandledErrors(() -> {
Value valueObject = fetchHandle(value);
long longValue = valueObject.asLong();
if (longValue < 0 || longValue > 4228250625L) {
throw reportError("Value " + Long.toHexString(longValue) + "does not fit in unsigned int 32", polyglot_generic_failure);
}
result.write((int) longValue);
});
}
Aggregations