Skip to content

Commit a34c0ca

Browse files
committed
update wasmtime and jni
1 parent 2b3adbc commit a34c0ca

File tree

8 files changed

+554
-326
lines changed

8 files changed

+554
-326
lines changed

Cargo.lock

+526-285
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasmtime-jni/Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ crate_type = ["cdylib"]
1414
anyhow = "1.0.32"
1515
flexi_logger = "0.16.1"
1616
#env_logger = "0.7.1"
17-
jni = "0.17.0"
17+
jni = "0.19.0"
1818
log = "0.4.11"
19-
wasmtime = { version = "0.20", features=["jitdump", "wat", "cache"] }
19+
wasi-common = "0.26.0"
20+
wasi-cap-std-sync = "0.26.0"
21+
wasmtime = { version = "0.26", features=["jitdump", "wat", "cache"] }
2022
wasmtime-jni-exports = { path = "../wasmtime-jni-exports" }
21-
wasmtime-wasi = "0.20.0"
23+
wasmtime-wasi = "0.26.0"

wasmtime-jni/src/ty/byte_slice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ops::Deref;
22

33
use anyhow::{anyhow, ensure, Error};
44
use log::debug;
5-
use wasmtime::{Val, ValType, WeakStore};
5+
use wasmtime::{Store, Val, ValType};
66
pub use wasmtime_jni_exports::WasmSlice;
77

88
use crate::ty::{Abi, ComplexTy, ReturnAbi, WasmAlloc, WasmSliceWrapper};
@@ -79,7 +79,7 @@ impl ComplexTy for ByteSlice {
7979
type Abi = WasmSlice;
8080

8181
#[inline]
82-
fn compatible_with_store<'a>(&self, _store: WeakStore<'a>) -> bool {
82+
fn compatible_with_store(&self, _store: &Store) -> bool {
8383
true
8484
}
8585
}

wasmtime-jni/src/ty/complex_ty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use anyhow::{anyhow, ensure, Error};
2-
use wasmtime::{Val, ValType, WeakStore};
2+
use wasmtime::{Store, Val, ValType};
33

44
use crate::ty::{WasmAlloc, WasmSliceWrapper};
55

66
pub(crate) trait ComplexTy {
77
type Abi: Abi;
88

9-
fn compatible_with_store<'a>(&self, _store: WeakStore<'a>) -> bool;
9+
fn compatible_with_store(&self, _store: &Store) -> bool;
1010
}
1111

