From ef84160a5d7a984d890d4a73ae08c9bbf2793b5f Mon Sep 17 00:00:00 2001 From: halx99 Date: Mon, 21 Jul 2025 14:21:18 +0800 Subject: [PATCH] Fix example lua --- examples/lua/main.cpp | 23 ++++++++++++++++------- yasio/bindings/lyasio.cpp | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/examples/lua/main.cpp b/examples/lua/main.cpp index ca216fcb..a0d8b66d 100644 --- a/examples/lua/main.cpp +++ b/examples/lua/main.cpp @@ -43,12 +43,20 @@ int main(int argc, char** argv) std::string new_package_path = s["package"]["path"]; - sol::function function = s.script_file("scripts/example.lua"); - - do + try + { + sol::table module_example = s.load_file("scripts/example.lua").call(); + sol::function update_func = module_example["update"]; + do + { + std::this_thread::sleep_for(std::chrono::milliseconds(16)); + } while (!update_func(50.0 / 1000)); + } + catch (const sol::error& e) { - std::this_thread::sleep_for(std::chrono::milliseconds(16)); - } while (!function.call(50.0 / 1000)); + std::cerr << "Error: " << e.what() << std::endl; + return -1; + } #else kaguya::State s; s.openlibs(); @@ -65,11 +73,12 @@ int main(int argc, char** argv) s["package"]["path"] = package_path; auto function = s.loadfile("scripts/example.lua"); - auto update = function(); + auto module_example = function(); + auto update_func = module_example["update"]; do { std::this_thread::sleep_for(std::chrono::milliseconds(50)); - } while (!update(50.0 / 1000)); + } while (!update_func(50.0 / 1000)); #endif return 0; } diff --git a/yasio/bindings/lyasio.cpp b/yasio/bindings/lyasio.cpp index 080a7d45..fd4b7858 100644 --- a/yasio/bindings/lyasio.cpp +++ b/yasio/bindings/lyasio.cpp @@ -795,7 +795,7 @@ end char version[32]; snprintf(version, sizeof(version), "%x.%x.%x", (YASIO_VERSION_NUM >> 16) & 0xff, (YASIO_VERSION_NUM >> 8) & 0xff, YASIO_VERSION_NUM & 0xff); - YASIO_EXPORT_ANY(version); + yasio_lib["version"] = std::string(version); return yasio_lib.push(); /* return 'yasio' table */ }