Search in sources :

Example 81 with ScriptObject

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

the class ArrayPrototype method inheritedKeys.

private static long[] inheritedKeys(OrdinaryObject array, long from, long to) {
    long[] indices = array.indices(from, to);
    for (ScriptObject prototype = array.getPrototype(); prototype != null; ) {
        assert prototype instanceof OrdinaryObject : "Wrong class " + prototype.getClass();
        OrdinaryObject proto = (OrdinaryObject) prototype;
        if (proto.hasIndexedProperties()) {
            long[] protoIndices = proto.indices(from, to);
            long[] newIndices = new long[indices.length + protoIndices.length];
            System.arraycopy(indices, 0, newIndices, 0, indices.length);
            System.arraycopy(protoIndices, 0, newIndices, indices.length, protoIndices.length);
            indices = newIndices;
        }
        prototype = proto.getPrototype();
    }
    Arrays.sort(indices);
    return indices;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)

Example 82 with ScriptObject

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

the class ArrayPrototype method iterationKindForSparse.

private static IterationKind iterationKindForSparse(OrdinaryObject arrayLike, long length) {
    IterationKind iteration = IterationKind.SparseOwnKeys;
    int protoDepth = 0;
    long indexed = 0;
    for (OrdinaryObject object = arrayLike; ; ) {
        indexed += object.getIndexedSize();
        ScriptObject prototype = object.getPrototype();
        if (prototype == null) {
            break;
        }
        if (!(prototype instanceof OrdinaryObject)) {
            return IterationKind.Slow;
        }
        object = (OrdinaryObject) prototype;
        if (object.hasSpecialIndexedProperties()) {
            return IterationKind.Slow;
        }
        if (object.hasIndexedProperties()) {
            if (object.hasIndexedAccessors()) {
                return IterationKind.Slow;
            }
            iteration = IterationKind.InheritedKeys;
        }
        if (++protoDepth == MAX_PROTO_DEPTH) {
            return IterationKind.Slow;
        }
    }
    double density = indexed / (double) length;
    if (density > 0.75) {
        return IterationKind.Slow;
    }
    return iteration;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ArrayIterationKind(com.github.anba.es6draft.runtime.objects.ArrayIteratorObject.ArrayIterationKind)

Example 83 with ScriptObject

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

the class IntlAbstractOperations method CanonicalizeLocaleList.

/**
     * 9.2.1 CanonicalizeLocaleList (locales)
     * 
     * @param cx
     *            the execution context
     * @param locales
     *            the locales array
     * @return the set of canonicalized locales
     */
public static Set<String> CanonicalizeLocaleList(ExecutionContext cx, Object locales) {
    /* step 1 */
    if (Type.isUndefined(locales)) {
        return emptySet();
    }
    /* steps 2-8 (string only) */
    if (Type.isString(locales)) {
        // handle the string-only case directly
        String tag = ToFlatString(cx, locales);
        LanguageTag langTag = IsStructurallyValidLanguageTag(tag);
        if (langTag == null) {
            throw newRangeError(cx, Messages.Key.IntlStructurallyInvalidLanguageTag, tag);
        }
        tag = CanonicalizeLanguageTag(langTag);
        return singleton(tag);
    }
    /* step 2 */
    LinkedHashSet<String> seen = new LinkedHashSet<>();
    /* step 3 (not applicable) */
    /* step 4 */
    ScriptObject o = ToObject(cx, locales);
    /* step 5 */
    long len = ToLength(cx, Get(cx, o, "length"));
    /* steps 6-7 */
    for (long k = 0; k < len; ++k) {
        /* step 7.a */
        long pk = k;
        /* step 7.b */
        boolean kPresent = HasProperty(cx, o, pk);
        /* step 7.c */
        if (kPresent) {
            /* step 7.c.i */
            Object kValue = Get(cx, o, pk);
            /* step 7.c.ii */
            if (!(Type.isString(kValue) || Type.isObject(kValue))) {
                throw newTypeError(cx, Messages.Key.IntlInvalidLanguageTagType, Type.of(kValue).toString());
            }
            /* step 7.c.iii */
            String tag = ToFlatString(cx, kValue);
            /* step 7.c.iv */
            LanguageTag langTag = IsStructurallyValidLanguageTag(tag);
            if (langTag == null) {
                throw newRangeError(cx, Messages.Key.IntlStructurallyInvalidLanguageTag, tag);
            }
            /* step 7.c.v */
            tag = CanonicalizeLanguageTag(langTag);
            /* step 7.c.vi */
            seen.add(tag);
        }
    }
    /* step 8 */
    return seen;
}
Also used : LanguageTag(com.github.anba.es6draft.runtime.objects.intl.LanguageTagParser.LanguageTag) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Example 84 with ScriptObject

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

the class NumberFormatConstructor method InitializeNumberFormat.

/**
     * 11.1.1 InitializeNumberFormat (numberFormat, locales, options)
     * 
     * @param cx
     *            the execution context
     * @param numberFormat
     *            the number format object
     * @param locales
     *            the locales array
     * @param opts
     *            the options object
     */
public static void InitializeNumberFormat(ExecutionContext cx, NumberFormatObject numberFormat, Object locales, Object opts) {
    /* steps 1-2 (FIXME: spec bug - unnecessary internal slot) */
    /* step 3 */
    Set<String> requestedLocales = CanonicalizeLocaleList(cx, locales);
    /* steps 4-5 */
    ScriptObject options;
    if (Type.isUndefined(opts)) {
        options = ObjectCreate(cx, Intrinsics.ObjectPrototype);
    } else {
        options = ToObject(cx, opts);
    }
    /* step 7 */
    String matcher = GetStringOption(cx, options, "localeMatcher", set("lookup", "best fit"), "best fit");
    /* step 6, 8 */
    OptionsRecord opt = new OptionsRecord(OptionsRecord.MatcherType.forName(matcher));
    /* step 9 */
    NumberFormatLocaleData localeData = new NumberFormatLocaleData();
    /* step 10 */
    ResolvedLocale r = ResolveLocale(cx.getRealm(), getAvailableLocalesLazy(cx), requestedLocales, opt, relevantExtensionKeys, localeData);
    /* step 11 */
    numberFormat.setLocale(r.getLocale());
    /* step 12 */
    numberFormat.setNumberingSystem(r.getValue(ExtensionKey.nu));
    /* step 13 (not applicable) */
    /* step 14 */
    String s = GetStringOption(cx, options, "style", set("decimal", "percent", "currency"), "decimal");
    /* step 15 */
    numberFormat.setStyle(s);
    /* step 16 */
    String c = GetStringOption(cx, options, "currency", null, null);
    /* step 17 */
    if (c != null && !IsWellFormedCurrencyCode(c)) {
        throw newRangeError(cx, Messages.Key.IntlInvalidCurrency, c);
    }
    /* step 18 */
    if ("currency".equals(s) && c == null) {
        throw newTypeError(cx, Messages.Key.IntlInvalidCurrency, "null");
    }
    /* step 19 */
    int cDigits = -1;
    if ("currency".equals(s)) {
        c = ToUpperCase(c);
        numberFormat.setCurrency(c);
        cDigits = CurrencyDigits(c);
    }
    /* step 20 */
    String cd = GetStringOption(cx, options, "currencyDisplay", set("code", "symbol", "name"), "symbol");
    /* step 21 */
    if ("currency".equals(s)) {
        numberFormat.setCurrencyDisplay(cd);
    }
    /* step 22 */
    int mnid = GetNumberOption(cx, options, "minimumIntegerDigits", 1, 21, 1);
    /* step 23 */
    numberFormat.setMinimumIntegerDigits(mnid);
    /* step 24 */
    int mnfdDefault = "currency".equals(s) ? cDigits : 0;
    /* step 25 */
    int mnfd = GetNumberOption(cx, options, "minimumFractionDigits", 0, 20, mnfdDefault);
    /* step 26 */
    numberFormat.setMinimumFractionDigits(mnfd);
    /* step 27 */
    int mxfdDefault = "currency".equals(s) ? Math.max(mnfd, cDigits) : "percent".equals(s) ? Math.max(mnfd, 0) : Math.max(mnfd, 3);
    /* step 28 */
    int mxfd = GetNumberOption(cx, options, "maximumFractionDigits", mnfd, 20, mxfdDefault);
    /* step 29 */
    numberFormat.setMaximumFractionDigits(mxfd);
    /* step 30 */
    Object mnsd = Get(cx, options, "minimumSignificantDigits");
    /* step 31 */
    Object mxsd = Get(cx, options, "maximumSignificantDigits");
    /* step 32 */
    if (!Type.isUndefined(mnsd) || !Type.isUndefined(mxsd)) {
        int _mnsd = GetNumberOption(cx, options, "minimumSignificantDigits", 1, 21, 1);
        int _mxsd = GetNumberOption(cx, options, "maximumSignificantDigits", _mnsd, 21, 21);
        numberFormat.setMinimumSignificantDigits(_mnsd);
        numberFormat.setMaximumSignificantDigits(_mxsd);
    }
    /* step 33 */
    boolean g = GetBooleanOption(cx, options, "useGrouping", true);
    /* step 34 */
    numberFormat.setUseGrouping(g);
    /* steps 35-40 (not applicable) */
    /* step 41 */
    numberFormat.setBoundFormat(null);
/* step 42 (FIXME: spec bug - unnecessary internal slot) */
/* step 43 (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) ToObject(com.github.anba.es6draft.runtime.AbstractOperations.ToObject)

Example 85 with ScriptObject

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

the class JSONObject method SerializeJSONArray.

/**
     * 24.3.2.4 Runtime Semantics: SerializeJSONArray( value )
     * 
     * @param cx
     *            the execution context
     * @param serializer
     *            the serializer state
     * @param value
     *            the script array object
     * @param stack
     *            the current stack
     */
private static void SerializeJSONArray(ExecutionContext cx, JSONSerializer serializer, ScriptObject value) {
    /* steps 1-2 */
    if (!serializer.stack.add(value)) {
        throw newTypeError(cx, Messages.Key.JSONCyclicValue);
    }
    /* steps 3-5 (not applicable) */
    /* steps 6-7 */
    long len = ToLength(cx, Get(cx, value, "length"));
    /* steps 8-11 */
    String gap = serializer.gap;
    StringBuilder result = serializer.result;
    result.append('[');
    if (len > 0) {
        serializer.level += 1;
        for (long index = 0; index < len; ++index) {
            if (!gap.isEmpty()) {
                indent(serializer, result);
            }
            // Inlined: SerializeJSONProperty
            Object v = Get(cx, value, index);
            v = TransformJSONValue(cx, serializer, value, ToString(index), v);
            if (!IsJSONSerializable(v)) {
                result.append("null");
            } else {
                SerializeJSONValue(cx, serializer, v);
            }
            if (index + 1 < len) {
                result.append(',');
            }
        }
        serializer.level -= 1;
        if (!gap.isEmpty()) {
            indent(serializer, result);
        }
    }
    result.append(']');
    /* step 12 */
    serializer.stack.remove(value);
/* steps 13-14 (not applicable) */
}
Also used : StringObject(com.github.anba.es6draft.runtime.types.builtins.StringObject) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) NumberObject(com.github.anba.es6draft.runtime.objects.number.NumberObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)

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