Skip to content

Commit

Permalink
wasi: Call uvwasi_destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed May 6, 2021
1 parent 585c9e8 commit 61e7d05
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
25 changes: 25 additions & 0 deletions test/unittests/wasi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright 2021 The Fizzy Authors.
// SPDX-License-Identifier: Apache-2.0

#include "uvwasi.hpp"
#include "wasi.hpp"
#include <gtest/gtest.h>
#include <test/utils/hex.hpp>
Expand All @@ -10,6 +11,30 @@
using namespace fizzy;
using namespace fizzy::test;

TEST(wasi, destroy_non_inited_uvwasi)
{
auto uvwasi = wasi::create_uvwasi();
}

TEST(wasi, init)
{
const char* args[]{"ABC"};

auto uvwasi = wasi::create_uvwasi();
EXPECT_EQ(uvwasi->init(std::size(args), args), UVWASI_ESUCCESS);
}

TEST(wasi, init_multiple)
{
auto uvwasi = wasi::create_uvwasi();

const char* args1[]{"ABC"};
EXPECT_EQ(uvwasi->init(std::size(args1), args1), UVWASI_ESUCCESS);

const char* args2[]{"DEF"};
EXPECT_EQ(uvwasi->init(std::size(args2), args2), UVWASI_ESUCCESS);
}

TEST(wasi, no_file)
{
const char* args[]{"ABC"};
Expand Down
2 changes: 1 addition & 1 deletion tools/wasi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include(ProjectUVWASI)
add_library(wasi)
add_library(fizzy::wasi ALIAS wasi)
target_include_directories(wasi PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(wasi PUBLIC fizzy::fizzy-internal PRIVATE uvwasi::uvwasi)
target_link_libraries(wasi PUBLIC fizzy::fizzy-internal uvwasi::uvwasi)
target_sources(wasi PRIVATE wasi.cpp wasi.hpp uvwasi.hpp uvwasi.cpp)

add_executable(fizzy-wasi)
Expand Down
11 changes: 10 additions & 1 deletion tools/wasi/uvwasi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ class UVWASIImpl final : public UVWASI
{
/// UVWASI state.
uvwasi_t m_state{};
bool m_inited = false;

public:
~UVWASIImpl() final
{
if (m_inited)
uvwasi_destroy(&m_state);
}

uvwasi_errno_t init(uvwasi_size_t argc, const char** argv) noexcept final
{
// Initialisation settings.
Expand All @@ -27,7 +34,9 @@ class UVWASIImpl final : public UVWASI
0, 1, 2,
nullptr, // NOTE: no special allocator
};
return uvwasi_init(&m_state, &options);
const auto ret = uvwasi_init(&m_state, &options);
m_inited = (ret == UVWASI_ESUCCESS);
return ret;
}

uvwasi_errno_t proc_exit(uvwasi_exitcode_t exit_code) noexcept final
Expand Down

0 comments on commit 61e7d05

Please sign in to comment.