Java Code Examples for jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator#arrayLikeIterator()

The following examples show how to use jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator#arrayLikeIterator() . 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: NativeJSAdapter.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Iterator<String> propertyIterator() {
    // Try __getIds__ first, if not found then try __getKeys__
    // In jdk6, we had added "__getIds__" so this is just for compatibility.
    Object func = adaptee.get(__getIds__);
    if (!(func instanceof ScriptFunction)) {
        func = adaptee.get(__getKeys__);
    }

    Object obj;
    if (func instanceof ScriptFunction) {
        obj = ScriptRuntime.apply((ScriptFunction)func, adaptee);
    } else {
        obj = new NativeArray(0);
    }

    final List<String> array = new ArrayList<>();
    for (final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(obj); iter.hasNext(); ) {
        array.add((String)iter.next());
    }

    return array.iterator();
}
 
Example 2
Source File: NativeJSAdapter.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Iterator<String> propertyIterator() {
    // Try __getIds__ first, if not found then try __getKeys__
    // In jdk6, we had added "__getIds__" so this is just for compatibility.
    Object func = adaptee.get(__getIds__);
    if (!(func instanceof ScriptFunction)) {
        func = adaptee.get(__getKeys__);
    }

    Object obj;
    if (func instanceof ScriptFunction) {
        obj = ScriptRuntime.apply((ScriptFunction)func, adaptee);
    } else {
        obj = new NativeArray(0);
    }

    final List<String> array = new ArrayList<>();
    for (final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(obj); iter.hasNext(); ) {
        array.add((String)iter.next());
    }

    return array.iterator();
}
 
Example 3
Source File: NativeJSAdapter.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Iterator<String> propertyIterator() {
    // Try __getIds__ first, if not found then try __getKeys__
    // In jdk6, we had added "__getIds__" so this is just for compatibility.
    Object func = adaptee.get(__getIds__);
    if (!(func instanceof ScriptFunction)) {
        func = adaptee.get(__getKeys__);
    }

    Object obj;
    if (func instanceof ScriptFunction) {
        obj = ScriptRuntime.apply((ScriptFunction)func, adaptee);
    } else {
        obj = new NativeArray(0);
    }

    final List<String> array = new ArrayList<>();
    for (final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(obj); iter.hasNext(); ) {
        array.add((String)iter.next());
    }

    return array.iterator();
}
 
Example 4
Source File: NativeJSAdapter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Iterator<String> propertyIterator() {
    // Try __getIds__ first, if not found then try __getKeys__
    // In jdk6, we had added "__getIds__" so this is just for compatibility.
    Object func = adaptee.get(__getIds__);
    if (!(func instanceof ScriptFunction)) {
        func = adaptee.get(__getKeys__);
    }

    Object obj;
    if (func instanceof ScriptFunction) {
        obj = ScriptRuntime.apply((ScriptFunction)func, adaptee);
    } else {
        obj = new NativeArray(0);
    }

    final List<String> array = new ArrayList<>();
    for (final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(obj); iter.hasNext(); ) {
        array.add((String)iter.next());
    }

    return array.iterator();
}
 
Example 5
Source File: NativeJSAdapter.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Iterator<String> propertyIterator() {
    // Try __getIds__ first, if not found then try __getKeys__
    // In jdk6, we had added "__getIds__" so this is just for compatibility.
    Object func = adaptee.get(__getIds__);
    if (!(func instanceof ScriptFunction)) {
        func = adaptee.get(__getKeys__);
    }

    Object obj;
    if (func instanceof ScriptFunction) {
        obj = ScriptRuntime.apply((ScriptFunction)func, adaptee);
    } else {
        obj = new NativeArray(0);
    }

    final List<String> array = new ArrayList<>();
    for (final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(obj); iter.hasNext(); ) {
        array.add((String)iter.next());
    }

    return array.iterator();
}
 
Example 6
Source File: NativeJSAdapter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Iterator<Object> valueIterator() {
    final Object obj = callAdaptee(new NativeArray(0), __getValues__);
    return ArrayLikeIterator.arrayLikeIterator(obj);
}
 
Example 7
Source File: NativeJSAdapter.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Iterator<Object> valueIterator() {
    final Object obj = callAdaptee(new NativeArray(0), __getValues__);
    return ArrayLikeIterator.arrayLikeIterator(obj);
}
 
Example 8
Source File: NativeJSON.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (replacer instanceof ScriptFunction) {
        state.replacerFunction = (ScriptFunction) replacer;
    } else if (isArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        final int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (JSType.isString(modSpace)) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, 0);

    return str("", wrapper, state);
}
 
Example 9
Source File: NativeJSON.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (replacer instanceof ScriptFunction) {
        state.replacerFunction = (ScriptFunction) replacer;
    } else if (isArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (modSpace instanceof String || modSpace instanceof ConsString) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, false);

    return str("", wrapper, state);
}
 
Example 10
Source File: NativeJSAdapter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Iterator<Object> valueIterator() {
    final Object obj = callAdaptee(new NativeArray(0), __getValues__);
    return ArrayLikeIterator.arrayLikeIterator(obj);
}
 
Example 11
Source File: NativeJSON.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (Bootstrap.isCallable(replacer)) {
        state.replacerFunction = replacer;
    } else if (isArray(replacer) ||
            isJSObjectArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        final int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (JSType.isString(modSpace)) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, 0);

    return str("", wrapper, state);
}
 
Example 12
Source File: NativeJSAdapter.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Iterator<Object> valueIterator() {
    final Object obj = callAdaptee(new NativeArray(0), __getValues__);
    return ArrayLikeIterator.arrayLikeIterator(obj);
}
 
Example 13
Source File: NativeJSON.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (Bootstrap.isCallable(replacer)) {
        state.replacerFunction = replacer;
    } else if (isArray(replacer) ||
            isJSObjectArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        final int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (JSType.isString(modSpace)) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, 0);

    return str("", wrapper, state);
}
 
Example 14
Source File: NativeJSAdapter.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Iterator<Object> valueIterator() {
    final Object obj = callAdaptee(new NativeArray(0), __getValues__);
    return ArrayLikeIterator.arrayLikeIterator(obj);
}
 
Example 15
Source File: NativeJSON.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (Bootstrap.isCallable(replacer)) {
        state.replacerFunction = replacer;
    } else if (isArray(replacer) ||
            isJSObjectArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        final int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (JSType.isString(modSpace)) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, 0);

    return str("", wrapper, state);
}
 
Example 16
Source File: NativeJSAdapter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Iterator<Object> valueIterator() {
    final Object obj = callAdaptee(new NativeArray(0), __getValues__);
    return ArrayLikeIterator.arrayLikeIterator(obj);
}
 
Example 17
Source File: NativeJSON.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (Bootstrap.isCallable(replacer)) {
        state.replacerFunction = replacer;
    } else if (isArray(replacer) ||
            isJSObjectArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        final int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (JSType.isString(modSpace)) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, 0);

    return str("", wrapper, state);
}
 
Example 18
Source File: NativeJSAdapter.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Iterator<Object> valueIterator() {
    final Object obj = callAdaptee(new NativeArray(0), __getValues__);
    return ArrayLikeIterator.arrayLikeIterator(obj);
}
 
Example 19
Source File: NativeJSON.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (Bootstrap.isCallable(replacer)) {
        state.replacerFunction = replacer;
    } else if (isArray(replacer) ||
            isJSObjectArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        final int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (JSType.isString(modSpace)) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, 0);

    return str("", wrapper, state);
}
 
Example 20
Source File: NativeJSON.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * ECMA 15.12.3 stringify ( value [ , replacer [ , space ] ] )
 *
 * @param self     self reference
 * @param value    ECMA script value (usually object or array)
 * @param replacer either a function or an array of strings and numbers
 * @param space    optional parameter - allows result to have whitespace injection
 *
 * @return a string in JSON format
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object stringify(final Object self, final Object value, final Object replacer, final Object space) {
    // The stringify method takes a value and an optional replacer, and an optional
    // space parameter, and returns a JSON text. The replacer can be a function
    // that can replace values, or an array of strings that will select the keys.

    // A default replacer method can be provided. Use of the space parameter can
    // produce text that is more easily readable.

    final StringifyState state = new StringifyState();

    // If there is a replacer, it must be a function or an array.
    if (replacer instanceof ScriptFunction) {
        state.replacerFunction = (ScriptFunction) replacer;
    } else if (isArray(replacer) ||
            replacer instanceof Iterable ||
            (replacer != null && replacer.getClass().isArray())) {

        state.propertyList = new ArrayList<>();

        final Iterator<Object> iter = ArrayLikeIterator.arrayLikeIterator(replacer);

        while (iter.hasNext()) {
            String item = null;
            final Object v = iter.next();

            if (v instanceof String) {
                item = (String) v;
            } else if (v instanceof ConsString) {
                item = v.toString();
            } else if (v instanceof Number ||
                    v instanceof NativeNumber ||
                    v instanceof NativeString) {
                item = JSType.toString(v);
            }

            if (item != null) {
                state.propertyList.add(item);
            }
        }
    }

    // If the space parameter is a number, make an indent
    // string containing that many spaces.

    String gap;

    // modifiable 'space' - parameter is final
    Object modSpace = space;
    if (modSpace instanceof NativeNumber) {
        modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
    } else if (modSpace instanceof NativeString) {
        modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
    }

    if (modSpace instanceof Number) {
        int indent = Math.min(10, JSType.toInteger(modSpace));
        if (indent < 1) {
            gap = "";
        } else {
            final StringBuilder sb = new StringBuilder();
            for (int i = 0; i < indent; i++) {
                sb.append(' ');
            }
            gap = sb.toString();
        }
    } else if (modSpace instanceof String || modSpace instanceof ConsString) {
        final String str = modSpace.toString();
        gap = str.substring(0, Math.min(10, str.length()));
    } else {
        gap = "";
    }

    state.gap = gap;

    final ScriptObject wrapper = Global.newEmptyInstance();
    wrapper.set("", value, false);

    return str("", wrapper, state);
}