blob: 105f51ee12786c07ea0ae8a99e8c6ced6ada7ad2 [file] [log] [blame]
swissChili281af442024-11-14 23:30:29 -05001const units = require("./units.lib.js");
swissChiliab615d82024-03-08 17:15:13 -05002
swissChili281af442024-11-14 23:30:29 -05003globalThis.printBuffer = "";
4
5units({
6 "print": function(out) {
7 globalThis.printBuffer += out + "\n";
8 }
9}).then((Module) => {
swissChiliab615d82024-03-08 17:15:13 -050010 let _do_a_conversion = Module.cwrap("do_a_conversion", "number", [
11 "number",
12 "number",
swissChili4f0a0a82024-03-08 18:58:38 -050013 "number",
swissChiliab615d82024-03-08 17:15:13 -050014 ]);
15
swissChili4f0a0a82024-03-08 18:58:38 -050016 async function convert(from, to = "", system="si") {
swissChiliab615d82024-03-08 17:15:13 -050017 let from_c = Module.stringToNewUTF8(from);
18 let to_c = Module.stringToNewUTF8(to);
swissChili4f0a0a82024-03-08 18:58:38 -050019 let sys_c = Module.stringToNewUTF8(system);
swissChiliab615d82024-03-08 17:15:13 -050020 let lenBefore = globalThis.printBuffer.length;
swissChili4f0a0a82024-03-08 18:58:38 -050021 _do_a_conversion(from_c, to === "" ? 0 : to_c, system);
swissChiliab615d82024-03-08 17:15:13 -050022 return globalThis.printBuffer.slice(lenBefore).trim();
23 }
24
25 window.convert = convert;
swissChili281af442024-11-14 23:30:29 -050026});