Skip to content

Commit 5fc7dc5

Browse files
Use rust struct instead of nested RArrays
1 parent cea0c26 commit 5fc7dc5

File tree

2 files changed

+60
-77
lines changed

2 files changed

+60
-77
lines changed

ext/src/ruby_api/wasi_config.rs

Lines changed: 56 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ use wasmtime_wasi::p2::{OutputFile, WasiCtx, WasiCtxBuilder};
1515
use wasmtime_wasi::preview1::WasiP1Ctx;
1616
use wasmtime_wasi::{DirPerms, FilePerms};
1717

18+
#[derive(Clone)]
19+
struct MappedDirectory {
20+
host_path: String,
21+
guest_path: String,
22+
dir_perms: DirPerms,
23+
file_perms: FilePerms,
24+
}
25+
1826
enum ReadStream {
1927
Inherit,
2028
Path(Opaque<RString>),
@@ -54,7 +62,7 @@ struct WasiConfigInner {
5462
env: Option<Opaque<RHash>>,
5563
args: Option<Opaque<RArray>>,
5664
deterministic: bool,
57-
mapped_directories: Option<Opaque<RArray>>,
65+
mapped_directories: Vec<MappedDirectory>,
5866
}
5967

6068
impl WasiConfigInner {
@@ -74,9 +82,6 @@ impl WasiConfigInner {
7482
if let Some(v) = self.args.as_ref() {
7583
marker.mark(*v);
7684
}
77-
if let Some(v) = self.mapped_directories.as_ref() {
78-
marker.mark(*v);
79-
}
8085
}
8186
}
8287

@@ -254,24 +259,47 @@ impl WasiConfig {
254259
guest_path: RString,
255260
dir_perms: Symbol,
256261
file_perms: Symbol,
257-
) -> RbSelf {
258-
let mapped_directory = RArray::new();
259-
mapped_directory.push(host_path).unwrap();
260-
mapped_directory.push(guest_path).unwrap();
261-
mapped_directory.push(dir_perms).unwrap();
262-
mapped_directory.push(file_perms).unwrap();
262+
) -> Result<RbSelf, Error> {
263+
let host_path = host_path.to_string().unwrap();
264+
let guest_path = guest_path.to_string().unwrap();
265+
let dir_perms_str = dir_perms.name().unwrap().to_string();
266+
let file_perms_str = file_perms.name().unwrap().to_string();
267+
268+
let dir_perms = match dir_perms_str.as_str() {
269+
"read" => DirPerms::READ,
270+
"mutate" => DirPerms::MUTATE,
271+
"all" => DirPerms::all(),
272+
_ => {
273+
return Err(error!(
274+
"Invalid dir_perms: {}. Use one of :read, :mutate, or :all",
275+
dir_perms_str
276+
))
277+
}
278+
};
279+
280+
let file_perms = match file_perms_str.as_str() {
281+
"read" => FilePerms::READ,
282+
"write" => FilePerms::WRITE,
283+
"all" => FilePerms::all(),
284+
_ => {
285+
return Err(error!(
286+
"Invalid file_perms: {}. Use one of :read, :write, or :all",
287+
file_perms_str
288+
))
289+
}
290+
};
263291

264-
let init_directory = RArray::new();
292+
let mapped_dir = MappedDirectory {
293+
host_path,
294+
guest_path,
295+
dir_perms,
296+
file_perms,
297+
};
265298

266299
let mut inner = rb_self.inner.borrow_mut();
267-
if inner.mapped_directories.is_none() {
268-
inner.mapped_directories = Some(init_directory.into());
269-
}
300+
inner.mapped_directories.push(mapped_dir);
270301

271-
let ruby = Ruby::get().unwrap();
272-
let mapped_directories = ruby.get_inner(inner.mapped_directories.unwrap());
273-
mapped_directories.push(mapped_directory).unwrap();
274-
rb_self
302+
Ok(rb_self)
275303
}
276304

277305
pub fn build_p1(&self, ruby: &Ruby) -> Result<WasiP1Ctx, Error> {
@@ -358,61 +386,16 @@ impl WasiConfig {
358386
deterministic_wasi_ctx::add_determinism_to_wasi_ctx_builder(&mut builder);
359387
}
360388

361-
if let Some(mapped_directories) = inner.mapped_directories.as_ref() {
362-
for item in unsafe { ruby.get_inner(*mapped_directories).as_slice() } {
363-
let mapped_directory = RArray::try_convert(*item)?;
364-
if mapped_directory.len() == 4 {
365-
let host_path =
366-
RString::try_convert(mapped_directory.entry(0)?)?.to_string()?;
367-
let guest_path =
368-
RString::try_convert(mapped_directory.entry(1)?)?.to_string()?;
369-
let dir_perms = Symbol::from_value(mapped_directory.entry(2)?)
370-
.unwrap()
371-
.name()?;
372-
let file_perms = Symbol::from_value(mapped_directory.entry(3)?)
373-
.unwrap()
374-
.name()?;
375-
376-
let host_path_dir = Path::new(&host_path);
377-
let guest_path_path = guest_path.as_str();
378-
379-
// Convert to FilePerms and DirPerms enums
380-
let dir_perms_flags;
381-
match dir_perms {
382-
std::borrow::Cow::Borrowed("read") => dir_perms_flags = DirPerms::READ,
383-
std::borrow::Cow::Borrowed("mutate") => dir_perms_flags = DirPerms::MUTATE,
384-
std::borrow::Cow::Borrowed("all") => dir_perms_flags = DirPerms::all(),
385-
_ => {
386-
return Err(error!(
387-
"Invalid dir_perms: {}. Use one of :read, :mutate, or :all",
388-
dir_perms
389-
))
390-
}
391-
}
392-
393-
let file_perms_flags;
394-
match file_perms {
395-
std::borrow::Cow::Borrowed("read") => file_perms_flags = FilePerms::READ,
396-
std::borrow::Cow::Borrowed("write") => file_perms_flags = FilePerms::WRITE,
397-
std::borrow::Cow::Borrowed("all") => file_perms_flags = FilePerms::all(),
398-
_ => {
399-
return Err(error!(
400-
"Invalid file_perms: {}. Use one of :read, :write, or :all",
401-
file_perms
402-
))
403-
}
404-
}
405-
406-
builder
407-
.preopened_dir(
408-
host_path_dir,
409-
guest_path_path,
410-
dir_perms_flags,
411-
file_perms_flags,
412-
)
413-
.map_err(|e| error!("{}", e))?;
414-
}
415-
}
389+
// Process mapped directories using our Rust Vec
390+
for mapped_dir in &inner.mapped_directories {
391+
builder
392+
.preopened_dir(
393+
Path::new(&mapped_dir.host_path),
394+
&mapped_dir.guest_path,
395+
mapped_dir.dir_perms,
396+
mapped_dir.file_perms,
397+
)
398+
.map_err(|e| error!("{}", e))?;
416399
}
417400

418401
Ok(builder)

spec/unit/wasi_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ module Wasmtime
316316
end
317317

318318
it "does not accept invalid permissions" do
319-
wasi_config = WasiConfig.new
320-
.set_mapped_directory(tempfile_path("tmp"), "/tmp", :mutate, :invalid_permission)
321-
322-
expect { run_fs.call(wasi_config) }.to raise_error do |error|
319+
expect {
320+
WasiConfig.new
321+
.set_mapped_directory(tempfile_path("tmp"), "/tmp", :mutate, :invalid_permission)
322+
}.to raise_error do |error|
323323
expect(error).to be_a(Wasmtime::Error)
324324
expect(error.message).to match(/Invalid file_perms: invalid_permission. Use one of :read, :write, or :all/)
325325
end

0 commit comments

Comments
 (0)