Skip to content

Add support for generating LuaCATS definitions for the Lua API#3755

Open
kalimag wants to merge 29 commits intoTASEmulators:masterfrom
kalimag:pr/generate-lua-api-definitions
Open

Add support for generating LuaCATS definitions for the Lua API#3755
kalimag wants to merge 29 commits intoTASEmulators:masterfrom
kalimag:pr/generate-lua-api-definitions

Conversation

@kalimag
Copy link
Copy Markdown
Contributor

@kalimag kalimag commented Aug 21, 2023

Adds the ability to generate API definitions in the LuaCATS format to the Lua Console.

LuaCATS is primarily the annotation format of Lua Language Server, which drives the most popular VS Code Lua extension and can be used in other editors with language server support.

LuaCATS is an extension/fork of the annotation format used by EmmyLua, but I'm not sure what the status of the latter is. These definitions might be compatible with EmmyLua, but the only spec of its format I could find is sparse and partially in Chinese. EmmyLua extension now uses LuaCATS annotations, too.

The generated definitions could be further improved, e.g. enumerating valid values for surfaceName parameters, specifying the structure of certain table values, and callback function signatures. But that would require extending the API metadata and I wanted to get some feedback first.

Current output:

Check if completed:

will resolve #3860

@YoshiRulz YoshiRulz self-requested a review August 21, 2023 07:49
@kalimag
Copy link
Copy Markdown
Contributor Author

kalimag commented Aug 21, 2023

Just for example, some issues LuaLS can detect in the bundled scripts using these definitions:

image

image

@kalimag kalimag force-pushed the pr/generate-lua-api-definitions branch from bd55b04 to 72fc938 Compare October 31, 2025 12:06
@YoshiRulz YoshiRulz added Request: Feature/Enhancement For feature requests or possible improvements re: Lua API/scripting Relating to EmuHawk's Lua API (not the Lua Console) Meta Relating to code organisation or to things that aren't code labels Oct 31, 2025
@vadosnaprimer
Copy link
Copy Markdown
Contributor

Let's move forward with this one!

I decided to move from notepad++ to vscode for lua coding after 15 years, and ended up making exactly the same thing independently yesterday, I didn't even remember this PR! And here it's done in a much nicer way, so let's have it!

Copy link
Copy Markdown
Member

@YoshiRulz YoshiRulz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After fixing malformed examples, works w/ SumnekoLua plugin in IDEA.

@vadosnaprimer vadosnaprimer force-pushed the pr/generate-lua-api-definitions branch from 48bca5b to 246190f Compare March 23, 2026 16:58
@vadosnaprimer

This comment was marked as off-topic.

@vadosnaprimer vadosnaprimer force-pushed the pr/generate-lua-api-definitions branch from 18425a1 to 0678148 Compare March 24, 2026 17:06
@vadosnaprimer vadosnaprimer marked this pull request as ready for review March 24, 2026 17:25
@YoshiRulz YoshiRulz self-requested a review March 26, 2026 11:06
{
// technically any string parameter can be passed a number, but let's just focus on the ones where it's commonly used
// like `gui.text` and `forms.settext` instead of polluting the entire API surface
if (parameter.Name is "message" or "caption" && parameter.ParameterType == typeof(string))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to encourage this. There are enough footguns in calling gui.* already with Lua not having named arguments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this will likely cause a lot of unnecessary warnings

Copy link
Copy Markdown
Member

@YoshiRulz YoshiRulz Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think type coercion warnings are good, but then I also consider dynamic languages like Lua to always be inferior to statically-typed languages. Is passing numbers for strings idiomatic in general, or only in concatenation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, number-to-string conversion is specifically a feature in BizHawk's Lua bindings, not a feature of the Lua runtime. That's why LLS warns about it unless the parameter definition allows numbers.

But reading numbers from memory and passing them to gui functions is a pretty common pattern in BizHawk scripts, so I added this special case for those functions to avoid warnings in existing, working scripts. I have a test directory with the bundled Lua scripts and some from the Lua scripts catalog, and removing this code causes 73 additional warnings, 16 of which are in BizHawk's own scripts (for example).

I have no strong opinions on deprecating this behavior, but currently it's valid so the definitions say it's valid.


if (parameter.Member.DeclaringType == typeof(GuiLuaLibrary) && parameter.Name == "surfaceName" && parameter.ParameterType == typeof(string))
{
return "surface";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should introduce new attributes if this grows to any more enums.

throw new NotSupportedException($"Unknown type {type.FullName} used in API. Generator must be updated to handle this.");
}

private static bool IsNullable(Type type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference types (i.e. string and byte[]) are nullable too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but this is about documenting when null/nil is valid, and in the Lua API it's generally not unless the parameter is optional.

If the Lua libraries used #nullabe annotations that information should be taken into account here, but currently that's not the case (and also I don't think .NET Framework has a reflection API to read that information).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reference types in the Lua API are nullable because Lua is not a .NET language and cannot "see" the NRT metadata.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not follow. Are you saying want every string parameter to be marked string? in the definitions?

Copy link
Copy Markdown
Member

@YoshiRulz YoshiRulz Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing nil for a parameter annotated as string results in a warning from the language server, so yes they all need to be string?. Unless you want to create another attribute and mark all the functions which throw on null arguments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the point, though? gui.addmessage(nil) throws an error, so the diagnostics should warn about it. event.onexit(function() end, nil) doesn't throw an error, so diagnostics shouldn't warn.

That both are a nullable reference type on the technical level is irrelevant. As far as Lua is concerned, anything goes and you can pass a dozen table arguments to memory.writebyte if you want, but the diagnostics should help you catch that before it causes a runtime error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Didn't see the edit)

Currently the assumption for reference types is: parameters cannot be null unless they are optional. That seems to work well with the existing API surface.

If there turn out to be exceptions where this doesn't hold true, I think it would be preferable to mark nullable parameters rather than not-nullable ones, and for that I'd prefer to just reuse C# #nullable annotations rather than creating a bespoke attribute, if reasonably possible.

vadosnaprimer and others added 7 commits March 27, 2026 23:34
revert if we don't want this anymore, but with LLS's ability to take in entire folders via config this feels nicer IMO

and yeah I wouldn't mind adding this output to releases under something like `_luacats_definitions` folder in `Assets/Lua`
These are globals rather than `require`'d imports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Meta Relating to code organisation or to things that aren't code re: Lua API/scripting Relating to EmuHawk's Lua API (not the Lua Console) Request: Feature/Enhancement For feature requests or possible improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output Lua annotations for editors

3 participants