@@ -78,6 +78,14 @@ class StackNode extends Node {
7878 */
7979 this . _currentNode = null ;
8080
81+ /**
82+ * Stores additional data for nodes that are added to the stack.
83+ *
84+ * @private
85+ * @type {Map<Node, {delta: number}> }
86+ */
87+ this . _nodeDataLibrary = new Map ( ) ;
88+
8189 /**
8290 * This flag can be used for type testing.
8391 *
@@ -111,10 +119,10 @@ class StackNode extends Node {
111119 * Adds a node to this stack.
112120 *
113121 * @param {Node } node - The node to add.
114- * @param {number } [index=this.nodes.length ] - The index where the node should be added.
122+ * @param {number } [index=-1 ] - The index of the node. If not specified, the node will be added to the end of the stack .
115123 * @return {StackNode } A reference to this stack node.
116124 */
117- addToStack ( node , index = this . nodes . length ) {
125+ addToStack ( node , index = - 1 ) {
118126
119127 if ( node . isNode !== true ) {
120128
@@ -123,6 +131,35 @@ class StackNode extends Node {
123131
124132 }
125133
134+
135+ if ( index === - 1 ) {
136+
137+ if ( this . _currentNode ) {
138+
139+ let nodeData = this . _nodeDataLibrary . get ( this . _currentNode ) ;
140+
141+ if ( nodeData === undefined ) {
142+
143+ nodeData = {
144+ delta : 0
145+ } ;
146+
147+ this . _nodeDataLibrary . set ( this . _currentNode , nodeData ) ;
148+
149+ }
150+
151+ nodeData . delta ++ ;
152+
153+ index = this . nodes . indexOf ( this . _currentNode ) + nodeData . delta ;
154+
155+ } else {
156+
157+ index = this . nodes . length ;
158+
159+ }
160+
161+ }
162+
126163 this . nodes . splice ( index , 0 , node ) ;
127164
128165 return this ;
@@ -137,7 +174,7 @@ class StackNode extends Node {
137174 */
138175 addToStackBefore ( node ) {
139176
140- const index = this . _currentNode ? this . nodes . indexOf ( this . _currentNode ) : 0 ;
177+ const index = this . _currentNode !== null ? this . nodes . indexOf ( this . _currentNode ) : - 1 ;
141178
142179 return this . addToStack ( node , index ) ;
143180
@@ -324,15 +361,18 @@ class StackNode extends Node {
324361
325362 //
326363
327- const buildNode = ( node ) => {
364+ for ( let i = 0 ; i < this . nodes . length ; i ++ ) {
365+
366+ const node = this . nodes [ i ] ;
367+ const previousNode = this . _currentNode ;
328368
329369 this . _currentNode = node ;
330370
331371 if ( node . isVarNode && node . isIntent ( builder ) ) {
332372
333373 if ( node . isAssign ( builder ) !== true ) {
334374
335- return ;
375+ continue ;
336376
337377 }
338378
@@ -353,33 +393,15 @@ class StackNode extends Node {
353393
354394 if ( node . isVarNode && parents && parents . length === 1 && parents [ 0 ] && parents [ 0 ] . isStackNode ) {
355395
356- return ; // skip var nodes that are only used in .toVarying()
396+ continue ; // skip var nodes that are only used in .toVarying()
357397
358398 }
359399
360400 node . build ( builder , 'void' ) ;
361401
362402 }
363403
364- } ;
365-
366- //
367-
368- const nodes = [ ...this . nodes ] ;
369-
370- for ( const node of nodes ) {
371-
372- buildNode ( node ) ;
373-
374- }
375-
376- this . _currentNode = null ;
377-
378- const newNodes = this . nodes . filter ( ( node ) => nodes . indexOf ( node ) === - 1 ) ;
379-
380- for ( const node of newNodes ) {
381-
382- buildNode ( node ) ;
404+ this . _currentNode = previousNode ;
383405
384406 }
385407
0 commit comments