1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
%include <typemaps.i>
// FIXME: We need to port more typemaps from Python
//===----------------------------------------------------------------------===//
// In Lua 5.3 and beyond the VM supports integers, so we need to remap
// SWIG's internal handling of integers.
%define LLDB_NUMBER_TYPEMAP(TYPE)
// Primitive integer mapping
%typemap(in,checkfn="lua_isinteger") TYPE
%{ $1 = ($type)lua_tointeger(L, $input); %}
%typemap(in,checkfn="lua_isinteger") const TYPE&($basetype temp)
%{ temp=($basetype)lua_tointeger(L,$input); $1=&temp;%}
%typemap(out) TYPE
%{ lua_pushinteger(L, (lua_Integer) $1); SWIG_arg++;%}
%typemap(out) const TYPE&
%{ lua_pushinteger(L, (lua_Integer) $1); SWIG_arg++;%}
// Pointer and reference mapping
%typemap(in,checkfn="lua_isinteger") TYPE *INPUT($*ltype temp), TYPE &INPUT($*ltype temp)
%{ temp = ($*ltype)lua_tointeger(L,$input);
$1 = &temp; %}
%typemap(in, numinputs=0) TYPE *OUTPUT ($*ltype temp)
%{ $1 = &temp; %}
%typemap(argout) TYPE *OUTPUT
%{ lua_pushinteger(L, (lua_Integer) *$1); SWIG_arg++;%}
%typemap(in) TYPE *INOUT = TYPE *INPUT;
%typemap(argout) TYPE *INOUT = TYPE *OUTPUT;
%typemap(in) TYPE &OUTPUT = TYPE *OUTPUT;
%typemap(argout) TYPE &OUTPUT = TYPE *OUTPUT;
%typemap(in) TYPE &INOUT = TYPE *INPUT;
%typemap(argout) TYPE &INOUT = TYPE *OUTPUT;
%typemap(in,checkfn="lua_isinteger") const TYPE *INPUT($*ltype temp)
%{ temp = ($*ltype)lua_tointeger(L,$input);
$1 = &temp; %}
%enddef // LLDB_NUMBER_TYPEMAP
LLDB_NUMBER_TYPEMAP(unsigned char);
LLDB_NUMBER_TYPEMAP(signed char);
LLDB_NUMBER_TYPEMAP(short);
LLDB_NUMBER_TYPEMAP(unsigned short);
LLDB_NUMBER_TYPEMAP(signed short);
LLDB_NUMBER_TYPEMAP(int);
LLDB_NUMBER_TYPEMAP(unsigned int);
LLDB_NUMBER_TYPEMAP(signed int);
LLDB_NUMBER_TYPEMAP(long);
LLDB_NUMBER_TYPEMAP(unsigned long);
LLDB_NUMBER_TYPEMAP(signed long);
LLDB_NUMBER_TYPEMAP(long long);
LLDB_NUMBER_TYPEMAP(unsigned long long);
LLDB_NUMBER_TYPEMAP(signed long long);
LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
%apply unsigned long { size_t };
%apply const unsigned long & { const size_t & };
%apply long { ssize_t };
%apply const long & { const ssize_t & };
//===----------------------------------------------------------------------===//
// FIXME:
// Ideally all the typemaps should be revisited in a future SB API revision.
// Typemaps, usually, modifies the function signatures and might spawn
// different LLDB APIs across languages (C++, Python, Lua...).
// Historically, typemaps have been used to replace SWIG's deficiencies,
// but SWIG itself evolved and some API design choices are now redundant.
//===----------------------------------------------------------------------===//
// Typemap definitions to allow SWIG to properly handle char buffer.
// typemap for a char buffer
%typemap(in) (char *dst, size_t dst_len) {
$2 = luaL_checkinteger(L, $input);
if ($2 <= 0) {
return luaL_error(L, "Positive integer expected");
}
$1 = (char *)malloc($2);
}
// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
// as char data instead of byte data.
%typemap(in) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
// Also SBProcess::ReadMemory.
%typemap(in) (void *buf, size_t size) = (char *dst, size_t dst_len);
// Return the char buffer. Discarding any previous return result
%typemap(argout) (char *dst, size_t dst_len) {
lua_pop(L, 1); // Blow away the previous result
if ($result == 0) {
lua_pushliteral(L, "");
} else {
lua_pushlstring(L, (const char *)$1, $result);
}
free($1);
// SWIG_arg was already incremented
}
// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
// as char data instead of byte data.
%typemap(argout) (void *char_buf, size_t size) = (char *dst, size_t dst_len);
// Also SBProcess::ReadMemory.
%typemap(argout) (void *buf, size_t size) = (char *dst, size_t dst_len);
//===----------------------------------------------------------------------===//
// Typemap for handling a snprintf-like API like SBThread::GetStopDescription.
%typemap(in) (char *dst_or_null, size_t dst_len) {
$2 = luaL_checkinteger(L, $input);
if ($2 <= 0) {
return luaL_error(L, "Positive integer expected");
}
$1 = (char *)malloc($2);
}
%typemap(argout) (char *dst_or_null, size_t dst_len) {
lua_pop(L, 1); // Blow away the previous result
lua_pushlstring(L, (const char *)$1, $result);
free($1);
// SWIG_arg was already incremented
}
//===----------------------------------------------------------------------===//
// Typemap for handling SBModule::GetVersion
%typemap(in) (uint32_t *versions, uint32_t num_versions) {
$2 = 99;
$1 = (uint32_t *)malloc(sizeof(uint32_t) * $2);
}
%typemap(argout) (uint32_t *versions, uint32_t num_versions) {
uint32_t count = result;
if (count >= $2)
count = $2;
lua_newtable(L);
int i = 0;
while (i++ < count) {
lua_pushinteger(L, $1[i - 1]);
lua_seti(L, -2, i);
}
SWIG_arg++;
free($1);
}
//===----------------------------------------------------------------------===//
// Typemap for handling SBDebugger::SetLoggingCallback
%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) {
$1 = LLDBSwigLuaCallLuaLogOutputCallback;
$2 = (void *)L;
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_settop(L, 2);
lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback);
lua_insert(L, 2);
lua_settable(L, LUA_REGISTRYINDEX);
}
//===----------------------------------------------------------------------===//
// Typemap for handling SBEvent::SBEvent(uint32_t event, const char *cstr, uint32_t cstr_len)
%typemap(in) (const char *cstr, uint32_t cstr_len) {
$1 = (char *)luaL_checklstring(L, $input, (size_t *)&$2);
}
// Typemap for handling SBProcess::PutSTDIN
%typemap(in) (const char *src, size_t src_len) {
$1 = (char *)luaL_checklstring(L, $input, &$2);
}
// Typemap for handling SBProcess::WriteMemory, SBTarget::GetInstructions...
%typemap(in) (const void *buf, size_t size),
(const void *data, size_t data_len) {
$1 = (void *)luaL_checklstring(L, $input, &$2);
}
//===----------------------------------------------------------------------===//
// Typemap for handling char ** in SBTarget::LaunchSimple, SBTarget::Launch...
// It should accept a Lua table of strings, for stuff like "argv" and "envp".
%typemap(in) char ** {
if (lua_istable(L, $input)) {
size_t size = lua_rawlen(L, $input);
$1 = (char **)malloc((size + 1) * sizeof(char *));
int i = 0, j = 0;
while (i++ < size) {
lua_rawgeti(L, $input, i);
if (!lua_isstring(L, -1)) {
// if current element cannot be converted to string, raise an error
lua_pop(L, 1);
return luaL_error(L, "List should only contain strings");
}
$1[j++] = (char *)lua_tostring(L, -1);
lua_pop(L, 1);
}
$1[j] = 0;
} else if (lua_isnil(L, $input)) {
// "nil" is also acceptable, equivalent as an empty table
$1 = NULL;
} else {
return luaL_error(L, "A list of strings expected");
}
}
%typemap(freearg) char ** {
free((char *) $1);
}
%typecheck(SWIG_TYPECHECK_STRING_ARRAY) char ** {
$1 = (lua_istable(L, $input) || lua_isnil(L, $input));
}
//===----------------------------------------------------------------------===//
// Typemap for file handles (e.g. used in SBDebugger::SetOutputFile)
%typemap(in) lldb::FileSP {
luaL_Stream *p = (luaL_Stream *)luaL_checkudata(L, $input, LUA_FILEHANDLE);
lldb::FileSP file_sp;
file_sp = std::make_shared<lldb_private::NativeFile>(p->f, false);
if (!file_sp->IsValid())
return luaL_error(L, "Invalid file");
$1 = file_sp;
}
%typecheck(SWIG_TYPECHECK_POINTER) lldb::FileSP {
$1 = (lua_isuserdata(L, $input)) &&
(luaL_testudata(L, $input, LUA_FILEHANDLE) != nullptr);
}
// Typemap for file handles (e.g. used in SBDebugger::GetOutputFileHandle)
%typemap(out) lldb::FileSP {
lldb::FileSP sp = $1;
if (sp && sp->IsValid()) {
luaL_Stream *p = (luaL_Stream *)lua_newuserdata(L, sizeof(luaL_Stream));
p->closef = &LLDBSwigLuaCloseFileHandle;
p->f = sp->GetStream();
luaL_setmetatable(L, LUA_FILEHANDLE);
SWIG_arg++;
}
}
//===----------------------------------------------------------------------===//
// Typemap for SBData::CreateDataFromUInt64Array, SBData::SetDataFromUInt64Array ...
%typemap(in) (uint64_t* array, size_t array_len),
(uint32_t* array, size_t array_len),
(int64_t* array, size_t array_len),
(int32_t* array, size_t array_len),
(double* array, size_t array_len) {
if (lua_istable(L, $input)) {
// It should accept a table of numbers.
$2 = lua_rawlen(L, $input);
$1 = ($1_ltype)malloc(($2) * sizeof($*1_type));
int i = 0, j = 0;
while (i++ < $2) {
lua_rawgeti(L, $input, i);
if (!lua_isnumber(L, -1)) {
// if current element cannot be converted to number, raise an error
lua_pop(L, 1);
return luaL_error(L, "List should only contain numbers");
}
$1[j++] = ($*1_ltype) lua_tonumber(L, -1);
lua_pop(L, 1);
}
} else if (lua_isnil(L, $input)) {
// "nil" is also acceptable, equivalent as an empty table
$1 = NULL;
$2 = 0;
} else {
// else raise an error
return luaL_error(L, "A list of numbers expected.");
}
}
%typemap(freearg) (uint64_t* array, size_t array_len),
(uint32_t* array, size_t array_len),
(int64_t* array, size_t array_len),
(int32_t* array, size_t array_len),
(double* array, size_t array_len) {
free($1);
}
//===----------------------------------------------------------------------===//
// Typemap for SBCommandReturnObject::PutCString
%typemap(in) (const char *string, int len) {
if (lua_isnil(L, $input)) {
$1 = NULL;
$2 = 0;
} else {
$1 = (char *)luaL_checklstring(L, $input, (size_t *)&$2);
}
}
//===----------------------------------------------------------------------===//
|