aboutsummaryrefslogtreecommitdiff
path: root/examples/hello_lua_mingw_w64/src/main.cpp
blob: 1486b988eeaa7bf49f82966fff1cb33d67acdf3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Example taken from http://lua-users.org/lists/lua-l/2010-06/msg00153.html
#include <stdio.h>
extern "C" {
#include <lua5.1/lua.h>
#include <lua5.1/lualib.h>
#include <lua5.1/lauxlib.h>
}
int main(int argc, char *argv[]) {
  // Open lua
  lua_State *L = lua_open();

  // Load the libraries
  luaL_openlibs(L);

  // Execution of a lua string
  luaL_dostring(L, "print \"Yo dude\"");

  // Load a string and then execute it.
  luaL_loadstring(L, "io.write(\"I'm here too\\n\")");
  lua_pcall(L, 0, LUA_MULTRET, 0);

  // Load from a file and then execute
  if (luaL_loadfile(L, "hello.lua") == 0) {
    // File loaded call it
    lua_pcall(L, 0, LUA_MULTRET, 0);
  }

  // Close lua
  lua_close (L);

  return 0;
}