| 1 |
/* vim: ts=4 sw=4 sts=4 et tw=78 |
| 2 |
* Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved. |
| 3 |
* Portions copyright (c) 2011 James R. McKaskill. |
| 4 |
* |
| 5 |
* This source code is licensed under the BSD-style license found in the |
| 6 |
* LICENSE file in the root directory of this source tree. An additional grant |
| 7 |
* of patent rights can be found in the PATENTS file in the same directory. |
| 8 |
*/ |
| 9 |
#include "ffi.h" |
| 10 |
|
| 11 |
#define LOCAL_ALIGN_MASK 15 |
| 12 |
#define LOCAL_ALIGNED_PTR(p) (void*) ( ((uintptr_t)p&LOCAL_ALIGN_MASK)?(((uintptr_t)p & ~LOCAL_ALIGN_MASK) + 0x10 ):(uintptr_t)p ) |
| 13 |
|
| 14 |
static int to_define_key; |
| 15 |
|
| 16 |
static void update_on_definition(lua_State* L, int ct_usr, int ct_idx) |
| 17 |
{ |
| 18 |
ct_usr = lua_absindex(L, ct_usr); |
| 19 |
ct_idx = lua_absindex(L, ct_idx); |
| 20 |
|
| 21 |
lua_pushlightuserdata(L, &to_define_key); |
| 22 |
lua_rawget(L, ct_usr); |
| 23 |
|
| 24 |
if (lua_isnil(L, -1)) { |
| 25 |
lua_pop(L, 1); /* pop the nil */ |
| 26 |
|
| 27 |
/* {} */ |
| 28 |
lua_newtable(L); |
| 29 |
|
| 30 |
/* {__mode='k'} */ |
| 31 |
lua_newtable(L); |
| 32 |
lua_pushliteral(L, "k"); |
| 33 |
lua_setfield(L, -2, "__mode"); |
| 34 |
|
| 35 |
/* setmetatable({}, {__mode='k'}) */ |
| 36 |
lua_setmetatable(L, -2); |
| 37 |
|
| 38 |
/* usr[TO_UPDATE_KEY] = setmetatable({}, {__mode='k'}) */ |
| 39 |
lua_pushlightuserdata(L, &to_define_key); |
| 40 |
lua_pushvalue(L, -2); |
| 41 |
lua_rawset(L, ct_usr); |
| 42 |
|
| 43 |
/* leave the table on the stack */ |
| 44 |
} |
| 45 |
|
| 46 |
/* to_update[ctype or cdata] = true */ |
| 47 |
lua_pushvalue(L, ct_idx); |
| 48 |
lua_pushboolean(L, 1); |
| 49 |
lua_rawset(L, -3); |
| 50 |
|
| 51 |
/* pop the to_update table */ |
| 52 |
lua_pop(L, 1); |
| 53 |
} |
| 54 |
|
| 55 |
void set_defined(lua_State* L, int ct_usr, struct ctype* ct) |
| 56 |
{ |
| 57 |
ct_usr = lua_absindex(L, ct_usr); |
| 58 |
|
| 59 |
ct->is_defined = 1; |
| 60 |
|
| 61 |
/* update ctypes and cdatas that were created before the definition came in */ |
| 62 |
lua_pushlightuserdata(L, &to_define_key); |
| 63 |
lua_rawget(L, ct_usr); |
| 64 |
|
| 65 |
if (!lua_isnil(L, -1)) { |
| 66 |
lua_pushnil(L); |
| 67 |
|
| 68 |
while (lua_next(L, -2)) { |
| 69 |
struct ctype* upd = (struct ctype*) lua_touserdata(L, -2); |
| 70 |
upd->base_size = ct->base_size; |
| 71 |
upd->align_mask = ct->align_mask; |
| 72 |
upd->is_defined = 1; |
| 73 |
upd->is_variable_struct = ct->is_variable_struct; |
| 74 |
upd->variable_increment = ct->variable_increment; |
| 75 |
assert(!upd->variable_size_known); |
| 76 |
lua_pop(L, 1); |
| 77 |
} |
| 78 |
|
| 79 |
lua_pop(L, 1); |
| 80 |
/* usr[TO_UPDATE_KEY] = nil */ |
| 81 |
lua_pushlightuserdata(L, &to_define_key); |
| 82 |
lua_pushnil(L); |
| 83 |
lua_rawset(L, ct_usr); |
| 84 |
} else { |
| 85 |
lua_pop(L, 1); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
struct ctype* push_ctype(lua_State* L, int ct_usr, const struct ctype* ct) |
| 90 |
{ |
| 91 |
struct ctype* ret; |
| 92 |
ct_usr = lua_absindex(L, ct_usr); |
| 93 |
|
| 94 |
ret = (struct ctype*) lua_newuserdata(L, sizeof(struct ctype)); |
| 95 |
/* |
| 96 |
{ |
| 97 |
void * p = NULL; |
| 98 |
p = lua_newuserdata(L, 16 + sizeof(struct ctype)); |
| 99 |
ret = (struct ctype*)LOCAL_ALIGNED_PTR(p); |
| 100 |
} |
| 101 |
*/ |
| 102 |
//*ret = *ct; 03-May-2022 trial fix |
| 103 |
memcpy(ret, ct, sizeof(struct ctype)); |
| 104 |
|
| 105 |
push_upval(L, &ctype_mt_key); |
| 106 |
lua_setmetatable(L, -2); |
| 107 |
|
| 108 |
#if LUA_VERSION_NUM == 501 |
| 109 |
if (!ct_usr || lua_isnil(L, ct_usr)) { |
| 110 |
push_upval(L, &niluv_key); |
| 111 |
lua_setfenv(L, -2); |
| 112 |
} |
| 113 |
#endif |
| 114 |
|
| 115 |
if (ct_usr && !lua_isnil(L, ct_usr)) { |
| 116 |
lua_pushvalue(L, ct_usr); |
| 117 |
lua_setuservalue(L, -2); |
| 118 |
} |
| 119 |
|
| 120 |
if (!ct->is_defined && ct_usr && !lua_isnil(L, ct_usr)) { |
| 121 |
update_on_definition(L, ct_usr, -1); |
| 122 |
} |
| 123 |
|
| 124 |
return ret; |
| 125 |
} |
| 126 |
|
| 127 |
size_t ctype_size(lua_State* L, const struct ctype* ct) |
| 128 |
{ |
| 129 |
if (ct->pointers - ct->is_array) { |
| 130 |
return sizeof(void*) * (ct->is_array ? ct->array_size : 1); |
| 131 |
|
| 132 |
} else if (!ct->is_defined || ct->type == VOID_TYPE) { |
| 133 |
return luaL_error(L, "can't calculate size of an undefined type"); |
| 134 |
|
| 135 |
} else if (ct->variable_size_known) { |
| 136 |
assert(ct->is_variable_struct && !ct->is_array); |
| 137 |
return ct->base_size + ct->variable_increment; |
| 138 |
|
| 139 |
} else if (ct->is_variable_array || ct->is_variable_struct) { |
| 140 |
return luaL_error(L, "internal error: calc size of variable type with unknown size"); |
| 141 |
|
| 142 |
} else { |
| 143 |
return ct->base_size * (ct->is_array ? ct->array_size : 1); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
void* push_cdata(lua_State* L, int ct_usr, const struct ctype* ct) |
| 148 |
{ |
| 149 |
struct cdata* cd; |
| 150 |
size_t sz = ct->is_reference ? sizeof(void*) : ctype_size(L, ct); |
| 151 |
ct_usr = lua_absindex(L, ct_usr); |
| 152 |
|
| 153 |
/* This is to stop valgrind from complaining. Bitfields are accessed in 8 |
| 154 |
* byte chunks so that the code doesn't have to deal with different access |
| 155 |
* patterns, but this means that occasionally it will read past the end of |
| 156 |
* the struct. As its not setting the bits past the end (only reading and |
| 157 |
* then writing the bits back) and the read is aligned its a non-issue, |
| 158 |
* but valgrind complains nonetheless. |
| 159 |
*/ |
| 160 |
if (ct->has_bitfield) { |
| 161 |
sz = ALIGN_UP(sz, 7); |
| 162 |
} |
| 163 |
|
| 164 |
{ |
| 165 |
void * p = NULL; |
| 166 |
//p = lua_newuserdata(L, 16 + sizeof(struct cdata) + sz); |
| 167 |
p = lua_newuserdata(L, sizeof(struct cdata) + sz); |
| 168 |
//cd = (struct cdata*)LOCAL_ALIGNED_PTR(p); |
| 169 |
cd = (struct cdata*)p; |
| 170 |
} |
| 171 |
memcpy((struct ctype*) &(cd->type), ct, sizeof(struct ctype)); |
| 172 |
memset((struct ctype*)cd+1, 0, sz); |
| 173 |
|
| 174 |
#if 0 |
| 175 |
/* TODO: handle cases where lua_newuserdata returns a pointer that is not |
| 176 |
* aligned */ |
| 177 |
assert((uintptr_t) (cd + 1) % 8 == 0); |
| 178 |
#endif |
| 179 |
|
| 180 |
#if LUA_VERSION_NUM == 501 |
| 181 |
if (!ct_usr || lua_isnil(L, ct_usr)) { |
| 182 |
push_upval(L, &niluv_key); |
| 183 |
lua_setfenv(L, -2); |
| 184 |
} |
| 185 |
#endif |
| 186 |
|
| 187 |
if (ct_usr && !lua_isnil(L, ct_usr)) { |
| 188 |
lua_pushvalue(L, ct_usr); |
| 189 |
lua_setuservalue(L, -2); |
| 190 |
} |
| 191 |
|
| 192 |
push_upval(L, &cdata_mt_key); |
| 193 |
lua_setmetatable(L, -2); |
| 194 |
|
| 195 |
if (!ct->is_defined && ct_usr && !lua_isnil(L, ct_usr)) { |
| 196 |
update_on_definition(L, ct_usr, -1); |
| 197 |
} |
| 198 |
|
| 199 |
return cd+1; |
| 200 |
} |
| 201 |
|
| 202 |
void push_callback(lua_State* L, cfunction luafunc, cfunction cfunc) |
| 203 |
{ |
| 204 |
cfunction* pf = (cfunction*) lua_newuserdata(L, 2 * sizeof(cfunction)); |
| 205 |
pf[0] = luafunc; |
| 206 |
pf[1] = cfunc; |
| 207 |
|
| 208 |
push_upval(L, &callback_mt_key); |
| 209 |
lua_setmetatable(L, -2); |
| 210 |
} |
| 211 |
|
| 212 |
/* returns the value as a ctype, pushes the user value onto the stack */ |
| 213 |
void check_ctype(lua_State* L, int idx, struct ctype* ct) |
| 214 |
{ |
| 215 |
if (lua_isstring(L, idx)) { |
| 216 |
struct parser P; |
| 217 |
P.line = 1; |
| 218 |
P.prev = P.next = lua_tostring(L, idx); |
| 219 |
P.align_mask = DEFAULT_ALIGN_MASK; |
| 220 |
parse_type(L, &P, ct); |
| 221 |
parse_argument(L, &P, -1, ct, NULL, NULL); |
| 222 |
lua_remove(L, -2); /* remove the user value from parse_type */ |
| 223 |
|
| 224 |
} else if (lua_getmetatable(L, idx)) { |
| 225 |
if (!equals_upval(L, -1, &ctype_mt_key) |
| 226 |
&& !equals_upval(L, -1, &cdata_mt_key)) { |
| 227 |
goto err; |
| 228 |
} |
| 229 |
|
| 230 |
lua_pop(L, 1); /* pop the metatable */ |
| 231 |
//*ct = *(struct ctype*) lua_touserdata(L, idx); |
| 232 |
memcpy(ct, lua_touserdata(L, idx), sizeof(struct ctype)); |
| 233 |
lua_getuservalue(L, idx); |
| 234 |
|
| 235 |
} else { |
| 236 |
goto err; |
| 237 |
} |
| 238 |
|
| 239 |
return; |
| 240 |
|
| 241 |
err: |
| 242 |
luaL_error(L, "expected cdata, ctype or string for arg #%d", idx); |
| 243 |
} |
| 244 |
|
| 245 |
/* to_cdata returns the struct cdata* and pushes the user value onto the |
| 246 |
* stack. If the index is not a ctype then ct is set to the zero value such |
| 247 |
* that ct->type is INVALID_TYPE, a nil is pushed, and NULL is returned. */ |
| 248 |
void* to_cdata(lua_State* L, int idx, struct ctype* ct) |
| 249 |
{ |
| 250 |
struct cdata* cd; |
| 251 |
|
| 252 |
memset(ct, 0, sizeof(struct ctype)); |
| 253 |
if (!lua_isuserdata(L, idx) || !lua_getmetatable(L, idx)) { |
| 254 |
lua_pushnil(L); |
| 255 |
return NULL; |
| 256 |
} |
| 257 |
|
| 258 |
if (!equals_upval(L, -1, &cdata_mt_key)) { |
| 259 |
lua_pop(L, 1); /* mt */ |
| 260 |
lua_pushnil(L); |
| 261 |
return NULL; |
| 262 |
} |
| 263 |
|
| 264 |
lua_pop(L, 1); /* mt */ |
| 265 |
|
| 266 |
/* |
| 267 |
*/ |
| 268 |
cd = (struct cdata*) lua_touserdata(L, idx); |
| 269 |
memcpy(ct, &(cd->type), sizeof(struct ctype)); |
| 270 |
/* |
| 271 |
{ |
| 272 |
void * p = NULL; |
| 273 |
p = lua_touserdata(L, idx); |
| 274 |
// *(struct ctype*) &cd->type = *ct; |
| 275 |
cd = (struct cdata*)LOCAL_ALIGNED_PTR(p); |
| 276 |
printf("%s:%d p=[%p] cd = [%p]\n", __FILE__, __LINE__, p, cd); |
| 277 |
} |
| 278 |
memcpy(ct, &(cd->type), sizeof(struct ctype)); |
| 279 |
*/ |
| 280 |
|
| 281 |
lua_getuservalue(L, idx); |
| 282 |
|
| 283 |
if (ct->is_reference) { |
| 284 |
return *(void**) (cd+1); |
| 285 |
|
| 286 |
} else if (ct->pointers && !ct->is_array) { |
| 287 |
return *(void**) (cd+1); |
| 288 |
|
| 289 |
} else { |
| 290 |
return cd + 1; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/* check_cdata returns the struct cdata* and pushes the user value onto the |
| 295 |
* stack. Also dereferences references. */ |
| 296 |
void* check_cdata(lua_State* L, int idx, struct ctype* ct) |
| 297 |
{ |
| 298 |
void* p = to_cdata(L, idx, ct); |
| 299 |
if (ct->type == INVALID_TYPE) { |
| 300 |
luaL_error(L, "expected cdata for arg #%d", idx); |
| 301 |
} |
| 302 |
return p; |
| 303 |
} |
| 304 |
|