Search in sources :

Example 6 with OrdinaryConstructorFunction

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

the class ClassPropertyGenerator method methodDefinitions.

private OutlinedCall methodDefinitions(MethodDefinitionsMethod node, CodeVisitor mv) {
    MethodTypeDescriptor methodDescriptor = MethodDefinitionsCodeVisitor.methodDescriptor(mv);
    MethodCode method = codegen.method(mv, "mdef", methodDescriptor);
    return outlined(new MethodDefinitionsCodeVisitor(node, method, mv), body -> {
        Variable<OrdinaryConstructorFunction> function = body.getFunctionParameter();
        Variable<OrdinaryObject> proto = body.getPrototypeParameter();
        StoreToArray<Object> staticFields = this.staticFields.from(body.getStaticFieldsParameter());
        StoreToArray<Object> instanceFields = this.instanceFields.from(body.getInstanceFieldsParameter());
        StoreToArray<InstanceMethod> instanceMethods = this.instanceMethods.from(body.getInstanceMethodsParameter());
        StoreToArray<Object> decorators = this.decorators.from(body.getDecoratorsParameter());
        ClassPropertyEvaluation(codegen, classDefinition, node.getProperties(), function, proto, staticFields, instanceFields, instanceMethods, decorators, body);
        return Completion.Normal;
    });
}
Also used : OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) MethodCode(com.github.anba.es6draft.compiler.assembler.Code.MethodCode) MethodTypeDescriptor(com.github.anba.es6draft.compiler.assembler.MethodTypeDescriptor) InstanceMethod(com.github.anba.es6draft.runtime.language.ClassOperations.InstanceMethod)

Example 7 with OrdinaryConstructorFunction

use of com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction 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)

Example 8 with OrdinaryConstructorFunction

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

the class Properties method createConstructor.

private static OrdinaryObject createConstructor(ExecutionContext cx, String className, OrdinaryObject proto, Converter converter, ObjectLayout layout) {
    Entry<Function, MethodHandle> constructorEntry = findConstructor(layout);
    if (constructorEntry != null) {
        // User supplied method, perform manual ClassDefinitionEvaluation for constructors
        Function function = constructorEntry.getKey();
        MethodHandle unreflect = constructorEntry.getValue();
        MethodHandle callMethod = getStaticMethodHandle(converter, unreflect, MethodKind.Call);
        MethodHandle constructMethod = getStaticMethodHandle(converter, unreflect, MethodKind.Construct);
        NativeConstructor constructor = new NativeConstructor(cx.getRealm(), className, function.arity(), callMethod, constructMethod);
        constructor.defineOwnProperty(cx, "prototype", new PropertyDescriptor(proto, false, false, false));
        proto.defineOwnProperty(cx, "constructor", new PropertyDescriptor(constructor, true, false, true));
        return constructor;
    }
    // Create default constructor
    String sourceText = String.format("(class %s { })", sanitizeName(className));
    Object constructor = ScriptLoading.eval(cx.getRealm(), "<Constructor>", sourceText);
    assert constructor instanceof OrdinaryConstructorFunction : constructor.getClass();
    return (OrdinaryConstructorFunction) constructor;
}
Also used : NativeFunction(com.github.anba.es6draft.runtime.types.builtins.NativeFunction) OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction) NativeTailCallFunction(com.github.anba.es6draft.runtime.types.builtins.NativeTailCallFunction) NativeConstructor(com.github.anba.es6draft.runtime.types.builtins.NativeConstructor) OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) PropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor) AccessorPropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor.AccessorPropertyDescriptor) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) MethodHandle(java.lang.invoke.MethodHandle)

Example 9 with OrdinaryConstructorFunction

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

the class ClassOperations method InitializeInstanceFields.

