8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys

Reviewed-by: jlaskey, hannesw
This commit is contained in:
Athijegannathan Sundararajan 2014-03-21 20:24:01 +05:30
parent 71ded92863
commit 4036a466ca
5 changed files with 83 additions and 14 deletions

View File

@ -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;
@ -349,6 +350,27 @@ public final class NativeArray extends ScriptObject {
return super.defineOwnProperty(key, desc, reject);
}
/**
* 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

View File

@ -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);

View File

@ -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) {

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* JDK-8037562: Nashorn: JSON.parse comes up with nonexistent entries if there are gaps between the keys
*
* @test
* @run
*/
var strs = [
'{ "0":0, "2":2 }',
'{ "0":"", "2":"" }',
'{ "0":0, "5":"hello" }',
'{ "0":"", "15":3234 }',
]
for (var i in strs) {
print(JSON.stringify(JSON.parse(strs[i])));
}

View File

@ -0,0 +1,4 @@
{"0":0,"2":2}
{"0":"","2":""}
{"0":0,"5":"hello"}
{"0":"","15":3234}