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(in string culture, 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(in string culture, 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(in string culture, 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(in string culture, 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(in string culture, 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(in string culture, 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(in string culture, 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(in string culture, 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(in string locale, in string culture, 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(in string culture, char* src, int srcLen, char* dstBuffer, int dstBufferCapacity, bool bToUpper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ internal sealed partial class CalendarData
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);
nint exceptionPtr = Interop.JsGlobalization.GetCalendarInfo(localeName, calendarId, buffer, CALENDAR_INFO_BUFFER_LEN, out int resultLength);
if (exceptionPtr != IntPtr.Zero) // JSFunctionBinding.cs
{
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new Exception(message);
}
string result = new string(buffer, 0, resultLength);
string[] subresults = result.Split("##");
if (subresults.Length < 14)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +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);
nint exceptionPtr = Interop.JsGlobalization.IndexOf(m_name, b, target.Length, a, source.Length, options, fromBeginning, out int result);
if (exceptionPtr != IntPtr.Zero)
{
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new Exception(message);
}
return result;
}
#elif TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
Expand Down Expand Up @@ -300,9 +304,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);
nint exceptionPtr = Interop.JsGlobalization.IndexOf(m_name, b, target.Length, a, source.Length, options, fromBeginning, out int result);
if (exceptionPtr != IntPtr.Zero)
{
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new Exception(message);
}
return result;
}
#elif TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,18 @@ private unsafe int JsCompareString(ReadOnlySpan<char> string1, ReadOnlySpan<char
string cultureName = m_name;
AssertComparisonSupported(options, cultureName);

int cmpResult;
fixed (char* pString1 = &MemoryMarshal.GetReference(string1))
fixed (char* pString2 = &MemoryMarshal.GetReference(string2))
{
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(cultureName, pString1, string1.Length, pString2, string2.Length, options, out int cmpResult);
if (exceptionPtr != IntPtr.Zero)
ilonatommy marked this conversation as resolved.
Show resolved Hide resolved
{
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new Exception(message);
}
return cmpResult;
}

return cmpResult;
}

private unsafe bool JsStartsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options)
Expand All @@ -69,17 +71,18 @@ private unsafe bool JsStartsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> p
string cultureName = m_name;
AssertIndexingSupported(options, cultureName);

bool result;
fixed (char* pSource = &MemoryMarshal.GetReference(source))
fixed (char* pPrefix = &MemoryMarshal.GetReference(prefix))
{
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(cultureName, pSource, source.Length, pPrefix, prefix.Length, options, out bool result);
if (exceptionPtr != IntPtr.Zero)
{
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new Exception(message);
}
return result;
}


return result;
}

private unsafe bool JsEndsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options)
Expand All @@ -89,16 +92,18 @@ private unsafe bool JsEndsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> pre
string cultureName = m_name;
AssertIndexingSupported(options, cultureName);

bool result;
fixed (char* pSource = &MemoryMarshal.GetReference(source))
fixed (char* pPrefix = &MemoryMarshal.GetReference(prefix))
{
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(cultureName, pSource, source.Length, pPrefix, prefix.Length, options, out bool result);
if (exceptionPtr != IntPtr.Zero)
{
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new Exception(message);
}
return result;
}

return result;
}

private unsafe int JsIndexOfCore(ReadOnlySpan<char> source, ReadOnlySpan<char> target, CompareOptions options, int* matchLengthPtr, bool fromBeginning)
Expand All @@ -108,25 +113,24 @@ private unsafe int JsIndexOfCore(ReadOnlySpan<char> source, ReadOnlySpan<char> t
string cultureName = m_name;
AssertIndexingSupported(options, cultureName);

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
fixed (char* pSource = &MemoryMarshal.GetReference(source))
fixed (char* pTarget = &MemoryMarshal.GetReference(target))
{
fixed (char* pSource = &MemoryMarshal.GetReference(source))
fixed (char* pTarget = &MemoryMarshal.GetReference(target))
nint exceptionPtr = Interop.JsGlobalization.IndexOf(m_name, pTarget, target.Length, pSource, source.Length, options, fromBeginning, out int idx);
if (exceptionPtr != IntPtr.Zero)
{
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);
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new Exception(message);
}
return idx;
}

return idx;
}

