$ show starwing/lua-protobuf
This repository provides a comprehensive protobuf (Protocol Buffers) support library for Lua (versions 5.1–5.4 and LuaJIT). At its core is a high-performance C module (`pb.c`/`pb.h`) implementing protobuf wire format encoding/decoding, type management, and a memory-efficient hash table and pool. On top of this, a pure Lua module (`protoc.lua`) parses `.proto` schema files, resolving imports, types, and options, and compiles them into the binary descriptor format required by the C core. Additional Lua modules (`pb.buffer`, `pb.slice`, `pb.conv`, `pb.io`) provide low-level wire format manipulation for advanced use cases. The library supports both proto2 and proto3 syntax, including maps, oneofs, packed repeated fields, extensions, enums, custom default values, hooks, and multiple independent type states. It is well-tested with nearly 100% code coverage via GitHub Actions and Coveralls. The architecture is cleanly layered: the C core is a single-header library with its own implementation (`pb.h` with `PB_IMPLEMENTATION`), making it easy to embed into other projects. The pure Lua compiler is self-contained and can be used independently. The codebase demonstrates strong craftsmanship in both C (efficient wire format parsing, custom allocator pool, hash table) and Lua (complete recursive-descent parser with error reporting). It is actively maintained, with recent commits and support for the latest Lua versions.
The LLM identified 3 files worth highlighting for reuse:
A single-header C library that provides full protobuf wire format encoding/decoding, type management, memory pool, hash table, and a .pb binary descriptor loader. The implementation is conditionally compiled via `PB_IMPLEMENTATION`. It encapsulates the entire protobuf state machine, including varint/fixed encoding, wire type dispatch, and schema loading. The design is self-contained (no external dependencies beyond standard C headers) and can be extracted into any C or C++ project with minimal adaptation. Demonstrates expert-level C: optimized varint reading with inline fast paths, a compact memory pool allocator, and a robust hash table with collision resolution via open addressing with last-free placeholder.
A complete pure-Lua protobuf `.proto` schema compiler. It implements a fully featured lexer (with integer, float, string, comment, and option parsing) and a recursive-descent parser that handles packages, imports, messages, enums, oneofs, extensions, services (including RPC), options, custom options, and all standard protobuf syntax elements. It resolves type references (including cross-file via `import`), validates field numbers and names, and compiles the parsed schema into a `FileDescriptorSet` binary that can be loaded by the C core. The code is modular, using metatable-based class emulation for Lexer and Parser, and includes an extensible import fallback mechanism (`unknown_import`, `unknown_type`). It can be used standalone without the C module for schema verification or as a learning tool for protobuf grammar.
The C source that binds the low-level protobuf engine (from `pb.h`) to the Lua runtime. It provides the full `pb` module API (`pb.encode`, `pb.decode`, `pb.type`, `pb.field`, `pb.load`, `pb.loadfile`, `pb.clear`, `pb.option`, `pb.hook`, `pb.state`, etc.) as well as sub-modules `pb.io`, `pb.conv`, `pb.buffer`, and `pb.slice`. The implementation is carefully crafted for Lua compatibility (5.1–5.4 and LuaJIT) using preprocessor macros to adapt to different Lua API versions. It demonstrates sophisticated Lua/C interaction: custom userdata metatables for buffer and slice objects, efficient conversion between Lua types and protobuf wire types, optional string-based int64 handling to avoid floating-point precision loss, and a multiple-state mechanism for isolated protobuf type databases. The code is well-commented and includes platform-specific workarounds (Windows binary I/O, coverage flags).