This repository was archived by the owner on Jul 12, 2023. It is now read-only.
forked from polyglot-compiler/JLang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjni_help.h
397 lines (357 loc) · 13 KB
/
jni_help.h
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright (C) 2018 Cornell University
#pragma once
#include "class.h"
#include "factory.h"
#include "helper.h"
#include "native.h"
#include "reflect.h"
#include "rep.h"
#include "stack_trace.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
typedef uint8_t u_char;
[[noreturn]] static void JniUnimplemented(const char *name) {
fprintf(stderr,
"- - - - - - - - - - - - - - - - - - - - - - - - - - -\n"
"The following JNI method is currently unimplemented:\n"
" %s\n"
"It is defined in " __FILE__ ".\n"
"Aborting for now.\n"
"- - - - - - - - - - - - - - - - - - - - - - - - - - -\n",
name);
DumpStackTrace();
abort();
}
// Begin helper methods.
static jstring CreateJavaString(const jchar *chars, jsize len) {
jcharArray charArray = CreateJavaCharArray(len);
JArrayRep *array = reinterpret_cast<JArrayRep *>(charArray);
void *data = array->Data();
memcpy(data, chars, sizeof(jchar) * len);
return CreateJavaString(charArray);
}
template <typename T> static T *GetJavaStaticFieldPtr(jfieldID id) {
auto f = reinterpret_cast<const JavaStaticFieldInfo *>(id);
auto ptr = f->ptr;
return reinterpret_cast<T *>(ptr);
}
// TODO check the type signature as well
template <typename T>
static void SetJavaStaticField(jclass clazz, jfieldID id, T obj) {
*GetJavaStaticFieldPtr<T>(id) = obj;
}
template <typename T> static T *GetJavaFieldPtr(jobject obj, jfieldID id) {
auto f = reinterpret_cast<const JavaFieldInfo *>(id);
auto raw = reinterpret_cast<char *>(obj) + f->offset;
return reinterpret_cast<T *>(raw);
}
template <typename T> static T GetJavaField(jobject obj, jfieldID id) {
return *GetJavaFieldPtr<T>(obj, id);
}
template <typename T>
static void SetJavaField(jobject obj, jfieldID id, T val) {
*GetJavaFieldPtr<T>(obj, id) = val;
}
template <typename T> static T *GetJavaArrayData(jarray arr, jboolean *isCopy) {
// Note: several JNI methods assume no copy.
if (isCopy != nullptr)
*isCopy = JNI_FALSE;
return reinterpret_cast<T *>(Unwrap(arr)->Data());
}
// Returns false if the given index is out-of-bounds.
static bool JavaArrayBoundsCheck(jarray arr, jsize index) {
jsize len = Unwrap(arr)->Length();
if (index < 0 || index >= len) {
// TODO: Should technically throw ArrayIndexOutOfBoundsException.
fprintf(stderr, "ERROR: Out-of-bounds array index %d in JNI method",
index);
abort();
return false;
}
return true;
}
// Similar to GetJavaArrayData, but performs a
// bounds check for the specified region.
template <typename T>
static T *GetJavaArrayDataForRegion(jarray arr, jsize start, jsize len) {
if (len == 0)
return nullptr;
if (!JavaArrayBoundsCheck(arr, start) ||
!JavaArrayBoundsCheck(arr, start + len - 1))
return nullptr;
// Assumes no copy.
return GetJavaArrayData<T>(arr, /*isCopy*/ nullptr) + start;
}
template <typename T>
static void GetJavaArrayRegion(jarray arr, jsize start, jsize len, T *buf) {
if (auto data = GetJavaArrayDataForRegion<T>(arr, start, len)) {
std::copy(data, data + len, buf);
}
}
template <typename T>
static void SetJavaArrayRegion(jarray arr, jsize start, jsize len,
const T *buf) {
if (auto data = GetJavaArrayDataForRegion<T>(arr, start, len)) {
std::copy(buf, buf + len, data);
}
}
// Parses one Java argument from a method signature and returns
// its single-character encoding. Updates the signature position.
static char ParseJavaArg(const char *&s) {
switch (*s) {
case 'Z':
case 'B':
case 'C':
case 'S':
case 'I':
case 'J':
case 'F':
case 'D':
return *s++;
case '[':
ParseJavaArg(++s);
return '[';
case 'L':
while (*s++ != ';')
continue;
return 'L';
default:
return '\0';
}
}
static size_t CountJavaArgs(const char *sig) {
const char *s = sig;
assert(*s == '(');
++s;
size_t count = 0;
while (*s != ')')
ParseJavaArg(s), ++count;
return count;
}
// Uses a Java method signature encoding to move
// a variable number of arguments into an array.
// Assumes the array is large enough.
static void ForwardJavaArgs(const char *sig, va_list args, jvalue *out) {
const char *s = sig;
jvalue *o = out;
assert(*s == '(');
++s;
static_assert(
sizeof(jint) == sizeof(int),
"We assume that jint is represented with int.\n"
"This is needed to handle vararg type promotions correctly.\n"
"See http://en.cppreference.com/w/cpp/language/variadic_arguments");
while (*s != ')') {
// Remember that va_arg can only be used on int, long, double,
// and pointers. We use static casts everywhere for visual consistency.
switch (ParseJavaArg(s)) {
case 'Z':
o->z = static_cast<jboolean>(va_arg(args, jint));
break;
case 'B':
o->b = static_cast<jbyte>(va_arg(args, jint));
break;
case 'C':
o->c = static_cast<jchar>(va_arg(args, jint));
break;
case 'S':
o->s = static_cast<jshort>(va_arg(args, jint));
break;
case 'I':
o->i = static_cast<jint>(va_arg(args, jint));
break;
case 'J':
o->j = static_cast<jlong>(va_arg(args, jlong));
break;
case 'F':
o->f = static_cast<jfloat>(va_arg(args, jdouble));
break;
case 'D':
o->d = static_cast<jdouble>(va_arg(args, jdouble));
break;
case '[':
o->l = static_cast<jarray>(va_arg(args, jarray));
break;
case 'L':
o->l = static_cast<jobject>(va_arg(args, jobject));
break;
default:
fprintf(stderr, "ERROR: Malformed method signature:\n\t%s\n", sig);
abort();
}
++o; // Advance out position.
}
}
// This type must precisely match the type used by JLang.
template <typename T> using JniTrampolineType = T (*)(void *, const jvalue *);
// Calls a Java method directly, without using a dispatch vector.
// All other method calling functions below delegate to this one.
template <typename T>
static T CallJavaNonvirtualMethod(jmethodID id, const jvalue *args) {
auto m = reinterpret_cast<const JavaMethodInfo *>(id);
if (!m->fnPtr || !m->trampoline) {
fprintf(stderr,
"ERROR: Attempting to call Java method with trampoline %p\n"
" and function pointer %p\n",
m->fnPtr, m->trampoline);
abort();
}
auto trampoline = reinterpret_cast<JniTrampolineType<T>>(m->trampoline);
return trampoline(m->fnPtr, args);
}
// Calls a Java instance method directly, without using a dispatch vector.
template <typename T>
static T CallJavaNonvirtualMethod(jobject obj, jmethodID id,
const jvalue *args) {
auto m = reinterpret_cast<const JavaMethodInfo *>(id);
auto num_args = CountJavaArgs(m->sig);
// Carefully include implicit receiver for non-static methods
if (!IS_STATIC_METHOD(m)) {
auto forward_args = std::vector<jvalue>(num_args + 1);
forward_args[0].l = obj;
std::copy(args, args + num_args, forward_args.begin() + 1);
return CallJavaNonvirtualMethod<T>(id, forward_args.data());
} else {
return CallJavaNonvirtualMethod<T>(id, args);
}
}
// Calls a Java instance method using the dispatch vector of [obj].
template <typename T>
static T CallJavaInstanceMethod(jobject obj, jmethodID id, const jvalue *args) {
auto m = reinterpret_cast<const JavaMethodInfo *>(id);
// We could implement virtual dispatch by using dispatch vector
// offsets. However, it's slightly simpler to just look up more
// precise method info from the class of the current object,
// which will include a direct function pointer. Then we
// can do a direct call.
auto clazz = Unwrap(obj)->Cdv()->Class()->Wrap();
m = GetJavaMethodInfo(clazz, m->name, m->sig).first;
if (m == NULL) {
return (T)NULL;
}
id = reinterpret_cast<jmethodID>(const_cast<JavaMethodInfo *>(m));
// TODO: The above lookup may fail due to type erasure,
// in particular if [obj] has a generic super class with substituted
// type parameter(s) which do not match the erasure type(s).
// See
// https://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html
return CallJavaNonvirtualMethod<T>(obj, id, args);
}
template <typename T>
static T CallJavaInstanceMethod(jobject obj, const char *name, const char *sig,
const jvalue *args) {
auto clazz = Unwrap(obj)->Cdv()->Class()->Wrap();
auto m = GetJavaMethodInfo(clazz, name, sig).first;
if (m == NULL) {
return (T)NULL;
}
auto id = reinterpret_cast<jmethodID>(const_cast<JavaMethodInfo *>(m));
return CallJavaNonvirtualMethod<T>(obj, id, args);
}
// Is exactly the same as a normal instance method, sugar for clarity
#define CallJavaConstructor(obj, id, args) \
CallJavaInstanceMethod<jobject>(obj, id, args)
// Calls a Java instance method using the dispatch vector of [obj].
template <typename T>
static T CallJavaInstanceMethod(jobject obj, jmethodID id, va_list args) {
auto m = reinterpret_cast<const JavaMethodInfo *>(id);
auto num_args = CountJavaArgs(m->sig);
auto forward_args = std::vector<jvalue>(num_args);
ForwardJavaArgs(m->sig, args, forward_args.data());
return CallJavaInstanceMethod<T>(obj, id, forward_args.data());
}
template <typename T>
static T CallJavaInterfaceMethod(jobject obj, jclass intf, const char *name,
const char *sig, const jvalue *args) {
auto methodInfoPair = GetJavaMethodInfo(intf, name, sig);
auto methodInfo = methodInfoPair.first;
if (methodInfo == NULL) {
return (T)NULL;
}
auto methodIndex = methodInfoPair.second;
void *methodToCall = __getInterfaceMethod(obj, methodInfo->intf_id_hash,
methodInfo->intf_id, methodIndex);
return ((T(*)(jobject))methodToCall)(obj);
}
// Calls a Java instance method using the dispatch vector of [obj].
template <typename T>
static T CallJavaStaticMethod(jclass cls, jmethodID id, va_list args) {
auto m = reinterpret_cast<const JavaMethodInfo *>(id);
auto num_args = CountJavaArgs(m->sig);
auto forward_args = std::vector<jvalue>(num_args);
ForwardJavaArgs(m->sig, args, forward_args.data());
return CallJavaNonvirtualMethod<T>(id, forward_args.data());
}
static jobjectArray GetJavaConstructors(jclass clazz, const JavaClassInfo *info,
jboolean publicOnly) {
// TODO pass public/private info to runtime, for now ignore the publicOnly
int ctor_count = 0;
for (int i = 0; i < info->num_methods; i++) {
if (IS_CONSTRUCTOR(&(info->methods[i]))) {
ctor_count++;
}
}
jobjectArray res = (jobjectArray)create1DArray(
"[Ljava.lang.reflect.Constructor;", ctor_count);
int ctors_copied = 0;
for (int i = 0; ctors_copied < ctor_count; i++) {
if (IS_CONSTRUCTOR(&(info->methods[i]))) {
POLYGLOT_ARRAY_STORE(
res, (jint)ctors_copied,
CreateConstructor(clazz, info, info->methods[i]));
ctors_copied++;
}
}
return res;
}
// Copied from JDK implementation, TODO replace with our own impl
// UTF8 helpers
// Writes a jchar a utf8 and returns the end
static u_char *utf8_write(u_char *base, jchar ch) {
if ((ch != 0) && (ch <= 0x7f)) {
base[0] = (u_char)ch;
return base + 1;
}
if (ch <= 0x7FF) {
/* 11 bits or less. */
unsigned char high_five = ch >> 6;
unsigned char low_six = ch & 0x3F;
base[0] = high_five | 0xC0; /* 110xxxxx */
base[1] = low_six | 0x80; /* 10xxxxxx */
return base + 2;
}
/* possibly full 16 bits. */
char high_four = ch >> 12;
char mid_six = (ch >> 6) & 0x3F;
char low_six = ch & 0x3f;
base[0] = high_four | 0xE0; /* 1110xxxx */
base[1] = mid_six | 0x80; /* 10xxxxxx */
base[2] = low_six | 0x80; /* 10xxxxxx */
return base + 3;
}
static int utf8_length(jchar *base, int length) {
int result = 0;
for (int index = 0; index < length; index++) {
jchar c = base[index];
if ((0x0001 <= c) && (c <= 0x007F))
result += 1;
else if (c <= 0x07FF)
result += 2;
else
result += 3;
}
return result;
}
static char *as_utf8(jchar *base, int length, u_char *result) {
int utf8_len = utf8_length(base, length);
u_char *p = result;
for (int index = 0; index < length; index++) {
p = utf8_write(p, base[index]);
}
*p = '\0';
assert(p == &result[utf8_len]);
return (char *)result;
}