Title: [196868] trunk/Source/_javascript_Core
Revision
196868
Author
[email protected]
Date
2016-02-20 15:51:33 -0800 (Sat, 20 Feb 2016)

Log Message

[ES6] Implement Proxy.[[Construct]]
https://bugs.webkit.org/show_bug.cgi?id=154440

Reviewed by Oliver Hunt.

This patch is mostly an implementation of
Proxy.[[Construct]] with respect to section 9.5.13
of the ECMAScript spec.
https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget

This patch also changes op_create_this to accept new.target's
that aren't JSFunctions. This is necessary implementing Proxy.[[Construct]] 
because we might construct a JSFunction with a new.target being
a Proxy. This will also be needed when we implement Reflect.construct.

* dfg/DFGOperations.cpp:
* dfg/DFGSpeculativeJIT32_64.cpp:
(JSC::DFG::SpeculativeJIT::compile):
* dfg/DFGSpeculativeJIT64.cpp:
(JSC::DFG::SpeculativeJIT::compile):
* jit/JITOpcodes.cpp:
(JSC::JIT::emit_op_create_this):
(JSC::JIT::emitSlow_op_create_this):
* jit/JITOpcodes32_64.cpp:
(JSC::JIT::emit_op_create_this):
(JSC::JIT::emitSlow_op_create_this):
* llint/LLIntData.cpp:
(JSC::LLInt::Data::performAssertions):
* llint/LowLevelInterpreter.asm:
* llint/LowLevelInterpreter32_64.asm:
* llint/LowLevelInterpreter64.asm:
* runtime/CommonSlowPaths.cpp:
(JSC::SLOW_PATH_DECL):
* runtime/ProxyObject.cpp:
(JSC::ProxyObject::finishCreation):
(JSC::ProxyObject::visitChildren):
(JSC::performProxyConstruct):
(JSC::ProxyObject::getConstructData):
* runtime/ProxyObject.h:
* tests/es6.yaml:
* tests/stress/proxy-construct.js: Added.
(assert):
(throw.new.Error.let.target):
(throw.new.Error):
(assert.let.target):
(assert.let.handler.get construct):
(let.target):
(let.handler.construct):
(i.catch):
(assert.let.handler.construct):
(assert.let.construct):
(assert.else.assert.let.target):
(assert.else.assert.let.construct):
(assert.else.assert):
(new.proxy.let.target):
(new.proxy.let.construct):
(new.proxy):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (196867 => 196868)


--- trunk/Source/_javascript_Core/ChangeLog	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-02-20 23:51:33 UTC (rev 196868)
@@ -1,3 +1,63 @@
+2016-02-19  Saam Barati  <[email protected]>
+
+        [ES6] Implement Proxy.[[Construct]]
+        https://bugs.webkit.org/show_bug.cgi?id=154440
+
+        Reviewed by Oliver Hunt.
+
+        This patch is mostly an implementation of
+        Proxy.[[Construct]] with respect to section 9.5.13
+        of the ECMAScript spec.
+        https://tc39.github.io/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget
+
+        This patch also changes op_create_this to accept new.target's
+        that aren't JSFunctions. This is necessary implementing Proxy.[[Construct]] 
+        because we might construct a JSFunction with a new.target being
+        a Proxy. This will also be needed when we implement Reflect.construct.
+
+        * dfg/DFGOperations.cpp:
+        * dfg/DFGSpeculativeJIT32_64.cpp:
+        (JSC::DFG::SpeculativeJIT::compile):
+        * dfg/DFGSpeculativeJIT64.cpp:
+        (JSC::DFG::SpeculativeJIT::compile):
+        * jit/JITOpcodes.cpp:
+        (JSC::JIT::emit_op_create_this):
+        (JSC::JIT::emitSlow_op_create_this):
+        * jit/JITOpcodes32_64.cpp:
+        (JSC::JIT::emit_op_create_this):
+        (JSC::JIT::emitSlow_op_create_this):
+        * llint/LLIntData.cpp:
+        (JSC::LLInt::Data::performAssertions):
+        * llint/LowLevelInterpreter.asm:
+        * llint/LowLevelInterpreter32_64.asm:
+        * llint/LowLevelInterpreter64.asm:
+        * runtime/CommonSlowPaths.cpp:
+        (JSC::SLOW_PATH_DECL):
+        * runtime/ProxyObject.cpp:
+        (JSC::ProxyObject::finishCreation):
+        (JSC::ProxyObject::visitChildren):
+        (JSC::performProxyConstruct):
+        (JSC::ProxyObject::getConstructData):
+        * runtime/ProxyObject.h:
+        * tests/es6.yaml:
+        * tests/stress/proxy-construct.js: Added.
+        (assert):
+        (throw.new.Error.let.target):
+        (throw.new.Error):
+        (assert.let.target):
+        (assert.let.handler.get construct):
+        (let.target):
+        (let.handler.construct):
+        (i.catch):
+        (assert.let.handler.construct):
+        (assert.let.construct):
+        (assert.else.assert.let.target):
+        (assert.else.assert.let.construct):
+        (assert.else.assert):
+        (new.proxy.let.target):
+        (new.proxy.let.construct):
+        (new.proxy):
+
 2016-02-19  Sukolsak Sakshuwong  <[email protected]>
 
         [INTL] Implement Number Format Functions

Modified: trunk/Source/_javascript_Core/dfg/DFGOperations.cpp (196867 => 196868)


--- trunk/Source/_javascript_Core/dfg/DFGOperations.cpp	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/dfg/DFGOperations.cpp	2016-02-20 23:51:33 UTC (rev 196868)
@@ -164,8 +164,10 @@
 {
     VM& vm = exec->vm();
     NativeCallFrameTracer tracer(&vm, exec);
+    if (constructor->type() == JSFunctionType)
+        return constructEmptyObject(exec, jsCast<JSFunction*>(constructor)->rareData(exec, inlineCapacity)->objectAllocationProfile()->structure());
 
-    return constructEmptyObject(exec, jsCast<JSFunction*>(constructor)->rareData(exec, inlineCapacity)->objectAllocationProfile()->structure());
+    return constructEmptyObject(exec);
 }
 
 EncodedJSValue JIT_OPERATION operationValueBitAnd(ExecState* exec, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2)

Modified: trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT32_64.cpp (196867 => 196868)


--- trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT32_64.cpp	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT32_64.cpp	2016-02-20 23:51:33 UTC (rev 196868)
@@ -3707,6 +3707,8 @@
         
         MacroAssembler::JumpList slowPath;
 
+        slowPath.append(m_jit.branch8(JITCompiler::NotEqual,
+            JITCompiler::Address(calleeGPR, JSCell::typeInfoTypeOffset()), TrustedImm32(JSFunctionType)));
         m_jit.loadPtr(JITCompiler::Address(calleeGPR, JSFunction::offsetOfRareData()), rareDataGPR);
         slowPath.append(m_jit.branchTestPtr(MacroAssembler::Zero, rareDataGPR));
         m_jit.loadPtr(JITCompiler::Address(rareDataGPR, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfile::offsetOfAllocator()), allocatorGPR);

Modified: trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT64.cpp (196867 => 196868)


--- trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT64.cpp	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT64.cpp	2016-02-20 23:51:33 UTC (rev 196868)
@@ -3740,6 +3740,8 @@
 
         MacroAssembler::JumpList slowPath;
 
+        slowPath.append(m_jit.branch8(JITCompiler::NotEqual,
+            JITCompiler::Address(calleeGPR, JSCell::typeInfoTypeOffset()), TrustedImm32(JSFunctionType)));
         m_jit.loadPtr(JITCompiler::Address(calleeGPR, JSFunction::offsetOfRareData()), rareDataGPR);
         slowPath.append(m_jit.branchTestPtr(MacroAssembler::Zero, rareDataGPR));
         m_jit.loadPtr(JITCompiler::Address(rareDataGPR, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfile::offsetOfAllocator()), allocatorGPR);

Modified: trunk/Source/_javascript_Core/jit/JITOpcodes.cpp (196867 => 196868)


--- trunk/Source/_javascript_Core/jit/JITOpcodes.cpp	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/jit/JITOpcodes.cpp	2016-02-20 23:51:33 UTC (rev 196868)
@@ -712,6 +712,7 @@
     RegisterID scratchReg = regT3;
 
     emitGetVirtualRegister(callee, calleeReg);
