11use std:: { collections:: HashMap , env, path:: PathBuf , time:: SystemTime } ;
22
33use magnus:: {
4- eval, exception , function, method,
4+ eval, function, method,
55 prelude:: * ,
66 value:: { self , InnerValue } ,
77 wrap, Error , ExceptionClass , RModule , Ruby ,
@@ -37,14 +37,19 @@ struct WasiVfsInner {
3737#[ wrap( class = "RubyWasmExt::WasiVfs" ) ]
3838struct WasiVfs ( std:: cell:: RefCell < WasiVfsInner > ) ;
3939
40+ /// Create a `magnus::Error` using Ruby's `StandardError`.
41+ ///
42+ /// Panics if called when no Ruby VM is available.
43+ fn ruby_standard_error ( message : impl Into < String > ) -> Error {
44+ let ruby = magnus:: Ruby :: get ( ) . expect ( "Ruby VM is not available" ) ;
45+ Error :: new ( ruby. exception_standard_error ( ) , message. into ( ) )
46+ }
47+
4048impl WasiVfs {
4149 fn run_cli ( args : Vec < String > ) -> Result < ( ) , Error > {
42- wasi_vfs_cli:: App :: from_iter ( args) . execute ( ) . map_err ( |e| {
43- Error :: new (
44- exception:: standard_error ( ) ,
45- format ! ( "failed to run wasi vfs cli: {}" , e) ,
46- )
47- } )
50+ wasi_vfs_cli:: App :: from_iter ( args)
51+ . execute ( )
52+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to run wasi vfs cli: {}" , e) ) )
4853 }
4954
5055 fn new ( ) -> Self {
@@ -60,12 +65,7 @@ impl WasiVfs {
6065
6166 fn pack ( & self , wasm_bytes : bytes:: Bytes ) -> Result < bytes:: Bytes , Error > {
6267 let output_bytes = wasi_vfs_cli:: pack ( & wasm_bytes, self . 0 . borrow ( ) . map_dirs . clone ( ) )
63- . map_err ( |e| {
64- Error :: new (
65- exception:: standard_error ( ) ,
66- format ! ( "failed to pack wasi vfs: {}" , e) ,
67- )
68- } ) ?;
68+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to pack wasi vfs: {}" , e) ) ) ?;
6969 Ok ( output_bytes. into ( ) )
7070 }
7171}
@@ -83,35 +83,27 @@ impl ComponentLink {
8383 & self ,
8484 body : impl FnOnce ( wit_component:: Linker ) -> Result < wit_component:: Linker , Error > ,
8585 ) -> Result < ( ) , Error > {
86- let mut linker = self . 0 . take ( ) . ok_or_else ( || {
87- Error :: new (
88- exception:: standard_error ( ) ,
89- "linker is already consumed" . to_string ( ) ,
90- )
91- } ) ?;
86+ let mut linker = self
87+ . 0
88+ . take ( )
89+ . ok_or_else ( || ruby_standard_error ( "linker is already consumed" ) ) ?;
9290 linker = body ( linker) ?;
9391 self . 0 . replace ( Some ( linker) ) ;
9492 Ok ( ( ) )
9593 }
9694
9795 fn library ( & self , name : String , module : bytes:: Bytes , dl_openable : bool ) -> Result < ( ) , Error > {
9896 self . linker ( |linker| {
99- linker. library ( & name, & module, dl_openable) . map_err ( |e| {
100- Error :: new (
101- exception:: standard_error ( ) ,
102- format ! ( "failed to link library: {}" , e) ,
103- )
104- } )
97+ linker
98+ . library ( & name, & module, dl_openable)
99+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to link library: {}" , e) ) )
105100 } )
106101 }
107102 fn adapter ( & self , name : String , module : bytes:: Bytes ) -> Result < ( ) , Error > {
108103 self . linker ( |linker| {
109- linker. adapter ( & name, & module) . map_err ( |e| {
110- Error :: new (
111- exception:: standard_error ( ) ,
112- format ! ( "failed to link adapter: {}" , e) ,
113- )
114- } )
104+ linker
105+ . adapter ( & name, & module)
106+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to link adapter: {}" , e) ) )
115107 } )
116108 }
117109 fn validate ( & self , validate : bool ) -> Result < ( ) , Error > {
@@ -128,18 +120,14 @@ impl ComponentLink {
128120 }
129121 fn encode ( & self ) -> Result < bytes:: Bytes , Error > {
130122 // Take the linker out of the cell and consume it
131- let linker = self . 0 . borrow_mut ( ) . take ( ) . ok_or_else ( || {
132- Error :: new (
133- exception:: standard_error ( ) ,
134- "linker is already consumed" . to_string ( ) ,
135- )
136- } ) ?;
137- let encoded = linker. encode ( ) . map_err ( |e| {
138- Error :: new (
139- exception:: standard_error ( ) ,
140- format ! ( "failed to encode linker: {}" , e) ,
141- )
142- } ) ?;
123+ let linker = self
124+ . 0
125+ . borrow_mut ( )
126+ . take ( )
127+ . ok_or_else ( || ruby_standard_error ( "linker is already consumed" ) ) ?;
128+ let encoded = linker
129+ . encode ( )
130+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to encode linker: {}" , e) ) ) ?;
143131 Ok ( encoded. into ( ) )
144132 }
145133}
@@ -160,12 +148,10 @@ impl ComponentEncode {
160148 wit_component:: ComponentEncoder ,
161149 ) -> Result < wit_component:: ComponentEncoder , Error > ,
162150 ) -> Result < ( ) , Error > {
163- let mut encoder = self . 0 . take ( ) . ok_or_else ( || {
164- Error :: new (
165- exception:: standard_error ( ) ,
166- "encoder is already consumed" . to_string ( ) ,
167- )
168- } ) ?;
151+ let mut encoder = self
152+ . 0
153+ . take ( )
154+ . ok_or_else ( || ruby_standard_error ( "encoder is already consumed" ) ) ?;
169155 encoder = body ( encoder) ?;
170156 self . 0 . replace ( Some ( encoder) ) ;
171157 Ok ( ( ) )
@@ -177,23 +163,17 @@ impl ComponentEncode {
177163
178164 fn adapter ( & self , name : String , module : bytes:: Bytes ) -> Result < ( ) , Error > {
179165 self . encoder ( |encoder| {
180- encoder. adapter ( & name, & module) . map_err ( |e| {
181- Error :: new (
182- exception:: standard_error ( ) ,
183- format ! ( "failed to encode adapter: {}" , e) ,
184- )
185- } )
166+ encoder
167+ . adapter ( & name, & module)
168+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to encode adapter: {}" , e) ) )
186169 } )
187170 }
188171
189172 fn module ( & self , module : bytes:: Bytes ) -> Result < ( ) , Error > {
190173 self . encoder ( |encoder| {
191- encoder. module ( & module) . map_err ( |e| {
192- Error :: new (
193- exception:: standard_error ( ) ,
194- format ! ( "failed to encode module: {}" , e) ,
195- )
196- } )
174+ encoder
175+ . module ( & module)
176+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to encode module: {}" , e) ) )
197177 } )
198178 }
199179
@@ -207,18 +187,14 @@ impl ComponentEncode {
207187
208188 fn encode ( & self ) -> Result < bytes:: Bytes , Error > {
209189 // Take the encoder out of the cell and consume it
210- let encoder = self . 0 . borrow_mut ( ) . take ( ) . ok_or_else ( || {
211- Error :: new (
212- exception:: standard_error ( ) ,
213- "encoder is already consumed" . to_string ( ) ,
214- )
215- } ) ?;
216- let encoded = encoder. encode ( ) . map_err ( |e| {
217- Error :: new (
218- exception:: standard_error ( ) ,
219- format ! ( "failed to encode component: {}" , e) ,
220- )
221- } ) ?;
190+ let encoder = self
191+ . 0
192+ . borrow_mut ( )
193+ . take ( )
194+ . ok_or_else ( || ruby_standard_error ( "encoder is already consumed" ) ) ?;
195+ let encoded = encoder
196+ . encode ( )
197+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to encode component: {}" , e) ) ) ?;
222198 Ok ( encoded. into ( ) )
223199 }
224200}
@@ -235,12 +211,10 @@ impl WasiVirt {
235211 & self ,
236212 body : impl FnOnce ( & mut wasi_virt:: WasiVirt ) -> Result < R , Error > ,
237213 ) -> Result < R , Error > {
238- let mut virt = self . 0 . take ( ) . ok_or_else ( || {
239- Error :: new (
240- exception:: standard_error ( ) ,
241- "wasi virt is already consumed" . to_string ( ) ,
242- )
243- } ) ?;
214+ let mut virt = self
215+ . 0
216+ . take ( )
217+ . ok_or_else ( || ruby_standard_error ( "wasi virt is already consumed" ) ) ?;
244218 let result = body ( & mut virt) ?;
245219 self . 0 . replace ( Some ( virt) ) ;
246220 Ok ( result)
@@ -269,10 +243,7 @@ impl WasiVirt {
269243 fn finish ( & self ) -> Result < bytes:: Bytes , Error > {
270244 self . virt ( |virt| {
271245 let result = virt. finish ( ) . map_err ( |e| {
272- Error :: new (
273- exception:: standard_error ( ) ,
274- format ! ( "failed to generate virtualization adapter: {}" , e) ,
275- )
246+ ruby_standard_error ( format ! ( "failed to generate virtualization adapter: {}" , e) )
276247 } ) ?;
277248 Ok ( result. adapter . into ( ) )
278249 } )
@@ -282,32 +253,21 @@ impl WasiVirt {
282253 let virt_adapter = self . finish ( ) ?;
283254 let tmpdir = env:: temp_dir ( ) ;
284255 let tmp_virt = tmpdir. join ( format ! ( "virt{}.wasm" , timestamp( ) ) ) ;
285- std:: fs:: write ( & tmp_virt, & virt_adapter) . map_err ( |e| {
286- Error :: new (
287- exception:: standard_error ( ) ,
288- format ! ( "failed to write virt adapter: {}" , e) ,
289- )
290- } ) ?;
256+ std:: fs:: write ( & tmp_virt, & virt_adapter)
257+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to write virt adapter: {}" , e) ) ) ?;
291258 let tmp_component = tmpdir. join ( format ! ( "component{}.wasm" , timestamp( ) ) ) ;
292- std:: fs:: write ( & tmp_component, & component_bytes) . map_err ( |e| {
293- Error :: new (
294- exception:: standard_error ( ) ,
295- format ! ( "failed to write component: {}" , e) ,
296- )
297- } ) ?;
259+ std:: fs:: write ( & tmp_component, & component_bytes)
260+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to write component: {}" , e) ) ) ?;
298261
299262 use wasm_compose:: { composer, config} ;
300263 let config = config:: Config {
301264 definitions : vec ! [ tmp_virt] ,
302265 ..Default :: default ( )
303266 } ;
304267 let composer = composer:: ComponentComposer :: new ( & tmp_component, & config) ;
305- let composed = composer. compose ( ) . map_err ( |e| {
306- Error :: new (
307- exception:: standard_error ( ) ,
308- format ! ( "failed to compose component: {}" , e) ,
309- )
310- } ) ?;
268+ let composed = composer
269+ . compose ( )
270+ . map_err ( |e| ruby_standard_error ( format ! ( "failed to compose component: {}" , e) ) ) ?;
311271 return Ok ( composed. into ( ) ) ;
312272
313273 fn timestamp ( ) -> u64 {
@@ -322,7 +282,7 @@ impl WasiVirt {
322282#[ magnus:: init]
323283fn init ( ruby : & Ruby ) -> Result < ( ) , Error > {
324284 let module = RUBY_WASM . get_inner_with ( ruby) ;
325- module. define_error ( "Error" , exception :: standard_error ( ) ) ?;
285+ module. define_error ( "Error" , ruby . exception_standard_error ( ) ) ?;
326286
327287 module. define_singleton_method ( "preinitialize" , function ! ( preinit, 1 ) ) ?;
328288
0 commit comments