Search in sources :

Example 16 with ScriptObject

use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.

the class WrapperProxy method set.

@Override
public boolean set(ExecutionContext cx, Symbol propertyKey, Object value, Object receiver) {
    /* modified 9.1.9 [[Set] (P, V, Receiver) */
    Property ownDesc = proxyTarget.getOwnProperty(cx, propertyKey);
    if (ownDesc == null) {
        // modified
        ScriptObject parent = getPrototype();
        if (parent != null) {
            return parent.set(cx, propertyKey, value, receiver);
        } else {
            ownDesc = new Property(UNDEFINED, true, true, true);
        }
    }
    if (ownDesc.isDataDescriptor()) {
        if (!ownDesc.isWritable()) {
            return false;
        }
        if (!Type.isObject(receiver)) {
            return false;
        }
        ScriptObject _receiver = Type.objectValue(receiver);
        Property existingDescriptor = _receiver.getOwnProperty(cx, propertyKey);
        if (existingDescriptor != null) {
            PropertyDescriptor valueDesc = new PropertyDescriptor(value);
            return _receiver.defineOwnProperty(cx, propertyKey, valueDesc);
        } else {
            return CreateDataProperty(cx, _receiver, propertyKey, value);
        }
    }
    assert ownDesc.isAccessorDescriptor();
    Callable setter = ownDesc.getSetter();
    if (setter == null) {
        return false;
    }
    setter.call(cx, receiver, value);
    return true;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) PropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor) Property(com.github.anba.es6draft.runtime.types.Property) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 17 with ScriptObject

use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.

the class JSONObject method SerializeJSONObject.

/**
     * 24.3.2.3 Runtime Semantics: SerializeJSONObject ( value )
     * 
     * @param cx
     *            the execution context
     * @param serializer
     *            the serializer state
     * @param value
     *            the script object
     */
private static void SerializeJSONObject(ExecutionContext cx, JSONSerializer serializer, ScriptObject value) {
    /* steps 1-2 */
    if (!serializer.stack.add(value)) {
        throw newTypeError(cx, Messages.Key.JSONCyclicValue);
    }
    /* steps 3-4 (not applicable) */
    /* steps 5-6 */
    Iterable<String> k;
    if (serializer.propertyList != null) {
        k = serializer.propertyList;
    } else {
        k = EnumerableOwnNames(cx, value);
    }
    /* step 7 (not applicable) */
    /* steps 8-10 */
    boolean isEmpty = true;
    String gap = serializer.gap;
    StringBuilder result = serializer.result;
    result.append('{');
    serializer.level += 1;
    for (String p : k) {
        // Inlined: SerializeJSONProperty
        Object v = Get(cx, value, p);
        v = TransformJSONValue(cx, serializer, value, p, v);
        if (!IsJSONSerializable(v)) {
            continue;
        }
        if (!isEmpty) {
            result.append(',');
        }
        isEmpty = false;
        if (!gap.isEmpty()) {
            indent(serializer, result);
        }
        QuoteJSONString(result, p);
        result.append(':');
        if (!gap.isEmpty()) {
            result.append(' ');
        }
        SerializeJSONValue(cx, serializer, v);
    }
    serializer.level -= 1;
    if (!isEmpty && !gap.isEmpty()) {
        indent(serializer, result);
    }
    result.append('}');
    /* step 11 */
    serializer.stack.remove(value);
/* steps 12-13 (not applicable) */
}
Also used : StringObject(com.github.anba.es6draft.runtime.types.builtins.StringObject) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) NumberObject(com.github.anba.es6draft.runtime.objects.number.NumberObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)

Example 18 with ScriptObject

use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.

the class ObjectConstructor method ObjectDefineProperties.

/**
     * 19.1.2.3.1 Runtime Semantics: ObjectDefineProperties ( O, Properties )
     * 
     * @param cx
     *            the execution context
     * @param o
     *            the script object
     * @param properties
     *            the properties object
     * @return the script object
     */
