@@ -5,20 +5,16 @@ use super::{
55} ;
66use crate :: error;
77use crate :: helpers:: OutputLimitedBuffer ;
8- use deterministic_wasi_ctx:: build_wasi_ctx as wasi_deterministic_ctx;
98use magnus:: {
109 class, function, gc:: Marker , method, prelude:: * , typed_data:: Obj , Error , Object , RString ,
1110 RTypedData , Ruby , TypedData , Value ,
1211} ;
1312use std:: { borrow:: Borrow , cell:: RefCell , fs:: File , path:: PathBuf } ;
14- use wasi_common:: pipe:: { ReadPipe , WritePipe } ;
15- use wasi_common:: WasiCtx as WasiCtxImpl ;
13+ use wasmtime_wasi:: preview1:: WasiP1Ctx as WasiCtxImpl ;
1614
1715/// @yard
1816/// WASI context to be sent as {Store#new}’s +wasi_ctx+ keyword argument.
1917///
20- /// Instance methods mutate the current object and return +self+.
21- ///
2218/// @see https://docs.rs/wasmtime-wasi/latest/wasmtime_wasi/struct.WasiCtx.html
2319/// Wasmtime's Rust doc
2420#[ magnus:: wrap( class = "Wasmtime::WasiCtx" , size, free_immediately) ]
@@ -29,96 +25,6 @@ pub struct WasiCtx {
2925type RbSelf = Obj < WasiCtx > ;
3026
3127impl WasiCtx {
32- /// @yard
33- /// Create a new deterministic {WasiCtx}. See https://github.com/Shopify/deterministic-wasi-ctx for more details
34- /// @return [WasiCtx]
35- pub fn deterministic ( ) -> Self {
36- Self {
37- inner : RefCell :: new ( wasi_deterministic_ctx ( ) ) ,
38- }
39- }
40-
41- /// @yard
42- /// Set stdin to read from the specified file.
43- /// @def set_stdin_file(path)
44- /// @param path [String] The path of the file to read from.
45- /// @return [WasiCtxBuilder] +self+
46- fn set_stdin_file ( rb_self : RbSelf , path : RString ) -> RbSelf {
47- let inner = rb_self. inner . borrow_mut ( ) ;
48- let cs = file_r ( path) . map ( wasi_file) . unwrap ( ) ;
49- inner. set_stdin ( cs) ;
50- rb_self
51- }
52-
53- /// @yard
54- /// Set stdin to the specified String.
55- /// @def set_stdin_string(content)
56- /// @param content [String]
57- /// @return [WasiCtx] +self+
58- fn set_stdin_string ( rb_self : RbSelf , content : RString ) -> RbSelf {
59- let inner = rb_self. inner . borrow_mut ( ) ;
60- let str = unsafe { content. as_slice ( ) } ;
61- let pipe = ReadPipe :: from ( str) ;
62- inner. set_stdin ( Box :: new ( pipe) ) ;
63- rb_self
64- }
65-
66- /// @yard
67- /// Set stdout to write to a file. Will truncate the file if it exists,
68- /// otherwise try to create it.
69- /// @def set_stdout_file(path)
70- /// @param path [String] The path of the file to write to.
71- /// @return [WasiCtx] +self+
72- fn set_stdout_file ( rb_self : RbSelf , path : RString ) -> RbSelf {
73- let inner = rb_self. inner . borrow_mut ( ) ;
74- let cs = file_w ( path) . map ( wasi_file) . unwrap ( ) ;
75- inner. set_stdout ( cs) ;
76- rb_self
77- }
78-
79- /// @yard
80- /// Set stdout to write to a string buffer.
81- /// If the string buffer is frozen, Wasm execution will raise a Wasmtime::Error error.
82- /// No encoding checks are done on the resulting string, it is the caller's responsibility to ensure the string contains a valid encoding
83- /// @def set_stdout_buffer(buffer, capacity)
84- /// @param buffer [String] The string buffer to write to.
85- /// @param capacity [Integer] The maximum number of bytes that can be written to the output buffer.
86- /// @return [WasiCtx] +self+
87- fn set_stdout_buffer ( rb_self : RbSelf , buffer : RString , capacity : usize ) -> RbSelf {
88- let inner = rb_self. inner . borrow_mut ( ) ;
89- let pipe = WritePipe :: new ( OutputLimitedBuffer :: new ( buffer. into ( ) , capacity) ) ;
90- inner. set_stdout ( Box :: new ( pipe) ) ;
91- rb_self
92- }
93-
94- /// @yard
95- /// Set stderr to write to a file. Will truncate the file if it exists,
96- /// otherwise try to create it.
97- /// @def set_stderr_file(path)
98- /// @param path [String] The path of the file to write to.
99- /// @return [WasiCtx] +self+
100- fn set_stderr_file ( rb_self : RbSelf , path : RString ) -> RbSelf {
101- let inner = rb_self. inner . borrow_mut ( ) ;
102- let cs = file_w ( path) . map ( wasi_file) . unwrap ( ) ;
103- inner. set_stderr ( cs) ;
104- rb_self
105- }
106-
107- /// @yard
108- /// Set stderr to write to a string buffer.
109- /// If the string buffer is frozen, Wasm execution will raise a Wasmtime::Error error.
110- /// No encoding checks are done on the resulting string, it is the caller's responsibility to ensure the string contains a valid encoding
111- /// @def set_stderr_buffer(buffer, capacity)
112- /// @param buffer [String] The string buffer to write to.
113- /// @param capacity [Integer] The maximum number of bytes that can be written to the output buffer.
114- /// @return [WasiCtx] +self+
115- fn set_stderr_buffer ( rb_self : RbSelf , buffer : RString , capacity : usize ) -> RbSelf {
116- let inner = rb_self. inner . borrow_mut ( ) ;
117- let pipe = WritePipe :: new ( OutputLimitedBuffer :: new ( buffer. into ( ) , capacity) ) ;
118- inner. set_stderr ( Box :: new ( pipe) ) ;
119- rb_self
120- }
121-
12228 pub fn from_inner ( inner : WasiCtxImpl ) -> Self {
12329 Self {
12430 inner : RefCell :: new ( inner) ,
@@ -131,13 +37,6 @@ impl WasiCtx {
13137}
13238
13339pub fn init ( ) -> Result < ( ) , Error > {
134- let class = root ( ) . define_class ( "WasiCtx" , class:: object ( ) ) ?;
135- class. define_singleton_method ( "deterministic" , function ! ( WasiCtx :: deterministic, 0 ) ) ?;
136- class. define_method ( "set_stdin_file" , method ! ( WasiCtx :: set_stdin_file, 1 ) ) ?;
137- class. define_method ( "set_stdin_string" , method ! ( WasiCtx :: set_stdin_string, 1 ) ) ?;
138- class. define_method ( "set_stdout_file" , method ! ( WasiCtx :: set_stdout_file, 1 ) ) ?;
139- class. define_method ( "set_stdout_buffer" , method ! ( WasiCtx :: set_stdout_buffer, 2 ) ) ?;
140- class. define_method ( "set_stderr_file" , method ! ( WasiCtx :: set_stderr_file, 1 ) ) ?;
141- class. define_method ( "set_stderr_buffer" , method ! ( WasiCtx :: set_stderr_buffer, 2 ) ) ?;
40+ root ( ) . define_class ( "WasiCtx" , class:: object ( ) ) ?;
14241 Ok ( ( ) )
14342}
0 commit comments