mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8074031: Canonicalize is-a-JS-string tests
Reviewed-by: hannesw, lagergren
This commit is contained in:
parent
74c88b0515
commit
b5aaccb15f
14 changed files with 52 additions and 38 deletions
|
@ -25,6 +25,8 @@
|
|||
|
||||
package jdk.nashorn.internal.lookup;
|
||||
|
||||
import static jdk.nashorn.internal.runtime.JSType.isString;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
|
@ -36,7 +38,6 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import jdk.nashorn.internal.runtime.ConsString;
|
||||
import jdk.nashorn.internal.runtime.Context;
|
||||
import jdk.nashorn.internal.runtime.Debug;
|
||||
import jdk.nashorn.internal.runtime.ScriptObject;
|
||||
|
@ -343,7 +344,7 @@ public final class MethodHandleFactory {
|
|||
final Object d = data[i];
|
||||
if (d == null) {
|
||||
sb.append("<null> ");
|
||||
} else if (d instanceof String || d instanceof ConsString) {
|
||||
} else if (isString(d)) {
|
||||
sb.append(d.toString());
|
||||
sb.append(' ');
|
||||
} else if (d.getClass().isArray()) {
|
||||
|
|
|
@ -28,6 +28,7 @@ package jdk.nashorn.internal.objects;
|
|||
import static jdk.nashorn.internal.lookup.Lookup.MH;
|
||||
import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError;
|
||||
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
|
||||
import static jdk.nashorn.internal.runtime.JSType.isString;
|
||||
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -55,7 +56,6 @@ import jdk.nashorn.internal.lookup.Lookup;
|
|||
import jdk.nashorn.internal.objects.annotations.Attribute;
|
||||
import jdk.nashorn.internal.objects.annotations.Property;
|
||||
import jdk.nashorn.internal.objects.annotations.ScriptClass;
|
||||
import jdk.nashorn.internal.runtime.ConsString;
|
||||
import jdk.nashorn.internal.runtime.Context;
|
||||
import jdk.nashorn.internal.runtime.ECMAErrors;
|
||||
import jdk.nashorn.internal.runtime.GlobalConstants;
|
||||
|
@ -577,7 +577,7 @@ public final class Global extends ScriptObject implements Scope {
|
|||
return new NativeBoolean((Boolean)obj, this);
|
||||
} else if (obj instanceof Number) {
|
||||
return new NativeNumber(((Number)obj).doubleValue(), this);
|
||||
} else if (obj instanceof String || obj instanceof ConsString) {
|
||||
} else if (isString(obj)) {
|
||||
return new NativeString((CharSequence)obj, this);
|
||||
} else if (obj instanceof Object[]) { // extension
|
||||
return new NativeArray(ArrayData.allocate((Object[])obj), this);
|
||||
|
@ -604,7 +604,7 @@ public final class Global extends ScriptObject implements Scope {
|
|||
* @return guarded invocation
|
||||
*/
|
||||
public static GuardedInvocation primitiveLookup(final LinkRequest request, final Object self) {
|
||||
if (self instanceof String || self instanceof ConsString) {
|
||||
if (isString(self)) {
|
||||
return NativeString.lookupPrimitive(request, self);
|
||||
} else if (self instanceof Number) {
|
||||
return NativeNumber.lookupPrimitive(request, self);
|
||||
|
@ -621,7 +621,7 @@ public final class Global extends ScriptObject implements Scope {
|
|||
* @return method handle to create wrapper objects for primitive receiver
|
||||
*/
|
||||
public static MethodHandle getPrimitiveWrapFilter(final Object self) {
|
||||
if (self instanceof String || self instanceof ConsString) {
|
||||
if (isString(self)) {
|
||||
return NativeString.WRAPFILTER;
|
||||
} else if (self instanceof Number) {
|
||||
return NativeNumber.WRAPFILTER;
|
||||
|
@ -947,7 +947,7 @@ public final class Global extends ScriptObject implements Scope {
|
|||
* This is directly invoked from generated when eval(code) is called in user code
|
||||
*/
|
||||
public static Object directEval(final Object self, final Object str, final Object callThis, final Object location, final boolean strict) {
|
||||
if (!(str instanceof String || str instanceof ConsString)) {
|
||||
if (!isString(str)) {
|
||||
return str;
|
||||
}
|
||||
final Global global = Global.instanceFrom(self);
|
||||
|
|
|
@ -30,6 +30,7 @@ import static java.lang.Double.isInfinite;
|
|||
import static java.lang.Double.isNaN;
|
||||
import static jdk.nashorn.internal.runtime.ECMAErrors.rangeError;
|
||||
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.Callable;
|
||||
|
@ -40,7 +41,6 @@ import jdk.nashorn.internal.objects.annotations.ScriptClass;
|
|||
import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
|
||||
import jdk.nashorn.internal.objects.annotations.Where;
|
||||
import jdk.nashorn.internal.parser.DateParser;
|
||||
import jdk.nashorn.internal.runtime.ConsString;
|
||||
import jdk.nashorn.internal.runtime.JSType;
|
||||
import jdk.nashorn.internal.runtime.PropertyMap;
|
||||
import jdk.nashorn.internal.runtime.ScriptEnvironment;
|
||||
|
@ -183,7 +183,7 @@ public final class NativeDate extends ScriptObject {
|
|||
case 1:
|
||||
double num;
|
||||
final Object arg = JSType.toPrimitive(args[0]);
|
||||
if (arg instanceof String || arg instanceof ConsString) {
|
||||
if (JSType.isString(arg)) {
|
||||
num = parseDateString(arg.toString());
|
||||
} else {
|
||||
num = timeClip(JSType.toNumber(args[0]));
|
||||
|
|
|
@ -181,7 +181,7 @@ public final class NativeJSON extends ScriptObject {
|
|||
}
|
||||
gap = sb.toString();
|
||||
}
|
||||
} else if (modSpace instanceof String || modSpace instanceof ConsString) {
|
||||
} else if (JSType.isString(modSpace)) {
|
||||
final String str = modSpace.toString();
|
||||
gap = str.substring(0, Math.min(10, str.length()));
|
||||
} else {
|
||||
|
|
|
@ -90,7 +90,7 @@ public final class NativeString extends ScriptObject implements OptimisticBuilti
|
|||
|
||||
private NativeString(final CharSequence value, final ScriptObject proto, final PropertyMap map) {
|
||||
super(proto, map);
|
||||
assert value instanceof String || value instanceof ConsString;
|
||||
assert JSType.isString(value);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
@ -155,7 +155,7 @@ public final class NativeString extends ScriptObject implements OptimisticBuilti
|
|||
final Object self = request.getReceiver();
|
||||
final Class<?> returnType = desc.getMethodType().returnType();
|
||||
|
||||
if (returnType == Object.class && (self instanceof String || self instanceof ConsString)) {
|
||||
if (returnType == Object.class && JSType.isString(self)) {
|
||||
try {
|
||||
return new GuardedInvocation(MH.findStatic(MethodHandles.lookup(), NativeString.class, "get", desc.getMethodType()), NashornGuards.getInstanceOf2Guard(String.class, ConsString.class));
|
||||
} catch (final LookupException e) {
|
||||
|
@ -1312,7 +1312,7 @@ public final class NativeString extends ScriptObject implements OptimisticBuilti
|
|||
}
|
||||
|
||||
private static CharSequence getCharSequence(final Object self) {
|
||||
if (self instanceof String || self instanceof ConsString) {
|
||||
if (JSType.isString(self)) {
|
||||
return (CharSequence)self;
|
||||
} else if (self instanceof NativeString) {
|
||||
return ((NativeString)self).getValue();
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
|
||||
package jdk.nashorn.internal.runtime;
|
||||
|
||||
import static jdk.nashorn.internal.runtime.JSType.isString;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
|
@ -52,8 +54,8 @@ public final class ConsString implements CharSequence {
|
|||
* @param right right char sequence
|
||||
*/
|
||||
public ConsString(final CharSequence left, final CharSequence right) {
|
||||
assert left instanceof String || left instanceof ConsString;
|
||||
assert right instanceof String || right instanceof ConsString;
|
||||
assert isString(left);
|
||||
assert isString(right);
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
length = left.length() + right.length();
|
||||
|
|
|
@ -311,7 +311,7 @@ public enum JSType {
|
|||
return JSType.BOOLEAN;
|
||||
}
|
||||
|
||||
if (obj instanceof String || obj instanceof ConsString) {
|
||||
if (isString(obj)) {
|
||||
return JSType.STRING;
|
||||
}
|
||||
|
||||
|
@ -349,7 +349,7 @@ public enum JSType {
|
|||
return JSType.BOOLEAN;
|
||||
}
|
||||
|
||||
if (obj instanceof String || obj instanceof ConsString) {
|
||||
if (isString(obj)) {
|
||||
return JSType.STRING;
|
||||
}
|
||||
|
||||
|
@ -455,8 +455,7 @@ public enum JSType {
|
|||
obj == ScriptRuntime.UNDEFINED ||
|
||||
obj instanceof Boolean ||
|
||||
obj instanceof Number ||
|
||||
obj instanceof String ||
|
||||
obj instanceof ConsString;
|
||||
isString(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -577,7 +576,7 @@ public enum JSType {
|
|||
return num != 0 && !Double.isNaN(num);
|
||||
}
|
||||
|
||||
if (obj instanceof String || obj instanceof ConsString) {
|
||||
if (isString(obj)) {
|
||||
return ((CharSequence)obj).length() > 0;
|
||||
}
|
||||
|
||||
|
@ -627,6 +626,15 @@ public enum JSType {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if object represents a primitive JavaScript string value.
|
||||
* @param obj the object
|
||||
* @return true if the object represents a primitive JavaScript string value.
|
||||
*/
|
||||
public static boolean isString(final Object obj) {
|
||||
return obj instanceof String || obj instanceof ConsString;
|
||||
}
|
||||
|
||||
/**
|
||||
* JavaScript compliant conversion of integer to String
|
||||
*
|
||||
|
|
|
@ -28,6 +28,7 @@ package jdk.nashorn.internal.runtime;
|
|||
import static jdk.nashorn.internal.lookup.Lookup.MH;
|
||||
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
|
||||
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.Serializable;
|
||||
|
@ -456,8 +457,7 @@ public abstract class ScriptFunctionData implements Serializable {
|
|||
}
|
||||
|
||||
static boolean isPrimitiveThis(final Object obj) {
|
||||
return obj instanceof String || obj instanceof ConsString ||
|
||||
obj instanceof Number || obj instanceof Boolean;
|
||||
return JSType.isString(obj) || obj instanceof Number || obj instanceof Boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -32,6 +32,7 @@ import static jdk.nashorn.internal.runtime.ECMAErrors.referenceError;
|
|||
import static jdk.nashorn.internal.runtime.ECMAErrors.syntaxError;
|
||||
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
|
||||
import static jdk.nashorn.internal.runtime.JSType.isRepresentableAsInt;
|
||||
import static jdk.nashorn.internal.runtime.JSType.isString;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
|
@ -56,7 +57,6 @@ import jdk.nashorn.internal.objects.NativeObject;
|
|||
import jdk.nashorn.internal.parser.Lexer;
|
||||
import jdk.nashorn.internal.runtime.linker.Bootstrap;
|
||||
|
||||
|
||||
/**
|
||||
* Utilities to be called by JavaScript runtime API and generated classes.
|
||||
*/
|
||||
|
@ -564,8 +564,7 @@ public final class ScriptRuntime {
|
|||
final Object xPrim = JSType.toPrimitive(x);
|
||||
final Object yPrim = JSType.toPrimitive(y);
|
||||
|
||||
if (xPrim instanceof String || yPrim instanceof String
|
||||
|| xPrim instanceof ConsString || yPrim instanceof ConsString) {
|
||||
if (isString(xPrim) || isString(yPrim)) {
|
||||
try {
|
||||
return new ConsString(JSType.toCharSequence(xPrim), JSType.toCharSequence(yPrim));
|
||||
} catch (final IllegalArgumentException iae) {
|
||||
|
@ -1010,7 +1009,7 @@ public final class ScriptRuntime {
|
|||
px = JSType.toPrimitive(x, Number.class);
|
||||
}
|
||||
|
||||
if (JSType.ofNoFunction(px) == JSType.STRING && JSType.ofNoFunction(py) == JSType.STRING) {
|
||||
if (isString(px) && isString(py)) {
|
||||
// May be String or ConsString
|
||||
return px.toString().compareTo(py.toString()) < 0;
|
||||
}
|
||||
|
|
|
@ -107,7 +107,7 @@ public final class ScriptingFunctions {
|
|||
|
||||
if (file instanceof File) {
|
||||
f = (File)file;
|
||||
} else if (file instanceof String || file instanceof ConsString) {
|
||||
} else if (JSType.isString(file)) {
|
||||
f = new java.io.File(((CharSequence)file).toString());
|
||||
}
|
||||
|
||||
|
|
|
@ -25,11 +25,13 @@
|
|||
|
||||
package jdk.nashorn.internal.runtime.linker;
|
||||
|
||||
import static jdk.nashorn.internal.runtime.JSType.isString;
|
||||
import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_CALL;
|
||||
import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_GETMEMBER;
|
||||
import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_GETSLOT;
|
||||
import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_SETMEMBER;
|
||||
import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_SETSLOT;
|
||||
import static jdk.nashorn.internal.runtime.linker.BrowserJSObjectLinker.JSObjectHandles.JSOBJECT_CALL;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import jdk.internal.dynalink.CallSiteDescriptor;
|
||||
|
@ -40,7 +42,6 @@ import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
|
|||
import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
|
||||
import jdk.nashorn.internal.lookup.MethodHandleFactory;
|
||||
import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
|
||||
import jdk.nashorn.internal.runtime.ConsString;
|
||||
import jdk.nashorn.internal.runtime.JSType;
|
||||
|
||||
/**
|
||||
|
@ -186,7 +187,7 @@ final class BrowserJSObjectLinker implements TypeBasedGuardingDynamicLinker {
|
|||
if (index > -1) {
|
||||
return JSOBJECT_GETSLOT.invokeExact(jsobj, index);
|
||||
}
|
||||
} else if (key instanceof String || key instanceof ConsString) {
|
||||
} else if (isString(key)) {
|
||||
final String name = key.toString();
|
||||
if (name.indexOf('(') != -1) {
|
||||
return fallback.invokeExact(jsobj, (Object) name);
|
||||
|
@ -202,7 +203,7 @@ final class BrowserJSObjectLinker implements TypeBasedGuardingDynamicLinker {
|
|||
JSOBJECT_SETSLOT.invokeExact(jsobj, (int)key, value);
|
||||
} else if (key instanceof Number) {
|
||||
JSOBJECT_SETSLOT.invokeExact(jsobj, getIndex((Number)key), value);
|
||||
} else if (key instanceof String || key instanceof ConsString) {
|
||||
} else if (isString(key)) {
|
||||
JSOBJECT_SETMEMBER.invokeExact(jsobj, key.toString(), value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
|
||||
package jdk.nashorn.internal.runtime.linker;
|
||||
|
||||
import static jdk.nashorn.internal.runtime.JSType.isString;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.util.Map;
|
||||
|
@ -38,7 +40,6 @@ import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
|
|||
import jdk.nashorn.api.scripting.JSObject;
|
||||
import jdk.nashorn.internal.lookup.MethodHandleFactory;
|
||||
import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
|
||||
import jdk.nashorn.internal.runtime.ConsString;
|
||||
import jdk.nashorn.internal.runtime.JSType;
|
||||
|
||||
/**
|
||||
|
@ -166,7 +167,7 @@ final class JSObjectLinker implements TypeBasedGuardingDynamicLinker {
|
|||
if (index > -1) {
|
||||
return ((JSObject)jsobj).getSlot(index);
|
||||
}
|
||||
} else if (key instanceof String || key instanceof ConsString) {
|
||||
} else if (isString(key)) {
|
||||
final String name = key.toString();
|
||||
// get with method name and signature. delegate it to beans linker!
|
||||
if (name.indexOf('(') != -1) {
|
||||
|
@ -183,7 +184,7 @@ final class JSObjectLinker implements TypeBasedGuardingDynamicLinker {
|
|||
((JSObject)jsobj).setSlot((Integer)key, value);
|
||||
} else if (key instanceof Number) {
|
||||
((JSObject)jsobj).setSlot(getIndex((Number)key), value);
|
||||
} else if (key instanceof String || key instanceof ConsString) {
|
||||
} else if (isString(key)) {
|
||||
((JSObject)jsobj).setMember(key.toString(), value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ package jdk.nashorn.internal.runtime.linker;
|
|||
|
||||
import static jdk.nashorn.internal.lookup.Lookup.MH;
|
||||
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
|
||||
import static jdk.nashorn.internal.runtime.JSType.isString;
|
||||
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
|
@ -78,7 +79,7 @@ final class JavaArgumentConverters {
|
|||
}
|
||||
|
||||
if (obj == UNDEFINED) {
|
||||
// NOTE: same reasoning for FindBugs NP_BOOLEAN_RETURN_NUL warning as in the preceding comment.
|
||||
// NOTE: same reasoning for FindBugs NP_BOOLEAN_RETURN_NULL warning as in the preceding comment.
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -87,7 +88,7 @@ final class JavaArgumentConverters {
|
|||
return num != 0 && !Double.isNaN(num);
|
||||
}
|
||||
|
||||
if (obj instanceof String || obj instanceof ConsString) {
|
||||
if (isString(obj)) {
|
||||
return ((CharSequence) obj).length() > 0;
|
||||
}
|
||||
|
||||
|
@ -207,7 +208,7 @@ final class JavaArgumentConverters {
|
|||
return f.longValue();
|
||||
} else if (obj instanceof Number) {
|
||||
return ((Number)obj).longValue();
|
||||
} else if (obj instanceof String || obj instanceof ConsString) {
|
||||
} else if (isString(obj)) {
|
||||
return JSType.toLong(obj);
|
||||
} else if (obj instanceof Boolean) {
|
||||
return (Boolean)obj ? 1L : 0L;
|
||||
|
|
|
@ -39,6 +39,7 @@ import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
|
|||
import jdk.internal.dynalink.support.TypeUtilities;
|
||||
import jdk.nashorn.internal.objects.Global;
|
||||
import jdk.nashorn.internal.runtime.ConsString;
|
||||
import jdk.nashorn.internal.runtime.JSType;
|
||||
import jdk.nashorn.internal.runtime.ScriptRuntime;
|
||||
|
||||
/**
|
||||
|
@ -170,7 +171,7 @@ final class NashornPrimitiveLinker implements TypeBasedGuardingDynamicLinker, Gu
|
|||
|
||||
@SuppressWarnings("unused")
|
||||
private static boolean isJavaScriptPrimitive(final Object o) {
|
||||
return o instanceof String || o instanceof Boolean || o instanceof Number || o instanceof ConsString || o == null;
|
||||
return JSType.isString(o) || o instanceof Boolean || o instanceof Number || o == null;
|
||||
}
|
||||
|
||||
private static final MethodHandle GUARD_PRIMITIVE = findOwnMH("isJavaScriptPrimitive", boolean.class, Object.class);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue