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

The following examples show how to use jdk.nashorn.internal.objects.annotations.Where. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: openjdk-jdk8u   Source File: NativeObject.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.2.3.5 Object.create ( O [, Properties] )
 *
 * @param self  self reference
 * @param proto prototype object
 * @param props properties to define
 * @return object created
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static ScriptObject create(final Object self, final Object proto, final Object props) {
    if (proto != null) {
        Global.checkObject(proto);
    }

    // FIXME: should we create a proper object with correct number of
    // properties?
    final ScriptObject newObj = Global.newEmptyInstance();
    newObj.setProto((ScriptObject)proto);
    if (props != UNDEFINED) {
        NativeObject.defineProperties(self, newObj, props);
    }

    return newObj;
}
 
Example 2
Source Project: openjdk-jdk9   Source File: NativeObject.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.2.3.5 Object.create ( O [, Properties] )
 *
 * @param self  self reference
 * @param proto prototype object
 * @param props properties to define
 * @return object created
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static ScriptObject create(final Object self, final Object proto, final Object props) {
    if (proto != null) {
        Global.checkObject(proto);
    }

    // FIXME: should we create a proper object with correct number of
    // properties?
    final ScriptObject newObj = Global.newEmptyInstance();
    newObj.setProto((ScriptObject)proto);
    if (props != UNDEFINED) {
        NativeObject.defineProperties(self, newObj, props);
    }

    return newObj;
}
 
Example 3
Source Project: TencentKona-8   Source File: NativeObject.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.2.3.5 Object.create ( O [, Properties] )
 *
 * @param self  self reference
 * @param proto prototype object
 * @param props properties to define
 * @return object created
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static ScriptObject create(final Object self, final Object proto, final Object props) {
    if (proto != null) {
        Global.checkObject(proto);
    }

    // FIXME: should we create a proper object with correct number of
    // properties?
    final ScriptObject newObj = Global.newEmptyInstance();
    newObj.setProto((ScriptObject)proto);
    if (props != UNDEFINED) {
        NativeObject.defineProperties(self, newObj, props);
    }

    return newObj;
}
 
Example 4
int getPrototypeMemberCount() {
    int count = 0;
    for (final MemberInfo memInfo : members) {
        switch (memInfo.getKind()) {
            case SETTER:
            case SPECIALIZED_FUNCTION:
                // SETTER was counted when GETTER was encountered.
                // SPECIALIZED_FUNCTION was counted as FUNCTION already.
                continue;
        }

        if (memInfo.getWhere() == Where.PROTOTYPE) {
            count++;
        }
    }
    return count;
}
 
