Skip to content
Snippets Groups Projects
Commit 3f214e71 authored by Leonardo Balter's avatar Leonardo Balter
Browse files

Proxy: ownKeys

parent 28f9d8db
No related branches found
No related tags found
No related merge requests found
Showing
with 407 additions and 0 deletions
// 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);
// 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);
// 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);
// 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);
// 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);
// 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);
});
// 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);
});
// 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);
// 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);
});
// 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);
});
// 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);
});
// 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);
});
// 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);
});
// 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);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment