-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_java.c
896 lines (804 loc) · 23.5 KB
/
lua_java.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <classfile_constants.h>
#include "myjni.h"
#include "jni_util.h"
#include "lua_interface.h"
#include "lua_java.h"
#include "java_bridge.h"
#include "lj_internal.h"
/* _ _ _ _ _ */
/* | | | | | (_) | */
/* | | | | |_ _| |___ */
/* | | | | __| | / __| */
/* | |__| | |_| | \__ \ */
/* \____/ \__|_|_|___/ */
static jvmtiEnv *lj_jvmti; /* accessed through current_jvmti() */
jvmtiError lj_err; /* accessed directly externally */
JavaVM *lj_jvm;
/* TODO: is this used for anything */
jthread lj_current_thread;
/* needed to have a lua state at jvmti callback */
lua_State *lj_L;
JNIEnv *current_jni()
{
JNIEnv *jni;
jint ret = (*lj_jvm)->AttachCurrentThread(lj_jvm, (void**)&jni, NULL);
assert(ret == JNI_OK);
return jni;
}
jvmtiEnv *current_jvmti()
{
return lj_jvmti;
}
/* TODO: should wrap this in a macro to better report error location */
void lj_check_jvmti_error_internal(lua_State *L, const char *file, int line, const char *func)
{
char *errmsg = "<Unknown Error>";
if(lj_err == JVMTI_ERROR_NONE)
return;
(*lj_jvmti)->GetErrorName(lj_jvmti, lj_err, &errmsg);
/* we never Deallocate() the errmsg returned from JVMTI */
if (IsDebuggerPresent())
DebugBreak();
(void) lua_interface_error(L, "[%s:%d:%s] Error %d from JVMTI: %s", file, line, func, lj_err, errmsg);
}
jobject get_current_java_thread()
{
JNIEnv *jni = current_jni();
jclass thread_class;
jmethodID getCurrentThread_method_id;
jobject current_thread;
if (lj_current_thread)
return lj_current_thread;
thread_class = (*jni)->FindClass(jni, "java/lang/Thread");
EXCEPTION_CHECK(jni);
assert(thread_class);
getCurrentThread_method_id = (*jni)->GetStaticMethodID(jni, thread_class,
"currentThread",
"()Ljava/lang/Thread;");
EXCEPTION_CHECK(jni);
assert(getCurrentThread_method_id);
current_thread = (*jni)->CallStaticObjectMethod(jni, thread_class,
getCurrentThread_method_id);
EXCEPTION_CHECK(jni);
assert(current_thread);
return current_thread;
}
/* _ ______ _ _ */
/* | | | ____| | | (_) */
/* | | _ _ __ _ | |__ _ _ _ __ ___| |_ _ ___ _ __ ___ */
/* | | | | | |/ _` | | __| | | | '_ \ / __| __| |/ _ \| '_ \/ __| */
/* | |___| |_| | (_| | | | | |_| | | | | (__| |_| | (_) | | | \__ \ */
/* |______\__,_|\__,_| |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ */
static int lj_set_breakpoint(lua_State *L)
{
jmethodID method_id;
jlocation location;
method_id = *(jmethodID *)luaL_checkudata(L, 1, "jmethod_id");
location = luaL_checkinteger(L, 2);
lua_pop(L, 2);
lj_err = (*lj_jvmti)->SetBreakpoint(lj_jvmti, method_id, location);
lj_check_jvmti_error(L);
return 0;
}
static int lj_clear_breakpoint(lua_State *L)
{
jmethodID method_id;
jlocation location;
method_id = *(jmethodID *)luaL_checkudata(L, 1, "jmethod_id");
location = luaL_checkinteger(L, 2);
lua_pop(L, 2);
lj_err = (*lj_jvmti)->ClearBreakpoint(lj_jvmti, method_id, location);
lj_check_jvmti_error(L);
return 0;
}
/**
* Convenience function to see if we can return a nil
* instead of throwing an error.
*/
static int local_variable_is_nil(jvmtiError err)
{
if (err == JVMTI_ERROR_INVALID_SLOT)
return 1;
else if (err == JVMTI_ERROR_TYPE_MISMATCH)
return 1;
else if (err == JVMTI_ERROR_NULL_POINTER)
return 1;
else
return 0;
}
static int lj_get_local_variable(lua_State *L)
{
jint depth;
jint slot;
const char *type;
jint val_i;
jlong val_j;
jfloat val_f;
jdouble val_d;
jobject val_l;
depth = luaL_checkinteger(L, 1);
slot = luaL_checkinteger(L, 2);
type = luaL_checkstring(L, 3);
lua_pop(L, 3);
if (!strcmp(type, "Z") ||
!strcmp(type, "B") ||
!strcmp(type, "C") ||
!strcmp(type, "S") ||
!strcmp(type, "I"))
{
lj_err = (*lj_jvmti)->GetLocalInt(lj_jvmti, get_current_java_thread(),
depth-1, slot, &val_i);
if (local_variable_is_nil(lj_err))
{
lua_pushnil(L);
}
else
{
lj_check_jvmti_error(L);
if (!strcmp(type, "Z"))
lua_pushboolean(L, val_i);
else
lua_pushinteger(L, val_i);
}
}
else if (!strcmp(type, "J"))
{
lj_err = (*lj_jvmti)->GetLocalLong(lj_jvmti, get_current_java_thread(),
depth-1, slot, &val_j);
if (local_variable_is_nil(lj_err))
{
lua_pushnil(L);
}
else
{
lj_check_jvmti_error(L);
lua_pushinteger(L, val_j);
}
}
else if (!strcmp(type, "F"))
{
lj_err = (*lj_jvmti)->GetLocalFloat(lj_jvmti, get_current_java_thread(),
depth-1, slot, &val_f);
if (local_variable_is_nil(lj_err))
{
lua_pushnil(L);
}
else
{
lj_check_jvmti_error(L);
lua_pushnumber(L, val_f);
}
}
else if (!strcmp(type, "D"))
{
lj_err = (*lj_jvmti)->GetLocalDouble(lj_jvmti, get_current_java_thread(),
depth-1, slot, &val_d);
if (local_variable_is_nil(lj_err))
{
lua_pushnil(L);
}
else
{
lj_check_jvmti_error(L);
lua_pushnumber(L, val_d);
}
}
else if (*type == 'L' || *type == '[')
{
/* GetLocalInstance() is new to JVMTI 1.2 */
/* if (slot == 0) */
/* lj_err = (*lj_jvmti)->GetLocalInstance(lj_jvmti, get_current_java_thread(), depth-1, &val_l); */
/* else */
lj_err = (*lj_jvmti)->GetLocalObject(lj_jvmti, get_current_java_thread(),
depth-1, slot, &val_l);
if (local_variable_is_nil(lj_err))
{
lua_pushnil(L);
}
else
{
lj_check_jvmti_error(L);
new_jobject(L, val_l);
}
}
else
{
lua_pushnil(L);
}
return 1;
}
static int lj_pointer_to_string(lua_State *L)
{
char buf[20];
const void *p;
p = lua_topointer(L, -1);
lua_pop(L, 1);
sprintf(buf, "%p", *(jobject *)p);
lua_pushstring(L, buf);
return 1;
}
/**
* Calling a Java method requires the following parameters:
* - object - target of method call
* - method id
* - return type - I, STR, Ljava/lang/Object;, etc
* - arg count - number of arguments
* - arguments(varargs...) - pairs of type/arg
*/
static int lj_call_method(lua_State *L)
{
JNIEnv *jni = current_jni();
jobject object;
jmethodID method_id;
const char *ret;
jvalue val;
int argcount;
int i;
int is_static;
int param_num;
int result_count = 1;
jvalue *jargs = NULL;
const char *argtype;
char *method_name;
object = *(jobject *)luaL_checkudata(L, 1, "jobject");
method_id = *(jmethodID *)luaL_checkudata(L, 2, "jmethod_id");
is_static = lua_toboolean(L, 3);
ret = luaL_checkstring(L, 4);
argcount = luaL_checkinteger(L, 5);
lj_err = (*lj_jvmti)->GetMethodName(lj_jvmti, method_id, &method_name, NULL, NULL);
lj_check_jvmti_error(L);
if (argcount > 0)
{
jargs = malloc(sizeof(jvalue) * argcount);
memset(jargs, 0, sizeof(jvalue) * argcount);
}
param_num = 6;
/* get arguments */
for (i = 0; i < argcount; ++i)
{
argtype = luaL_checkstring(L, param_num++);
assert(argtype);
if (!strcmp("V", argtype))
{
jargs[i].l = NULL;
param_num++; /* skip the nil */
}
else if ('L' == argtype[0] || '[' == argtype[0])
{
jargs[i].l = *(jobject *)luaL_checkudata(L, param_num++, "jobject");
}
else if (!strcmp("STR", argtype)) /* TODO non-standard indicator */
{
jargs[i].l = (*jni)->NewStringUTF(jni, luaL_checkstring(L, param_num++));
}
else if (!strcmp("Z", argtype))
{
luaL_checktype(L, param_num, LUA_TBOOLEAN);
jargs[i].z = lua_toboolean(L, param_num);
param_num++;
}
else if (!strcmp("B", argtype))
{
jargs[i].b = luaL_checkinteger(L, param_num++);
}
else if (!strcmp("C", argtype)) /* TODO should this be a one-element string? (not int) */
{
jargs[i].c = luaL_checkinteger(L, param_num++);
}
else if (!strcmp("S", argtype))
{
jargs[i].s = luaL_checkinteger(L, param_num++);
}
else if (!strcmp("I", argtype))
{
jargs[i].i = luaL_checkinteger(L, param_num++);
}
else if (!strcmp("J", argtype))
{
jargs[i].j = luaL_checkinteger(L, param_num++);
}
else if (!strcmp("F", argtype))
{
jargs[i].f = luaL_checknumber(L, param_num++);
}
else if (!strcmp("D", argtype))
{
jargs[i].d = luaL_checknumber(L, param_num++);
}
else
{
(void)lua_interface_error(L, "Unknown argument type '%s' for argument %d\n", argtype, i);
}
}
lua_pop(L, (2 * argcount) + 5);
memset(&val, 0, sizeof(val));
/* call method - in order shown in JNI docs*/
if (!strcmp("V", ret))
{
if (is_static)
(*jni)->CallStaticVoidMethodA(jni, object, method_id, jargs);
else
(*jni)->CallVoidMethodA(jni, object, method_id, jargs);
EXCEPTION_CHECK(jni);
result_count = 0;
}
else if (ret[0] == 'L' || ret[0] == '[' || !strcmp("STR", ret))
{
if (!strcmp("<init>", method_name))
val.l = (*jni)->NewObjectA(jni, object, method_id, jargs);
else if (is_static)
val.l = (*jni)->CallStaticObjectMethodA(jni, object, method_id, jargs);
else
val.l = (*jni)->CallObjectMethodA(jni, object, method_id, jargs);
EXCEPTION_CHECK(jni);
if (!strcmp("L", ret) || ret[0] == '[')
new_jobject(L, val.l);
else
new_string(L, jni, val.l);
}
else if (!strcmp("Z", ret))
{
if (is_static)
val.z = (*jni)->CallStaticBooleanMethodA(jni, object, method_id, jargs);
else
val.z = (*jni)->CallBooleanMethodA(jni, object, method_id, jargs);
EXCEPTION_CHECK(jni);
lua_pushboolean(L, val.z);
}
else if (!strcmp("B", ret))
{
if (is_static)
val.b = (*jni)->CallStaticByteMethodA(jni, object, method_id, jargs);
else
val.b = (*jni)->CallByteMethodA(jni, object, method_id, jargs);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.b);
}
else if (!strcmp("C", ret))
{
if (is_static)
val.c = (*jni)->CallStaticCharMethodA(jni, object, method_id, jargs);
else
val.c = (*jni)->CallCharMethodA(jni, object, method_id, jargs);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.c);
}
else if (!strcmp("S", ret))
{
if (is_static)
val.s = (*jni)->CallStaticShortMethodA(jni, object, method_id, jargs);
else
val.s = (*jni)->CallShortMethodA(jni, object, method_id, jargs);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.s);
}
else if (!strcmp("I", ret))
{
if (is_static)
val.i = (*jni)->CallStaticIntMethodA(jni, object, method_id, jargs);
else
val.i = (*jni)->CallIntMethodA(jni, object, method_id, jargs);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.i);
}
else if (!strcmp("J", ret))
{
if (is_static)
val.j = (*jni)->CallStaticLongMethodA(jni, object, method_id, jargs);
else
val.j = (*jni)->CallLongMethodA(jni, object, method_id, jargs);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.j);
}
else if (!strcmp("F", ret))
{
if (is_static)
val.f = (*jni)->CallStaticFloatMethodA(jni, object, method_id, jargs);
else
val.f = (*jni)->CallFloatMethodA(jni, object, method_id, jargs);
EXCEPTION_CHECK(jni);
lua_pushnumber(L, val.f);
}
else if (!strcmp("D", ret))
{
if (is_static)
val.d = (*jni)->CallStaticDoubleMethodA(jni, object, method_id, jargs);
else
val.d = (*jni)->CallDoubleMethodA(jni, object, method_id, jargs);
EXCEPTION_CHECK(jni);
lua_pushnumber(L, val.d);
}
else
{
(void)lua_interface_error(L, "Unknown return type '%s'", ret);
}
if (argcount > 0)
free(jargs);
free_jvmti_refs(lj_jvmti, method_name, (void *)-1);
return result_count;
}
static int lj_toString(lua_State *L)
{
JNIEnv *jni = current_jni();
jobject object;
jclass class;
jmethodID method_id;
jstring string;
object = *(jobject *)luaL_checkudata(L, 1, "jobject");
lua_pop(L, 1);
class = (*jni)->FindClass(jni, "java/lang/Object");
EXCEPTION_CHECK(jni);
if (class == NULL)
{
lua_pushnil(L);
return 1;
}
method_id = (*jni)->GetMethodID(jni, class, "toString", "()Ljava/lang/String;");
EXCEPTION_CHECK(jni);
if (method_id == NULL)
{
lua_pushnil(L);
return 1;
}
string = (jstring)(*jni)->CallObjectMethod(jni, object, method_id);
EXCEPTION_CHECK(jni);
if (string == NULL)
{
lua_pushnil(L);
return 1;
}
new_string(L, jni, string);
return 1;
}
static int lj_get_field(lua_State *L)
{
JNIEnv *jni = current_jni();
jvalue val;
jobject object;
lj_field_id *field_id;
char *sig;
int is_static;
object = *(jobject *)luaL_checkudata(L, 1, "jobject");
field_id = (lj_field_id *)luaL_checkudata(L, 2, "jfield_id");
luaL_checktype(L, 3, LUA_TBOOLEAN);
is_static = lua_toboolean(L, -1);
lua_pop(L, 3);
lj_err = (*lj_jvmti)->GetFieldName(lj_jvmti, field_id->class, field_id->field_id,
NULL, &sig, NULL);
lj_check_jvmti_error(L);
if (*sig == 'L' || *sig == '[')
{
if (is_static)
val.l = (*jni)->GetStaticObjectField(jni, object, field_id->field_id);
else
val.l = (*jni)->GetObjectField(jni, object, field_id->field_id);
EXCEPTION_CHECK(jni);
new_jobject(L, val.l);
}
else if (!strcmp(sig, "Z"))
{
if (is_static)
val.z = (*jni)->GetStaticBooleanField(jni, object, field_id->field_id);
else
val.z = (*jni)->GetBooleanField(jni, object, field_id->field_id);
EXCEPTION_CHECK(jni);
lua_pushboolean(L, val.z);
}
else if (!strcmp(sig, "B"))
{
if (is_static)
val.b = (*jni)->GetStaticByteField(jni, object, field_id->field_id);
else
val.b = (*jni)->GetByteField(jni, object, field_id->field_id);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.b);
}
else if (!strcmp(sig, "C"))
{
if (is_static)
val.c = (*jni)->GetStaticCharField(jni, object, field_id->field_id);
else
val.c = (*jni)->GetCharField(jni, object, field_id->field_id);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.c);
}
else if (!strcmp(sig, "S"))
{
if (is_static)
val.s = (*jni)->GetStaticShortField(jni, object, field_id->field_id);
else
val.s = (*jni)->GetShortField(jni, object, field_id->field_id);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.s);
}
else if (!strcmp(sig, "I"))
{
if (is_static)
val.i = (*jni)->GetStaticIntField(jni, object, field_id->field_id);
else
val.i = (*jni)->GetIntField(jni, object, field_id->field_id);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.i);
}
else if (!strcmp(sig, "J"))
{
if (is_static)
val.j = (*jni)->GetStaticLongField(jni, object, field_id->field_id);
else
val.j = (*jni)->GetLongField(jni, object, field_id->field_id);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.j);
}
else if (!strcmp(sig, "F"))
{
if (is_static)
val.f = (*jni)->GetStaticFloatField(jni, object, field_id->field_id);
else
val.f = (*jni)->GetFloatField(jni, object, field_id->field_id);
EXCEPTION_CHECK(jni);
lua_pushnumber(L, val.f);
}
else if (!strcmp(sig, "D"))
{
if (is_static)
val.d = (*jni)->GetStaticDoubleField(jni, object, field_id->field_id);
else
val.d = (*jni)->GetDoubleField(jni, object, field_id->field_id);
EXCEPTION_CHECK(jni);
lua_pushnumber(L, val.d);
}
else
{
lj_print_message("Unknown return type '%s' for field\n", sig);
lua_pushnil(L);
}
return 1;
}
static int lj_get_current_thread(lua_State *L)
{
jthread thread;
/* JVMTI seems to be returning the agent thread, even though it says it won't */
/* lj_err = (*lj_jvmti)->GetCurrentThread(lj_jvmti, &thread); */
/* lj_check_jvmti_error(L); */
thread = get_current_java_thread();
new_jobject(L, thread);
return 1;
}
static int lj_get_all_threads(lua_State *L)
{
jint threads_count;
jthread *threads;
int i;
lj_err = (*lj_jvmti)->GetAllThreads(lj_jvmti, &threads_count, &threads);
lj_check_jvmti_error(L);
lua_newtable(L);
for (i = 0; i < threads_count; ++i)
{
new_jobject(L, threads[i]);
lua_rawseti(L, -2, i+1);
}
free_jvmti_refs(lj_jvmti, threads, (void *)-1);
return 1;
}
static int lj_get_array_length(lua_State *L)
{
JNIEnv *jni = current_jni();
jobject array = *(jobject *)luaL_checkudata(L, 1, "jobject");
jsize length = (*jni)->GetArrayLength(jni, array);
EXCEPTION_CHECK(jni);
lua_pop(L, 1);
lua_pushinteger(L, length);
return 1;
}
static int lj_get_array_element(lua_State *L)
{
JNIEnv *jni = current_jni();
jobject array = *(jobject *)luaL_checkudata(L, 1, "jobject");
const char *class_name = luaL_checkstring(L, 2);
jsize index = luaL_checkinteger(L, 3) - 1;
jvalue val;
lua_pop(L, 3);
assert(*class_name == '[');
switch (class_name[1])
{
case 'L':
case '[':
val.l = (*jni)->GetObjectArrayElement(jni, array, index);
EXCEPTION_CHECK(jni);
new_jobject(L, val.l);
break;
case 'Z':
(*jni)->GetBooleanArrayRegion(jni, array, index, 1, &val.z);
EXCEPTION_CHECK(jni);
lua_pushboolean(L, val.z);
break;
case 'B':
(*jni)->GetByteArrayRegion(jni, array, index, 1, &val.b);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.b);
break;
case 'C':
(*jni)->GetCharArrayRegion(jni, array, index, 1, &val.c);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.c);
break;
case 'S':
(*jni)->GetShortArrayRegion(jni, array, index, 1, &val.s);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.s);
break;
case 'I':
(*jni)->GetIntArrayRegion(jni, array, index, 1, &val.i);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.i);
break;
case 'J':
(*jni)->GetLongArrayRegion(jni, array, index, 1, &val.j);
EXCEPTION_CHECK(jni);
lua_pushinteger(L, val.j);
break;
case 'F':
(*jni)->GetFloatArrayRegion(jni, array, index, 1, &val.f);
EXCEPTION_CHECK(jni);
lua_pushnumber(L, val.f);
break;
case 'D':
(*jni)->GetDoubleArrayRegion(jni, array, index, 1, &val.d);
EXCEPTION_CHECK(jni);
lua_pushnumber(L, val.d);
break;
default:
(void)lua_interface_error(L, "Unknown array class: %s\n", class_name);
}
return 1;
}
static int lj_set_array_element(lua_State *L)
{
JNIEnv *jni = current_jni();
jobject array = *(jobject *) luaL_checkudata(L, 1, "jobject");
const char *class_name = luaL_checkstring(L, 2);
jsize index = luaL_checkinteger(L, 3) - 1;
jvalue val;
int val_num = 4;
assert(*class_name == '[');
switch (class_name[1])
{
case 'L':
case '[':
if (lua_isnil(L, val_num)) {
val.l = NULL;
} else {
val.l = *(jobject *) luaL_checkudata(L, val_num, "jobject");
}
(*jni)->SetObjectArrayElement(jni, array, index, val.l);
EXCEPTION_CHECK(jni);
break;
/* TODO rest */
case 'Z':
luaL_checktype(L, val_num, LUA_TBOOLEAN);
val.z = lua_toboolean(L, val_num);
(*jni)->SetBooleanArrayRegion(jni, array, index, 1, &val.z);
EXCEPTION_CHECK(jni);
break;
case 'B':
val.b = luaL_checkinteger(L, val_num);
(*jni)->SetByteArrayRegion(jni, array, index, 1, &val.b);
EXCEPTION_CHECK(jni);
break;
case 'C':
val.c = luaL_checkinteger(L, val_num);
(*jni)->SetCharArrayRegion(jni, array, index, 1, &val.c);
EXCEPTION_CHECK(jni);
break;
case 'S':
val.s = luaL_checkinteger(L, val_num);
(*jni)->SetShortArrayRegion(jni, array, index, 1, &val.s);
EXCEPTION_CHECK(jni);
break;
case 'I':
val.i = luaL_checkinteger(L, val_num);
(*jni)->SetIntArrayRegion(jni, array, index, 1, &val.i);
EXCEPTION_CHECK(jni);
break;
case 'J':
val.j = luaL_checkinteger(L, val_num);
(*jni)->SetLongArrayRegion(jni, array, index, 1, &val.j);
EXCEPTION_CHECK(jni);
break;
case 'F':
val.f = luaL_checknumber(L, val_num);
(*jni)->SetFloatArrayRegion(jni, array, index, 1, &val.f);
EXCEPTION_CHECK(jni);
break;
case 'D':
val.d = luaL_checknumber(L, val_num);
(*jni)->SetDoubleArrayRegion(jni, array, index, 1, &val.d);
EXCEPTION_CHECK(jni);
break;
}
lua_pop(L, 4);
return 0;
}
static int lj_new_global_ref(lua_State *L)
{
JNIEnv *jni = current_jni();
jobject object = *(jobject *)luaL_checkudata(L, 1, "jobject");
jobject global_ref = (*jni)->NewGlobalRef(jni, object);
EXCEPTION_CHECK(jni);
lua_pop(L, 1);
new_jobject(L, global_ref);
return 1;
}
static int lj_delete_global_ref(lua_State *L)
{
JNIEnv *jni = current_jni();
jobject object = *(jobject *)luaL_checkudata(L, 1, "jobject");
(*jni)->DeleteGlobalRef(jni, object);
EXCEPTION_CHECK(jni);
lua_pop(L, 1);
return 0;
}
/* _____ _____ */
/* /\ | __ \_ _| */
/* / \ | |__) || | */
/* / /\ \ | ___/ | | */
/* / ____ \| | _| |_ */
/* /_/ \_\_| |_____| *//* in more ways than one... */
/* from lua_jvmti_event.c */
int lj_set_jvmti_callback(lua_State *L);
int lj_clear_jvmti_callback(lua_State *L);
void lj_init_jvmti_event();
/* registration for subordinate .c files */
void lj_class_register(lua_State *L);
void lj_field_register(lua_State *L);
void lj_force_early_return_register(lua_State *L);
void lj_method_register(lua_State *L);
void lj_raw_monitor_register(lua_State *L);
void lj_stack_frame_register(lua_State *L);
void lj_watch_register(lua_State *L);
void lj_init(lua_State *L, JavaVM *jvm, jvmtiEnv *jvmti)
{
lj_L = L;
/* add C functions */
lj_class_register(L);
lj_field_register(L);
lj_force_early_return_register(L);
lj_method_register(L);
lj_raw_monitor_register(L);
lj_stack_frame_register(L);
lj_watch_register(L);
lua_register(L, "lj_set_breakpoint", lj_set_breakpoint);
lua_register(L, "lj_clear_breakpoint", lj_clear_breakpoint);
lua_register(L, "lj_get_local_variable", lj_get_local_variable);
lua_register(L, "lj_pointer_to_string", lj_pointer_to_string);
lua_register(L, "lj_call_method", lj_call_method);
lua_register(L, "lj_toString", lj_toString);
lua_register(L, "lj_get_field", lj_get_field);
lua_register(L, "lj_get_current_thread", lj_get_current_thread);
lua_register(L, "lj_get_all_threads", lj_get_all_threads);
lua_register(L, "lj_get_array_length", lj_get_array_length);
lua_register(L, "lj_get_array_element", lj_get_array_element);
lua_register(L, "lj_set_array_element", lj_set_array_element);
lua_register(L, "lj_new_global_ref", lj_new_global_ref);
lua_register(L, "lj_delete_global_ref", lj_delete_global_ref);
lua_register(L, "lj_set_jvmti_callback", lj_set_jvmti_callback);
lua_register(L, "lj_clear_jvmti_callback", lj_clear_jvmti_callback);
/* save pointers for global use */
lj_jvm = jvm;
lj_jvmti = jvmti;
lj_err = EV_ENABLET(BREAKPOINT, NULL);
lj_check_jvmti_error(L);
lj_init_jvmti_event();
}
void lj_print_message(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
fflush(stdout);
va_end(ap);
}