summaryrefslogtreecommitdiffstats
path: root/core/jni/BindTest.cpp
blob: 5fae4005bb448c8c63c21fa23c7fd40e903a8dbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* //device/libs/android_runtime/BindTest.cpp
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/

#include <stdio.h>
#include <stdlib.h>
#include <jam-public.h>

static u4 offset_instanceString;
static FieldBlock *fb_classString = NULL;
static Class *class_ReturnedObject = NULL;
static MethodBlock *mb_ReturnedObject_setReturnedString = NULL;
static MethodBlock *mb_Java_Lang_Object_Equals = NULL;

static u4 offset_mObj;
static u4 offset_mBool;
static u4 offset_mInt;
static u4 offset_mString;
static u4 offset_mDouble;
static u4 offset_mLong;


/* native String getString(); */
static uintptr_t *
getString(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    RETURN_OBJ (createString ("String"));
}

/* native String getNullString(); */
static uintptr_t *
getNullString(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    RETURN_OBJ (createString (NULL));
}

/* native String getBooleanTrue(); */
static uintptr_t *
getBooleanTrue(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    RETURN_BOOLEAN (TRUE);
}

/* native String getBooleanFalse(); */
static uintptr_t *
getBooleanFalse(Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    RETURN_BOOLEAN (FALSE);
}

/* native Object nonvoidThrowsException() */
static uintptr_t *
nonvoidThrowsException (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    if (1) {
        signalException("java/lang/NullPointerException", NULL);
        goto exception;
    } 
    
    RETURN_OBJ (NULL);
exception:
    RETURN_VOID;
}

/* native void setInstanceString(String s); */
static uintptr_t *
setInstanceString (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    Object *jthis = (Object *) ostack[0];
    
    JOBJ_set_obj(jthis, offset_instanceString, ostack[1]);

    RETURN_VOID;
}

/* native void setClassString(String s) */
static uintptr_t *
setClassString (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
//    Object *jthis = (Object *) ostack[0];

    fb_classString->static_value = ostack[1];

    RETURN_VOID;
}

/* native String makeStringFromThreeChars(char a, char b, char c); */
static uintptr_t *
makeStringFromThreeChars (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    // Object *jthis =  ostack[0];
    char a = (char) ostack[1];
    char b = (char) ostack[2];
    char c = (char) ostack[3];

    char str[4];

    str[0] = a;
    str[1] = b;
    str[2] = c;
    str[3] = 0;
    
    RETURN_OBJ(createString(str));
}

/* native ReturnedObject makeReturnedObject(String a); */
static uintptr_t *
makeReturnedObject (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    //Object *jthis = (Object*)ostack[0];
    Object *a = (Object*)ostack[1];

    Object *ret;

    ret = allocObject(class_ReturnedObject);

    executeMethod(ret, mb_ReturnedObject_setReturnedString, a);
    
    RETURN_OBJ (ret);
}

/* native double addDoubles(double a, double b); */
static uintptr_t *
addDoubles (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    //Object *jthis = (Object*)ostack[0];        
    double a = JARG_get_double(1);
    double b = JARG_get_double(3);

    RETURN_DOUBLE(a+b);
}

/* native void setAll (Object obj, boolean bool, int i, String str, double d, long l) */
static uintptr_t *
setAll  (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    Object *jthis = JARG_get_obj(0);

    Object *obj     = JARG_get_obj(1);
    bool b          = JARG_get_bool(2);
    int i           = JARG_get_int(3);
    char *str       = JARG_get_cstr_strdup(4);
    double d        = JARG_get_double(5);
    long long ll    = JARG_get_long_long(5+2);

    JOBJ_set_obj(jthis, offset_mObj, obj);
    JOBJ_set_bool(jthis, offset_mBool, b);
    JOBJ_set_int(jthis, offset_mInt, i);
    JOBJ_set_cstr(jthis, offset_mString, str);
    free(str);
    str = NULL;
    JOBJ_set_double(jthis, offset_mDouble, d);
    JOBJ_set_long_long(jthis, offset_mLong, ll);

    RETURN_VOID;
}

