@Override protected Object hashKey(Object key) { if (key instanceof ConsString) { // ConsString -> String return ((ConsString) key).toString(); } if (key instanceof Integer) { // int -> double return (double) (Integer) key; } if (key instanceof Long) { // long -> double return (double) (Long) key; } if (key instanceof Double) { // Map +/-0 to +0 to enforce SameValueZero comparison semantics. double v = (Double) key; return v == 0 ? +0d : v; } if (key instanceof SIMDValue) { // Map +/-0 to +0 to enforce SameValueZero comparison semantics. return hashKeySIMD((SIMDValue) key); } return key; }
/** * 12.8 Additive Operators<br> * 12.8.3 The Addition operator ( + ) * * @param lstr * the left-hand side operand * @param rstr * the right-hand side operand * @param cx * the execution context * @return the concatenated string */ public static CharSequence add(CharSequence lstr, CharSequence rstr, ExecutionContext cx) { int llen = lstr.length(), rlen = rstr.length(); if (llen == 0) { return rstr; } if (rlen == 0) { return lstr; } int newlen = llen + rlen; if (newlen < 0 || newlen > StringObject.MAX_LENGTH) { throw newInternalError(cx, Messages.Key.InvalidStringSize); } if (newlen <= 10) { // return new StringBuilder(newlen).append(lstr).append(rstr).toString(); return inlineString(lstr, rstr, llen, rlen); } return new ConsString(lstr, rstr); }
/** * Returns a {@link ScriptIterator} for {@code iterable}. * * @param cx * the execution context * @param iterable * the iterable object * @param method * the iterator method * @return the iterator object or {@code null} */ public static ScriptIterator<?> GetScriptIterator(ExecutionContext cx, Object iterable, Callable method) { if (iterable instanceof ArrayObject) { ArrayObject array = (ArrayObject) iterable; if (ArrayScriptIterator.isBuiltinIterator(cx, array, method)) { return new ArrayScriptIterator(cx, array); } } else if (iterable instanceof TypedArrayObject) { TypedArrayObject typedArray = (TypedArrayObject) iterable; if (TypedArrayScriptIterator.isBuiltinIterator(cx, typedArray, method)) { return new TypedArrayScriptIterator(cx, typedArray); } } else if (iterable instanceof String || iterable instanceof ConsString) { if (StringScriptIterator.isBuiltinIterator(cx, method)) { return new StringScriptIterator(cx, iterable.toString()); } } else if (iterable instanceof MapObject) { MapObject map = (MapObject) iterable; if (MapScriptIterator.isBuiltinIterator(cx, map, method)) { return new MapScriptIterator(cx, map); } } else if (iterable instanceof SetObject) { SetObject set = (SetObject) iterable; if (SetScriptIterator.isBuiltinIterator(cx, set, method)) { return new SetScriptIterator(cx, set); } } else if (iterable instanceof ArgumentsObject) { ArgumentsObject arguments = (ArgumentsObject) iterable; if (ArgumentsScriptIterator.isBuiltinIterator(cx, arguments, method)) { return new ArgumentsScriptIterator(cx, arguments); } } return null; }
/** * 7.1.12 ToString ( argument ) * * @param cx * the execution context * @param value * the argument value * @return the string result */ public static String ToFlatString(ExecutionContext cx, Object value) { // Inlined: `if (Type.isString(value)) return Type.stringValue(value);` if (value instanceof String) { return (String) value; } if (value instanceof ConsString) { return ((ConsString) value).toString(); } return ToStringSlow(cx, value).toString(); }
/** * 7.1.12 ToString ( argument ) * * @param cx * the execution context * @param value * the argument value * @return the string result */ public static CharSequence ToString(ExecutionContext cx, Object value) { // Inlined: `if (Type.isString(value)) return Type.stringValue(value);` if (value instanceof String) { return (String) value; } if (value instanceof ConsString) { return (ConsString) value; } return ToStringSlow(cx, value); }
@Override public byte[] toBytes(CharSequence cs) { if (cs instanceof ConsString) { return ((ConsString) cs).toByteArray(new byte[cs.length() * 2 + 2]); } return toBytes(cs.toString()); }
public void testConsStringSerialization() throws IOException, ClassNotFoundException { ConsString r1 = new ConsString("foo", "bar"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(r1); oos.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); CharSequence r2 = (CharSequence) ois.readObject(); assertEquals("still the same at the other end", r1.toString(), r2.toString()); }
/** * Returns a {@link ScriptIterator} for {@code iterable}. * * @param cx * the execution context * @param iterable * the iterable object * @return the iterator object or {@code null} */ public static ScriptIterator<?> GetScriptIterator(ExecutionContext cx, Object iterable) { if (iterable instanceof ArrayObject) { ArrayObject array = (ArrayObject) iterable; if (ArrayScriptIterator.isBuiltinIterator(cx, array)) { return new ArrayScriptIterator(cx, array); } } else if (iterable instanceof ArrayIteratorObject) { ArrayIteratorObject arrayIterator = (ArrayIteratorObject) iterable; if (ArrayIteratorScriptIterator.isBuiltinIterator(cx, arrayIterator)) { return new ArrayIteratorScriptIterator(cx, arrayIterator); } } else if (iterable instanceof TypedArrayObject) { TypedArrayObject typedArray = (TypedArrayObject) iterable; if (TypedArrayScriptIterator.isBuiltinIterator(cx, typedArray)) { return new TypedArrayScriptIterator(cx, typedArray); } } else if (iterable instanceof String || iterable instanceof ConsString) { if (StringScriptIterator.isBuiltinIterator(cx)) { return new StringScriptIterator(cx, iterable.toString()); } } else if (iterable instanceof MapObject) { MapObject map = (MapObject) iterable; if (MapScriptIterator.isBuiltinIterator(cx, map)) { return new MapScriptIterator(cx, map); } } else if (iterable instanceof MapIteratorObject) { MapIteratorObject mapIterator = (MapIteratorObject) iterable; if (MapIteratorScriptIterator.isBuiltinIterator(cx, mapIterator)) { return new MapIteratorScriptIterator(cx, mapIterator); } } else if (iterable instanceof SetObject) { SetObject set = (SetObject) iterable; if (SetScriptIterator.isBuiltinIterator(cx, set)) { return new SetScriptIterator(cx, set); } } else if (iterable instanceof SetIteratorObject) { SetIteratorObject setIterator = (SetIteratorObject) iterable; if (SetIteratorScriptIterator.isBuiltinIterator(cx, setIterator)) { return new SetIteratorScriptIterator(cx, setIterator); } } else if (iterable instanceof ArgumentsObject) { ArgumentsObject arguments = (ArgumentsObject) iterable; if (ArgumentsScriptIterator.isBuiltinIterator(cx, arguments)) { return new ArgumentsScriptIterator(cx, arguments); } } else if (iterable instanceof GeneratorObject) { GeneratorObject generator = (GeneratorObject) iterable; if (GeneratorScriptIterator.isBuiltinIterator(cx, generator)) { return new GeneratorScriptIterator(cx, generator); } } return null; }
/** * Converts java representation into nearest JavaScript representation. * * @param value Value to be converted. * @param scope Scope where is the value. * @return Converted object. */ public static Object javaToJS(Object value, Scriptable scope) { if (value instanceof ConsString) { value = value.toString(); } return Context.javaToJS(value, scope); }