Skip to content

Commit

Permalink
capi: Add function to get global type by index
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Jan 11, 2021
1 parent d046b25 commit 8f7ff15
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
17 changes: 17 additions & 0 deletions include/fizzy/fizzy.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,23 @@ bool fizzy_module_has_table(const FizzyModule* module);
/// @return true if module has a memory definition, false otherwise.
bool fizzy_module_has_memory(const FizzyModule* module);

/// Get number of globals defined in the module.
///
/// @param module Pointer to module. Cannot be NULL.
/// @return Number of globals in the module.
uint32_t fizzy_get_global_count(const FizzyModule* module);

/// Get type of a given global defined in the module.
///
/// @param module Pointer to module. Cannot be NULL.
/// @param global_idx Global index. Can be either index of an imported global or of a global
/// defined in module. Behaviour is undefined if index is not valid according
/// to module definition.
/// @return Type of the global corresponding to the index.
///
/// @note All module global indices are greater than all imported global indices.
FizzyGlobalType fizzy_get_global_type(const FizzyModule* module, uint32_t global_idx);

/// Find index of exported function by name.
///
/// @param module Pointer to module. Cannot be NULL.
Expand Down
10 changes: 10 additions & 0 deletions lib/fizzy/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,16 @@ bool fizzy_module_has_memory(const FizzyModule* module)
return unwrap(module)->has_memory();
}

uint32_t fizzy_get_global_count(const FizzyModule* module)
{
return static_cast<uint32_t>(unwrap(module)->get_global_count());
}

FizzyGlobalType fizzy_get_global_type(const FizzyModule* module, uint32_t global_idx)
{
return wrap(unwrap(module)->get_global_type(global_idx));
}

bool fizzy_find_exported_function_index(
const FizzyModule* module, const char* name, uint32_t* out_func_idx)
{
Expand Down
91 changes: 91 additions & 0 deletions test/unittests/capi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,3 +1194,94 @@ TEST(capi, get_type)

fizzy_free_module(module_imported_func);
}

TEST(capi, get_global_count)
{
/* wat2wasm
(module)
*/
const auto wasm = from_hex("0061736d01000000");

const auto* module_empty = fizzy_parse(wasm.data(), wasm.size());
ASSERT_NE(module_empty, nullptr);

EXPECT_EQ(fizzy_get_global_count(module_empty), 0);
fizzy_free_module(module_empty);

/* wat2wasm
(global i32 (i32.const 0))
*/
const auto wasm_one_global = from_hex("0061736d010000000606017f0041000b");

const auto* module_one_global = fizzy_parse(wasm_one_global.data(), wasm_one_global.size());
ASSERT_NE(module_one_global, nullptr);

EXPECT_EQ(fizzy_get_global_count(module_one_global), 1);
fizzy_free_module(module_one_global);

/* wat2wasm
(global (import "mod" "g") i32)
(global i32 (i32.const 0))
*/
const auto wasm_imported_global =
from_hex("0061736d01000000020a01036d6f640167037f000606017f0041000b");

const auto* module_imported_global =
fizzy_parse(wasm_imported_global.data(), wasm_imported_global.size());
ASSERT_NE(module_imported_global, nullptr);

EXPECT_EQ(fizzy_get_global_count(module_imported_global), 2);
fizzy_free_module(module_imported_global);
}

TEST(capi, get_global_type)
{
/* wat2wasm
(global i32 (i32.const 0))
(global (mut i64) (i64.const 0))
(global f32 (f32.const 0))
(global (mut f64) (f64.const 0))
*/
const auto wasm = from_hex(
"0061736d01000000061f047f0041000b7e0142000b7d0043000000000b7c014400000000000000000b");

const auto* module = fizzy_parse(wasm.data(), wasm.size());
ASSERT_NE(module, nullptr);
ASSERT_EQ(fizzy_get_global_count(module), 4);

EXPECT_EQ(fizzy_get_global_type(module, 0).value_type, FizzyValueTypeI32);
EXPECT_EQ(fizzy_get_global_type(module, 0).is_mutable, false);
EXPECT_EQ(fizzy_get_global_type(module, 1).value_type, FizzyValueTypeI64);
EXPECT_EQ(fizzy_get_global_type(module, 1).is_mutable, true);
EXPECT_EQ(fizzy_get_global_type(module, 2).value_type, FizzyValueTypeF32);
EXPECT_EQ(fizzy_get_global_type(module, 2).is_mutable, false);
EXPECT_EQ(fizzy_get_global_type(module, 3).value_type, FizzyValueTypeF64);
EXPECT_EQ(fizzy_get_global_type(module, 3).is_mutable, true);

fizzy_free_module(module);

/* wat2wasm
(global (import "mod" "g1") i32)
(global (import "mod" "g2") (mut i64))
(global (import "mod" "g3") f32)
(global (import "mod" "g4") (mut f64))
*/
const auto wasm_imports = from_hex(
"0061736d01000000022904036d6f64026731037f00036d6f64026732037e01036d6f64026733037d00036d6f64"
"026734037c01");

const auto* module_imports = fizzy_parse(wasm_imports.data(), wasm_imports.size());
ASSERT_NE(module_imports, nullptr);
ASSERT_EQ(fizzy_get_global_count(module_imports), 4);

EXPECT_EQ(fizzy_get_global_type(module_imports, 0).value_type, FizzyValueTypeI32);
EXPECT_EQ(fizzy_get_global_type(module_imports, 0).is_mutable, false);
EXPECT_EQ(fizzy_get_global_type(module_imports, 1).value_type, FizzyValueTypeI64);
EXPECT_EQ(fizzy_get_global_type(module_imports, 1).is_mutable, true);
EXPECT_EQ(fizzy_get_global_type(module_imports, 2).value_type, FizzyValueTypeF32);
EXPECT_EQ(fizzy_get_global_type(module_imports, 2).is_mutable, false);
EXPECT_EQ(fizzy_get_global_type(module_imports, 3).value_type, FizzyValueTypeF64);
EXPECT_EQ(fizzy_get_global_type(module_imports, 3).is_mutable, true);

fizzy_free_module(module_imports);
}

0 comments on commit 8f7ff15

Please sign in to comment.