/* native void compareAll (Object obj, boolean bool, int i, String str, double d, long l) */
static uintptr_t *
compareAll  (Class *clazz, MethodBlock *mb, uintptr_t *ostack)
{
    Object *jthis = JARG_get_obj(0);

    Object *obj     = JARG_get_obj(1);
    bool b          = JARG_get_bool(2);
    int i           = JARG_get_int(3);
    Object *strObj  = JARG_get_obj(4);
    double d        = JARG_get_double(5);
    long long ll    = JARG_get_long_long(5+2);

    bool ret;

    void *result;

    Object *mStringObj = JOBJ_get_obj(jthis, offset_mString);

    char *s = JARG_get_cstr_strdup(4);
    
    result = executeMethod (strObj, lookupVirtualMethod(strObj,mb_Java_Lang_Object_Equals), 
                JOBJ_get_obj(jthis, offset_mString));

    if (exceptionOccurred()) {
        RETURN_VOID;
    }

    ret =   (*(uintptr_t *)result != 0)
            && (obj == JOBJ_get_obj(jthis, offset_mObj))
            && (b == JOBJ_get_bool(jthis, offset_mBool))
            && (i == JOBJ_get_int(jthis, offset_mInt))
            && (d == JOBJ_get_double(jthis, offset_mDouble))
            && (ll == JOBJ_get_long_long(jthis, offset_mLong));


    RETURN_BOOLEAN(ret);
}

static VMMethod methods[] = {
    {"getString", getString},
    {"getNullString", getNullString},
    {"getBooleanTrue", getBooleanTrue},
    {"getBooleanFalse", getBooleanFalse},
    {"nonvoidThrowsException", nonvoidThrowsException},
    {"setInstanceString",     setInstanceString},
    {"setClassString",     setClassString},
    {"makeStringFromThreeChars", makeStringFromThreeChars}, 
    {"makeReturnedObject", makeReturnedObject},
    {"addDoubles", addDoubles},
    {"setAll", setAll},
    {"compareAll", compareAll},
    {NULL, NULL}
};


void register_BindTest()
{
    jamvm_registerClass("BindTest", methods);

    Class *clazz = NULL;

    clazz = findClassFromClassLoader("BindTest", getSystemClassLoader());

    if (clazz == NULL) {
        fprintf(stderr, "Error: BindTest not found\n");
	clearException();
        return;
    }
    
    FieldBlock *fb;

    fb = findField(clazz, "instanceString", "Ljava/lang/String;");

    if (fb == NULL || ((fb->access_flags & ACC_STATIC) == ACC_STATIC)) {
        fprintf(stderr, "Error: BindTest.instanceString not found or error\n");        
        return;
    }  

    offset_instanceString = fb->offset;

    fb_classString = findField(clazz, "classString", "Ljava/lang/String;");

    if (fb_classString == NULL || ((fb_classString->access_flags & ACC_STATIC) != ACC_STATIC)) {
        fprintf(stderr, "Error: BindTest.classString not found or error\n");        
        return;
    }  


    class_ReturnedObject = findClassFromClassLoader("ReturnedObject", getSystemClassLoader());

    if (class_ReturnedObject == NULL) {
        fprintf(stderr, "Error: ReturnedObject class not found or error\n");        
        return;
    }
    
    mb_ReturnedObject_setReturnedString=
           findMethod (class_ReturnedObject, "setReturnedString", "(Ljava/lang/String;)V");

    if (mb_ReturnedObject_setReturnedString == NULL) {
        fprintf(stderr, "Error: ReturnedObject.setReturnedString class not found or error\n");        
        return;
    }

    offset_mObj = findField(clazz, "mObj", "Ljava/lang/Object;")->offset;
    offset_mBool = findField(clazz, "mBool", "Z" )->offset;
    offset_mInt = findField(clazz, "mInt", "I")->offset;
    offset_mString = findField(clazz, "mString", "Ljava/lang/String;")->offset;
    offset_mDouble = findField(clazz, "mDouble", "D")->offset;
    offset_mLong = findField(clazz, "mLong", "J")->offset;


    mb_Java_Lang_Object_Equals = findMethod (
                                    findClassFromClassLoader("java/lang/Object", getSystemClassLoader()),
                                    "equals", "(Ljava/lang/Object;)Z");

}