mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-17 01:51:39 +09:00
Merge pull request #914 from palaviv/wasm-import
Add browser.load_module
This commit is contained in:
@@ -31,29 +31,39 @@ pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &s
|
||||
.map_err(|e| vm.new_exception(notfound_error.clone(), e))?;
|
||||
let source = util::read_file(file_path.as_path())
|
||||
.map_err(|e| vm.new_exception(import_error.clone(), e.to_string()))?;
|
||||
let code_obj = compile::compile(
|
||||
|
||||
import_file(
|
||||
vm,
|
||||
&source,
|
||||
&compile::Mode::Exec,
|
||||
module_name,
|
||||
file_path.to_str().unwrap().to_string(),
|
||||
source,
|
||||
)
|
||||
.map_err(|err| vm.new_syntax_error(&err))?;
|
||||
// trace!("Code object: {:?}", code_obj);
|
||||
|
||||
let attrs = vm.ctx.new_dict();
|
||||
attrs.set_item("__name__", vm.new_str(module_name.to_string()), vm)?;
|
||||
let module = vm.ctx.new_module(module_name, attrs.clone());
|
||||
|
||||
// Store module in cache to prevent infinite loop with mutual importing libs:
|
||||
sys_modules.set_item(module_name, module.clone(), vm)?;
|
||||
|
||||
// Execute main code in module:
|
||||
vm.run_code_obj(code_obj, Scope::new(None, attrs))?;
|
||||
|
||||
Ok(module)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn import_file(
|
||||
vm: &VirtualMachine,
|
||||
module_name: &str,
|
||||
file_path: String,
|
||||
content: String,
|
||||
) -> PyResult {
|
||||
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap();
|
||||
let code_obj = compile::compile(vm, &content, &compile::Mode::Exec, file_path)
|
||||
.map_err(|err| vm.new_syntax_error(&err))?;
|
||||
// trace!("Code object: {:?}", code_obj);
|
||||
|
||||
let attrs = vm.ctx.new_dict();
|
||||
attrs.set_item("__name__", vm.new_str(module_name.to_string()), vm)?;
|
||||
let module = vm.ctx.new_module(module_name, attrs.clone());
|
||||
|
||||
// Store module in cache to prevent infinite loop with mutual importing libs:
|
||||
sys_modules.set_item(module_name, module.clone(), vm)?;
|
||||
|
||||
// Execute main code in module:
|
||||
vm.run_code_obj(code_obj, Scope::new(None, attrs))?;
|
||||
Ok(module)
|
||||
}
|
||||
|
||||
fn find_source(vm: &VirtualMachine, current_path: PathBuf, name: &str) -> Result<PathBuf, String> {
|
||||
let sys_path = vm.get_attribute(vm.sys_module.clone(), "path").unwrap();
|
||||
let mut paths: Vec<PathBuf> = objsequence::get_elements(&sys_path)
|
||||
|
||||
Reference in New Issue
Block a user