Fix ModelCollection missing Symbol.species, crashing on .push() after filter/map/slice - #1
Merged
Conversation
… plain Arrays ModelCollection extends Array but never overrides the static Symbol.species getter. Per the ES2015 species-construction protocol, that means any Array method that derives a new array from a ModelCollection instance (filter, map, slice, concat, etc.) constructs the result via `new ModelCollection(n)` where n is the derived array's length -- the same call shape as the plain Array(length) constructor. ModelCollection's own constructor instead treats that first argument as itemConstructor, so the derived array ends up with $itemConstructor set to a number. Any later .push() on it then throws "Right-hand side of 'instanceof' is not an object", since push() checks `item instanceof that.$itemConstructor`. This reliably crashes any consumer -- DWC itself or a plugin -- that does something as ordinary as `objectModel.tools.filter(...).push(...)`, and takes down the whole page since nothing catches a TypeError thrown deep inside a shared model getter/setter. Reported against a plugin hitting this via `objectModel.tools.filter(isOffsettable).map(g10For).filter(Boolean)` then `.push(saveCommand)` -- no plugin-specific logic involved, any chain of derived-array operations ending in .push() reproduces it. Overriding Symbol.species to return the plain Array constructor makes filter/map/slice/etc. return ordinary arrays, leaving the custom push()/update() behaviour only on genuine ModelCollection instances (constructed directly with an item type), which is the only place it's meaningful anyway. Added a regression test that reproduces the crash pre-fix and passes post-fix.
Contributor
|
Thanks! I may clean up the comment above |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Summary
ModelCollectionextendsArraybut never overrides the staticSymbol.speciesgetter. Per the ES2015 species-construction protocol, that means anyArraymethod that derives a new array from aModelCollectioninstance (filter,map,slice,concat, etc.) constructs the result vianew ModelCollection(n), wherenis the derived array's length — the same call shape as the plainArray(length)constructor.ModelCollection's own constructor instead treats that first argument asitemConstructor:So a derived array ends up with
$itemConstructorset to a number (the length). Any later.push()on it then throws:— since
push()checksitem instanceof that.$itemConstructor.This reliably crashes any consumer — DWC core or a plugin — that does something as ordinary as:
Nothing catches a
TypeErrorthrown deep inside a shared model getter/setter, so it takes down the whole page.How I found this
Investigating a crash report in a DWC plugin (duet-tool-align) that builds a G-code command list via
objectModel.tools.filter(isOffsettable).map(g10For).filter(Boolean), then doescmds.push(saveCommand). Traced the minified production stack trace back to this exact class/method in a locally-built copy ofv3.7-devand reproduced it directly againstModelCollectionwith no plugin-specific logic involved — any chain of derived-array operations ending in.push()reproduces it.Minimal repro (also covered by the added test):
Fix
Override
Symbol.speciesto return the plainArrayconstructor, sofilter/map/slice/etc. return ordinary arrays. This leaves the custompush()/update()behaviour only on genuineModelCollectioninstances (constructed directly with an item type), which is the only place it's meaningful anyway.Testing
__tests__/collection.ts: constructs aModelCollection, derives viafilter/map, and asserts the result is a plain array and that.push()on it doesn't throw. Confirmed this test fails with the exact reported error on the unmodified class, and passes with the fix.npx tsc -p tsconfig.test.json --noEmit— clean.jestsuite (npx jest --config jestconfig.json) — 36/36 passing, including the new test.