Skip to content

Commit 9de2e08

Browse files
ilonatommymatouskozak
authored andcommitted
[browser][hybrid] drop wrap_error_root, wrap_no_error_root (dotnet#101390)
* Calendar - done * Culture info - done. * Missing change to the previous commit. * Change case - done. * issing change to previous commit * Collation - done. * Locales - done. * Feedback * remove MonoString that is not thread-safe * Preserve stack from JS correctly.
1 parent 40966b4 commit 9de2e08

16 files changed

+297
-343
lines changed

src/libraries/Common/src/Interop/Browser/Interop.Calendar.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ internal static partial class Interop
99
internal static unsafe partial class JsGlobalization
1010
{
1111
[MethodImplAttribute(MethodImplOptions.InternalCall)]
12-
internal static extern unsafe int GetCalendarInfo(in string culture, CalendarId calendarId, char* buffer, int bufferLength, out int exceptionalResult, out object result);
12+
internal static extern unsafe nint GetCalendarInfo(char* culture, int cultureLength, CalendarId calendarId, char* buffer, int bufferMaxLength, out int bufferLength);
1313
}
1414
}

src/libraries/Common/src/Interop/Browser/Interop.CompareInfo.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ internal static partial class Interop
88
internal static unsafe partial class JsGlobalization
99
{
1010
[MethodImplAttribute(MethodImplOptions.InternalCall)]
11-
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);
11+
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);
1212

1313
[MethodImplAttribute(MethodImplOptions.InternalCall)]
14-
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);
14+
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);
1515

1616
[MethodImplAttribute(MethodImplOptions.InternalCall)]
17-
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);
17+
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);
1818

1919
[MethodImplAttribute(MethodImplOptions.InternalCall)]
20-
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);
20+
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);
2121
}
2222
}

src/libraries/Common/src/Interop/Browser/Interop.Locale.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ internal static partial class Interop
88
internal static unsafe partial class JsGlobalization
99
{
1010
[MethodImplAttribute(MethodImplOptions.InternalCall)]
11-
internal static extern unsafe int GetCultureInfo(in string culture, char* buffer, int bufferLength, out int exceptionalResult, out object result);
11+
internal static extern unsafe nint GetCultureInfo(char* culture, int cultureLength, char* buffer, int bufferMaxLength, out int resultLength);
1212
[MethodImplAttribute(MethodImplOptions.InternalCall)]
13-
internal static extern unsafe int GetFirstDayOfWeek(in string culture, out int exceptionalResult, out object result);
13+
internal static extern unsafe nint GetFirstDayOfWeek(char* culture, int cultureLength, out int resultPtr);
1414
[MethodImplAttribute(MethodImplOptions.InternalCall)]
15-
internal static extern unsafe int GetFirstWeekOfYear(in string culture, out int exceptionalResult, out object result);
15+
internal static extern unsafe nint GetFirstWeekOfYear(char* culture, int cultureLength, out int resultPtr);
1616
[MethodImplAttribute(MethodImplOptions.InternalCall)]
17-
internal static extern unsafe int GetLocaleInfo(in string locale, in string culture, char* buffer, int bufferLength, out int exceptionalResult, out object result);
17+
internal static extern unsafe nint GetLocaleInfo(char* locale, int localeLength, char* culture, int cultureLength, char* buffer, int bufferLength, out int resultLength);
1818
}
1919
}

src/libraries/Common/src/Interop/Browser/Interop.TextInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ internal static partial class Interop
88
internal static unsafe partial class JsGlobalization
99
{
1010
[MethodImplAttribute(MethodImplOptions.InternalCall)]
11-
internal static extern unsafe void ChangeCaseInvariant(char* src, int srcLen, char* dstBuffer, int dstBufferCapacity, bool bToUpper, out int exceptionalResult, out object result);
11+
internal static extern unsafe nint ChangeCaseInvariant(char* src, int srcLen, char* dstBuffer, int dstBufferCapacity, bool bToUpper);
1212
[MethodImplAttribute(MethodImplOptions.InternalCall)]
13-
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);
13+
internal static extern unsafe nint ChangeCase(char* culture, int cultureLen, char* src, int srcLen, char* dstBuffer, int dstBufferCapacity, bool bToUpper);
1414
}
1515
}

