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.
170 lines
5.1 KiB
170 lines
5.1 KiB
12 years ago
|
/*
|
||
|
* upb - a minimalist implementation of protocol buffers.
|
||
|
*
|
||
|
* Copyright (c) 2012 Google Inc. See LICENSE for details.
|
||
|
* Author: Josh Haberman <jhaberman@gmail.com>
|
||
|
*
|
||
|
* Lua extension that provides access to upb_table. This is an internal-only
|
||
|
* interface and exists for the sole purpose of writing a C code generator in
|
||
|
* Lua that can dump a upb_table as static C initializers. This lets us use
|
||
|
* Lua for convenient string manipulation while saving us from re-implementing
|
||
|
* the upb_table hash function and hash table layout / collision strategy in
|
||
|
* Lua.
|
||
|
*
|
||
|
* Since this is used only as part of the toolchain (and not part of the
|
||
|
* runtime) we do not hold this module to the same stringent requirements as
|
||
|
* the main Lua modules (for example that misbehaving Lua programs cannot
|
||
|
* crash the interpreter).
|
||
|
*/
|
||
|
|
||
|
#include <float.h>
|
||
|
#include <math.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
11 years ago
|
|
||
12 years ago
|
#include "lauxlib.h"
|
||
11 years ago
|
#include "upb/bindings/lua/upb.h"
|
||
12 years ago
|
#include "upb/def.h"
|
||
|
|
||
|
static void lupbtable_setnum(lua_State *L, int tab, const char *key,
|
||
|
lua_Number val) {
|
||
|
lua_pushnumber(L, val);
|
||
|
lua_setfield(L, tab - 1, key);
|
||
|
}
|
||
|
|
||
11 years ago
|
static void lupbtable_pushval(lua_State *L, _upb_value val, upb_ctype_t ctype) {
|
||
|
switch (ctype) {
|
||
12 years ago
|
case UPB_CTYPE_INT32:
|
||
12 years ago
|
lua_pushnumber(L, val.int32);
|
||
12 years ago
|
break;
|
||
|
case UPB_CTYPE_PTR:
|
||
12 years ago
|
lupb_def_pushwrapper(L, val.ptr, NULL);
|
||
12 years ago
|
break;
|
||
|
case UPB_CTYPE_CSTR:
|
||
12 years ago
|
lua_pushstring(L, val.cstr);
|
||
12 years ago
|
break;
|
||
|
default:
|
||
11 years ago
|
luaL_error(L, "Unexpected type: %d", ctype);
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Sets a few fields common to both hash table entries and arrays.
|
||
11 years ago
|
static void lupbtable_setmetafields(lua_State *L, int ctype, const void *ptr) {
|
||
12 years ago
|
// We tack this onto every entry so we know it even if the entries
|
||
|
// don't stay with the table.
|
||
11 years ago
|
lua_pushnumber(L, ctype);
|
||
12 years ago
|
lua_setfield(L, -2, "valtype");
|
||
|
|
||
|
// Set this to facilitate linking.
|
||
|
lua_pushlightuserdata(L, (void*)ptr);
|
||
|
lua_setfield(L, -2, "ptr");
|
||
|
}
|
||
|
|
||
|
static void lupbtable_pushent(lua_State *L, const upb_tabent *e,
|
||
11 years ago
|
bool inttab, int ctype) {
|
||
12 years ago
|
lua_newtable(L);
|
||
|
if (!upb_tabent_isempty(e)) {
|
||
|
if (inttab) {
|
||
|
lua_pushnumber(L, e->key.num);
|
||
|
} else {
|
||
|
lua_pushstring(L, e->key.str);
|
||
|
}
|
||
|
lua_setfield(L, -2, "key");
|
||
11 years ago
|
lupbtable_pushval(L, e->val, ctype);
|
||
12 years ago
|
lua_setfield(L, -2, "value");
|
||
|
}
|
||
|
lua_pushlightuserdata(L, (void*)e->next);
|
||
|
lua_setfield(L, -2, "next");
|
||
11 years ago
|
lupbtable_setmetafields(L, ctype, e);
|
||
12 years ago
|
}
|
||
|
|
||
|
// Dumps the shared part of upb_table into a Lua table.
|
||
|
static void lupbtable_pushtable(lua_State *L, const upb_table *t, bool inttab) {
|
||
|
lua_newtable(L);
|
||
|
lupbtable_setnum(L, -1, "count", t->count);
|
||
|
lupbtable_setnum(L, -1, "mask", t->mask);
|
||
11 years ago
|
lupbtable_setnum(L, -1, "ctype", t->ctype);
|
||
12 years ago
|
lupbtable_setnum(L, -1, "size_lg2", t->size_lg2);
|
||
|
|
||
|
lua_newtable(L);
|
||
|
for (int i = 0; i < upb_table_size(t); i++) {
|
||
11 years ago
|
lupbtable_pushent(L, &t->entries[i], inttab, t->ctype);
|
||
12 years ago
|
lua_rawseti(L, -2, i + 1);
|
||
|
}
|
||
|
lua_setfield(L, -2, "entries");
|
||
|
}
|
||
|
|
||
|
// Dumps a upb_inttable to a Lua table.
|
||
|
static void lupbtable_pushinttable(lua_State *L, const upb_inttable *t) {
|
||
|
lupbtable_pushtable(L, &t->t, true);
|
||
|
lupbtable_setnum(L, -1, "array_size", t->array_size);
|
||
|
lupbtable_setnum(L, -1, "array_count", t->array_count);
|
||
|
|
||
|
lua_newtable(L);
|
||
|
for (int i = 0; i < t->array_size; i++) {
|
||
|
lua_newtable(L);
|
||
|
if (upb_arrhas(t->array[i])) {
|
||
11 years ago
|
lupbtable_pushval(L, t->array[i], t->t.ctype);
|
||
12 years ago
|
lua_setfield(L, -2, "val");
|
||
|
}
|
||
11 years ago
|
lupbtable_setmetafields(L, t->t.ctype, &t->array[i]);
|
||
12 years ago
|
lua_rawseti(L, -2, i + 1);
|
||
|
}
|
||
|
lua_setfield(L, -2, "array");
|
||
|
}
|
||
|
|
||
|
static void lupbtable_pushstrtable(lua_State *L, const upb_strtable *t) {
|
||
|
lupbtable_pushtable(L, &t->t, false);
|
||
|
}
|
||
|
|
||
|
static int lupbtable_msgdef_itof(lua_State *L) {
|
||
|
const upb_msgdef *m = lupb_msgdef_check(L, 1);
|
||
|
lupbtable_pushinttable(L, &m->itof);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
static int lupbtable_msgdef_ntof(lua_State *L) {
|
||
|
const upb_msgdef *m = lupb_msgdef_check(L, 1);
|
||
|
lupbtable_pushstrtable(L, &m->ntof);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
static int lupbtable_enumdef_iton(lua_State *L) {
|
||
|
const upb_enumdef *e = lupb_enumdef_check(L, 1);
|
||
|
lupbtable_pushinttable(L, &e->iton);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
static int lupbtable_enumdef_ntoi(lua_State *L) {
|
||
|
const upb_enumdef *e = lupb_enumdef_check(L, 1);
|
||
|
lupbtable_pushstrtable(L, &e->ntoi);
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
static void lupbtable_setfieldi(lua_State *L, const char *field, int i) {
|
||
|
lua_pushnumber(L, i);
|
||
|
lua_setfield(L, -2, field);
|
||
|
}
|
||
|
|
||
|
static const struct luaL_Reg lupbtable_toplevel_m[] = {
|
||
|
{"msgdef_itof", lupbtable_msgdef_itof},
|
||
|
{"msgdef_ntof", lupbtable_msgdef_ntof},
|
||
|
{"enumdef_iton", lupbtable_enumdef_iton},
|
||
|
{"enumdef_ntoi", lupbtable_enumdef_ntoi},
|
||
|
{NULL, NULL}
|
||
|
};
|
||
|
|
||
|
int luaopen_upbtable(lua_State *L) {
|
||
|
lupb_newlib(L, "upb.table", lupbtable_toplevel_m);
|
||
|
|
||
11 years ago
|
// We define these here because they are not public.
|
||
|
lupbtable_setfieldi(L, "CTYPE_PTR", UPB_CTYPE_PTR);
|
||
|
lupbtable_setfieldi(L, "CTYPE_CSTR", UPB_CTYPE_CSTR);
|
||
12 years ago
|
lupbtable_setfieldi(L, "CTYPE_INT32", UPB_CTYPE_INT32);
|
||
|
|
||
|
lua_pushlightuserdata(L, NULL);
|
||
|
lua_setfield(L, -2, "NULL");
|
||
|
|
||
|
return 1; // Return a single Lua value, the package table created above.
|
||
|
}
|