mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2026-02-07 17:16:25 +00:00
Property key query for Proxy objects always returned all keys even if no symbols were requested symbols were present in the resulting array. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.usz@partner.samsung.com
226 lines
5.2 KiB
JavaScript
226 lines
5.2 KiB
JavaScript
// Copyright JS Foundation and other contributors, http://js.foundation
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
function array_check(result_array, expected_array) {
|
|
assert(result_array instanceof Array);
|
|
assert(result_array.length === expected_array.length);
|
|
for (var idx = 0; idx < expected_array.length; idx++) {
|
|
assert(result_array[idx] === expected_array[idx]);
|
|
}
|
|
}
|
|
|
|
var target = {};
|
|
var handler = { ownKeys (target) {
|
|
throw 42;
|
|
}};
|
|
|
|
var proxy = new Proxy(target, handler);
|
|
|
|
try {
|
|
// Array.prototype.sort
|
|
Array.prototype.sort.call(proxy);
|
|
assert(false);
|
|
} catch (e) {
|
|
assert(e === 42);
|
|
}
|
|
|
|
try {
|
|
// 19.1.2.14.4
|
|
Object.keys(proxy);
|
|
assert(false);
|
|
} catch (e) {
|
|
assert(e === 42);
|
|
}
|
|
|
|
try {
|
|
// 19.1.2.7.1
|
|
Object.getOwnPropertyNames(proxy);
|
|
assert(false);
|
|
} catch (e) {
|
|
assert(e === 42);
|
|
}
|
|
|
|
try {
|
|
// 19.1.2.8.1
|
|
Object.getOwnPropertySymbols(proxy);
|
|
assert(false);
|
|
} catch (e) {
|
|
assert(e === 42);
|
|
}
|
|
|
|
// test basic functionality
|
|
var symA = Symbol("smA");
|
|
var symB = Symbol("smB");
|
|
var target = { prop1: "prop1", prop2: "prop2"};
|
|
target[symB] = "s3";
|
|
var handler = {
|
|
ownKeys: function(target) {
|
|
return ["foo", "bar", symA];
|
|
}
|
|
}
|
|
|
|
var proxy = new Proxy(target, handler);
|
|
|
|
array_check(Reflect.ownKeys(proxy), ["foo", "bar", symA]);
|
|
array_check(Object.getOwnPropertyNames(proxy), ["foo", "bar"]);
|
|
array_check(Object.keys(proxy), ["foo", "bar"]);
|
|
array_check(Object.getOwnPropertySymbols(proxy), [symA]);
|
|
|
|
handler.ownKeys = function(target) {return Object.getOwnPropertyNames(target);};
|
|
|
|
array_check(Reflect.ownKeys(proxy), ["prop1", "prop2"]);
|
|
array_check(Object.getOwnPropertyNames(proxy), ["prop1", "prop2"]);
|
|
array_check(Object.keys(proxy), ["prop1", "prop2"]);
|
|
array_check(Object.getOwnPropertySymbols(proxy), []);
|
|
|
|
// test with no trap
|
|
var target = { prop1: "prop1", prop2: "prop2"};
|
|
var handler = {};
|
|
var proxy = new Proxy(target, handler);
|
|
|
|
assert(JSON.stringify(Object.getOwnPropertyNames(proxy)) === '["prop1","prop2"]');
|
|
|
|
// test wtih "undefined" trap
|
|
var target = { prop1: "prop1", prop2: "prop2"};
|
|
var handler = {ownKeys: null};
|
|
var proxy = new Proxy(target, handler);
|
|
|
|
assert(JSON.stringify(Object.getOwnPropertyNames(proxy)) === '["prop1","prop2"]');
|
|
|
|
// test with invalid trap
|
|
var target = { prop1: "prop1", prop2: "prop2"};
|
|
var handler = {ownKeys: 42};
|
|
var proxy = new Proxy(target, handler);
|
|
|
|
try {
|
|
Object.getOwnPropertyNames(proxy);
|
|
assert(false);
|
|
} catch (e) {
|
|
assert(e instanceof TypeError);
|
|
}
|
|
|
|
// test when CreateListFromArrayLike called on non-object
|
|
var target = { prop1: "prop1", prop2: "prop2"};
|
|
var handler = {
|
|
ownKeys: function(target) {
|
|
return "foo";
|
|
}
|
|
};
|
|
|
|
var proxy = new Proxy(target, handler);
|
|
|
|
try {
|
|
Object.getOwnPropertyNames(proxy);
|
|
assert(false);
|
|
} catch (e) {
|
|
assert(e instanceof TypeError);
|
|
}
|
|
|
|
// test with invalid property key
|
|
var target = {};
|
|
var handler = {
|
|
ownKeys: function(target) {
|
|
return [5];
|
|
}
|
|
};
|
|
|
|
var proxy = new Proxy(target, handler);
|
|
|
|
try {
|
|
Object.getOwnPropertyNames(proxy);
|
|
assert(false);
|
|
} catch (e) {
|
|
assert(e instanceof TypeError);
|
|
}
|
|
|
|
// test with duplicated keys
|
|
var target = { prop1: "prop1", prop2: "prop2"};
|
|
var handler = {
|
|
ownKeys: function(target) {
|
|
return ["a", "a", "a"];
|
|
}
|
|
};
|
|
|
|
var proxy = new Proxy(target, handler);
|
|
|
|
assert(JSON.stringify(Object.getOwnPropertyNames(proxy)) === '["a","a","a"]');
|
|
|
|
// test with lots of keys
|
|
var keyslist = [];
|
|
|
|
var handler = {
|
|
ownKeys: function(target) {
|
|
for (var idx = 0; idx < 30; idx++) {
|
|
keyslist.push("K" + idx);
|
|
}
|
|
return keyslist;
|
|
}
|
|
};
|
|
|
|
var proxy = new Proxy(target, handler);
|
|
assert(JSON.stringify(Object.getOwnPropertyNames(proxy)) === JSON.stringify(keyslist));
|
|
|
|
// test when invariants gets violated
|
|
var target = {
|
|
"target_one": 1
|
|
};
|
|
Object.defineProperty(target, "nonconf", {value: 1, configurable: false});
|
|
|
|
var keys = ["foo"];
|
|
|
|
var handler = {
|
|
ownKeys: function(target) {
|
|
return keys;
|
|
}
|
|
};
|
|
|
|
var proxy = new Proxy(target, handler);
|
|
|
|
try {
|
|
Object.getOwnPropertyNames(proxy);
|
|
assert(false);
|
|
} catch (e) {
|
|
assert(e instanceof TypeError);
|
|
}
|
|
|
|
keys = ["nonconf"];
|
|
assert(JSON.stringify(Object.getOwnPropertyNames(proxy)) === '["nonconf"]');
|
|
|
|
Object.preventExtensions(target);
|
|
keys = ["foo", "nonconf"];
|
|
|
|
try {
|
|
Object.getOwnPropertyNames(proxy);
|
|
assert(false);
|
|
} catch (e) {
|
|
assert(e instanceof TypeError);
|
|
}
|
|
|
|
keys = ["target_one", "nonconf"];
|
|
|
|
assert(JSON.stringify(Object.getOwnPropertyNames(proxy)) === '["target_one","nonconf"]');
|
|
|
|
keys = ["target_one", "nonconf", "foo"];
|
|
|
|
try {
|
|
Object.getOwnPropertyNames(proxy);
|
|
assert(false);
|
|
} catch (e) {
|
|
assert(e instanceof TypeError);
|
|
}
|