]> 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_int8_t, l4_int16_t, l4_int32_t, l4_mword_t,
23         int, short, long, signed char
24 %{$1 = ($type)lua_tointeger(L, $input);%}
25
26 %typemap(in,checkfn="lua_isnumber") l4_uint8_t, l4_uint16_t, l4_uint32_t, l4_addr_t,
27         l4_umword_t, unsigned int, unsigned short, unsigned long, unsigned char
28 %{//SWIG_contract_assert((lua_tonumber(L,$input)>=0),"number must not be negative")
29 $1 = ($type)lua_tointeger(L, $input);%}
30
31 %typemap(in,checkfn="lua_isnumber") enum SWIGTYPE
32 %{$1 = ($type)(int)lua_tointeger(L, $input);%}
33
34 %typemap(out) enum SWIGTYPE
35 %{  lua_pushinteger(L, (int)($1)); SWIG_arg++;%}
36
37 %typemap(out) l4_addr_t, l4_umword_t, l4_mword_t, l4_int32_t, l4_uint32_t,
38              l4_int8_t, l4_uint8_t, l4_int16_t, l4_uint16_t, l4_size_t,
39              int,short,long,unsigned int,unsigned short,unsigned long,
40              signed char,unsigned char
41 %{  lua_pushinteger(L, $1); SWIG_arg++;%}
42
43 %typemap(out) l4_int32_t*,int*,short*,long*,
44              unsigned int*,unsigned short*,unsigned long*,
45              signed char*,unsigned char*
46 %{  lua_pushinteger(L, *$1); SWIG_arg++;%}
47
48 %typemap(typecheck,precedence=SWIG_TYPECHECK_STRING) cxx::String, cxx::String const & {
49    $1 = lua_isstring(L, $input);
50 }
51
52 %typemap(in,checkfn="lua_isstring") cxx::String
53 %{$1 = cxx::String(lua_tostring(L, $input), lua_rawlen(L, $input));%}
54
55 %typemap(in,checkfn="lua_isstring") cxx::String const & (cxx::String tmp)
56 %{tmp = cxx::String(lua_tostring(L, $input), lua_rawlen(L, $input)); $1 = &tmp;%}
57
58
59 // typemap for big integers
60 %define %l4re_lua_bigint(name, type32, type, fmt, conversion)
61   %typemap(typecheck,precedence=SWIG_TYPECHECK_INTEGER) name
62   { $1 = lua_isnumber(L, $input) || lua_isstring(L, $input); }
63
64   %typemap(in) name
65   %{
66
67   #if SWIG_RECURSIVE_DEFINED LNUM_INT32
68     if (lua_isinteger(L, $input))
69       $1 = (type32)lua_tointeger(L, $input);
70     else
71   #elif SWIG_RECURSIVE_DEFINED LNUM_INT64
72     if (lua_isinteger(L, $input))
73       $1 = lua_tointeger(L, $input);
74     else
75   #endif
76     if (lua_isstring(L, $input))
77       {
78         char *e = 0;
79         char const *s = lua_tostring(L, $input);
80         type a = conversion(s, &e, 0);
81         if (s == e || *e != 0)
82           {
83             lua_pushfstring(L, "big number conversion error '%s'", s);
84             SWIG_fail;
85           }
86         $1 = a;
87       }
88     else if (lua_isnumber(L, $input))
89       $1 = lua_tonumber(L, $input);
90     else
91       {
92         lua_pushfstring(L, "big number expected (string or number) got %s",
93                         lua_typename(L, lua_type(L, $input)));
94         SWIG_fail;
95       }
96   %}
97
98   %typemap(out) name
99   %{lua_pushfstring(L, fmt, $1); ++SWIG_arg;%}
100 %enddef
101 %l4re_lua_bigint(SWIG_BIGUINT, l4_uint32_t, l4_uint64_t, "0x%llx", strtoull)
102 %l4re_lua_bigint(SWIG_BIGINT, l4_int32_t, l4_int64_t, "0x%llx", strtoll)
103
104 %apply SWIG_BIGINT { l4_int64_t }
105 %apply SWIG_BIGUINT { l4_uint64_t }
106