src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Browser.cs

+27-26
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,33 @@ internal sealed partial class CalendarData
1414
private const int CALENDAR_INFO_BUFFER_LEN = 1000;
1515
private unsafe bool JSLoadCalendarDataFromBrowser(string localeName, CalendarId calendarId)
1616
{
17-
char* buffer = stackalloc char[CALENDAR_INFO_BUFFER_LEN];
18-
int exception;
19-
object exResult;
20-
int resultLength = Interop.JsGlobalization.GetCalendarInfo(localeName, calendarId, buffer, CALENDAR_INFO_BUFFER_LEN, out exception, out exResult);
21-
if (exception != 0)
22-
throw new Exception((string)exResult);
23-
string result = new string(buffer, 0, resultLength);
24-
string[] subresults = result.Split("##");
25-
if (subresults.Length < 14)
26-
throw new Exception("CalendarInfo recieved from the Browser is in icorrect format.");
27-
// JS always has one result per locale, so even arrays are initialized with one element
28-
this.sNativeName = string.IsNullOrEmpty(subresults[0]) ? ((CalendarId)calendarId).ToString() : subresults[0]; // this is EnglishName, not NativeName but it's the best we can do
29-
this.saYearMonths = new string[] { subresults[1] };
30-
this.sMonthDay = subresults[2];
31-
this.saLongDates = new string[] { subresults[3] };
32-
this.saShortDates = new string[] { subresults[4] };
33-
this.saEraNames = new string[] { subresults[5] };
34-
this.saAbbrevEraNames = new string[] { subresults[6] };
35-
this.saDayNames = subresults[7].Split("||");
36-
this.saAbbrevDayNames = subresults[8].Split("||");
37-
this.saSuperShortDayNames = subresults[9].Split("||");
38-
this.saMonthNames = ResizeMonthsArray(subresults[10].Split("||"));
39-
this.saAbbrevMonthNames = ResizeMonthsArray(subresults[11].Split("||"));
40-
this.saMonthGenitiveNames = ResizeMonthsArray(subresults[12].Split("||"));
41-
this.saAbbrevMonthGenitiveNames = ResizeMonthsArray(subresults[13].Split("||"));
42-
return true;
17+
ReadOnlySpan<char> localeNameSpan = localeName.AsSpan();
18+
fixed (char* pLocaleName = &MemoryMarshal.GetReference(localeNameSpan))
19+
{
20+
char* buffer = stackalloc char[CALENDAR_INFO_BUFFER_LEN];
21+
nint exceptionPtr = Interop.JsGlobalization.GetCalendarInfo(pLocaleName, localeNameSpan.Length, calendarId, buffer, CALENDAR_INFO_BUFFER_LEN, out int resultLength);
22+
Helper.MarshalAndThrowIfException(exceptionPtr);
23+
string result = new string(buffer, 0, resultLength);
24+
string[] subresults = result.Split("##");
25+
if (subresults.Length < 14)
26+
throw new Exception("CalendarInfo recieved from the Browser is in icorrect format.");
27+
// JS always has one result per locale, so even arrays are initialized with one element
28+
this.sNativeName = string.IsNullOrEmpty(subresults[0]) ? ((CalendarId)calendarId).ToString() : subresults[0]; // this is EnglishName, not NativeName but it's the best we can do
29+
this.saYearMonths = new string[] { subresults[1] };
30+
this.sMonthDay = subresults[2];
31+
this.saLongDates = new string[] { subresults[3] };
32+
this.saShortDates = new string[] { subresults[4] };
33+
this.saEraNames = new string[] { subresults[5] };
34+
this.saAbbrevEraNames = new string[] { subresults[6] };
35+
this.saDayNames = subresults[7].Split("||");
36+
this.saAbbrevDayNames = subresults[8].Split("||");
37+
this.saSuperShortDayNames = subresults[9].Split("||");
38+
this.saMonthNames = ResizeMonthsArray(subresults[10].Split("||"));
39+
this.saAbbrevMonthNames = ResizeMonthsArray(subresults[11].Split("||"));
40+
this.saMonthGenitiveNames = ResizeMonthsArray(subresults[12].Split("||"));
41+
this.saAbbrevMonthGenitiveNames = ResizeMonthsArray(subresults[13].Split("||"));
42+
return true;
43+
}
4344

4445
static string[] ResizeMonthsArray(string[] months)
4546
{

src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs

+14-8
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,13 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan<char> source, Rea
198198
#if TARGET_BROWSER
199199
if (GlobalizationMode.Hybrid)
200200
{
201-
int result = Interop.JsGlobalization.IndexOf(m_name, b, target.Length, a, source.Length, options, fromBeginning, out int exception, out object ex_result);
202-
if (exception != 0)
203-
throw new Exception((string)ex_result);
204-
return result;
201+
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
202+
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
203+
{
204+
nint exceptionPtr = Interop.JsGlobalization.IndexOf(pCultureName, cultureNameSpan.Length, b, target.Length, a, source.Length, options, fromBeginning, out int result);
205+
Helper.MarshalAndThrowIfException(exceptionPtr);
206+
return result;
207+
}
205208
}
206209
#elif TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
207210
if (GlobalizationMode.Hybrid)
@@ -300,10 +303,13 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan<char> source, ReadOnlySpan<
300303
#if TARGET_BROWSER
301304
if (GlobalizationMode.Hybrid)
302305
{
303-
int result = Interop.JsGlobalization.IndexOf(m_name, b, target.Length, a, source.Length, options, fromBeginning, out int exception, out object ex_result);
304-
if (exception != 0)
305-
throw new Exception((string)ex_result);
306-
return result;
306+
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
307+
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
308+
{
309+
nint exceptionPtr = Interop.JsGlobalization.IndexOf(pCultureName, cultureNameSpan.Length, b, target.Length, a, source.Length, options, fromBeginning, out int result);
310+
Helper.MarshalAndThrowIfException(exceptionPtr);
311+
return result;
312+
}
307313
}
308314
#elif TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
309315
if (GlobalizationMode.Hybrid)

src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.WebAssembly.cs

+27-39
Original file line numberDiff line numberDiff line change
@@ -47,86 +47,74 @@ private static void AssertIndexingSupported(CompareOptions options, string cultu
4747
private unsafe int JsCompareString(ReadOnlySpan<char> string1, ReadOnlySpan<char> string2, CompareOptions options)
4848
{
4949
AssertHybridOnWasm(options);
50-
string cultureName = m_name;
51-
AssertComparisonSupported(options, cultureName);
50+
AssertComparisonSupported(options, m_name);
5251

53-
int cmpResult;
52+
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
5453
fixed (char* pString1 = &MemoryMarshal.GetReference(string1))
5554
fixed (char* pString2 = &MemoryMarshal.GetReference(string2))
55+
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
5656
{
57-
cmpResult = Interop.JsGlobalization.CompareString(cultureName, pString1, string1.Length, pString2, string2.Length, options, out int exception, out object ex_result);
58-
if (exception != 0)
59-
throw new Exception((string)ex_result);
57+
nint exceptionPtr = Interop.JsGlobalization.CompareString(pCultureName, cultureNameSpan.Length, pString1, string1.Length, pString2, string2.Length, options, out int cmpResult);
58+
Helper.MarshalAndThrowIfException(exceptionPtr);
59+
return cmpResult;
6060
}
61-
62-
return cmpResult;
6361
}
6462

6563
private unsafe bool JsStartsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options)
6664
{
6765
AssertHybridOnWasm(options);
6866
Debug.Assert(!prefix.IsEmpty);
69-
string cultureName = m_name;
70-
AssertIndexingSupported(options, cultureName);
67+
AssertIndexingSupported(options, m_name);
7168

72-
bool result;
69+
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
7370
fixed (char* pSource = &MemoryMarshal.GetReference(source))
7471
fixed (char* pPrefix = &MemoryMarshal.GetReference(prefix))
72+
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
7573
{
76-
result = Interop.JsGlobalization.StartsWith(cultureName, pSource, source.Length, pPrefix, prefix.Length, options, out int exception, out object ex_result);
77-
if (exception != 0)
78-
throw new Exception((string)ex_result);
74+
nint exceptionPtr = Interop.JsGlobalization.StartsWith(pCultureName, cultureNameSpan.Length, pSource, source.Length, pPrefix, prefix.Length, options, out bool result);
75+
Helper.MarshalAndThrowIfException(exceptionPtr);
76+
return result;
7977
}
80-
81-
82-
return result;
8378
}
8479

8580
private unsafe bool JsEndsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options)
8681
{
8782
AssertHybridOnWasm(options);
8883
Debug.Assert(!prefix.IsEmpty);
89-
string cultureName = m_name;
90-
AssertIndexingSupported(options, cultureName);
84+
AssertIndexingSupported(options, m_name);
9185

92-
bool result;
86+
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
9387
fixed (char* pSource = &MemoryMarshal.GetReference(source))
9488
fixed (char* pPrefix = &MemoryMarshal.GetReference(prefix))
89+
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
9590
{
96-
result = Interop.JsGlobalization.EndsWith(cultureName, pSource, source.Length, pPrefix, prefix.Length, options, out int exception, out object ex_result);
97-
if (exception != 0)
98-
throw new Exception((string)ex_result);
91+
nint exceptionPtr = Interop.JsGlobalization.EndsWith(pCultureName, cultureNameSpan.Length, pSource, source.Length, pPrefix, prefix.Length, options, out bool result);
92+
Helper.MarshalAndThrowIfException(exceptionPtr);
93+
return result;
9994
}
100-
101-
return result;
10295
}
10396

10497
private unsafe int JsIndexOfCore(ReadOnlySpan<char> source, ReadOnlySpan<char> target, CompareOptions options, int* matchLengthPtr, bool fromBeginning)
10598
{
10699
AssertHybridOnWasm(options);
107100
Debug.Assert(!target.IsEmpty);
108-
string cultureName = m_name;
109-
AssertIndexingSupported(options, cultureName);
101+
AssertIndexingSupported(options, m_name);
110102

111-
int idx;
112103
if (_isAsciiEqualityOrdinal && CanUseAsciiOrdinalForOptions(options))
113104
{
114-
idx = (options & CompareOptions.IgnoreCase) != 0 ?
105+
return (options & CompareOptions.IgnoreCase) != 0 ?
115106
IndexOfOrdinalIgnoreCaseHelper(source, target, options, matchLengthPtr, fromBeginning) :
116107
IndexOfOrdinalHelper(source, target, options, matchLengthPtr, fromBeginning);
117108
}
118-
else
109+
ReadOnlySpan<char> cultureNameSpan = m_name.AsSpan();
110+
fixed (char* pSource = &MemoryMarshal.GetReference(source))
111+
fixed (char* pTarget = &MemoryMarshal.GetReference(target))
112+
fixed (char* pCultureName = &MemoryMarshal.GetReference(cultureNameSpan))
119113
{
120-
fixed (char* pSource = &MemoryMarshal.GetReference(source))
121-
fixed (char* pTarget = &MemoryMarshal.GetReference(target))
122-
{
123-
idx = Interop.JsGlobalization.IndexOf(m_name, pTarget, target.Length, pSource, source.Length, options, fromBeginning, out int exception, out object ex_result);
124-
if (exception != 0)
125-
throw new Exception((string)ex_result);
126-
}
114+
nint exceptionPtr = Interop.JsGlobalization.IndexOf(pCultureName, cultureNameSpan.Length, pTarget, target.Length, pSource, source.Length, options, fromBeginning, out int idx);
115+
Helper.MarshalAndThrowIfException(exceptionPtr);
116+
return idx;
127117
}
128-
129-
return idx;
130118
}
131119

132120
// there are chars that are ignored by ICU hashing algorithm but not ignored by invariant hashing

0 commit comments

Comments
 (0)