Java Code Examples for jdk.nashorn.internal.objects.annotations.Attribute

The following examples show how to use jdk.nashorn.internal.objects.annotations.Attribute. 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
Source Project: jdk8u60   Source File: NativeArray.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.7 Array.prototype.push (args...)
 *
 * @param self self reference
 * @param args arguments to push
 * @return array length after pushes
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object push(final Object self, final Object... args) {
    try {
        final ScriptObject sobj   = (ScriptObject)self;

        if (bulkable(sobj) && sobj.getArray().length() + args.length <= JSType.MAX_UINT) {
            final ArrayData newData = sobj.getArray().push(true, args);
            sobj.setArray(newData);
            return newData.length();
        }

        long len = JSType.toUint32(sobj.getLength());
        for (final Object element : args) {
            sobj.set(len++, element, CALLSITE_STRICT);
        }
        sobj.set("length", len, CALLSITE_STRICT);

        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError(Context.getGlobal(), e, "not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 2
Source Project: openjdk-jdk8u   Source File: NativeArray.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.20 Array.prototype.filter ( callbackfn [ , thisArg ] )
 *
 * @param self        self reference
 * @param callbackfn  callback function per element
 * @param thisArg     this argument
 * @return filtered array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static NativeArray filter(final Object self, final Object callbackfn, final Object thisArg) {
    return new IteratorAction<NativeArray>(Global.toObject(self), callbackfn, thisArg, new NativeArray()) {
        private long to = 0;
        private final MethodHandle filterInvoker = getFILTER_CALLBACK_INVOKER();

        @Override
        protected boolean forEach(final Object val, final double i) throws Throwable {
            if ((boolean)filterInvoker.invokeExact(callbackfn, thisArg, val, i, self)) {
                result.defineOwnProperty(ArrayIndex.getArrayIndex(to++), val);
            }
            return true;
        }
    }.apply();
}
 
Example 3
/**
 * ECMA 15.4.4.20 Array.prototype.filter ( callbackfn [ , thisArg ] )
 *
 * @param self        self reference
 * @param callbackfn  callback function per element
 * @param thisArg     this argument
 * @return filtered array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static NativeArray filter(final Object self, final Object callbackfn, final Object thisArg) {
    return new IteratorAction<NativeArray>(Global.toObject(self), callbackfn, thisArg, new NativeArray()) {
        private long to = 0;
        private final MethodHandle filterInvoker = getFILTER_CALLBACK_INVOKER();

        @Override
        protected boolean forEach(final Object val, final double i) throws Throwable {
            if ((boolean)filterInvoker.invokeExact(callbackfn, thisArg, val, i, self)) {
                result.defineOwnProperty(ArrayIndex.getArrayIndex(to++), val);
            }
            return true;
        }
    }.apply();
}
 
Example 4
Source Project: openjdk-jdk9   Source File: NativeFunction.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.3.4.4 Function.prototype.call (thisArg [ , arg1 [ , arg2, ... ] ] )
 *
 * @param self self reference
 * @param args arguments for call
 * @return result of call
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static Object call(final Object self, final Object... args) {
    checkCallable(self);

    final Object thiz = (args.length == 0) ? UNDEFINED : args[0];
    Object[] arguments;

    if (args.length > 1) {
        arguments = new Object[args.length - 1];
        System.arraycopy(args, 1, arguments, 0, arguments.length);
    } else {
        arguments = ScriptRuntime.EMPTY_ARRAY;
    }

    if (self instanceof ScriptFunction) {
        return ScriptRuntime.apply((ScriptFunction)self, thiz, arguments);
    } else if (self instanceof JSObject) {
        return ((JSObject)self).call(thiz, arguments);
    }

    throw new AssertionError("should not reach here");
}
 
Example 5
Source Project: openjdk-jdk8u   Source File: NativeString.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.5.4.11 String.prototype.replace (searchValue, replaceValue)
 * @param self        self reference
 * @param string      item to replace
 * @param replacement item to replace it with
 * @return string after replacement
 * @throws Throwable if replacement fails
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String replace(final Object self, final Object string, final Object replacement) throws Throwable {

    final String str = checkObjectToString(self);

    final NativeRegExp nativeRegExp;
    if (string instanceof NativeRegExp) {
        nativeRegExp = (NativeRegExp) string;
    } else {
        nativeRegExp = NativeRegExp.flatRegExp(JSType.toString(string));
    }

    if (Bootstrap.isCallable(replacement)) {
        return nativeRegExp.replace(str, "", replacement);
    }

    return nativeRegExp.replace(str, JSType.toString(replacement), null);
}
 
Example 6
Source Project: openjdk-jdk9   Source File: NativeJava.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns synchronized wrapper version of the given ECMAScript function.
 * @param self not used
 * @param func the ECMAScript function whose synchronized version is returned.
 * @param obj the object (i.e, lock) on which the function synchronizes.
 * @return synchronized wrapper version of the given ECMAScript function.
 */
@Function(name="synchronized", attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object synchronizedFunc(final Object self, final Object func, final Object obj) {
    if (func instanceof ScriptFunction) {
        return ((ScriptFunction)func).createSynchronized(obj);
    }

    throw typeError("not.a.function", ScriptRuntime.safeToString(func));
}
 
Example 7
Source Project: openjdk-jdk9   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.10 Object.preventExtensions ( O )
 *
 * @param self self reference
 * @param obj  object, for which to set the internal extensible property to false
 * @return object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object preventExtensions(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).preventExtensions();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).preventExtensions();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 8
Source Project: openjdk-jdk9   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the Uint8ClampedArray property.
 * @param self self reference
 * @return the value of the Uint8ClampedArray property
 */
@Getter(name = "Uint8ClampedArray", attributes = Attribute.NOT_ENUMERABLE)
public static Object getUint8ClampedArray(final Object self) {
    final Global global = Global.instanceFrom(self);
    if (global.uint8ClampedArray == LAZY_SENTINEL) {
        global.uint8ClampedArray = global.getBuiltinUint8ClampedArray();
    }
    return global.uint8ClampedArray;
}
 
Example 9
Source Project: jdk8u60   Source File: NativeDate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.9.5.41 Date.prototype.setUTCFullYear (year [, month [, date ] ] )
 *
 * @param self self reference
 * @param args UTC full year (optional second and third arguments are month and date)
 * @return time
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 3)
public static double setUTCFullYear(final Object self, final Object... args) {
    final NativeDate nd   = ensureNativeDate(self);
    if (nd.isValidDate()) {
        setFields(nd, YEAR, args, false);
    } else {
        final double[] d = convertArgs(args, 0, YEAR, YEAR, 3);
        nd.setTime(timeClip(makeDate(makeDay(d[0], d[1], d[2]), 0)));
    }
    return nd.getTime();
}
 
Example 10
Source Project: TencentKona-8   Source File: NativeString.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.10 String.prototype.match (regexp)
 * @param self   self reference
 * @param regexp regexp expression
 * @return array of regexp matches
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static ScriptObject match(final Object self, final Object regexp) {

    final String str = checkObjectToString(self);

    NativeRegExp nativeRegExp;
    if (regexp == UNDEFINED) {
        nativeRegExp = new NativeRegExp("");
    } else {
        nativeRegExp = Global.toRegExp(regexp);
    }

    if (!nativeRegExp.getGlobal()) {
        return nativeRegExp.exec(str);
    }

    nativeRegExp.setLastIndex(0);

    int previousLastIndex = 0;
    final List<Object> matches = new ArrayList<>();

    Object result;
    while ((result = nativeRegExp.exec(str)) != null) {
        final int thisIndex = nativeRegExp.getLastIndex();
        if (thisIndex == previousLastIndex) {
            nativeRegExp.setLastIndex(thisIndex + 1);
            previousLastIndex = thisIndex + 1;
        } else {
            previousLastIndex = thisIndex;
        }
        matches.add(((ScriptObject)result).get(0));
    }

    if (matches.isEmpty()) {
        return null;
    }

    return new NativeArray(matches.toArray());
}
 
Example 11
Source Project: TencentKona-8   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.4 Object.getOwnPropertyNames ( O )
 *
 * @param self self reference
 * @param obj  object to query for property names
 * @return array of property names
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static ScriptObject getOwnPropertyNames(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return new NativeArray(((ScriptObject)obj).getOwnKeys(true));
    } else if (obj instanceof ScriptObjectMirror) {
        return new NativeArray(((ScriptObjectMirror)obj).getOwnKeys(true));
    } else {
        throw notAnObject(obj);
    }
}
 
Example 12
Source Project: TencentKona-8   Source File: NativeDate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.9.5.26 Date.prototype.getTimezoneOffset ( )
 *
 * @param self self reference
 * @return time zone offset or NaN if N/A
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double getTimezoneOffset(final Object self) {
    final NativeDate nd = getNativeDate(self);
    if (nd != null && nd.isValidDate()) {
        final long msec = (long) nd.getTime();
        return - nd.getTimeZone().getOffset(msec) / msPerMinute;
    }
    return Double.NaN;
}
 
Example 13
Source Project: openjdk-jdk8u   Source File: NativeDataView.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set 8-bit unsigned int at the given byteOffset
 *
 * @param self DataView object
 * @param byteOffset byte offset to write at
 * @param value byte value to set
 * @return undefined
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 2)
public static Object setUint8(final Object self, final Object byteOffset, final Object value) {
    try {
        getBuffer(self).put(JSType.toInt32(byteOffset), (byte)JSType.toInt32(value));
        return UNDEFINED;
    } catch (final IllegalArgumentException iae) {
        throw rangeError(iae, "dataview.offset");
    }
}
 
