| [9af201e] | 1 | // Copyright (C) 2011-2012 Software Languages Lab, Vrije Universiteit Brussel
|
|---|
| 2 | // This code is dual-licensed under both the Apache License and the MPL
|
|---|
| 3 |
|
|---|
| 4 | // Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 5 | // you may not use this file except in compliance with the License.
|
|---|
| 6 | // You may obtain a copy of the License at
|
|---|
| 7 | //
|
|---|
| 8 | // http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 9 | //
|
|---|
| 10 | // Unless required by applicable law or agreed to in writing, software
|
|---|
| 11 | // distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 13 | // See the License for the specific language governing permissions and
|
|---|
| 14 | // limitations under the License.
|
|---|
| 15 |
|
|---|
| 16 | /* Version: MPL 1.1
|
|---|
| 17 | *
|
|---|
| 18 | * The contents of this file are subject to the Mozilla Public License Version
|
|---|
| 19 | * 1.1 (the "License"); you may not use this file except in compliance with
|
|---|
| 20 | * the License. You may obtain a copy of the License at
|
|---|
| 21 | * http://www.mozilla.org/MPL/
|
|---|
| 22 | *
|
|---|
| 23 | * Software distributed under the License is distributed on an "AS IS" basis,
|
|---|
| 24 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|---|
| 25 | * for the specific language governing rights and limitations under the
|
|---|
| 26 | * License.
|
|---|
| 27 | *
|
|---|
| 28 | * The Original Code is a shim for the ES-Harmony reflection module
|
|---|
| 29 | *
|
|---|
| 30 | * The Initial Developer of the Original Code is
|
|---|
| 31 | * Tom Van Cutsem, Vrije Universiteit Brussel.
|
|---|
| 32 | * Portions created by the Initial Developer are Copyright (C) 2011-2012
|
|---|
| 33 | * the Initial Developer. All Rights Reserved.
|
|---|
| 34 | *
|
|---|
| 35 | * Contributor(s):
|
|---|
| 36 | *
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | // ----------------------------------------------------------------------------
|
|---|
| 40 |
|
|---|
| 41 | // This file is a polyfill for the upcoming ECMAScript Reflect API,
|
|---|
| 42 | // including support for Proxies. See the draft specification at:
|
|---|
| 43 | // http://wiki.ecmascript.org/doku.php?id=harmony:reflect_api
|
|---|
| 44 | // http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxies
|
|---|
| 45 |
|
|---|
| 46 | // For an implementation of the Handler API, see handlers.js, which implements:
|
|---|
| 47 | // http://wiki.ecmascript.org/doku.php?id=harmony:virtual_object_api
|
|---|
| 48 |
|
|---|
| 49 | // This implementation supersedes the earlier polyfill at:
|
|---|
| 50 | // code.google.com/p/es-lab/source/browse/trunk/src/proxies/DirectProxies.js
|
|---|
| 51 |
|
|---|
| 52 | // This code was tested on tracemonkey / Firefox 12
|
|---|
| 53 | // (and should run fine on older Firefox versions starting with FF4)
|
|---|
| 54 | // The code also works correctly on
|
|---|
| 55 | // v8 --harmony_proxies --harmony_weakmaps (v3.6.5.1)
|
|---|
| 56 |
|
|---|
| 57 | // Language Dependencies:
|
|---|
| 58 | // - ECMAScript 5/strict
|
|---|
| 59 | // - "old" (i.e. non-direct) Harmony Proxies
|
|---|
| 60 | // - Harmony WeakMaps
|
|---|
| 61 | // Patches:
|
|---|
| 62 | // - Object.{freeze,seal,preventExtensions}
|
|---|
| 63 | // - Object.{isFrozen,isSealed,isExtensible}
|
|---|
| 64 | // - Object.getPrototypeOf
|
|---|
| 65 | // - Object.keys
|
|---|
| 66 | // - Object.prototype.valueOf
|
|---|
| 67 | // - Object.prototype.isPrototypeOf
|
|---|
| 68 | // - Object.prototype.toString
|
|---|
| 69 | // - Object.prototype.hasOwnProperty
|
|---|
| 70 | // - Object.getOwnPropertyDescriptor
|
|---|
| 71 | // - Object.defineProperty
|
|---|
| 72 | // - Object.defineProperties
|
|---|
| 73 | // - Object.getOwnPropertyNames
|
|---|
| 74 | // - Object.getOwnPropertySymbols
|
|---|
| 75 | // - Object.getPrototypeOf
|
|---|
| 76 | // - Object.setPrototypeOf
|
|---|
| 77 | // - Object.assign
|
|---|
| 78 | // - Function.prototype.toString
|
|---|
| 79 | // - Date.prototype.toString
|
|---|
| 80 | // - Array.isArray
|
|---|
| 81 | // - Array.prototype.concat
|
|---|
| 82 | // - Proxy
|
|---|
| 83 | // Adds new globals:
|
|---|
| 84 | // - Reflect
|
|---|
| 85 |
|
|---|
| 86 | // Direct proxies can be created via Proxy(target, handler)
|
|---|
| 87 |
|
|---|
| 88 | // ----------------------------------------------------------------------------
|
|---|
| 89 |
|
|---|
| 90 | (function(global){ // function-as-module pattern
|
|---|
| 91 | "use strict";
|
|---|
| 92 |
|
|---|
| 93 | // === Direct Proxies: Invariant Enforcement ===
|
|---|
| 94 |
|
|---|
| 95 | // Direct proxies build on non-direct proxies by automatically wrapping
|
|---|
| 96 | // all user-defined proxy handlers in a Validator handler that checks and
|
|---|
| 97 | // enforces ES5 invariants.
|
|---|
| 98 |
|
|---|
| 99 | // A direct proxy is a proxy for an existing object called the target object.
|
|---|
| 100 |
|
|---|
| 101 | // A Validator handler is a wrapper for a target proxy handler H.
|
|---|
| 102 | // The Validator forwards all operations to H, but additionally
|
|---|
| 103 | // performs a number of integrity checks on the results of some traps,
|
|---|
| 104 | // to make sure H does not violate the ES5 invariants w.r.t. non-configurable
|
|---|
| 105 | // properties and non-extensible, sealed or frozen objects.
|
|---|
| 106 |
|
|---|
| 107 | // For each property that H exposes as own, non-configurable
|
|---|
| 108 | // (e.g. by returning a descriptor from a call to getOwnPropertyDescriptor)
|
|---|
| 109 | // the Validator handler defines those properties on the target object.
|
|---|
| 110 | // When the proxy becomes non-extensible, also configurable own properties
|
|---|
| 111 | // are checked against the target.
|
|---|
| 112 | // We will call properties that are defined on the target object
|
|---|
| 113 | // "fixed properties".
|
|---|
| 114 |
|
|---|
| 115 | // We will name fixed non-configurable properties "sealed properties".
|
|---|
| 116 | // We will name fixed non-configurable non-writable properties "frozen
|
|---|
| 117 | // properties".
|
|---|
| 118 |
|
|---|
| 119 | // The Validator handler upholds the following invariants w.r.t. non-configurability:
|
|---|
| 120 | // - getOwnPropertyDescriptor cannot report sealed properties as non-existent
|
|---|
| 121 | // - getOwnPropertyDescriptor cannot report incompatible changes to the
|
|---|
| 122 | // attributes of a sealed property (e.g. reporting a non-configurable
|
|---|
| 123 | // property as configurable, or reporting a non-configurable, non-writable
|
|---|
| 124 | // property as writable)
|
|---|
| 125 | // - getPropertyDescriptor cannot report sealed properties as non-existent
|
|---|
| 126 | // - getPropertyDescriptor cannot report incompatible changes to the
|
|---|
| 127 | // attributes of a sealed property. It _can_ report incompatible changes
|
|---|
| 128 | // to the attributes of non-own, inherited properties.
|
|---|
| 129 | // - defineProperty cannot make incompatible changes to the attributes of
|
|---|
| 130 | // sealed properties
|
|---|
| 131 | // - deleteProperty cannot report a successful deletion of a sealed property
|
|---|
| 132 | // - hasOwn cannot report a sealed property as non-existent
|
|---|
| 133 | // - has cannot report a sealed property as non-existent
|
|---|
| 134 | // - get cannot report inconsistent values for frozen data
|
|---|
| 135 | // properties, and must report undefined for sealed accessors with an
|
|---|
| 136 | // undefined getter
|
|---|
| 137 | // - set cannot report a successful assignment for frozen data
|
|---|
| 138 | // properties or sealed accessors with an undefined setter.
|
|---|
| 139 | // - get{Own}PropertyNames lists all sealed properties of the target.
|
|---|
| 140 | // - keys lists all enumerable sealed properties of the target.
|
|---|
| 141 | // - enumerate lists all enumerable sealed properties of the target.
|
|---|
| 142 | // - if a property of a non-extensible proxy is reported as non-existent,
|
|---|
| 143 | // then it must forever be reported as non-existent. This applies to
|
|---|
| 144 | // own and inherited properties and is enforced in the
|
|---|
| 145 | // deleteProperty, get{Own}PropertyDescriptor, has{Own},
|
|---|
| 146 | // get{Own}PropertyNames, keys and enumerate traps
|
|---|
| 147 |
|
|---|
| 148 | // Violation of any of these invariants by H will result in TypeError being
|
|---|
| 149 | // thrown.
|
|---|
| 150 |
|
|---|
| 151 | // Additionally, once Object.preventExtensions, Object.seal or Object.freeze
|
|---|
| 152 | // is invoked on the proxy, the set of own property names for the proxy is
|
|---|
| 153 | // fixed. Any property name that is not fixed is called a 'new' property.
|
|---|
| 154 |
|
|---|
| 155 | // The Validator upholds the following invariants regarding extensibility:
|
|---|
| 156 | // - getOwnPropertyDescriptor cannot report new properties as existent
|
|---|
| 157 | // (it must report them as non-existent by returning undefined)
|
|---|
| 158 | // - defineProperty cannot successfully add a new property (it must reject)
|
|---|
| 159 | // - getOwnPropertyNames cannot list new properties
|
|---|
| 160 | // - hasOwn cannot report true for new properties (it must report false)
|
|---|
| 161 | // - keys cannot list new properties
|
|---|
| 162 |
|
|---|
| 163 | // Invariants currently not enforced:
|
|---|
| 164 | // - getOwnPropertyNames lists only own property names
|
|---|
| 165 | // - keys lists only enumerable own property names
|
|---|
| 166 | // Both traps may list more property names than are actually defined on the
|
|---|
| 167 | // target.
|
|---|
| 168 |
|
|---|
| 169 | // Invariants with regard to inheritance are currently not enforced.
|
|---|
| 170 | // - a non-configurable potentially inherited property on a proxy with
|
|---|
| 171 | // non-mutable ancestry cannot be reported as non-existent
|
|---|
| 172 | // (An object with non-mutable ancestry is a non-extensible object whose
|
|---|
| 173 | // [[Prototype]] is either null or an object with non-mutable ancestry.)
|
|---|
| 174 |
|
|---|
| 175 | // Changes in Handler API compared to previous harmony:proxies, see:
|
|---|
| 176 | // http://wiki.ecmascript.org/doku.php?id=strawman:direct_proxies
|
|---|
| 177 | // http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxies
|
|---|
| 178 |
|
|---|
| 179 | // ----------------------------------------------------------------------------
|
|---|
| 180 |
|
|---|
| 181 | // ---- WeakMap polyfill ----
|
|---|
| 182 |
|
|---|
| 183 | // TODO: find a proper WeakMap polyfill
|
|---|
| 184 |
|
|---|
| 185 | // define an empty WeakMap so that at least the Reflect module code
|
|---|
| 186 | // will work in the absence of WeakMaps. Proxy emulation depends on
|
|---|
| 187 | // actual WeakMaps, so will not work with this little shim.
|
|---|
| 188 | if (typeof WeakMap === "undefined") {
|
|---|
| 189 | global.WeakMap = function(){};
|
|---|
| 190 | global.WeakMap.prototype = {
|
|---|
| 191 | get: function(k) { return undefined; },
|
|---|
| 192 | set: function(k,v) { throw new Error("WeakMap not supported"); }
|
|---|
| 193 | };
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | // ---- Normalization functions for property descriptors ----
|
|---|
| 197 |
|
|---|
| 198 | function isStandardAttribute(name) {
|
|---|
| 199 | return /^(get|set|value|writable|enumerable|configurable)$/.test(name);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | // Adapted from ES5 section 8.10.5
|
|---|
| 203 | function toPropertyDescriptor(obj) {
|
|---|
| 204 | if (Object(obj) !== obj) {
|
|---|
| 205 | throw new TypeError("property descriptor should be an Object, given: "+
|
|---|
| 206 | obj);
|
|---|
| 207 | }
|
|---|
| 208 | var desc = {};
|
|---|
| 209 | if ('enumerable' in obj) { desc.enumerable = !!obj.enumerable; }
|
|---|
| 210 | if ('configurable' in obj) { desc.configurable = !!obj.configurable; }
|
|---|
| 211 | if ('value' in obj) { desc.value = obj.value; }
|
|---|
| 212 | if ('writable' in obj) { desc.writable = !!obj.writable; }
|
|---|
| 213 | if ('get' in obj) {
|
|---|
| 214 | var getter = obj.get;
|
|---|
| 215 | if (getter !== undefined && typeof getter !== "function") {
|
|---|
| 216 | throw new TypeError("property descriptor 'get' attribute must be "+
|
|---|
| 217 | "callable or undefined, given: "+getter);
|
|---|
| 218 | }
|
|---|
| 219 | desc.get = getter;
|
|---|
| 220 | }
|
|---|
| 221 | if ('set' in obj) {
|
|---|
| 222 | var setter = obj.set;
|
|---|
| 223 | if (setter !== undefined && typeof setter !== "function") {
|
|---|
| 224 | throw new TypeError("property descriptor 'set' attribute must be "+
|
|---|
| 225 | "callable or undefined, given: "+setter);
|
|---|
| 226 | }
|
|---|
| 227 | desc.set = setter;
|
|---|
| 228 | }
|
|---|
| 229 | if ('get' in desc || 'set' in desc) {
|
|---|
| 230 | if ('value' in desc || 'writable' in desc) {
|
|---|
| 231 | throw new TypeError("property descriptor cannot be both a data and an "+
|
|---|
| 232 | "accessor descriptor: "+obj);
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 | return desc;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | function isAccessorDescriptor(desc) {
|
|---|
| 239 | if (desc === undefined) return false;
|
|---|
| 240 | return ('get' in desc || 'set' in desc);
|
|---|
| 241 | }
|
|---|
| 242 | function isDataDescriptor(desc) {
|
|---|
| 243 | if (desc === undefined) return false;
|
|---|
| 244 | return ('value' in desc || 'writable' in desc);
|
|---|
| 245 | }
|
|---|
| 246 | function isGenericDescriptor(desc) {
|
|---|
| 247 | if (desc === undefined) return false;
|
|---|
| 248 | return !isAccessorDescriptor(desc) && !isDataDescriptor(desc);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | function toCompletePropertyDescriptor(desc) {
|
|---|
| 252 | var internalDesc = toPropertyDescriptor(desc);
|
|---|
| 253 | if (isGenericDescriptor(internalDesc) || isDataDescriptor(internalDesc)) {
|
|---|
| 254 | if (!('value' in internalDesc)) { internalDesc.value = undefined; }
|
|---|
| 255 | if (!('writable' in internalDesc)) { internalDesc.writable = false; }
|
|---|
| 256 | } else {
|
|---|
| 257 | if (!('get' in internalDesc)) { internalDesc.get = undefined; }
|
|---|
| 258 | if (!('set' in internalDesc)) { internalDesc.set = undefined; }
|
|---|
| 259 | }
|
|---|
| 260 | if (!('enumerable' in internalDesc)) { internalDesc.enumerable = false; }
|
|---|
| 261 | if (!('configurable' in internalDesc)) { internalDesc.configurable = false; }
|
|---|
| 262 | return internalDesc;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | function isEmptyDescriptor(desc) {
|
|---|
| 266 | return !('get' in desc) &&
|
|---|
| 267 | !('set' in desc) &&
|
|---|
| 268 | !('value' in desc) &&
|
|---|
| 269 | !('writable' in desc) &&
|
|---|
| 270 | !('enumerable' in desc) &&
|
|---|
| 271 | !('configurable' in desc);
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | function isEquivalentDescriptor(desc1, desc2) {
|
|---|
| 275 | return sameValue(desc1.get, desc2.get) &&
|
|---|
| 276 | sameValue(desc1.set, desc2.set) &&
|
|---|
| 277 | sameValue(desc1.value, desc2.value) &&
|
|---|
| 278 | sameValue(desc1.writable, desc2.writable) &&
|
|---|
| 279 | sameValue(desc1.enumerable, desc2.enumerable) &&
|
|---|
| 280 | sameValue(desc1.configurable, desc2.configurable);
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | // copied from http://wiki.ecmascript.org/doku.php?id=harmony:egal
|
|---|
| 284 | function sameValue(x, y) {
|
|---|
| 285 | if (x === y) {
|
|---|
| 286 | // 0 === -0, but they are not identical
|
|---|
| 287 | return x !== 0 || 1 / x === 1 / y;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | // NaN !== NaN, but they are identical.
|
|---|
| 291 | // NaNs are the only non-reflexive value, i.e., if x !== x,
|
|---|
| 292 | // then x is a NaN.
|
|---|
| 293 | // isNaN is broken: it converts its argument to number, so
|
|---|
| 294 | // isNaN("foo") => true
|
|---|
| 295 | return x !== x && y !== y;
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | /**
|
|---|
| 299 | * Returns a fresh property descriptor that is guaranteed
|
|---|
| 300 | * to be complete (i.e. contain all the standard attributes).
|
|---|
| 301 | * Additionally, any non-standard enumerable properties of
|
|---|
| 302 | * attributes are copied over to the fresh descriptor.
|
|---|
| 303 | *
|
|---|
| 304 | * If attributes is undefined, returns undefined.
|
|---|
| 305 | *
|
|---|
| 306 | * See also: http://wiki.ecmascript.org/doku.php?id=harmony:proxies_semantics
|
|---|
| 307 | */
|
|---|
| 308 | function normalizeAndCompletePropertyDescriptor(attributes) {
|
|---|
| 309 | if (attributes === undefined) { return undefined; }
|
|---|
| 310 | var desc = toCompletePropertyDescriptor(attributes);
|
|---|
| 311 | // Note: no need to call FromPropertyDescriptor(desc), as we represent
|
|---|
| 312 | // "internal" property descriptors as proper Objects from the start
|
|---|
| 313 | for (var name in attributes) {
|
|---|
| 314 | if (!isStandardAttribute(name)) {
|
|---|
| 315 | Object.defineProperty(desc, name,
|
|---|
| 316 | { value: attributes[name],
|
|---|
| 317 | writable: true,
|
|---|
| 318 | enumerable: true,
|
|---|
| 319 | configurable: true });
|
|---|
| 320 | }
|
|---|
| 321 | }
|
|---|
| 322 | return desc;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | /**
|
|---|
| 326 | * Returns a fresh property descriptor whose standard
|
|---|
| 327 | * attributes are guaranteed to be data properties of the right type.
|
|---|
| 328 | * Additionally, any non-standard enumerable properties of
|
|---|
| 329 | * attributes are copied over to the fresh descriptor.
|
|---|
| 330 | *
|
|---|
| 331 | * If attributes is undefined, will throw a TypeError.
|
|---|
| 332 | *
|
|---|
| 333 | * See also: http://wiki.ecmascript.org/doku.php?id=harmony:proxies_semantics
|
|---|
| 334 | */
|
|---|
| 335 | function normalizePropertyDescriptor(attributes) {
|
|---|
| 336 | var desc = toPropertyDescriptor(attributes);
|
|---|
| 337 | // Note: no need to call FromGenericPropertyDescriptor(desc), as we represent
|
|---|
| 338 | // "internal" property descriptors as proper Objects from the start
|
|---|
| 339 | for (var name in attributes) {
|
|---|
| 340 | if (!isStandardAttribute(name)) {
|
|---|
| 341 | Object.defineProperty(desc, name,
|
|---|
| 342 | { value: attributes[name],
|
|---|
| 343 | writable: true,
|
|---|
| 344 | enumerable: true,
|
|---|
| 345 | configurable: true });
|
|---|
| 346 | }
|
|---|
| 347 | }
|
|---|
| 348 | return desc;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | // store a reference to the real ES5 primitives before patching them later
|
|---|
| 352 | var prim_preventExtensions = Object.preventExtensions,
|
|---|
| 353 | prim_seal = Object.seal,
|
|---|
| 354 | prim_freeze = Object.freeze,
|
|---|
| 355 | prim_isExtensible = Object.isExtensible,
|
|---|
| 356 | prim_isSealed = Object.isSealed,
|
|---|
| 357 | prim_isFrozen = Object.isFrozen,
|
|---|
| 358 | prim_getPrototypeOf = Object.getPrototypeOf,
|
|---|
| 359 | prim_getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor,
|
|---|
| 360 | prim_defineProperty = Object.defineProperty,
|
|---|
| 361 | prim_defineProperties = Object.defineProperties,
|
|---|
| 362 | prim_keys = Object.keys,
|
|---|
| 363 | prim_getOwnPropertyNames = Object.getOwnPropertyNames,
|
|---|
| 364 | prim_getOwnPropertySymbols = Object.getOwnPropertySymbols,
|
|---|
| 365 | prim_assign = Object.assign,
|
|---|
| 366 | prim_isArray = Array.isArray,
|
|---|
| 367 | prim_concat = Array.prototype.concat,
|
|---|
| 368 | prim_isPrototypeOf = Object.prototype.isPrototypeOf,
|
|---|
| 369 | prim_hasOwnProperty = Object.prototype.hasOwnProperty;
|
|---|
| 370 |
|
|---|
| 371 | // these will point to the patched versions of the respective methods on
|
|---|
| 372 | // Object. They are used within this module as the "intrinsic" bindings
|
|---|
| 373 | // of these methods (i.e. the "original" bindings as defined in the spec)
|
|---|
| 374 | var Object_isFrozen,
|
|---|
| 375 | Object_isSealed,
|
|---|
| 376 | Object_isExtensible,
|
|---|
| 377 | Object_getPrototypeOf,
|
|---|
| 378 | Object_getOwnPropertyNames;
|
|---|
| 379 |
|
|---|
| 380 | /**
|
|---|
| 381 | * A property 'name' is fixed if it is an own property of the target.
|
|---|
| 382 | */
|
|---|
| 383 | function isFixed(name, target) {
|
|---|
| 384 | return ({}).hasOwnProperty.call(target, name);
|
|---|
| 385 | }
|
|---|
| 386 | function isSealed(name, target) {
|
|---|
| 387 | var desc = Object.getOwnPropertyDescriptor(target, name);
|
|---|
| 388 | if (desc === undefined) { return false; }
|
|---|
| 389 | return desc.configurable === false;
|
|---|
| 390 | }
|
|---|
| 391 | function isSealedDesc(desc) {
|
|---|
| 392 | return desc !== undefined && desc.configurable === false;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | /**
|
|---|
| 396 | * Performs all validation that Object.defineProperty performs,
|
|---|
| 397 | * without actually defining the property. Returns a boolean
|
|---|
| 398 | * indicating whether validation succeeded.
|
|---|
| 399 | *
|
|---|
| 400 | * Implementation transliterated from ES5.1 section 8.12.9
|
|---|
| 401 | */
|
|---|
| 402 | function isCompatibleDescriptor(extensible, current, desc) {
|
|---|
| 403 | if (current === undefined && extensible === false) {
|
|---|
| 404 | return false;
|
|---|
| 405 | }
|
|---|
| 406 | if (current === undefined && extensible === true) {
|
|---|
| 407 | return true;
|
|---|
| 408 | }
|
|---|
| 409 | if (isEmptyDescriptor(desc)) {
|
|---|
| 410 | return true;
|
|---|
| 411 | }
|
|---|
| 412 | if (isEquivalentDescriptor(current, desc)) {
|
|---|
| 413 | return true;
|
|---|
| 414 | }
|
|---|
| 415 | if (current.configurable === false) {
|
|---|
| 416 | if (desc.configurable === true) {
|
|---|
| 417 | return false;
|
|---|
| 418 | }
|
|---|
| 419 | if ('enumerable' in desc && desc.enumerable !== current.enumerable) {
|
|---|
| 420 | return false;
|
|---|
| 421 | }
|
|---|
| 422 | }
|
|---|
| 423 | if (isGenericDescriptor(desc)) {
|
|---|
| 424 | return true;
|
|---|
| 425 | }
|
|---|
| 426 | if (isDataDescriptor(current) !== isDataDescriptor(desc)) {
|
|---|
| 427 | if (current.configurable === false) {
|
|---|
| 428 | return false;
|
|---|
| 429 | }
|
|---|
| 430 | return true;
|
|---|
| 431 | }
|
|---|
| 432 | if (isDataDescriptor(current) && isDataDescriptor(desc)) {
|
|---|
| 433 | if (current.configurable === false) {
|
|---|
| 434 | if (current.writable === false && desc.writable === true) {
|
|---|
| 435 | return false;
|
|---|
| 436 | }
|
|---|
| 437 | if (current.writable === false) {
|
|---|
| 438 | if ('value' in desc && !sameValue(desc.value, current.value)) {
|
|---|
| 439 | return false;
|
|---|
| 440 | }
|
|---|
| 441 | }
|
|---|
| 442 | }
|
|---|
| 443 | return true;
|
|---|
| 444 | }
|
|---|
| 445 | if (isAccessorDescriptor(current) && isAccessorDescriptor(desc)) {
|
|---|
| 446 | if (current.configurable === false) {
|
|---|
| 447 | if ('set' in desc && !sameValue(desc.set, current.set)) {
|
|---|
| 448 | return false;
|
|---|
| 449 | }
|
|---|
| 450 | if ('get' in desc && !sameValue(desc.get, current.get)) {
|
|---|
| 451 | return false;
|
|---|
| 452 | }
|
|---|
| 453 | }
|
|---|
| 454 | }
|
|---|
| 455 | return true;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | // ES6 7.3.11 SetIntegrityLevel
|
|---|
| 459 | // level is one of "sealed" or "frozen"
|
|---|
| 460 | function setIntegrityLevel(target, level) {
|
|---|
| 461 | var ownProps = Object_getOwnPropertyNames(target);
|
|---|
| 462 | var pendingException = undefined;
|
|---|
| 463 | if (level === "sealed") {
|
|---|
| 464 | var l = +ownProps.length;
|
|---|
| 465 | var k;
|
|---|
| 466 | for (var i = 0; i < l; i++) {
|
|---|
| 467 | k = String(ownProps[i]);
|
|---|
| 468 | try {
|
|---|
| 469 | Object.defineProperty(target, k, { configurable: false });
|
|---|
| 470 | } catch (e) {
|
|---|
| 471 | if (pendingException === undefined) {
|
|---|
| 472 | pendingException = e;
|
|---|
| 473 | }
|
|---|
| 474 | }
|
|---|
| 475 | }
|
|---|
| 476 | } else {
|
|---|
| 477 | // level === "frozen"
|
|---|
| 478 | var l = +ownProps.length;
|
|---|
| 479 | var k;
|
|---|
| 480 | for (var i = 0; i < l; i++) {
|
|---|
| 481 | k = String(ownProps[i]);
|
|---|
| 482 | try {
|
|---|
| 483 | var currentDesc = Object.getOwnPropertyDescriptor(target, k);
|
|---|
| 484 | if (currentDesc !== undefined) {
|
|---|
| 485 | var desc;
|
|---|
| 486 | if (isAccessorDescriptor(currentDesc)) {
|
|---|
| 487 | desc = { configurable: false }
|
|---|
| 488 | } else {
|
|---|
| 489 | desc = { configurable: false, writable: false }
|
|---|
| 490 | }
|
|---|
| 491 | Object.defineProperty(target, k, desc);
|
|---|
| 492 | }
|
|---|
| 493 | } catch (e) {
|
|---|
| 494 | if (pendingException === undefined) {
|
|---|
| 495 | pendingException = e;
|
|---|
| 496 | }
|
|---|
| 497 | }
|
|---|
| 498 | }
|
|---|
| 499 | }
|
|---|
| 500 | if (pendingException !== undefined) {
|
|---|
| 501 | throw pendingException;
|
|---|
| 502 | }
|
|---|
| 503 | return Reflect.preventExtensions(target);
|
|---|
| 504 | }
|
|---|
| 505 |
|
|---|
| 506 | // ES6 7.3.12 TestIntegrityLevel
|
|---|
| 507 | // level is one of "sealed" or "frozen"
|
|---|
| 508 | function testIntegrityLevel(target, level) {
|
|---|
| 509 | var isExtensible = Object_isExtensible(target);
|
|---|
| 510 | if (isExtensible) return false;
|
|---|
| 511 |
|
|---|
| 512 | var ownProps = Object_getOwnPropertyNames(target);
|
|---|
| 513 | var pendingException = undefined;
|
|---|
| 514 | var configurable = false;
|
|---|
| 515 | var writable = false;
|
|---|
| 516 |
|
|---|
| 517 | var l = +ownProps.length;
|
|---|
| 518 | var k;
|
|---|
| 519 | var currentDesc;
|
|---|
| 520 | for (var i = 0; i < l; i++) {
|
|---|
| 521 | k = String(ownProps[i]);
|
|---|
| 522 | try {
|
|---|
| 523 | currentDesc = Object.getOwnPropertyDescriptor(target, k);
|
|---|
| 524 | configurable = configurable || currentDesc.configurable;
|
|---|
| 525 | if (isDataDescriptor(currentDesc)) {
|
|---|
| 526 | writable = writable || currentDesc.writable;
|
|---|
| 527 | }
|
|---|
| 528 | } catch (e) {
|
|---|
| 529 | if (pendingException === undefined) {
|
|---|
| 530 | pendingException = e;
|
|---|
| 531 | configurable = true;
|
|---|
| 532 | }
|
|---|
| 533 | }
|
|---|
| 534 | }
|
|---|
| 535 | if (pendingException !== undefined) {
|
|---|
| 536 | throw pendingException;
|
|---|
| 537 | }
|
|---|
| 538 | if (level === "frozen" && writable === true) {
|
|---|
| 539 | return false;
|
|---|
| 540 | }
|
|---|
| 541 | if (configurable === true) {
|
|---|
| 542 | return false;
|
|---|
| 543 | }
|
|---|
| 544 | return true;
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | // ---- The Validator handler wrapper around user handlers ----
|
|---|
| 548 |
|
|---|
| 549 | /**
|
|---|
| 550 | * @param target the object wrapped by this proxy.
|
|---|
| 551 | * As long as the proxy is extensible, only non-configurable properties
|
|---|
| 552 | * are checked against the target. Once the proxy becomes non-extensible,
|
|---|
| 553 | * invariants w.r.t. non-extensibility are also enforced.
|
|---|
| 554 | *
|
|---|
| 555 | * @param handler the handler of the direct proxy. The object emulated by
|
|---|
| 556 | * this handler is validated against the target object of the direct proxy.
|
|---|
| 557 | * Any violations that the handler makes against the invariants
|
|---|
| 558 | * of the target will cause a TypeError to be thrown.
|
|---|
| 559 | *
|
|---|
| 560 | * Both target and handler must be proper Objects at initialization time.
|
|---|
| 561 | */
|
|---|
| 562 | function Validator(target, handler) {
|
|---|
| 563 | // for non-revokable proxies, these are const references
|
|---|
| 564 | // for revokable proxies, on revocation:
|
|---|
| 565 | // - this.target is set to null
|
|---|
| 566 | // - this.handler is set to a handler that throws on all traps
|
|---|
| 567 | this.target = target;
|
|---|
| 568 | this.handler = handler;
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| 571 | Validator.prototype = {
|
|---|
| 572 |
|
|---|
| 573 | /**
|
|---|
| 574 | * If getTrap returns undefined, the caller should perform the
|
|---|
| 575 | * default forwarding behavior.
|
|---|
| 576 | * If getTrap returns normally otherwise, the return value
|
|---|
| 577 | * will be a callable trap function. When calling the trap function,
|
|---|
| 578 | * the caller is responsible for binding its |this| to |this.handler|.
|
|---|
| 579 | */
|
|---|
| 580 | getTrap: function(trapName) {
|
|---|
| 581 | var trap = this.handler[trapName];
|
|---|
| 582 | if (trap === undefined) {
|
|---|
| 583 | // the trap was not defined,
|
|---|
| 584 | // perform the default forwarding behavior
|
|---|
| 585 | return undefined;
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | if (typeof trap !== "function") {
|
|---|
| 589 | throw new TypeError(trapName + " trap is not callable: "+trap);
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | return trap;
|
|---|
| 593 | },
|
|---|
| 594 |
|
|---|
| 595 | // === fundamental traps ===
|
|---|
| 596 |
|
|---|
| 597 | /**
|
|---|
| 598 | * If name denotes a fixed property, check:
|
|---|
| 599 | * - whether targetHandler reports it as existent
|
|---|
| 600 | * - whether the returned descriptor is compatible with the fixed property
|
|---|
| 601 | * If the proxy is non-extensible, check:
|
|---|
| 602 | * - whether name is not a new property
|
|---|
| 603 | * Additionally, the returned descriptor is normalized and completed.
|
|---|
| 604 | */
|
|---|
| 605 | getOwnPropertyDescriptor: function(name) {
|
|---|
| 606 | "use strict";
|
|---|
| 607 |
|
|---|
| 608 | var trap = this.getTrap("getOwnPropertyDescriptor");
|
|---|
| 609 | if (trap === undefined) {
|
|---|
| 610 | return Reflect.getOwnPropertyDescriptor(this.target, name);
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | name = String(name);
|
|---|
| 614 | var desc = trap.call(this.handler, this.target, name);
|
|---|
| 615 | desc = normalizeAndCompletePropertyDescriptor(desc);
|
|---|
| 616 |
|
|---|
| 617 | var targetDesc = Object.getOwnPropertyDescriptor(this.target, name);
|
|---|
| 618 | var extensible = Object.isExtensible(this.target);
|
|---|
| 619 |
|
|---|
| 620 | if (desc === undefined) {
|
|---|
| 621 | if (isSealedDesc(targetDesc)) {
|
|---|
| 622 | throw new TypeError("cannot report non-configurable property '"+name+
|
|---|
| 623 | "' as non-existent");
|
|---|
| 624 | }
|
|---|
| 625 | if (!extensible && targetDesc !== undefined) {
|
|---|
| 626 | // if handler is allowed to return undefined, we cannot guarantee
|
|---|
| 627 | // that it will not return a descriptor for this property later.
|
|---|
| 628 | // Once a property has been reported as non-existent on a non-extensible
|
|---|
| 629 | // object, it should forever be reported as non-existent
|
|---|
| 630 | throw new TypeError("cannot report existing own property '"+name+
|
|---|
| 631 | "' as non-existent on a non-extensible object");
|
|---|
| 632 | }
|
|---|
| 633 | return undefined;
|
|---|
| 634 | }
|
|---|
| 635 |
|
|---|
| 636 | // at this point, we know (desc !== undefined), i.e.
|
|---|
| 637 | // targetHandler reports 'name' as an existing property
|
|---|
| 638 |
|
|---|
| 639 | // Note: we could collapse the following two if-tests into a single
|
|---|
| 640 | // test. Separating out the cases to improve error reporting.
|
|---|
| 641 |
|
|---|
| 642 | if (!extensible) {
|
|---|
| 643 | if (targetDesc === undefined) {
|
|---|
| 644 | throw new TypeError("cannot report a new own property '"+
|
|---|
| 645 | name + "' on a non-extensible object");
|
|---|
| 646 | }
|
|---|
| 647 | }
|
|---|
| 648 |
|
|---|
| 649 | if (name !== undefined) {
|
|---|
| 650 | if (!isCompatibleDescriptor(extensible, targetDesc, desc)) {
|
|---|
| 651 | throw new TypeError("cannot report incompatible property descriptor "+
|
|---|
| 652 | "for property '"+name+"'");
|
|---|
| 653 | }
|
|---|
| 654 | }
|
|---|
| 655 |
|
|---|
| 656 | if (desc.configurable === false) {
|
|---|
| 657 | if (targetDesc === undefined || targetDesc.configurable === true) {
|
|---|
| 658 | // if the property is configurable or non-existent on the target,
|
|---|
| 659 | // but is reported as a non-configurable property, it may later be
|
|---|
| 660 | // reported as configurable or non-existent, which violates the
|
|---|
| 661 | // invariant that if the property might change or disappear, the
|
|---|
| 662 | // configurable attribute must be true.
|
|---|
| 663 | throw new TypeError(
|
|---|
| 664 | "cannot report a non-configurable descriptor " +
|
|---|
| 665 | "for configurable or non-existent property '" + name + "'");
|
|---|
| 666 | }
|
|---|
| 667 | if ('writable' in desc && desc.writable === false) {
|
|---|
| 668 | if (targetDesc.writable === true) {
|
|---|
| 669 | // if the property is non-configurable, writable on the target,
|
|---|
| 670 | // but is reported as non-configurable, non-writable, it may later
|
|---|
| 671 | // be reported as non-configurable, writable again, which violates
|
|---|
| 672 | // the invariant that a non-configurable, non-writable property
|
|---|
| 673 | // may not change state.
|
|---|
| 674 | throw new TypeError(
|
|---|
| 675 | "cannot report non-configurable, writable property '" + name +
|
|---|
| 676 | "' as non-configurable, non-writable");
|
|---|
| 677 | }
|
|---|
| 678 | }
|
|---|
| 679 | }
|
|---|
| 680 |
|
|---|
| 681 | return desc;
|
|---|
| 682 | },
|
|---|
| 683 |
|
|---|
| 684 | /**
|
|---|
| 685 | * In the direct proxies design with refactored prototype climbing,
|
|---|
| 686 | * this trap is deprecated. For proxies-as-prototypes, instead
|
|---|
| 687 | * of calling this trap, the get, set, has or enumerate traps are
|
|---|
| 688 | * called instead.
|
|---|
| 689 | *
|
|---|
| 690 | * In this implementation, we "abuse" getPropertyDescriptor to
|
|---|
| 691 | * support trapping the get or set traps for proxies-as-prototypes.
|
|---|
| 692 | * We do this by returning a getter/setter pair that invokes
|
|---|
| 693 | * the corresponding traps.
|
|---|
| 694 | *
|
|---|
| 695 | * While this hack works for inherited property access, it has some
|
|---|
| 696 | * quirks:
|
|---|
| 697 | *
|
|---|
| 698 | * In Firefox, this trap is only called after a prior invocation
|
|---|
| 699 | * of the 'has' trap has returned true. Hence, expect the following
|
|---|
| 700 | * behavior:
|
|---|
| 701 | * <code>
|
|---|
| 702 | * var child = Object.create(Proxy(target, handler));
|
|---|
| 703 | * child[name] // triggers handler.has(target, name)
|
|---|
| 704 | * // if that returns true, triggers handler.get(target, name, child)
|
|---|
| 705 | * </code>
|
|---|
| 706 | *
|
|---|
| 707 | * On v8, the 'in' operator, when applied to an object that inherits
|
|---|
| 708 | * from a proxy, will call getPropertyDescriptor and walk the proto-chain.
|
|---|
| 709 | * That calls the below getPropertyDescriptor trap on the proxy. The
|
|---|
| 710 | * result of the 'in'-operator is then determined by whether this trap
|
|---|
| 711 | * returns undefined or a property descriptor object. That is why
|
|---|
| 712 | * we first explicitly trigger the 'has' trap to determine whether
|
|---|
| 713 | * the property exists.
|
|---|
| 714 | *
|
|---|
| 715 | * This has the side-effect that when enumerating properties on
|
|---|
| 716 | * an object that inherits from a proxy in v8, only properties
|
|---|
| 717 | * for which 'has' returns true are returned:
|
|---|
| 718 | *
|
|---|
| 719 | * <code>
|
|---|
| 720 | * var child = Object.create(Proxy(target, handler));
|
|---|
| 721 | * for (var prop in child) {
|
|---|
| 722 | * // only enumerates prop if (prop in child) returns true
|
|---|
| 723 | * }
|
|---|
| 724 | * </code>
|
|---|
| 725 | */
|
|---|
| 726 | getPropertyDescriptor: function(name) {
|
|---|
| 727 | var handler = this;
|
|---|
| 728 |
|
|---|
| 729 | if (!handler.has(name)) return undefined;
|
|---|
| 730 |
|
|---|
| 731 | return {
|
|---|
| 732 | get: function() {
|
|---|
| 733 | return handler.get(this, name);
|
|---|
| 734 | },
|
|---|
| 735 | set: function(val) {
|
|---|
| 736 | if (handler.set(this, name, val)) {
|
|---|
| 737 | return val;
|
|---|
| 738 | } else {
|
|---|
| 739 | throw new TypeError("failed assignment to "+name);
|
|---|
| 740 | }
|
|---|
| 741 | },
|
|---|
| 742 | enumerable: true,
|
|---|
| 743 | configurable: true
|
|---|
| 744 | };
|
|---|
| 745 | },
|
|---|
| 746 |
|
|---|
| 747 | /**
|
|---|
| 748 | * If name denotes a fixed property, check for incompatible changes.
|
|---|
| 749 | * If the proxy is non-extensible, check that new properties are rejected.
|
|---|
| 750 | */
|
|---|
| 751 | defineProperty: function(name, desc) {
|
|---|
| 752 | // TODO(tvcutsem): the current tracemonkey implementation of proxies
|
|---|
| 753 | // auto-completes 'desc', which is not correct. 'desc' should be
|
|---|
| 754 | // normalized, but not completed. Consider:
|
|---|
| 755 | // Object.defineProperty(proxy, 'foo', {enumerable:false})
|
|---|
| 756 | // This trap will receive desc =
|
|---|
| 757 | // {value:undefined,writable:false,enumerable:false,configurable:false}
|
|---|
| 758 | // This will also set all other attributes to their default value,
|
|---|
| 759 | // which is unexpected and different from [[DefineOwnProperty]].
|
|---|
| 760 | // Bug filed: https://bugzilla.mozilla.org/show_bug.cgi?id=601329
|
|---|
| 761 |
|
|---|
| 762 | var trap = this.getTrap("defineProperty");
|
|---|
| 763 | if (trap === undefined) {
|
|---|
| 764 | // default forwarding behavior
|
|---|
| 765 | return Reflect.defineProperty(this.target, name, desc);
|
|---|
| 766 | }
|
|---|
| 767 |
|
|---|
| 768 | name = String(name);
|
|---|
| 769 | var descObj = normalizePropertyDescriptor(desc);
|
|---|
| 770 | var success = trap.call(this.handler, this.target, name, descObj);
|
|---|
| 771 | success = !!success; // coerce to Boolean
|
|---|
| 772 |
|
|---|
| 773 | if (success === true) {
|
|---|
| 774 |
|
|---|
| 775 | var targetDesc = Object.getOwnPropertyDescriptor(this.target, name);
|
|---|
| 776 | var extensible = Object.isExtensible(this.target);
|
|---|
| 777 |
|
|---|
| 778 | // Note: we could collapse the following two if-tests into a single
|
|---|
| 779 | // test. Separating out the cases to improve error reporting.
|
|---|
| 780 |
|
|---|
| 781 | if (!extensible) {
|
|---|
| 782 | if (targetDesc === undefined) {
|
|---|
| 783 | throw new TypeError("cannot successfully add a new property '"+
|
|---|
| 784 | name + "' to a non-extensible object");
|
|---|
| 785 | }
|
|---|
| 786 | }
|
|---|
| 787 |
|
|---|
| 788 | if (targetDesc !== undefined) {
|
|---|
| 789 | if (!isCompatibleDescriptor(extensible, targetDesc, desc)) {
|
|---|
| 790 | throw new TypeError("cannot define incompatible property "+
|
|---|
| 791 | "descriptor for property '"+name+"'");
|
|---|
| 792 | }
|
|---|
| 793 | if (isDataDescriptor(targetDesc) &&
|
|---|
| 794 | targetDesc.configurable === false &&
|
|---|
| 795 | targetDesc.writable === true) {
|
|---|
| 796 | if (desc.configurable === false && desc.writable === false) {
|
|---|
| 797 | // if the property is non-configurable, writable on the target
|
|---|
| 798 | // but was successfully reported to be updated to
|
|---|
| 799 | // non-configurable, non-writable, it can later be reported
|
|---|
| 800 | // again as non-configurable, writable, which violates
|
|---|
| 801 | // the invariant that non-configurable, non-writable properties
|
|---|
| 802 | // cannot change state
|
|---|
| 803 | throw new TypeError(
|
|---|
| 804 | "cannot successfully define non-configurable, writable " +
|
|---|
| 805 | " property '" + name + "' as non-configurable, non-writable");
|
|---|
| 806 | }
|
|---|
| 807 | }
|
|---|
| 808 | }
|
|---|
| 809 |
|
|---|
| 810 | if (desc.configurable === false && !isSealedDesc(targetDesc)) {
|
|---|
| 811 | // if the property is configurable or non-existent on the target,
|
|---|
| 812 | // but is successfully being redefined as a non-configurable property,
|
|---|
| 813 | // it may later be reported as configurable or non-existent, which violates
|
|---|
| 814 | // the invariant that if the property might change or disappear, the
|
|---|
| 815 | // configurable attribute must be true.
|
|---|
| 816 | throw new TypeError(
|
|---|
| 817 | "cannot successfully define a non-configurable " +
|
|---|
| 818 | "descriptor for configurable or non-existent property '" +
|
|---|
| 819 | name + "'");
|
|---|
| 820 | }
|
|---|
| 821 |
|
|---|
| 822 | }
|
|---|
| 823 |
|
|---|
| 824 | return success;
|
|---|
| 825 | },
|
|---|
| 826 |
|
|---|
| 827 | /**
|
|---|
| 828 | * On success, check whether the target object is indeed non-extensible.
|
|---|
| 829 | */
|
|---|
| 830 | preventExtensions: function() {
|
|---|
| 831 | var trap = this.getTrap("preventExtensions");
|
|---|
| 832 | if (trap === undefined) {
|
|---|
| 833 | // default forwarding behavior
|
|---|
| 834 | return Reflect.preventExtensions(this.target);
|
|---|
| 835 | }
|
|---|
| 836 |
|
|---|
| 837 | var success = trap.call(this.handler, this.target);
|
|---|
| 838 | success = !!success; // coerce to Boolean
|
|---|
| 839 | if (success) {
|
|---|
| 840 | if (Object_isExtensible(this.target)) {
|
|---|
| 841 | throw new TypeError("can't report extensible object as non-extensible: "+
|
|---|
| 842 | this.target);
|
|---|
| 843 | }
|
|---|
| 844 | }
|
|---|
| 845 | return success;
|
|---|
| 846 | },
|
|---|
| 847 |
|
|---|
| 848 | /**
|
|---|
| 849 | * If name denotes a sealed property, check whether handler rejects.
|
|---|
| 850 | */
|
|---|
| 851 | delete: function(name) {
|
|---|
| 852 | "use strict";
|
|---|
| 853 | var trap = this.getTrap("deleteProperty");
|
|---|
| 854 | if (trap === undefined) {
|
|---|
| 855 | // default forwarding behavior
|
|---|
| 856 | return Reflect.deleteProperty(this.target, name);
|
|---|
| 857 | }
|
|---|
| 858 |
|
|---|
| 859 | name = String(name);
|
|---|
| 860 | var res = trap.call(this.handler, this.target, name);
|
|---|
| 861 | res = !!res; // coerce to Boolean
|
|---|
| 862 |
|
|---|
| 863 | var targetDesc;
|
|---|
| 864 | if (res === true) {
|
|---|
| 865 | targetDesc = Object.getOwnPropertyDescriptor(this.target, name);
|
|---|
| 866 | if (targetDesc !== undefined && targetDesc.configurable === false) {
|
|---|
| 867 | throw new TypeError("property '" + name + "' is non-configurable "+
|
|---|
| 868 | "and can't be deleted");
|
|---|
| 869 | }
|
|---|
| 870 | if (targetDesc !== undefined && !Object_isExtensible(this.target)) {
|
|---|
| 871 | // if the property still exists on a non-extensible target but
|
|---|
| 872 | // is reported as successfully deleted, it may later be reported
|
|---|
| 873 | // as present, which violates the invariant that an own property,
|
|---|
| 874 | // deleted from a non-extensible object cannot reappear.
|
|---|
| 875 | throw new TypeError(
|
|---|
| 876 | "cannot successfully delete existing property '" + name +
|
|---|
| 877 | "' on a non-extensible object");
|
|---|
| 878 | }
|
|---|
| 879 | }
|
|---|
| 880 |
|
|---|
| 881 | return res;
|
|---|
| 882 | },
|
|---|
| 883 |
|
|---|
| 884 | /**
|
|---|
| 885 | * The getOwnPropertyNames trap was replaced by the ownKeys trap,
|
|---|
| 886 | * which now also returns an array (of strings or symbols) and
|
|---|
| 887 | * which performs the same rigorous invariant checks as getOwnPropertyNames
|
|---|
| 888 | *
|
|---|
| 889 | * See issue #48 on how this trap can still get invoked by external libs
|
|---|
| 890 | * that don't use the patched Object.getOwnPropertyNames function.
|
|---|
| 891 | */
|
|---|
| 892 | getOwnPropertyNames: function() {
|
|---|
| 893 | // Note: removed deprecation warning to avoid dependency on 'console'
|
|---|
| 894 | // (and on node, should anyway use util.deprecate). Deprecation warnings
|
|---|
| 895 | // can also be annoying when they are outside of the user's control, e.g.
|
|---|
| 896 | // when an external library calls unpatched Object.getOwnPropertyNames.
|
|---|
| 897 | // Since there is a clean fallback to `ownKeys`, the fact that the
|
|---|
| 898 | // deprecated method is still called is mostly harmless anyway.
|
|---|
| 899 | // See also issues #65 and #66.
|
|---|
| 900 | // console.warn("getOwnPropertyNames trap is deprecated. Use ownKeys instead");
|
|---|
| 901 | return this.ownKeys();
|
|---|
| 902 | },
|
|---|
| 903 |
|
|---|
| 904 | /**
|
|---|
| 905 | * Checks whether the trap result does not contain any new properties
|
|---|
| 906 | * if the proxy is non-extensible.
|
|---|
| 907 | *
|
|---|
| 908 | * Any own non-configurable properties of the target that are not included
|
|---|
| 909 | * in the trap result give rise to a TypeError. As such, we check whether the
|
|---|
| 910 | * returned result contains at least all sealed properties of the target
|
|---|
| 911 | * object.
|
|---|
| 912 | *
|
|---|
| 913 | * Additionally, the trap result is normalized.
|
|---|
| 914 | * Instead of returning the trap result directly:
|
|---|
| 915 | * - create and return a fresh Array,
|
|---|
| 916 | * - of which each element is coerced to a String
|
|---|
| 917 | *
|
|---|
| 918 | * This trap is called a.o. by Reflect.ownKeys, Object.getOwnPropertyNames
|
|---|
| 919 | * and Object.keys (the latter filters out only the enumerable own properties).
|
|---|
| 920 | */
|
|---|
| 921 | ownKeys: function() {
|
|---|
| 922 | var trap = this.getTrap("ownKeys");
|
|---|
| 923 | if (trap === undefined) {
|
|---|
| 924 | // default forwarding behavior
|
|---|
| 925 | return Reflect.ownKeys(this.target);
|
|---|
| 926 | }
|
|---|
| 927 |
|
|---|
| 928 | var trapResult = trap.call(this.handler, this.target);
|
|---|
| 929 |
|
|---|
| 930 | // propNames is used as a set of strings
|
|---|
| 931 | var propNames = Object.create(null);
|
|---|
| 932 | var numProps = +trapResult.length;
|
|---|
| 933 | var result = new Array(numProps);
|
|---|
| 934 |
|
|---|
| 935 | for (var i = 0; i < numProps; i++) {
|
|---|
| 936 | var s = String(trapResult[i]);
|
|---|
| 937 | if (!Object.isExtensible(this.target) && !isFixed(s, this.target)) {
|
|---|
| 938 | // non-extensible proxies don't tolerate new own property names
|
|---|
| 939 | throw new TypeError("ownKeys trap cannot list a new "+
|
|---|
| 940 | "property '"+s+"' on a non-extensible object");
|
|---|
| 941 | }
|
|---|
| 942 |
|
|---|
| 943 | propNames[s] = true;
|
|---|
| 944 | result[i] = s;
|
|---|
| 945 | }
|
|---|
| 946 |
|
|---|
| 947 | var ownProps = Object_getOwnPropertyNames(this.target);
|
|---|
| 948 | var target = this.target;
|
|---|
| 949 | ownProps.forEach(function (ownProp) {
|
|---|
| 950 | if (!propNames[ownProp]) {
|
|---|
| 951 | if (isSealed(ownProp, target)) {
|
|---|
| 952 | throw new TypeError("ownKeys trap failed to include "+
|
|---|
| 953 | "non-configurable property '"+ownProp+"'");
|
|---|
| 954 | }
|
|---|
| 955 | if (!Object.isExtensible(target) &&
|
|---|
| 956 | isFixed(ownProp, target)) {
|
|---|
| 957 | // if handler is allowed to report ownProp as non-existent,
|
|---|
| 958 | // we cannot guarantee that it will never later report it as
|
|---|
| 959 | // existent. Once a property has been reported as non-existent
|
|---|
| 960 | // on a non-extensible object, it should forever be reported as
|
|---|
| 961 | // non-existent
|
|---|
| 962 | throw new TypeError("ownKeys trap cannot report existing own property '"+
|
|---|
| 963 | ownProp+"' as non-existent on a non-extensible object");
|
|---|
| 964 | }
|
|---|
| 965 | }
|
|---|
| 966 | });
|
|---|
| 967 |
|
|---|
| 968 | return result;
|
|---|
| 969 | },
|
|---|
| 970 |
|
|---|
| 971 | /**
|
|---|
| 972 | * Checks whether the trap result is consistent with the state of the
|
|---|
| 973 | * wrapped target.
|
|---|
| 974 | */
|
|---|
| 975 | isExtensible: function() {
|
|---|
| 976 | var trap = this.getTrap("isExtensible");
|
|---|
| 977 | if (trap === undefined) {
|
|---|
| 978 | // default forwarding behavior
|
|---|
| 979 | return Reflect.isExtensible(this.target);
|
|---|
| 980 | }
|
|---|
| 981 |
|
|---|
| 982 | var result = trap.call(this.handler, this.target);
|
|---|
| 983 | result = !!result; // coerce to Boolean
|
|---|
| 984 | var state = Object_isExtensible(this.target);
|
|---|
| 985 | if (result !== state) {
|
|---|
| 986 | if (result) {
|
|---|
| 987 | throw new TypeError("cannot report non-extensible object as extensible: "+
|
|---|
| 988 | this.target);
|
|---|
| 989 | } else {
|
|---|
| 990 | throw new TypeError("cannot report extensible object as non-extensible: "+
|
|---|
| 991 | this.target);
|
|---|
| 992 | }
|
|---|
| 993 | }
|
|---|
| 994 | return state;
|
|---|
| 995 | },
|
|---|
| 996 |
|
|---|
| 997 | /**
|
|---|
| 998 | * Check whether the trap result corresponds to the target's [[Prototype]]
|
|---|
| 999 | */
|
|---|
| 1000 | getPrototypeOf: function() {
|
|---|
| 1001 | var trap = this.getTrap("getPrototypeOf");
|
|---|
| 1002 | if (trap === undefined) {
|
|---|
| 1003 | // default forwarding behavior
|
|---|
| 1004 | return Reflect.getPrototypeOf(this.target);
|
|---|
| 1005 | }
|
|---|
| 1006 |
|
|---|
| 1007 | var allegedProto = trap.call(this.handler, this.target);
|
|---|
| 1008 |
|
|---|
| 1009 | if (!Object_isExtensible(this.target)) {
|
|---|
| 1010 | var actualProto = Object_getPrototypeOf(this.target);
|
|---|
| 1011 | if (!sameValue(allegedProto, actualProto)) {
|
|---|
| 1012 | throw new TypeError("prototype value does not match: " + this.target);
|
|---|
| 1013 | }
|
|---|
| 1014 | }
|
|---|
| 1015 |
|
|---|
| 1016 | return allegedProto;
|
|---|
| 1017 | },
|
|---|
| 1018 |
|
|---|
| 1019 | /**
|
|---|
| 1020 | * If target is non-extensible and setPrototypeOf trap returns true,
|
|---|
| 1021 | * check whether the trap result corresponds to the target's [[Prototype]]
|
|---|
| 1022 | */
|
|---|
| 1023 | setPrototypeOf: function(newProto) {
|
|---|
| 1024 | var trap = this.getTrap("setPrototypeOf");
|
|---|
| 1025 | if (trap === undefined) {
|
|---|
| 1026 | // default forwarding behavior
|
|---|
| 1027 | return Reflect.setPrototypeOf(this.target, newProto);
|
|---|
| 1028 | }
|
|---|
| 1029 |
|
|---|
| 1030 | var success = trap.call(this.handler, this.target, newProto);
|
|---|
| 1031 |
|
|---|
| 1032 | success = !!success;
|
|---|
| 1033 | if (success && !Object_isExtensible(this.target)) {
|
|---|
| 1034 | var actualProto = Object_getPrototypeOf(this.target);
|
|---|
| 1035 | if (!sameValue(newProto, actualProto)) {
|
|---|
| 1036 | throw new TypeError("prototype value does not match: " + this.target);
|
|---|
| 1037 | }
|
|---|
| 1038 | }
|
|---|
| 1039 |
|
|---|
| 1040 | return success;
|
|---|
| 1041 | },
|
|---|
| 1042 |
|
|---|
| 1043 | /**
|
|---|
| 1044 | * In the direct proxies design with refactored prototype climbing,
|
|---|
| 1045 | * this trap is deprecated. For proxies-as-prototypes, for-in will
|
|---|
| 1046 | * call the enumerate() trap. If that trap is not defined, the
|
|---|
| 1047 | * operation is forwarded to the target, no more fallback on this
|
|---|
| 1048 | * fundamental trap.
|
|---|
| 1049 | */
|
|---|
| 1050 | getPropertyNames: function() {
|
|---|
| 1051 | throw new TypeError("getPropertyNames trap is deprecated");
|
|---|
| 1052 | },
|
|---|
| 1053 |
|
|---|
| 1054 | // === derived traps ===
|
|---|
| 1055 |
|
|---|
| 1056 | /**
|
|---|
| 1057 | * If name denotes a fixed property, check whether the trap returns true.
|
|---|
| 1058 | */
|
|---|
| 1059 | has: function(name) {
|
|---|
| 1060 | var trap = this.getTrap("has");
|
|---|
| 1061 | if (trap === undefined) {
|
|---|
| 1062 | // default forwarding behavior
|
|---|
| 1063 | return Reflect.has(this.target, name);
|
|---|
| 1064 | }
|
|---|
| 1065 |
|
|---|
| 1066 | name = String(name);
|
|---|
| 1067 | var res = trap.call(this.handler, this.target, name);
|
|---|
| 1068 | res = !!res; // coerce to Boolean
|
|---|
| 1069 |
|
|---|
| 1070 | if (res === false) {
|
|---|
| 1071 | if (isSealed(name, this.target)) {
|
|---|
| 1072 | throw new TypeError("cannot report existing non-configurable own "+
|
|---|
| 1073 | "property '"+ name + "' as a non-existent "+
|
|---|
| 1074 | "property");
|
|---|
| 1075 | }
|
|---|
| 1076 | if (!Object.isExtensible(this.target) &&
|
|---|
| 1077 | isFixed(name, this.target)) {
|
|---|
| 1078 | // if handler is allowed to return false, we cannot guarantee
|
|---|
| 1079 | // that it will not return true for this property later.
|
|---|
| 1080 | // Once a property has been reported as non-existent on a non-extensible
|
|---|
| 1081 | // object, it should forever be reported as non-existent
|
|---|
| 1082 | throw new TypeError("cannot report existing own property '"+name+
|
|---|
| 1083 | "' as non-existent on a non-extensible object");
|
|---|
| 1084 | }
|
|---|
| 1085 | }
|
|---|
| 1086 |
|
|---|
| 1087 | // if res === true, we don't need to check for extensibility
|
|---|
| 1088 | // even for a non-extensible proxy that has no own name property,
|
|---|
| 1089 | // the property may have been inherited
|
|---|
| 1090 |
|
|---|
| 1091 | return res;
|
|---|
| 1092 | },
|
|---|
| 1093 |
|
|---|
| 1094 | /**
|
|---|
| 1095 | * If name denotes a fixed non-configurable, non-writable data property,
|
|---|
| 1096 | * check its return value against the previously asserted value of the
|
|---|
| 1097 | * fixed property.
|
|---|
| 1098 | */
|
|---|
| 1099 | get: function(receiver, name) {
|
|---|
| 1100 |
|
|---|
| 1101 | // experimental support for invoke() trap on platforms that
|
|---|
| 1102 | // support __noSuchMethod__
|
|---|
| 1103 | /*
|
|---|
| 1104 | if (name === '__noSuchMethod__') {
|
|---|
| 1105 | var handler = this;
|
|---|
| 1106 | return function(name, args) {
|
|---|
| 1107 | return handler.invoke(receiver, name, args);
|
|---|
| 1108 | }
|
|---|
| 1109 | }
|
|---|
| 1110 | */
|
|---|
| 1111 |
|
|---|
| 1112 | var trap = this.getTrap("get");
|
|---|
| 1113 | if (trap === undefined) {
|
|---|
| 1114 | // default forwarding behavior
|
|---|
| 1115 | return Reflect.get(this.target, name, receiver);
|
|---|
| 1116 | }
|
|---|
| 1117 |
|
|---|
| 1118 | name = String(name);
|
|---|
| 1119 | var res = trap.call(this.handler, this.target, name, receiver);
|
|---|
| 1120 |
|
|---|
| 1121 | var fixedDesc = Object.getOwnPropertyDescriptor(this.target, name);
|
|---|
| 1122 | // check consistency of the returned value
|
|---|
| 1123 | if (fixedDesc !== undefined) { // getting an existing property
|
|---|
| 1124 | if (isDataDescriptor(fixedDesc) &&
|
|---|
| 1125 | fixedDesc.configurable === false &&
|
|---|
| 1126 | fixedDesc.writable === false) { // own frozen data property
|
|---|
| 1127 | if (!sameValue(res, fixedDesc.value)) {
|
|---|
| 1128 | throw new TypeError("cannot report inconsistent value for "+
|
|---|
| 1129 | "non-writable, non-configurable property '"+
|
|---|
| 1130 | name+"'");
|
|---|
| 1131 | }
|
|---|
| 1132 | } else { // it's an accessor property
|
|---|
| 1133 | if (isAccessorDescriptor(fixedDesc) &&
|
|---|
| 1134 | fixedDesc.configurable === false &&
|
|---|
| 1135 | fixedDesc.get === undefined) {
|
|---|
| 1136 | if (res !== undefined) {
|
|---|
| 1137 | throw new TypeError("must report undefined for non-configurable "+
|
|---|
| 1138 | "accessor property '"+name+"' without getter");
|
|---|
| 1139 | }
|
|---|
| 1140 | }
|
|---|
| 1141 | }
|
|---|
| 1142 | }
|
|---|
| 1143 |
|
|---|
| 1144 | return res;
|
|---|
| 1145 | },
|
|---|
| 1146 |
|
|---|
| 1147 | /**
|
|---|
| 1148 | * If name denotes a fixed non-configurable, non-writable data property,
|
|---|
| 1149 | * check that the trap rejects the assignment.
|
|---|
| 1150 | */
|
|---|
| 1151 | set: function(receiver, name, val) {
|
|---|
| 1152 | var trap = this.getTrap("set");
|
|---|
| 1153 | if (trap === undefined) {
|
|---|
| 1154 | // default forwarding behavior
|
|---|
| 1155 | return Reflect.set(this.target, name, val, receiver);
|
|---|
| 1156 | }
|
|---|
| 1157 |
|
|---|
| 1158 | name = String(name);
|
|---|
| 1159 | var res = trap.call(this.handler, this.target, name, val, receiver);
|
|---|
| 1160 | res = !!res; // coerce to Boolean
|
|---|
| 1161 |
|
|---|
| 1162 | // if success is reported, check whether property is truly assignable
|
|---|
| 1163 | if (res === true) {
|
|---|
| 1164 | var fixedDesc = Object.getOwnPropertyDescriptor(this.target, name);
|
|---|
| 1165 | if (fixedDesc !== undefined) { // setting an existing property
|
|---|
| 1166 | if (isDataDescriptor(fixedDesc) &&
|
|---|
| 1167 | fixedDesc.configurable === false &&
|
|---|
| 1168 | fixedDesc.writable === false) {
|
|---|
| 1169 | if (!sameValue(val, fixedDesc.value)) {
|
|---|
| 1170 | throw new TypeError("cannot successfully assign to a "+
|
|---|
| 1171 | "non-writable, non-configurable property '"+
|
|---|
| 1172 | name+"'");
|
|---|
| 1173 | }
|
|---|
| 1174 | } else {
|
|---|
| 1175 | if (isAccessorDescriptor(fixedDesc) &&
|
|---|
| 1176 | fixedDesc.configurable === false && // non-configurable
|
|---|
| 1177 | fixedDesc.set === undefined) { // accessor with undefined setter
|
|---|
| 1178 | throw new TypeError("setting a property '"+name+"' that has "+
|
|---|
| 1179 | " only a getter");
|
|---|
| 1180 | }
|
|---|
| 1181 | }
|
|---|
| 1182 | }
|
|---|
| 1183 | }
|
|---|
| 1184 |
|
|---|
| 1185 | return res;
|
|---|
| 1186 | },
|
|---|
| 1187 |
|
|---|
| 1188 | /**
|
|---|
| 1189 | * Any own enumerable non-configurable properties of the target that are not
|
|---|
| 1190 | * included in the trap result give rise to a TypeError. As such, we check
|
|---|
| 1191 | * whether the returned result contains at least all sealed enumerable properties
|
|---|
| 1192 | * of the target object.
|
|---|
| 1193 | *
|
|---|
| 1194 | * The trap should return an iterator.
|
|---|
| 1195 | *
|
|---|
| 1196 | * However, as implementations of pre-direct proxies still expect enumerate
|
|---|
| 1197 | * to return an array of strings, we convert the iterator into an array.
|
|---|
| 1198 | */
|
|---|
| 1199 | enumerate: function() {
|
|---|
| 1200 | var trap = this.getTrap("enumerate");
|
|---|
| 1201 | if (trap === undefined) {
|
|---|
| 1202 | // default forwarding behavior
|
|---|
| 1203 | var trapResult = Reflect.enumerate(this.target);
|
|---|
| 1204 | var result = [];
|
|---|
| 1205 | var nxt = trapResult.next();
|
|---|
| 1206 | while (!nxt.done) {
|
|---|
| 1207 | result.push(String(nxt.value));
|
|---|
| 1208 | nxt = trapResult.next();
|
|---|
| 1209 | }
|
|---|
| 1210 | return result;
|
|---|
| 1211 | }
|
|---|
| 1212 |
|
|---|
| 1213 | var trapResult = trap.call(this.handler, this.target);
|
|---|
| 1214 |
|
|---|
| 1215 | if (trapResult === null ||
|
|---|
| 1216 | trapResult === undefined ||
|
|---|
| 1217 | trapResult.next === undefined) {
|
|---|
| 1218 | throw new TypeError("enumerate trap should return an iterator, got: "+
|
|---|
| 1219 | trapResult);
|
|---|
| 1220 | }
|
|---|
| 1221 |
|
|---|
| 1222 | // propNames is used as a set of strings
|
|---|
| 1223 | var propNames = Object.create(null);
|
|---|
| 1224 |
|
|---|
| 1225 | // var numProps = +trapResult.length;
|
|---|
| 1226 | var result = []; // new Array(numProps);
|
|---|
| 1227 |
|
|---|
| 1228 | // trapResult is supposed to be an iterator
|
|---|
| 1229 | // drain iterator to array as current implementations still expect
|
|---|
| 1230 | // enumerate to return an array of strings
|
|---|
| 1231 | var nxt = trapResult.next();
|
|---|
| 1232 |
|
|---|
| 1233 | while (!nxt.done) {
|
|---|
| 1234 | var s = String(nxt.value);
|
|---|
| 1235 | if (propNames[s]) {
|
|---|
| 1236 | throw new TypeError("enumerate trap cannot list a "+
|
|---|
| 1237 | "duplicate property '"+s+"'");
|
|---|
| 1238 | }
|
|---|
| 1239 | propNames[s] = true;
|
|---|
| 1240 | result.push(s);
|
|---|
| 1241 | nxt = trapResult.next();
|
|---|
| 1242 | }
|
|---|
| 1243 |
|
|---|
| 1244 | /*for (var i = 0; i < numProps; i++) {
|
|---|
| 1245 | var s = String(trapResult[i]);
|
|---|
| 1246 | if (propNames[s]) {
|
|---|
| 1247 | throw new TypeError("enumerate trap cannot list a "+
|
|---|
| 1248 | "duplicate property '"+s+"'");
|
|---|
| 1249 | }
|
|---|
| 1250 |
|
|---|
| 1251 | propNames[s] = true;
|
|---|
| 1252 | result[i] = s;
|
|---|
| 1253 | } */
|
|---|
| 1254 |
|
|---|
| 1255 | var ownEnumerableProps = Object.keys(this.target);
|
|---|
| 1256 | var target = this.target;
|
|---|
| 1257 | ownEnumerableProps.forEach(function (ownEnumerableProp) {
|
|---|
| 1258 | if (!propNames[ownEnumerableProp]) {
|
|---|
| 1259 | if (isSealed(ownEnumerableProp, target)) {
|
|---|
| 1260 | throw new TypeError("enumerate trap failed to include "+
|
|---|
| 1261 | "non-configurable enumerable property '"+
|
|---|
| 1262 | ownEnumerableProp+"'");
|
|---|
| 1263 | }
|
|---|
| 1264 | if (!Object.isExtensible(target) &&
|
|---|
| 1265 | isFixed(ownEnumerableProp, target)) {
|
|---|
| 1266 | // if handler is allowed not to report ownEnumerableProp as an own
|
|---|
| 1267 | // property, we cannot guarantee that it will never report it as
|
|---|
| 1268 | // an own property later. Once a property has been reported as
|
|---|
| 1269 | // non-existent on a non-extensible object, it should forever be
|
|---|
| 1270 | // reported as non-existent
|
|---|
| 1271 | throw new TypeError("cannot report existing own property '"+
|
|---|
| 1272 | ownEnumerableProp+"' as non-existent on a "+
|
|---|
| 1273 | "non-extensible object");
|
|---|
| 1274 | }
|
|---|
| 1275 | }
|
|---|
| 1276 | });
|
|---|
| 1277 |
|
|---|
| 1278 | return result;
|
|---|
| 1279 | },
|
|---|
| 1280 |
|
|---|
| 1281 | /**
|
|---|
| 1282 | * The iterate trap is deprecated by the enumerate trap.
|
|---|
| 1283 | */
|
|---|
| 1284 | iterate: Validator.prototype.enumerate,
|
|---|
| 1285 |
|
|---|
| 1286 | /**
|
|---|
| 1287 | * Any own non-configurable properties of the target that are not included
|
|---|
| 1288 | * in the trap result give rise to a TypeError. As such, we check whether the
|
|---|
| 1289 | * returned result contains at least all sealed properties of the target
|
|---|
| 1290 | * object.
|
|---|
| 1291 | *
|
|---|
| 1292 | * The trap result is normalized.
|
|---|
| 1293 | * The trap result is not returned directly. Instead:
|
|---|
| 1294 | * - create and return a fresh Array,
|
|---|
| 1295 | * - of which each element is coerced to String,
|
|---|
| 1296 | * - which does not contain duplicates
|
|---|
| 1297 | *
|
|---|
| 1298 | * FIXME: keys trap is deprecated
|
|---|
| 1299 | */
|
|---|
| 1300 | /*
|
|---|
| 1301 | keys: function() {
|
|---|
| 1302 | var trap = this.getTrap("keys");
|
|---|
| 1303 | if (trap === undefined) {
|
|---|
| 1304 | // default forwarding behavior
|
|---|
| 1305 | return Reflect.keys(this.target);
|
|---|
| 1306 | }
|
|---|
| 1307 |
|
|---|
| 1308 | var trapResult = trap.call(this.handler, this.target);
|
|---|
| 1309 |
|
|---|
| 1310 | // propNames is used as a set of strings
|
|---|
| 1311 | var propNames = Object.create(null);
|
|---|
| 1312 | var numProps = +trapResult.length;
|
|---|
| 1313 | var result = new Array(numProps);
|
|---|
| 1314 |
|
|---|
| 1315 | for (var i = 0; i < numProps; i++) {
|
|---|
| 1316 | var s = String(trapResult[i]);
|
|---|
| 1317 | if (propNames[s]) {
|
|---|
| 1318 | throw new TypeError("keys trap cannot list a "+
|
|---|
| 1319 | "duplicate property '"+s+"'");
|
|---|
| 1320 | }
|
|---|
| 1321 | if (!Object.isExtensible(this.target) && !isFixed(s, this.target)) {
|
|---|
| 1322 | // non-extensible proxies don't tolerate new own property names
|
|---|
| 1323 | throw new TypeError("keys trap cannot list a new "+
|
|---|
| 1324 | "property '"+s+"' on a non-extensible object");
|
|---|
| 1325 | }
|
|---|
| 1326 |
|
|---|
| 1327 | propNames[s] = true;
|
|---|
| 1328 | result[i] = s;
|
|---|
| 1329 | }
|
|---|
| 1330 |
|
|---|
| 1331 | var ownEnumerableProps = Object.keys(this.target);
|
|---|
| 1332 | var target = this.target;
|
|---|
| 1333 | ownEnumerableProps.forEach(function (ownEnumerableProp) {
|
|---|
| 1334 | if (!propNames[ownEnumerableProp]) {
|
|---|
| 1335 | if (isSealed(ownEnumerableProp, target)) {
|
|---|
| 1336 | throw new TypeError("keys trap failed to include "+
|
|---|
| 1337 | "non-configurable enumerable property '"+
|
|---|
| 1338 | ownEnumerableProp+"'");
|
|---|
| 1339 | }
|
|---|
| 1340 | if (!Object.isExtensible(target) &&
|
|---|
| 1341 | isFixed(ownEnumerableProp, target)) {
|
|---|
| 1342 | // if handler is allowed not to report ownEnumerableProp as an own
|
|---|
| 1343 | // property, we cannot guarantee that it will never report it as
|
|---|
| 1344 | // an own property later. Once a property has been reported as
|
|---|
| 1345 | // non-existent on a non-extensible object, it should forever be
|
|---|
| 1346 | // reported as non-existent
|
|---|
| 1347 | throw new TypeError("cannot report existing own property '"+
|
|---|
| 1348 | ownEnumerableProp+"' as non-existent on a "+
|
|---|
| 1349 | "non-extensible object");
|
|---|
| 1350 | }
|
|---|
| 1351 | }
|
|---|
| 1352 | });
|
|---|
| 1353 |
|
|---|
| 1354 | return result;
|
|---|
| 1355 | },
|
|---|
| 1356 | */
|
|---|
| 1357 |
|
|---|
| 1358 | /**
|
|---|
| 1359 | * New trap that reifies [[Call]].
|
|---|
| 1360 | * If the target is a function, then a call to
|
|---|
| 1361 | * proxy(...args)
|
|---|
| 1362 | * Triggers this trap
|
|---|
| 1363 | */
|
|---|
| 1364 | apply: function(target, thisBinding, args) {
|
|---|
| 1365 | var trap = this.getTrap("apply");
|
|---|
| 1366 | if (trap === undefined) {
|
|---|
| 1367 | return Reflect.apply(target, thisBinding, args);
|
|---|
| 1368 | }
|
|---|
| 1369 |
|
|---|
| 1370 | if (typeof this.target === "function") {
|
|---|
| 1371 | return trap.call(this.handler, target, thisBinding, args);
|
|---|
| 1372 | } else {
|
|---|
| 1373 | throw new TypeError("apply: "+ target + " is not a function");
|
|---|
| 1374 | }
|
|---|
| 1375 | },
|
|---|
| 1376 |
|
|---|
| 1377 | /**
|
|---|
| 1378 | * New trap that reifies [[Construct]].
|
|---|
| 1379 | * If the target is a function, then a call to
|
|---|
| 1380 | * new proxy(...args)
|
|---|
| 1381 | * Triggers this trap
|
|---|
| 1382 | */
|
|---|
| 1383 | construct: function(target, args, newTarget) {
|
|---|
| 1384 | var trap = this.getTrap("construct");
|
|---|
| 1385 | if (trap === undefined) {
|
|---|
| 1386 | return Reflect.construct(target, args, newTarget);
|
|---|
| 1387 | }
|
|---|
| 1388 |
|
|---|
| 1389 | if (typeof target !== "function") {
|
|---|
| 1390 | throw new TypeError("new: "+ target + " is not a function");
|
|---|
| 1391 | }
|
|---|
| 1392 |
|
|---|
| 1393 | if (newTarget === undefined) {
|
|---|
| 1394 | newTarget = target;
|
|---|
| 1395 | } else {
|
|---|
| 1396 | if (typeof newTarget !== "function") {
|
|---|
| 1397 | throw new TypeError("new: "+ newTarget + " is not a function");
|
|---|
| 1398 | }
|
|---|
| 1399 | }
|
|---|
| 1400 | return trap.call(this.handler, target, args, newTarget);
|
|---|
| 1401 | }
|
|---|
| 1402 | };
|
|---|
| 1403 |
|
|---|
| 1404 | // ---- end of the Validator handler wrapper handler ----
|
|---|
| 1405 |
|
|---|
| 1406 | // In what follows, a 'direct proxy' is a proxy
|
|---|
| 1407 | // whose handler is a Validator. Such proxies can be made non-extensible,
|
|---|
| 1408 | // sealed or frozen without losing the ability to trap.
|
|---|
| 1409 |
|
|---|
| 1410 | // maps direct proxies to their Validator handlers
|
|---|
| 1411 | var directProxies = new WeakMap();
|
|---|
| 1412 |
|
|---|
| 1413 | // patch Object.{preventExtensions,seal,freeze} so that
|
|---|
| 1414 | // they recognize fixable proxies and act accordingly
|
|---|
| 1415 | Object.preventExtensions = function(subject) {
|
|---|
| 1416 | var vhandler = directProxies.get(subject);
|
|---|
| 1417 | if (vhandler !== undefined) {
|
|---|
| 1418 | if (vhandler.preventExtensions()) {
|
|---|
| 1419 | return subject;
|
|---|
| 1420 | } else {
|
|---|
| 1421 | throw new TypeError("preventExtensions on "+subject+" rejected");
|
|---|
| 1422 | }
|
|---|
| 1423 | } else {
|
|---|
| 1424 | return prim_preventExtensions(subject);
|
|---|
| 1425 | }
|
|---|
| 1426 | };
|
|---|
| 1427 | Object.seal = function(subject) {
|
|---|
| 1428 | setIntegrityLevel(subject, "sealed");
|
|---|
| 1429 | return subject;
|
|---|
| 1430 | };
|
|---|
| 1431 | Object.freeze = function(subject) {
|
|---|
| 1432 | setIntegrityLevel(subject, "frozen");
|
|---|
| 1433 | return subject;
|
|---|
| 1434 | };
|
|---|
| 1435 | Object.isExtensible = Object_isExtensible = function(subject) {
|
|---|
| 1436 | var vHandler = directProxies.get(subject);
|
|---|
| 1437 | if (vHandler !== undefined) {
|
|---|
| 1438 | return vHandler.isExtensible();
|
|---|
| 1439 | } else {
|
|---|
| 1440 | return prim_isExtensible(subject);
|
|---|
| 1441 | }
|
|---|
| 1442 | };
|
|---|
| 1443 | Object.isSealed = Object_isSealed = function(subject) {
|
|---|
| 1444 | return testIntegrityLevel(subject, "sealed");
|
|---|
| 1445 | };
|
|---|
| 1446 | Object.isFrozen = Object_isFrozen = function(subject) {
|
|---|
| 1447 | return testIntegrityLevel(subject, "frozen");
|
|---|
| 1448 | };
|
|---|
| 1449 | Object.getPrototypeOf = Object_getPrototypeOf = function(subject) {
|
|---|
| 1450 | var vHandler = directProxies.get(subject);
|
|---|
| 1451 | if (vHandler !== undefined) {
|
|---|
| 1452 | return vHandler.getPrototypeOf();
|
|---|
| 1453 | } else {
|
|---|
| 1454 | return prim_getPrototypeOf(subject);
|
|---|
| 1455 | }
|
|---|
| 1456 | };
|
|---|
| 1457 |
|
|---|
| 1458 | // patch Object.getOwnPropertyDescriptor to directly call
|
|---|
| 1459 | // the Validator.prototype.getOwnPropertyDescriptor trap
|
|---|
| 1460 | // This is to circumvent an assertion in the built-in Proxy
|
|---|
| 1461 | // trapping mechanism of v8, which disallows that trap to
|
|---|
| 1462 | // return non-configurable property descriptors (as per the
|
|---|
| 1463 | // old Proxy design)
|
|---|
| 1464 | Object.getOwnPropertyDescriptor = function(subject, name) {
|
|---|
| 1465 | var vhandler = directProxies.get(subject);
|
|---|
| 1466 | if (vhandler !== undefined) {
|
|---|
| 1467 | return vhandler.getOwnPropertyDescriptor(name);
|
|---|
| 1468 | } else {
|
|---|
| 1469 | return prim_getOwnPropertyDescriptor(subject, name);
|
|---|
| 1470 | }
|
|---|
| 1471 | };
|
|---|
| 1472 |
|
|---|
| 1473 | // patch Object.defineProperty to directly call
|
|---|
| 1474 | // the Validator.prototype.defineProperty trap
|
|---|
| 1475 | // This is to circumvent two issues with the built-in
|
|---|
| 1476 | // trap mechanism:
|
|---|
| 1477 | // 1) the current tracemonkey implementation of proxies
|
|---|
| 1478 | // auto-completes 'desc', which is not correct. 'desc' should be
|
|---|
| 1479 | // normalized, but not completed. Consider:
|
|---|
| 1480 | // Object.defineProperty(proxy, 'foo', {enumerable:false})
|
|---|
| 1481 | // This trap will receive desc =
|
|---|
| 1482 | // {value:undefined,writable:false,enumerable:false,configurable:false}
|
|---|
| 1483 | // This will also set all other attributes to their default value,
|
|---|
| 1484 | // which is unexpected and different from [[DefineOwnProperty]].
|
|---|
| 1485 | // Bug filed: https://bugzilla.mozilla.org/show_bug.cgi?id=601329
|
|---|
| 1486 | // 2) the current spidermonkey implementation does not
|
|---|
| 1487 | // throw an exception when this trap returns 'false', but instead silently
|
|---|
| 1488 | // ignores the operation (this is regardless of strict-mode)
|
|---|
| 1489 | // 2a) v8 does throw an exception for this case, but includes the rather
|
|---|
| 1490 | // unhelpful error message:
|
|---|
| 1491 | // 'Proxy handler #<Object> returned false from 'defineProperty' trap'
|
|---|
| 1492 | Object.defineProperty = function(subject, name, desc) {
|
|---|
| 1493 | var vhandler = directProxies.get(subject);
|
|---|
| 1494 | if (vhandler !== undefined) {
|
|---|
| 1495 | var normalizedDesc = normalizePropertyDescriptor(desc);
|
|---|
| 1496 | var success = vhandler.defineProperty(name, normalizedDesc);
|
|---|
| 1497 | if (success === false) {
|
|---|
| 1498 | throw new TypeError("can't redefine property '"+name+"'");
|
|---|
| 1499 | }
|
|---|
| 1500 | return subject;
|
|---|
| 1501 | } else {
|
|---|
| 1502 | return prim_defineProperty(subject, name, desc);
|
|---|
| 1503 | }
|
|---|
| 1504 | };
|
|---|
| 1505 |
|
|---|
| 1506 | Object.defineProperties = function(subject, descs) {
|
|---|
| 1507 | var vhandler = directProxies.get(subject);
|
|---|
| 1508 | if (vhandler !== undefined) {
|
|---|
| 1509 | var names = Object.keys(descs);
|
|---|
| 1510 | for (var i = 0; i < names.length; i++) {
|
|---|
| 1511 | var name = names[i];
|
|---|
| 1512 | var normalizedDesc = normalizePropertyDescriptor(descs[name]);
|
|---|
| 1513 | var success = vhandler.defineProperty(name, normalizedDesc);
|
|---|
| 1514 | if (success === false) {
|
|---|
| 1515 | throw new TypeError("can't redefine property '"+name+"'");
|
|---|
| 1516 | }
|
|---|
| 1517 | }
|
|---|
| 1518 | return subject;
|
|---|
| 1519 | } else {
|
|---|
| 1520 | return prim_defineProperties(subject, descs);
|
|---|
| 1521 | }
|
|---|
| 1522 | };
|
|---|
| 1523 |
|
|---|
| 1524 | Object.keys = function(subject) {
|
|---|
| 1525 | var vHandler = directProxies.get(subject);
|
|---|
| 1526 | if (vHandler !== undefined) {
|
|---|
| 1527 | var ownKeys = vHandler.ownKeys();
|
|---|
| 1528 | var result = [];
|
|---|
| 1529 | for (var i = 0; i < ownKeys.length; i++) {
|
|---|
| 1530 | var k = String(ownKeys[i]);
|
|---|
| 1531 | var desc = Object.getOwnPropertyDescriptor(subject, k);
|
|---|
| 1532 | if (desc !== undefined && desc.enumerable === true) {
|
|---|
| 1533 | result.push(k);
|
|---|
| 1534 | }
|
|---|
| 1535 | }
|
|---|
| 1536 | return result;
|
|---|
| 1537 | } else {
|
|---|
| 1538 | return prim_keys(subject);
|
|---|
| 1539 | }
|
|---|
| 1540 | }
|
|---|
| 1541 |
|
|---|
| 1542 | Object.getOwnPropertyNames = Object_getOwnPropertyNames = function(subject) {
|
|---|
| 1543 | var vHandler = directProxies.get(subject);
|
|---|
| 1544 | if (vHandler !== undefined) {
|
|---|
| 1545 | return vHandler.ownKeys();
|
|---|
| 1546 | } else {
|
|---|
| 1547 | return prim_getOwnPropertyNames(subject);
|
|---|
| 1548 | }
|
|---|
| 1549 | }
|
|---|
| 1550 |
|
|---|
| 1551 | // fixes issue #71 (Calling Object.getOwnPropertySymbols() on a Proxy
|
|---|
| 1552 | // throws an error)
|
|---|
| 1553 | if (prim_getOwnPropertySymbols !== undefined) {
|
|---|
| 1554 | Object.getOwnPropertySymbols = function(subject) {
|
|---|
| 1555 | var vHandler = directProxies.get(subject);
|
|---|
| 1556 | if (vHandler !== undefined) {
|
|---|
| 1557 | // as this shim does not support symbols, a Proxy never advertises
|
|---|
| 1558 | // any symbol-valued own properties
|
|---|
| 1559 | return [];
|
|---|
| 1560 | } else {
|
|---|
| 1561 | return prim_getOwnPropertySymbols(subject);
|
|---|
| 1562 | }
|
|---|
| 1563 | };
|
|---|
| 1564 | }
|
|---|
| 1565 |
|
|---|
| 1566 | // fixes issue #72 ('Illegal access' error when using Object.assign)
|
|---|
| 1567 | // Object.assign polyfill based on a polyfill posted on MDN:
|
|---|
| 1568 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/\
|
|---|
| 1569 | // Global_Objects/Object/assign
|
|---|
| 1570 | // Note that this polyfill does not support Symbols, but this Proxy Shim
|
|---|
| 1571 | // does not support Symbols anyway.
|
|---|
| 1572 | if (prim_assign !== undefined) {
|
|---|
| 1573 | Object.assign = function (target) {
|
|---|
| 1574 |
|
|---|
| 1575 | // check if any argument is a proxy object
|
|---|
| 1576 | var noProxies = true;
|
|---|
| 1577 | for (var i = 0; i < arguments.length; i++) {
|
|---|
| 1578 | var vHandler = directProxies.get(arguments[i]);
|
|---|
| 1579 | if (vHandler !== undefined) {
|
|---|
| 1580 | noProxies = false;
|
|---|
| 1581 | break;
|
|---|
| 1582 | }
|
|---|
| 1583 | }
|
|---|
| 1584 | if (noProxies) {
|
|---|
| 1585 | // not a single argument is a proxy, perform built-in algorithm
|
|---|
| 1586 | return prim_assign.apply(Object, arguments);
|
|---|
| 1587 | }
|
|---|
| 1588 |
|
|---|
| 1589 | // there is at least one proxy argument, use the polyfill
|
|---|
| 1590 |
|
|---|
| 1591 | if (target === undefined || target === null) {
|
|---|
| 1592 | throw new TypeError('Cannot convert undefined or null to object');
|
|---|
| 1593 | }
|
|---|
| 1594 |
|
|---|
| 1595 | var output = Object(target);
|
|---|
| 1596 | for (var index = 1; index < arguments.length; index++) {
|
|---|
| 1597 | var source = arguments[index];
|
|---|
| 1598 | if (source !== undefined && source !== null) {
|
|---|
| 1599 | for (var nextKey in source) {
|
|---|
| 1600 | if (source.hasOwnProperty(nextKey)) {
|
|---|
| 1601 | output[nextKey] = source[nextKey];
|
|---|
| 1602 | }
|
|---|
| 1603 | }
|
|---|
| 1604 | }
|
|---|
| 1605 | }
|
|---|
| 1606 | return output;
|
|---|
| 1607 | };
|
|---|
| 1608 | }
|
|---|
| 1609 |
|
|---|
| 1610 | // returns whether an argument is a reference to an object,
|
|---|
| 1611 | // which is legal as a WeakMap key.
|
|---|
| 1612 | function isObject(arg) {
|
|---|
| 1613 | var type = typeof arg;
|
|---|
| 1614 | return (type === 'object' && arg !== null) || (type === 'function');
|
|---|
| 1615 | };
|
|---|
| 1616 |
|
|---|
| 1617 | // a wrapper for WeakMap.get which returns the undefined value
|
|---|
| 1618 | // for keys that are not objects (in which case the underlying
|
|---|
| 1619 | // WeakMap would have thrown a TypeError).
|
|---|
| 1620 | function safeWeakMapGet(map, key) {
|
|---|
| 1621 | return isObject(key) ? map.get(key) : undefined;
|
|---|
| 1622 | };
|
|---|
| 1623 |
|
|---|
| 1624 | // returns a new function of zero arguments that recursively
|
|---|
| 1625 | // unwraps any proxies specified as the |this|-value.
|
|---|
| 1626 | // The primitive is assumed to be a zero-argument method
|
|---|
| 1627 | // that uses its |this|-binding.
|
|---|
| 1628 | function makeUnwrapping0ArgMethod(primitive) {
|
|---|
| 1629 | return function builtin() {
|
|---|
| 1630 | var vHandler = safeWeakMapGet(directProxies, this);
|
|---|
| 1631 | if (vHandler !== undefined) {
|
|---|
| 1632 | return builtin.call(vHandler.target);
|
|---|
| 1633 | } else {
|
|---|
| 1634 | return primitive.call(this);
|
|---|
| 1635 | }
|
|---|
| 1636 | }
|
|---|
| 1637 | };
|
|---|
| 1638 |
|
|---|
| 1639 | // returns a new function of 1 arguments that recursively
|
|---|
| 1640 | // unwraps any proxies specified as the |this|-value.
|
|---|
| 1641 | // The primitive is assumed to be a 1-argument method
|
|---|
| 1642 | // that uses its |this|-binding.
|
|---|
| 1643 | function makeUnwrapping1ArgMethod(primitive) {
|
|---|
| 1644 | return function builtin(arg) {
|
|---|
| 1645 | var vHandler = safeWeakMapGet(directProxies, this);
|
|---|
| 1646 | if (vHandler !== undefined) {
|
|---|
| 1647 | return builtin.call(vHandler.target, arg);
|
|---|
| 1648 | } else {
|
|---|
| 1649 | return primitive.call(this, arg);
|
|---|
| 1650 | }
|
|---|
| 1651 | }
|
|---|
| 1652 | };
|
|---|
| 1653 |
|
|---|
| 1654 | Object.prototype.valueOf =
|
|---|
| 1655 | makeUnwrapping0ArgMethod(Object.prototype.valueOf);
|
|---|
| 1656 | Object.prototype.toString =
|
|---|
| 1657 | makeUnwrapping0ArgMethod(Object.prototype.toString);
|
|---|
| 1658 | Function.prototype.toString =
|
|---|
| 1659 | makeUnwrapping0ArgMethod(Function.prototype.toString);
|
|---|
| 1660 | Date.prototype.toString =
|
|---|
| 1661 | makeUnwrapping0ArgMethod(Date.prototype.toString);
|
|---|
| 1662 |
|
|---|
| 1663 | Object.prototype.isPrototypeOf = function builtin(arg) {
|
|---|
| 1664 | // bugfix thanks to Bill Mark:
|
|---|
| 1665 | // built-in isPrototypeOf does not unwrap proxies used
|
|---|
| 1666 | // as arguments. So, we implement the builtin ourselves,
|
|---|
| 1667 | // based on the ECMAScript 6 spec. Our encoding will
|
|---|
| 1668 | // make sure that if a proxy is used as an argument,
|
|---|
| 1669 | // its getPrototypeOf trap will be called.
|
|---|
| 1670 | while (true) {
|
|---|
| 1671 | var vHandler2 = safeWeakMapGet(directProxies, arg);
|
|---|
| 1672 | if (vHandler2 !== undefined) {
|
|---|
| 1673 | arg = vHandler2.getPrototypeOf();
|
|---|
| 1674 | if (arg === null) {
|
|---|
| 1675 | return false;
|
|---|
| 1676 | } else if (sameValue(arg, this)) {
|
|---|
| 1677 | return true;
|
|---|
| 1678 | }
|
|---|
| 1679 | } else {
|
|---|
| 1680 | return prim_isPrototypeOf.call(this, arg);
|
|---|
| 1681 | }
|
|---|
| 1682 | }
|
|---|
| 1683 | };
|
|---|
| 1684 |
|
|---|
| 1685 | Array.isArray = function(subject) {
|
|---|
| 1686 | var vHandler = safeWeakMapGet(directProxies, subject);
|
|---|
| 1687 | if (vHandler !== undefined) {
|
|---|
| 1688 | return Array.isArray(vHandler.target);
|
|---|
| 1689 | } else {
|
|---|
| 1690 | return prim_isArray(subject);
|
|---|
| 1691 | }
|
|---|
| 1692 | };
|
|---|
| 1693 |
|
|---|
| 1694 | function isProxyArray(arg) {
|
|---|
| 1695 | var vHandler = safeWeakMapGet(directProxies, arg);
|
|---|
| 1696 | if (vHandler !== undefined) {
|
|---|
| 1697 | return Array.isArray(vHandler.target);
|
|---|
| 1698 | }
|
|---|
| 1699 | return false;
|
|---|
| 1700 | }
|
|---|
| 1701 |
|
|---|
| 1702 | // Array.prototype.concat internally tests whether one of its
|
|---|
| 1703 | // arguments is an Array, by checking whether [[Class]] == "Array"
|
|---|
| 1704 | // As such, it will fail to recognize proxies-for-arrays as arrays.
|
|---|
| 1705 | // We patch Array.prototype.concat so that it "unwraps" proxies-for-arrays
|
|---|
| 1706 | // by making a copy. This will trigger the exact same sequence of
|
|---|
| 1707 | // traps on the proxy-for-array as if we would not have unwrapped it.
|
|---|
| 1708 | // See <https://github.com/tvcutsem/harmony-reflect/issues/19> for more.
|
|---|
| 1709 | Array.prototype.concat = function(/*...args*/) {
|
|---|
| 1710 | var length;
|
|---|
| 1711 | for (var i = 0; i < arguments.length; i++) {
|
|---|
| 1712 | if (isProxyArray(arguments[i])) {
|
|---|
| 1713 | length = arguments[i].length;
|
|---|
| 1714 | arguments[i] = Array.prototype.slice.call(arguments[i], 0, length);
|
|---|
| 1715 | }
|
|---|
| 1716 | }
|
|---|
| 1717 | return prim_concat.apply(this, arguments);
|
|---|
| 1718 | };
|
|---|
| 1719 |
|
|---|
| 1720 | // setPrototypeOf support on platforms that support __proto__
|
|---|
| 1721 |
|
|---|
| 1722 | var prim_setPrototypeOf = Object.setPrototypeOf;
|
|---|
| 1723 |
|
|---|
| 1724 | // patch and extract original __proto__ setter
|
|---|
| 1725 | var __proto__setter = (function() {
|
|---|
| 1726 | var protoDesc = prim_getOwnPropertyDescriptor(Object.prototype,'__proto__');
|
|---|
| 1727 | if (protoDesc === undefined ||
|
|---|
| 1728 | typeof protoDesc.set !== "function") {
|
|---|
| 1729 | return function() {
|
|---|
| 1730 | throw new TypeError("setPrototypeOf not supported on this platform");
|
|---|
| 1731 | }
|
|---|
| 1732 | }
|
|---|
| 1733 |
|
|---|
| 1734 | // see if we can actually mutate a prototype with the generic setter
|
|---|
| 1735 | // (e.g. Chrome v28 doesn't allow setting __proto__ via the generic setter)
|
|---|
| 1736 | try {
|
|---|
| 1737 | protoDesc.set.call({},{});
|
|---|
| 1738 | } catch (e) {
|
|---|
| 1739 | return function() {
|
|---|
| 1740 | throw new TypeError("setPrototypeOf not supported on this platform");
|
|---|
| 1741 | }
|
|---|
| 1742 | }
|
|---|
| 1743 |
|
|---|
| 1744 | prim_defineProperty(Object.prototype, '__proto__', {
|
|---|
| 1745 | set: function(newProto) {
|
|---|
| 1746 | return Object.setPrototypeOf(this, Object(newProto));
|
|---|
| 1747 | }
|
|---|
| 1748 | });
|
|---|
| 1749 |
|
|---|
| 1750 | return protoDesc.set;
|
|---|
| 1751 | }());
|
|---|
| 1752 |
|
|---|
| 1753 | Object.setPrototypeOf = function(target, newProto) {
|
|---|
| 1754 | var handler = directProxies.get(target);
|
|---|
| 1755 | if (handler !== undefined) {
|
|---|
| 1756 | if (handler.setPrototypeOf(newProto)) {
|
|---|
| 1757 | return target;
|
|---|
| 1758 | } else {
|
|---|
| 1759 | throw new TypeError("proxy rejected prototype mutation");
|
|---|
| 1760 | }
|
|---|
| 1761 | } else {
|
|---|
| 1762 | if (!Object_isExtensible(target)) {
|
|---|
| 1763 | throw new TypeError("can't set prototype on non-extensible object: " +
|
|---|
| 1764 | target);
|
|---|
| 1765 | }
|
|---|
| 1766 | if (prim_setPrototypeOf)
|
|---|
| 1767 | return prim_setPrototypeOf(target, newProto);
|
|---|
| 1768 |
|
|---|
| 1769 | if (Object(newProto) !== newProto || newProto === null) {
|
|---|
| 1770 | throw new TypeError("Object prototype may only be an Object or null: " +
|
|---|
| 1771 | newProto);
|
|---|
| 1772 | // throw new TypeError("prototype must be an object or null")
|
|---|
| 1773 | }
|
|---|
| 1774 | __proto__setter.call(target, newProto);
|
|---|
| 1775 | return target;
|
|---|
| 1776 | }
|
|---|
| 1777 | }
|
|---|
| 1778 |
|
|---|
| 1779 | Object.prototype.hasOwnProperty = function(name) {
|
|---|
| 1780 | var handler = safeWeakMapGet(directProxies, this);
|
|---|
| 1781 | if (handler !== undefined) {
|
|---|
| 1782 | var desc = handler.getOwnPropertyDescriptor(name);
|
|---|
| 1783 | return desc !== undefined;
|
|---|
| 1784 | } else {
|
|---|
| 1785 | return prim_hasOwnProperty.call(this, name);
|
|---|
| 1786 | }
|
|---|
| 1787 | }
|
|---|
| 1788 |
|
|---|
| 1789 | // ============= Reflection module =============
|
|---|
| 1790 | // see http://wiki.ecmascript.org/doku.php?id=harmony:reflect_api
|
|---|
| 1791 |
|
|---|
| 1792 | var Reflect = {
|
|---|
| 1793 | getOwnPropertyDescriptor: function(target, name) {
|
|---|
| 1794 | return Object.getOwnPropertyDescriptor(target, name);
|
|---|
| 1795 | },
|
|---|
| 1796 | defineProperty: function(target, name, desc) {
|
|---|
| 1797 |
|
|---|
| 1798 | // if target is a proxy, invoke its "defineProperty" trap
|
|---|
| 1799 | var handler = directProxies.get(target);
|
|---|
| 1800 | if (handler !== undefined) {
|
|---|
| 1801 | return handler.defineProperty(target, name, desc);
|
|---|
| 1802 | }
|
|---|
| 1803 |
|
|---|
| 1804 | // Implementation transliterated from [[DefineOwnProperty]]
|
|---|
| 1805 | // see ES5.1 section 8.12.9
|
|---|
| 1806 | // this is the _exact same algorithm_ as the isCompatibleDescriptor
|
|---|
| 1807 | // algorithm defined above, except that at every place it
|
|---|
| 1808 | // returns true, this algorithm actually does define the property.
|
|---|
| 1809 | var current = Object.getOwnPropertyDescriptor(target, name);
|
|---|
| 1810 | var extensible = Object.isExtensible(target);
|
|---|
| 1811 | if (current === undefined && extensible === false) {
|
|---|
| 1812 | return false;
|
|---|
| 1813 | }
|
|---|
| 1814 | if (current === undefined && extensible === true) {
|
|---|
| 1815 | Object.defineProperty(target, name, desc); // should never fail
|
|---|
| 1816 | return true;
|
|---|
| 1817 | }
|
|---|
| 1818 | if (isEmptyDescriptor(desc)) {
|
|---|
| 1819 | return true;
|
|---|
| 1820 | }
|
|---|
| 1821 | if (isEquivalentDescriptor(current, desc)) {
|
|---|
| 1822 | return true;
|
|---|
| 1823 | }
|
|---|
| 1824 | if (current.configurable === false) {
|
|---|
| 1825 | if (desc.configurable === true) {
|
|---|
| 1826 | return false;
|
|---|
| 1827 | }
|
|---|
| 1828 | if ('enumerable' in desc && desc.enumerable !== current.enumerable) {
|
|---|
| 1829 | return false;
|
|---|
| 1830 | }
|
|---|
| 1831 | }
|
|---|
| 1832 | if (isGenericDescriptor(desc)) {
|
|---|
| 1833 | // no further validation necessary
|
|---|
| 1834 | } else if (isDataDescriptor(current) !== isDataDescriptor(desc)) {
|
|---|
| 1835 | if (current.configurable === false) {
|
|---|
| 1836 | return false;
|
|---|
| 1837 | }
|
|---|
| 1838 | } else if (isDataDescriptor(current) && isDataDescriptor(desc)) {
|
|---|
| 1839 | if (current.configurable === false) {
|
|---|
| 1840 | if (current.writable === false && desc.writable === true) {
|
|---|
| 1841 | return false;
|
|---|
| 1842 | }
|
|---|
| 1843 | if (current.writable === false) {
|
|---|
| 1844 | if ('value' in desc && !sameValue(desc.value, current.value)) {
|
|---|
| 1845 | return false;
|
|---|
| 1846 | }
|
|---|
| 1847 | }
|
|---|
| 1848 | }
|
|---|
| 1849 | } else if (isAccessorDescriptor(current) && isAccessorDescriptor(desc)) {
|
|---|
| 1850 | if (current.configurable === false) {
|
|---|
| 1851 | if ('set' in desc && !sameValue(desc.set, current.set)) {
|
|---|
| 1852 | return false;
|
|---|
| 1853 | }
|
|---|
| 1854 | if ('get' in desc && !sameValue(desc.get, current.get)) {
|
|---|
| 1855 | return false;
|
|---|
| 1856 | }
|
|---|
| 1857 | }
|
|---|
| 1858 | }
|
|---|
| 1859 | Object.defineProperty(target, name, desc); // should never fail
|
|---|
| 1860 | return true;
|
|---|
| 1861 | },
|
|---|
| 1862 | deleteProperty: function(target, name) {
|
|---|
| 1863 | var handler = directProxies.get(target);
|
|---|
| 1864 | if (handler !== undefined) {
|
|---|
| 1865 | return handler.delete(name);
|
|---|
| 1866 | }
|
|---|
| 1867 |
|
|---|
| 1868 | var desc = Object.getOwnPropertyDescriptor(target, name);
|
|---|
| 1869 | if (desc === undefined) {
|
|---|
| 1870 | return true;
|
|---|
| 1871 | }
|
|---|
| 1872 | if (desc.configurable === true) {
|
|---|
| 1873 | delete target[name];
|
|---|
| 1874 | return true;
|
|---|
| 1875 | }
|
|---|
| 1876 | return false;
|
|---|
| 1877 | },
|
|---|
| 1878 | getPrototypeOf: function(target) {
|
|---|
| 1879 | return Object.getPrototypeOf(target);
|
|---|
| 1880 | },
|
|---|
| 1881 | setPrototypeOf: function(target, newProto) {
|
|---|
| 1882 |
|
|---|
| 1883 | var handler = directProxies.get(target);
|
|---|
| 1884 | if (handler !== undefined) {
|
|---|
| 1885 | return handler.setPrototypeOf(newProto);
|
|---|
| 1886 | }
|
|---|
| 1887 |
|
|---|
| 1888 | if (Object(newProto) !== newProto || newProto === null) {
|
|---|
| 1889 | throw new TypeError("Object prototype may only be an Object or null: " +
|
|---|
| 1890 | newProto);
|
|---|
| 1891 | }
|
|---|
| 1892 |
|
|---|
| 1893 | if (!Object_isExtensible(target)) {
|
|---|
| 1894 | return false;
|
|---|
| 1895 | }
|
|---|
| 1896 |
|
|---|
| 1897 | var current = Object.getPrototypeOf(target);
|
|---|
| 1898 | if (sameValue(current, newProto)) {
|
|---|
| 1899 | return true;
|
|---|
| 1900 | }
|
|---|
| 1901 |
|
|---|
| 1902 | if (prim_setPrototypeOf) {
|
|---|
| 1903 | try {
|
|---|
| 1904 | prim_setPrototypeOf(target, newProto);
|
|---|
| 1905 | return true;
|
|---|
| 1906 | } catch (e) {
|
|---|
| 1907 | return false;
|
|---|
| 1908 | }
|
|---|
| 1909 | }
|
|---|
| 1910 |
|
|---|
| 1911 | __proto__setter.call(target, newProto);
|
|---|
| 1912 | return true;
|
|---|
| 1913 | },
|
|---|
| 1914 | preventExtensions: function(target) {
|
|---|
| 1915 | var handler = directProxies.get(target);
|
|---|
| 1916 | if (handler !== undefined) {
|
|---|
| 1917 | return handler.preventExtensions();
|
|---|
| 1918 | }
|
|---|
| 1919 | prim_preventExtensions(target);
|
|---|
| 1920 | return true;
|
|---|
| 1921 | },
|
|---|
| 1922 | isExtensible: function(target) {
|
|---|
| 1923 | return Object.isExtensible(target);
|
|---|
| 1924 | },
|
|---|
| 1925 | has: function(target, name) {
|
|---|
| 1926 | return name in target;
|
|---|
| 1927 | },
|
|---|
| 1928 | get: function(target, name, receiver) {
|
|---|
| 1929 | receiver = receiver || target;
|
|---|
| 1930 |
|
|---|
| 1931 | // if target is a proxy, invoke its "get" trap
|
|---|
| 1932 | var handler = directProxies.get(target);
|
|---|
| 1933 | if (handler !== undefined) {
|
|---|
| 1934 | return handler.get(receiver, name);
|
|---|
| 1935 | }
|
|---|
| 1936 |
|
|---|
| 1937 | var desc = Object.getOwnPropertyDescriptor(target, name);
|
|---|
| 1938 | if (desc === undefined) {
|
|---|
| 1939 | var proto = Object.getPrototypeOf(target);
|
|---|
| 1940 | if (proto === null) {
|
|---|
| 1941 | return undefined;
|
|---|
| 1942 | }
|
|---|
| 1943 | return Reflect.get(proto, name, receiver);
|
|---|
| 1944 | }
|
|---|
| 1945 | if (isDataDescriptor(desc)) {
|
|---|
| 1946 | return desc.value;
|
|---|
| 1947 | }
|
|---|
| 1948 | var getter = desc.get;
|
|---|
| 1949 | if (getter === undefined) {
|
|---|
| 1950 | return undefined;
|
|---|
| 1951 | }
|
|---|
| 1952 | return desc.get.call(receiver);
|
|---|
| 1953 | },
|
|---|
| 1954 | // Reflect.set implementation based on latest version of [[SetP]] at
|
|---|
| 1955 | // http://wiki.ecmascript.org/doku.php?id=harmony:proto_climbing_refactoring
|
|---|
| 1956 | set: function(target, name, value, receiver) {
|
|---|
| 1957 | receiver = receiver || target;
|
|---|
| 1958 |
|
|---|
| 1959 | // if target is a proxy, invoke its "set" trap
|
|---|
| 1960 | var handler = directProxies.get(target);
|
|---|
| 1961 | if (handler !== undefined) {
|
|---|
| 1962 | return handler.set(receiver, name, value);
|
|---|
| 1963 | }
|
|---|
| 1964 |
|
|---|
| 1965 | // first, check whether target has a non-writable property
|
|---|
| 1966 | // shadowing name on receiver
|
|---|
| 1967 | var ownDesc = Object.getOwnPropertyDescriptor(target, name);
|
|---|
| 1968 |
|
|---|
| 1969 | if (ownDesc === undefined) {
|
|---|
| 1970 | // name is not defined in target, search target's prototype
|
|---|
| 1971 | var proto = Object.getPrototypeOf(target);
|
|---|
| 1972 |
|
|---|
| 1973 | if (proto !== null) {
|
|---|
| 1974 | // continue the search in target's prototype
|
|---|
| 1975 | return Reflect.set(proto, name, value, receiver);
|
|---|
| 1976 | }
|
|---|
| 1977 |
|
|---|
| 1978 | // Rev16 change. Cf. https://bugs.ecmascript.org/show_bug.cgi?id=1549
|
|---|
| 1979 | // target was the last prototype, now we know that 'name' is not shadowed
|
|---|
| 1980 | // by an existing (accessor or data) property, so we can add the property
|
|---|
| 1981 | // to the initial receiver object
|
|---|
| 1982 | // (this branch will intentionally fall through to the code below)
|
|---|
| 1983 | ownDesc =
|
|---|
| 1984 | { value: undefined,
|
|---|
| 1985 | writable: true,
|
|---|
| 1986 | enumerable: true,
|
|---|
| 1987 | configurable: true };
|
|---|
| 1988 | }
|
|---|
| 1989 |
|
|---|
| 1990 | // we now know that ownDesc !== undefined
|
|---|
| 1991 | if (isAccessorDescriptor(ownDesc)) {
|
|---|
| 1992 | var setter = ownDesc.set;
|
|---|
| 1993 | if (setter === undefined) return false;
|
|---|
| 1994 | setter.call(receiver, value); // assumes Function.prototype.call
|
|---|
| 1995 | return true;
|
|---|
| 1996 | }
|
|---|
| 1997 | // otherwise, isDataDescriptor(ownDesc) must be true
|
|---|
| 1998 | if (ownDesc.writable === false) return false;
|
|---|
| 1999 | // we found an existing writable data property on the prototype chain.
|
|---|
| 2000 | // Now update or add the data property on the receiver, depending on
|
|---|
| 2001 | // whether the receiver already defines the property or not.
|
|---|
| 2002 | var existingDesc = Object.getOwnPropertyDescriptor(receiver, name);
|
|---|
| 2003 | if (existingDesc !== undefined) {
|
|---|
| 2004 | var updateDesc =
|
|---|
| 2005 | { value: value,
|
|---|
| 2006 | // FIXME: it should not be necessary to describe the following
|
|---|
| 2007 | // attributes. Added to circumvent a bug in tracemonkey:
|
|---|
| 2008 | // https://bugzilla.mozilla.org/show_bug.cgi?id=601329
|
|---|
| 2009 | writable: existingDesc.writable,
|
|---|
| 2010 | enumerable: existingDesc.enumerable,
|
|---|
| 2011 | configurable: existingDesc.configurable };
|
|---|
| 2012 | Object.defineProperty(receiver, name, updateDesc);
|
|---|
| 2013 | return true;
|
|---|
| 2014 | } else {
|
|---|
| 2015 | if (!Object.isExtensible(receiver)) return false;
|
|---|
| 2016 | var newDesc =
|
|---|
| 2017 | { value: value,
|
|---|
| 2018 | writable: true,
|
|---|
| 2019 | enumerable: true,
|
|---|
| 2020 | configurable: true };
|
|---|
| 2021 | Object.defineProperty(receiver, name, newDesc);
|
|---|
| 2022 | return true;
|
|---|
| 2023 | }
|
|---|
| 2024 | },
|
|---|
| 2025 | /*invoke: function(target, name, args, receiver) {
|
|---|
| 2026 | receiver = receiver || target;
|
|---|
| 2027 |
|
|---|
| 2028 | var handler = directProxies.get(target);
|
|---|
| 2029 | if (handler !== undefined) {
|
|---|
| 2030 | return handler.invoke(receiver, name, args);
|
|---|
| 2031 | }
|
|---|
| 2032 |
|
|---|
| 2033 | var fun = Reflect.get(target, name, receiver);
|
|---|
| 2034 | return Function.prototype.apply.call(fun, receiver, args);
|
|---|
| 2035 | },*/
|
|---|
| 2036 | enumerate: function(target) {
|
|---|
| 2037 | var handler = directProxies.get(target);
|
|---|
| 2038 | var result;
|
|---|
| 2039 | if (handler !== undefined) {
|
|---|
| 2040 | // handler.enumerate should return an iterator directly, but the
|
|---|
| 2041 | // iterator gets converted to an array for backward-compat reasons,
|
|---|
| 2042 | // so we must re-iterate over the array
|
|---|
| 2043 | result = handler.enumerate(handler.target);
|
|---|
| 2044 | } else {
|
|---|
| 2045 | result = [];
|
|---|
| 2046 | for (var name in target) { result.push(name); };
|
|---|
| 2047 | }
|
|---|
| 2048 | var l = +result.length;
|
|---|
| 2049 | var idx = 0;
|
|---|
| 2050 | return {
|
|---|
| 2051 | next: function() {
|
|---|
| 2052 | if (idx === l) return { done: true };
|
|---|
| 2053 | return { done: false, value: result[idx++] };
|
|---|
| 2054 | }
|
|---|
| 2055 | };
|
|---|
| 2056 | },
|
|---|
| 2057 | // imperfect ownKeys implementation: in ES6, should also include
|
|---|
| 2058 | // symbol-keyed properties.
|
|---|
| 2059 | ownKeys: function(target) {
|
|---|
| 2060 | return Object_getOwnPropertyNames(target);
|
|---|
| 2061 | },
|
|---|
| 2062 | apply: function(target, receiver, args) {
|
|---|
| 2063 | // target.apply(receiver, args)
|
|---|
| 2064 | return Function.prototype.apply.call(target, receiver, args);
|
|---|
| 2065 | },
|
|---|
| 2066 | construct: function(target, args, newTarget) {
|
|---|
| 2067 | // return new target(...args);
|
|---|
| 2068 |
|
|---|
| 2069 | // if target is a proxy, invoke its "construct" trap
|
|---|
| 2070 | var handler = directProxies.get(target);
|
|---|
| 2071 | if (handler !== undefined) {
|
|---|
| 2072 | return handler.construct(handler.target, args, newTarget);
|
|---|
| 2073 | }
|
|---|
| 2074 |
|
|---|
| 2075 | if (typeof target !== "function") {
|
|---|
| 2076 | throw new TypeError("target is not a function: " + target);
|
|---|
| 2077 | }
|
|---|
| 2078 | if (newTarget === undefined || newTarget === target) {
|
|---|
| 2079 | // If newTarget is undefined, then newTarget is set to `target` and
|
|---|
| 2080 | // `Reflect.construct(target, ...args)` becomes equivalent to
|
|---|
| 2081 | // `new target(...args)`
|
|---|
| 2082 | // if `target` is an ES2015 Class constructor, it must be called using
|
|---|
| 2083 | // the `new` operator. Hence we use the new operator on a bound function
|
|---|
| 2084 | // to trigger the [[Construct]] internal method. This technique will work
|
|---|
| 2085 | // for both plain constructor functions and ES2015 classes
|
|---|
| 2086 | return new (Function.prototype.bind.apply(target, [null].concat(args)));
|
|---|
| 2087 | } else {
|
|---|
| 2088 | if (typeof newTarget !== "function") {
|
|---|
| 2089 | throw new TypeError("newTarget is not a function: " + target);
|
|---|
| 2090 | }
|
|---|
| 2091 | // if newTarget is a *different* constructor function, we need to
|
|---|
| 2092 | // emulate [[Construct]] by falling back to [[Call]] with a hand-crafted
|
|---|
| 2093 | // new instance inheriting from newTarget.prototype
|
|---|
| 2094 | // Unfortunately this won't work if target is an ES2015 Constructor
|
|---|
| 2095 | // function, whose [[Call]] method throws an error (it must be invoked
|
|---|
| 2096 | // using the `new` operator)
|
|---|
| 2097 | var proto = newTarget.prototype;
|
|---|
| 2098 | var instance = (Object(proto) === proto) ? Object.create(proto) : {};
|
|---|
| 2099 | var result = Function.prototype.apply.call(target, instance, args);
|
|---|
| 2100 | return Object(result) === result ? result : instance;
|
|---|
| 2101 | }
|
|---|
| 2102 | }
|
|---|
| 2103 | };
|
|---|
| 2104 |
|
|---|
| 2105 | // feature-test whether the Reflect global exists
|
|---|
| 2106 | if (global.Reflect !== undefined) {
|
|---|
| 2107 | // Reflect exists, add/override the shimmed methods
|
|---|
| 2108 | Object.getOwnPropertyNames(Reflect).forEach(function (key) {
|
|---|
| 2109 | global.Reflect[key] = Reflect[key];
|
|---|
| 2110 | });
|
|---|
| 2111 | } else {
|
|---|
| 2112 | // Reflect doesn't exist, define it as the shimmed Reflect object
|
|---|
| 2113 | global.Reflect = Reflect;
|
|---|
| 2114 | }
|
|---|
| 2115 |
|
|---|
| 2116 | // feature-test whether the Proxy global exists, with
|
|---|
| 2117 | // the harmony-era Proxy.create API
|
|---|
| 2118 | if (typeof Proxy !== "undefined" &&
|
|---|
| 2119 | typeof Proxy.create !== "undefined") {
|
|---|
| 2120 |
|
|---|
| 2121 | var primCreate = Proxy.create,
|
|---|
| 2122 | primCreateFunction = Proxy.createFunction;
|
|---|
| 2123 |
|
|---|
| 2124 | var revokedHandler = primCreate({
|
|---|
| 2125 | get: function() { throw new TypeError("proxy is revoked"); }
|
|---|
| 2126 | });
|
|---|
| 2127 |
|
|---|
| 2128 | global.Proxy = function(target, handler) {
|
|---|
| 2129 | // check that target is an Object
|
|---|
| 2130 | if (Object(target) !== target) {
|
|---|
| 2131 | throw new TypeError("Proxy target must be an Object, given "+target);
|
|---|
| 2132 | }
|
|---|
| 2133 | // check that handler is an Object
|
|---|
| 2134 | if (Object(handler) !== handler) {
|
|---|
| 2135 | throw new TypeError("Proxy handler must be an Object, given "+handler);
|
|---|
| 2136 | }
|
|---|
| 2137 |
|
|---|
| 2138 | var vHandler = new Validator(target, handler);
|
|---|
| 2139 | var proxy;
|
|---|
| 2140 | if (typeof target === "function") {
|
|---|
| 2141 | proxy = primCreateFunction(vHandler,
|
|---|
| 2142 | // call trap
|
|---|
| 2143 | function() {
|
|---|
| 2144 | var args = Array.prototype.slice.call(arguments);
|
|---|
| 2145 | return vHandler.apply(target, this, args);
|
|---|
| 2146 | },
|
|---|
| 2147 | // construct trap
|
|---|
| 2148 | function() {
|
|---|
| 2149 | var args = Array.prototype.slice.call(arguments);
|
|---|
| 2150 | return vHandler.construct(target, args);
|
|---|
| 2151 | });
|
|---|
| 2152 | } else {
|
|---|
| 2153 | proxy = primCreate(vHandler, Object.getPrototypeOf(target));
|
|---|
| 2154 | }
|
|---|
| 2155 | directProxies.set(proxy, vHandler);
|
|---|
| 2156 | return proxy;
|
|---|
| 2157 | };
|
|---|
| 2158 |
|
|---|
| 2159 | global.Proxy.revocable = function(target, handler) {
|
|---|
| 2160 | var proxy = new Proxy(target, handler);
|
|---|
| 2161 | var revoke = function() {
|
|---|
| 2162 | var vHandler = directProxies.get(proxy);
|
|---|
| 2163 | if (vHandler !== null) {
|
|---|
| 2164 | vHandler.target = null;
|
|---|
| 2165 | vHandler.handler = revokedHandler;
|
|---|
| 2166 | }
|
|---|
| 2167 | return undefined;
|
|---|
| 2168 | };
|
|---|
| 2169 | return {proxy: proxy, revoke: revoke};
|
|---|
| 2170 | }
|
|---|
| 2171 |
|
|---|
| 2172 | // add the old Proxy.create and Proxy.createFunction methods
|
|---|
| 2173 | // so old code that still depends on the harmony-era Proxy object
|
|---|
| 2174 | // is not broken. Also ensures that multiple versions of this
|
|---|
| 2175 | // library should load fine
|
|---|
| 2176 | global.Proxy.create = primCreate;
|
|---|
| 2177 | global.Proxy.createFunction = primCreateFunction;
|
|---|
| 2178 |
|
|---|
| 2179 | } else {
|
|---|
| 2180 | // Proxy global not defined, or old API not available
|
|---|
| 2181 | if (typeof Proxy === "undefined") {
|
|---|
| 2182 | // Proxy global not defined, add a Proxy function stub
|
|---|
| 2183 | global.Proxy = function(_target, _handler) {
|
|---|
| 2184 | throw new Error("proxies not supported on this platform. On v8/node/iojs, make sure to pass the --harmony_proxies flag");
|
|---|
| 2185 | };
|
|---|
| 2186 | }
|
|---|
| 2187 | // Proxy global defined but old API not available
|
|---|
| 2188 | // presumably Proxy global already supports new API, leave untouched
|
|---|
| 2189 | }
|
|---|
| 2190 |
|
|---|
| 2191 | // for node.js modules, export every property in the Reflect object
|
|---|
| 2192 | // as part of the module interface
|
|---|
| 2193 | if (typeof exports !== 'undefined') {
|
|---|
| 2194 | Object.keys(Reflect).forEach(function (key) {
|
|---|
| 2195 | exports[key] = Reflect[key];
|
|---|
| 2196 | });
|
|---|
| 2197 | }
|
|---|
| 2198 |
|
|---|
| 2199 | // function-as-module pattern
|
|---|
| 2200 | }(typeof exports !== 'undefined' ? global : this)); |
|---|