Search in sources :

Example 1 with PrivateName

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

the class ClassOperations method initializeInstanceMethods.

private static void initializeInstanceMethods(ScriptObject thisValue, InstanceMethod[] instanceMethods, ExecutionContext cx) {
    HashSet<PrivateName> accessors = new HashSet<>();
    for (InstanceMethod instanceMethod : instanceMethods) {
        PrivateName name = instanceMethod.name;
        FunctionObject method = instanceMethod.method;
        switch(instanceMethod.kind) {
            case Method:
                {
                    Property desc = new Property(method, false, false, false);
                    privateFieldDefine(cx, thisValue, name, desc);
                    break;
                }
            case Getter:
                if (accessors.add(name)) {
                    Property desc = new Property(method, null, false, false);
                    privateFieldDefine(cx, thisValue, name, desc);
                } else {
                    addGetter(thisValue.get(name), method);
                }
                break;
            case Setter:
                if (accessors.add(name)) {
                    Property desc = new Property(null, method, false, false);
                    privateFieldDefine(cx, thisValue, name, desc);
                } else {
                    addSetter(thisValue.get(name), method);
                }
                break;
            default:
                throw new AssertionError();
        }
    }
}
Also used : PrivateName(com.github.anba.es6draft.runtime.types.PrivateName) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) Property(com.github.anba.es6draft.runtime.types.Property) HashSet(java.util.HashSet)

Example 2 with PrivateName

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

the class PropertyOperations method setPrivateValue.

/**
 * PrivateFieldSet (P, O, value )
 *
 * @param baseValue
 *            the object value
 * @param name
 *            the private name identifier
 * @param value
 *            the new value
 * @param cx
 *            the execution context
 */
public static void setPrivateValue(Object baseValue, String name, Object value, ExecutionContext cx) {
    PrivateName privateName = resolvePrivateName(name, cx);
    /* step 2 */
    if (!Type.isObject(baseValue)) {
        throw newTypeError(cx, Messages.Key.NotObjectType);
    }
    ScriptObject obj = Type.objectValue(baseValue);
    /* steps 3, 5 */
    Property desc = obj.get(privateName);
    /* step 4 */
    if (desc == null) {
        throw newTypeError(cx, Messages.Key.PrivateFieldNotPresent, name);
    }
    /* steps 6-7 */
    if (desc.isDataDescriptor()) {
        /* step 6.a */
        if (!desc.isWritable()) {
            throw newTypeError(cx, Messages.Key.PropertyNotModifiable, name);
        }
        /* step 6.b */
        desc.setValue(value);
    } else {
        /* step 7.a */
        assert desc.isAccessorDescriptor();
        /* step 7.b */
        Callable setter = desc.getSetter();
        /* step 7.c */
        if (setter == null) {
            throw newTypeError(cx, Messages.Key.PrivateFieldNoSetter, name);
        }
        /* step 7.d */
        setter.call(cx, obj, value);
    }
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) PrivateName(com.github.anba.es6draft.runtime.types.PrivateName) Property(com.github.anba.es6draft.runtime.types.Property) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 3 with PrivateName

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

the class PropertyOperations method resolvePrivateName.

/* *********************************************************************************************************** */
private static PrivateName resolvePrivateName(String name, ExecutionContext cx) {
    for (LexicalEnvironment<?> env = cx.getLexicalEnvironment(); env != null; env = env.getOuter()) {
        EnvironmentRecord envRec = env.getEnvRec();
        if (!(envRec instanceof DeclarativeEnvironmentRecord)) {
            break;
        }
        Object value = ((DeclarativeEnvironmentRecord) envRec).getBindingValueOrNull(name, true);
        if (value != null) {
            return (PrivateName) value;
        }
    }
    throw newReferenceError(cx, Messages.Key.UnresolvablePrivateField, name);
}
Also used : PrivateName(com.github.anba.es6draft.runtime.types.PrivateName) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionEnvironmentRecord(com.github.anba.es6draft.runtime.FunctionEnvironmentRecord) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)

Example 4 with PrivateName

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

the class PropertyOperations method getPrivateValue.

/**
 * PrivateFieldGet (P, O )
 *
 * @param baseValue
 *            the object value
 * @param name
 *            the private name identifier
 * @param cx
 *            the execution context
 * @return the private name value
 */
public static Object getPrivateValue(Object baseValue, String name, ExecutionContext cx) {
    PrivateName privateName = resolvePrivateName(name, cx);
    /* step 2 */
    if (!Type.isObject(baseValue)) {
        throw newTypeError(cx, Messages.Key.NotObjectType);
    }
    ScriptObject obj = Type.objectValue(baseValue);
    /* steps 3, 5 */
    Property desc = obj.get(privateName);
    /* step 4 */
    if (desc == null) {
        throw newTypeError(cx, Messages.Key.PrivateFieldNotPresent, name);
    }
    /* step 6 */
    if (desc.isDataDescriptor()) {
        return desc.getValue();
    }
    /* step 7 */
    assert desc.isAccessorDescriptor();
    /* step 8 */
    Callable getter = desc.getGetter();
    /* step 9 */
    if (getter == null) {
        throw newTypeError(cx, Messages.Key.PrivateFieldNoGetter, name);
    }
    /* step 10 */
    return getter.call(cx, obj);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) PrivateName(com.github.anba.es6draft.runtime.types.PrivateName) Property(com.github.anba.es6draft.runtime.types.Property) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 5 with PrivateName

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

the class ClassPropertyGenerator method InitializePrivateName.

private void InitializePrivateName(StoreToArray<Object> target, PrivateNameProperty privateName, CodeVisitor mv) {
    // TODO: The spec uses a separate lexical environment for private names.
    Name name = privateName.getName();
    Value<DeclarativeEnvironmentRecord> scopeEnvRec = getLexicalEnvironmentRecord(Types.DeclarativeEnvironmentRecord, mv);
    BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(scopeEnvRec, name);
    MutableValue<Object> arrayElement = target.element(Object.class, mv);
    arrayElement.store(mv, __ -> {
        mv.anew(Methods.PrivateName_new, mv.vconst(name.getIdentifier()));
    });
    op.initializeBinding(scopeEnvRec, name, arrayElement, mv);
}
Also used : OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) PropName(com.github.anba.es6draft.semantics.StaticSemantics.PropName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) FieldName(com.github.anba.es6draft.compiler.assembler.FieldName) PrivateName(com.github.anba.es6draft.runtime.types.PrivateName)

Aggregations

PrivateName (com.github.anba.es6draft.runtime.types.PrivateName)8 Property (com.github.anba.es6draft.runtime.types.Property)4 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)4 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)3 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)3 Name (com.github.anba.es6draft.ast.scope.Name)2 FieldName (com.github.anba.es6draft.compiler.assembler.FieldName)2 EnvironmentRecord (com.github.anba.es6draft.runtime.EnvironmentRecord)2 FunctionEnvironmentRecord (com.github.anba.es6draft.runtime.FunctionEnvironmentRecord)2 Callable (com.github.anba.es6draft.runtime.types.Callable)2 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)2 PropName (com.github.anba.es6draft.semantics.StaticSemantics.PropName)2 InstanceMethod (com.github.anba.es6draft.runtime.language.ClassOperations.InstanceMethod)1 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)1 HashSet (java.util.HashSet)1