SHARE
TWEET

Untitled

a guest Jun 27th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. print = 123
  2. print = Hi 0
  3. Result = 3
  4. print = Hi 1
  5. Result = 3
  6. print = Hi 2
  7. Result = 3
  8. print = Hi 3
  9. Result = 3
  10.  
  11. print = Hi 4
  12. Result = 3
  13. print = Hi 5
  14. Result = 3
  15. print = Hi 6
  16. Result = 3
  17. print = Hi 7
  18. Result = 3
  19.  
  20. print = 123
  21. print = Hi 0
  22. Result = 3
  23. print = Hi 1
  24. Result = 3
  25. print = Hi 2
  26. Result = 3
  27. print = Hi 3
  28. Result = 3
  29.  
  30. print = Hi 8
  31. Result = 3
  32. print = Hi 9
  33. Result = 3
  34. print = Hi 10
  35. Result = 3
  36. print = Hi 11
  37. Result = 3
  38.      
  39. print = 123
  40. print = Hi 0
  41. Result = 3
  42. print = Hi 1
  43. Result = 3
  44. print = Hi 2
  45. Result = 3
  46. print = Hi 3
  47. Result = 3
  48.  
  49. print = Hi 4
  50. Result = 3
  51. print = Hi 5
  52. Result = 3
  53. print = Hi 6
  54. Result = 3
  55. print = Hi 7
  56. Result = 3
  57.  
  58. print = 123
  59. print = Hi 0
  60. Result = 3
  61. print = Hi 1
  62. Result = 3
  63. print = Hi 2
  64. Result = 3
  65. print = Hi 3
  66. Result = 3
  67.  
  68. print = Hi 4
  69. Result = 3
  70. print = Hi 5
  71. Result = 3
  72. print = Hi 6
  73. Result = 3
  74. print = Hi 7
  75. Result = 3
  76.      
  77. #pragma once
  78.  
  79.     #pragma comment(lib, "lua53.lib")
  80.     extern "C" {
  81.     #include "lua.h"
  82.     #include "lauxlib.h"
  83.     #include "lualib.h"
  84.     }
  85.  
  86.     #include <iostream>
  87.     #include <LuaBridge/LuaBridge.h>
  88.     using namespace std;
  89.     using namespace luabridge;
  90.     class Lua_sup
  91.     {
  92.     private:
  93.  
  94.     string m_name;
  95.     lua_State* L ;
  96.     bool init_state = false;
  97.     inline void PrintError(int run_status)
  98.     {
  99.         switch (run_status)
  100.         {
  101.         case LUA_ERRRUN:
  102.         {
  103.  
  104.             printf( "LUA: Runtime Error: %sn", lua_tostring(L, -1));
  105.             break;
  106.         }
  107.         case LUA_ERRSYNTAX:
  108.         {
  109.  
  110.             printf("LUA: Syntax Error: %sn", lua_tostring(L, -1));
  111.             break;
  112.         }
  113.         case LUA_ERRMEM:
  114.         {
  115.  
  116.  
  117.             printf("LUA: Memory Alloc Error: %sn", lua_tostring(L, -1));
  118.             break;
  119.         }
  120.         case LUA_ERRERR:
  121.         {
  122.  
  123.             printf("LUA: Error returning Error: %sn", lua_tostring(L, -1));
  124.             break;
  125.         }
  126.         default:
  127.         {
  128.  
  129.             printf("LUA: Unknown Error: %sn", lua_tostring(L, -1));
  130.             break;
  131.         }
  132.         }
  133.     }
  134.     public:
  135.     Lua_sup(string name, lua_State* s);
  136.  
  137.     ~Lua_sup();
  138.     void init();
  139.     void test();
  140.     bool get_state() { return init_state; }
  141.     };
  142.  
  143. //.cpp
  144. #include "Lua_sup.h"
  145.  
  146.  
  147. void printMessage(const std::string& s) {
  148.     cout << "print = " << s << endl;
  149. }
  150.  
  151. string get_string() {
  152.     return "Hi ";
  153. }
  154. Lua_sup::Lua_sup(string name, lua_State* s)
  155. {
  156.     m_name = name;
  157.     L = lua_newthread(s);
  158. }
  159.  
  160.  
  161. Lua_sup::~Lua_sup()
  162. {
  163.  
  164.  
  165.     lua_close(L);
  166. }
  167.  
  168. void Lua_sup::init()
  169. {
  170.     //L = luaL_newstate();
  171.  
  172.     getGlobalNamespace(L).addFunction("printMessage", printMessage);
  173.     int load_status = luaL_loadfile(L, m_name.c_str());
  174.     if (load_status != 0)    {
  175.  
  176.         cout << "Error";
  177.         PrintError(load_status);
  178.         init_state = false;
  179.     }
  180.     lua_pcall(L, 0, 0, 0);
  181.     getGlobalNamespace(L).addFunction("get_string", get_string);
  182.     getGlobalNamespace(L).addFunction("printMessage", printMessage);
  183.     init_state = true;
  184. }
  185.  
  186.  
  187. void Lua_sup::test()
  188. {
  189.     LuaRef hello_world_sum = getGlobal(L, "hello_world_sum");
  190.     LuaRef plot = getGlobal(L, "plot");
  191.     for (int i = 0; i < 3; i++)
  192.     {
  193.         int result = hello_world_sum(1, 2);
  194.         cout << "Result = " << result << endl;
  195.  
  196.  
  197.     }
  198.  
  199.  
  200.     int result = hello_world_sum(1, 2);
  201.     cout << "Result = " << result << endl;
  202.  
  203.     getchar();
  204. }
  205.  
  206.  
  207. //my main
  208. #include "Lua_sup.h"
  209. using namespace luabridge;
  210.  
  211.  
  212.  
  213.  
  214. int main() {
  215.     lua_State* L = luaL_newstate();
  216.  
  217.     luaL_openlibs(L);
  218.  
  219.  
  220.     Lua_sup lua("script.lua", L);
  221.     lua.init();
  222.     lua.test();
  223.     lua.test();
  224.     Lua_sup lua1("123.lua", L);
  225.     lua1.init();
  226.     lua1.test();
  227.  
  228.     lua.test();
  229. }
  230.      
  231. local meta = {_index=_G}
  232. -- Metatable for the new environment
  233. local function load_sandboxed(file)
  234.   local new_env = setmetatable({}, meta}
  235.   -- Create a new environment with metatable. Missing keys
  236.   -- will be looked for in _G before returning nil
  237.   loadfile(file, "t", new_env)
  238.   -- Load the file with a custom environment.
  239.   -- Only allow text files.
  240. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top