Skip to content

Commit 98d2700

Browse files
committed
Fix CI
1 parent dc232b9 commit 98d2700

File tree

7 files changed

+108
-70
lines changed

7 files changed

+108
-70
lines changed

Makefile

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,7 @@ test-config:
4949

5050
tests: test-javy test-core test-runner test-cli test-wpt test-config
5151

52-
fmt: fmt-quickjs-wasm-sys fmt-quickjs-wasm-rs fmt-javy fmt-apis fmt-core fmt-cli
53-
54-
fmt-quickjs-wasm-sys:
55-
cargo fmt --package=quickjs-wasm-sys -- --check
56-
cargo clippy --package=quickjs-wasm-sys --target=wasm32-wasi --all-targets -- -D warnings
57-
58-
fmt-quickjs-wasm-rs:
59-
cargo fmt --package=quickjs-wasm-rs -- --check
60-
cargo clippy --package=quickjs-wasm-rs --target=wasm32-wasi --all-targets -- -D warnings
52+
fmt: fmt-javy fmt-apis fmt-core fmt-cli
6153

6254
fmt-javy:
6355
cargo fmt --package=javy -- --check

crates/cli/benches/benchmark.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use wasi_common::{
77
sync::WasiCtxBuilder,
88
WasiCtx,
99
};
10-
use wasmtime::{Engine, Linker, Module, Store};
10+
use wasmtime::{AsContextMut, Engine, Linker, Module, Store};
1111

1212
struct FunctionCase {
1313
name: String,
@@ -84,22 +84,26 @@ impl FunctionCase {
8484
Ok(function_case)
8585
}
8686

87-
pub fn run(&self, linker: &mut Linker<WasiCtx>, mut store: &mut Store<WasiCtx>) -> Result<()> {
87+
pub fn run(
88+
&self,
89+
linker: &mut Linker<WasiCtx>,
90+
mut store: impl AsContextMut<Data = WasiCtx>,
91+
) -> Result<()> {
8892
let js_module = match &self.precompiled_elf_bytes {
8993
Some(bytes) => unsafe { Module::deserialize(&self.engine, bytes) }?,
9094
None => Module::new(&self.engine, &self.wasm_bytes)?,
9195
};
9296

93-
let consumer_instance = linker.instantiate(&mut store, &js_module)?;
94-
linker.instance(&mut store, "consumer", consumer_instance)?;
97+
let consumer_instance = linker.instantiate(store.as_context_mut(), &js_module)?;
98+
linker.instance(store.as_context_mut(), "consumer", consumer_instance)?;
9599

96100
linker
97-
.get(&mut store, "consumer", "_start")
101+
.get(store.as_context_mut(), "consumer", "_start")
98102
.unwrap()
99103
.into_func()
100104
.unwrap()
101-
.typed::<(), ()>(&mut store)?
102-
.call(&mut store, ())?;
105+
.typed::<(), ()>(store.as_context())?
106+
.call(store.as_context_mut(), ())?;
103107
Ok(())
104108
}
105109

@@ -120,8 +124,8 @@ impl FunctionCase {
120124
"../../target/wasm32-wasi/release/javy_quickjs_provider_wizened.wasm",
121125
))?,
122126
)?;
123-
let instance = linker.instantiate(&mut store, &qjs_provider)?;
124-
linker.instance(&mut store, "javy_quickjs_provider_v2", instance)?;
127+
let instance = linker.instantiate(store.as_context_mut(), &qjs_provider)?;
128+
linker.instance(store.as_context_mut(), "javy_quickjs_provider_v2", instance)?;
125129
}
126130

127131
Ok((linker, store))
@@ -169,7 +173,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
169173
|b, f| {
170174
b.iter_with_setup(
171175
|| function_case.setup().unwrap(),
172-
|(mut linker, mut store)| f.run(&mut linker, &mut store).unwrap(),
176+
|(mut linker, mut store)| f.run(&mut linker, store.as_context_mut()).unwrap(),
173177
)
174178
},
175179
);

crates/cli/src/bytecode.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use anyhow::{anyhow, Result};
22
use wasi_common::{sync::WasiCtxBuilder, WasiCtx};
3-
use wasmtime::{Engine, Instance, Linker, Memory, Module, Store};
3+
use wasmtime::{AsContextMut, Engine, Instance, Linker, Memory, Module, Store};
44

55
pub const QUICKJS_PROVIDER_MODULE: &[u8] =
66
include_bytes!(concat!(env!("OUT_DIR"), "/provider.wasm"));
77

