Search in sources :

Example 46 with MethodCode

use of com.github.anba.es6draft.compiler.assembler.Code.MethodCode in project es6draft by anba.

the class DefaultCodeGenerator method classMethodDecorators.

private MethodName classMethodDecorators(ClassDefinition node, CodeVisitor parent) {
    MethodTypeDescriptor descriptor = ClassMethodDecoratorsVisitor.methodDescriptor();
    MethodCode method = codegen.method(parent, "classdecorators", descriptor);
    ClassMethodDecoratorsVisitor mv = new ClassMethodDecoratorsVisitor(method);
    mv.begin();
    Variable<ExecutionContext> cx = mv.getExecutionContext();
    Variable<OrdinaryConstructorFunction> constructor = mv.getConstructor();
    Variable<OrdinaryObject> prototype = mv.getPrototype();
    // List of <1..n callable, property key>.
    Variable<Object[]> decorators = mv.getDecorators();
    Variable<Object> propertyKey = mv.newVariable("propertyKey", Object.class);
    Variable<Object> propertyDesc = mv.newVariable("propertyDesc", Object.class);
    Variable<Object> result = mv.newVariable("result", Object.class);
    int index = 0;
    for (MethodDefinition methodDef : DecoratedMethods(node.getMethods())) {
        List<Expression> decoratorsList = methodDef.getDecorators();
        assert !decoratorsList.isEmpty();
        assert !methodDef.isCallConstructor();
        Variable<? extends OrdinaryObject> object;
        if (methodDef.isStatic()) {
            object = constructor;
        } else {
            object = prototype;
        }
        mv.store(propertyKey, mv.arrayElement(decorators, index + decoratorsList.size(), Object.class));
        mv.lineInfo(methodDef);
        mv.invoke(Methods.DecoratorOperations_propertyDescriptor, object, propertyKey, cx);
        mv.store(propertyDesc);
        for (Expression decoratorExpr : decoratorsList) {
            Value<Object> decorator = mv.arrayElement(decorators, index++, Object.class);
            invokeDynamicCall(mv, decoratorExpr, decorator, cx, mv.undefinedValue(), object, propertyKey, propertyDesc);
            mv.store(result);
            Jump isObject = new Jump();
            mv.invoke(Methods.Type_isObject, result);
            mv.ifeq(isObject);
            {
                mv.store(propertyDesc, result);
            }
            mv.mark(isObject);
        }
        Jump isObject = new Jump();
        mv.invoke(Methods.Type_isObject, propertyDesc);
        mv.ifeq(isObject);
        {
            mv.lineInfo(methodDef);
            mv.invoke(Methods.DecoratorOperations_defineProperty, object, propertyKey, propertyDesc, cx);
        }
        mv.mark(isObject);
        // Skip over property key element.
        index += 1;
    }
    mv._return();
    mv.end();
    return method.name();
}
Also used : OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) MethodCode(com.github.anba.es6draft.compiler.assembler.Code.MethodCode)

Aggregations

MethodCode (com.github.anba.es6draft.compiler.assembler.Code.MethodCode)46 MethodTypeDescriptor (com.github.anba.es6draft.compiler.assembler.MethodTypeDescriptor)12 Completion (com.github.anba.es6draft.compiler.StatementGenerator.Completion)8 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)6 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)6 GeneratorState (com.github.anba.es6draft.compiler.CodeVisitor.GeneratorState)5 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)4 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)4 OrdinaryConstructorFunction (com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction)4 SwitchClause (com.github.anba.es6draft.ast.SwitchClause)3 Jump (com.github.anba.es6draft.compiler.assembler.Jump)3 LabelState (com.github.anba.es6draft.compiler.CodeVisitor.LabelState)2 ResumptionPoint (com.github.anba.es6draft.runtime.internal.ResumptionPoint)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2 ArrayList (java.util.ArrayList)2 MethodDefinition (com.github.anba.es6draft.ast.MethodDefinition)1 ValType (com.github.anba.es6draft.compiler.DefaultCodeGenerator.ValType)1 InstanceMethod (com.github.anba.es6draft.runtime.language.ClassOperations.InstanceMethod)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1