| 
									
										
										
										
											2020-07-03 22:43:08 -07:00
										 |  |  | describe("[[Construct]] trap normal behavior", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     test("forwarding when not defined in handler", () => { | 
					
						
							|  |  |  |         let p = new Proxy( | 
					
						
							|  |  |  |             function () { | 
					
						
							|  |  |  |                 this.x = 5; | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             { construct: null } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         expect(new p().x).toBe(5); | 
					
						
							|  |  |  |         p = new Proxy( | 
					
						
							|  |  |  |             function () { | 
					
						
							|  |  |  |                 this.x = 5; | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             { construct: undefined } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         expect(new p().x).toBe(5); | 
					
						
							|  |  |  |         p = new Proxy(function () { | 
					
						
							|  |  |  |             this.x = 5; | 
					
						
							|  |  |  |         }, {}); | 
					
						
							|  |  |  |         expect(new p().x).toBe(5); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-06-25 15:30:58 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     test("trapping 'new'", () => { | 
					
						
							|  |  |  |         function f(value) { | 
					
						
							|  |  |  |             this.x = value; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-03 22:43:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |         let p; | 
					
						
							|  |  |  |         const handler = { | 
					
						
							| 
									
										
										
										
											2021-05-10 11:54:01 +01:00
										 |  |  |             construct(target, arguments_, newTarget) { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |                 expect(target).toBe(f); | 
					
						
							|  |  |  |                 expect(newTarget).toBe(p); | 
					
						
							| 
									
										
										
										
											2021-05-10 11:54:01 +01:00
										 |  |  |                 if (arguments_[1]) { | 
					
						
							|  |  |  |                     return Reflect.construct(target, [arguments_[0] * 2], newTarget); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return Reflect.construct(target, arguments_, newTarget); | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |             }, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         p = new Proxy(f, handler); | 
					
						
							| 
									
										
										
										
											2020-07-03 22:43:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |         expect(new p(15).x).toBe(15); | 
					
						
							|  |  |  |         expect(new p(15, true).x).toBe(30); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-06-25 15:30:58 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     test("trapping Reflect.construct", () => { | 
					
						
							|  |  |  |         function f(value) { | 
					
						
							|  |  |  |             this.x = value; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-03 22:43:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |         let p; | 
					
						
							|  |  |  |         function theNewTarget() {} | 
					
						
							|  |  |  |         const handler = { | 
					
						
							| 
									
										
										
										
											2021-05-10 11:54:01 +01:00
										 |  |  |             construct(target, arguments_, newTarget) { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |                 expect(target).toBe(f); | 
					
						
							|  |  |  |                 expect(newTarget).toBe(theNewTarget); | 
					
						
							| 
									
										
										
										
											2021-05-10 11:54:01 +01:00
										 |  |  |                 if (arguments_[1]) { | 
					
						
							|  |  |  |                     return Reflect.construct(target, [arguments_[0] * 2], newTarget); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return Reflect.construct(target, arguments_, newTarget); | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |             }, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         p = new Proxy(f, handler); | 
					
						
							| 
									
										
										
										
											2020-07-03 22:43:08 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |         Reflect.construct(p, [15], theNewTarget); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-03 22:43:08 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe("[[Construct]] invariants", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     test("target must have a [[Construct]] slot", () => { | 
					
						
							|  |  |  |         [{}, [], new Proxy({}, {})].forEach(item => { | 
					
						
							|  |  |  |             expect(() => { | 
					
						
							|  |  |  |                 new new Proxy(item, {})(); | 
					
						
							|  |  |  |             }).toThrowWithMessage(TypeError, "[object ProxyObject] is not a constructor"); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-06-25 14:50:57 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-03 22:43:08 -07:00
										 |  |  | }); |