Skip to content
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

Make SimpleExpression copy the received array before returning it #5329

Conversation

TPGamesNL
Copy link
Member

Description

The Javadoc of Expressions getArray and getAll state that the returned array must not be an internal array (i.e. it can be modified freely). However, the Javadoc of SimpleExpression#get(Event) and PropertyExpression#get(Event, F[]) do not have this requirement.

The implementations of SimpleExpression getArray and getAll, and PropertyExpression#getAll did not use this properly, as it assumed the returned arrays (from the get methods) were not internal, as they were sometimes directly returned. This means that an internal array could be returned in a getArray or getAll method, which should not be allowed.

I changed it so the implementations of getArray and getAll will now never return the array received directly. Alternatively, I could've added the requirement to SimpleExpression and PropertyExpression to not return an internal array. However, since these classes are made to allow for an simpler implementation of expressions, I felt like this is not something those simple implementation should be expected to deal with (and on top of that, it'd be a breaking change).


Target Minecraft Versions: any
Requirements: none
Related Issues: #5322

@TPGamesNL TPGamesNL added bug An issue that needs to be fixed. Alternatively, a PR fixing an issue. enhancement Feature request, an issue about something that could be improved, or a PR improving something. labels Jan 6, 2023
Copy link
Contributor

@TheLimeGlass TheLimeGlass left a comment

Choose a reason for hiding this comment

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

Wont this affect how the changers work? The change method uses the getArray and would allow for modification of the internal objects using super.change if that's the goal of the expression.

@TheLimeGlass TheLimeGlass added the 2.7 Targeting a 2.7.X version release label Jan 27, 2023
@TPGamesNL
Copy link
Member Author

Wont this affect how the changers work? The change method uses the getArray and would allow for modification of the internal objects using super.change if that's the goal of the expression.

No, changers operate on the objects in the array, not on the array itself. The objects can still be modified just fine. Besides, usually getArray already makes a copy of the array it got from get, this was just one case that wasn't covered.

@TheLimeGlass TheLimeGlass merged commit a052b1e into SkriptLang:master Jan 31, 2023
@TPGamesNL TPGamesNL deleted the enhancement/simple-expression-copy-array branch January 31, 2023 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.7 Targeting a 2.7.X version release bug An issue that needs to be fixed. Alternatively, a PR fixing an issue. enhancement Feature request, an issue about something that could be improved, or a PR improving something.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants