fix(runtime): resolve the na helper to NaN in Series.from() (UDT = na defaults) - #315
Merged
Merged
Conversation
UDT field defaults declared as `float x = na` stored the runtime NAHelper object in the field instead of NaN. `na(obj.x)` masked it (NAHelper.any special-cases the helper), but `nz(obj.x, v)` returned the helper untouched, and pushing that into a typed array threw: Cannot call 'array.unshift' with argument 'value'='[object Object]'. An argument of 'literal object' type was used but a 'float' is expected. Series.from() only unwrapped helpers whose `__value` is a Series (time, time_close). NAHelper's `__value` is a scalar NaN, so it fell through and was wrapped as an opaque object. Resolve scalar `__value` helpers at this common unwrap point so UDT defaults, nz(), math.* and every other consumer see a real NaN. Adds a regression test to tests/core/udt-field-defaults.test.ts. Co-authored-by: Cursor <cursoragent@cursor.com>
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_325eab94-ca17-4a79-ba82-29b87330c6a2) |
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.
Problem
Running a Pine v6 script with a user-defined type whose fields default to
na(e.g. LuxAlgo's RSI Divergence: Out-of-Sample Optimizer) throws on the first bar:Root cause
type SimStatedeclaresfloat rsi = na. The transpiler emits the default as['float', na], wherenaat runtime is theNAHelpersingleton.Type.newresolves defaults viaSeries.from(default).get(0).Series.from()only unwrapped helpers whose__valueis aSeries(time,time_close);NAHelper.__valueis a scalarNaN, so the helper fell through and was wrapped as an opaque object. The field ended up holding the helper itself.na(obj.x)masked the problem (NAHelper.anyspecial-cases the helper), butnz(obj.x, 50)only checksnull/undefined/NaN, so it returned the helper unchanged. On bar 0 the script hits exactlysim.rsi := nz(sim.rsi, 50)and thensim.rsi_hist.unshift(sim.rsi), which rejects the object.Fix
Series.from()now resolves any__valuehelper whose value is a scalar (thenahelper) to that scalar. This is the common unwrap point, so it fixes UDT= nadefaults,nz(na),math.*on such fields, and any other consumer in one place. Duck-typed on__valueto avoid aSeries→Corecircular import.Tests
tests/core/udt-field-defaults.test.ts(explicit \= na` defaults resolve to a real na (NaN), not the na helper object`). Fails with the exact error above before the fix; passes after.Note
Medium Risk
Central conversion in
Series.from()affects any code path that unwraps__valuehelpers, though the change is narrow and well-covered by tests.Overview
Fixes UDT fields declared with
= na(e.g.float x = na) storing the runtimeNAHelperobject instead of NaN, which brokenz(), arithmetic, and typedarrayoperations that expect scalars.Series.from()now unwraps duck-typed helpers with__value: it still returns backingSeriesinstances for dual-use helpers liketime, and for scalar__value(includingna’s NaN) wraps that value in a one-elementSeriesinstead of treating the whole helper as an opaque object.Adds a v6 UDT regression test covering
nz(obj.x, …),na(obj.x), andobj.x + 1so defaults behave like realnaend-to-end.Reviewed by Cursor Bugbot for commit a606e27. Bugbot is set up for automated code reviews on this repo. Configure here.