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: has_start_function check #685

Merged
merged 1 commit into from
Jan 8, 2021
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
6 changes: 6 additions & 0 deletions include/fizzy/fizzy.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ bool fizzy_module_has_memory(const FizzyModule* module);
bool fizzy_find_exported_function_index(
const FizzyModule* module, const char* name, uint32_t* out_func_idx);

/// Check whether module has a start function.
///
/// @param module Pointer to module. Cannot be NULL.
/// @return true if module has a start function, false otherwise.
bool fizzy_module_has_start_function(const FizzyModule* module);

/// Instantiate a module.
///
/// The instance takes ownership of the module, i.e. fizzy_free_module must not be called on the
Expand Down
5 changes: 5 additions & 0 deletions lib/fizzy/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ bool fizzy_find_exported_global(
return true;
}

bool fizzy_module_has_start_function(const FizzyModule* module)
{
return unwrap(module)->startfunc.has_value();
}

FizzyInstance* fizzy_instantiate(const FizzyModule* module,
const FizzyExternalFunction* imported_functions, size_t imported_functions_size,
const FizzyExternalTable* imported_table, const FizzyExternalMemory* imported_memory,
Expand Down
26 changes: 26 additions & 0 deletions test/unittests/capi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,32 @@ TEST(capi, find_exported_global)
fizzy_free_instance(instance);
}

TEST(capi, has_start_function)
{
/* wat2wasm
(module)
*/
const auto wasm_no_start = from_hex("0061736d01000000");
const auto module_no_start = fizzy_parse(wasm_no_start.data(), wasm_no_start.size());
ASSERT_NE(module_no_start, nullptr);

EXPECT_FALSE(fizzy_module_has_start_function(module_no_start));

fizzy_free_module(module_no_start);

/* wat2wasm
(func)
(start 0)
*/
const auto wasm_start = from_hex("0061736d01000000010401600000030201000801000a040102000b");
const auto module_start = fizzy_parse(wasm_start.data(), wasm_start.size());
ASSERT_NE(module_start, nullptr);

EXPECT_TRUE(fizzy_module_has_start_function(module_start));

fizzy_free_module(module_start);
}

TEST(capi, instantiate)
{
uint8_t wasm_prefix[]{0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00};
Expand Down