Search in sources :

Example 21 with ScriptObject

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

the class AsyncGeneratorFunctionConstructor method CreateDynamicFunction.

/**
     * 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
     * 
     * @param callerContext
     *            the caller execution context
     * @param cx
     *            the execution context
     * @param newTarget
     *            the newTarget constructor function
     * @param args
     *            the function arguments
     * @return the new async generator function object
     */
private static FunctionObject CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Intrinsics fallbackProto = Intrinsics.AsyncGenerator;
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.AsyncGenerator, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.asyncGenerator(source, parameters, bodyText).getFunction();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* step 12 */
    boolean strict = function.isStrict();
    /* steps 21-22 */
    ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
    /* step 23 */
    OrdinaryAsyncGenerator f = FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
    /* steps 24-25 */
    LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
    /* step 26 */
    FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
    /* step 27 */
    OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.AsyncGeneratorPrototype);
    f.infallibleDefineOwnProperty("prototype", new Property(prototype, true, false, false));
    /* step 28 (not applicable) */
    /* step 29 */
    SetFunctionName(f, "anonymous");
    /* step 30 */
    return f;
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) FunctionConstructor.functionSource(com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource) Source(com.github.anba.es6draft.runtime.internal.Source) OrdinaryAsyncGenerator(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncGenerator) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) Property(com.github.anba.es6draft.runtime.types.Property) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 22 with ScriptObject

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

the class DateTimeFormatConstructor method ToDateTimeOptions.

/**
     * 12.1.2 ToDateTimeOptions (options, required, defaults)
     * 
     * @param cx
     *            the execution context
     * @param opts
     *            the options object
     * @param required
     *            the required date field
     * @param defaults
     *            the default date field
     * @return the date-time options script object
     */
public static ScriptObject ToDateTimeOptions(ExecutionContext cx, Object opts, String required, String defaults) {
    /* steps 1-2 */
    OrdinaryObject options = ObjectCreate(cx, Type.isUndefined(opts) ? null : ToObject(cx, opts));
    /* step 3 */
    boolean needDefaults = true;
    /* step 4 */
    if ("date".equals(required) || "any".equals(required)) {
        // FIXME: spec vs. impl (short circuit after first undefined value?)
        for (String prop : array("weekday", "year", "month", "day")) {
            Object kvalue = Get(cx, options, prop);
            if (!Type.isUndefined(kvalue)) {
                needDefaults = false;
            }
        }
    }
    /* step 5 */
    if ("time".equals(required) || "any".equals(required)) {
        // FIXME: spec vs. impl (short circuit after first undefined value?)
        for (String prop : array("hour", "minute", "second")) {
            Object kvalue = Get(cx, options, prop);
            if (!Type.isUndefined(kvalue)) {
                needDefaults = false;
            }
        }
    }
    /* step 6 */
    if (needDefaults && ("date".equals(defaults) || "all".equals(defaults))) {
        for (String prop : array("year", "month", "day")) {
            CreateDataPropertyOrThrow(cx, options, prop, "numeric");
        }
    }
    /* step 7 */
    if (needDefaults && ("time".equals(defaults) || "all".equals(defaults))) {
        for (String prop : array("hour", "minute", "second")) {
            CreateDataPropertyOrThrow(cx, options, prop, "numeric");
        }
    }
    /* step 8 */
    return options;
}
Also used : 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) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Example 23 with ScriptObject

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

the class DateTimeFormatConstructor method InitializeDateTimeFormat.

/**
     * 12.1.1 InitializeDateTimeFormat (dateTimeFormat, locales, options)
     * 
     * @param cx
     *            the execution context
     * @param dateTimeFormat
     *            the date format object
     * @param locales
     *            the locales array
     * @param opts
     *            the options object
     */
