Skip to content

Commit c2aece5

Browse files
committed
finished (and gave up on) wasm support
1 parent 894e543 commit c2aece5

File tree

5 files changed

+235
-46
lines changed

5 files changed

+235
-46
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group = 'net.ioixd'
11-
version = '1.1'
11+
version = '1.2'
1212

1313
compileJava {
1414
options.compilerArgs += ["-h", file("include")]

native/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ lazy_static = "1.4.0"
1111
libloading = "0.8.0"
1212
once_cell = "1.18.0"
1313
parking_lot = "0.12.1"
14+
wasi-common = "12.0.1"
1415
wasmtime = "12.0.1"
1516

17+
1618
[lib]
1719
crate_type = ["cdylib"]

native/src/lib.rs

+33-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ pub mod loader;
22
pub mod shared;
33
pub mod wasm;
44

5-
use std::{error::Error, fmt::Display};
5+
use std::{
6+
error::Error,
7+
fmt::{Debug, Display},
8+
};
69

710
use jni::{
811
objects::{JObject, JString},
@@ -25,7 +28,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_loadPlugin<'a>(
2528
.unwrap()
2629
.to_string_lossy()
2730
.to_string();
28-
if wasm == 1 {
31+
if wasm == jni::sys::JNI_TRUE {
2932
WASMLoader::load_plugin(env, file)
3033
} else {
3134
SharedLoader::load_plugin(env, file)
@@ -44,7 +47,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_enablePlugin<'a>(
4447
.unwrap()
4548
.to_string_lossy()
4649
.to_string();
47-
if wasm == 1 {
50+
if wasm == jni::sys::JNI_TRUE {
4851
WASMLoader::enable_plugin(file)
4952
} else {
5053
SharedLoader::enable_plugin(file)
@@ -63,7 +66,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_disablePlugin<'a>(
6366
.unwrap()
6467
.to_string_lossy()
6568
.to_string();
66-
if wasm == 1 {
69+
if wasm == jni::sys::JNI_TRUE {
6770
WASMLoader::disable_plugin(file)
6871
} else {
6972
SharedLoader::disable_plugin(file)
@@ -76,7 +79,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryNames<'a>(
7679
_obj: JObject,
7780
wasm: jboolean,
7881
) -> JString<'a> {
79-
if wasm == 1 {
82+
if wasm == jni::sys::JNI_TRUE {
8083
WASMLoader::library_names(env)
8184
} else {
8285
SharedLoader::library_names(env)
@@ -100,7 +103,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryHasFunction<'a>(
100103
.to_string_lossy()
101104
.to_string();
102105

103-
if wasm == 1 {
106+
if wasm == jni::sys::JNI_TRUE {
104107
WASMLoader::library_has_function(env, libname, funcname).into()
105108
} else {
106109
SharedLoader::library_has_function(env, libname, funcname).into()
@@ -124,7 +127,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_sendEvent<'a>(
124127
let funcname = unwrap_result_or_java_error(&mut env, &libname, funcname_raw)
125128
.to_string_lossy()
126129
.to_string();
127-
if wasm == 1 {
130+
if wasm == jni::sys::JNI_TRUE {
128131
WASMLoader::send_event(env, libname, funcname, ev).into()
129132
} else {
130133
SharedLoader::send_event(env, libname, funcname, ev).into()
@@ -151,7 +154,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_execute<'a>(
151154
.to_string_lossy()
152155
.to_string();
153156

154-
if wasm == 1 {
157+
if wasm == jni::sys::JNI_TRUE {
155158
WASMLoader::execute(env, libname, funcname, address, plugin, ev)
156159
} else {
157160
SharedLoader::execute(env, libname, funcname, address, plugin, ev)
@@ -167,7 +170,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_BiConsumerLink_acceptNative<'a>(
167170
obj2: JObject,
168171
wasm: jboolean,
169172
) {
170-
if wasm == 1 {
173+
if wasm == jni::sys::JNI_TRUE {
171174
throw_with_error(
172175
&mut env,
173176
"java/lang/UnsupportedOperationException",
@@ -209,6 +212,26 @@ where
209212
}
210213
}
211214
}
215+
pub fn unwrap_wasm_result_or_java_error<V, S>(
216+
mut env: &mut JNIEnv,
217+
libname: S,
218+
opt: Result<V, wasmtime::Error>,
219+
) -> V
220+
where
221+
S: Into<String> + Display,
222+
{
223+
match opt {
224+
Ok(v) => v,
225+
Err(err) => {
226+
throw_with_error(
227+
&mut env,
228+
"net/ioixd/blackbox/exceptions/NativeLibrarySymbolLoadException",
229+
format!("error with {}: {:?}", &libname, err),
230+
);
231+
unreachable!();
232+
}
233+
}
234+
}
212235

213236
pub fn unwrap_option_or_java_error<V, S>(
214237
mut env: &mut JNIEnv,
@@ -287,4 +310,5 @@ pub fn throw_with_error(
287310
if let Err(_) = er {
288311
env.exception_describe().unwrap();
289312
}
313+
env.exception_describe().unwrap();
290314
}

0 commit comments

Comments
 (0)