<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[197544] trunk/Source/JavaScriptCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/197544">197544</a></dd>
<dt>Author</dt> <dd>sbarati@apple.com</dd>
<dt>Date</dt> <dd>2016-03-03 19:43:06 -0800 (Thu, 03 Mar 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>[ES6] Implement Proxy.[[SetPrototypeOf]]
https://bugs.webkit.org/show_bug.cgi?id=154931

Reviewed by Ryosuke Niwa.

This patch is a straight forward implementation of Proxy.[[SetPrototypeOf]]
with respect to section 9.5.2 of the ECMAScript spec.
https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v

* runtime/JSObject.cpp:
(JSC::JSObject::putInlineSlow):
* runtime/ProxyObject.cpp:
(JSC::ProxyObject::put):
(JSC::ProxyObject::getGenericPropertyNames):
(JSC::ProxyObject::performSetPrototype):
(JSC::ProxyObject::setPrototype):
(JSC::ProxyObject::visitChildren):
* runtime/ProxyObject.h:
* tests/es6.yaml:
* tests/stress/proxy-set-prototype-of.js: Added.
(assert):
(throw.new.Error.let.handler.get setPrototypeOf):
(throw.new.Error.set let):
(throw.new.Error.set catch):
(throw.new.Error):
(assert.let.handler.setPrototypeOf):
(assert.set let):
(assert.set catch):
(let.handler.setPrototypeOf):
(set let):
(set catch):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSObjectcpp">trunk/Source/JavaScriptCore/runtime/JSObject.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeProxyObjectcpp">trunk/Source/JavaScriptCore/runtime/ProxyObject.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeProxyObjecth">trunk/Source/JavaScriptCore/runtime/ProxyObject.h</a></li>
<li><a href="#trunkSourceJavaScriptCoretestses6yaml">trunk/Source/JavaScriptCore/tests/es6.yaml</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressproxysetprototypeofjs">trunk/Source/JavaScriptCore/tests/stress/proxy-set-prototype-of.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (197543 => 197544)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-03-04 03:19:11 UTC (rev 197543)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-03-04 03:43:06 UTC (rev 197544)
</span><span class="lines">@@ -1,3 +1,37 @@
</span><ins>+2016-03-03  Saam Barati  &lt;sbarati@apple.com&gt;
+
+        [ES6] Implement Proxy.[[SetPrototypeOf]]
+        https://bugs.webkit.org/show_bug.cgi?id=154931
+
+        Reviewed by Ryosuke Niwa.
+
+        This patch is a straight forward implementation of Proxy.[[SetPrototypeOf]]
+        with respect to section 9.5.2 of the ECMAScript spec.
+        https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v
+
+        * runtime/JSObject.cpp:
+        (JSC::JSObject::putInlineSlow):
+        * runtime/ProxyObject.cpp:
+        (JSC::ProxyObject::put):
+        (JSC::ProxyObject::getGenericPropertyNames):
+        (JSC::ProxyObject::performSetPrototype):
+        (JSC::ProxyObject::setPrototype):
+        (JSC::ProxyObject::visitChildren):
+        * runtime/ProxyObject.h:
+        * tests/es6.yaml:
+        * tests/stress/proxy-set-prototype-of.js: Added.
+        (assert):
+        (throw.new.Error.let.handler.get setPrototypeOf):
+        (throw.new.Error.set let):
+        (throw.new.Error.set catch):
+        (throw.new.Error):
+        (assert.let.handler.setPrototypeOf):
+        (assert.set let):
+        (assert.set catch):
+        (let.handler.setPrototypeOf):
+        (set let):
+        (set catch):
+
</ins><span class="cx"> 2016-03-03  Keith Miller  &lt;keith_miller@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         JSArrayBuffers should be collected less aggressively
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSObjectcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSObject.cpp (197543 => 197544)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSObject.cpp        2016-03-04 03:19:11 UTC (rev 197543)
+++ trunk/Source/JavaScriptCore/runtime/JSObject.cpp        2016-03-04 03:43:06 UTC (rev 197544)
</span><span class="lines">@@ -429,7 +429,7 @@
</span><span class="cx">                 }
</span><span class="cx">             }
</span><span class="cx">         }
</span><del>-        if (obj-&gt;type() == ProxyObjectType) {
</del><ins>+        if (obj-&gt;type() == ProxyObjectType &amp;&amp; propertyName != vm.propertyNames-&gt;underscoreProto) {
</ins><span class="cx">             ProxyObject* proxy = jsCast&lt;ProxyObject*&gt;(obj);
</span><span class="cx">             proxy-&gt;ProxyObject::put(proxy, exec, propertyName, value, slot);
</span><span class="cx">             return;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeProxyObjectcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/ProxyObject.cpp (197543 => 197544)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/ProxyObject.cpp        2016-03-04 03:19:11 UTC (rev 197543)
+++ trunk/Source/JavaScriptCore/runtime/ProxyObject.cpp        2016-03-04 03:43:06 UTC (rev 197544)
</span><span class="lines">@@ -405,6 +405,11 @@
</span><span class="cx"> void ProxyObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot&amp; slot)
</span><span class="cx"> {
</span><span class="cx">     VM&amp; vm = exec-&gt;vm();
</span><ins>+    if (propertyName == vm.propertyNames-&gt;underscoreProto) {
+        Base::put(cell, exec, propertyName, value, slot);
+        return;
+    }
+
</ins><span class="cx">     ProxyObject* thisObject = jsCast&lt;ProxyObject*&gt;(cell);
</span><span class="cx">     auto performDefaultPut = [&amp;] () {
</span><span class="cx">         JSObject* target = jsCast&lt;JSObject*&gt;(thisObject-&gt;target());
</span><span class="lines">@@ -955,6 +960,66 @@
</span><span class="cx">     RELEASE_ASSERT_NOT_REACHED();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool ProxyObject::performSetPrototype(ExecState* exec, JSValue prototype, bool shouldThrowIfCantSet)
+{
+    ASSERT(prototype.isObject() || prototype.isNull());
+
+    VM&amp; vm = exec-&gt;vm();
+
+    JSValue handlerValue = this-&gt;handler();
+    if (handlerValue.isNull()) {
+        throwVMTypeError(exec, ASCIILiteral(&quot;Proxy 'handler' is null. It should be an Object.&quot;));
+        return false;
+    }
+
+    JSObject* handler = jsCast&lt;JSObject*&gt;(handlerValue);
+    CallData callData;
+    CallType callType;
+    JSValue setPrototypeOfMethod = handler-&gt;getMethod(exec, callData, callType, makeIdentifier(vm, &quot;setPrototypeOf&quot;), ASCIILiteral(&quot;'setPrototypeOf' property of a Proxy's handler should be callable.&quot;));
+    if (vm.exception())
+        return false;
+
+    JSObject* target = this-&gt;target();
+    if (setPrototypeOfMethod.isUndefined())
+        return target-&gt;setPrototype(vm, exec, prototype, shouldThrowIfCantSet);
+
+    MarkedArgumentBuffer arguments;
+    arguments.append(target);
+    arguments.append(prototype);
+    JSValue trapResult = call(exec, setPrototypeOfMethod, callType, callData, handler, arguments);
+    if (vm.exception())
+        return false;
+
+    bool trapResultAsBool = trapResult.toBoolean(exec);
+    if (vm.exception())
+        return false;
+    
+    if (!trapResultAsBool) {
+        if (shouldThrowIfCantSet)
+            throwVMTypeError(exec, ASCIILiteral(&quot;Proxy 'setPrototypeOf' returned false indicating it could not set the prototype value. The operation was expected to succeed.&quot;));
+        return false;
+    }
+
+    bool targetIsExtensible = target-&gt;isExtensible(exec);
+    if (vm.exception())
+        return false;
+    if (targetIsExtensible)
+        return true;
+
+    JSValue targetPrototype = target-&gt;prototype();
+    if (!sameValue(exec, prototype, targetPrototype)) {
+        throwVMTypeError(exec, ASCIILiteral(&quot;Proxy 'setPrototypeOf' trap returned true when its target is non-extensible and the new prototype value is not the same as the current prototype value. It should have returned false.&quot;));
+        return false;
+    }
+
+    return true;
+}
+
+bool ProxyObject::setPrototype(JSObject* object, ExecState* exec, JSValue prototype, bool shouldThrowIfCantSet)
+{
+    return jsCast&lt;ProxyObject*&gt;(object)-&gt;performSetPrototype(exec, prototype, shouldThrowIfCantSet);
+}
+
</ins><span class="cx"> void ProxyObject::visitChildren(JSCell* cell, SlotVisitor&amp; visitor)
</span><span class="cx"> {
</span><span class="cx">     ProxyObject* thisObject = jsCast&lt;ProxyObject*&gt;(cell);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeProxyObjecth"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/ProxyObject.h (197543 => 197544)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/ProxyObject.h        2016-03-04 03:19:11 UTC (rev 197543)
+++ trunk/Source/JavaScriptCore/runtime/ProxyObject.h        2016-03-04 03:43:06 UTC (rev 197544)
</span><span class="lines">@@ -83,6 +83,7 @@
</span><span class="cx">     static NO_RETURN_DUE_TO_CRASH void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&amp;, EnumerationMode);
</span><span class="cx">     static NO_RETURN_DUE_TO_CRASH void getStructurePropertyNames(JSObject*, ExecState*, PropertyNameArray&amp;, EnumerationMode);
</span><span class="cx">     static NO_RETURN_DUE_TO_CRASH void getGenericPropertyNames(JSObject*, ExecState*, PropertyNameArray&amp;, EnumerationMode);
</span><ins>+    static bool setPrototype(JSObject*, ExecState*, JSValue prototype, bool shouldThrowIfCantSet);
</ins><span class="cx">     static void visitChildren(JSCell*, SlotVisitor&amp;);
</span><span class="cx"> 
</span><span class="cx">     bool getOwnPropertySlotCommon(ExecState*, PropertyName, PropertySlot&amp;);
</span><span class="lines">@@ -96,6 +97,7 @@
</span><span class="cx">     bool performIsExtensible(ExecState*);
</span><span class="cx">     bool performDefineOwnProperty(ExecState*, PropertyName, const PropertyDescriptor&amp;, bool shouldThrow);
</span><span class="cx">     void performGetOwnPropertyNames(ExecState*, PropertyNameArray&amp;, EnumerationMode);
</span><ins>+    bool performSetPrototype(ExecState*, JSValue prototype, bool shouldThrowIfCantSet);
</ins><span class="cx"> 
</span><span class="cx">     WriteBarrier&lt;JSObject&gt; m_target;
</span><span class="cx">     WriteBarrier&lt;Unknown&gt; m_handler;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestses6yaml"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/tests/es6.yaml (197543 => 197544)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/es6.yaml        2016-03-04 03:19:11 UTC (rev 197543)
+++ trunk/Source/JavaScriptCore/tests/es6.yaml        2016-03-04 03:43:06 UTC (rev 197544)
</span><span class="lines">@@ -719,9 +719,9 @@
</span><span class="cx"> - path: es6/Array_is_subclassable_Array.prototype.concat.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/Array_is_subclassable_Array.prototype.filter.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/Array_is_subclassable_Array.prototype.map.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/Array_is_subclassable_Array.prototype.slice.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/Array_is_subclassable_Array.prototype.splice.js
</span><span class="lines">@@ -1069,7 +1069,7 @@
</span><span class="cx"> - path: es6/Proxy_set_handler_instances_of_proxies.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/Proxy_setPrototypeOf_handler.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/Reflect_Reflect.construct.js
</span><span class="cx">   cmd: runES6 :fail
</span><span class="cx"> - path: es6/Reflect_Reflect.construct_creates_instance_from_newTarget_argument.js
</span><span class="lines">@@ -1197,9 +1197,9 @@
</span><span class="cx"> - path: es6/well-known_symbols_Symbol.species_Array.prototype.concat.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/well-known_symbols_Symbol.species_Array.prototype.filter.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/well-known_symbols_Symbol.species_Array.prototype.map.js
</span><del>-  cmd: runES6 :fail
</del><ins>+  cmd: runES6 :normal
</ins><span class="cx"> - path: es6/well-known_symbols_Symbol.species_Array.prototype.slice.js
</span><span class="cx">   cmd: runES6 :normal
</span><span class="cx"> - path: es6/well-known_symbols_Symbol.species_Array.prototype.splice.js
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressproxysetprototypeofjs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/proxy-set-prototype-of.js (0 => 197544)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/proxy-set-prototype-of.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/proxy-set-prototype-of.js        2016-03-04 03:43:06 UTC (rev 197544)
</span><span class="lines">@@ -0,0 +1,416 @@
</span><ins>+function assert(b) {
+    if (!b)
+        throw new Error(&quot;Bad assertion!&quot;);
+}
+
+{
+    let target = {};
+    let error = null;
+    let handler = {
+        get setPrototypeOf() {
+            error = new Error;
+            throw error;
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let setters = [
+            () =&gt; Reflect.setPrototypeOf(proxy, {}),
+            () =&gt; Object.setPrototypeOf(proxy, {}),
+            () =&gt; proxy.__proto__ = {},
+        ];
+        for (let set of setters) {
+            let threw = false;
+            try {
+                set();
+            } catch(e) {
+                assert(e === error);
+                threw = true;
+            }
+            assert(threw);
+        }
+    }
+}
+
+{
+    let target = {};
+    let error = null;
+    let handler = {
+        setPrototypeOf: function() {
+            error = new Error;
+            throw error;
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let setters = [
+            () =&gt; Reflect.setPrototypeOf(proxy, {}),
+            () =&gt; Object.setPrototypeOf(proxy, {}),
+            () =&gt; proxy.__proto__ = {},
+        ];
+        for (let set of setters) {
+            let threw = false;
+            try {
+                set();
+            } catch(e) {
+                assert(e === error);
+                threw = true;
+            }
+            assert(threw);
+        }
+    }
+}
+
+{
+    let target = {};
+    let error = null;
+    let handler = {
+        setPrototypeOf: 25
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let setters = [
+            () =&gt; Reflect.setPrototypeOf(proxy, {}),
+            () =&gt; Object.setPrototypeOf(proxy, {}),
+            () =&gt; proxy.__proto__ = {},
+        ];
+        for (let set of setters) {
+            let threw = false;
+            try {
+                set();
+            } catch(e) {
+                assert(e.toString() === &quot;TypeError: 'setPrototypeOf' property of a Proxy's handler should be callable.&quot;);
+                threw = true;
+            }
+            assert(threw);
+        }
+    }
+}
+
+{
+    let target = {};
+    target.__proto__ = null;
+    let handler = {
+        setPrototypeOf: function(theTarget, value) {
+            return true;
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let setters = [
+            () =&gt; Reflect.setPrototypeOf(proxy, {}),
+            () =&gt; Object.setPrototypeOf(proxy, {}),
+            () =&gt; proxy.__proto__ = {},
+        ];
+        for (let set of setters) {
+            let result = set();
+            assert(result);
+            assert(Reflect.getPrototypeOf(target) === null);
+            // FIXME: when we implement Proxy.[[GetPrototypeOf]] this should work.
+            //assert(Reflect.getPrototypeOf(proxy) === null);
+            //assert(proxy.__proto__ === null);
+        }
+    }
+}
+
+{
+    let target = {};
+    target.__proto__ = null;
+    let handler = {
+        setPrototypeOf: function(theTarget, value) {
+            assert(theTarget === target);
+            return Reflect.setPrototypeOf(theTarget, value);
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let obj = {};
+        let setters = [
+            () =&gt; Reflect.setPrototypeOf(proxy, obj),
+            () =&gt; Object.setPrototypeOf(proxy, obj),
+            () =&gt; proxy.__proto__ = obj,
+        ];
+        for (let set of setters) {
+            let result = set();
+            assert(result);
+            assert(Reflect.getPrototypeOf(target) === obj);
+            // FIXME: when we implement Proxy.[[GetPrototypeOf]] this should work.
+            //assert(Reflect.getPrototypeOf(proxy) === obj);
+            //assert(proxy.__proto__ === obj);
+        }
+    }
+}
+
+{
+    let target = {};
+    target.__proto__ = null;
+    Reflect.preventExtensions(target);
+    let called = false;
+    let handler = {
+        setPrototypeOf: function(theTarget, value) {
+            called = true;
+            assert(theTarget === target);
+            assert(value !== null);
+            Reflect.setPrototypeOf(theTarget, value);
+            return true;
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let obj = {};
+        let setters = [
+            () =&gt; Reflect.setPrototypeOf(proxy, obj),
+            () =&gt; Object.setPrototypeOf(proxy, obj),
+            () =&gt; proxy.__proto__ = obj,
+        ];
+        for (let set of setters) {
+            let threw = false;
+            try {
+                set();
+            } catch(e) {
+                threw = true;
+                assert(called);
+                called = false;
+                assert(e.toString() === &quot;TypeError: Proxy 'setPrototypeOf' trap returned true when its target is non-extensible and the new prototype value is not the same as the current prototype value. It should have returned false.&quot;);
+            }
+
+            assert(threw);
+            assert(Reflect.getPrototypeOf(target) === null);
+            // FIXME: when we implement Proxy.[[GetPrototypeOf]] this should work.
+            //assert(Reflect.getPrototypeOf(proxy) === null);
+            //assert(proxy.__proto__ === null);
+        }
+    }
+}
+
+{
+    let target = {};
+    target.__proto__ = null;
+    Reflect.preventExtensions(target);
+    let called = false;
+    let handler = {
+        setPrototypeOf: function(theTarget, value) {
+            called = true;
+            assert(theTarget === target);
+            assert(value === null);
+            return Reflect.setPrototypeOf(theTarget, value);
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let proto = null;
+        let setters = [
+            [() =&gt; Reflect.setPrototypeOf(proxy, null), true],
+            [() =&gt; Object.setPrototypeOf(proxy, null), proxy],
+            [() =&gt; proxy.__proto__ = null, null]
+        ];
+        for (let [set, expectedResult] of setters) {
+            let result = set();
+            assert(result === expectedResult);
+            assert(called);
+            called = false;
+            assert(Reflect.getPrototypeOf(target) === null);
+            // FIXME: when we implement Proxy.[[GetPrototypeOf]] this should work.
+            //assert(Reflect.getPrototypeOf(proxy) === null);
+            //assert(proxy.__proto__ === null);
+        }
+    }
+}
+
+{
+    let target = {};
+    let obj = {};
+    target.__proto__ = obj;
+    Reflect.preventExtensions(target);
+    let called = false;
+    let handler = {
+        setPrototypeOf: function(theTarget, value) {
+            called = true;
+            assert(theTarget === target);
+            assert(value === obj);
+            return Reflect.setPrototypeOf(theTarget, value);
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let proto = null;
+        let setters = [
+            [() =&gt; Reflect.setPrototypeOf(proxy, obj), true],
+            [() =&gt; Object.setPrototypeOf(proxy, obj), proxy],
+            [() =&gt; proxy.__proto__ = obj, obj]
+        ];
+        for (let [set, expectedResult] of setters) {
+            let result = set();
+            assert(result === expectedResult);
+            assert(called);
+            called = false;
+            assert(Reflect.getPrototypeOf(target) === obj);
+            // FIXME: when we implement Proxy.[[GetPrototypeOf]] this should work.
+            //assert(Reflect.getPrototypeOf(proxy) === null);
+            //assert(proxy.__proto__ === null);
+        }
+    }
+}
+
+{
+    let target = {};
+    target.__proto__ = null;
+    Reflect.preventExtensions(target);
+    let handler = {
+        setPrototypeOf: function(theTarget, value) {
+            return Reflect.setPrototypeOf(theTarget, value);
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let obj = {};
+        let threw = false;
+        try {
+            Object.setPrototypeOf(proxy, obj);
+        } catch(e) {
+            threw = true;
+            assert(e.toString() === &quot;TypeError: Proxy 'setPrototypeOf' returned false indicating it could not set the prototype value. The operation was expected to succeed.&quot;);
+        }
+
+        assert(threw);
+        assert(Reflect.getPrototypeOf(target) === null);
+        // FIXME: when we implement Proxy.[[GetPrototypeOf]] this should work.
+        //assert(Reflect.getPrototypeOf(proxy) === null);
+        //assert(proxy.__proto__ === null);
+    }
+}
+
+{
+    let target = {};
+    target.__proto__ = null;
+    Reflect.preventExtensions(target);
+    let handler = {
+        setPrototypeOf: function(theTarget, value) {
+            return Reflect.setPrototypeOf(theTarget, value);
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let obj = {};
+        let threw = false;
+        try {
+            proxy.__proto__ = obj;
+        } catch(e) {
+            threw = true;
+            assert(e.toString() === &quot;TypeError: Proxy 'setPrototypeOf' returned false indicating it could not set the prototype value. The operation was expected to succeed.&quot;);
+        }
+
+        assert(threw);
+        assert(Reflect.getPrototypeOf(target) === null);
+        // FIXME: when we implement Proxy.[[GetPrototypeOf]] this should work.
+        //assert(Reflect.getPrototypeOf(proxy) === null);
+        //assert(proxy.__proto__ === null);
+    }
+}
+
+{
+    let target = {};
+    target.__proto__ = null;
+    Reflect.preventExtensions(target);
+
+    let called = false;
+    let handler = {
+        setPrototypeOf: function(theTarget, value) {
+            called = true;
+            return Reflect.setPrototypeOf(theTarget, value);
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let result = Reflect.setPrototypeOf(proxy, {});
+        assert(!result);
+        assert(Reflect.getPrototypeOf(target) === null);
+
+        assert(called);
+        called = false;
+        // FIXME: when we implement Proxy.[[GetPrototypeOf]] this should work.
+        //assert(Reflect.getPrototypeOf(proxy) === null);
+        //assert(proxy.__proto__ === null);
+    }
+}
+
+{
+    let target = {};
+    target.__proto__ = null;
+    Reflect.preventExtensions(target);
+
+    let called = false;
+    let handler = {
+        setPrototypeOf: function(theTarget, value) {
+            called = true;
+            return Reflect.setPrototypeOf(theTarget, value);
+        }
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let result = Reflect.setPrototypeOf(proxy, null);
+        assert(result);
+        assert(Reflect.getPrototypeOf(target) === null);
+        assert(called);
+        called = false;
+
+        // FIXME: when we implement Proxy.[[GetPrototypeOf]] this should work.
+        //assert(Reflect.getPrototypeOf(proxy) === null);
+        //assert(proxy.__proto__ === null);
+    }
+}
+
+{
+    let target = {};
+    let handler = {
+        setPrototypeOf: null
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let obj = {};
+        let result = Reflect.setPrototypeOf(proxy, obj);
+        assert(result);
+        assert(Reflect.getPrototypeOf(target) === obj);
+    }
+}
+
+{
+    let target = {};
+    let handler = {
+        setPrototypeOf: undefined
+    };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let obj = {};
+        let result = Reflect.setPrototypeOf(proxy, obj);
+        assert(result);
+        assert(Reflect.getPrototypeOf(target) === obj);
+    }
+}
+
+{
+    let target = {};
+    let handler = { };
+    
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i &lt; 500; i++) {
+        let obj = {};
+        let result = Reflect.setPrototypeOf(proxy, obj);
+        assert(result);
+        assert(Reflect.getPrototypeOf(target) === obj);
+    }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>