Search in sources :

Example 86 with ExecutionContext

use of com.github.anba.es6draft.runtime.ExecutionContext 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 87 with ExecutionContext

use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.

the class Properties method getStaticMethodHandle.

private static MethodHandle getStaticMethodHandle(Lookup lookup, Method method) throws IllegalAccessException {
    MethodHandle handle = lookup.unreflect(method);
    MethodType type = handle.type();
    Class<?>[] params = type.parameterArray();
    int p = 0, pcount = type.parameterCount();
    boolean callerContext = false;
    // First three parameters are (ExecutionContext, ExecutionContext?, Object=ThisValue)
    if (!(p < pcount && ExecutionContext.class.equals(params[p++]))) {
        throw new IllegalArgumentException(type.toString());
    }
    if (p < pcount && ExecutionContext.class.equals(params[p])) {
        callerContext = true;
        p++;
    }
    if (!(p < pcount && Object.class.equals(params[p++]))) {
        throw new IllegalArgumentException(type.toString());
    }
    // Always required to return Object (for now at least)
    if (!Object.class.equals(type.returnType())) {
        throw new IllegalArgumentException(type.toString());
    }
    // Collect remaining arguments into Object[]
    if (!(p + 1 == pcount && Object[].class.equals(params[p]))) {
        final int fixedArguments = callerContext ? 3 : 2;
        final boolean varargs = handle.isVarargsCollector();
        // Otherwise all trailing arguments need to be of type Object or Object[]
        for (; p < pcount; ++p) {
            if (Object.class.equals(params[p])) {
                continue;
            }
            if (p + 1 == pcount && Object[].class.equals(params[p]) && varargs) {
                continue;
            }
            throw new IllegalArgumentException(type.toString());
        }
        // Trailing Object[] arguments are no longer spread in var-args methods (jdk8u40, jdk9).
        if (varargs) {
            handle = handle.asFixedArity();
        }
        // Convert to (ExecutionContext, Object, ...) -> Object handle
        handle = toCanonical(handle, fixedArguments, varargs, method);
    }
    if (!callerContext) {
        handle = MethodHandles.dropArguments(handle, 1, ExecutionContext.class);
    }
    return handle;
}
Also used : MethodType(java.lang.invoke.MethodType) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) MethodHandle(java.lang.invoke.MethodHandle)

Example 88 with ExecutionContext

use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.

the class CollatorConstructor method construct.

/**
 * 10.1.2 Intl.Collator([ locales [, options]])
 */
@Override
public CollatorObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object locales = argument(args, 0);
    Object options = argument(args, 1);
    /* step 1 (not applicable) */
    /* steps 2-5 */
    CollatorObject collator = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.Intl_CollatorPrototype, CollatorObject::new);
    /* step 6 */
    InitializeCollator(calleeContext, collator, locales, options);
    return collator;
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ToObject(com.github.anba.es6draft.runtime.AbstractOperations.ToObject)

Example 89 with ExecutionContext

use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.

the class DateTimeFormatConstructor method construct.

/**
 * 12.2.1 Intl.DateTimeFormat([ locales [, options ]])
 */
@Override
public DateTimeFormatObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object locales = argument(args, 0);
    Object options = argument(args, 1);
    /* step 1 (not applicable) */
    /* step 2 */
    DateTimeFormatObject obj = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.Intl_DateTimeFormatPrototype, DateTimeFormatObject::new);
    /* step 3 */
    InitializeDateTimeFormat(calleeContext, obj, locales, options);
    /* step 6 */
    return obj;
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Example 90 with ExecutionContext

use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.

the class ListFormatConstructor method construct.

/**
 * Intl.ListFormat ([ locales [ , options ]])
 */
@Override
public ListFormatObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object locales = argument(args, 0);
    Object options = argument(args, 1);
    /* step 1 (not applicable) */
    /* step 2 */
    ListFormatObject listFormat = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.Intl_ListFormatPrototype, ListFormatObject::new);
    /* step 3 */
    InitializeListFormat(calleeContext, listFormat, locales, options);
    return listFormat;
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ToObject(com.github.anba.es6draft.runtime.AbstractOperations.ToObject)

Aggregations

ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)95 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)61 Realm (com.github.anba.es6draft.runtime.Realm)17 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)16 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)15 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)14 AbstractOperations (com.github.anba.es6draft.runtime.AbstractOperations)11 Declaration (com.github.anba.es6draft.ast.Declaration)10 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)10 Name (com.github.anba.es6draft.ast.scope.Name)10 Callable (com.github.anba.es6draft.runtime.types.Callable)10 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)10 HashSet (java.util.HashSet)10 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)9 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)9 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)8 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)8 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)8 ArrayDeque (java.util.ArrayDeque)8 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)7