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