Skip to content
Draft
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
2 changes: 1 addition & 1 deletion ecsact/si/wasmer/detail/guest_imports.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace ecsact::wasm::detail {
using allowed_guest_imports_t = std::unordered_map<
std::string_view, // Function name
std::function<minst_import_resolve_func()>>;
std::function<minst_import_resolve_t(const minst_import)>>;

using allowed_guest_modules_t = std::unordered_map<
std::string_view, // Module name
Expand Down
82 changes: 58 additions & 24 deletions ecsact/si/wasmer/detail/guest_imports/env.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,44 @@
namespace ecsact::wasm::detail {

const auto guest_env_module_imports = allowed_guest_imports_t{
{
"memory",
[](const minst_import imp) -> minst_import_resolve_t {
return minst_import_resolve_memory{
.memory_limits = wasm_limits_t{1, 10},
};
},
},
{
"__indirect_function_table",
[](const minst_import imp) -> minst_import_resolve_t {
return minst_import_resolve_table{
.table_type = imp.as_tabletype(),
};
},
},
{
"__stack_pointer",
[](const minst_import imp) -> minst_import_resolve_t {
return minst_import_resolve_global{};
},
},
{
"__memory_base",
[](const minst_import imp) -> minst_import_resolve_t {
return minst_import_resolve_global{};
},
},
{
"__table_base",
[](const minst_import imp) -> minst_import_resolve_t {
return minst_import_resolve_global{};
},
},
{
"ecsact_system_execution_context_action",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_2_0(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32) // out_action_data
Expand All @@ -21,8 +55,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_parent",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_1_1(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32) // parent context (return)
Expand All @@ -33,8 +67,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_same",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_2_1(
wasm_valtype_new(WASM_I32), // context a
wasm_valtype_new(WASM_I32), // context b
Expand All @@ -46,8 +80,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_get",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_4_0(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32), // component_id
Expand All @@ -60,8 +94,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_update",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_4_0(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32), // component_id
Expand All @@ -74,8 +108,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_has",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_3_0(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32), // component_id
Expand All @@ -87,8 +121,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_generate",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_4_0(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32), // component_count
Expand All @@ -101,8 +135,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_add",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_3_0(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32), // component_id
Expand All @@ -114,8 +148,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_remove",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_3_0(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32), // component_id
Expand All @@ -127,8 +161,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_other",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_2_1(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32), // entity_id
Expand All @@ -140,8 +174,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_entity",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_1_1(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32) // entity 9return)
Expand All @@ -152,8 +186,8 @@ const auto guest_env_module_imports = allowed_guest_imports_t{
},
{
"ecsact_system_execution_context_stream_toggle",
[]() -> minst_import_resolve_func {
return {
[](const minst_import) -> minst_import_resolve_t {
return minst_import_resolve_func{
wasm_functype_new_4_0(
wasm_valtype_new(WASM_I32), // context
wasm_valtype_new(WASM_I32), // component_id
Expand Down
22 changes: 22 additions & 0 deletions ecsact/si/wasmer/detail/minst/minst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ using ecsact::wasm::detail::minst;
using ecsact::wasm::detail::minst_export;
using ecsact::wasm::detail::minst_import;
using ecsact::wasm::detail::minst_import_resolve_func;
using ecsact::wasm::detail::minst_import_resolve_global;
using ecsact::wasm::detail::minst_import_resolve_memory;
using ecsact::wasm::detail::minst_import_resolve_table;
using ecsact::wasm::detail::minst_trap;

namespace {
Expand Down Expand Up @@ -43,6 +46,10 @@ auto minst_import::kind() const -> wasm_externkind_enum {
);
}

auto minst_import::as_tabletype() const -> const wasm_tabletype_t* {
return wasm_externtype_as_tabletype_const(wasm_importtype_type(import_type));
}

auto minst_export::name() const -> std::string_view {
auto name = wasm_exporttype_name(export_type);
return std::string_view{
Expand Down Expand Up @@ -108,6 +115,21 @@ auto minst_import_resolve_func::as_extern( //
return wasm_func_as_extern(func);
}

auto minst_import_resolve_memory::as_extern( //
wasm_store_t* store
) -> wasm_extern_t* {
auto memory_type = wasm_memorytype_new(&memory_limits);
auto memory = wasm_memory_new(store, memory_type);
return wasm_memory_as_extern(memory);
}

auto minst_import_resolve_table::as_extern( //
wasm_store_t* store
) -> wasm_extern_t* {
auto* table = wasm_table_new(store, table_type, nullptr);
return wasm_table_as_extern(table);
}

auto minst_trap::message() const -> std::string {
auto trap_msg = wasm_message_t{};
wasm_trap_message(trap, &trap_msg);
Expand Down
28 changes: 26 additions & 2 deletions ecsact/si/wasmer/detail/minst/minst.hh
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,39 @@ struct minst_import_resolve_func {
auto as_extern(wasm_store_t* store) -> wasm_extern_t*;
};

using minst_import_resolve_t =
std::optional<std::variant<minst_import_resolve_func>>;
struct minst_import_resolve_memory {
wasm_limits_t memory_limits;

auto as_extern(wasm_store_t* store) -> wasm_extern_t*;
};

struct minst_import_resolve_table {
const wasm_tabletype_t* table_type;

auto as_extern(wasm_store_t* store) -> wasm_extern_t*;
};

struct minst_import_resolve_global {
// TODO

auto as_extern(wasm_store_t* store) -> wasm_extern_t*;
};

using minst_import_resolve_t = std::optional<std::variant<
minst_import_resolve_func,
minst_import_resolve_memory,
minst_import_resolve_table,
minst_import_resolve_global>>;

struct minst_import {
wasm_importtype_t* import_type;

auto name() const -> std::string_view;
auto module() const -> std::string_view;
auto kind() const -> wasm_externkind_enum;

// only valid when kind() == WASM_EXTERN_TABLE
auto as_tabletype() const -> const wasm_tabletype_t*;
};

struct minst_export {
Expand Down
6 changes: 3 additions & 3 deletions ecsact/si/wasmer/detail/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ ecsact_si_wasm_error ecsact_si_wasm_load(
auto module_name = imp.module();
auto method_name = imp.name();

if(imp.module() == "env") {
if(module_name == "env") {
auto itr = guest_env_module_imports.find(method_name);
if(itr == guest_env_module_imports.end()) {
return std::nullopt;
}
return itr->second();
return itr->second(imp);
}

if(imp.module() == "wasi_snapshot_preview1") {
if(module_name == "wasi_snapshot_preview1") {
auto itr = guest_wasi_module_imports.find(method_name);
if(itr == guest_wasi_module_imports.end()) {
return std::nullopt;
Expand Down