diff --git a/extend/lbson/src/bson.h b/extend/lbson/src/bson.h index 4e0ca7bf..db4b2b7a 100644 --- a/extend/lbson/src/bson.h +++ b/extend/lbson/src/bson.h @@ -137,15 +137,15 @@ namespace lbson { } int binary(lua_State* L) { + m_buffer.clean(); size_t data_len = 0; const char* value = lua_tolstring(L, 1, &data_len); - luaL_Buffer b; - luaL_buffinit(L, &b); - luaL_addchar(&b, 0); - luaL_addchar(&b, (uint8_t)bson_type::BSON_BINARY); - luaL_addchar(&b, 0); - luaL_addlstring(&b, value, data_len); - luaL_pushresult(&b); + m_buffer.write(0); + m_buffer.write((uint8_t)bson_type::BSON_BINARY); + m_buffer.write(0); //subtype + m_buffer.write(data_len); + m_buffer.push_data((uint8_t*)value, data_len); + lua_pushlstring(L, (const char*)m_buffer.head(), m_buffer.size()); return 1; } @@ -545,12 +545,13 @@ namespace lbson { break; case bson_type::BSON_BINARY: { lua_createtable(L, 0, 4); + int32_t len = read_val(L, slice); lua_pushinteger(L, (uint32_t)bt); lua_setfield(L, -2, "__type"); lua_pushinteger(L, read_val(L, slice)); lua_setfield(L, -2, "subtype"); - const char* s = read_bytes(L, slice, sz); - lua_pushlstring(L, s, sz); + const char* s = read_bytes(L, slice, len); + lua_pushlstring(L, s, len); lua_setfield(L, -2, "binray"); } break; @@ -639,7 +640,7 @@ namespace lbson { m_bson->unpack_dict(L, m_slice, false); } catch (const exception& e){ lua_settop(L, otop); - throw e; + throw lua_exception(e.what()); } return lua_gettop(L) - otop; }