refactored scripting to hide actual script implementation
This commit is contained in:
parent
550d27273f
commit
a99906317d
14 changed files with 140 additions and 62 deletions
|
@ -1,28 +1,29 @@
|
|||
#ifndef PW_SCRIPTING_SCRIPT_HPP
|
||||
#define PW_SCRIPTING_SCRIPT_HPP
|
||||
|
||||
#include <pw/scripting/scripting.hpp>
|
||||
|
||||
#include <pw/core/globals.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace pw {
|
||||
|
||||
class script {
|
||||
protected:
|
||||
|
||||
scripting::state _state;
|
||||
scripting::table _namespace;
|
||||
|
||||
public:
|
||||
|
||||
script();
|
||||
~script();
|
||||
|
||||
bool load(const std::string &s);
|
||||
|
||||
int run(const std::string& s);
|
||||
|
||||
struct state {
|
||||
virtual int run(const std::string& script) = 0;
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
void load_modules();
|
||||
std::unique_ptr<state> _state;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -20,4 +20,11 @@ target_include_directories(
|
|||
../include
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
pwscripting
|
||||
PUBLIC
|
||||
${CMAKE_SOURCE_DIR}/src/deps/lua-5.3.4/src
|
||||
${CMAKE_SOURCE_DIR}/src/deps/sol
|
||||
)
|
||||
|
||||
target_link_libraries(pwscripting lua pwcore)
|
||||
|
|
|
@ -1,56 +1,101 @@
|
|||
#include "pw/scripting/script.hpp"
|
||||
|
||||
#include "pw/scripting/scripting.hpp"
|
||||
|
||||
#include "pw/core/vector.hpp"
|
||||
#include "pw/core/quaternion.hpp"
|
||||
#include "pw/core/axisangle.hpp"
|
||||
|
||||
|
||||
pw::script::script() {
|
||||
struct lua_state : public pw::script::state {
|
||||
|
||||
// create global pw namespace
|
||||
_namespace = _state.create_named_table("pw");
|
||||
pw::scripting::state _state;
|
||||
pw::scripting::table _namespace;
|
||||
|
||||
// add the script as a user type
|
||||
_namespace.new_usertype<script>("script",
|
||||
"initialize",&script::load_modules,
|
||||
"run",&script::run
|
||||
);
|
||||
|
||||
// add this instance as "script"
|
||||
_namespace.set("script",this);
|
||||
}
|
||||
void load_modules();
|
||||
|
||||
pw::script::~script()
|
||||
{
|
||||
_state.collect_garbage();
|
||||
}
|
||||
lua_state() {
|
||||
// create global pw namespace
|
||||
_namespace = _state.create_named_table("pw");
|
||||
|
||||
bool pw::script::load(const std::string &s)
|
||||
{
|
||||
return _state.load(s.c_str());
|
||||
}
|
||||
// add the script as a user type
|
||||
_namespace.new_usertype<lua_state>("script",
|
||||
"initialize",&lua_state::load_modules
|
||||
);
|
||||
|
||||
int pw::script::run(const std::string &s)
|
||||
// add this instance as "script"
|
||||
_namespace.set("script",this);
|
||||
}
|
||||
|
||||
virtual int run(const std::string &s);
|
||||
|
||||
};
|
||||
|
||||
int lua_state::run(const std::string &s)
|
||||
{
|
||||
return _state.script(s.c_str()).get<int>();
|
||||
}
|
||||
|
||||
void pw::script::load_modules() {
|
||||
void lua_state::load_modules() {
|
||||
|
||||
// open all libraries
|
||||
_state.open_libraries();
|
||||
|
||||
typedef double Scalar;
|
||||
|
||||
_namespace.set("pi",Pi<Scalar>());
|
||||
_namespace.set("pi",pw::Pi<Scalar>());
|
||||
|
||||
#if 0
|
||||
_namespace.new_usertype<vector3d>("vector3",
|
||||
"set",&vector3d::set,
|
||||
"x", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::x), &vector3d::set_x),
|
||||
"y", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::y), &vector3d::set_y),
|
||||
"z", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::z), &vector3d::set_z),
|
||||
"v",&vector3d::values
|
||||
"v",&vector3d::values,
|
||||
"clone",&vector3d::clone
|
||||
);
|
||||
|
||||
_namespace.new_usertype<quaterniond>("quaternion",
|
||||
"set",&quaterniond::set,
|
||||
"x", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::x), &quaterniond::set_x),
|
||||
"y", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::y), &quaterniond::set_y),
|
||||
"z", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::z), &quaterniond::set_z),
|
||||
"w", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::w), &quaterniond::set_w)
|
||||
// "v",&vector3d::values,
|
||||
// "clone",&vector3d::clone
|
||||
);
|
||||
|
||||
_namespace.new_usertype<axisangled>("axisangle",
|
||||
"axis",scripting::property(&axisangled::axis,&axisangled::set_axis),
|
||||
"angle",scripting::property(&axisangled::angle,&axisangled::set_angle)
|
||||
);
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
pw::script::script() {
|
||||
}
|
||||
|
||||
pw::script::~script()
|
||||
{
|
||||
// _state.collect_garbage();
|
||||
}
|
||||
|
||||
//bool pw::script::load(const std::string &s)
|
||||
//{
|
||||
// return _state.load(s.c_str());
|
||||
//}
|
||||
|
||||
int pw::script::run(const std::string &s)
|
||||
{
|
||||
return _state->run(s);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue