File: exception_propagation.js

package info (click to toggle)
kjs 5.103.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,020 kB
  • sloc: cpp: 36,704; javascript: 5,079; yacc: 790; perl: 191; sh: 52; makefile: 7
file content (489 lines) | stat: -rw-r--r-- 9,460 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
var global = this;
function myfunc() {
}
function throwex() {
  throw new Error("test exception");
}

//---------------------------
var func_ret_with_ex_func = 4;
try {
  func_ret_with_ex_func = throwex()();
}
catch (e) {
}
shouldBe("func_ret_with_ex_func", "4");

// ---------------------------------

var func_ret_from_ex_throw_args = 4;
try {
  func_ret_from_ex_throw_args = Math.abs(throwex());
}
catch (e) {
}
shouldBe("func_ret_from_ex_throw_args", "4");

// ---------------------------------

var set_from_throw_func_args = 4;
try {
  throwex()(set_from_throw_func_args = 1);
}
catch (e) {
}
shouldBe("set_from_throw_func_args","4");

// ---------------------------------

var set_from_func_throw_args = 4;
try {
  myfunc(throwex(), set_from_func_throw_args = 1);
}
catch (e) {
}
shouldBe("set_from_func_throw_args","4");

// ---------------------------------

var set_from_before_func_throw_args = 4;
try {
  myfunc(set_from_before_func_throw_args = 1, throwex());
}
catch (e) {
}
shouldBe("set_from_before_func_throw_args","1");

// ---------------------------------

// ### move to function.js
var function_param_order = "";
function aparam() {
  function_param_order += "a";
}
function bparam() {
  function_param_order += "b";
}
function cparam() {
  function_param_order += "c";
}
myfunc(aparam(),bparam(),cparam());
shouldBe("function_param_order","'abc'");

// ---------------------------------
// ### move to function.js
var new_param_order = "";
function anewparam() {
  new_param_order += "a";
}
function bnewparam() {
  new_param_order += "b";
}
function cnewparam() {
  new_param_order += "c";
}
new myfunc(anewparam(),bnewparam(),cnewparam());
shouldBe("new_param_order","'abc'");

// ---------------------------------
// ### move to function.js
var elision_param_order = "";
function aelision() {
  elision_param_order += "a";
}
function belision() {
  elision_param_order += "b";
}
function celision() {
  elision_param_order += "c";
}
[aelision(),belision(),celision()];
shouldBe("elision_param_order","'abc'");

// ---------------------------------
// ### move to function.js
var comma_order = "";
function acomma() {
  comma_order += "a";
}
function bcomma() {
  comma_order += "b";
}
function ccomma() {
  comma_order += "c";
}
acomma(),bcomma(),ccomma();
shouldBe("comma_order","'abc'");

// ---------------------------------

function checkOperator(op,name) {
  var code =(
    "global."+name+"_part1 = 4;\n"+
    "try {\n"+
    "  ("+name+"_part1 = 1) "+op+" throwex();\n"+
    "}\n"+
    "catch (e) {\n"+
    "}\n"+
    "shouldBe('"+name+"_part1', '1');\n"+
    "global."+name+"_part2 = 4;\n"+
    "try {\n"+
    "  throwex() "+op+" ("+name+"_part2 = 1);\n"+
    "}\n"+
    "catch (e) {\n"+
    "}\n"+
    "shouldBe('"+name+"_part2', '4');\n");
//  print("\n\n\n");
//  print(code);
  eval(code);
}

checkOperator("==","OpEqEq");
checkOperator("!=","OpNotEq");
checkOperator("===","OpStrEq");
checkOperator("!==","OpStrNEq");
// ### these generate a syntax error in mozilla - kjs should do the same (?)
//checkOperator("+=","OpPlusEq");
//checkOperator("-=","OpMinusEq");
//checkOperator("*=","OpMultEq");
//checkOperator("/=","OpDivEq");
//                  OpPlusPlus,
//		  OpMinusMinus,
checkOperator("<","OpLess");
checkOperator("<=","OpLessEq");
checkOperator(">","OpGreater");
checkOperator(">=","OpGreaterEq");
//checkOperator("&=","OpAndEq");
//checkOperator("^=","OpXOrEq");
//checkOperator("|=","OpOrEq");
//checkOperator("%=","OpModEq");
checkOperator("&&","OpAnd");
checkOperator("||","OpOr");
checkOperator("&","OpBitAnd");
checkOperator("^","OpBitXOr");
checkOperator("|","OpBitOr");
checkOperator("<<","OpLShift");
checkOperator(">>","OpRShift");
checkOperator(">>>","OpURShift");
//		  OpIn,
checkOperator("instanceof","OpInstanceOf");

// ---------------------------------
var set_from_if_stmt = 4;
try {
  if (throwex()) {
    set_from_if_stmt = 1;
  }
}
catch (e) {
}
shouldBe("set_from_if_stmt","4");

// ---------------------------------
var set_from_if_else_stmt = 4;
try {
  if (throwex()) {
    set_from_if_else_stmt = 1;
  }
  else {
    undefined;
  }
}
catch (e) {
}
shouldBe("set_from_if_else_stmt","4");

// ---------------------------------

var set_from_else_in_if_else_stmt = 4;
try {
  if (throwex()) {
    undefined;
  }
  else {
    set_from_else_in_if_else_stmt = 1;
  }
}
catch (e) {
}
shouldBe("set_from_else_in_if_else_stmt","4");

// ---------------------------------

var comma_left = 4;
try {
  comma_left = 1, throwex();
}
catch (e) {
}
shouldBe("comma_left","1");

