@@ -121,29 +121,48 @@ const {
121121 player = new myModule . Game . Player ( namePtr ) ;
122122 __release ( namePtr ) ;
123123 }
124- console . log ( " Player (new): " + __getString ( player . toString ( ) ) ) ;
124+
125+ // Let's see how our player looks now by calling toString
126+ {
127+ const strPtr = player . toString ( ) ;
128+ console . log ( " Player (new): " + __getString ( strPtr ) ) ;
129+ __release ( strPtr ) ;
130+ }
125131
126132 // Move them and log again
127- player . move ( 10 , 20 ) ;
128- console . log ( " Player (moved): " + __getString ( player . toString ( ) ) ) ;
133+ {
134+ player . move ( 10 , 20 ) ;
135+ const strPtr = player . toString ( ) ;
136+ console . log ( " Player (moved): " + __getString ( strPtr ) ) ;
137+ __release ( strPtr ) ;
138+ }
129139
130140 // Obtaining just the position. Note that we can `wrap` any pointer with
131- // the matching class within the object structure made by the loader.
141+ // the matching class within the object structure made by the loader, and
142+ // that a position's x and y properties are just basic values, not objects,
143+ // so tracking references does not apply to them.
132144 {
133- const positionPtr = player . position ; // calls a getter (implicit `return`)
145+ const positionPtr = player . position ; // implicit getter, retained for us
134146 const position = myModule . Game . Position . wrap ( positionPtr ) ;
135147 console . log ( " Position (wrapped): " + position . x + "/" + position . y ) ;
136148
137149 position . x -= 100 ;
138150 position . y += 200 ;
139- console . log ( " Position (moved): " + __getString ( position . toString ( ) ) ) ;
140151
141- __release ( positionPtr ) ; // we are done with the returned object
152+ const strPtr = position . toString ( ) ;
153+ console . log ( " Position (moved): " + __getString ( strPtr ) ) ;
154+ __release ( strPtr ) ;
155+
156+ __release ( positionPtr ) ;
142157 }
143158
144159 // Finish 'em
145- player . kill ( ) ;
146- console . log ( " Player (finished): " + __getString ( player . toString ( ) ) ) ;
160+ {
161+ player . kill ( ) ;
162+ const strPtr = player . toString ( ) ;
163+ console . log ( " Player (finished): " + __getString ( strPtr ) ) ;
164+ __release ( strPtr ) ; // we are done with the returned object
165+ }
147166
148167 __release ( player ) ; // a tidy house, a tidy mind.
149168}
0 commit comments