swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 1 | const units = require("./units.lib.js"); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 2 | |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 3 | globalThis.printBuffer = ""; |
| 4 | |
| 5 | units({ |
| 6 | "print": function(out) { |
| 7 | globalThis.printBuffer += out + "\n"; |
| 8 | } |
| 9 | }).then((Module) => { |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 10 | let _do_a_conversion = Module.cwrap("do_a_conversion", "number", [ |
| 11 | "number", |
| 12 | "number", |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 13 | "number", |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 14 | ]); |
| 15 | |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 16 | async function convert(from, to = "", system="si") { |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 17 | let from_c = Module.stringToNewUTF8(from); |
| 18 | let to_c = Module.stringToNewUTF8(to); |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 19 | let sys_c = Module.stringToNewUTF8(system); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 20 | let lenBefore = globalThis.printBuffer.length; |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 21 | _do_a_conversion(from_c, to === "" ? 0 : to_c, system); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 22 | return globalThis.printBuffer.slice(lenBefore).trim(); |
| 23 | } |
| 24 | |
| 25 | window.convert = convert; |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame] | 26 | }); |