88
pub fn compile_source(js_source_code: &[u8]) -> Result<Vec<u8>> {
99
let (mut store, instance, memory) = create_wasm_env()?;
1010
let (js_src_ptr, js_src_len) =
11-
copy_source_code_into_instance(js_source_code, &mut store, &instance, &memory)?;
12-
let ret_ptr = call_compile(js_src_ptr, js_src_len, &mut store, &instance)?;
13-
let bytecode = copy_bytecode_from_instance(ret_ptr, &mut store, &memory)?;
11+
copy_source_code_into_instance(js_source_code, store.as_context_mut(), &instance, &memory)?;
12+
let ret_ptr = call_compile(js_src_ptr, js_src_len, store.as_context_mut(), &instance)?;
13+
let bytecode = copy_bytecode_from_instance(ret_ptr, store.as_context_mut(), &memory)?;
1414
Ok(bytecode)
1515
}
1616

@@ -24,59 +24,70 @@ fn create_wasm_env() -> Result<(Store<WasiCtx>, Instance, Memory)> {
2424
)?;
2525
let wasi = WasiCtxBuilder::new().inherit_stderr().build();
2626
let mut store = Store::new(&engine, wasi);
27-
let instance = linker.instantiate(&mut store, &module)?;
28-
let memory = instance.get_memory(&mut store, "memory").unwrap();
27+
let instance = linker.instantiate(store.as_context_mut(), &module)?;
28+
let memory = instance
29+
.get_memory(store.as_context_mut(), "memory")
30+
.unwrap();
2931
Ok((store, instance, memory))
3032
}
3133

3234
fn copy_source_code_into_instance(
3335
js_source_code: &[u8],
34-
mut store: &mut Store<WasiCtx>,
36+
mut store: impl AsContextMut,
3537
instance: &Instance,
3638
memory: &Memory,
3739
) -> Result<(u32, u32)> {
38-
let realloc_fn = instance
39-
.get_typed_func::<(u32, u32, u32, u32), u32>(&mut store, "canonical_abi_realloc")?;
40+
let realloc_fn = instance.get_typed_func::<(u32, u32, u32, u32), u32>(
41+
store.as_context_mut(),
42+
"canonical_abi_realloc",
43+
)?;
4044
let js_src_len = js_source_code.len().try_into()?;
4145

4246
let original_ptr = 0;
4347
let original_size = 0;
4448
let alignment = 1;
4549
let size = js_src_len;
46-
let js_source_ptr =
47-
realloc_fn.call(&mut store, (original_ptr, original_size, alignment, size))?;
50+
let js_source_ptr = realloc_fn.call(
51+
store.as_context_mut(),
52+
(original_ptr, original_size, alignment, size),
53+
)?;
4854

49-
memory.write(&mut store, js_source_ptr.try_into()?, js_source_code)?;
55+
memory.write(
56+
store.as_context_mut(),
57+
js_source_ptr.try_into()?,
58+
js_source_code,
59+
)?;
5060

5161
Ok((js_source_ptr, js_src_len))
5262
}
5363

5464
fn call_compile(
5565
js_src_ptr: u32,
5666
js_src_len: u32,
57-
mut store: &mut Store<WasiCtx>,
67+
mut store: impl AsContextMut,
5868
instance: &Instance,
5969
) -> Result<u32> {
60-
let compile_src_fn = instance.get_typed_func::<(u32, u32), u32>(&mut store, "compile_src")?;
70+
let compile_src_fn =
71+
instance.get_typed_func::<(u32, u32), u32>(store.as_context_mut(), "compile_src")?;
6172
let ret_ptr = compile_src_fn
62-
.call(&mut store, (js_src_ptr, js_src_len))
73+
.call(store.as_context_mut(), (js_src_ptr, js_src_len))
6374
.map_err(|_| anyhow!("JS compilation failed"))?;
6475
Ok(ret_ptr)
6576
}
6677

6778
fn copy_bytecode_from_instance(
6879
ret_ptr: u32,
69-
mut store: &mut Store<WasiCtx>,
80+
mut store: impl AsContextMut,
7081
memory: &Memory,
7182
) -> Result<Vec<u8>> {
7283
let mut ret_buffer = [0; 8];
73-
memory.read(&mut store, ret_ptr.try_into()?, &mut ret_buffer)?;
84+
memory.read(store.as_context_mut(), ret_ptr.try_into()?, &mut ret_buffer)?;
7485

7586
let bytecode_ptr = u32::from_le_bytes(ret_buffer[0..4].try_into()?);
7687
let bytecode_len = u32::from_le_bytes(ret_buffer[4..8].try_into()?);
7788

7889
let mut bytecode = vec![0; bytecode_len.try_into()?];
79-
memory.read(&mut store, bytecode_ptr.try_into()?, &mut bytecode)?;
90+
memory.read(store.as_context(), bytecode_ptr.try_into()?, &mut bytecode)?;
8091

8192
Ok(bytecode)
8293
}

crates/cli/tests/dylib_test.rs

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::Result;
22
use std::boxed::Box;
33
use std::str;
44
use wasi_common::{pipe::WritePipe, sync::WasiCtxBuilder, WasiCtx, WasiFile};
5-
use wasmtime::{Engine, Instance, Linker, Store};
5+
use wasmtime::{AsContextMut, Engine, Instance, Linker, Store};
66

77
mod common;
88

@@ -49,9 +49,10 @@ fn run_js_src<T: WasiFile + Clone + 'static>(js_src: &str, stderr: &T) -> Result
4949
let (instance, mut store) = create_wasm_env(stderr)?;
5050

5151
let eval_bytecode_func =
52-
instance.get_typed_func::<(u32, u32), ()>(&mut store, "eval_bytecode")?;
53-
let (bytecode_ptr, bytecode_len) = compile_src(js_src.as_bytes(), &instance, &mut store)?;
54-
eval_bytecode_func.call(&mut store, (bytecode_ptr, bytecode_len))?;
52+
instance.get_typed_func::<(u32, u32), ()>(store.as_context_mut(), "eval_bytecode")?;
53+
let (bytecode_ptr, bytecode_len) =
54+
compile_src(js_src.as_bytes(), &instance, store.as_context_mut())?;
55+
eval_bytecode_func.call(store.as_context_mut(), (bytecode_ptr, bytecode_len))?;
5556
Ok(())
5657
}
5758

@@ -62,11 +63,14 @@ fn run_invoke<T: WasiFile + Clone + 'static>(
6263
) -> Result<()> {
6364
let (instance, mut store) = create_wasm_env(stderr)?;
6465

65-
let invoke_func = instance.get_typed_func::<(u32, u32, u32, u32), ()>(&mut store, "invoke")?;
66-
let (bytecode_ptr, bytecode_len) = compile_src(js_src.as_bytes(), &instance, &mut store)?;
67-
let (fn_name_ptr, fn_name_len) = copy_func_name(fn_to_invoke, &instance, &mut store)?;
66+
let invoke_func =
67+
instance.get_typed_func::<(u32, u32, u32, u32), ()>(store.as_context_mut(), "invoke")?;
68+
let (bytecode_ptr, bytecode_len) =
69+
compile_src(js_src.as_bytes(), &instance, store.as_context_mut())?;
70+
let (fn_name_ptr, fn_name_len) =
71+
copy_func_name(fn_to_invoke, &instance, store.as_context_mut())?;
6872
invoke_func.call(
69-
&mut store,
73+
store.as_context_mut(),
7074
(bytecode_ptr, bytecode_len, fn_name_ptr, fn_name_len),
7175
)?;
7276
Ok(())
@@ -84,25 +88,36 @@ fn create_wasm_env<T: WasiFile + Clone + 'static>(
8488
let module = common::create_quickjs_provider_module(&engine)?;
8589

8690
let mut store = Store::new(&engine, wasi);
87-
let instance = linker.instantiate(&mut store, &module)?;
91+
let instance = linker.instantiate(store.as_context_mut(), &module)?;
8892

8993
Ok((instance, store))
9094
}
9195

