Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve NativeJavaList to behave more like NativeArray 2 #1083

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
19 changes: 14 additions & 5 deletions rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -3526,6 +3526,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;
}
Expand Down Expand Up @@ -3685,6 +3691,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;
Expand Down Expand Up @@ -3715,9 +3728,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) {
Expand All @@ -3726,8 +3736,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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Number> 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<String> 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<String> 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));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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<String> 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("Array.prototype.includes.call(value, \"foobar\")", list));
assertEquals(1, runScriptAsInt("Array.prototype.includes.call(value, \"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<String> 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("Array.prototype.includes.call(value, value2)", list, "foobar"));
assertEquals(
1, runScriptAsInt("Array.prototype.includes.call(value, 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));
});
}
}
Loading
Loading