blob: 6b10f29402da3e13640915da894cf90ed496de35 [file] [log] [blame]
swissChili281af442024-11-14 23:30:29 -05001const units = require("./units.lib.js");
swissChiliab615d82024-03-08 17:15:13 -05002
swissChili0512b1c2024-12-26 19:13:51 -08003export function setupUnits() {
4 globalThis.printBuffer = "";
swissChili281af442024-11-14 23:30:29 -05005
swissChili0512b1c2024-12-26 19:13:51 -08006 window.convertPromise = units({
7 "print": function(out) {
8 globalThis.printBuffer += out + "\n";
9 }
10 }).then((Module) => {
11 let _do_a_conversion = Module.cwrap("do_a_conversion", "number", [
12 "number",
13 "number",
14 "number",
15 ]);
swissChiliab615d82024-03-08 17:15:13 -050016
swissChili0512b1c2024-12-26 19:13:51 -080017 async function convert(from, to = "", system="si") {
18 let from_c = Module.stringToNewUTF8(from);
19 let to_c = Module.stringToNewUTF8(to);
20 let sys_c = Module.stringToNewUTF8(system);
21 let lenBefore = globalThis.printBuffer.length;
22 _do_a_conversion(from_c, to === "" ? 0 : to_c, system);
23 return globalThis.printBuffer.slice(lenBefore).trim();
24 }
swissChiliab615d82024-03-08 17:15:13 -050025
swissChili0512b1c2024-12-26 19:13:51 -080026 window.convert = convert;
27
28 return convert;
swissChili281af442024-11-14 23:30:29 -050029});
swissChili0512b1c2024-12-26 19:13:51 -080030}