public static ScriptObject ObjectDefineProperties(ExecutionContext cx, Object o, Object properties) {
    /* step 1 */
    if (!Type.isObject(o)) {
        throw newTypeError(cx, Messages.Key.NotObjectType);
    }
    ScriptObject obj = Type.objectValue(o);
    /* steps 2-3 */
    ScriptObject props = ToObject(cx, properties);
    /* steps 4-5 */
    List<?> keys = props.ownPropertyKeys(cx);
    /* step 6 */
    int initialSize = Math.min(32, keys.size());
    ArrayList<PropertyDescriptor> descriptors = new ArrayList<>(initialSize);
    ArrayList<Object> names = new ArrayList<>(initialSize);
    /* step 7 */
    for (Object nextKey : keys) {
        Property propDesc = props.getOwnProperty(cx, nextKey);
        if (propDesc != null && propDesc.isEnumerable()) {
            Object descObj = Get(cx, props, nextKey);
            PropertyDescriptor desc = ToPropertyDescriptor(cx, descObj);
            descriptors.add(desc);
            names.add(nextKey);
        }
    }
    /* step 8 */
    for (int i = 0, size = names.size(); i < size; ++i) {
        Object p = names.get(i);
        PropertyDescriptor desc = descriptors.get(i);
        DefinePropertyOrThrow(cx, obj, p, desc);
    }
    /* step 9 */
    return obj;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ToPropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor.ToPropertyDescriptor) PropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor) FromPropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor.FromPropertyDescriptor) ArrayList(java.util.ArrayList) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ImmutablePrototypeObject(com.github.anba.es6draft.runtime.types.builtins.ImmutablePrototypeObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) Property(com.github.anba.es6draft.runtime.types.Property)

Example 19 with ScriptObject

use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.

the class ObjectConstructor method construct.

/**
     * 19.1.1.1 Object ( [ value ] )
     */
@Override
public ScriptObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object value = argument(args, 0);
    /* step 1 */
    if (newTarget != this) {
        return OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.ObjectPrototype);
    }
    /* step 2 */
    if (Type.isUndefinedOrNull(value)) {
        // (= `this`) is not the intrinsic %Object% constructor function.
        return OrdinaryCreateFromConstructor(calleeContext, this, Intrinsics.ObjectPrototype);
    }
    /* step 3 */
    return ToObject(calleeContext, value);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ImmutablePrototypeObject(com.github.anba.es6draft.runtime.types.builtins.ImmutablePrototypeObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Example 20 with ScriptObject

use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.

the class ObjectConstructor method GetOwnPropertyNames.

/**
     * 19.1.2.8.1 Runtime Semantics: GetOwnPropertyKeys ( O, Type ), with Type = String
     * 
     * @param cx
     *            the execution context
     * @param o
     *            the script object
     * @return the own string-valued property keys of <var>o</var>
     */
public static ArrayObject GetOwnPropertyNames(ExecutionContext cx, Object o) {
    /* steps 1-2 */
    ScriptObject obj = ToObject(cx, o);
    /* steps 3-4 */
    List<?> keys = obj.ownPropertyKeys(cx);
    /* step 5 */
    int initialSize = Math.min(32, keys.size());
    ArrayList<String> nameList = new ArrayList<>(initialSize);
    /* step 6 */
    for (Object key : keys) {
        if (key instanceof String) {
            nameList.add((String) key);
        }
    }
    /* step 7 */
    return CreateArrayFromList(cx, nameList);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ArrayList(java.util.ArrayList) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ImmutablePrototypeObject(com.github.anba.es6draft.runtime.types.builtins.ImmutablePrototypeObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Aggregations

ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)129 Callable (com.github.anba.es6draft.runtime.types.Callable)43 Property (com.github.anba.es6draft.runtime.types.Property)32 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)26 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)21 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)16 Constructor (com.github.anba.es6draft.runtime.types.Constructor)11 Realm (com.github.anba.es6draft.runtime.Realm)9 PropertyDescriptor (com.github.anba.es6draft.runtime.types.PropertyDescriptor)9 ParserException (com.github.anba.es6draft.parser.ParserException)8 CompilationException (com.github.anba.es6draft.compiler.CompilationException)7 Source (com.github.anba.es6draft.runtime.internal.Source)7 ArrayList (java.util.ArrayList)7 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)6 ImmutablePrototypeObject (com.github.anba.es6draft.runtime.types.builtins.ImmutablePrototypeObject)6 OrdinaryCreateFromConstructor (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject.OrdinaryCreateFromConstructor)6 Test (org.junit.Test)6 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)5 IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)4 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)4