diff --git a/src/mono/browser/runtime/pinvoke.c b/src/mono/browser/runtime/pinvoke.c index 973bd26171985f..c82033cd2d85c1 100644 --- a/src/mono/browser/runtime/pinvoke.c +++ b/src/mono/browser/runtime/pinvoke.c @@ -50,10 +50,7 @@ wasm_dl_is_pinvoke_table (void *handle) static int export_compare_key (const void *k1, const void *k2) { - const char *key1 = ((UnmanagedExport*)k1)->key; - const char *key2 = ((UnmanagedExport*)k2)->key; - - return strcmp (key1, key2); + return strcmp (((UnmanagedExport*)k1)->key, ((UnmanagedExport*)k2)->key); } static int @@ -75,7 +72,7 @@ void* wasm_dl_get_native_to_interp (uint32_t token, const char *key, void *extra_arg) { #ifdef GEN_PINVOKE - UnmanagedExport needle = { token, key, NULL }; + UnmanagedExport needle = { key, token, NULL }; int count = (sizeof (wasm_native_to_interp_table) / sizeof (UnmanagedExport)); // comparison must match the one used in the PInvokeTableGenerator to ensure the same order diff --git a/src/mono/browser/runtime/pinvoke.h b/src/mono/browser/runtime/pinvoke.h index dd3ea7d480fade..a2d32dfa2c9c52 100644 --- a/src/mono/browser/runtime/pinvoke.h +++ b/src/mono/browser/runtime/pinvoke.h @@ -15,8 +15,8 @@ typedef struct { } PinvokeTable; typedef struct { - uint32_t token; const char *key; + uint32_t token; void *func; } UnmanagedExport; diff --git a/src/tasks/WasmAppBuilder/JoinedString.cs b/src/tasks/WasmAppBuilder/JoinedString.cs index 5efd37a76f65b2..871870ca23a010 100644 --- a/src/tasks/WasmAppBuilder/JoinedString.cs +++ b/src/tasks/WasmAppBuilder/JoinedString.cs @@ -96,16 +96,21 @@ public override string ToString() internal sealed class JoinedStringStreamWriter : StreamWriter { - private string CompilerNewLine = Environment.NewLine; + + // since we are intentionally using multi-line string writes, + // we want to capture the compile-time new line + private string CompileTimeNewLine = """ + + """; public JoinedStringStreamWriter(Stream stream) : base(stream) { - NewLine = CompilerNewLine; + NewLine = CompileTimeNewLine; } public JoinedStringStreamWriter(string path, bool append) : base(path, append) { - NewLine = CompilerNewLine; + NewLine = CompileTimeNewLine; } #if NET8_0_OR_GREATER diff --git a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs index b56a013e63f4de..6141263987adcd 100644 --- a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs +++ b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs @@ -136,20 +136,22 @@ private void EmitPInvokeTable(StreamWriter w, SortedDictionary m var moduleImports = new Dictionary>(); foreach (var module in modules.Keys) { - var assemblies_pinvokes = pinvokes + static string ListRefs(IGrouping l) => + string.Join(", ", l.Select(c => c.Method.DeclaringType!.Module!.Assembly!.GetName()!.Name!).Distinct().OrderBy(n => n)); + + var imports = pinvokes .Where(l => l.Module == module && !l.Skip) .OrderBy(l => l.EntryPoint, StringComparer.Ordinal) .GroupBy(d => d.EntryPoint, StringComparer.Ordinal) - .Select(l => $"{{\"{EscapeLiteral(l.Key)}\", {CEntryPoint(l.First())}}}, " - + "// " + string.Join(", ", l.Select(c => c.Method.DeclaringType!.Module!.Assembly!.GetName()!.Name!).Distinct().OrderBy(n => n)) + w.NewLine + " ") + .Select(l => $"{{\"{EscapeLiteral(l.Key)}\", {CEntryPoint(l.First())}}}, // {ListRefs(l)}{w.NewLine} ") .ToList(); - moduleImports[module] = assemblies_pinvokes; + moduleImports[module] = imports; w.Write( $$""" static PinvokeImport {{_fixupSymbolName(module)}}_imports [] = { - {{string.Join("", assemblies_pinvokes)}}{NULL, NULL} + {{string.Join("", imports)}}{NULL, NULL} }; """); @@ -388,7 +390,7 @@ private void EmitNativeToInterp(StreamWriter w, List callbacks) $$""" static UnmanagedExport wasm_native_to_interp_table[] = { - {{callbacks.Join($",{w.NewLine} ", cb => $"{{{cb.Token}, \"{EscapeLiteral(cb.Key)}\", {cb.EntrySymbol}}}")}} + {{callbacks.Join($",{w.NewLine} ", cb => $"{{\"{EscapeLiteral(cb.Key)}\", {cb.Token}, {cb.EntrySymbol}}}")}} }; """);