Example 5
/**
 * 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 6
Source Project: openjdk-jdk8u   Source File: NativeDebug.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Expands the event queue capacity, or truncates if capacity is lower than
 * current capacity. Then only the newest entries are kept
 * @param self self reference
 * @param newCapacity new capacity
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static void expandEventQueueCapacity(final Object self, final Object newCapacity) {
    final LinkedList<RuntimeEvent<?>> q = getEventQueue(self);
    final int nc = JSType.toInt32(newCapacity);
    while (q.size() > nc) {
        q.removeFirst();
    }
    setEventQueueCapacity(self, nc);
}
 
Example 7
Source Project: openjdk-jdk8u   Source File: NativeJava.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a Java array or {@link Collection}, returns a JavaScript array with a shallow copy of its contents. Note
 * that in most cases, you can use Java arrays and lists natively in Nashorn; in cases where for some reason you
 * need to have an actual JavaScript native array (e.g. to work with the array comprehensions functions), you will
 * want to use this method. Example:
 * <pre>
 * var File = Java.type("java.io.File")
 * var listHomeDir = new File("~").listFiles()
 * var jsListHome = Java.from(listHomeDir)
 * var jpegModifiedDates = jsListHome
 *     .filter(function(val) { return val.getName().endsWith(".jpg") })
 *     .map(function(val) { return val.lastModified() })
 * </pre>
 * @param self not used
 * @param objArray the java array or collection. Can be null.
 * @return a JavaScript array with the copy of Java array's or collection's contents. Returns null if objArray is
 * null.
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static NativeArray from(final Object self, final Object objArray) {
    if (objArray == null) {
        return null;
    } else if (objArray instanceof Collection) {
        return new NativeArray(((Collection<?>)objArray).toArray());
    } else if (objArray instanceof Object[]) {
        return new NativeArray(((Object[])objArray).clone());
    } else if (objArray instanceof int[]) {
        return new NativeArray(((int[])objArray).clone());
    } else if (objArray instanceof double[]) {
        return new NativeArray(((double[])objArray).clone());
    } else if (objArray instanceof long[]) {
        return new NativeArray(((long[])objArray).clone());
    } else if (objArray instanceof byte[]) {
        return new NativeArray(copyArray((byte[])objArray));
    } else if (objArray instanceof short[]) {
        return new NativeArray(copyArray((short[])objArray));
    } else if (objArray instanceof char[]) {
        return new NativeArray(copyArray((char[])objArray));
    } else if (objArray instanceof float[]) {
        return new NativeArray(copyArray((float[])objArray));
    } else if (objArray instanceof boolean[]) {
        return new NativeArray(copyArray((boolean[])objArray));
    }

    throw typeError("cant.convert.to.javascript.array", objArray.getClass().getName());
}
 
Example 8
Source Project: openjdk-jdk8u   Source File: NativeDebug.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the ArrayData for this ScriptObject
 * @param self self
 * @param obj script object to check
 * @return ArrayData, ArrayDatas have toString methods, return Undefined if data missing
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object getArrayData(final Object self, final Object obj) {
    try {
        return ((ScriptObject)obj).getArray();
    } catch (final ClassCastException e) {
        return ScriptRuntime.UNDEFINED;
    }
}
 
Example 9
Source Project: jdk8u60   Source File: NativeString.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.3.2 String.fromCharCode ( [ char0 [ , char1 [ , ... ] ] ] )
 * @param self  self reference
 * @param args  array of arguments to be interpreted as char
 * @return string with arguments translated to charcodes
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, arity = 1, where = Where.CONSTRUCTOR)
public static String fromCharCode(final Object self, final Object... args) {
    final char[] buf = new char[args.length];
    int index = 0;
    for (final Object arg : args) {
        buf[index++] = (char)JSType.toUint16(arg);
    }
    return new String(buf);
}
 
Example 10
Source Project: nashorn   Source File: NativeDebug.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: get map from {@link ScriptObject}
 *
 * @param self self reference
 * @param obj script object
 * @return the map for the current ScriptObject
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object map(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).getMap();
    }
    return UNDEFINED;
}
 
Example 11
Source Project: nashorn   Source File: ScriptClassInfo.java    License: GNU General Public License v2.0 5 votes vote down vote up
int getInstancePropertyCount() {
    int count = 0;
    for (final MemberInfo memInfo : members) {
        if (memInfo.getWhere() == Where.INSTANCE) {
            count++;
        }
    }
    return count;
}
 
Example 12
Source Project: jdk8u60   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).makeSynchronizedFunction(obj);
    }

    throw typeError("not.a.function", ScriptRuntime.safeToString(func));
}
 
Example 13
Source Project: openjdk-jdk9   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Object.setPrototypeOf ( O, proto )
 * Also found in ES6 draft specification.
 *
 * @param  self self reference
 * @param  obj object to set prototype for
 * @param  proto prototype object to be used
 * @return object whose prototype is set
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object setPrototypeOf(final Object self, final Object obj, final Object proto) {
    if (obj instanceof ScriptObject) {
        ((ScriptObject)obj).setPrototypeOf(proto);
        return obj;
    } else if (obj instanceof ScriptObjectMirror) {
        ((ScriptObjectMirror)obj).setProto(proto);
        return obj;
    }

    throw notAnObject(obj);
}
 
Example 14
@Override
public FieldVisitor visitField(final int fieldAccess, final String fieldName,
        final String fieldDesc, final String signature, final Object value) {
    final MemberInfo memInfo = scriptClassInfo.find(fieldName, fieldDesc, fieldAccess);
    if (memInfo != null && memInfo.getKind() == Kind.PROPERTY &&
            memInfo.getWhere() != Where.INSTANCE && !memInfo.isStaticFinal()) {
        // non-instance @Property fields - these have to go elsewhere unless 'static final'
        return null;
    }

    final FieldVisitor delegateFV = super.visitField(fieldAccess, fieldName, fieldDesc,
            signature, value);
    return new FieldVisitor(Opcodes.ASM4, delegateFV) {
        @Override
        public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
            if (ScriptClassInfo.annotations.containsKey(desc)) {
                // ignore script field annotations
                return null;
            }

            return fv.visitAnnotation(desc, visible);
        }

        @Override
        public void visitAttribute(final Attribute attr) {
            fv.visitAttribute(attr);
        }

        @Override
        public void visitEnd() {
            fv.visitEnd();
        }
    };
}
 
Example 15
Source Project: jdk8u60   Source File: ScriptClassInfo.java    License: GNU General Public License v2.0 5 votes vote down vote up
boolean isPrototypeNeeded() {
    // Prototype class generation is needed if we have atleast one
    // prototype property or @Constructor defined in the class.
    for (final MemberInfo memInfo : members) {
        if (memInfo.getWhere() == Where.PROTOTYPE || memInfo.isConstructor()) {
            return true;
        }
    }
    return false;
}
 
Example 16
Source Project: openjdk-jdk8u   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.8 Object.seal ( O )
 *
 * @param self self reference
 * @param obj  object to seal
 * @return sealed object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object seal(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).seal();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).seal();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 17
Source Project: openjdk-jdk9   Source File: NativeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the Error object provided.
 *
 * @param self self reference
 * @param errorObj the error object
 * @return undefined
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object captureStackTrace(final Object self, final Object errorObj) {
    final ScriptObject sobj = Global.checkObject(errorObj);
    initException(sobj);
    sobj.delete(STACK, false);
    if (! sobj.has("stack")) {
        final ScriptFunction getStack = ScriptFunction.createBuiltin("getStack", GET_STACK);
        final ScriptFunction setStack = ScriptFunction.createBuiltin("setStack", SET_STACK);
        sobj.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
    }
    return UNDEFINED;
}
 
Example 18
Source Project: hottub   Source File: NativeJava.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a Java array or {@link Collection}, returns a JavaScript array with a shallow copy of its contents. Note
 * that in most cases, you can use Java arrays and lists natively in Nashorn; in cases where for some reason you
 * need to have an actual JavaScript native array (e.g. to work with the array comprehensions functions), you will
 * want to use this method. Example:
 * <pre>
 * var File = Java.type("java.io.File")
 * var listHomeDir = new File("~").listFiles()
 * var jsListHome = Java.from(listHomeDir)
 * var jpegModifiedDates = jsListHome
 *     .filter(function(val) { return val.getName().endsWith(".jpg") })
 *     .map(function(val) { return val.lastModified() })
 * </pre>
 * @param self not used
 * @param objArray the java array or collection. Can be null.
 * @return a JavaScript array with the copy of Java array's or collection's contents. Returns null if objArray is
 * null.
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static NativeArray from(final Object self, final Object objArray) {
    if (objArray == null) {
        return null;
    } else if (objArray instanceof Collection) {
        return new NativeArray(((Collection<?>)objArray).toArray());
    } else if (objArray instanceof Object[]) {
        return new NativeArray(((Object[])objArray).clone());
    } else if (objArray instanceof int[]) {
        return new NativeArray(((int[])objArray).clone());
    } else if (objArray instanceof double[]) {
        return new NativeArray(((double[])objArray).clone());
    } else if (objArray instanceof long[]) {
        return new NativeArray(((long[])objArray).clone());
    } else if (objArray instanceof byte[]) {
        return new NativeArray(copyArray((byte[])objArray));
    } else if (objArray instanceof short[]) {
        return new NativeArray(copyArray((short[])objArray));
    } else if (objArray instanceof char[]) {
        return new NativeArray(copyArray((char[])objArray));
    } else if (objArray instanceof float[]) {
        return new NativeArray(copyArray((float[])objArray));
    } else if (objArray instanceof boolean[]) {
        return new NativeArray(copyArray((boolean[])objArray));
    }

    throw typeError("cant.convert.to.javascript.array", objArray.getClass().getName());
}
 
Example 19
/**
 * ECMA 15.2.3.14 Object.keys ( O )
 *
 * @param self self reference
 * @param obj  object from which to extract keys
 * @return array of keys in object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object keys(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        final ScriptObject sobj = (ScriptObject)obj;
        return new NativeArray(sobj.getOwnKeys(false));
    } else if (obj instanceof ScriptObjectMirror) {
        final ScriptObjectMirror sobjMirror = (ScriptObjectMirror)obj;
        return new NativeArray(sobjMirror.getOwnKeys(false));
    } else {
        throw notAnObject(obj);
    }
}
 
Example 20
/**
 * Return the ArrayData for this ScriptObject
 * @param self self
 * @param obj script object to check
 * @return ArrayData, ArrayDatas have toString methods, return Undefined if data missing
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object getArrayData(final Object self, final Object obj) {
    try {
        return ((ScriptObject)obj).getArray();
    } catch (final ClassCastException e) {
        return ScriptRuntime.UNDEFINED;
    }
}
 
Example 21
Source Project: TencentKona-8   Source File: NativeDebug.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove a specific runtime event from the event queue
 * @param self self reference
 * @param event event to remove
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static void removeRuntimeEvent(final Object self, final Object event) {
    final LinkedList<RuntimeEvent<?>> q  = getEventQueue(self);
    final RuntimeEvent<?>             re = getEvent(event);
    if (!q.remove(re)) {
        throw new IllegalStateException("runtime event " + re + " was not in event queue");
    }
}
 
Example 22
Source Project: hottub   Source File: NativeDebug.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add a runtime event to the runtime event queue. The queue has a fixed
 * size {@link RuntimeEvent#RUNTIME_EVENT_QUEUE_SIZE} and the oldest
 * entry will be thrown out of the queue is about to overflow
 * @param self self reference
 * @param event event to add
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static void addRuntimeEvent(final Object self, final Object event) {
    final LinkedList<RuntimeEvent<?>> q = getEventQueue(self);
    final int cap = (Integer)getEventQueueCapacity(self);
    while (q.size() >= cap) {
        q.removeFirst();
    }
    q.addLast(getEvent(event));
}
 
Example 23
Source Project: hottub   Source File: NativeDebug.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Expands the event queue capacity, or truncates if capacity is lower than
 * current capacity. Then only the newest entries are kept
 * @param self self reference
 * @param newCapacity new capacity
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static void expandEventQueueCapacity(final Object self, final Object newCapacity) {
    final LinkedList<RuntimeEvent<?>> q = getEventQueue(self);
    final int nc = JSType.toInt32(newCapacity);
    while (q.size() > nc) {
        q.removeFirst();
    }
    setEventQueueCapacity(self, nc);
}
 
Example 24
Source Project: openjdk-8   Source File: ScriptClassInfo.java    License: GNU General Public License v2.0 5 votes vote down vote up
int getInstancePropertyCount() {
    int count = 0;
    for (final MemberInfo memInfo : members) {
        if (memInfo.getWhere() == Where.INSTANCE) {
            count++;
        }
    }
    return count;
}
 
Example 25
Source Project: TencentKona-8   Source File: NativeJava.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 26
Source Project: jdk8u60   Source File: NativeDebug.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Do not call overridden toString -- use default toString impl
 *
 * @param self self reference
 * @param obj  object to represent as a string
 * @return string representation
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static String toIdentString(final Object self, final Object obj) {
    if (obj == null) {
        return "null";
    }

    final int hash = System.identityHashCode(obj);
    return obj.getClass() + "@" + Integer.toHexString(hash);
}
 
Example 27
Source Project: jdk8u60   Source File: NativeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the Error object provided.
 *
 * @param self self reference
 * @param errorObj the error object
 * @return undefined
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object captureStackTrace(final Object self, final Object errorObj) {
    final ScriptObject sobj = Global.checkObject(errorObj);
    initException(sobj);
    sobj.delete(STACK, false);
    if (! sobj.has("stack")) {
        final ScriptFunction getStack = ScriptFunctionImpl.makeFunction("getStack", GET_STACK);
        final ScriptFunction setStack = ScriptFunctionImpl.makeFunction("setStack", SET_STACK);
        sobj.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
    }
    return UNDEFINED;
}
 
Example 28
Source Project: TencentKona-8   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Object.setPrototypeOf ( O, proto )
 * Also found in ES6 draft specification.
 *
 * @param  self self reference
 * @param  obj object to set prototype for
 * @param  proto prototype object to be used
 * @return object whose prototype is set
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object setPrototypeOf(final Object self, final Object obj, final Object proto) {
    if (obj instanceof ScriptObject) {
        ((ScriptObject)obj).setPrototypeOf(proto);
        return obj;
    } else if (obj instanceof ScriptObjectMirror) {
        ((ScriptObjectMirror)obj).setProto(proto);
        return obj;
    }

    throw notAnObject(obj);
}
 
Example 29
Source Project: openjdk-jdk8u   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.9 Object.freeze ( O )
 *
 * @param self self reference
 * @param obj object to freeze
 * @return frozen object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object freeze(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).freeze();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).freeze();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 30
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.11 Object.isSealed ( O )
 *
 * @param self self reference
 * @param obj check whether an object is sealed
 * @return true if sealed, false otherwise
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static boolean isSealed(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).isSealed();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).isSealed();
    } else {
        throw notAnObject(obj);
    }
}