* Mounts a file in the Lua environment synchronously. * @param luaWasm - Lua WebAssembly module. * @param path - Path to the file in the Lua environment. * @param content - Content of the file to be mounted.
(luaWasm: LuaWasm, path: string, content: string | ArrayBufferView)
| 46 | * @param content - Content of the file to be mounted. |
| 47 | */ |
| 48 | public mountFileSync(luaWasm: LuaWasm, path: string, content: string | ArrayBufferView): void { |
| 49 | const fileSep = path.lastIndexOf('/') |
| 50 | const file = path.substring(fileSep + 1) |
| 51 | const body = path.substring(0, path.length - file.length - 1) |
| 52 | |
| 53 | if (body.length > 0) { |
| 54 | const parts = body.split('/').reverse() |
| 55 | let parent = '' |
| 56 | |
| 57 | while (parts.length) { |
| 58 | const part = parts.pop() |
| 59 | if (!part) { |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | const current = `${parent}/${part}` |
| 64 | try { |
| 65 | luaWasm.module.FS.mkdir(current) |
| 66 | } catch { |
| 67 | // ignore EEXIST |
| 68 | } |
| 69 | |
| 70 | parent = current |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | luaWasm.module.FS.writeFile(path, content) |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Creates a Lua engine with the specified options. |