Add display.isValidObject function#908
Open
XeduR wants to merge 1 commit into
Open
Conversation
- Add display.isValidObject( object ) to init.lua - Set _isRemoved on removed proxies; cascade to group and snapshot descendants - Clear _isRemoved on re-insert so same-frame re-parent rescues work - Make double-remove a no-op instead of an error - Set _isInvalid on newImage / newImageRect proxies when the bitmap loads as zero bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
display.isValidObject
Adds a Lua-level API for checking whether a given object is a valid display object that has not been removed.
Current behaviour
Object removal
Calling
display.remove( object )orobject:removeSelf()will take until the next frame to finish removing the object. In certain situations, like with collision events, callbacks, etc. it's possible that an object has been removed, but the removal isn't yet finished, which results in a crash if the app tries to access the object.Example
There is currently no reliable way to programmatically checking if a display object has already been removed and developers need to implement their own ad-hoc approaches to test for it.
Image object validity
Solar2D detects when
display.newImageordisplay.newImageRectis given a file that is not an image, or if the image is corrupted or otherwise invalid. Solar2D sends a warning about it to console viaCoronaLuaWarning(L, "file '%s' does not contain a valid image", imageName);. Funnily enough, Solar2D does not currently provide a reliable way to check this programmatically.What this PR does
This PR adds two internal proxy flags to objects,
_isRemovedand_isInvalid, that the engine sets at the moment an object is removed or when Solar2D detects an image is invalid upon trying to create it.This PR also adds a new
display.isValidObject( object )that verifies if a given object is 1) a display object, 2) it has not been removed, and 3) it is not invalid. The function always return a Boolean value, giving developers a reliable way of checking if their display objects are indeed valid or not.Example
Example
Unit tests
The test project runs 18 unit tests that exercise
display.isValidObjectacross all the edge cases: type safety with non-display-object inputs (nil, numbers, strings, tables, functions), removal viadisplay.removeandremoveSelf, idempotent double-removal, stage validity, group cascade at various nesting depths,finalizelistener timing, same-frame re-parent rescue for both groups and snapshots, every display primitive type (rect, circle, line, polygon, text, container, snapshot, group, capture), widgets, sprites, emitters, snapshot internals (snapshot.groupandsnapshot.canvaswith children), and corrupt/missing images vianewImageandnewImageRectto verify the_isInvalidflag. Each test checksisValidObjectbefore removal, same-frame after removal, and next-frame after removal, reporting pass/fail.isValidObject test.zip