mirror of
https://github.com/tc39/test262.git
synced 2025-07-05 13:14:38 +02:00
Proxy: setPrototypeOf
This commit is contained in:
parent
d012f5c680
commit
54f3f23f72
@ -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.2
|
||||||
|
description: >
|
||||||
|
Return a boolean trap result if target is extensible.
|
||||||
|
info: >
|
||||||
|
[[SetPrototypeOf]] (V)
|
||||||
|
|
||||||
|
...
|
||||||
|
13. If extensibleTarget is true, return booleanTrapResult.
|
||||||
|
...
|
||||||
|
flags: [onlyStrict]
|
||||||
|
features: [Reflect]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var target = {};
|
||||||
|
var p = new Proxy(target, {
|
||||||
|
setPrototypeOf: function(t, v) {
|
||||||
|
return v.attr;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var result = Reflect.setPrototypeOf(p, { attr: 0 });
|
||||||
|
assert.sameValue(result, false);
|
||||||
|
|
||||||
|
result = Reflect.setPrototypeOf(p, { attr: 1 });
|
||||||
|
assert.sameValue(result, true);
|
36
test/built-ins/Proxy/setPrototypeOf/call-parameters.js
Normal file
36
test/built-ins/Proxy/setPrototypeOf/call-parameters.js
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
// 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.2
|
||||||
|
description: >
|
||||||
|
Trap is called with handler on its context, first parameter is target and
|
||||||
|
second parameter is the given value.
|
||||||
|
info: >
|
||||||
|
[[SetPrototypeOf]] (V)
|
||||||
|
|
||||||
|
...
|
||||||
|
9. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target, V»)).
|
||||||
|
...
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var _handler, _target, _value;
|
||||||
|
var target = {};
|
||||||
|
var val = {foo: 1};
|
||||||
|
var handler = {
|
||||||
|
setPrototypeOf: function(t, v) {
|
||||||
|
_handler = this;
|
||||||
|
_target = t;
|
||||||
|
_value = v;
|
||||||
|
|
||||||
|
Object.setPrototypeOf(t, v);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var p = new Proxy(target, handler);
|
||||||
|
|
||||||
|
Object.setPrototypeOf(p, val);
|
||||||
|
|
||||||
|
assert.sameValue(_handler, handler);
|
||||||
|
assert.sameValue(_target, target);
|
||||||
|
assert.sameValue(_value, val);
|
@ -0,0 +1,38 @@
|
|||||||
|
// 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.2
|
||||||
|
description: >
|
||||||
|
Throws a TypeError exception if boolean trap result is true, target is
|
||||||
|
not extensible, and the given parameter is not the same object as the target
|
||||||
|
prototype.
|
||||||
|
info: >
|
||||||
|
[[SetPrototypeOf]] (V)
|
||||||
|
|
||||||
|
...
|
||||||
|
2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
|
||||||
|
...
|
||||||
|
5. Let target be the value of the [[ProxyTarget]] internal slot of O.
|
||||||
|
6. Let trap be GetMethod(handler, "setPrototypeOf").
|
||||||
|
...
|
||||||
|
9. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target, V»)).
|
||||||
|
14. Let targetProto be target.[[GetPrototypeOf]]().
|
||||||
|
15. ReturnIfAbrupt(targetProto).
|
||||||
|
16. If booleanTrapResult is true and SameValue(V, targetProto) is false,
|
||||||
|
throw a TypeError exception.
|
||||||
|
...
|
||||||
|
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var target = {};
|
||||||
|
var p = new Proxy(target, {
|
||||||
|
setPrototypeOf: function(t, v) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.preventExtensions(target);
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Object.setPrototypeOf(p, {});
|
||||||
|
});
|
@ -0,0 +1,20 @@
|
|||||||
|
// 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.2
|
||||||
|
description: >
|
||||||
|
Return true if boolean trap result is true, target is not extensible, and
|
||||||
|
given parameter is the same as target prototype.
|
||||||
|
features: [Reflect]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var target = Object.create(Array.prototype);
|
||||||
|
var p = new Proxy(target, {
|
||||||
|
setPrototypeOf: function(t, v) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.preventExtensions(target);
|
||||||
|
|
||||||
|
assert(Reflect.setPrototypeOf(p, Array.prototype));
|
@ -0,0 +1,19 @@
|
|||||||
|
// 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.2
|
||||||
|
description: >
|
||||||
|
Return false if boolean trap result is false.
|
||||||
|
features: [Reflect]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var target = [];
|
||||||
|
var p = new Proxy(target, {
|
||||||
|
setPrototypeOf: function(t, v) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.preventExtensions(target);
|
||||||
|
|
||||||
|
assert.sameValue(Reflect.setPrototypeOf(p, {}), false);
|
15
test/built-ins/Proxy/setPrototypeOf/null-handler.js
Normal file
15
test/built-ins/Proxy/setPrototypeOf/null-handler.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// 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.2
|
||||||
|
description: >
|
||||||
|
Throws a TypeError exception if handler is null
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var p = Proxy.revocable({},{});
|
||||||
|
|
||||||
|
p.revoke();
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Object.setPrototypeOf(p.proxy, {});
|
||||||
|
});
|
23
test/built-ins/Proxy/setPrototypeOf/return-is-abrupt.js
Normal file
23
test/built-ins/Proxy/setPrototypeOf/return-is-abrupt.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// 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.2
|
||||||
|
description: >
|
||||||
|
Trap returns abrupt.
|
||||||
|
info: >
|
||||||
|
[[SetPrototypeOf]] (V)
|
||||||
|
|
||||||
|
9. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target, V»)).
|
||||||
|
10. ReturnIfAbrupt(booleanTrapResult).
|
||||||
|
includes: [Test262Error.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var p = new Proxy({}, {
|
||||||
|
setPrototypeOf: function() {
|
||||||
|
throw new Test262Error();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(Test262Error, function() {
|
||||||
|
Object.setPrototypeOf(p, {value: 1});
|
||||||
|
});
|
32
test/built-ins/Proxy/setPrototypeOf/trap-is-not-callable.js
Normal file
32
test/built-ins/Proxy/setPrototypeOf/trap-is-not-callable.js
Normal file
@ -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.2
|
||||||
|
description: >
|
||||||
|
Throws a TypeError exception if trap is not callable.
|
||||||
|
info: >
|
||||||
|
[[SetPrototypeOf]] (V)
|
||||||
|
|
||||||
|
...
|
||||||
|
2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
|
||||||
|
...
|
||||||
|
5. Let target be the value of the [[ProxyTarget]] internal slot of O.
|
||||||
|
6. Let trap be GetMethod(handler, "setPrototypeOf").
|
||||||
|
...
|
||||||
|
7.3.9 GetMethod (O, P)
|
||||||
|
...
|
||||||
|
2. Let func be GetV(O, P).
|
||||||
|
5. If IsCallable(func) is false, throw a TypeError exception.
|
||||||
|
...
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var target = {};
|
||||||
|
var p = new Proxy(target, {
|
||||||
|
setPrototypeOf: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Object.setPrototypeOf(p, {
|
||||||
|
value: 1
|
||||||
|
});
|
||||||
|
});
|
14
test/built-ins/Proxy/setPrototypeOf/trap-is-undefined.js
Normal file
14
test/built-ins/Proxy/setPrototypeOf/trap-is-undefined.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// 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.2
|
||||||
|
description: >
|
||||||
|
Return target.[[SetPrototypeOf]] (V) if trap is undefined.
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var target = {};
|
||||||
|
var p = new Proxy(target, {});
|
||||||
|
|
||||||
|
Object.setPrototypeOf(p, {attr: 1});
|
||||||
|
|
||||||
|
assert.sameValue(target.attr, 1);
|
Loading…
x
Reference in New Issue
Block a user