Home | History | Annotate | Download | only in jni
      1 /* //device/libs/android_runtime/BindTest.cpp
      2 **
      3 ** Copyright 2006, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <jam-public.h>
     21 
     22 static u4 offset_instanceString;
     23 static FieldBlock *fb_classString = NULL;
     24 static Class *class_ReturnedObject = NULL;
     25 static MethodBlock *mb_ReturnedObject_setReturnedString = NULL;
     26 static MethodBlock *mb_Java_Lang_Object_Equals = NULL;
     27 
     28 static u4 offset_mObj;
     29 static u4 offset_mBool;
     30 static u4 offset_mInt;
     31 static u4 offset_mString;
     32 static u4 offset_mDouble;
     33 static u4 offset_mLong;
     34 
     35 
     36 /* native String getString(); */
     37 static uintptr_t *
     38 getString(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
     39 {
     40     RETURN_OBJ (createString ("String"));
     41 }
     42 
     43 /* native String getNullString(); */
     44 static uintptr_t *
     45 getNullString(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
     46 {
     47     RETURN_OBJ (createString (NULL));
     48 }
     49 
     50 /* native String getBooleanTrue(); */
     51 static uintptr_t *
     52 getBooleanTrue(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
     53 {
     54     RETURN_BOOLEAN (TRUE);
     55 }
     56 
     57 /* native String getBooleanFalse(); */
     58 static uintptr_t *
     59 getBooleanFalse(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
     60 {
     61     RETURN_BOOLEAN (FALSE);
     62 }
     63 
     64 /* native Object nonvoidThrowsException() */
     65 static uintptr_t *
     66 nonvoidThrowsException (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
     67 {
     68     if (1) {
     69         signalException("java/lang/NullPointerException", NULL);
     70         goto exception;
     71     }
     72 
     73     RETURN_OBJ (NULL);
     74 exception:
     75     RETURN_VOID;
     76 }
     77 
     78 /* native void setInstanceString(String s); */
     79 static uintptr_t *
     80 setInstanceString (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
     81 {
     82     Object *jthis = (Object *) ostack[0];
     83 
     84     JOBJ_set_obj(jthis, offset_instanceString, ostack[1]);
     85 
     86     RETURN_VOID;
     87 }
     88 
     89 /* native void setClassString(String s) */
     90 static uintptr_t *
     91 setClassString (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
     92 {
     93 //    Object *jthis = (Object *) ostack[0];
     94 
     95     fb_classString->static_value = ostack[1];
     96 
     97     RETURN_VOID;
     98 }
     99 
    100 /* native String makeStringFromThreeChars(char a, char b, char c); */
    101 static uintptr_t *
    102 makeStringFromThreeChars (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
    103 {
    104     // Object *jthis =  ostack[0];
    105     char a = (char) ostack[1];
    106     char b = (char) ostack[2];
    107     char c = (char) ostack[3];
    108 
    109     char str[4];
    110 
    111     str[0] = a;
    112     str[1] = b;
    113     str[2] = c;
    114     str[3] = 0;
    115 
    116     RETURN_OBJ(createString(str));
    117 }
    118 
    119 /* native ReturnedObject makeReturnedObject(String a); */
    120 static uintptr_t *
    121 makeReturnedObject (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
    122 {
    123     //Object *jthis = (Object*)ostack[0];
    124     Object *a = (Object*)ostack[1];
    125 
    126     Object *ret;
    127 
    128     ret = allocObject(class_ReturnedObject);
    129 
    130     executeMethod(ret, mb_ReturnedObject_setReturnedString, a);
    131 
    132     RETURN_OBJ (ret);
    133 }
    134 
    135 /* native double addDoubles(double a, double b); */
    136 static uintptr_t *
    137 addDoubles (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
    138 {
    139     //Object *jthis = (Object*)ostack[0];
    140     double a = JARG_get_double(1);
    141     double b = JARG_get_double(3);
    142 
    143     RETURN_DOUBLE(a+b);
    144 }
    145 
    146 /* native void setAll (Object obj, boolean bool, int i, String str, double d, long l) */
    147 static uintptr_t *
    148 setAll  (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
    149 {
    150     Object *jthis = JARG_get_obj(0);
    151 
    152     Object *obj     = JARG_get_obj(1);
    153     bool b          = JARG_get_bool(2);
    154     int i           = JARG_get_int(3);
    155     char *str       = JARG_get_cstr_strdup(4);
    156     double d        = JARG_get_double(5);
    157     long long ll    = JARG_get_long_long(5+2);
    158 
    159     JOBJ_set_obj(jthis, offset_mObj, obj);
    160     JOBJ_set_bool(jthis, offset_mBool, b);
    161     JOBJ_set_int(jthis, offset_mInt, i);
    162     JOBJ_set_cstr(jthis, offset_mString, str);
    163     free(str);
    164     str = NULL;
    165     JOBJ_set_double(jthis, offset_mDouble, d);
    166     JOBJ_set_long_long(jthis, offset_mLong, ll);
    167 
    168     RETURN_VOID;
    169 }
    170 
    171 /* native void compareAll (Object obj, boolean bool, int i, String str, double d, long l) */
    172 static uintptr_t *
    173 compareAll  (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
    174 {
    175     Object *jthis = JARG_get_obj(0);
    176 
    177     Object *obj     = JARG_get_obj(1);
    178     bool b          = JARG_get_bool(2);
    179     int i           = JARG_get_int(3);
    180     Object *strObj  = JARG_get_obj(4);
    181     double d        = JARG_get_double(5);
    182     long long ll    = JARG_get_long_long(5+2);
    183 
    184     bool ret;
    185 
    186     void *result;
    187 
    188     Object *mStringObj = JOBJ_get_obj(jthis, offset_mString);
    189 
    190     char *s = JARG_get_cstr_strdup(4);
    191 
    192     result = executeMethod (strObj, lookupVirtualMethod(strObj,mb_Java_Lang_Object_Equals),
    193                 JOBJ_get_obj(jthis, offset_mString));
    194 
    195     if (exceptionOccurred()) {
    196         RETURN_VOID;
    197     }
    198 
    199     ret =   (*(uintptr_t *)result != 0)
    200             && (obj == JOBJ_get_obj(jthis, offset_mObj))
    201             && (b == JOBJ_get_bool(jthis, offset_mBool))
    202             && (i == JOBJ_get_int(jthis, offset_mInt))
    203             && (d == JOBJ_get_double(jthis, offset_mDouble))
    204             && (ll == JOBJ_get_long_long(jthis, offset_mLong));
    205 
    206 
    207     RETURN_BOOLEAN(ret);
    208 }
    209 
    210 static VMMethod methods[] = {
    211     {"getString", getString},
    212     {"getNullString", getNullString},
    213     {"getBooleanTrue", getBooleanTrue},
    214     {"getBooleanFalse", getBooleanFalse},
    215     {"nonvoidThrowsException", nonvoidThrowsException},
    216     {"setInstanceString",     setInstanceString},
    217     {"setClassString",     setClassString},
    218     {"makeStringFromThreeChars", makeStringFromThreeChars},
    219     {"makeReturnedObject", makeReturnedObject},
    220     {"addDoubles", addDoubles},
    221     {"setAll", setAll},
    222     {"compareAll", compareAll},
    223     {NULL, NULL}
    224 };
    225 
    226 
    227 void register_BindTest()
    228 {
    229     jamvm_registerClass("BindTest", methods);
    230 
    231     Class *clazz = NULL;
    232 
    233     clazz = findClassFromClassLoader("BindTest", getSystemClassLoader());
    234 
    235     if (clazz == NULL) {
    236         fprintf(stderr, "Error: BindTest not found\n");
    237 	clearException();
    238         return;
    239     }
    240 
    241     FieldBlock *fb;
    242 
    243     fb = findField(clazz, "instanceString", "Ljava/lang/String;");
    244 
    245     if (fb == NULL || ((fb->access_flags & ACC_STATIC) == ACC_STATIC)) {
    246         fprintf(stderr, "Error: BindTest.instanceString not found or error\n");
    247         return;
    248     }
    249 
    250     offset_instanceString = fb->offset;
    251 
    252     fb_classString = findField(clazz, "classString", "Ljava/lang/String;");
    253 
    254     if (fb_classString == NULL || ((fb_classString->access_flags & ACC_STATIC) != ACC_STATIC)) {
    255         fprintf(stderr, "Error: BindTest.classString not found or error\n");
    256         return;
    257     }
    258 
    259 
    260     class_ReturnedObject = findClassFromClassLoader("ReturnedObject", getSystemClassLoader());
    261 
    262     if (class_ReturnedObject == NULL) {
    263         fprintf(stderr, "Error: ReturnedObject class not found or error\n");
    264         return;
    265     }
    266 
    267     mb_ReturnedObject_setReturnedString=
    268            findMethod (class_ReturnedObject, "setReturnedString", "(Ljava/lang/String;)V");
    269 
    270     if (mb_ReturnedObject_setReturnedString == NULL) {
    271         fprintf(stderr, "Error: ReturnedObject.setReturnedString class not found or error\n");
    272         return;
    273     }
    274 
    275     offset_mObj = findField(clazz, "mObj", "Ljava/lang/Object;")->offset;
    276     offset_mBool = findField(clazz, "mBool", "Z" )->offset;
    277     offset_mInt = findField(clazz, "mInt", "I")->offset;
    278     offset_mString = findField(clazz, "mString", "Ljava/lang/String;")->offset;
    279     offset_mDouble = findField(clazz, "mDouble", "D")->offset;
    280     offset_mLong = findField(clazz, "mLong", "J")->offset;
    281 
    282 
    283     mb_Java_Lang_Object_Equals = findMethod (
    284                                     findClassFromClassLoader("java/lang/Object", getSystemClassLoader()),
    285                                     "equals", "(Ljava/lang/Object;)Z");
    286 
    287 }
    288 
    289 
    290