diff --git a/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMAbilityRegistry.cpp b/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMAbilityRegistry.cpp new file mode 100644 index 0000000..0b01454 --- /dev/null +++ b/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMAbilityRegistry.cpp @@ -0,0 +1,13 @@ +#include "WASMAbilityRegistry.hpp" +#include "../WASMHelperFile.hpp" +#include "wasm.h" + +using namespace CreatureLib::Library; +using namespace PkmnLib::Library; + +void WASMAbilityRegistry::Register(ArbUt::Dictionary& externs, + WebAssemblyScriptResolver* resolver) { + REGISTER_GETTER("ability_get_name", Talent, GetName, resolver); + REGISTER_GETTER("ability_get_effect", Talent, GetEffect, resolver); + REGISTER_GETTER("ability_get_parameters", Talent, GetParameters, resolver); +} diff --git a/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMAbilityRegistry.hpp b/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMAbilityRegistry.hpp new file mode 100644 index 0000000..75e4837 --- /dev/null +++ b/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMAbilityRegistry.hpp @@ -0,0 +1,10 @@ +#ifndef PKMNLIB_WASMABILITYREGISTRY_HPP +#define PKMNLIB_WASMABILITYREGISTRY_HPP +#include "../../WebAssemblyScriptResolver.hpp" + +class WASMAbilityRegistry { +public: + static void Register(ArbUt::Dictionary& externs, WebAssemblyScriptResolver* resolver); +}; + +#endif // PKMNLIB_WASMABILITYREGISTRY_HPP diff --git a/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMEffectParameter.cpp b/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMEffectParameter.cpp new file mode 100644 index 0000000..612e105 --- /dev/null +++ b/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMEffectParameter.cpp @@ -0,0 +1,16 @@ +#include "WASMEffectParameter.hpp" +#include "../../../../Battling/Library/BattleLibrary.hpp" +#include "../../WebAssemblyScriptResolver.hpp" +#include "../WASMHelperFile.hpp" +#include "wasm.h" + +using namespace CreatureLib::Library; + +void WASMEffectParameter::Register(ArbUt::Dictionary& externs, + WebAssemblyScriptResolver* resolver) { + REGISTER_GETTER("effect_parameter_get_type", EffectParameter, GetType, resolver) + REGISTER_GETTER("effect_parameter_as_bool", EffectParameter, AsBool, resolver) + REGISTER_GETTER("effect_parameter_as_int", EffectParameter, AsInt, resolver) + REGISTER_GETTER("effect_parameter_as_float", EffectParameter, AsFloat, resolver) + REGISTER_GETTER("effect_parameter_as_string", EffectParameter, AsString, resolver) +} diff --git a/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMEffectParameter.hpp b/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMEffectParameter.hpp new file mode 100644 index 0000000..2793ed0 --- /dev/null +++ b/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMEffectParameter.hpp @@ -0,0 +1,10 @@ +#ifndef PKMNLIB_WASMEFFECTPARAMETER_HPP +#define PKMNLIB_WASMEFFECTPARAMETER_HPP +#include "../../WebAssemblyScriptResolver.hpp" + +class WASMEffectParameter { +public: + static void Register(ArbUt::Dictionary& externs, WebAssemblyScriptResolver* resolver); +}; + +#endif // PKMNLIB_WASMEFFECTPARAMETER_HPP diff --git a/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMFormeRegistry.hpp b/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMFormeRegistry.hpp index 9dd74cc..3ccdd55 100644 --- a/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMFormeRegistry.hpp +++ b/src/ScriptResolving/WASM/InterfaceMethods/Library/WASMFormeRegistry.hpp @@ -7,8 +7,7 @@ class WebAssemblyScriptResolver; class WASMFormeRegistry { public: - static void Register(ArbUt::Dictionary& externs, - WebAssemblyScriptResolver* resolver); + static void Register(ArbUt::Dictionary& externs, WebAssemblyScriptResolver* resolver); }; #endif // PKMNLIB_WASMFORMEREGISTRY_HPP diff --git a/src/ScriptResolving/WASM/InterfaceMethods/TypeRegistry.hpp b/src/ScriptResolving/WASM/InterfaceMethods/TypeRegistry.hpp index c27fdd9..664c563 100644 --- a/src/ScriptResolving/WASM/InterfaceMethods/TypeRegistry.hpp +++ b/src/ScriptResolving/WASM/InterfaceMethods/TypeRegistry.hpp @@ -5,11 +5,14 @@ #include "../WebAssemblyScriptResolver.hpp" #include "Arbutils/Collections/Dictionary.hpp" #include "Library/LibraryMethods.hpp" +#include "Library/WASMAbilityRegistry.hpp" +#include "Library/WASMEffectParameter.hpp" +#include "Library/WASMFormeRegistry.hpp" #include "Library/WASMItemRegistry.hpp" #include "Library/WASMMoveDataRegistry.hpp" #include "Library/WASMSpeciesRegistry.hpp" -#include "Library/WASMFormeRegistry.hpp" #include "WASMCoreMethods.hpp" +#include "WASMListRegistry.hpp" #include "WASMStringView.hpp" class TypeRegistry { @@ -17,12 +20,15 @@ public: static void Register(ArbUt::Dictionary& externs, WebAssemblyScriptResolver* resolver) { WASMCoreMethods::Register(externs, resolver); WASMStringView::Register(externs, resolver); + WASMListRegistry::Register(externs, resolver); + LibraryMethods::Register(externs, resolver); + WASMEffectParameter::Register(externs, resolver); WASMMoveDataRegistry::Register(externs, resolver); WASMItemRegistry::Register(externs, resolver); WASMSpeciesRegistry::Register(externs, resolver); WASMFormeRegistry::Register(externs, resolver); - + WASMAbilityRegistry::Register(externs, resolver); } }; diff --git a/src/ScriptResolving/WASM/InterfaceMethods/WASMHelperFile.hpp b/src/ScriptResolving/WASM/InterfaceMethods/WASMHelperFile.hpp index a6f2cd1..95af186 100644 --- a/src/ScriptResolving/WASM/InterfaceMethods/WASMHelperFile.hpp +++ b/src/ScriptResolving/WASM/InterfaceMethods/WASMHelperFile.hpp @@ -1,6 +1,7 @@ #ifndef PKMNLIB_HELPERFILE_H #define PKMNLIB_HELPERFILE_H #include +#include #include #include #include @@ -122,8 +123,10 @@ private: } } else if constexpr (std::is_same()) { return wasm_valtype_new_i64(); + } else if constexpr (is_specialization()) { + return wasm_valtype_new_i64(); } - THROW("Unhandled value type: ", std::string(typeid(T).name())); + THROW("Unhandled value type: ", std::string(abi::__cxa_demangle(typeid(T).name(), 0, 0, 0))); } template inline static wasm_val_t ToVal(const T& val) { @@ -150,6 +153,9 @@ private: } else if constexpr (std::is_same()) { auto v = &val; return WASM_I64_VAL(reinterpret_cast(v)); + } else if constexpr (is_specialization()) { + auto v = &val; + return WASM_I64_VAL(reinterpret_cast(v)); } THROW("Unhandled value type: ", typeid(T).name()); } diff --git a/src/ScriptResolving/WASM/InterfaceMethods/WASMListRegistry.cpp b/src/ScriptResolving/WASM/InterfaceMethods/WASMListRegistry.cpp new file mode 100644 index 0000000..eab5e08 --- /dev/null +++ b/src/ScriptResolving/WASM/InterfaceMethods/WASMListRegistry.cpp @@ -0,0 +1,20 @@ +#include "WASMListRegistry.hpp" +#include "WASMHelperFile.hpp" + +wasm_func_t* List_GetLength(WebAssemblyScriptResolver* resolver) { + return WasmHelpers::CreateFunc*>( + resolver, {[](WebAssemblyScriptResolver*, const ArbUt::List* list) -> size_t { return list->Count(); }}); +} + +wasm_func_t* List_GetValue(WebAssemblyScriptResolver* resolver) { + return WasmHelpers::CreateFunc*, size_t>( + resolver, {[](WebAssemblyScriptResolver*, const ArbUt::List* list, size_t index) -> void* { + return list->At(index); + }}); +} + +void WASMListRegistry::Register(ArbUt::Dictionary& externs, + WebAssemblyScriptResolver* resolver) { + externs.Insert("list_get_length", List_GetLength(resolver)); + externs.Insert("list_get_at", List_GetValue(resolver)); +} diff --git a/src/ScriptResolving/WASM/InterfaceMethods/WASMListRegistry.hpp b/src/ScriptResolving/WASM/InterfaceMethods/WASMListRegistry.hpp new file mode 100644 index 0000000..b327356 --- /dev/null +++ b/src/ScriptResolving/WASM/InterfaceMethods/WASMListRegistry.hpp @@ -0,0 +1,10 @@ +#ifndef PKMNLIB_WASMLISTREGISTRY_HPP +#define PKMNLIB_WASMLISTREGISTRY_HPP +#include "../WebAssemblyScriptResolver.hpp" + +class WASMListRegistry { +public: + static void Register(ArbUt::Dictionary& externs, WebAssemblyScriptResolver* resolver); +}; + +#endif // PKMNLIB_WASMLISTREGISTRY_HPP diff --git a/src/ScriptResolving/WASM/InterfaceMethods/WASMStringView.cpp b/src/ScriptResolving/WASM/InterfaceMethods/WASMStringView.cpp index 4628c17..e0fcab6 100644 --- a/src/ScriptResolving/WASM/InterfaceMethods/WASMStringView.cpp +++ b/src/ScriptResolving/WASM/InterfaceMethods/WASMStringView.cpp @@ -23,6 +23,6 @@ wasm_func_t* ConstString_GetStr(WebAssemblyScriptResolver* resolver) { void WASMStringView::Register(ArbUt::Dictionary& externs, WebAssemblyScriptResolver* resolver) { - externs.Insert("arbutils_const_string_get_hash", ConstString_GetHash(resolver)); - externs.Insert("arbutils_const_string_get_str", ConstString_GetStr(resolver)); + externs.Insert("const_string_get_hash", ConstString_GetHash(resolver)); + externs.Insert("const_string_get_str", ConstString_GetStr(resolver)); } diff --git a/src/ScriptResolving/WASM/InterfaceMethods/pkmn_lib.wit b/src/ScriptResolving/WASM/InterfaceMethods/pkmn_lib_host.wit similarity index 93% rename from src/ScriptResolving/WASM/InterfaceMethods/pkmn_lib.wit rename to src/ScriptResolving/WASM/InterfaceMethods/pkmn_lib_host.wit index a60bb5d..c17bea1 100644 --- a/src/ScriptResolving/WASM/InterfaceMethods/pkmn_lib.wit +++ b/src/ScriptResolving/WASM/InterfaceMethods/pkmn_lib_host.wit @@ -1,4 +1,5 @@ -// This file documents the entire interface API currently used by the WebAssembly script resolver. +// This file documents the entire Host interface API currently used by the WebAssembly script resolver, so the methods +// that can be used from the client wasm. // Currently, we only define a bunch of functions, but in the future when interface types are actually implemented, // we'd like to implement those as well. @@ -11,10 +12,15 @@ _print: function(i32 message_pointer, s32 message_length) // StringView class type const_string = u64; -arbutils_const_string_get_hash: function(const_string ptr) -> u32 +const_string_get_hash: function(const_string ptr) -> u32 // Returns a pointer to the WASM memory where the string can be found. // NOTE: This requires us to copy the string into WASM memory. -arbutils_const_string_get_str: function(const_string ptr) -> s32 +const_string_get_str: function(const_string ptr) -> s32 + +// List class +type list = u64 +list_get_length: function(list) -> u64 +list_get_at: function(list, u64) -> u64 // Library types type pokemon_library = u64 @@ -149,5 +155,4 @@ forme_get_hidden_ability_count: function(forme) -> u64 forme_get_ability: function(forme, bool hidden, u8 index) -> ability forme_has_flag_by_hash: function(forme, u32) -> bool - - +// Ability class diff --git a/src/ScriptResolving/WASM/WebAssemblyBattleScript.cpp b/src/ScriptResolving/WASM/WebAssemblyBattleScript.cpp index faa276e..63d14bf 100644 --- a/src/ScriptResolving/WASM/WebAssemblyBattleScript.cpp +++ b/src/ScriptResolving/WASM/WebAssemblyBattleScript.cpp @@ -22,15 +22,16 @@ WebAssemblyBattleScript::~WebAssemblyBattleScript() { if (!funcOpt.has_value()) { \ return; \ } \ - auto& func = funcOpt.value(); \ + auto& func = funcOpt.value(); \ parameter_setup; \ func.Call(); void WebAssemblyBattleScript::OnInitialize(const CreatureLib::Battling::BattleLibrary* library, - const ArbUt::List&) { - WASM_CALL(Initialize, "script_on_initialize", 2, 0, { + const ArbUt::List& parameters) { + WASM_CALL(Initialize, "script_on_initialize", 3, 0, { func.Loadi32(0, _wasmPtr); func.LoadExternRef(1, library); + func.LoadExternRef(2, ¶meters); }); } diff --git a/tests/ScriptTests/WASM/gen7_scripts_rs.wasm b/tests/ScriptTests/WASM/gen7_scripts_rs.wasm index 7ef8ada..b4e0875 100755 Binary files a/tests/ScriptTests/WASM/gen7_scripts_rs.wasm and b/tests/ScriptTests/WASM/gen7_scripts_rs.wasm differ