Search in sources :

Example 91 with ExecutionContext

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

the class PluralRulesConstructor method construct.

/**
 * Intl.PluralRules ([ locales [ , options ]])
 */
@Override
public PluralRulesObject 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 */
    PluralRulesObject pluralRules = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.Intl_PluralRulesPrototype, PluralRulesObject::new);
    /* step 3 */
    InitializePluralRules(calleeContext, pluralRules, locales, options);
    return pluralRules;
}
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 92 with ExecutionContext

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

the class SetConstructor method construct.

/**
 * 23.2.1.1 Set ([ iterable ])
 */
@Override
public SetObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object iterable = argument(args, 0);
    /* step 1 (not applicable) */
    /* steps 2-3 */
    SetObject set = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.SetPrototype, SetObject::new);
    /* steps 4-5, 7 */
    if (Type.isUndefinedOrNull(iterable)) {
        return set;
    }
    /* step 6 */
    Object _adder = Get(calleeContext, set, "add");
    if (!IsCallable(_adder)) {
        throw newTypeError(calleeContext, Messages.Key.PropertyNotCallable, "add");
    }
    Callable adder = (Callable) _adder;
    boolean isBuiltin = SetPrototype.isBuiltinAdd(adder);
    if (isBuiltin && iterable instanceof SetObject) {
        SetObject other = (SetObject) iterable;
        if (ScriptIterators.isBuiltinSetIterator(calleeContext, other)) {
            set.getSetData().setAll(other.getSetData());
            return set;
        }
    }
    ScriptIterator<?> iter = GetIterator(calleeContext, iterable);
    /* step 8 */
    try {
        if (isBuiltin) {
            iter.forEachRemaining(nextValue -> set.getSetData().set(nextValue, null));
        } else {
            while (iter.hasNext()) {
                /* steps 8.a-c */
                Object nextValue = iter.next();
                /* steps 8.d-e */
                adder.call(calleeContext, set, nextValue);
            }
        }
        return set;
    } catch (ScriptException e) {
        iter.close(e);
        throw e;
    }
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) IsCallable(com.github.anba.es6draft.runtime.AbstractOperations.IsCallable) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 93 with ExecutionContext

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

the class DateConstructor method construct.

/**
 * 20.3.2.1 Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )<br>
 * 20.3.2.2 Date (value)<br>
 * 20.3.2.3 Date ( )<br>
 */
@Override
public DateObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Realm realm = calleeContext.getRealm();
    /* steps 1-2 */
    int numberOfArgs = args.length;
    /* step 3 */
    final double dateValue;
    if (numberOfArgs >= 2) {
        // [20.3.2.1]
        /* step 3.a */
        double year = ToNumber(calleeContext, args[0]);
        /* step 3.b */
        double month = ToNumber(calleeContext, args[1]);
        /* step 3.c */
        double date = (args.length > 2 ? ToNumber(calleeContext, args[2]) : 1);
        /* step 3.d */
        double hour = (args.length > 3 ? ToNumber(calleeContext, args[3]) : 0);
        /* step 3.e */
        double min = (args.length > 4 ? ToNumber(calleeContext, args[4]) : 0);
        /* step 3.f */
        double sec = (args.length > 5 ? ToNumber(calleeContext, args[5]) : 0);
        /* step 3.g */
        double ms = (args.length > 6 ? ToNumber(calleeContext, args[6]) : 0);
        /* step 3.h */
        // ToInteger
        int intYear = (int) year;
        if (!Double.isNaN(year) && 0 <= intYear && intYear <= 99) {
            year = 1900 + intYear;
        }
        /* step 3.i */
        double finalDate = MakeDate(MakeDay(year, month, date), MakeTime(hour, min, sec, ms));
        /* step 3.k */
        dateValue = TimeClip(UTC(realm, finalDate));
    } else if (numberOfArgs == 1) {
        // [20.3.2.2]
        double tv;
        if (args[0] instanceof DateObject) {
            /* step 3.a */
            // TODO: spec improvement - inline thisTimeValue() call.
            tv = ((DateObject) args[0]).getDateValue();
        } else {
            /* step 3.b */
            Object v = ToPrimitive(calleeContext, args[0]);
            if (Type.isString(v)) {
                tv = (double) Properties.parse(calleeContext, null, v);
            } else {
                tv = ToNumber(calleeContext, v);
            }
        }
        dateValue = TimeClip(tv);
    } else {
        // [20.3.2.3]
        if (!calleeContext.getRealm().isGranted(Permission.CurrentTime)) {
            throw newTypeError(calleeContext, Messages.Key.NoPermission, "Date");
        }
        dateValue = System.currentTimeMillis();
    }
    DateObject obj = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.DatePrototype, DateObject::new);
    obj.setDateValue(dateValue);
    return obj;
/* step 4 (not applicable) */
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Realm(com.github.anba.es6draft.runtime.Realm)

Example 94 with ExecutionContext

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

the class DateConstructor method call.

/**
 * 20.3.2.1 Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )<br>
 * 20.3.2.2 Date (value)<br>
 * 20.3.2.3 Date ( )<br>
 */
@Override
public String call(ExecutionContext callerContext, Object thisValue, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    /* step 4 */
    if (!calleeContext.getRealm().isGranted(Permission.CurrentTime)) {
        throw newTypeError(calleeContext, Messages.Key.NoPermission, "Date");
    }
    long now = System.currentTimeMillis();
    return DatePrototype.ToDateString(calleeContext.getRealm(), now);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext)

Example 95 with ExecutionContext

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

the class NumberConstructor method construct.

/**
 * 20.1.1.1 Number ( [ value ] )
 */
@Override
public NumberObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    /* steps 1-2 */
    double n;
    if (args.length == 0) {
        n = 0d;
    } else {
        // Extension: BigInt:
        Number prim = ToNumeric(calleeContext, args[0]);
        if (Type.isBigInt(prim)) {
            n = Type.bigIntValue(prim).doubleValue();
        } else {
            n = prim.doubleValue();
        }
    }
    /* steps 4-6 */
    return NumberCreate(calleeContext, n, GetPrototypeFromConstructor(calleeContext, newTarget, Intrinsics.NumberPrototype));
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) StringToNumber(org.mozilla.javascript.StringToNumber)

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