Skip to content

Commit bda0906

Browse files
committed
Feedback
1 parent 9a98931 commit bda0906

File tree

6 files changed

+53
-78
lines changed

6 files changed

+53
-78
lines changed

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ private unsafe bool JSLoadCalendarDataFromBrowser(string localeName, CalendarId
1616
{
1717
char* buffer = stackalloc char[CALENDAR_INFO_BUFFER_LEN];
1818
nint exceptionPtr = Interop.JsGlobalization.GetCalendarInfo(localeName, calendarId, buffer, CALENDAR_INFO_BUFFER_LEN, out int resultLength);
19-
if (exceptionPtr != IntPtr.Zero) // JSFunctionBinding.cs
20-
{
21-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
22-
Marshal.FreeHGlobal(exceptionPtr);
23-
throw new Exception(message);
24-
}
19+
Helper.MarshalAndThrowIfException(exceptionPtr);
2520
string result = new string(buffer, 0, resultLength);
2621
string[] subresults = result.Split("##");
2722
if (subresults.Length < 14)

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

+2-12
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,7 @@ private unsafe int IndexOfOrdinalIgnoreCaseHelper(ReadOnlySpan<char> source, Rea
199199
if (GlobalizationMode.Hybrid)
200200
{
201201
nint exceptionPtr = Interop.JsGlobalization.IndexOf(m_name, b, target.Length, a, source.Length, options, fromBeginning, out int result);
202-
if (exceptionPtr != IntPtr.Zero)
203-
{
204-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
205-
Marshal.FreeHGlobal(exceptionPtr);
206-
throw new Exception(message);
207-
}
202+
Helper.MarshalAndThrowIfException(exceptionPtr);
208203
return result;
209204
}
210205
#elif TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS
@@ -305,12 +300,7 @@ private unsafe int IndexOfOrdinalHelper(ReadOnlySpan<char> source, ReadOnlySpan<
305300
if (GlobalizationMode.Hybrid)
306301
{
307302
nint exceptionPtr = Interop.JsGlobalization.IndexOf(m_name, b, target.Length, a, source.Length, options, fromBeginning, out int result);
308-
if (exceptionPtr != IntPtr.Zero)
309-
{
310-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
311-
Marshal.FreeHGlobal(exceptionPtr);
312-
throw new Exception(message);
313-
}
303+
Helper.MarshalAndThrowIfException(exceptionPtr);
314304
return result;
315305
}
316306
#elif TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS

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

+4-24
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ private unsafe int JsCompareString(ReadOnlySpan<char> string1, ReadOnlySpan<char
5454
fixed (char* pString2 = &MemoryMarshal.GetReference(string2))
5555
{
5656
nint exceptionPtr = Interop.JsGlobalization.CompareString(cultureName, pString1, string1.Length, pString2, string2.Length, options, out int cmpResult);
57-
if (exceptionPtr != IntPtr.Zero)
58-
{
59-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
60-
Marshal.FreeHGlobal(exceptionPtr);
61-
throw new Exception(message);
62-
}
57+
Helper.MarshalAndThrowIfException(exceptionPtr);
6358
return cmpResult;
6459
}
6560
}
@@ -75,12 +70,7 @@ private unsafe bool JsStartsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> p
7570
fixed (char* pPrefix = &MemoryMarshal.GetReference(prefix))
7671
{
7772
nint exceptionPtr = Interop.JsGlobalization.StartsWith(cultureName, pSource, source.Length, pPrefix, prefix.Length, options, out bool result);
78-
if (exceptionPtr != IntPtr.Zero)
79-
{
80-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
81-
Marshal.FreeHGlobal(exceptionPtr);
82-
throw new Exception(message);
83-
}
73+
Helper.MarshalAndThrowIfException(exceptionPtr);
8474
return result;
8575
}
8676
}
@@ -96,12 +86,7 @@ private unsafe bool JsEndsWith(ReadOnlySpan<char> source, ReadOnlySpan<char> pre
9686
fixed (char* pPrefix = &MemoryMarshal.GetReference(prefix))
9787
{
9888
nint exceptionPtr = Interop.JsGlobalization.EndsWith(cultureName, pSource, source.Length, pPrefix, prefix.Length, options, out bool result);
99-
if (exceptionPtr != IntPtr.Zero)
100-
{
101-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
102-
Marshal.FreeHGlobal(exceptionPtr);
103-
throw new Exception(message);
104-
}
89+
Helper.MarshalAndThrowIfException(exceptionPtr);
10590
return result;
10691
}
10792
}
@@ -123,12 +108,7 @@ private unsafe int JsIndexOfCore(ReadOnlySpan<char> source, ReadOnlySpan<char> t
123108
fixed (char* pTarget = &MemoryMarshal.GetReference(target))
124109
{
125110
nint exceptionPtr = Interop.JsGlobalization.IndexOf(m_name, pTarget, target.Length, pSource, source.Length, options, fromBeginning, out int idx);
126-
if (exceptionPtr != IntPtr.Zero)
127-
{
128-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
129-
Marshal.FreeHGlobal(exceptionPtr);
130-
throw new Exception(message);
131-
}
111+
Helper.MarshalAndThrowIfException(exceptionPtr);
132112
return idx;
133113
}
134114
}

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

+33-19
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ private unsafe (string, string) JSGetLocaleInfo(string cultureName, string local
4444
{
4545
char* buffer = stackalloc char[LOCALE_INFO_BUFFER_LEN];
4646
nint exceptionPtr = Interop.JsGlobalization.GetLocaleInfo(cultureName, localeName, buffer, LOCALE_INFO_BUFFER_LEN, out int resultLength);
47-
if (exceptionPtr != IntPtr.Zero)
48-
{
49-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
50-
Marshal.FreeHGlobal(exceptionPtr);
51-
throw new Exception(message);
52-
}
47+
Helper.MarshalAndThrowIfException(exceptionPtr);
5348
string result = new string(buffer, 0, resultLength);
5449
string[] subresults = result.Split("##");
5550
if (subresults.Length == 0)
@@ -71,12 +66,7 @@ private static unsafe CultureData JSLoadCultureInfoFromBrowser(string localeName
7166
{
7267
char* buffer = stackalloc char[CULTURE_INFO_BUFFER_LEN];
7368
nint exceptionPtr = Interop.JsGlobalization.GetCultureInfo(localeName, buffer, CULTURE_INFO_BUFFER_LEN, out int resultLength);
74-
if (exceptionPtr != IntPtr.Zero)
75-
{
76-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
77-
Marshal.FreeHGlobal(exceptionPtr);
78-
throw new Exception(message);
79-
}
69+
Helper.MarshalAndThrowIfException(exceptionPtr);
8070
string result = new string(buffer, 0, resultLength);
8171
string[] subresults = result.Split("##");
8272
if (subresults.Length < 4)
@@ -93,11 +83,13 @@ private static unsafe int GetFirstDayOfWeek(string localeName)
9383
nint exceptionPtr = Interop.JsGlobalization.GetFirstDayOfWeek(localeName, out int result);
9484
if (exceptionPtr != IntPtr.Zero)
9585
{
86+
int success = Helper.MarshalAndThrowIfException(
87+
exceptionPtr,
88+
failOnlyDebug: true,
89+
failureMessage: $"[CultureData.GetFirstDayOfWeek()] failed with");
9690
// Failed, just use 0
97-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
98-
Marshal.FreeHGlobal(exceptionPtr);
99-
Debug.Fail($"[CultureData.GetFirstDayOfWeek()] failed with {message}");
100-
return 0;
91+
if (success == -1)
92+
return 0;
10193
}
10294
return result;
10395
}
@@ -107,13 +99,35 @@ private static unsafe int GetFirstWeekOfYear(string localeName)
10799
nint exceptionPtr = Interop.JsGlobalization.GetFirstWeekOfYear(localeName, out int result);
108100
if (exceptionPtr != IntPtr.Zero)
109101
{
102+
int success = Helper.MarshalAndThrowIfException(
103+
exceptionPtr,
104+
failOnlyDebug: true,
105+
failureMessage: $"[CultureData.GetFirstWeekOfYear()] failed with");
110106
// Failed, just use 0
107+
if (success == -1)
108+
return 0;
109+
}
110+
return result;
111+
}
112+
}
113+
114+
internal static class Helper
115+
{
116+
internal static int MarshalAndThrowIfException(nint exceptionPtr, bool failOnlyDebug = false, string failureMessage = "")
117+
{
118+
if (exceptionPtr != IntPtr.Zero)
119+
{
111120
string message = Marshal.PtrToStringUni(exceptionPtr)!;
112121
Marshal.FreeHGlobal(exceptionPtr);
113-
Debug.Fail($"[CultureData.GetFirstWeekOfYear()] failed with {message}");
114-
return 0;
122+
if (failOnlyDebug)
123+
{
124+
Debug.Fail($"{failureMessage} {message}");
125+
return -1;
126+
}
127+
throw new Exception(message);
115128
}
116-
return result;
129+
return 0;
117130
}
131+
118132
}
119133
}

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ internal unsafe void JsChangeCase(char* src, int srcLen, char* dstBuffer, int ds
1717
nint exceptionPtr = HasEmptyCultureName ?
1818
Interop.JsGlobalization.ChangeCaseInvariant(src, srcLen, dstBuffer, dstBufferCapacity, toUpper) :
1919
Interop.JsGlobalization.ChangeCase(_cultureName, src, srcLen, dstBuffer, dstBufferCapacity, toUpper);
20-
if (exceptionPtr != IntPtr.Zero)
21-
{
22-
string message = Marshal.PtrToStringUni(exceptionPtr)!;
23-
Marshal.FreeHGlobal(exceptionPtr);
24-
throw new Exception(message);
25-
}
20+
Helper.MarshalAndThrowIfException(exceptionPtr);
2621
}
2722
}
2823
}

src/mono/browser/runtime/corebindings.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdint.h>
88
#include <assert.h>
99
#include <sys/types.h>
10+
#include <uchar.h>
1011

1112
#include <mono/metadata/appdomain.h>
1213
#include <mono/metadata/class.h>
@@ -58,17 +59,17 @@ extern void mono_wasm_invoke_jsimport_ST (int function_handle, void *args);
5859
#endif /* DISABLE_THREADS */
5960

6061
// HybridGlobalization
61-
extern void* mono_wasm_change_case_invariant (const uint16_t* src, int32_t srcLength, uint16_t* dst, int32_t dstLength, mono_bool bToUpper);
62-
extern void* mono_wasm_change_case (MonoString **culture, const uint16_t* src, int32_t srcLength, uint16_t* dst, int32_t dstLength, mono_bool bToUpper);
63-
extern void* mono_wasm_compare_string (MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, int *resultPtr);
64-
extern void* mono_wasm_starts_with (MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, mono_bool *resultPtr);
65-
extern void* mono_wasm_ends_with (MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, mono_bool *resultPtr);
66-
extern void* mono_wasm_index_of (MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, mono_bool fromBeginning, int *resultPtr);
67-
extern void* mono_wasm_get_calendar_info (MonoString **culture, int32_t calendarId, const uint16_t* result, int32_t resultMaxLength, int *resultLength);
68-
extern void* mono_wasm_get_locale_info (MonoString **locale, MonoString **culture, const uint16_t* result, int32_t resultMaxLength, int *resultLength);
69-
extern void* mono_wasm_get_culture_info (MonoString **culture, const uint16_t* result, int32_t resultMaxLength, int *resultLength);
70-
extern void* mono_wasm_get_first_day_of_week (MonoString **culture, int *resultPtr);
71-
extern void* mono_wasm_get_first_week_of_year (MonoString **culture, int *resultPtr);
62+
extern char16_t* mono_wasm_change_case_invariant (const uint16_t* src, int32_t srcLength, uint16_t* dst, int32_t dstLength, mono_bool bToUpper);
63+
extern char16_t* mono_wasm_change_case (MonoString **culture, const uint16_t* src, int32_t srcLength, uint16_t* dst, int32_t dstLength, mono_bool bToUpper);
64+
extern char16_t* mono_wasm_compare_string (MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, int *resultPtr);
65+
extern char16_t* mono_wasm_starts_with (MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, mono_bool *resultPtr);
66+
extern char16_t* mono_wasm_ends_with (MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, mono_bool *resultPtr);
67+
extern char16_t* mono_wasm_index_of (MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, mono_bool fromBeginning, int *resultPtr);
68+
extern char16_t* mono_wasm_get_calendar_info (MonoString **culture, int32_t calendarId, const uint16_t* result, int32_t resultMaxLength, int *resultLength);
69+
extern char16_t* mono_wasm_get_locale_info (MonoString **locale, MonoString **culture, const uint16_t* result, int32_t resultMaxLength, int *resultLength);
70+
extern char16_t* mono_wasm_get_culture_info (MonoString **culture, const uint16_t* result, int32_t resultMaxLength, int *resultLength);
71+
extern char16_t* mono_wasm_get_first_day_of_week (MonoString **culture, int *resultPtr);
72+
extern char16_t* mono_wasm_get_first_week_of_year (MonoString **culture, int *resultPtr);
7273

7374
void bindings_initialize_internals (void)
7475
{

0 commit comments

Comments
 (0)