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();
}
}
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations