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

The following examples show how to use jdk.nashorn.internal.runtime.ScriptFunction#isStrict() . These examples are extracted from open source projects. 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
/**
 * 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 2
/**
 * 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 3
/**
 * 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 4
/**
 * 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 5
/**
 * 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
/**
 * 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 7
/**
 * 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 8
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 9
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
/**
 * 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 11
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 12
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 13
/**
 * 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
/**
 * 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 15
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 16
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 17
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 Project: hottub   File: NativeArray.java    License: 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
/**
 * 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;
}