Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
941 lines
29 KiB
941 lines
29 KiB
4 years ago
|
/*
|
||
4 years ago
|
* Copyright (c) 2009-2021, Google LLC
|
||
4 years ago
|
* All rights reserved.
|
||
|
*
|
||
|
* Redistribution and use in source and binary forms, with or without
|
||
|
* modification, are permitted provided that the following conditions are met:
|
||
|
* * Redistributions of source code must retain the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer.
|
||
|
* * Redistributions in binary form must reproduce the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer in the
|
||
|
* documentation and/or other materials provided with the distribution.
|
||
|
* * Neither the name of Google LLC nor the
|
||
|
* names of its contributors may be used to endorse or promote products
|
||
|
* derived from this software without specific prior written permission.
|
||
|
*
|
||
3 years ago
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
|
* ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
|
||
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||
4 years ago
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*/
|
||
8 years ago
|
|
||
2 years ago
|
#include "upb/reflection/def.h"
|
||
3 years ago
|
|
||
8 years ago
|
#include <float.h>
|
||
|
#include <math.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
3 years ago
|
|
||
8 years ago
|
#include "lauxlib.h"
|
||
2 years ago
|
#include "lua/upb.h"
|
||
3 years ago
|
#include "upb/reflection.h"
|
||
8 years ago
|
|
||
|
#define LUPB_ENUMDEF "lupb.enumdef"
|
||
3 years ago
|
#define LUPB_ENUMVALDEF "lupb.enumvaldef"
|
||
8 years ago
|
#define LUPB_FIELDDEF "lupb.fielddef"
|
||
|
#define LUPB_FILEDEF "lupb.filedef"
|
||
|
#define LUPB_MSGDEF "lupb.msgdef"
|
||
|
#define LUPB_ONEOFDEF "lupb.oneof"
|
||
3 years ago
|
#define LUPB_SYMTAB "lupb.defpool"
|
||
8 years ago
|
#define LUPB_OBJCACHE "lupb.objcache"
|
||
|
|
||
3 years ago
|
static void lupb_DefPool_pushwrapper(lua_State* L, int narg, const void* def,
|
||
|
const char* type);
|
||
8 years ago
|
|
||
6 years ago
|
/* lupb_wrapper ***************************************************************/
|
||
8 years ago
|
|
||
5 years ago
|
/* Wrappers around upb def objects. The userval contains a reference to the
|
||
3 years ago
|
* defpool. */
|
||
8 years ago
|
|
||
5 years ago
|
#define LUPB_SYMTAB_INDEX 1
|
||
|
|
||
5 years ago
|
typedef struct {
|
||
3 years ago
|
const void* def; /* upb_MessageDef, upb_EnumDef, upb_OneofDef, etc. */
|
||
5 years ago
|
} lupb_wrapper;
|
||
|
|
||
3 years ago
|
static const void* lupb_wrapper_check(lua_State* L, int narg,
|
||
|
const char* type) {
|
||
|
lupb_wrapper* w = luaL_checkudata(L, narg, type);
|
||
5 years ago
|
return w->def;
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
static void lupb_wrapper_pushdefpool(lua_State* L, int narg) {
|
||
5 years ago
|
lua_getiuservalue(L, narg, LUPB_SYMTAB_INDEX);
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
/* lupb_wrapper_pushwrapper()
|
||
|
*
|
||
|
* For a given def wrapper at index |narg|, pushes a wrapper for the given |def|
|
||
3 years ago
|
* and the given |type|. The new wrapper will be part of the same defpool. */
|
||
3 years ago
|
static void lupb_wrapper_pushwrapper(lua_State* L, int narg, const void* def,
|
||
|
const char* type) {
|
||
3 years ago
|
lupb_wrapper_pushdefpool(L, narg);
|
||
3 years ago
|
lupb_DefPool_pushwrapper(L, -1, def, type);
|
||
3 years ago
|
lua_replace(L, -2); /* Remove defpool from stack. */
|
||
5 years ago
|
}
|
||
8 years ago
|
|
||
3 years ago
|
/* lupb_MessageDef_pushsubmsgdef()
|
||
5 years ago
|
*
|
||
|
* Pops the msgdef wrapper at the top of the stack and replaces it with a msgdef
|
||
3 years ago
|
* wrapper for field |f| of this msgdef (submsg may not be direct, for example
|
||
|
* it may be the submessage of the map value).
|
||
5 years ago
|
*/
|
||
3 years ago
|
void lupb_MessageDef_pushsubmsgdef(lua_State* L, const upb_FieldDef* f) {
|
||
|
const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
|
||
4 years ago
|
assert(m);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, -1, m, LUPB_MSGDEF);
|
||
3 years ago
|
lua_replace(L, -2); /* Replace msgdef with submsgdef. */
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
/* lupb_FieldDef **************************************************************/
|
||
8 years ago
|
|
||
3 years ago
|
const upb_FieldDef* lupb_FieldDef_check(lua_State* L, int narg) {
|
||
5 years ago
|
return lupb_wrapper_check(L, narg, LUPB_FIELDDEF);
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_ContainingOneof(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
const upb_OneofDef* o = upb_FieldDef_ContainingOneof(f);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, o, LUPB_ONEOFDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_ContainingType(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
const upb_MessageDef* m = upb_FieldDef_ContainingType(f);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, m, LUPB_MSGDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_Default(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
upb_CType type = upb_FieldDef_CType(f);
|
||
|
if (type == kUpb_CType_Message) {
|
||
|
return luaL_error(L, "Message fields do not have explicit defaults.");
|
||
8 years ago
|
}
|
||
3 years ago
|
lupb_pushmsgval(L, 0, type, upb_FieldDef_Default(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_Type(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
lua_pushnumber(L, upb_FieldDef_Type(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_HasSubDef(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
lua_pushboolean(L, upb_FieldDef_HasSubDef(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_Index(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
lua_pushinteger(L, upb_FieldDef_Index(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_IsExtension(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
lua_pushboolean(L, upb_FieldDef_IsExtension(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_Label(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
lua_pushinteger(L, upb_FieldDef_Label(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_Name(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
lua_pushstring(L, upb_FieldDef_Name(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_Number(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
int32_t num = upb_FieldDef_Number(f);
|
||
4 years ago
|
if (num) {
|
||
8 years ago
|
lua_pushinteger(L, num);
|
||
4 years ago
|
} else {
|
||
8 years ago
|
lua_pushnil(L);
|
||
4 years ago
|
}
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_IsPacked(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
lua_pushboolean(L, upb_FieldDef_IsPacked(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_MessageSubDef(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, m, LUPB_MSGDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_EnumSubDef(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
const upb_EnumDef* e = upb_FieldDef_EnumSubDef(f);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, e, LUPB_ENUMDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FieldDef_CType(lua_State* L) {
|
||
|
const upb_FieldDef* f = lupb_FieldDef_check(L, 1);
|
||
|
lua_pushinteger(L, upb_FieldDef_CType(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static const struct luaL_Reg lupb_FieldDef_m[] = {
|
||
|
{"containing_oneof", lupb_FieldDef_ContainingOneof},
|
||
|
{"containing_type", lupb_FieldDef_ContainingType},
|
||
|
{"default", lupb_FieldDef_Default},
|
||
|
{"descriptor_type", lupb_FieldDef_Type},
|
||
|
{"has_subdef", lupb_FieldDef_HasSubDef},
|
||
|
{"index", lupb_FieldDef_Index},
|
||
|
{"is_extension", lupb_FieldDef_IsExtension},
|
||
|
{"label", lupb_FieldDef_Label},
|
||
|
{"name", lupb_FieldDef_Name},
|
||
|
{"number", lupb_FieldDef_Number},
|
||
|
{"packed", lupb_FieldDef_IsPacked},
|
||
|
{"msgsubdef", lupb_FieldDef_MessageSubDef},
|
||
|
{"enumsubdef", lupb_FieldDef_EnumSubDef},
|
||
|
{"type", lupb_FieldDef_CType},
|
||
|
{NULL, NULL}};
|
||
8 years ago
|
|
||
3 years ago
|
/* lupb_OneofDef **************************************************************/
|
||
8 years ago
|
|
||
3 years ago
|
const upb_OneofDef* lupb_OneofDef_check(lua_State* L, int narg) {
|
||
5 years ago
|
return lupb_wrapper_check(L, narg, LUPB_ONEOFDEF);
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
static int lupb_OneofDef_ContainingType(lua_State* L) {
|
||
|
const upb_OneofDef* o = lupb_OneofDef_check(L, 1);
|
||
|
const upb_MessageDef* m = upb_OneofDef_ContainingType(o);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, m, LUPB_MSGDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_OneofDef_Field(lua_State* L) {
|
||
|
const upb_OneofDef* o = lupb_OneofDef_check(L, 1);
|
||
4 years ago
|
int32_t idx = lupb_checkint32(L, 2);
|
||
3 years ago
|
int count = upb_OneofDef_FieldCount(o);
|
||
5 years ago
|
|
||
4 years ago
|
if (idx < 0 || idx >= count) {
|
||
3 years ago
|
const char* msg =
|
||
|
lua_pushfstring(L, "index %d exceeds field count %d", idx, count);
|
||
4 years ago
|
return luaL_argerror(L, 2, msg);
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
lupb_wrapper_pushwrapper(L, 1, upb_OneofDef_Field(o, idx), LUPB_FIELDDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_oneofiter_next(lua_State* L) {
|
||
|
const upb_OneofDef* o = lupb_OneofDef_check(L, lua_upvalueindex(1));
|
||
|
int* index = lua_touserdata(L, lua_upvalueindex(2));
|
||
|
const upb_FieldDef* f;
|
||
|
if (*index == upb_OneofDef_FieldCount(o)) return 0;
|
||
|
f = upb_OneofDef_Field(o, (*index)++);
|
||
4 years ago
|
lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), f, LUPB_FIELDDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_OneofDef_Fields(lua_State* L) {
|
||
|
int* index = lua_newuserdata(L, sizeof(int));
|
||
|
lupb_OneofDef_check(L, 1);
|
||
4 years ago
|
*index = 0;
|
||
5 years ago
|
|
||
4 years ago
|
/* Closure upvalues are: oneofdef, index. */
|
||
8 years ago
|
lua_pushcclosure(L, &lupb_oneofiter_next, 2);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_OneofDef_len(lua_State* L) {
|
||
|
const upb_OneofDef* o = lupb_OneofDef_check(L, 1);
|
||
|
lua_pushinteger(L, upb_OneofDef_FieldCount(o));
|
||
4 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
/* lupb_OneofDef_lookupfield()
|
||
4 years ago
|
*
|
||
|
* Handles:
|
||
|
* oneof.lookup_field(field_number)
|
||
|
* oneof.lookup_field(field_name)
|
||
|
*/
|
||
3 years ago
|
static int lupb_OneofDef_lookupfield(lua_State* L) {
|
||
|
const upb_OneofDef* o = lupb_OneofDef_check(L, 1);
|
||
|
const upb_FieldDef* f;
|
||
4 years ago
|
|
||
|
switch (lua_type(L, 2)) {
|
||
|
case LUA_TNUMBER:
|
||
3 years ago
|
f = upb_OneofDef_LookupNumber(o, lua_tointeger(L, 2));
|
||
4 years ago
|
break;
|
||
|
case LUA_TSTRING:
|
||
3 years ago
|
f = upb_OneofDef_LookupName(o, lua_tostring(L, 2));
|
||
4 years ago
|
break;
|
||
|
default: {
|
||
3 years ago
|
const char* msg = lua_pushfstring(L, "number or string expected, got %s",
|
||
4 years ago
|
luaL_typename(L, 2));
|
||
|
return luaL_argerror(L, 2, msg);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
lupb_wrapper_pushwrapper(L, 1, f, LUPB_FIELDDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_OneofDef_Name(lua_State* L) {
|
||
|
const upb_OneofDef* o = lupb_OneofDef_check(L, 1);
|
||
|
lua_pushstring(L, upb_OneofDef_Name(o));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
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}};
|
||
8 years ago
|
|
||
3 years ago
|
static const struct luaL_Reg lupb_OneofDef_mm[] = {{"__len", lupb_OneofDef_len},
|
||
|
{NULL, NULL}};
|
||
8 years ago
|
|
||
3 years ago
|
/* lupb_MessageDef
|
||
|
* ****************************************************************/
|
||
8 years ago
|
|
||
|
typedef struct {
|
||
3 years ago
|
const upb_MessageDef* md;
|
||
|
} lupb_MessageDef;
|
||
8 years ago
|
|
||
3 years ago
|
const upb_MessageDef* lupb_MessageDef_check(lua_State* L, int narg) {
|
||
5 years ago
|
return lupb_wrapper_check(L, narg, LUPB_MSGDEF);
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
static int lupb_MessageDef_FieldCount(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
|
||
|
lua_pushinteger(L, upb_MessageDef_FieldCount(m));
|
||
4 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_MessageDef_OneofCount(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
|
||
|
lua_pushinteger(L, upb_MessageDef_OneofCount(m));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static bool lupb_MessageDef_pushnested(lua_State* L, int msgdef, int name) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, msgdef);
|
||
3 years ago
|
lupb_wrapper_pushdefpool(L, msgdef);
|
||
|
upb_DefPool* defpool = lupb_DefPool_check(L, -1);
|
||
4 years ago
|
lua_pop(L, 1);
|
||
|
|
||
|
/* Construct full package.Message.SubMessage name. */
|
||
3 years ago
|
lua_pushstring(L, upb_MessageDef_FullName(m));
|
||
4 years ago
|
lua_pushstring(L, ".");
|
||
|
lua_pushvalue(L, name);
|
||
|
lua_concat(L, 3);
|
||
3 years ago
|
const char* nested_name = lua_tostring(L, -1);
|
||
4 years ago
|
|
||
|
/* Try lookup. */
|
||
3 years ago
|
const upb_MessageDef* nested =
|
||
3 years ago
|
upb_DefPool_FindMessageByName(defpool, nested_name);
|
||
4 years ago
|
if (!nested) return false;
|
||
|
lupb_wrapper_pushwrapper(L, msgdef, nested, LUPB_MSGDEF);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
3 years ago
|
/* lupb_MessageDef_Field()
|
||
5 years ago
|
*
|
||
|
* Handles:
|
||
|
* msg.field(field_number) -> fielddef
|
||
|
* msg.field(field_name) -> fielddef
|
||
|
*/
|
||
3 years ago
|
static int lupb_MessageDef_Field(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
|
||
|
const upb_FieldDef* f;
|
||
5 years ago
|
|
||
|
switch (lua_type(L, 2)) {
|
||
|
case LUA_TNUMBER:
|
||
3 years ago
|
f = upb_MessageDef_FindFieldByNumber(m, lua_tointeger(L, 2));
|
||
5 years ago
|
break;
|
||
|
case LUA_TSTRING:
|
||
3 years ago
|
f = upb_MessageDef_FindFieldByName(m, lua_tostring(L, 2));
|
||
5 years ago
|
break;
|
||
|
default: {
|
||
3 years ago
|
const char* msg = lua_pushfstring(L, "number or string expected, got %s",
|
||
5 years ago
|
luaL_typename(L, 2));
|
||
|
return luaL_argerror(L, 2, msg);
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, f, LUPB_FIELDDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
/* lupb_MessageDef_FindByNameWithSize()
|
||
5 years ago
|
*
|
||
|
* Handles:
|
||
|
* msg.lookup_name(name) -> fielddef or oneofdef
|
||
|
*/
|
||
3 years ago
|
static int lupb_MessageDef_FindByNameWithSize(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
|
||
|
const upb_FieldDef* f;
|
||
|
const upb_OneofDef* o;
|
||
5 years ago
|
|
||
3 years ago
|
if (!upb_MessageDef_FindByName(m, lua_tostring(L, 2), &f, &o)) {
|
||
8 years ago
|
lua_pushnil(L);
|
||
|
} else if (o) {
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, o, LUPB_ONEOFDEF);
|
||
8 years ago
|
} else {
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, f, LUPB_FIELDDEF);
|
||
8 years ago
|
}
|
||
5 years ago
|
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
/* lupb_MessageDef_Name()
|
||
5 years ago
|
*
|
||
|
* Handles:
|
||
|
* msg.name() -> string
|
||
|
*/
|
||
3 years ago
|
static int lupb_MessageDef_Name(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
|
||
|
lua_pushstring(L, upb_MessageDef_Name(m));
|
||
5 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_msgfielditer_next(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, lua_upvalueindex(1));
|
||
|
int* index = lua_touserdata(L, lua_upvalueindex(2));
|
||
|
const upb_FieldDef* f;
|
||
|
if (*index == upb_MessageDef_FieldCount(m)) return 0;
|
||
|
f = upb_MessageDef_Field(m, (*index)++);
|
||
4 years ago
|
lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), f, LUPB_FIELDDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_MessageDef_Fields(lua_State* L) {
|
||
|
int* index = lua_newuserdata(L, sizeof(int));
|
||
|
lupb_MessageDef_check(L, 1);
|
||
4 years ago
|
*index = 0;
|
||
5 years ago
|
|
||
4 years ago
|
/* Closure upvalues are: msgdef, index. */
|
||
8 years ago
|
lua_pushcclosure(L, &lupb_msgfielditer_next, 2);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_MessageDef_File(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
|
||
|
const upb_FileDef* file = upb_MessageDef_File(m);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, file, LUPB_FILEDEF);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_MessageDef_FullName(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
|
||
|
lua_pushstring(L, upb_MessageDef_FullName(m));
|
||
5 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_MessageDef_index(lua_State* L) {
|
||
|
if (!lupb_MessageDef_pushnested(L, 1, 2)) {
|
||
4 years ago
|
luaL_error(L, "No such nested message");
|
||
|
}
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_msgoneofiter_next(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, lua_upvalueindex(1));
|
||
|
int* index = lua_touserdata(L, lua_upvalueindex(2));
|
||
|
const upb_OneofDef* o;
|
||
|
if (*index == upb_MessageDef_OneofCount(m)) return 0;
|
||
|
o = upb_MessageDef_Oneof(m, (*index)++);
|
||
4 years ago
|
lupb_wrapper_pushwrapper(L, lua_upvalueindex(1), o, LUPB_ONEOFDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_MessageDef_Oneofs(lua_State* L) {
|
||
|
int* index = lua_newuserdata(L, sizeof(int));
|
||
|
lupb_MessageDef_check(L, 1);
|
||
4 years ago
|
*index = 0;
|
||
5 years ago
|
|
||
4 years ago
|
/* Closure upvalues are: msgdef, index. */
|
||
8 years ago
|
lua_pushcclosure(L, &lupb_msgoneofiter_next, 2);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_MessageDef_IsMapEntry(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
|
||
|
lua_pushboolean(L, upb_MessageDef_IsMapEntry(m));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_MessageDef_Syntax(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
|
||
|
lua_pushinteger(L, upb_MessageDef_Syntax(m));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_MessageDef_tostring(lua_State* L) {
|
||
|
const upb_MessageDef* m = lupb_MessageDef_check(L, 1);
|
||
5 years ago
|
lua_pushfstring(L, "<upb.MessageDef name=%s, field_count=%d>",
|
||
3 years ago
|
upb_MessageDef_FullName(m),
|
||
|
(int)upb_MessageDef_FieldCount(m));
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
static const struct luaL_Reg lupb_MessageDef_mm[] = {
|
||
|
{"__call", lupb_MessageDef_call},
|
||
|
{"__index", lupb_MessageDef_index},
|
||
|
{"__len", lupb_MessageDef_FieldCount},
|
||
|
{"__tostring", lupb_MessageDef_tostring},
|
||
|
{NULL, NULL}};
|
||
|
|
||
|
static const struct luaL_Reg lupb_MessageDef_m[] = {
|
||
|
{"field", lupb_MessageDef_Field},
|
||
|
{"fields", lupb_MessageDef_Fields},
|
||
|
{"field_count", lupb_MessageDef_FieldCount},
|
||
|
{"file", lupb_MessageDef_File},
|
||
|
{"full_name", lupb_MessageDef_FullName},
|
||
|
{"lookup_name", lupb_MessageDef_FindByNameWithSize},
|
||
|
{"name", lupb_MessageDef_Name},
|
||
|
{"oneof_count", lupb_MessageDef_OneofCount},
|
||
|
{"oneofs", lupb_MessageDef_Oneofs},
|
||
|
{"syntax", lupb_MessageDef_Syntax},
|
||
|
{"_map_entry", lupb_MessageDef_IsMapEntry},
|
||
|
{NULL, NULL}};
|
||
|
|
||
|
/* lupb_EnumDef ***************************************************************/
|
||
|
|
||
|
const upb_EnumDef* lupb_EnumDef_check(lua_State* L, int narg) {
|
||
5 years ago
|
return lupb_wrapper_check(L, narg, LUPB_ENUMDEF);
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
static int lupb_EnumDef_len(lua_State* L) {
|
||
|
const upb_EnumDef* e = lupb_EnumDef_check(L, 1);
|
||
|
lua_pushinteger(L, upb_EnumDef_ValueCount(e));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_EnumDef_File(lua_State* L) {
|
||
|
const upb_EnumDef* e = lupb_EnumDef_check(L, 1);
|
||
|
const upb_FileDef* file = upb_EnumDef_File(e);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, file, LUPB_FILEDEF);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
/* lupb_EnumDef_Value()
|
||
5 years ago
|
*
|
||
|
* Handles:
|
||
3 years ago
|
* enum.value(number) -> enumval
|
||
|
* enum.value(name) -> enumval
|
||
5 years ago
|
*/
|
||
3 years ago
|
static int lupb_EnumDef_Value(lua_State* L) {
|
||
|
const upb_EnumDef* e = lupb_EnumDef_check(L, 1);
|
||
|
const upb_EnumValueDef* ev;
|
||
5 years ago
|
|
||
|
switch (lua_type(L, 2)) {
|
||
3 years ago
|
case LUA_TNUMBER:
|
||
3 years ago
|
ev = upb_EnumDef_FindValueByNumber(e, lupb_checkint32(L, 2));
|
||
5 years ago
|
break;
|
||
3 years ago
|
case LUA_TSTRING:
|
||
3 years ago
|
ev = upb_EnumDef_FindValueByName(e, lua_tostring(L, 2));
|
||
5 years ago
|
break;
|
||
|
default: {
|
||
3 years ago
|
const char* msg = lua_pushfstring(L, "number or string expected, got %s",
|
||
5 years ago
|
luaL_typename(L, 2));
|
||
|
return luaL_argerror(L, 2, msg);
|
||
8 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
3 years ago
|
lupb_wrapper_pushwrapper(L, 1, ev, LUPB_ENUMVALDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static const struct luaL_Reg lupb_EnumDef_mm[] = {{"__len", lupb_EnumDef_len},
|
||
|
{NULL, NULL}};
|
||
8 years ago
|
|
||
3 years ago
|
static const struct luaL_Reg lupb_EnumDef_m[] = {
|
||
|
{"file", lupb_EnumDef_File}, {"value", lupb_EnumDef_Value}, {NULL, NULL}};
|
||
5 years ago
|
|
||
3 years ago
|
/* lupb_EnumValueDef
|
||
|
* ************************************************************/
|
||
8 years ago
|
|
||
3 years ago
|
const upb_EnumValueDef* lupb_enumvaldef_check(lua_State* L, int narg) {
|
||
3 years ago
|
return lupb_wrapper_check(L, narg, LUPB_ENUMVALDEF);
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_EnumValueDef_Enum(lua_State* L) {
|
||
|
const upb_EnumValueDef* ev = lupb_enumvaldef_check(L, 1);
|
||
|
const upb_EnumDef* e = upb_EnumValueDef_Enum(ev);
|
||
3 years ago
|
lupb_wrapper_pushwrapper(L, 1, e, LUPB_ENUMDEF);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_EnumValueDef_FullName(lua_State* L) {
|
||
|
const upb_EnumValueDef* ev = lupb_enumvaldef_check(L, 1);
|
||
|
lua_pushstring(L, upb_EnumValueDef_FullName(ev));
|
||
3 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_EnumValueDef_Name(lua_State* L) {
|
||
|
const upb_EnumValueDef* ev = lupb_enumvaldef_check(L, 1);
|
||
|
lua_pushstring(L, upb_EnumValueDef_Name(ev));
|
||
3 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_EnumValueDef_Number(lua_State* L) {
|
||
|
const upb_EnumValueDef* ev = lupb_enumvaldef_check(L, 1);
|
||
|
lupb_pushint32(L, upb_EnumValueDef_Number(ev));
|
||
3 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
|
static const struct luaL_Reg lupb_enumvaldef_m[] = {
|
||
3 years ago
|
{"enum", lupb_EnumValueDef_Enum},
|
||
|
{"full_name", lupb_EnumValueDef_FullName},
|
||
|
{"name", lupb_EnumValueDef_Name},
|
||
|
{"number", lupb_EnumValueDef_Number},
|
||
|
{NULL, NULL}};
|
||
3 years ago
|
|
||
3 years ago
|
/* lupb_FileDef ***************************************************************/
|
||
8 years ago
|
|
||
3 years ago
|
const upb_FileDef* lupb_FileDef_check(lua_State* L, int narg) {
|
||
5 years ago
|
return lupb_wrapper_check(L, narg, LUPB_FILEDEF);
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FileDef_Dependency(lua_State* L) {
|
||
|
const upb_FileDef* f = lupb_FileDef_check(L, 1);
|
||
6 years ago
|
int index = luaL_checkint(L, 2);
|
||
3 years ago
|
const upb_FileDef* dep = upb_FileDef_Dependency(f, index);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, dep, LUPB_FILEDEF);
|
||
6 years ago
|
return 1;
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FileDef_DependencyCount(lua_State* L) {
|
||
|
const upb_FileDef* f = lupb_FileDef_check(L, 1);
|
||
|
lua_pushnumber(L, upb_FileDef_DependencyCount(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FileDef_enum(lua_State* L) {
|
||
|
const upb_FileDef* f = lupb_FileDef_check(L, 1);
|
||
8 years ago
|
int index = luaL_checkint(L, 2);
|
||
3 years ago
|
const upb_EnumDef* e = upb_FileDef_TopLevelEnum(f, index);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, e, LUPB_ENUMDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FileDef_enumcount(lua_State* L) {
|
||
|
const upb_FileDef* f = lupb_FileDef_check(L, 1);
|
||
|
lua_pushnumber(L, upb_FileDef_TopLevelEnumCount(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FileDef_msg(lua_State* L) {
|
||
|
const upb_FileDef* f = lupb_FileDef_check(L, 1);
|
||
6 years ago
|
int index = luaL_checkint(L, 2);
|
||
3 years ago
|
const upb_MessageDef* m = upb_FileDef_TopLevelMessage(f, index);
|
||
5 years ago
|
lupb_wrapper_pushwrapper(L, 1, m, LUPB_MSGDEF);
|
||
6 years ago
|
return 1;
|
||
|
}
|
||
8 years ago
|
|
||
3 years ago
|
static int lupb_FileDef_msgcount(lua_State* L) {
|
||
|
const upb_FileDef* f = lupb_FileDef_check(L, 1);
|
||
|
lua_pushnumber(L, upb_FileDef_TopLevelMessageCount(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FileDef_Name(lua_State* L) {
|
||
|
const upb_FileDef* f = lupb_FileDef_check(L, 1);
|
||
|
lua_pushstring(L, upb_FileDef_Name(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FileDef_Package(lua_State* L) {
|
||
|
const upb_FileDef* f = lupb_FileDef_check(L, 1);
|
||
|
lua_pushstring(L, upb_FileDef_Package(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FileDef_Pool(lua_State* L) {
|
||
|
const upb_FileDef* f = lupb_FileDef_check(L, 1);
|
||
3 years ago
|
const upb_DefPool* defpool = upb_FileDef_Pool(f);
|
||
|
lupb_wrapper_pushwrapper(L, 1, defpool, LUPB_SYMTAB);
|
||
4 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_FileDef_Syntax(lua_State* L) {
|
||
|
const upb_FileDef* f = lupb_FileDef_check(L, 1);
|
||
|
lua_pushnumber(L, upb_FileDef_Syntax(f));
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static const struct luaL_Reg lupb_FileDef_m[] = {
|
||
|
{"dep", lupb_FileDef_Dependency},
|
||
|
{"depcount", lupb_FileDef_DependencyCount},
|
||
|
{"enum", lupb_FileDef_enum},
|
||
|
{"enumcount", lupb_FileDef_enumcount},
|
||
|
{"msg", lupb_FileDef_msg},
|
||
|
{"msgcount", lupb_FileDef_msgcount},
|
||
|
{"name", lupb_FileDef_Name},
|
||
|
{"package", lupb_FileDef_Package},
|
||
3 years ago
|
{"defpool", lupb_FileDef_Pool},
|
||
3 years ago
|
{"syntax", lupb_FileDef_Syntax},
|
||
|
{NULL, NULL}};
|
||
8 years ago
|
|
||
3 years ago
|
/* lupb_DefPool
|
||
|
* ****************************************************************/
|
||
8 years ago
|
|
||
3 years ago
|
/* The defpool owns all defs. Thus GC-rooting the defpool ensures that all
|
||
5 years ago
|
* underlying defs stay alive.
|
||
|
*
|
||
3 years ago
|
* The defpool's userval is a cache of def* -> object. */
|
||
5 years ago
|
|
||
5 years ago
|
#define LUPB_CACHE_INDEX 1
|
||
|
|
||
8 years ago
|
typedef struct {
|
||
3 years ago
|
upb_DefPool* defpool;
|
||
3 years ago
|
} lupb_DefPool;
|
||
8 years ago
|
|
||
3 years ago
|
upb_DefPool* lupb_DefPool_check(lua_State* L, int narg) {
|
||
3 years ago
|
lupb_DefPool* ldefpool = luaL_checkudata(L, narg, LUPB_SYMTAB);
|
||
|
if (!ldefpool->defpool) {
|
||
8 years ago
|
luaL_error(L, "called into dead object");
|
||
|
}
|
||
3 years ago
|
return ldefpool->defpool;
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
void lupb_DefPool_pushwrapper(lua_State* L, int narg, const void* def,
|
||
|
const char* type) {
|
||
5 years ago
|
narg = lua_absindex(L, narg);
|
||
5 years ago
|
assert(luaL_testudata(L, narg, LUPB_SYMTAB));
|
||
|
|
||
5 years ago
|
if (def == NULL) {
|
||
|
lua_pushnil(L);
|
||
|
return;
|
||
|
}
|
||
|
|
||
3 years ago
|
lua_getiuservalue(L, narg, LUPB_CACHE_INDEX); /* Get cache. */
|
||
5 years ago
|
|
||
|
/* Index by "def" pointer. */
|
||
5 years ago
|
lua_rawgetp(L, -1, def);
|
||
5 years ago
|
|
||
|
/* Stack is now: cache, cached value. */
|
||
|
if (lua_isnil(L, -1)) {
|
||
|
/* Create new wrapper. */
|
||
3 years ago
|
lupb_wrapper* w = lupb_newuserdata(L, sizeof(*w), 1, type);
|
||
5 years ago
|
w->def = def;
|
||
3 years ago
|
lua_replace(L, -2); /* Replace nil */
|
||
5 years ago
|
|
||
3 years ago
|
/* Set defpool as userval. */
|
||
5 years ago
|
lua_pushvalue(L, narg);
|
||
|
lua_setiuservalue(L, -2, LUPB_SYMTAB_INDEX);
|
||
|
|
||
5 years ago
|
/* Add wrapper to the the cache. */
|
||
5 years ago
|
lua_pushvalue(L, -1);
|
||
|
lua_rawsetp(L, -3, def);
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
lua_replace(L, -2); /* Remove cache, leaving only the wrapper. */
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
/* upb_DefPool_New()
|
||
5 years ago
|
*
|
||
|
* Handles:
|
||
3 years ago
|
* upb.DefPool() -> <new instance>
|
||
5 years ago
|
*/
|
||
3 years ago
|
static int lupb_DefPool_New(lua_State* L) {
|
||
3 years ago
|
lupb_DefPool* ldefpool =
|
||
|
lupb_newuserdata(L, sizeof(*ldefpool), 1, LUPB_SYMTAB);
|
||
|
ldefpool->defpool = upb_DefPool_New();
|
||
5 years ago
|
|
||
|
/* Create our object cache. */
|
||
|
lua_newtable(L);
|
||
|
|
||
|
/* Cache metatable: specifies that values are weak. */
|
||
|
lua_createtable(L, 0, 1);
|
||
|
lua_pushstring(L, "v");
|
||
|
lua_setfield(L, -2, "__mode");
|
||
|
lua_setmetatable(L, -2);
|
||
|
|
||
3 years ago
|
/* Put the defpool itself in the cache metatable. */
|
||
4 years ago
|
lua_pushvalue(L, -2);
|
||
3 years ago
|
lua_rawsetp(L, -2, ldefpool->defpool);
|
||
4 years ago
|
|
||
5 years ago
|
/* Set the cache as our userval. */
|
||
5 years ago
|
lua_setiuservalue(L, -2, LUPB_CACHE_INDEX);
|
||
5 years ago
|
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_DefPool_gc(lua_State* L) {
|
||
3 years ago
|
lupb_DefPool* ldefpool = luaL_checkudata(L, 1, LUPB_SYMTAB);
|
||
|
upb_DefPool_Free(ldefpool->defpool);
|
||
|
ldefpool->defpool = NULL;
|
||
8 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_DefPool_AddFile(lua_State* L) {
|
||
5 years ago
|
size_t len;
|
||
3 years ago
|
upb_DefPool* s = lupb_DefPool_check(L, 1);
|
||
|
const char* str = luaL_checklstring(L, 2, &len);
|
||
|
upb_Arena* arena = lupb_Arena_pushnew(L);
|
||
|
const google_protobuf_FileDescriptorProto* file;
|
||
|
const upb_FileDef* file_def;
|
||
|
upb_Status status;
|
||
|
|
||
|
upb_Status_Clear(&status);
|
||
5 years ago
|
file = google_protobuf_FileDescriptorProto_parse(str, len, arena);
|
||
|
|
||
|
if (!file) {
|
||
|
luaL_argerror(L, 2, "failed to parse descriptor");
|
||
|
}
|
||
|
|
||
3 years ago
|
file_def = upb_DefPool_AddFile(s, file, &status);
|
||
5 years ago
|
lupb_checkstatus(L, &status);
|
||
|
|
||
3 years ago
|
lupb_DefPool_pushwrapper(L, 1, file_def, LUPB_FILEDEF);
|
||
5 years ago
|
|
||
|
return 1;
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
static int lupb_DefPool_addset(lua_State* L) {
|
||
6 years ago
|
size_t i, n, len;
|
||
3 years ago
|
const google_protobuf_FileDescriptorProto* const* files;
|
||
|
google_protobuf_FileDescriptorSet* set;
|
||
|
upb_DefPool* s = lupb_DefPool_check(L, 1);
|
||
|
const char* str = luaL_checklstring(L, 2, &len);
|
||
|
upb_Arena* arena = lupb_Arena_pushnew(L);
|
||
|
upb_Status status;
|
||
|
|
||
|
upb_Status_Clear(&status);
|
||
6 years ago
|
set = google_protobuf_FileDescriptorSet_parse(str, len, arena);
|
||
8 years ago
|
|
||
6 years ago
|
if (!set) {
|
||
|
luaL_argerror(L, 2, "failed to parse descriptor");
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
files = google_protobuf_FileDescriptorSet_file(set, &n);
|
||
|
for (i = 0; i < n; i++) {
|
||
3 years ago
|
upb_DefPool_AddFile(s, files[i], &status);
|
||
5 years ago
|
lupb_checkstatus(L, &status);
|
||
6 years ago
|
}
|
||
8 years ago
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_DefPool_FindMessageByName(lua_State* L) {
|
||
|
const upb_DefPool* s = lupb_DefPool_check(L, 1);
|
||
|
const upb_MessageDef* m =
|
||
|
upb_DefPool_FindMessageByName(s, luaL_checkstring(L, 2));
|
||
|
lupb_DefPool_pushwrapper(L, 1, m, LUPB_MSGDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_DefPool_FindEnumByName(lua_State* L) {
|
||
|
const upb_DefPool* s = lupb_DefPool_check(L, 1);
|
||
|
const upb_EnumDef* e = upb_DefPool_FindEnumByName(s, luaL_checkstring(L, 2));
|
||
|
lupb_DefPool_pushwrapper(L, 1, e, LUPB_ENUMDEF);
|
||
8 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_DefPool_FindEnumByNameval(lua_State* L) {
|
||
|
const upb_DefPool* s = lupb_DefPool_check(L, 1);
|
||
|
const upb_EnumValueDef* e =
|
||
|
upb_DefPool_FindEnumByNameval(s, luaL_checkstring(L, 2));
|
||
|
lupb_DefPool_pushwrapper(L, 1, e, LUPB_ENUMVALDEF);
|
||
3 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static int lupb_DefPool_tostring(lua_State* L) {
|
||
3 years ago
|
lua_pushfstring(L, "<upb.DefPool>");
|
||
5 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
static const struct luaL_Reg lupb_DefPool_m[] = {
|
||
|
{"add_file", lupb_DefPool_AddFile},
|
||
|
{"add_set", lupb_DefPool_addset},
|
||
|
{"lookup_msg", lupb_DefPool_FindMessageByName},
|
||
|
{"lookup_enum", lupb_DefPool_FindEnumByName},
|
||
|
{"lookup_enumval", lupb_DefPool_FindEnumByNameval},
|
||
|
{NULL, NULL}};
|
||
8 years ago
|
|
||
3 years ago
|
static const struct luaL_Reg lupb_DefPool_mm[] = {
|
||
|
{"__gc", lupb_DefPool_gc},
|
||
|
{"__tostring", lupb_DefPool_tostring},
|
||
|
{NULL, NULL}};
|
||
8 years ago
|
|
||
|
/* lupb toplevel **************************************************************/
|
||
|
|
||
3 years ago
|
static void lupb_setfieldi(lua_State* L, const char* field, int i) {
|
||
8 years ago
|
lua_pushinteger(L, i);
|
||
|
lua_setfield(L, -2, field);
|
||
|
}
|
||
|
|
||
|
static const struct luaL_Reg lupbdef_toplevel_m[] = {
|
||
3 years ago
|
{"DefPool", lupb_DefPool_New}, {NULL, NULL}};
|
||
8 years ago
|
|
||
3 years ago
|
void lupb_def_registertypes(lua_State* L) {
|
||
8 years ago
|
lupb_setfuncs(L, lupbdef_toplevel_m);
|
||
|
|
||
5 years ago
|
/* Register types. */
|
||
3 years ago
|
lupb_register_type(L, LUPB_ENUMDEF, lupb_EnumDef_m, lupb_EnumDef_mm);
|
||
|
lupb_register_type(L, LUPB_ENUMVALDEF, lupb_enumvaldef_m, NULL);
|
||
|
lupb_register_type(L, LUPB_FIELDDEF, lupb_FieldDef_m, NULL);
|
||
|
lupb_register_type(L, LUPB_FILEDEF, lupb_FileDef_m, NULL);
|
||
|
lupb_register_type(L, LUPB_MSGDEF, lupb_MessageDef_m, lupb_MessageDef_mm);
|
||
|
lupb_register_type(L, LUPB_ONEOFDEF, lupb_OneofDef_m, lupb_OneofDef_mm);
|
||
|
lupb_register_type(L, LUPB_SYMTAB, lupb_DefPool_m, lupb_DefPool_mm);
|
||
8 years ago
|
|
||
|
/* Register constants. */
|
||
3 years ago
|
lupb_setfieldi(L, "LABEL_OPTIONAL", kUpb_Label_Optional);
|
||
|
lupb_setfieldi(L, "LABEL_REQUIRED", kUpb_Label_Required);
|
||
|
lupb_setfieldi(L, "LABEL_REPEATED", kUpb_Label_Repeated);
|
||
|
|
||
|
lupb_setfieldi(L, "TYPE_DOUBLE", kUpb_CType_Double);
|
||
|
lupb_setfieldi(L, "TYPE_FLOAT", kUpb_CType_Float);
|
||
|
lupb_setfieldi(L, "TYPE_INT64", kUpb_CType_Int64);
|
||
|
lupb_setfieldi(L, "TYPE_UINT64", kUpb_CType_UInt64);
|
||
|
lupb_setfieldi(L, "TYPE_INT32", kUpb_CType_Int32);
|
||
|
lupb_setfieldi(L, "TYPE_BOOL", kUpb_CType_Bool);
|
||
|
lupb_setfieldi(L, "TYPE_STRING", kUpb_CType_String);
|
||
|
lupb_setfieldi(L, "TYPE_MESSAGE", kUpb_CType_Message);
|
||
|
lupb_setfieldi(L, "TYPE_BYTES", kUpb_CType_Bytes);
|
||
|
lupb_setfieldi(L, "TYPE_UINT32", kUpb_CType_UInt32);
|
||
|
lupb_setfieldi(L, "TYPE_ENUM", kUpb_CType_Enum);
|
||
|
|
||
3 years ago
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_DOUBLE", kUpb_FieldType_Double);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_FLOAT", kUpb_FieldType_Float);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_INT64", kUpb_FieldType_Int64);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_UINT64", kUpb_FieldType_UInt64);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_INT32", kUpb_FieldType_Int32);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_FIXED64", kUpb_FieldType_Fixed64);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_FIXED32", kUpb_FieldType_Fixed32);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_BOOL", kUpb_FieldType_Bool);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_STRING", kUpb_FieldType_String);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_GROUP", kUpb_FieldType_Group);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_MESSAGE", kUpb_FieldType_Message);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_BYTES", kUpb_FieldType_Bytes);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_UINT32", kUpb_FieldType_UInt32);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_ENUM", kUpb_FieldType_Enum);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_SFIXED32", kUpb_FieldType_SFixed32);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_SFIXED64", kUpb_FieldType_SFixed64);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_SINT32", kUpb_FieldType_SInt32);
|
||
|
lupb_setfieldi(L, "DESCRIPTOR_TYPE_SINT64", kUpb_FieldType_SInt64);
|
||
3 years ago
|
|
||
|
lupb_setfieldi(L, "SYNTAX_PROTO2", kUpb_Syntax_Proto2);
|
||
|
lupb_setfieldi(L, "SYNTAX_PROTO3", kUpb_Syntax_Proto3);
|
||
8 years ago
|
}
|