// there are chars that are ignored by ICU hashing algorithm but not ignored by invariant hashing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace System.Globalization
{
Expand Down Expand Up @@ -42,9 +43,13 @@ private void JSInitLocaleInfo()
private unsafe (string, string) JSGetLocaleInfo(string cultureName, string localeName)
{
char* buffer = stackalloc char[LOCALE_INFO_BUFFER_LEN];
int resultLength = Interop.JsGlobalization.GetLocaleInfo(cultureName, localeName, buffer, LOCALE_INFO_BUFFER_LEN, out int exception, out object exResult);
if (exception != 0)
throw new Exception((string)exResult);
nint exceptionPtr = Interop.JsGlobalization.GetLocaleInfo(cultureName, localeName, buffer, LOCALE_INFO_BUFFER_LEN, out int resultLength);
if (exceptionPtr != IntPtr.Zero)
{
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new Exception(message);
}
string result = new string(buffer, 0, resultLength);
string[] subresults = result.Split("##");
if (subresults.Length == 0)
Expand All @@ -65,9 +70,13 @@ private string JSGetNativeDisplayName(string localeName, string cultureName)
private static unsafe CultureData JSLoadCultureInfoFromBrowser(string localeName, CultureData culture)
{
char* buffer = stackalloc char[CULTURE_INFO_BUFFER_LEN];
int resultLength = Interop.JsGlobalization.GetCultureInfo(localeName, buffer, CULTURE_INFO_BUFFER_LEN, out int exception, out object exResult);
if (exception != 0)
throw new Exception((string)exResult);
nint exceptionPtr = Interop.JsGlobalization.GetCultureInfo(localeName, buffer, CULTURE_INFO_BUFFER_LEN, out int resultLength);
if (exceptionPtr != IntPtr.Zero)
{
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new Exception(message);
}
string result = new string(buffer, 0, resultLength);
string[] subresults = result.Split("##");
if (subresults.Length < 4)
Expand All @@ -81,23 +90,27 @@ private static unsafe CultureData JSLoadCultureInfoFromBrowser(string localeName

private static unsafe int GetFirstDayOfWeek(string localeName)
{
int result = Interop.JsGlobalization.GetFirstDayOfWeek(localeName, out int exception, out object ex_result);
if (exception != 0)
nint exceptionPtr = Interop.JsGlobalization.GetFirstDayOfWeek(localeName, out int result);
if (exceptionPtr != IntPtr.Zero)
{
// Failed, just use 0
Debug.Fail($"[CultureData.GetFirstDayOfWeek()] failed with {ex_result}");
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
Debug.Fail($"[CultureData.GetFirstDayOfWeek()] failed with {message}");
return 0;
}
return result;
}

private static unsafe int GetFirstWeekOfYear(string localeName)
{
int result = Interop.JsGlobalization.GetFirstWeekOfYear(localeName, out int exception, out object ex_result);
if (exception != 0)
nint exceptionPtr = Interop.JsGlobalization.GetFirstWeekOfYear(localeName, out int result);
if (exceptionPtr != IntPtr.Zero)
{
// Failed, just use 0
Debug.Fail($"[CultureData.GetFirstDayOfWeek()] failed with {ex_result}");
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
Debug.Fail($"[CultureData.GetFirstWeekOfYear()] failed with {message}");
return 0;
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Runtime.InteropServices;

namespace System.Globalization
{
Expand All @@ -13,18 +14,15 @@ internal unsafe void JsChangeCase(char* src, int srcLen, char* dstBuffer, int ds
Debug.Assert(!GlobalizationMode.UseNls);
Debug.Assert(GlobalizationMode.Hybrid);

int exception;
object ex_result;
if (HasEmptyCultureName)
nint exceptionPtr = HasEmptyCultureName ?
Interop.JsGlobalization.ChangeCaseInvariant(src, srcLen, dstBuffer, dstBufferCapacity, toUpper) :
Interop.JsGlobalization.ChangeCase(_cultureName, src, srcLen, dstBuffer, dstBufferCapacity, toUpper);
if (exceptionPtr != IntPtr.Zero)
{
Interop.JsGlobalization.ChangeCaseInvariant(src, srcLen, dstBuffer, dstBufferCapacity, toUpper, out exception, out ex_result);
string message = Marshal.PtrToStringUni(exceptionPtr)!;
Marshal.FreeHGlobal(exceptionPtr);
throw new Exception(message);
}
else
{
Interop.JsGlobalization.ChangeCase(_cultureName, src, srcLen, dstBuffer, dstBufferCapacity, toUpper, out exception, out ex_result);
}
if (exception != 0)
throw new Exception((string)ex_result);
}
}
}
Loading
Loading