• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/99_bugs/63_tostring_exception_propagation

  1 An exception raised by a tostring() callback must reach the caller of
  2 the string conversion. A failed callback made the formatter fall back to
  3 traversing the value, and the next callback invoked by the traversal or
  4 by a subsequent conversion discarded the pending exception, so that the
  5 error got lost and further callbacks ran.
  6 
  7 1. A failed callback must neither fall back to formatting the value nor
  8 let later conversions invoke further callbacks.
  9 
 10 -- Testcase --
 11 for (let name in [ 'tostring', '__tostring__' ]) {
 12         for (let format in [ '%s%s', '%J%J' ]) {
 13                 let events = [];
 14                 let later = proto({}, { tostring: () => {
 15                         push(events, 'unexpected');
 16 
 17                         return 'Y';
 18                 } });
 19                 let value = proto({ child: later }, {
 20                         [name]: () => {
 21                                 push(events, 'throw');
 22                                 gc();
 23                                 die('original');
 24                         }
 25                 });
 26 
 27                 try {
 28                         sprintf(format, value, later);
 29                         push(events, 'success');
 30                 }
 31                 catch (e) {
 32                         push(events, e.message);
 33                 }
 34 
 35                 print(name, ' ', format, ': ', join(',', events), '\n');
 36         }
 37 }
 38 -- End --
 39 
 40 -- Args --
 41 -R
 42 -- End --
 43 
 44 -- Expect stdout --
 45 tostring %s%s: throw,original
 46 tostring %J%J: throw,original
 47 __tostring__ %s%s: throw,original
 48 __tostring__ %J%J: throw,original
 49 -- End --
 50 
 51 
 52 2. Traversing nested arrays and objects stops at the first failing
 53 callback, the containers are writable and formattable again afterwards.
 54 
 55 -- Testcase --
 56 for (let shape in [ 'array', 'object' ]) {
 57         let events = [];
 58         let first = proto({}, { tostring: () => {
 59                 push(events, 'throw');
 60                 gc();
 61                 die('original');
 62         } });
 63         let later = proto({}, { tostring: () => {
 64                 push(events, 'unexpected');
 65 
 66                 return 'Y';
 67         } });
 68         let inner = (shape == 'array') ? [ first, later ] : { a: first, b: later };
 69         let outer = [ inner, later ];
 70 
 71         try {
 72                 sprintf('%.2J', outer);
 73                 push(events, 'success');
 74         }
 75         catch (e) {
 76                 push(events, e.message);
 77         }
 78 
 79         print(shape, ': ', join(',', events), '\n');
 80 
 81         if (shape == 'array') {
 82                 inner[0] = 'A';
 83                 inner[1] = 'B';
 84                 push(inner, 'C');
 85         }
 86         else {
 87                 inner.a = 'A';
 88                 inner.b = 'B';
 89                 inner.c = 'C';
 90         }
 91 
 92         outer[1] = 'D';
 93         push(outer, 'E');
 94         gc();
 95 
 96         print(sprintf('%J', outer), '\n');
 97 }
 98 -- End --
 99 
100 -- Args --
101 -R
102 -- End --
103 
104 -- Expect stdout --
105 array: throw,original
106 [ [ "A", "B", "C" ], "D", "E" ]
107 object: throw,original
108 [ { "a": "A", "b": "B", "c": "C" }, "D", "E" ]
109 -- End --
110 
111 
112 3. Packing a character receives the original exception, neither the
113 callbacks of the properties of the failing value nor the one of the next
114 field are invoked.
115 
116 -- Testcase --
117 let struct;
118 
119 try {
120         struct = require('struct');
121 }
122 catch (e) {
123         exit(77);
124 }
125 
126 for (let mode in [ 'buffer', 'module', 'format' ]) {
127         let events = [];
128         let child = proto({}, { tostring: () => {
129                 push(events, 'child');
130 
131                 return 'C';
132         } });
133         let first = proto({ child }, { tostring: () => {
134                 push(events, 'throw');
135                 die('original');
136         } });
137         let second = proto({}, { tostring: () => {
138                 push(events, 'next');
139 
140                 return 'Y';
141         } });
142 
143         try {
144                 if (mode == 'buffer')
145                         struct.buffer().put('2c', first, second);
146                 else if (mode == 'module')
147                         struct.pack('2c', first, second);
148                 else
149                         struct.new('2c').pack(first, second);
150 
151                 push(events, 'success');
152         }
153         catch (e) {
154                 push(events, e.message);
155         }
156 
157         print(mode, ': ', join(',', events), '\n');
158 }
159 -- End --
160 
161 -- Args --
162 -R
163 -- End --
164 
165 -- Expect stdout --
166 buffer: throw,original
167 module: throw,original
168 format: throw,original
169 -- End --
170 
171 
172 4. An exception caught within a callback does not affect the conversion
173 invoking that callback.
174 
175 -- Testcase --
176 let bad = proto({}, { tostring: () => die('inner') });
177 let first = proto({}, { tostring: () => {
178         try {
179                 sprintf('%J', [ bad ]);
180         }
181         catch (e) {
182                 print(e.message, '\n');
183         }
184 
185         gc();
186 
187         return 'A';
188 } });
189 let second = proto({}, { tostring: () => 'B' });
190 
191 print(sprintf('%s%s', first, second), '\n');
192 -- End --
193 
194 -- Args --
195 -R
196 -- End --
197 
198 -- Expect stdout --
199 inner
200 AB
201 -- End --
202 
203 
204 5. Calling exit() within the callback of a nested value still stops the
205 traversal without invoking further callbacks.
206 
207 -- Testcase --
208 sprintf('%J', {
209         a: { value: proto({}, { tostring: () => exit(3) }) },
210         b: proto({}, { tostring: () => {
211                 print('unexpected callback\n');
212 
213                 return 'B';
214         } })
215 });
216 
217 print('not reached\n');
218 -- End --
219 
220 -- Args --
221 -R
222 -- End --
223 
224 -- Expect exitcode --
225 3
226 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt