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.JSPluralRules;
import com.oracle.truffle.js.runtime.util.IntlUtil;
public abstract class InitializePluralRulesNode extends JavaScriptBaseNode {
private final JSContext context;
@Child JSToCanonicalizedLocaleListNode toCanonicalizedLocaleListNode;
@Child CreateOptionsObjectNode createOptionsNode;
@Child GetStringOptionNode getLocaleMatcherOption;
@Child SetNumberFormatDigitOptionsNode setNumberFormatDigitOptions;
@Child GetStringOptionNode getTypeOption;
private final BranchProfile errorBranch = BranchProfile.create();
protected InitializePluralRulesNode(JSContext context) {
this.context = context;
this.toCanonicalizedLocaleListNode = JSToCanonicalizedLocaleListNode.create(context);
this.createOptionsNode = CreateOptionsObjectNodeGen.create(context);
this.getLocaleMatcherOption = GetStringOptionNode.create(context, IntlUtil.LOCALE_MATCHER,
new String[]{IntlUtil.LOOKUP, IntlUtil.BEST_FIT}, IntlUtil.BEST_FIT);
this.getTypeOption = GetStringOptionNode.create(context, IntlUtil.TYPE, new String[]{IntlUtil.CARDINAL, IntlUtil.ORDINAL}, IntlUtil.CARDINAL);
this.setNumberFormatDigitOptions = SetNumberFormatDigitOptionsNode.create(context);
}
public abstract DynamicObject executeInit(DynamicObject collator, Object locales, Object options);
public static InitializePluralRulesNode createInitalizePluralRulesNode(JSContext context) {
return InitializePluralRulesNodeGen.create(context);
}
@Specialization
public DynamicObject initializePluralRules(DynamicObject pluralRulesObj, Object localesArg, Object optionsArg) {
try {
JSPluralRules.InternalState state = JSPluralRules.getInternalState(pluralRulesObj);
String[] locales = toCanonicalizedLocaleListNode.executeLanguageTags(localesArg);
DynamicObject options = createOptionsNode.execute(optionsArg);
getLocaleMatcherOption.executeValue(options);
String optType = getTypeOption.executeValue(options);
state.setType(optType);
state.resolveLocaleAndNumberingSystem(context, locales, null);
setNumberFormatDigitOptions.execute(state, options, 0, 3, false);
state.initializeNumberFormatter();
state.initializePluralRules();
} catch (MissingResourceException e) {
errorBranch.enter();
throw Errors.createICU4JDataError(e);
}
return pluralRulesObj;
}
}