Пример #1
0
int main(int argc,char* argv[])
{
  lua_State *L1=0,*L2=0,*L3=0;

  printf("This is a test of having two SWIG'ed modules and three lua states\n"
	"statically linked together.\n"
	"Its mainly to check that all the types are correctly managed\n\n");
	
  DEBUG("creating lua states(L1,L2,L3)");
  L1=lua_open();
  L2=lua_open();
  L3=lua_open();
  DEBUG("ok\n\n");

  DEBUG("luaopen_example(L1)..");
  luaopen_example(L1);
  DEBUG("ok\n");
	
  DEBUG("Testing Module L1\n");
  DEBUG("This module should know about Foo* but not Bar*\n");
  testModule(L1);
  DEBUG("End Testing Module L1\n\n");

  DEBUG("luaopen_example2(L2)..");
  luaopen_example2(L2);
  DEBUG("ok\n");
	
  DEBUG("Testing Module L2\n");
  DEBUG("This module should know about Bar* but not Foo*\n");
  testModule(L2);
  DEBUG("End Testing Module L2\n\n");

  DEBUG("luaopen_example(L3)..");
  luaopen_example(L3);
  DEBUG("ok\n");
  DEBUG("luaopen_example2(L3)..");
  luaopen_example2(L3);
  DEBUG("ok\n");

  DEBUG("Testing Module L3\n");
  DEBUG("This module should know about Foo* and Bar*\n");
  testModule(L3);
  DEBUG("End Testing Module L3\n\n");

  DEBUG("Testing Module L1 again\n");
  DEBUG("It now should know about Foo* and Bar*\n");
  testModule(L1);
  DEBUG("End Testing Module L1 again\n\n");

  DEBUG("close all..");
  lua_close(L1);
  lua_close(L2);
  lua_close(L3);
  DEBUG("ok, exiting\n");
  return 0;
}
Пример #2
0
int main(int argc,char* argv[]) {
  lua_State *L;
  int ok;
  int res;
  char str[80];
  printf("[C] Welcome to the simple embedded Lua example v2\n");
  printf("[C] We are in C\n");
  printf("[C] opening a Lua state & loading the libraries\n");
  L=lua_open();
  luaopen_base(L);
  luaopen_string(L);
  luaopen_math(L);
  printf("[C] now loading the SWIG wrappered library\n");
  luaopen_example(L);
  printf("[C] all looks ok\n");
  printf("\n");
  printf("[C] lets load the file 'runme.lua'\n");
  printf("[C] any lua code in this file will be executed\n");
  if (luaL_loadfile(L, "runme.lua") || lua_pcall(L, 0, 0, 0)) {
    printf("[C] ERROR: cannot run lua file: %s",lua_tostring(L, -1));
    exit(3);
  }
  printf("[C] We are now back in C, all looks ok\n");
  printf("\n");
  printf("[C] lets call the Lua function 'add(1,1)'\n");
  printf("[C] using the C function 'call_add'\n");
  ok=call_add(L,1,1,&res);
  printf("[C] the function returned %d with value %d\n",ok,res);
  printf("\n");
  printf("[C] lets do this rather easier\n");
  printf("[C] we will call the same Lua function using a generic C function 'call_va'\n");
  ok=call_va(L,"add","ii>i",1,1,&res);
  printf("[C] the function returned %d with value %d\n",ok,res);
  printf("\n");
  printf("[C] we will now use the same generic C function to call 'append(\"cat\",\"dog\")'\n");
  ok=call_va(L,"append","ss>s","cat","dog",str);
  printf("[C] the function returned %d with value %s\n",ok,str);
  printf("\n");
  printf("[C] we can also make some bad calls to ensure the code doesn't fail\n");
  printf("[C] calling adds(1,2)\n");
  ok=call_va(L,"adds","ii>i",1,2,&res);
  printf("[C] the function returned %d with value %d\n",ok,res);
  printf("[C] calling add(1,'fred')\n");
  ok=call_va(L,"add","is>i",1,"fred",&res);
  printf("[C] the function returned %d with value %d\n",ok,res);
  printf("\n");
  printf("[C] Note: no protection if you mess up the va-args, this is C\n");
  printf("\n");
  printf("[C] Finally we will call the wrappered gcd function gdc(6,9):\n");
  printf("[C] This will pass the values to Lua, then call the wrappered function\n");
  printf("    Which will get the values from Lua, call the C code \n");
  printf("    and return the value to Lua and eventually back to C\n");
  printf("[C] Certainly not the best way to do it :-)\n");
  ok=call_va(L,"gcd","ii>i",6,9,&res);
  printf("[C] the function returned %d with value %d\n",ok,res);
  printf("\n");
  printf("[C] all finished, closing the lua state\n");
  lua_close(L);
  return 0;
}
Пример #3
0
int main(int argc, char* argv[]) {
  printf("[C++] Welcome to the simple embedded Lua example v3\n");
  printf("[C++] We are in C++\n");
  printf("[C++] opening a Lua state & loading the libraries\n");
  lua_State *L = lua_open();
  luaopen_base(L);
  luaopen_string(L);
  luaopen_math(L);
  printf("[C++] now loading the SWIG wrappered library\n");
  luaopen_example(L);
  printf("[C++] all looks ok\n");
  printf("\n");
  printf("[C++] lets create an Engine and pass a pointer to Lua\n");
  Engine engine;
  /* this code will pass a pointer into lua, but C++ still owns the object
  this is a little tedious, to do, but lets do it
  we need to pass the pointer (obviously), the type name 
  and a flag which states if Lua should delete the pointer once its finished with it
  The type name is a class name string which is registered with SWIG
  (normally, just look in the wrapper file to get this)
  in this case we don't want Lua to delete the pointer so the ownership flag is 0
  */
  push_pointer(L,&engine,"Engine *",0);
  lua_setglobal(L, "pEngine");  // set as global variable

  printf("[C++] now lets load the file 'runme.lua'\n");
  printf("[C++] any lua code in this file will be executed\n");
  if (luaL_loadfile(L, "runme.lua") || lua_pcall(L, 0, 0, 0)) {
    printf("[C++] ERROR: cannot run lua file: %s", lua_tostring(L, -1));
    exit(3);
  }
  printf("[C++] We are now back in C++, all looks ok\n");
  printf("\n");

  printf("[C++] Lets call the Lua function onEvent(e)\n");
  printf("[C++] We will give it different events, as we wish\n");
  printf("[C++] Starting with STARTUP\n");
  Event ev;
  ev.mType = Event::STARTUP;
  call_onEvent(L, ev);
  printf("[C++] ok\n");
  printf("[C++] now we will try MOUSEPRESS,KEYPRESS,MOUSEPRESS\n");
  ev.mType = Event::MOUSEPRESS;
  call_onEvent(L, ev);
  ev.mType = Event::KEYPRESS;
  call_onEvent(L, ev);
  ev.mType = Event::MOUSEPRESS;
  call_onEvent(L, ev);
  printf("[C++] ok\n");
  printf("[C++] Finally we will SHUTDOWN\n");
  ev.mType = Event::SHUTDOWN;
  call_onEvent(L, ev);
  printf("[C++] ok\n");

  printf("\n");
  printf("[C++] all finished, closing the lua state\n");
  lua_close(L);
  return 0;
}
Пример #4
0
int main(int argc,char* argv[])
{
 lua_State *L;
 if (argc<2)
 {
  printf("%s: <filename.lua>\n",argv[0]);
  return 0;
 }
 L=luaL_newstate();
 luaopen_base(L); // load basic libs (eg. print)
 luaopen_example(L);  // load the wrappered module
 if (luaL_loadfile(L,argv[1])==0) // load and run the file
  lua_pcall(L,0,0,0);
 else
  printf("unable to load %s\n",argv[1]);
 lua_close(L);
 return 0;
}
Пример #5
0
int main(int argc,char* argv[]) {
  lua_State *L;
  int ok;
  printf("[C] Welcome to the simple embedded lua example\n");
  printf("[C] We are in C\n");
  printf("[C] opening a lua state & loading the libraries\n");
  L=lua_open();
  luaopen_base(L);
  luaopen_string(L);
  luaopen_math(L);
  printf("[C] now loading the SWIG wrapped library\n");
  luaopen_example(L);
  printf("[C] all looks ok\n");
  printf("\n");
  if (argc != 2 || argv[1] == NULL || strlen(argv[1]) == 0) {
    printf("[C] ERROR: no lua file given on command line\n");
    exit(3);
  }
  printf("[C] let's load the file '%s'\n", argv[1]);
  printf("[C] any lua code in this file will be executed\n");
  if (luaL_loadfile(L, argv[1]) || lua_pcall(L, 0, 0, 0)) {
    printf("[C] ERROR: cannot run lua file: %s",lua_tostring(L, -1));
    exit(3);
  }
  printf("[C] We are now back in C, all looks ok\n");
  printf("\n");
  printf("[C] let's call the function 'do_tests()'\n");
  ok=dostring(L,"do_tests()");
  printf("[C] We are back in C, the dostring() function returned %d\n",ok);
  printf("\n");
  printf("[C] Let's call lua again, but create an error\n");
  ok=dostring(L,"no_such_function()");
  printf("[C] We are back in C, the dostring() function returned %d\n",ok);
  printf("[C] it should also have returned 1 and printed an error message\n");
  printf("\n");
  printf("[C] Let's call lua again, calling the greeting function\n");
  ok=dostring(L,"call_greeting()");
  printf("[C] This was C=>Lua=>C (getting a bit complex)\n");
  printf("\n");
  printf("[C] all finished, closing the lua state\n");
  lua_close(L);
  return 0;
}