9296
fn compile_src(
9397
js_src: &[u8],
9498
instance: &Instance,
95-
mut store: &mut Store<WasiCtx>,
99+
mut store: impl AsContextMut,
96100
) -> Result<(u32, u32)> {
97-
let memory = instance.get_memory(&mut store, "memory").unwrap();
98-
let compile_src_func = instance.get_typed_func::<(u32, u32), u32>(&mut store, "compile_src")?;
99-
100-
let js_src_ptr = allocate_memory(instance, store, 1, js_src.len().try_into()?)?;
101-
memory.write(&mut store, js_src_ptr.try_into()?, js_src)?;
101+
let memory = instance
102+
.get_memory(store.as_context_mut(), "memory")
103+
.unwrap();
104+
let compile_src_func =
105+
instance.get_typed_func::<(u32, u32), u32>(store.as_context_mut(), "compile_src")?;
106+
107+
let js_src_ptr = allocate_memory(
108+
instance,
109+
store.as_context_mut(),
110+
1,
111+
js_src.len().try_into()?,
112+
)?;
113+
memory.write(store.as_context_mut(), js_src_ptr.try_into()?, js_src)?;
102114

103-
let ret_ptr = compile_src_func.call(&mut store, (js_src_ptr, js_src.len().try_into()?))?;
115+
let ret_ptr = compile_src_func.call(
116+
store.as_context_mut(),
117+
(js_src_ptr, js_src.len().try_into()?),
118+
)?;
104119
let mut ret_buffer = [0; 8];
105-
memory.read(&mut store, ret_ptr.try_into()?, &mut ret_buffer)?;
120+
memory.read(store.as_context(), ret_ptr.try_into()?, &mut ret_buffer)?;
106121
let bytecode_ptr = u32::from_le_bytes(ret_buffer[0..4].try_into()?);
107122
let bytecode_len = u32::from_le_bytes(ret_buffer[4..8].try_into()?);
108123

@@ -112,27 +127,40 @@ fn compile_src(
112127
fn copy_func_name(
113128
fn_name: &str,
114129
instance: &Instance,
115-
mut store: &mut Store<WasiCtx>,
130+
mut store: impl AsContextMut,
116131
) -> Result<(u32, u32)> {
117-
let memory = instance.get_memory(&mut store, "memory").unwrap();
132+
let memory = instance
133+
.get_memory(store.as_context_mut(), "memory")
134+
.unwrap();
118135
let fn_name_bytes = fn_name.as_bytes();
119-
let fn_name_ptr = allocate_memory(instance, store, 1, fn_name_bytes.len().try_into()?)?;
120-
memory.write(&mut store, fn_name_ptr.try_into()?, fn_name_bytes)?;
136+
let fn_name_ptr = allocate_memory(
137+
instance,
138+
store.as_context_mut(),
139+
1,
140+
fn_name_bytes.len().try_into()?,
141+
)?;
142+
memory.write(
143+
store.as_context_mut(),
144+
fn_name_ptr.try_into()?,
145+
fn_name_bytes,
146+
)?;
121147

122148
Ok((fn_name_ptr, fn_name_bytes.len().try_into()?))
123149
}
124150

125151
fn allocate_memory(
126152
instance: &Instance,
127-
mut store: &mut Store<WasiCtx>,
153+
mut store: impl AsContextMut,
128154
alignment: u32,
129155
new_size: u32,
130156
) -> Result<u32> {
131-
let realloc_func = instance
132-
.get_typed_func::<(u32, u32, u32, u32), u32>(&mut store, "canonical_abi_realloc")?;
157+
let realloc_func = instance.get_typed_func::<(u32, u32, u32, u32), u32>(
158+
store.as_context_mut(),
159+
"canonical_abi_realloc",
160+
)?;
133161
let orig_ptr = 0;
134162
let orig_size = 0;
135163
realloc_func
136-
.call(&mut store, (orig_ptr, orig_size, alignment, new_size))
164+
.call(store, (orig_ptr, orig_size, alignment, new_size))
137165
.map_err(Into::into)
138166
}

crates/cli/tests/dynamic_linking_test.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use uuid::Uuid;
77
use wasi_common::pipe::{ReadPipe, WritePipe};
88
use wasi_common::sync::WasiCtxBuilder;
99
use wasi_common::WasiCtx;
10-
use wasmtime::{Config, Engine, ExternType, Linker, Module, Store};
10+
use wasmtime::{AsContextMut, Config, Engine, ExternType, Linker, Module, Store};
1111

1212
mod common;
1313

@@ -223,15 +223,16 @@ fn invoke_fn_on_generated_module(
223223
let quickjs_provider_module = common::create_quickjs_provider_module(&engine)?;
224224
let js_module = Module::from_binary(&engine, &js_wasm)?;
225225

226-
let quickjs_provider_instance = linker.instantiate(&mut store, &quickjs_provider_module)?;
226+
let quickjs_provider_instance =
227+
linker.instantiate(store.as_context_mut(), &quickjs_provider_module)?;
227228
linker.instance(
228-
&mut store,
229+
store.as_context_mut(),
229230
"javy_quickjs_provider_v2",
230231
quickjs_provider_instance,
231232
)?;
232-
let js_instance = linker.instantiate(&mut store, &js_module)?;
233-
let func = js_instance.get_typed_func::<(), ()>(&mut store, func)?;
234-
func.call(&mut store, ())?;
233+
let js_instance = linker.instantiate(store.as_context_mut(), &js_module)?;
234+
let func = js_instance.get_typed_func::<(), ()>(store.as_context_mut(), func)?;
235+
func.call(store.as_context_mut(), ())?;
235236

236237
drop(store); // Need to drop store to access contents of stderr.
237238
let log_output = stderr.try_into_inner().unwrap().into_inner();

crates/javy/src/alloc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const ZERO_SIZE_ALLOCATION_PTR: *mut u8 = 1 as _;
1818
///
1919
/// 1. Allocate memory of new_size with alignment.
2020
/// 2. If original_ptr != 0.
21-
/// a. copy min(new_size, original_size) bytes from original_ptr to new memory.
22-
/// b. de-allocate original_ptr.
21+
/// a. copy min(new_size, original_size) bytes from original_ptr to new memory.
22+
/// b. de-allocate original_ptr.
2323
/// 3. Return new memory ptr.
2424
///
2525
/// # Safety

crates/javy/tests/misc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#[cfg(feature = "json")]
12
use anyhow::Result;
3+
#[cfg(feature = "json")]
24
use javy::{quickjs::context::EvalOptions, Config, Runtime};
35

46
#[cfg(feature = "json")]

0 commit comments

Comments
 (0)