-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
87 lines (75 loc) · 2.29 KB
/
lib.rs
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
#[macro_use] extern crate lazy_static;
extern crate jni;
#[macro_use] extern crate log;
extern crate android_logger;
use log::Level;
use android_logger::Config;
use jni::{
objects::{GlobalRef, JClass, JObject},
sys::jbyteArray,
JNIEnv, JavaVM,
};
use std::sync::Mutex;
use wasmer_runtime::{compile, func, imports, Ctx, ImportObject, Instance, Module};
lazy_static! {
static ref ENV: Mutex<Option<JavaVM>> = { Mutex::new(None) };
static ref CLASS: Mutex<Option<GlobalRef>> = { Mutex::new(None) };
}
#[no_mangle]
pub unsafe extern "C" fn Java_com_wasmer_android_MainActivity_JNIExecuteWasm(
env: JNIEnv,
_: JClass,
callback: JObject,
module_bytes: jbyteArray,
) {
android_logger::init_once(
Config::default().with_min_level(Level::Trace)
);
std::panic::set_hook(Box::new(|panic_info| {
error!("ERR: {}", panic_info.to_string());
}));
// build module
let module_bytes = env.convert_byte_array(module_bytes).unwrap();
let main_instance = load_module(&module_bytes);
// set global variables
let class = env.new_global_ref(callback).unwrap();
*CLASS.lock().unwrap() = Some(class);
let vm = env.get_java_vm().unwrap();
*ENV.lock().unwrap() = Some(vm);
// Succeeds
java_test();
java_test();
// fails to call java_test
main_instance.call("main", &[]).unwrap();
java_test();
}
pub fn load_module(module_bytes: &[u8]) -> Instance {
// Compile the module.
let module = compile(&module_bytes).unwrap();
// Create the ImportObject with our interface imported.
let import_object = create_import_object(&module);
// Instantiate the module.
let module_instance = module.instantiate(&import_object).unwrap();
module_instance
}
fn create_import_object(_module: &Module) -> ImportObject {
let import_object = imports! {
"env" => {
"Test" => func!(java_test),
},
};
import_object
}
fn java_test() {
// Get env.
let ovm = &*ENV.lock().unwrap();
let vm = ovm.as_ref().unwrap();
let env = vm.get_env().unwrap();
// Get the class.
let o_class = &*CLASS.lock().unwrap();
let class_ref = o_class.as_ref().unwrap();
let class = class_ref.as_obj();
// Call java code.
env.call_method(*class, "Test", "()V", &[])
.unwrap();
}