| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- // Verifies the order of getOwnPropertyDescriptor trap called for ownKeys
- function test1() {
- var obj = {};
- var proxy = new Proxy(obj, {
- getOwnPropertyDescriptor: function (target, property) {
- print('getOwnPropertyDescriptor on proxy : ' + property.toString());
- return { configurable: true, enumerable: true, value: 10 };
- },
- ownKeys: function (target) {
- print('ownKeys for proxy');
- return ["prop0", "prop1", Symbol("prop2"), Symbol("prop5")];
- }
- });
- var proxy2 = new Proxy(proxy, {
- getOwnPropertyDescriptor: function (target, property) {
- print('getOwnPropertyDescriptor on proxy2 : ' + property.toString());
- return { configurable: true, enumerable: true, value: 10 };
- },
- ownKeys: function (target) {
- print('ownKeys for proxy2');
- return ["prop2", "prop3", Symbol("prop4"), Symbol("prop5")];
- }
- });
- print('***Testing Object.getOwnPropertyNames()');
- print(Object.getOwnPropertyNames(proxy2));
- print('***Testing Object.keys()');
- print(Object.keys(proxy2));
- print('***Testing Object.getOwnPropertySymbols()');
- print(Object.getOwnPropertySymbols(proxy2).length);
- print('***Testing Object.freeze()');
- try{
- Object.freeze(proxy2);
- print('Object.freeze should fail because underlying OwnPropertyKeys should fail since target becomes non-extensible');
- } catch (e) {
- if (!(e instanceof TypeError)) {
- $ERROR('incorrect instanceof Error' + e);
- }
- }
- }
- // Verifies that we don't call GetPropertyDescriptor of target for Object.keys unnecessarily if we throw TypeERROR before
- function test2() {
- var obj = {};
- Object.defineProperty(obj, "a", { value: 5, configurable: false });
- var proxy = new Proxy(obj, {
- getOwnPropertyDescriptor: function (target, property) {
- print('getOwnPropertyDescriptor on proxy : ' + property.toString());
- return Object.getOwnPropertyDescriptor(target, property);
- },
- ownKeys: function (target) {
- print('ownKeys for proxy');
- return ["a", "prop0", "prop1", Symbol("prop2"), Symbol("prop5")];
- }
- });
- var proxy2 = new Proxy(proxy, {
- getOwnPropertyDescriptor: function (target, property) {
- print('getOwnPropertyDescriptor on proxy2 : ' + property.toString());
- return Object.getOwnPropertyDescriptor(target, property);
- },
- ownKeys: function (target) {
- print('ownKeys for proxy2');
- return ["prop2", "prop3", Symbol("prop4"), Symbol("prop5")];
- }
- });
- print('***Testing Object.keys()');
- try{
- print(Object.keys(proxy2));
- print('Should throw TypeError because ownKeys doesnt return non-configurable key.');
- } catch (e) {
- if (!(e instanceof TypeError)) {
- print('incorrect instanceof Error');
- }
- }
- }
- function test3() {
- var obj = {};
- var count = 0;
- var proxy = new Proxy(obj, {
- get: function (target, property, receiver) {
- print('get on proxy : ' + property.toString());
- return count++ * 5;
- },
- getOwnPropertyDescriptor: function (target, property) {
- print('getOwnPropertyDescriptor on proxy : ' + property.toString());
- return { configurable: true, enumerable: true, value: 10 };
- },
- ownKeys: function (target) {
- print('ownKeys for proxy');
- return ["prop0", "prop1", Symbol("prop2"), Symbol("prop5")];
- }
- });
- var proxy2 = new Proxy(proxy, {
- get: function (target, property, receiver) {
- print('get on proxy2 : ' + property.toString());
- return Reflect.get(target, property, receiver);
- },
- getOwnPropertyDescriptor: function (target, property) {
- print('getOwnPropertyDescriptor on proxy2 : ' + property.toString());
- return { configurable: true, enumerable: true, value: 10 };
- },
- ownKeys: function (target) {
- print('ownKeys for proxy2');
- return ["prop2", "prop3", Symbol("prop4"), Symbol("prop5")];
- }
- });
- print('***Testing Object.assign()');
- var answer = Object.assign(obj, null, proxy, proxy2);
- var symbols = Object.getOwnPropertySymbols(answer);
- var names = Object.getOwnPropertyNames(answer);
- print("PropertyNames returned : ");
- for (i = 0; i < names.length; i++)
- {
- print(names[i].toString())
- }
- print("PropertySymbols returned : ");
- for (i = 0; i < symbols.length; i++)
- {
- print(symbols[i].toString())
- }
- }
- function test4() {
- print("***Traps whose value is null are ignored");
- function getProxy(trap, result, obj) {
- const proxy = new Proxy(obj, {
- [trap]: () => {
- print(`"${trap}" called`);
- return result;
- }
- });
- return new Proxy(proxy, {
- [trap]: null
- });
- }
- Object.getPrototypeOf(getProxy("getPrototypeOf", {}, {}));
- Object.setPrototypeOf(getProxy("setPrototypeOf", true, {}), {});
- Object.isExtensible(getProxy("isExtensible", true, {}));
- Object.preventExtensions(getProxy("preventExtensions", false, {}));
- Object.getOwnPropertyDescriptor(getProxy("getOwnPropertyDescriptor", undefined, {}));
- Object.defineProperty(getProxy("defineProperty", true, {}), "prop", { value: 0 });
- "prop" in getProxy("has", true, {});
- getProxy("get", 0, {}).prop;
- getProxy("set", true, {}).prop = 0;
- delete getProxy("deleteProperty", true, {}).prop;
- Object.keys(getProxy("ownKeys", [], {}));
- getProxy("apply", 0, function () {})();
- new (getProxy("construct", {}, function () {}));
- }
- test1();
- test2();
- test3();
- test4();
|