Java Code Examples for jdk.nashorn.internal.runtime.ScriptFunction#isStrict()

The following examples show how to use jdk.nashorn.internal.runtime.ScriptFunction#isStrict() . 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: NativeArguments.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory to create correct Arguments object based on strict mode.
 *
 * @param arguments the actual arguments array passed
 * @param callee the callee function that uses arguments object
 * @param numParams the number of declared (named) function parameters
 * @return Arguments Object
 */
public static ScriptObject allocate(final Object[] arguments, final ScriptFunction callee, final int numParams) {
    // Strict functions won't always have a callee for arguments, and will pass null instead.
    final boolean isStrict = callee == null || callee.isStrict();
    final Global global = Global.instance();
    final ScriptObject proto = global.getObjectPrototype();
    if (isStrict) {
        return new NativeStrictArguments(arguments, numParams, proto, global.getStrictArgumentsMap());
    }
    return new NativeArguments(arguments, callee, numParams, proto, global.getArgumentsMap());
}
 
Example 2
Source File: NativeRegExp.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private String callReplaceValue(final ScriptFunction function, final RegExpMatcher matcher, final String string) {
    final Object[] groups = groups(matcher);
    final Object[] args   = Arrays.copyOf(groups, groups.length + 2);

    args[groups.length]     = matcher.start();
    args[groups.length + 1] = string;

    final Object self = function.isStrict() ? UNDEFINED : Global.instance();

    return JSType.toString(ScriptRuntime.apply(function, self, args));
}
 
Example 3
Source File: NativeArray.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static Object[] sort(final Object[] array, final Object comparefn) {
    final ScriptFunction cmp = compareFunction(comparefn);

    final List<Object> list = Arrays.asList(array);
    final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Global.instance();

    Collections.sort(list, new Comparator<Object>() {
        private final MethodHandle call_cmp = getCALL_CMP();
        @Override
        public int compare(final Object x, final Object y) {
            if (x == ScriptRuntime.UNDEFINED && y == ScriptRuntime.UNDEFINED) {
                return 0;
            } else if (x == ScriptRuntime.UNDEFINED) {
                return 1;
            } else if (y == ScriptRuntime.UNDEFINED) {
                return -1;
            }

            if (cmp != null) {
                try {
                    return (int)Math.signum((double)call_cmp.invokeExact(cmp, cmpThis, x, y));
                } catch (final RuntimeException | Error e) {
                    throw e;
                } catch (final Throwable t) {
                    throw new RuntimeException(t);
                }
            }

            return JSType.toString(x).compareTo(JSType.toString(y));
        }
    });

    return list.toArray(new Object[array.length]);
}
 
Example 4
Source File: NativeArguments.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory to create correct Arguments object based on strict mode.
 *
 * @param arguments the actual arguments array passed
 * @param callee the callee function that uses arguments object
 * @param numParams the number of declared (named) function parameters
 * @return Arguments Object
 */
public static ScriptObject allocate(final Object[] arguments, final ScriptFunction callee, final int numParams) {
    // Strict functions won't always have a callee for arguments, and will pass null instead.
    final boolean isStrict = callee == null || callee.isStrict();
    final Global global = Global.instance();
    final ScriptObject proto = global.getObjectPrototype();
    if (isStrict) {
        return new NativeStrictArguments(arguments, numParams, proto, global.getStrictArgumentsMap());
    }
    return new NativeArguments(arguments, callee, numParams, proto, global.getArgumentsMap());
}
 
Example 5
Source File: NativeArguments.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory to create correct Arguments object based on strict mode.
 *
 * @param arguments the actual arguments array passed
 * @param callee the callee function that uses arguments object
 * @param numParams the number of declared (named) function parameters
 * @return Arguments Object
 */
public static ScriptObject allocate(final Object[] arguments, final ScriptFunction callee, final int numParams) {
    // Strict functions won't always have a callee for arguments, and will pass null instead.
    final boolean isStrict = callee == null || callee.isStrict();
    final Global global = Global.instance();
    final ScriptObject proto = global.getObjectPrototype();
    if (isStrict) {
        return new NativeStrictArguments(arguments, numParams, proto, NativeStrictArguments.getInitialMap());
    }
    return new NativeArguments(arguments, callee, numParams, proto, NativeArguments.getInitialMap());
}
 
