📄 kni.c
字号:
/* * Copyright (c) 1998-2002 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. * * Use is subject to license terms. *//*======================================================================= * SYSTEM: KVM * SUBSYSTEM: K Native Interface (KNI) * FILE: kni.c * OVERVIEW: KNI implementation for the KVM. * AUTHORS: Efren A. Serra, Antero Taivalsaari *=======================================================================*//*========================================================================= * Include files *=======================================================================*/#include "kni.h"/*========================================================================= * Definitions and declarations *=======================================================================*//* Get the contents of a handle *//* (not intended for end users) */#define KNI_UNHAND(handle) (*(handle))/* Set the contents of a handle *//* (not intended for end users) */#define KNI_SETHAND(handle, value) (*(cell*)(handle)) = (cell)(value)/*========================================================================= * Private functions *=======================================================================*//*========================================================================= * FUNCTION: kvm_resetOperandStack() * OVERVIEW: Private function for resetting the operand * stack of the KVM to the state it is intended to * be upon exiting a native function or before * pushing the return value of a native function * to be operand stack. * INTERFACE: * parameter(s): <none> * returns: <nothing> *=======================================================================*/void kvm_resetOperandStack() { int decrement = (CurrentNativeMethod->accessFlags & ACC_STATIC) ? 0 : 1; setSP(CurrentThread->nativeLp - decrement);}/*========================================================================= * FUNCTION: KNI_registerCleanup() * OVERVIEW: Private function for calling KVM-specific * native cleanup registration functionality. * For more comments on this feature, see file * "kni.h" or "garbage.h". * INTERFACE: * parameter(s): objectHandle: the object for whom a native cleanup * callback is registered * cb: the callback routine to be called when the * object is garbage collected. * returns: <nothing> *=======================================================================*/void KNI_registerCleanup(jobject objectHandle, KNICleanupCallback cb) { /* We can safely cast the parameters, */ /* since they are functionally equivalent */ registerCleanup((INSTANCE_HANDLE)objectHandle, (CleanupCallback)cb);}/*========================================================================= * Public KNI functions *=======================================================================*//*========================================================================= * Version information *=======================================================================*//*========================================================================= * FUNCTION: KNI_GetVersion() * OVERVIEW: Returns the KNI version information. * INTERFACE: * parameter(s): <none> * returns: The current KNI version *=======================================================================*/jint KNI_GetVersion() { return KNI_VERSION;}/*========================================================================= * Class and interface operations *=======================================================================*//*========================================================================= * FUNCTION: KNI_FindClass() * OVERVIEW: Returns a handle to a class or interface type. * This operation does not actually load the class. * If the class hasn't been loaded yet, or if the * class cannot be found, the function sets the * classHandle to NULL. * INTERFACE: * parameter(s): name: a class descriptor as a UTF8 string * (example: "java/lang/String") * classHandle: a handle to which the class pointer * will be stored. * returns: nothing directly, but returns the class pointer * in handle 'classHandle'. If the class cannot be * found, classHandle will be set to NULL. *=======================================================================*/void KNI_FindClass(const char* name, jclass classHandle) { CLASS clazz = getRawClass(name); if (IS_ARRAY_CLASS(clazz) || ((INSTANCE_CLASS)clazz)->status >= CLASS_READY) { KNI_SETHAND(classHandle, clazz); } else { KNI_SETHAND(classHandle, NULL); }}/*========================================================================= * FUNCTION: KNI_GetSuperClass() * OVERVIEW: Returns a handle to the superclass of the given class * or interface. * INTERFACE: * parameter(s): classHandle: a handle pointing to a class * superclassHandle: another handle to which the * superclass pointer will be stored * returns: nothing directly, but returns the superclass pointer * in handle 'superclassHandle'. If no superclass can * be found, superclassHandle will be set to NULL. *=======================================================================*/void KNI_GetSuperClass(jclass classHandle, jclass superclassHandle) { INSTANCE_CLASS clazz = (INSTANCE_CLASS)KNI_UNHAND(classHandle); if (INCLUDEDEBUGCODE && clazz == 0) return; KNI_SETHAND(superclassHandle, clazz->superClass);}/*========================================================================= * FUNCTION: KNI_IsAssignableFrom() * OVERVIEW: Determines whether an instance of one class or interface * can be assigned to an instance of another class or * interface * INTERFACE: * parameter(s): classHandle1: a handle to a class or interface * classHandle2: a handle to a class or interface * returns: true or false *=======================================================================*/jboolean KNI_IsAssignableFrom(jclass classHandle1, jclass classHandle2) { CLASS class1 = (CLASS)KNI_UNHAND(classHandle1); CLASS class2 = (CLASS)KNI_UNHAND(classHandle2); return isAssignableTo(class1, class2);}/*========================================================================= * Exceptions and errors *=======================================================================*//*========================================================================= * FUNCTION: KNI_ThrowNew() * OVERVIEW: Raises an exception in the current thread. * INTERFACE: * parameter(s): name: class descriptor in UTF8 format * (example: "java/lang/Exception") * message: the exception message * returns: KNI_OK upon success; otherwise KNI_ERR * NOTE: KNI_ThrowNew does not run the constructor * on the new exception objects. *=======================================================================*/jint KNI_ThrowNew(const char* name, const char* message) { /* If the exception is thrown from a native */ /* finalizer (CurrentNativeMethod is NULL), */ /* then the exception must not be allowed. */ if (CurrentNativeMethod == NULL) return KNI_ERR; /* Redundant check: Make sure the exception class exists */ if (getClass(name)) { CurrentThread->pendingException = (char*)name; CurrentThread->exceptionMessage = (char*)message; return KNI_OK; } else { return KNI_ERR; }}/*========================================================================= * FUNCTION: KNI_FatalError() * OVERVIEW: Print an error message and stop the VM immediately. * INTERFACE: * parameter(s): message: an error message * returns: <nothing> (the function does not return) *=======================================================================*/void KNI_FatalError(const char* message) { fatalError(message);}/*========================================================================= * Object operations *=======================================================================*//*======================================================================= * FUNCTION: KNI_GetObjectClass() * OVERVIEW: Returns the class of a given instance. * INTERFACE: * parameter(s): objectHandle: a handle to an object * classHandle: another handle to which the * class pointer will be assigned * returns: Nothing directly, but the handle 'classHandle' * will contain the class pointer that was found. *=======================================================================*/void KNI_GetObjectClass(jobject objectHandle, jclass classHandle) { INSTANCE object = (INSTANCE)KNI_UNHAND(objectHandle); KNI_SETHAND(classHandle, object->ofClass);}/*========================================================================= * FUNCTION: KNI_IsInstanceOf() * OVERVIEW: Determines whether an object is an instance of * the given class or interface. * INTERFACE: * parameter(s): An pointer to an instance object and a pointer * to a class object * returns: true or false *=======================================================================*/jboolean KNI_IsInstanceOf(jobject objectHandle, jclass classHandle) { INSTANCE object = (INSTANCE)KNI_UNHAND(objectHandle); CLASS class1 = (CLASS)object->ofClass; CLASS class2 = (CLASS)KNI_UNHAND(classHandle); return isAssignableToFast(class1, class2);}/*========================================================================= * Instance field access *=======================================================================*//*========================================================================= * FUNCTION: KNI_GetFieldID() * OVERVIEW: Get the fieldID of the field with the given * name and signature. This operation is used before * accessing the instance variables of an object. * INTERFACE: * parameter(s): - classHandle: handle to a class * - name: UTF8 string containing the name of the field. * - signature: UTF8 string containing the signature * of the field (using the standard Java field * signature format, e.g., "I", "Ljava/lang/Object;") * returns: - A jfieldID *=======================================================================*/jfieldIDKNI_GetFieldID(jclass classHandle, const char* name, const char* signature) { FIELD field; INSTANCE_CLASS clazz = (INSTANCE_CLASS)KNI_UNHAND(classHandle); if (clazz == 0) return NULL; field = lookupField(clazz, getNameAndTypeKey(name, signature)); return (jfieldID)(field == NULL || field->accessFlags & ACC_STATIC) ? NULL : field;}/*======================================================================= * FUNCTION: KNI_Get<Type>Field() * OVERVIEW: Return the value of a primitive instance field. * No type checking is performed. * INTERFACE: * parameter(s): objectHandle: handle to an object * fid: a fieldID * returns: The value of the field *=======================================================================*/jboolean KNI_GetBooleanField(jobject objectHandle, jfieldID fid) { INSTANCE object = (INSTANCE)KNI_UNHAND(objectHandle); if (INCLUDEDEBUGCODE && (object == 0 || fid == 0)) return 0; return (jboolean)(object->data[fid->u.offset].cell);}jbyte KNI_GetByteField(jobject objectHandle, jfieldID fid) { INSTANCE object = (INSTANCE)KNI_UNHAND(objectHandle);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -