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.nodes.cast.JSToObjectNode;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.builtins.intl.JSDisplayNames;
import com.oracle.truffle.js.runtime.util.IntlUtil;
public abstract class InitializeDisplayNamesNode extends JavaScriptBaseNode {
private final JSContext context;
@Child JSToCanonicalizedLocaleListNode toCanonicalizedLocaleListNode;
@Child JSToObjectNode toObjectNode;
@Child GetStringOptionNode getLocaleMatcherOption;
@Child GetStringOptionNode getStyleOption;
@Child GetStringOptionNode getTypeOption;
@Child GetStringOptionNode getFallbackOption;
private final BranchProfile errorBranch = BranchProfile.create();
protected InitializeDisplayNamesNode(JSContext context) {
this.context = context;
this.toCanonicalizedLocaleListNode = JSToCanonicalizedLocaleListNode.create(context);
this.toObjectNode = JSToObjectNode.createToObject(context);
this.getLocaleMatcherOption = GetStringOptionNode.create(context, IntlUtil.LOCALE_MATCHER, new String[]{IntlUtil.LOOKUP, IntlUtil.BEST_FIT}, IntlUtil.BEST_FIT);
this.getStyleOption = GetStringOptionNode.create(context, IntlUtil.STYLE, new String[]{IntlUtil.NARROW, IntlUtil.SHORT, IntlUtil.LONG}, IntlUtil.LONG);
this.getTypeOption = GetStringOptionNode.create(context, IntlUtil.TYPE, new String[]{IntlUtil.LANGUAGE, IntlUtil.REGION, IntlUtil.SCRIPT, IntlUtil.CURRENCY}, null);
this.getFallbackOption = GetStringOptionNode.create(context, IntlUtil.FALLBACK, new String[]{IntlUtil.CODE, IntlUtil.NONE}, IntlUtil.CODE);
}
public abstract DynamicObject executeInit(DynamicObject displayNames, Object locales, Object options);
public static InitializeDisplayNamesNode createInitalizeDisplayNamesNode(JSContext context) {
return InitializeDisplayNamesNodeGen.create(context);
}
@Specialization
public DynamicObject initializeDisplayNames(DynamicObject displayNamesObject, Object localesArg, Object optionsArg) {
try {
String[] locales = toCanonicalizedLocaleListNode.executeLanguageTags(localesArg);
Object options = toObjectNode.execute(optionsArg);
getLocaleMatcherOption.executeValue(options);
String optStyle = getStyleOption.executeValue(options);
String optType = getTypeOption.executeValue(options);
if (optType == null) {
errorBranch.enter();
throw Errors.createTypeError("'type' option not specified");
}
String optFallback = getFallbackOption.executeValue(options);
JSDisplayNames.InternalState state = JSDisplayNames.getInternalState(displayNamesObject);
JSDisplayNames.setupInternalState(context, state, locales, optStyle, optType, optFallback);
} catch (MissingResourceException e) {
errorBranch.enter();
throw Errors.createICU4JDataError(e);
}
return displayNamesObject;
}
}