1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_8_1/strict/shell.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 + 1.6 +/* 1.7 + * Any copyright is dedicated to the Public Domain. 1.8 + * http://creativecommons.org/licenses/publicdomain/ 1.9 + */ 1.10 + 1.11 + 1.12 +/* 1.13 + * Return true if both of these return true: 1.14 + * - LENIENT_PRED applied to CODE 1.15 + * - STRICT_PRED applied to CODE with a use strict directive added to the front 1.16 + * 1.17 + * Run STRICT_PRED first, for testing code that affects the global environment 1.18 + * in loose mode, but fails in strict mode. 1.19 + */ 1.20 +function testLenientAndStrict(code, lenient_pred, strict_pred) { 1.21 + return (strict_pred("'use strict'; " + code) && 1.22 + lenient_pred(code)); 1.23 +} 1.24 + 1.25 +/* 1.26 + * completesNormally(CODE) returns true if evaluating CODE (as eval 1.27 + * code) completes normally (rather than throwing an exception). 1.28 + */ 1.29 +function completesNormally(code) { 1.30 + try { 1.31 + eval(code); 1.32 + return true; 1.33 + } catch (exception) { 1.34 + return false; 1.35 + } 1.36 +} 1.37 + 1.38 +/* 1.39 + * raisesException(EXCEPTION)(CODE) returns true if evaluating CODE (as eval 1.40 + * code) throws an exception object whose prototype is 1.41 + * EXCEPTION.prototype, and returns false if it throws any other error 1.42 + * or evaluates successfully. For example: raises(TypeError)("0()") == 1.43 + * true. 1.44 + */ 1.45 +function raisesException(exception) { 1.46 + return function (code) { 1.47 + try { 1.48 + eval(code); 1.49 + return false; 1.50 + } catch (actual) { 1.51 + return exception.prototype.isPrototypeOf(actual); 1.52 + } 1.53 + }; 1.54 +}; 1.55 + 1.56 +/* 1.57 + * parsesSuccessfully(CODE) returns true if CODE parses as function 1.58 + * code without an error. 1.59 + */ 1.60 +function parsesSuccessfully(code) { 1.61 + try { 1.62 + Function(code); 1.63 + return true; 1.64 + } catch (exception) { 1.65 + return false; 1.66 + } 1.67 +}; 1.68 + 1.69 +/* 1.70 + * parseRaisesException(EXCEPTION)(CODE) returns true if parsing CODE 1.71 + * as function code raises EXCEPTION. 1.72 + */ 1.73 +function parseRaisesException(exception) { 1.74 + return function (code) { 1.75 + try { 1.76 + Function(code); 1.77 + return false; 1.78 + } catch (actual) { 1.79 + return exception.prototype.isPrototypeOf(actual); 1.80 + } 1.81 + }; 1.82 +}; 1.83 + 1.84 +/* 1.85 + * Return the result of applying uneval to VAL, and replacing all runs 1.86 + * of whitespace with a single horizontal space (poor man's 1.87 + * tokenization). 1.88 + */ 1.89 +function clean_uneval(val) { 1.90 + return uneval(val).replace(/\s+/g, ' '); 1.91 +}