1
1
use std:: { path:: PathBuf , sync:: Arc } ;
2
2
3
+ use parking_lot:: Mutex ;
4
+ use swc_common:: sync:: { Lazy , OnceCell } ;
5
+ use wasmer:: Store ;
3
6
use wasmer_wasix:: Runtime ;
4
7
8
+ /// A shared instance to plugin runtime engine.
9
+ /// ref: https://github.com/wasmerio/wasmer/issues/3793#issuecomment-1607117480
10
+ static ENGINE : Lazy < Mutex < wasmer:: Engine > > = Lazy :: new ( || {
11
+ // Use empty enumset to disable simd.
12
+ use enumset:: EnumSet ;
13
+ use wasmer:: { BaseTunables , CompilerConfig , EngineBuilder , Target , Triple } ;
14
+ let mut set = EnumSet :: new ( ) ;
15
+
16
+ // [TODO]: Should we use is_x86_feature_detected! macro instead?
17
+ #[ cfg( target_arch = "x86_64" ) ]
18
+ set. insert ( wasmer:: CpuFeature :: SSE2 ) ;
19
+ let target = Target :: new ( Triple :: host ( ) , set) ;
20
+
21
+ let config = wasmer_compiler_cranelift:: Cranelift :: default ( ) ;
22
+ let mut engine = EngineBuilder :: new ( Box :: new ( config) as Box < dyn CompilerConfig > )
23
+ . set_target ( Some ( target) )
24
+ . engine ( ) ;
25
+ let tunables = BaseTunables :: for_target ( engine. target ( ) ) ;
26
+ engine. set_tunables ( tunables) ;
27
+ parking_lot:: Mutex :: new ( wasmer:: Engine :: from ( engine) )
28
+ } ) ;
29
+
30
+ /// Dummy http client for wasix runtime to avoid instantiation failure for the
31
+ /// default pluggable runtime. We don't support network in the host runtime
32
+ /// anyway (we init vnet instead), and for the default runtime mostly it's for
33
+ /// the wapm registry which is redundant for the plugin.
5
34
#[ derive( Debug ) ]
6
35
struct StubHttpClient ;
7
36
@@ -38,11 +67,10 @@ pub fn build_wasi_runtime(
38
67
SharedCache :: default ( ) . with_fallback ( wasmer_wasix:: runtime:: module_cache:: in_memory ( ) ) ;
39
68
40
69
let dummy_loader = BuiltinPackageLoader :: new_with_client ( "." , Arc :: new ( StubHttpClient ) ) ;
41
-
42
70
let rt = PluggableRuntime {
43
71
rt : Arc :: new ( TokioTaskManager :: shared ( ) ) ,
44
72
networking : Arc :: new ( virtual_net:: UnsupportedVirtualNetworking :: default ( ) ) ,
45
- engine : Some ( wasmer :: Engine :: default ( ) ) ,
73
+ engine : Some ( ENGINE . lock ( ) . clone ( ) ) ,
46
74
tty : None ,
47
75
source : Arc :: new ( MultiSource :: new ( ) ) ,
48
76
module_cache : Arc :: new ( cache) ,
@@ -52,3 +80,17 @@ pub fn build_wasi_runtime(
52
80
53
81
Some ( Arc :: new ( rt) )
54
82
}
83
+
84
+ /// Creates an instnace of [Store] with custom engine instead of default one to
85
+ /// disable simd for certain platform targets
86
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
87
+ #[ allow( unused_mut) ]
88
+ pub ( crate ) fn new_store ( ) -> Store {
89
+ let engine = ENGINE . lock ( ) . clone ( ) ;
90
+ Store :: new ( engine)
91
+ }
92
+
93
+ #[ cfg( target_arch = "wasm32" ) ]
94
+ pub ( crate ) fn new_store ( ) -> Store {
95
+ Store :: default ( )
96
+ }
0 commit comments