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.JSRelativeTimeFormat;
import com.oracle.truffle.js.runtime.util.IntlUtil;
public abstract class InitializeRelativeTimeFormatNode extends JavaScriptBaseNode {
private final JSContext context;
@Child JSToCanonicalizedLocaleListNode toCanonicalizedLocaleListNode;
@Child CreateOptionsObjectNode createOptionsNode;
@Child GetStringOptionNode getLocaleMatcherOption;
@Child GetStringOptionNode getNumberingSystemOption;
@Child GetStringOptionNode getStyleOption;
@Child GetStringOptionNode getNumericOption;
private final BranchProfile errorBranch = BranchProfile.create();
protected InitializeRelativeTimeFormatNode(JSContext context) {
this.context = context;
this.toCanonicalizedLocaleListNode = JSToCanonicalizedLocaleListNode.create(context);
this.createOptionsNode = CreateOptionsObjectNodeGen.create(context);
this.getStyleOption = GetStringOptionNode.create(context, IntlUtil.STYLE, new String[]{IntlUtil.LONG, IntlUtil.SHORT, IntlUtil.NARROW}, IntlUtil.LONG);
this.getNumericOption = GetStringOptionNode.create(context, "numeric", new String[]{IntlUtil.ALWAYS, IntlUtil.AUTO}, IntlUtil.ALWAYS);
this.getLocaleMatcherOption = GetStringOptionNode.create(context, IntlUtil.LOCALE_MATCHER,
new String[]{IntlUtil.LOOKUP, IntlUtil.BEST_FIT}, IntlUtil.BEST_FIT);
this.getNumberingSystemOption = GetStringOptionNode.create(context, IntlUtil.NUMBERING_SYSTEM, null, null);
}
public abstract DynamicObject executeInit(DynamicObject collator, Object locales, Object options);
public static InitializeRelativeTimeFormatNode createInitalizeRelativeTimeFormatNode(JSContext context) {
return InitializeRelativeTimeFormatNodeGen.create(context);
}
@Specialization
public DynamicObject initializeRelativeTimeFormat(DynamicObject relativeTimeFormatObj, Object localesArg, Object optionsArg) {
try {
JSRelativeTimeFormat.InternalState state = JSRelativeTimeFormat.getInternalState(relativeTimeFormatObj);
String[] locales = toCanonicalizedLocaleListNode.executeLanguageTags(localesArg);
DynamicObject options = createOptionsNode.execute(optionsArg);
getLocaleMatcherOption.executeValue(options);
String numberingSystem = getNumberingSystemOption.executeValue(options);
if (numberingSystem != null) {
IntlUtil.validateUnicodeLocaleIdentifierType(numberingSystem, errorBranch);
numberingSystem = IntlUtil.normalizeUnicodeLocaleIdentifierType(numberingSystem);
}
String style = getStyleOption.executeValue(options);
String numeric = getNumericOption.executeValue(options);
state.setStyle(style);
state.setNumeric(numeric);
state.resolveLocaleAndNumberingSystem(context, locales, numberingSystem);
state.initializeRelativeTimeFormatter();
} catch (MissingResourceException e) {
errorBranch.enter();
throw Errors.createICU4JDataError(e);
}
return relativeTimeFormatObj;
}
}