MCPcopy Index your code
hub / github.com/RustPython/RustPython / new

Method new

crates/wasm/src/vm_class.rs:39–78  ·  view source on GitHub ↗
(id: String, inject_browser_module: bool)

Source from the content-addressed store, hash-verified

37
38impl StoredVirtualMachine {
39 fn new(id: String, inject_browser_module: bool) -> StoredVirtualMachine {
40 let mut settings = Settings::default();
41 settings.allow_external_library = false;
42
43 let mut builder = Interpreter::builder(settings);
44
45 #[cfg(feature = "freeze-stdlib")]
46 {
47 let defs = rustpython_stdlib::stdlib_module_defs(&builder.ctx);
48 builder = builder
49 .add_native_modules(&defs)
50 .add_frozen_modules(rustpython_pylib::FROZEN_STDLIB);
51 }
52
53 // Add wasm-specific modules
54 let js_def = js_module::module_def(&builder.ctx);
55 builder = builder.add_native_module(js_def);
56
57 if inject_browser_module {
58 let window_def = _window::module_def(&builder.ctx);
59 let browser_def = browser_module::module_def(&builder.ctx);
60 builder = builder
61 .add_native_modules(&[window_def, browser_def])
62 .add_frozen_modules(rustpython_vm::py_freeze!(dir = "../Lib"));
63 }
64
65 let interp = builder
66 .init_hook(move |vm| {
67 vm.wasm_id = Some(id);
68 })
69 .build();
70
71 let scope = interp.enter(|vm| vm.new_scope_with_builtins());
72
73 StoredVirtualMachine {
74 interp,
75 scope,
76 held_objects: RefCell::new(Vec::new()),
77 }
78 }
79}
80
81// It's fine that it's thread local, since WASM doesn't even have threads yet. thread_local!

Callers

nothing calls this directly

Calls 10

stdlib_module_defsFunction · 0.85
newFunction · 0.85
add_frozen_modulesMethod · 0.80
add_native_modulesMethod · 0.80
add_native_moduleMethod · 0.80
init_hookMethod · 0.80
SomeClass · 0.50
buildMethod · 0.45
enterMethod · 0.45

Tested by

no test coverage detected