|
| 1 | +# lua-bitn Development Guide |
| 2 | + |
| 3 | +## Project Structure |
| 4 | + |
| 5 | +``` |
| 6 | +lua-bitn/ |
| 7 | +├── src/bitn/ |
| 8 | +│ ├── init.lua # Module aggregator, exports bit16/bit32/bit64 |
| 9 | +│ ├── bit16.lua # 16-bit bitwise operations |
| 10 | +│ ├── bit32.lua # 32-bit bitwise operations |
| 11 | +│ └── bit64.lua # 64-bit bitwise operations (uses {high, low} pairs) |
| 12 | +├── tests/ |
| 13 | +│ ├── test_bit16.lua # 16-bit test vectors |
| 14 | +│ ├── test_bit32.lua # 32-bit test vectors |
| 15 | +│ └── test_bit64.lua # 64-bit test vectors |
| 16 | +├── .github/workflows/ |
| 17 | +│ ├── build.yml # CI: lint, test matrix, build |
| 18 | +│ └── release.yml # Release automation |
| 19 | +├── run_tests.sh # Main test runner |
| 20 | +├── run_tests_matrix.sh # Multi-version test runner |
| 21 | +└── Makefile # Build automation |
| 22 | +``` |
| 23 | + |
| 24 | +## Key Commands |
| 25 | + |
| 26 | +```bash |
| 27 | +# Run tests |
| 28 | +make test |
| 29 | + |
| 30 | +# Run specific module tests |
| 31 | +make test-bit32 |
| 32 | + |
| 33 | +# Run across Lua versions |
| 34 | +make test-matrix |
| 35 | + |
| 36 | +# Format code |
| 37 | +make format |
| 38 | + |
| 39 | +# Lint code |
| 40 | +make lint |
| 41 | + |
| 42 | +# Build single-file distribution |
| 43 | +make build |
| 44 | +``` |
| 45 | + |
| 46 | +## Architecture |
| 47 | + |
| 48 | +### Module Design |
| 49 | + |
| 50 | +Each bit module (bit16, bit32, bit64) provides the same API: |
| 51 | +- Bitwise: band, bor, bxor, bnot |
| 52 | +- Shifts: lshift, rshift, arshift |
| 53 | +- Rotates: rol, ror |
| 54 | +- Arithmetic: add, mask |
| 55 | +- Byte conversions: uN_to_be_bytes, uN_to_le_bytes, be_bytes_to_uN, le_bytes_to_uN |
| 56 | + |
| 57 | +### 64-bit Representation |
| 58 | + |
| 59 | +64-bit values use `{high, low}` pairs for Lua 5.1 compatibility: |
| 60 | +```lua |
| 61 | +-- 0x123456789ABCDEF0 represented as: |
| 62 | +local value = {0x12345678, 0x9ABCDEF0} |
| 63 | +``` |
| 64 | + |
| 65 | +### Pure Lua Implementation |
| 66 | + |
| 67 | +All operations are implemented using basic Lua arithmetic to ensure |
| 68 | +compatibility across all Lua versions without native bit library dependencies. |
| 69 | + |
| 70 | +## Testing |
| 71 | + |
| 72 | +Tests use Lua table-based vectors for easy maintenance: |
| 73 | + |
| 74 | +```lua |
| 75 | +local test_vectors = { |
| 76 | + { name = "band(0xFF, 0x0F)", fn = bit32.band, inputs = {0xFF, 0x0F}, expected = 0x0F }, |
| 77 | + -- ... |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +Run with: `./run_tests.sh` or `make test` |
| 82 | + |
| 83 | +## Building |
| 84 | + |
| 85 | +The build process uses `amalg` to create a single-file distribution: |
| 86 | + |
| 87 | +```bash |
| 88 | +make build |
| 89 | +# Output: build/bitn.lua |
| 90 | +``` |
| 91 | + |
| 92 | +Version is automatically injected from git tags during release. |
| 93 | + |
| 94 | +## CI/CD |
| 95 | + |
| 96 | +- **build.yml**: Runs on push/PR to main |
| 97 | + - Format check with stylua |
| 98 | + - Lint with luacheck |
| 99 | + - Test matrix (Lua 5.1-5.4, LuaJIT 2.0/2.1) |
| 100 | + - Build single-file distribution |
| 101 | + |
| 102 | +- **release.yml**: Runs on version tags (v*) |
| 103 | + - Builds and publishes release with bitn.lua artifact |
| 104 | + |
| 105 | +## Code Style |
| 106 | + |
| 107 | +- 2-space indentation |
| 108 | +- 120 column width |
| 109 | +- Double quotes preferred |
| 110 | +- LuaDoc annotations for all public functions |
0 commit comments