Skip to content

Commit 673d573

Browse files
authored
Remove deprecated Magnus call in trap (#533)
1 parent da86b05 commit 673d573

6 files changed

Lines changed: 23 additions & 15 deletions

File tree

ext/src/ruby_api/component/func.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Func {
107107
)?;
108108

109109
func.call(store.context_mut(), &params, &mut results)
110-
.map_err(|e| store_context_value.handle_wasm_error(e))?;
110+
.map_err(|e| store_context_value.handle_wasm_error(ruby, e))?;
111111

112112
let result = match results_ty.len() {
113113
0 => Ok(ruby.qnil().as_value()),
@@ -127,7 +127,7 @@ impl Func {
127127
};
128128

129129
func.post_return(store.context_mut())
130-
.map_err(|e| store_context_value.handle_wasm_error(e))?;
130+
.map_err(|e| store_context_value.handle_wasm_error(ruby, e))?;
131131

132132
result
133133
}

ext/src/ruby_api/func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl<'a> Func<'a> {
219219
let mut results = vec![Val::null_func_ref(); func_ty.results().len()];
220220

221221
func.call(context, &params, &mut results)
222-
.map_err(|e| store.handle_wasm_error(e))?;
222+
.map_err(|e| store.handle_wasm_error(ruby, e))?;
223223

224224
match results.as_slice() {
225225
[] => Ok(().into_value_with(ruby)),

ext/src/ruby_api/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Instance {
6464

6565
let module = module.get();
6666
let inner = InstanceImpl::new(context, module, &imports)
67-
.map_err(|e| StoreContextValue::from(wrapped_store).handle_wasm_error(e))?;
67+
.map_err(|e| StoreContextValue::from(wrapped_store).handle_wasm_error(ruby, e))?;
6868

6969
Ok(Self {
7070
inner,

ext/src/ruby_api/linker.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,27 @@ impl Linker {
266266
/// @param store [Store]
267267
/// @param mod [Module]
268268
/// @return [Instance]
269-
pub fn instantiate(&self, store: Obj<Store>, module: &Module) -> Result<Instance, Error> {
270-
if *self.has_wasi.borrow() && !store.context().data().has_wasi_p1_ctx() {
269+
pub fn instantiate(
270+
ruby: &Ruby,
271+
rb_self: Obj<Self>,
272+
store: Obj<Store>,
273+
module: &Module,
274+
) -> Result<Instance, Error> {
275+
if *rb_self.has_wasi.borrow() && !store.context().data().has_wasi_p1_ctx() {
271276
return err!("{}", errors::missing_wasi_p1_ctx_error());
272277
}
273278

274-
self.inner
279+
rb_self
280+
.inner
275281
.borrow_mut()
276282
.instantiate(store.context_mut(), module.get())
277-
.map_err(|e| StoreContextValue::from(store).handle_wasm_error(e))
283+
.map_err(|e| StoreContextValue::from(store).handle_wasm_error(ruby, e))
278284
.map(|instance| {
279-
self.refs.borrow().iter().for_each(|val| store.retain(*val));
285+
rb_self
286+
.refs
287+
.borrow()
288+
.iter()
289+
.for_each(|val| store.retain(*val));
280290
Instance::from_inner(store, instance)
281291
})
282292
}

ext/src/ruby_api/store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,14 @@ impl StoreContextValue<'_> {
346346
};
347347
}
348348

349-
pub fn handle_wasm_error(&self, error: wasmtime::Error) -> Error {
349+
pub fn handle_wasm_error(&self, ruby: &Ruby, error: wasmtime::Error) -> Error {
350350
if let Ok(Some(error)) = self.take_last_error() {
351351
error
352352
} else if let Some(exit) = error.downcast_ref::<I32Exit>() {
353353
wasi_exit_error().new_instance((exit.0,)).unwrap().into()
354354
} else {
355355
Trap::try_from(error)
356-
.map(|trap| trap.into())
356+
.map(|trap| trap.into_error(ruby))
357357
.unwrap_or_else(|e| error!("{}", e))
358358
}
359359
}

ext/src/ruby_api/trap.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,9 @@ impl Trap {
8686
rb_self.code()?.into_value_with(ruby).inspect()
8787
))
8888
}
89-
}
9089

91-
impl From<Trap> for Error {
92-
fn from(trap: Trap) -> Self {
93-
magnus::Exception::from_value(Obj::wrap(trap).as_value())
90+
pub fn into_error(self, ruby: &Ruby) -> Error {
91+
magnus::Exception::from_value(ruby.obj_wrap(self).as_value())
9492
.unwrap() // Can't fail: Wasmtime::Trap is an Exception
9593
.into()
9694
}

0 commit comments

Comments
 (0)