shell.js (1960B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /*--- 8 defines: [testLenientAndStrict, parsesSuccessfully, parseRaisesException, returns] 9 allow_unused: True 10 ---*/ 11 (function(global) { 12 13 /* 14 * Return true if both of these return true: 15 * - LENIENT_PRED applied to CODE 16 * - STRICT_PRED applied to CODE with a use strict directive added to the front 17 * 18 * Run STRICT_PRED first, for testing code that affects the global environment 19 * in loose mode, but fails in strict mode. 20 */ 21 global.testLenientAndStrict = function testLenientAndStrict(code, lenient_pred, strict_pred) { 22 return (strict_pred("'use strict'; " + code) && 23 lenient_pred(code)); 24 } 25 26 /* 27 * parsesSuccessfully(CODE) returns true if CODE parses as function 28 * code without an error. 29 */ 30 global.parsesSuccessfully = function parsesSuccessfully(code) { 31 try { 32 Function(code); 33 return true; 34 } catch (exception) { 35 return false; 36 } 37 }; 38 39 /* 40 * parseRaisesException(EXCEPTION)(CODE) returns true if parsing CODE 41 * as function code raises EXCEPTION. 42 */ 43 global.parseRaisesException = function parseRaisesException(exception) { 44 return function (code) { 45 try { 46 Function(code); 47 return false; 48 } catch (actual) { 49 return exception.prototype.isPrototypeOf(actual); 50 } 51 }; 52 }; 53 54 /* 55 * returns(VALUE)(CODE) returns true if evaluating CODE (as eval code) 56 * completes normally (rather than throwing an exception), yielding a value 57 * strictly equal to VALUE. 58 */ 59 global.returns = function returns(value) { 60 return function(code) { 61 try { 62 return eval(code) === value; 63 } catch (exception) { 64 return false; 65 } 66 } 67 } 68 69 })(this);