mirror of
https://github.com/tc39/test262.git
synced 2025-07-19 20:14:56 +02:00
Proxy: ownKeys
This commit is contained in:
parent
28f9d8dbd2
commit
3f214e715f
@ -0,0 +1,32 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
8. Let trapResultArray be Call(trap, handler, «target»).
|
||||
---*/
|
||||
|
||||
var _target, _handler;
|
||||
var target = {
|
||||
foo: 1,
|
||||
bar: 2
|
||||
};
|
||||
|
||||
var handler = {
|
||||
ownKeys: function(t) {
|
||||
_handler = this;
|
||||
_target = t;
|
||||
return Object.getOwnPropertyNames(t);
|
||||
}
|
||||
}
|
||||
var p = new Proxy(target, handler);
|
||||
|
||||
var names = Object.getOwnPropertyNames(p);
|
||||
|
||||
assert.sameValue(names[0], "foo");
|
||||
assert.sameValue(names[1], "bar");
|
||||
assert.sameValue(names.length, 2);
|
||||
assert.sameValue(_handler, handler);
|
||||
assert.sameValue(_target, target);
|
@ -0,0 +1,35 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
8. Let trapResultArray be Call(trap, handler, «target»).
|
||||
features: [Symbol]
|
||||
---*/
|
||||
|
||||
var _target, _handler;
|
||||
var target = {};
|
||||
var a = Symbol('a');
|
||||
var b = Symbol('b');
|
||||
|
||||
target[a] = 1;
|
||||
target[b] = 2;
|
||||
|
||||
var handler = {
|
||||
ownKeys: function(t) {
|
||||
_handler = this;
|
||||
_target = t;
|
||||
return Object.getOwnPropertySymbols(t);
|
||||
}
|
||||
}
|
||||
var p = new Proxy(target, handler);
|
||||
|
||||
var symbols = Object.getOwnPropertySymbols(p);
|
||||
|
||||
assert.sameValue(symbols[0], a);
|
||||
assert.sameValue(symbols[1], b);
|
||||
assert.sameValue(symbols.length, 2);
|
||||
assert.sameValue(_handler, handler);
|
||||
assert.sameValue(_target, target);
|
31
test/built-ins/Proxy/ownKeys/call-parameters-object-keys.js
Normal file
31
test/built-ins/Proxy/ownKeys/call-parameters-object-keys.js
Normal file
@ -0,0 +1,31 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
8. Let trapResultArray be Call(trap, handler, «target»).
|
||||
---*/
|
||||
|
||||
var _target, _handler;
|
||||
var target = {
|
||||
foo: 1,
|
||||
bar: 2
|
||||
};
|
||||
var handler = {
|
||||
ownKeys: function(t) {
|
||||
_handler = this;
|
||||
_target = t;
|
||||
return Object.keys(t);
|
||||
}
|
||||
};
|
||||
var p = new Proxy(target, handler);
|
||||
|
||||
var keys = Object.keys(p);
|
||||
|
||||
assert.sameValue(keys[0], "foo");
|
||||
assert.sameValue(keys[1], "bar");
|
||||
assert.sameValue(keys.length, 2);
|
||||
assert.sameValue(_handler, handler);
|
||||
assert.sameValue(_target, target);
|
@ -0,0 +1,27 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
If target is extensible, return the non-falsy trap result if target doesn't
|
||||
contain any non-configurable keys.
|
||||
info: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
...
|
||||
19. If extensibleTarget is true and targetNonconfigurableKeys is empty, then
|
||||
a. Return trapResult.
|
||||
---*/
|
||||
|
||||
var p = new Proxy({attr: 42}, {
|
||||
ownKeys: function() {
|
||||
return ["foo", "bar"];
|
||||
}
|
||||
});
|
||||
|
||||
var keys = Object.getOwnPropertyNames(p);
|
||||
|
||||
assert.sameValue(keys[0], "foo");
|
||||
assert.sameValue(keys[1], "bar");
|
||||
|
||||
assert.sameValue(keys.length, 2);
|
@ -0,0 +1,34 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
If target is extensible, return the non-falsy trap result if it contains all
|
||||
of target's non-configurable keys.
|
||||
info: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
...
|
||||
22. If extensibleTarget is true, return trapResult.
|
||||
---*/
|
||||
|
||||
var target = {};
|
||||
|
||||
Object.defineProperty(target, "foo", {
|
||||
configurable: false,
|
||||
enumerable: true,
|
||||
value: true
|
||||
});
|
||||
|
||||
var p = new Proxy(target, {
|
||||
ownKeys: function() {
|
||||
return ["foo", "bar"];
|
||||
}
|
||||
});
|
||||
|
||||
var keys = Object.getOwnPropertyNames(p);
|
||||
|
||||
assert.sameValue(keys[0], "foo");
|
||||
assert.sameValue(keys[1], "bar");
|
||||
|
||||
assert.sameValue(keys.length, 2);
|
@ -0,0 +1,32 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
If target is not extensible, the result must contain all the keys of the own
|
||||
properties of the target object.
|
||||
info: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
...
|
||||
23. Repeat, for each key that is an element of targetConfigurableKeys,
|
||||
a. If key is not an element of uncheckedResultKeys, throw a TypeError
|
||||
exception.
|
||||
---*/
|
||||
|
||||
var target = {
|
||||
foo: 1,
|
||||
bar: 2
|
||||
};
|
||||
|
||||
var p = new Proxy(target, {
|
||||
ownKeys: function() {
|
||||
return ["foo"];
|
||||
}
|
||||
});
|
||||
|
||||
Object.preventExtensions(target);
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
Object.keys(p);
|
||||
});
|
@ -0,0 +1,29 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
If target is not extensible, the result can't contain keys names not
|
||||
contained in the target object.
|
||||
info: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
...
|
||||
24. If uncheckedResultKeys is not empty, throw a TypeError exception.
|
||||
---*/
|
||||
|
||||
var target = {
|
||||
foo: 1
|
||||
};
|
||||
|
||||
var p = new Proxy(target, {
|
||||
ownKeys: function() {
|
||||
return ["foo", "bar"];
|
||||
}
|
||||
});
|
||||
|
||||
Object.preventExtensions(target);
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
Object.keys(p);
|
||||
});
|
33
test/built-ins/Proxy/ownKeys/not-extensible-return-keys.js
Normal file
33
test/built-ins/Proxy/ownKeys/not-extensible-return-keys.js
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
If target is not extensible, the result must contain all the keys of the own
|
||||
properties of the target object and no other values
|
||||
info: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
...
|
||||
25. Return trapResult.
|
||||
---*/
|
||||
|
||||
var target = {
|
||||
foo: 1,
|
||||
bar: 2
|
||||
};
|
||||
|
||||
var p = new Proxy(target, {
|
||||
ownKeys: function() {
|
||||
return ["foo", "bar"];
|
||||
}
|
||||
});
|
||||
|
||||
Object.preventExtensions(target);
|
||||
|
||||
var keys = Object.keys(p);
|
||||
|
||||
assert.sameValue(keys[0], "foo");
|
||||
assert.sameValue(keys[1], "bar");
|
||||
|
||||
assert.sameValue(keys.length, 2);
|
17
test/built-ins/Proxy/ownKeys/null-handler.js
Normal file
17
test/built-ins/Proxy/ownKeys/null-handler.js
Normal file
@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
2. If handler is null, throw a TypeError exception.
|
||||
---*/
|
||||
|
||||
var p = Proxy.revocable({}, {});
|
||||
|
||||
p.revoke();
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
Object.keys(p.proxy);
|
||||
});
|
@ -0,0 +1,35 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
The result List must contain the keys of all non-configurable own properties
|
||||
of the target object.
|
||||
info: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
...
|
||||
21. Repeat, for each key that is an element of targetNonconfigurableKeys,
|
||||
a. If key is not an element of uncheckedResultKeys, throw a TypeError
|
||||
exception.
|
||||
---*/
|
||||
|
||||
var target = {
|
||||
foo: 1
|
||||
};
|
||||
|
||||
Object.defineProperty(target, "attr", {
|
||||
configurable: false,
|
||||
enumerable: true,
|
||||
value: true
|
||||
});
|
||||
|
||||
var p = new Proxy(target, {
|
||||
ownKeys: function() {
|
||||
return ["foo"];
|
||||
}
|
||||
});
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
Object.keys(p);
|
||||
});
|
28
test/built-ins/Proxy/ownKeys/return-is-abrupt.js
Normal file
28
test/built-ins/Proxy/ownKeys/return-is-abrupt.js
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
Trap returns abrupt.
|
||||
info: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
...
|
||||
8. Let trapResultArray be Call(trap, handler, «target»).
|
||||
9. Let trapResult be CreateListFromArrayLike(trapResultArray, «String, Symbol»).
|
||||
7.3.17 CreateListFromArrayLike (obj [, elementTypes] )
|
||||
|
||||
1. ReturnIfAbrupt(obj).
|
||||
...
|
||||
includes: [Test262Error.js]
|
||||
---*/
|
||||
|
||||
var p = new Proxy({}, {
|
||||
ownKeys: function() {
|
||||
throw new Test262Error();
|
||||
}
|
||||
});
|
||||
|
||||
assert.throws(Test262Error, function() {
|
||||
Object.keys(p);
|
||||
});
|
@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
If return is not a list object, throw a TypeError exception
|
||||
info: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
8. Let trapResultArray be Call(trap, handler, «target»).
|
||||
9. Let trapResult be CreateListFromArrayLike(trapResultArray, «String,
|
||||
Symbol»).
|
||||
...
|
||||
7.3.17 CreateListFromArrayLike (obj [, elementTypes] )
|
||||
3. If Type(obj) is not Object, throw a TypeError exception.
|
||||
features: [Symbol]
|
||||
---*/
|
||||
|
||||
var target = {};
|
||||
var p = new Proxy(target, {
|
||||
ownKeys: function() {
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
Object.keys(p);
|
||||
});
|
24
test/built-ins/Proxy/ownKeys/trap-is-not-callable.js
Normal file
24
test/built-ins/Proxy/ownKeys/trap-is-not-callable.js
Normal file
@ -0,0 +1,24 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
Trap is not callable.
|
||||
info: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
5. Let trap be GetMethod(handler, "ownKeys").
|
||||
...
|
||||
|
||||
7.3.9 GetMethod (O, P)
|
||||
|
||||
5. If IsCallable(func) is false, throw a TypeError exception.
|
||||
---*/
|
||||
|
||||
var p = new Proxy({attr:1}, {
|
||||
ownKeys: {}
|
||||
});
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
Object.keys(p);
|
||||
});
|
22
test/built-ins/Proxy/ownKeys/trap-is-undefined.js
Normal file
22
test/built-ins/Proxy/ownKeys/trap-is-undefined.js
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||
// This code is governed by the BSD license found in the LICENSE file.
|
||||
/*---
|
||||
es6id: 9.5.12
|
||||
description: >
|
||||
[[OwnPropertyKeys]] ( )
|
||||
|
||||
7. If trap is undefined, then Return target.[[OwnPropertyKeys]]()
|
||||
---*/
|
||||
|
||||
var target = {
|
||||
foo: 1,
|
||||
bar: 2
|
||||
};
|
||||
var p = new Proxy(target, {});
|
||||
|
||||
var keys = Object.keys(p);
|
||||
|
||||
assert.sameValue(keys[0], "foo");
|
||||
assert.sameValue(keys[1], "bar");
|
||||
|
||||
assert.sameValue(keys.length, 2);
|
Loading…
x
Reference in New Issue
Block a user