aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsundar <none@none>2014-03-21 20:24:01 +0530
committersundar <none@none>2014-03-21 20:24:01 +0530
commitac10ca52720e57aad8bc472932f1dad762a9decf (patch)
tree42550b27a1140a40519d7d2ceb0561225852041d /src
parent309992a1f232250da32a00eac20fbca68074de2f (diff)
downloadnashorn-ac10ca52720e57aad8bc472932f1dad762a9decf.tar.gz
8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
Reviewed-by: jlaskey, hannesw
Diffstat (limited to 'src')
-rw-r--r--src/jdk/nashorn/internal/objects/NativeArray.java22
-rw-r--r--src/jdk/nashorn/internal/runtime/JSONFunctions.java1
-rw-r--r--src/jdk/nashorn/internal/runtime/ScriptObject.java29
3 files changed, 38 insertions, 14 deletions
diff --git a/src/jdk/nashorn/internal/objects/NativeArray.java b/src/jdk/nashorn/internal/objects/NativeArray.java
index 168a2f8a..d95caf2c 100644
--- a/src/jdk/nashorn/internal/objects/NativeArray.java
+++ b/src/jdk/nashorn/internal/objects/NativeArray.java
@@ -31,6 +31,7 @@ import static jdk.nashorn.internal.runtime.PropertyDescriptor.VALUE;
import static jdk.nashorn.internal.runtime.PropertyDescriptor.WRITABLE;
import static jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator.arrayLikeIterator;
import static jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator.reverseArrayLikeIterator;
+import static jdk.nashorn.internal.runtime.arrays.ArrayIndex.isValidArrayIndex;
import java.lang.invoke.MethodHandle;
import java.util.ArrayList;
@@ -350,6 +351,27 @@ public final class NativeArray extends ScriptObject {
}
/**
+ * Spec. mentions use of [[DefineOwnProperty]] for indexed properties in
+ * certain places (eg. Array.prototype.map, filter). We can not use ScriptObject.set
+ * method in such cases. This is because set method uses inherited setters (if any)
+ * from any object in proto chain such as Array.prototype, Object.prototype.
+ * This method directly sets a particular element value in the current object.
+ *
+ * @param index key for property
+ * @param value value to define
+ */
+ @Override
+ public final void defineOwnProperty(final int index, final Object value) {
+ assert isValidArrayIndex(index) : "invalid array index";
+ final long longIndex = ArrayIndex.toLongIndex(index);
+ if (longIndex >= getArray().length()) {
+ // make array big enough to hold..
+ setArray(getArray().ensure(longIndex));
+ }
+ setArray(getArray().set(index, value, false));
+ }
+
+ /**
* Return the array contents upcasted as an ObjectArray, regardless of
* representation
*
diff --git a/src/jdk/nashorn/internal/runtime/JSONFunctions.java b/src/jdk/nashorn/internal/runtime/JSONFunctions.java
index 64863184..f12945fe 100644
--- a/src/jdk/nashorn/internal/runtime/JSONFunctions.java
+++ b/src/jdk/nashorn/internal/runtime/JSONFunctions.java
@@ -101,7 +101,6 @@ public final class JSONFunctions {
// apply 'reviver' function if available
private static Object applyReviver(final Global global, final Object unfiltered, final Object reviver) {
if (reviver instanceof ScriptFunction) {
- assert global instanceof Global;
final ScriptObject root = global.newObject();
root.addOwnProperty("", Property.WRITABLE_ENUMERABLE_CONFIGURABLE, unfiltered);
return walk(root, "", (ScriptFunction)reviver);
diff --git a/src/jdk/nashorn/internal/runtime/ScriptObject.java b/src/jdk/nashorn/internal/runtime/ScriptObject.java
index 05e236e3..1519024a 100644
--- a/src/jdk/nashorn/internal/runtime/ScriptObject.java
+++ b/src/jdk/nashorn/internal/runtime/ScriptObject.java
@@ -593,23 +593,16 @@ public abstract class ScriptObject implements PropertyAccess {
}
/**
- * Spec. mentions use of [[DefineOwnProperty]] for indexed properties in
- * certain places (eg. Array.prototype.map, filter). We can not use ScriptObject.set
- * method in such cases. This is because set method uses inherited setters (if any)
- * from any object in proto chain such as Array.prototype, Object.prototype.
- * This method directly sets a particular element value in the current object.
+ * Almost like defineOwnProperty(int,Object) for arrays this one does
+ * not add 'gap' elements (like the array one does).
*
* @param index key for property
* @param value value to define
*/
- public final void defineOwnProperty(final int index, final Object value) {
+ public void defineOwnProperty(final int index, final Object value) {
assert isValidArrayIndex(index) : "invalid array index";
final long longIndex = ArrayIndex.toLongIndex(index);
- if (longIndex >= getArray().length()) {
- // make array big enough to hold..
- setArray(getArray().ensure(longIndex));
- }
- setArray(getArray().set(index, value, false));
+ setValueAtArrayIndex(longIndex, index, value, false);
}
private void checkIntegerKey(final String key) {
@@ -2747,9 +2740,7 @@ public abstract class ScriptObject implements PropertyAccess {
* @param strict are we in strict mode
*/
private void doesNotHave(final int index, final Object value, final boolean strict) {
- final long oldLength = getArray().length();
final long longIndex = ArrayIndex.toLongIndex(index);
-
if (getMap().containsArrayKeys()) {
final String key = JSType.toString(longIndex);
final FindProperty find = findProperty(key, true);
@@ -2760,6 +2751,18 @@ public abstract class ScriptObject implements PropertyAccess {
}
}
+ setValueAtArrayIndex(longIndex, index, value, strict);
+ }
+
+ /**
+ * Handle when an array doesn't have a slot - possibly grow and/or convert array.
+ *
+ * @param index key as index
+ * @param value element value
+ * @param strict are we in strict mode
+ */
+ private void setValueAtArrayIndex(final long longIndex, final int index, final Object value, final boolean strict) {
+ final long oldLength = getArray().length();
if (longIndex >= oldLength) {
if (!isExtensible()) {
if (strict) {