-
Notifications
You must be signed in to change notification settings - Fork 10
/
jni_hello.nim
55 lines (48 loc) · 2.04 KB
/
jni_hello.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
# Resources:
# - System.loadLibrary - hello-jni in Android samples, and also:
# https://github.com/skanti/Android-Manual-Build-Command-Line/blob/3fea20b3b52ac04cb0208a691529a89cfb2064c1/hello-jni/src/com/example/hellojni/HelloJNI.java#L25
# - <clinit> - http://mariokmk.github.io/programming/2015/03/06/learning-android-bytecode.html
{.experimental: "codeReordering".}
import src / dali
# Extended czak_hello.nim
const
Activity = "Landroid/app/Activity;"
HelloActivity = "Lcom/akavel/hello2/HelloActivity;"
Bundle = "Landroid/os/Bundle;"
TextView = "Landroid/widget/TextView;"
System = "Ljava/lang/System;"
String = "Ljava/lang/String;"
Context = "Landroid/content/Context;"
View = "Landroid/view/View;"
CharSequence = "Ljava/lang/CharSequence;"
var dex = newDex()
dex.classes.add:
dclass com.akavel.hello2.HelloActivity {.public.} of Activity:
proc `<clinit>`() {.static, constructor, regs:2, ins:0, outs:1.} =
# System.loadLibrary("hello-mello")
const_string(0, "hello-mello")
invoke_static(0, jproto System.loadLibrary(String))
return_void()
proc `<init>`() {.public, constructor, regs:1, ins:1, outs:1.} =
invoke_direct(0, jproto Activity.`<init>`())
return_void()
proc onCreate(Bundle) {.public, regs:4, ins:2, outs:2.} =
# ins: this, arg0
# super.onCreate(arg0)
invoke_super(2, 3, jproto Activity.onCreate(Bundle))
# v0 = new TextView(this)
new_instance(0, TextView)
invoke_direct(0, 2, jproto TextView.`<init>`(Context))
# v1 = this.stringFromJNI()
# NOTE: failure to call a Native function should result in
# java.lang.UnsatisfiedLinkError exception
invoke_virtual(2, jproto HelloActivity.stringFromJNI() -> String)
move_result_object(1)
# v0.setText(v1)
invoke_virtual(0, 1, jproto TextView.setText(CharSequence))
# this.setContentView(v0)
invoke_virtual(2, 0, jproto HelloActivity.setContentView(View))
# return
return_void()
proc stringFromJNI(): String {.public, native.}
stdout.write(dex.render)