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.JSCollator;
import com.oracle.truffle.js.runtime.util.IntlUtil;
public abstract class InitializeCollatorNode extends JavaScriptBaseNode {
private final JSContext context;
@Child JSToCanonicalizedLocaleListNode toCanonicalizedLocaleListNode;
@Child CreateOptionsObjectNode createOptionsNode;
@Child GetStringOptionNode getUsageOption;
@Child GetStringOptionNode getLocaleMatcherOption;
@Child GetBooleanOptionNode getNumericOption;
@Child GetStringOptionNode getCaseFirstOption;
@Child GetStringOptionNode getSensitivityOption;
@Child GetBooleanOptionNode getIgnorePunctuationOption;
private final BranchProfile errorBranch = BranchProfile.create();
protected InitializeCollatorNode(JSContext context) {
this.context = context;
this.toCanonicalizedLocaleListNode = JSToCanonicalizedLocaleListNode.create(context);
this.createOptionsNode = CreateOptionsObjectNodeGen.create(context);
this.getUsageOption = GetStringOptionNode.create(context, IntlUtil.USAGE, new String[]{IntlUtil.SORT, IntlUtil.SEARCH}, IntlUtil.SORT);
this.getLocaleMatcherOption = GetStringOptionNode.create(context, IntlUtil.LOCALE_MATCHER, new String[]{IntlUtil.LOOKUP, IntlUtil.BEST_FIT}, IntlUtil.BEST_FIT);
this.getNumericOption = GetBooleanOptionNode.create(context, IntlUtil.NUMERIC, null);
this.getCaseFirstOption = GetStringOptionNode.create(context, IntlUtil.CASE_FIRST, new String[]{IntlUtil.UPPER, IntlUtil.LOWER, IntlUtil.FALSE}, null);
this.getSensitivityOption = GetStringOptionNode.create(context, IntlUtil.SENSITIVITY, new String[]{IntlUtil.BASE, IntlUtil.ACCENT, IntlUtil.CASE, IntlUtil.VARIANT}, null);
this.getIgnorePunctuationOption = GetBooleanOptionNode.create(context, IntlUtil.IGNORE_PUNCTUATION, false);
}
public abstract DynamicObject executeInit(DynamicObject collator, Object locales, Object options);
public static InitializeCollatorNode createInitalizeCollatorNode(JSContext context) {
return InitializeCollatorNodeGen.create(context);
}
@Specialization
public DynamicObject initializeCollator(DynamicObject collatorObj, Object localesArg, Object optionsArg) {
try {
JSCollator.InternalState state = JSCollator.getInternalState(collatorObj);
String[] locales = toCanonicalizedLocaleListNode.executeLanguageTags(localesArg);
DynamicObject options = createOptionsNode.execute(optionsArg);
String usage = getUsageOption.executeValue(options);
String optLocaleMatcher = getLocaleMatcherOption.executeValue(options);
Boolean optkn = getNumericOption.executeValue(options);
String optkf = getCaseFirstOption.executeValue(options);
String sensitivity = getSensitivityOption.executeValue(options);
Boolean ignorePunctuation = getIgnorePunctuationOption.executeValue(options);
JSCollator.initializeCollator(context, state, locales, usage, optLocaleMatcher, optkn, optkf, sensitivity, ignorePunctuation);
} catch (MissingResourceException e) {
errorBranch.enter();
throw Errors.createICU4JDataError(e);
}
return collatorObj;
}
}