-
Notifications
You must be signed in to change notification settings - Fork 21
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
30c7aae
b29b4e5
18ab998
0477a2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
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, | ||
}; | ||
use std::cell::RefCell; | ||
use std::path::Path; | ||
use std::{fs::File, path::PathBuf}; | ||
use wasi_common::pipe::{ReadPipe, WritePipe}; | ||
|
||
|
@@ -47,6 +49,7 @@ struct WasiCtxBuilderInner { | |
stderr: Option<WriteStream>, | ||
env: Option<Opaque<RHash>>, | ||
args: Option<Opaque<RArray>>, | ||
mapped_directories: Option<Opaque<RArray>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
impl WasiCtxBuilderInner { | ||
|
@@ -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(); | ||
|
@@ -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) | ||
} | ||
|
@@ -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(()) | ||
|
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not bump the version number There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
end |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.