Skip to content

Commit b8fe280

Browse files
committed
bsearch for tables too
1 parent ee82d55 commit b8fe280

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

src/mono/browser/runtime/pinvoke.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ mono_wasm_pinvoke_vararg_stub (void)
2323
/* This is just a stub used to mark vararg pinvokes */
2424
}
2525

26+
int
27+
table_compare_name (const void *t1, const void *t2)
28+
{
29+
return strcmp (((PinvokeTable*)t1)->name, ((PinvokeTable*)t2)->name);
30+
}
31+
2632
void*
2733
wasm_dl_lookup_pinvoke_table (const char *name)
2834
{
29-
for (int i = 0; i < sizeof (pinvoke_tables) / sizeof (PinvokeTable); ++i) {
30-
if (!strcmp (name, pinvoke_tables [i].name))
31-
return &pinvoke_tables [i];
32-
}
33-
return NULL;
35+
PinvokeImport needle = { name, NULL };
36+
return bsearch (&needle, pinvoke_tables, (sizeof (pinvoke_tables) / sizeof (PinvokeTable)), sizeof (PinvokeTable), table_compare_name);
3437
}
3538

3639
int

src/mono/browser/runtime/runtime.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ wasm_dl_load (const char *name, int flags, char **err, void *user_data)
263263
}
264264

265265
int
266-
import_compare_key (const void *k1, const void *k2)
266+
import_compare_name (const void *k1, const void *k2)
267267
{
268268
const PinvokeImport *e1 = (const PinvokeImport*)k1;
269269
const PinvokeImport *e2 = (const PinvokeImport*)k2;
@@ -282,19 +282,12 @@ wasm_dl_symbol (void *handle, const char *name, char **err, void *user_data)
282282
}
283283
#endif
284284
PinvokeTable* index = (PinvokeTable*)handle;
285-
int i = 0;
286-
287-
assert(wasm_dl_is_pinvoke_table (handle));
288-
for (i = 0; index->imports[i].name != NULL; ++i) {}
289-
assert (i == index->count);
290-
291285
PinvokeImport key = { name, NULL };
292-
PinvokeImport* result = (PinvokeImport *)bsearch(&key, index->imports, index->count, sizeof(PinvokeImport), import_compare_key);
286+
PinvokeImport* result = (PinvokeImport *)bsearch(&key, index->imports, index->count, sizeof(PinvokeImport), import_compare_name);
293287
if (!result) {
294288
// *err = g_strdup_printf ("Symbol not found: %s", name);
295289
return NULL;
296290
}
297-
298291
return result->func;
299292
}
300293

src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void ScanAssembly(Assembly asm)
4343

4444
public IEnumerable<string> Generate(string[] pinvokeModules, string outputPath)
4545
{
46-
var modules = new Dictionary<string, string>();
46+
var modules = new SortedDictionary<string, string>(StringComparer.Ordinal);
4747
foreach (var module in pinvokeModules)
4848
modules[module] = module;
4949

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

65-
private void EmitPInvokeTable(StreamWriter w, Dictionary<string, string> modules, List<PInvoke> pinvokes)
65+
private void EmitPInvokeTable(StreamWriter w, SortedDictionary<string, string> modules, List<PInvoke> pinvokes)
6666
{
6767

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

100100
var pinvokesGroupedByEntryPoint = pinvokes
101101
.Where(l => modules.ContainsKey(l.Module))
102-
.OrderBy(l => l.EntryPoint)
103-
.GroupBy(CEntryPoint);
102+
.OrderBy(l => l.EntryPoint, StringComparer.Ordinal)
103+
.GroupBy(CEntryPoint, StringComparer.Ordinal);
104104
var comparer = new PInvokeComparer();
105105
foreach (IGrouping<string, PInvoke> group in pinvokesGroupedByEntryPoint)
106106
{
@@ -348,7 +348,6 @@ private void EmitNativeToInterp(StreamWriter w, List<PInvokeCallback> callbacks)
348348
callbackNames.Add(cb.EntrySymbol);
349349
if (keys.Contains(cb.Key))
350350
{
351-
352351
Error($"Two callbacks with the same Name and number of arguments '{cb.Key}' are not supported.");
353352
}
354353
keys.Add(cb.Key);

0 commit comments

Comments
 (0)