dommy 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dommy/animation.rb +1 -0
  3. data/lib/dommy/backend.rb +3 -0
  4. data/lib/dommy/blob.rb +6 -0
  5. data/lib/dommy/browser.rb +12 -4
  6. data/lib/dommy/character_data.rb +1 -13
  7. data/lib/dommy/cookie_store.rb +2 -1
  8. data/lib/dommy/document.rb +67 -30
  9. data/lib/dommy/element.rb +11 -0
  10. data/lib/dommy/element_internals.rb +12 -0
  11. data/lib/dommy/encodings/legacy_multibyte.rb +800 -0
  12. data/lib/dommy/encodings/multibyte_overrides.rb +76 -0
  13. data/lib/dommy/encodings/prescan.rb +234 -0
  14. data/lib/dommy/encodings.rb +121 -57
  15. data/lib/dommy/event.rb +107 -54
  16. data/lib/dommy/event_source.rb +1 -0
  17. data/lib/dommy/fetch.rb +244 -53
  18. data/lib/dommy/file_reader.rb +1 -0
  19. data/lib/dommy/form_data.rb +22 -9
  20. data/lib/dommy/form_entry_list.rb +3 -3
  21. data/lib/dommy/html_canvas_element.rb +85 -12
  22. data/lib/dommy/html_elements/base.rb +1 -0
  23. data/lib/dommy/html_elements/forms/input.rb +3 -0
  24. data/lib/dommy/html_elements/interactive.rb +56 -48
  25. data/lib/dommy/html_elements/metadata.rb +23 -0
  26. data/lib/dommy/interaction/form_submission.rb +24 -42
  27. data/lib/dommy/internal/accessibility_tree.rb +149 -162
  28. data/lib/dommy/internal/accessibility_visibility.rb +16 -17
  29. data/lib/dommy/internal/accessible_name.rb +231 -246
  30. data/lib/dommy/internal/aria_role.rb +182 -195
  31. data/lib/dommy/internal/aria_snapshot.rb +35 -38
  32. data/lib/dommy/internal/aria_state.rb +102 -118
  33. data/lib/dommy/internal/autofill.rb +75 -75
  34. data/lib/dommy/internal/backend_prefilter.rb +127 -130
  35. data/lib/dommy/internal/child_navigable.rb +86 -83
  36. data/lib/dommy/internal/close_watcher_manager.rb +1 -0
  37. data/lib/dommy/internal/css/ancestor_filter.rb +64 -64
  38. data/lib/dommy/internal/css/calc.rb +90 -71
  39. data/lib/dommy/internal/css/cascade.rb +69 -67
  40. data/lib/dommy/internal/css/color.rb +249 -187
  41. data/lib/dommy/internal/css/computed_style_builder.rb +34 -0
  42. data/lib/dommy/internal/css/computed_style_declaration.rb +50 -11
  43. data/lib/dommy/internal/css/counters.rb +67 -67
  44. data/lib/dommy/internal/css/custom_properties.rb +114 -114
  45. data/lib/dommy/internal/css/parser.rb +277 -239
  46. data/lib/dommy/internal/css/property_registry.rb +321 -302
  47. data/lib/dommy/internal/css/renderability.rb +37 -35
  48. data/lib/dommy/internal/css/rule_index.rb +2 -2
  49. data/lib/dommy/internal/css/rule_index_inputs.rb +2 -2
  50. data/lib/dommy/internal/css/supports.rb +115 -113
  51. data/lib/dommy/internal/css/ua_stylesheet.rb +55 -53
  52. data/lib/dommy/internal/css_rule_text.rb +121 -119
  53. data/lib/dommy/internal/css_source.rb +1 -3
  54. data/lib/dommy/internal/css_syntax.rb +157 -92
  55. data/lib/dommy/internal/declarative_shadow_roots.rb +1 -0
  56. data/lib/dommy/internal/directionality.rb +120 -123
  57. data/lib/dommy/internal/document_live_ranges.rb +26 -1
  58. data/lib/dommy/internal/dom_matching.rb +185 -184
  59. data/lib/dommy/internal/element_geometry.rb +8 -1
  60. data/lib/dommy/internal/element_matching.rb +71 -69
  61. data/lib/dommy/internal/element_shadow.rb +1 -13
  62. data/lib/dommy/internal/element_state.rb +255 -265
  63. data/lib/dommy/internal/event_handlers.rb +66 -64
  64. data/lib/dommy/internal/exception_report.rb +88 -86
  65. data/lib/dommy/internal/focusability.rb +205 -205
  66. data/lib/dommy/internal/global_functions.rb +49 -47
  67. data/lib/dommy/internal/idl_reflection.rb +99 -97
  68. data/lib/dommy/internal/js_number.rb +35 -33
  69. data/lib/dommy/internal/light_dismiss.rb +130 -132
  70. data/lib/dommy/internal/literal_lookup.rb +75 -75
  71. data/lib/dommy/internal/mime_type.rb +54 -49
  72. data/lib/dommy/internal/mutation_coordinator.rb +46 -0
  73. data/lib/dommy/internal/mutation_observer_microtask.rb +60 -0
  74. data/lib/dommy/internal/namespaces.rb +51 -49
  75. data/lib/dommy/internal/node_adopter.rb +25 -1
  76. data/lib/dommy/internal/node_equality.rb +61 -59
  77. data/lib/dommy/internal/post_insertion_steps.rb +1 -0
  78. data/lib/dommy/internal/referrer.rb +144 -0
  79. data/lib/dommy/internal/rendered_text/collector.rb +22 -3
  80. data/lib/dommy/internal/rendered_text/computed_box.rb +3 -2
  81. data/lib/dommy/internal/rendered_text/fragment.rb +37 -38
  82. data/lib/dommy/internal/rendered_text/items.rb +7 -5
  83. data/lib/dommy/internal/selector_matcher.rb +361 -359
  84. data/lib/dommy/internal/selector_parser.rb +74 -62
  85. data/lib/dommy/internal/sequential_focus_navigation.rb +150 -151
  86. data/lib/dommy/internal/shadow_root_registry.rb +7 -0
  87. data/lib/dommy/internal/slot_assignment.rb +117 -0
  88. data/lib/dommy/internal/slot_signals.rb +125 -0
  89. data/lib/dommy/internal/unsafe_html.rb +79 -77
  90. data/lib/dommy/internal/url_parser.rb +5 -29
  91. data/lib/dommy/internal/web_idl.rb +158 -153
  92. data/lib/dommy/internal/window_bound.rb +14 -0
  93. data/lib/dommy/internal/window_constructors.rb +3 -0
  94. data/lib/dommy/internal/xml_serialization.rb +298 -296
  95. data/lib/dommy/js/dom_interfaces.rb +13 -1
  96. data/lib/dommy/js/host_bridge.rb +23 -1
  97. data/lib/dommy/js/host_callbacks.rb +7 -0
  98. data/lib/dommy/js/host_runtime.js +625 -61
  99. data/lib/dommy/js/import_map.rb +1 -1
  100. data/lib/dommy/js/observable_runtime.js +295 -128
  101. data/lib/dommy/js/webidl_members.js +1 -1
  102. data/lib/dommy/js/webidl_signatures.js +35 -6
  103. data/lib/dommy/media_query_list.rb +1 -0
  104. data/lib/dommy/message_channel.rb +2 -0
  105. data/lib/dommy/mutation_observer.rb +25 -29
  106. data/lib/dommy/navigator.rb +4 -1
  107. data/lib/dommy/node.rb +4 -0
  108. data/lib/dommy/notification.rb +1 -0
  109. data/lib/dommy/promise.rb +12 -0
  110. data/lib/dommy/range.rb +20 -3
  111. data/lib/dommy/shadow_root.rb +25 -1
  112. data/lib/dommy/streams.rb +18 -0
  113. data/lib/dommy/style_declaration.rb +15 -4
  114. data/lib/dommy/text_codec.rb +7 -5
  115. data/lib/dommy/tree_walker.rb +68 -61
  116. data/lib/dommy/version.rb +1 -1
  117. data/lib/dommy/web_socket.rb +1 -0
  118. data/lib/dommy/window.rb +22 -4
  119. data/lib/dommy/worker.rb +1 -0
  120. data/lib/dommy/xml_http_request.rb +12 -2
  121. data/lib/dommy.rb +17 -2
  122. metadata +11 -3
@@ -29,6 +29,10 @@ globalThis.__rbHost = (function () {
29
29
  // later length-shrinking mutation (pop/shift/splice) does `symbol >= newLen`
30
30
  // and throws "cannot convert symbol to number". A WeakMap lookup is pure.
31
31
  const proxyHandles = new WeakMap(); // proxy -> handle (identity, trap-free)
32
+ // proxy -> its target, for the bridge's own definitions on a window (the
33
+ // mirrored constructors), which must land on the target rather than go
34
+ // through the defineProperty trap that makes a page's definition a global.
35
+ const proxyTargets = new WeakMap();
32
36
  // proxy -> the interface name it was built for. A Ruby object can be freed and
33
37
  // its handle id reused for a DIFFERENT object, so a cached proxy is only
34
38
  // trustworthy while it still describes the same interface — otherwise the new
@@ -64,6 +68,86 @@ globalThis.__rbHost = (function () {
64
68
  return w != null && proxyHandles.get(w) === handle;
65
69
  };
66
70
 
71
+ // Set by unifyGlobalWindow when the engine's global object IS the top window
72
+ // (`window === globalThis`, as in a browser): the window's host proxy then
73
+ // sits at the bottom of globalThis's prototype chain, where its traps answer
74
+ // the names globalThis does not have, and the window's handle stands for
75
+ // globalThis itself (makeProxy, dehydrate). Null while the two are separate
76
+ // objects whose namespaces are kept in step (the default).
77
+ let unifiedWindowHandle = null;
78
+ let unifiedWindowProxy = null;
79
+ // The names globalThis owned once it became the window: the window proxy
80
+ // below it never answers them (see unifyGlobalWindow).
81
+ let unifiedGlobalNames = new Set();
82
+ // The window's members unifyGlobalWindow defined on globalThis, which a
83
+ // rebinding defines again for the new window.
84
+ let unifiedWindowMembers = new Set();
85
+ // Window members HTML defines in mixins the generated member table leaves
86
+ // out (WindowOrWorkerGlobalScope, …), and the writable Window attributes
87
+ // that are not [Replaceable]: an assignment reaches the host rather than
88
+ // replacing the global.
89
+ const WINDOW_MIXIN_READONLY = ["crypto", "indexedDB", "caches"];
90
+ const WINDOW_MIXIN_REPLACEABLE = ["performance", "visualViewport"];
91
+ const WINDOW_HOST_WRITABLE = new Set(["name", "status", "opener"]);
92
+
93
+ // The bridge's bare globals that stand for a Window member — `fetch`,
94
+ // `setTimeout`, `scrollY` — reach the window's own implementation through
95
+ // windowMember (below), never by reading the window. The window answers a
96
+ // property of globalThis as its own (windowOwnGlobal), so a forward that
97
+ // read `window.fetch` would read itself, and a page that saved the bare
98
+ // `fetch` and then replaced `window.fetch` with a wrapper calling it would
99
+ // recurse; reaching the implementation directly, a saved forward is what a
100
+ // saved native function is in a browser. An embedder declares that its
101
+ // forwards do so by passing each to the non-enumerable
102
+ // `__rbWindowForward(fn)`; until one does, its forwards may still read the
103
+ // window, so a Window member keeps coming from the host unless the page
104
+ // defined it on the window (windowDefinitions), as it did before.
105
+ let windowForwardsDirect = false;
106
+ const windowDefinitions = new Set();
107
+ Object.defineProperty(globalThis, "__rbWindowForward", {
108
+ value(fn) {
109
+ windowForwardsDirect = true;
110
+ return fn;
111
+ },
112
+ writable: true, enumerable: false, configurable: true,
113
+ });
114
+
115
+ // What the global window's own implementation answers for `name` — a host
116
+ // method or attribute, an unforgeable attribute of its target, a member of
117
+ // its prototype — without looking at globalThis: what a bare forward calls,
118
+ // as the non-enumerable `__rbWindowMember(name)`.
119
+ let answeringFromHost = false;
120
+ function windowMember(name) {
121
+ const w = unifiedWindowProxy || globalThis.window;
122
+ if (w == null) return undefined;
123
+ const outer = answeringFromHost;
124
+ answeringFromHost = true;
125
+ try {
126
+ return w[name];
127
+ } finally {
128
+ answeringFromHost = outer;
129
+ }
130
+ }
131
+ Object.defineProperty(globalThis, "__rbWindowMember", {
132
+ value: windowMember, writable: true, enumerable: false, configurable: true,
133
+ });
134
+
135
+ // A property of globalThis that the global window answers as its own: in a
136
+ // browser the window IS the global object, so a global the page made, and a
137
+ // Window member — the bare forward standing for it, or what the page
138
+ // replaced it with through either name — is the window's property too. Not
139
+ // an event handler (the host registers the listener behind it), nor where
140
+ // the target has the name unforgeable (location, document).
141
+ function windowOwnGlobal(shape, t, prop) {
142
+ if (isEventHandlerName(shape, prop)) return undefined;
143
+ const d = Reflect.getOwnPropertyDescriptor(globalThis, prop);
144
+ if (d === undefined) return undefined;
145
+ const own = Reflect.getOwnPropertyDescriptor(t, prop);
146
+ if (own !== undefined && !own.configurable && d.configurable) return undefined;
147
+ if (windowForwardsDirect || windowDefinitions.has(prop)) return d;
148
+ return d.get !== undefined || d.set !== undefined || shape.methods.has(prop) ? undefined : d;
149
+ }
150
+
67
151
  const unforgeableGetters = new Map();
68
152
  function unforgeableGetter(name) {
69
153
  let fn = unforgeableGetters.get(name);
@@ -169,6 +253,14 @@ globalThis.__rbHost = (function () {
169
253
  return isProxy(arg) ? arg : String(arg);
170
254
  }
171
255
 
256
+ // Whether `name` on interface `iface` is the ParentNode / ChildNode
257
+ // `(Node or DOMString)...` member. The names are shared with members that
258
+ // take something else — FormData's, URLSearchParams' and Headers' `append`
259
+ // — so the interface has to be a Node.
260
+ function nodeOrStringMember(name, iface) {
261
+ return NODE_OR_STRING_METHODS.has(name) && !!iface && signatureChain(iface).indexOf("Node") !== -1;
262
+ }
263
+
172
264
  // HTML "get the current value of the event handler", the compile step: an
173
265
  // event handler content attribute's body becomes a function named after the
174
266
  // handler (`function onclick(event) {\n…\n}`, so its source text reads as a
@@ -419,8 +511,9 @@ globalThis.__rbHost = (function () {
419
511
  // the DOM changes underneath a cached parentNode / attribute snapshot and the
420
512
  // next read hands back the state from before the call.
421
513
  function memberMethodStub(name, iface) {
422
- const coerce = NODE_OR_STRING_METHODS.has(name);
514
+ const coerce = nodeOrStringMember(name, iface);
423
515
  const readOnly = NON_MUTATING_METHODS.has(name);
516
+ const dehydrateCallArgs = withImageDataRead(dehydrateArgs, iface, name);
424
517
  const stub = withArity(function (...args) {
425
518
  // Resolve back through the receiver: the proxy get trap returns the
426
519
  // specialized per-proxy wrapper (epoch bumps, cached getAttribute, the
@@ -437,7 +530,7 @@ globalThis.__rbHost = (function () {
437
530
  checkReceiver(this, iface, name);
438
531
  const call = declaredConversion(receiverInterface(this, iface), "operations", name);
439
532
  const converted = call ? convertArguments(call, args) : args;
440
- const wire = dehydrateArgs(coerce ? converted.map(coerceNodeOrString) : converted);
533
+ const wire = dehydrateCallArgs(coerce ? converted.map(coerceNodeOrString) : converted);
441
534
  return readOnly
442
535
  ? hostCallResult(name, __rb_host_call(this[HKEY], name, wire), iface)
443
536
  : callMutating(this[HKEY], name, wire, iface);
@@ -468,10 +561,99 @@ globalThis.__rbHost = (function () {
468
561
  return hostSetWire(handle, name, dehydrateTop(value));
469
562
  }
470
563
 
564
+ // ImageData's `data`: one Uint8ClampedArray per ImageData, which holds its
565
+ // pixels. Dommy paints nothing, so an ImageData starts transparent black and
566
+ // only the page's writes change it. They stay here and do not cross the
567
+ // bridge when the ImageData does (a 1920x1080 image is 8 MB) — except for a
568
+ // ImageData the host may keep beyond the life of any proxy of it: a
569
+ // deserialized clone (a MessageEvent's data, a history state), or one the
570
+ // page handed to the host (`new MessageEvent("m", {data: img})`). Its pixels
571
+ // go to the host when its last proxy goes.
572
+ //
573
+ // The pixels are keyed by handle, not by proxy: QuickJS frees a proxy as
574
+ // soon as nothing refers to it (`e.data.data` drops `e.data` at once), and
575
+ // the next read makes a new proxy for the same handle, which must find the
576
+ // same array. A handle lasts until its last proxy is finalized (see
577
+ // `finalizers`), which is when an array the page could have written goes
578
+ // back to a host that holds the pixels.
579
+ //
580
+ // pixels the Uint8ClampedArray `data` returns
581
+ // hostBacked the host may keep this ImageData (a clone, or one it was
582
+ // given), so keeps its pixels
583
+ // exposed the array may hold what the host does not (the page has had
584
+ // it, or it came from a serialized record)
585
+ const pixelsByHandle = new Map();
586
+ function pixelsOf(proxy) {
587
+ const handle = proxyHandles.get(proxy);
588
+ let entry = pixelsByHandle.get(handle);
589
+ if (entry === undefined) {
590
+ // No proxy of it has had pixels yet. One the host keeps (a clone) is
591
+ // host-backed, whatever pixels it starts with.
592
+ entry = { pixels: null, hostBacked: rehydrate(__rb_host_get(handle, "__dommyClone")) === true, exposed: false };
593
+ pixelsByHandle.set(handle, entry);
594
+ }
595
+ if (entry.pixels === null) entry.pixels = hostPixels(handle);
596
+ entry.exposed = true;
597
+ return entry.pixels;
598
+ }
599
+
600
+ // The pixels the host holds for an ImageData (a clone's), else transparent
601
+ // black of its size.
602
+ function hostPixels(handle) {
603
+ const held = rehydrate(__rb_host_get(handle, "__dommyPixels"));
604
+ if (held instanceof Uint8Array) return new Uint8ClampedArray(held.buffer, held.byteOffset, held.byteLength);
605
+ const width = rehydrate(__rb_host_get(handle, "width"));
606
+ const height = rehydrate(__rb_host_get(handle, "height"));
607
+ return new Uint8ClampedArray(width * height * 4);
608
+ }
609
+
610
+ // The operations that take an ImageData as such (WebIDL types the argument
611
+ // ImageData, or ImageBitmapSource): each reads its pixels during the call
612
+ // and keeps nothing, so the ImageData stays the page's alone. Everywhere
613
+ // else an ImageData reaches the host as an `any` or `object` value — an
614
+ // event's data, a signal's reason, a stream's chunk — which the host keeps.
615
+ const IMAGE_DATA_READERS = new Set([
616
+ "CanvasRenderingContext2D.createImageData", "CanvasRenderingContext2D.putImageData",
617
+ "OffscreenCanvasRenderingContext2D.createImageData", "OffscreenCanvasRenderingContext2D.putImageData",
618
+ "Window.createImageBitmap", "WorkerGlobalScope.createImageBitmap",
619
+ ]);
620
+ let readingImageData = false;
621
+ // `fn`, for operation `name` of `iface`, with the ImageData it is given
622
+ // crossing as one the host only reads, when it is such an operation.
623
+ function withImageDataRead(fn, iface, name) {
624
+ if (!IMAGE_DATA_READERS.has(iface + "." + name)) return fn;
625
+ return function (...args) {
626
+ const outer = readingImageData;
627
+ readingImageData = true;
628
+ try {
629
+ return fn.apply(this, args);
630
+ } finally {
631
+ readingImageData = outer;
632
+ }
633
+ };
634
+ }
635
+
636
+ // The ImageData crosses to the host, which may keep it: its pixels the page
637
+ // writes go there when its last proxy goes.
638
+ function markHostBacked(handle) {
639
+ const entry = pixelsByHandle.get(handle);
640
+ if (entry === undefined) pixelsByHandle.set(handle, { pixels: null, hostBacked: true, exposed: false });
641
+ else entry.hostBacked = true;
642
+ }
643
+
644
+ // The handle is going: a host-backed ImageData's pixels the page may have
645
+ // written go back to the host, which keeps the ImageData.
646
+ function releasePixels(handle) {
647
+ const entry = pixelsByHandle.get(handle);
648
+ if (entry === undefined) return;
649
+ pixelsByHandle.delete(handle);
650
+ if (entry.hostBacked && entry.exposed) __rb_host_set(handle, "__dommyPixels", dehydrate(entry.pixels));
651
+ }
652
+
471
653
  // hostSet for a value already in its wire form.
472
654
  function hostSetWire(handle, name, wire) {
473
655
  bumpDomEpoch();
474
- const handled = __rb_host_set(handle, name, wire);
656
+ const handled = __rb_host_set(handle, hostName(name), wire);
475
657
  bumpDomEpoch();
476
658
  if (handled && typeof handled === "object" && handled.__rb_exception__) {
477
659
  throw makeHostError(handled.__rb_exception__);
@@ -617,6 +799,7 @@ globalThis.__rbHost = (function () {
617
799
  const ref = cache.get(handle);
618
800
  if (!ref || ref.deref() === undefined) {
619
801
  cache.delete(handle);
802
+ releasePixels(handle);
620
803
  __rb_release_handle(handle);
621
804
  }
622
805
  });
@@ -653,6 +836,17 @@ globalThis.__rbHost = (function () {
653
836
  return id;
654
837
  }
655
838
 
839
+ // Forget functions registered with registerCallback that will not be
840
+ // called again; a later call for one of their ids does nothing.
841
+ function releaseCallbacks(...fns) {
842
+ for (const fn of fns) {
843
+ const id = callbackIds.get(fn);
844
+ if (id === undefined) continue;
845
+ callbacks.delete(id);
846
+ callbackIds.delete(fn);
847
+ }
848
+ }
849
+
656
850
  // Called from Ruby when a host event dispatch reaches a JS-registered
657
851
  // listener. The live function (closure intact) is invoked; tagged args
658
852
  // (e.g. an Event handle) are rehydrated to proxies first.
@@ -772,6 +966,15 @@ globalThis.__rbHost = (function () {
772
966
  return out;
773
967
  }
774
968
 
969
+ // A property name as the host sees it. A page-chosen name crossing as the
970
+ // `name` argument of a host call is a string like any argument, so it gets
971
+ // the same lone-surrogate scrub dehydrate gives values — otherwise
972
+ // `storage["\uD800"] = v` and `storage.setItem("\uD800", v)` would reach
973
+ // Ruby as two different keys on an engine that passes raw names through.
974
+ function hostName(name) {
975
+ return typeof name === "string" && /[\ud800-\udfff]/.test(name) ? scrubLoneSurrogates(name) : name;
976
+ }
977
+
775
978
  // The spec's "flatten options": addEventListener / removeEventListener observe
776
979
  // only `capture` (plus once / passive / signal for add), each read exactly once.
777
980
  // Flattening here — before dehydrate walks the bag — means an option object's
@@ -817,8 +1020,15 @@ globalThis.__rbHost = (function () {
817
1020
  const promise = new Promise((resolve, reject) => { resolveFn = resolve; rejectFn = reject; });
818
1021
  hostPromiseProxies.set(promise, proxy);
819
1022
  realmPromises.set(handle, new WeakRef(promise));
1023
+ // The host settles the promise once, so the two callbacks are released
1024
+ // then: the callback table holds what it registers for good, and the
1025
+ // resolve function would keep the Promise, its host proxy, the
1026
+ // PromiseValue and the value it settled with (a fetch's Response) alive
1027
+ // for the life of the page.
1028
+ const onFulfilled = (value) => { releaseCallbacks(onFulfilled, onRejected); resolveFn(value); };
1029
+ const onRejected = (reason) => { releaseCallbacks(onFulfilled, onRejected); rejectFn(reason); };
820
1030
  // Subscribe without a chained host promise (nothing to hand back).
821
- __rb_host_promise_subscribe(handle, dehydrate(resolveFn), dehydrate(rejectFn));
1031
+ __rb_host_promise_subscribe(handle, dehydrate(onFulfilled), dehydrate(onRejected));
822
1032
  return promise;
823
1033
  }
824
1034
 
@@ -837,7 +1047,11 @@ globalThis.__rbHost = (function () {
837
1047
  function dehydrate(v, seen) {
838
1048
  if (typeof v === "string") return /[\ud800-\udfff]/.test(v) ? scrubLoneSurrogates(v) : v;
839
1049
  if (typeof v === "function") return { __rb_callback: registerCallback(v) };
840
- if (isProxy(v)) return { __rb_handle: proxyHandles.get(v) };
1050
+ if (isProxy(v)) {
1051
+ const handle = proxyHandles.get(v);
1052
+ if (!readingImageData && proxyInterfaces.get(v) === "ImageData") markHostBacked(handle);
1053
+ return { __rb_handle: handle };
1054
+ }
841
1055
  if (hostPromiseProxies.has(v)) return { __rb_handle: proxyHandles.get(hostPromiseProxies.get(v)) };
842
1056
  // A BufferSource (ArrayBuffer or any typed-array/DataView view) crosses as
843
1057
  // its raw bytes, so host code gets a uniform byte buffer (TextDecoder.decode,
@@ -877,7 +1091,7 @@ globalThis.__rbHost = (function () {
877
1091
  // getter.
878
1092
  const proto = Object.getPrototypeOf(v);
879
1093
  const isExotic = proto !== Object.prototype && proto !== null;
880
- const handlesEvents = "handleEvent" in v;
1094
+ const handlesEvents = "handleEvent" in v || listenerObjects.has(v);
881
1095
  const acceptsNodes = "acceptNode" in v;
882
1096
  if (isExotic || handlesEvents || acceptsNodes) {
883
1097
  const ref = { __rb_js_ref: registerJsRef(v) };
@@ -1106,12 +1320,53 @@ globalThis.__rbHost = (function () {
1106
1320
  return Array.prototype.map.call(args, dehydrateTop);
1107
1321
  }
1108
1322
 
1323
+ // WebIDL's QuotaExceededError: a DOMException named "QuotaExceededError"
1324
+ // with the amount `requested` and the `quota`, each null when unknown. Made
1325
+ // once DOMException exists, and installed as a global.
1326
+ //
1327
+ // Spec: https://webidl.spec.whatwg.org/#quotaexceedederror
1328
+ let quotaExceededErrorClass = null;
1329
+ function quotaExceededError() {
1330
+ if (quotaExceededErrorClass !== null) return quotaExceededErrorClass;
1331
+ const Base = globalThis.DOMException;
1332
+ if (typeof Base !== "function") return null;
1333
+ const amounts = new WeakMap();
1334
+ const amount = (options, key) => {
1335
+ const value = options == null ? undefined : options[key];
1336
+ if (value === undefined) return null;
1337
+ const number = Number(value);
1338
+ if (!Number.isFinite(number)) throw new TypeError("QuotaExceededError: " + key + " is not a finite number");
1339
+ if (number < 0) throw new RangeError("QuotaExceededError: " + key + " is negative");
1340
+ return number;
1341
+ };
1342
+ class QuotaExceededError extends Base {
1343
+ constructor(message = "", options = undefined) {
1344
+ const quota = amount(options, "quota");
1345
+ const requested = amount(options, "requested");
1346
+ if (quota !== null && requested !== null && requested < quota) {
1347
+ throw new RangeError("QuotaExceededError: requested is less than quota");
1348
+ }
1349
+ super(message === undefined ? "" : String(message), "QuotaExceededError");
1350
+ amounts.set(this, { quota, requested });
1351
+ }
1352
+ get quota() { const a = amounts.get(this); if (!a) throw new TypeError("Illegal invocation"); return a.quota; }
1353
+ get requested() { const a = amounts.get(this); if (!a) throw new TypeError("Illegal invocation"); return a.requested; }
1354
+ }
1355
+ Object.defineProperty(globalThis, "QuotaExceededError", { value: QuotaExceededError, writable: true, enumerable: false, configurable: true });
1356
+ quotaExceededErrorClass = QuotaExceededError;
1357
+ return QuotaExceededError;
1358
+ }
1359
+
1109
1360
  // A host call that raised a Dommy::DOMException comes back tagged so it can be
1110
1361
  // re-thrown JS-side as a real DOMException (name + legacy code, and
1111
1362
  // `instanceof DOMException`). Without this the quickjs gem flattens it to a
1112
1363
  // plain Error, breaking assert_throws_dom and the DOM's error contracts.
1113
1364
  function makeHostError(info) {
1114
1365
  const G = globalThis;
1366
+ if (info.name === "QuotaExceededError" && !info.js_native) {
1367
+ const Quota = quotaExceededError();
1368
+ if (Quota) return new Quota(info.message);
1369
+ }
1115
1370
  // A deliberate JS-native error (TypeError, RangeError, …): build the real
1116
1371
  // constructor so `instanceof` holds. URL construction failures arrive here
1117
1372
  // as TypeError (per the URL Standard), not as a DOMException.
@@ -1417,6 +1672,8 @@ globalThis.__rbHost = (function () {
1417
1672
  if (!isProxy(value)) throw dataCloneError("The object could not be cloned");
1418
1673
  const snapshot = rehydrate(__rb_host_clone(proxyHandles.get(value), !!forStorage));
1419
1674
  serialized = { type: "Platform", interface: primaryInterfaceOf(value), snapshot };
1675
+ const entry = pixelsByHandle.get(proxyHandles.get(value));
1676
+ if (entry !== undefined && entry.pixels !== null) serialized.pixels = entry.pixels.slice();
1420
1677
  } else if (type === "function") {
1421
1678
  throw dataCloneError("A function could not be cloned");
1422
1679
  } else if (brandedBy(SC.symbolValueOf, value) || brandedBy(SC.weakMapHas, value, {}) ||
@@ -1522,6 +1779,15 @@ globalThis.__rbHost = (function () {
1522
1779
  case "Platform":
1523
1780
  // A new instance from the host's snapshot.
1524
1781
  value = rehydrate(__rb_host_clone(proxyHandles.get(serialized.snapshot), false));
1782
+ // An ImageData's pixels: the record's, a copy for each deserialization
1783
+ // (none when its data was never read: made at the first read). Only a
1784
+ // clone the host will keep — a message's, a history state's — sends
1785
+ // them across, when its last proxy goes (releasePixels); a
1786
+ // structuredClone() result stays in this realm.
1787
+ if (serialized.interface === "ImageData") {
1788
+ const pixels = serialized.pixels === undefined ? null : serialized.pixels.slice();
1789
+ pixelsByHandle.set(proxyHandles.get(value), { pixels, hostBacked: deserializingForHost, exposed: pixels !== null });
1790
+ }
1525
1791
  break;
1526
1792
  case "TransferredArrayBuffer":
1527
1793
  case "TransferredPlatform":
@@ -1637,15 +1903,23 @@ globalThis.__rbHost = (function () {
1637
1903
  // this realm, as `{ value, ports }` (the MessagePorts among the transferred
1638
1904
  // values, in order), or `{ error }` when deserialization threw — the caller
1639
1905
  // then fires `messageerror`.
1906
+ // Whether the value being deserialized goes to the host (a record the host
1907
+ // asked for: a message, a history state), which may keep it beyond any proxy
1908
+ // of it.
1909
+ let deserializingForHost = false;
1640
1910
  function deserializeRecord(id) {
1641
1911
  const record = serializedRecords.get(id);
1642
1912
  if (!record) return { error: "The serialized message is gone" };
1913
+ const outer = deserializingForHost;
1914
+ deserializingForHost = true;
1643
1915
  try {
1644
1916
  const { deserialized, transferredValues } = structuredDeserializeWithTransfer(record);
1645
1917
  const ports = transferredValues.filter((v) => isProxy(v) && primaryInterfaceOf(v) === "MessagePort");
1646
1918
  return { value: dehydrateSettle(deserialized), ports: ports.map((p) => ({ __rb_handle: proxyHandles.get(p) })) };
1647
1919
  } catch (e) {
1648
1920
  return { error: String(e && e.message) };
1921
+ } finally {
1922
+ deserializingForHost = outer;
1649
1923
  }
1650
1924
  }
1651
1925
  function releaseRecord(id) {
@@ -1975,6 +2249,12 @@ globalThis.__rbHost = (function () {
1975
2249
  bubbles: "boolean", cancelable: "boolean", composed: "boolean",
1976
2250
  command: "DOMString", source: "any",
1977
2251
  },
2252
+ // `key`, `oldValue` and `newValue` are `DOMString?` and `storageArea` a
2253
+ // `Storage?`, so null passes through to Ruby; `url` is a USVString.
2254
+ StorageEvent: {
2255
+ bubbles: "boolean", cancelable: "boolean", composed: "boolean",
2256
+ key: "any", newValue: "any", oldValue: "any", storageArea: "any", url: "DOMString",
2257
+ },
1978
2258
  };
1979
2259
 
1980
2260
  // WebIDL argument coercion for a constructor that takes `(DOMString type,
@@ -2293,11 +2573,46 @@ globalThis.__rbHost = (function () {
2293
2573
  const coerced = coerceConstructorArgs(name, args);
2294
2574
  return makeJsEvent(name, coerced[0], coerced[1]);
2295
2575
  }
2576
+ if (name === "ImageData") return constructImageData(args);
2296
2577
  const r = rehydrate(__rb_construct(name, dehydrateArgs(coerceConstructorArgs(name, args))));
2297
2578
  if (r == null) throw new TypeError("Illegal constructor");
2298
2579
  return r;
2299
2580
  }
2300
2581
 
2582
+ // `new ImageData(data, sw [, sh [, settings]])` keeps `data` itself as its
2583
+ // pixels (`img.data === data`, and a write to either is seen through the
2584
+ // other), so it stays here: the host gets the size alone. Anything but a
2585
+ // Uint8ClampedArray first is the `(sw, sh [, settings])` overload, whose
2586
+ // `sw` and `sh` are ToNumber'd here (`[2]` is 2, a buffer or other view
2587
+ // NaN, so 0), so an array never reaches the host as pixels.
2588
+ //
2589
+ // Spec: https://html.spec.whatwg.org/#dom-imagedata-with-data
2590
+ function constructImageData(args) {
2591
+ const data = args[0];
2592
+ if (!(data instanceof Uint8ClampedArray)) {
2593
+ const sized = args.map((arg, i) => (i < 2 ? +arg : arg));
2594
+ const r = rehydrate(__rb_construct("ImageData", dehydrateArgs(sized)));
2595
+ if (r == null) throw new TypeError("Illegal constructor");
2596
+ return r;
2597
+ }
2598
+ const fail = (message, errorName) => new globalThis.DOMException("Failed to construct 'ImageData': " + message, errorName);
2599
+ if (args.length < 2) throw new TypeError("Failed to construct 'ImageData': 2 arguments required, but only 1 present.");
2600
+ const length = data.length;
2601
+ if (length === 0 || length % 4 !== 0) throw fail("the data length is not a nonzero multiple of 4", "InvalidStateError");
2602
+ const width = Number(args[1]) >>> 0;
2603
+ if (width === 0) throw fail("the source width is zero", "IndexSizeError");
2604
+ const pixelCount = length / 4;
2605
+ if (pixelCount % width !== 0) throw fail("the data length is not a multiple of the width", "IndexSizeError");
2606
+ const height = pixelCount / width;
2607
+ if (args[2] !== undefined && (Number(args[2]) >>> 0) !== height) {
2608
+ throw fail("the data length is not width * height * 4", "IndexSizeError");
2609
+ }
2610
+ const r = rehydrate(__rb_construct("ImageData", dehydrateArgs([width, height])));
2611
+ if (r == null) throw new TypeError("Illegal constructor");
2612
+ pixelsByHandle.set(proxyHandles.get(r), { pixels: data, hostBacked: false, exposed: true });
2613
+ return r;
2614
+ }
2615
+
2301
2616
  // ===== iterable<K, V> (WebIDL §3.7.9) =====
2302
2617
  //
2303
2618
  // A pair iterable's prototype gets entries, keys, values and forEach, and an
@@ -2584,10 +2899,10 @@ globalThis.__rbHost = (function () {
2584
2899
  }
2585
2900
  }
2586
2901
  lazyChains.delete(name);
2587
- if (lazyGlobals.delete(name)) {
2588
- Object.defineProperty(globalThis, name, { value: ctor, writable: true, enumerable: true, configurable: true });
2589
- } else if (!(name in globalThis)) {
2590
- globalThis[name] = ctor;
2902
+ // An interface object is a writable, non-enumerable, configurable global
2903
+ // (WebIDL).
2904
+ if (lazyGlobals.delete(name) || !(name in globalThis)) {
2905
+ Object.defineProperty(globalThis, name, { value: ctor, writable: true, enumerable: false, configurable: true });
2591
2906
  }
2592
2907
  protos.set(name, proto);
2593
2908
  if (staticNames !== null) attachStaticsTo(name, ctor, staticNames[name]);
@@ -2623,24 +2938,39 @@ globalThis.__rbHost = (function () {
2623
2938
  }
2624
2939
  }
2625
2940
 
2941
+ // WebIDL `[LegacyWindowAlias]`: a further global name for an interface
2942
+ // object, `webkitURL` being URL itself. Like the interface object's own
2943
+ // global it is writable, non-enumerable and configurable.
2944
+ const LEGACY_WINDOW_ALIASES = { webkitURL: "URL" };
2945
+
2946
+ function exposeLegacyWindowAliases() {
2947
+ for (const alias in LEGACY_WINDOW_ALIASES) {
2948
+ const target = LEGACY_WINDOW_ALIASES[alias];
2949
+ if (alias in globalThis || !lazyChains.has(target) && !protos.has(target)) continue;
2950
+ defineLazyGlobal(alias, () => protoNamed(target).constructor);
2951
+ }
2952
+ }
2953
+
2626
2954
  // A global that builds its value the first time it is read, then becomes an
2627
2955
  // ordinary data property holding it. Assigning it first replaces it, as
2628
2956
  // assigning any global does.
2957
+ // Interface objects and legacy factory functions are its only users, so it
2958
+ // is non-enumerable as WebIDL has them.
2629
2959
  function defineLazyGlobal(name, build) {
2630
2960
  lazyGlobals.add(name);
2631
2961
  Object.defineProperty(globalThis, name, {
2632
2962
  get() {
2633
2963
  const value = build();
2634
2964
  if (lazyGlobals.delete(name)) {
2635
- Object.defineProperty(globalThis, name, { value, writable: true, enumerable: true, configurable: true });
2965
+ Object.defineProperty(globalThis, name, { value, writable: true, enumerable: false, configurable: true });
2636
2966
  }
2637
2967
  return value;
2638
2968
  },
2639
2969
  set(value) {
2640
2970
  lazyGlobals.delete(name);
2641
- Object.defineProperty(globalThis, name, { value, writable: true, enumerable: true, configurable: true });
2971
+ Object.defineProperty(globalThis, name, { value, writable: true, enumerable: false, configurable: true });
2642
2972
  },
2643
- enumerable: true,
2973
+ enumerable: false,
2644
2974
  configurable: true,
2645
2975
  });
2646
2976
  }
@@ -2660,6 +2990,7 @@ globalThis.__rbHost = (function () {
2660
2990
  }
2661
2991
  }
2662
2992
  exposeNamedConstructors();
2993
+ exposeLegacyWindowAliases();
2663
2994
  }
2664
2995
 
2665
2996
  // 1c: expose an interface constructor's static/class methods (URL.createObjectURL,
@@ -2704,6 +3035,7 @@ globalThis.__rbHost = (function () {
2704
3035
  // `instanceof` and `doc.defaultView.X` in iframe documents.
2705
3036
  const w = target || globalThis.window;
2706
3037
  if (!w) return;
3038
+ const own = proxyTargets.get(w) || w;
2707
3039
  // The bare `customElements` is the window's registry, the same object as
2708
3040
  // `window.customElements` ([Replaceable]: an assignment replaces it).
2709
3041
  if (!target) {
@@ -2715,8 +3047,15 @@ globalThis.__rbHost = (function () {
2715
3047
  enumerable: true, configurable: true,
2716
3048
  });
2717
3049
  }
3050
+ // QuotaExceededError is built on its first use, which installs it as a
3051
+ // global: build it now, so it is one from the start, unified or not.
3052
+ const quotaExceeded = quotaExceededError();
3053
+ // Unified, the window is the global object: its constructors and builtins
3054
+ // are already its own properties.
3055
+ if (own === globalThis) return;
2718
3056
  const names = [...protos.keys(), ...lazyChains.keys()];
2719
3057
  if (!names.includes("DOMException") && typeof globalThis.DOMException === "function") names.push("DOMException");
3058
+ if (quotaExceeded) names.push("QuotaExceededError");
2720
3059
  // Mirror the JS built-in constructors too, so an iframe's contentWindow
2721
3060
  // resolves `defaultView.TypeError` / `defaultView.Array` like a real window
2722
3061
  // (WPT reaches for `(root.ownerDocument).defaultView.TypeError`). The same
@@ -2728,18 +3067,20 @@ globalThis.__rbHost = (function () {
2728
3067
  // otherwise resolves them to the host-backed Constructor proxy (a
2729
3068
  // non-constructable "object"), so replace those the same way as interfaces.
2730
3069
  for (const alias in NAMED_CONSTRUCTORS) { names.push(alias); interfaceNames.add(alias); }
3070
+ // So are the legacy window aliases, which name interface objects.
3071
+ for (const alias in LEGACY_WINDOW_ALIASES) { names.push(alias); interfaceNames.add(alias); }
2731
3072
  for (const name of names) {
2732
3073
  // A global not built yet stays unbuilt: the window resolves it through
2733
3074
  // the global the first time it is read, then holds it as a value.
2734
3075
  if (lazyGlobals.has(name)) {
2735
3076
  try {
2736
- Object.defineProperty(w, name, {
3077
+ Object.defineProperty(own, name, {
2737
3078
  get() {
2738
3079
  const value = globalThis[name];
2739
- Object.defineProperty(w, name, { value, configurable: true, writable: true });
3080
+ Object.defineProperty(own, name, { value, configurable: true, writable: true });
2740
3081
  return value;
2741
3082
  },
2742
- set(value) { Object.defineProperty(w, name, { value, configurable: true, writable: true }); },
3083
+ set(value) { Object.defineProperty(own, name, { value, configurable: true, writable: true }); },
2743
3084
  configurable: true,
2744
3085
  });
2745
3086
  } catch (e) { /* non-configurable / frozen — leave as-is */ }
@@ -2761,7 +3102,7 @@ globalThis.__rbHost = (function () {
2761
3102
  // replaced unconditionally rather than only when it isn't already a
2762
3103
  // function — else `new cw.Event(...)` throws "not a constructor".
2763
3104
  if (current == null || interfaceNames.has(name)) {
2764
- Object.defineProperty(w, name, { value: ctor, configurable: true, writable: true });
3105
+ Object.defineProperty(own, name, { value: ctor, configurable: true, writable: true });
2765
3106
  }
2766
3107
  } catch (e) { /* non-configurable / frozen — leave as-is */ }
2767
3108
  }
@@ -2778,14 +3119,113 @@ globalThis.__rbHost = (function () {
2778
3119
  for (const name of JS_GLOBALS) {
2779
3120
  if (!(name in globalThis)) continue;
2780
3121
  try {
2781
- Object.defineProperty(w, name, { value: globalThis[name], configurable: true, writable: true });
3122
+ Object.defineProperty(own, name, { value: globalThis[name], configurable: true, writable: true });
2782
3123
  } catch (e) { /* non-configurable / frozen — leave as-is */ }
2783
3124
  }
2784
3125
  }
2785
3126
 
2786
3127
  // Bind a bridged Ruby object to a JS global (the bridge's define_host_object).
3128
+ // `window` and `document` are the window's [LegacyUnforgeable] readonly
3129
+ // attributes: an assignment to the bare name leaves them (a strict-mode one
3130
+ // throws). They stay configurable so a host can bind them again (a fresh
3131
+ // document per WPT file in a reused VM).
3132
+ const UNFORGEABLE_GLOBALS = new Set(["window", "document"]);
2787
3133
  function defineGlobal(name, handle) {
2788
- globalThis[name] = makeProxy(handle);
3134
+ const value = makeProxy(handle);
3135
+ if (UNFORGEABLE_GLOBALS.has(name)) {
3136
+ Object.defineProperty(globalThis, name, { value, writable: false, enumerable: true, configurable: true });
3137
+ } else {
3138
+ globalThis[name] = value;
3139
+ }
3140
+ }
3141
+
3142
+ // Make the engine's global object the top window, given the window's handle:
3143
+ // `window`, `self`, `frames`, `top`, `parent`, `document.defaultView` and
3144
+ // every other place a host hands out the window are then globalThis, as in a
3145
+ // browser. Called instead of defineGlobal("window", …); calling it again
3146
+ // rebinds to another window.
3147
+ //
3148
+ // The shape is WebIDL's for a [Global] interface: the window's operations
3149
+ // and attributes are own properties of the global itself (an operation a
3150
+ // data property, an attribute an accessor reaching the host, a
3151
+ // [Replaceable] one replaced by an assignment), so a bare `fetch` never
3152
+ // crosses a trap, and `globalThis.fetch = f` replaces it. The window's host
3153
+ // proxy goes to the bottom of globalThis's prototype chain, the place of
3154
+ // HTML's named properties object: it answers what the global does not own —
3155
+ // a named element, an event handler attribute, a host property no IDL
3156
+ // lists — but never a name the global owned when it was made (an interface
3157
+ // object, a builtin, a window member), so one the page deletes stays
3158
+ // deleted. An unresolvable bare name still asks its `has` trap and throws a
3159
+ // ReferenceError, which needs an engine that consults a Proxy's `has` while
3160
+ // resolving an identifier (QuickJS does not: there a bare name a Proxy on
3161
+ // the global's chain lacks reads as undefined), so a backend opts in.
3162
+ function unifyGlobalWindow(handle) {
3163
+ unifiedWindowHandle = null;
3164
+ const proxy = makeProxy(handle, "Window");
3165
+ unifiedWindowProxy = proxy;
3166
+ unifiedWindowHandle = handle;
3167
+ proxyHandles.set(globalThis, handle);
3168
+ proxyInterfaces.set(globalThis, "Window");
3169
+ Object.setPrototypeOf(globalThis, proxy);
3170
+ const previousMembers = unifiedWindowMembers;
3171
+ unifiedWindowMembers = new Set();
3172
+ const define = (name, desc) => {
3173
+ try {
3174
+ Object.defineProperty(globalThis, name, { enumerable: true, configurable: true, ...desc });
3175
+ unifiedWindowMembers.add(name);
3176
+ } catch (e) { /* a non-configurable global keeps its definition */ }
3177
+ };
3178
+ const replaceable = (name, get) => define(name, {
3179
+ get, set(value) { define(name, { value, writable: true }); },
3180
+ });
3181
+ // Not on globalThis, or the previous window's member there.
3182
+ const missing = (name) => previousMembers.has(name) || !Object.hasOwn(globalThis, name);
3183
+ // [LegacyUnforgeable] readonly; configurable so a host can rebind it.
3184
+ define("window", { value: globalThis, writable: false });
3185
+ // Getter only, so a strict-mode assignment throws as on a browser's.
3186
+ define("top", { get() { return globalThis; } });
3187
+ // The bridge's own globals are no window members for Object.keys(window)
3188
+ // or for-in to list (the split window proxy's ownKeys leaves them out).
3189
+ for (const name of Object.keys(globalThis)) {
3190
+ if (!name.startsWith("__rb")) continue;
3191
+ try {
3192
+ Object.defineProperty(globalThis, name, { enumerable: false });
3193
+ } catch (e) { /* a non-configurable global stays enumerable */ }
3194
+ }
3195
+ for (const name of ["self", "frames", "parent"]) replaceable(name, () => globalThis);
3196
+ define("location", {
3197
+ get() { return windowMember("location"); },
3198
+ set(value) { hostSet(handle, "location", convertAttributeValue("Window", "location", value)); },
3199
+ });
3200
+ const idl = globalThis.__rbIdlMembers || {};
3201
+ const members = idl.Window || {};
3202
+ const operations = [...(members.m || []), ...((idl.EventTarget || {}).m || [])];
3203
+ for (const name of operations) {
3204
+ if (!missing(name)) continue;
3205
+ const fn = windowMember(name);
3206
+ if (typeof fn === "function") define(name, { value: fn, writable: true });
3207
+ }
3208
+ for (const name of members.g || []) {
3209
+ if (missing(name)) define(name, { get() { return windowMember(name); } });
3210
+ }
3211
+ // A mixin member the window has no object for (Dommy implements neither
3212
+ // indexedDB nor caches) is no global: a getter answering undefined would
3213
+ // make `"indexedDB" in globalThis` claim the feature and refuse the
3214
+ // polyfill a page assigns in its place.
3215
+ for (const name of WINDOW_MIXIN_READONLY) {
3216
+ if (missing(name) && windowMember(name) != null) define(name, { get() { return windowMember(name); } });
3217
+ }
3218
+ for (const name of [...(members.p || []), ...WINDOW_MIXIN_REPLACEABLE]) {
3219
+ // An event handler attribute stays with the proxy, which registers the
3220
+ // listener behind it with the host.
3221
+ if (!missing(name) || name.startsWith("on")) continue;
3222
+ if (WINDOW_HOST_WRITABLE.has(name)) {
3223
+ define(name, { get() { return windowMember(name); }, set(value) { hostSet(handle, name, value); } });
3224
+ } else {
3225
+ replaceable(name, () => windowMember(name));
3226
+ }
3227
+ }
3228
+ unifiedGlobalNames = new Set(Reflect.ownKeys(globalThis).filter((k) => typeof k === "string"));
2789
3229
  }
2790
3230
 
2791
3231
  // Expose the seeded constructors on a secondary window — an iframe's
@@ -2807,9 +3247,12 @@ globalThis.__rbHost = (function () {
2807
3247
  function defineLegacyEventAccessor() {
2808
3248
  Object.defineProperty(globalThis, "event", {
2809
3249
  configurable: true, enumerable: false,
2810
- get() { const w = globalThis.window; return w ? w.event : undefined; },
3250
+ get() { return windowMember("event"); },
2811
3251
  });
2812
- defineGlobalEventHandlers();
3252
+ // Unified, a bare `onload` already reaches the window's handler attribute
3253
+ // through the prototype chain; an accessor forwarding to `window.onload`
3254
+ // would read itself.
3255
+ if (unifiedWindowHandle === null) defineGlobalEventHandlers();
2813
3256
  }
2814
3257
 
2815
3258
  // The Window's event handler IDL attributes, reachable as bare globals
@@ -3105,9 +3548,16 @@ globalThis.__rbHost = (function () {
3105
3548
  };
3106
3549
  }
3107
3550
 
3551
+ // Objects passed as an EventListener: one with no handleEvent yet is still a
3552
+ // callback interface (handleEvent is looked up when it is called), so it
3553
+ // crosses as a live reference rather than as a data map.
3554
+ const listenerObjects = new WeakSet();
3555
+
3108
3556
  function listenerStub(prop, ctx) {
3109
3557
  const handle = ctx.handle;
3110
3558
  return (...args) => {
3559
+ const listener = args[1];
3560
+ if (listener !== null && typeof listener === "object") listenerObjects.add(listener);
3111
3561
  if (args.length >= 3) args[2] = flattenListenerOptions(prop, args[2]);
3112
3562
  return hostCallResult(prop, __rb_host_call(handle, prop, dehydrateArgs(args)), ctx.ifaceName);
3113
3563
  };
@@ -3275,16 +3725,18 @@ globalThis.__rbHost = (function () {
3275
3725
  if (!fn) {
3276
3726
  if (CANCELED_STATE_METHODS.has(prop)) fn = canceledStateStub(prop, ctx);
3277
3727
  else if (NON_MUTATING_METHODS.has(prop)) fn = nonMutatingStub(prop, ctx);
3278
- else if (NODE_OR_STRING_METHODS.has(prop)) fn = nodeOrStringStub(prop, ctx);
3728
+ else if (nodeOrStringMember(prop, ctx.ifaceName)) fn = nodeOrStringStub(prop, ctx);
3279
3729
  else fn = mutatingStub(prop, ctx);
3280
3730
  }
3281
- fn = withConvertedArguments(fn, ctx.ifaceName, "operations", prop);
3731
+ fn = withConvertedArguments(withImageDataRead(fn, ctx.ifaceName, prop), ctx.ifaceName, "operations", prop);
3282
3732
  if (prop === "toString" && ctx.ifaceName) fn = brandCheckedStringifier(fn, ctx);
3283
3733
  withArity(fn, prop, ctx.ifaceName);
3284
- // A special stub whose name no arity table lists takes the length its IDL
3285
- // declares (the shortest overload's required count): `postMessage.length`
3286
- // and `structuredClone.length` are 1.
3287
- if (special && fn.length === 0) {
3734
+ // An operation's function is named after it (`fetch.name`), and one no
3735
+ // arity table lists takes the length its IDL declares (the shortest
3736
+ // overload's required count): `postMessage.length` and
3737
+ // `structuredClone.length` are 1.
3738
+ if (typeof prop === "string") Object.defineProperty(fn, "name", { value: prop, configurable: true });
3739
+ if (fn.length === 0) {
3288
3740
  const call = declaredConversion(ctx.ifaceName, "operations", prop);
3289
3741
  if (call && call.required) Object.defineProperty(fn, "length", { value: call.required, configurable: true });
3290
3742
  }
@@ -3322,7 +3774,7 @@ globalThis.__rbHost = (function () {
3322
3774
  }
3323
3775
 
3324
3776
  function isNamedKeyOf(handle, named, prop) {
3325
- return !!named && typeof prop === "string" && namedKeysOf(handle, named).indexOf(prop) !== -1;
3777
+ return !!named && typeof prop === "string" && namedKeysOf(handle, named).indexOf(hostName(prop)) !== -1;
3326
3778
  }
3327
3779
 
3328
3780
  // WebIDL named-property visibility: a named property is EXPOSED (reachable via
@@ -3462,6 +3914,27 @@ globalThis.__rbHost = (function () {
3462
3914
  return true;
3463
3915
  }
3464
3916
 
3917
+ // The global window: a write to a property of globalThis it answers as its
3918
+ // own (see windowOwnGlobal), or to a bare forward the bridge put there for a
3919
+ // Window member (`window.fetch = f` replaces the bare `fetch`), is a write
3920
+ // to globalThis — a data property's value, or an accessor's setter with the
3921
+ // window as `this`. A forward accessor keeps routing to the host (an
3922
+ // assignment to `location` navigates). The page has then defined a data
3923
+ // property on the window, which reads back from globalThis even where the
3924
+ // embedder's forwards may read the window (windowDefinitions).
3925
+ function setWindowOwnGlobal(handle, shape, t, prop, value, receiver) {
3926
+ if (typeof prop !== "string" || !isGlobalWindow(handle) || isEventHandlerName(shape, prop)) return undefined;
3927
+ const d = Reflect.getOwnPropertyDescriptor(globalThis, prop);
3928
+ if (d === undefined) return undefined;
3929
+ if ("value" in d) {
3930
+ if (!Reflect.set(globalThis, prop, value)) return false;
3931
+ windowDefinitions.add(prop);
3932
+ return true;
3933
+ }
3934
+ if (windowOwnGlobal(shape, t, prop) === undefined) return undefined;
3935
+ return Reflect.set(globalThis, prop, value, receiver);
3936
+ }
3937
+
3465
3938
  // The global window: a write to a name the host doesn't already resolve
3466
3939
  // becomes a JS global (`window.X = …` ≡ `globalThis.X = …`), so window-attached
3467
3940
  // and globalThis-attached globals converge on ONE storage. A host-resolved
@@ -3475,17 +3948,47 @@ globalThis.__rbHost = (function () {
3475
3948
  // unset, so the null-means-unresolved test below would otherwise divert them
3476
3949
  // to a plain (never-firing) JS global.
3477
3950
  if (isEventHandlerName(shape, prop)) return undefined;
3478
- const cur = __rb_host_get(handle, prop);
3951
+ const cur = __rb_host_get(handle, hostName(prop));
3479
3952
  const absent = cur !== null && typeof cur === "object" && cur.__rb_absent === true;
3480
3953
  if (!absent && rehydrate(cur) !== null) return undefined;
3481
3954
 
3955
+ // Unified, this write reached the window proxy through globalThis's
3956
+ // prototype chain (globalThis has no own `prop`), so an assignment would
3957
+ // come straight back here: define the global instead.
3958
+ if (unifiedWindowHandle !== null) {
3959
+ return Reflect.defineProperty(globalThis, prop, { value, writable: true, enumerable: true, configurable: true });
3960
+ }
3482
3961
  globalThis[prop] = value;
3483
3962
  return true;
3484
3963
  }
3485
3964
 
3965
+ // The unified global window (see unifyGlobalWindow): a write reaches the
3966
+ // window proxy through globalThis's prototype chain only when globalThis has
3967
+ // no own property by that name, with globalThis as the receiver. It is then
3968
+ // OrdinarySet over the window's own chain, as in a browser: an accessor runs
3969
+ // with globalThis as `this` (a writable attribute reaches the host, a
3970
+ // readonly one refuses), and an operation or any other inherited data
3971
+ // property is shadowed by an own property of globalThis (`globalThis.fetch =
3972
+ // f` replaces the bare `fetch`). A name the chain lacks goes on to the rules
3973
+ // below (a host attribute, else a new global).
3974
+ function setThroughGlobalPrototype(handle, shape, t, prop, value, receiver) {
3975
+ if (handle !== unifiedWindowHandle || receiver !== globalThis) return undefined;
3976
+ if (unifiedGlobalNames.has(prop)) {
3977
+ return Reflect.defineProperty(globalThis, prop, { value, writable: true, enumerable: true, configurable: true });
3978
+ }
3979
+ for (let o = t; o !== null; o = Reflect.getPrototypeOf(o)) {
3980
+ if (Reflect.getOwnPropertyDescriptor(o, prop) !== undefined) return Reflect.set(o, prop, value, receiver);
3981
+ }
3982
+ if (typeof prop === "symbol") {
3983
+ return Reflect.defineProperty(globalThis, prop, { value, writable: true, enumerable: true, configurable: true });
3984
+ }
3985
+ return undefined;
3986
+ }
3987
+
3486
3988
  // The rules that can CLAIM a write, in the order they are asked. A write no
3487
3989
  // rule claims is a host property write — setHostProperty, which always answers.
3488
3990
  const SET_RULES = [
3991
+ setThroughGlobalPrototype,
3489
3992
  setSymbolKey,
3490
3993
  setImmutablePrototype,
3491
3994
  setUnforgeableAttribute,
@@ -3494,6 +3997,7 @@ globalThis.__rbHost = (function () {
3494
3997
  rejectIndexedWrite,
3495
3998
  rejectNamedWrite,
3496
3999
  setJsExpando,
4000
+ setWindowOwnGlobal,
3497
4001
  setWindowGlobal,
3498
4002
  ];
3499
4003
 
@@ -3633,15 +4137,16 @@ globalThis.__rbHost = (function () {
3633
4137
  return attrsCache;
3634
4138
  };
3635
4139
  // A cached-attribute read: answers from the snapshot when one is
3636
- // available, else falls back to a normal bridge call. Lookup lowercases
3637
- // the argument — snapshots exist only for elements whose Ruby-side lookup
3638
- // is case-insensitive, so this matches get_attribute exactly. "__proto__"
4140
+ // available, else falls back to a normal bridge call. Lookup ASCII-
4141
+ // lowercases the argument — snapshots exist only for elements whose
4142
+ // Ruby-side lookup is case-insensitive, so this matches get_attribute
4143
+ // exactly (toLowerCase would also fold "Ä" to "ä"). "__proto__"
3639
4144
  // is excluded (a snapshot object can't represent it as a data property).
3640
4145
  const cachedAttrRead = (method, name) => {
3641
4146
  let attrs = null;
3642
4147
  let key;
3643
4148
  if (typeof name === "string") {
3644
- key = name.toLowerCase();
4149
+ key = name.replace(/[A-Z]+/g, (s) => s.toLowerCase());
3645
4150
  if (key !== "__proto__") attrs = attrsSnapshot();
3646
4151
  }
3647
4152
  if (attrs === null) return rehydrate(__rb_host_call(handle, method, dehydrateArgs([name])));
@@ -3684,7 +4189,7 @@ globalThis.__rbHost = (function () {
3684
4189
  namedKeyCache = new Set(namedKeysOf(handle, named));
3685
4190
  namedKeyEpoch = domEpoch;
3686
4191
  }
3687
- return namedKeyCache.has(prop);
4192
+ return namedKeyCache.has(hostName(prop));
3688
4193
  }
3689
4194
  : (prop) => isNamedKeyOf(handle, named, prop);
3690
4195
  const namedShadowedByProto = (t, prop) => namedShadowedByProtoOf(named, t, prop);
@@ -3698,17 +4203,29 @@ globalThis.__rbHost = (function () {
3698
4203
  // would resurrect the host-backed constructor on the next read. Re-assigning
3699
4204
  // the name clears the tombstone. Only the global window is affected.
3700
4205
  const deletedGlobals = new Set();
4206
+ // Whether the host answers `prop` here, so that a global deleted under
4207
+ // that name needs a tombstone.
4208
+ const hostResolves = (prop) => {
4209
+ if (methods.has(prop)) return true;
4210
+ const raw = __rb_host_get(handle, hostName(prop));
4211
+ return raw !== null && !(typeof raw === "object" && raw.__rb_absent === true);
4212
+ };
3701
4213
  return {
3702
4214
  get(t, prop, receiver) {
3703
4215
  if (prop === HKEY) return handle;
4216
+ if (handle === unifiedWindowHandle && receiver === globalThis && unifiedGlobalNames.has(prop)) return undefined;
3704
4217
  if (typeof prop === "symbol") return Reflect.get(t, prop, receiver);
3705
- if (typeof prop === "string" && isGlobalWindow(handle) && deletedGlobals.has(prop)) return undefined;
4218
+ if (isGlobalWindow(handle) && !answeringFromHost) {
4219
+ const d = windowOwnGlobal(shape, t, prop);
4220
+ if (d !== undefined) return "value" in d ? d.value : (d.get === undefined ? undefined : d.get.call(receiver));
4221
+ if (deletedGlobals.has(prop)) return undefined;
4222
+ }
3706
4223
  if (Object.hasOwn(t, prop)) return Reflect.get(t, prop, receiver);
3707
4224
  // [LegacyOverrideBuiltIns] (HTMLFormElement): a named control shadows the
3708
4225
  // prototype's methods AND accessors, so resolve it before either. An own
3709
4226
  // expando (checked above) still wins.
3710
4227
  if (named && named.overrideBuiltins && typeof prop === "string" && isNamedKey(prop)) {
3711
- return rehydrate(__rb_named_get(handle, prop));
4228
+ return rehydrate(__rb_named_get(handle, hostName(prop)));
3712
4229
  }
3713
4230
  if (methods.has(prop)) {
3714
4231
  // A read-only collection operation resolves to the interface
@@ -3732,6 +4249,7 @@ globalThis.__rbHost = (function () {
3732
4249
  return Reflect.get(t, prop, receiver);
3733
4250
  }
3734
4251
  if (constCache !== null && constCache.has(prop)) return constCache.get(prop);
4252
+ if (prop === "data" && shape.name === "ImageData" && isProxy(receiver)) return pixelsOf(receiver);
3735
4253
  if (ifaceFrozenArrays !== null && ifaceFrozenArrays.has(prop)) {
3736
4254
  return frozenArrayRead(ifaceAttrCache, prop, rehydrate(__rb_host_get(handle, prop)));
3737
4255
  }
@@ -3772,7 +4290,7 @@ globalThis.__rbHost = (function () {
3772
4290
  // A collection's `.length` is the epoch-cached live count — the same
3773
4291
  // value index-range checks use, fetched once per epoch not per read.
3774
4292
  if (arrayLike && prop === "length") return liveLength();
3775
- const raw = __rb_host_get(handle, prop);
4293
+ const raw = __rb_host_get(handle, hostName(prop));
3776
4294
  // The host signals a genuinely-absent property with the ABSENT tag (value
3777
4295
  // is `undefined`); a present-but-null property is bare nil (→ JS null).
3778
4296
  // "Host owns nothing here" = absent OR (legacy) null, and only that drives
@@ -3792,13 +4310,13 @@ globalThis.__rbHost = (function () {
3792
4310
  // global of the same name (an OWN globalThis prop — inherited names already
3793
4311
  // resolved via `prop in t` above), so e.g. a UMD bundle's
3794
4312
  // `globalThis.Stimulus = …` is visible as `window.Stimulus`.
3795
- if (hostHasNoValue && isGlobalWindow(handle) && Object.hasOwn(globalThis, prop) &&
3796
- !isEventHandlerName(shape, prop)) return globalThis[prop];
4313
+ if (hostHasNoValue && isGlobalWindow(handle) && unifiedWindowHandle === null &&
4314
+ Object.hasOwn(globalThis, prop) && !isEventHandlerName(shape, prop)) return globalThis[prop];
3797
4315
  // A Window's named properties (HTML "named access on the Window
3798
4316
  // object") come after every member and global: they live on the named
3799
4317
  // properties object at the bottom of its prototype chain.
3800
4318
  if (hostHasNoValue && named && named.global && typeof prop === "string" && isNamedKey(prop)) {
3801
- return rehydrate(__rb_named_get(handle, prop));
4319
+ return rehydrate(__rb_named_get(handle, hostName(prop)));
3802
4320
  }
3803
4321
  // A legacy platform collection returns `undefined` (not the host's null)
3804
4322
  // for a string property that resolves to no value. An out-of-range array
@@ -3829,33 +4347,61 @@ globalThis.__rbHost = (function () {
3829
4347
  // Named properties (HTMLCollection ids/names, dataset keys, attr names)
3830
4348
  // are reflected too — non-enumerable for [LegacyUnenumerableNamedProperties].
3831
4349
  getOwnPropertyDescriptor(t, prop) {
3832
- if (typeof prop === "string" && isGlobalWindow(handle) && deletedGlobals.has(prop)) return undefined;
3833
- if (typeof prop !== "symbol" && Object.hasOwn(t, prop)) return Reflect.getOwnPropertyDescriptor(t, prop);
3834
- // The global window reflects JS globals as own properties. Clamp
3835
- // configurable (a top-level `var` is non-configurable on globalThis,
3836
- // but the proxy invariant forbids reporting non-configurable for a
3837
- // prop absent from the target).
3838
- if (typeof prop !== "symbol" && isGlobalWindow(handle) && Object.hasOwn(globalThis, prop)) {
4350
+ // The global window reflects globalThis's properties as its own, ahead
4351
+ // of a mirror on the target. One the target has non-configurable — an
4352
+ // unforgeable attribute, or a global defined so (see defineProperty) —
4353
+ // is reported so, as a proxy must; any other is configurable, as only
4354
+ // the target's own properties may be reported otherwise (a top-level
4355
+ // `var` is non-configurable on globalThis).
4356
+ if (typeof prop === "string" && isGlobalWindow(handle)) {
4357
+ const own = Reflect.getOwnPropertyDescriptor(t, prop);
3839
4358
  const d = Reflect.getOwnPropertyDescriptor(globalThis, prop);
3840
- if (d) { d.configurable = true; return d; }
4359
+ if (own !== undefined && !own.configurable) return d !== undefined && !d.configurable ? d : own;
4360
+ if (d !== undefined) { d.configurable = true; return d; }
4361
+ if (deletedGlobals.has(prop)) return undefined;
3841
4362
  }
4363
+ if (typeof prop !== "symbol" && Object.hasOwn(t, prop)) return Reflect.getOwnPropertyDescriptor(t, prop);
3842
4364
  if (isIndexInRange(prop)) {
3843
4365
  // Indexed properties are enumerable + configurable but NOT writable
3844
4366
  // (these collections have no indexed property setter).
3845
4367
  return {
3846
- value: rehydrate(__rb_host_get(handle, prop)),
4368
+ value: rehydrate(__rb_host_get(handle, hostName(prop))),
3847
4369
  writable: false, enumerable: true, configurable: true,
3848
4370
  };
3849
4371
  }
3850
4372
  if (isNamedKey(prop) && !named.global && !namedShadowedByProto(t, prop)) {
3851
4373
  return {
3852
- value: rehydrate(__rb_host_get(handle, prop)),
4374
+ value: rehydrate(__rb_host_get(handle, hostName(prop))),
3853
4375
  writable: named.writable, enumerable: named.enumerable, configurable: true,
3854
4376
  };
3855
4377
  }
3856
4378
  return Reflect.getOwnPropertyDescriptor(t, prop);
3857
4379
  },
3858
4380
  defineProperty(t, prop, desc) {
4381
+ // A property defined on the global window is a global (the window IS
4382
+ // the global object in a browser), so the bare name resolves to it. One
4383
+ // the target has non-configurable and globalThis does not (an
4384
+ // unforgeable attribute) stays the target's. A global defined
4385
+ // non-configurable is mirrored on the target, as a proxy may report a
4386
+ // property non-configurable only if its target has it so; the target's
4387
+ // other copy goes once globalThis has taken the definition.
4388
+ if (typeof prop === "string" && isGlobalWindow(handle) && !isEventHandlerName(shape, prop)) {
4389
+ const own = Reflect.getOwnPropertyDescriptor(t, prop);
4390
+ const global = Reflect.getOwnPropertyDescriptor(globalThis, prop);
4391
+ if (own !== undefined && !own.configurable && (global === undefined || global.configurable)) {
4392
+ return Reflect.defineProperty(t, prop, desc);
4393
+ }
4394
+ if (!Reflect.defineProperty(globalThis, prop, desc)) return false;
4395
+ deletedGlobals.delete(prop);
4396
+ windowDefinitions.add(prop);
4397
+ const defined = Reflect.getOwnPropertyDescriptor(globalThis, prop);
4398
+ if (defined.configurable) {
4399
+ if (own !== undefined) Reflect.deleteProperty(t, prop);
4400
+ } else {
4401
+ Reflect.defineProperty(t, prop, defined);
4402
+ }
4403
+ return true;
4404
+ }
3859
4405
  if (typeof prop === "string" && deletedGlobals.has(prop)) deletedGlobals.delete(prop);
3860
4406
  // Cannot redefine a live indexed or read-only named property.
3861
4407
  if (arrayLike && isArrayIndex(prop)) return false;
@@ -3882,8 +4428,11 @@ globalThis.__rbHost = (function () {
3882
4428
  setPrototypeOf(t, proto) {
3883
4429
  return fixedShape ? proto === Reflect.getPrototypeOf(t) : Reflect.setPrototypeOf(t, proto);
3884
4430
  },
4431
+ // A window refuses too: HTML's WindowProxy [[PreventExtensions]] returns
4432
+ // false, and the global window's own keys include globalThis's, which a
4433
+ // non-extensible target could not report.
3885
4434
  preventExtensions(t) {
3886
- return fixedShape ? false : Reflect.preventExtensions(t);
4435
+ return fixedShape || shape.name === "Window" ? false : Reflect.preventExtensions(t);
3887
4436
  },
3888
4437
  deleteProperty(t, prop) {
3889
4438
  // The global window: deleting a JS global through the window drops it
@@ -3895,7 +4444,8 @@ globalThis.__rbHost = (function () {
3895
4444
  // A named property behind it (`<div id=x>` after `window.x = 1;
3896
4445
  // delete window.x`) shows through again; the host does not resolve
3897
4446
  // it, so it needs no tombstone.
3898
- if (!(named && named.global && isNamedKey(prop))) deletedGlobals.add(prop);
4447
+ windowDefinitions.delete(prop);
4448
+ if (!(named && named.global && isNamedKey(prop)) && hostResolves(prop)) deletedGlobals.add(prop);
3899
4449
  Reflect.deleteProperty(t, prop);
3900
4450
  }
3901
4451
  return removed;
@@ -3907,7 +4457,7 @@ globalThis.__rbHost = (function () {
3907
4457
  // Named deleter (dataset): remove the backing attribute — a DOM
3908
4458
  // mutation, so invalidate attribute snapshots.
3909
4459
  bumpDomEpoch();
3910
- if (rehydrate(__rb_host_delete(handle, prop))) return true;
4460
+ if (rehydrate(__rb_host_delete(handle, hostName(prop)))) return true;
3911
4461
  } else if (!named.global && isNamedKey(prop)) {
3912
4462
  return false; // read-only named property cannot be deleted
3913
4463
  }
@@ -3916,6 +4466,15 @@ globalThis.__rbHost = (function () {
3916
4466
  },
3917
4467
  ownKeys(t) {
3918
4468
  const keys = Reflect.ownKeys(t);
4469
+ // The global window's own keys are globalThis's too (see
4470
+ // getOwnPropertyDescriptor), less the bridge's internal names.
4471
+ if (isGlobalWindow(handle)) {
4472
+ const seen = new Set(keys);
4473
+ for (const k of Reflect.ownKeys(globalThis)) {
4474
+ if (typeof k === "string" && !seen.has(k) && !k.startsWith("__rb")) keys.push(k);
4475
+ }
4476
+ return keys;
4477
+ }
3919
4478
  // (A [Global]'s named properties are not its own: see `named.global`.)
3920
4479
  if (!arrayLike && (!named || named.global)) return keys;
3921
4480
  const n = arrayLike ? liveLength() : 0;
@@ -3933,7 +4492,12 @@ globalThis.__rbHost = (function () {
3933
4492
  return result;
3934
4493
  },
3935
4494
  has(t, prop) {
3936
- if (typeof prop === "string" && isGlobalWindow(handle) && deletedGlobals.has(prop)) return false;
4495
+ if (handle === unifiedWindowHandle && unifiedGlobalNames.has(prop)) return false;
4496
+ // The global window has its JS globals (`"Stimulus" in window`).
4497
+ if (typeof prop === "string" && isGlobalWindow(handle)) {
4498
+ if (Object.hasOwn(globalThis, prop)) return true;
4499
+ if (deletedGlobals.has(prop)) return false;
4500
+ }
3937
4501
  // An out-of-range index on an array-like is genuinely absent (`2 in
3938
4502
  // nodeList` is false past its length). A supported named key is present.
3939
4503
  if (arrayLike && isArrayIndex(prop)) return Number(prop) < liveLength() || Reflect.has(t, prop);
@@ -3946,9 +4510,6 @@ globalThis.__rbHost = (function () {
3946
4510
  // and History answer it from the host below, and `"length" in div`
3947
4511
  // or in a URLSearchParams is false, as it is in a browser.)
3948
4512
  if (methods.has(prop)) return true;
3949
- // The global window also reports its JS globals (`"Stimulus" in window`);
3950
- // inherited names already answered true via Reflect.has(t) above.
3951
- if (isGlobalWindow(handle) && Object.hasOwn(globalThis, prop)) return true;
3952
4513
  // Event-handler IDL attributes (onclick, oninput, …) the object's
3953
4514
  // interfaces declare exist as null-default properties, so
3954
4515
  // `("oninput" in document)` is true even when unset — React's
@@ -3968,7 +4529,7 @@ globalThis.__rbHost = (function () {
3968
4529
  // sentinel, tagged `__rb_undefined`) — e.g. AbortSignal's `reason`
3969
4530
  // before abort — so report the latter present (`"reason" in signal`).
3970
4531
  if (constCache !== null && constCache.has(prop)) return true;
3971
- const raw = __rb_host_get(handle, prop);
4532
+ const raw = __rb_host_get(handle, hostName(prop));
3972
4533
  // A genuinely-absent property (ABSENT tag) reports MISSING; a
3973
4534
  // present-but-undefined one (UNDEFINED tag) reports present.
3974
4535
  if (raw !== null && typeof raw === "object" && raw.__rb_absent) return false;
@@ -3979,6 +4540,7 @@ globalThis.__rbHost = (function () {
3979
4540
  }
3980
4541
 
3981
4542
  function makeProxy(handle, iface) {
4543
+ if (handle === unifiedWindowHandle) return globalThis;
3982
4544
  const ref = cache.get(handle);
3983
4545
  if (ref) {
3984
4546
  const existing = ref.deref();
@@ -3989,6 +4551,7 @@ globalThis.__rbHost = (function () {
3989
4551
  if (existing) {
3990
4552
  cache.delete(handle);
3991
4553
  pinned.delete(handle);
4554
+ pixelsByHandle.delete(handle);
3992
4555
  proxyHandles.delete(existing);
3993
4556
  proxyInterfaces.delete(existing);
3994
4557
  }
@@ -4020,6 +4583,7 @@ globalThis.__rbHost = (function () {
4020
4583
  const p = new Proxy(target, makeHandler(handle, shape, new Map()));
4021
4584
  cache.set(handle, new WeakRef(p));
4022
4585
  proxyHandles.set(p, handle);
4586
+ proxyTargets.set(p, target);
4023
4587
  proxyInterfaces.set(p, desc.name);
4024
4588
  // A DOM node's JS wrapper must be STABLE for the node's lifetime, exactly as
4025
4589
  // in a browser (same node -> the same object every time). Otherwise an
@@ -4466,7 +5030,7 @@ globalThis.__rbHost = (function () {
4466
5030
  Object.defineProperty(cssProto, "size", {
4467
5031
  get: function size() { return customStateSetOf(this).set.size; }, enumerable: true, configurable: true,
4468
5032
  });
4469
- globalThis.CustomStateSet = CustomStateSet;
5033
+ Object.defineProperty(globalThis, "CustomStateSet", { value: CustomStateSet, writable: true, enumerable: false, configurable: true });
4470
5034
 
4471
5035
  // ElementInternals.prototype.states ([SameObject]).
4472
5036
  function installElementInternalsStates(proto) {
@@ -4548,7 +5112,7 @@ globalThis.__rbHost = (function () {
4548
5112
 
4549
5113
  return {
4550
5114
  makeProxy, invokeCallback, invokeJsRefHandleEvent, invokeJsRefAcceptNode, runScript, scheduleMicrotask,
4551
- bumpDomEpoch,
5115
+ bumpDomEpoch, windowMember,
4552
5116
  // `tag` is the public top-level dehydrate (used by engine gems' eval_tagged
4553
5117
  // for evaluate() results): it tags a top-level `undefined` so
4554
5118
  // `evaluate("undefined")` yields UNDEFINED on every engine, not just those
@@ -4570,7 +5134,7 @@ globalThis.__rbHost = (function () {
4570
5134
  seedInterfaces, ceConstruct, ceUpgrade, ceInvoke, attachStatics, exposeConstructorsOnWindow,
4571
5135
  // Realm wiring the Ruby bridge drives, kept here rather than as JS built in
4572
5136
  // Ruby strings (see defineGlobal / defineLegacyEventAccessor).
4573
- defineGlobal, exposeConstructorsOnSubWindow, defineLegacyEventAccessor, compileEventHandler,
5137
+ defineGlobal, unifyGlobalWindow, exposeConstructorsOnSubWindow, defineLegacyEventAccessor, compileEventHandler,
4574
5138
  // wasm host bridge (handle-oriented access for a wasm guest)
4575
5139
  wasmGlobalRef, wasmEval, wasmGet, wasmSet, wasmCall, wasmApply, wasmNew,
4576
5140
  wasmTypeof, wasmToString, wasmStrictEqual, wasmIsNull, wasmInstanceof,