+    addSlowCase(branch8(NotEqual, Address(calleeReg, JSCell::typeInfoTypeOffset()), TrustedImm32(JSFunctionType)));
     loadPtr(Address(calleeReg, JSFunction::offsetOfRareData()), rareDataReg);
     addSlowCase(branchTestPtr(Zero, rareDataReg));
     loadPtr(Address(rareDataReg, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfile::offsetOfAllocator()), allocatorReg);
@@ -729,6 +730,7 @@
 
 void JIT::emitSlow_op_create_this(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
 {
+    linkSlowCase(iter); // Callee::m_type != JSFunctionType.
     linkSlowCase(iter); // doesn't have rare data
     linkSlowCase(iter); // doesn't have an allocation profile
     linkSlowCase(iter); // allocation failed

Modified: trunk/Source/_javascript_Core/jit/JITOpcodes32_64.cpp (196867 => 196868)


--- trunk/Source/_javascript_Core/jit/JITOpcodes32_64.cpp	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/jit/JITOpcodes32_64.cpp	2016-02-20 23:51:33 UTC (rev 196868)
@@ -970,6 +970,7 @@
     RegisterID scratchReg = regT3;
 
     emitLoadPayload(callee, calleeReg);
+    addSlowCase(branch8(NotEqual, Address(calleeReg, JSCell::typeInfoTypeOffset()), TrustedImm32(JSFunctionType)));
     loadPtr(Address(calleeReg, JSFunction::offsetOfRareData()), rareDataReg);
     addSlowCase(branchTestPtr(Zero, rareDataReg));
     loadPtr(Address(rareDataReg, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfile::offsetOfAllocator()), allocatorReg);
@@ -987,6 +988,7 @@
 
 void JIT::emitSlow_op_create_this(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
 {
+    linkSlowCase(iter); // Callee::m_type != JSFunctionType.
     linkSlowCase(iter); // doesn't have rare data
     linkSlowCase(iter); // doesn't have an allocation profile
     linkSlowCase(iter); // allocation failed

Modified: trunk/Source/_javascript_Core/llint/LLIntData.cpp (196867 => 196868)


--- trunk/Source/_javascript_Core/llint/LLIntData.cpp	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/llint/LLIntData.cpp	2016-02-20 23:51:33 UTC (rev 196868)
@@ -146,6 +146,7 @@
     STATIC_ASSERT(SymbolType == 7);
     STATIC_ASSERT(ObjectType == 21);
     STATIC_ASSERT(FinalObjectType == 22);
+    STATIC_ASSERT(JSFunctionType == 24);
     STATIC_ASSERT(MasqueradesAsUndefined == 1);
     STATIC_ASSERT(ImplementsDefaultHasInstance == 2);
     STATIC_ASSERT(FirstConstantRegisterIndex == 0x40000000);

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter.asm (196867 => 196868)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter.asm	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter.asm	2016-02-20 23:51:33 UTC (rev 196868)
@@ -330,6 +330,7 @@
 const SymbolType = 7
 const ObjectType = 21
 const FinalObjectType = 22
+const JSFunctionType = 24
 
 # Type flags constants.
 const MasqueradesAsUndefined = 1

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm (196867 => 196868)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm	2016-02-20 23:51:33 UTC (rev 196868)
@@ -691,6 +691,7 @@
     traceExecution()
     loadi 8[PC], t0
     loadp PayloadOffset[cfr, t0, 8], t0
+    bbneq JSCell::m_type[t0], JSFunctionType, .opCreateThisSlow
     loadp JSFunction::m_rareData[t0], t5
     btpz t5, .opCreateThisSlow
     loadp FunctionRareData::m_objectAllocationProfile + ObjectAllocationProfile::m_allocator[t5], t1

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm (196867 => 196868)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm	2016-02-20 23:51:33 UTC (rev 196868)
@@ -597,6 +597,7 @@
     traceExecution()
     loadisFromInstruction(2, t0)
     loadp [cfr, t0, 8], t0
+    bbneq JSCell::m_type[t0], JSFunctionType, .opCreateThisSlow
     loadp JSFunction::m_rareData[t0], t3
     btpz t3, .opCreateThisSlow
     loadp FunctionRareData::m_objectAllocationProfile + ObjectAllocationProfile::m_allocator[t3], t1

Modified: trunk/Source/_javascript_Core/runtime/CommonSlowPaths.cpp (196867 => 196868)


--- trunk/Source/_javascript_Core/runtime/CommonSlowPaths.cpp	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/runtime/CommonSlowPaths.cpp	2016-02-20 23:51:33 UTC (rev 196868)
@@ -221,17 +221,22 @@
 SLOW_PATH_DECL(slow_path_create_this)
 {
     BEGIN();
-    JSFunction* constructor = jsCast<JSFunction*>(OP(2).jsValue().asCell());
+    JSObject* result;
+    JSCell* constructorAsCell = OP(2).jsValue().asCell();
+    if (constructorAsCell->type() == JSFunctionType) {
+        JSFunction* constructor = jsCast<JSFunction*>(constructorAsCell);
+        auto& cacheWriteBarrier = pc[4].u.jsCell;
+        if (!cacheWriteBarrier)
+            cacheWriteBarrier.set(exec->vm(), exec->codeBlock(), constructor);
+        else if (cacheWriteBarrier.unvalidatedGet() != JSCell::seenMultipleCalleeObjects() && cacheWriteBarrier.get() != constructor)
+            cacheWriteBarrier.setWithoutWriteBarrier(JSCell::seenMultipleCalleeObjects());
 
-    auto& cacheWriteBarrier = pc[4].u.jsCell;
-    if (!cacheWriteBarrier)
-        cacheWriteBarrier.set(exec->vm(), exec->codeBlock(), constructor);
-    else if (cacheWriteBarrier.unvalidatedGet() != JSCell::seenMultipleCalleeObjects() && cacheWriteBarrier.get() != constructor)
-        cacheWriteBarrier.setWithoutWriteBarrier(JSCell::seenMultipleCalleeObjects());
-
-    size_t inlineCapacity = pc[3].u.operand;
-    Structure* structure = constructor->rareData(exec, inlineCapacity)->objectAllocationProfile()->structure();
-    RETURN(constructEmptyObject(exec, structure));
+        size_t inlineCapacity = pc[3].u.operand;
+        Structure* structure = constructor->rareData(exec, inlineCapacity)->objectAllocationProfile()->structure();
+        result = constructEmptyObject(exec, structure);
+    } else
+        result = constructEmptyObject(exec);
+    RETURN(result);
 }
 
 SLOW_PATH_DECL(slow_path_to_this)

Modified: trunk/Source/_javascript_Core/runtime/ProxyObject.cpp (196867 => 196868)


--- trunk/Source/_javascript_Core/runtime/ProxyObject.cpp	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/runtime/ProxyObject.cpp	2016-02-20 23:51:33 UTC (rev 196868)
@@ -65,10 +65,14 @@
         return;
     }
 
-    CallData ignored;
     JSObject* targetAsObject = jsCast<JSObject*>(target);
-    m_isCallable = targetAsObject->methodTable(vm)->getCallData(targetAsObject, ignored) != CallTypeNone;
 
+    CallData ignoredCallData;
+    m_isCallable = targetAsObject->methodTable(vm)->getCallData(targetAsObject, ignoredCallData) != CallTypeNone;
+
+    ConstructData ignoredConstructData;
+    m_isConstructible = jsCast<JSObject*>(target)->methodTable(vm)->getConstructData(jsCast<JSObject*>(target), ignoredConstructData) != ConstructTypeNone;
+
     m_target.set(vm, this, targetAsObject);
     m_handler.set(vm, this, handler);
 }
@@ -351,4 +355,54 @@
     visitor.append(&thisObject->m_handler);
 }
 
+static EncodedJSValue JSC_HOST_CALL performProxyConstruct(ExecState* exec)
+{
+    VM& vm = exec->vm();
+    ProxyObject* proxy = jsCast<ProxyObject*>(exec->callee());
+    JSValue handlerValue = proxy->handler();
+    if (handlerValue.isNull())
+        return throwVMTypeError(exec, ASCIILiteral("Proxy 'handler' is null. It should be an Object."));
+
+    JSObject* handler = jsCast<JSObject*>(handlerValue);
+    CallData callData;
+    CallType callType;
+    JSValue constructMethod = handler->getMethod(exec, callData, callType, makeIdentifier(vm, "construct"), ASCIILiteral("'construct' property of a Proxy's handler should be constructible."));
+    if (exec->hadException())
+        return JSValue::encode(jsUndefined());
+    JSObject* target = proxy->target();
+    if (constructMethod.isUndefined()) {
+        ConstructData constructData;
+        ConstructType constructType = target->methodTable(vm)->getConstructData(target, constructData);
+        RELEASE_ASSERT(constructType != ConstructTypeNone);
+        return JSValue::encode(construct(exec, target, constructType, constructData, ArgList(exec), exec->newTarget()));
+    }
+
+    JSArray* argArray = constructArray(exec, static_cast<ArrayAllocationProfile*>(nullptr), ArgList(exec));
+    if (exec->hadException())
+        return JSValue::encode(jsUndefined());
+    MarkedArgumentBuffer arguments;
+    arguments.append(target);
+    arguments.append(argArray);
+    arguments.append(exec->newTarget());
+    JSValue result = call(exec, constructMethod, callType, callData, handler, arguments);
+    if (exec->hadException())
+        return JSValue::encode(jsUndefined());
+    if (!result.isObject())
+        return throwVMTypeError(exec, ASCIILiteral("Result from Proxy handler's 'construct' method should be an object."));
+    return JSValue::encode(result);
+}
+
+ConstructType ProxyObject::getConstructData(JSCell* cell, ConstructData& constructData)
+{
+    ProxyObject* proxy = jsCast<ProxyObject*>(cell);
+    if (!proxy->m_isConstructible) {
+        constructData.js.functionExecutable = nullptr;
+        constructData.js.scope = nullptr;
+        return ConstructTypeNone;
+    }
+
+    constructData.native.function = performProxyConstruct;
+    return ConstructTypeHost;
+}
+
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/runtime/ProxyObject.h (196867 => 196868)


--- trunk/Source/_javascript_Core/runtime/ProxyObject.h	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/runtime/ProxyObject.h	2016-02-20 23:51:33 UTC (rev 196868)
@@ -62,6 +62,7 @@
     static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
     static bool getOwnPropertySlotByIndex(JSObject*, ExecState*, unsigned propertyName, PropertySlot&);
     static CallType getCallData(JSCell*, CallData&);
+    static ConstructType getConstructData(JSCell*, ConstructData&);
     static void visitChildren(JSCell*, SlotVisitor&);
 
     bool getOwnPropertySlotCommon(ExecState*, PropertyName, PropertySlot&);
@@ -71,6 +72,7 @@
     WriteBarrier<JSObject> m_target;
     WriteBarrier<Unknown> m_handler;
     bool m_isCallable : 1;
+    bool m_isConstructible : 1;
 };
 
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/tests/es6.yaml (196867 => 196868)


