CapsAdmin

Untitled

Mar 24th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include "mmyy.hpp"
  2.  
  3. #include <stdio.h>
  4.  
  5. #pragma comment(lib, "lua51.lib")
  6. #pragma comment(lib, "mmyy.lib")
  7.  
  8. class  MyState : public mmyy::State
  9. {
  10. public:
  11.  
  12.     // redirect output if nesseceary
  13.     void Msg(const char * str)
  14.     {
  15.         printf("%s", str);
  16.     }
  17.     void MsgN(const char * str)
  18.     {
  19.         printf("%s\n", str);
  20.     }
  21.     void OnError(const char * str)
  22.     {
  23.         printf("[lua error] %s\n", str);
  24.     }
  25.  
  26.     // this will give us some nice functions to easily push and get MyState
  27.     MMYY_WRAP_CLASS(MyState, State, state)
  28.  
  29.     // a workaround to make it so we can call Push from base (implement this ???)
  30.     template <typename T>
  31.     void Push(T var)
  32.     {
  33.         mmyy::State::Push(var);
  34.     }
  35.  
  36. };
  37.  
  38. // if we only have one state, we assume that this will always be the state used when a lua function is called
  39. MyState *my;
  40.  
  41. LUALIB_FUNCTION(_G, State)
  42. {
  43.     auto self = new MyState();
  44.  
  45.     my->Push(self);
  46.  
  47.     return 1;
  48. }
  49.  
  50. // uh oh
  51. LUALIB_FUNCTION(_G, GetCurrentState)
  52. {
  53.     my->Push(my);
  54.  
  55.     return 1;
  56. }
  57.  
  58. LUAMTA_FUNCTION(state, Error)
  59. {
  60.     auto self = my->ToState(1);
  61.  
  62.     self->Error(my->ToString(2));
  63.  
  64.     return 0;
  65. }
  66.  
  67. LUAMTA_FUNCTION(state, RunString)
  68. {
  69.     auto self = my->ToState(1);
  70.  
  71.     my->Push(self->RunString(my->ToString(2)));
  72.  
  73.     return 1;
  74. }
  75.  
  76. int main()
  77. {
  78.     my = new MyState();
  79.  
  80.     my->SetPlatformName("a");
  81.  
  82.     my->RunString("state = State()");
  83.     my->RunString("state.hello = 'hello world'");
  84.     my->RunString("state:Error('aaaaaaaaaaaaaaaa')");
  85.     my->RunString("print(state.hello)");
  86.  
  87.     my->RunString("state = GetCurrentState()");
  88.     my->RunString("state:RunString('aisjdiasjd iasjdi asjidj sad')");
  89.  
  90.     return 0;
  91. }
  92.  
  93. /*
  94. [lua error] aaaaaaaaaaaaaaaa
  95. hello world
  96. [lua error] [string "aisjdiasjd iasjdi asjidj sad"]:1: '=' expected near 'iasjdi'
  97. */
Advertisement
Add Comment
Please, Sign In to add comment