~ubuntu-branches/ubuntu/oneiric/swig1.3/oneiric

« back to all changes in this revision

Viewing changes to Examples/lua/embed/embed.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2008-06-20 18:33:37 UTC
  • mfrom: (1.2.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080620183337-hockvwcewu29409c
Tags: 1.3.35-4ubuntu1
* Merge from debian unstable, remaining changes:
  - Drop support for pike.
  - Use python2.5 instead of python2.4.
  - use php5
  - Clean Runtime/ as well.
  - Force a few environment variables.
  - debian/Rules (clean): Remove Lib/ocaml/swigp4.ml.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* embed.c a simple test for an embeded interpreter
 
2
 
 
3
The idea is that we wrapper a few simple function (example.c)
 
4
and write our own app to call it.
 
5
 
 
6
What it will do is load the wrappered lib, load runme.lua and then call some functions.
 
7
To make life easier, all the printf's have either [C] or [Lua] at the start
 
8
so you can see where they are coming from.
 
9
 
 
10
We will be using the luaL_dostring()/lua_dostring() function to call into lua 
 
11
 
 
12
*/
 
13
 
 
14
#include <stdlib.h>
 
15
#include <stdio.h>
 
16
 
 
17
#include <lua.h>
 
18
#include <lauxlib.h>
 
19
#include <lualib.h>
 
20
 
 
21
/* the SWIG wrappered library */
 
22
extern int luaopen_example(lua_State*L);
 
23
 
 
24
/* a really simple way of calling lua from C
 
25
 just give it a lua state & a string to execute
 
26
Unfortunately lua keeps changing its API's.
 
27
In lua 5.0.X its lua_dostring()
 
28
In lua 5.1.X its luaL_dostring()
 
29
so we have a few extra compiles
 
30
*/
 
31
int dostring(lua_State *L, char* str) {
 
32
  int ok;
 
33
#if (defined(LUA_VERSION_NUM) && (LUA_VERSION_NUM>=501))
 
34
 
 
35
  ok=luaL_dostring(L,str);      /* looks like this is lua 5.1.X or later, good */
 
36
#else
 
37
 
 
38
  ok=lua_dostring(L,str);       /* might be lua 5.0.x, using lua_dostring */
 
39
#endif
 
40
 
 
41
  if (ok!=0)
 
42
    printf("[C] ERROR in dostring: %s\n",lua_tostring(L,-1));
 
43
  return ok;
 
44
}
 
45
 
 
46
 
 
47
int main(int argc,char* argv[]) {
 
48
  lua_State *L;
 
49
  int ok;
 
50
  printf("[C] Welcome to the simple embedded lua example\n");
 
51
  printf("[C] We are in C\n");
 
52
  printf("[C] opening a lua state & loading the libraries\n");
 
53
  L=lua_open();
 
54
  luaopen_base(L);
 
55
  luaopen_string(L);
 
56
  luaopen_math(L);
 
57
  printf("[C] now loading the SWIG wrappered library\n");
 
58
  luaopen_example(L);
 
59
  printf("[C] all looks ok\n");
 
60
  printf("\n");
 
61
  printf("[C] lets load the file 'runme.lua'\n");
 
62
  printf("[C] any lua code in this file will be executed\n");
 
63
  if (luaL_loadfile(L, "runme.lua") || lua_pcall(L, 0, 0, 0)) {
 
64
    printf("[C] ERROR: cannot run lua file: %s",lua_tostring(L, -1));
 
65
    exit(3);
 
66
  }
 
67
  printf("[C] We are now back in C, all looks ok\n");
 
68
  printf("\n");
 
69
  printf("[C] lets call the function 'do_tests()'\n");
 
70
  ok=dostring(L,"do_tests()");
 
71
  printf("[C] We are back in C, the dostring() function returned %d\n",ok);
 
72
  printf("\n");
 
73
  printf("[C] Lets call lua again, but create an error\n");
 
74
  ok=dostring(L,"no_such_function()");
 
75
  printf("[C] We are back in C, the dostring() function returned %d\n",ok);
 
76
  printf("[C] it should also have returned 1 and printed an error message\n");
 
77
  printf("\n");
 
78
  printf("[C] Lets call lua again, calling the greeting function\n");
 
79
  ok=dostring(L,"call_greeting()");
 
80
  printf("[C] This was C=>Lua=>C (getting a bit complex)\n");
 
81
  printf("\n");
 
82
  printf("[C] all finished, closing the lua state\n");
 
83
  lua_close(L);
 
84
  return 0;
 
85
}