@@ -74,6 +74,13 @@ pub struct VirtualMachine {
74
74
75
75
pub const NSIG : usize = 64 ;
76
76
77
+ #[ derive( Copy , Clone ) ]
78
+ pub enum InitParameter {
79
+ NoInitialize ,
80
+ InitializeInternal ,
81
+ InitializeExternal ,
82
+ }
83
+
77
84
/// Struct containing all kind of settings for the python vm.
78
85
pub struct PySettings {
79
86
/// -d command line switch
@@ -109,17 +116,9 @@ pub struct PySettings {
109
116
/// sys.argv
110
117
pub argv : Vec < String > ,
111
118
112
- /// Initialize VM after VM created
113
- /// - Some(true):
114
- /// VM will be created and initialized with the importers, which require external
115
- /// filesystem access
116
- /// - Some(false):
117
- /// VM will be created and initialized with the importers wihout external filesystem access
118
- /// - None:
119
- /// VM will be created but not initialized, so it is ready to be injected with modules.
120
- /// After injection, the `initialize_as_external` or `initialize_without_external`
121
- /// should be called, such that the VM will be ready to use.
122
- pub initialize_with_external_importer : Option < bool > ,
119
+ /// Initialization parameter to decide to initialize or not,
120
+ /// and to decide the importer required external filesystem access or not
121
+ pub initialization_parameter : InitParameter ,
123
122
}
124
123
125
124
/// Trace events for sys.settrace and sys.setprofile.
@@ -153,7 +152,7 @@ impl Default for PySettings {
153
152
dont_write_bytecode : false ,
154
153
path_list : vec ! [ ] ,
155
154
argv : vec ! [ ] ,
156
- initialize_with_external_importer : Some ( true ) ,
155
+ initialization_parameter : InitParameter :: InitializeExternal ,
157
156
}
158
157
}
159
158
}
@@ -181,7 +180,7 @@ impl VirtualMachine {
181
180
let profile_func = RefCell :: new ( ctx. none ( ) ) ;
182
181
let trace_func = RefCell :: new ( ctx. none ( ) ) ;
183
182
let signal_handlers = RefCell :: new ( arr ! [ ctx. none( ) ; 64 ] ) ;
184
- let initialize_parameter = settings. initialize_with_external_importer ;
183
+ let initialize_parameter = settings. initialization_parameter ;
185
184
186
185
let mut vm = VirtualMachine {
187
186
builtins : builtins. clone ( ) ,
@@ -215,37 +214,32 @@ impl VirtualMachine {
215
214
vm. new_str ( "sys" . to_owned ( ) ) ,
216
215
vm. get_none ( ) ,
217
216
) ;
218
- if let Some ( with_external_importer) = initialize_parameter {
219
- vm. _initialize ( with_external_importer) ;
220
- vm. initialized = true ;
221
- }
217
+ vm. initialize ( initialize_parameter) ;
222
218
vm
223
219
}
224
220
225
- fn _initialize ( & mut self , with_external_importer : bool ) {
221
+ pub fn initialize ( & mut self , initialize_parameter : InitParameter ) {
226
222
flame_guard ! ( "init VirtualMachine" ) ;
227
223
228
- if self . initialized {
229
- panic ! ( "Double Initialize Error" ) ;
230
- }
231
-
232
- builtins:: make_module ( self , self . builtins . clone ( ) ) ;
233
- sysmodule:: make_module ( self , self . sys_module . clone ( ) , self . builtins . clone ( ) ) ;
234
-
235
- #[ cfg( not( target_arch = "wasm32" ) ) ]
236
- import:: import_builtin ( self , "signal" ) . expect ( "Couldn't initialize signal module" ) ;
224
+ match initialize_parameter {
225
+ InitParameter :: NoInitialize => { }
226
+ _ => {
227
+ if self . initialized {
228
+ panic ! ( "Double Initialize Error" ) ;
229
+ }
237
230
238
- import:: init_importlib ( self , with_external_importer) . expect ( "Initialize importlib fail" ) ;
231
+ builtins:: make_module ( self , self . builtins . clone ( ) ) ;
232
+ sysmodule:: make_module ( self , self . sys_module . clone ( ) , self . builtins . clone ( ) ) ;
239
233
240
- self . initialized = true ;
241
- }
234
+ # [ cfg ( not ( target_arch = "wasm32" ) ) ]
235
+ import :: import_builtin ( self , "signal" ) . expect ( "Couldn't initialize signal module" ) ;
242
236
243
- pub fn initialize_with_external_importer ( & mut self ) {
244
- self . _initialize ( true ) ;
245
- }
237
+ import:: init_importlib ( self , initialize_parameter)
238
+ . expect ( "Initialize importlib fail" ) ;
246
239
247
- pub fn initialize_without_external_importer ( & mut self ) {
248
- self . _initialize ( false ) ;
240
+ self . initialized = true ;
241
+ }
242
+ }
249
243
}
250
244
251
245
pub fn run_code_obj ( & self , code : PyCodeRef , scope : Scope ) -> PyResult {
0 commit comments