LuaScriptingContext.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * LuaScriptingContext.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "LuaWrapper.h"
  12. #include "LuaReference.h"
  13. #include "../../lib/ScriptHandler.h"
  14. #include "../../lib/CScriptingModule.h"
  15. namespace scripting
  16. {
  17. class LuaContext : public ContextBase
  18. {
  19. public:
  20. static const std::string STATE_FIELD;
  21. LuaContext(const Script * source, const Environment * env_);
  22. virtual ~LuaContext();
  23. void run(const JsonNode & initialState) override;
  24. void run(ServerCallback * server, const JsonNode & initialState) override;
  25. //log error and return nil from LuaCFunction
  26. int errorRetVoid(const std::string & message);
  27. JsonNode callGlobal(const std::string & name, const JsonNode & parameters) override;
  28. JsonNode callGlobal(ServerCallback * cb, const std::string & name, const JsonNode & parameters) override;
  29. void getGlobal(const std::string & name, int & value) override;
  30. void getGlobal(const std::string & name, std::string & value) override;
  31. void getGlobal(const std::string & name, double & value) override;
  32. void getGlobal(const std::string & name, JsonNode & value) override;
  33. void setGlobal(const std::string & name, int value) override;
  34. void setGlobal(const std::string & name, const std::string & value) override;
  35. void setGlobal(const std::string & name, double value) override;
  36. void setGlobal(const std::string & name, const JsonNode & value) override;
  37. JsonNode saveState() override;
  38. void pop(JsonNode & value);
  39. void popAll();
  40. void push(const std::string & value);
  41. void push(lua_CFunction f, void * opaque);
  42. std::string toStringRaw(int index);
  43. private:
  44. lua_State * L;
  45. const Script * script;
  46. const Environment * env;
  47. std::shared_ptr<LuaReference> modules;
  48. std::shared_ptr<LuaReference> scriptClosure;
  49. void cleanupGlobals();
  50. void registerCore();
  51. //require global function
  52. static int require(lua_State * L);
  53. //print global function
  54. static int print(lua_State * L);
  55. static int logError(lua_State * L);
  56. //require function implementation
  57. int loadModule();
  58. int printImpl();
  59. int logErrorImpl();
  60. };
  61. }