From 6ee37329949c9cbcd4ae3b3a559c3eb96de31d52 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Thu, 24 Jan 2019 09:48:34 +0100 Subject: [PATCH] testing other registration methods for the registry --- src/core/src/core.cpp | 34 +++- src/engine/pixwerx.cpp | 4 +- src/geometry/src/primitives.cpp | 2 +- src/scripting/include/pw/scripting/script.hpp | 3 + src/scripting/src/runtime_lua.cpp | 1 + src/scripting/src/runtime_lua.hpp | 15 +- src/scripting/src/script.cpp | 21 +- src/scripting/src/script_core.cpp | 182 +++++++++--------- src/scripts/tests/test_core.lua | 3 +- 9 files changed, 139 insertions(+), 126 deletions(-) diff --git a/src/core/src/core.cpp b/src/core/src/core.cpp index ed9a32e..0a76adb 100644 --- a/src/core/src/core.cpp +++ b/src/core/src/core.cpp @@ -1,20 +1,34 @@ +#include "pw/core/core.hpp" +#include "pw/core/debug.hpp" -//#include +namespace pw { -//#include -//#include +template struct module { + + module() { + debug::d() << __PRETTY_FUNCTION__; + } + + struct proxy { + proxy() { + debug::d() << __PRETTY_FUNCTION__; + } + }; + + static proxy _proxy; + +}; + +template typename module::proxy module::_proxy; -#include "pw/core/matrixbase.hpp" -//#include "pw/core/matrix.hpp" +struct stuff { + +}; -void test_matrixbase() { - using namespace pw; +module mod; -// matrix4x4f m; - -// m.set_identity(); } diff --git a/src/engine/pixwerx.cpp b/src/engine/pixwerx.cpp index 0ca884b..78d871c 100644 --- a/src/engine/pixwerx.cpp +++ b/src/engine/pixwerx.cpp @@ -21,8 +21,6 @@ int main(int argc,const char** argv) { -// pw_runtime_lua_core(); - argagg::parser argparser {{ { "help", {"-h", "--help"}, "shows this help message", 0}, @@ -58,6 +56,8 @@ int main(int argc,const char** argv) { input.close(); + pw::script::initialize(); + pw::script s; return s.eval(sout.str().c_str()); diff --git a/src/geometry/src/primitives.cpp b/src/geometry/src/primitives.cpp index 01248d3..70e123c 100644 --- a/src/geometry/src/primitives.cpp +++ b/src/geometry/src/primitives.cpp @@ -80,7 +80,7 @@ mesh primitives::sphere(real_t radius,int divisions_latitude,int divisions_longi } } - + return geom; } } diff --git a/src/scripting/include/pw/scripting/script.hpp b/src/scripting/include/pw/scripting/script.hpp index ac6645a..8123757 100644 --- a/src/scripting/include/pw/scripting/script.hpp +++ b/src/scripting/include/pw/scripting/script.hpp @@ -30,11 +30,14 @@ namespace pw { class script { public: + script(); ~script(); int eval(const std::string& s); + static void initialize(); + protected: struct state; diff --git a/src/scripting/src/runtime_lua.cpp b/src/scripting/src/runtime_lua.cpp index f06b86f..c31a556 100644 --- a/src/scripting/src/runtime_lua.cpp +++ b/src/scripting/src/runtime_lua.cpp @@ -17,4 +17,5 @@ runtime_lua::runtime_lua() } + } diff --git a/src/scripting/src/runtime_lua.hpp b/src/scripting/src/runtime_lua.hpp index 6bc02b9..c711259 100644 --- a/src/scripting/src/runtime_lua.hpp +++ b/src/scripting/src/runtime_lua.hpp @@ -29,22 +29,9 @@ protected: }; -template -struct runtime_lua_register -{ - runtime_lua_register() - { - debug::d() << __PRETTY_FUNCTION__; - runtime_lua::get().add("test",&T::register_function); - } - - static runtime_lua_register _instance; -}; - -template runtime_lua_register runtime_lua_register::_instance; +void register_core_lua(); } - #endif diff --git a/src/scripting/src/script.cpp b/src/scripting/src/script.cpp index ac90cd3..8666e1b 100644 --- a/src/scripting/src/script.cpp +++ b/src/scripting/src/script.cpp @@ -15,7 +15,7 @@ struct script::state { state(); - void load_modules(); + void load_all(); void load(const std::string& name); int run(const std::string &s); @@ -29,7 +29,8 @@ int script::state::run(const std::string &s) void script::state::load(const std::string& name) { - runtime_lua::get().register_functions()[name](_state,_namespace); + auto f = runtime_lua::get().register_functions()[name]; + f(_state,_namespace); } script::state::state() @@ -40,21 +41,24 @@ script::state::state() // add the script as a user type _namespace.new_usertype("script", - "initialize",&state::load_modules, + "initialize",&state::load_all, "load",&state::load ); // add this instance as "script" _namespace.set("script",this); - debug::d() << "available " << runtime_lua::get().register_functions().size(); - } -void script::state::load_modules() { +void script::state::load_all() { // open all libraries _state.open_libraries(); + + // + for (auto m : runtime_lua::get().register_functions()) { + m.second(_state,_namespace); + } } // @@ -75,6 +79,11 @@ int script::eval(const std::string &s) return _state->run(s); } +void script::initialize() +{ + register_core_lua(); +} + } diff --git a/src/scripting/src/script_core.cpp b/src/scripting/src/script_core.cpp index 00400bc..725ab2b 100644 --- a/src/scripting/src/script_core.cpp +++ b/src/scripting/src/script_core.cpp @@ -11,100 +11,98 @@ namespace pw { -struct lua_core { - std::string module_name() const { return "core"; } - - void register_function(sol::state& lua,sol::table& ns) - { - - typedef real_t Scalar; - - ns.set("pi",pw::pi()); - -// ns.new_usertype -// ( -// "vector3", -// sol::constructors(), -// "x", sol::property(sol::resolve(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;}), -// "y", sol::property(sol::resolve(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;}), -// "z", sol::property(sol::resolve(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;}), -// "cross",&vector3::cross, -// "lerp",&vector3::lerp -// ); - - - -// ns.new_usertype -// ( -// "quaternion", -// sol::constructors(), -// "x", sol::property(sol::resolve(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;}), -// "y", sol::property(sol::resolve(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;}), -// "z", sol::property(sol::resolve(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;}), -// "w", sol::property(sol::resolve(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}), -// "identity",sol::readonly_property(&quaternion::identity), -// "dot",&quaternion::dot, -// "inverse",sol::readonly_property(&quaternion::inverse), -// "normalized",&quaternion::normalized, -// "lerp",&quaternion::lerp, -// "slerp",&quaternion::slerp -// ); - - ns.new_usertype - ("axisangle", - sol::constructors(), - "axis",sol::property(&axisangle::axis,&axisangle::set_axis), - "angle",sol::property(&axisangle::angle,&axisangle::set_angle) - ); - - - ns.new_usertype("size", - sol::constructors(), - "width",&size::width, - "height",&size::height - // "none",sol::debug::level::none - ); - - ns.new_usertype("point", - sol::constructors(), - "x",&point::x, - "y",&point::y - ); - - - ns.new_usertype("debug", - "new",sol::no_constructor, - "get",&debug::get, - "write",&debug::write - // "none",sol::debug::level::none - ); - - - ns.new_usertype("timer", - "now",sol::readonly_property(&timer::now), - "elapsed",sol::readonly_property(&timer::elapsed), - "reset",&timer::reset - ); - - ns.new_usertype - ( - "mesh", - sol::constructors() - ); - - // lua["mesh"].new_enum() - // ( - // "mesh", - // sol::constructors() - // ); - } -}; - -struct lua_core_register : runtime_lua_register +void register_core_function(sol::state& lua,sol::table& ns) { -}; -//PW_RUNTIME_LUA_REGISTER(core) + debug::d() << "registering "; + typedef real_t Scalar; + + ns.set("pi",pw::pi()); + + ns.new_usertype + ( + "vector3", + sol::constructors(), + "x", sol::property(sol::resolve(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;}), + "y", sol::property(sol::resolve(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;}), + "z", sol::property(sol::resolve(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;}), + "cross",&vector3::cross, + "lerp",&vector3::lerp + ); + + + + ns.new_usertype + ( + "quaternion", + sol::constructors(), + "x", sol::property(sol::resolve(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;}), + "y", sol::property(sol::resolve(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;}), + "z", sol::property(sol::resolve(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;}), + "w", sol::property(sol::resolve(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}), + "identity",sol::readonly_property(&quaternion::identity), + "dot",&quaternion::dot, + "inverse",sol::readonly_property(&quaternion::inverse), + "normalized",&quaternion::normalized, + "lerp",&quaternion::lerp, + "slerp",&quaternion::slerp + ); + + ns.new_usertype + ("axisangle", + sol::constructors(), + "axis",sol::property(&axisangle::axis,&axisangle::set_axis), + "angle",sol::property(&axisangle::angle,&axisangle::set_angle) + ); + + + ns.new_usertype("size", + sol::constructors(), + "width",&size::width, + "height",&size::height + // "none",sol::debug::level::none + ); + + ns.new_usertype("point", + sol::constructors(), + "x",&point::x, + "y",&point::y + ); + + + ns.new_usertype("debug", + "new",sol::no_constructor, + "get",&debug::get, + "write",&debug::write + // "none",sol::debug::level::none + ); + + + ns.new_usertype("timer", + "now",sol::readonly_property(&timer::now), + "elapsed",sol::readonly_property(&timer::elapsed), + "reset",&timer::reset + ); + + ns.new_usertype + ( + "mesh", + sol::constructors() + ); + + // lua["mesh"].new_enum() + // ( + // "mesh", + // sol::constructors() + // ); +} + + + +void register_core_lua() +{ + runtime_lua::get().add("core",register_core_function); +} } diff --git a/src/scripts/tests/test_core.lua b/src/scripts/tests/test_core.lua index 5658363..962760b 100644 --- a/src/scripts/tests/test_core.lua +++ b/src/scripts/tests/test_core.lua @@ -5,7 +5,8 @@ -- loading our libraries pw.script:initialize() -os.exit() +pw.script:load("core") + local v1 = pw.vector3.new(3,2,1)