Search in sources :

Example 1 with ToObject

use of com.github.anba.es6draft.runtime.AbstractOperations.ToObject in project es6draft by anba.

the class ShellCompleter method complete.

@Override
public Optional<Completion> complete(String line, int cursor) {
    ExecutionContext cx = realm.defaultContext();
    ScriptObject object = realm.getGlobalThis();
    String leftContext = line.substring(0, cursor);
    if (leftContext.isEmpty()) {
        ArrayList<String> candidates = createCandidates(getPropertyNames(cx, object), "", "");
        return Optional.of(new Completion(line, 0, cursor, candidates));
    }
    Matcher m = hierarchyPattern.matcher(leftContext);
    if (!m.find()) {
        return Optional.empty();
    }
    ArrayList<String> segments = segments(m.group(1));
    StringBuilder prefix = new StringBuilder();
    List<String> properties = segments.subList(0, segments.size() - 1);
    if (!properties.isEmpty() && "this".equals(properties.get(0))) {
        // skip leading `this` segment in property traversal
        properties = properties.subList(1, properties.size());
        prefix.append("this.");
    }
    for (String property : properties) {
        if (!HasProperty(cx, object, property)) {
            return Optional.empty();
        }
        Object value = Get(cx, object, property);
        if (Type.isObject(value)) {
            object = Type.objectValue(value);
        } else if (!Type.isUndefinedOrNull(value)) {
            object = ToObject(cx, value);
        } else {
            return Optional.empty();
        }
        prefix.append(property).append('.');
    }
    String partial = segments.get(segments.size() - 1);
    ArrayList<String> candidates = createCandidates(getPropertyNames(cx, object), partial, prefix.toString());
    return Optional.of(new Completion(line, m.start(1), cursor, candidates));
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Completion(com.github.anba.es6draft.repl.console.ShellConsole.Completion) Matcher(java.util.regex.Matcher) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ToObject(com.github.anba.es6draft.runtime.AbstractOperations.ToObject)

Example 2 with ToObject

use of com.github.anba.es6draft.runtime.AbstractOperations.ToObject 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)

Aggregations

ToObject (com.github.anba.es6draft.runtime.AbstractOperations.ToObject)2 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)2 Completion (com.github.anba.es6draft.repl.console.ShellConsole.Completion)1 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)1 OptionsRecord (com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord)1 ResolvedLocale (com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.ResolvedLocale)1 Matcher (java.util.regex.Matcher)1