Example 6
Source File: NativeRegExp.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private String callReplaceValue(final ScriptFunction function, final RegExpMatcher matcher, final String string) {
    final Object[] groups = groups(matcher);
    final Object[] args   = Arrays.copyOf(groups, groups.length + 2);

    args[groups.length]     = matcher.start();
    args[groups.length + 1] = string;

    final Object self = function.isStrict() ? UNDEFINED : Global.instance();

    return JSType.toString(ScriptRuntime.apply(function, self, args));
}
 
Example 7
Source File: NativeArray.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static Object[] sort(final Object[] array, final Object comparefn) {
    final ScriptFunction cmp = compareFunction(comparefn);

    final List<Object> list = Arrays.asList(array);
    final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Global.instance();

    Collections.sort(list, new Comparator<Object>() {
        private final MethodHandle call_cmp = getCALL_CMP();
        @Override
        public int compare(final Object x, final Object y) {
            if (x == ScriptRuntime.UNDEFINED && y == ScriptRuntime.UNDEFINED) {
                return 0;
            } else if (x == ScriptRuntime.UNDEFINED) {
                return 1;
            } else if (y == ScriptRuntime.UNDEFINED) {
                return -1;
            }

            if (cmp != null) {
                try {
                    return (int)Math.signum((double)call_cmp.invokeExact(cmp, cmpThis, x, y));
                } catch (final RuntimeException | Error e) {
                    throw e;
                } catch (final Throwable t) {
                    throw new RuntimeException(t);
                }
            }

            return JSType.toString(x).compareTo(JSType.toString(y));
        }
    });

    return list.toArray(new Object[array.length]);
}
 
Example 8
Source File: NativeArguments.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory to create correct Arguments object based on strict mode.
 *
 * @param arguments the actual arguments array passed
 * @param callee the callee function that uses arguments object
 * @param numParams the number of declared (named) function parameters
 * @return Arguments Object
 */
public static ScriptObject allocate(final Object[] arguments, final ScriptFunction callee, final int numParams) {
    // Strict functions won't always have a callee for arguments, and will pass null instead.
    final boolean isStrict = callee == null || callee.isStrict();
    final Global global = Global.instance();
    final ScriptObject proto = global.getObjectPrototype();
    if (isStrict) {
        return new NativeStrictArguments(arguments, numParams, proto, global.getStrictArgumentsMap());
    }
    return new NativeArguments(arguments, callee, numParams, proto, global.getArgumentsMap());
}
 
Example 9
Source File: NativeRegExp.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private String callReplaceValue(final ScriptFunction function, final RegExpMatcher matcher, final String string) {
    final Object[] groups = groups(matcher);
    final Object[] args   = Arrays.copyOf(groups, groups.length + 2);

    args[groups.length]     = matcher.start();
    args[groups.length + 1] = string;

    final Object self = function.isStrict() ? UNDEFINED : Global.instance();

    return JSType.toString(ScriptRuntime.apply(function, self, args));
}
 
Example 10
Source File: NativeArray.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static Object[] sort(final Object[] array, final Object comparefn) {
    final ScriptFunction cmp = compareFunction(comparefn);

    final List<Object> list = Arrays.asList(array);
    final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Global.instance();

    Collections.sort(list, new Comparator<Object>() {
        private final MethodHandle call_cmp = getCALL_CMP();
        @Override
        public int compare(final Object x, final Object y) {
            if (x == ScriptRuntime.UNDEFINED && y == ScriptRuntime.UNDEFINED) {
                return 0;
            } else if (x == ScriptRuntime.UNDEFINED) {
                return 1;
            } else if (y == ScriptRuntime.UNDEFINED) {
                return -1;
            }

            if (cmp != null) {
                try {
                    return (int)Math.signum((double)call_cmp.invokeExact(cmp, cmpThis, x, y));
                } catch (final RuntimeException | Error e) {
                    throw e;
                } catch (final Throwable t) {
                    throw new RuntimeException(t);
                }
            }

            return JSType.toString(x).compareTo(JSType.toString(y));
        }
    });

    return list.toArray(new Object[array.length]);
}
 