// ---------------------------------

var comma_left = 4;
try {
   throwex(), comma_left = 1;
}
catch (e) {
}
shouldBe("comma_left","4");

var vardecl_assign_throws = 4;
try {
  var vardecl_assign_throws = throwex();
}
catch (e) {
}
shouldBe("vardecl_assign_throws","4");

// ---------------------------------

var var_assign_before_throw_run = false;
function var_assign_before_throw() {
  var_assign_before_throw_run = true;
  return 1;
}

var var_assign_after_throw_run = false;
function var_assign_after_throw() {
  var_assign_after_throw_run = true;
  return 1;
}

try {
  var var_assign1 = var_assign_before_throw(),
      var_assign2 = throwex(),
      var_assign1 = var_assign_before_throw();
}
catch (e) {
}
shouldBe("var_assign_before_throw_run","true");
shouldBe("var_assign_after_throw_run","false");

// ---------------------------------

var do_val = 4;
try {
  do {
    do_val++;
  }
  while (throwex());
}
catch (e) {
}
shouldBe("do_val","5");

// ---------------------------------
var while_val = 4;
try {
  while (throwex()) {
    while_val++;
  }
}
catch (e) {
}
shouldBe("while_val","4");

// ---------------------------------
var for_val_part1_throw2 = 4;
try {
  for (for_val_part1_throw2 = 1; throwex(); ) {
  }
}
catch (e) {
}
shouldBe("for_val_part1_throw2","1");

// ---------------------------------
var for_val_part1_throw3 = 4;
try {
  for (for_val_part1_throw3 = 1; ; throwex()) {
  }
}
catch (e) {
}
shouldBe("for_val_part1_throw3","1");

// ---------------------------------
var for_val_part2_throw1 = 4;
try {
  for (throwex(); for_val_part2_throw1 = 1; ) {
  }
}
catch (e) {
}
shouldBe("for_val_part2_throw1","4");

// ---------------------------------
var for_val_part2_throw3 = 4;
try {
  for (; for_val_part2_throw3 = 1; throwex()) {
  }
}
catch (e) {
}
shouldBe("for_val_part2_throw3","1");

// ---------------------------------
var for_val_part3_throw1 = 4;
try {
  for (throwex(); ; for_val_part3_throw1 = 1) {
  }
}
catch (e) {
}
shouldBe("for_val_part3_throw1","4");

// ---------------------------------
var for_val_part3_throw2 = 4;
try {
  for (; throwex(); for_val_part3_throw2 = 1) {
  }
}
catch (e) {
}
shouldBe("for_val_part3_throw2","4");

// ---------------------------------
var for_val_part1_throwbody = 4;
try {
  for (for_val_part1_throwbody = 1; ;) {
    throwex();
  }
}
catch (e) {
}
shouldBe("for_val_part1_throwbody","1");

// ---------------------------------
var for_val_part2_throwbody = 4;
try {
  for (; for_val_part2_throwbody = 1; ) {
    throwex();
  }
}
catch (e) {
}
shouldBe("for_val_part2_throwbody","1");

// ---------------------------------
var for_val_part3_throwbody = 4;
try {
  for (; ; for_val_part3_throwbody = 1) {
    throwex();
  }
}
catch (e) {
}
shouldBe("for_val_part3_throwbody","4");

// ---------------------------------
// ### mozilla gives a syntax error on for (throwex() in forin_test_obj)... should we?
// --- per spec, no, but we do in KDE4, so I commented this out for now. -- Maks

/*
var forin_test_obj = new Object();
forin_test_obj.a = 1;
forin_test_obj.b = 2;
forin_test_obj.c = 3;
var forin_count = 4;
function forin_lexpr() {
//  if (forincount == 1);
//    throwex();
  return new Object();
}
try {
  for (throwex() in forin_test_obj) {
    forin_count++;
  }
}
catch (e) {
}
shouldBe("forin_count","4");
*/
testFailed("Parsing of a CallExpression in for .. in");

// ---------------------------------
var set_inside_with_throw = 4;
try {
  with (throwex()) {
    set_inside_with_throw = 1;
  }
}
catch (e) {
}
shouldBe("set_inside_with_throw","4");

// ---------------------------------
var set_inside_with_cantconverttoobject = 4;
try {
  with (undefined) {
    print("FAIL. This message should not be displayed");
    set_inside_with_cantconverttoobject = 1;
  }
}
catch (e) {
}
shouldBe("set_inside_with_cantconverttoobject","4");
// ### test case, sw


// ---------------------------------
// Test to make sure finally propagates exceptions

finallyRan = false;
function testFinally() {
    try {
        throw 42;
    } finally {
	finallyRan = true;
    }
}

var finallyPropagated = null;
try {
    testFinally();
} catch (e) {
    finallyPropagated = e;
}

shouldBe('finallyRan', 'true');
shouldBe('finallyPropagated', '42');

// A messier one, throwing things all over the place
out = "";
try {
    try {
        throw "a";
    } catch(e) {
	out += "<catch:" + e + ">";
        throw "b";
    } finally {
        out += "<finally>";
        throw("c");
    }
} catch (e) {
    out += "<catch:" + e + ">";
}

shouldBe('out', '"<catch:a><finally><catch:c>"');

// Basically the same thing, only not throwing a new one from finally
out = "";
try {
    try {
        throw "a";
    } catch(e) {
	out += "<catch:" + e + ">";
        throw "b";
    } finally {
        out += "<finally>";
    }
} catch (e) {
    out += "<catch:" + e + ">";
}

shouldBe('out', '"<catch:a><finally><catch:b>"');


debug("Done.");