org.mozilla.javascript.ConsString Java Examples

The following examples show how to use org.mozilla.javascript.ConsString. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LinkedMapImpl.java    From es6draft with MIT License 6 votes vote down vote up
@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;
}
 
Example #2
Source File: Operators.java    From es6draft with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: ScriptIterators.java    From es6draft with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: AbstractOperations.java    From es6draft with MIT License 5 votes vote down vote up
/**
 * 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();
}
 
Example #5
Source File: AbstractOperations.java    From es6draft with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: UTF16Encoding.java    From es6draft with MIT License 5 votes vote down vote up
@Override
public byte[] toBytes(CharSequence cs) {
    if (cs instanceof ConsString) {
        return ((ConsString) cs).toByteArray(new byte[cs.length() * 2 + 2]);
    }
    return toBytes(cs.toString());
}
 
Example #7
Source File: UCS2Encoding.java    From es6draft with MIT License 5 votes vote down vote up
@Override
public byte[] toBytes(CharSequence cs) {
    if (cs instanceof ConsString) {
        return ((ConsString) cs).toByteArray(new byte[cs.length() * 2 + 2]);
    }
    return toBytes(cs.toString());
}
 
Example #8
Source File: ContinuationsApiTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
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());
    
}
 
Example #9
Source File: ContinuationsApiTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
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());

  }
 
Example #10
Source File: ScriptIterators.java    From es6draft with MIT License 4 votes vote down vote up
/**
 * 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;
}