Skip to content

[WIP] Lua typecasting #997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 21, 2021
Merged

Conversation

Jampi0n
Copy link
Contributor

@Jampi0n Jampi0n commented Apr 14, 2021

This is an attempt to fix the lua bugs described in #994 and #995 (outside of the null string issue).
I marked it WIP, because it contains parts that can be improved, needs some clean up and better unit tests. My understanding of the code base is limited, so I'm sure there is lots of stuff that should be done differently.

I will summarize the changes and how it works:

  1. The fromIndex and toIndex functions have been restored for lua. This is important, because these functions are added to the intermediate program and omitting them would result in a different intermediate program for lua and JASS. Compiletime functions are run on the intermediate program, so they need to be the same semantically and especially their types need to match.
  2. This means implicitly that generics are implemented as integers, because generic arguments will be converted to index and generic return values will be converted from index. Since lua has no fixed types, the function bodies of generic functions do not need to be changed. They will just only deal with integers now (and class types, but they are considered integers by the intermediate language).
  3. In order to ensure type safety in lua, the fromIndex and toIndex functions are wrapped in functions that ensure the correct types: typeFromIndex(...) -> typeFromIndex(intEnsure(...)) and typeToIndex(...) -> intEnsure(typeToIndex(...))
    If for example HashMap.get now returns nil (the indexToObject function will return nil for non existing keys), the intEnsure function that is wrapped around HashMap.get will convert nil to 0.
    While these functions are added to the intermediate program in lua, the program is still semantically the same as without these function, because they are just identity functions for the intermediate language.
  4. During compiletime, class types are still represented as integers, so they will evaluate to 0 if they are null. Compiletime expression that evaluate to 0 and are not of numeric type will be replaced by null/nil.

This could serve as temporary fix until the new generics are implemented. If this is considered useful I can do some improvements, but I didn't want to spend more time on something that may not be of use.

Jampi0n and others added 10 commits April 14, 2021 16:10
uses typeEnsure functions to ensure correct types at all times
the intermediate language is not the same as in JASS, so compiletime execution is not correct
The typeEnsure functions have no effect on compiletime, so lua compiletime should now be exactly like JASS compiletime.
index type is ensured with intEnsure
this effectively makes generics integers like in JASS
casting type parameters to/from int still uses lua functions, even though they are already integers, so it would not be necessary
compiletime results are initially evaluated to 0 for objects that are null
in lua the 0 must be replaced with nil
use entry type for arrays and member type for member variables
use regular expressions to only check the functions of interest and ignore other parts of the output
Indexing table literals only works with parentheses around them.
@Frotty
Copy link
Member

Frotty commented May 21, 2021

As we discussed in DMs, I will merge this even if the implementation might not be ideal, since it makes the lua backend finally usable and new generics/type classes still seem a bit far in the future.
Thanks a lot for all the work you invested into this 👍

@Frotty Frotty merged commit 31d097f into wurstscript:master May 21, 2021
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