bindings: GLFW and Vulkan, generated with snake_case names

`use glfw` and `use vulkan` load the native libraries and expose every
function with a snake_case name, plus constants, struct sizes and field
offsets.  Generated from the official headers/registry, not hand-written:

- tools/gen_glfw.py: parses glfw3.h -> std/glfw/glfw.cat (124 fns +
  GLFW_* constants + GLFWvidmode/GLFWgammaramp offsets).
- tools/gen_vulkan.py: parses vk.xml -> std/vulkan/vulkan.cat (650
  commands + 1454 struct sizes/offsets + enum constants), with a
  recursive x86-64 layout engine that resolves nested structs and
  macro-sized arrays (sizes verified against the C headers).
- ffi: `memory` values pass their address where a pointer is expected;
  `ffi.pointer(addr, name, sig)` binds a call at a raw address (Vulkan
  dispatch).  `VkResult`/enum returns are ints.
- compiler: a keyword is allowed as a member name after `.` /
  method-call position (glfw.false, glfw.visible).
- vendor/lib/libglfw.so.3: the host has no libglfw, so it is vendored.

Proved live: GLFW 3.4 window + framebuffer under xvfb-run; a Vulkan
1.0 instance + physical-device enumeration on lavapipe (headless).
tests/glfw.cat, tests/vulkan.cat wired into the suite (125 green).
This commit is contained in:
Minus committed 2026-11-17 18:48:32 +00:00
1 parent 210054f46c
commit 04009c3c5e
18 files changed
+13539 -2

No files matched your search

+52
View File
@@ -275,6 +275,58 @@ Gramática da assinatura: `RET(ARG,ARG,...)`, com tipos `void`, `bool`,
Requer `-lffi -ldl` (já no Makefile).
## `memory` — buffers com limites verificados
Para passar **structs** ao código nativo (GLFW, Vulkan, ...), `memory(n)`
aloca um buffer de n bytes cujo acesso é todo verificado:
```lua
local m = memory(16)
m:set_u32(0, 1234)
print(m:u32(0)) -- 1234
m:set_text(4, "hi") -- "hi" + NUL em 4..6
print(m:text(4)) -- hi
m:ptr() -- endereço, para uma chamada FFI
```
Campos: `u8..u64`, `i8..i64`, `f32`, `f64`, `text`, `set_*`, `ptr`,
`size`, `fill`, `copy`. Um acesso fora dos limites **levanta erro** em
vez de corromper a memória. Um valor `memory` passado onde se espera um
ponteiro entrega o endereço do buffer. Literais `0x`/`0b`/`0o` também
são aceitos (`0xAB`, `0b1010`).
## Bindings GLFW e Vulkan
`std/glfw` e `std/vulkan` são gerados dos headers/registry oficiais
(`tools/gen_glfw.py`, `tools/gen_vulkan.py`) e expõem nomes
**snake_case**:
```lua
use glfw
local v = memory(12)
glfw.get_version(v, v:ptr() + 4, v:ptr() + 8) -- 3 4 0
glfw.init()
glfw.window_hint(glfw.visible, glfw.false)
glfw.window_hint(0x00022001, 0) -- CLIENT_API = NO_API
local win = glfw.create_window(320, 240, "cat", 0, 0)
use vulkan
local ai = memory(vulkan.size_vk_application_info)
ai:fill(0)
ai:set_u32(0, vulkan.structure_type_application_info)
ai:set_u32(24, 0x00400000)
local ci = memory(vulkan.size_vk_instance_create_info)
ci:fill(0)
ci:set_u32(0, vulkan.structure_type_instance_create_info)
ci:set_u64(24, ai:ptr())
local inst = memory(8)
vulkan.create_instance(ci, 0, inst) -- VK_SUCCESS
```
Vulkan roda **headless** (lavapipe/llvmpipe); GLFW precisa de um display
(`xvfb-run` no CI). O modele expõe comandos, tamanhos de struct
(`vulkan.size_vk_*`), offsets de campos e constantes.
## Funções também são objetos
Uma closure tem sua própria tabela de membros, então carrega `_doc_` (e