annotate lib/swig/swigwin-2.0.11/Lib/lua/lua_fnptr.i @ 2467:43faa2865d57

mistake in previous commit
author zipi
date Sun, 17 Aug 2014 20:58:38 +0100
parents b3009adc0e2f
children
rev   line source
1899
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
1 /* -----------------------------------------------------------------------------
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
2 * lua_fnptr.i
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
3 *
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
4 * SWIG Library file containing the main typemap code to support Lua modules.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
5 * ----------------------------------------------------------------------------- */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
6
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
7 /* -----------------------------------------------------------------------------
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
8 * Basic function pointer support
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
9 * ----------------------------------------------------------------------------- */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
10 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
11 The structure: SWIGLUA_FN provides a simple (local only) wrapping for a function.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
12
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
13 For example if you wanted to have a C/C++ function take a lua function as a parameter.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
14 You could declare it as:
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
15 int my_func(int a, int b, SWIGLUA_FN fn);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
16 note: it should be passed by value, not byref or as a pointer.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
17
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
18 The SWIGLUA_FN holds a pointer to the lua_State, and the stack index where the function is held.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
19 The macro SWIGLUA_FN_GET() will put a copy of the lua function at the top of the stack.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
20 After that its fairly simple to write the rest of the code (assuming know how to use lua),
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
21 just push the parameters, call the function and return the result.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
22
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
23 int my_func(int a, int b, SWIGLUA_FN fn)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
24 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
25 SWIGLUA_FN_GET(fn);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
26 lua_pushnumber(fn.L,a);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
27 lua_pushnumber(fn.L,b);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
28 lua_call(fn.L,2,1); // 2 in, 1 out
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
29 return luaL_checknumber(fn.L,-1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
30 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
31
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
32 SWIG will automatically performs the wrapping of the arguments in and out.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
33
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
34 However: if you wish to store the function between calls, look to the SWIGLUA_REF below.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
35
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
36 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
37 // this is for the C code only, we don't want SWIG to wrapper it for us.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
38 %{
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
39 typedef struct{
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
40 lua_State* L; /* the state */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
41 int idx; /* the index on the stack */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
42 }SWIGLUA_FN;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
43
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
44 #define SWIGLUA_FN_GET(fn) {lua_pushvalue(fn.L,fn.idx);}
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
45 %}
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
46
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
47 // the actual typemap
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
48 %typemap(in,checkfn="lua_isfunction") SWIGLUA_FN
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
49 %{ $1.L=L; $1.idx=$input; %}
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
50
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
51 /* -----------------------------------------------------------------------------
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
52 * Storing lua object support
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
53 * ----------------------------------------------------------------------------- */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
54 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
55 The structure: SWIGLUA_REF provides a mechanism to store object (usually functions)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
56 between calls to the interpreter.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
57
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
58 For example if you wanted to have a C/C++ function take a lua function as a parameter.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
59 Then call it later, You could declare it as:
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
60 SWIGLUA_REF myref;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
61 void set_func(SWIGLUA_REF ref);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
62 SWIGLUA_REF get_func();
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
63 void call_func(int val);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
64 note: it should be passed by value, not byref or as a pointer.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
65
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
66 The SWIGLUA_REF holds a pointer to the lua_State, and an integer reference to the object.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
67 Because it holds a permanent ref to an object, the SWIGLUA_REF must be handled with a bit more care.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
68 It should be initialised to {0,0}. The function swiglua_ref_set() should be used to set it.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
69 swiglua_ref_clear() should be used to clear it when not in use, and swiglua_ref_get() to get the
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
70 data back.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
71
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
72 Note: the typemap does not check that the object is in fact a function,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
73 if you need that you must add it yourself.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
74
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
75
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
76 int my_func(int a, int b, SWIGLUA_FN fn)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
77 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
78 SWIGLUA_FN_GET(fn);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
79 lua_pushnumber(fn.L,a);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
80 lua_pushnumber(fn.L,b);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
81 lua_call(fn.L,2,1); // 2 in, 1 out
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
82 return luaL_checknumber(fn.L,-1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
83 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
84
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
85 SWIG will automatically performs the wrapping of the arguments in and out.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
86
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
87 However: if you wish to store the function between calls, look to the SWIGLUA_REF below.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
88
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
89 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
90
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
91 %{
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
92 typedef struct{
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
93 lua_State* L; /* the state */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
94 int ref; /* a ref in the lua global index */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
95 }SWIGLUA_REF;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
96
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
97
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
98 void swiglua_ref_clear(SWIGLUA_REF* pref){
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
99 if (pref->L!=0 && pref->ref!=LUA_NOREF && pref->ref!=LUA_REFNIL){
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
100 luaL_unref(pref->L,LUA_REGISTRYINDEX,pref->ref);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
101 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
102 pref->L=0; pref->ref=0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
103 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
104
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
105 void swiglua_ref_set(SWIGLUA_REF* pref,lua_State* L,int idx){
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
106 // swiglua_ref_clear(pref); /* just in case */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
107 pref->L=L;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
108 lua_pushvalue(L,idx); /* copy obj to top */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
109 pref->ref=luaL_ref(L,LUA_REGISTRYINDEX); /* remove obj from top & put into registry */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
110 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
111
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
112 void swiglua_ref_get(SWIGLUA_REF* pref){
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
113 if (pref->L!=0)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
114 lua_rawgeti(pref->L,LUA_REGISTRYINDEX,pref->ref);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
115 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
116
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
117 %}
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
118
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
119 %typemap(in) SWIGLUA_REF
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
120 %{ swiglua_ref_set(&$1,L,$input); %}
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
121
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
122 %typemap(out) SWIGLUA_REF
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
123 %{ if ($1.L!=0) {swiglua_ref_get(&$1);} else {lua_pushnil(L);}
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
124 SWIG_arg++; %}
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
125