Skip to content

Commit a2d0e67

Browse files
committed
Simplify passing array of imported functions
1 parent a312928 commit a2d0e67

3 files changed

Lines changed: 23 additions & 59 deletions

File tree

include/fizzy/fizzy.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,9 @@ typedef struct fizzy_external_function
4545
void* context;
4646
} fizzy_external_function;
4747

48-
struct fizzy_external_function_vector;
49-
50-
struct fizzy_external_function_vector* fizzy_new_external_function_vector(
51-
const struct fizzy_external_function* functions, uint32_t size);
52-
53-
void fizzy_free_external_function_vector(struct fizzy_external_function_vector*);
54-
55-
// Takes ownership of module and imported_functions.
56-
// imported_functions may be NULL.
57-
struct fizzy_instance* fizzy_instantiate(
58-
struct fizzy_module* module, struct fizzy_external_function_vector* imported_functions);
48+
// Takes ownership of module.
49+
struct fizzy_instance* fizzy_instantiate(struct fizzy_module* module,
50+
const struct fizzy_external_function* imported_functions, uint32_t imported_functions_size);
5951

6052
void fizzy_free_instance(struct fizzy_instance*);
6153

lib/fizzy/capi.cpp

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,22 @@ void fizzy_free_module(fizzy_module* module)
5959
delete module;
6060
}
6161

62-
struct fizzy_external_function_vector
63-
{
64-
std::vector<fizzy::ExternalFunction> vector;
65-
};
66-
6762
struct fizzy_instance
6863
{
6964
fizzy::Instance* instance;
7065
};
7166

72-
fizzy_external_function_vector* fizzy_new_external_function_vector(
73-
const fizzy_external_function* cfunctions, uint32_t size)
67+
fizzy_instance* fizzy_instantiate(fizzy_module* module,
68+
const fizzy_external_function* imported_functions, uint32_t imported_functions_size)
7469
{
75-
std::vector<fizzy::ExternalFunction> functions(size);
76-
std::transform(
77-
cfunctions, cfunctions + size, functions.begin(), [](const fizzy_external_function& cfunc) {
78-
auto func = [cfunc](fizzy::Instance& instance, fizzy::span<const fizzy::Value> args,
70+
try
71+
{
72+
std::vector<fizzy::ExternalFunction> functions(imported_functions_size);
73+
for (size_t imported_func_idx = 0; imported_func_idx < imported_functions_size;
74+
++imported_func_idx)
75+
{
76+
auto func = [cfunc = imported_functions[imported_func_idx]](fizzy::Instance& instance,
77+
fizzy::span<const fizzy::Value> args,
7978
int depth) -> fizzy::ExecutionResult {
8079
fizzy_instance cinstance{&instance};
8180
const auto cres = cfunc.function(cfunc.context, &cinstance,
@@ -89,43 +88,20 @@ fizzy_external_function_vector* fizzy_new_external_function_vector(
8988
else
9089
return bit_cast<fizzy::Value>(cres.value);
9190
};
92-
// TODO leave type empty for now
93-
return fizzy::ExternalFunction{std::move(func), {}};
94-
});
95-
96-
return new fizzy_external_function_vector{std::move(functions)};
97-
}
9891

99-
void fizzy_free_external_function_vector(fizzy_external_function_vector* vector)
100-
{
101-
delete vector;
102-
}
92+
// TODO get type from input array
93+
auto func_type = module->module.imported_function_types[imported_func_idx];
10394

104-
fizzy_instance* fizzy_instantiate(
105-
fizzy_module* module, fizzy_external_function_vector* imported_functions)
106-
{
107-
try
108-
{
109-
// TODO temp: fill types of imported funcs
110-
if (imported_functions)
111-
{
112-
for (size_t imported_func_idx = 0;
113-
imported_func_idx < imported_functions->vector.size(); ++imported_func_idx)
114-
{
115-
imported_functions->vector[imported_func_idx].type =
116-
module->module.imported_function_types[imported_func_idx];
117-
}
95+
functions[imported_func_idx] =
96+
fizzy::ExternalFunction{std::move(func), std::move(func_type)};
11897
}
11998

120-
auto instance = fizzy::instantiate(
121-
std::move(module->module), imported_functions ? std::move(imported_functions->vector) :
122-
std::vector<fizzy::ExternalFunction>{});
99+
auto instance = fizzy::instantiate(std::move(module->module), std::move(functions));
123100

124101
auto cinstance = std::make_unique<fizzy_instance>();
125102
cinstance->instance = instance.release();
126103

127104
fizzy_free_module(module);
128-
fizzy_free_external_function_vector(imported_functions);
129105
return cinstance.release();
130106
}
131107
catch (...)

test/unittests/capi_test.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ TEST(capi, instantiate)
2929
uint8_t wasm_prefix[]{0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00};
3030
auto module = fizzy_parse(wasm_prefix, sizeof(wasm_prefix));
3131

32-
auto instance = fizzy_instantiate(module, nullptr);
32+
auto instance = fizzy_instantiate(module, nullptr, 0);
3333
EXPECT_TRUE(instance);
3434

3535
fizzy_free_instance(instance);
@@ -48,7 +48,7 @@ TEST(capi, execute)
4848

4949
auto module = fizzy_parse(wasm.data(), wasm.size());
5050

51-
auto instance = fizzy_instantiate(module, nullptr);
51+
auto instance = fizzy_instantiate(module, nullptr, 0);
5252

5353
auto res = fizzy_execute(instance, 0, nullptr, 0, 0);
5454
EXPECT_FALSE(res.trapped);
@@ -86,9 +86,7 @@ TEST(capi, execute_with_host_function)
8686
},
8787
nullptr}};
8888

89-
auto imported_funcs = fizzy_new_external_function_vector(host_funcs, 2);
90-
91-
auto instance = fizzy_instantiate(module, imported_funcs);
89+
auto instance = fizzy_instantiate(module, host_funcs, 2);
9290

9391
auto res = fizzy_execute(instance, 0, nullptr, 0, 0);
9492
EXPECT_FALSE(res.trapped);
@@ -119,7 +117,7 @@ TEST(capi, imported_function_from_another_module)
119117
const auto bin1 = from_hex(
120118
"0061736d0100000001070160027f7f017f030201000707010373756200000a09010700200020016b0b");
121119
auto module1 = fizzy_parse(bin1.data(), bin1.size());
122-
auto instance1 = fizzy_instantiate(module1, nullptr);
120+
auto instance1 = fizzy_instantiate(module1, nullptr, 0);
123121

124122
/* wat2wasm
125123
(module
@@ -146,9 +144,7 @@ TEST(capi, imported_function_from_another_module)
146144
};
147145
fizzy_external_function host_funcs[] = {{sub, instance1}};
148146

149-
auto imported_funcs = fizzy_new_external_function_vector(host_funcs, 1);
150-
151-
auto instance2 = fizzy_instantiate(module2, imported_funcs);
147+
auto instance2 = fizzy_instantiate(module2, host_funcs, 1);
152148

153149
fizzy_value args[] = {{44}, {2}};
154150
auto res2 = fizzy_execute(instance2, 1, args, 2, 0);

0 commit comments

Comments
 (0)