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

Function load_module

crates/wasm/src/browser_module.rs:221–253  ·  view source on GitHub ↗
(module: PyStrRef, path: PyStrRef, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

219
220 #[pyfunction]
221 fn load_module(module: PyStrRef, path: PyStrRef, vm: &VirtualMachine) -> PyResult {
222 let weak_vm = weak_vm(vm);
223
224 let opts = web_sys::RequestInit::new();
225 opts.set_method("GET");
226
227 let request = web_sys::Request::new_with_str_and_init(path.expect_str(), &opts)
228 .map_err(|err| convert::js_py_typeerror(vm, err))?;
229
230 let window = window();
231 let request_prom = window.fetch_with_request(&request);
232
233 let future = async move {
234 let val = JsFuture::from(request_prom).await?;
235 let response = val
236 .dyn_into::<web_sys::Response>()
237 .expect("val to be of type Response");
238 let text = JsFuture::from(response.text()?).await?;
239 let stored_vm = &weak_vm
240 .upgrade()
241 .expect("that the vm is valid when the promise resolves");
242 stored_vm.interp.enter(move |vm| {
243 let resp_text = text.as_string().unwrap();
244 let res = import_source(vm, module.expect_str(), &resp_text);
245 match res {
246 Ok(_) => Ok(JsValue::null()),
247 Err(err) => Err(convert::py_err_to_js_err(vm, &err)),
248 }
249 })
250 };
251
252 Ok(PyPromise::from_future(future).into_pyobject(vm))
253 }
254}

Callers

nothing calls this directly

Calls 13

weak_vmFunction · 0.85
newFunction · 0.85
js_py_typeerrorFunction · 0.85
windowFunction · 0.85
import_sourceFunction · 0.85
py_err_to_js_errFunction · 0.85
expect_strMethod · 0.80
ErrClass · 0.50
upgradeMethod · 0.45
enterMethod · 0.45
unwrapMethod · 0.45
as_stringMethod · 0.45

Tested by

no test coverage detected