-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_jni_export_old.nim
68 lines (53 loc) · 2 KB
/
test_jni_export_old.nim
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
import ../jnim/private / [ jni_api, jni_generator, jni_export_old ],
./common,
unittest
suite "jni_export_old":
setup:
if not isJNIThreadInitialized():
initJNIForTests()
test "Make proxy":
jclassDef io.github.yglukhov.jnim.ExportTestClass$Interface of JVMObject
jclass io.github.yglukhov.jnim.ExportTestClass$Tester of JVMObject:
proc new
proc callVoidMethod(r: Interface)
type MyObj = ref object of RootObj
a: int
var mr = MyObj.new()
mr.a = 1
proc handler(env: pointer, o: RootRef, proxiedThis, meth: jobject, args: jobjectArray): jobject {.cdecl.} =
let mr = cast[MyObj](o)
inc mr.a
let runnableClazz = Interface.getJVMClassForType()
for i in 0 .. 3:
let pr = makeProxy(runnableClazz.get, mr, handler)
let tr = Tester.new()
tr.callVoidMethod(Interface.fromJObject(pr))
check: mr.a == 5
test "Implement dispatcher":
jclassDef io.github.yglukhov.jnim.ExportTestClass$Interface of JVMObject
jclass io.github.yglukhov.jnim.ExportTestClass$Tester of JVMObject:
proc new
proc callVoidMethod(r: Interface)
proc callIntMethod(r: Interface): jint
proc callStringMethod(r: Interface): string
proc callStringMethodWithArgs(r: Interface, s: string, i: jint): string
type MyObj = ref object of RootObj
a: int
implementDispatcher(MyObj, MyObj_dispatcher):
proc voidMethod(self: MyObj) =
inc self.a
proc intMethod(self: MyObj): jint =
return 123
proc stringMethod(self: MyObj): string =
return "123"
proc stringMethodWithArgs(self: MyObj, s: string, i: jint): string =
return "123" & $i & s
let mr = MyObj.new()
mr.a = 5
let pr = makeProxy(Interface, mr, MyObj_dispatcher)
let tr = Tester.new()
tr.callVoidMethod(pr)
check: mr.a == 6
check: tr.callIntMethod(pr) == 123
check: tr.callStringMethod(pr) == "123"
check: tr.callStringMethodWithArgs(pr, "789", 456) == "123456789"