1 /* 2 * The contents of this file are subject to the Netscape Public 3 * License Version 1.1 (the "License"); you may not use this file 4 * except in compliance with the License. You may obtain a copy of 5 * the License at http://www.mozilla.org/NPL/ 6 * 7 * Software distributed under the License is distributed on an 8 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed 9 * or implied. See the License for the specific language governing 10 * rights and limitations under the License. 11 * 12 * The Original Code is mozilla.org code. 13 * 14 * The Initial Developer of the Original Code is Netscape 15 * Communications Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. 17 * All Rights Reserved. 18 * 19 * Contributors: jlaprise (at) delanotech.com,pschwartau (at) netscape.com 20 * Date: 2001-07-10 21 * 22 * SUMMARY: Invoking try...catch through Function.call 23 * See http://bugzilla.mozilla.org/show_bug.cgi?id=49286 24 * 25 * 1) Define a function with a try...catch block in it 26 * 2) Invoke the function via the call method of Function 27 * 3) Pass bad syntax to the try...catch block 28 * 4) We should catch the error! 29 */ 30 //------------------------------------------------------------------------------------------------- 31 var UBound = 0; 32 var bug = 49286; 33 var summary = 'Invoking try...catch through Function.call'; 34 var cnErrorCaught = 'Error caught'; 35 var cnErrorNotCaught = 'Error NOT caught'; 36 var cnGoodSyntax = '1==2'; 37 var cnBadSyntax = '1=2'; 38 var status = ''; 39 var statusitems = []; 40 var actual = ''; 41 var actualvalues = []; 42 var expect= ''; 43 var expectedvalues = []; 44 45 46 var obj = new testObject(); 47 48 status = 'Section A of test: direct call of f'; 49 actual = f.call(obj); 50 expect = cnErrorCaught; 51 addThis(); 52 53 status = 'Section B of test: indirect call of f'; 54 actual = g.call(obj); 55 expect = cnErrorCaught; 56 addThis(); 57 58 59 60 //----------------------------------------- 61 test(); 62 //----------------------------------------- 63 64 65 function test() 66 { 67 enterFunc ('test'); 68 printBugNumber (bug); 69 printStatus (summary); 70 71 for (var i=0; i<UBound; i++) 72 { 73 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 74 } 75 76 exitFunc ('test'); 77 } 78 79 80 // An object storing bad syntax as a property - 81 function testObject() 82 { 83 this.badSyntax = cnBadSyntax; 84 this.goodSyntax = cnGoodSyntax; 85 } 86 87 88 // A function wrapping a try...catch block 89 function f() 90 { 91 try 92 { 93 eval(this.badSyntax); 94 } 95 catch(e) 96 { 97 return cnErrorCaught; 98 } 99 return cnErrorNotCaught; 100 } 101 102 103 // A function wrapping a call to f - 104 function g() 105 { 106 return f.call(this); 107 } 108 109 110 function addThis() 111 { 112 statusitems[UBound] = status; 113 actualvalues[UBound] = actual; 114 expectedvalues[UBound] = expect; 115 UBound++; 116 } 117