]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/io/server/src/lua_typemap.i
update
[l4.git] / l4 / pkg / io / server / src / lua_typemap.i
1 // vi:ft=cpp
2 /*
3  * (c) 2011 Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10
11 %{
12 #define SWIG_RECURSIVE_DEFINED defined
13 %}
14
15 /* Refined typemaps for our L4 types and our lua lib with integer support */
16
17 %typemap(typecheck,precedence=SWIG_TYPECHECK_INTEGER) l4_int8_t,l4int16_t,l4_int32_t,
18   l4_mword_t,l4uint8_t,l4_uint16_t,l4_uint32_t,l4_umword_t,
19   l4_size_t,l4_addr_t
20 {$1 = lua_isnumber(L, $input);}
21
22 %typemap(in,checkfn="lua_isnumber") l4_int32_t, int, short, long, signed char
23 %{$1 = ($type)lua_tointeger(L, $input);%}
24
25 %typemap(in,checkfn="lua_isnumber") l4_addr_t, l4_umword_t, unsigned int, unsigned short,
26         unsigned long, unsigned char
27 %{//SWIG_contract_assert((lua_tonumber(L,$input)>=0),"number must not be negative")
28 $1 = ($type)lua_tointeger(L, $input);%}
29
30 %typemap(in,checkfn="lua_isnumber") enum SWIGTYPE
31 %{$1 = ($type)(int)lua_tointeger(L, $input);%}
32
33 %typemap(out) enum SWIGTYPE
34 %{  lua_pushinteger(L, (int)($1)); SWIG_arg++;%}
35
36 %typemap(out) l4_addr_t,l4_umword_t,l4_int32_t,int,short,long,
37              unsigned int,unsigned short,unsigned long,
38              signed char,unsigned char
39 %{  lua_pushinteger(L, $1); SWIG_arg++;%}
40
41 %typemap(out) l4_int32_t*,int*,short*,long*,
42              unsigned int*,unsigned short*,unsigned long*,
43              signed char*,unsigned char*
44 %{  lua_pushinteger(L, *$1); SWIG_arg++;%}
45
46 %typemap(typecheck,precedence=SWIG_TYPECHECK_INTEGER) l4_addr_t,l4_umword_t,l4_int32_t {
47    $1 = lua_isnumber(L, $input);
48 }
49
50 %typemap(typecheck,precedence=SWIG_TYPECHECK_STRING) cxx::String, cxx::String const & {
51    $1 = lua_isstring(L, $input);
52 }
53
54 %typemap(in,checkfn="lua_isstring") cxx::String
55 %{$1 = cxx::String(lua_tostring(L, $input), lua_rawlen(L, $input));%}
56
57 %typemap(in,checkfn="lua_isstring") cxx::String const & (cxx::String tmp)
58 %{tmp = cxx::String(lua_tostring(L, $input), lua_rawlen(L, $input)); $1 = &tmp;%}
59
60
61 // typemap for big integers
62 %define %l4re_lua_bigint(name, type32, type, fmt, conversion)
63   %typemap(typecheck,precedence=SWIG_TYPECHECK_INTEGER) name
64   { $1 = lua_isnumber(L, $input) || lua_isstring(L, $input); }
65
66   %typemap(in) name
67   %{
68
69   #if SWIG_RECURSIVE_DEFINED LNUM_INT32
70     if (lua_isinteger(L, $input))
71       $1 = (type32)lua_tointeger(L, $input);
72     else
73   #elif SWIG_RECURSIVE_DEFINED LNUM_INT64
74     if (lua_isinteger(L, $input))
75       $1 = lua_tointeger(L, $input);
76     else
77   #endif
78     if (lua_isstring(L, $input))
79       {
80         char *e = 0;
81         char const *s = lua_tostring(L, $input);
82         type a = conversion(s, &e, 0);
83         if (s == e || *e != 0)
84           {
85             lua_pushfstring(L, "big number conversion error '%s'", s);
86             SWIG_fail;
87           }
88         $1 = a;
89       }
90     else if (lua_isnumber(L, $input))
91       $1 = lua_tonumber(L, $input);
92     else
93       {
94         lua_pushfstring(L, "big number expected (string or number) got %s",
95                         lua_typename(L, lua_type(L, $input)));
96         SWIG_fail;
97       }
98   %}
99
100   %typemap(out) name
101   %{lua_pushfstring(L, fmt, $1); ++SWIG_arg;%}
102 %enddef
103 %l4re_lua_bigint(SWIG_BIGUINT, l4_uint32_t, l4_uint64_t, "0x%llx", strtoull)
104 %l4re_lua_bigint(SWIG_BIGINT, l4_int32_t, l4_int64_t, "0x%llx", strtoll)
105
106 %apply SWIG_BIGINT { l4_int64_t }
107 %apply SWIG_BIGUINT { l4_uint64_t }
108