feat(ladfile_builder): First look into a native Luau (.d.luau) LAD backend#546
Open
jonasdedden wants to merge 1 commit into
Open
feat(ladfile_builder): First look into a native Luau (.d.luau) LAD backend#546jonasdedden wants to merge 1 commit into
jonasdedden wants to merge 1 commit into
Conversation
bevy_mod_scripting can dump its reflection registry to a LAD file and ships post-processors for the Lua Language Server (`--- @class` .lua) and mdbook, but no native Luau one — and luau-lsp cannot consume the LuaLS dialect. Luau users (the `luau` runtime feature already exists) therefore have no way to type-check their scripts from the registry. Add that backend as a feature-gated module in ladfile_builder: - `luau::lad_to_luau(&LadFile, &LuauBackendConfig) -> String`: the whole backend as a pure conversion, emitting `declare class … end` / `declare name: T`. - `luau::LuauLadPlugin`: a `LadFilePlugin` processor, wired into `default_processors()` behind the new `luau_files` feature. It needs no new dependencies (pure string generation over the `ladfile` types), which is why it's a module here rather than a separate backend crate like the LuaLS one. Handles Luau grammar edges: reserved words can't be bare identifiers (quoted `["end"]` for fields, suffixed elsewhere), the unit type is `()` only in a return arrow (`nil` otherwise), static accessor globals are excluded from real instance handles, and unknown types resolve to `any`. `focus_crates` scopes which crates' types get full classes so the output stays readable. An opt-in `HandleBranding` config emits a `Reg<T>` phantom brand: it rewrites the component getter to `get_component: <T>(…, reg: Reg<T>) -> T?` and brands each host-registered `<Component><suffix>` registration global as `Reg<Component>`, giving cast-free, statically-typed component access. Off by default since it assumes a host naming convention. Tested against `ladfile::EXAMPLE_LADFILE` (general surface, brand gating) and a small crafted fixture (full brand path); generated output verified to load and type-check under luau-lsp. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Changed Files
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a first proposal for generating Luau type definition files (corresponding issue: #545). Please let me know whether you like the general approach or not.
Tests
src/lib.rscarries module tests that parse a real registry dump (src/test_assets/bindings.lad.json) and assert both modes:converts_general_surface— classes/fields/world/host globals present, and noReg<T>leakage in the default output.converts_branded_surface—Reg<T>, the genericget_component, and the…Typehandle branding all present when opted in.sanitizes_and_escapes— reserved-word handling.Plus a well-formedness sweep asserting no Luau keyword is emitted as a bare field/method/param name.
cargo testin this folder runs them againstladfile.Luau grammar edges handled
["end"]for fields, suffixed (end_) for methods/params/type names;()only in a return arrow — rendered asnilelsewhere;is_staticseparates those from real instance handles likeworld;any(Luau treats it permissively), kept focused viafocus_crates.