Skip to content

Conversation

@vurvdev
Copy link
Contributor

@vurvdev vurvdev commented Dec 23, 2024

Luau is a variant of Lua semi-recently becoming popular due to its open-sourcing and use by Roblox. It has a mix of features from 5.1-5.4 that make it not really fit into any traditional Lua version.

Why

There's roblox-ts but that's another project and not everyone is using Luau for Roblox. We're using it for our own game engine - and I'm aware of other projects making use of Luau (the popular mlua rust library supports it, despite being a "lua" library)

Luau does include a type system, but it's not yet capable enough for my usecases. Plus, that's ignoring all the other benefits that Typescript brings.

Changes

This PR:

  • Adds a Luau luaTarget
  • Adds a continue statement used solely by Luau.
  • Adds the if <COND> then <X> else <Y> ternary operator used solely by Luau.

There are other Luau-only features like type hints and compound assignment operators - but they could be done later on and aren't as pertinent

Please let me know how I should incorporate test cases - couldn't figure out how to do so myself, although I tested these features in a local project.

Copy link
Member

@Perryvw Perryvw left a comment

Choose a reason for hiding this comment

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

Looks good, though the test run seems to indicate you forgot some stuff still.

It is not really possible to execute luau in our tests so that's fine, but looking at the change it might be nice if you add a luau-specific translation snapshot test containing a continue and ternary expression here: https://github.com/TypeScriptToLua/TypeScriptToLua/blob/master/test/translation/transformation.spec.ts

Also fixed not providing Luau to the `expectEachVersionExceptJit` function. It seems to work properly in the test suite but I need to make sure that this is intended or if the function should be renamed to something else (like `expectOnlyPUC`) and exclude Luau as well.
@vurvdev
Copy link
Contributor Author

vurvdev commented Dec 25, 2024

Added a transformation test although I'd like to make sure you're okay with the current implementations for two potential issues:

  1. I implemented the Luau specific tests simply by checking if filename in the test spec includes "luau". Not the most elegant solution but the simplest. Maybe better off splitting into folders per version but that'd open up more questions on how I should implement that. Up to you.

  2. I don't understand the rationale behind expectEachVersionExceptJit to confide in my solution (I have it currently passing the expectation for Luau as well). Depending on the intention, it could be renamed and exclude Luau as well.

@Perryvw
Copy link
Member

Perryvw commented Dec 25, 2024

You can just run the tests locally with npm test (after npm install ofc) to check if everything is working. or even npx jest <test file name> to run specific files.

Now stored in separate folders. Future proof and doesn't feel gross.
Also disabled Luau in expectEachVersionExceptJit. Ideally the function would be renamed but the change would be too large here (not necessary either).
@vurvdev
Copy link
Contributor Author

vurvdev commented Dec 27, 2024

Tests should be in a good state now

Luau supports both math.atan & math.atan2 so having a certain result isn't important. Same goes for table.unpack and unpack.

Had to pass an empty function for the continue case where luau wouldn't make a label at all since it has a proper continue statement.
[tstl.LuaTarget.Lua53]: expectContinueGotoLabel,
[tstl.LuaTarget.Lua54]: expectContinueGotoLabel,
[tstl.LuaTarget.LuaJIT]: expectContinueGotoLabel,
[tstl.LuaTarget.Luau]: () => undefined, // TODO: This is N/A.
Copy link
Member

Choose a reason for hiding this comment

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

Why is this this N/A? Shouldn't this be expecting the Luau continue statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That test specifically looks for the label or sentinel variable generated by the code for the other targets which isn't applicable to Luau since it just uses the builtin continue without a need for either.

@Perryvw Perryvw merged commit 7cb97ad into TypeScriptToLua:master Dec 29, 2024
5 checks passed
@vurvdev vurvdev deleted the luau branch December 29, 2024 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants