@optique/core 1.0.0-dev.1848 → 1.0.0-dev.1850

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.
@@ -46,12 +46,15 @@ const annotationWrapperKeys = new Set([
46
46
  ]);
47
47
  const injectedAnnotationWrappers = /* @__PURE__ */ new WeakSet();
48
48
  const protectedAnnotationTargets = /* @__PURE__ */ new WeakMap();
49
- const annotationProtectionCache = /* @__PURE__ */ new WeakMap();
49
+ const protectedAnnotationStateViews = /* @__PURE__ */ new WeakMap();
50
50
  function throwReadonlyAnnotationMutation() {
51
51
  throw new TypeError("Cannot mutate read-only annotation data.");
52
52
  }
53
- function registerProtectedAnnotationView(target, view) {
54
- annotationProtectionCache.set(target, view);
53
+ function createAnnotationProtectionContext() {
54
+ return { cache: /* @__PURE__ */ new WeakMap() };
55
+ }
56
+ function registerProtectedAnnotationView(context, target, view) {
57
+ context.cache.set(target, view);
55
58
  protectedAnnotationTargets.set(view, target);
56
59
  return view;
57
60
  }
@@ -69,8 +72,8 @@ function cacheProtectedMethod(cache, key, factory) {
69
72
  cache.set(key, created);
70
73
  return created;
71
74
  }
72
- function defineProtectedDataProperty(target, key, descriptor) {
73
- const value = protectAnnotationValue(descriptor.value);
75
+ function defineProtectedDataProperty(context, target, key, descriptor) {
76
+ const value = protectAnnotationValue(descriptor.value, context);
74
77
  Object.defineProperty(target, key, {
75
78
  configurable: descriptor.configurable,
76
79
  enumerable: descriptor.enumerable,
@@ -78,62 +81,64 @@ function defineProtectedDataProperty(target, key, descriptor) {
78
81
  set: () => throwReadonlyAnnotationMutation()
79
82
  });
80
83
  }
81
- function createProtectedObjectView(target) {
84
+ function createProtectedObjectView(target, context) {
82
85
  if (Array.isArray(target)) {
83
86
  const view$1 = new Array(target.length);
87
+ registerProtectedAnnotationView(context, target, view$1);
84
88
  for (const key of Reflect.ownKeys(target)) {
85
89
  if (key === "length") continue;
86
90
  const descriptor = Object.getOwnPropertyDescriptor(target, key);
87
91
  if (descriptor == null) continue;
88
92
  if ("value" in descriptor) {
89
- defineProtectedDataProperty(view$1, key, descriptor);
93
+ defineProtectedDataProperty(context, view$1, key, descriptor);
90
94
  continue;
91
95
  }
92
96
  Object.defineProperty(view$1, key, {
93
97
  configurable: descriptor.configurable,
94
98
  enumerable: descriptor.enumerable,
95
- get: descriptor.get == null ? void 0 : () => protectAnnotationValue(descriptor.get.call(target)),
99
+ get: descriptor.get == null ? void 0 : () => protectAnnotationValue(descriptor.get.call(target), context),
96
100
  set: () => throwReadonlyAnnotationMutation()
97
101
  });
98
102
  }
99
- return registerProtectedAnnotationView(target, Object.freeze(view$1));
103
+ return Object.freeze(view$1);
100
104
  }
101
105
  const view = Object.create(Object.getPrototypeOf(target));
106
+ registerProtectedAnnotationView(context, target, view);
102
107
  for (const key of Reflect.ownKeys(target)) {
103
108
  const descriptor = Object.getOwnPropertyDescriptor(target, key);
104
109
  if (descriptor == null) continue;
105
110
  if ("value" in descriptor) {
106
- defineProtectedDataProperty(view, key, descriptor);
111
+ defineProtectedDataProperty(context, view, key, descriptor);
107
112
  continue;
108
113
  }
109
114
  Object.defineProperty(view, key, {
110
115
  configurable: descriptor.configurable,
111
116
  enumerable: descriptor.enumerable,
112
- get: descriptor.get == null ? void 0 : () => protectAnnotationValue(descriptor.get.call(target)),
117
+ get: descriptor.get == null ? void 0 : () => protectAnnotationValue(descriptor.get.call(target), context),
113
118
  set: () => throwReadonlyAnnotationMutation()
114
119
  });
115
120
  }
116
- return registerProtectedAnnotationView(target, Object.freeze(view));
121
+ return Object.freeze(view);
117
122
  }
118
- function createProtectedMapView(target) {
123
+ function createProtectedMapView(target, context) {
119
124
  const methodCache = /* @__PURE__ */ new Map();
120
125
  const view = new Proxy(target, {
121
126
  get(target$1, key) {
122
127
  if (key === "size") return target$1.size;
123
128
  if (key === "set" || key === "delete" || key === "clear") return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
124
- if (key === "get") return cacheProtectedMethod(methodCache, key, () => (lookup) => protectAnnotationValue(target$1.get(unwrapProtectedAnnotationTarget(lookup))));
129
+ if (key === "get") return cacheProtectedMethod(methodCache, key, () => (lookup) => protectAnnotationValue(target$1.get(unwrapProtectedAnnotationTarget(lookup)), context));
125
130
  if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) => target$1.has(unwrapProtectedAnnotationTarget(lookup)));
126
131
  if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) => target$1.forEach((value$1, mapKey) => {
127
- callback.call(thisArg, protectAnnotationValue(value$1), protectAnnotationValue(mapKey), view);
132
+ callback.call(thisArg, protectAnnotationValue(value$1, context), protectAnnotationValue(mapKey, context), view);
128
133
  }));
129
134
  if (key === "keys") return cacheProtectedMethod(methodCache, key, () => function* () {
130
- for (const value$1 of target$1.keys()) yield protectAnnotationValue(value$1);
135
+ for (const value$1 of target$1.keys()) yield protectAnnotationValue(value$1, context);
131
136
  });
132
137
  if (key === "values") return cacheProtectedMethod(methodCache, key, () => function* () {
133
- for (const value$1 of target$1.values()) yield protectAnnotationValue(value$1);
138
+ for (const value$1 of target$1.values()) yield protectAnnotationValue(value$1, context);
134
139
  });
135
140
  if (key === "entries" || key === Symbol.iterator) return cacheProtectedMethod(methodCache, key, () => function* () {
136
- for (const [entryKey, entryValue] of target$1.entries()) yield [protectAnnotationValue(entryKey), protectAnnotationValue(entryValue)];
141
+ for (const [entryKey, entryValue] of target$1.entries()) yield [protectAnnotationValue(entryKey, context), protectAnnotationValue(entryValue, context)];
137
142
  });
138
143
  const value = Reflect.get(target$1, key, target$1);
139
144
  return typeof value === "function" ? value.bind(target$1) : value;
@@ -154,9 +159,9 @@ function createProtectedMapView(target) {
154
159
  throwReadonlyAnnotationMutation();
155
160
  }
156
161
  });
157
- return registerProtectedAnnotationView(target, view);
162
+ return registerProtectedAnnotationView(context, target, view);
158
163
  }
159
- function createProtectedSetView(target) {
164
+ function createProtectedSetView(target, context) {
160
165
  const methodCache = /* @__PURE__ */ new Map();
161
166
  const view = new Proxy(target, {
162
167
  get(target$1, key) {
@@ -164,15 +169,15 @@ function createProtectedSetView(target) {
164
169
  if (key === "add" || key === "delete" || key === "clear") return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
165
170
  if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) => target$1.has(unwrapProtectedAnnotationTarget(lookup)));
166
171
  if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) => target$1.forEach((value$1) => {
167
- const protectedValue = protectAnnotationValue(value$1);
172
+ const protectedValue = protectAnnotationValue(value$1, context);
168
173
  callback.call(thisArg, protectedValue, protectedValue, view);
169
174
  }));
170
175
  if (key === "keys" || key === "values" || key === Symbol.iterator) return cacheProtectedMethod(methodCache, key, () => function* () {
171
- for (const value$1 of target$1.values()) yield protectAnnotationValue(value$1);
176
+ for (const value$1 of target$1.values()) yield protectAnnotationValue(value$1, context);
172
177
  });
173
178
  if (key === "entries") return cacheProtectedMethod(methodCache, key, () => function* () {
174
179
  for (const value$1 of target$1.values()) {
175
- const protectedValue = protectAnnotationValue(value$1);
180
+ const protectedValue = protectAnnotationValue(value$1, context);
176
181
  yield [protectedValue, protectedValue];
177
182
  }
178
183
  });
@@ -195,9 +200,9 @@ function createProtectedSetView(target) {
195
200
  throwReadonlyAnnotationMutation();
196
201
  }
197
202
  });
198
- return registerProtectedAnnotationView(target, view);
203
+ return registerProtectedAnnotationView(context, target, view);
199
204
  }
200
- function createProtectedDateView(target) {
205
+ function createProtectedDateView(target, context) {
201
206
  const methodCache = /* @__PURE__ */ new Map();
202
207
  const view = new Proxy(target, {
203
208
  get(target$1, key) {
@@ -221,9 +226,9 @@ function createProtectedDateView(target) {
221
226
  throwReadonlyAnnotationMutation();
222
227
  }
223
228
  });
224
- return registerProtectedAnnotationView(target, view);
229
+ return registerProtectedAnnotationView(context, target, view);
225
230
  }
226
- function createProtectedRegExpView(target) {
231
+ function createProtectedRegExpView(target, context) {
227
232
  const methodCache = /* @__PURE__ */ new Map();
228
233
  const cloned = new RegExp(target);
229
234
  cloned.lastIndex = target.lastIndex;
@@ -249,9 +254,9 @@ function createProtectedRegExpView(target) {
249
254
  throwReadonlyAnnotationMutation();
250
255
  }
251
256
  });
252
- return registerProtectedAnnotationView(target, view);
257
+ return registerProtectedAnnotationView(context, target, view);
253
258
  }
254
- function createProtectedURLSearchParamsView(target) {
259
+ function createProtectedURLSearchParamsView(target, context) {
255
260
  const methodCache = /* @__PURE__ */ new Map();
256
261
  const view = new Proxy(target, {
257
262
  get(target$1, key) {
@@ -285,12 +290,12 @@ function createProtectedURLSearchParamsView(target) {
285
290
  throwReadonlyAnnotationMutation();
286
291
  }
287
292
  });
288
- return registerProtectedAnnotationView(target, view);
293
+ return registerProtectedAnnotationView(context, target, view);
289
294
  }
290
- function createProtectedURLView(target) {
295
+ function createProtectedURLView(target, context) {
291
296
  const view = new Proxy(target, {
292
297
  get(target$1, key) {
293
- if (key === "searchParams") return protectAnnotationValue(target$1.searchParams);
298
+ if (key === "searchParams") return protectAnnotationValue(target$1.searchParams, context);
294
299
  const value = Reflect.get(target$1, key, target$1);
295
300
  return typeof value === "function" ? value.bind(target$1) : value;
296
301
  },
@@ -310,23 +315,23 @@ function createProtectedURLView(target) {
310
315
  throwReadonlyAnnotationMutation();
311
316
  }
312
317
  });
313
- return registerProtectedAnnotationView(target, view);
318
+ return registerProtectedAnnotationView(context, target, view);
314
319
  }
315
- function protectAnnotationValue(value) {
320
+ function protectAnnotationValue(value, context) {
316
321
  if (value == null || typeof value !== "object") return value;
317
322
  const target = value;
318
323
  if (isProtectedAnnotationView(value)) return value;
319
- const cached = annotationProtectionCache.get(target);
324
+ const cached = context.cache.get(target);
320
325
  if (cached !== void 0) return cached;
321
- if (target instanceof Map) return createProtectedMapView(target);
322
- if (target instanceof Set) return createProtectedSetView(target);
323
- if (target instanceof Date) return createProtectedDateView(target);
324
- if (target instanceof RegExp) return createProtectedRegExpView(target);
325
- if (typeof URLSearchParams === "function" && target instanceof URLSearchParams) return createProtectedURLSearchParamsView(target);
326
- if (typeof URL === "function" && target instanceof URL) return createProtectedURLView(target);
327
- if (Array.isArray(target)) return createProtectedObjectView(target);
326
+ if (target instanceof Map) return createProtectedMapView(target, context);
327
+ if (target instanceof Set) return createProtectedSetView(target, context);
328
+ if (target instanceof Date) return createProtectedDateView(target, context);
329
+ if (target instanceof RegExp) return createProtectedRegExpView(target, context);
330
+ if (typeof URLSearchParams === "function" && target instanceof URLSearchParams) return createProtectedURLSearchParamsView(target, context);
331
+ if (typeof URL === "function" && target instanceof URL) return createProtectedURLView(target, context);
332
+ if (Array.isArray(target)) return createProtectedObjectView(target, context);
328
333
  const proto = Object.getPrototypeOf(target);
329
- if (proto === Object.prototype || proto === null) return createProtectedObjectView(target);
334
+ if (proto === Object.prototype || proto === null) return createProtectedObjectView(target, context);
330
335
  return value;
331
336
  }
332
337
  /**
@@ -349,7 +354,17 @@ function getAnnotations(state) {
349
354
  if (state == null || typeof state !== "object") return void 0;
350
355
  const stateObj = state;
351
356
  const annotations = stateObj[annotationKey];
352
- if (annotations != null && typeof annotations === "object") return protectAnnotationValue(annotations);
357
+ if (annotations != null && typeof annotations === "object") {
358
+ if (isProtectedAnnotationView(annotations)) return annotations;
359
+ const cached = protectedAnnotationStateViews.get(stateObj);
360
+ if (cached?.raw === annotations) return cached.view;
361
+ const protectedView = protectAnnotationValue(annotations, createAnnotationProtectionContext());
362
+ protectedAnnotationStateViews.set(stateObj, {
363
+ raw: annotations,
364
+ view: protectedView
365
+ });
366
+ return protectedView;
367
+ }
353
368
  return void 0;
354
369
  }
355
370
  /**
@@ -450,7 +465,7 @@ function hasMeaningfulAnnotations(annotations) {
450
465
  */
451
466
  function injectAnnotations(state, annotations) {
452
467
  if (!hasMeaningfulAnnotations(annotations)) return state;
453
- const protectedAnnotations = protectAnnotationValue(annotations);
468
+ const protectedAnnotations = protectAnnotationValue(annotations, createAnnotationProtectionContext());
454
469
  if (state == null || typeof state !== "object") {
455
470
  const wrapper = {};
456
471
  Object.defineProperties(wrapper, {
@@ -45,12 +45,15 @@ const annotationWrapperKeys = new Set([
45
45
  ]);
46
46
  const injectedAnnotationWrappers = /* @__PURE__ */ new WeakSet();
47
47
  const protectedAnnotationTargets = /* @__PURE__ */ new WeakMap();
48
- const annotationProtectionCache = /* @__PURE__ */ new WeakMap();
48
+ const protectedAnnotationStateViews = /* @__PURE__ */ new WeakMap();
49
49
  function throwReadonlyAnnotationMutation() {
50
50
  throw new TypeError("Cannot mutate read-only annotation data.");
51
51
  }
52
- function registerProtectedAnnotationView(target, view) {
53
- annotationProtectionCache.set(target, view);
52
+ function createAnnotationProtectionContext() {
53
+ return { cache: /* @__PURE__ */ new WeakMap() };
54
+ }
55
+ function registerProtectedAnnotationView(context, target, view) {
56
+ context.cache.set(target, view);
54
57
  protectedAnnotationTargets.set(view, target);
55
58
  return view;
56
59
  }
@@ -68,8 +71,8 @@ function cacheProtectedMethod(cache, key, factory) {
68
71
  cache.set(key, created);
69
72
  return created;
70
73
  }
71
- function defineProtectedDataProperty(target, key, descriptor) {
72
- const value = protectAnnotationValue(descriptor.value);
74
+ function defineProtectedDataProperty(context, target, key, descriptor) {
75
+ const value = protectAnnotationValue(descriptor.value, context);
73
76
  Object.defineProperty(target, key, {
74
77
  configurable: descriptor.configurable,
75
78
  enumerable: descriptor.enumerable,
@@ -77,62 +80,64 @@ function defineProtectedDataProperty(target, key, descriptor) {
77
80
  set: () => throwReadonlyAnnotationMutation()
78
81
  });
79
82
  }
80
- function createProtectedObjectView(target) {
83
+ function createProtectedObjectView(target, context) {
81
84
  if (Array.isArray(target)) {
82
85
  const view$1 = new Array(target.length);
86
+ registerProtectedAnnotationView(context, target, view$1);
83
87
  for (const key of Reflect.ownKeys(target)) {
84
88
  if (key === "length") continue;
85
89
  const descriptor = Object.getOwnPropertyDescriptor(target, key);
86
90
  if (descriptor == null) continue;
87
91
  if ("value" in descriptor) {
88
- defineProtectedDataProperty(view$1, key, descriptor);
92
+ defineProtectedDataProperty(context, view$1, key, descriptor);
89
93
  continue;
90
94
  }
91
95
  Object.defineProperty(view$1, key, {
92
96
  configurable: descriptor.configurable,
93
97
  enumerable: descriptor.enumerable,
94
- get: descriptor.get == null ? void 0 : () => protectAnnotationValue(descriptor.get.call(target)),
98
+ get: descriptor.get == null ? void 0 : () => protectAnnotationValue(descriptor.get.call(target), context),
95
99
  set: () => throwReadonlyAnnotationMutation()
96
100
  });
97
101
  }
98
- return registerProtectedAnnotationView(target, Object.freeze(view$1));
102
+ return Object.freeze(view$1);
99
103
  }
100
104
  const view = Object.create(Object.getPrototypeOf(target));
105
+ registerProtectedAnnotationView(context, target, view);
101
106
  for (const key of Reflect.ownKeys(target)) {
102
107
  const descriptor = Object.getOwnPropertyDescriptor(target, key);
103
108
  if (descriptor == null) continue;
104
109
  if ("value" in descriptor) {
105
- defineProtectedDataProperty(view, key, descriptor);
110
+ defineProtectedDataProperty(context, view, key, descriptor);
106
111
  continue;
107
112
  }
108
113
  Object.defineProperty(view, key, {
109
114
  configurable: descriptor.configurable,
110
115
  enumerable: descriptor.enumerable,
111
- get: descriptor.get == null ? void 0 : () => protectAnnotationValue(descriptor.get.call(target)),
116
+ get: descriptor.get == null ? void 0 : () => protectAnnotationValue(descriptor.get.call(target), context),
112
117
  set: () => throwReadonlyAnnotationMutation()
113
118
  });
114
119
  }
115
- return registerProtectedAnnotationView(target, Object.freeze(view));
120
+ return Object.freeze(view);
116
121
  }
117
- function createProtectedMapView(target) {
122
+ function createProtectedMapView(target, context) {
118
123
  const methodCache = /* @__PURE__ */ new Map();
119
124
  const view = new Proxy(target, {
120
125
  get(target$1, key) {
121
126
  if (key === "size") return target$1.size;
122
127
  if (key === "set" || key === "delete" || key === "clear") return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
123
- if (key === "get") return cacheProtectedMethod(methodCache, key, () => (lookup) => protectAnnotationValue(target$1.get(unwrapProtectedAnnotationTarget(lookup))));
128
+ if (key === "get") return cacheProtectedMethod(methodCache, key, () => (lookup) => protectAnnotationValue(target$1.get(unwrapProtectedAnnotationTarget(lookup)), context));
124
129
  if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) => target$1.has(unwrapProtectedAnnotationTarget(lookup)));
125
130
  if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) => target$1.forEach((value$1, mapKey) => {
126
- callback.call(thisArg, protectAnnotationValue(value$1), protectAnnotationValue(mapKey), view);
131
+ callback.call(thisArg, protectAnnotationValue(value$1, context), protectAnnotationValue(mapKey, context), view);
127
132
  }));
128
133
  if (key === "keys") return cacheProtectedMethod(methodCache, key, () => function* () {
129
- for (const value$1 of target$1.keys()) yield protectAnnotationValue(value$1);
134
+ for (const value$1 of target$1.keys()) yield protectAnnotationValue(value$1, context);
130
135
  });
131
136
  if (key === "values") return cacheProtectedMethod(methodCache, key, () => function* () {
132
- for (const value$1 of target$1.values()) yield protectAnnotationValue(value$1);
137
+ for (const value$1 of target$1.values()) yield protectAnnotationValue(value$1, context);
133
138
  });
134
139
  if (key === "entries" || key === Symbol.iterator) return cacheProtectedMethod(methodCache, key, () => function* () {
135
- for (const [entryKey, entryValue] of target$1.entries()) yield [protectAnnotationValue(entryKey), protectAnnotationValue(entryValue)];
140
+ for (const [entryKey, entryValue] of target$1.entries()) yield [protectAnnotationValue(entryKey, context), protectAnnotationValue(entryValue, context)];
136
141
  });
137
142
  const value = Reflect.get(target$1, key, target$1);
138
143
  return typeof value === "function" ? value.bind(target$1) : value;
@@ -153,9 +158,9 @@ function createProtectedMapView(target) {
153
158
  throwReadonlyAnnotationMutation();
154
159
  }
155
160
  });
156
- return registerProtectedAnnotationView(target, view);
161
+ return registerProtectedAnnotationView(context, target, view);
157
162
  }
158
- function createProtectedSetView(target) {
163
+ function createProtectedSetView(target, context) {
159
164
  const methodCache = /* @__PURE__ */ new Map();
160
165
  const view = new Proxy(target, {
161
166
  get(target$1, key) {
@@ -163,15 +168,15 @@ function createProtectedSetView(target) {
163
168
  if (key === "add" || key === "delete" || key === "clear") return cacheProtectedMethod(methodCache, key, () => (..._args) => throwReadonlyAnnotationMutation());
164
169
  if (key === "has") return cacheProtectedMethod(methodCache, key, () => (lookup) => target$1.has(unwrapProtectedAnnotationTarget(lookup)));
165
170
  if (key === "forEach") return cacheProtectedMethod(methodCache, key, () => (callback, thisArg) => target$1.forEach((value$1) => {
166
- const protectedValue = protectAnnotationValue(value$1);
171
+ const protectedValue = protectAnnotationValue(value$1, context);
167
172
  callback.call(thisArg, protectedValue, protectedValue, view);
168
173
  }));
169
174
  if (key === "keys" || key === "values" || key === Symbol.iterator) return cacheProtectedMethod(methodCache, key, () => function* () {
170
- for (const value$1 of target$1.values()) yield protectAnnotationValue(value$1);
175
+ for (const value$1 of target$1.values()) yield protectAnnotationValue(value$1, context);
171
176
  });
172
177
  if (key === "entries") return cacheProtectedMethod(methodCache, key, () => function* () {
173
178
  for (const value$1 of target$1.values()) {
174
- const protectedValue = protectAnnotationValue(value$1);
179
+ const protectedValue = protectAnnotationValue(value$1, context);
175
180
  yield [protectedValue, protectedValue];
176
181
  }
177
182
  });
@@ -194,9 +199,9 @@ function createProtectedSetView(target) {
194
199
  throwReadonlyAnnotationMutation();
195
200
  }
196
201
  });
197
- return registerProtectedAnnotationView(target, view);
202
+ return registerProtectedAnnotationView(context, target, view);
198
203
  }
199
- function createProtectedDateView(target) {
204
+ function createProtectedDateView(target, context) {
200
205
  const methodCache = /* @__PURE__ */ new Map();
201
206
  const view = new Proxy(target, {
202
207
  get(target$1, key) {
@@ -220,9 +225,9 @@ function createProtectedDateView(target) {
220
225
  throwReadonlyAnnotationMutation();
221
226
  }
222
227
  });
223
- return registerProtectedAnnotationView(target, view);
228
+ return registerProtectedAnnotationView(context, target, view);
224
229
  }
225
- function createProtectedRegExpView(target) {
230
+ function createProtectedRegExpView(target, context) {
226
231
  const methodCache = /* @__PURE__ */ new Map();
227
232
  const cloned = new RegExp(target);
228
233
  cloned.lastIndex = target.lastIndex;
@@ -248,9 +253,9 @@ function createProtectedRegExpView(target) {
248
253
  throwReadonlyAnnotationMutation();
249
254
  }
250
255
  });
251
- return registerProtectedAnnotationView(target, view);
256
+ return registerProtectedAnnotationView(context, target, view);
252
257
  }
253
- function createProtectedURLSearchParamsView(target) {
258
+ function createProtectedURLSearchParamsView(target, context) {
254
259
  const methodCache = /* @__PURE__ */ new Map();
255
260
  const view = new Proxy(target, {
256
261
  get(target$1, key) {
@@ -284,12 +289,12 @@ function createProtectedURLSearchParamsView(target) {
284
289
  throwReadonlyAnnotationMutation();
285
290
  }
286
291
  });
287
- return registerProtectedAnnotationView(target, view);
292
+ return registerProtectedAnnotationView(context, target, view);
288
293
  }
289
- function createProtectedURLView(target) {
294
+ function createProtectedURLView(target, context) {
290
295
  const view = new Proxy(target, {
291
296
  get(target$1, key) {
292
- if (key === "searchParams") return protectAnnotationValue(target$1.searchParams);
297
+ if (key === "searchParams") return protectAnnotationValue(target$1.searchParams, context);
293
298
  const value = Reflect.get(target$1, key, target$1);
294
299
  return typeof value === "function" ? value.bind(target$1) : value;
295
300
  },
@@ -309,23 +314,23 @@ function createProtectedURLView(target) {
309
314
  throwReadonlyAnnotationMutation();
310
315
  }
311
316
  });
312
- return registerProtectedAnnotationView(target, view);
317
+ return registerProtectedAnnotationView(context, target, view);
313
318
  }
314
- function protectAnnotationValue(value) {
319
+ function protectAnnotationValue(value, context) {
315
320
  if (value == null || typeof value !== "object") return value;
316
321
  const target = value;
317
322
  if (isProtectedAnnotationView(value)) return value;
318
- const cached = annotationProtectionCache.get(target);
323
+ const cached = context.cache.get(target);
319
324
  if (cached !== void 0) return cached;
320
- if (target instanceof Map) return createProtectedMapView(target);
321
- if (target instanceof Set) return createProtectedSetView(target);
322
- if (target instanceof Date) return createProtectedDateView(target);
323
- if (target instanceof RegExp) return createProtectedRegExpView(target);
324
- if (typeof URLSearchParams === "function" && target instanceof URLSearchParams) return createProtectedURLSearchParamsView(target);
325
- if (typeof URL === "function" && target instanceof URL) return createProtectedURLView(target);
326
- if (Array.isArray(target)) return createProtectedObjectView(target);
325
+ if (target instanceof Map) return createProtectedMapView(target, context);
326
+ if (target instanceof Set) return createProtectedSetView(target, context);
327
+ if (target instanceof Date) return createProtectedDateView(target, context);
328
+ if (target instanceof RegExp) return createProtectedRegExpView(target, context);
329
+ if (typeof URLSearchParams === "function" && target instanceof URLSearchParams) return createProtectedURLSearchParamsView(target, context);
330
+ if (typeof URL === "function" && target instanceof URL) return createProtectedURLView(target, context);
331
+ if (Array.isArray(target)) return createProtectedObjectView(target, context);
327
332
  const proto = Object.getPrototypeOf(target);
328
- if (proto === Object.prototype || proto === null) return createProtectedObjectView(target);
333
+ if (proto === Object.prototype || proto === null) return createProtectedObjectView(target, context);
329
334
  return value;
330
335
  }
331
336
  /**
@@ -348,7 +353,17 @@ function getAnnotations(state) {
348
353
  if (state == null || typeof state !== "object") return void 0;
349
354
  const stateObj = state;
350
355
  const annotations = stateObj[annotationKey];
351
- if (annotations != null && typeof annotations === "object") return protectAnnotationValue(annotations);
356
+ if (annotations != null && typeof annotations === "object") {
357
+ if (isProtectedAnnotationView(annotations)) return annotations;
358
+ const cached = protectedAnnotationStateViews.get(stateObj);
359
+ if (cached?.raw === annotations) return cached.view;
360
+ const protectedView = protectAnnotationValue(annotations, createAnnotationProtectionContext());
361
+ protectedAnnotationStateViews.set(stateObj, {
362
+ raw: annotations,
363
+ view: protectedView
364
+ });
365
+ return protectedView;
366
+ }
352
367
  return void 0;
353
368
  }
354
369
  /**
@@ -449,7 +464,7 @@ function hasMeaningfulAnnotations(annotations) {
449
464
  */
450
465
  function injectAnnotations(state, annotations) {
451
466
  if (!hasMeaningfulAnnotations(annotations)) return state;
452
- const protectedAnnotations = protectAnnotationValue(annotations);
467
+ const protectedAnnotations = protectAnnotationValue(annotations, createAnnotationProtectionContext());
453
468
  if (state == null || typeof state !== "object") {
454
469
  const wrapper = {};
455
470
  Object.defineProperties(wrapper, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.1848+b4e193fa",
3
+ "version": "1.0.0-dev.1850+6944c47b",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",