public static void InitializeInstanceFields(ScriptObject thisValue, ExecutionContext cx) {
    EnvironmentRecord envRec = cx.getThisEnvironment();
    assert envRec instanceof FunctionEnvironmentRecord;
    FunctionEnvironmentRecord fEnvRec = (FunctionEnvironmentRecord) envRec;
    FunctionObject activeFunction = fEnvRec.getFunctionObject();
    assert activeFunction instanceof OrdinaryConstructorFunction;
    OrdinaryConstructorFunction constructor = (OrdinaryConstructorFunction) activeFunction;
    InitializeInstanceFields(thisValue, constructor, cx);
}
Also used : OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) FunctionEnvironmentRecord(com.github.anba.es6draft.runtime.FunctionEnvironmentRecord) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) FunctionEnvironmentRecord(com.github.anba.es6draft.runtime.FunctionEnvironmentRecord) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord)

Example 10 with OrdinaryConstructorFunction

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

the class FunctionOperations method EvaluateFunctionExpression.

/**
 * 14.1 Function Definitions
 * <p>
 * 14.1.21 Runtime Semantics: Evaluation
 * <ul>
 * <li>FunctionExpression : function ( FormalParameters ) { FunctionBody }
 * <li>FunctionExpression : function BindingIdentifier ( FormalParameters ) { FunctionBody }
 * </ul>
 *
 * @param fd
 *            the function runtime info object
 * @param cx
 *            the execution context
 * @return the new function instance
 */
public static OrdinaryConstructorFunction EvaluateFunctionExpression(RuntimeInfo.Function fd, ExecutionContext cx) {
    OrdinaryConstructorFunction closure;
    if (!fd.is(RuntimeInfo.FunctionFlags.ScopedName)) {
        /* step 1 (not applicable) */
        /* step 2 */
        LexicalEnvironment<?> scope = cx.getLexicalEnvironment();
        /* step 3 */
        closure = ConstructorFunctionCreate(cx, FunctionKind.Normal, fd, scope);
        /* step 4 */
        MakeConstructor(cx, closure);
    } else {
        /* step 1 (not applicable) */
        /* step 2 */
        LexicalEnvironment<?> scope = cx.getLexicalEnvironment();
        /* step 3 */
        LexicalEnvironment<DeclarativeEnvironmentRecord> funcEnv = newDeclarativeEnvironment(scope);
        /* step 4 */
        DeclarativeEnvironmentRecord envRec = funcEnv.getEnvRec();
        /* step 5 */
        String name = fd.functionName();
        /* step 6 */
        envRec.createImmutableBinding(name, false);
        /* step 7 */
        closure = ConstructorFunctionCreate(cx, FunctionKind.Normal, fd, funcEnv);
        /* step 8 */
        MakeConstructor(cx, closure);
        /* step 9 */
        SetFunctionName(closure, name);
        /* step 10 */
        envRec.initializeBinding(name, closure);
    }
    /* step 5/11 */
    return closure;
}
Also used : OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)

Aggregations

OrdinaryConstructorFunction (com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction)10 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)6 MethodCode (com.github.anba.es6draft.compiler.assembler.Code.MethodCode)3 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)2 PropertyDescriptor (com.github.anba.es6draft.runtime.types.PropertyDescriptor)2 AccessorPropertyDescriptor (com.github.anba.es6draft.runtime.types.PropertyDescriptor.AccessorPropertyDescriptor)2 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)2 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)2 BlockScope (com.github.anba.es6draft.ast.scope.BlockScope)1 Name (com.github.anba.es6draft.ast.scope.Name)1 LabelledHashKey (com.github.anba.es6draft.compiler.CodeVisitor.LabelledHashKey)1 MethodTypeDescriptor (com.github.anba.es6draft.compiler.assembler.MethodTypeDescriptor)1 EnvironmentRecord (com.github.anba.es6draft.runtime.EnvironmentRecord)1 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)1 FunctionEnvironmentRecord (com.github.anba.es6draft.runtime.FunctionEnvironmentRecord)1 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)1 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)1 InstanceMethod (com.github.anba.es6draft.runtime.language.ClassOperations.InstanceMethod)1 Property (com.github.anba.es6draft.runtime.types.Property)1 BuiltinFunction (com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)1