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 < OnceCell < 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
+ OnceCell :: with_value ( 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,16 @@ 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 (
74
+ ENGINE
75
+ . get ( )
76
+ . expect ( "Engine should be initialized all time" )
77
+ . lock ( )
78
+ . clone ( ) ,
79
+ ) ,
46
80
tty : None ,
47
81
source : Arc :: new ( MultiSource :: new ( ) ) ,
48
82
module_cache : Arc :: new ( cache) ,
@@ -52,3 +86,21 @@ pub fn build_wasi_runtime(
52
86
53
87
Some ( Arc :: new ( rt) )
54
88
}
89
+
90
+ /// Creates an instnace of [Store] with custom engine instead of default one to
91
+ /// disable simd for certain platform targets
92
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
93
+ #[ allow( unused_mut) ]
94
+ pub ( crate ) fn new_store ( ) -> Store {
95
+ let engine = ENGINE
96
+ . get ( )
97
+ . expect ( "Engine should be initialized all time" )
98
+ . lock ( )
99
+ . clone ( ) ;
100
+ Store :: new ( engine)
101
+ }
102
+
103
+ #[ cfg( target_arch = "wasm32" ) ]
104
+ pub ( crate ) fn new_store ( ) -> Store {
105
+ Store :: default ( )
106
+ }
0 commit comments