Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Jint/Native/Global/GlobalObject.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public partial class GlobalObject
private static readonly Key propertyInt32Array = "Int32Array";
private static readonly Key propertyInt8Array = "Int8Array";
private static readonly Key propertyIterator = "Iterator";
//private static readonly Key propertyIntl = "Intl";
private static readonly Key propertyIntl = "Intl";
private static readonly Key propertyJSON = "JSON";
private static readonly Key propertyMap = "Map";
private static readonly Key propertyMath = "Math";
Expand Down Expand Up @@ -110,7 +110,7 @@ protected override void Initialize()
properties.AddDangerous(propertyInt16Array, new LazyPropertyDescriptor<GlobalObject>(this, static global => global._realm.Intrinsics.Int16Array, PropertyFlags));
properties.AddDangerous(propertyInt32Array, new LazyPropertyDescriptor<GlobalObject>(this, static global => global._realm.Intrinsics.Int32Array, PropertyFlags));
properties.AddDangerous(propertyInt8Array, new LazyPropertyDescriptor<GlobalObject>(this, static global => global._realm.Intrinsics.Int8Array, PropertyFlags));
// TODO properties.AddDapropertygerous(propertyIntl, new LazyPropertyDescriptor<GlobalObject>(this, static global => global._realm.Intrinsics.Intl, propertyFlags));
properties.AddDangerous(propertyIntl, new LazyPropertyDescriptor<GlobalObject>(this, static global => global._realm.Intrinsics.Intl, PropertyFlags));
properties.AddDangerous(propertyIterator, new LazyPropertyDescriptor<GlobalObject>(this, static global => global._realm.Intrinsics.Iterator, PropertyFlags));
properties.AddDangerous(propertyJSON, new LazyPropertyDescriptor<GlobalObject>(this, static global => global._realm.Intrinsics.Json, PropertyFlags));
properties.AddDangerous(propertyMap, new LazyPropertyDescriptor<GlobalObject>(this, static global => global._realm.Intrinsics.Map, PropertyFlags));
Expand Down
159 changes: 142 additions & 17 deletions Jint/Native/Intl/IntlInstance.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue

using System.Linq;
using Jint.Native.Object;
using Jint.Native.Symbol;
using Jint.Runtime;
Expand All @@ -24,21 +23,20 @@ internal IntlInstance(
_prototype = objectPrototype;
}

/// <summary>
/// Gets the CLDR provider from engine options.
/// </summary>
private ICldrProvider CldrProvider => _engine.Options.Intl.CldrProvider;

protected override void Initialize()
{
// TODO check length
var properties = new PropertyDictionary(10, checkExistingKeys: false)
{
["Collator"] = new(_realm.Intrinsics.Collator, false, false, true),
["DateTimeFormat"] = new(_realm.Intrinsics.DateTimeFormat, false, false, true),
["DisplayNames"] = new(_realm.Intrinsics.DisplayNames, false, false, true),
["ListFormat"] = new(_realm.Intrinsics.ListFormat, false, false, true),
["Locale"] = new(_realm.Intrinsics.Locale, false, false, true),
["NumberFormat"] = new(_realm.Intrinsics.NumberFormat, false, false, true),
["PluralRules"] = new(_realm.Intrinsics.PluralRules, false, false, true),
["RelativeTimeFormat"] = new(_realm.Intrinsics.RelativeTimeFormat, false, false, true),
["Segmenter"] = new(_realm.Intrinsics.Segmenter, false, false, true),
["getCanonicalLocales "] = new(new ClrFunction(Engine, "getCanonicalLocales ", GetCanonicalLocales, 1, PropertyFlag.Configurable), true, false, true),
const PropertyFlag PropertyFlags = PropertyFlag.Writable | PropertyFlag.Configurable;

var properties = new PropertyDictionary(3, checkExistingKeys: false)
{
["Locale"] = new(_realm.Intrinsics.Locale, PropertyFlags),
["getCanonicalLocales"] = new(new ClrFunction(Engine, "getCanonicalLocales", GetCanonicalLocales, 1, PropertyFlag.Configurable), PropertyFlags),
["supportedValuesOf"] = new(new ClrFunction(Engine, "supportedValuesOf", SupportedValuesOf, 1, PropertyFlag.Configurable), PropertyFlags),
};
SetProperties(properties);

Expand All @@ -49,8 +47,135 @@ protected override void Initialize()
SetSymbols(symbols);
}

private JsValue GetCanonicalLocales(JsValue thisObject, JsCallArguments arguments)
/// <summary>
/// https://tc39.es/ecma402/#sec-intl.getcanonicallocales
/// </summary>
private JsArray GetCanonicalLocales(JsValue thisObject, JsCallArguments arguments)
{
var locales = arguments.At(0);
return new JsArray(_engine, IntlUtilities.CanonicalizeLocaleList(_engine, locales).Select(x => new JsString(x)).ToArray<JsValue>());
}

/// <summary>
/// https://tc39.es/ecma402/#sec-intl.supportedvaluesof
/// </summary>
private JsValue SupportedValuesOf(JsValue thisObject, JsCallArguments arguments)
{
return new JsArray(_engine);
var key = TypeConverter.ToString(arguments.At(0));

string[] values;
switch (key)
{
case "calendar":
values = GetSupportedCalendars();
break;
case "collation":
values = GetSupportedCollations();
break;
case "currency":
values = GetSupportedCurrencies();
break;
case "numberingSystem":
values = GetSupportedNumberingSystems();
break;
case "timeZone":
values = GetSupportedTimeZones();
break;
case "unit":
values = GetSupportedUnits();
break;
default:
Throw.RangeError(_realm, $"Invalid key: {key}");
return Undefined;
}

// Sort values alphabetically
System.Array.Sort(values, StringComparer.Ordinal);

var result = new JsArray(_engine, (uint) values.Length);
for (var i = 0; i < values.Length; i++)
{
result.SetIndexValue((uint) i, values[i], updateLength: true);
}

return result;
}

private string[] GetSupportedCalendars()
{
// Use CLDR provider for supported calendars
var calendars = CldrProvider.GetSupportedCalendars();
var result = new string[calendars.Count];
var i = 0;
foreach (var calendar in calendars)
{
result[i++] = calendar;
}
return result;
}

private string[] GetSupportedCollations()
{
// Use CLDR provider for supported collations
var collations = CldrProvider.GetSupportedCollations();
var result = new string[collations.Count];
var i = 0;
foreach (var collation in collations)
{
result[i++] = collation;
}
return result;
}

private string[] GetSupportedCurrencies()
{
// Use CLDR provider for supported currencies
var currencies = CldrProvider.GetSupportedCurrencies();
var result = new string[currencies.Count];
var i = 0;
foreach (var currency in currencies)
{
result[i++] = currency;
}
return result;
}

private string[] GetSupportedNumberingSystems()
{
// Use CLDR provider for supported numbering systems
var systems = CldrProvider.GetSupportedNumberingSystems();
var result = new string[systems.Count];
var i = 0;
foreach (var system in systems)
{
result[i++] = system;
}
return result;
}

private string[] GetSupportedTimeZones()
{
// Use CLDR provider for supported time zones
var zones = CldrProvider.GetSupportedTimeZones();
var result = new string[zones.Count];
var i = 0;
foreach (var zone in zones)
{
result[i++] = zone;
}
return result;
}

private string[] GetSupportedUnits()
{
// Use CLDR provider for supported units
var units = CldrProvider.GetSupportedUnits();
var result = new string[units.Count];
var i = 0;
foreach (var unit in units)
{
result[i++] = unit;
}
return result;
}
}
Loading