Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Sep 22, 2023
1 parent 5d0a4c5 commit 388f5f5
Show file tree
Hide file tree
Showing 18 changed files with 544 additions and 422 deletions.
12 changes: 11 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ filegroup(
cc_library(
name = "ecsact_si_wasm",
copts = copts,
defines = ["ECSACTSI_WASM_API="],
defines = [
"ECSACTSI_WASM_API=",
"ECSACT_DYNAMIC_API=",
],
hdrs = [":headers"],
srcs = [":sources"],
deps = [
Expand All @@ -43,7 +46,14 @@ cc_library(
"ecsact/wasm/detail/minst/minst.cc",
],
deps = [
":cpp_util",
"@ecsact_runtime//:common",
"@wasmer",
],
)

cc_library(
name = "cpp_util",
copts = copts,
hdrs = ["ecsact/wasm/detail/cpp_util.hh"],
)
4 changes: 2 additions & 2 deletions bazel/copts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ copts = selects.with_or({
"-std=c++20",
],
("@rules_cc//cc/compiler:clang"): [
"-std=c++2b",
"-std=c++20",
"-fexperimental-library",
],
("@rules_cc//cc/compiler:msvc-cl", "@rules_cc//cc/compiler:clang-cl"): [
"/std:c++latest",
"/std:c++20",
"/permissive-",
"/Zc:preprocessor",
],
Expand Down
5 changes: 5 additions & 0 deletions ecsact/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ typedef enum ecsactsi_wasm_error {
* WASM file contains correctly named guest import, but was not a function
*/
ECSACTSI_WASM_ERR_GUEST_IMPORT_INVALID,

/**
* Invoking `_initialize()` resulted in a wasm trap
*/
ECSACTSI_WASM_ERR_INITIALIZE_FAIL,
} ecsactsi_wasm_error;

ECSACTSI_WASM_API_FN(void, ecsactsi_wasm_last_error_message)
Expand Down
10 changes: 6 additions & 4 deletions ecsact/wasm.hh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

#include <iostream>
#include <format>
#include <string>
#include <cstdint>
#include <filesystem>
#include <optional>
#include <string_view>
#include <span>
#include <concepts>
#include <print>
#include "ecsact/wasm.h"

namespace ecsact::wasm {
Expand Down Expand Up @@ -130,13 +132,13 @@ inline auto consume_and_print_logs() -> void {
switch(l) {
default:
case log_level::info:
std::println(stdout, "[INFO] {}", m);
std::cout << std::format("[INFO] {}", m);
break;
case log_level::warning:
std::println(stderr, "[WARNING] {}", m);
std::cerr << std::format("[WARNING] {}", m);
break;
case log_level::error:
std::println(stderr, "[ERROR] {}", m);
std::cerr << std::format("[ERROR] {}", m);
break;
}
});
Expand Down
33 changes: 33 additions & 0 deletions ecsact/wasm/detail/cpp_util.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <utility>

#ifndef defer
struct deferred_invoker {};

template<typename F>
struct deferred {
F _defer_fn;

~deferred() {
_defer_fn();
}
};

template<typename F>
deferred<F> operator*(deferred_invoker, F&& f) {
return {std::forward<F>(f)};
}

/**
* USAGE: defer { statements; }
*/
# define defer auto _deferred##__LINE__ = deferred_invoker{}* [&]()
#endif // defer

template<typename... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
};
template<typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
12 changes: 12 additions & 0 deletions ecsact/wasm/detail/globals.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "ecsact/wasm/detail/globals.hh"

namespace {
wasm_engine_t* _engine = nullptr;
}

auto ecsact::wasm::detail::engine() -> wasm_engine_t* {
if(!_engine) {
_engine = wasm_engine_new();
}
return _engine;
}
7 changes: 7 additions & 0 deletions ecsact/wasm/detail/globals.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <wasm.h>

namespace ecsact::wasm::detail {
auto engine() -> wasm_engine_t*;
}
11 changes: 6 additions & 5 deletions ecsact/wasm/detail/guest_imports.hh
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#pragma once

#include <string>
#include <string_view>
#include <functional>
#include <map>
#include <wasm.h>
#include "ecsact/wasm/detail/minst/minst.hh"

namespace ecsactsi_wasm::detail {
namespace ecsact::wasm::detail {
using allowed_guest_imports_t = std::unordered_map<
std::string, // Function name
std::function<wasm_func_t*(wasm_store_t*)>>;
std::string_view, // Function name
std::function<minst_import_resolve_func()>>;

using allowed_guest_modules_t = std::unordered_map<
std::string, // Module name
std::string_view, // Module name
allowed_guest_imports_t>;
} // namespace ecsactsi_wasm::detail
Loading

0 comments on commit 388f5f5

Please sign in to comment.