use core::ffi::c_void;
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock, RwLock};
use super::events;
use super::util::*;
use crate::{capi, glue};
pub const DEFAULT_QUOTA_BYTES: usize = 5 * 1024 * 1024;
pub trait StorageHost: Send + Sync {
fn load(&self) -> Vec<(String, String)>;
fn store(&self, entries: &[(String, String)]);
fn quota_bytes(&self) -> usize {
DEFAULT_QUOTA_BYTES
}
fn describe(&self) -> String {
String::from("the host's storage backend")
}
}
static HOST: RwLock<Option<&'static dyn StorageHost>> = RwLock::new(None);
pub fn set_host(host: &'static dyn StorageHost) {
*HOST.write().unwrap() = Some(host);
}
pub fn host() -> Option<&'static dyn StorageHost> {
*HOST.read().unwrap()
}
fn advise_no_host() {
static ONCE: OnceLock<()> = OnceLock::new();
ONCE.get_or_init(|| {
eprintln!(
"[redjs] localStorage: no StorageHost installed — this run is IN MEMORY ONLY and \
nothing survives a restart. Call redjs::webapi::storage::set_host()."
);
});
}
#[derive(Default)]
struct Area {
entries: Vec<(String, String)>,
loaded: bool,
}
impl Area {
fn index_of(&self, key: &str) -> Option<usize> {
self.entries.iter().position(|(k, _)| k == key)
}
fn size(&self) -> usize {
self.entries.iter().map(|(k, v)| utf16_bytes(k) + utf16_bytes(v)).sum()
}
}
fn utf16_bytes(s: &str) -> usize {
s.encode_utf16().count() * 2
}
fn local_area() -> &'static Mutex<Area> {
static A: OnceLock<Mutex<Area>> = OnceLock::new();
A.get_or_init(|| Mutex::new(Area::default()))
}
fn session_areas() -> &'static Mutex<HashMap<usize, Area>> {
static A: OnceLock<Mutex<HashMap<usize, Area>>> = OnceLock::new();
A.get_or_init(|| Mutex::new(HashMap::new()))
}
fn with_area<R>(local: bool, g: usize, f: impl FnOnce(&mut Area, usize) -> (R, bool)) -> R {
if local {
let mut area = local_area().lock().unwrap();
if !area.loaded {
area.loaded = true;
match host() {
Some(h) => area.entries = h.load(),
None => advise_no_host(),
}
}
let quota = host().map(|h| h.quota_bytes()).unwrap_or(DEFAULT_QUOTA_BYTES);
let (out, dirty) = f(&mut area, quota);
if dirty {
if let Some(h) = host() {
h.store(&area.entries);
}
}
out
} else {
let mut areas = session_areas().lock().unwrap();
let area = areas.entry(g).or_default();
f(area, DEFAULT_QUOTA_BYTES).0
}
}
#[cfg(test)]
fn reset_local_for_tests() {
let mut area = local_area().lock().unwrap();
area.entries.clear();
area.loaded = false;
}
struct StorageData {
local: bool,
g: usize,
}
struct StorageClasses {
storage_class: capi::JSClassRef,
storage: Class,
event: Class,
s_ev: *mut c_void,
}
unsafe impl Send for StorageClasses {}
unsafe impl Sync for StorageClasses {}
fn classes_reg() -> &'static Mutex<HashMap<usize, &'static StorageClasses>> {
static R: OnceLock<Mutex<HashMap<usize, &'static StorageClasses>>> = OnceLock::new();
R.get_or_init(|| Mutex::new(HashMap::new()))
}
fn classes(g: *mut c_void) -> &'static StorageClasses {
classes_reg()
.lock()
.unwrap()
.get(&(g as usize))
.copied()
.expect("webapi::storage not installed")
}
fn proxies() -> &'static Mutex<HashMap<(usize, u64), bool>> {
static P: OnceLock<Mutex<HashMap<(usize, u64), bool>>> = OnceLock::new();
P.get_or_init(|| Mutex::new(HashMap::new()))
}
pub(crate) fn forget(gone: crate::context::Gone) {
classes_reg().lock().unwrap().remove(&gone.key);
session_areas().lock().unwrap().remove(&gone.key);
proxies().lock().unwrap().retain(|(g, _), _| *g != gone.key);
}
unsafe fn this_area(g: *mut c_void, cf: *mut c_void) -> Option<(bool, usize)> {
let this = cf_this(cf);
if let Some(d) = host_data::<StorageData>(g, classes(g).storage_class, this) {
return Some((d.local, d.g));
}
proxies().lock().unwrap().get(&(g as usize, this)).map(|&local| (local, g as usize))
}
macro_rules! this_storage {
($g:expr, $cf:expr, $member:literal) => {
match this_area($g, $cf) {
Some(pair) => pair,
None => {
return throw_type_error(
$g,
concat!("Storage.", $member, " called on an object that is not a Storage"),
)
}
}
};
}
#[crate::host_fn]
unsafe fn storage_length(g: *mut c_void, cf: *mut c_void) -> u64 {
let (local, ctx) = this_storage!(g, cf, "length");
let n = with_area(local, ctx, |a, _| (a.entries.len(), false));
enc_i32(n as i32)
}
#[crate::host_fn]
unsafe fn storage_key(g: *mut c_void, cf: *mut c_void) -> u64 {
let (local, ctx) = this_storage!(g, cf, "key");
let n = read_number(g, cf_arg(cf, 0));
let idx = if n.is_finite() && n >= 0.0 { n as u64 } else { 0 };
let found = with_area(local, ctx, |a, _| {
(a.entries.get(idx as usize).map(|(k, _)| k.clone()), false)
});
match found {
Some(k) => js_string(g, &k),
None => NULL,
}
}
#[crate::host_fn]
unsafe fn storage_get_item(g: *mut c_void, cf: *mut c_void) -> u64 {
let (local, ctx) = this_storage!(g, cf, "getItem");
let Some(key) = read_string(g, cf_arg(cf, 0)) else { return UNDEF };
let found =
with_area(local, ctx, |a, _| (a.index_of(&key).map(|i| a.entries[i].1.clone()), false));
match found {
Some(v) => js_string(g, &v),
None => NULL,
}
}
#[crate::host_fn]
unsafe fn storage_set_item(g: *mut c_void, cf: *mut c_void) -> u64 {
let (local, ctx) = this_storage!(g, cf, "setItem");
let Some(key) = read_string(g, cf_arg(cf, 0)) else { return UNDEF };
let Some(value) = read_string(g, cf_arg(cf, 1)) else { return UNDEF };
enum Outcome {
Unchanged,
Stored,
Quota(usize),
}
let outcome = with_area(local, ctx, |a, quota| {
match a.index_of(&key) {
Some(i) => {
if a.entries[i].1 == value {
return (Outcome::Unchanged, false);
}
let after =
a.size() - utf16_bytes(&a.entries[i].1) + utf16_bytes(&value);
if after > quota {
return (Outcome::Quota(quota), false);
}
a.entries[i].1 = value;
}
None => {
let after = a.size() + utf16_bytes(&key) + utf16_bytes(&value);
if after > quota {
return (Outcome::Quota(quota), false);
}
a.entries.push((key.clone(), value));
}
}
(Outcome::Stored, true)
});
match outcome {
Outcome::Unchanged | Outcome::Stored => UNDEF,
Outcome::Quota(quota) => throw_quota_exceeded(
g,
&format!(
"Failed to execute 'setItem' on 'Storage': Setting the value of '{key}' exceeded \
the quota ({} KiB).",
quota / 1024
),
),
}
}
#[crate::host_fn]
unsafe fn storage_remove_item(g: *mut c_void, cf: *mut c_void) -> u64 {
let (local, ctx) = this_storage!(g, cf, "removeItem");
let Some(key) = read_string(g, cf_arg(cf, 0)) else { return UNDEF };
with_area(local, ctx, |a, _| match a.index_of(&key) {
Some(i) => {
a.entries.remove(i);
((), true)
}
None => ((), false),
});
UNDEF
}
#[crate::host_fn]
unsafe fn storage_clear(g: *mut c_void, cf: *mut c_void) -> u64 {
let (local, ctx) = this_storage!(g, cf, "clear");
with_area(local, ctx, |a, _| {
let had = !a.entries.is_empty();
a.entries.clear();
((), had)
});
UNDEF
}
unsafe fn throw_quota_exceeded(g: *mut c_void, msg: &str) -> u64 {
glue::rjs_throw(g, super::domexception::create_quota_exceeded(g, msg, None, None));
UNDEF
}
#[crate::host_fn]
unsafe fn storage_event_construct(g: *mut c_void, cf: *mut c_void) -> u64 {
if cf_argc(cf) < 1 {
return throw_type_error(g, "StorageEvent constructor requires a type argument");
}
let c = classes(g);
let obj = ordinary_create(g, cf_new_target(cf), c.event.proto);
events::init_event(g, obj, cf_arg(cf, 0), cf_arg(cf, 1), false);
let init = cf_arg(cf, 1);
let slots = new_object(g, UNDEF);
let member = |name: &str| {
let raw = if is_cell(init) { get(g, init, name) } else { UNDEF };
if raw == UNDEF || raw == NULL {
NULL
} else {
match read_string(g, raw) {
Some(s) => js_string(g, &s),
None => NULL,
}
}
};
define(g, slots, "key", member("key"), 0);
define(g, slots, "oldValue", member("oldValue"), 0);
define(g, slots, "newValue", member("newValue"), 0);
let url = if is_cell(init) { get(g, init, "url") } else { UNDEF };
let url = if url == UNDEF {
js_string(g, "")
} else {
read_string(g, url).map(|s| js_string(g, &s)).unwrap_or_else(|| js_string(g, ""))
};
define(g, slots, "url", url, 0);
let area = if is_cell(init) { get(g, init, "storageArea") } else { UNDEF };
define(g, slots, "storageArea", if is_cell(area) { area } else { NULL }, 0);
glue::rjs_define(g, obj, c.s_ev, slots, 0);
obj
}
unsafe fn ev_member(g: *mut c_void, cf: *mut c_void, name: &str) -> u64 {
let slots = get_a(g, cf_this(cf), classes(g).s_ev);
if !is_cell(slots) {
return throw_type_error(g, "StorageEvent accessor on wrong this");
}
get(g, slots, name)
}
#[crate::host_fn]
unsafe fn ev_key(g: *mut c_void, cf: *mut c_void) -> u64 {
ev_member(g, cf, "key")
}
#[crate::host_fn]
unsafe fn ev_old_value(g: *mut c_void, cf: *mut c_void) -> u64 {
ev_member(g, cf, "oldValue")
}
#[crate::host_fn]
unsafe fn ev_new_value(g: *mut c_void, cf: *mut c_void) -> u64 {
ev_member(g, cf, "newValue")
}
#[crate::host_fn]
unsafe fn ev_url(g: *mut c_void, cf: *mut c_void) -> u64 {
ev_member(g, cf, "url")
}
#[crate::host_fn]
unsafe fn ev_storage_area(g: *mut c_void, cf: *mut c_void) -> u64 {
ev_member(g, cf, "storageArea")
}
fn listener_added(ty: &str) {
if ty != "storage" {
return;
}
static ONCE: OnceLock<()> = OnceLock::new();
ONCE.get_or_init(|| {
eprintln!(
"[redjs] a 'storage' listener was added, and this runtime never fires one: the event \
exists to tell one browsing context that ANOTHER wrote a key, and there is only one \
context here. Writes made by this process are visible immediately through \
localStorage itself. `new StorageEvent(...)` + dispatchEvent still works if you want \
to notify your own components."
);
});
}
const STORAGE_JS: &str = r#"(() => {
const proto = Storage.prototype;
// Spec: no [LegacyOverrideBuiltIns], so `length`/`getItem`/… shadow a stored
// key of the same name. `Reflect.has(proto, p)` IS that rule.
const builtin = (p) => Reflect.has(proto, p);
const wrap = (store) => new Proxy(store, {
get(t, p) {
if (typeof p === "symbol" || builtin(p)) return Reflect.get(t, p, t);
const v = t.getItem(p);
return v === null ? undefined : v;
},
set(t, p, v) {
if (typeof p === "symbol") return Reflect.set(t, p, v, t);
t.setItem(p, v);
return true;
},
has(t, p) {
if (typeof p === "symbol") return Reflect.has(t, p);
return builtin(p) || t.getItem(p) !== null;
},
deleteProperty(t, p) {
if (typeof p === "symbol") return Reflect.deleteProperty(t, p);
t.removeItem(p);
return true;
},
ownKeys(t) {
const n = t.length;
const out = new Array(n);
for (let i = 0; i < n; i++) out[i] = t.key(i);
return out;
},
getOwnPropertyDescriptor(t, p) {
if (typeof p === "symbol") return Reflect.getOwnPropertyDescriptor(t, p);
const v = t.getItem(p);
if (v === null) return undefined;
return { value: v, writable: true, enumerable: true, configurable: true };
},
defineProperty(t, p, d) {
if (typeof p === "symbol") return Reflect.defineProperty(t, p, d);
if (!("value" in d)) return false;
t.setItem(p, d.value);
return true;
},
});
// `Object.prototype.toString.call(localStorage)` is "[object Storage]" in a
// browser; without this it would be "[object Object]".
Object.defineProperty(proto, Symbol.toStringTag, {
value: "Storage", writable: false, enumerable: false, configurable: true,
});
for (const name of ["localStorage", "sessionStorage"]) {
Object.defineProperty(globalThis, name, {
value: wrap(globalThis[name]), writable: false, enumerable: true, configurable: true,
});
}
})();"#;
pub unsafe fn install(g: *mut c_void, event_proto: u64) -> i32 {
let storage = define_class(g, &ClassSpec {
name: "Storage",
arity: 0,
construct: None,
parent_proto: 0,
methods: &[
("key", 1, storage_key),
("getItem", 1, storage_get_item),
("setItem", 2, storage_set_item),
("removeItem", 1, storage_remove_item),
("clear", 0, storage_clear),
],
getters: &[("length", storage_length)],
accessors: &[],
});
let event = define_class(g, &ClassSpec {
name: "StorageEvent",
arity: 1,
construct: Some(storage_event_construct),
parent_proto: event_proto,
methods: &[],
getters: &[
("key", ev_key),
("oldValue", ev_old_value),
("newValue", ev_new_value),
("url", ev_url),
("storageArea", ev_storage_area),
],
accessors: &[],
});
let storage_class = make_host_class::<StorageData>("Storage");
let s_ev = atom_priv(g, "redjs#storageEvent");
let boxed: &'static StorageClasses =
Box::leak(Box::new(StorageClasses { storage_class, storage, event, s_ev }));
classes_reg().lock().unwrap().insert(g as usize, boxed);
let global = glue::rjs_global(g);
for (name, local) in [("localStorage", true), ("sessionStorage", false)] {
let obj = host_create(
g,
storage_class,
boxed.storage.proto,
StorageData { local, g: g as usize },
);
crate::ffi::redjs_protect(g, obj);
define(g, global, name, obj, 0);
}
events::on_listener_added(listener_added);
let url_name = "redjs/storage.js";
let mut out = 0u64;
if glue::rjs_eval8(
g,
STORAGE_JS.as_ptr(),
STORAGE_JS.len(),
url_name.as_ptr(),
url_name.len(),
&mut out,
) == 1
{
let (mut msg, mut stack) = (0u64, 0u64);
glue::rjs_error_info(g, out, &mut msg, &mut stack);
eprintln!("[redjs] storage.js failed: {}", read_string(g, msg).unwrap_or_default());
return 1;
}
let global_proxy = state(g).global_proxy;
for (name, local) in [("localStorage", true), ("sessionStorage", false)] {
let p = get(g, global_proxy, name);
if !is_cell(p) {
eprintln!("[redjs] storage.js did not publish {name}");
return 1;
}
crate::ffi::redjs_protect(g, p);
proxies().lock().unwrap().insert((g as usize, p), local);
}
0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_quota_is_counted_in_utf16_bytes_like_a_browser() {
assert_eq!(utf16_bytes("abc"), 6);
assert_eq!(utf16_bytes("\u{1F600}"), 4);
assert_eq!(utf16_bytes(""), 0);
}
#[test]
fn an_areas_size_is_keys_plus_values() {
let mut a = Area::default();
a.entries.push(("ab".into(), "cd".into()));
a.entries.push(("e".into(), "".into()));
assert_eq!(a.size(), 4 + 4 + 2);
}
#[test]
fn lookup_is_by_exact_key_and_order_is_insertion_order() {
let mut a = Area::default();
for k in ["z", "a", "m"] {
a.entries.push((k.into(), k.into()));
}
assert_eq!(a.index_of("z"), Some(0));
assert_eq!(a.index_of("a"), Some(1));
assert_eq!(a.index_of("m"), Some(2));
assert_eq!(a.index_of("Z"), None, "keys are case-sensitive");
assert_eq!(a.index_of(""), None);
}
struct RecordingHost {
seed: Vec<(String, String)>,
writes: Mutex<Vec<Vec<(String, String)>>>,
quota: usize,
}
impl StorageHost for RecordingHost {
fn load(&self) -> Vec<(String, String)> {
self.seed.clone()
}
fn store(&self, entries: &[(String, String)]) {
self.writes.lock().unwrap().push(entries.to_vec());
}
fn quota_bytes(&self) -> usize {
self.quota
}
}
#[test]
fn the_default_quota_is_the_browsers_five_mebibytes() {
struct Bare;
impl StorageHost for Bare {
fn load(&self) -> Vec<(String, String)> {
Vec::new()
}
fn store(&self, _: &[(String, String)]) {}
}
assert_eq!(Bare.quota_bytes(), 5 * 1024 * 1024);
assert_eq!(DEFAULT_QUOTA_BYTES, 5_242_880);
}
#[test]
fn a_host_describes_itself_even_without_overriding() {
struct Bare;
impl StorageHost for Bare {
fn load(&self) -> Vec<(String, String)> {
Vec::new()
}
fn store(&self, _: &[(String, String)]) {}
}
assert!(!Bare.describe().is_empty(), "an advisory has to be able to name it");
}
#[test]
fn store_receives_the_whole_area_not_a_delta() {
let h = RecordingHost {
seed: vec![("a".into(), "1".into())],
writes: Mutex::new(Vec::new()),
quota: 1024,
};
let mut area = Area { entries: h.load(), loaded: true };
area.entries.push(("b".into(), "2".into()));
h.store(&area.entries);
let writes = h.writes.lock().unwrap();
assert_eq!(writes.len(), 1);
assert_eq!(writes[0], vec![("a".into(), "1".into()), ("b".into(), "2".into())]);
}
}
#[cfg(test)]
mod jsc_tests {
use super::*;
use crate::api::Context;
use crate::webapi::testing::*;
struct MemoryHost;
static SEED: Mutex<Vec<(String, String)>> = Mutex::new(Vec::new());
static WRITES: Mutex<u32> = Mutex::new(0);
impl StorageHost for MemoryHost {
fn load(&self) -> Vec<(String, String)> {
SEED.lock().unwrap().clone()
}
fn store(&self, entries: &[(String, String)]) {
*SEED.lock().unwrap() = entries.to_vec();
*WRITES.lock().unwrap() += 1;
}
fn quota_bytes(&self) -> usize {
256
}
}
fn serial() -> std::sync::MutexGuard<'static, ()> {
static L: Mutex<()> = Mutex::new(());
L.lock().unwrap_or_else(|e| e.into_inner())
}
fn fresh() -> (Context, std::sync::MutexGuard<'static, ()>) {
let guard = serial();
static H: MemoryHost = MemoryHost;
set_host(&H);
SEED.lock().unwrap().clear();
*WRITES.lock().unwrap() = 0;
reset_local_for_tests();
(ctx(), guard)
}
#[test]
fn both_areas_are_storage_instances_on_the_global() {
let (c, _g) = fresh();
ok(&c, "localStorage instanceof Storage");
ok(&c, "sessionStorage instanceof Storage");
ok(&c, "localStorage !== sessionStorage");
ok(&c, "Object.prototype.toString.call(localStorage) === '[object Storage]'");
ok(&c, "localStorage === localStorage");
assert!(throws(&c, "new Storage()").contains("Illegal constructor"));
}
#[test]
fn the_six_members_behave_like_a_browsers() {
let (c, _g) = fresh();
assert_eq!(s(&c, "localStorage.length"), "0");
assert_eq!(s(&c, "localStorage.getItem('nope')"), "null");
assert_eq!(s(&c, "localStorage.key(0)"), "null");
s(&c, "localStorage.setItem('a', '1'); localStorage.setItem('b', '2')");
assert_eq!(s(&c, "localStorage.length"), "2");
assert_eq!(s(&c, "localStorage.getItem('a')"), "1");
assert_eq!(s(&c, "localStorage.key(0) + ',' + localStorage.key(1)"), "a,b");
assert_eq!(s(&c, "localStorage.key(2)"), "null");
s(&c, "localStorage.removeItem('a')");
assert_eq!(s(&c, "localStorage.getItem('a')"), "null");
assert_eq!(s(&c, "localStorage.length"), "1");
s(&c, "localStorage.removeItem('a')");
s(&c, "localStorage.clear()");
assert_eq!(s(&c, "localStorage.length"), "0");
}
#[test]
fn everything_is_coerced_to_a_string_on_the_way_in() {
let (c, _g) = fresh();
s(&c, "localStorage.setItem('n', 42)");
s(&c, "localStorage.setItem('o', { a: 1 })");
s(&c, "localStorage.setItem('u')");
s(&c, "localStorage.setItem(7, 'seven')");
assert_eq!(s(&c, "typeof localStorage.getItem('n')"), "string");
assert_eq!(s(&c, "localStorage.getItem('n')"), "42");
assert_eq!(s(&c, "localStorage.getItem('o')"), "[object Object]");
assert_eq!(s(&c, "localStorage.getItem('u')"), "undefined");
assert_eq!(s(&c, "localStorage.getItem('7')"), "seven");
}
#[test]
fn named_properties_are_the_same_store_as_the_methods() {
let (c, _g) = fresh();
s(&c, "localStorage.token = 'abc'");
assert_eq!(s(&c, "localStorage.getItem('token')"), "abc");
s(&c, "localStorage.setItem('other', 'x')");
assert_eq!(s(&c, "localStorage.other"), "x");
ok(&c, "'token' in localStorage");
ok(&c, "!('missing' in localStorage)");
assert_eq!(s(&c, "localStorage.missing"), "undefined");
assert_eq!(s(&c, "Object.keys(localStorage).join(',')"), "token,other");
assert_eq!(s(&c, "JSON.stringify(localStorage)"), r#"{"token":"abc","other":"x"}"#);
s(&c, "delete localStorage.token");
assert_eq!(s(&c, "localStorage.length"), "1");
ok(&c, "!('token' in localStorage)");
}
#[test]
fn a_builtin_name_is_never_shadowed_by_a_stored_key() {
let (c, _g) = fresh();
s(&c, "localStorage.setItem('getItem', 'not a function')");
assert_eq!(s(&c, "typeof localStorage.getItem"), "function");
assert_eq!(s(&c, "localStorage.getItem('getItem')"), "not a function");
s(&c, "localStorage.setItem('length', 'nine')");
assert_eq!(s(&c, "localStorage.length"), "2");
assert_eq!(s(&c, "localStorage.getItem('length')"), "nine");
}
#[test]
fn session_storage_is_separate_and_never_reaches_the_host() {
let (c, _g) = fresh();
s(&c, "localStorage.setItem('k', 'local')");
s(&c, "sessionStorage.setItem('k', 'session')");
assert_eq!(s(&c, "localStorage.getItem('k')"), "local");
assert_eq!(s(&c, "sessionStorage.getItem('k')"), "session");
assert_eq!(SEED.lock().unwrap().clone(), vec![("k".to_string(), "local".to_string())]);
}
#[test]
fn a_write_reaches_the_host_and_a_no_op_write_does_not() {
let (c, _g) = fresh();
s(&c, "localStorage.setItem('a', '1')");
assert_eq!(*WRITES.lock().unwrap(), 1);
s(&c, "localStorage.setItem('a', '1')");
assert_eq!(*WRITES.lock().unwrap(), 1, "an identical write is not a write");
s(&c, "localStorage.setItem('a', '2')");
assert_eq!(*WRITES.lock().unwrap(), 2);
s(&c, "localStorage.removeItem('zzz')");
assert_eq!(*WRITES.lock().unwrap(), 2, "removing nothing is not a write");
s(&c, "localStorage.clear()");
assert_eq!(*WRITES.lock().unwrap(), 3);
s(&c, "localStorage.clear()");
assert_eq!(*WRITES.lock().unwrap(), 3, "clearing an empty area is not a write");
}
#[test]
fn the_area_is_loaded_from_the_host_exactly_once_and_lazily() {
let (c, _g) = fresh();
*SEED.lock().unwrap() = vec![("restored".into(), "yes".into())];
assert_eq!(s(&c, "localStorage.getItem('restored')"), "yes");
assert_eq!(s(&c, "localStorage.length"), "1");
}
#[test]
fn past_the_quota_setitem_throws_a_quota_exceeded_error_and_stores_nothing() {
let (c, _g) = fresh();
let msg = throws(&c, "localStorage.setItem('big', 'x'.repeat(1000))");
assert!(msg.contains("exceeded the quota"), "{msg}");
assert_eq!(s(&c, "localStorage.length"), "0", "a refused write stores nothing");
assert_eq!(
s(
&c,
"(() => { try { localStorage.setItem('b', 'x'.repeat(1000)); return 'no throw'; } \
catch (e) { return e.name + '/' + e.code + '/' + (e instanceof QuotaExceededError) \
+ '/' + (e instanceof DOMException) + '/' + e.quota; } })()"
),
"QuotaExceededError/22/true/true/null"
);
s(&c, "localStorage.setItem('small', 'ok')");
assert_eq!(s(&c, "localStorage.getItem('small')"), "ok");
}
#[test]
fn storage_event_is_constructible_and_dispatchable_even_though_we_never_fire_it() {
let (c, _g) = fresh();
assert_eq!(
s(
&c,
"let seen = null;
addEventListener('storage', (e) => { seen = e.key + '=' + e.newValue + '|' + \
e.oldValue + '|' + e.url; });
dispatchEvent(new StorageEvent('storage', { key: 'k', newValue: 'new', \
oldValue: 'old' }));
seen"
),
"k=new|old|"
);
ok(&c, "new StorageEvent('storage') instanceof Event");
ok(&c, "new StorageEvent('storage').key === null");
ok(&c, "new StorageEvent('storage').storageArea === null");
ok(&c, "new StorageEvent('storage', { storageArea: localStorage }).storageArea === localStorage");
assert!(throws(&c, "new StorageEvent()").contains("requires a type"));
}
#[test]
fn the_members_refuse_a_foreign_this() {
let (c, _g) = fresh();
let msg = throws(&c, "Storage.prototype.getItem.call({}, 'k')");
assert!(msg.contains("not a Storage"), "{msg}");
let msg = throws(
&c,
"Object.getOwnPropertyDescriptor(Storage.prototype, 'length').get.call({})",
);
assert!(msg.contains("not a Storage"), "{msg}");
}
}