From 54f3f23f72e99ec769caeffbba5abefac369af3e Mon Sep 17 00:00:00 2001
From: Leonardo Balter <leonardo.balter@gmail.com>
Date: Tue, 2 Jun 2015 17:54:29 -0400
Subject: [PATCH] Proxy: setPrototypeOf

---
 .../boolean-trap-result-extensible-target.js  | 28 ++++++++++++++
 .../Proxy/setPrototypeOf/call-parameters.js   | 36 ++++++++++++++++++
 ...nsible-target-not-same-target-prototype.js | 38 +++++++++++++++++++
 ...extensible-target-same-target-prototype.js | 20 ++++++++++
 ...t-extensible-trap-is-false-return-false.js | 19 ++++++++++
 .../Proxy/setPrototypeOf/null-handler.js      | 15 ++++++++
 .../Proxy/setPrototypeOf/return-is-abrupt.js  | 23 +++++++++++
 .../setPrototypeOf/trap-is-not-callable.js    | 32 ++++++++++++++++
 .../Proxy/setPrototypeOf/trap-is-undefined.js | 14 +++++++
 9 files changed, 225 insertions(+)
 create mode 100644 test/built-ins/Proxy/setPrototypeOf/boolean-trap-result-extensible-target.js
 create mode 100644 test/built-ins/Proxy/setPrototypeOf/call-parameters.js
 create mode 100644 test/built-ins/Proxy/setPrototypeOf/not-extensible-target-not-same-target-prototype.js
 create mode 100644 test/built-ins/Proxy/setPrototypeOf/not-extensible-target-same-target-prototype.js
 create mode 100644 test/built-ins/Proxy/setPrototypeOf/not-extensible-trap-is-false-return-false.js
 create mode 100644 test/built-ins/Proxy/setPrototypeOf/null-handler.js
 create mode 100644 test/built-ins/Proxy/setPrototypeOf/return-is-abrupt.js
 create mode 100644 test/built-ins/Proxy/setPrototypeOf/trap-is-not-callable.js
 create mode 100644 test/built-ins/Proxy/setPrototypeOf/trap-is-undefined.js

diff --git a/test/built-ins/Proxy/setPrototypeOf/boolean-trap-result-extensible-target.js b/test/built-ins/Proxy/setPrototypeOf/boolean-trap-result-extensible-target.js
new file mode 100644
index 0000000000..6365345c48
--- /dev/null
+++ b/test/built-ins/Proxy/setPrototypeOf/boolean-trap-result-extensible-target.js
@@ -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);
diff --git a/test/built-ins/Proxy/setPrototypeOf/call-parameters.js b/test/built-ins/Proxy/setPrototypeOf/call-parameters.js
new file mode 100644
index 0000000000..e62aa54869
--- /dev/null
+++ b/test/built-ins/Proxy/setPrototypeOf/call-parameters.js
@@ -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);
diff --git a/test/built-ins/Proxy/setPrototypeOf/not-extensible-target-not-same-target-prototype.js b/test/built-ins/Proxy/setPrototypeOf/not-extensible-target-not-same-target-prototype.js
new file mode 100644
index 0000000000..8d015b8703
--- /dev/null
+++ b/test/built-ins/Proxy/setPrototypeOf/not-extensible-target-not-same-target-prototype.js
@@ -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, {});
+});
diff --git a/test/built-ins/Proxy/setPrototypeOf/not-extensible-target-same-target-prototype.js b/test/built-ins/Proxy/setPrototypeOf/not-extensible-target-same-target-prototype.js
new file mode 100644
index 0000000000..8af42c56a3
--- /dev/null
+++ b/test/built-ins/Proxy/setPrototypeOf/not-extensible-target-same-target-prototype.js
@@ -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));
diff --git a/test/built-ins/Proxy/setPrototypeOf/not-extensible-trap-is-false-return-false.js b/test/built-ins/Proxy/setPrototypeOf/not-extensible-trap-is-false-return-false.js
new file mode 100644
index 0000000000..59a2633879
--- /dev/null
+++ b/test/built-ins/Proxy/setPrototypeOf/not-extensible-trap-is-false-return-false.js
@@ -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);
diff --git a/test/built-ins/Proxy/setPrototypeOf/null-handler.js b/test/built-ins/Proxy/setPrototypeOf/null-handler.js
new file mode 100644
index 0000000000..4d4a7a6b68
--- /dev/null
+++ b/test/built-ins/Proxy/setPrototypeOf/null-handler.js
@@ -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, {});
+});
diff --git a/test/built-ins/Proxy/setPrototypeOf/return-is-abrupt.js b/test/built-ins/Proxy/setPrototypeOf/return-is-abrupt.js
new file mode 100644
index 0000000000..9675507370
--- /dev/null
+++ b/test/built-ins/Proxy/setPrototypeOf/return-is-abrupt.js
@@ -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});
+});
diff --git a/test/built-ins/Proxy/setPrototypeOf/trap-is-not-callable.js b/test/built-ins/Proxy/setPrototypeOf/trap-is-not-callable.js
new file mode 100644
index 0000000000..a6e77ed123
--- /dev/null
+++ b/test/built-ins/Proxy/setPrototypeOf/trap-is-not-callable.js
@@ -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
+    });
+});
diff --git a/test/built-ins/Proxy/setPrototypeOf/trap-is-undefined.js b/test/built-ins/Proxy/setPrototypeOf/trap-is-undefined.js
new file mode 100644
index 0000000000..1dc0ef324f
--- /dev/null
+++ b/test/built-ins/Proxy/setPrototypeOf/trap-is-undefined.js
@@ -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);
-- 
GitLab