1212
pub(crate) trait Abi: Copy {
@@ -197,7 +197,7 @@ macro_rules! direct_complex_ty {
197197
impl ComplexTy for $t {
198198
type Abi = Self;
199199

200-
fn compatible_with_store<'a>(&self, _store: WeakStore<'a>) -> bool {
200+
fn compatible_with_store(&self, _store: &Store) -> bool {
201201
true
202202
}
203203
}

wasmtime-jni/src/ty/str.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ impl<'j> WasmTy for WasmJString<'j> {
1212
type Abi = WasmSlice;
1313

1414
#[inline]
15-
fn compatible_with_store<'a>(&self, _store: WeakStore<'a>) -> bool {
15+
fn compatible_with_store(&self, _store: &Store) -> bool {
1616
true
1717
}
1818

1919
#[inline]
20-
fn into_abi<'a>(self, store: WeakStore<'a>) -> Self::Abi {
20+
fn into_abi(self, store: &Store) -> Self::Abi {
2121
let jstr = self
2222
.env
2323
.get_string(&self.string)
@@ -33,7 +33,7 @@ impl<'j> WasmTy for WasmJString<'j> {
3333
}
3434

3535
#[inline]
36-
unsafe fn from_abi<'a>(abi: Self::Abi, _store: WeakStore<'a>) -> Self {
36+
unsafe fn from_abi<'a>(abi: Self::Abi, _store: &Store) -> Self {
3737
unimplemented!();
3838
}
3939

wasmtime-jni/src/wasm_function.rs

+7-19
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ pub extern "system" fn Java_net_bluejekyll_wasmtime_WasmFunction_createFunc<'j>(
7575
None
7676
};
7777

78-
let wasm_ret: Box<[ValType]> =
79-
wasm_ret.map_or_else(|| Box::new([]) as Box<_>, |v| Box::new([v]) as Box<_>);
78+
let wasm_ret: Vec<ValType> = wasm_ret.map_or_else(|| vec![], |v| vec![v]);
8079

8180
let func = move |caller: Caller, inputs: &[Val], outputs: &mut [Val]| -> Result<(), Trap> {
8281
let java_args = &java_args;
@@ -235,22 +234,15 @@ pub extern "system" fn Java_net_bluejekyll_wasmtime_WasmFunction_createFunc<'j>(
235234
let len = env
236235
.get_array_length(jarray)
237236
.context("failed to get Java array length")?;
238-
let (jbytes, _is_copy) = env
239-
.get_byte_array_elements(jarray)
237+
let jbytes = env
238+
.get_byte_array_elements(jarray, ReleaseMode::NoCopyBack)
240239
.context("failed to get java array elements")?;
241-
let byte_array: &[u8] =
242-
unsafe { slice::from_raw_parts(jbytes as *const u8, len as usize) };
240+
let byte_array: &[u8] = unsafe {
241+
slice::from_raw_parts(jbytes.as_ptr() as *const u8, len as usize)
242+
};
243243

244244
let bytes = wasm_alloc.alloc_bytes(byte_array)?;
245245

246-
// release the array reference, CopyBack is following the JNI guidelines
247-
env.release_byte_array_elements(
248-
jarray,
249-
unsafe { &mut *jbytes },
250-
ReleaseMode::CopyBack,
251-
)
252-
.context("failed to release Java array elements")?;
253-
254246
// get mutable reference to the return by ref pointer and then store
255247
unsafe {
256248
let ret_by_ref_loc = wasm_alloc.obj_as_mut::<WasmSlice>(ptr);
@@ -311,11 +303,7 @@ pub extern "system" fn Java_net_bluejekyll_wasmtime_WasmFunction_createFunc<'j>(
311303
Ok(())
312304
};
313305

314-
let func = Func::new(
315-
&store,
316-
FuncType::new(wasm_args.into_boxed_slice(), wasm_ret),
317-
func,
318-
);
306+
let func = Func::new(&store, FuncType::new(wasm_args, wasm_ret), func);
319307

320308
Ok(OpaquePtr::from(func).make_opaque())
321309
})

wasmtime-jni/src/wasm_linker.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use jni::objects::{JClass, JString};
44
use jni::sys::jlong;
55
use jni::JNIEnv;
66
use wasmtime::{Func, Linker, Module};
7-
use wasmtime_wasi::{Wasi, WasiCtx};
7+
use wasmtime_wasi::Wasi;
88

99
use crate::opaque_ptr::OpaquePtr;
1010
use crate::wasm_exception;
@@ -70,7 +70,11 @@ pub extern "system" fn Java_net_bluejekyll_wasmtime_WasmLinker_instantiateNtv<'j
7070
) -> jlong {
7171
wasm_exception::attempt(&env, |_env| {
7272
let store = linker.store();
73-
let wasi = Wasi::new(&store, WasiCtx::new(std::env::args())?);
73+
let wasi_ctx = wasi_cap_std_sync::WasiCtxBuilder::new()
74+
.inherit_env()?
75+
.build()?;
76+
77+
let wasi = Wasi::new(&store, wasi_ctx);
7478
wasi.add_to_linker(&mut linker)?;
7579

7680
let instance = linker.instantiate(&module)?;

wasmtime-jni/src/wasm_value.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -238,22 +238,15 @@ impl<'j> WasmVal<'j> {
238238
WasmVal::ByteArray { jarray, .. } => {
239239
// This is should be safe, it's copied into while borrowed the WASM context.
240240
let len = env.get_array_length(jarray)?;
241-
let (jbytes, _is_copy) = env.get_byte_array_elements(jarray)?;
241+
let jbytes = env.get_byte_array_elements(jarray, ReleaseMode::NoCopyBack)?;
242242
let byte_array: &[u8] =
243-
unsafe { slice::from_raw_parts(jbytes as *const u8, len as usize) };
243+
unsafe { slice::from_raw_parts(jbytes.as_ptr() as *const u8, len as usize) };
244244

245245
// the module might not have the memory exported
246246
let wasm_alloc = wasm_alloc
247247
.ok_or_else(|| anyhow!("no memory or allocator supplied from module"))?;
248248
let wasm_slice = wasm_alloc.alloc_bytes(byte_array)?;
249249

250-
// release the array reference, CopyBack is following the JNI guidelines
251-
env.release_byte_array_elements(
252-
jarray,
253-
unsafe { &mut *jbytes },
254-
ReleaseMode::CopyBack,
255-
)
256-
.context("failed to release Java array elements")?;
257250
wasm_slice.store_to_args(args);
258251
return Ok(Some(wasm_slice));
259252
}

0 commit comments

Comments
 (0)