Example 11
Source File: NativeArguments.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory to create correct Arguments object based on strict mode.
 *
 * @param arguments the actual arguments array passed
 * @param callee the callee function that uses arguments object
 * @param numParams the number of declared (named) function parameters
 * @return Arguments Object
 */
public static ScriptObject allocate(final Object[] arguments, final ScriptFunction callee, final int numParams) {
    // Strict functions won't always have a callee for arguments, and will pass null instead.
    final boolean isStrict = callee == null || callee.isStrict();
    final Global global = Global.instance();
    final ScriptObject proto = global.getObjectPrototype();
    if (isStrict) {
        return new NativeStrictArguments(arguments, numParams, proto, NativeStrictArguments.getInitialMap());
    }
    return new NativeArguments(arguments, callee, numParams, proto, NativeArguments.getInitialMap());
}
 
Example 12
Source File: NativeArguments.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory to create correct Arguments object based on strict mode.
 *
 * @param arguments the actual arguments array passed
 * @param callee the callee function that uses arguments object
 * @param numParams the number of declared (named) function parameters
 * @return Arguments Object
 */
public static ScriptObject allocate(final Object[] arguments, final ScriptFunction callee, final int numParams) {
    // Strict functions won't always have a callee for arguments, and will pass null instead.
    final boolean isStrict = callee == null || callee.isStrict();
    final Global global = Global.instance();
    final ScriptObject proto = global.getObjectPrototype();
    if (isStrict) {
        return new NativeStrictArguments(arguments, numParams, proto, NativeStrictArguments.getInitialMap());
    }
    return new NativeArguments(arguments, callee, numParams, proto, NativeArguments.getInitialMap());
}
 
Example 13
Source File: NativeArguments.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory to create correct Arguments object based on strict mode.
 *
 * @param arguments the actual arguments array passed
 * @param callee the callee function that uses arguments object
 * @param numParams the number of declared (named) function parameters
 * @return Arguments Object
 */
public static ScriptObject allocate(final Object[] arguments, final ScriptFunction callee, final int numParams) {
    // Strict functions won't always have a callee for arguments, and will pass null instead.
    final boolean isStrict = callee == null || callee.isStrict();
    final Global global = Global.instance();
    final ScriptObject proto = global.getObjectPrototype();
    if (isStrict) {
        return new NativeStrictArguments(arguments, numParams, proto, NativeStrictArguments.getInitialMap());
    }
    return new NativeArguments(arguments, callee, numParams, proto, NativeArguments.getInitialMap());
}
 
Example 14
Source File: NativeArguments.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory to create correct Arguments object based on strict mode.
 *
 * @param arguments the actual arguments array passed
 * @param callee the callee function that uses arguments object
 * @param numParams the number of declared (named) function parameters
 * @return Arguments Object
 */
public static ScriptObject allocate(final Object[] arguments, final ScriptFunction callee, final int numParams) {
    // Strict functions won't always have a callee for arguments, and will pass null instead.
    final boolean isStrict = callee == null || callee.isStrict();
    final Global global = Global.instance();
    final ScriptObject proto = global.getObjectPrototype();
    if (isStrict) {
        return new NativeStrictArguments(arguments, numParams, proto, NativeStrictArguments.getInitialMap());
    }
    return new NativeArguments(arguments, callee, numParams, proto, NativeArguments.getInitialMap());
}
 
Example 15
Source File: NativeArguments.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory to create correct Arguments object based on strict mode.
 *
 * @param arguments the actual arguments array passed
 * @param callee the callee function that uses arguments object
 * @param numParams the number of declared (named) function parameters
 * @return Arguments Object
 */
public static ScriptObject allocate(final Object[] arguments, final ScriptFunction callee, final int numParams) {
    // Strict functions won't always have a callee for arguments, and will pass null instead.
    final boolean isStrict = callee == null || callee.isStrict();
    final Global global = Global.instance();
    final ScriptObject proto = global.getObjectPrototype();
    if (isStrict) {
        return new NativeStrictArguments(arguments, numParams, proto, NativeStrictArguments.getInitialMap());
    }
    return new NativeArguments(arguments, callee, numParams, proto, NativeArguments.getInitialMap());
}
 
Example 16
Source File: NativeArguments.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory to create correct Arguments object based on strict mode.
 *
 * @param arguments the actual arguments array passed
 * @param callee the callee function that uses arguments object
 * @param numParams the number of declared (named) function parameters
 * @return Arguments Object
 */