public static void InitializeDateTimeFormat(ExecutionContext cx, DateTimeFormatObject dateTimeFormat, Object locales, Object opts) {
    /* steps 1-2 (FIXME: spec bug - unnecessary internal slot) */
    /* step 3 */
    Set<String> requestedLocales = CanonicalizeLocaleList(cx, locales);
    /* step 4 */
    ScriptObject options = ToDateTimeOptions(cx, opts, "any", "date");
    /* step 6 */
    String matcher = GetStringOption(cx, options, "localeMatcher", set("lookup", "best fit"), "best fit");
    /* step 5, 7 */
    OptionsRecord opt = new OptionsRecord(OptionsRecord.MatcherType.forName(matcher));
    /* step 8 */
    DateTimeFormatLocaleData localeData = new DateTimeFormatLocaleData();
    /* step 9 */
    ResolvedLocale r = ResolveLocale(cx.getRealm(), getAvailableLocalesLazy(cx), requestedLocales, opt, relevantExtensionKeys, localeData);
    /* step 10 */
    dateTimeFormat.setLocale(r.getLocale());
    /* step 11 */
    dateTimeFormat.setCalendar(r.getValue(ExtensionKey.ca));
    /* step 12 */
    dateTimeFormat.setNumberingSystem(r.getValue(ExtensionKey.nu));
    /* step 13 */
    String dataLocale = r.getDataLocale();
    /* step 14 */
    Object tz = Get(cx, options, "timeZone");
    /* steps 15-16 */
    String timeZone;
    if (!Type.isUndefined(tz)) {
        /* step 15.a */
        timeZone = ToFlatString(cx, tz);
        /* step 15.b */
        if (!IsValidTimeZoneName(timeZone)) {
            throw newRangeError(cx, Messages.Key.IntlInvalidOption, timeZone);
        }
        /* step 15.c */
        timeZone = CanonicalizeTimeZoneName(timeZone);
    } else {
        /* step 16.a */
        timeZone = DefaultTimeZone(cx.getRealm());
    }
    /* step 17 */
    dateTimeFormat.setTimeZone(timeZone);
    /* step 18 (moved) */
    /* step 19 */
    // FIXME: spec should propably define exact iteration order here
    String weekday = GetStringOption(cx, options, "weekday", set("narrow", "short", "long"), null);
    String era = GetStringOption(cx, options, "era", set("narrow", "short", "long"), null);
    String year = GetStringOption(cx, options, "year", set("2-digit", "numeric"), null);
    String month = GetStringOption(cx, options, "month", set("2-digit", "numeric", "narrow", "short", "long"), null);
    String day = GetStringOption(cx, options, "day", set("2-digit", "numeric"), null);
    String hour = GetStringOption(cx, options, "hour", set("2-digit", "numeric"), null);
    String minute = GetStringOption(cx, options, "minute", set("2-digit", "numeric"), null);
    String second = GetStringOption(cx, options, "second", set("2-digit", "numeric"), null);
    String timeZoneName = GetStringOption(cx, options, "timeZoneName", set("short", "long"), null);
    /* steps 20-21 (moved) */
    /* step 22 */
    String formatMatcher = GetStringOption(cx, options, "formatMatcher", set("basic", "best fit"), "best fit");
    /* steps 23-26 (moved) */
    /* step 27 */
    Boolean hour12 = GetBooleanOption(cx, options, "hour12", null);
    /* steps 18, 20-21, 23-26, 28-29 */
    FormatMatcherRecord formatRecord = new FormatMatcherRecord(weekday, era, year, month, day, hour, minute, second, timeZoneName, hour12);
    Lazy<String> pattern;
    if ("basic".equals(formatMatcher)) {
        pattern = new BasicFormatPattern(formatRecord, dataLocale);
    } else {
        pattern = new BestFitFormatPattern(formatRecord, dataLocale);
    }
    /* step 30 */
    dateTimeFormat.setPattern(pattern);
    /* step 31 */
    dateTimeFormat.setBoundFormat(null);
/* step 32 (FIXME: spec bug - unnecessary internal slot) */
/* step 33 (omitted) */
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OptionsRecord(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord) ResolvedLocale(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.ResolvedLocale) 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 24 with ScriptObject

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

the class ArrayConstructor method construct.

/**
     * 22.1.1.1 Array ( )<br>
     * 22.1.1.2 Array (len)<br>
     * 22.1.1.3 Array (...items )
     */
@Override
public ArrayObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    /* step 1 */
    int numberOfArgs = args.length;
    /* steps 2-3 (not applicable) */
    /* steps 4-5 */
    ScriptObject proto = GetPrototypeFromConstructor(calleeContext, newTarget, Intrinsics.ArrayPrototype);
    if (numberOfArgs == 0) {
        /* step 6 */
        return ArrayCreate(calleeContext, 0, proto);
    } else if (numberOfArgs == 1) {
        // [22.1.1.2]
        Object len = args[0];
        /* steps 6-11 */
        if (!Type.isNumber(len)) {
            return DenseArrayCreate(calleeContext, proto, len);
        } else {
            double llen = Type.numberValue(len);
            long intLen = ToUint32(llen);
            if (intLen != llen) {
                throw newRangeError(calleeContext, Messages.Key.InvalidArrayLength);
            }
            return ArrayCreate(calleeContext, intLen, proto);
        }
    } else {
        /* steps 6-12 */
        return DenseArrayCreate(calleeContext, proto, args);
    }
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Example 25 with ScriptObject

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

the class ErrorObject method getErrorObjectProperty.

/**
     * Specialized property retrieval to prevent any script execution.
     * 
     * @param error
     *            the error object
     * @param propertyName
     *            the property key
     * @param defaultValue
     *            the default value
     * @return property string value
     */
private static String getErrorObjectProperty(ErrorObject error, String propertyName, String defaultValue) {
    Property property = error.lookupOwnProperty(propertyName);
    if (property == null) {
        ScriptObject proto = error.getPrototype();
        if (proto instanceof ErrorPrototype || proto instanceof NativeErrorPrototype) {
            property = ((OrdinaryObject) proto).lookupOwnProperty(propertyName);
        }
    }
    Object value = property != null && property.isDataDescriptor() ? property.getValue() : null;
    if (value == null || Type.isUndefined(value)) {
        return defaultValue;
    }
    // Prevent possible recursion
    if (value instanceof ErrorObject) {
        return "<error>";
    }
    return Objects.toString(value);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) Property(com.github.anba.es6draft.runtime.types.Property)

Aggregations

ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)129 Callable (com.github.anba.es6draft.runtime.types.Callable)43 Property (com.github.anba.es6draft.runtime.types.Property)32 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)26 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)21 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)16 Constructor (com.github.anba.es6draft.runtime.types.Constructor)11 Realm (com.github.anba.es6draft.runtime.Realm)9 PropertyDescriptor (com.github.anba.es6draft.runtime.types.PropertyDescriptor)9 ParserException (com.github.anba.es6draft.parser.ParserException)8 CompilationException (com.github.anba.es6draft.compiler.CompilationException)7 Source (com.github.anba.es6draft.runtime.internal.Source)7 ArrayList (java.util.ArrayList)7 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)6 ImmutablePrototypeObject (com.github.anba.es6draft.runtime.types.builtins.ImmutablePrototypeObject)6 OrdinaryCreateFromConstructor (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject.OrdinaryCreateFromConstructor)6 Test (org.junit.Test)6 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)5 IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)4 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)4