lua stack: userdata, key
| 187 | |
| 188 | // lua stack: userdata, key |
| 189 | int luabind::detail::class_rep::gettable(lua_State* L) |
| 190 | { |
| 191 | // if key is nil, return nil |
| 192 | if (lua_isnil(L, 2)) |
| 193 | { |
| 194 | lua_pushnil(L); |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, 1)); |
| 199 | |
| 200 | // we have to ignore the first argument since this may point to |
| 201 | // a method that is not present in this class (but in a subclass) |
| 202 | const char* key = lua_tostring(L, 2); |
| 203 | |
| 204 | #ifndef LUABIND_NO_ERROR_CHECKING |
| 205 | |
| 206 | if (std::strlen(key) != lua_strlen(L, 2)) |
| 207 | { |
| 208 | { |
| 209 | string_class msg("luabind does not support " |
| 210 | "member names with extra nulls:\n"); |
| 211 | msg += string_class(lua_tostring(L, 2), lua_strlen(L, 2)); |
| 212 | lua_pushstring(L, msg.c_str()); |
| 213 | } |
| 214 | lua_error(L); |
| 215 | } |
| 216 | |
| 217 | #endif |
| 218 | |
| 219 | // special case to see if this is a null-pointer |
| 220 | if (key && !std::strcmp(key, "__ok")) |
| 221 | { |
| 222 | class_rep* crep = obj->crep(); |
| 223 | |
| 224 | void* p = crep->extractor() ? crep->extractor()(obj->ptr()) |
| 225 | : obj->ptr(); |
| 226 | |
| 227 | lua_pushboolean(L, p != 0); |
| 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | // First, look in the instance's table |
| 232 | detail::lua_reference const& tbl = obj->get_lua_table(); |
| 233 | if (tbl.is_valid()) |
| 234 | { |
| 235 | tbl.get(L); |
| 236 | lua_pushvalue(L, 2); |
| 237 | lua_gettable(L, -2); |
| 238 | if (!lua_isnil(L, -1)) |
| 239 | { |
| 240 | lua_remove(L, -2); // more table |
| 241 | return 1; |
| 242 | } |
| 243 | lua_pop(L, 2); |
| 244 | } |
| 245 | |
| 246 | // Then look in the class' table for this member |
no test coverage detected