|
|
|
@ -159,10 +159,11 @@ static int lupb_fielddef_name(lua_State *L) { |
|
|
|
|
static int lupb_fielddef_number(lua_State *L) { |
|
|
|
|
const upb_fielddef *f = lupb_fielddef_check(L, 1); |
|
|
|
|
int32_t num = upb_fielddef_number(f); |
|
|
|
|
if (num) |
|
|
|
|
if (num) { |
|
|
|
|
lua_pushinteger(L, num); |
|
|
|
|
else |
|
|
|
|
} else { |
|
|
|
|
lua_pushnil(L); |
|
|
|
|
} |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -224,58 +225,72 @@ static int lupb_oneofdef_containingtype(lua_State *L) { |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* lupb_oneofdef_field()
|
|
|
|
|
* |
|
|
|
|
* Handles: |
|
|
|
|
* oneof.field(field_number) |
|
|
|
|
* oneof.field(field_name) |
|
|
|
|
*/ |
|
|
|
|
static int lupb_oneofdef_field(lua_State *L) { |
|
|
|
|
const upb_oneofdef *o = lupb_oneofdef_check(L, 1); |
|
|
|
|
const upb_fielddef *f; |
|
|
|
|
int32_t idx = lupb_checkint32(L, 2); |
|
|
|
|
int count = upb_oneofdef_fieldcount(o); |
|
|
|
|
|
|
|
|
|
switch (lua_type(L, 2)) { |
|
|
|
|
case LUA_TNUMBER: |
|
|
|
|
f = upb_oneofdef_itof(o, lua_tointeger(L, 2)); |
|
|
|
|
break; |
|
|
|
|
case LUA_TSTRING: |
|
|
|
|
f = upb_oneofdef_ntofz(o, lua_tostring(L, 2)); |
|
|
|
|
break; |
|
|
|
|
default: { |
|
|
|
|
const char *msg = lua_pushfstring(L, "number or string expected, got %s", |
|
|
|
|
luaL_typename(L, 2)); |
|
|
|
|
return luaL_argerror(L, 2, msg); |
|
|
|
|
} |
|
|
|
|
if (idx < 0 || idx >= count) { |
|
|
|
|
const char *msg = lua_pushfstring(L, "index %d exceeds field count %d", |
|
|
|
|
idx, count); |
|
|
|
|
return luaL_argerror(L, 2, msg); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lupb_wrapper_pushwrapper(L, 1, f, LUPB_FIELDDEF); |
|
|
|
|
lupb_wrapper_pushwrapper(L, 1, upb_oneofdef_field(o, idx), LUPB_FIELDDEF); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int lupb_oneofiter_next(lua_State *L) { |
|
|
|
|
upb_oneof_iter *i = lua_touserdata(L, lua_upvalueindex(1)); |
|
|
|
|
const upb_oneofdef *o = lupb_oneofdef_check(L, lua_upvalueindex(1)); |
|
|
|
|
int *index = lua_touserdata(L, lua_upvalueindex(2)); |
|
|
|
|
const upb_fielddef *f; |
|
|
|
|
if (upb_oneof_done(i)) return 0; |
|
|
|
|
f = upb_oneof_iter_field(i); |
|
|
|
|
upb_oneof_next(i); |
|
|
|
|
lupb_symtab_pushwrapper(L, lua_upvalueindex(2), f, LUPB_FIELDDEF); |
|
|
|
|
if (*index == upb_oneofdef_fieldcount(o)) return 0; |
|
|
|
|
f = upb_oneofdef_field(o, (*index)++); |
|
|
|
|
lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), f, LUPB_FIELDDEF); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int lupb_oneofdef_fields(lua_State *L) { |
|
|
|
|
const upb_oneofdef *o = lupb_oneofdef_check(L, 1); |
|
|
|
|
upb_oneof_iter *i = lua_newuserdata(L, sizeof(upb_oneof_iter)); |
|
|
|
|
lupb_wrapper_pushsymtab(L, 1); |
|
|
|
|
upb_oneof_begin(i, o); |
|
|
|
|
lupb_oneofdef_check(L, 1); |
|
|
|
|
int *index = lua_newuserdata(L, sizeof(int)); |
|
|
|
|
*index = 0; |
|
|
|
|
|
|
|
|
|
/* Closure upvalues are: iter, symtab. */ |
|
|
|
|
/* Closure upvalues are: oneofdef, index. */ |
|
|
|
|
lua_pushcclosure(L, &lupb_oneofiter_next, 2); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int lupb_oneofdef_len(lua_State *L) { |
|
|
|
|
const upb_oneofdef *o = lupb_oneofdef_check(L, 1); |
|
|
|
|
lua_pushinteger(L, upb_oneofdef_numfields(o)); |
|
|
|
|
lua_pushinteger(L, upb_oneofdef_fieldcount(o)); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* lupb_oneofdef_lookupfield()
|
|
|
|
|
* |
|
|
|
|
* Handles: |
|
|
|
|
* oneof.lookup_field(field_number) |
|
|
|
|
* oneof.lookup_field(field_name) |
|
|
|
|
*/ |
|
|
|
|
static int lupb_oneofdef_lookupfield(lua_State *L) { |
|
|
|
|
const upb_oneofdef *o = lupb_oneofdef_check(L, 1); |
|
|
|
|
const upb_fielddef *f; |
|
|
|
|
|
|
|
|
|
switch (lua_type(L, 2)) { |
|
|
|
|
case LUA_TNUMBER: |
|
|
|
|
f = upb_oneofdef_itof(o, lua_tointeger(L, 2)); |
|
|
|
|
break; |
|
|
|
|
case LUA_TSTRING: |
|
|
|
|
f = upb_oneofdef_ntofz(o, lua_tostring(L, 2)); |
|
|
|
|
break; |
|
|
|
|
default: { |
|
|
|
|
const char *msg = lua_pushfstring(L, "number or string expected, got %s", |
|
|
|
|
luaL_typename(L, 2)); |
|
|
|
|
return luaL_argerror(L, 2, msg); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lupb_wrapper_pushwrapper(L, 1, f, LUPB_FIELDDEF); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -289,6 +304,7 @@ static const struct luaL_Reg lupb_oneofdef_m[] = { |
|
|
|
|
{"containing_type", lupb_oneofdef_containingtype}, |
|
|
|
|
{"field", lupb_oneofdef_field}, |
|
|
|
|
{"fields", lupb_oneofdef_fields}, |
|
|
|
|
{"lookup_field", lupb_oneofdef_lookupfield}, |
|
|
|
|
{"name", lupb_oneofdef_name}, |
|
|
|
|
{NULL, NULL} |
|
|
|
|
}; |
|
|
|
@ -309,9 +325,15 @@ const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg) { |
|
|
|
|
return lupb_wrapper_check(L, narg, LUPB_MSGDEF); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int lupb_msgdef_len(lua_State *L) { |
|
|
|
|
static int lupb_msgdef_fieldcount(lua_State *L) { |
|
|
|
|
const upb_msgdef *m = lupb_msgdef_check(L, 1); |
|
|
|
|
lua_pushinteger(L, upb_msgdef_fieldcount(m)); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int lupb_msgdef_oneofcount(lua_State *L) { |
|
|
|
|
const upb_msgdef *m = lupb_msgdef_check(L, 1); |
|
|
|
|
lua_pushinteger(L, upb_msgdef_numfields(m)); |
|
|
|
|
lua_pushinteger(L, upb_msgdef_oneofcount(m)); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -376,23 +398,21 @@ static int lupb_msgdef_name(lua_State *L) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int lupb_msgfielditer_next(lua_State *L) { |
|
|
|
|
upb_msg_field_iter *i = lua_touserdata(L, lua_upvalueindex(1)); |
|
|
|
|
const upb_msgdef *m = lupb_msgdef_check(L, lua_upvalueindex(1)); |
|
|
|
|
int *index = lua_touserdata(L, lua_upvalueindex(2)); |
|
|
|
|
const upb_fielddef *f; |
|
|
|
|
|
|
|
|
|
if (upb_msg_field_done(i)) return 0; |
|
|
|
|
f = upb_msg_iter_field(i); |
|
|
|
|
lupb_symtab_pushwrapper(L, lua_upvalueindex(2), f, LUPB_FIELDDEF); |
|
|
|
|
upb_msg_field_next(i); |
|
|
|
|
if (*index == upb_msgdef_fieldcount(m)) return 0; |
|
|
|
|
f = upb_msgdef_field(m, (*index)++); |
|
|
|
|
lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), f, LUPB_FIELDDEF); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int lupb_msgdef_fields(lua_State *L) { |
|
|
|
|
const upb_msgdef *m = lupb_msgdef_check(L, 1); |
|
|
|
|
upb_msg_field_iter *i = lua_newuserdata(L, sizeof(upb_msg_field_iter)); |
|
|
|
|
lupb_wrapper_pushsymtab(L, 1); |
|
|
|
|
upb_msg_field_begin(i, m); |
|
|
|
|
lupb_msgdef_check(L, 1); |
|
|
|
|
int *index = lua_newuserdata(L, sizeof(int)); |
|
|
|
|
*index = 0; |
|
|
|
|
|
|
|
|
|
/* Closure upvalues are: iter, symtab. */ |
|
|
|
|
/* Closure upvalues are: msgdef, index. */ |
|
|
|
|
lua_pushcclosure(L, &lupb_msgfielditer_next, 2); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
@ -411,22 +431,21 @@ static int lupb_msgdef_fullname(lua_State *L) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int lupb_msgoneofiter_next(lua_State *L) { |
|
|
|
|
upb_msg_oneof_iter *i = lua_touserdata(L, lua_upvalueindex(1)); |
|
|
|
|
const upb_msgdef *m = lupb_msgdef_check(L, lua_upvalueindex(1)); |
|
|
|
|
int *index = lua_touserdata(L, lua_upvalueindex(2)); |
|
|
|
|
const upb_oneofdef *o; |
|
|
|
|
if (upb_msg_oneof_done(i)) return 0; |
|
|
|
|
o = upb_msg_iter_oneof(i); |
|
|
|
|
upb_msg_oneof_next(i); |
|
|
|
|
lupb_symtab_pushwrapper(L, lua_upvalueindex(2), o, LUPB_ONEOFDEF); |
|
|
|
|
if (*index == upb_msgdef_oneofcount(m)) return 0; |
|
|
|
|
o = upb_msgdef_oneof(m, (*index)++); |
|
|
|
|
lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), o, LUPB_ONEOFDEF); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int lupb_msgdef_oneofs(lua_State *L) { |
|
|
|
|
const upb_msgdef *m = lupb_msgdef_check(L, 1); |
|
|
|
|
upb_msg_oneof_iter *i = lua_newuserdata(L, sizeof(upb_msg_oneof_iter)); |
|
|
|
|
lupb_wrapper_pushsymtab(L, 1); |
|
|
|
|
upb_msg_oneof_begin(i, m); |
|
|
|
|
lupb_msgdef_check(L, 1); |
|
|
|
|
int *index = lua_newuserdata(L, sizeof(int)); |
|
|
|
|
*index = 0; |
|
|
|
|
|
|
|
|
|
/* Closure upvalues are: iter, symtab. */ |
|
|
|
|
/* Closure upvalues are: msgdef, index. */ |
|
|
|
|
lua_pushcclosure(L, &lupb_msgoneofiter_next, 2); |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
@ -452,7 +471,7 @@ static int lupb_msgdef_tostring(lua_State *L) { |
|
|
|
|
|
|
|
|
|
static const struct luaL_Reg lupb_msgdef_mm[] = { |
|
|
|
|
{"__call", lupb_msg_pushnew}, |
|
|
|
|
{"__len", lupb_msgdef_len}, |
|
|
|
|
{"__len", lupb_msgdef_fieldcount}, |
|
|
|
|
{"__tostring", lupb_msgdef_tostring}, |
|
|
|
|
{NULL, NULL} |
|
|
|
|
}; |
|
|
|
@ -460,10 +479,12 @@ static const struct luaL_Reg lupb_msgdef_mm[] = { |
|
|
|
|
static const struct luaL_Reg lupb_msgdef_m[] = { |
|
|
|
|
{"field", lupb_msgdef_field}, |
|
|
|
|
{"fields", lupb_msgdef_fields}, |
|
|
|
|
{"field_count", lupb_msgdef_fieldcount}, |
|
|
|
|
{"file", lupb_msgdef_file}, |
|
|
|
|
{"full_name", lupb_msgdef_fullname}, |
|
|
|
|
{"lookup_name", lupb_msgdef_lookupname}, |
|
|
|
|
{"name", lupb_msgdef_name}, |
|
|
|
|
{"oneof_count", lupb_msgdef_oneofcount}, |
|
|
|
|
{"oneofs", lupb_msgdef_oneofs}, |
|
|
|
|
{"syntax", lupb_msgdef_syntax}, |
|
|
|
|
{"_map_entry", lupb_msgdef_mapentry}, |
|
|
|
|