Java Code Examples for jdk.nashorn.internal.runtime.ScriptObject

The following examples show how to use jdk.nashorn.internal.runtime.ScriptObject. 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
@Override
public void putAll(final Map<? extends String, ? extends Object> map) {
    Objects.requireNonNull(map);
    final ScriptObject oldGlobal = Context.getGlobal();
    final boolean globalChanged = (oldGlobal != global);
    inGlobal(new Callable<Object>() {
        @Override public Object call() {
            for (final Map.Entry<? extends String, ? extends Object> entry : map.entrySet()) {
                final Object value = entry.getValue();
                final Object modValue = globalChanged? wrapLikeMe(value, oldGlobal) : value;
                final String key = entry.getKey();
                checkKey(key);
                sobj.set(key, unwrap(modValue, global), getCallSiteFlags());
            }
            return null;
        }
    });
}
 
Example 2
Source Project: openjdk-8-source   Source File: NativeArray.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.2 Array.prototype.toString ( )
 *
 * @param self self reference
 * @return string representation of array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toString(final Object self) {
    final Object obj = Global.toObject(self);
    if (obj instanceof ScriptObject) {
        final InvokeByName joinInvoker = getJOIN();
        final ScriptObject sobj = (ScriptObject)obj;
        try {
            final Object join = joinInvoker.getGetter().invokeExact(sobj);
            if (Bootstrap.isCallable(join)) {
                return joinInvoker.getInvoker().invokeExact(join, sobj);
            }
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }
    }

    // FIXME: should lookup Object.prototype.toString and call that?
    return ScriptRuntime.builtinObjectToString(self);
}
 
Example 3
/**
 * Helper function to generate a function signature based on stack contents
 * and argument count and return type
 *
 * @param returnType return type
 * @param argCount   argument count
 *
 * @return function signature for stack contents
 */
private String getDynamicSignature(final Type returnType, final int argCount) {
    final Type[]         paramTypes = new Type[argCount];

    int pos = 0;
    for (int i = argCount - 1; i >= 0; i--) {
        Type pt = stack.peek(pos++);
        // "erase" specific ScriptObject subtype info - except for NativeArray.
        // NativeArray is used for array/List/Deque conversion for Java calls.
        if (ScriptObject.class.isAssignableFrom(pt.getTypeClass()) &&
            !NativeArray.class.isAssignableFrom(pt.getTypeClass())) {
            pt = Type.SCRIPT_OBJECT;
        }
        paramTypes[i] = pt;
    }
    final String descriptor = Type.getMethodDescriptor(returnType, paramTypes);
    for (int i = 0; i < argCount; i++) {
        popType(paramTypes[argCount - i - 1]);
    }

    return descriptor;
}
 
Example 4
Source Project: hottub   Source File: NativeError.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.stack
 * "stack" property is a string typed value containing JavaScript stack frames.
 * Each frame information is separated bv "\n" character.
 *
 * @param self  self reference
 *
 * @return      value of "stack" property
 */
public static Object getStack(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.has(STACK)) {
        return sobj.get(STACK);
    }

    final Object exception = ECMAException.getException(sobj);
    if (exception instanceof Throwable) {
        final Object value = getScriptStackString(sobj, (Throwable)exception);
        if (sobj.hasOwnProperty(STACK)) {
            sobj.put(STACK, value, false);
        } else {
            sobj.addOwnProperty(STACK, Attribute.NOT_ENUMERABLE, value);
        }

        return value;
    }

    return UNDEFINED;
}
 
Example 5
Source Project: openjdk-8   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 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)) {
            if (sobj.getArray().length() + args.length <= JSType.MAX_UINT) {
                final ArrayData newData = sobj.getArray().push(true, args);
                sobj.setArray(newData);
                return newData.length();
            }
            //fallthru
        }

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

        return len;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 6
/**
 * ECMA 15.5.4.14 String.prototype.split (separator, limit)
 *
 * @param self      self reference
 * @param separator separator for split
 * @param limit     limit for splits
 * @return array object in which splits have been placed
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static ScriptObject split(final Object self, final Object separator, final Object limit) {
    final String str = checkObjectToString(self);
    final long lim = limit == UNDEFINED ? JSType.MAX_UINT : JSType.toUint32(limit);

    if (separator == UNDEFINED) {
        return lim == 0 ? new NativeArray() : new NativeArray(new Object[]{str});
    }

    if (separator instanceof NativeRegExp) {
        return ((NativeRegExp) separator).split(str, lim);
    }

    // when separator is a string, it is treated as a literal search string to be used for splitting.
    return splitString(str, JSType.toString(separator), lim);
}
 
Example 7
Source Project: openjdk-jdk9   Source File: NativeArray.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * ECMA 15.4.4.2 Array.prototype.toString ( )
 *
 * @param self self reference
 * @return string representation of array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toString(final Object self) {
    final Object obj = Global.toObject(self);
    if (obj instanceof ScriptObject) {
        final InvokeByName joinInvoker = getJOIN();
        final ScriptObject sobj = (ScriptObject)obj;
        try {
            final Object join = joinInvoker.getGetter().invokeExact(sobj);
            if (Bootstrap.isCallable(join)) {
                return joinInvoker.getInvoker().invokeExact(join, sobj);
            }
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }
    }

    // FIXME: should lookup Object.prototype.toString and call that?
    return ScriptRuntime.builtinObjectToString(self);
}
 
Example 8
private void popBlockScope(final Block block) {
    final Label exitLabel     = new Label("block_exit");
    final Label recoveryLabel = new Label("block_catch");
    final Label skipLabel     = new Label("skip_catch");

    /* pop scope a la try-finally */
    method.loadCompilerConstant(SCOPE);
    method.invoke(ScriptObject.GET_PROTO);
    method.storeCompilerConstant(SCOPE);
    method._goto(skipLabel);
    method.label(exitLabel);

    method._catch(recoveryLabel);
    method.loadCompilerConstant(SCOPE);
    method.invoke(ScriptObject.GET_PROTO);
    method.storeCompilerConstant(SCOPE);
    method.athrow();
    method.label(skipLabel);
    method._try(block.getEntryLabel(), exitLabel, recoveryLabel, Throwable.class);
}
 
Example 9
/**
 * ECMA 15.4.4.2 Array.prototype.toString ( )
 *
 * @param self self reference
 * @return string representation of array
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object toString(final Object self) {
    final Object obj = Global.toObject(self);
    if (obj instanceof ScriptObject) {
        final InvokeByName joinInvoker = getJOIN();
        final ScriptObject sobj = (ScriptObject)obj;
        try {
            final Object join = joinInvoker.getGetter().invokeExact(sobj);
            if (Bootstrap.isCallable(join)) {
                return joinInvoker.getInvoker().invokeExact(join, sobj);
            }
        } catch (final RuntimeException | Error e) {
            throw e;
        } catch (final Throwable t) {
            throw new RuntimeException(t);
        }
    }

    // FIXME: should lookup Object.prototype.toString and call that?
    return ScriptRuntime.builtinObjectToString(self);
}
 
Example 10
Source Project: nashorn   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 11
private StaticClass getClassAdapterClass(final ScriptObject classOverrides, final ProtectionDomain protectionDomain) {
    JavaAdapterServices.setClassOverrides(classOverrides);
    try {
        return classAdapterGenerator.generateClass(commonLoader, protectionDomain);
    } finally {
        JavaAdapterServices.setClassOverrides(null);
    }
}
 
Example 12
Source Project: openjdk-jdk8u   Source File: TypeEvaluator.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static Object evaluatePropertySafely(final ScriptObject sobj, final String name) {
    final FindProperty find = sobj.findProperty(name, true);
    if (find == null) {
        return null;
    }
    final Property     property = find.getProperty();
    final ScriptObject owner    = find.getOwner();
    if (property.hasGetterFunction(owner)) {
        // Possible side effects; can't evaluate safely
        return null;
    }
    return property.getObjectValue(owner, owner);
}
 
Example 13
Source Project: hottub   Source File: NativeRangeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("LeakingThisInConstructor")
private NativeRangeError(final Object msg, final ScriptObject proto, final PropertyMap map) {
    super(proto, map);
    if (msg != UNDEFINED) {
        this.instMessage = JSType.toString(msg);
    } else {
        this.delete(NativeError.MESSAGE, false);
    }
    NativeError.initException(this);
}
 
Example 14
Source Project: nashorn   Source File: NativeDebug.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: get spill vector from {@link ScriptObject}
 *
 * @param self self reference
 * @param obj script object
 * @return the spill vector for the given ScriptObject
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object spill(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).spill;
    }
    return UNDEFINED;
}
 
Example 15
Source Project: openjdk-jdk9   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 16
Source Project: TencentKona-8   Source File: NativeString.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private static Object get(final Object self, final long key) {
    final CharSequence cs = JSType.toCharSequence(self);
    if (key >= 0 && key < cs.length()) {
        return String.valueOf(cs.charAt((int)key));
    }
    return ((ScriptObject) Global.toObject(self)).get(key);
}
 
Example 17
Source Project: jdk8u_nashorn   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 18
Source Project: nashorn   Source File: NativeJava.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a script object and a Java type, converts the script object into the desired Java type. Currently it
 * performs shallow creation of Java arrays, as well as wrapping of objects in Lists and Dequeues. Example:
 * <pre>
 * var anArray = [1, "13", false]
 * var javaIntArray = Java.to(anArray, "int[]")
 * print(javaIntArray[0]) // prints 1
 * print(javaIntArray[1]) // prints 13, as string "13" was converted to number 13 as per ECMAScript ToNumber conversion
 * print(javaIntArray[2]) // prints 0, as boolean false was converted to number 0 as per ECMAScript ToNumber conversion
 * </pre>
 * @param self not used
 * @param obj the script object. Can be null.
 * @param objType either a {@link #type(Object, Object) type object} or a String describing the type of the Java
 * object to create. Can not be null. If undefined, a "default" conversion is presumed (allowing the argument to be
 * omitted).
 * @return a Java object whose value corresponds to the original script object's value. Specifically, for array
 * target types, returns a Java array of the same type with contents converted to the array's component type. Does
 * not recursively convert for multidimensional arrays. For {@link List} or {@link Deque}, returns a live wrapper
 * around the object, see {@link ListAdapter} for details. Returns null if obj is null.
 * @throws ClassNotFoundException if the class described by objType is not found
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object to(final Object self, final Object obj, final Object objType) throws ClassNotFoundException {
    if (obj == null) {
        return null;
    }

    if (!(obj instanceof ScriptObject) && !(obj instanceof JSObject)) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    final Class<?> targetClass;
    if(objType == UNDEFINED) {
        targetClass = Object[].class;
    } else {
        final StaticClass targetType;
        if(objType instanceof StaticClass) {
            targetType = (StaticClass)objType;
        } else {
            targetType = type(objType);
        }
        targetClass = targetType.getRepresentedClass();
    }

    if(targetClass.isArray()) {
        return JSType.toJavaArray(obj, targetClass.getComponentType());
    }

    if(targetClass == List.class || targetClass == Deque.class) {
        return ListAdapter.create(obj);
    }

    throw typeError("unsupported.java.to.type", targetClass.getName());
}
 
Example 19
Source Project: TencentKona-8   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 5.2.3.7 Object.defineProperties ( O, Properties )
 *
 * @param self  self reference
 * @param obj   object in which to define properties
 * @param props properties
 * @return object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static ScriptObject defineProperties(final Object self, final Object obj, final Object props) {
    final ScriptObject sobj     = Global.checkObject(obj);
    final Object       propsObj = Global.toObject(props);

    if (propsObj instanceof ScriptObject) {
        final Object[] keys = ((ScriptObject)propsObj).getOwnKeys(false);
        for (final Object key : keys) {
            final String prop = JSType.toString(key);
            sobj.defineOwnProperty(prop, ((ScriptObject)propsObj).get(prop), true);
        }
    }
    return sobj;
}
 
Example 20
@Override
public boolean enterPropertyNode(final PropertyNode propertyNode) {
    if(propertyNode.getKeyName().equals(ScriptObject.PROTO_PROPERTY_NAME)) {
        tagNeverOptimistic(propertyNode.getValue());
    }
    return super.enterPropertyNode(propertyNode);
}
 
Example 21
Source Project: openjdk-jdk9   Source File: NativeArray.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.4.4.6 Array.prototype.pop ()
 *
 * @param self self reference
 * @return array after pop
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object pop(final Object self) {
    try {
        final ScriptObject sobj = (ScriptObject)self;

        if (bulkable(sobj)) {
            return sobj.getArray().pop();
        }

        final long len = JSType.toUint32(sobj.getLength());

        if (len == 0) {
            sobj.set("length", 0, CALLSITE_STRICT);
            return ScriptRuntime.UNDEFINED;
        }

        final long   index   = len - 1;
        final Object element = sobj.get(index);

        sobj.delete(index, true);
        sobj.set("length", index, CALLSITE_STRICT);

        return element;
    } catch (final ClassCastException | NullPointerException e) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(self));
    }
}
 
Example 22
Source Project: hottub   Source File: NativeError.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Nashorn extension: Error.prototype.fileName
 *
 * @param self  self reference
 * @param value value of file name
 *
 * @return value that was set
 */
public static Object setFileName(final Object self, final Object value) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.hasOwnProperty(FILENAME)) {
        sobj.put(FILENAME, value, false);
    } else {
        sobj.addOwnProperty(FILENAME, Attribute.NOT_ENUMERABLE, value);
    }
    return value;
}
 
Example 23
/**
 * Allocate and initialize a new <init> method.
 *
 * @param classEmitter  Open class emitter.
 *
 * @return Open method emitter.
 */
