Search in sources :

Example 1 with NativeSignature

use of com.oracle.truffle.espresso.ffi.NativeSignature in project graal by oracle.

the class JniEnv method RegisterNative.

// endregion DirectBuffers
// region Register/Unregister natives
@JniImpl
@TruffleBoundary
public int RegisterNative(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject methodNamePtr, @Pointer TruffleObject methodSignaturePtr, @Pointer TruffleObject closure) {
    String methodName = NativeUtils.interopPointerToString(methodNamePtr);
    String methodSignature = NativeUtils.interopPointerToString(methodSignaturePtr);
    assert methodName != null && methodSignature != null;
    Symbol<Name> name = getNames().lookup(methodName);
    Symbol<Signature> signature = getSignatures().lookupValidSignature(methodSignature);
    Meta meta = getMeta();
    if (name == null || signature == null) {
        setPendingException(Meta.initException(meta.java_lang_NoSuchMethodError));
        return JNI_ERR;
    }
    Method targetMethod = clazz.getMirrorKlass().lookupDeclaredMethod(name, signature);
    if (targetMethod != null && targetMethod.isNative()) {
        targetMethod.unregisterNative();
        getSubstitutions().removeRuntimeSubstitution(targetMethod);
    } else {
        setPendingException(Meta.initException(meta.java_lang_NoSuchMethodError));
        return JNI_ERR;
    }
    Substitutions.EspressoRootNodeFactory factory = null;
    // Lookup known VM methods to shortcut native boudaries.
    factory = lookupKnownVmMethods(closure, targetMethod);
    if (factory == null) {
        NativeSignature ns = Method.buildJniNativeSignature(targetMethod.getParsedSignature());
        final TruffleObject boundNative = getNativeAccess().bindSymbol(closure, ns);
        factory = createJniRootNodeFactory(() -> new NativeMethodNode(boundNative, targetMethod.getMethodVersion()), targetMethod);
    }
    Symbol<Type> classType = clazz.getMirrorKlass().getType();
    getSubstitutions().registerRuntimeSubstitution(classType, name, signature, factory, true);
    return JNI_OK;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Method(com.oracle.truffle.espresso.impl.Method) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) NativeType(com.oracle.truffle.espresso.ffi.NativeType) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) Signature(com.oracle.truffle.espresso.descriptors.Symbol.Signature) Substitutions(com.oracle.truffle.espresso.substitutions.Substitutions) NativeMethodNode(com.oracle.truffle.espresso.nodes.NativeMethodNode) IntrinsifiedNativeMethodNode(com.oracle.truffle.espresso.nodes.IntrinsifiedNativeMethodNode) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 2 with NativeSignature

use of com.oracle.truffle.espresso.ffi.NativeSignature in project graal by oracle.

the class NativeEnv method createNativeClosureForFactory.

@TruffleBoundary
private TruffleObject createNativeClosureForFactory(CallableFromNative.Factory factory, String methodName) {
    // Dummy placeholder for unimplemented/unknown methods.
    if (factory == null) {
        String envName = NativeEnv.this.getClass().getSimpleName();
        getLogger().log(Level.FINER, "Fetching unknown/unimplemented {0} method: {1}", new Object[] { envName, methodName });
        @Pointer TruffleObject errorClosure = getNativeAccess().createNativeClosure(new Callback(0, new Callback.Function() {

            @Override
            public Object call(Object... args) {
                CompilerDirectives.transferToInterpreter();
                getLogger().log(Level.SEVERE, "Calling unimplemented {0} method: {1}", new Object[] { envName, methodName });
                throw EspressoError.unimplemented(envName + " method: " + methodName);
            }
        }), NativeSignature.create(NativeType.VOID));
        nativeClosures.add(errorClosure);
        return errorClosure;
    }
    NativeSignature signature = factory.jniNativeSignature();
    Callback target = intrinsicWrapper(factory);
    @Pointer TruffleObject nativeClosure = getNativeAccess().createNativeClosure(target, signature);
    nativeClosures.add(nativeClosure);
    return nativeClosure;
}
Also used : NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) Pointer(com.oracle.truffle.espresso.ffi.Pointer) RawPointer(com.oracle.truffle.espresso.ffi.RawPointer) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)2 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 NativeSignature (com.oracle.truffle.espresso.ffi.NativeSignature)2 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)1 Signature (com.oracle.truffle.espresso.descriptors.Symbol.Signature)1 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)1 NativeType (com.oracle.truffle.espresso.ffi.NativeType)1 Pointer (com.oracle.truffle.espresso.ffi.Pointer)1 RawPointer (com.oracle.truffle.espresso.ffi.RawPointer)1 Method (com.oracle.truffle.espresso.impl.Method)1 Meta (com.oracle.truffle.espresso.meta.Meta)1 IntrinsifiedNativeMethodNode (com.oracle.truffle.espresso.nodes.IntrinsifiedNativeMethodNode)1 NativeMethodNode (com.oracle.truffle.espresso.nodes.NativeMethodNode)1 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)1 Substitutions (com.oracle.truffle.espresso.substitutions.Substitutions)1