Skip to content

Commit

Permalink
bsearch for tables too
Browse files Browse the repository at this point in the history
  • Loading branch information
lewing committed Sep 8, 2024
1 parent a179289 commit 868fdb3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
13 changes: 8 additions & 5 deletions src/mono/browser/runtime/pinvoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ mono_wasm_pinvoke_vararg_stub (void)
/* This is just a stub used to mark vararg pinvokes */
}

int
table_compare_name (const void *t1, const void *t2)
{
return strcmp (((PinvokeTable*)t1)->name, ((PinvokeTable*)t2)->name);
}

void*
wasm_dl_lookup_pinvoke_table (const char *name)
{
for (int i = 0; i < sizeof (pinvoke_tables) / sizeof (PinvokeTable); ++i) {
if (!strcmp (name, pinvoke_tables [i].name))
return &pinvoke_tables [i];
}
return NULL;
PinvokeImport needle = { name, NULL };
return bsearch (&needle, pinvoke_tables, (sizeof (pinvoke_tables) / sizeof (PinvokeTable)), sizeof (PinvokeTable), table_compare_name);
}

int
Expand Down
11 changes: 2 additions & 9 deletions src/mono/browser/runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ wasm_dl_load (const char *name, int flags, char **err, void *user_data)
}

int
import_compare_key (const void *k1, const void *k2)
import_compare_name (const void *k1, const void *k2)
{
const PinvokeImport *e1 = (const PinvokeImport*)k1;
const PinvokeImport *e2 = (const PinvokeImport*)k2;
Expand All @@ -282,19 +282,12 @@ wasm_dl_symbol (void *handle, const char *name, char **err, void *user_data)
}
#endif
PinvokeTable* index = (PinvokeTable*)handle;
int i = 0;

assert(wasm_dl_is_pinvoke_table (handle));
for (i = 0; index->imports[i].name != NULL; ++i) {}
assert (i == index->count);

PinvokeImport key = { name, NULL };
PinvokeImport* result = (PinvokeImport *)bsearch(&key, index->imports, index->count, sizeof(PinvokeImport), import_compare_key);
PinvokeImport* result = (PinvokeImport *)bsearch(&key, index->imports, index->count, sizeof(PinvokeImport), import_compare_name);
if (!result) {
// *err = g_strdup_printf ("Symbol not found: %s", name);
return NULL;
}

return result->func;
}

Expand Down
9 changes: 4 additions & 5 deletions src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void ScanAssembly(Assembly asm)

public IEnumerable<string> Generate(string[] pinvokeModules, string outputPath)
{
var modules = new Dictionary<string, string>();
var modules = new SortedDictionary<string, string>(StringComparer.Ordinal);
foreach (var module in pinvokeModules)
modules[module] = module;

Expand All @@ -62,7 +62,7 @@ public IEnumerable<string> Generate(string[] pinvokeModules, string outputPath)
return signatures;
}

private void EmitPInvokeTable(StreamWriter w, Dictionary<string, string> modules, List<PInvoke> pinvokes)
private void EmitPInvokeTable(StreamWriter w, SortedDictionary<string, string> modules, List<PInvoke> pinvokes)
{

foreach (var pinvoke in pinvokes)
Expand Down Expand Up @@ -99,8 +99,8 @@ private void EmitPInvokeTable(StreamWriter w, Dictionary<string, string> modules

var pinvokesGroupedByEntryPoint = pinvokes
.Where(l => modules.ContainsKey(l.Module))
.OrderBy(l => l.EntryPoint)
.GroupBy(CEntryPoint);
.OrderBy(l => l.EntryPoint, StringComparer.Ordinal)
.GroupBy(CEntryPoint, StringComparer.Ordinal);
var comparer = new PInvokeComparer();
foreach (IGrouping<string, PInvoke> group in pinvokesGroupedByEntryPoint)
{
Expand Down Expand Up @@ -348,7 +348,6 @@ private void EmitNativeToInterp(StreamWriter w, List<PInvokeCallback> callbacks)
callbackNames.Add(cb.EntrySymbol);
if (keys.Contains(cb.Key))
{

Error($"Two callbacks with the same Name and number of arguments '{cb.Key}' are not supported.");
}
keys.Add(cb.Key);
Expand Down

0 comments on commit 868fdb3

Please sign in to comment.