private static MethodEmitter newInitMethod(final ClassEmitter classEmitter) {
    final MethodEmitter init = classEmitter.init(PropertyMap.class);
    init.begin();
    init.load(Type.OBJECT, JAVA_THIS.slot());
    init.load(Type.OBJECT, INIT_MAP.slot());
    init.invoke(constructorNoLookup(ScriptObject.class, PropertyMap.class));

    return init;
}
 
Example 24
private NativeReferenceError(final Object msg, final ScriptObject proto, final PropertyMap map) {
    super(proto, map);
    if (msg != UNDEFINED) {
        this.instMessage = JSType.toString(msg);
    } else {
        this.delete(NativeError.MESSAGE, false);
    }
}
 
Example 25
private static String[] getOwnKeys(final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).getOwnKeys(false);
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).getOwnKeys(false);
    } else if (obj instanceof JSObject) {
        // No notion of "own keys" or "proto" for general JSObject! We just
        // return all keys of the object. This will be useful for POJOs
        // implementing JSObject interface.
        return ((JSObject)obj).keySet().toArray(new String[0]);
    } else {
        throw new AssertionError("should not reach here");
    }
}
 
Example 26
Source Project: openjdk-8-source   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * These classes are generated by nasgen tool and so we have to use
 * reflection to load and create new instance of these classes.
 */
private ScriptObject initConstructor(final String name) {
    try {
        // Assuming class name pattern for built-in JS constructors.
        final StringBuilder sb = new StringBuilder("jdk.nashorn.internal.objects.");

        sb.append("Native");
        sb.append(name);
        sb.append("$Constructor");

        final Class<?>     funcClass = Class.forName(sb.toString());
        final ScriptObject res       = (ScriptObject)funcClass.newInstance();

        if (res instanceof ScriptFunction) {
            // All global constructor prototypes are not-writable,
            // not-enumerable and not-configurable.
            final ScriptFunction func = (ScriptFunction)res;
            func.modifyOwnProperty(func.getProperty("prototype"), Attribute.NON_ENUMERABLE_CONSTANT);
        }

        if (res.getProto() == null) {
            res.setProto(getObjectPrototype());
        }

        return res;

    } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
 
Example 27
Source Project: openjdk-8   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 28
Source Project: hottub   Source File: TypeEvaluator.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static Type getPropertyType(final ScriptObject sobj, final String name) {
    final FindProperty find = sobj.findProperty(name, true);
    if (find == null) {
        return null;
    }

    final Property property      = find.getProperty();
    final Class<?> propertyClass = property.getType();
    if (propertyClass == null) {
        // propertyClass == null means its value is Undefined. It is probably not initialized yet, so we won't make
        // a type assumption yet.
        return null;
    } else if (propertyClass.isPrimitive()) {
        return Type.typeFor(propertyClass);
    }

    final ScriptObject owner = find.getOwner();
    if (property.hasGetterFunction(owner)) {
        // Can have side effects, so we can't safely evaluate it; since !propertyClass.isPrimitive(), it's Object.
        return Type.OBJECT;
    }

    // Safely evaluate the property, and return the narrowest type for the actual value (e.g. Type.INT for a boxed
    // integer).
    final Object value = property.needsDeclaration() ? ScriptRuntime.UNDEFINED : property.getObjectValue(owner, owner);
    if (value == ScriptRuntime.UNDEFINED) {
        return null;
    }
    return Type.typeFor(JSType.unboxedFieldType(value));
}
 
Example 29
Source Project: openjdk-jdk8u   Source File: Global.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 9.9 ToObject implementation
 *
 * @param obj  an item for which to run ToObject
 * @return ToObject version of given item
 */
public static Object toObject(final Object obj) {
    if (obj == null || obj == UNDEFINED) {
        throw typeError("not.an.object", ScriptRuntime.safeToString(obj));
    }

    if (obj instanceof ScriptObject) {
        return obj;
    }

    return instance().wrapAsObject(obj);
}
 
Example 30
Source Project: openjdk-jdk8u   Source File: ScriptUtils.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Make a script object mirror on given object if needed.
 *
 * @param obj object to be wrapped
 * @return wrapped object
 * @throws IllegalArgumentException if obj cannot be wrapped
 */
public static ScriptObjectMirror wrap(final Object obj) {
    if (obj instanceof ScriptObjectMirror) {
        return (ScriptObjectMirror)obj;
    }

    if (obj instanceof ScriptObject) {
        final ScriptObject sobj = (ScriptObject)obj;
        return (ScriptObjectMirror) ScriptObjectMirror.wrap(sobj, Context.getGlobal());
    }

    throw new IllegalArgumentException();
}