]> git.ipfire.org Git - thirdparty/gcc.git/blame - libjava/jni.cc
Makefile.in (s-mlib): Only pass MULTIARCH_DIRNAME if MULTILIB_OSDIRNAMES is not defined.
[thirdparty/gcc.git] / libjava / jni.cc
CommitLineData
ee9dd372
TT
1// jni.cc - JNI implementation, including the jump table.
2
476924c9 3/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
d1ee8381 4 Free Software Foundation
ee9dd372
TT
5
6 This file is part of libgcj.
7
8This software is copyrighted work licensed under the terms of the
9Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10details. */
11
12#include <config.h>
13
c79d7702 14#include <stdio.h>
ee9dd372 15#include <stddef.h>
7e648cf9 16#include <string.h>
ee9dd372 17
8a922095
TT
18#include <gcj/cni.h>
19#include <jvm.h>
20#include <java-assert.h>
ee9dd372 21#include <jni.h>
54c2f04b
AG
22#ifdef ENABLE_JVMPI
23#include <jvmpi.h>
24#endif
86acf60c 25#ifdef INTERPRETER
94f473ee 26#include <jvmti.h>
befd7566 27#include "jvmti-int.h"
86acf60c 28#endif
355dff4c
TT
29#include <java/lang/Class.h>
30#include <java/lang/ClassLoader.h>
8a922095
TT
31#include <java/lang/Throwable.h>
32#include <java/lang/ArrayIndexOutOfBoundsException.h>
355dff4c 33#include <java/lang/StringIndexOutOfBoundsException.h>
d1ee8381 34#include <java/lang/StringBuffer.h>
6901a009 35#include <java/lang/UnsatisfiedLinkError.h>
8a922095
TT
36#include <java/lang/InstantiationException.h>
37#include <java/lang/NoSuchFieldError.h>
38#include <java/lang/NoSuchMethodError.h>
39#include <java/lang/reflect/Constructor.h>
355dff4c 40#include <java/lang/reflect/Method.h>
8a922095 41#include <java/lang/reflect/Modifier.h>
7e648cf9 42#include <java/lang/OutOfMemoryError.h>
7e648cf9 43#include <java/lang/Integer.h>
4547105f 44#include <java/lang/ThreadGroup.h>
c93d7fae 45#include <java/lang/Thread.h>
e976ed37 46#include <java/lang/IllegalAccessError.h>
d2ba8a75 47#include <java/nio/Buffer.h>
6f3aed57 48#include <java/nio/DirectByteBufferImpl.h>
7ef52736 49#include <java/nio/DirectByteBufferImpl$ReadWrite.h>
6f3aed57
MK
50#include <java/util/IdentityHashMap.h>
51#include <gnu/gcj/RawData.h>
8b6e7690 52#include <java/lang/ClassNotFoundException.h>
8a922095 53
355dff4c
TT
54#include <gcj/method.h>
55#include <gcj/field.h>
56
ee6713e7 57#include <java-interp.h>
c93d7fae 58#include <java-threads.h>
ee6713e7 59
107abb2f
BM
60using namespace gcj;
61
8a922095
TT
62// This enum is used to select different template instantiations in
63// the invocation code.
64enum invocation_type
65{
66 normal,
67 nonvirtual,
68 static_type,
69 constructor
70};
71
aaf0766e 72// Forward declarations.
f06a83c0
MK
73extern struct JNINativeInterface_ _Jv_JNIFunctions;
74extern struct JNIInvokeInterface_ _Jv_JNI_InvokeFunctions;
355dff4c 75
7e648cf9
TT
76// Number of slots in the default frame. The VM must allow at least
77// 16.
84973b27 78#define FRAME_SIZE 16
7e648cf9 79
8d00f617
TT
80// Mark value indicating this is an overflow frame.
81#define MARK_NONE 0
82// Mark value indicating this is a user frame.
83#define MARK_USER 1
84// Mark value indicating this is a system frame.
85#define MARK_SYSTEM 2
86
7e648cf9
TT
87// This structure is used to keep track of local references.
88struct _Jv_JNI_LocalFrame
89{
262fa8a4
TT
90 // This is one of the MARK_ constants.
91 unsigned char marker;
84973b27
GH
92
93 // Flag to indicate some locals were allocated.
262fa8a4 94 bool allocated_p;
7e648cf9
TT
95
96 // Number of elements in frame.
84973b27 97 int size;
7e648cf9 98
262fa8a4
TT
99 // The class loader of the JNI method that allocated this frame.
100 ::java::lang::ClassLoader *loader;
101
7e648cf9
TT
102 // Next frame in chain.
103 _Jv_JNI_LocalFrame *next;
104
105 // The elements. These are allocated using the C "struct hack".
106 jobject vec[0];
107};
108
39986dd5 109// This holds a reference count for all local references.
ac8d9921 110static java::util::IdentityHashMap *local_ref_table;
39986dd5 111// This holds a reference count for all global references.
ac8d9921 112static java::util::IdentityHashMap *global_ref_table;
7e648cf9 113
aaf0766e 114// The only VM.
fb5b5d97 115JavaVM *_Jv_the_vm;
aaf0766e 116
54c2f04b
AG
117#ifdef ENABLE_JVMPI
118// The only JVMPI interface description.
119static JVMPI_Interface _Jv_JVMPI_Interface;
120
121static jint
122jvmpiEnableEvent (jint event_type, void *)
123{
124 switch (event_type)
125 {
126 case JVMPI_EVENT_OBJECT_ALLOC:
127 _Jv_JVMPI_Notify_OBJECT_ALLOC = _Jv_JVMPI_Interface.NotifyEvent;
128 break;
63dc70a1 129
54c2f04b
AG
130 case JVMPI_EVENT_THREAD_START:
131 _Jv_JVMPI_Notify_THREAD_START = _Jv_JVMPI_Interface.NotifyEvent;
132 break;
63dc70a1 133
54c2f04b
AG
134 case JVMPI_EVENT_THREAD_END:
135 _Jv_JVMPI_Notify_THREAD_END = _Jv_JVMPI_Interface.NotifyEvent;
136 break;
63dc70a1 137
54c2f04b
AG
138 default:
139 return JVMPI_NOT_AVAILABLE;
140 }
63dc70a1 141
54c2f04b
AG
142 return JVMPI_SUCCESS;
143}
144
145static jint
146jvmpiDisableEvent (jint event_type, void *)
147{
148 switch (event_type)
149 {
150 case JVMPI_EVENT_OBJECT_ALLOC:
151 _Jv_JVMPI_Notify_OBJECT_ALLOC = NULL;
152 break;
63dc70a1 153
54c2f04b
AG
154 default:
155 return JVMPI_NOT_AVAILABLE;
156 }
63dc70a1 157
54c2f04b
AG
158 return JVMPI_SUCCESS;
159}
160#endif
161
8a922095
TT
162\f
163
7e648cf9
TT
164void
165_Jv_JNI_Init (void)
166{
ac8d9921
TT
167 local_ref_table = new java::util::IdentityHashMap;
168 global_ref_table = new java::util::IdentityHashMap;
39986dd5 169
54c2f04b
AG
170#ifdef ENABLE_JVMPI
171 _Jv_JVMPI_Interface.version = 1;
172 _Jv_JVMPI_Interface.EnableEvent = &jvmpiEnableEvent;
173 _Jv_JVMPI_Interface.DisableEvent = &jvmpiDisableEvent;
174 _Jv_JVMPI_Interface.EnableGC = &_Jv_EnableGC;
175 _Jv_JVMPI_Interface.DisableGC = &_Jv_DisableGC;
176 _Jv_JVMPI_Interface.RunGC = &_Jv_RunGC;
177#endif
7e648cf9
TT
178}
179
8a922095
TT
180// Tell the GC that a certain pointer is live.
181static void
ac8d9921 182mark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
8a922095 183{
7e648cf9
TT
184 JvSynchronize sync (ref_table);
185
186 using namespace java::lang;
187 Integer *refcount = (Integer *) ref_table->get (obj);
188 jint val = (refcount == NULL) ? 0 : refcount->intValue ();
b099f07d 189 // FIXME: what about out of memory error?
7e648cf9 190 ref_table->put (obj, new Integer (val + 1));
8a922095
TT
191}
192
193// Unmark a pointer.
194static void
ac8d9921 195unmark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
8a922095 196{
7e648cf9
TT
197 JvSynchronize sync (ref_table);
198
199 using namespace java::lang;
200 Integer *refcount = (Integer *) ref_table->get (obj);
201 JvAssert (refcount);
202 jint val = refcount->intValue () - 1;
39986dd5 203 JvAssert (val >= 0);
7e648cf9
TT
204 if (val == 0)
205 ref_table->remove (obj);
206 else
b099f07d 207 // FIXME: what about out of memory error?
7e648cf9
TT
208 ref_table->put (obj, new Integer (val));
209}
210
819138ac
TT
211// "Unwrap" some random non-reference type. This exists to simplify
212// other template functions.
213template<typename T>
214static T
215unwrap (T val)
216{
217 return val;
218}
219
220// Unwrap a weak reference, if required.
221template<typename T>
222static T *
223unwrap (T *obj)
224{
225 using namespace gnu::gcj::runtime;
226 // We can compare the class directly because JNIWeakRef is `final'.
227 // Doing it this way is much faster.
aee42017 228 if (obj == NULL || obj->getClass () != &JNIWeakRef::class$)
819138ac
TT
229 return obj;
230 JNIWeakRef *wr = reinterpret_cast<JNIWeakRef *> (obj);
231 return reinterpret_cast<T *> (wr->get ());
232}
233
3141ed0f
TT
234jobject
235_Jv_UnwrapJNIweakReference (jobject obj)
236{
237 return unwrap (obj);
238}
239
7e648cf9
TT
240\f
241
8319dc87
PB
242static jobject JNICALL
243_Jv_JNI_NewGlobalRef (JNIEnv *, jobject obj)
7e648cf9 244{
819138ac
TT
245 // This seems weird but I think it is correct.
246 obj = unwrap (obj);
39986dd5 247 mark_for_gc (obj, global_ref_table);
7e648cf9
TT
248 return obj;
249}
250
8319dc87
PB
251static void JNICALL
252_Jv_JNI_DeleteGlobalRef (JNIEnv *, jobject obj)
7e648cf9 253{
819138ac
TT
254 // This seems weird but I think it is correct.
255 obj = unwrap (obj);
10caa6ef
TT
256
257 // NULL is ok here -- the JNI specification doesn't say so, but this
258 // is a no-op.
259 if (! obj)
260 return;
261
39986dd5 262 unmark_for_gc (obj, global_ref_table);
7e648cf9
TT
263}
264
8319dc87
PB
265static void JNICALL
266_Jv_JNI_DeleteLocalRef (JNIEnv *env, jobject obj)
7e648cf9
TT
267{
268 _Jv_JNI_LocalFrame *frame;
269
819138ac
TT
270 // This seems weird but I think it is correct.
271 obj = unwrap (obj);
272
10caa6ef
TT
273 // NULL is ok here -- the JNI specification doesn't say so, but this
274 // is a no-op.
275 if (! obj)
276 return;
277
7e648cf9
TT
278 for (frame = env->locals; frame != NULL; frame = frame->next)
279 {
e62bad3d 280 for (int i = 0; i < frame->size; ++i)
7e648cf9
TT
281 {
282 if (frame->vec[i] == obj)
283 {
284 frame->vec[i] = NULL;
39986dd5 285 unmark_for_gc (obj, local_ref_table);
7e648cf9
TT
286 return;
287 }
288 }
289
290 // Don't go past a marked frame.
8d00f617 291 JvAssert (frame->marker == MARK_NONE);
7e648cf9
TT
292 }
293
294 JvAssert (0);
295}
296
8319dc87
PB
297static jint JNICALL
298_Jv_JNI_EnsureLocalCapacity (JNIEnv *env, jint size)
7e648cf9
TT
299{
300 // It is easier to just always allocate a new frame of the requested
301 // size. This isn't the most efficient thing, but for now we don't
302 // care. Note that _Jv_JNI_PushLocalFrame relies on this right now.
303
b099f07d
TT
304 _Jv_JNI_LocalFrame *frame;
305 try
7e648cf9 306 {
b099f07d
TT
307 frame = (_Jv_JNI_LocalFrame *) _Jv_Malloc (sizeof (_Jv_JNI_LocalFrame)
308 + size * sizeof (jobject));
309 }
310 catch (jthrowable t)
311 {
312 env->ex = t;
aaf0766e 313 return JNI_ERR;
7e648cf9
TT
314 }
315
8d00f617 316 frame->marker = MARK_NONE;
7e648cf9 317 frame->size = size;
262fa8a4 318 frame->allocated_p = false;
7e648cf9 319 memset (&frame->vec[0], 0, size * sizeof (jobject));
262fa8a4 320 frame->loader = env->locals->loader;
7e648cf9
TT
321 frame->next = env->locals;
322 env->locals = frame;
323
324 return 0;
325}
326
8319dc87
PB
327static jint JNICALL
328_Jv_JNI_PushLocalFrame (JNIEnv *env, jint size)
7e648cf9
TT
329{
330 jint r = _Jv_JNI_EnsureLocalCapacity (env, size);
331 if (r < 0)
332 return r;
333
334 // The new frame is on top.
8d00f617 335 env->locals->marker = MARK_USER;
7e648cf9
TT
336
337 return 0;
338}
339
8319dc87
PB
340static jobject JNICALL
341_Jv_JNI_NewLocalRef (JNIEnv *env, jobject obj)
7e648cf9 342{
819138ac
TT
343 // This seems weird but I think it is correct.
344 obj = unwrap (obj);
345
7e648cf9
TT
346 // Try to find an open slot somewhere in the topmost frame.
347 _Jv_JNI_LocalFrame *frame = env->locals;
348 bool done = false, set = false;
a5c30a8c 349 for (; frame != NULL && ! done; frame = frame->next)
7e648cf9
TT
350 {
351 for (int i = 0; i < frame->size; ++i)
a5c30a8c
TT
352 {
353 if (frame->vec[i] == NULL)
354 {
355 set = true;
356 done = true;
357 frame->vec[i] = obj;
262fa8a4 358 frame->allocated_p = true;
a5c30a8c
TT
359 break;
360 }
361 }
362
363 // If we found a slot, or if the frame we just searched is the
364 // mark frame, then we are done.
d3ae0d49 365 if (done || frame == NULL || frame->marker != MARK_NONE)
a5c30a8c 366 break;
7e648cf9
TT
367 }
368
369 if (! set)
370 {
371 // No slots, so we allocate a new frame. According to the spec
372 // we could just die here. FIXME: return value.
373 _Jv_JNI_EnsureLocalCapacity (env, 16);
374 // We know the first element of the new frame will be ok.
375 env->locals->vec[0] = obj;
262fa8a4 376 env->locals->allocated_p = true;
7e648cf9
TT
377 }
378
39986dd5 379 mark_for_gc (obj, local_ref_table);
7e648cf9
TT
380 return obj;
381}
382
8319dc87
PB
383static jobject JNICALL
384_Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result, int stop)
7e648cf9
TT
385{
386 _Jv_JNI_LocalFrame *rf = env->locals;
387
388 bool done = false;
389 while (rf != NULL && ! done)
63dc70a1 390 {
7e648cf9
TT
391 for (int i = 0; i < rf->size; ++i)
392 if (rf->vec[i] != NULL)
39986dd5 393 unmark_for_gc (rf->vec[i], local_ref_table);
7e648cf9
TT
394
395 // If the frame we just freed is the marker frame, we are done.
8d00f617 396 done = (rf->marker == stop);
7e648cf9
TT
397
398 _Jv_JNI_LocalFrame *n = rf->next;
84973b27
GH
399 // When N==NULL, we've reached the reusable bottom_locals, and we must
400 // not free it. However, we must be sure to clear all its elements.
7e648cf9 401 if (n == NULL)
ee7f72e4 402 {
84973b27
GH
403 if (rf->allocated_p)
404 memset (&rf->vec[0], 0, rf->size * sizeof (jobject));
262fa8a4 405 rf->allocated_p = false;
84973b27 406 rf = NULL;
ee7f72e4
TT
407 break;
408 }
409
410 _Jv_Free (rf);
7e648cf9
TT
411 rf = n;
412 }
413
80a44e08
TT
414 // Update the local frame information.
415 env->locals = rf;
416
7e648cf9 417 return result == NULL ? NULL : _Jv_JNI_NewLocalRef (env, result);
8a922095
TT
418}
419
8319dc87
PB
420static jobject JNICALL
421_Jv_JNI_PopLocalFrame (JNIEnv *env, jobject result)
8d00f617
TT
422{
423 return _Jv_JNI_PopLocalFrame (env, result, MARK_USER);
424}
425
e976ed37
AH
426// Make sure an array's type is compatible with the type of the
427// destination.
428template<typename T>
429static bool
430_Jv_JNI_check_types (JNIEnv *env, JArray<T> *array, jclass K)
431{
432 jclass klass = array->getClass()->getComponentType();
433 if (__builtin_expect (klass != K, false))
434 {
435 env->ex = new java::lang::IllegalAccessError ();
436 return false;
437 }
438 else
439 return true;
440}
441
8d00f617
TT
442// Pop a `system' frame from the stack. This is `extern "C"' as it is
443// used by the compiler.
444extern "C" void
445_Jv_JNI_PopSystemFrame (JNIEnv *env)
446{
84973b27
GH
447 // Only enter slow path when we're not at the bottom, or there have been
448 // allocations. Usually this is false and we can just null out the locals
449 // field.
4e2d1dbd 450
84973b27
GH
451 if (__builtin_expect ((env->locals->next
452 || env->locals->allocated_p), false))
453 _Jv_JNI_PopLocalFrame (env, NULL, MARK_SYSTEM);
454 else
455 env->locals = NULL;
86acf60c
DD
456
457#ifdef INTERPRETER
84973b27 458 if (__builtin_expect (env->ex != NULL, false))
6e84eab8
TT
459 {
460 jthrowable t = env->ex;
461 env->ex = NULL;
befd7566
KS
462 if (JVMTI_REQUESTED_EVENT (Exception))
463 _Jv_ReportJVMTIExceptionThrow (t);
6e84eab8
TT
464 throw t;
465 }
86acf60c 466#endif
8d00f617
TT
467}
468
4d6a988a
GH
469template<typename T> T extract_from_jvalue(jvalue const & t);
470template<> jboolean extract_from_jvalue(jvalue const & jv) { return jv.z; }
471template<> jbyte extract_from_jvalue(jvalue const & jv) { return jv.b; }
472template<> jchar extract_from_jvalue(jvalue const & jv) { return jv.c; }
473template<> jshort extract_from_jvalue(jvalue const & jv) { return jv.s; }
474template<> jint extract_from_jvalue(jvalue const & jv) { return jv.i; }
475template<> jlong extract_from_jvalue(jvalue const & jv) { return jv.j; }
476template<> jfloat extract_from_jvalue(jvalue const & jv) { return jv.f; }
477template<> jdouble extract_from_jvalue(jvalue const & jv) { return jv.d; }
478template<> jobject extract_from_jvalue(jvalue const & jv) { return jv.l; }
479
480
ee7f72e4
TT
481// This function is used from other template functions. It wraps the
482// return value appropriately; we specialize it so that object returns
483// are turned into local references.
484template<typename T>
485static T
486wrap_value (JNIEnv *, T value)
487{
488 return value;
489}
490
80a44e08
TT
491// This specialization is used for jobject, jclass, jstring, jarray,
492// etc.
4d6a988a 493template<typename R, typename T>
80a44e08
TT
494static T *
495wrap_value (JNIEnv *env, T *value)
56f2b5bd
MD
496{
497 return (value == NULL
498 ? value
80a44e08 499 : (T *) _Jv_JNI_NewLocalRef (env, (jobject) value));
56f2b5bd
MD
500}
501
8a922095
TT
502\f
503
8319dc87
PB
504static jint JNICALL
505_Jv_JNI_GetVersion (JNIEnv *)
8a922095 506{
880f8c16 507 return JNI_VERSION_1_4;
8a922095 508}
ee9dd372 509
8319dc87
PB
510static jclass JNICALL
511_Jv_JNI_DefineClass (JNIEnv *env, const char *name, jobject loader,
8e9031ec 512 const jbyte *buf, jsize bufLen)
355dff4c 513{
b099f07d
TT
514 try
515 {
819138ac
TT
516 loader = unwrap (loader);
517
35e058a2 518 jstring sname = JvNewStringUTF (name);
b099f07d 519 jbyteArray bytes = JvNewByteArray (bufLen);
355dff4c 520
b099f07d
TT
521 jbyte *elts = elements (bytes);
522 memcpy (elts, buf, bufLen * sizeof (jbyte));
355dff4c 523
b099f07d
TT
524 java::lang::ClassLoader *l
525 = reinterpret_cast<java::lang::ClassLoader *> (loader);
526
35e058a2 527 jclass result = l->defineClass (sname, bytes, 0, bufLen);
b099f07d
TT
528 return (jclass) wrap_value (env, result);
529 }
530 catch (jthrowable t)
531 {
532 env->ex = t;
533 return NULL;
534 }
355dff4c
TT
535}
536
8319dc87
PB
537static jclass JNICALL
538_Jv_JNI_FindClass (JNIEnv *env, const char *name)
355dff4c
TT
539{
540 // FIXME: assume that NAME isn't too long.
541 int len = strlen (name);
542 char s[len + 1];
543 for (int i = 0; i <= len; ++i)
544 s[i] = (name[i] == '/') ? '.' : name[i];
355dff4c 545
b099f07d
TT
546 jclass r = NULL;
547 try
355dff4c 548 {
b099f07d
TT
549 // This might throw an out of memory exception.
550 jstring n = JvNewStringUTF (s);
551
af98124e 552 java::lang::ClassLoader *loader = NULL;
262fa8a4
TT
553 if (env->locals->loader != NULL)
554 loader = env->locals->loader;
af98124e
TT
555
556 if (loader == NULL)
b099f07d
TT
557 {
558 // FIXME: should use getBaseClassLoader, but we don't have that
559 // yet.
560 loader = java::lang::ClassLoader::getSystemClassLoader ();
561 }
355dff4c 562
b099f07d 563 r = loader->loadClass (n);
7896beb2 564 _Jv_InitClass (r);
b099f07d
TT
565 }
566 catch (jthrowable t)
567 {
568 env->ex = t;
569 }
355dff4c 570
278abd28 571 return (jclass) wrap_value (env, r);
355dff4c
TT
572}
573
8319dc87
PB
574static jclass JNICALL
575_Jv_JNI_GetSuperclass (JNIEnv *env, jclass clazz)
ee9dd372 576{
819138ac 577 return (jclass) wrap_value (env, unwrap (clazz)->getSuperclass ());
ee9dd372
TT
578}
579
8319dc87
PB
580static jboolean JNICALL
581_Jv_JNI_IsAssignableFrom (JNIEnv *, jclass clazz1, jclass clazz2)
ee9dd372 582{
852993e3 583 return unwrap (clazz2)->isAssignableFrom (unwrap (clazz1));
ee9dd372
TT
584}
585
8319dc87
PB
586static jint JNICALL
587_Jv_JNI_Throw (JNIEnv *env, jthrowable obj)
8a922095 588{
278abd28 589 // We check in case the user did some funky cast.
819138ac 590 obj = unwrap (obj);
39986dd5 591 JvAssert (obj != NULL && java::lang::Throwable::class$.isInstance (obj));
355dff4c 592 env->ex = obj;
8a922095
TT
593 return 0;
594}
595
8319dc87
PB
596static jint JNICALL
597_Jv_JNI_ThrowNew (JNIEnv *env, jclass clazz, const char *message)
8a922095
TT
598{
599 using namespace java::lang::reflect;
600
819138ac 601 clazz = unwrap (clazz);
39986dd5 602 JvAssert (java::lang::Throwable::class$.isAssignableFrom (clazz));
278abd28 603
b099f07d
TT
604 int r = JNI_OK;
605 try
606 {
607 JArray<jclass> *argtypes
39986dd5
TT
608 = (JArray<jclass> *) JvNewObjectArray (1, &java::lang::Class::class$,
609 NULL);
8a922095 610
b099f07d 611 jclass *elts = elements (argtypes);
36739040 612 elts[0] = &java::lang::String::class$;
8a922095 613
b099f07d 614 Constructor *cons = clazz->getConstructor (argtypes);
8a922095 615
36739040
TT
616 jobjectArray values = JvNewObjectArray (1, &java::lang::String::class$,
617 NULL);
b099f07d
TT
618 jobject *velts = elements (values);
619 velts[0] = JvNewStringUTF (message);
8a922095 620
b099f07d 621 jobject obj = cons->newInstance (values);
8a922095 622
b099f07d
TT
623 env->ex = reinterpret_cast<jthrowable> (obj);
624 }
625 catch (jthrowable t)
626 {
627 env->ex = t;
628 r = JNI_ERR;
629 }
630
631 return r;
8a922095
TT
632}
633
8319dc87
PB
634static jthrowable JNICALL
635_Jv_JNI_ExceptionOccurred (JNIEnv *env)
8a922095 636{
278abd28 637 return (jthrowable) wrap_value (env, env->ex);
8a922095
TT
638}
639
8319dc87
PB
640static void JNICALL
641_Jv_JNI_ExceptionDescribe (JNIEnv *env)
8a922095 642{
355dff4c
TT
643 if (env->ex != NULL)
644 env->ex->printStackTrace();
8a922095
TT
645}
646
8319dc87
PB
647static void JNICALL
648_Jv_JNI_ExceptionClear (JNIEnv *env)
8a922095 649{
355dff4c
TT
650 env->ex = NULL;
651}
652
8319dc87
PB
653static jboolean JNICALL
654_Jv_JNI_ExceptionCheck (JNIEnv *env)
355dff4c
TT
655{
656 return env->ex != NULL;
8a922095
TT
657}
658
8319dc87
PB
659static void JNICALL
660_Jv_JNI_FatalError (JNIEnv *, const char *message)
8a922095
TT
661{
662 JvFail (message);
663}
664
7e648cf9
TT
665\f
666
8319dc87
PB
667static jboolean JNICALL
668_Jv_JNI_IsSameObject (JNIEnv *, jobject obj1, jobject obj2)
8a922095 669{
819138ac 670 return unwrap (obj1) == unwrap (obj2);
8a922095
TT
671}
672
8319dc87
PB
673static jobject JNICALL
674_Jv_JNI_AllocObject (JNIEnv *env, jclass clazz)
ee9dd372 675{
8a922095
TT
676 jobject obj = NULL;
677 using namespace java::lang::reflect;
278abd28 678
b099f07d
TT
679 try
680 {
819138ac 681 clazz = unwrap (clazz);
b099f07d
TT
682 JvAssert (clazz && ! clazz->isArray ());
683 if (clazz->isInterface() || Modifier::isAbstract(clazz->getModifiers()))
684 env->ex = new java::lang::InstantiationException ();
685 else
f5310108 686 obj = _Jv_AllocObject (clazz);
b099f07d
TT
687 }
688 catch (jthrowable t)
8a922095 689 {
b099f07d 690 env->ex = t;
8a922095
TT
691 }
692
278abd28 693 return wrap_value (env, obj);
ee9dd372
TT
694}
695
8319dc87
PB
696static jclass JNICALL
697_Jv_JNI_GetObjectClass (JNIEnv *env, jobject obj)
ee9dd372 698{
819138ac 699 obj = unwrap (obj);
278abd28
TT
700 JvAssert (obj);
701 return (jclass) wrap_value (env, obj->getClass());
ee9dd372
TT
702}
703
8319dc87
PB
704static jboolean JNICALL
705_Jv_JNI_IsInstanceOf (JNIEnv *, jobject obj, jclass clazz)
ee9dd372 706{
819138ac 707 return unwrap (clazz)->isInstance(unwrap (obj));
ee9dd372
TT
708}
709
8a922095
TT
710\f
711
712//
713// This section concerns method invocation.
714//
715
716template<jboolean is_static>
8319dc87
PB
717static jmethodID JNICALL
718_Jv_JNI_GetAnyMethodID (JNIEnv *env, jclass clazz,
8e9031ec 719 const char *name, const char *sig)
ee9dd372 720{
b099f07d
TT
721 try
722 {
819138ac 723 clazz = unwrap (clazz);
b099f07d 724 _Jv_InitClass (clazz);
8a922095 725
b099f07d 726 _Jv_Utf8Const *name_u = _Jv_makeUtf8Const ((char *) name, -1);
f86f42a8
TT
727
728 // FIXME: assume that SIG isn't too long.
729 int len = strlen (sig);
730 char s[len + 1];
731 for (int i = 0; i <= len; ++i)
732 s[i] = (sig[i] == '/') ? '.' : sig[i];
733 _Jv_Utf8Const *sig_u = _Jv_makeUtf8Const ((char *) s, -1);
8a922095 734
b099f07d 735 JvAssert (! clazz->isPrimitive());
8a922095 736
b099f07d 737 using namespace java::lang::reflect;
8a922095 738
b099f07d 739 while (clazz != NULL)
8a922095 740 {
b099f07d
TT
741 jint count = JvNumMethods (clazz);
742 jmethodID meth = JvGetFirstMethod (clazz);
743
744 for (jint i = 0; i < count; ++i)
745 {
746 if (((is_static && Modifier::isStatic (meth->accflags))
747 || (! is_static && ! Modifier::isStatic (meth->accflags)))
748 && _Jv_equalUtf8Consts (meth->name, name_u)
749 && _Jv_equalUtf8Consts (meth->signature, sig_u))
750 return meth;
751
752 meth = meth->getNextMethod();
753 }
8a922095 754
b099f07d 755 clazz = clazz->getSuperclass ();
8a922095
TT
756 }
757
d1ee8381
MW
758 java::lang::StringBuffer *name_sig =
759 new java::lang::StringBuffer (JvNewStringUTF (name));
666ff4f6
MW
760 name_sig->append ((jchar) ' ');
761 name_sig->append (JvNewStringUTF (s));
d1ee8381 762 env->ex = new java::lang::NoSuchMethodError (name_sig->toString ());
b099f07d
TT
763 }
764 catch (jthrowable t)
765 {
766 env->ex = t;
8a922095
TT
767 }
768
8a922095
TT
769 return NULL;
770}
771
772// This is a helper function which turns a va_list into an array of
773// `jvalue's. It needs signature information in order to do its work.
774// The array of values must already be allocated.
775static void
776array_from_valist (jvalue *values, JArray<jclass> *arg_types, va_list vargs)
777{
778 jclass *arg_elts = elements (arg_types);
779 for (int i = 0; i < arg_types->length; ++i)
780 {
b0af98d7
TT
781 // Here we assume that sizeof(int) >= sizeof(jint), because we
782 // use `int' when decoding the varargs. Likewise for
7694d69a
TT
783 // float, and double. Also we assume that sizeof(jlong) >=
784 // sizeof(int), i.e. that jlong values are not further
785 // promoted.
786 JvAssert (sizeof (int) >= sizeof (jint));
787 JvAssert (sizeof (jlong) >= sizeof (int));
788 JvAssert (sizeof (double) >= sizeof (jfloat));
789 JvAssert (sizeof (double) >= sizeof (jdouble));
8a922095 790 if (arg_elts[i] == JvPrimClass (byte))
63dc70a1 791 values[i].b = (jbyte) va_arg (vargs, int);
8a922095 792 else if (arg_elts[i] == JvPrimClass (short))
63dc70a1 793 values[i].s = (jshort) va_arg (vargs, int);
8a922095 794 else if (arg_elts[i] == JvPrimClass (int))
b0af98d7 795 values[i].i = (jint) va_arg (vargs, int);
8a922095 796 else if (arg_elts[i] == JvPrimClass (long))
7694d69a 797 values[i].j = (jlong) va_arg (vargs, jlong);
8a922095 798 else if (arg_elts[i] == JvPrimClass (float))
b0af98d7 799 values[i].f = (jfloat) va_arg (vargs, double);
8a922095 800 else if (arg_elts[i] == JvPrimClass (double))
b0af98d7 801 values[i].d = (jdouble) va_arg (vargs, double);
8a922095 802 else if (arg_elts[i] == JvPrimClass (boolean))
63dc70a1 803 values[i].z = (jboolean) va_arg (vargs, int);
8a922095 804 else if (arg_elts[i] == JvPrimClass (char))
63dc70a1 805 values[i].c = (jchar) va_arg (vargs, int);
8a922095
TT
806 else
807 {
808 // An object.
819138ac 809 values[i].l = unwrap (va_arg (vargs, jobject));
8a922095
TT
810 }
811 }
812}
813
814// This can call any sort of method: virtual, "nonvirtual", static, or
815// constructor.
816template<typename T, invocation_type style>
8319dc87
PB
817static T JNICALL
818_Jv_JNI_CallAnyMethodV (JNIEnv *env, jobject obj, jclass klass,
8e9031ec 819 jmethodID id, va_list vargs)
8a922095 820{
819138ac
TT
821 obj = unwrap (obj);
822 klass = unwrap (klass);
823
8a922095
TT
824 jclass decl_class = klass ? klass : obj->getClass ();
825 JvAssert (decl_class != NULL);
826
827 jclass return_type;
828 JArray<jclass> *arg_types;
8a922095 829
b099f07d
TT
830 try
831 {
832 _Jv_GetTypesFromSignature (id, decl_class,
833 &arg_types, &return_type);
834
835 jvalue args[arg_types->length];
836 array_from_valist (args, arg_types, vargs);
8a922095 837
b099f07d
TT
838 // For constructors we need to pass the Class we are instantiating.
839 if (style == constructor)
840 return_type = klass;
1adbc4d8 841
b099f07d 842 jvalue result;
0da021f5
TT
843 _Jv_CallAnyMethodA (obj, return_type, id,
844 style == constructor,
b9b5672b 845 style == normal,
0da021f5 846 arg_types, args, &result);
8a922095 847
4d6a988a 848 return wrap_value (env, extract_from_jvalue<T>(result));
b099f07d
TT
849 }
850 catch (jthrowable t)
851 {
852 env->ex = t;
853 }
854
855 return wrap_value (env, (T) 0);
8a922095
TT
856}
857
858template<typename T, invocation_type style>
8319dc87
PB
859static T JNICALL
860_Jv_JNI_CallAnyMethod (JNIEnv *env, jobject obj, jclass klass,
8e9031ec 861 jmethodID method, ...)
8a922095
TT
862{
863 va_list args;
864 T result;
865
866 va_start (args, method);
867 result = _Jv_JNI_CallAnyMethodV<T, style> (env, obj, klass, method, args);
868 va_end (args);
869
870 return result;
871}
872
873template<typename T, invocation_type style>
8319dc87
PB
874static T JNICALL
875_Jv_JNI_CallAnyMethodA (JNIEnv *env, jobject obj, jclass klass,
f06a83c0 876 jmethodID id, const jvalue *args)
8a922095 877{
819138ac
TT
878 obj = unwrap (obj);
879 klass = unwrap (klass);
880
8a922095
TT
881 jclass decl_class = klass ? klass : obj->getClass ();
882 JvAssert (decl_class != NULL);
883
884 jclass return_type;
885 JArray<jclass> *arg_types;
b099f07d
TT
886 try
887 {
888 _Jv_GetTypesFromSignature (id, decl_class,
889 &arg_types, &return_type);
8a922095 890
b099f07d
TT
891 // For constructors we need to pass the Class we are instantiating.
892 if (style == constructor)
893 return_type = klass;
1adbc4d8 894
819138ac
TT
895 // Unwrap arguments as required. Eww.
896 jclass *type_elts = elements (arg_types);
897 jvalue arg_copy[arg_types->length];
898 for (int i = 0; i < arg_types->length; ++i)
899 {
900 if (type_elts[i]->isPrimitive ())
901 arg_copy[i] = args[i];
902 else
903 arg_copy[i].l = unwrap (args[i].l);
904 }
905
b099f07d 906 jvalue result;
0da021f5
TT
907 _Jv_CallAnyMethodA (obj, return_type, id,
908 style == constructor,
b9b5672b 909 style == normal,
0da021f5 910 arg_types, arg_copy, &result);
8a922095 911
4d6a988a 912 return wrap_value (env, extract_from_jvalue<T>(result));
b099f07d
TT
913 }
914 catch (jthrowable t)
915 {
916 env->ex = t;
917 }
918
919 return wrap_value (env, (T) 0);
8a922095
TT
920}
921
922template<invocation_type style>
8319dc87
PB
923static void JNICALL
924_Jv_JNI_CallAnyVoidMethodV (JNIEnv *env, jobject obj, jclass klass,
8e9031ec 925 jmethodID id, va_list vargs)
8a922095 926{
819138ac
TT
927 obj = unwrap (obj);
928 klass = unwrap (klass);
929
8a922095
TT
930 jclass decl_class = klass ? klass : obj->getClass ();
931 JvAssert (decl_class != NULL);
932
933 jclass return_type;
934 JArray<jclass> *arg_types;
b099f07d
TT
935 try
936 {
937 _Jv_GetTypesFromSignature (id, decl_class,
938 &arg_types, &return_type);
8a922095 939
b099f07d
TT
940 jvalue args[arg_types->length];
941 array_from_valist (args, arg_types, vargs);
8a922095 942
b099f07d
TT
943 // For constructors we need to pass the Class we are instantiating.
944 if (style == constructor)
945 return_type = klass;
1adbc4d8 946
0da021f5
TT
947 _Jv_CallAnyMethodA (obj, return_type, id,
948 style == constructor,
b9b5672b 949 style == normal,
0da021f5 950 arg_types, args, NULL);
b099f07d
TT
951 }
952 catch (jthrowable t)
953 {
954 env->ex = t;
955 }
8a922095
TT
956}
957
958template<invocation_type style>
8319dc87
PB
959static void JNICALL
960_Jv_JNI_CallAnyVoidMethod (JNIEnv *env, jobject obj, jclass klass,
8e9031ec 961 jmethodID method, ...)
8a922095
TT
962{
963 va_list args;
964
965 va_start (args, method);
966 _Jv_JNI_CallAnyVoidMethodV<style> (env, obj, klass, method, args);
967 va_end (args);
968}
969
970template<invocation_type style>
8319dc87
PB
971static void JNICALL
972_Jv_JNI_CallAnyVoidMethodA (JNIEnv *env, jobject obj, jclass klass,
f06a83c0 973 jmethodID id, const jvalue *args)
8a922095 974{
8a922095
TT
975 jclass decl_class = klass ? klass : obj->getClass ();
976 JvAssert (decl_class != NULL);
977
978 jclass return_type;
979 JArray<jclass> *arg_types;
b099f07d
TT
980 try
981 {
982 _Jv_GetTypesFromSignature (id, decl_class,
983 &arg_types, &return_type);
8a922095 984
819138ac
TT
985 // Unwrap arguments as required. Eww.
986 jclass *type_elts = elements (arg_types);
987 jvalue arg_copy[arg_types->length];
988 for (int i = 0; i < arg_types->length; ++i)
989 {
990 if (type_elts[i]->isPrimitive ())
991 arg_copy[i] = args[i];
992 else
993 arg_copy[i].l = unwrap (args[i].l);
994 }
995
0da021f5
TT
996 _Jv_CallAnyMethodA (obj, return_type, id,
997 style == constructor,
b9b5672b 998 style == normal,
0da021f5 999 arg_types, args, NULL);
b099f07d
TT
1000 }
1001 catch (jthrowable t)
1002 {
1003 env->ex = t;
1004 }
8a922095
TT
1005}
1006
1007// Functions with this signature are used to implement functions in
1008// the CallMethod family.
1009template<typename T>
8319dc87
PB
1010static T JNICALL
1011_Jv_JNI_CallMethodV (JNIEnv *env, jobject obj,
8e9031ec 1012 jmethodID id, va_list args)
8a922095
TT
1013{
1014 return _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
1015}
1016
1017// Functions with this signature are used to implement functions in
1018// the CallMethod family.
1019template<typename T>
8319dc87
PB
1020static T JNICALL
1021_Jv_JNI_CallMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
8a922095
TT
1022{
1023 va_list args;
1024 T result;
1025
1026 va_start (args, id);
1027 result = _Jv_JNI_CallAnyMethodV<T, normal> (env, obj, NULL, id, args);
1028 va_end (args);
1029
1030 return result;
1031}
1032
1033// Functions with this signature are used to implement functions in
1034// the CallMethod family.
1035template<typename T>
8319dc87
PB
1036static T JNICALL
1037_Jv_JNI_CallMethodA (JNIEnv *env, jobject obj,
f06a83c0 1038 jmethodID id, const jvalue *args)
8a922095
TT
1039{
1040 return _Jv_JNI_CallAnyMethodA<T, normal> (env, obj, NULL, id, args);
1041}
1042
8319dc87
PB
1043static void JNICALL
1044_Jv_JNI_CallVoidMethodV (JNIEnv *env, jobject obj,
8e9031ec 1045 jmethodID id, va_list args)
8a922095
TT
1046{
1047 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1048}
1049
8319dc87
PB
1050static void JNICALL
1051_Jv_JNI_CallVoidMethod (JNIEnv *env, jobject obj, jmethodID id, ...)
8a922095
TT
1052{
1053 va_list args;
1054
1055 va_start (args, id);
1056 _Jv_JNI_CallAnyVoidMethodV<normal> (env, obj, NULL, id, args);
1057 va_end (args);
ee9dd372
TT
1058}
1059
8319dc87
PB
1060static void JNICALL
1061_Jv_JNI_CallVoidMethodA (JNIEnv *env, jobject obj,
f06a83c0 1062 jmethodID id, const jvalue *args)
ee9dd372 1063{
8a922095
TT
1064 _Jv_JNI_CallAnyVoidMethodA<normal> (env, obj, NULL, id, args);
1065}
1066
1067// Functions with this signature are used to implement functions in
1068// the CallStaticMethod family.
1069template<typename T>
8319dc87
PB
1070static T JNICALL
1071_Jv_JNI_CallStaticMethodV (JNIEnv *env, jclass klass,
8e9031ec 1072 jmethodID id, va_list args)
8a922095 1073{
5ef57049 1074 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
819138ac 1075 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
5ef57049 1076
8a922095
TT
1077 return _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass, id, args);
1078}
1079
1080// Functions with this signature are used to implement functions in
1081// the CallStaticMethod family.
1082template<typename T>
8319dc87
PB
1083static T JNICALL
1084_Jv_JNI_CallStaticMethod (JNIEnv *env, jclass klass,
8e9031ec 1085 jmethodID id, ...)
8a922095
TT
1086{
1087 va_list args;
1088 T result;
1089
5ef57049 1090 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
819138ac 1091 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
5ef57049 1092
8a922095
TT
1093 va_start (args, id);
1094 result = _Jv_JNI_CallAnyMethodV<T, static_type> (env, NULL, klass,
1095 id, args);
1096 va_end (args);
1097
1098 return result;
1099}
1100
1101// Functions with this signature are used to implement functions in
1102// the CallStaticMethod family.
1103template<typename T>
8319dc87
PB
1104static T JNICALL
1105_Jv_JNI_CallStaticMethodA (JNIEnv *env, jclass klass, jmethodID id,
f06a83c0 1106 const jvalue *args)
8a922095 1107{
5ef57049 1108 JvAssert (((id->accflags) & java::lang::reflect::Modifier::STATIC));
819138ac 1109 JvAssert (java::lang::Class::class$.isInstance (unwrap (klass)));
5ef57049 1110
8a922095
TT
1111 return _Jv_JNI_CallAnyMethodA<T, static_type> (env, NULL, klass, id, args);
1112}
1113
8319dc87
PB
1114static void JNICALL
1115_Jv_JNI_CallStaticVoidMethodV (JNIEnv *env, jclass klass,
8e9031ec 1116 jmethodID id, va_list args)
8a922095
TT
1117{
1118 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1119}
1120
8319dc87
PB
1121static void JNICALL
1122_Jv_JNI_CallStaticVoidMethod (JNIEnv *env, jclass klass,
8e9031ec 1123 jmethodID id, ...)
8a922095
TT
1124{
1125 va_list args;
1126
1127 va_start (args, id);
1128 _Jv_JNI_CallAnyVoidMethodV<static_type> (env, NULL, klass, id, args);
1129 va_end (args);
1130}
1131
8319dc87
PB
1132static void JNICALL
1133_Jv_JNI_CallStaticVoidMethodA (JNIEnv *env, jclass klass,
f06a83c0 1134 jmethodID id, const jvalue *args)
8a922095
TT
1135{
1136 _Jv_JNI_CallAnyVoidMethodA<static_type> (env, NULL, klass, id, args);
1137}
1138
8319dc87
PB
1139static jobject JNICALL
1140_Jv_JNI_NewObjectV (JNIEnv *env, jclass klass,
8e9031ec 1141 jmethodID id, va_list args)
8a922095 1142{
278abd28 1143 JvAssert (klass && ! klass->isArray ());
a61d70b8
JH
1144 JvAssert (! strcmp (id->name->chars(), "<init>")
1145 && id->signature->len() > 2
1146 && id->signature->chars()[0] == '('
1147 && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
1adbc4d8
TT
1148 ")V"));
1149
8a922095
TT
1150 return _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1151 id, args);
1152}
1153
8319dc87
PB
1154static jobject JNICALL
1155_Jv_JNI_NewObject (JNIEnv *env, jclass klass, jmethodID id, ...)
8a922095 1156{
278abd28 1157 JvAssert (klass && ! klass->isArray ());
a61d70b8
JH
1158 JvAssert (! strcmp (id->name->chars(), "<init>")
1159 && id->signature->len() > 2
1160 && id->signature->chars()[0] == '('
1161 && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
1adbc4d8 1162 ")V"));
278abd28 1163
8a922095
TT
1164 va_list args;
1165 jobject result;
1166
1167 va_start (args, id);
1168 result = _Jv_JNI_CallAnyMethodV<jobject, constructor> (env, NULL, klass,
1169 id, args);
1170 va_end (args);
1171
1172 return result;
1173}
1174
8319dc87
PB
1175static jobject JNICALL
1176_Jv_JNI_NewObjectA (JNIEnv *env, jclass klass, jmethodID id,
f06a83c0 1177 const jvalue *args)
8a922095 1178{
278abd28 1179 JvAssert (klass && ! klass->isArray ());
a61d70b8
JH
1180 JvAssert (! strcmp (id->name->chars(), "<init>")
1181 && id->signature->len() > 2
1182 && id->signature->chars()[0] == '('
1183 && ! strcmp (&id->signature->chars()[id->signature->len() - 2],
1adbc4d8
TT
1184 ")V"));
1185
8a922095
TT
1186 return _Jv_JNI_CallAnyMethodA<jobject, constructor> (env, NULL, klass,
1187 id, args);
1188}
1189
1190\f
1191
1192template<typename T>
8319dc87
PB
1193static T JNICALL
1194_Jv_JNI_GetField (JNIEnv *env, jobject obj, jfieldID field)
8a922095 1195{
819138ac 1196 obj = unwrap (obj);
278abd28 1197 JvAssert (obj);
8a922095 1198 T *ptr = (T *) ((char *) obj + field->getOffset ());
ee7f72e4 1199 return wrap_value (env, *ptr);
5dc489c1
TT
1200}
1201
8a922095 1202template<typename T>
8319dc87
PB
1203static void JNICALL
1204_Jv_JNI_SetField (JNIEnv *, jobject obj, jfieldID field, T value)
8a922095 1205{
819138ac
TT
1206 obj = unwrap (obj);
1207 value = unwrap (value);
1208
278abd28 1209 JvAssert (obj);
8a922095
TT
1210 T *ptr = (T *) ((char *) obj + field->getOffset ());
1211 *ptr = value;
1212}
1213
1214template<jboolean is_static>
8319dc87
PB
1215static jfieldID JNICALL
1216_Jv_JNI_GetAnyFieldID (JNIEnv *env, jclass clazz,
8e9031ec 1217 const char *name, const char *sig)
8a922095 1218{
b099f07d
TT
1219 try
1220 {
819138ac
TT
1221 clazz = unwrap (clazz);
1222
b099f07d 1223 _Jv_InitClass (clazz);
8a922095 1224
b099f07d 1225 _Jv_Utf8Const *a_name = _Jv_makeUtf8Const ((char *) name, -1);
8a922095 1226
f86f42a8
TT
1227 // FIXME: assume that SIG isn't too long.
1228 int len = strlen (sig);
1229 char s[len + 1];
1230 for (int i = 0; i <= len; ++i)
1231 s[i] = (sig[i] == '/') ? '.' : sig[i];
88b886f5
AH
1232 java::lang::ClassLoader *loader = clazz->getClassLoaderInternal ();
1233 jclass field_class = _Jv_FindClassFromSignature ((char *) s, loader);
8b6e7690
TT
1234 if (! field_class)
1235 throw new java::lang::ClassNotFoundException(JvNewStringUTF(s));
8a922095 1236
b099f07d 1237 while (clazz != NULL)
8a922095 1238 {
83c64db6
TT
1239 // We acquire the class lock so that fields aren't resolved
1240 // while we are running.
1241 JvSynchronize sync (clazz);
1242
b099f07d
TT
1243 jint count = (is_static
1244 ? JvNumStaticFields (clazz)
1245 : JvNumInstanceFields (clazz));
1246 jfieldID field = (is_static
1247 ? JvGetFirstStaticField (clazz)
1248 : JvGetFirstInstanceField (clazz));
1249 for (jint i = 0; i < count; ++i)
1250 {
b099f07d 1251 _Jv_Utf8Const *f_name = field->getNameUtf8Const(clazz);
8a922095 1252
83c64db6
TT
1253 // The field might be resolved or it might not be. It
1254 // is much simpler to always resolve it.
36739040 1255 _Jv_Linker::resolve_field (field, loader);
b099f07d
TT
1256 if (_Jv_equalUtf8Consts (f_name, a_name)
1257 && field->getClass() == field_class)
1258 return field;
8a922095 1259
b099f07d
TT
1260 field = field->getNextField ();
1261 }
1262
1263 clazz = clazz->getSuperclass ();
8a922095
TT
1264 }
1265
b099f07d
TT
1266 env->ex = new java::lang::NoSuchFieldError ();
1267 }
1268 catch (jthrowable t)
1269 {
1270 env->ex = t;
8a922095 1271 }
8a922095
TT
1272 return NULL;
1273}
1274
1275template<typename T>
8319dc87
PB
1276static T JNICALL
1277_Jv_JNI_GetStaticField (JNIEnv *env, jclass, jfieldID field)
8a922095
TT
1278{
1279 T *ptr = (T *) field->u.addr;
ee7f72e4 1280 return wrap_value (env, *ptr);
5dc489c1
TT
1281}
1282
8a922095 1283template<typename T>
8319dc87
PB
1284static void JNICALL
1285_Jv_JNI_SetStaticField (JNIEnv *, jclass, jfieldID field, T value)
8a922095 1286{
819138ac 1287 value = unwrap (value);
8a922095
TT
1288 T *ptr = (T *) field->u.addr;
1289 *ptr = value;
1290}
1291
8319dc87
PB
1292static jstring JNICALL
1293_Jv_JNI_NewString (JNIEnv *env, const jchar *unichars, jsize len)
8a922095 1294{
b099f07d
TT
1295 try
1296 {
1297 jstring r = _Jv_NewString (unichars, len);
1298 return (jstring) wrap_value (env, r);
1299 }
1300 catch (jthrowable t)
1301 {
1302 env->ex = t;
1303 return NULL;
1304 }
ee9dd372
TT
1305}
1306
8319dc87
PB
1307static jsize JNICALL
1308_Jv_JNI_GetStringLength (JNIEnv *, jstring string)
ee9dd372 1309{
819138ac 1310 return unwrap (string)->length();
ee9dd372
TT
1311}
1312
8319dc87
PB
1313static const jchar * JNICALL
1314_Jv_JNI_GetStringChars (JNIEnv *, jstring string, jboolean *isCopy)
8a922095 1315{
819138ac 1316 string = unwrap (string);
8a922095 1317 jchar *result = _Jv_GetStringChars (string);
39986dd5 1318 mark_for_gc (string, global_ref_table);
8a922095
TT
1319 if (isCopy)
1320 *isCopy = false;
1321 return (const jchar *) result;
1322}
1323
8319dc87
PB
1324static void JNICALL
1325_Jv_JNI_ReleaseStringChars (JNIEnv *, jstring string, const jchar *)
8a922095 1326{
819138ac 1327 unmark_for_gc (unwrap (string), global_ref_table);
8a922095
TT
1328}
1329
8319dc87
PB
1330static jstring JNICALL
1331_Jv_JNI_NewStringUTF (JNIEnv *env, const char *bytes)
8a922095 1332{
b099f07d
TT
1333 try
1334 {
476924c9
TT
1335 // For compatibility with the JDK.
1336 if (!bytes)
1337 return NULL;
b099f07d
TT
1338 jstring result = JvNewStringUTF (bytes);
1339 return (jstring) wrap_value (env, result);
1340 }
1341 catch (jthrowable t)
1342 {
1343 env->ex = t;
1344 return NULL;
1345 }
8a922095
TT
1346}
1347
8319dc87
PB
1348static jsize JNICALL
1349_Jv_JNI_GetStringUTFLength (JNIEnv *, jstring string)
8a922095 1350{
819138ac 1351 return JvGetStringUTFLength (unwrap (string));
8a922095
TT
1352}
1353
8319dc87
PB
1354static const char * JNICALL
1355_Jv_JNI_GetStringUTFChars (JNIEnv *env, jstring string,
8e9031ec 1356 jboolean *isCopy)
8a922095 1357{
b099f07d
TT
1358 try
1359 {
6c363de7
TT
1360 string = unwrap (string);
1361 if (string == NULL)
1362 return NULL;
1363 jsize len = JvGetStringUTFLength (string);
b099f07d 1364 char *r = (char *) _Jv_Malloc (len + 1);
b86ca9a2 1365 JvGetStringUTFRegion (string, 0, string->length(), r);
b099f07d 1366 r[len] = '\0';
8a922095 1367
b099f07d
TT
1368 if (isCopy)
1369 *isCopy = true;
8a922095 1370
b099f07d
TT
1371 return (const char *) r;
1372 }
1373 catch (jthrowable t)
1374 {
1375 env->ex = t;
1376 return NULL;
1377 }
8a922095
TT
1378}
1379
8319dc87
PB
1380static void JNICALL
1381_Jv_JNI_ReleaseStringUTFChars (JNIEnv *, jstring, const char *utf)
8a922095
TT
1382{
1383 _Jv_Free ((void *) utf);
1384}
1385
8319dc87
PB
1386static void JNICALL
1387_Jv_JNI_GetStringRegion (JNIEnv *env, jstring string, jsize start,
8e9031ec 1388 jsize len, jchar *buf)
355dff4c 1389{
819138ac 1390 string = unwrap (string);
355dff4c
TT
1391 jchar *result = _Jv_GetStringChars (string);
1392 if (start < 0 || start > string->length ()
1393 || len < 0 || start + len > string->length ())
b099f07d
TT
1394 {
1395 try
1396 {
1397 env->ex = new java::lang::StringIndexOutOfBoundsException ();
1398 }
1399 catch (jthrowable t)
1400 {
1401 env->ex = t;
1402 }
1403 }
355dff4c
TT
1404 else
1405 memcpy (buf, &result[start], len * sizeof (jchar));
1406}
1407
8319dc87
PB
1408static void JNICALL
1409_Jv_JNI_GetStringUTFRegion (JNIEnv *env, jstring str, jsize start,
8e9031ec 1410 jsize len, char *buf)
355dff4c 1411{
819138ac
TT
1412 str = unwrap (str);
1413
355dff4c
TT
1414 if (start < 0 || start > str->length ()
1415 || len < 0 || start + len > str->length ())
b099f07d
TT
1416 {
1417 try
1418 {
1419 env->ex = new java::lang::StringIndexOutOfBoundsException ();
1420 }
1421 catch (jthrowable t)
1422 {
1423 env->ex = t;
1424 }
1425 }
355dff4c
TT
1426 else
1427 _Jv_GetStringUTFRegion (str, start, len, buf);
1428}
1429
8319dc87
PB
1430static const jchar * JNICALL
1431_Jv_JNI_GetStringCritical (JNIEnv *, jstring str, jboolean *isCopy)
355dff4c 1432{
819138ac 1433 jchar *result = _Jv_GetStringChars (unwrap (str));
355dff4c
TT
1434 if (isCopy)
1435 *isCopy = false;
1436 return result;
1437}
1438
8319dc87
PB
1439static void JNICALL
1440_Jv_JNI_ReleaseStringCritical (JNIEnv *, jstring, const jchar *)
355dff4c
TT
1441{
1442 // Nothing.
1443}
1444
8319dc87
PB
1445static jsize JNICALL
1446_Jv_JNI_GetArrayLength (JNIEnv *, jarray array)
8a922095 1447{
819138ac 1448 return unwrap (array)->length;
8a922095
TT
1449}
1450
cb0be2e7 1451static jobjectArray JNICALL
8319dc87 1452_Jv_JNI_NewObjectArray (JNIEnv *env, jsize length,
8e9031ec 1453 jclass elementClass, jobject init)
8a922095 1454{
b099f07d
TT
1455 try
1456 {
819138ac
TT
1457 elementClass = unwrap (elementClass);
1458 init = unwrap (init);
1459
9bc825c4 1460 _Jv_CheckCast (elementClass, init);
b099f07d 1461 jarray result = JvNewObjectArray (length, elementClass, init);
cb0be2e7 1462 return (jobjectArray) wrap_value (env, result);
b099f07d
TT
1463 }
1464 catch (jthrowable t)
1465 {
1466 env->ex = t;
1467 return NULL;
1468 }
8a922095
TT
1469}
1470
8319dc87
PB
1471static jobject JNICALL
1472_Jv_JNI_GetObjectArrayElement (JNIEnv *env, jobjectArray array,
8e9031ec 1473 jsize index)
8a922095 1474{
9bc825c4
TT
1475 if ((unsigned) index >= (unsigned) array->length)
1476 _Jv_ThrowBadArrayIndex (index);
819138ac 1477 jobject *elts = elements (unwrap (array));
278abd28 1478 return wrap_value (env, elts[index]);
8a922095
TT
1479}
1480
8319dc87
PB
1481static void JNICALL
1482_Jv_JNI_SetObjectArrayElement (JNIEnv *env, jobjectArray array,
8e9031ec 1483 jsize index, jobject value)
8a922095 1484{
b099f07d
TT
1485 try
1486 {
819138ac
TT
1487 array = unwrap (array);
1488 value = unwrap (value);
1489
b099f07d 1490 _Jv_CheckArrayStore (array, value);
9bc825c4
TT
1491 if ((unsigned) index >= (unsigned) array->length)
1492 _Jv_ThrowBadArrayIndex (index);
b099f07d
TT
1493 jobject *elts = elements (array);
1494 elts[index] = value;
1495 }
1496 catch (jthrowable t)
1497 {
1498 env->ex = t;
1499 }
8a922095
TT
1500}
1501
1502template<typename T, jclass K>
8319dc87
PB
1503static JArray<T> * JNICALL
1504_Jv_JNI_NewPrimitiveArray (JNIEnv *env, jsize length)
8a922095 1505{
b099f07d
TT
1506 try
1507 {
1508 return (JArray<T> *) wrap_value (env, _Jv_NewPrimArray (K, length));
1509 }
1510 catch (jthrowable t)
1511 {
1512 env->ex = t;
1513 return NULL;
1514 }
8a922095
TT
1515}
1516
e976ed37 1517template<typename T, jclass K>
8319dc87
PB
1518static T * JNICALL
1519_Jv_JNI_GetPrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
8e9031ec 1520 jboolean *isCopy)
8a922095 1521{
819138ac 1522 array = unwrap (array);
e976ed37
AH
1523 if (! _Jv_JNI_check_types (env, array, K))
1524 return NULL;
8a922095
TT
1525 T *elts = elements (array);
1526 if (isCopy)
1527 {
1528 // We elect never to copy.
1529 *isCopy = false;
1530 }
39986dd5 1531 mark_for_gc (array, global_ref_table);
8a922095
TT
1532 return elts;
1533}
1534
e976ed37 1535template<typename T, jclass K>
8319dc87
PB
1536static void JNICALL
1537_Jv_JNI_ReleasePrimitiveArrayElements (JNIEnv *env, JArray<T> *array,
8e9031ec 1538 T *, jint /* mode */)
8a922095 1539{
819138ac 1540 array = unwrap (array);
e976ed37 1541 _Jv_JNI_check_types (env, array, K);
8a922095
TT
1542 // Note that we ignore MODE. We can do this because we never copy
1543 // the array elements. My reading of the JNI documentation is that
1544 // this is an option for the implementor.
39986dd5 1545 unmark_for_gc (array, global_ref_table);
8a922095
TT
1546}
1547
e976ed37 1548template<typename T, jclass K>
8319dc87
PB
1549static void JNICALL
1550_Jv_JNI_GetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
8e9031ec 1551 jsize start, jsize len,
8a922095
TT
1552 T *buf)
1553{
819138ac 1554 array = unwrap (array);
e976ed37
AH
1555 if (! _Jv_JNI_check_types (env, array, K))
1556 return;
819138ac 1557
55cc31c0
TT
1558 // The cast to unsigned lets us save a comparison.
1559 if (start < 0 || len < 0
ecd554cd 1560 || (unsigned long) (start + len) > (unsigned long) array->length)
8a922095 1561 {
b099f07d
TT
1562 try
1563 {
1564 // FIXME: index.
1565 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1566 }
1567 catch (jthrowable t)
1568 {
1569 // Could have thown out of memory error.
1570 env->ex = t;
1571 }
8a922095
TT
1572 }
1573 else
1574 {
1575 T *elts = elements (array) + start;
1576 memcpy (buf, elts, len * sizeof (T));
1577 }
1578}
1579
e976ed37 1580template<typename T, jclass K>
8319dc87
PB
1581static void JNICALL
1582_Jv_JNI_SetPrimitiveArrayRegion (JNIEnv *env, JArray<T> *array,
f06a83c0 1583 jsize start, jsize len, const T *buf)
8a922095 1584{
819138ac 1585 array = unwrap (array);
e976ed37
AH
1586 if (! _Jv_JNI_check_types (env, array, K))
1587 return;
819138ac 1588
55cc31c0
TT
1589 // The cast to unsigned lets us save a comparison.
1590 if (start < 0 || len < 0
ecd554cd 1591 || (unsigned long) (start + len) > (unsigned long) array->length)
8a922095 1592 {
b099f07d
TT
1593 try
1594 {
1595 // FIXME: index.
1596 env->ex = new java::lang::ArrayIndexOutOfBoundsException ();
1597 }
1598 catch (jthrowable t)
1599 {
1600 env->ex = t;
1601 }
8a922095
TT
1602 }
1603 else
1604 {
1605 T *elts = elements (array) + start;
1606 memcpy (elts, buf, len * sizeof (T));
1607 }
1608}
1609
8319dc87
PB
1610static void * JNICALL
1611_Jv_JNI_GetPrimitiveArrayCritical (JNIEnv *, jarray array,
8e9031ec 1612 jboolean *isCopy)
355dff4c 1613{
819138ac 1614 array = unwrap (array);
355dff4c
TT
1615 // FIXME: does this work?
1616 jclass klass = array->getClass()->getComponentType();
1617 JvAssert (klass->isPrimitive ());
1618 char *r = _Jv_GetArrayElementFromElementType (array, klass);
1619 if (isCopy)
1620 *isCopy = false;
1621 return r;
1622}
1623
8319dc87
PB
1624static void JNICALL
1625_Jv_JNI_ReleasePrimitiveArrayCritical (JNIEnv *, jarray, void *, jint)
355dff4c
TT
1626{
1627 // Nothing.
1628}
1629
8319dc87
PB
1630static jint JNICALL
1631_Jv_JNI_MonitorEnter (JNIEnv *env, jobject obj)
8a922095 1632{
b099f07d
TT
1633 try
1634 {
819138ac 1635 _Jv_MonitorEnter (unwrap (obj));
55cc31c0 1636 return 0;
b099f07d
TT
1637 }
1638 catch (jthrowable t)
1639 {
1640 env->ex = t;
1641 }
1642 return JNI_ERR;
8a922095
TT
1643}
1644
8319dc87
PB
1645static jint JNICALL
1646_Jv_JNI_MonitorExit (JNIEnv *env, jobject obj)
8a922095 1647{
b099f07d
TT
1648 try
1649 {
819138ac 1650 _Jv_MonitorExit (unwrap (obj));
55cc31c0 1651 return 0;
b099f07d
TT
1652 }
1653 catch (jthrowable t)
1654 {
1655 env->ex = t;
1656 }
1657 return JNI_ERR;
8a922095
TT
1658}
1659
ee9dd372 1660// JDK 1.2
8319dc87
PB
1661jobject JNICALL
1662_Jv_JNI_ToReflectedField (JNIEnv *env, jclass cls, jfieldID fieldID,
8e9031ec 1663 jboolean)
ee9dd372 1664{
b099f07d
TT
1665 try
1666 {
819138ac 1667 cls = unwrap (cls);
b099f07d
TT
1668 java::lang::reflect::Field *field = new java::lang::reflect::Field();
1669 field->declaringClass = cls;
1670 field->offset = (char*) fieldID - (char *) cls->fields;
1671 field->name = _Jv_NewStringUtf8Const (fieldID->getNameUtf8Const (cls));
1672 return wrap_value (env, field);
1673 }
1674 catch (jthrowable t)
1675 {
1676 env->ex = t;
1677 }
1678 return NULL;
ee9dd372
TT
1679}
1680
1681// JDK 1.2
8319dc87
PB
1682static jfieldID JNICALL
1683_Jv_JNI_FromReflectedField (JNIEnv *, jobject f)
ee9dd372 1684{
355dff4c
TT
1685 using namespace java::lang::reflect;
1686
819138ac 1687 f = unwrap (f);
355dff4c 1688 Field *field = reinterpret_cast<Field *> (f);
ee9dd372
TT
1689 return _Jv_FromReflectedField (field);
1690}
1691
8319dc87
PB
1692jobject JNICALL
1693_Jv_JNI_ToReflectedMethod (JNIEnv *env, jclass klass, jmethodID id,
8e9031ec 1694 jboolean)
355dff4c
TT
1695{
1696 using namespace java::lang::reflect;
1697
b099f07d 1698 jobject result = NULL;
819138ac 1699 klass = unwrap (klass);
b099f07d
TT
1700
1701 try
355dff4c 1702 {
b099f07d
TT
1703 if (_Jv_equalUtf8Consts (id->name, init_name))
1704 {
1705 // A constructor.
1706 Constructor *cons = new Constructor ();
1707 cons->offset = (char *) id - (char *) &klass->methods;
1708 cons->declaringClass = klass;
1709 result = cons;
1710 }
1711 else
1712 {
1713 Method *meth = new Method ();
1714 meth->offset = (char *) id - (char *) &klass->methods;
1715 meth->declaringClass = klass;
1716 result = meth;
1717 }
355dff4c 1718 }
b099f07d 1719 catch (jthrowable t)
355dff4c 1720 {
b099f07d 1721 env->ex = t;
355dff4c
TT
1722 }
1723
278abd28 1724 return wrap_value (env, result);
355dff4c
TT
1725}
1726
8319dc87
PB
1727static jmethodID JNICALL
1728_Jv_JNI_FromReflectedMethod (JNIEnv *, jobject method)
355dff4c
TT
1729{
1730 using namespace java::lang::reflect;
819138ac 1731 method = unwrap (method);
39986dd5 1732 if (Method::class$.isInstance (method))
355dff4c
TT
1733 return _Jv_FromReflectedMethod (reinterpret_cast<Method *> (method));
1734 return
1735 _Jv_FromReflectedConstructor (reinterpret_cast<Constructor *> (method));
1736}
1737
819138ac 1738// JDK 1.2.
8319dc87
PB
1739jweak JNICALL
1740_Jv_JNI_NewWeakGlobalRef (JNIEnv *env, jobject obj)
819138ac
TT
1741{
1742 using namespace gnu::gcj::runtime;
1743 JNIWeakRef *ref = NULL;
1744
1745 try
1746 {
1747 // This seems weird but I think it is correct.
1748 obj = unwrap (obj);
1749 ref = new JNIWeakRef (obj);
1750 mark_for_gc (ref, global_ref_table);
1751 }
1752 catch (jthrowable t)
1753 {
1754 env->ex = t;
1755 }
1756
1757 return reinterpret_cast<jweak> (ref);
1758}
1759
8319dc87
PB
1760void JNICALL
1761_Jv_JNI_DeleteWeakGlobalRef (JNIEnv *, jweak obj)
819138ac 1762{
30423732
MT
1763 // JDK compatibility.
1764 if (obj == NULL)
1765 return;
1766
819138ac
TT
1767 using namespace gnu::gcj::runtime;
1768 JNIWeakRef *ref = reinterpret_cast<JNIWeakRef *> (obj);
1769 unmark_for_gc (ref, global_ref_table);
1770 ref->clear ();
1771}
1772
a4f291fc
TT
1773\f
1774
880f8c16
TT
1775// Direct byte buffers.
1776
8319dc87
PB
1777static jobject JNICALL
1778_Jv_JNI_NewDirectByteBuffer (JNIEnv *, void *address, jlong length)
880f8c16 1779{
6f3aed57
MK
1780 using namespace gnu::gcj;
1781 using namespace java::nio;
7ef52736
MK
1782 return new DirectByteBufferImpl$ReadWrite
1783 (reinterpret_cast<RawData *> (address), length);
880f8c16
TT
1784}
1785
8319dc87
PB
1786static void * JNICALL
1787_Jv_JNI_GetDirectBufferAddress (JNIEnv *, jobject buffer)
880f8c16 1788{
6f3aed57 1789 using namespace java::nio;
d2ba8a75
MK
1790 if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
1791 return NULL;
1792 Buffer *tmp = static_cast<Buffer *> (buffer);
1793 return reinterpret_cast<void *> (tmp->address);
880f8c16
TT
1794}
1795
8319dc87
PB
1796static jlong JNICALL
1797_Jv_JNI_GetDirectBufferCapacity (JNIEnv *, jobject buffer)
880f8c16 1798{
6f3aed57 1799 using namespace java::nio;
d2ba8a75
MK
1800 if (! _Jv_IsInstanceOf (buffer, &Buffer::class$))
1801 return -1;
1802 Buffer *tmp = static_cast<Buffer *> (buffer);
1803 if (tmp->address == NULL)
1804 return -1;
1805 return tmp->capacity();
880f8c16
TT
1806}
1807
e0441a5b 1808static jobjectRefType JNICALL
78b3c197 1809_Jv_JNI_GetObjectRefType (JNIEnv *, MAYBE_UNUSED jobject object)
e0441a5b
MK
1810{
1811 JvFail("GetObjectRefType not implemented");
1812 return JNIInvalidRefType;
1813}
1814
880f8c16
TT
1815\f
1816
e7f7d233
TT
1817struct NativeMethodCacheEntry : public JNINativeMethod
1818{
1819 char *className;
1820};
1821
a4f291fc 1822// Hash table of native methods.
e7f7d233 1823static NativeMethodCacheEntry *nathash;
a4f291fc
TT
1824// Number of slots used.
1825static int nathash_count = 0;
1826// Number of slots available. Must be power of 2.
1827static int nathash_size = 0;
1828
1829#define DELETED_ENTRY ((char *) (~0))
1830
1831// Compute a hash value for a native method descriptor.
1832static int
e7f7d233 1833hash (const NativeMethodCacheEntry *method)
a4f291fc
TT
1834{
1835 char *ptr;
1836 int hash = 0;
1837
e7f7d233
TT
1838 ptr = method->className;
1839 while (*ptr)
1840 hash = (31 * hash) + *ptr++;
1841
a4f291fc
TT
1842 ptr = method->name;
1843 while (*ptr)
1844 hash = (31 * hash) + *ptr++;
1845
1846 ptr = method->signature;
1847 while (*ptr)
1848 hash = (31 * hash) + *ptr++;
1849
1850 return hash;
1851}
1852
1853// Find the slot where a native method goes.
e7f7d233
TT
1854static NativeMethodCacheEntry *
1855nathash_find_slot (const NativeMethodCacheEntry *method)
a4f291fc
TT
1856{
1857 jint h = hash (method);
1858 int step = (h ^ (h >> 16)) | 1;
1859 int w = h & (nathash_size - 1);
1860 int del = -1;
1861
1862 for (;;)
1863 {
e7f7d233 1864 NativeMethodCacheEntry *slotp = &nathash[w];
a4f291fc
TT
1865 if (slotp->name == NULL)
1866 {
1867 if (del >= 0)
1868 return &nathash[del];
1869 else
1870 return slotp;
1871 }
1872 else if (slotp->name == DELETED_ENTRY)
1873 del = w;
1874 else if (! strcmp (slotp->name, method->name)
e7f7d233
TT
1875 && ! strcmp (slotp->signature, method->signature)
1876 && ! strcmp (slotp->className, method->className))
a4f291fc
TT
1877 return slotp;
1878 w = (w + step) & (nathash_size - 1);
1879 }
1880}
1881
1882// Find a method. Return NULL if it isn't in the hash table.
1883static void *
e7f7d233 1884nathash_find (NativeMethodCacheEntry *method)
a4f291fc
TT
1885{
1886 if (nathash == NULL)
1887 return NULL;
e7f7d233 1888 NativeMethodCacheEntry *slot = nathash_find_slot (method);
a4f291fc
TT
1889 if (slot->name == NULL || slot->name == DELETED_ENTRY)
1890 return NULL;
1891 return slot->fnPtr;
1892}
1893
1894static void
1895natrehash ()
1896{
1897 if (nathash == NULL)
1898 {
1899 nathash_size = 1024;
1900 nathash =
e7f7d233
TT
1901 (NativeMethodCacheEntry *) _Jv_AllocBytes (nathash_size
1902 * sizeof (NativeMethodCacheEntry));
a4f291fc
TT
1903 }
1904 else
1905 {
1906 int savesize = nathash_size;
e7f7d233 1907 NativeMethodCacheEntry *savehash = nathash;
a4f291fc
TT
1908 nathash_size *= 2;
1909 nathash =
e7f7d233
TT
1910 (NativeMethodCacheEntry *) _Jv_AllocBytes (nathash_size
1911 * sizeof (NativeMethodCacheEntry));
a4f291fc
TT
1912
1913 for (int i = 0; i < savesize; ++i)
1914 {
1915 if (savehash[i].name != NULL && savehash[i].name != DELETED_ENTRY)
1916 {
e7f7d233 1917 NativeMethodCacheEntry *slot = nathash_find_slot (&savehash[i]);
a4f291fc
TT
1918 *slot = savehash[i];
1919 }
1920 }
1921 }
1922}
1923
1924static void
e7f7d233 1925nathash_add (const NativeMethodCacheEntry *method)
a4f291fc
TT
1926{
1927 if (3 * nathash_count >= 2 * nathash_size)
1928 natrehash ();
e7f7d233 1929 NativeMethodCacheEntry *slot = nathash_find_slot (method);
a4f291fc
TT
1930 // If the slot has a real entry in it, then there is no work to do.
1931 if (slot->name != NULL && slot->name != DELETED_ENTRY)
1932 return;
e7f7d233 1933 // FIXME: memory leak?
a4f291fc 1934 slot->name = strdup (method->name);
e7f7d233 1935 slot->className = strdup (method->className);
f1b2b25c
AG
1936 // This was already strduped in _Jv_JNI_RegisterNatives.
1937 slot->signature = method->signature;
a4f291fc
TT
1938 slot->fnPtr = method->fnPtr;
1939}
1940
8319dc87
PB
1941static jint JNICALL
1942_Jv_JNI_RegisterNatives (JNIEnv *env, jclass klass,
8e9031ec
TT
1943 const JNINativeMethod *methods,
1944 jint nMethods)
8ade4771 1945{
a4f291fc
TT
1946 // Synchronize while we do the work. This must match
1947 // synchronization in some other functions that manipulate or use
1948 // the nathash table.
1949 JvSynchronize sync (global_ref_table);
8ade4771 1950
e7f7d233 1951 NativeMethodCacheEntry dottedMethod;
f1b2b25c 1952
8ade4771
TT
1953 // Look at each descriptor given us, and find the corresponding
1954 // method in the class.
1955 for (int j = 0; j < nMethods; ++j)
1956 {
1957 bool found = false;
1958
a4f291fc 1959 _Jv_Method *imeths = JvGetFirstMethod (klass);
8ade4771
TT
1960 for (int i = 0; i < JvNumMethods (klass); ++i)
1961 {
a4f291fc 1962 _Jv_Method *self = &imeths[i];
8ade4771 1963
f1b2b25c
AG
1964 // Copy this JNINativeMethod and do a slash to dot
1965 // conversion on the signature.
1966 dottedMethod.name = methods[j].name;
e7f7d233
TT
1967 // FIXME: we leak a little memory here if the method
1968 // is not found.
f1b2b25c
AG
1969 dottedMethod.signature = strdup (methods[j].signature);
1970 dottedMethod.fnPtr = methods[j].fnPtr;
e7f7d233 1971 dottedMethod.className = _Jv_GetClassNameUtf8 (klass)->chars();
f1b2b25c
AG
1972 char *c = dottedMethod.signature;
1973 while (*c)
1974 {
1975 if (*c == '/')
1976 *c = '.';
1977 c++;
1978 }
1979
1980 if (! strcmp (self->name->chars (), dottedMethod.name)
1981 && ! strcmp (self->signature->chars (), dottedMethod.signature))
8ade4771 1982 {
252d7884 1983 if (! (self->accflags & java::lang::reflect::Modifier::NATIVE))
8ade4771
TT
1984 break;
1985
1986 // Found a match that is native.
8ade4771 1987 found = true;
f1b2b25c 1988 nathash_add (&dottedMethod);
a4f291fc 1989
8ade4771
TT
1990 break;
1991 }
1992 }
1993
1994 if (! found)
1995 {
1996 jstring m = JvNewStringUTF (methods[j].name);
b099f07d
TT
1997 try
1998 {
252d7884 1999 env->ex = new java::lang::NoSuchMethodError (m);
b099f07d
TT
2000 }
2001 catch (jthrowable t)
2002 {
2003 env->ex = t;
2004 }
8ade4771
TT
2005 return JNI_ERR;
2006 }
2007 }
2008
2009 return JNI_OK;
2010}
2011
8319dc87
PB
2012static jint JNICALL
2013_Jv_JNI_UnregisterNatives (JNIEnv *, jclass)
8ade4771 2014{
a4f291fc 2015 // FIXME -- we could implement this.
8ade4771
TT
2016 return JNI_ERR;
2017}
2018
355dff4c
TT
2019\f
2020
facc279f
TT
2021// Add a character to the buffer, encoding properly.
2022static void
2023add_char (char *buf, jchar c, int *here)
2024{
2025 if (c == '_')
2026 {
2027 buf[(*here)++] = '_';
2028 buf[(*here)++] = '1';
2029 }
2030 else if (c == ';')
2031 {
2032 buf[(*here)++] = '_';
2033 buf[(*here)++] = '2';
2034 }
2035 else if (c == '[')
2036 {
2037 buf[(*here)++] = '_';
2038 buf[(*here)++] = '3';
2039 }
736458d6
MD
2040
2041 // Also check for `.' here because we might be passed an internal
2042 // qualified class name like `foo.bar'.
2043 else if (c == '/' || c == '.')
facc279f 2044 buf[(*here)++] = '_';
abfb7844 2045 else if ((c >= '0' && c <= '9')
5152512c
TT
2046 || (c >= 'a' && c <= 'z')
2047 || (c >= 'A' && c <= 'Z'))
facc279f
TT
2048 buf[(*here)++] = (char) c;
2049 else
2050 {
2051 // "Unicode" character.
2052 buf[(*here)++] = '_';
2053 buf[(*here)++] = '0';
2054 for (int i = 0; i < 4; ++i)
2055 {
2056 int val = c & 0x0f;
5152512c 2057 buf[(*here) + 3 - i] = (val > 10) ? ('a' + val - 10) : ('0' + val);
facc279f
TT
2058 c >>= 4;
2059 }
2060 *here += 4;
2061 }
2062}
2063
2064// Compute a mangled name for a native function. This computes the
2065// long name, and also returns an index which indicates where a NUL
2066// can be placed to create the short name. This function assumes that
2067// the buffer is large enough for its results.
2068static void
2069mangled_name (jclass klass, _Jv_Utf8Const *func_name,
2070 _Jv_Utf8Const *signature, char *buf, int *long_start)
2071{
2072 strcpy (buf, "Java_");
2073 int here = 5;
2074
2075 // Add fully qualified class name.
2076 jchar *chars = _Jv_GetStringChars (klass->getName ());
2077 jint len = klass->getName ()->length ();
2078 for (int i = 0; i < len; ++i)
2079 add_char (buf, chars[i], &here);
2080
2081 // Don't use add_char because we need a literal `_'.
2082 buf[here++] = '_';
2083
b4d49f49
PB
2084 const unsigned char *fn = (const unsigned char *) func_name->chars ();
2085 const unsigned char *limit = fn + func_name->len ();
facc279f
TT
2086 for (int i = 0; ; ++i)
2087 {
2088 int ch = UTF8_GET (fn, limit);
2089 if (ch < 0)
2090 break;
2091 add_char (buf, ch, &here);
2092 }
2093
2094 // This is where the long signature begins.
2095 *long_start = here;
2096 buf[here++] = '_';
2097 buf[here++] = '_';
2098
b4d49f49
PB
2099 const unsigned char *sig = (const unsigned char *) signature->chars ();
2100 limit = sig + signature->len ();
90a883ae 2101 JvAssert (sig[0] == '(');
cdd59e7b
TT
2102 ++sig;
2103 while (1)
facc279f
TT
2104 {
2105 int ch = UTF8_GET (sig, limit);
2106 if (ch == ')' || ch < 0)
2107 break;
2108 add_char (buf, ch, &here);
2109 }
2110
2111 buf[here] = '\0';
2112}
2113
262fa8a4
TT
2114JNIEnv *
2115_Jv_GetJNIEnvNewFrameWithLoader (::java::lang::ClassLoader *loader)
355dff4c 2116{
8d00f617 2117 JNIEnv *env = _Jv_GetCurrentJNIEnv ();
84973b27 2118 if (__builtin_expect (env == NULL, false))
8d00f617
TT
2119 {
2120 env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
cd6d4007 2121 env->functions = &_Jv_JNIFunctions;
8d00f617 2122 env->locals = NULL;
b08122a7 2123 // We set env->ex below.
ee6713e7 2124
84973b27
GH
2125 // Set up the bottom, reusable frame.
2126 env->bottom_locals = (_Jv_JNI_LocalFrame *)
2127 _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2128 + (FRAME_SIZE
2129 * sizeof (jobject)));
262fa8a4 2130
84973b27
GH
2131 env->bottom_locals->marker = MARK_SYSTEM;
2132 env->bottom_locals->size = FRAME_SIZE;
2133 env->bottom_locals->next = NULL;
262fa8a4
TT
2134 env->bottom_locals->allocated_p = false;
2135 // We set the klass field below.
84973b27
GH
2136 memset (&env->bottom_locals->vec[0], 0,
2137 env->bottom_locals->size * sizeof (jobject));
2138
8d00f617
TT
2139 _Jv_SetCurrentJNIEnv (env);
2140 }
355dff4c 2141
84973b27
GH
2142 // If we're in a simple JNI call (non-nested), we can just reuse the
2143 // locals frame we allocated many calls ago, back when the env was first
2144 // built, above.
7e648cf9 2145
84973b27 2146 if (__builtin_expect (env->locals == NULL, true))
262fa8a4
TT
2147 {
2148 env->locals = env->bottom_locals;
2149 env->locals->loader = loader;
2150 }
84973b27
GH
2151 else
2152 {
2153 // Alternatively, we might be re-entering JNI, in which case we can't
2154 // reuse the bottom_locals frame, because it is already underneath
2155 // us. So we need to make a new one.
84973b27
GH
2156 _Jv_JNI_LocalFrame *frame
2157 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2158 + (FRAME_SIZE
2159 * sizeof (jobject)));
262fa8a4 2160
84973b27
GH
2161 frame->marker = MARK_SYSTEM;
2162 frame->size = FRAME_SIZE;
262fa8a4 2163 frame->allocated_p = false;
84973b27 2164 frame->next = env->locals;
262fa8a4 2165 frame->loader = loader;
84973b27
GH
2166
2167 memset (&frame->vec[0], 0,
2168 frame->size * sizeof (jobject));
2169
2170 env->locals = frame;
2171 }
355dff4c 2172
b08122a7
TT
2173 env->ex = NULL;
2174
8d00f617
TT
2175 return env;
2176}
2177
262fa8a4
TT
2178// Return the current thread's JNIEnv; if one does not exist, create
2179// it. Also create a new system frame for use. This is `extern "C"'
2180// because the compiler calls it.
2181extern "C" JNIEnv *
2182_Jv_GetJNIEnvNewFrame (jclass klass)
2183{
2184 return _Jv_GetJNIEnvNewFrameWithLoader (klass->getClassLoaderInternal());
2185}
2186
84973b27
GH
2187// Destroy the env's reusable resources. This is called from the thread
2188// destructor "finalize_native" in natThread.cc
2189void
2190_Jv_FreeJNIEnv (_Jv_JNIEnv *env)
2191{
2192 if (env == NULL)
2193 return;
2194
2195 if (env->bottom_locals != NULL)
2196 _Jv_Free (env->bottom_locals);
2197
2198 _Jv_Free (env);
2199}
2200
8d00f617
TT
2201// Return the function which implements a particular JNI method. If
2202// we can't find the function, we throw the appropriate exception.
2203// This is `extern "C"' because the compiler uses it.
2204extern "C" void *
2205_Jv_LookupJNIMethod (jclass klass, _Jv_Utf8Const *name,
3953c057 2206 _Jv_Utf8Const *signature, MAYBE_UNUSED int args_size)
8d00f617 2207{
b4d49f49
PB
2208 int name_length = name->len();
2209 int sig_length = signature->len();
2210 char buf[10 + 6 * (name_length + sig_length) + 12];
8d00f617
TT
2211 int long_start;
2212 void *function;
2213
a4f291fc
TT
2214 // Synchronize on something convenient. Right now we use the hash.
2215 JvSynchronize sync (global_ref_table);
2216
2217 // First see if we have an override in the hash table.
b4d49f49
PB
2218 strncpy (buf, name->chars (), name_length);
2219 buf[name_length] = '\0';
2220 strncpy (buf + name_length + 1, signature->chars (), sig_length);
2221 buf[name_length + sig_length + 1] = '\0';
e7f7d233 2222 NativeMethodCacheEntry meth;
a4f291fc 2223 meth.name = buf;
b4d49f49 2224 meth.signature = buf + name_length + 1;
e7f7d233 2225 meth.className = _Jv_GetClassNameUtf8(klass)->chars();
a4f291fc
TT
2226 function = nathash_find (&meth);
2227 if (function != NULL)
2228 return function;
2229
2230 // If there was no override, then look in the symbol table.
c79d7702
RM
2231 buf[0] = '_';
2232 mangled_name (klass, name, signature, buf + 1, &long_start);
2233 char c = buf[long_start + 1];
2234 buf[long_start + 1] = '\0';
2235
2236 function = _Jv_FindSymbolInExecutable (buf + 1);
2237#ifdef WIN32
2238 // On Win32, we use the "stdcall" calling convention (see JNICALL
2239 // in jni.h).
2240 //
2241 // For a function named 'fooBar' that takes 'nn' bytes as arguments,
2242 // by default, MinGW GCC exports it as 'fooBar@nn', MSVC exports it
2243 // as '_fooBar@nn' and Borland C exports it as 'fooBar'. We try to
2244 // take care of all these variations here.
2245
2246 char asz_buf[12]; /* '@' + '2147483647' (32-bit INT_MAX) + '\0' */
2247 char long_nm_sv[11]; /* Ditto, except for the '\0'. */
2248
8d00f617
TT
2249 if (function == NULL)
2250 {
c79d7702
RM
2251 // We have tried searching for the 'fooBar' form (BCC) - now
2252 // try the others.
2253
2254 // First, save the part of the long name that will be damaged
2255 // by appending '@nn'.
2256 memcpy (long_nm_sv, (buf + long_start + 1 + 1), sizeof (long_nm_sv));
2257
2258 sprintf (asz_buf, "@%d", args_size);
2259 strcat (buf, asz_buf);
2260
2261 // Search for the '_fooBar@nn' form (MSVC).
8d00f617 2262 function = _Jv_FindSymbolInExecutable (buf);
c79d7702
RM
2263
2264 if (function == NULL)
2265 {
2266 // Search for the 'fooBar@nn' form (MinGW GCC).
2267 function = _Jv_FindSymbolInExecutable (buf + 1);
2268 }
2269 }
b5bb72ec 2270#endif /* WIN32 */
c79d7702
RM
2271
2272 if (function == NULL)
2273 {
2274 buf[long_start + 1] = c;
2275#ifdef WIN32
2276 // Restore the part of the long name that was damaged by
2277 // appending the '@nn'.
2278 memcpy ((buf + long_start + 1 + 1), long_nm_sv, sizeof (long_nm_sv));
2279#endif /* WIN32 */
2280 function = _Jv_FindSymbolInExecutable (buf + 1);
8d00f617
TT
2281 if (function == NULL)
2282 {
c79d7702
RM
2283#ifdef WIN32
2284 strcat (buf, asz_buf);
2285 function = _Jv_FindSymbolInExecutable (buf);
2286 if (function == NULL)
2287 function = _Jv_FindSymbolInExecutable (buf + 1);
2288
2289 if (function == NULL)
2290#endif /* WIN32 */
2291 {
b4d49f49 2292 jstring str = JvNewStringUTF (name->chars ());
c79d7702
RM
2293 throw new java::lang::UnsatisfiedLinkError (str);
2294 }
8d00f617
TT
2295 }
2296 }
2297
2298 return function;
2299}
2300
5152512c
TT
2301#ifdef INTERPRETER
2302
8d00f617
TT
2303// This function is the stub which is used to turn an ordinary (CNI)
2304// method call into a JNI call.
2305void
4c42b3d8
DD
2306_Jv_JNIMethod::call (ffi_cif *, void *ret, INTERP_FFI_RAW_TYPE *args,
2307 void *__this)
8d00f617
TT
2308{
2309 _Jv_JNIMethod* _this = (_Jv_JNIMethod *) __this;
2310
2311 JNIEnv *env = _Jv_GetJNIEnvNewFrame (_this->defining_class);
2312
ee6713e7
TT
2313 // FIXME: we should mark every reference parameter as a local. For
2314 // now we assume a conservative GC, and we assume that the
2315 // references are on the stack somewhere.
2316
facc279f
TT
2317 // We cache the value that we find, of course, but if we don't find
2318 // a value we don't cache that fact -- we might subsequently load a
2319 // library which finds the function in question.
a4f291fc
TT
2320 {
2321 // Synchronize on a convenient object to ensure sanity in case two
2322 // threads reach this point for the same function at the same
2323 // time.
2324 JvSynchronize sync (global_ref_table);
2325 if (_this->function == NULL)
c79d7702
RM
2326 {
2327 int args_size = sizeof (JNIEnv *) + _this->args_raw_size;
2328
2329 if (_this->self->accflags & java::lang::reflect::Modifier::STATIC)
2330 args_size += sizeof (_this->defining_class);
2331
2332 _this->function = _Jv_LookupJNIMethod (_this->defining_class,
2333 _this->self->name,
2334 _this->self->signature,
2335 args_size);
2336 }
a4f291fc 2337 }
facc279f 2338
4c42b3d8
DD
2339 JvAssert (_this->args_raw_size % sizeof (INTERP_FFI_RAW_TYPE) == 0);
2340 INTERP_FFI_RAW_TYPE
2341 real_args[2 + _this->args_raw_size / sizeof (INTERP_FFI_RAW_TYPE)];
d348bda4
TT
2342 int offset = 0;
2343
2344 // First argument is always the environment pointer.
8d00f617 2345 real_args[offset++].ptr = env;
d348bda4
TT
2346
2347 // For a static method, we pass in the Class. For non-static
2348 // methods, the `this' argument is already handled.
2349 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2350 real_args[offset++].ptr = _this->defining_class;
2351
415791db
TT
2352 // In libgcj, the callee synchronizes.
2353 jobject sync = NULL;
2354 if ((_this->self->accflags & java::lang::reflect::Modifier::SYNCHRONIZED))
2355 {
2356 if ((_this->self->accflags & java::lang::reflect::Modifier::STATIC))
2357 sync = _this->defining_class;
2358 else
2359 sync = (jobject) args[0].ptr;
2360 _Jv_MonitorEnter (sync);
2361 }
2362
d348bda4
TT
2363 // Copy over passed-in arguments.
2364 memcpy (&real_args[offset], args, _this->args_raw_size);
39273131
KG
2365
2366 // Add a frame to the composite (interpreted + JNI) call stack
2367 java::lang::Thread *thread = java::lang::Thread::currentThread();
2368 _Jv_NativeFrame nat_frame (_this, thread);
d348bda4 2369
facc279f 2370 // The actual call to the JNI function.
ed674251 2371#if FFI_NATIVE_RAW_API
79c2c6da 2372 ffi_raw_call (&_this->jni_cif, (void (*)()) _this->function,
d348bda4 2373 ret, real_args);
ed674251
UW
2374#else
2375 ffi_java_raw_call (&_this->jni_cif, (void (*)()) _this->function,
2376 ret, real_args);
2377#endif
355dff4c 2378
3141ed0f
TT
2379 // We might need to unwrap a JNI weak reference here.
2380 if (_this->jni_cif.rtype == &ffi_type_pointer)
2381 {
2382 _Jv_value *val = (_Jv_value *) ret;
2383 val->object_value = unwrap (val->object_value);
2384 }
2385
415791db
TT
2386 if (sync != NULL)
2387 _Jv_MonitorExit (sync);
2388
8d00f617 2389 _Jv_JNI_PopSystemFrame (env);
355dff4c 2390}
355dff4c 2391
f39b788a
TT
2392#endif /* INTERPRETER */
2393
355dff4c
TT
2394\f
2395
aaf0766e
TT
2396//
2397// Invocation API.
2398//
2399
2400// An internal helper function.
2401static jint
880f8c16
TT
2402_Jv_JNI_AttachCurrentThread (JavaVM *, jstring name, void **penv,
2403 void *args, jboolean is_daemon)
aaf0766e
TT
2404{
2405 JavaVMAttachArgs *attach = reinterpret_cast<JavaVMAttachArgs *> (args);
2406 java::lang::ThreadGroup *group = NULL;
2407
2408 if (attach)
2409 {
2410 // FIXME: do we really want to support 1.1?
880f8c16
TT
2411 if (attach->version != JNI_VERSION_1_4
2412 && attach->version != JNI_VERSION_1_2
aaf0766e
TT
2413 && attach->version != JNI_VERSION_1_1)
2414 return JNI_EVERSION;
2415
39986dd5 2416 JvAssert (java::lang::ThreadGroup::class$.isInstance (attach->group));
aaf0766e
TT
2417 group = reinterpret_cast<java::lang::ThreadGroup *> (attach->group);
2418 }
2419
2420 // Attaching an already-attached thread is a no-op.
ec5dd52b
TT
2421 JNIEnv *env = _Jv_GetCurrentJNIEnv ();
2422 if (env != NULL)
2423 {
2424 *penv = reinterpret_cast<void *> (env);
2425 return 0;
2426 }
aaf0766e 2427
ec5dd52b 2428 env = (JNIEnv *) _Jv_MallocUnchecked (sizeof (JNIEnv));
2d759f71
TT
2429 if (env == NULL)
2430 return JNI_ERR;
cd6d4007 2431 env->functions = &_Jv_JNIFunctions;
aaf0766e 2432 env->ex = NULL;
84973b27 2433 env->bottom_locals
aaf0766e
TT
2434 = (_Jv_JNI_LocalFrame *) _Jv_MallocUnchecked (sizeof (_Jv_JNI_LocalFrame)
2435 + (FRAME_SIZE
2436 * sizeof (jobject)));
84973b27 2437 env->locals = env->bottom_locals;
2d759f71
TT
2438 if (env->locals == NULL)
2439 {
2440 _Jv_Free (env);
2441 return JNI_ERR;
2442 }
d3ae0d49 2443
262fa8a4 2444 env->locals->allocated_p = false;
d3ae0d49
MK
2445 env->locals->marker = MARK_SYSTEM;
2446 env->locals->size = FRAME_SIZE;
262fa8a4 2447 env->locals->loader = NULL;
c66b9fd9 2448 env->locals->next = NULL;
d3ae0d49
MK
2449
2450 for (int i = 0; i < env->locals->size; ++i)
2451 env->locals->vec[i] = NULL;
2452
aaf0766e
TT
2453 *penv = reinterpret_cast<void *> (env);
2454
8ade4771
TT
2455 // This thread might already be a Java thread -- this function might
2456 // have been called simply to set the new JNIEnv.
2457 if (_Jv_ThreadCurrent () == NULL)
2458 {
b099f07d
TT
2459 try
2460 {
880f8c16
TT
2461 if (is_daemon)
2462 _Jv_AttachCurrentThreadAsDaemon (name, group);
2463 else
2464 _Jv_AttachCurrentThread (name, group);
b099f07d
TT
2465 }
2466 catch (jthrowable t)
2467 {
2468 return JNI_ERR;
2469 }
8ade4771 2470 }
aaf0766e
TT
2471 _Jv_SetCurrentJNIEnv (env);
2472
2473 return 0;
2474}
2475
2476// This is the one actually used by JNI.
fb5b5d97 2477jint JNICALL
8319dc87 2478_Jv_JNI_AttachCurrentThread (JavaVM *vm, void **penv, void *args)
aaf0766e 2479{
880f8c16
TT
2480 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, false);
2481}
2482
8319dc87
PB
2483static jint JNICALL
2484_Jv_JNI_AttachCurrentThreadAsDaemon (JavaVM *vm, void **penv,
8e9031ec 2485 void *args)
880f8c16
TT
2486{
2487 return _Jv_JNI_AttachCurrentThread (vm, NULL, penv, args, true);
aaf0766e
TT
2488}
2489
8319dc87
PB
2490static jint JNICALL
2491_Jv_JNI_DestroyJavaVM (JavaVM *vm)
aaf0766e 2492{
fb5b5d97 2493 JvAssert (_Jv_the_vm && vm == _Jv_the_vm);
aaf0766e 2494
1349c688
BM
2495 union
2496 {
2497 JNIEnv *env;
2498 void *env_p;
2499 };
2500
aaf0766e
TT
2501 if (_Jv_ThreadCurrent () != NULL)
2502 {
b099f07d
TT
2503 jstring main_name;
2504 // This sucks.
2505 try
2506 {
2507 main_name = JvNewStringLatin1 ("main");
2508 }
2509 catch (jthrowable t)
2510 {
2511 return JNI_ERR;
2512 }
2513
1349c688 2514 jint r = _Jv_JNI_AttachCurrentThread (vm, main_name, &env_p,
880f8c16 2515 NULL, false);
aaf0766e
TT
2516 if (r < 0)
2517 return r;
2518 }
2519 else
2520 env = _Jv_GetCurrentJNIEnv ();
2521
2522 _Jv_ThreadWait ();
2523
2524 // Docs say that this always returns an error code.
2525 return JNI_ERR;
2526}
2527
8319dc87
PB
2528jint JNICALL
2529_Jv_JNI_DetachCurrentThread (JavaVM *)
aaf0766e 2530{
c93d7fae
PB
2531 jint code = _Jv_DetachCurrentThread ();
2532 return code ? JNI_EDETACHED : 0;
aaf0766e
TT
2533}
2534
8319dc87
PB
2535static jint JNICALL
2536_Jv_JNI_GetEnv (JavaVM *, void **penv, jint version)
aaf0766e
TT
2537{
2538 if (_Jv_ThreadCurrent () == NULL)
2539 {
2540 *penv = NULL;
2541 return JNI_EDETACHED;
2542 }
2543
54c2f04b
AG
2544#ifdef ENABLE_JVMPI
2545 // Handle JVMPI requests.
2546 if (version == JVMPI_VERSION_1)
2547 {
2548 *penv = (void *) &_Jv_JVMPI_Interface;
2549 return 0;
2550 }
2551#endif
2552
86acf60c 2553#ifdef INTERPRETER
94f473ee
KS
2554 // Handle JVMTI requests
2555 if (version == JVMTI_VERSION_1_0)
2556 {
2557 *penv = (void *) _Jv_GetJVMTIEnv ();
2558 return 0;
2559 }
86acf60c 2560#endif
94f473ee 2561
aaf0766e 2562 // FIXME: do we really want to support 1.1?
880f8c16
TT
2563 if (version != JNI_VERSION_1_4 && version != JNI_VERSION_1_2
2564 && version != JNI_VERSION_1_1)
aaf0766e
TT
2565 {
2566 *penv = NULL;
2567 return JNI_EVERSION;
2568 }
2569
2570 *penv = (void *) _Jv_GetCurrentJNIEnv ();
2571 return 0;
2572}
2573
8ade4771
TT
2574JavaVM *
2575_Jv_GetJavaVM ()
aaf0766e
TT
2576{
2577 // FIXME: synchronize
fb5b5d97 2578 if (! _Jv_the_vm)
aaf0766e
TT
2579 {
2580 JavaVM *nvm = (JavaVM *) _Jv_MallocUnchecked (sizeof (JavaVM));
8ade4771
TT
2581 if (nvm != NULL)
2582 nvm->functions = &_Jv_JNI_InvokeFunctions;
fb5b5d97 2583 _Jv_the_vm = nvm;
aaf0766e
TT
2584 }
2585
8ade4771
TT
2586 // If this is a Java thread, we want to make sure it has an
2587 // associated JNIEnv.
2588 if (_Jv_ThreadCurrent () != NULL)
2589 {
2590 void *ignore;
fb5b5d97 2591 _Jv_JNI_AttachCurrentThread (_Jv_the_vm, &ignore, NULL);
8ade4771
TT
2592 }
2593
fb5b5d97 2594 return _Jv_the_vm;
8ade4771
TT
2595}
2596
8319dc87
PB
2597static jint JNICALL
2598_Jv_JNI_GetJavaVM (JNIEnv *, JavaVM **vm)
8ade4771
TT
2599{
2600 *vm = _Jv_GetJavaVM ();
2601 return *vm == NULL ? JNI_ERR : JNI_OK;
aaf0766e
TT
2602}
2603
2604\f
2605
8a922095 2606#define RESERVED NULL
ee9dd372 2607
f06a83c0 2608struct JNINativeInterface_ _Jv_JNIFunctions =
ee9dd372 2609{
8a922095
TT
2610 RESERVED,
2611 RESERVED,
2612 RESERVED,
2613 RESERVED,
65422ec5
TT
2614 _Jv_JNI_GetVersion, // GetVersion
2615 _Jv_JNI_DefineClass, // DefineClass
2616 _Jv_JNI_FindClass, // FindClass
2617 _Jv_JNI_FromReflectedMethod, // FromReflectedMethod
2618 _Jv_JNI_FromReflectedField, // FromReflectedField
2619 _Jv_JNI_ToReflectedMethod, // ToReflectedMethod
2620 _Jv_JNI_GetSuperclass, // GetSuperclass
2621 _Jv_JNI_IsAssignableFrom, // IsAssignableFrom
2622 _Jv_JNI_ToReflectedField, // ToReflectedField
2623 _Jv_JNI_Throw, // Throw
2624 _Jv_JNI_ThrowNew, // ThrowNew
2625 _Jv_JNI_ExceptionOccurred, // ExceptionOccurred
2626 _Jv_JNI_ExceptionDescribe, // ExceptionDescribe
2627 _Jv_JNI_ExceptionClear, // ExceptionClear
2628 _Jv_JNI_FatalError, // FatalError
2629
2630 _Jv_JNI_PushLocalFrame, // PushLocalFrame
2631 _Jv_JNI_PopLocalFrame, // PopLocalFrame
2632 _Jv_JNI_NewGlobalRef, // NewGlobalRef
2633 _Jv_JNI_DeleteGlobalRef, // DeleteGlobalRef
2634 _Jv_JNI_DeleteLocalRef, // DeleteLocalRef
2635
2636 _Jv_JNI_IsSameObject, // IsSameObject
2637
2638 _Jv_JNI_NewLocalRef, // NewLocalRef
2639 _Jv_JNI_EnsureLocalCapacity, // EnsureLocalCapacity
2640
2641 _Jv_JNI_AllocObject, // AllocObject
2642 _Jv_JNI_NewObject, // NewObject
2643 _Jv_JNI_NewObjectV, // NewObjectV
2644 _Jv_JNI_NewObjectA, // NewObjectA
2645 _Jv_JNI_GetObjectClass, // GetObjectClass
2646 _Jv_JNI_IsInstanceOf, // IsInstanceOf
2647 _Jv_JNI_GetAnyMethodID<false>, // GetMethodID
2648
2649 _Jv_JNI_CallMethod<jobject>, // CallObjectMethod
2650 _Jv_JNI_CallMethodV<jobject>, // CallObjectMethodV
2651 _Jv_JNI_CallMethodA<jobject>, // CallObjectMethodA
2652 _Jv_JNI_CallMethod<jboolean>, // CallBooleanMethod
2653 _Jv_JNI_CallMethodV<jboolean>, // CallBooleanMethodV
2654 _Jv_JNI_CallMethodA<jboolean>, // CallBooleanMethodA
2655 _Jv_JNI_CallMethod<jbyte>, // CallByteMethod
2656 _Jv_JNI_CallMethodV<jbyte>, // CallByteMethodV
2657 _Jv_JNI_CallMethodA<jbyte>, // CallByteMethodA
2658 _Jv_JNI_CallMethod<jchar>, // CallCharMethod
2659 _Jv_JNI_CallMethodV<jchar>, // CallCharMethodV
2660 _Jv_JNI_CallMethodA<jchar>, // CallCharMethodA
2661 _Jv_JNI_CallMethod<jshort>, // CallShortMethod
2662 _Jv_JNI_CallMethodV<jshort>, // CallShortMethodV
2663 _Jv_JNI_CallMethodA<jshort>, // CallShortMethodA
2664 _Jv_JNI_CallMethod<jint>, // CallIntMethod
2665 _Jv_JNI_CallMethodV<jint>, // CallIntMethodV
2666 _Jv_JNI_CallMethodA<jint>, // CallIntMethodA
2667 _Jv_JNI_CallMethod<jlong>, // CallLongMethod
2668 _Jv_JNI_CallMethodV<jlong>, // CallLongMethodV
2669 _Jv_JNI_CallMethodA<jlong>, // CallLongMethodA
2670 _Jv_JNI_CallMethod<jfloat>, // CallFloatMethod
2671 _Jv_JNI_CallMethodV<jfloat>, // CallFloatMethodV
2672 _Jv_JNI_CallMethodA<jfloat>, // CallFloatMethodA
2673 _Jv_JNI_CallMethod<jdouble>, // CallDoubleMethod
2674 _Jv_JNI_CallMethodV<jdouble>, // CallDoubleMethodV
2675 _Jv_JNI_CallMethodA<jdouble>, // CallDoubleMethodA
2676 _Jv_JNI_CallVoidMethod, // CallVoidMethod
2677 _Jv_JNI_CallVoidMethodV, // CallVoidMethodV
2678 _Jv_JNI_CallVoidMethodA, // CallVoidMethodA
8a922095
TT
2679
2680 // Nonvirtual method invocation functions follow.
65422ec5
TT
2681 _Jv_JNI_CallAnyMethod<jobject, nonvirtual>, // CallNonvirtualObjectMethod
2682 _Jv_JNI_CallAnyMethodV<jobject, nonvirtual>, // CallNonvirtualObjectMethodV
2683 _Jv_JNI_CallAnyMethodA<jobject, nonvirtual>, // CallNonvirtualObjectMethodA
2684 _Jv_JNI_CallAnyMethod<jboolean, nonvirtual>, // CallNonvirtualBooleanMethod
2685 _Jv_JNI_CallAnyMethodV<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodV
2686 _Jv_JNI_CallAnyMethodA<jboolean, nonvirtual>, // CallNonvirtualBooleanMethodA
2687 _Jv_JNI_CallAnyMethod<jbyte, nonvirtual>, // CallNonvirtualByteMethod
2688 _Jv_JNI_CallAnyMethodV<jbyte, nonvirtual>, // CallNonvirtualByteMethodV
2689 _Jv_JNI_CallAnyMethodA<jbyte, nonvirtual>, // CallNonvirtualByteMethodA
2690 _Jv_JNI_CallAnyMethod<jchar, nonvirtual>, // CallNonvirtualCharMethod
2691 _Jv_JNI_CallAnyMethodV<jchar, nonvirtual>, // CallNonvirtualCharMethodV
2692 _Jv_JNI_CallAnyMethodA<jchar, nonvirtual>, // CallNonvirtualCharMethodA
2693 _Jv_JNI_CallAnyMethod<jshort, nonvirtual>, // CallNonvirtualShortMethod
2694 _Jv_JNI_CallAnyMethodV<jshort, nonvirtual>, // CallNonvirtualShortMethodV
2695 _Jv_JNI_CallAnyMethodA<jshort, nonvirtual>, // CallNonvirtualShortMethodA
2696 _Jv_JNI_CallAnyMethod<jint, nonvirtual>, // CallNonvirtualIntMethod
2697 _Jv_JNI_CallAnyMethodV<jint, nonvirtual>, // CallNonvirtualIntMethodV
2698 _Jv_JNI_CallAnyMethodA<jint, nonvirtual>, // CallNonvirtualIntMethodA
2699 _Jv_JNI_CallAnyMethod<jlong, nonvirtual>, // CallNonvirtualLongMethod
2700 _Jv_JNI_CallAnyMethodV<jlong, nonvirtual>, // CallNonvirtualLongMethodV
2701 _Jv_JNI_CallAnyMethodA<jlong, nonvirtual>, // CallNonvirtualLongMethodA
2702 _Jv_JNI_CallAnyMethod<jfloat, nonvirtual>, // CallNonvirtualFloatMethod
2703 _Jv_JNI_CallAnyMethodV<jfloat, nonvirtual>, // CallNonvirtualFloatMethodV
2704 _Jv_JNI_CallAnyMethodA<jfloat, nonvirtual>, // CallNonvirtualFloatMethodA
2705 _Jv_JNI_CallAnyMethod<jdouble, nonvirtual>, // CallNonvirtualDoubleMethod
2706 _Jv_JNI_CallAnyMethodV<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodV
2707 _Jv_JNI_CallAnyMethodA<jdouble, nonvirtual>, // CallNonvirtualDoubleMethodA
2708 _Jv_JNI_CallAnyVoidMethod<nonvirtual>, // CallNonvirtualVoidMethod
2709 _Jv_JNI_CallAnyVoidMethodV<nonvirtual>, // CallNonvirtualVoidMethodV
2710 _Jv_JNI_CallAnyVoidMethodA<nonvirtual>, // CallNonvirtualVoidMethodA
2711
2712 _Jv_JNI_GetAnyFieldID<false>, // GetFieldID
2713 _Jv_JNI_GetField<jobject>, // GetObjectField
2714 _Jv_JNI_GetField<jboolean>, // GetBooleanField
2715 _Jv_JNI_GetField<jbyte>, // GetByteField
2716 _Jv_JNI_GetField<jchar>, // GetCharField
2717 _Jv_JNI_GetField<jshort>, // GetShortField
2718 _Jv_JNI_GetField<jint>, // GetIntField
2719 _Jv_JNI_GetField<jlong>, // GetLongField
2720 _Jv_JNI_GetField<jfloat>, // GetFloatField
2721 _Jv_JNI_GetField<jdouble>, // GetDoubleField
2722 _Jv_JNI_SetField, // SetObjectField
2723 _Jv_JNI_SetField, // SetBooleanField
2724 _Jv_JNI_SetField, // SetByteField
2725 _Jv_JNI_SetField, // SetCharField
2726 _Jv_JNI_SetField, // SetShortField
2727 _Jv_JNI_SetField, // SetIntField
2728 _Jv_JNI_SetField, // SetLongField
2729 _Jv_JNI_SetField, // SetFloatField
2730 _Jv_JNI_SetField, // SetDoubleField
2731 _Jv_JNI_GetAnyMethodID<true>, // GetStaticMethodID
2732
2733 _Jv_JNI_CallStaticMethod<jobject>, // CallStaticObjectMethod
2734 _Jv_JNI_CallStaticMethodV<jobject>, // CallStaticObjectMethodV
2735 _Jv_JNI_CallStaticMethodA<jobject>, // CallStaticObjectMethodA
2736 _Jv_JNI_CallStaticMethod<jboolean>, // CallStaticBooleanMethod
2737 _Jv_JNI_CallStaticMethodV<jboolean>, // CallStaticBooleanMethodV
2738 _Jv_JNI_CallStaticMethodA<jboolean>, // CallStaticBooleanMethodA
2739 _Jv_JNI_CallStaticMethod<jbyte>, // CallStaticByteMethod
2740 _Jv_JNI_CallStaticMethodV<jbyte>, // CallStaticByteMethodV
2741 _Jv_JNI_CallStaticMethodA<jbyte>, // CallStaticByteMethodA
2742 _Jv_JNI_CallStaticMethod<jchar>, // CallStaticCharMethod
2743 _Jv_JNI_CallStaticMethodV<jchar>, // CallStaticCharMethodV
2744 _Jv_JNI_CallStaticMethodA<jchar>, // CallStaticCharMethodA
2745 _Jv_JNI_CallStaticMethod<jshort>, // CallStaticShortMethod
2746 _Jv_JNI_CallStaticMethodV<jshort>, // CallStaticShortMethodV
2747 _Jv_JNI_CallStaticMethodA<jshort>, // CallStaticShortMethodA
2748 _Jv_JNI_CallStaticMethod<jint>, // CallStaticIntMethod
2749 _Jv_JNI_CallStaticMethodV<jint>, // CallStaticIntMethodV
2750 _Jv_JNI_CallStaticMethodA<jint>, // CallStaticIntMethodA
2751 _Jv_JNI_CallStaticMethod<jlong>, // CallStaticLongMethod
2752 _Jv_JNI_CallStaticMethodV<jlong>, // CallStaticLongMethodV
2753 _Jv_JNI_CallStaticMethodA<jlong>, // CallStaticLongMethodA
2754 _Jv_JNI_CallStaticMethod<jfloat>, // CallStaticFloatMethod
2755 _Jv_JNI_CallStaticMethodV<jfloat>, // CallStaticFloatMethodV
2756 _Jv_JNI_CallStaticMethodA<jfloat>, // CallStaticFloatMethodA
2757 _Jv_JNI_CallStaticMethod<jdouble>, // CallStaticDoubleMethod
2758 _Jv_JNI_CallStaticMethodV<jdouble>, // CallStaticDoubleMethodV
2759 _Jv_JNI_CallStaticMethodA<jdouble>, // CallStaticDoubleMethodA
2760 _Jv_JNI_CallStaticVoidMethod, // CallStaticVoidMethod
2761 _Jv_JNI_CallStaticVoidMethodV, // CallStaticVoidMethodV
2762 _Jv_JNI_CallStaticVoidMethodA, // CallStaticVoidMethodA
2763
2764 _Jv_JNI_GetAnyFieldID<true>, // GetStaticFieldID
2765 _Jv_JNI_GetStaticField<jobject>, // GetStaticObjectField
2766 _Jv_JNI_GetStaticField<jboolean>, // GetStaticBooleanField
2767 _Jv_JNI_GetStaticField<jbyte>, // GetStaticByteField
2768 _Jv_JNI_GetStaticField<jchar>, // GetStaticCharField
2769 _Jv_JNI_GetStaticField<jshort>, // GetStaticShortField
2770 _Jv_JNI_GetStaticField<jint>, // GetStaticIntField
2771 _Jv_JNI_GetStaticField<jlong>, // GetStaticLongField
2772 _Jv_JNI_GetStaticField<jfloat>, // GetStaticFloatField
2773 _Jv_JNI_GetStaticField<jdouble>, // GetStaticDoubleField
2774 _Jv_JNI_SetStaticField, // SetStaticObjectField
2775 _Jv_JNI_SetStaticField, // SetStaticBooleanField
2776 _Jv_JNI_SetStaticField, // SetStaticByteField
2777 _Jv_JNI_SetStaticField, // SetStaticCharField
2778 _Jv_JNI_SetStaticField, // SetStaticShortField
2779 _Jv_JNI_SetStaticField, // SetStaticIntField
2780 _Jv_JNI_SetStaticField, // SetStaticLongField
2781 _Jv_JNI_SetStaticField, // SetStaticFloatField
2782 _Jv_JNI_SetStaticField, // SetStaticDoubleField
2783 _Jv_JNI_NewString, // NewString
2784 _Jv_JNI_GetStringLength, // GetStringLength
2785 _Jv_JNI_GetStringChars, // GetStringChars
2786 _Jv_JNI_ReleaseStringChars, // ReleaseStringChars
2787 _Jv_JNI_NewStringUTF, // NewStringUTF
2788 _Jv_JNI_GetStringUTFLength, // GetStringUTFLength
370b2564 2789 _Jv_JNI_GetStringUTFChars, // GetStringUTFChars
65422ec5
TT
2790 _Jv_JNI_ReleaseStringUTFChars, // ReleaseStringUTFChars
2791 _Jv_JNI_GetArrayLength, // GetArrayLength
2792 _Jv_JNI_NewObjectArray, // NewObjectArray
2793 _Jv_JNI_GetObjectArrayElement, // GetObjectArrayElement
2794 _Jv_JNI_SetObjectArrayElement, // SetObjectArrayElement
8a922095 2795 _Jv_JNI_NewPrimitiveArray<jboolean, JvPrimClass (boolean)>,
65422ec5
TT
2796 // NewBooleanArray
2797 _Jv_JNI_NewPrimitiveArray<jbyte, JvPrimClass (byte)>, // NewByteArray
2798 _Jv_JNI_NewPrimitiveArray<jchar, JvPrimClass (char)>, // NewCharArray
2799 _Jv_JNI_NewPrimitiveArray<jshort, JvPrimClass (short)>, // NewShortArray
2800 _Jv_JNI_NewPrimitiveArray<jint, JvPrimClass (int)>, // NewIntArray
2801 _Jv_JNI_NewPrimitiveArray<jlong, JvPrimClass (long)>, // NewLongArray
2802 _Jv_JNI_NewPrimitiveArray<jfloat, JvPrimClass (float)>, // NewFloatArray
2803 _Jv_JNI_NewPrimitiveArray<jdouble, JvPrimClass (double)>, // NewDoubleArray
e976ed37
AH
2804 _Jv_JNI_GetPrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2805 // GetBooleanArrayElements
2806 _Jv_JNI_GetPrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2807 // GetByteArrayElements
2808 _Jv_JNI_GetPrimitiveArrayElements<jchar, JvPrimClass (char)>,
2809 // GetCharArrayElements
2810 _Jv_JNI_GetPrimitiveArrayElements<jshort, JvPrimClass (short)>,
2811 // GetShortArrayElements
2812 _Jv_JNI_GetPrimitiveArrayElements<jint, JvPrimClass (int)>,
2813 // GetIntArrayElements
2814 _Jv_JNI_GetPrimitiveArrayElements<jlong, JvPrimClass (long)>,
2815 // GetLongArrayElements
2816 _Jv_JNI_GetPrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2817 // GetFloatArrayElements
2818 _Jv_JNI_GetPrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2819 // GetDoubleArrayElements
2820 _Jv_JNI_ReleasePrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
2821 // ReleaseBooleanArrayElements
2822 _Jv_JNI_ReleasePrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
2823 // ReleaseByteArrayElements
2824 _Jv_JNI_ReleasePrimitiveArrayElements<jchar, JvPrimClass (char)>,
2825 // ReleaseCharArrayElements
2826 _Jv_JNI_ReleasePrimitiveArrayElements<jshort, JvPrimClass (short)>,
2827 // ReleaseShortArrayElements
2828 _Jv_JNI_ReleasePrimitiveArrayElements<jint, JvPrimClass (int)>,
2829 // ReleaseIntArrayElements
2830 _Jv_JNI_ReleasePrimitiveArrayElements<jlong, JvPrimClass (long)>,
2831 // ReleaseLongArrayElements
2832 _Jv_JNI_ReleasePrimitiveArrayElements<jfloat, JvPrimClass (float)>,
2833 // ReleaseFloatArrayElements
2834 _Jv_JNI_ReleasePrimitiveArrayElements<jdouble, JvPrimClass (double)>,
2835 // ReleaseDoubleArrayElements
2836 _Jv_JNI_GetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2837 // GetBooleanArrayRegion
2838 _Jv_JNI_GetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2839 // GetByteArrayRegion
2840 _Jv_JNI_GetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2841 // GetCharArrayRegion
2842 _Jv_JNI_GetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2843 // GetShortArrayRegion
2844 _Jv_JNI_GetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2845 // GetIntArrayRegion
2846 _Jv_JNI_GetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2847 // GetLongArrayRegion
2848 _Jv_JNI_GetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2849 // GetFloatArrayRegion
2850 _Jv_JNI_GetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2851 // GetDoubleArrayRegion
2852 _Jv_JNI_SetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
2853 // SetBooleanArrayRegion
2854 _Jv_JNI_SetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
2855 // SetByteArrayRegion
2856 _Jv_JNI_SetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
2857 // SetCharArrayRegion
2858 _Jv_JNI_SetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
2859 // SetShortArrayRegion
2860 _Jv_JNI_SetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
2861 // SetIntArrayRegion
2862 _Jv_JNI_SetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
2863 // SetLongArrayRegion
2864 _Jv_JNI_SetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
2865 // SetFloatArrayRegion
2866 _Jv_JNI_SetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
2867 // SetDoubleArrayRegion
65422ec5
TT
2868 _Jv_JNI_RegisterNatives, // RegisterNatives
2869 _Jv_JNI_UnregisterNatives, // UnregisterNatives
2870 _Jv_JNI_MonitorEnter, // MonitorEnter
2871 _Jv_JNI_MonitorExit, // MonitorExit
2872 _Jv_JNI_GetJavaVM, // GetJavaVM
2873
2874 _Jv_JNI_GetStringRegion, // GetStringRegion
2875 _Jv_JNI_GetStringUTFRegion, // GetStringUTFRegion
2876 _Jv_JNI_GetPrimitiveArrayCritical, // GetPrimitiveArrayCritical
2877 _Jv_JNI_ReleasePrimitiveArrayCritical, // ReleasePrimitiveArrayCritical
2878 _Jv_JNI_GetStringCritical, // GetStringCritical
2879 _Jv_JNI_ReleaseStringCritical, // ReleaseStringCritical
355dff4c 2880
819138ac
TT
2881 _Jv_JNI_NewWeakGlobalRef, // NewWeakGlobalRef
2882 _Jv_JNI_DeleteWeakGlobalRef, // DeleteWeakGlobalRef
355dff4c 2883
880f8c16
TT
2884 _Jv_JNI_ExceptionCheck, // ExceptionCheck
2885
2886 _Jv_JNI_NewDirectByteBuffer, // NewDirectByteBuffer
2887 _Jv_JNI_GetDirectBufferAddress, // GetDirectBufferAddress
e0441a5b
MK
2888 _Jv_JNI_GetDirectBufferCapacity, // GetDirectBufferCapacity
2889
2890 _Jv_JNI_GetObjectRefType // GetObjectRefType
ee9dd372 2891};
aaf0766e 2892
f06a83c0 2893struct JNIInvokeInterface_ _Jv_JNI_InvokeFunctions =
aaf0766e
TT
2894{
2895 RESERVED,
2896 RESERVED,
2897 RESERVED,
2898
2899 _Jv_JNI_DestroyJavaVM,
2900 _Jv_JNI_AttachCurrentThread,
2901 _Jv_JNI_DetachCurrentThread,
880f8c16
TT
2902 _Jv_JNI_GetEnv,
2903 _Jv_JNI_AttachCurrentThreadAsDaemon
aaf0766e 2904};