Skip to content

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

Open
kalimag wants to merge 31 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 31 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
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs Outdated
@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.

Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs Outdated
Comment thread src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs Outdated
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
@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.

Comment thread src/BizHawk.Client.EmuHawk/tools/Lua/LuaAutocompleteInstaller.cs Outdated
@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.

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.

Reference types are now annotated as nullable, unless they are compiled with #nullable enable (and not marked as nullable in C#).

Still need to do the legwork of adding #nullable to existing Lua libs.

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.

Wait they are available with reflection? I was under the impression that you needed NullabilityInfoContext from .NET Core.
I'll see about removing the need for dynamic.

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.

Sure, it just gets rather complicated to correctly interpret the attribute data without NullabilityInfoContext once you get into generics and nested arrays and whatever. But for this purpose it's simple enough.

Comment thread src/BizHawk.Client.EmuHawk/tools/Lua/LuaAutocompleteInstaller.cs
@YoshiRulz
Copy link
Copy Markdown
Member

YoshiRulz commented Apr 11, 2026

Please rebase.
Do you mind if I add [LuaZeroIndexed] myself? It could be used in the existing TASVideos wiki markup, and those functions' current documentation make no mention of it. edit: fdec9a3

@kalimag kalimag force-pushed the pr/generate-lua-api-definitions branch from d7a355e to aee330b Compare April 12, 2026 22:43
@YoshiRulz YoshiRulz added Waiting on dev consensus and removed Request: Feature/Enhancement For feature requests or possible improvements labels Apr 13, 2026
YoshiRulz added a commit that referenced this pull request Apr 14, 2026
the PR #3755 adds a button to generate these automatically, similar to
the existing TASVideos wiki generation
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) Waiting on dev consensus

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output Lua annotations for editors

3 participants