Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capi: Cloning a module #674

Merged
merged 1 commit into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/fizzy/fizzy.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ 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, or NULL in case
/// memory for a module could not be allocated.
///
/// @note Creating a copy is needed if more than single instance of a module is required, because
/// instantiation takes ownership of a module, and the same module cannot be instantiated twice.
/// @note Input module is not modified neither in success nor in failure case.
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can't do a "deep equal" without some new API 😉


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