Search in sources :

Example 1 with NativeMethod

use of org.jikesrvm.classloader.NativeMethod in project JikesRVM by JikesRVM.

the class JNIFunctions method RegisterNatives.

/**
 * RegisterNatives: registers implementation of native methods
 * @param env A JREF index for the JNI environment object
 * @param classJREF a JREF index for the class to register native methods in
 * @param methodsAddress the address of an array of native methods to be registered
 * @param nmethods the number of native methods in the array
 * @return 0 is successful -1 if failed
 * @throws NoSuchMethodError if a specified method cannot be found or is not native
 */
private static int RegisterNatives(JNIEnvironment env, int classJREF, Address methodsAddress, int nmethods) {
    if (traceJNI)
        VM.sysWriteln("JNI called: RegisterNatives");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        // get the target class
        Class<?> jcls = (Class<?>) env.getJNIRef(classJREF);
        RVMType type = java.lang.JikesRVMSupport.getTypeForClass(jcls);
        if (!type.isClassType()) {
            env.recordException(new NoSuchMethodError());
            return 0;
        }
        RVMClass klass = type.asClass();
        if (!klass.isInitialized()) {
            RuntimeEntrypoints.initializeClassForDynamicLink(klass);
        }
        // Create list of methods and verify them to avoid partial success
        NativeMethod[] methods = new NativeMethod[nmethods];
        AddressArray symbols = AddressArray.create(nmethods);
        Address curMethod = methodsAddress;
        for (int i = 0; i < nmethods; i++) {
            String methodString = JNIGenericHelpers.createStringFromC(curMethod.loadAddress());
            Atom methodName = Atom.findOrCreateAsciiAtom(methodString);
            String sigString = JNIGenericHelpers.createStringFromC(curMethod.loadAddress(Offset.fromIntSignExtend(BYTES_IN_ADDRESS)));
            Atom sigName = Atom.findOrCreateAsciiAtom(sigString);
            // Find the target method
            RVMMethod meth = klass.findDeclaredMethod(methodName, sigName);
            if (meth == null || !meth.isNative()) {
                env.recordException(new NoSuchMethodError(klass + ": " + methodName + " " + sigName));
                return -1;
            }
            methods[i] = (NativeMethod) meth;
            symbols.set(i, curMethod.loadAddress(Offset.fromIntSignExtend(BYTES_IN_ADDRESS * 2)));
            curMethod = curMethod.plus(3 * BYTES_IN_ADDRESS);
        }
        // Register methods
        for (int i = 0; i < nmethods; i++) {
            methods[i].registerNativeSymbol(symbols.get(i));
        }
        return 0;
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
        return -1;
    }
}
Also used : AddressArray(org.vmmagic.unboxed.AddressArray) Address(org.vmmagic.unboxed.Address) RVMType(org.jikesrvm.classloader.RVMType) Atom(org.jikesrvm.classloader.Atom) RVMClass(org.jikesrvm.classloader.RVMClass) RVMMethod(org.jikesrvm.classloader.RVMMethod) NativeMethod(org.jikesrvm.classloader.NativeMethod) RVMClass(org.jikesrvm.classloader.RVMClass)

Aggregations

Atom (org.jikesrvm.classloader.Atom)1 NativeMethod (org.jikesrvm.classloader.NativeMethod)1 RVMClass (org.jikesrvm.classloader.RVMClass)1 RVMMethod (org.jikesrvm.classloader.RVMMethod)1 RVMType (org.jikesrvm.classloader.RVMType)1 Address (org.vmmagic.unboxed.Address)1 AddressArray (org.vmmagic.unboxed.AddressArray)1