From 04756b47faa061d67fb3a762dbde5932bdd8d80f Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Mon, 8 Nov 2021 16:36:47 +0100 Subject: [PATCH 1/5] Add: failing testcase --- .../tests/NativeJavaListArrayCompatTest.java | 412 ++++++++++++++++++ 1 file changed, 412 insertions(+) create mode 100644 testsrc/org/mozilla/javascript/tests/NativeJavaListArrayCompatTest.java diff --git a/testsrc/org/mozilla/javascript/tests/NativeJavaListArrayCompatTest.java b/testsrc/org/mozilla/javascript/tests/NativeJavaListArrayCompatTest.java new file mode 100644 index 0000000000..5aa202970a --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/NativeJavaListArrayCompatTest.java @@ -0,0 +1,412 @@ +/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mozilla.javascript.tests; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.Function; +import junit.framework.TestCase; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.ContextFactory; +import org.mozilla.javascript.NativeArray; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.tools.shell.Global; + +/** + * Test all Array-prototype function against NativeJavaList + * + * @author Roland Praml, FOCONIS AG + */ +public class NativeJavaListArrayCompatTest extends TestCase { + + public void testArrayConcat() { + List list = abcList(); + assertEquals( + Arrays.asList("x", "a", "b", "c"), + runScript("['x'].concat(value)", list, Function.identity())); + assertEquals( + Arrays.asList("a", "b", "c", "x"), + runScript("Array.prototype.concat.call(value, ['x'])", list, Function.identity())); + } + + public void testArrayCopyWithin() { + List list = abcList(); + list.add("d"); + list.add("e"); + // copy to index 0 the element at index 3 + runScriptAsString("Array.prototype.copyWithin.call(value, 0, 3, 4)", list); + assertEquals(Arrays.asList("d", "b", "c", "d", "e"), list); + } + + public void testArrayEntries() { + List list = abcList(); + + assertEquals( + "0,a,1,b,2,c,", + runScriptAsString( + "var iter = Array.prototype.entries.call(value);\n" + + "var ret = '';\n" + + "for (e of iter) { ret += e + ','; }\n" + + "ret;", + list)); + } + + public void testArrayEvery() { + List list = abcList(); + + assertEquals(1, runScriptAsInt("Array.prototype.every.call(value, e => e < 'd')", list)); + assertEquals(0, runScriptAsInt("Array.prototype.every.call(value, e => e < 'b')", list)); + } + + public void testArrayFill() { + List list = abcList(); + runScriptAsString("Array.prototype.fill.call(value, 'd', 1, 4)", list); + assertEquals("[a, d, d]", list.toString()); + } + + public void testArrayFilter() { + List list = abcList(); + assertEquals( + "a,b", runScriptAsString("Array.prototype.filter.call(value, e => e < 'c')", list)); + } + + public void testArrayFind() { + List list = abcList(); + assertEquals( + "c", runScriptAsString("Array.prototype.find.call(value, e => e > 'b')", list)); + } + + public void testArrayFindIndex() { + List list = abcList(); + assertEquals( + 2, runScriptAsInt("Array.prototype.findIndex.call(value, e => e > 'b')", list)); + } + + public void testArrayForEach() { + List list = abcList(); + assertEquals( + "a@0,b@1,c@2,", + runScriptAsString( + "var ret='';\n" + + "Array.prototype.forEach.call(value, (e,i) => ret += e + '@' + i + ',');\n" + + "ret", + list)); + // note: forEach is also implemented in java.util.Iterable, but does not support second + // parameter + list = abcList(); + assertEquals( + "a@undefined,b@undefined,c@undefined,", + runScriptAsString( + "var ret='';\n" + + "value.forEach((e,i) => ret += e + '@' + i + ',');\n" + + "ret", + list)); + } + + public void testArrayFrom() { + List list = abcList(); + Object from = runScript("Array.from(value)", list, Function.identity()); + assertTrue(from instanceof NativeArray); + assertEquals(list, from); + } + + public void testArrayIncludes() { + + List list = abcList(); + assertEquals("true", runScriptAsString("Array.prototype.includes.call(value, 'b')", list)); + assertEquals("false", runScriptAsString("Array.prototype.includes.call(value, 'd')", list)); + + List listD = new ArrayList<>(); + listD.add(1.0); + listD.add(2.0); + listD.add(3.0); + assertEquals("true", runScriptAsString("Array.prototype.includes.call(value, 2)", listD)); + assertEquals("false", runScriptAsString("Array.prototype.includes.call(value, 4)", listD)); + + List listI = new ArrayList<>(); + listI.add(1); + listI.add(2); + listI.add(3); + assertEquals("true", runScriptAsString("Array.prototype.includes.call(value, 2)", listI)); + assertEquals("false", runScriptAsString("Array.prototype.includes.call(value, 4)", listD)); + } + + public void testArrayIndexOf() { + + List list = abcList(); + assertEquals(1, runScriptAsInt("value.indexOf('b')", list)); + assertEquals(-1, runScriptAsInt("value.indexOf('d')", list)); + assertEquals(1, runScriptAsInt("Array.prototype.indexOf.call(value,'b')", list)); + assertEquals(-1, runScriptAsInt("Array.prototype.indexOf.call(value,'d')", list)); + + List listD = new ArrayList<>(); + listD.add(1.0); + listD.add(2.0); + listD.add(3.0); + assertEquals(1, runScriptAsInt("value.indexOf(2)", listD)); + assertEquals(-1, runScriptAsInt("value.indexOf(4)", listD)); + assertEquals(1, runScriptAsInt("Array.prototype.indexOf.call(value, 2)", listD)); + assertEquals(-1, runScriptAsInt("Array.prototype.indexOf.call(value, 4)", listD)); + + List listI = new ArrayList<>(); + listI.add(1); + listI.add(2); + listI.add(3); + + // NOTE: This will invoke the java.util.List.indexOf method, which + // is not type aware! So the argument is converted to a double and we will not find '2.0' + // in the integer list + assertEquals(-1, runScriptAsInt("value.indexOf(2)", listI)); + assertEquals(1, runScriptAsInt("value.indexOf(2)", listD)); + + // but we will find it with the Array.indexOf method + assertEquals(1, runScriptAsInt("Array.prototype.indexOf.call(value, 2)", listI)); + assertEquals(1, runScriptAsInt("Array.prototype.indexOf.call(value, 2)", listD)); + } + + public void testArrayIsArray() { + List list = abcList(); // list is only array-like, but not instance of Array + assertEquals("false", runScriptAsString("Array.isArray(value)", list)); + } + + public void testArrayJoin() { + List list = abcList(); + assertEquals("a-b-c", runScriptAsString("Array.prototype.join.call(value, '-')", list)); + } + + public void testArrayKeys() { + List list = abcList(); + assertEquals( + "0,1,2,", + runScriptAsString( + "var ret = '';\n" + + "var iter = Array.prototype.keys.call(value);\n" + + "for (e of iter) { ret += e + ',' };\n" + + "ret", + list)); + } + + public void testArrayLastIndexOf() { + + List list = abcList(); + assertEquals(1, runScriptAsInt("Array.prototype.lastIndexOf.call(value,'b')", list)); + assertEquals(-1, runScriptAsInt("Array.prototype.lastIndexOf.call(value,'d')", list)); + + List listD = new ArrayList<>(); + listD.add(1.0); + listD.add(2.0); + listD.add(3.0); + + List listI = new ArrayList<>(); + listI.add(1); + listI.add(2); + listI.add(3); + + // NOTE: This will invoke the java.util.List.indexOf method, which + // is not type aware! So the argument is converted to a double and we will not find '2.0' + // in the integer list + assertEquals(-1, runScriptAsInt("value.lastIndexOf(2)", listI)); + assertEquals(1, runScriptAsInt("value.lastIndexOf(2)", listD)); + + // but we will find it with the Array.indexOf method + assertEquals(1, runScriptAsInt("Array.prototype.indexOf.call(value, 2)", listI)); + assertEquals(1, runScriptAsInt("Array.prototype.indexOf.call(value, 2)", listD)); + } + + public void testInstanceOfArray() { + List list = abcList(); + assertEquals(0, runScriptAsInt("(value instanceof Object)", list)); + assertEquals(1, runScriptAsInt("(value instanceof java.util.List)", list)); + assertEquals(1, runScriptAsInt("(value instanceof java.util.ArrayList)", list)); + assertEquals(0, runScriptAsInt("(value instanceof Array)", list)); + } + + public void testArrayMap() { + List list = numberList(); + assertEquals( + "8,20,4,6,0", runScriptAsString("Array.prototype.map.call(value, x => x*2)", list)); + } + + public void testArrayPop() { + List list = abcList(); + assertEquals("c", runScriptAsString("Array.prototype.pop.call(value)", list)); + assertEquals("[a, b]", list.toString()); + } + + public void testArrayPush() { + List list = abcList(); + assertEquals("4", runScriptAsString("Array.prototype.push.call(value, 'd')", list)); + assertEquals("[a, b, c, d]", list.toString()); + } + + public void testArrayReduce() { + List list = numberList(); + assertEquals(19, runScriptAsInt("Array.prototype.reduce.call(value, (a,b) => a+b)", list)); + List abc = abcList(); + assertEquals( + "abc", runScriptAsString("Array.prototype.reduce.call(value, (a,b) => a+b)", abc)); + } + + public void testArrayReduceRight() { + List list = numberList(); + assertEquals( + 19, runScriptAsInt("Array.prototype.reduceRight.call(value, (a,b) => a+b)", list)); + List abc = abcList(); + assertEquals( + "cba", + runScriptAsString("Array.prototype.reduceRight.call(value, (a,b) => a+b)", abc)); + } + + public void testArrayReverse() { + List list = abcList(); + runScriptAsString("Array.prototype.reverse.call(value)", list); + assertEquals(Arrays.asList("c", "b", "a"), list); + } + + public void testArrayShift() { + List list = abcList(); + assertEquals("a", runScriptAsString("Array.prototype.shift.call(value)", list)); + assertEquals(Arrays.asList("b", "c"), list); + } + + public void testArraySlice() { + List list = abcList(); + list.add("d"); + list.add("e"); + assertEquals("b,c", runScriptAsString("Array.prototype.slice.call(value, 1, 3)", list)); + } + + public void testArraySome() { + List list = abcList(); + + assertEquals(0, runScriptAsInt("Array.prototype.some.call(value, e => e > 'd')", list)); + assertEquals(1, runScriptAsInt("Array.prototype.some.call(value, e => e > 'b')", list)); + } + + public void testArraySortString() { + List list = new ArrayList<>(); + list.add("a"); + list.add("d"); + list.add("e"); + list.add("b"); + list.add("c"); + runScriptAsString("Array.sort(value)", list); + assertEquals("[a, b, c, d, e]", list.toString()); + } + + public void testArraySortStringJavaComp() { + List list = new ArrayList<>(); + list.add("a"); + list.add("d"); + list.add("e"); + list.add("b"); + list.add("c"); + // java.util.List.sort will override Array.sort + runScriptAsString("value.sort(java.util.Comparator.naturalOrder())", list); + assertEquals("[a, b, c, d, e]", list.toString()); + } + + public void testArraySortNumbers() { + List list = numberList(); + // NOTE: When Array.sort sorts on string representation! + assertEquals(Integer.class, list.get(0).getClass()); + runScriptAsString("Array.prototype.sort.call(value);", list); + assertEquals("[0, 10, 2, 3, 4]", list.toString()); + assertEquals(Integer.class, list.get(0).getClass()); + runScriptAsString("Array.prototype.sort.call(value, (a,b)=>b-a);", list); + assertEquals("[10, 4, 3, 2, 0]", list.toString()); + } + + public void testArraySortNumbersNoWrap() { + List list = numberList(); + assertEquals(Integer.class, list.get(0).getClass()); + // NOTE: When we do not wrap primitives, the type of values can change + runScriptDontWrapPrimitive("Array.prototype.sort.call(value);", list); + assertEquals("[0.0, 10.0, 2.0, 3.0, 4.0]", list.toString()); + assertEquals(Double.class, list.get(0).getClass()); // Attention: Type has changed! + } + + public void testArraySortNumberJavaComp() { + List list = numberList(); + runScriptAsString("value.sort(java.util.Comparator.naturalOrder())", list); + assertEquals("[0, 2, 3, 4, 10]", list.toString()); + assertEquals(Integer.class, list.get(0).getClass()); + } + + public void testArraySplice() { + List list = abcList(); + runScriptAsString("Array.prototype.splice.call(value, 1, 0, 'x')", list); + assertEquals("[a, x, b, c]", list.toString()); + } + + public void testArrayUnshift() { + List list = abcList(); + assertEquals(4, runScriptAsInt("Array.prototype.unshift.call(value, 'x')", list)); + assertEquals("[x, a, b, c]", list.toString()); + } + + public void testObjectKeys() { + List list = new ArrayList<>(); + NativeArray arr; + + arr = (NativeArray) runScript("Object.keys(value)", list, Function.identity()); + assertEquals(0, arr.size()); + + list = abcList(); + + arr = (NativeArray) runScript("Object.keys(value)", list, Function.identity()); + assertEquals(Arrays.asList("0", "1", "2"), arr); + arr = (NativeArray) runScript("Object.values(value)", list, Function.identity()); + assertEquals(Arrays.asList("a", "b", "c"), arr); + } + + private List abcList() { + List list = new ArrayList<>(); + list.add("a"); + list.add("b"); + list.add("c"); + return list; + } + + private List numberList() { + return new ArrayList<>(Arrays.asList(4, 10, 2, 3, 0)); + } + + private int runScriptAsInt(String scriptSourceText, Object value) { + return runScript(scriptSourceText, value, Context::toNumber).intValue(); + } + + private String runScriptAsString(String scriptSourceText, Object value) { + return runScript(scriptSourceText, value, Context::toString); + } + + private T runScript(String scriptSourceText, Object value, Function convert) { + + return ContextFactory.getGlobal() + .call( + context -> { + context.setLanguageVersion(Context.VERSION_ES6); + Scriptable scope = context.initStandardObjects(new Global()); + scope.put("value", scope, Context.javaToJS(value, scope)); + return convert.apply( + context.evaluateString(scope, scriptSourceText, "", 1, null)); + }); + } + + private void runScriptDontWrapPrimitive(String scriptSourceText, Object value) { + ContextFactory.getGlobal() + .call( + context -> { + context.setLanguageVersion(Context.VERSION_ES6); + context.getWrapFactory().setJavaPrimitiveWrap(false); + Scriptable scope = context.initStandardObjects(new Global()); + scope.put("value", scope, Context.javaToJS(value, scope)); + return context.evaluateString(scope, scriptSourceText, "", 1, null); + }); + } +} From 4c02bc2cb799b4d519bebd6f690f9b4eca0ff8e3 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Mon, 8 Nov 2021 16:59:20 +0100 Subject: [PATCH 2/5] Changed unwrap to fix Array.indexOf / lastIndexOf and Array.includes --- src/org/mozilla/javascript/ScriptRuntime.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/org/mozilla/javascript/ScriptRuntime.java b/src/org/mozilla/javascript/ScriptRuntime.java index 279de7cfc0..4475c4dc38 100644 --- a/src/org/mozilla/javascript/ScriptRuntime.java +++ b/src/org/mozilla/javascript/ScriptRuntime.java @@ -3446,6 +3446,12 @@ public static boolean same(Object x, Object y) { /** Implement "SameValueZero" from ECMA 7.2.9 */ public static boolean sameZero(Object x, Object y) { + if (x instanceof Wrapper) { + x = ((Wrapper) x).unwrap(); + } + if (y instanceof Wrapper) { + y = ((Wrapper) y).unwrap(); + } if (!typeof(x).equals(typeof(y))) { return false; } @@ -3605,6 +3611,13 @@ private static boolean eqString(CharSequence x, Object y) { } public static boolean shallowEq(Object x, Object y) { + if (x instanceof Wrapper) { + x = ((Wrapper) x).unwrap(); + } + if (y instanceof Wrapper) { + y = ((Wrapper) y).unwrap(); + } + if (x == y) { if (!(x instanceof Number)) { return true; @@ -3635,9 +3648,6 @@ public static boolean shallowEq(Object x, Object y) { return x.equals(y); } } else if (x instanceof Scriptable) { - if (x instanceof Wrapper && y instanceof Wrapper) { - return ((Wrapper) x).unwrap() == ((Wrapper) y).unwrap(); - } if (x instanceof Delegator) { x = ((Delegator) x).getDelegee(); if (y instanceof Delegator) { @@ -3646,8 +3656,7 @@ public static boolean shallowEq(Object x, Object y) { if (x == y) { return true; } - } - if (y instanceof Delegator && ((Delegator) y).getDelegee() == x) { + } else if (y instanceof Delegator && ((Delegator) y).getDelegee() == x) { return true; } } else { From 69a1e88e6a33ce7654e3aa3d9e46604fb92843b4 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Tue, 9 Nov 2021 13:14:29 +0100 Subject: [PATCH 3/5] Copied test cases from #561 --- .../javascript/tests/AccessingJavaList.java | 89 ++++++++++++++++++ .../tests/FindingJavaStringsFromJSArray.java | 94 +++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 testsrc/org/mozilla/javascript/tests/AccessingJavaList.java create mode 100644 testsrc/org/mozilla/javascript/tests/FindingJavaStringsFromJSArray.java diff --git a/testsrc/org/mozilla/javascript/tests/AccessingJavaList.java b/testsrc/org/mozilla/javascript/tests/AccessingJavaList.java new file mode 100644 index 0000000000..4acd489137 --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/AccessingJavaList.java @@ -0,0 +1,89 @@ +/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mozilla.javascript.tests; + +import java.util.ArrayList; +import java.util.List; +import junit.framework.TestCase; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.ContextFactory; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.tools.shell.Global; + +public class AccessingJavaList extends TestCase { + protected final Global global = new Global(); + + public AccessingJavaList() { + global.init(ContextFactory.getGlobal()); + } + + public void testAccessingJavaListIntegerValues() { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + + assertEquals(2, runScriptAsInt("value[1]", list)); + assertEquals(3, runScriptAsInt("value[2]", list)); + } + + public void testUpdateingJavaListIntegerValues() { + List list = new ArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + + assertEquals(2, runScriptAsInt("value[1]", list)); + assertEquals(5, runScriptAsInt("value[1]=5;value[1]", list)); + assertEquals(5, list.get(1).intValue()); + } + + public void testAccessingJavaListStringValues() { + List list = new ArrayList<>(); + list.add("a"); + list.add("b"); + list.add("c"); + + assertEquals("b", runScriptAsString("value[1]", list)); + assertEquals("c", runScriptAsString("value[2]", list)); + } + + public void testUpdatetingJavaListStringValues() { + List list = new ArrayList<>(); + list.add("a"); + list.add("b"); + list.add("c"); + + assertEquals("b", runScriptAsString("value[1]", list)); + assertEquals("f", runScriptAsString("value[1]=\"f\";value[1]", list)); + assertEquals("f", list.get(1)); + } + + private int runScriptAsInt(final String scriptSourceText, final Object value) { + return ContextFactory.getGlobal() + .call( + context -> { + Scriptable scope = context.initStandardObjects(global); + scope.put("value", scope, Context.javaToJS(value, scope)); + return (int) + Context.toNumber( + context.evaluateString( + scope, scriptSourceText, "", 1, null)); + }); + } + + private String runScriptAsString(final String scriptSourceText, final Object value) { + return ContextFactory.getGlobal() + .call( + context -> { + Scriptable scope = context.initStandardObjects(global); + scope.put("value", scope, Context.javaToJS(value, scope)); + return Context.toString( + context.evaluateString(scope, scriptSourceText, "", 1, null)); + }); + } +} diff --git a/testsrc/org/mozilla/javascript/tests/FindingJavaStringsFromJSArray.java b/testsrc/org/mozilla/javascript/tests/FindingJavaStringsFromJSArray.java new file mode 100644 index 0000000000..140d2ef5ef --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/FindingJavaStringsFromJSArray.java @@ -0,0 +1,94 @@ +package org.mozilla.javascript.tests; + +import java.util.ArrayList; +import java.util.List; +import junit.framework.TestCase; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.ContextFactory; +import org.mozilla.javascript.Scriptable; + +public class FindingJavaStringsFromJSArray extends TestCase { + public void testFindingJSStringFromJavaList() { + List list = new ArrayList<>(); + list.add("foo"); + list.add("bar"); + list.add("baz"); + + assertEquals(-1, runScriptAsInt("value.indexOf(\"foobar\")", list)); + assertEquals(1, runScriptAsInt("value.indexOf(\"bar\")", list)); + assertEquals(0, runScriptAsInt("value.includes(\"foobar\")", list)); + assertEquals(1, runScriptAsInt("value.includes(\"bar\")", list)); + } + + public void testFindingJSStringFromJavaArray() { + String[] array = new String[3]; + array[0] = "foo"; + array[1] = "bar"; + array[2] = "baz"; + + assertEquals(-1, runScriptAsInt("value.indexOf(\"foobar\")", array)); + assertEquals(1, runScriptAsInt("value.indexOf(\"bar\")", array)); + assertEquals(0, runScriptAsInt("value.includes(\"foobar\")", array)); + assertEquals(1, runScriptAsInt("value.includes(\"bar\")", array)); + } + + public void testFindingJavaStringFromJavaList() { + List list = new ArrayList<>(); + list.add("foo"); + list.add("bar"); + list.add("baz"); + + assertEquals(-1, runScriptAsInt("value.indexOf(value2)", list, "foobar")); + assertEquals(1, runScriptAsInt("value.indexOf(value2)", list, "bar")); + assertEquals(0, runScriptAsInt("value.includes(value2)", list, "foobar")); + assertEquals(1, runScriptAsInt("value.includes(value2)", list, "bar")); + } + + public void testFindingJavaStringFromJavaArray() { + String[] array = new String[3]; + array[0] = "foo"; + array[1] = "bar"; + array[2] = "baz"; + + assertEquals(-1, runScriptAsInt("value.indexOf(value2)", array, "foobar")); + assertEquals(1, runScriptAsInt("value.indexOf(value2)", array, "bar")); + } + + public void testFindingJavaStringFromJSArray() { + assertEquals(-1, runScriptAsInt("[\"foo\", \"bar\", \"baz\"].indexOf(value)", "foobar")); + assertEquals(1, runScriptAsInt("[\"foo\", \"bar\", \"baz\"].indexOf(value)", "bar")); + } + + public void testFindingJSStringFromJSArray() { + assertEquals(-1, runScriptAsInt("[\"foo\", \"bar\", \"baz\"].indexOf(\"foobar\")", null)); + assertEquals(1, runScriptAsInt("[\"foo\", \"bar\", \"baz\"].indexOf(\"bar\")", null)); + } + + private int runScriptAsInt(final String scriptSourceText, final Object value) { + return ContextFactory.getGlobal() + .call( + context -> { + Scriptable scope = context.initStandardObjects(); + scope.put("value", scope, Context.javaToJS(value, scope)); + return (int) + Context.toNumber( + context.evaluateString( + scope, scriptSourceText, "", 1, null)); + }); + } + + private int runScriptAsInt( + final String scriptSourceText, final Object value, final Object value2) { + return ContextFactory.getGlobal() + .call( + context -> { + Scriptable scope = context.initStandardObjects(); + scope.put("value", scope, Context.javaToJS(value, scope)); + scope.put("value2", scope, Context.javaToJS(value2, scope)); + return (int) + Context.toNumber( + context.evaluateString( + scope, scriptSourceText, "", 1, null)); + }); + } +} From faeada0e575b81a32142433baafe623789d95b4a Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Tue, 9 Nov 2021 13:28:50 +0100 Subject: [PATCH 4/5] Fix Test from #561 to pass current code source --- .../tests/FindingJavaStringsFromJSArray.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/testsrc/org/mozilla/javascript/tests/FindingJavaStringsFromJSArray.java b/testsrc/org/mozilla/javascript/tests/FindingJavaStringsFromJSArray.java index 140d2ef5ef..19c0e19979 100644 --- a/testsrc/org/mozilla/javascript/tests/FindingJavaStringsFromJSArray.java +++ b/testsrc/org/mozilla/javascript/tests/FindingJavaStringsFromJSArray.java @@ -16,8 +16,8 @@ public void testFindingJSStringFromJavaList() { assertEquals(-1, runScriptAsInt("value.indexOf(\"foobar\")", list)); assertEquals(1, runScriptAsInt("value.indexOf(\"bar\")", list)); - assertEquals(0, runScriptAsInt("value.includes(\"foobar\")", list)); - assertEquals(1, runScriptAsInt("value.includes(\"bar\")", list)); + assertEquals(0, runScriptAsInt("Array.prototype.includes.call(value, \"foobar\")", list)); + assertEquals(1, runScriptAsInt("Array.prototype.includes.call(value, \"bar\")", list)); } public void testFindingJSStringFromJavaArray() { @@ -40,8 +40,10 @@ public void testFindingJavaStringFromJavaList() { assertEquals(-1, runScriptAsInt("value.indexOf(value2)", list, "foobar")); assertEquals(1, runScriptAsInt("value.indexOf(value2)", list, "bar")); - assertEquals(0, runScriptAsInt("value.includes(value2)", list, "foobar")); - assertEquals(1, runScriptAsInt("value.includes(value2)", list, "bar")); + assertEquals( + 0, runScriptAsInt("Array.prototype.includes.call(value, value2)", list, "foobar")); + assertEquals( + 1, runScriptAsInt("Array.prototype.includes.call(value, value2)", list, "bar")); } public void testFindingJavaStringFromJavaArray() { From f9af7e2cb054150af3b0e29e6e8852b8f3f205ca Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Fri, 12 Nov 2021 09:57:10 +0100 Subject: [PATCH 5/5] Trigger rebuild