Example 14
/**
 * Nashorn extension: String.prototype.trimLeft ( )
 * @param self self reference
 * @return string trimmed left from whitespace
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String trimLeft(final Object self) {

    final String str = checkObjectToString(self);
    int start = 0;
    final int end   = str.length() - 1;

    while (start <= end && ScriptRuntime.isJSWhitespace(str.charAt(start))) {
        start++;
    }

    return str.substring(start, end + 1);
}
 
Example 15
Source Project: openjdk-jdk8u   Source File: NativeDebug.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Object util - getClass
 *
 * @param self self reference
 * @param obj  object
 * @return class of {@code obj}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object getClass(final Object self, final Object obj) {
    if (obj != null) {
        return obj.getClass();
    }
    return UNDEFINED;
}
 
Example 16
Source Project: openjdk-jdk9   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the ArrayBuffer property.
 * @param self self reference
 * @return the value of the ArrayBuffer property
 */
@Getter(name = "ArrayBuffer", attributes = Attribute.NOT_ENUMERABLE)
public static Object getArrayBuffer(final Object self) {
    final Global global = Global.instanceFrom(self);
    if (global.arrayBuffer == LAZY_SENTINEL) {
        global.arrayBuffer = global.getBuiltinArrayBuffer();
    }
    return global.arrayBuffer;
}
 
