1%include <typemaps.i>
2
3// FIXME: We need to port more typemaps from Python
4
5//===----------------------------------------------------------------------===//
6
7// In Lua 5.3 and beyond the VM supports integers, so we need to remap
8// SWIG's internal handling of integers.
9
10
11%define LLDB_NUMBER_TYPEMAP(TYPE)
12
13// Primitive integer mapping
14%typemap(in,checkfn="lua_isinteger") TYPE
15%{ $1 = (TYPE)lua_tointeger(L, $input); %}
16%typemap(in,checkfn="lua_isinteger") const TYPE&($basetype temp)
17%{ temp=($basetype)lua_tointeger(L,$input); $1=&temp;%}
18%typemap(out) TYPE
19%{ lua_pushinteger(L, (lua_Integer) $1); SWIG_arg++;%}
20%typemap(out) const TYPE&
21%{ lua_pushinteger(L, (lua_Integer) $1); SWIG_arg++;%}
22
23// Pointer and reference mapping
24%typemap(in,checkfn="lua_isinteger") TYPE *INPUT($*ltype temp), TYPE &INPUT($*ltype temp)
25%{ temp = ($*ltype)lua_tointeger(L,$input);
26   $1 = &temp; %}
27%typemap(in, numinputs=0) TYPE *OUTPUT ($*ltype temp)
28%{ $1 = &temp; %}
29%typemap(argout) TYPE *OUTPUT
30%{  lua_pushinteger(L, (lua_Integer) *$1); SWIG_arg++;%}
31%typemap(in) TYPE *INOUT = TYPE *INPUT;
32%typemap(argout) TYPE *INOUT = TYPE *OUTPUT;
33%typemap(in) TYPE &OUTPUT = TYPE *OUTPUT;
34%typemap(argout) TYPE &OUTPUT = TYPE *OUTPUT;
35%typemap(in) TYPE &INOUT = TYPE *INPUT;
36%typemap(argout) TYPE &INOUT = TYPE *OUTPUT;
37%typemap(in,checkfn="lua_isinteger") const TYPE *INPUT($*ltype temp)
38%{ temp = ($*ltype)lua_tointeger(L,$input);
39   $1 = &temp; %}
40
41%enddef // LLDB_NUMBER_TYPEMAP
42
43LLDB_NUMBER_TYPEMAP(unsigned char);
44LLDB_NUMBER_TYPEMAP(signed char);
45LLDB_NUMBER_TYPEMAP(short);
46LLDB_NUMBER_TYPEMAP(unsigned short);
47LLDB_NUMBER_TYPEMAP(signed short);
48LLDB_NUMBER_TYPEMAP(int);
49LLDB_NUMBER_TYPEMAP(unsigned int);
50LLDB_NUMBER_TYPEMAP(signed int);
51LLDB_NUMBER_TYPEMAP(long);
52LLDB_NUMBER_TYPEMAP(unsigned long);
53LLDB_NUMBER_TYPEMAP(signed long);
54LLDB_NUMBER_TYPEMAP(long long);
55LLDB_NUMBER_TYPEMAP(unsigned long long);
56LLDB_NUMBER_TYPEMAP(signed long long);
57
58%apply unsigned long { size_t };
59%apply const unsigned long & { const size_t & };
60%apply long { ssize_t };
61%apply const long & { const ssize_t & };
62
63//===----------------------------------------------------------------------===//
64
65// FIXME:
66//  Ideally all the typemaps should be revisited in a future SB API revision.
67//  Typemaps, usually, modifies the function signatures and might spawn
68//  different LLDB APIs across languages (C++, Python, Lua...).
69//  Historically, typemaps have been used to replace SWIG's deficiencies,
70//  but SWIG itself evolved and some API design choices are now redundant.
71
72//===----------------------------------------------------------------------===//
73
74// Typemap definitions to allow SWIG to properly handle char buffer.
75
76// typemap for a char buffer
77%typemap(in) (char *dst, size_t dst_len) {
78   $2 = luaL_checkinteger(L, $input);
79   if ($2 <= 0) {
80       return luaL_error(L, "Positive integer expected");
81   }
82   $1 = (char *) malloc($2);
83}
84
85// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
86// as char data instead of byte data.
87%typemap(in) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
88
89// Return the char buffer.  Discarding any previous return result
90%typemap(argout) (char *dst, size_t dst_len) {
91   lua_pop(L, 1); // Blow away the previous result
92   if ($result == 0) {
93      lua_pushliteral(L, "");
94   } else {
95      lua_pushlstring(L, (const char *)$1, $result);
96   }
97   free($1);
98   // SWIG_arg was already incremented
99}
100
101// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
102// as char data instead of byte data.
103%typemap(argout) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
104
105//===----------------------------------------------------------------------===//
106