@@ -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-
6762struct 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 (...)
0 commit comments