package com.oracle.truffle.js.nodes.intl;
import java.util.MissingResourceException;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.js.nodes.JavaScriptBaseNode;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.builtins.intl.JSListFormat;
import com.oracle.truffle.js.runtime.util.IntlUtil;
public abstract class InitializeListFormatNode extends JavaScriptBaseNode {
private final JSContext context;
@Child JSToCanonicalizedLocaleListNode toCanonicalizedLocaleListNode;
@Child CreateOptionsObjectNode createOptionsNode;
@Child GetStringOptionNode getLocaleMatcherOption;
@Child GetStringOptionNode getTypeOption;
@Child GetStringOptionNode getStyleOption;
private final BranchProfile errorBranch = BranchProfile.create();
protected InitializeListFormatNode(JSContext context) {
this.context = context;
this.toCanonicalizedLocaleListNode = JSToCanonicalizedLocaleListNode.create(context);
this.createOptionsNode = CreateOptionsObjectNodeGen.create(context);
this.getTypeOption = GetStringOptionNode.create(context, IntlUtil.TYPE, new String[]{IntlUtil.CONJUNCTION, IntlUtil.DISJUNCTION, IntlUtil.UNIT}, IntlUtil.CONJUNCTION);
this.getStyleOption = GetStringOptionNode.create(context, IntlUtil.STYLE, new String[]{IntlUtil.LONG, IntlUtil.SHORT, IntlUtil.NARROW}, IntlUtil.LONG);
this.getLocaleMatcherOption = GetStringOptionNode.create(context, IntlUtil.LOCALE_MATCHER,
new String[]{IntlUtil.LOOKUP, IntlUtil.BEST_FIT}, IntlUtil.BEST_FIT);
}
public abstract DynamicObject executeInit(DynamicObject collator, Object locales, Object options);
public static InitializeListFormatNode createInitalizeListFormatNode(JSContext context) {
return InitializeListFormatNodeGen.create(context);
}
@Specialization
public DynamicObject initializeListFormat(DynamicObject listFormatObj, Object localesArg, Object optionsArg) {
try {
JSListFormat.InternalState state = JSListFormat.getInternalState(listFormatObj);
String[] locales = toCanonicalizedLocaleListNode.executeLanguageTags(localesArg);
DynamicObject options = createOptionsNode.execute(optionsArg);
getLocaleMatcherOption.executeValue(options);
String optType = getTypeOption.executeValue(options);
String optStyle = getStyleOption.executeValue(options);
state.setType(optType);
state.setStyle(optStyle);
JSListFormat.setLocale(context, state, locales);
JSListFormat.setupInternalListFormatter(state);
} catch (MissingResourceException e) {
errorBranch.enter();
throw Errors.createICU4JDataError(e);
}
return listFormatObj;
}
}