(
&mut self,
name: InternedString<'gc>,
)
| 90 | } |
| 91 | |
| 92 | pub fn get_or_load_module( |
| 93 | &mut self, |
| 94 | name: InternedString<'gc>, |
| 95 | ) -> Result<ModuleSource, VmError> { |
| 96 | // Return early if module is already loaded |
| 97 | if self.modules.contains_key(&name) { |
| 98 | return Ok(ModuleSource::Cached); |
| 99 | } |
| 100 | |
| 101 | let module_name = name.to_str().unwrap(); |
| 102 | |
| 103 | if module_name.starts_with("std.") { |
| 104 | // This is a standard library module, it should be registered during VM initialization |
| 105 | // If we reach here, it means the module wasn't registered |
| 106 | return Err(VmError::RuntimeError(format!( |
| 107 | "Standard library module '{}' not found.", |
| 108 | module_name |
| 109 | ))); |
| 110 | } |
| 111 | |
| 112 | // For user script modules, find and load the source |
| 113 | let module_path = self.find_module_file(&name)?; |
| 114 | let source = std::fs::read_to_string(&module_path) |
| 115 | .map_err(|e| VmError::RuntimeError(format!("Failed to read module: {}", e)))?; |
| 116 | |
| 117 | Ok(ModuleSource::New { |
| 118 | source, |
| 119 | path: module_path, |
| 120 | }) |
| 121 | } |
| 122 | |
| 123 | fn find_module_file(&self, name: &InternedString) -> Result<PathBuf, VmError> { |
| 124 | let module_name = name.to_str().unwrap(); |
no test coverage detected