| 751 | } |
| 752 | |
| 753 | void ofxLua::clearTable() { |
| 754 | if(!isValid()) { |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | if(tables.empty()) { |
| 759 | ofLogWarning("ofxLua") << "No table to clear, did you push?"; |
| 760 | return; |
| 761 | } |
| 762 | if(!lua_istable(L, LUA_STACK_TOP)) { |
| 763 | ofLogWarning("ofxLua") << "Couldn't clear table, stack var is not a table"; |
| 764 | } |
| 765 | |
| 766 | // clears all elements by setting them to nil, this way the original table |
| 767 | // pointer is preserved |
| 768 | lua_pushvalue(L, LUA_STACK_TOP); // stack: -1 => table |
| 769 | lua_pushnil(L); // stack: -1 => table; -2 => nil |
| 770 | |
| 771 | while(lua_next(L, -2)) { |
| 772 | // stack: -3 => table; -2 => key; -1 => value |
| 773 | lua_pop(L, 1); // stack: -2 => table; -1 => key |
| 774 | |
| 775 | lua_pushvalue(L, -1); |
| 776 | lua_pushnil(L); |
| 777 | lua_settable(L, -4); // stack: -2 => table; -1 => key |
| 778 | } |
| 779 | |
| 780 | // stack: -1 => table |
| 781 | lua_pop(L, 1); // stack: |
| 782 | } |
| 783 | |
| 784 | void ofxLua::clearTable(const std::string& tableName) { |
| 785 | if(!pushTable(tableName)) { |
no test coverage detected