1 /* 2 * Copyright 2009, The Android Open Source Project 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "MyJavaVM.h" 28 29 #include <JNIUtility.h> 30 #include <jni.h> 31 32 static JNIEnv* s_env; 33 static JavaVM* s_jvm; 34 35 // JavaVM functions 36 jint vm_attachCurrentThread(JavaVM*, JNIEnv** env, void*) { 37 *env = s_env; 38 return JNI_OK; 39 } 40 41 // JNIEnv functions 42 jobject env_callObjectMethodV(JNIEnv*, jobject, jmethodID, va_list) { 43 return MY_JOBJECT; 44 } 45 void env_callVoidMethodV(JNIEnv*, jobject, jmethodID, va_list) {} 46 void env_deleteRef(JNIEnv*, jobject) {} 47 jboolean env_exceptionCheck(JNIEnv*) { 48 return false; 49 } 50 jclass env_findClass(JNIEnv*, const char*) { 51 return (jclass) 1; 52 } 53 jbyte* env_getByteArrayElements(JNIEnv*, jbyteArray, jboolean*) { 54 return NULL; 55 } 56 jmethodID env_getMethodID(JNIEnv*, jclass, const char*, const char*) { 57 return (jmethodID) 1; 58 } 59 jclass env_getObjectClass(JNIEnv*, jobject) { 60 return (jclass) 1; 61 } 62 static const char* s_fakeString = "Fake Java String"; 63 const jchar* env_getStringChars(JNIEnv*, jstring, jboolean* isCopy) { 64 if (isCopy) 65 *isCopy = false; 66 return (const jchar*)s_fakeString; 67 } 68 jsize env_getStringLength(JNIEnv*, jstring) { 69 return sizeof(s_fakeString) - 1; 70 } 71 jbyteArray env_newByteArray(JNIEnv*, jsize) { 72 return (jbyteArray) 1; 73 } 74 jobject env_newRef(JNIEnv*, jobject obj) { 75 return obj; 76 } 77 jobject env_newObjectV(JNIEnv*, jclass, jmethodID, va_list) { 78 return MY_JOBJECT; 79 } 80 jstring env_newString(JNIEnv*, const jchar*, jsize) { 81 return (jstring) 1; 82 } 83 void env_releaseByteArrayElements(JNIEnv*, jbyteArray, jbyte*, jint) {} 84 void env_releaseStringChars(JNIEnv*, jstring, const jchar*) {} 85 void env_setByteArrayRegion(JNIEnv*, jbyteArray, jsize, jsize, const jbyte*) {} 86 void env_setIntField(JNIEnv*, jobject, jfieldID, jint) {} 87 88 void InitializeJavaVM() { 89 // First, create the fake vm 90 s_jvm = new JavaVM; 91 JNIInvokeInterface* i = new JNIInvokeInterface; 92 memset(i, 0, sizeof(JNIInvokeInterface)); 93 s_jvm->functions = i; 94 95 // Now, assign the functions of the vm to our fake ones. 96 i->AttachCurrentThread = vm_attachCurrentThread; 97 98 // Create the fake env next 99 s_env = new JNIEnv; 100 JNINativeInterface* n = new JNINativeInterface; 101 memset(n, 0, sizeof(JNINativeInterface)); 102 s_env->functions = n; 103 104 // Point the functions we care about to out fake ones. 105 n->CallObjectMethodV = env_callObjectMethodV; 106 n->CallVoidMethodV = env_callVoidMethodV; 107 n->DeleteLocalRef = env_deleteRef; 108 n->DeleteGlobalRef = env_deleteRef; 109 n->DeleteWeakGlobalRef = env_deleteRef; 110 n->ExceptionCheck = env_exceptionCheck; 111 n->FindClass = env_findClass; 112 n->GetByteArrayElements = env_getByteArrayElements; 113 n->GetMethodID = env_getMethodID; 114 n->GetObjectClass = env_getObjectClass; 115 n->GetStringChars = env_getStringChars; 116 n->GetStringLength = env_getStringLength; 117 n->NewByteArray = env_newByteArray; 118 n->NewLocalRef = env_newRef; 119 n->NewGlobalRef = env_newRef; 120 n->NewWeakGlobalRef = env_newRef; 121 n->NewObjectV = env_newObjectV; 122 n->NewString = env_newString; 123 n->ReleaseByteArrayElements = env_releaseByteArrayElements; 124 n->ReleaseStringChars = env_releaseStringChars; 125 n->SetByteArrayRegion = env_setByteArrayRegion; 126 n->SetIntField = env_setIntField; 127 128 // Tell WebCore about the vm 129 JSC::Bindings::setJavaVM(s_jvm); 130 } 131