⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 native.c

📁 This is a java virtual machine implement in c
💻 C
字号:
/*0001*//*
/*0002./ * Copyright (c) 1998-2001 Sun Microsystems, Inc. All Rights Reserved.
/*0003./ *
/*0004./ * This software is the confidential and proprietary information of Sun
/*0005./ * Microsystems, Inc. ("Confidential Information").  You shall not
/*0006./ * disclose such Confidential Information and shall use it only in
/*0007./ * accordance with the terms of the license agreement you entered into
/*0008./ * with Sun.
/*0009./ *
/*0010./ * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
/*0011./ * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/*0012./ * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
/*0013./ * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
/*0014./ * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
/*0015./ * THIS SOFTWARE OR ITS DERIVATIVES.
/*0016./ *
/*0017./ */
/*0018*/
/*0019*//*=========================================================================
/*0020./ * SYSTEM:    KVM
/*0021./ * SUBSYSTEM: Native function interface
/*0022./ * FILE:      native.c
/*0023./ * OVERVIEW:  This file defines the interface for plugging in
/*0024./ *            the native functions needed by the Java Virtual
/*0025./ *            Machine. The implementation here is _not_ based
/*0026./ *            on JNI (Java Native Interface), because JNI seems
/*0027./ *            too expensive for small devices.
/*0028./ * AUTHOR:    Antero Taivalsaari, Sun Labs
/*0029./ *            Edited by Doug Simon 11/1998
/*0030./ *            Frank Yellin
/*0031./ *=======================================================================*/
/*0032*/
/*0033*//*=========================================================================
/*0034./ * Include files
/*0035./ *=======================================================================*/
/*0036*/
/*0037*/#include <global.h>
/*0038*/
/*0039*/METHOD CurrentNativeMethod;
/*0040*/
/*0041*//*=========================================================================
/*0042./ * Operations on native functions
/*0043./ *=======================================================================*/
/*0044*/int xstrcmp(const char *s1, const char *s2) {
/*0045*/    if (s1 == NULL) {
/*0046*/        s1 = "";
/*0047*/    }
/*0048*/    if (s2 == NULL) {
/*0049*/       s2 = "";
/*0050*/    }
/*0051*/    return strcmp(s1, s2);
/*0052*/}
/*0053*/
/*0054*//*=========================================================================
/*0055./ * FUNCTION:      getNativeFunction()
/*0056./ * TYPE:          lookup operation
/*0057./ * OVERVIEW:      Given a function name as a string, try to find
/*0058./ *                a corresponding native function and return a
/*0059./ *                a pointer to it if found.
/*0060./ * INTERFACE:
/*0061./ *   parameters:  class name, method name
/*0062./ *   returns:     function pointer or NIL if not found.
/*0063./ *=======================================================================*/
/*0064*/
/*0065*/NativeFunctionPtr
/*0066*/getNativeFunction(INSTANCE_CLASS clazz, const char* methodName, 
/*0067*/                                        const char *methodSignature)
/*0068*/{
/*0069*/#if !ROMIZING
/*0070*/    const ClassNativeImplementationType *cptr;
/*0071*/    const NativeImplementationType *mptr;
/*0072*/    UString UBaseName    = clazz->clazz.baseName;
/*0073*/    UString UPackageName = clazz->clazz.packageName;
/*0074*/    char* baseName;
/*0075*/    char* packageName;
/*0076*/
/*0077*/
/*0078*/    /* Package names can be NULL -> must do an explicit check */
/*0079*/    /* to ensure that string comparison below will not fail */
/*0080*/    if (UPackageName == NULL) {
/*0081*/        packageName = "";
/*0082*/    } else {
/*0083*/        packageName = UStringInfo(UPackageName);
/*0084*/    }
/*0085*/
/*0086*/    if (UBaseName == NULL) {
/*0087*/        baseName = "";
/*0088*/    } else {
/*0089*/        baseName = UStringInfo(UBaseName);
/*0090*/    }
/*0091*/    
/*0092*/
/*0093*/    for (cptr = nativeImplementations; cptr->baseName != NULL ; cptr++) {                 
/*0094*/        if (   (xstrcmp(cptr->packageName, packageName) == 0) 
/*0095*/            && (xstrcmp(cptr->baseName, baseName) == 0)) { 
/*0096*/            break;
/*0097*/        }
/*0098*/    }
/*0099*/
/*0100*/    for (mptr = cptr->implementation; mptr != NULL ; mptr++) {
/*0101*/        const char *name = mptr->name;
/*0102*/        if (name == NULL) {
/*0103*/            return NULL;
/*0104*/        }
/*0105*/        if (strcmp(name, methodName) == 0) {
/*0106*/            const char *signature = mptr->signature;
/*0107*/            /* The signature is NULL for non-overloaded native methods. */
/*0108*/            if (signature == NULL || (xstrcmp(signature, methodSignature) == 0)){
/*0109*/                return mptr->implementation;
/*0110*/            }
/*0111*/        }
/*0112*/    }
/*0113*/
/*0114*/#else
/*0115*/    return NULL;
/*0116*/#endif /* !ROMIZING */
/*0117*/}
/*0118*/
/*0119*//*=========================================================================
/*0120./ * FUNCTION:      invokeNativeFunction()
/*0121./ * TYPE:          private operation
/*0122./ * OVERVIEW:      Invoke a native function, resolving the native
/*0123./ *                function reference if necessary.
/*0124./ * INTERFACE:
/*0125./ *   parameters:  method pointer
/*0126./ *   returns:     <nothing>
/*0127./ * NOTE:          Native functions are automatically synchronized, i.e.,
/*0128./ *                they cannot be interrupted by other threads unless
/*0129./ *                the native code happens to invoke the Java interpreter.
/*0130./ *=======================================================================*/
/*0131*/
/*0132*/void invokeNativeFunction(METHOD thisMethod)
/*0133*/{
/*0137*/    NativeFunctionPtr native = thisMethod->u.native.code;
/*0138*/
/*0139*/    if (native == NULL) {
/*0140*/        /*  Native function not found; throw error */
/*0141*/
/*0142*/        /* The GC may get confused by the arguments on the stack */
/*0143*/        setSP(getSP() - thisMethod->argCount);
/*0144*/
/*0145*/        START_TEMPORARY_ROOTS
/*0146*/            DECLARE_TEMPORARY_ROOT(char*, className, 
/*0147*/                     getClassName((CLASS)(thisMethod->ofClass)));
/*0148*/            sprintf(str_buffer,
/*0149*/                    KVM_MSG_NATIVE_METHOD_NOT_FOUND_2STRPARAMS,
/*0150*/                    className, methodName(thisMethod));
/*0151*/        END_TEMPORARY_ROOTS
/*0152*/        fatalError(str_buffer);
/*0153*/    }
/*0154*/
/*0163*/    /* Call the native function we are supposed to call */
/*0164*/    CurrentNativeMethod = thisMethod;
/*0165*/    native();
/*0166*/    CurrentNativeMethod = NULL;
/*0186*/
/*0187*/}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -