Skip to content

Commit

Permalink
finished (and gave up on) wasm support
Browse files Browse the repository at this point in the history
  • Loading branch information
IoIxD committed Sep 1, 2023
1 parent 894e543 commit c2aece5
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 46 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'net.ioixd'
version = '1.1'
version = '1.2'

compileJava {
options.compilerArgs += ["-h", file("include")]
Expand Down
2 changes: 2 additions & 0 deletions native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ lazy_static = "1.4.0"
libloading = "0.8.0"
once_cell = "1.18.0"
parking_lot = "0.12.1"
wasi-common = "12.0.1"
wasmtime = "12.0.1"


[lib]
crate_type = ["cdylib"]
42 changes: 33 additions & 9 deletions native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ pub mod loader;
pub mod shared;
pub mod wasm;

use std::{error::Error, fmt::Display};
use std::{
error::Error,
fmt::{Debug, Display},
};

use jni::{
objects::{JObject, JString},
Expand All @@ -25,7 +28,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_loadPlugin<'a>(
.unwrap()
.to_string_lossy()
.to_string();
if wasm == 1 {
if wasm == jni::sys::JNI_TRUE {
WASMLoader::load_plugin(env, file)
} else {
SharedLoader::load_plugin(env, file)
Expand All @@ -44,7 +47,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_enablePlugin<'a>(
.unwrap()
.to_string_lossy()
.to_string();
if wasm == 1 {
if wasm == jni::sys::JNI_TRUE {
WASMLoader::enable_plugin(file)
} else {
SharedLoader::enable_plugin(file)
Expand All @@ -63,7 +66,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_disablePlugin<'a>(
.unwrap()
.to_string_lossy()
.to_string();
if wasm == 1 {
if wasm == jni::sys::JNI_TRUE {
WASMLoader::disable_plugin(file)
} else {
SharedLoader::disable_plugin(file)
Expand All @@ -76,7 +79,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryNames<'a>(
_obj: JObject,
wasm: jboolean,
) -> JString<'a> {
if wasm == 1 {
if wasm == jni::sys::JNI_TRUE {
WASMLoader::library_names(env)
} else {
SharedLoader::library_names(env)
Expand All @@ -100,7 +103,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryHasFunction<'a>(
.to_string_lossy()
.to_string();

if wasm == 1 {
if wasm == jni::sys::JNI_TRUE {
WASMLoader::library_has_function(env, libname, funcname).into()
} else {
SharedLoader::library_has_function(env, libname, funcname).into()
Expand All @@ -124,7 +127,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_sendEvent<'a>(
let funcname = unwrap_result_or_java_error(&mut env, &libname, funcname_raw)
.to_string_lossy()
.to_string();
if wasm == 1 {
if wasm == jni::sys::JNI_TRUE {
WASMLoader::send_event(env, libname, funcname, ev).into()
} else {
SharedLoader::send_event(env, libname, funcname, ev).into()
Expand All @@ -151,7 +154,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_execute<'a>(
.to_string_lossy()
.to_string();

if wasm == 1 {
if wasm == jni::sys::JNI_TRUE {
WASMLoader::execute(env, libname, funcname, address, plugin, ev)
} else {
SharedLoader::execute(env, libname, funcname, address, plugin, ev)
Expand All @@ -167,7 +170,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_BiConsumerLink_acceptNative<'a>(
obj2: JObject,
wasm: jboolean,
) {
if wasm == 1 {
if wasm == jni::sys::JNI_TRUE {
throw_with_error(
&mut env,
"java/lang/UnsupportedOperationException",
Expand Down Expand Up @@ -209,6 +212,26 @@ where
}
}
}
pub fn unwrap_wasm_result_or_java_error<V, S>(
mut env: &mut JNIEnv,
libname: S,
opt: Result<V, wasmtime::Error>,
) -> V
where
S: Into<String> + Display,
{
match opt {
Ok(v) => v,
Err(err) => {
throw_with_error(
&mut env,
"net/ioixd/blackbox/exceptions/NativeLibrarySymbolLoadException",
format!("error with {}: {:?}", &libname, err),
);
unreachable!();
}
}
}

pub fn unwrap_option_or_java_error<V, S>(
mut env: &mut JNIEnv,
Expand Down Expand Up @@ -287,4 +310,5 @@ pub fn throw_with_error(
if let Err(_) = er {
env.exception_describe().unwrap();
}
env.exception_describe().unwrap();
}
Loading

0 comments on commit c2aece5

Please sign in to comment.