Skip to content

Commit

Permalink
capi: Add function to clone a module
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Dec 15, 2020
1 parent 87d25ef commit afa78e8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/fizzy/fizzy.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ const FizzyModule* fizzy_parse(const uint8_t* wasm_binary, size_t wasm_binary_si
/// If passed pointer is NULL, has no effect.
void fizzy_free_module(const FizzyModule* module);

/// Make a copy of a module.
///
/// @param module Pointer to module. Cannot be NULL.
/// @returns Pointer to a newly allocated module, identical to @a module.
///
/// @note Creating a copy is required if more than single instance of a module is required, because
/// instantiation takes ownetship of a module, and the same module can't be instantiated twice.
const FizzyModule* fizzy_clone_module(const FizzyModule* module);

/// Get type of the function defined in the module.
///
/// @param module Pointer to module. Cannot be NULL.
Expand Down
13 changes: 13 additions & 0 deletions lib/fizzy/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,19 @@ void fizzy_free_module(const FizzyModule* module)
delete unwrap(module);
}

const FizzyModule* fizzy_clone_module(const FizzyModule* module)
{
try
{
auto clone = std::make_unique<fizzy::Module>(*unwrap(module));
return wrap(clone.release());
}
catch (...)
{
return nullptr;
}
}

FizzyFunctionType fizzy_get_function_type(const FizzyModule* module, uint32_t func_idx)
{
return wrap(unwrap(module)->get_function_type(func_idx));
Expand Down
43 changes: 43 additions & 0 deletions test/unittests/capi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@ TEST(capi, free_module_null)
fizzy_free_module(nullptr);
}

TEST(capi, clone_module)
{
/* wat2wasm
(func (param i32 i32) (result i32) (i32.const 0))
*/
const auto wasm = from_hex("0061736d0100000001070160027f7f017f030201000a0601040041000b");
const auto* module1 = fizzy_parse(wasm.data(), wasm.size());
EXPECT_NE(module1, nullptr);

const auto* module2 = fizzy_clone_module(module1);
EXPECT_NE(module2, nullptr);
EXPECT_NE(module1, module2);

const auto type2 = fizzy_get_function_type(module2, 0);
EXPECT_EQ(type2.output, FizzyValueTypeI32);
ASSERT_EQ(type2.inputs_size, 2);
EXPECT_EQ(type2.inputs[0], FizzyValueTypeI32);
EXPECT_EQ(type2.inputs[1], FizzyValueTypeI32);

fizzy_free_module(module2);
fizzy_free_module(module1);
}

TEST(capi, get_function_type)
{
/* wat2wasm
Expand Down Expand Up @@ -397,6 +420,26 @@ TEST(capi, instantiate_imported_globals)
fizzy_instantiate(module, nullptr, 0, nullptr, nullptr, globals_type_mismatch, 4), nullptr);
}

TEST(capi, instantiate_twice)
{
uint8_t wasm_prefix[]{0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00};
const auto* module1 = fizzy_parse(wasm_prefix, sizeof(wasm_prefix));
ASSERT_NE(module1, nullptr);

auto* instance1 = fizzy_instantiate(module1, nullptr, 0, nullptr, nullptr, nullptr, 0);
EXPECT_NE(instance1, nullptr);

const auto* module2 = fizzy_clone_module(module1);
ASSERT_NE(module2, nullptr);

auto* instance2 = fizzy_instantiate(module2, nullptr, 0, nullptr, nullptr, nullptr, 0);
EXPECT_NE(instance2, nullptr);
EXPECT_NE(instance1, instance2);

fizzy_free_instance(instance2);
fizzy_free_instance(instance1);
}

TEST(capi, resolve_instantiate_no_imports)
{
/* wat2wasm
Expand Down

0 comments on commit afa78e8

Please sign in to comment.