attempting to cleanup the scripting interface

This commit is contained in:
Hartmut Seichter 2019-01-23 09:05:09 +01:00
parent a497ac1f7d
commit 61f8e6c071
15 changed files with 215 additions and 227 deletions

View file

@ -53,5 +53,5 @@ int main(int argc,const char** argv) {
pw::script s;
return s.run(sout.str().c_str());
return s.eval(sout.str().c_str());
}

View file

@ -5,15 +5,16 @@ set(hdrs
set(srcs
src/script.cpp
src/script_core.hpp
# src/script_core.hpp
src/script_core.cpp
src/script_system.hpp
# src/script_system.hpp
src/script_system.cpp
src/script_scene.hpp
# src/script_scene.hpp
src/script_scene.cpp
src/script_visual.hpp
# src/script_visual.hpp
src/script_visual.cpp
src/scripting.hpp
src/runtime_lua.hpp
src/runtime_lua.cpp
)
add_library(pwscripting

View file

@ -1,10 +1,30 @@
/*
* Copyright (c) 1999-2019 Hartmut Seichter
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifndef PW_SCRIPTING_SCRIPT_HPP
#define PW_SCRIPTING_SCRIPT_HPP
#include <pw/core/globals.hpp>
#include <string>
namespace pw {
class script {
@ -13,7 +33,7 @@ public:
script();
~script();
int run(const std::string& s);
int eval(const std::string& s);
protected:

View file

@ -0,0 +1,64 @@
#ifndef PW_SCRIPTING_RUNTIME_LUA_HPP
#define PW_SCRIPTING_RUNTIME_LUA_HPP
#include "sol/sol.hpp"
#include <lua.hpp>
#include <map>
namespace pw {
class runtime_lua {
public:
typedef std::function<void(sol::state&,sol::table&)> register_function_t;
typedef std::map<std::string,register_function_t> register_func_list_t;
static runtime_lua& get();
register_func_list_t register_functions() { return _register_function_list; }
void add(const std::string& name,register_function_t f);
protected:
runtime_lua();
register_func_list_t _register_function_list;
};
template<typename Derived>
struct runtime_lua_register
{
Derived& derived() { return static_cast<Derived&>(*this); }
const Derived& derived() const { return static_cast<const Derived&>(*this); }
runtime_lua_register()
{
printf("Test\n");
runtime_lua::get().add(derived().module_name(),&Derived::register_function);
}
static runtime_lua_register<Derived> _instance;
};
extern "C" {
typedef void (* CModuleFunction) (void);
}
struct ModuleFunctionProxy
{
ModuleFunctionProxy(CModuleFunction function) { (function)(); }
};
}
#define PW_RUNTIME_LUA_USE(name) \
extern "C" void pw_runtime_lua_##name(void) { printf("Meh!\n"); };
#define PW_RUNTIME_LUA_REGISTER(name) \
extern "C" void pw_runtime_lua_##name(void); \
static pw::ModuleFunctionProxy proxy_##name(pw_runtime_lua_##name);
#endif

View file

@ -1,11 +1,6 @@
#include "pw/scripting/script.hpp"
#include "scripting.hpp"
//#include "script_core.hpp"
//#include "script_system.hpp"
//#include "script_scene.hpp"
//#include "script_visual.hpp"
#include "runtime_lua.hpp"
#include "pw/core/debug.hpp"
@ -16,24 +11,11 @@ struct script::state {
sol::state _state;
sol::table _namespace;
state();
void load_modules();
void load(const std::string& name);
state() {
// create global pw namespace
_namespace = _state.create_named_table("pw");
// add the script as a user type
_namespace.new_usertype<state>("script",
"initialize",&state::load_modules,
"load",&state::load
);
// add this instance as "script"
_namespace.set("script",this);
}
int run(const std::string &s);
};
@ -45,28 +27,36 @@ int script::state::run(const std::string &s)
void script::state::load(const std::string& name)
{
lua_runtime::register_function_list[name](_state,_namespace);
runtime_lua::get().register_functions()[name](_state,_namespace);
}
script::state::state()
{
// create global pw namespace
_namespace = _state.create_named_table("pw");
// add the script as a user type
_namespace.new_usertype<state>("script",
"initialize",&state::load_modules,
"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() {
// open all libraries
_state.open_libraries();
for (auto lib_reg : lua_runtime::register_function_list) {
lib_reg.second(_state,_namespace);
debug::d() << lib_reg.first;
}
// script_core::load(_namespace);
// script_system::load(_namespace);
// script_scene::load(_namespace);
// script_visual::load(_namespace);
}
//
//
// script
//
script::script()
@ -76,13 +66,11 @@ script::script()
script::~script()
{
_state = nullptr;
}
int script::run(const std::string &s)
int script::eval(const std::string &s)
{
return _state->run(s);
}

View file

@ -1,5 +1,3 @@
#include "script_core.hpp"
#include "pw/core/vector.hpp"
#include "pw/core/quaternion.hpp"
#include "pw/core/axisangle.hpp"
@ -9,12 +7,16 @@
#include "pw/core/timer.hpp"
#include "pw/core/mesh.hpp"
#include "scripting.hpp"
#include "runtime_lua.hpp"
namespace pw {
void lua_register_core(sol::state& lua,sol::table& ns)
{
struct lua_core : runtime_lua_register<lua_core> {
std::string module_name() const { return "core"; }
void register_function(sol::state& lua,sol::table& ns)
{
typedef real_t Scalar;
@ -92,23 +94,18 @@ void lua_register_core(sol::state& lua,sol::table& ns)
sol::constructors<mesh()>()
);
// lua["mesh"].new_enum()
// (
// "mesh",
// sol::constructors<mesh()>()
// );
}
struct _register_core {
static _register_core& get() {
lua_runtime::register_function_list["core"] = lua_register_core;
static _register_core instance;
return instance;
// lua["mesh"].new_enum()
// (
// "mesh",
// sol::constructors<mesh()>()
// );
}
};
};
PW_RUNTIME_LUA_REGISTER(core)
}

View file

@ -1,17 +0,0 @@
#ifndef PW_SCRIPTING_PRIVATE_CORE_HPP
#define PW_SCRIPTING_PRIVATE_CORE_HPP
#include "scripting.hpp"
namespace pw {
struct script_core {
static void load(sol::table& ns);
};
}
#endif

View file

@ -1,10 +1,11 @@
#include "script_scene.hpp"
//#include "script_scene.hpp"
#include <pw/scene/node.hpp>
#include "pw/scene/node.hpp"
#include "runtime_lua.hpp"
namespace pw {
void lua_register_scene(sol::table &ns)
void lua_register_scene(sol::state& lua,sol::table &ns)
{
ns.new_usertype<node>("node",

View file

@ -1,17 +0,0 @@
#ifndef PW_SCRIPTING_PRIVATE_SCENE_HPP
#define PW_SCRIPTING_PRIVATE_SCENE_HPP
#include "scripting.hpp"
namespace pw {
//struct script_scene {
// static void load(scripting::table& ns);
//};
}
#endif

View file

@ -1,9 +1,11 @@
#include "script_system.hpp"
//#include "script_system.hpp"
#include "pw/system/window.hpp"
#include "pw/system/input.hpp"
#include "pw/system/display.hpp"
#include "runtime_lua.hpp"
#include "pw/core/debug.hpp"
namespace pw {

View file

@ -1,17 +0,0 @@
#ifndef PW_SCRIPTING_PRIVATE_SYSTEM_HPP
#define PW_SCRIPTING_PRIVATE_SYSTEM_HPP
#include "scripting.hpp"
namespace pw {
//struct script_system {
// static void load(scripting::table& ns);
//};
}
#endif

View file

@ -1,8 +1,9 @@
#include "script_visual.hpp"
#include "pw/core/debug.hpp"
#include "pw/visual/pipeline.hpp"
#include "runtime_lua.hpp"
namespace pw {
void lua_register_visual(sol::table &ns)

View file

@ -1,17 +0,0 @@
#ifndef PW_SCRIPTING_PRIVATE_VISUAL_HPP
#define PW_SCRIPTING_PRIVATE_VISUAL_HPP
#include "scripting.hpp"
namespace pw {
//struct script_visual {
// static void load(scripting::table& ns);
//};
}
#endif

View file

@ -1,22 +0,0 @@
#ifndef PW_SCRIPTING_SCRIPTING_HPP
#define PW_SCRIPTING_SCRIPTING_HPP
#include "sol/sol.hpp"
#include <lua.hpp>
#include <map>
namespace pw {
struct lua_runtime {
typedef std::function<void(sol::state&,sol::table&)> register_function_t;
typedef std::map<std::string,register_function_t> register_func_list_t;
static register_func_list_t register_function_list;
};
lua_runtime::register_func_list_t lua_runtime::register_function_list = lua_runtime::register_func_list_t();
}
#endif

View file

@ -1,7 +1,11 @@
--
-- pixwerx - test - core
--
-- loading our libraries
pw.script:initialize()
print("hello pixwerx!")
os.exit()
local v1 = pw.vector3.new(3,2,1)