addToLibrary({ register_serviceworker: function() { window.registerServiceWorker() }, unregister_serviceworker: function() { window.unregisterServiceWorker() }, update: function() { window.update() }, serviceworker_possible: function() { return window.serviceWorkerPossible() ? 1 : 0; }, serviceworker_registered: function() { return window.serviceWorkerRegistered() ? 1 : 0 }, open_filepicker: function() { window.filePicker.show(); }, set_filter: function(filter) { window.filePicker.setFilter(Module.UTF8ToString(filter)); }, file_picker_confirmed: function() { return window.filePicker.wasConfirmed(); }, file_picker_closed: function() { return window.filePicker.wasClosed(); }, file_picker_cancelled: function() { return window.filePicker.wasCancelled(); }, get_first_file: function() { if (window.filePicker.wasConfirmed()) { let output = window.filePicker.getFirstFile(); let outptr = window.stringToAllocatedUTF8(output); return outptr; } else { return 0; } }, file_picker_visible: function() { return window.filePicker.visible; }, file_picker_loading: function() { return window.filePicker.loading; }, clear_file_selection: function() { window.filePicker.clearSelection(); }, get_size: function(x, y) { let canvas = document.getElementById("canvas"); setValue(x, canvas.offsetWidth, "i32"); setValue(y, canvas.offsetHeight, "i32"); }, is_puter_enabled: function() { return window.puterEnabled; }, enable_puter: function(enable) { window.puterEnabled = enable; }, write_storage: async function(keyPtr, valuePtr, valueLen) { let key = Module.UTF8ToString(keyPtr) /** * @type {Uint8Array} */ let valueData = HEAPU8.subarray(valuePtr, valuePtr + valueLen) let valueBase64 = window.bytesToBase64(valueData); if (valueBase64 === null) { window.logln(window.loglevel.ERROR, `Couldn't encode key '${key}' to base64.`); return; } if (window.puterEnabled) { value = await puter.kv.set(key, valueBase64); } localStorage.setItem(key, valueBase64); }, remove_storage: async function(keyPtr) { let key = Module.UTF8ToString(keyPtr) if (window.puterEnabled) { await puter.kv.del(key) } localStorage.removeItem(Module.UTF8ToString(key)); }, get_dpi: function() { return window.devicePixelRatio; }, read_storage: async function(keyPtr, valuePtr, lenPtr) { let key = Module.UTF8ToString(keyPtr); let valueBase64 = localStorage.getItem(key); if (window.puterEnabled) { let puterValue = await puter.kv.get(key) if (puterValue !== null) { valueBase64 = puterValue; } } if (valueBase64 === null || valueBase64 === undefined) { window.logln(window.loglevel.ERROR, `Could not find key '${key}.`) setValue(valuePtr, 0, "i32"); return; } /** * @type {Uint8Array|null} */ let value = null; try { value = window.base64ToBytes(valueBase64) } catch (e) { console.error(e) value = null; } if (value === null || value === undefined) { window.logln(window.loglevel.ERROR, `Could not decode key '${key}'.`) setValue(valuePtr, 0, "i32"); return; } else { let len = value.length; let outptr = Module._malloc(len + 1); for (let i = 0; i < len; i++) { setValue(outptr + i, value[i], 'i8'); } if (lenPtr !== 0) { setValue(lenPtr, value.length, "i32") } setValue(outptr + len, 0, 'i8') setValue(valuePtr, outptr, "i32"); } }, find_keys: async function(prefixPtr, pppOut) { let prefix = Module.UTF8ToString(prefixPtr); let storageLen = localStorage.length; let puterList = []; if (window.puterEnabled) { puterList = await puter.kv.list(false); } let arr = []; let dict = {}; for (let i = 0; i < storageLen; i++) { let key = localStorage.key(i); if (key.startsWith(prefix)) { let base64Test = localStorage.getItem(key); try { window.base64ToBytes(base64Test) } catch (e) { continue; } arr = [...arr, key]; dict[key] = true; } } if (window.puterEnabled) { for (let i = 0; i < puterList.length; i++) { let key = puterList[i]; if (key.startsWith(prefix)) { if (dict[key] !== true) { let base64Test = await puter.kv.get(key); try { window.base64ToBytes(base64Test) } catch (e) { continue; } arr = [...arr, key]; } } } } let arrLen = arr.length; let ptrLen = arrLen + 1; let arrOutPtr = Module._malloc(ptrLen * 4); for (let i = 0; i < ptrLen; i++) { setValue(arrOutPtr + (i * 4), 0, 'i32'); } for (let i = 0; i < arrLen; i++) { let outptr = window.stringToAllocatedUTF8(arr[i]); setValue(arrOutPtr + (i * 4), outptr, "i32"); } setValue(pppOut, arrOutPtr, "i32") }, quit: function() { puter.exit(); } })