Skip to content

Commit b90cb5c

Browse files
authored
Remove deprecated magnus calls in module and component (#532)
1 parent 9d16c8b commit b90cb5c

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

ext/src/ruby_api/component.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ mod wasi_command;
66

77
use super::root;
88
use magnus::{
9-
class, class::RClass, function, method, prelude::*, r_string::RString, value::Lazy, Error,
10-
Module, Object, RModule, Ruby,
9+
class::{self, RClass},
10+
function, method,
11+
prelude::*,
12+
r_string::RString,
13+
typed_data::Obj,
14+
value::Lazy,
15+
Error, Module, Object, RModule, Ruby,
1116
};
1217
use rb_sys::tracking_allocator::ManuallyTracked;
1318
use wasmtime::component::Component as ComponentImpl;
@@ -113,11 +118,11 @@ impl Component {
113118
/// Serialize the component.
114119
/// @return [String]
115120
/// @see .deserialize
116-
pub fn serialize(&self) -> Result<RString, Error> {
117-
let bytes = self.get().serialize();
121+
pub fn serialize(ruby: &Ruby, rb_self: Obj<Self>) -> Result<RString, Error> {
122+
let bytes = rb_self.get().serialize();
118123

119124
bytes
120-
.map(|bytes| RString::from_slice(&bytes))
125+
.map(|bytes| ruby.str_from_slice(&bytes))
121126
.map_err(|e| error!("{:?}", e))
122127
}
123128

ext/src/ruby_api/convert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,5 @@ pub trait WrapWasmtimeExternType<T>
223223
where
224224
T: TypedData,
225225
{
226-
fn wrap_wasmtime_type(&self) -> Result<T, Error>;
226+
fn wrap_wasmtime_type(&self, ruby: &Ruby) -> Result<T, Error>;
227227
}

ext/src/ruby_api/externals.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,20 @@ impl<'a> WrapWasmtimeType<'a, Extern<'a>> for wasmtime::Extern {
214214
}
215215

216216
impl WrapWasmtimeExternType<ExternType> for wasmtime::ExternType {
217-
fn wrap_wasmtime_type(&self) -> Result<ExternType, Error> {
217+
fn wrap_wasmtime_type(&self, ruby: &Ruby) -> Result<ExternType, Error> {
218218
match self {
219-
wasmtime::ExternType::Func(ft) => Ok(ExternType::Func(Obj::wrap(
220-
FuncType::from_inner(ft.clone()),
221-
))),
222-
wasmtime::ExternType::Global(gt) => Ok(ExternType::Global(Obj::wrap(
223-
GlobalType::from_inner(gt.clone()),
224-
))),
225-
wasmtime::ExternType::Memory(mt) => Ok(ExternType::Memory(Obj::wrap(
226-
MemoryType::from_inner(mt.clone()),
227-
))),
228-
wasmtime::ExternType::Table(tt) => Ok(ExternType::Table(Obj::wrap(
229-
TableType::from_inner(tt.clone()),
230-
))),
219+
wasmtime::ExternType::Func(ft) => Ok(ExternType::Func(
220+
ruby.obj_wrap(FuncType::from_inner(ft.clone())),
221+
)),
222+
wasmtime::ExternType::Global(gt) => Ok(ExternType::Global(
223+
ruby.obj_wrap(GlobalType::from_inner(gt.clone())),
224+
)),
225+
wasmtime::ExternType::Memory(mt) => Ok(ExternType::Memory(
226+
ruby.obj_wrap(MemoryType::from_inner(mt.clone())),
227+
)),
228+
wasmtime::ExternType::Table(tt) => Ok(ExternType::Table(
229+
ruby.obj_wrap(TableType::from_inner(tt.clone())),
230+
)),
231231
wasmtime::ExternType::Tag(_) => {
232232
not_implemented!("exception handling not yet implemented")
233233
}

ext/src/ruby_api/module.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::{
1414
helpers::{nogvl, Tmplock},
1515
};
1616
use magnus::{
17-
class, function, method, rb_sys::AsRawValue, Error, Module as _, Object, RArray, RHash,
18-
RString, Ruby,
17+
class, function, method, rb_sys::AsRawValue, typed_data::Obj, Error, Module as _, Object,
18+
RArray, RHash, RString, Ruby,
1919
};
2020
use rb_sys::{
2121
rb_str_locktmp, rb_str_unlocktmp, tracking_allocator::ManuallyTracked, RSTRING_LEN, RSTRING_PTR,
@@ -101,12 +101,12 @@ impl Module {
101101
/// Serialize the module.
102102
/// @return [String]
103103
/// @see .deserialize
104-
pub fn serialize(&self) -> Result<RString, Error> {
105-
let module = self.get();
104+
pub fn serialize(ruby: &Ruby, rb_self: Obj<Self>) -> Result<RString, Error> {
105+
let module = rb_self.get();
106106
let bytes = module.serialize();
107107

108108
bytes
109-
.map(|bytes| RString::from_slice(&bytes))
109+
.map(|bytes| ruby.str_from_slice(&bytes))
110110
.map_err(|e| error!("{:?}", e))
111111
}
112112

@@ -117,16 +117,16 @@ impl Module {
117117
/// @yard
118118
/// Returns the list of imports that this Module has and must be satisfied.
119119
/// @return [Array<Hash>] An array of hashes containing import information
120-
pub fn imports(&self) -> Result<RArray, Error> {
121-
let module = self.get();
120+
pub fn imports(ruby: &Ruby, rb_self: Obj<Self>) -> Result<RArray, Error> {
121+
let module = rb_self.get();
122122
let imports = module.imports();
123123

124-
let result = RArray::with_capacity(imports.len());
124+
let result = ruby.ary_new_capa(imports.len());
125125
for import in imports {
126-
let hash = RHash::new();
126+
let hash = ruby.hash_new();
127127
hash.aset("module", import.module())?;
128128
hash.aset("name", import.name())?;
129-
hash.aset("type", import.ty().wrap_wasmtime_type()?)?;
129+
hash.aset("type", import.ty().wrap_wasmtime_type(ruby)?)?;
130130
result.push(hash)?;
131131
}
132132
Ok(result)

0 commit comments

Comments
 (0)