Skip to content

Commit 20e1775

Browse files
committed
capi: Get module from an instance
1 parent 0e396fe commit 20e1775

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

include/fizzy/fizzy.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ FizzyInstance* fizzy_instantiate(const FizzyModule* module,
125125
/// If passed pointer is NULL, has no effect.
126126
void fizzy_free_instance(FizzyInstance* instance);
127127

128+
/// Get pointer to module of an instance.
129+
///
130+
/// @note The returned pointer respresents non-owning, "view"-access to the module and must not be
131+
/// passed to fizzy_free_module.
132+
const FizzyModule* fizzy_get_instance_module(FizzyInstance* instance);
133+
128134
/// Get pointer to memory of an instance.
129135
///
130136
/// @returns Pointer to memory data or NULL in case instance doesn't have any memory.

lib/fizzy/capi.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ void fizzy_free_instance(FizzyInstance* instance)
176176
delete unwrap(instance);
177177
}
178178

179+
const FizzyModule* fizzy_get_instance_module(FizzyInstance* instance)
180+
{
181+
return wrap(unwrap(instance)->module.get());
182+
}
183+
179184
uint8_t* fizzy_get_instance_memory_data(FizzyInstance* instance)
180185
{
181186
auto& memory = unwrap(instance)->memory;

test/unittests/capi_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ TEST(capi, free_instance_null)
116116
fizzy_free_instance(nullptr);
117117
}
118118

119+
TEST(capi, get_instance_module)
120+
{
121+
/* wat2wasm
122+
(func (param i32 i32))
123+
*/
124+
const auto wasm = from_hex("0061736d0100000001060160027f7f00030201000a040102000b");
125+
auto module = fizzy_parse(wasm.data(), wasm.size());
126+
EXPECT_NE(module, nullptr);
127+
128+
auto instance = fizzy_instantiate(module, nullptr, 0);
129+
EXPECT_NE(instance, nullptr);
130+
131+
auto instance_module = fizzy_get_instance_module(instance);
132+
EXPECT_NE(instance_module, nullptr);
133+
134+
EXPECT_EQ(fizzy_get_function_type(instance_module, 0).inputs_size, 2);
135+
136+
fizzy_free_instance(instance);
137+
}
138+
119139
TEST(capi, memory_access_no_memory)
120140
{
121141
/* wat2wasm

0 commit comments

Comments
 (0)