Skip to content

Commit 87e998e

Browse files
committed
capi: Get function type from module
1 parent c80ba7c commit 87e998e

3 files changed

Lines changed: 73 additions & 10 deletions

File tree

include/fizzy/fizzy.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,21 @@ typedef FizzyExecutionResult (*FizzyExternalFn)(void* context, FizzyInstance* in
5151
const union FizzyValue* args, size_t args_size, int depth);
5252

5353
/// Value type.
54-
enum FizzyValueType
55-
{
56-
FizzyValueTypeI32 = 0x7f,
57-
FizzyValueTypeI64 = 0x7e,
58-
FizzyValueTypeF32 = 0x7d,
59-
FizzyValueTypeF64 = 0x7c,
60-
};
54+
typedef uint8_t FizzyValueType;
55+
static const FizzyValueType FizzyValueTypeI32 = 0x7f;
56+
static const FizzyValueType FizzyValueTypeI64 = 0x7e;
57+
static const FizzyValueType FizzyValueTypeF32 = 0x7d;
58+
static const FizzyValueType FizzyValueTypeF64 = 0x7c;
6159

6260
/// Function type.
6361
typedef struct FizzyFunctionType
6462
{
6563
/// Pointer to input types array.
66-
const enum FizzyValueType* inputs;
64+
const FizzyValueType* inputs;
6765
/// Input types array size.
6866
size_t inputs_size;
6967
/// Pointer to output types array.
70-
const enum FizzyValueType* outputs;
68+
const FizzyValueType* outputs;
7169
/// Output types array size.
7270
size_t outputs_size;
7371
} FizzyFunctionType;
@@ -97,6 +95,15 @@ const FizzyModule* fizzy_parse(const uint8_t* wasm_binary, size_t wasm_binary_si
9795
/// If passed pointer is NULL, has no effect.
9896
void fizzy_free_module(const FizzyModule* module);
9997

98+
/// Get type of the function defined in the module.
99+
///
100+
/// @param module Pointer to module.
101+
/// @param func_idx Function index. Can be either index of an imported function or of a function
102+
/// defined in module.
103+
///
104+
/// @note All module function indices are greater than all imported function indices.
105+
FizzyFunctionType fizzy_get_function_type(const FizzyModule* module, uint32_t func_idx);
106+
100107
/// Find index of exported function by name.
101108
///
102109
/// @param module Pointer to module.

lib/fizzy/capi.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,21 @@ inline const fizzy::Module* unwrap(const FizzyModule* module) noexcept
2121
return reinterpret_cast<const fizzy::Module*>(module);
2222
}
2323

24+
inline const FizzyValueType* wrap(const fizzy::ValType* value_types) noexcept
25+
{
26+
static_assert(sizeof(FizzyValueType) == sizeof(fizzy::ValType));
27+
return reinterpret_cast<const FizzyValueType*>(value_types);
28+
}
29+
2430
inline fizzy::ValType unwrap(FizzyValueType value_type) noexcept
2531
{
26-
return static_cast<fizzy::ValType>(static_cast<uint8_t>(value_type));
32+
return static_cast<fizzy::ValType>(value_type);
33+
}
34+
35+
inline FizzyFunctionType wrap(const fizzy::FuncType& type) noexcept
36+
{
37+
return {(type.inputs.empty() ? nullptr : wrap(&type.inputs[0])), type.inputs.size(),
38+
(type.outputs.empty() ? nullptr : wrap(&type.outputs[0])), type.outputs.size()};
2739
}
2840

2941
inline fizzy::FuncType unwrap(const FizzyFunctionType& type)
@@ -133,6 +145,11 @@ void fizzy_free_module(const FizzyModule* module)
133145
delete unwrap(module);
134146
}
135147

148+
FizzyFunctionType fizzy_get_function_type(const FizzyModule* module, uint32_t func_idx)
149+
{
150+
return wrap(unwrap(module)->get_function_type(func_idx));
151+
}
152+
136153
bool fizzy_find_exported_function(
137154
const FizzyModule* module, const char* name, uint32_t* out_func_idx)
138155
{

test/unittests/capi_test.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,45 @@ TEST(capi, free_module_null)
3232
fizzy_free_module(nullptr);
3333
}
3434

35+
TEST(capi, get_function_type)
36+
{
37+
/* wat2wasm
38+
(func)
39+
(func (param i32 i32) (result i32) (i32.const 0))
40+
(func (param i64))
41+
(func (param f64) (result f32) (f32.const 0))
42+
*/
43+
const auto wasm = from_hex(
44+
"0061736d0100000001130460000060027f7f017f60017e0060017c017d030504000102030a140402000b040041"
45+
"000b02000b070043000000000b");
46+
const auto module = fizzy_parse(wasm.data(), wasm.size());
47+
EXPECT_NE(module, nullptr);
48+
49+
const auto type0 = fizzy_get_function_type(module, 0);
50+
EXPECT_EQ(type0.inputs_size, 0);
51+
EXPECT_EQ(type0.outputs_size, 0);
52+
53+
const auto type1 = fizzy_get_function_type(module, 1);
54+
EXPECT_EQ(type1.inputs_size, 2);
55+
EXPECT_EQ(type1.inputs[0], FizzyValueTypeI32);
56+
EXPECT_EQ(type1.inputs[1], FizzyValueTypeI32);
57+
EXPECT_EQ(type1.outputs_size, 1);
58+
EXPECT_EQ(type1.outputs[0], FizzyValueTypeI32);
59+
60+
const auto type2 = fizzy_get_function_type(module, 2);
61+
EXPECT_EQ(type2.inputs_size, 1);
62+
EXPECT_EQ(type2.inputs[0], FizzyValueTypeI64);
63+
EXPECT_EQ(type2.outputs_size, 0);
64+
65+
const auto type3 = fizzy_get_function_type(module, 3);
66+
EXPECT_EQ(type3.inputs_size, 1);
67+
EXPECT_EQ(type3.inputs[0], FizzyValueTypeF64);
68+
EXPECT_EQ(type3.outputs_size, 1);
69+
EXPECT_EQ(type3.outputs[0], FizzyValueTypeF32);
70+
71+
fizzy_free_module(module);
72+
}
73+
3574
TEST(capi, find_exported_function)
3675
{
3776
/* wat2wasm

0 commit comments

Comments
 (0)