-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make handling of object indices more in line with the spec (#1392)
In general, treat invalid index values, such as negative numbers, as strings and not integers. This will result in object properties being correctly ordered, and JSON.stringify producing the right output as well. * Make handling of object indices more in line with the spec * In more cases, treat out-of-range numeric indices as strings * For typed arrays, correctly implement index semantics, which are different than for regular objects * Also fix index handling in JSON.parse
- Loading branch information
Showing
7 changed files
with
195 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.mozilla.javascript.tests; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
import org.mozilla.javascript.ScriptRuntime; | ||
|
||
public class IndexTest { | ||
private void expectInteger(ScriptRuntime.StringIdOrIndex id, int v) { | ||
assertEquals(v, id.getIndex()); | ||
assertNull(id.getStringId()); | ||
} | ||
|
||
private void expectString(ScriptRuntime.StringIdOrIndex id, String s) { | ||
assertEquals(s, id.getStringId()); | ||
} | ||
|
||
@Test | ||
public void testNumericIndices() { | ||
// Normal integers | ||
expectInteger(ScriptRuntime.toStringIdOrIndex(0), 0); | ||
expectInteger(ScriptRuntime.toStringIdOrIndex(1), 1); | ||
expectInteger(ScriptRuntime.toStringIdOrIndex(Integer.MAX_VALUE), Integer.MAX_VALUE); | ||
// Negative integers | ||
expectString(ScriptRuntime.toStringIdOrIndex(-1), "-1"); | ||
expectString(ScriptRuntime.toStringIdOrIndex(Integer.MIN_VALUE), "-2147483648"); | ||
// Floating-point -- but rounding is weird so just check nullness | ||
ScriptRuntime.StringIdOrIndex id; | ||
id = ScriptRuntime.toStringIdOrIndex(1.1f); | ||
assertNotNull(id.getStringId()); | ||
} | ||
|
||
@Test | ||
public void testStringIndices() { | ||
// Normal integers | ||
expectInteger(ScriptRuntime.toStringIdOrIndex("0"), 0); | ||
expectInteger(ScriptRuntime.toStringIdOrIndex("1"), 1); | ||
expectInteger( | ||
ScriptRuntime.toStringIdOrIndex(String.valueOf(Integer.MAX_VALUE)), | ||
Integer.MAX_VALUE); | ||
// Negative integers | ||
expectString(ScriptRuntime.toStringIdOrIndex("-1"), "-1"); | ||
expectString( | ||
ScriptRuntime.toStringIdOrIndex(String.valueOf(Integer.MIN_VALUE)), "-2147483648"); | ||
// Floating-point | ||
expectString(ScriptRuntime.toStringIdOrIndex("3.14"), "3.14"); | ||
expectString(ScriptRuntime.toStringIdOrIndex("1.1"), "1.1"); | ||
// Out of range | ||
expectString( | ||
ScriptRuntime.toStringIdOrIndex(String.valueOf(Long.MAX_VALUE)), | ||
"9223372036854775807"); | ||
// Others | ||
expectString(ScriptRuntime.toStringIdOrIndex(Double.NaN), "NaN"); | ||
// Junk | ||
expectString( | ||
ScriptRuntime.toStringIdOrIndex("This is not an integer"), | ||
"This is not an integer"); | ||
} | ||
} |
Oops, something went wrong.