called from the metamethod for __newindex the object pointer is passed on the lua stack lua stack: userdata, key, value
| 270 | // the object pointer is passed on the lua stack |
| 271 | // lua stack: userdata, key, value |
| 272 | bool luabind::detail::class_rep::settable(lua_State* L) |
| 273 | { |
| 274 | // if the key is 'nil' fail |
| 275 | if (lua_isnil(L, 2)) return false; |
| 276 | |
| 277 | // we have to ignore the first argument since this may point to |
| 278 | // a method that is not present in this class (but in a subclass) |
| 279 | |
| 280 | const char* key = lua_tostring(L, 2); |
| 281 | |
| 282 | if (std::strlen(key) == lua_strlen(L, 2)) |
| 283 | { |
| 284 | map_class<const char*, callback, ltstr>::iterator j = m_setters.find(key); |
| 285 | if (j != m_setters.end()) |
| 286 | { |
| 287 | // the name is a data member |
| 288 | #ifndef LUABIND_NO_ERROR_CHECKING |
| 289 | if (j->second.match(L, 3) < 0) |
| 290 | { |
| 291 | string_class msg("the attribute '"); |
| 292 | msg += m_name; |
| 293 | msg += "."; |
| 294 | msg += key; |
| 295 | msg += "' is of type: "; |
| 296 | j->second.sig(L, msg); |
| 297 | msg += "\nand does not match: ("; |
| 298 | msg += stack_content_by_name(L, 3); |
| 299 | msg += ")"; |
| 300 | lua_pushstring(L, msg.c_str()); |
| 301 | return false; |
| 302 | } |
| 303 | #endif |
| 304 | j->second.func(L, j->second.pointer_offset); |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | if (m_getters.find(key) != m_getters.end()) |
| 309 | { |
| 310 | // this means that we have a getter but no |
| 311 | // setter for an attribute. We will then fail |
| 312 | // because that attribute is read-only |
| 313 | string_class msg("the attribute '"); |
| 314 | msg += m_name; |
| 315 | msg += "."; |
| 316 | msg += key; |
| 317 | msg += "' is read only"; |
| 318 | lua_pushstring(L, msg.c_str()); |
| 319 | return false; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // set the attribute to the object's table |
| 324 | object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, 1)); |
| 325 | detail::lua_reference& tbl = obj->get_lua_table(); |
| 326 | if (!tbl.is_valid()) |
| 327 | { |
| 328 | // this is the first time we are trying to add |
| 329 | // a member to this instance, create the table. |
no test coverage detected