--- trunk/Source/_javascript_Core/tests/es6.yaml	2016-02-20 22:47:51 UTC (rev 196867)
+++ trunk/Source/_javascript_Core/tests/es6.yaml	2016-02-20 23:51:33 UTC (rev 196868)
@@ -911,7 +911,7 @@
 - path: es6/Proxy_Array.isArray_support.js
   cmd: runES6 :fail
 - path: es6/Proxy_construct_handler.js
-  cmd: runES6 :fail
+  cmd: runES6 :normal
 - path: es6/Proxy_constructor_requires_new.js
   cmd: runES6 :normal
 - path: es6/Proxy_defineProperty_handler.js
@@ -967,7 +967,7 @@
 - path: es6/Proxy_internal_get_calls_ClassDefinitionEvaluation.js
   cmd: runES6 :normal
 - path: es6/Proxy_internal_get_calls_CreateDynamicFunction.js
-  cmd: runES6 :fail
+  cmd: runES6 :normal
 - path: es6/Proxy_internal_get_calls_CreateListFromArrayLike.js
   cmd: runES6 :normal
 - path: es6/Proxy_internal_get_calls_Date.prototype.toJSON.js

Added: trunk/Source/_javascript_Core/tests/stress/proxy-construct.js (0 => 196868)


--- trunk/Source/_javascript_Core/tests/stress/proxy-construct.js	                        (rev 0)
+++ trunk/Source/_javascript_Core/tests/stress/proxy-construct.js	2016-02-20 23:51:33 UTC (rev 196868)
@@ -0,0 +1,263 @@
+function assert(b) {
+    if (!b)
+        throw new Error("Bad assertion");
+}
+
+{
+    let target = function() { }
+    let handler = {
+        construct: 45
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            new proxy;
+        } catch(e) {
+            threw = true;
+            assert(e.toString() === "TypeError: 'construct' property of a Proxy's handler should be constructible.");
+        }
+        assert(threw);
+    }
+}
+
+{
+    let target = function() { }
+    let handler = {
+        construct: "hello"
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            new proxy;
+        } catch(e) {
+            threw = true;
+            assert(e.toString() === "TypeError: 'construct' property of a Proxy's handler should be constructible.");
+        }
+        assert(threw);
+    }
+}
+
+{
+    let target = function() { }
+    let error = null;
+    let handler = {
+        get construct() {
+            error = new Error;
+            throw error;
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            new proxy;
+        } catch(e) {
+            threw = true;
+            assert(e === error);
+        }
+        assert(threw);
+        error = null;
+    }
+}
+
+{
+    let target = function() { }
+    let error = null;
+    let handler = {
+        construct: function() {
+            error = new Error;
+            throw error;
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            new proxy;
+        } catch(e) {
+            threw = true;
+            assert(e === error);
+        }
+        assert(threw);
+        error = null;
+    }
+}
+
+{
+    let error = null;
+    let target = function() {
+        assert(new.target === proxy);
+        error = new Error;
+        throw error;
+    };
+    let handler = { };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            new proxy;
+        } catch(e) {
+            assert(e === error);
+            error = null;
+            threw = true;
+        }
+        assert(threw);
+    }
+}
+
+{
+    let target = function() { }
+    let handler = {
+        construct: function() {
+            return 25;
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            new proxy;
+        } catch(e) {
+            threw = true;
+            assert(e.toString() === "TypeError: Result from Proxy handler's 'construct' method should be an object.");
+        }
+        assert(threw);
+    }
+}
+
+{
+    let target = function() { }
+    let handler = {
+        construct: function() {
+            return "hello";
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            new proxy;
+        } catch(e) {
+            threw = true;
+            assert(e.toString() === "TypeError: Result from Proxy handler's 'construct' method should be an object.");
+        }
+        assert(threw);
+    }
+}
+
+{
+    let target = function() { }
+    let handler = {
+        construct: function() {
+            return Symbol();
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        let threw = false;
+        try {
+            new proxy;
+        } catch(e) {
+            threw = true;
+            assert(e.toString() === "TypeError: Result from Proxy handler's 'construct' method should be an object.");
+        }
+        assert(threw);
+    }
+}
+
+{
+    let a = {};
+    let b = {};
+    let retValue = null;
+    let target = function() {
+        return retValue;
+    };
+    let error = null;
+    let handler = {
+        construct: function(theTarget, argArray, newTarget) {
+            assert(theTarget === target);
+            assert(newTarget === proxy);
+            return new theTarget(...argArray);
+        }
+    };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        retValue = i % 2 ? a : b;
+        assert(new proxy === retValue);
+    }
+}
+
+{
+    let a = {};
+    let b = {};
+    let target = function() {
+        assert(new.target === proxy);
+        return a;
+    };
+    let error = null;
+    let construct = function(theTarget, argArray, newTarget) {
+        assert(theTarget === target);
+        assert(newTarget === proxy);
+        return b;
+    };
+    let handler = { };
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        if (i % 2)
+            handler.construct = construct;
+        else
+            handler.construct = null;
+        let result = new proxy;
+        if (i % 2)
+            assert(result === b)
+        else
+            assert(result === a);
+    }
+}
+
+{
+    let target = function(...args) {
+        assert(new.target === target);
+        assert(args[0] === 0);
+        assert(args[1] === 1);
+        assert(args[2] === "foo");
+    };
+    let error = null;
+    let construct = function(theTarget, argArray, newTarget) {
+        assert(theTarget === target);
+        assert(newTarget === proxy);
+        return new target(...argArray);
+    };
+    let handler = { };
+    handler.construct = construct;
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        new proxy(0, 1, "foo");
+    }
+}
+
+{
+    let obj = null;
+    let target = function(...args) {
+        assert(new.target === target);
+        assert(args[0] === 0);
+        assert(obj);
+        assert(args[1] === obj);
+        assert(args[2] === "foo");
+    };
+    let error = null;
+    let construct = function(theTarget, argArray, newTarget) {
+        assert(theTarget === target);
+        assert(newTarget === proxy);
+        return new target(...argArray);
+    };
+    let handler = { };
+    handler.construct = construct;
+    let proxy = new Proxy(target, handler);
+    for (let i = 0; i < 500; i++) {
+        obj = {};
+        new proxy(0, obj, "foo");
+    }
+}
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to