mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:42:43 +00:00 
			
		
		
		
	 6e05685ad4
			
		
	
	
		6e05685ad4
		
	
	
	
	
		
			
			Previously, when a loop detected an unwind of type ScopeType::Function (which means a return statement was executed inside of the loop), it would just return undefined. This set the VM's last_value to undefined, when it should have been the returned value. This patch makes all loop statements return the appropriate value in the above case.
		
			
				
	
	
		
			53 lines
		
	
	
	
		
			1.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
	
		
			1.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| describe("returning from loops", () => {
 | |
|     test("returning from while loops", () => {
 | |
|         function foo() {
 | |
|             while (true) {
 | |
|                 return 10;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         expect(foo()).toBe(10);
 | |
|     });
 | |
| 
 | |
|     test("returning from do-while loops", () => {
 | |
|         function foo() {
 | |
|             do {
 | |
|                 return 10;
 | |
|             } while (true);
 | |
|         }
 | |
| 
 | |
|         expect(foo()).toBe(10);
 | |
|     });
 | |
| 
 | |
|     test("returning from for loops", () => {
 | |
|         function foo() {
 | |
|             for (let i = 0; i < 5; i++) {
 | |
|                 return 10;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         expect(foo()).toBe(10);
 | |
|     });
 | |
| 
 | |
|     test("returning from for-in loops", () => {
 | |
|         function foo() {
 | |
|             const o = { a: 1, b: 2 };
 | |
|             for (let a in o) {
 | |
|                 return 10;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         expect(foo()).toBe(10);
 | |
|     });
 | |
| 
 | |
|     test("returning from for-of loops", () => {
 | |
|         function foo() {
 | |
|             const o = [1, 2, 3];
 | |
|             for (let a of o) {
 | |
|                 return 10;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         expect(foo()).toBe(10);
 | |
|     });
 | |
| });
 |