Example 17
/**
 * Returns name of a java type {@link StaticClass}.
 * @param self not used
 * @param type the type whose name is returned
 * @return name of the given type
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object typeName(final Object self, final Object type) {
    if (type instanceof StaticClass) {
        return ((StaticClass)type).getRepresentedClass().getName();
    } else if (type instanceof Class) {
        return ((Class<?>)type).getName();
    } else {
        return UNDEFINED;
    }
}
 
Example 18
Source Project: TencentKona-8   Source File: NativeDate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.9.4.3 Date.UTC (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )
 *
 * @param self self reference
 * @param args mandatory args are year, month. Optional are date, hours, minutes, seconds and milliseconds
 * @return a time clip according to the ECMA specification
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 7, where = Where.CONSTRUCTOR)
public static double UTC(final Object self, final Object... args) {
    final NativeDate nd = new NativeDate(0);
    final double[] d = convertCtorArgs(args);
    final double time = d == null ? Double.NaN : timeClip(makeDate(d));
    nd.setTime(time);
    return time;
}
 
Example 19
Source Project: jdk8u60   Source File: NativeArray.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.10 Array.prototype.slice ( start [ , end ] )
 *
 * @param self  self reference
 * @param start start of slice (inclusive)
 * @param end   end of slice (optional, exclusive)
 * @return sliced array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object slice(final Object self, final Object start, final Object end) {
    final Object       obj                 = Global.toObject(self);
    if (!(obj instanceof ScriptObject)) {
        return ScriptRuntime.UNDEFINED;
    }

    final ScriptObject sobj                = (ScriptObject)obj;
    final long         len                 = JSType.toUint32(sobj.getLength());
    final long         relativeStart       = JSType.toLong(start);
    final long         relativeEnd         = end == ScriptRuntime.UNDEFINED ? len : JSType.toLong(end);

    long k = relativeStart < 0 ? Math.max(len + relativeStart, 0) : Math.min(relativeStart, len);
    final long finale = relativeEnd < 0 ? Math.max(len + relativeEnd, 0) : Math.min(relativeEnd, len);

    if (k >= finale) {
        return new NativeArray(0);
    }

    if (bulkable(sobj)) {
        return new NativeArray(sobj.getArray().slice(k, finale));
    }

    // Construct array with proper length to have a deleted filter on undefined elements
    final NativeArray copy = new NativeArray(finale - k);
    for (long n = 0; k < finale; n++, k++) {
        if (sobj.has(k)) {
            copy.defineOwnProperty(ArrayIndex.getArrayIndex(n), sobj.get(k));
        }
    }

    return copy;
}
 
Example 20
Source Project: jdk8u60   Source File: NativeString.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.5 String.prototype.charCodeAt (pos)
 * @param self self reference
 * @param pos  position in string
 * @return number representing charcode at position
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double charCodeAt(final Object self, final Object pos) {
    final String str = checkObjectToString(self);
    final int    idx = JSType.toInteger(pos);
    return idx < 0 || idx >= str.length() ? Double.NaN : str.charAt(idx);
}
 
Example 21
/**
 * Nashorn extension: get context, context utility
 *
 * @param self self reference
 * @return context
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object getContext(final Object self) {
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(new RuntimePermission(Context.NASHORN_GET_CONTEXT));
    }
    return Global.getThisContext();
}
 
Example 22
Source Project: openjdk-jdk9   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the URIError property.
 * @param self self reference
 * @return the value of URIError property
 */
