Added more Lua tests that are passing.

Also ripped out the ctype checking in upb_table, it
was not helpful (didn't help catch bugs) but was causing
problems.
pull/13171/head
Joshua Haberman 5 years ago
parent ae66e571d4
commit d6c3152c0b
  1. 26
      tests/bindings/lua/main.c
  2. 295
      tests/bindings/lua/test_upb.lua
  3. 2
      upb/bindings/lua/msg.c
  4. 2
      upb/msg.c
  5. 58
      upb/table.c
  6. 46
      upb/table.int.h

@ -2,23 +2,43 @@
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <signal.h>
#include "upb/bindings/lua/upb.h"
lua_State *L;
static void interrupt(lua_State *L, lua_Debug *ar) {
(void)ar;
lua_sethook(L, NULL, 0, 0);
luaL_error(L, "SIGINT");
}
static void sighandler(int i) {
fprintf(stderr, "Signal!\n");
signal(i, SIG_DFL);
lua_sethook(L, interrupt, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
}
const char *init =
"package.preload['lupb'] = ... "
"package.path = './?.lua;./third_party/lunit/?.lua'";
int main() {
int ret = 0;
lua_State *L = luaL_newstate();
L = luaL_newstate();
luaL_openlibs(L);
lua_pushcfunction(L, luaopen_lupb);
ret = luaL_loadstring(L, init);
lua_pushcfunction(L, luaopen_lupb);
if (ret || lua_pcall(L, 1, LUA_MULTRET, 0) ||
luaL_dofile(L, "tests/bindings/lua/test_upb.lua")) {
signal(SIGINT, sighandler);
ret = ret ||
lua_pcall(L, 1, LUA_MULTRET, 0) ||
luaL_dofile(L, "tests/bindings/lua/test_upb.lua");
signal(SIGINT, SIG_DFL);
if (ret) {
fprintf(stderr, "error testing Lua: %s\n", lua_tostring(L, -1));
ret = 1;
}

@ -405,122 +405,6 @@ function test_symtab()
assert_equal(msgdef3:field("field5"):subdef(), msgdef2)
end
function test_numeric_array()
local function test_for_numeric_type(upb_type, val, too_big, too_small, bad3)
local array = upb.Array(upb_type)
assert_equal(0, #array)
-- 0 is never a valid index in Lua.
assert_error_match("array index", function() return array[0] end)
-- Past the end of the array.
assert_error_match("array index", function() return array[1] end)
array[1] = val
assert_equal(val, array[1])
assert_equal(1, #array)
assert_equal(val, array[1])
-- Past the end of the array.
assert_error_match("array index", function() return array[2] end)
array[2] = 10
assert_equal(val, array[1])
assert_equal(10, array[2])
assert_equal(2, #array)
-- Past the end of the array.
assert_error_match("array index", function() return array[3] end)
local n = 1
for i, val in upb.ipairs(array) do
assert_equal(n, i)
n = n + 1
assert_equal(array[i], val)
end
-- Values that are out of range.
local errmsg = "not an integer or out of range"
if too_small then
assert_error_match(errmsg, function() array[3] = too_small end)
end
if too_big then
assert_error_match(errmsg, function() array[3] = too_big end)
end
if bad3 then
assert_error_match(errmsg, function() array[3] = bad3 end)
end
-- Can't assign other Lua types.
errmsg = "bad argument #3"
assert_error_match(errmsg, function() array[3] = "abc" end)
assert_error_match(errmsg, function() array[3] = true end)
assert_error_match(errmsg, function() array[3] = false end)
assert_error_match(errmsg, function() array[3] = nil end)
assert_error_match(errmsg, function() array[3] = {} end)
assert_error_match(errmsg, function() array[3] = print end)
assert_error_match(errmsg, function() array[3] = array end)
end
-- in-range of 64-bit types but not exactly representable as double
local bad64 = 2^68 - 1
test_for_numeric_type(upb.TYPE_UINT32, 2^32 - 1, 2^32, -1, 5.1)
test_for_numeric_type(upb.TYPE_UINT64, 2^63, 2^64, -1, bad64)
test_for_numeric_type(upb.TYPE_INT32, 2^31 - 1, 2^31, -2^31 - 1, 5.1)
-- Enums don't exist at a language level in Lua, so we just represent enum
-- values as int32s.
test_for_numeric_type(upb.TYPE_ENUM, 2^31 - 1, 2^31, -2^31 - 1, 5.1)
test_for_numeric_type(upb.TYPE_INT64, 2^62, 2^63, -2^64, bad64)
test_for_numeric_type(upb.TYPE_FLOAT, 340282306073709652508363335590014353408)
test_for_numeric_type(upb.TYPE_DOUBLE, 10^101)
end
function test_string_array()
local function test_for_string_type(upb_type)
local array = upb.Array(upb_type)
assert_equal(0, #array)
-- 0 is never a valid index in Lua.
assert_error_match("array index", function() return array[0] end)
-- Past the end of the array.
assert_error_match("array index", function() return array[1] end)
array[1] = "foo"
assert_equal("foo", array[1])
assert_equal(1, #array)
-- Past the end of the array.
assert_error_match("array index", function() return array[2] end)
local array2 = upb.Array(upb_type)
assert_equal(0, #array2)
array[2] = "bar"
assert_equal("foo", array[1])
assert_equal("bar", array[2])
assert_equal(2, #array)
-- Past the end of the array.
assert_error_match("array index", function() return array[3] end)
local n = 1
for i, val in upb.ipairs(array) do
assert_equal(n, i)
n = n + 1
assert_equal(array[i], val)
end
assert_equal(3, n)
-- Can't assign other Lua types.
assert_error_match("Expected string", function() array[3] = 123 end)
assert_error_match("Expected string", function() array[3] = true end)
assert_error_match("Expected string", function() array[3] = false end)
assert_error_match("Expected string", function() array[3] = nil end)
assert_error_match("Expected string", function() array[3] = {} end)
assert_error_match("Expected string", function() array[3] = print end)
assert_error_match("Expected string", function() array[3] = array end)
end
test_for_string_type(upb.TYPE_STRING)
test_for_string_type(upb.TYPE_BYTES)
end
function test_msg_primitives()
local function test_for_numeric_type(upb_type, val, too_big, too_small, bad3)
local symtab = upb.SymbolTable{
@ -699,6 +583,8 @@ function test_msg_submsg()
assert_error_match("msg expected", function() msg.submsg = print end)
end
--]]
-- Lua 5.1 and 5.2 have slightly different semantics for how a finalizer
-- can be defined in Lua.
if _VERSION >= 'Lua 5.2' then
@ -719,33 +605,174 @@ function test_finalizer()
defer(function()
assert_error_match("called into dead object", function()
-- Generic def call.
t[1]:full_name()
end)
assert_error_match("called into dead object", function()
-- Specific msgdef call.
t[1]:add()
end)
assert_error_match("called into dead object", function()
t[2]:values()
end)
assert_error_match("called into dead object", function()
t[3]:number()
end)
assert_error_match("called into dead object", function()
t[4]:lookup()
t[1]:lookup_msg("abc")
end)
end)
t = {
upb.MessageDef(),
upb.EnumDef(),
upb.FieldDef(),
upb.SymbolTable(),
}
end
collectgarbage()
end
--]]
-- in-range of 64-bit types but not exactly representable as double
local bad64 = 2^68 - 1
local numeric_types = {
[upb.TYPE_UINT32] = {
valid_val = 2^32 - 1,
too_big = 2^32,
too_small = -1,
other_bad = 5.1
},
[upb.TYPE_UINT64] = {
valid_val = 2^63,
too_big = 2^64,
too_small = -1,
other_bad = bad64
},
[upb.TYPE_INT32] = {
valid_val = 2^31 - 1,
too_big = 2^31,
too_small = -2^31 - 1,
other_bad = 5.1
},
-- Enums don't exist at a language level in Lua, so we just represent enum
-- values as int32s.
[upb.TYPE_ENUM] = {
valid_val = 2^31 - 1,
too_big = 2^31,
too_small = -2^31 - 1,
other_bad = 5.1
},
[upb.TYPE_INT64] = {
valid_val = 2^62,
too_big = 2^63,
too_small = -2^64,
other_bad = bad64
},
[upb.TYPE_FLOAT] = {
valid_val = 340282306073709652508363335590014353408
},
[upb.TYPE_DOUBLE] = {
valid_val = 10^101
},
}
function test_string_array()
local function test_for_string_type(upb_type)
local array = upb.Array(upb_type)
assert_equal(0, #array)
-- 0 is never a valid index in Lua.
assert_error_match("array index", function() return array[0] end)
-- Past the end of the array.
assert_error_match("array index", function() return array[1] end)
array[1] = "foo"
assert_equal("foo", array[1])
assert_equal(1, #array)
-- Past the end of the array.
assert_error_match("array index", function() return array[2] end)
local array2 = upb.Array(upb_type)
assert_equal(0, #array2)
array[2] = "bar"
assert_equal("foo", array[1])
assert_equal("bar", array[2])
assert_equal(2, #array)
-- Past the end of the array.
assert_error_match("array index", function() return array[3] end)
-- Can't assign other Lua types.
assert_error_match("Expected string", function() array[3] = 123 end)
assert_error_match("Expected string", function() array[3] = true end)
assert_error_match("Expected string", function() array[3] = false end)
assert_error_match("Expected string", function() array[3] = nil end)
assert_error_match("Expected string", function() array[3] = {} end)
assert_error_match("Expected string", function() array[3] = print end)
assert_error_match("Expected string", function() array[3] = array end)
end
test_for_string_type(upb.TYPE_STRING)
test_for_string_type(upb.TYPE_BYTES)
end
function test_numeric_array()
local function test_for_numeric_type(upb_type)
local array = upb.Array(upb_type)
local vals = numeric_types[upb_type]
assert_equal(0, #array)
-- 0 is never a valid index in Lua.
assert_error_match("array index", function() return array[0] end)
-- Past the end of the array.
assert_error_match("array index", function() return array[1] end)
array[1] = vals.valid_val
assert_equal(vals.valid_val, array[1])
assert_equal(1, #array)
assert_equal(vals.valid_val, array[1])
-- Past the end of the array.
assert_error_match("array index", function() return array[2] end)
array[2] = 10
assert_equal(vals.valid_val, array[1])
assert_equal(10, array[2])
assert_equal(2, #array)
-- Past the end of the array.
assert_error_match("array index", function() return array[3] end)
-- Values that are out of range.
local errmsg = "not an integer or out of range"
if vals.too_small then
assert_error_match(errmsg, function() array[3] = vals.too_small end)
end
if vals.too_big then
assert_error_match(errmsg, function() array[3] = vals.too_big end)
end
if vals.other_bad then
assert_error_match(errmsg, function() array[3] = vals.other_bad end)
end
-- Can't assign other Lua types.
errmsg = "bad argument #3"
assert_error_match(errmsg, function() array[3] = "abc" end)
assert_error_match(errmsg, function() array[3] = true end)
assert_error_match(errmsg, function() array[3] = false end)
assert_error_match(errmsg, function() array[3] = nil end)
assert_error_match(errmsg, function() array[3] = {} end)
assert_error_match(errmsg, function() array[3] = print end)
assert_error_match(errmsg, function() array[3] = array end)
end
for k in pairs(numeric_types) do
test_for_numeric_type(k)
end
end
function test_numeric_map()
local function test_for_numeric_types(key_type, val_type)
local map = upb.Map(key_type, val_type)
local key_vals = numeric_types[key_type]
local val_vals = numeric_types[val_type]
assert_equal(0, #map)
-- Unset keys return nil
assert_nil(map[key_vals.valid_val])
map[key_vals.valid_val] = val_vals.valid_val
assert_equal(1, #map)
end
for k in pairs(numeric_types) do
for v in pairs(numeric_types) do
test_for_numeric_types(k, v)
end
end
end
function test_foo()
local symtab = upb.SymbolTable()

@ -510,7 +510,7 @@ typedef struct {
#define MAP_MSGDEF_INDEX 1
static lupb_map *lupb_map_check(lua_State *L, int narg) {
return luaL_checkudata(L, narg, LUPB_ARRAY);
return luaL_checkudata(L, narg, LUPB_MAP);
}
/* lupb_map Public API */

@ -197,7 +197,7 @@ upb_map *upb_map_new(upb_arena *a, upb_fieldtype_t key_type,
return NULL;
}
upb_strtable_init(&map->table, UPB_CTYPE_INT32);
upb_strtable_init2(&map->table, UPB_CTYPE_INT32, upb_arena_alloc(a));
map->key_size_lg2 = _upb_fieldtype_to_mapsizelg2[key_type];
map->val_size_lg2 = _upb_fieldtype_to_mapsizelg2[value_type];

@ -16,12 +16,6 @@
#define ARRAY_SIZE(x) \
((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
static void upb_check_alloc(upb_table *t, upb_alloc *a) {
UPB_UNUSED(t);
UPB_UNUSED(a);
UPB_ASSERT_DEBUGVAR(t->alloc == a);
}
static const double MAX_LOAD = 0.85;
/* The minimum utilization of the array part of a mixed hash/array table. This
@ -100,17 +94,12 @@ static bool isfull(upb_table *t) {
}
}
static bool init(upb_table *t, upb_ctype_t ctype, uint8_t size_lg2,
upb_alloc *a) {
static bool init(upb_table *t, uint8_t size_lg2, upb_alloc *a) {
size_t bytes;
t->count = 0;
t->ctype = ctype;
t->size_lg2 = size_lg2;
t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
#ifndef NDEBUG
t->alloc = a;
#endif
bytes = upb_table_size(t) * sizeof(upb_tabent);
if (bytes > 0) {
t->entries = upb_malloc(a, bytes);
@ -123,7 +112,6 @@ static bool init(upb_table *t, upb_ctype_t ctype, uint8_t size_lg2,
}
static void uninit(upb_table *t, upb_alloc *a) {
upb_check_alloc(t, a);
upb_free(a, mutable_entries(t));
}
@ -159,7 +147,7 @@ static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v,
const upb_tabent *e = findentry(t, key, hash, eql);
if (e) {
if (v) {
_upb_value_setval(v, e->val.val, t->ctype);
_upb_value_setval(v, e->val.val);
}
return true;
} else {
@ -175,7 +163,6 @@ static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey,
upb_tabent *our_e;
UPB_ASSERT(findentry(t, key, hash, eql) == NULL);
UPB_ASSERT_DEBUGVAR(val.ctype == t->ctype);
t->count++;
mainpos_e = getentry_mutable(t, hash);
@ -221,7 +208,7 @@ static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
if (eql(chain->key, key)) {
/* Element to remove is at the head of its chain. */
t->count--;
if (val) _upb_value_setval(val, chain->val.val, t->ctype);
if (val) _upb_value_setval(val, chain->val.val);
if (removed) *removed = chain->key;
if (chain->next) {
upb_tabent *move = (upb_tabent*)chain->next;
@ -241,7 +228,7 @@ static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
/* Found element to remove. */
upb_tabent *rm = (upb_tabent*)chain->next;
t->count--;
if (val) _upb_value_setval(val, chain->next->val.val, t->ctype);
if (val) _upb_value_setval(val, chain->next->val.val);
if (removed) *removed = rm->key;
rm->key = 0; /* Make the slot empty. */
chain->next = rm->next;
@ -294,7 +281,7 @@ static bool streql(upb_tabkey k1, lookupkey_t k2) {
}
bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) {
return init(&t->t, ctype, 2, a);
return init(&t->t, 2, a);
}
void upb_strtable_uninit2(upb_strtable *t, upb_alloc *a) {
@ -308,9 +295,7 @@ bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a) {
upb_strtable new_table;
upb_strtable_iter i;
upb_check_alloc(&t->t, a);
if (!init(&new_table.t, t->t.ctype, size_lg2, a))
if (!init(&new_table.t, size_lg2, a))
return false;
upb_strtable_begin(&i, t);
for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) {
@ -330,8 +315,6 @@ bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len,
upb_tabkey tabkey;
uint32_t hash;
upb_check_alloc(&t->t, a);
if (isfull(&t->t)) {
/* Need to resize. New table of double the size, add old elements to it. */
if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) {
@ -398,7 +381,7 @@ upb_strview upb_strtable_iter_key(const upb_strtable_iter *i) {
upb_value upb_strtable_iter_value(const upb_strtable_iter *i) {
UPB_ASSERT(!upb_strtable_done(i));
return _upb_value_val(str_tabent(i)->val.val, i->t->t.ctype);
return _upb_value_val(str_tabent(i)->val.val);
}
void upb_strtable_iter_setdone(upb_strtable_iter *i) {
@ -464,11 +447,11 @@ static void check(upb_inttable *t) {
#endif
}
bool upb_inttable_sizedinit(upb_inttable *t, upb_ctype_t ctype,
size_t asize, int hsize_lg2, upb_alloc *a) {
bool upb_inttable_sizedinit(upb_inttable *t, size_t asize, int hsize_lg2,
upb_alloc *a) {
size_t array_bytes;
if (!init(&t->t, ctype, hsize_lg2, a)) return false;
if (!init(&t->t, hsize_lg2, a)) return false;
/* Always make the array part at least 1 long, so that we know key 0
* won't be in the hash part, which simplifies things. */
t->array_size = UPB_MAX(1, asize);
@ -485,7 +468,7 @@ bool upb_inttable_sizedinit(upb_inttable *t, upb_ctype_t ctype,
}
bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) {
return upb_inttable_sizedinit(t, ctype, 0, 4, a);
return upb_inttable_sizedinit(t, 0, 4, a);
}
void upb_inttable_uninit2(upb_inttable *t, upb_alloc *a) {
@ -499,8 +482,6 @@ bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
tabval.val = val.val;
UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */
upb_check_alloc(&t->t, a);
if (key < t->array_size) {
UPB_ASSERT(!upb_arrhas(t->array[key]));
t->array_count++;
@ -511,7 +492,7 @@ bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
size_t i;
upb_table new_table;
if (!init(&new_table, t->t.ctype, t->t.size_lg2 + 1, a)) {
if (!init(&new_table, t->t.size_lg2 + 1, a)) {
return false;
}
@ -520,7 +501,7 @@ bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
uint32_t hash;
upb_value v;
_upb_value_setval(&v, e->val.val, t->t.ctype);
_upb_value_setval(&v, e->val.val);
hash = upb_inthash(e->key);
insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql);
}
@ -539,7 +520,7 @@ bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) {
const upb_tabval *table_v = inttable_val_const(t, key);
if (!table_v) return false;
if (v) _upb_value_setval(v, table_v->val, t->t.ctype);
if (v) _upb_value_setval(v, table_v->val);
return true;
}
@ -557,7 +538,7 @@ bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
upb_tabval empty = UPB_TABVALUE_EMPTY_INIT;
t->array_count--;
if (val) {
_upb_value_setval(val, t->array[key].val, t->t.ctype);
_upb_value_setval(val, t->array[key].val);
}
mutable_array(t)[key] = empty;
success = true;
@ -572,7 +553,6 @@ bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
}
bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a) {
upb_check_alloc(&t->t, a);
return upb_inttable_insert2(t, upb_inttable_count(t), val, a);
}
@ -585,7 +565,6 @@ upb_value upb_inttable_pop(upb_inttable *t) {
bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
upb_alloc *a) {
upb_check_alloc(&t->t, a);
return upb_inttable_insert2(t, (uintptr_t)key, val, a);
}
@ -610,8 +589,6 @@ void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) {
int size_lg2;
upb_inttable new_t;
upb_check_alloc(&t->t, a);
upb_inttable_begin(&i, t);
for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
uintptr_t key = upb_inttable_iter_key(&i);
@ -644,7 +621,7 @@ void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) {
size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0;
int hashsize_lg2 = log2ceil(hash_size);
upb_inttable_sizedinit(&new_t, t->t.ctype, arr_size, hashsize_lg2, a);
upb_inttable_sizedinit(&new_t, arr_size, hashsize_lg2, a);
upb_inttable_begin(&i, t);
for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
uintptr_t k = upb_inttable_iter_key(&i);
@ -710,8 +687,7 @@ uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) {
upb_value upb_inttable_iter_value(const upb_inttable_iter *i) {
UPB_ASSERT(!upb_inttable_done(i));
return _upb_value_val(
i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val,
i->t->t.ctype);
i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val);
}
void upb_inttable_iter_setdone(upb_inttable_iter *i) {

@ -52,19 +52,8 @@ typedef enum {
typedef struct {
uint64_t val;
#ifndef NDEBUG
/* In debug mode we carry the value type around also so we can check accesses
* to be sure the right member is being read. */
upb_ctype_t ctype;
#endif
} upb_value;
#ifdef NDEBUG
#define SET_TYPE(dest, val) UPB_UNUSED(val)
#else
#define SET_TYPE(dest, val) dest = val
#endif
/* Like strdup(), which isn't always available since it's not ANSI C. */
char *upb_strdup(const char *s, upb_alloc *a);
/* Variant that works with a length-delimited rather than NULL-delimited string,
@ -75,15 +64,13 @@ UPB_INLINE char *upb_gstrdup(const char *s) {
return upb_strdup(s, &upb_alloc_global);
}
UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val,
upb_ctype_t ctype) {
UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val) {
v->val = val;
SET_TYPE(v->ctype, ctype);
}
UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) {
UPB_INLINE upb_value _upb_value_val(uint64_t val) {
upb_value ret;
_upb_value_setval(&ret, val, ctype);
_upb_value_setval(&ret, val);
return ret;
}
@ -98,7 +85,6 @@ UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) {
#define FUNCS(name, membername, type_t, converter, proto_type) \
UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
val->val = (converter)cval; \
SET_TYPE(val->ctype, proto_type); \
} \
UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
upb_value ret; \
@ -106,7 +92,6 @@ UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) {
return ret; \
} \
UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
UPB_ASSERT_DEBUGVAR(val.ctype == proto_type); \
return (type_t)(converter)val.val; \
}
@ -124,12 +109,10 @@ FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR)
UPB_INLINE void upb_value_setfloat(upb_value *val, float cval) {
memcpy(&val->val, &cval, sizeof(cval));
SET_TYPE(val->ctype, UPB_CTYPE_FLOAT);
}
UPB_INLINE void upb_value_setdouble(upb_value *val, double cval) {
memcpy(&val->val, &cval, sizeof(cval));
SET_TYPE(val->ctype, UPB_CTYPE_DOUBLE);
}
UPB_INLINE upb_value upb_value_float(float cval) {
@ -173,7 +156,6 @@ typedef struct {
#define UPB_TABVALUE_EMPTY_INIT {-1}
/* upb_table ******************************************************************/
typedef struct _upb_tabent {
@ -190,7 +172,6 @@ typedef struct _upb_tabent {
typedef struct {
size_t count; /* Number of entries in the hash part. */
size_t mask; /* Mask to turn hash value -> bucket. */
upb_ctype_t ctype; /* Type of all values. */
uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
/* Hash table entries.
@ -200,17 +181,6 @@ typedef struct {
* initialize const hash tables. Then we cast away const when we have to.
*/
const upb_tabent *entries;
#ifndef NDEBUG
/* This table's allocator. We make the user pass it in to every relevant
* function and only use this to check it in debug mode. We do this solely
* to keep upb_table as small as possible. This might seem slightly paranoid
* but the plan is to use upb_table for all map fields and extension sets in
* a forthcoming message representation, so there could be a lot of these.
* If this turns out to be too annoying later, we can change it (since this
* is an internal-only header file). */
upb_alloc *alloc;
#endif
} upb_table;
typedef struct {
@ -224,12 +194,6 @@ typedef struct {
size_t array_count; /* Array part number of elements. */
} upb_inttable;
#define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \
{UPB_TABLE_INIT(count, mask, ctype, size_lg2, ent), a, asize, acount}
#define UPB_EMPTY_INTTABLE_INIT(ctype) \
UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0)
#define UPB_ARRAY_EMPTYENT -1
UPB_INLINE size_t upb_table_size(const upb_table *t) {
@ -399,7 +363,7 @@ UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key,
if (key < t->array_size) {
upb_tabval arrval = t->array[key];
if (upb_arrhas(arrval)) {
_upb_value_setval(v, arrval.val, t->t.ctype);
_upb_value_setval(v, arrval.val);
return true;
} else {
return false;
@ -409,7 +373,7 @@ UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key,
if (t->t.entries == NULL) return false;
for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) {
if ((uint32_t)e->key == key) {
_upb_value_setval(v, e->val.val, t->t.ctype);
_upb_value_setval(v, e->val.val);
return true;
}
if (e->next == NULL) return false;

Loading…
Cancel
Save