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
28 #include "MyJavaVM.h"
29 #include "jni_utility.h"
30
31 #include <jni.h>
32
33 static JNIEnv* s_env;
34 static JavaVM* s_jvm;
35
36 // JavaVM functions
vm_attachCurrentThread(JavaVM *,JNIEnv ** env,void *)37 jint vm_attachCurrentThread(JavaVM*, JNIEnv** env, void*) {
38 *env = s_env;
39 return JNI_OK;
40 }
41
42 // JNIEnv functions
env_callObjectMethodV(JNIEnv *,jobject,jmethodID,va_list)43 jobject env_callObjectMethodV(JNIEnv*, jobject, jmethodID, va_list) {
44 return MY_JOBJECT;
45 }
env_callVoidMethodV(JNIEnv *,jobject,jmethodID,va_list)46 void env_callVoidMethodV(JNIEnv*, jobject, jmethodID, va_list) {}
env_deleteRef(JNIEnv *,jobject)47 void env_deleteRef(JNIEnv*, jobject) {}
env_exceptionCheck(JNIEnv *)48 jboolean env_exceptionCheck(JNIEnv*) {
49 return false;
50 }
env_findClass(JNIEnv *,const char *)51 jclass env_findClass(JNIEnv*, const char*) {
52 return (jclass) 1;
53 }
env_getByteArrayElements(JNIEnv *,jbyteArray,jboolean *)54 jbyte* env_getByteArrayElements(JNIEnv*, jbyteArray, jboolean*) {
55 return NULL;
56 }
env_getMethodID(JNIEnv *,jclass,const char *,const char *)57 jmethodID env_getMethodID(JNIEnv*, jclass, const char*, const char*) {
58 return (jmethodID) 1;
59 }
env_getObjectClass(JNIEnv *,jobject)60 jclass env_getObjectClass(JNIEnv*, jobject) {
61 return (jclass) 1;
62 }
63 static const char* s_fakeString = "Fake Java String";
env_getStringChars(JNIEnv *,jstring,jboolean * isCopy)64 const jchar* env_getStringChars(JNIEnv*, jstring, jboolean* isCopy) {
65 if (isCopy)
66 *isCopy = false;
67 return (const jchar*)s_fakeString;
68 }
env_getStringLength(JNIEnv *,jstring)69 jsize env_getStringLength(JNIEnv*, jstring) {
70 return sizeof(s_fakeString) - 1;
71 }
env_newByteArray(JNIEnv *,jsize)72 jbyteArray env_newByteArray(JNIEnv*, jsize) {
73 return (jbyteArray) 1;
74 }
env_newRef(JNIEnv *,jobject obj)75 jobject env_newRef(JNIEnv*, jobject obj) {
76 return obj;
77 }
env_newObjectV(JNIEnv *,jclass,jmethodID,va_list)78 jobject env_newObjectV(JNIEnv*, jclass, jmethodID, va_list) {
79 return MY_JOBJECT;
80 }
env_newString(JNIEnv *,const jchar *,jsize)81 jstring env_newString(JNIEnv*, const jchar*, jsize) {
82 return (jstring) 1;
83 }
env_releaseByteArrayElements(JNIEnv *,jbyteArray,jbyte *,jint)84 void env_releaseByteArrayElements(JNIEnv*, jbyteArray, jbyte*, jint) {}
env_releaseStringChars(JNIEnv *,jstring,const jchar *)85 void env_releaseStringChars(JNIEnv*, jstring, const jchar*) {}
env_setByteArrayRegion(JNIEnv *,jbyteArray,jsize,jsize,const jbyte *)86 void env_setByteArrayRegion(JNIEnv*, jbyteArray, jsize, jsize, const jbyte*) {}
env_setIntField(JNIEnv *,jobject,jfieldID,jint)87 void env_setIntField(JNIEnv*, jobject, jfieldID, jint) {}
88
InitializeJavaVM()89 void InitializeJavaVM() {
90 // First, create the fake vm
91 s_jvm = new JavaVM;
92 JNIInvokeInterface* i = new JNIInvokeInterface;
93 memset(i, 0, sizeof(JNIInvokeInterface));
94 s_jvm->functions = i;
95
96 // Now, assign the functions of the vm to our fake ones.
97 i->AttachCurrentThread = vm_attachCurrentThread;
98
99 // Create the fake env next
100 s_env = new JNIEnv;
101 JNINativeInterface* n = new JNINativeInterface;
102 memset(n, 0, sizeof(JNINativeInterface));
103 s_env->functions = n;
104
105 // Point the functions we care about to out fake ones.
106 n->CallObjectMethodV = env_callObjectMethodV;
107 n->CallVoidMethodV = env_callVoidMethodV;
108 n->DeleteLocalRef = env_deleteRef;
109 n->DeleteGlobalRef = 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->NewGlobalRef = env_newRef;
119 n->NewObjectV = env_newObjectV;
120 n->NewString = env_newString;
121 n->ReleaseByteArrayElements = env_releaseByteArrayElements;
122 n->ReleaseStringChars = env_releaseStringChars;
123 n->SetByteArrayRegion = env_setByteArrayRegion;
124 n->SetIntField = env_setIntField;
125
126 // Tell WebCore about the vm
127 JSC::Bindings::setJavaVM(s_jvm);
128 }
129