Skip to content

Commit 51178ea

Browse files
committed
import_object_create_for_host_funcs: tweak api to accept muitiple modules
it might be convenient when a host module has an alias. eg. wasi_snapshot_preview1 and wasi_unstable.
1 parent e9a0528 commit 51178ea

File tree

4 files changed

+58
-29
lines changed

4 files changed

+58
-29
lines changed

lib/host_instance.c

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@ _dtor(struct import_object *im)
2525
}
2626

2727
int
28-
import_object_create_for_host_funcs(const struct name *module_name,
29-
const struct host_func *funcs,
30-
size_t nfuncs, struct host_instance *hi,
28+
import_object_create_for_host_funcs(const struct host_module *modules,
29+
size_t n, struct host_instance *hi,
3130
struct import_object **impp)
3231
{
3332
struct import_object *im;
3433
struct funcinst *fis = NULL;
34+
size_t nfuncs;
35+
size_t i;
3536
int ret;
37+
38+
nfuncs = 0;
39+
for (i = 0; i < n; i++) {
40+
const struct host_module *hm = &modules[i];
41+
nfuncs += hm->nfuncs;
42+
}
43+
3644
assert(nfuncs > 0);
3745
ret = import_object_alloc(nfuncs, &im);
3846
if (ret != 0) {
@@ -45,26 +53,33 @@ import_object_create_for_host_funcs(const struct name *module_name,
4553
}
4654
im->dtor = _dtor;
4755
im->dtor_arg = fis;
48-
size_t i;
49-
for (i = 0; i < nfuncs; i++) {
50-
struct functype *ft;
51-
ret = functype_from_string(funcs[i].type, &ft);
52-
if (ret != 0) {
53-
xlog_error("failed to parse functype: %s",
54-
funcs[i].type);
55-
goto fail;
56+
size_t idx = 0;
57+
for (i = 0; i < n; i++) {
58+
const struct host_module *hm = &modules[i];
59+
size_t j;
60+
for (j = 0; j < hm->nfuncs; j++) {
61+
const struct host_func *func = &hm->funcs[j];
62+
struct functype *ft;
63+
ret = functype_from_string(func->type, &ft);
64+
if (ret != 0) {
65+
xlog_error("failed to parse functype: %s",
66+
func->type);
67+
goto fail;
68+
}
69+
struct funcinst *fi = &fis[idx];
70+
fi->is_host = true;
71+
fi->u.host.func = func->func;
72+
fi->u.host.type = ft;
73+
fi->u.host.instance = hi;
74+
struct import_object_entry *e = &im->entries[idx];
75+
e->module_name = hm->module_name;
76+
e->name = &func->name;
77+
e->type = IMPORT_FUNC;
78+
e->u.func = fi;
79+
idx++;
5680
}
57-
struct funcinst *fi = &fis[i];
58-
fi->is_host = true;
59-
fi->u.host.func = funcs[i].func;
60-
fi->u.host.type = ft;
61-
fi->u.host.instance = hi;
62-
struct import_object_entry *e = &im->entries[i];
63-
e->module_name = module_name;
64-
e->name = &funcs[i].name;
65-
e->type = IMPORT_FUNC;
66-
e->u.func = fi;
6781
}
82+
assert(idx == nfuncs);
6883
*impp = im;
6984
return 0;
7085
fail:

lib/host_instance.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ struct host_instance {
2727
int dummy;
2828
};
2929

30+
struct host_module {
31+
const struct name *module_name;
32+
const struct host_func *funcs;
33+
size_t nfuncs;
34+
};
35+
3036
struct import_object;
31-
int import_object_create_for_host_funcs(const struct name *module_name,
32-
const struct host_func *funcs,
33-
size_t nfuncs,
37+
int import_object_create_for_host_funcs(const struct host_module *hm, size_t n,
3438
struct host_instance *hi,
3539
struct import_object **impp);

lib/wasi.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,11 +3180,16 @@ wasi_instance_destroy(struct wasi_instance *inst)
31803180
const struct name wasi_snapshot_preview1 =
31813181
NAME_FROM_CSTR_LITERAL("wasi_snapshot_preview1");
31823182

3183+
static const struct host_module module_wasi_snapshot_preview1 = {
3184+
.module_name = &wasi_snapshot_preview1,
3185+
.funcs = wasi_funcs,
3186+
.nfuncs = ARRAYCOUNT(wasi_funcs),
3187+
};
3188+
31833189
int
31843190
import_object_create_for_wasi(struct wasi_instance *wasi,
31853191
struct import_object **impp)
31863192
{
31873193
return import_object_create_for_host_funcs(
3188-
&wasi_snapshot_preview1, wasi_funcs, ARRAYCOUNT(wasi_funcs),
3189-
&wasi->hi, impp);
3194+
&module_wasi_snapshot_preview1, 1, &wasi->hi, impp);
31903195
}

lib/wasi_threads.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,16 @@ const struct host_func wasi_threads_funcs[] = {
358358

359359
const struct name wasi_threads_module_name = NAME_FROM_CSTR_LITERAL("wasi");
360360

361+
static const struct host_module module_wasi_threads = {
362+
.module_name = &wasi_threads_module_name,
363+
.funcs = wasi_threads_funcs,
364+
.nfuncs = ARRAYCOUNT(wasi_threads_funcs),
365+
};
366+
361367
int
362368
import_object_create_for_wasi_threads(struct wasi_threads_instance *th,
363369
struct import_object **impp)
364370
{
365-
return import_object_create_for_host_funcs(
366-
&wasi_threads_module_name, wasi_threads_funcs,
367-
ARRAYCOUNT(wasi_threads_funcs), &th->hi, impp);
371+
return import_object_create_for_host_funcs(&module_wasi_threads, 1,
372+
&th->hi, impp);
368373
}

0 commit comments

Comments
 (0)