public static ScriptObject allocate(final Object[] arguments, final ScriptFunction callee, final int numParams) {
    // Strict functions won't always have a callee for arguments, and will pass null instead.
    final boolean isStrict = callee == null || callee.isStrict();
    final Global global = Global.instance();
    final ScriptObject proto = global.getObjectPrototype();
    if (isStrict) {
        return new NativeStrictArguments(arguments, numParams, proto, NativeStrictArguments.getInitialMap());
    }
    return new NativeArguments(arguments, callee, numParams, proto, NativeArguments.getInitialMap());
}
 
Example 17
Source File: NativeArray.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static Object[] sort(final Object[] array, final Object comparefn) {
    final ScriptFunction cmp = compareFunction(comparefn);

    final List<Object> list = Arrays.asList(array);
    final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Global.instance();

    try {
        Collections.sort(list, new Comparator<Object>() {
            private final MethodHandle call_cmp = getCALL_CMP();
            @Override
            public int compare(final Object x, final Object y) {
                if (x == ScriptRuntime.UNDEFINED && y == ScriptRuntime.UNDEFINED) {
                    return 0;
                } else if (x == ScriptRuntime.UNDEFINED) {
                    return 1;
                } else if (y == ScriptRuntime.UNDEFINED) {
                    return -1;
                }

                if (cmp != null) {
                    try {
                        return (int)Math.signum((double)call_cmp.invokeExact(cmp, cmpThis, x, y));
                    } catch (final RuntimeException | Error e) {
                        throw e;
                    } catch (final Throwable t) {
                        throw new RuntimeException(t);
                    }
                }

                return JSType.toString(x).compareTo(JSType.toString(y));
            }
        });
    } catch (final IllegalArgumentException iae) {
        // Collections.sort throws IllegalArgumentException when
        // Comparison method violates its general contract

        // See ECMA spec 15.4.4.11 Array.prototype.sort (comparefn).
        // If "comparefn" is not undefined and is not a consistent
        // comparison function for the elements of this array, the
        // behaviour of sort is implementation-defined.
    }

    return list.toArray(new Object[array.length]);
}
 
Example 18
Source File: NativeArray.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static Object[] sort(final Object[] array, final Object comparefn) {
    final ScriptFunction cmp = compareFunction(comparefn);

    final List<Object> list = Arrays.asList(array);
    final Object cmpThis = cmp == null || cmp.isStrict() ? ScriptRuntime.UNDEFINED : Global.instance();

    try {
        Collections.sort(list, new Comparator<Object>() {
            private final MethodHandle call_cmp = getCALL_CMP();
            @Override
            public int compare(final Object x, final Object y) {
                if (x == ScriptRuntime.UNDEFINED && y == ScriptRuntime.UNDEFINED) {
                    return 0;
                } else if (x == ScriptRuntime.UNDEFINED) {
                    return 1;
                } else if (y == ScriptRuntime.UNDEFINED) {
                    return -1;
                }

                if (cmp != null) {
                    try {
                        return (int)Math.signum((double)call_cmp.invokeExact(cmp, cmpThis, x, y));
                    } catch (final RuntimeException | Error e) {
                        throw e;
                    } catch (final Throwable t) {
                        throw new RuntimeException(t);
                    }
                }

                return JSType.toString(x).compareTo(JSType.toString(y));
            }
        });
    } catch (final IllegalArgumentException iae) {
        // Collections.sort throws IllegalArgumentException when
        // Comparison method violates its general contract

        // See ECMA spec 15.4.4.11 Array.prototype.sort (comparefn).
        // If "comparefn" is not undefined and is not a consistent
        // comparison function for the elements of this array, the
        // behaviour of sort is implementation-defined.
    }

    return list.toArray(new Object[array.length]);
}
 
Example 19
Source File: JavaAdapterServices.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Given a script function used as a delegate for a SAM adapter, figure out
 * the right object to use as its "this" when called.
 * @param delegate the delegate function
 * @param global the current global of the adapter
 * @return either the passed global, or UNDEFINED if the function is strict.
 */
public static Object getCallThis(final ScriptFunction delegate, final Object global) {
    return delegate.isStrict() ? ScriptRuntime.UNDEFINED : global;
}