mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:52:45 +00:00 
			
		
		
		
	LibJS: Treat NaN in Value::to_i32() as zero
Let's treat it as zero like the ECMAScript spec does in toInteger().
That way we can use to_i32() and don't have to care about weird input
input values where a number is expected, i.e.
"foo".charAt() === "f"
"foo".charAt("bar") === "f"
"foo".charAt(0) === "f"
			
			
This commit is contained in:
		
							parent
							
								
									ecb03716d4
								
							
						
					
					
						commit
						00fe7f82c0
					
				
					 2 changed files with 9 additions and 0 deletions
				
			
		|  | @ -274,6 +274,10 @@ i32 Value::to_i32(Interpreter& interpreter) const | |||
|     auto number = to_number(interpreter); | ||||
|     if (interpreter.exception()) | ||||
|         return 0; | ||||
|     if (number.is_nan()) | ||||
|         return 0; | ||||
|     // FIXME: What about infinity though - that's UB...
 | ||||
|     // Maybe NumericLimits<i32>::max() for +Infinity and NumericLimits<i32>::min() for -Infinity?
 | ||||
|     return number.as_i32(); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -13,6 +13,11 @@ try { | |||
|     assert(s.charAt(5) === 'r'); | ||||
|     assert(s.charAt(6) === ''); | ||||
| 
 | ||||
|     assert(s.charAt() === 'f'); | ||||
|     assert(s.charAt(NaN) === 'f'); | ||||
|     assert(s.charAt("foo") === 'f'); | ||||
|     assert(s.charAt(undefined) === 'f'); | ||||
| 
 | ||||
|     console.log("PASS"); | ||||
| } catch (e) { | ||||
|     console.log("FAIL: " + e); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Linus Groh
						Linus Groh