Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[browser][hybrid] drop wrap_error_root, wrap_no_error_root #101390

Merged
merged 11 commits into from
Apr 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ internal static partial class Interop
internal static unsafe partial class JsGlobalization
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int GetCalendarInfo(in string culture, CalendarId calendarId, char* buffer, int bufferLength, out int exceptionalResult, out object result);
internal static extern unsafe nint GetCalendarInfo(char* culture, int cultureLength, CalendarId calendarId, char* buffer, int bufferMaxLength, out int bufferLength);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ internal static partial class Interop
internal static unsafe partial class JsGlobalization
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int CompareString(in string culture, char* str1, int str1Len, char* str2, int str2Len, global::System.Globalization.CompareOptions options, out int exceptionalResult, out object result);
internal static extern unsafe nint CompareString(char* culture, int cultureLength, char* str1, int str1Len, char* str2, int str2Len, global::System.Globalization.CompareOptions options, out int resultPtr);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe bool StartsWith(in string culture, char* str1, int str1Len, char* str2, int str2Len, global::System.Globalization.CompareOptions options, out int exceptionalResult, out object result);
internal static extern unsafe nint StartsWith(char* culture, int cultureLength, char* str1, int str1Len, char* str2, int str2Len, global::System.Globalization.CompareOptions options, out bool resultPtr);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe bool EndsWith(in string culture, char* str1, int str1Len, char* str2, int str2Len, global::System.Globalization.CompareOptions options, out int exceptionalResult, out object result);
internal static extern unsafe nint EndsWith(char* culture, int cultureLength, char* str1, int str1Len, char* str2, int str2Len, global::System.Globalization.CompareOptions options, out bool resultPtr);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int IndexOf(in string culture, char* str1, int str1Len, char* str2, int str2Len, global::System.Globalization.CompareOptions options, bool fromBeginning, out int exceptionalResult, out object result);
internal static extern unsafe nint IndexOf(char* culture, int cultureLength, char* str1, int str1Len, char* str2, int str2Len, global::System.Globalization.CompareOptions options, bool fromBeginning, out int resultPtr);
}
}
8 changes: 4 additions & 4 deletions src/libraries/Common/src/Interop/Browser/Interop.Locale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ internal static partial class Interop
internal static unsafe partial class JsGlobalization
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int GetCultureInfo(in string culture, char* buffer, int bufferLength, out int exceptionalResult, out object result);
internal static extern unsafe nint GetCultureInfo(char* culture, int cultureLength, char* buffer, int bufferMaxLength, out int resultLength);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int GetFirstDayOfWeek(in string culture, out int exceptionalResult, out object result);
internal static extern unsafe nint GetFirstDayOfWeek(char* culture, int cultureLength, out int resultPtr);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int GetFirstWeekOfYear(in string culture, out int exceptionalResult, out object result);
internal static extern unsafe nint GetFirstWeekOfYear(char* culture, int cultureLength, out int resultPtr);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int GetLocaleInfo(in string locale, in string culture, char* buffer, int bufferLength, out int exceptionalResult, out object result);
internal static extern unsafe nint GetLocaleInfo(char* locale, int localeLength, char* culture, int cultureLength, char* buffer, int bufferLength, out int resultLength);
}
}
4 changes: 2 additions & 2 deletions src/libraries/Common/src/Interop/Browser/Interop.TextInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ internal static partial class Interop
internal static unsafe partial class JsGlobalization
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe void ChangeCaseInvariant(char* src, int srcLen, char* dstBuffer, int dstBufferCapacity, bool bToUpper, out int exceptionalResult, out object result);
internal static extern unsafe nint ChangeCaseInvariant(char* src, int srcLen, char* dstBuffer, int dstBufferCapacity, bool bToUpper);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe void ChangeCase(in string culture, char* src, int srcLen, char* dstBuffer, int dstBufferCapacity, bool bToUpper, out int exceptionalResult, out object result);
internal static extern unsafe nint ChangeCase(char* culture, int cultureLen, char* src, int srcLen, char* dstBuffer, int dstBufferCapacity, bool bToUpper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,33 @@ internal sealed partial class CalendarData
private const int CALENDAR_INFO_BUFFER_LEN = 1000;
private unsafe bool JSLoadCalendarDataFromBrowser(string localeName, CalendarId calendarId)
{
char* buffer = stackalloc char[CALENDAR_INFO_BUFFER_LEN];
int exception;
object exResult;
int resultLength = Interop.JsGlobalization.GetCalendarInfo(localeName, calendarId, buffer, CALENDAR_INFO_BUFFER_LEN, out exception, out exResult);
if (exception != 0)
throw new Exception((string)exResult);
string result = new string(buffer, 0, resultLength);
string[] subresults = result.Split("##");
if (subresults.Length < 14)
throw new Exception("CalendarInfo recieved from the Browser is in icorrect format.");
// JS always has one result per locale, so even arrays are initialized with one element
this.sNativeName = string.IsNullOrEmpty(subresults[0]) ? ((CalendarId)calendarId).ToString() : subresults[0]; // this is EnglishName, not NativeName but it's the best we can do
this.saYearMonths = new string[] { subresults[1] };
this.sMonthDay = subresults[2];
this.saLongDates = new string[] { subresults[3] };
this.saShortDates = new string[] { subresults[4] };
this.saEraNames = new string[] { subresults[5] };
this.saAbbrevEraNames = new string[] { subresults[6] };
this.saDayNames = subresults[7].Split("||");
this.saAbbrevDayNames = subresults[8].Split("||");
this.saSuperShortDayNames = subresults[9].Split("||");
this.saMonthNames = ResizeMonthsArray(subresults[10].Split("||"));
this.saAbbrevMonthNames = ResizeMonthsArray(subresults[11].Split("||"));
this.saMonthGenitiveNames = ResizeMonthsArray(subresults[12].Split("||"));
this.saAbbrevMonthGenitiveNames = ResizeMonthsArray(subresults[13].Split("||"));
return true;
ReadOnlySpan<char> localeNameSpan = localeName.AsSpan();
fixed (char* pLocaleName = &MemoryMarshal.GetReference(localeNameSpan))
{
char* buffer = stackalloc char[CALENDAR_INFO_BUFFER_LEN];
nint exceptionPtr = Interop.JsGlobalization.GetCalendarInfo(pLocaleName, localeNameSpan.Length, calendarId, buffer, CALENDAR_INFO_BUFFER_LEN, out int resultLength);
Helper.MarshalAndThrowIfException(exceptionPtr);
string result = new string(buffer, 0, resultLength);
string[] subresults = result.Split("##");
if (subresults.Length < 14)
throw new Exception("CalendarInfo recieved from the Browser is in icorrect format.");
// JS always has one result per locale, so even arrays are initialized with one element
this.sNativeName = string.IsNullOrEmpty(subresults[0]) ? ((CalendarId)calendarId).ToString() : subresults[0]; // this is EnglishName, not NativeName but it's the best we can do
this.saYearMonths = new string[] { subresults[1] };
this.sMonthDay = subresults[2];
this.saLongDates = new string[] { subresults[3] };
this.saShortDates = new string[] { subresults[4] };
this.saEraNames = new string[] { subresults[5] };
this.saAbbrevEraNames = new string[] { subresults[6] };
this.saDayNames = subresults[7].Split("||");
this.saAbbrevDayNames = subresults[8].Split("||");
this.saSuperShortDayNames = subresults[9].Split("||");
this.saMonthNames = ResizeMonthsArray(subresults[10].Split("||"));
this.saAbbrevMonthNames = ResizeMonthsArray(subresults[11].Split("||"));
this.saMonthGenitiveNames = ResizeMonthsArray(subresults[12].Split("||"));
this.saAbbrevMonthGenitiveNames = ResizeMonthsArray(subresults[13].Split("||"));
return true;
}

static string[] ResizeMonthsArray(string[] months)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,13 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan<char> source, Rea
#if TARGET_BROWSER
if (GlobalizationMode.Hybrid)
{
int result = Interop.JsGlobalization.IndexOf(m_name, b, target.Length, a, source.Length, options, fromBeginning, out int exception, out object ex_result);
if (exception != 0)
throw new Exception((string)ex_result);
return result;
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
{
nint exceptionPtr = Interop.JsGlobalization.IndexOf(pCultureName, cultureNameSpan.Length, b, target.Length, a, source.Length, options, fromBeginning, out int result);
Helper.MarshalAndThrowIfException(exceptionPtr);
return result;
}
}
#elif TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
if (GlobalizationMode.Hybrid)
Expand Down Expand Up @@ -300,10 +303,13 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan<char> source, ReadOnlySpan<
#if TARGET_BROWSER
if (GlobalizationMode.Hybrid)
{
int result = Interop.JsGlobalization.IndexOf(m_name, b, target.Length, a, source.Length, options, fromBeginning, out int exception, out object ex_result);
if (exception != 0)
throw new Exception((string)ex_result);
return result;
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
{
nint exceptionPtr = Interop.JsGlobalization.IndexOf(pCultureName, cultureNameSpan.Length, b, target.Length, a, source.Length, options, fromBeginning, out int result);
Helper.MarshalAndThrowIfException(exceptionPtr);
return result;
}
}
#elif TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
if (GlobalizationMode.Hybrid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,86 +47,74 @@ private static void AssertIndexingSupported(CompareOptions options, string cultu
private unsafe int JsCompareString(ReadOnlySpan<char> string1, ReadOnlySpan<char> string2, CompareOptions options)
{
AssertHybridOnWasm(options);
string cultureName = m_name;
AssertComparisonSupported(options, cultureName);
AssertComparisonSupported(options, m_name);

int cmpResult;
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
fixed (char* pString1 = &MemoryMarshal.GetReference(string1))
fixed (char* pString2 = &MemoryMarshal.GetReference(string2))
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
{
cmpResult = Interop.JsGlobalization.CompareString(cultureName, pString1, string1.Length, pString2, string2.Length, options, out int exception, out object ex_result);
if (exception != 0)
throw new Exception((string)ex_result);
nint exceptionPtr = Interop.JsGlobalization.CompareString(pCultureName, cultureNameSpan.Length, pString1, string1.Length, pString2, string2.Length, options, out int cmpResult);
Helper.MarshalAndThrowIfException(exceptionPtr);
return cmpResult;
}

return cmpResult;
}

private unsafe bool JsStartsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options)
{
AssertHybridOnWasm(options);
Debug.Assert(!prefix.IsEmpty);
string cultureName = m_name;
AssertIndexingSupported(options, cultureName);
AssertIndexingSupported(options, m_name);

bool result;
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
fixed (char* pSource = &MemoryMarshal.GetReference(source))
fixed (char* pPrefix = &MemoryMarshal.GetReference(prefix))
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
{
result = Interop.JsGlobalization.StartsWith(cultureName, pSource, source.Length, pPrefix, prefix.Length, options, out int exception, out object ex_result);
if (exception != 0)
throw new Exception((string)ex_result);
nint exceptionPtr = Interop.JsGlobalization.StartsWith(pCultureName, cultureNameSpan.Length, pSource, source.Length, pPrefix, prefix.Length, options, out bool result);
Helper.MarshalAndThrowIfException(exceptionPtr);
return result;
}


return result;
}

private unsafe bool JsEndsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options)
{
AssertHybridOnWasm(options);
Debug.Assert(!prefix.IsEmpty);
string cultureName = m_name;
AssertIndexingSupported(options, cultureName);
AssertIndexingSupported(options, m_name);

bool result;
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
fixed (char* pSource = &MemoryMarshal.GetReference(source))
fixed (char* pPrefix = &MemoryMarshal.GetReference(prefix))
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
{
result = Interop.JsGlobalization.EndsWith(cultureName, pSource, source.Length, pPrefix, prefix.Length, options, out int exception, out object ex_result);
if (exception != 0)
throw new Exception((string)ex_result);
nint exceptionPtr = Interop.JsGlobalization.EndsWith(pCultureName, cultureNameSpan.Length, pSource, source.Length, pPrefix, prefix.Length, options, out bool result);
Helper.MarshalAndThrowIfException(exceptionPtr);
return result;
}

return result;
}

private unsafe int JsIndexOfCore(ReadOnlySpan<char> source, ReadOnlySpan<char> target, CompareOptions options, int* matchLengthPtr, bool fromBeginning)
{
AssertHybridOnWasm(options);
Debug.Assert(!target.IsEmpty);
string cultureName = m_name;
AssertIndexingSupported(options, cultureName);
AssertIndexingSupported(options, m_name);

int idx;
if (_isAsciiEqualityOrdinal && CanUseAsciiOrdinalForOptions(options))
{
idx = (options & CompareOptions.IgnoreCase) != 0 ?
return (options & CompareOptions.IgnoreCase) != 0 ?
IndexOfOrdinalIgnoreCaseHelper(source, target, options, matchLengthPtr, fromBeginning) :
IndexOfOrdinalHelper(source, target, options, matchLengthPtr, fromBeginning);
}
else
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
fixed (char* pSource = &MemoryMarshal.GetReference(source))
fixed (char* pTarget = &MemoryMarshal.GetReference(target))
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
{
fixed (char* pSource = &MemoryMarshal.GetReference(source))
fixed (char* pTarget = &MemoryMarshal.GetReference(target))
{
idx = Interop.JsGlobalization.IndexOf(m_name, pTarget, target.Length, pSource, source.Length, options, fromBeginning, out int exception, out object ex_result);
if (exception != 0)
throw new Exception((string)ex_result);
}
nint exceptionPtr = Interop.JsGlobalization.IndexOf(pCultureName, cultureNameSpan.Length, pTarget, target.Length, pSource, source.Length, options, fromBeginning, out int idx);
Helper.MarshalAndThrowIfException(exceptionPtr);
return idx;
}

return idx;
}

// there are chars that are ignored by ICU hashing algorithm but not ignored by invariant hashing
Expand Down
Loading
Loading