Skip to content

Commit 74de9c5

Browse files
committed
Support for 'notation' option in Intl.PluralRules.
1 parent 8ff76a3 commit 74de9c5

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/nodes/intl/InitializePluralRulesNode.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -58,6 +58,7 @@
5858
public abstract class InitializePluralRulesNode extends JavaScriptBaseNode {
5959

6060
private static final List<String> TYPE_OPTION_VALUES = List.of(IntlUtil.CARDINAL, IntlUtil.ORDINAL);
61+
private static final List<String> NOTATION_OPTION_VALUES = List.of(IntlUtil.STANDARD, IntlUtil.SCIENTIFIC, IntlUtil.ENGINEERING, IntlUtil.COMPACT);
6162

6263
private final JSContext context;
6364

@@ -69,6 +70,7 @@ public abstract class InitializePluralRulesNode extends JavaScriptBaseNode {
6970
@Child SetNumberFormatDigitOptionsNode setNumberFormatDigitOptions;
7071

7172
@Child GetStringOptionNode getTypeOption;
73+
@Child GetStringOptionNode getNotationOption;
7274
private final BranchProfile errorBranch = BranchProfile.create();
7375

7476
protected InitializePluralRulesNode(JSContext context) {
@@ -77,6 +79,7 @@ protected InitializePluralRulesNode(JSContext context) {
7779
this.coerceOptionsToObjectNode = CoerceOptionsToObjectNodeGen.create(context);
7880
this.getLocaleMatcherOption = GetStringOptionNode.create(context, IntlUtil.KEY_LOCALE_MATCHER, GetStringOptionNode.LOCALE_MATCHER_OPTION_VALUES, IntlUtil.BEST_FIT);
7981
this.getTypeOption = GetStringOptionNode.create(context, IntlUtil.KEY_TYPE, TYPE_OPTION_VALUES, IntlUtil.CARDINAL);
82+
this.getNotationOption = GetStringOptionNode.create(context, IntlUtil.KEY_NOTATION, NOTATION_OPTION_VALUES, IntlUtil.STANDARD);
8083
this.setNumberFormatDigitOptions = SetNumberFormatDigitOptionsNode.create(context);
8184
}
8285

@@ -98,9 +101,11 @@ public JSPluralRulesObject initializePluralRules(JSPluralRulesObject pluralRules
98101

99102
getLocaleMatcherOption.executeValue(options);
100103
String optType = getTypeOption.executeValue(options);
101-
102104
state.setType(optType);
103105

106+
String notation = getNotationOption.executeValue(options);
107+
state.setNotation(notation);
108+
104109
state.resolveLocaleAndNumberingSystem(context, locales, null);
105110
setNumberFormatDigitOptions.execute(state, options, 0, 3, false);
106111

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/builtins/intl/JSNumberFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private static JSNumberFormatObject create(JSObjectFactory factory, JSRealm real
199199
return factory.trackAllocation(newObj);
200200
}
201201

202-
private static Notation notationToICUNotation(String notation, String compactDisplay) {
202+
static Notation notationToICUNotation(String notation, String compactDisplay) {
203203
Notation icuNotation;
204204
switch (notation) {
205205
case IntlUtil.STANDARD:

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/builtins/intl/JSPluralRules.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -49,6 +49,7 @@
4949
import org.graalvm.shadowed.com.ibm.icu.number.LocalizedNumberFormatter;
5050
import org.graalvm.shadowed.com.ibm.icu.number.LocalizedNumberRangeFormatter;
5151
import org.graalvm.shadowed.com.ibm.icu.number.NumberRangeFormatter;
52+
import org.graalvm.shadowed.com.ibm.icu.number.UnlocalizedNumberFormatter;
5253
import org.graalvm.shadowed.com.ibm.icu.text.PluralRules;
5354
import org.graalvm.shadowed.com.ibm.icu.text.PluralRules.PluralType;
5455

@@ -150,13 +151,15 @@ public static class InternalState extends JSNumberFormat.BasicInternalState {
150151
private LocalizedNumberRangeFormatter numberRangeFormatter;
151152

152153
private String type;
154+
private String notation;
153155
private PluralRules pluralRules;
154156
private final List<TruffleString> pluralCategories = new LinkedList<>();
155157

156158
@Override
157159
void fillResolvedOptions(JSContext context, JSRealm realm, JSDynamicObject result) {
158160
JSObjectUtil.putDataProperty(result, IntlUtil.KEY_LOCALE, Strings.fromJavaString(getLocale()), JSAttributes.getDefault());
159161
JSObjectUtil.putDataProperty(result, IntlUtil.KEY_TYPE, Strings.fromJavaString(type), JSAttributes.getDefault());
162+
JSObjectUtil.putDataProperty(result, IntlUtil.KEY_NOTATION, Strings.fromJavaString(notation), JSAttributes.getDefault());
160163
super.fillBasicResolvedOptions(result);
161164
JSObjectUtil.putDataProperty(result, IntlUtil.KEY_PLURAL_CATEGORIES, JSRuntime.createArrayFromList(realm.getContext(), realm, pluralCategories), JSAttributes.getDefault());
162165
super.fillRoundingResolvedOptions(result);
@@ -177,8 +180,10 @@ public void initializePluralRules() {
177180
@TruffleBoundary
178181
public void initializeNumberFormatter() {
179182
super.initializeNumberFormatter();
180-
numberFormatter = getUnlocalizedFormatter().locale(getJavaLocale());
181-
numberRangeFormatter = NumberRangeFormatter.withLocale(getJavaLocale()).numberFormatterBoth(getUnlocalizedFormatter());
183+
UnlocalizedNumberFormatter formatter = getUnlocalizedFormatter();
184+
formatter = formatter.notation(JSNumberFormat.notationToICUNotation(notation, IntlUtil.LONG));
185+
numberFormatter = formatter.locale(getJavaLocale());
186+
numberRangeFormatter = NumberRangeFormatter.withLocale(getJavaLocale()).numberFormatterBoth(formatter);
182187
}
183188

184189
public PluralRules getPluralRules() {
@@ -196,6 +201,10 @@ public LocalizedNumberRangeFormatter getNumberRangeFormatter() {
196201
public void setType(String type) {
197202
this.type = type;
198203
}
204+
205+
public void setNotation(String notation) {
206+
this.notation = notation;
207+
}
199208
}
200209

201210
@TruffleBoundary

0 commit comments

Comments
 (0)