@@ -9,7 +9,7 @@ use super::{
9
9
root,
10
10
store:: { Store , StoreContextValue , StoreData } ,
11
11
} ;
12
- use crate :: { define_rb_intern , err, error} ;
12
+ use crate :: { err, error, ruby_api :: errors } ;
13
13
use magnus:: {
14
14
block:: Proc , class, function, gc:: Marker , method, prelude:: * , scan_args, scan_args:: scan_args,
15
15
typed_data:: Obj , DataTypeFunctions , Error , Object , RArray , RHash , RString , Ruby , TypedData ,
@@ -18,18 +18,14 @@ use magnus::{
18
18
use std:: cell:: RefCell ;
19
19
use wasmtime:: Linker as LinkerImpl ;
20
20
21
- define_rb_intern ! (
22
- WASI => "wasi" ,
23
- ) ;
24
-
25
21
/// @yard
26
22
/// @see https://docs.rs/wasmtime/latest/wasmtime/struct.Linker.html Wasmtime's Rust doc
27
23
#[ derive( TypedData ) ]
28
24
#[ magnus( class = "Wasmtime::Linker" , size, mark, free_immediately) ]
29
25
pub struct Linker {
30
26
inner : RefCell < LinkerImpl < StoreData > > ,
31
27
refs : RefCell < Vec < Value > > ,
32
- has_wasi : bool ,
28
+ has_wasi : RefCell < bool > ,
33
29
}
34
30
35
31
unsafe impl Send for Linker { }
@@ -42,25 +38,15 @@ impl DataTypeFunctions for Linker {
42
38
43
39
impl Linker {
44
40
/// @yard
45
- /// @def new(engine, wasi: false )
41
+ /// @def new(engine)
46
42
/// @param engine [Engine]
47
- /// @param wasi [Boolean] Whether WASI should be defined in this Linker. Defaults to false.
48
43
/// @return [Linker]
49
- pub fn new ( args : & [ Value ] ) -> Result < Self , Error > {
50
- let args = scan_args:: scan_args :: < ( & Engine , ) , ( ) , ( ) , ( ) , _ , ( ) > ( args) ?;
51
- let kw = scan_args:: get_kwargs :: < _ , ( ) , ( Option < bool > , ) , ( ) > ( args. keywords , & [ ] , & [ * WASI ] ) ?;
52
- let ( engine, ) = args. required ;
53
- let wasi = kw. optional . 0 . unwrap_or ( false ) ;
54
-
55
- let mut inner: LinkerImpl < StoreData > = LinkerImpl :: new ( engine. get ( ) ) ;
56
- if wasi {
57
- wasmtime_wasi:: preview1:: add_to_linker_sync ( & mut inner, |s| s. wasi_ctx_mut ( ) )
58
- . map_err ( |e| error ! ( "{}" , e) ) ?
59
- }
44
+ pub fn new ( engine : & Engine ) -> Result < Self , Error > {
45
+ let inner: LinkerImpl < StoreData > = LinkerImpl :: new ( engine. get ( ) ) ;
60
46
Ok ( Self {
61
47
inner : RefCell :: new ( inner) ,
62
48
refs : Default :: default ( ) ,
63
- has_wasi : wasi ,
49
+ has_wasi : RefCell :: new ( false ) ,
64
50
} )
65
51
}
66
52
@@ -280,14 +266,8 @@ impl Linker {
280
266
/// @param mod [Module]
281
267
/// @return [Instance]
282
268
pub fn instantiate ( & self , store : Obj < Store > , module : & Module ) -> Result < Instance , Error > {
283
- if self . has_wasi && !store. context ( ) . data ( ) . has_wasi_ctx ( ) {
284
- return err ! (
285
- "Store is missing WASI configuration.\n \n \
286
- When using `wasi: true`, the Store given to\n \
287
- `Linker#instantiate` must have a WASI configuration.\n \
288
- To fix this, provide the `wasi_config` when creating the Store:\n \
289
- Wasmtime::Store.new(engine, wasi_config: WasiConfig.new)"
290
- ) ;
269
+ if * self . has_wasi . borrow ( ) && !store. context ( ) . data ( ) . has_wasi_p1_ctx ( ) {
270
+ return err ! ( "{}" , errors:: missing_wasi_p1_ctx_error( ) ) ;
291
271
}
292
272
293
273
self . inner
@@ -322,11 +302,18 @@ impl Linker {
322
302
let mut inner = self . inner . borrow_mut ( ) ;
323
303
deterministic_wasi_ctx:: replace_scheduling_functions ( & mut inner) . map_err ( |e| error ! ( "{e}" ) )
324
304
}
305
+
306
+ pub ( crate ) fn add_wasi_p1 ( & self ) -> Result < ( ) , Error > {
307
+ * self . has_wasi . borrow_mut ( ) = true ;
308
+ let mut inner = self . inner . borrow_mut ( ) ;
309
+ wasmtime_wasi:: preview1:: add_to_linker_sync ( & mut inner, |s| s. wasi_p1_ctx_mut ( ) )
310
+ . map_err ( |e| error ! ( "{e}" ) )
311
+ }
325
312
}
326
313
327
314
pub fn init ( ) -> Result < ( ) , Error > {
328
315
let class = root ( ) . define_class ( "Linker" , class:: object ( ) ) ?;
329
- class. define_singleton_method ( "new" , function ! ( Linker :: new, - 1 ) ) ?;
316
+ class. define_singleton_method ( "new" , function ! ( Linker :: new, 1 ) ) ?;
330
317
class. define_method ( "allow_shadowing=" , method ! ( Linker :: set_allow_shadowing, 1 ) ) ?;
331
318
class. define_method (
332
319
"allow_unknown_exports=" ,
0 commit comments