Skip to content

Commit

Permalink
wasi: Split run() procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Mar 1, 2021
1 parent bb29aaa commit 9d76188
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion test/unittests/wasi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ TEST(wasi, no_file)
const char* args[]{"ABC"};

std::ostringstream err;
EXPECT_FALSE(wasi::run(std::size(args), args, err));
EXPECT_FALSE(wasi::load_and_run(std::size(args), args, err));
EXPECT_EQ(err.str(), "File does not exist: \"ABC\"\n");
}
2 changes: 1 addition & 1 deletion tools/wasi/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main(int argc, const char** argv)
}

// Drop "fizzy-wasi" from the argv, but keep the wasm file name in argv[1].
const bool res = fizzy::wasi::run(argc - 1, argv + 1, std::cerr);
const bool res = fizzy::wasi::load_and_run(argc - 1, argv + 1, std::cerr);
return res ? 0 : 1;
}
catch (const std::exception& ex)
Expand Down
15 changes: 10 additions & 5 deletions tools/wasi/wasi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ std::optional<bytes> load_wasm_binary(const std::filesystem::path& path, std::os
return bytes(std::istreambuf_iterator<char>{wasm_file}, std::istreambuf_iterator<char>{});
}

bool run(int argc, const char** argv, std::ostream& err)
bool run(bytes_view wasm_binary, int argc, const char* argv[], std::ostream& err)
{
constexpr auto ns = "wasi_snapshot_preview1";
const std::vector<ImportedFunction> wasi_functions = {
Expand Down Expand Up @@ -156,10 +156,6 @@ bool run(int argc, const char** argv, std::ostream& err)
return false;
}

const auto wasm_binary = load_wasm_binary(argv[0], err);
if (wasm_binary.empty())
return false;

auto module = parse(wasm_binary);
auto imports = resolve_imported_functions(*module, wasi_functions);
auto instance =
Expand Down Expand Up @@ -197,4 +193,13 @@ bool run(int argc, const char** argv, std::ostream& err)

return true;
}

bool load_and_run(int argc, const char** argv, std::ostream& err)
{
const auto wasm_binary = load_wasm_binary(argv[0], err);
if (!wasm_binary)
return false;

return run(*wasm_binary, argc, argv, err);
}
} // namespace fizzy::wasi
18 changes: 14 additions & 4 deletions tools/wasi/wasi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ namespace fizzy::wasi
{
std::optional<bytes> load_wasm_binary(const std::filesystem::path& path, std::ostream& err);

/// Executes WASI main function with given CLI arguments.
/// Loads the wasm file (path in first CLI argument) and
/// executes WASI main function with given CLI arguments.
///
/// @param argc Number of CLI arguments.
/// @param argv Array of CLI arguments.
/// @param argc Number of CLI arguments. Must be >= 1.
/// @param argv Array of CLI arguments. The first argument must be wasm file path.
/// @param err Error output stream.
/// @return False in case of error.
///
/// @todo Make noexcept.
bool run(int argc, const char** argv, std::ostream& err);
bool load_and_run(int argc, const char** argv, std::ostream& err);

/// Executes WASI main function from the wasm binary with given CLI arguments.
///
/// @param wasm_binary Wasm binary.
/// @param argc Number of CLI arguments.
/// @param argv Array of CLI arguments. The first argument should be wasm file path.
/// @param err Error output stream.
/// @return False in case of error.
bool run(bytes_view wasm_binary, int argc, const char* argv[], std::ostream& err);
} // namespace fizzy::wasi

0 comments on commit 9d76188

Please sign in to comment.