Appearance
| 1 | namespace IR.WasmGC is | |
| 2 | use IO.File | |
| 3 | use IO.Path | |
| 4 | use IO.StreamReader | |
| 5 | ||
| 6 | /// The JavaScript loader that runs a module: instantiates it with the | |
| 7 | /// host imports, starts it, and reports its exit status. | |
| 8 | class WASMGC_LOADER is | |
| 9 | /// Writes the loader for the module at `module_path` beside it, | |
| 10 | /// with the module's name and the extension `.mjs`. | |
| 11 | /// | |
| 12 | /// - module_path: where the module was written | |
| 13 | write(module_path: string) static is | |
| 14 | let loader_path = Path.change_extension(module_path, ".mjs") ?? "{module_path}.mjs" | |
| 15 | ||
| 16 | File.write_all_text(loader_path, text_for(Path.get_file_name(module_path) ?? module_path)) | |
| 17 | si | |
| 18 | ||
| 19 | /// The loader's text for a module file of this name, which it | |
| 20 | /// loads from beside itself. | |
| 21 | /// | |
| 22 | /// - module_file: the module's file name | |
| 23 | text_for(module_file: string) -> string static => | |
| 24 | _template().replace("__GHUL_MODULE_FILE__", _escaped(module_file)) | |
| 25 | ||
| 26 | @IF.dotnet() | |
| 27 | _template() -> string static is | |
| 28 | let assembly = System.Reflection.Assembly.get_executing_assembly() | |
| 29 | let stream = assembly.get_manifest_resource_stream("wasmgc-loader.mjs") | |
| 30 | ||
| 31 | assert stream? else "the wasm loader is not embedded in the compiler" | |
| 32 | ||
| 33 | let use reader = StreamReader(stream) | |
| 34 | ||
| 35 | return reader.read_to_end() | |
| 36 | si | |
| 37 | ||
| 38 | @IF.not.dotnet() | |
| 39 | _template() -> string static is | |
| 40 | if let text = WASMGC_LOADER_TEXT.text then | |
| 41 | return text | |
| 42 | fi | |
| 43 | ||
| 44 | throw Ghul.NotSupportedException("the wasm loader is not available in this build of the compiler") | |
| 45 | si | |
| 46 | ||
| 47 | // The file name, as the inside of a JavaScript string literal. | |
| 48 | _escaped(text: string) -> string static => | |
| 49 | text.replace("\\", "\\\\").replace("\"", "\\\"") | |
| 50 | si | |
| 51 | si |