storage: maintain Lookup size correctly

This commit is contained in:
Pierre Tachoire
2026-02-27 08:51:22 +01:00
parent c61eda0d24
commit ef6a7a6904
2 changed files with 14 additions and 1 deletions

View File

@@ -88,3 +88,12 @@
localStorage.clear(); localStorage.clear();
testing.expectEqual(0, localStorage.length) testing.expectEqual(0, localStorage.length)
</script> </script>
<script id="localstorage_limits">
localStorage.clear();
for (i = 0; i < 5; i++) {
const v = "v".repeat(1024 * 1024);
localStorage.setItem(v, v);
}
testing.expectError("QuotaExceededError", () => localStorage.setItem("last", "v"));
</script>

View File

@@ -86,11 +86,15 @@ pub const Lookup = struct {
pub fn removeItem(self: *Lookup, key_: ?[]const u8) void { pub fn removeItem(self: *Lookup, key_: ?[]const u8) void {
const k = key_ orelse return; const k = key_ orelse return;
_ = self._data.remove(k); if (self._data.get(k)) |value| {
self._size -= value.len;
_ = self._data.remove(k);
}
} }
pub fn clear(self: *Lookup) void { pub fn clear(self: *Lookup) void {
self._data.clearRetainingCapacity(); self._data.clearRetainingCapacity();
self._size = 0;
} }
pub fn key(self: *const Lookup, index: u32) ?[]const u8 { pub fn key(self: *const Lookup, index: u32) ?[]const u8 {