--- a/nashorn/src/jdk/nashorn/internal/objects/NativeArray.java Wed Jul 05 19:34:04 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeArray.java Tue Mar 25 14:52:34 2014 -0700
@@ -31,6 +31,7 @@
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 @@
}
/**
+ * 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
*
--- a/nashorn/src/jdk/nashorn/internal/runtime/JSONFunctions.java Wed Jul 05 19:34:04 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/JSONFunctions.java Tue Mar 25 14:52:34 2014 -0700
@@ -101,7 +101,6 @@
// 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);
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java Wed Jul 05 19:34:04 2017 +0200
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java Tue Mar 25 14:52:34 2014 -0700
@@ -593,23 +593,16 @@
}
/**
- * 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 @@
* @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 @@
}
}
+ 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) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8037562.js Tue Mar 25 14:52:34 2014 -0700
@@ -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])));
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8037562.js.EXPECTED Tue Mar 25 14:52:34 2014 -0700
@@ -0,0 +1,4 @@
+{"0":0,"2":2}
+{"0":"","2":""}
+{"0":0,"5":"hello"}
+{"0":"","15":3234}
--- a/nashorn/test/src/jdk/nashorn/internal/runtime/NoPersistenceCachingTest.java Wed Jul 05 19:34:04 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/internal/runtime/NoPersistenceCachingTest.java Tue Mar 25 14:52:34 2014 -0700
@@ -37,6 +37,8 @@
import javax.script.ScriptEngineManager;
import javax.script.SimpleScriptContext;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeTest;
/**
* @test
@@ -50,8 +52,8 @@
private ScriptContext context1, context2, context3;
private ByteArrayOutputStream stderr;
private PrintStream prevStderr;
- private final String script = "print('Hello')";
+ @BeforeTest
public void setupTest() {
stderr = new ByteArrayOutputStream();
prevStderr = System.err;
@@ -69,33 +71,33 @@
}
String[] options = new String[]{"--log=compiler:finest"};
engine = nashornFactory.getScriptEngine(options);
+ context1 = engine.getContext();
+ context2 = new SimpleScriptContext();
+ context2.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
+ context3 = new SimpleScriptContext();
+ context3.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
}
+ @AfterTest
public void setErrTest() {
System.setErr(prevStderr);
}
public void runTest(int numberOfContext, String expectedOutputPattern,
int expectedPatternOccurrence) {
- setupTest();
+
try {
switch (numberOfContext) {
case 2:
- context1 = engine.getContext();
- context2 = new SimpleScriptContext();
- context2.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
- engine.eval(script, context1);
- engine.eval(script, context2);
+ String scriptTwoContexts = "print('HelloTwoContexts')";
+ engine.eval(scriptTwoContexts, context1);
+ engine.eval(scriptTwoContexts, context2);
break;
case 3:
- context1 = engine.getContext();
- context2 = new SimpleScriptContext();
- context2.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
- context3 = new SimpleScriptContext();
- context3.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
- engine.eval(script, context1);
- engine.eval(script, context2);
- engine.eval(script, context3);
+ String scriptThreeContexts = "print('HelloThreeContexts')";
+ engine.eval(scriptThreeContexts, context1);
+ engine.eval(scriptThreeContexts, context2);
+ engine.eval(scriptThreeContexts, context3);
break;
}
} catch (final Exception se) {
@@ -113,7 +115,7 @@
+ expectedPatternOccurrence + " and found: " + matches + "\n"
+ stderr);
}
- setErrTest();
+ stderr.reset();
}
private static String getCodeCachePattern() {