Skip to content

Commit

Permalink
Small clean-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
lewing committed Sep 8, 2024
1 parent 868fdb3 commit 96a6abe
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
7 changes: 2 additions & 5 deletions src/mono/browser/runtime/pinvoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/pinvoke.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ typedef struct {
} PinvokeTable;

typedef struct {
uint32_t token;
const char *key;
uint32_t token;
void *func;
} UnmanagedExport;

Expand Down
11 changes: 8 additions & 3 deletions src/tasks/WasmAppBuilder/JoinedString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,22 @@ private void EmitPInvokeTable(StreamWriter w, SortedDictionary<string, string> m
var moduleImports = new Dictionary<string, List<string>>();
foreach (var module in modules.Keys)
{
var assemblies_pinvokes = pinvokes
static string ListRefs(IGrouping<string, PInvoke> 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}
};

""");
Expand Down Expand Up @@ -388,7 +390,7 @@ private void EmitNativeToInterp(StreamWriter w, List<PInvokeCallback> 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}}}")}}
};

""");
Expand Down

0 comments on commit 96a6abe

Please sign in to comment.