summaryrefslogtreecommitdiff
path: root/upb/bindings/lua/upb/pb.c
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2018-08-12 19:23:26 -0700
committerJoshua Haberman <jhaberman@gmail.com>2018-08-12 19:23:26 -0700
commitc8f6a27e6b27ed5d51cf6c8da5ec080b9952fa99 (patch)
tree8d9e160dcb6f52f530a00868f342ab08121d1684 /upb/bindings/lua/upb/pb.c
parentb0a6602fc8fddf71ac959d43b4cd82707e6235b9 (diff)
Enforced that upb_msg lives in an Arena only, and other simplifying.
upb_msg was trying to be general enough that it could either live in an arena or be allocated with malloc()/free(). This was too much complexity for too little benefit. We should commit to just saying that upb_msg is arena-only. I also ripped out the code to glue upb_msg to the existing handlers-based encoder/decoder. upb_msg has its own, small, simple encoder/decoder. I'm trying to whittle down upb_msg to a small and simple core. I updated the Lua extension for these changes. Lua needs some more work to properly create arenas per message. For now I just created a single global arena.
Diffstat (limited to 'upb/bindings/lua/upb/pb.c')
-rw-r--r--upb/bindings/lua/upb/pb.c117
1 files changed, 18 insertions, 99 deletions
diff --git a/upb/bindings/lua/upb/pb.c b/upb/bindings/lua/upb/pb.c
index 1d56066..466e8f7 100644
--- a/upb/bindings/lua/upb/pb.c
+++ b/upb/bindings/lua/upb/pb.c
@@ -6,135 +6,56 @@
*/
#include "upb/bindings/lua/upb.h"
-#include "upb/pb/decoder.h"
-#include "upb/pb/encoder.h"
+#include "upb/decode.h"
+#include "upb/encode.h"
#define LUPB_PBDECODERMETHOD "lupb.pb.decodermethod"
-static int lupb_pb_strtomessage(lua_State *L) {
+static int lupb_pb_decode(lua_State *L) {
size_t len;
upb_status status = UPB_STATUS_INIT;
- const char *pb = lua_tolstring(L, 1, &len);
- const upb_msglayout *layout = lua_touserdata(L, lua_upvalueindex(1));
- const upb_pbdecodermethod *method = lua_touserdata(L, lua_upvalueindex(2));
- const upb_handlers *handlers = upb_pbdecodermethod_desthandlers(method);
-
- upb_arena *msg_arena;
+ const upb_msglayout *layout;
+ upb_msg *msg = lupb_msg_checkmsg2(L, 1, &layout);
+ const char *pb = lua_tolstring(L, 2, &len);
+ upb_stringview buf = upb_stringview_make(pb, len);
upb_env env;
- upb_sink sink;
- upb_pbdecoder *decoder;
- void *msg;
-
- lupb_arena_new(L);
- msg_arena = lupb_arena_check(L, -1);
- msg = upb_msg_new(layout, upb_arena_alloc(msg_arena));
upb_env_init(&env);
upb_env_reporterrorsto(&env, &status);
- upb_sink_reset(&sink, handlers, msg);
- decoder = upb_pbdecoder_create(&env, method, &sink);
- upb_bufsrc_putbuf(pb, len, upb_pbdecoder_input(decoder));
+ upb_decode(buf, msg, (const void*)layout, &env);
/* Free resources before we potentially bail on error. */
upb_env_uninit(&env);
lupb_checkstatus(L, &status);
- /* References the arena at the top of the stack. */
- lupb_msg_pushref(L, lua_upvalueindex(3), msg);
- return 1;
+ return 0;
}
-static int lupb_pb_messagetostr(lua_State *L) {
- const lupb_msgclass *lmsgclass = lua_touserdata(L, lua_upvalueindex(1));
- const upb_msg *msg = lupb_msg_checkmsg(L, 1, lmsgclass);
- const upb_visitorplan *vp = lua_touserdata(L, lua_upvalueindex(2));
- const upb_handlers *encode_handlers = lua_touserdata(L, lua_upvalueindex(3));
-
+static int lupb_pb_encode(lua_State *L) {
+ const upb_msglayout *layout;
+ const upb_msg *msg = lupb_msg_checkmsg2(L, 1, &layout);
upb_env env;
- upb_bufsink *bufsink;
- upb_bytessink *bytessink;
- upb_pb_encoder *encoder;
- upb_visitor *visitor;
- const char *buf;
- size_t len;
+ size_t size;
upb_status status = UPB_STATUS_INIT;
+ char *result;
upb_env_init(&env);
upb_env_reporterrorsto(&env, &status);
- bufsink = upb_bufsink_new(&env);
- bytessink = upb_bufsink_sink(bufsink);
- encoder = upb_pb_encoder_create(&env, encode_handlers, bytessink);
- visitor = upb_visitor_create(&env, vp, upb_pb_encoder_input(encoder));
- upb_visitor_visitmsg(visitor, msg);
- buf = upb_bufsink_getdata(bufsink, &len);
- lua_pushlstring(L, buf, len);
+ result = upb_encode(msg, (const void*)layout, &env, &size);
/* Free resources before we potentially bail on error. */
upb_env_uninit(&env);
lupb_checkstatus(L, &status);
+ lua_pushlstring(L, result, size);
return 1;
}
-static int lupb_pb_makestrtomsgdecoder(lua_State *L) {
- const upb_msglayout *layout = lupb_msgclass_getlayout(L, 1);
- const upb_handlers *handlers = lupb_msgclass_getmergehandlers(L, 1);
- const upb_pbdecodermethod *m;
-
- upb_pbdecodermethodopts opts;
- upb_pbdecodermethodopts_init(&opts, handlers);
-
- m = upb_pbdecodermethod_new(&opts, &m);
-
- /* Push upvalues for the closure. */
- lua_pushlightuserdata(L, (void*)layout);
- lua_pushlightuserdata(L, (void*)m);
- lua_pushvalue(L, 1);
-
- /* Upvalue for the closure, only to keep the decodermethod alive. */
- lupb_refcounted_pushnewrapper(
- L, upb_pbdecodermethod_upcast(m), LUPB_PBDECODERMETHOD, &m);
-
- lua_pushcclosure(L, &lupb_pb_strtomessage, 4);
-
- return 1; /* The decoder closure. */
-}
-
-static int lupb_pb_makemsgtostrencoder(lua_State *L) {
- const lupb_msgclass *lmsgclass = lupb_msgclass_check(L, 1);
- const upb_msgdef *md = lupb_msgclass_getmsgdef(lmsgclass);
- upb_msgfactory *factory = lupb_msgclass_getfactory(lmsgclass);
- const upb_handlers *encode_handlers;
- const upb_visitorplan *vp;
-
- encode_handlers = upb_pb_encoder_newhandlers(md, &encode_handlers);
- vp = upb_msgfactory_getvisitorplan(factory, encode_handlers);
-
- /* Push upvalues for the closure. */
- lua_pushlightuserdata(L, (void*)lmsgclass);
- lua_pushlightuserdata(L, (void*)vp);
- lua_pushlightuserdata(L, (void*)encode_handlers);
-
- /* Upvalues for the closure, only to keep the other upvalues alive. */
- lua_pushvalue(L, 1);
- lupb_refcounted_pushnewrapper(
- L, upb_handlers_upcast(encode_handlers), LUPB_PBDECODERMETHOD, &encode_handlers);
-
- lua_pushcclosure(L, &lupb_pb_messagetostr, 5);
-
- return 1; /* The decoder closure. */
-}
-
-static const struct luaL_Reg decodermethod_mm[] = {
- {"__gc", lupb_refcounted_gc},
- {NULL, NULL}
-};
-
static const struct luaL_Reg toplevel_m[] = {
- {"MakeStringToMessageDecoder", lupb_pb_makestrtomsgdecoder},
- {"MakeMessageToStringEncoder", lupb_pb_makemsgtostrencoder},
+ {"decode", lupb_pb_decode},
+ {"encode", lupb_pb_encode},
{NULL, NULL}
};
@@ -144,7 +65,5 @@ int luaopen_upb_pb_c(lua_State *L) {
return 1;
}
- lupb_register_type(L, LUPB_PBDECODERMETHOD, NULL, decodermethod_mm);
-
return 1;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback