|
| 1 | +use super::{ |
| 2 | + root, |
| 3 | + wasi_ctx_builder::{file_r, file_w, wasi_file}, |
| 4 | + WasiCtxBuilder, |
| 5 | +}; |
| 6 | +use crate::error; |
| 7 | +use deterministic_wasi_ctx::build_wasi_ctx as wasi_deterministic_ctx; |
| 8 | +use magnus::{ |
| 9 | + class, function, gc::Marker, method, prelude::*, typed_data::Obj, Error, Object, RString, |
| 10 | + RTypedData, Ruby, TypedData, Value, |
| 11 | +}; |
| 12 | +use std::{borrow::Borrow, cell::RefCell, fs::File, path::PathBuf}; |
| 13 | +use wasi_common::pipe::ReadPipe; |
| 14 | +use wasmtime_wasi::WasiCtx as WasiCtxImpl; |
| 15 | + |
| 16 | +/// @yard |
| 17 | +/// WASI context to be sent as {Store#new}’s +wasi_ctx+ keyword argument. |
| 18 | +/// |
| 19 | +/// Instance methods mutate the current object and return +self+. |
| 20 | +/// |
| 21 | +/// @see https://docs.rs/wasmtime-wasi/latest/wasmtime_wasi/struct.WasiCtx.html |
| 22 | +/// Wasmtime's Rust doc |
| 23 | +#[magnus::wrap(class = "Wasmtime::WasiCtx", size, free_immediately)] |
| 24 | +pub struct WasiCtx { |
| 25 | + inner: RefCell<WasiCtxImpl>, |
| 26 | +} |
| 27 | + |
| 28 | +type RbSelf = Obj<WasiCtx>; |
| 29 | + |
| 30 | +impl WasiCtx { |
| 31 | + /// @yard |
| 32 | + /// Create a new deterministic {WasiCtx}. See https://github.com/Shopify/deterministic-wasi-ctx for more details |
| 33 | + /// @return [WasiCtx] |
| 34 | + pub fn deterministic() -> Self { |
| 35 | + Self { |
| 36 | + inner: RefCell::new(wasi_deterministic_ctx()), |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// @yard |
| 41 | + /// Set stdin to read from the specified file. |
| 42 | + /// @param path [String] The path of the file to read from. |
| 43 | + /// @def set_stdin_file(path) |
| 44 | + /// @return [WasiCtxBuilder] +self+ |
| 45 | + fn set_stdin_file(rb_self: RbSelf, path: RString) -> RbSelf { |
| 46 | + let inner = rb_self.inner.borrow_mut(); |
| 47 | + let cs = file_r(path).map(wasi_file).unwrap(); |
| 48 | + inner.set_stdin(cs); |
| 49 | + rb_self |
| 50 | + } |
| 51 | + |
| 52 | + /// @yard |
| 53 | + /// Set stdin to the specified String. |
| 54 | + /// @param content [String] |
| 55 | + /// @def set_stdin_string(content) |
| 56 | + /// @return [WasiCtx] +self+ |
| 57 | + fn set_stdin_string(rb_self: RbSelf, content: RString) -> RbSelf { |
| 58 | + let inner = rb_self.inner.borrow_mut(); |
| 59 | + let str = unsafe { content.as_slice() }; |
| 60 | + let pipe = ReadPipe::from(str); |
| 61 | + inner.set_stdin(Box::new(pipe)); |
| 62 | + rb_self |
| 63 | + } |
| 64 | + |
| 65 | + /// @yard |
| 66 | + /// Set stdout to write to a file. Will truncate the file if it exists, |
| 67 | + /// otherwise try to create it. |
| 68 | + /// @param path [String] The path of the file to write to. |
| 69 | + /// @def set_stdout_file(path) |
| 70 | + /// @return [WasiCtx] +self+ |
| 71 | + fn set_stdout_file(rb_self: RbSelf, path: RString) -> RbSelf { |
| 72 | + let inner = rb_self.inner.borrow_mut(); |
| 73 | + let cs = file_w(path).map(wasi_file).unwrap(); |
| 74 | + inner.set_stdout(cs); |
| 75 | + rb_self |
| 76 | + } |
| 77 | + |
| 78 | + /// @yard |
| 79 | + /// Set stderr to write to a file. Will truncate the file if it exists, |
| 80 | + /// otherwise try to create it. |
| 81 | + /// @param path [String] The path of the file to write to. |
| 82 | + /// @def set_stderr_file(path) |
| 83 | + /// @return [WasiCtx] +self+ |
| 84 | + fn set_stderr_file(rb_self: RbSelf, path: RString) -> RbSelf { |
| 85 | + let inner = rb_self.inner.borrow_mut(); |
| 86 | + let cs = file_w(path).map(wasi_file).unwrap(); |
| 87 | + inner.set_stderr(cs); |
| 88 | + rb_self |
| 89 | + } |
| 90 | + |
| 91 | + pub fn from_inner(inner: WasiCtxImpl) -> Self { |
| 92 | + Self { |
| 93 | + inner: RefCell::new(inner), |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + pub fn get_inner(&self) -> WasiCtxImpl { |
| 98 | + return self.inner.borrow().clone(); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +pub fn init() -> Result<(), Error> { |
| 103 | + let class = root().define_class("WasiCtx", class::object())?; |
| 104 | + class.define_singleton_method("deterministic", function!(WasiCtx::deterministic, 0))?; |
| 105 | + class.define_method("set_stdin_file", method!(WasiCtx::set_stdin_file, 1))?; |
| 106 | + class.define_method("set_stdin_string", method!(WasiCtx::set_stdin_string, 1))?; |
| 107 | + class.define_method("set_stdout_file", method!(WasiCtx::set_stdout_file, 1))?; |
| 108 | + class.define_method("set_stderr_file", method!(WasiCtx::set_stderr_file, 1))?; |
| 109 | + Ok(()) |
| 110 | +} |
0 commit comments