Skip to content

Implement preopened_dir using set_mapped_directories setting #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
wasmtime (29.0.0)
wasmtime (30.0.0)
rb_sys (~> 0.9.108)

GEM
Expand Down
40 changes: 39 additions & 1 deletion ext/src/ruby_api/wasi_ctx_builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use super::{root, WasiCtx};
use crate::error;
use crate::helpers::OutputLimitedBuffer;
use cap_std::fs::Dir;
use magnus::{
class, function, gc::Marker, method, typed_data::Obj, value::Opaque, DataTypeFunctions, Error,
Module, Object, RArray, RHash, RString, Ruby, TryConvert, TypedData,
Integer, Module, Object, RArray, RHash, RString, Ruby, TryConvert, TypedData,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see Integer being used, I think we can revert that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I will revert this.

};
use std::cell::RefCell;
use std::path::Path;
use std::{fs::File, path::PathBuf};
use wasi_common::pipe::{ReadPipe, WritePipe};

Expand Down Expand Up @@ -47,6 +49,7 @@ struct WasiCtxBuilderInner {
stderr: Option<WriteStream>,
env: Option<Opaque<RHash>>,
args: Option<Opaque<RArray>>,
mapped_directories: Option<Opaque<RArray>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When holding references to Ruby objects, we must GC mark them, otherwise the GC might collect the object. See WasiCtxBuilderInner::mark on L56 for how it's done.

}

impl WasiCtxBuilderInner {
Expand Down Expand Up @@ -222,6 +225,17 @@ impl WasiCtxBuilder {
rb_self
}

/// @yard
/// Set mapped directories to the specified +Array+.
/// @param mapped_directories [Array<Array<String>>]
/// @def set_mapped_directories(mapped_directories)
/// @return [WasiCtxBuilder] +self+
pub fn set_mapped_directories(rb_self: RbSelf, mapped_directories: RArray) -> RbSelf {
let mut inner = rb_self.inner.borrow_mut();
inner.mapped_directories = Some(mapped_directories.into());
rb_self
}

pub fn build(ruby: &Ruby, rb_self: RbSelf) -> Result<WasiCtx, Error> {
let mut builder = wasi_common::sync::WasiCtxBuilder::new();
let inner = rb_self.inner.borrow();
Expand Down Expand Up @@ -281,6 +295,25 @@ impl WasiCtxBuilder {
builder.envs(&env_vec).map_err(|e| error!("{}", e))?;
}

if let Some(mapped_directories) = inner.mapped_directories.as_ref() {
for item in unsafe { ruby.get_inner(*mapped_directories).as_slice() } {
let mapped_directory = RArray::try_convert(*item)?;
if mapped_directory.len() == 2 {
let host_path =
RString::try_convert(mapped_directory.entry(0)?)?.to_string()?;
let guest_path =
RString::try_convert(mapped_directory.entry(1)?)?.to_string()?;

let host_path_dir = Dir::from_std_file(File::open(host_path).unwrap());
let guest_path_path = PathBuf::from(guest_path.as_str());

builder
.preopened_dir(host_path_dir, guest_path_path)
.map_err(|e| error!("{}", e))?;
}
}
}

let ctx = WasiCtx::from_inner(builder.build());
Ok(ctx)
}
Expand Down Expand Up @@ -339,6 +372,11 @@ pub fn init() -> Result<(), Error> {

class.define_method("set_argv", method!(WasiCtxBuilder::set_argv, 1))?;

class.define_method(
"set_mapped_directories",
method!(WasiCtxBuilder::set_mapped_directories, 1),
)?;

class.define_method("build", method!(WasiCtxBuilder::build, 0))?;

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion lib/wasmtime/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Wasmtime
VERSION = "29.0.0"
VERSION = "30.0.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not bump the version number

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

end
Loading