@Getter(name = "URIError", attributes = Attribute.NOT_ENUMERABLE)
public static Object getURIError(final Object self) {
    final Global global = Global.instanceFrom(self);
    if (global.uriError == LAZY_SENTINEL) {
        global.uriError = global.getBuiltinURIError();
    }
    return global.uriError;
}
 
Example 23
Source Project: openjdk-jdk8u   Source File: NativeDataView.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get 32-bit unsigned int from given byteOffset
 *
 * @param self DataView object
 * @param byteOffset byte offset to read from
 * @param littleEndian (optional) flag indicating whether to read in little endian order
 * @return 32-bit unsigned int value at the byteOffset
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1)
public static double getUint32(final Object self, final Object byteOffset, final Object littleEndian) {
    try {
        return 0xFFFFFFFFL & getBuffer(self, littleEndian).getInt(JSType.toInt32(byteOffset));
    } catch (final IllegalArgumentException iae) {
        throw rangeError(iae, "dataview.offset");
    }
}
 
Example 24
Source Project: openjdk-jdk9   Source File: NativeNumber.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.7.4.7 Number.prototype.toPrecision (precision)
 *
 * @param self      self reference
 * @param precision use {@code precision - 1} digits after the significand's decimal point or call {@link JSType#toString} if undefined
 *
 * @return number in decimal exponentiation notation or decimal fixed notation depending on {@code precision}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static String toPrecision(final Object self, final Object precision) {
    final double x = getNumberValue(self);
    if (precision == UNDEFINED) {
        return JSType.toString(x);
    }
    return (toPrecision(x, JSType.toInteger(precision)));
}
 
Example 25
/**
 * Nashorn extension: Error.prototype.columnNumber
 *
 * @param self  self reference
 * @param value value of column number
 *
 * @return value that was set
 */
public static Object setColumnNumber(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(COLUMNNUMBER)) {
        sobj.put(COLUMNNUMBER, value, false);
    } else {
        sobj.addOwnProperty(COLUMNNUMBER, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 26
Source Project: TencentKona-8   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for ECMA 15.1.4.8 RegExp property
 *
 * @param self self reference
 * @return RegExp property value
 */
@Getter(name = "RegExp", attributes = Attribute.NOT_ENUMERABLE)
public static Object getRegExp(final Object self) {
    final Global global = Global.instanceFrom(self);
    if (global.regexp == LAZY_SENTINEL) {
        global.regexp = global.getBuiltinRegExp();
    }
    return global.regexp;
}
 
Example 27
Source Project: openjdk-jdk9   Source File: NativeWeakSet.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA6 23.4.3.3 WeakSet.prototype.delete ( value )
 *
 * @param self the self reference
 * @param value the value
 * @return true if value was deleted
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static boolean delete(final Object self, final Object value) {
    final Map<Object, Boolean> map = getSet(self).map;
    if (isPrimitive(value)) {
        return false;
    }
    final boolean returnValue = map.containsKey(value);
    map.remove(value);
    return returnValue;
}
 
Example 28
Source Project: openjdk-jdk9   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the WeakSet property.
 *
 * @param self self reference
 * @return  the value of the WeakSet property
 */
@Getter(name = "WeakSet", attributes = Attribute.NOT_ENUMERABLE)
public static Object getWeakSet(final Object self) {
    final Global global = Global.instanceFrom(self);
    if (global.weakSet == LAZY_SENTINEL) {
        global.weakSet = global.getBuiltinWeakSet();
    }
    return global.weakSet;
}
 
Example 29
Source Project: TencentKona-8   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter for the RangeError property.
 * @param self self reference
 * @return the value of RangeError property
 */
@Getter(name = "RangeError", attributes = Attribute.NOT_ENUMERABLE)
public static Object getRangeError(final Object self) {
    final Global global = Global.instanceFrom(self);
    if (global.rangeError == LAZY_SENTINEL) {
        global.rangeError = global.getBuiltinRangeError();
    }
    return global.rangeError;
}
 
Example 30
Source Project: TencentKona-8   Source File: NativeRegExp.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Getter for non-standard RegExp.$8 property.
 * @param self self object
 * @return last regexp input
 */
@Getter(where = Where.CONSTRUCTOR, attributes = Attribute.CONSTANT, name = "$8")
public static Object getGroup8(final Object self) {
    final RegExpResult match = Global.instance().getLastRegExpResult();
    return match == null ? "" : match.getGroup(8);
}