@@ -121,29 +121,48 @@ const {
121
121
player = new myModule . Game . Player ( namePtr ) ;
122
122
__release ( namePtr ) ;
123
123
}
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
+ }
125
131
126
132
// 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
+ }
129
139
130
140
// 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.
132
144
{
133
- const positionPtr = player . position ; // calls a getter (implicit `return`)
145
+ const positionPtr = player . position ; // implicit getter, retained for us
134
146
const position = myModule . Game . Position . wrap ( positionPtr ) ;
135
147
console . log ( " Position (wrapped): " + position . x + "/" + position . y ) ;
136
148
137
149
position . x -= 100 ;
138
150
position . y += 200 ;
139
- console . log ( " Position (moved): " + __getString ( position . toString ( ) ) ) ;
140
151
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 ) ;
142
157
}
143
158
144
159
// 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
+ }
147
166
148
167
__release ( player ) ; // a tidy house, a tidy mind.
149
168
}
0 commit comments