Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | 14x | import { ArrayCtor, ArrayProtoShift } from './Array'; import { getBrand } from './basic'; import { TO_STRING_BRAND_ARRAY, TO_STRING_BRAND_BIG_INT, TO_STRING_BRAND_BOOLEAN, TO_STRING_BRAND_MAP, TO_STRING_BRAND_NUMBER, TO_STRING_BRAND_OBJECT, TO_STRING_BRAND_REG_EXP, TO_STRING_BRAND_SET, TO_STRING_BRAND_STRING, } from './constants'; import { JSONParse } from './JSON'; import { MapCtor, MapProtoEntries, MapProtoSet, toSafeMap } from './Map'; import { getNearMembraneProxySerializedValue, isNearMembraneProxy } from './NearMembrane'; import { ObjectCtor, ObjectKeys, ObjectProto } from './Object'; import { ReflectApply, ReflectGetPrototypeOf } from './Reflect'; import { RegExpCtor } from './RegExp'; import { SetCtor, SetProtoAdd, SetProtoValues } from './Set'; const SEEN_OBJECTS = toSafeMap(new MapCtor<object, object>()); function cloneBoxedPrimitive(object: object): object { return ObjectCtor(getNearMembraneProxySerializedValue(object)); } function cloneMap(map: Map<any, any>, queue: any[]): Map<any, any> { // Section 2.7.3 StructuredSerializeInternal: // https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal // Step 26.1.1: Let copiedList be a new empty List. const clone = new MapCtor(); // Step 26.1.2: For each Record { [[Key]], [[Value]] } entry of value.[[MapData]]... const entriesIterable = ReflectApply(MapProtoEntries, map, []); // Step 26.1.3 For each Record { [[Key]], [[Value]] } entry of copiedList: let { length: queueOffset } = queue; // eslint-disable-next-line no-constant-condition while (true) { const { done, value: subKeyValuePair } = entriesIterable.next(); if (done) { break; } const { 0: subKey, 1: subValue } = subKeyValuePair; let subCloneKey: any; // Step 26.1.3.1: Let serializedKey be ? StructuredSerializeInternal(entry.[[Key]], forStorage, memory). queue[queueOffset++] = [ (subClone: any) => { subCloneKey = subClone; }, subKey, ]; // Step 26.1.3.2: Let serializedValue be ? StructuredSerializeInternal(entry.[[Value]], forStorage, memory). queue[queueOffset++] = [ (subCloneValue: any) => { ReflectApply(MapProtoSet, clone, [subCloneKey, subCloneValue]); }, subValue, ]; } return clone; } function cloneRegExp(regexp: RegExp): RegExp { const { flags, source } = JSONParse(getNearMembraneProxySerializedValue(regexp) as string); return new RegExpCtor(source, flags); } function cloneSet(set: Set<any>, queue: any[]): Set<any> { // Section 2.7.3 StructuredSerializeInternal: // https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal // Step 26.2.1: Let copiedList be a new empty List. const clone = new SetCtor(); // Step 26.2.2: For each entry of value.[[SetData]]... const valuesIterable = ReflectApply(SetProtoValues, set, []); // Step 26.2.3: For each entry of copiedList: let { length: queueOffset } = queue; // eslint-disable-next-line no-constant-condition while (true) { const { done, value: subValue } = valuesIterable.next(); if (done) { break; } // Step 26.2.3.1: Let serializedEntry be ? StructuredSerializeInternal(entry, forStorage, memory). queue[queueOffset++] = [ (subCloneValue: any) => { ReflectApply(SetProtoAdd, clone, [subCloneValue]); }, subValue, ]; } return clone; } function enqueue(queue: any[], originalValue: object, cloneValue: object) { // Section 2.7.3 StructuredSerializeInternal: // https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal // Step 26.4: Otherwise, for each key in ! EnumerableOwnPropertyNames(value, key)... // Note: Object.keys() performs EnumerableOwnPropertyNames() internally as // defined in ECMA262: // https://tc39.es/ecma262/#sec-object.keys const keys = ObjectKeys(originalValue); let { length: queueOffset } = queue; for (let i = 0, { length } = keys; i < length; i += 1) { // Step 26.4.1.1: Let inputValue be ? value.[[Get]](key, value). // The [[Get]] operation is defined in ECMA262 for ordinary objects, // argument objects, integer-indexed exotic objects, module namespace // objects, and proxy objects. // https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver const key = keys[i]; const subValue = (originalValue as any)[key]; queue[queueOffset++] = [ (subCloneValue: object) => { // Step 26.4.1.3: Property descriptor attributes are not // preserved during deserialization because only keys and // values are captured in serialized.[[Properties]]. (cloneValue as any)[key] = subCloneValue; }, subValue, ]; } } // This function is the unguarded internal variant of `partialStructuredClone()`. // Any error thrown that is captured by `partialStructuredClone()` is treated as // a `DataCloneError`. This function clones blue membrane proxied arrays, plain // objects, maps, regexps, sets, and boxed primitives. The following non-membrane // proxied objects are set by reference instead of cloning: // ArrayBuffer // BigInt64Array // BigUint64Array // Blob // DataView // Date // DOMException // DOMMatrix // DOMMatrixReadOnly // DOMPoint // DOMPointReadOnly // DOMQuad // DOMRect // DOMRectReadOnly // Error // EvalError // File // FileList // Float32Array // Float64Array // ImageBitMap // ImageData // Int8Array // Int16Array // Int32Array // RangeError // ReferenceError // SyntaxError // TypeError // Uint8Array // Uint8ClampedArray // Uint16Array // Uint32Array // URIError // // Note: // This function performs brand checks using `Object.prototype.toString`. The // results can be faked with `Symbol.toStringTag` property values and are a poor // substitute for native internal slot checks. However, for our purposes they // are perfectly fine and avoid having to repeatedly walk the prototype of proxied // values. Cloned values should be passed to native methods, like `postMessage()`, // which perform their own validation with internal slot checks. function partialStructuredCloneInternal(value: any): any { // Using a queue instead of recursive function calls avoids call stack limits // and enables cloning more complex and deeply nested objects. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Too_much_recursion let result: any; const queue = [ [ (subClone: any) => { result = subClone; }, value, ], ]; // eslint-disable-next-line no-labels queueLoop: while (queue.length) { // Section 2.7.3 StructuredSerializeInternal: // https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal // prettier-ignore const { 0: setter, 1: originalValue, } = ReflectApply(ArrayProtoShift, queue, []); // Step 4: If Type(value) is Undefined, Null, Boolean, Number, BigInt, or String if ( originalValue === null || originalValue === undefined || typeof originalValue === 'boolean' || typeof originalValue === 'number' || typeof originalValue === 'string' || typeof originalValue === 'bigint' ) { setter(originalValue); // eslint-disable-next-line no-continue, no-extra-label, no-labels continue queueLoop; } // Step 5: If Type(value) is Symbol, then throw a 'DataCloneError' DOMException. if (typeof originalValue === 'symbol') { // Stop cloning and set the original value and defer throwing to // native methods. setter(originalValue); // eslint-disable-next-line no-extra-label, no-labels break queueLoop; } // To support circular references check if the original value has been // seen. If it has then use the clone associated with its record instead // of creating a new clone. let cloneValue = SEEN_OBJECTS.get(originalValue); if (cloneValue) { setter(cloneValue); // eslint-disable-next-line no-continue, no-extra-label, no-labels continue queueLoop; } // Perform a brand check on originalValue. const brand = getBrand(originalValue); // eslint-disable-next-line default-case switch (brand) { // Step 19: Otherwise, if value is a platform object... case TO_STRING_BRAND_OBJECT: { const proto = ReflectGetPrototypeOf(originalValue); if ( proto === ObjectProto || proto === null || // Possible `Object.prototype` from another document. ReflectGetPrototypeOf(proto) === null ) { cloneValue = {}; // Step 19.4: Set deep to true. enqueue(queue, originalValue, cloneValue); } break; } // Step 18: Otherwise, if value is an Array exotic object... case TO_STRING_BRAND_ARRAY: // Step 18.1 Let valueLenDescriptor be ? OrdinaryGetOwnProperty(value, 'length'). // Note: Rather than perform the more complex OrdinaryGetOwnProperty() // operation for 'length' because it is a non-configurable property // we can access it with the simpler [[Get]]() operation defined // in ECMA262. // https://tc39.es/ecma262/#sec-integer-indexed-exotic-objects-get-p-receiver cloneValue = ArrayCtor(originalValue.length); // Step 18.4: Set deep to true. enqueue(queue, originalValue, cloneValue); break; // Step 15: Otherwise, if value has [[MapData]] internal slot... // Step 15.2: Set deep to true. case TO_STRING_BRAND_MAP: cloneValue = cloneMap(originalValue, queue); break; // Step 16: Otherwise, if value has [[SetData]] internal slot... // Step 16.2: Set deep to true. case TO_STRING_BRAND_SET: cloneValue = cloneSet(originalValue, queue); break; } if (cloneValue === undefined) { // istanbul ignore else Iif (!isNearMembraneProxy(originalValue)) { // Skip cloning non-membrane proxied objects. SEEN_OBJECTS.set(originalValue, originalValue); setter(originalValue); // eslint-disable-next-line no-extra-label, no-labels continue queueLoop; } // Cases ordered by a guestimate on frequency of encounter. // eslint-disable-next-line default-case switch (brand) { // Step 12: Otherwise, if value has a [[RegExpMatcher]] internal slot... case TO_STRING_BRAND_REG_EXP: cloneValue = cloneRegExp(originalValue); break; // Step 7: If value has a [[BooleanData]] internal slot... case TO_STRING_BRAND_BOOLEAN: // Step 8: Otherwise, if value has a [[NumberData]] internal slot... // eslint-disable-next-line no-fallthrough case TO_STRING_BRAND_NUMBER: // Step 9: Otherwise, if value has a [[BigIntData]] internal slot... // eslint-disable-next-line no-fallthrough case TO_STRING_BRAND_BIG_INT: // Step 10: Otherwise, if value has a [[StringData]] internal slot... // eslint-disable-next-line no-fallthrough case TO_STRING_BRAND_STRING: cloneValue = cloneBoxedPrimitive(originalValue); break; } } // Step 21: Otherwise, if IsCallable(value) is true, then throw a 'DataCloneError' // Step 20: Otherwise, if value is a platform object, then throw a 'DataCloneError' if (cloneValue === undefined) { // Stop cloning and set the original value and defer throwing to // native methods. setter(originalValue); // eslint-disable-next-line no-extra-label, no-labels break queueLoop; } SEEN_OBJECTS.set(originalValue, cloneValue); setter(cloneValue); } return result; } export function partialStructuredClone(value: any): any { let result = value; try { result = partialStructuredCloneInternal(value); // eslint-disable-next-line no-empty } catch {} SEEN_OBJECTS.clear(); return result; } |