Search in sources :

Example 51 with CEntryPoint

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();
}
Also used : ObjectHandle(org.graalvm.nativeimage.ObjectHandle) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 52 with CEntryPoint

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));
}
Also used : CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) Date(java.util.Date) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 53 with CEntryPoint

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);
    });
}
Also used : Value(org.graalvm.polyglot.Value) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 54 with CEntryPoint

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))));
    });
}
Also used : CEntryPointContext(org.graalvm.nativeimage.c.function.CEntryPointContext) Context(org.graalvm.polyglot.Context) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 55 with CEntryPoint

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);
    });
}
Also used : Value(org.graalvm.polyglot.Value) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Aggregations

CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)81 CEntryPointOptions (com.oracle.svm.core.c.function.CEntryPointOptions)26 Value (org.graalvm.polyglot.Value)23 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)19 Context (org.graalvm.polyglot.Context)19 Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)7 ObjectHandle (org.graalvm.nativeimage.ObjectHandle)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)4 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)4 JNIObjectHandle (com.oracle.svm.jni.nativeapi.JNIObjectHandle)3 PinnedObject (org.graalvm.nativeimage.PinnedObject)3 Log (com.oracle.svm.core.log.Log)2 JNIAccessibleMethodDescriptor (com.oracle.svm.jni.access.JNIAccessibleMethodDescriptor)2 JNINativeLinkage (com.oracle.svm.jni.access.JNINativeLinkage)2 JNIMethodId (com.oracle.svm.jni.nativeapi.JNIMethodId)2 JNINativeMethod (com.oracle.svm.jni.nativeapi.JNINativeMethod)2 Executable (java.lang.reflect.Executable)2 Method (java.lang.reflect.Method)2 ByteBuffer (java.nio.ByteBuffer)2