Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | |
michael@0 | 3 | /* |
michael@0 | 4 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 5 | * http://creativecommons.org/licenses/publicdomain/ |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | |
michael@0 | 9 | /* |
michael@0 | 10 | * Return true if both of these return true: |
michael@0 | 11 | * - LENIENT_PRED applied to CODE |
michael@0 | 12 | * - STRICT_PRED applied to CODE with a use strict directive added to the front |
michael@0 | 13 | * |
michael@0 | 14 | * Run STRICT_PRED first, for testing code that affects the global environment |
michael@0 | 15 | * in loose mode, but fails in strict mode. |
michael@0 | 16 | */ |
michael@0 | 17 | function testLenientAndStrict(code, lenient_pred, strict_pred) { |
michael@0 | 18 | return (strict_pred("'use strict'; " + code) && |
michael@0 | 19 | lenient_pred(code)); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | /* |
michael@0 | 23 | * completesNormally(CODE) returns true if evaluating CODE (as eval |
michael@0 | 24 | * code) completes normally (rather than throwing an exception). |
michael@0 | 25 | */ |
michael@0 | 26 | function completesNormally(code) { |
michael@0 | 27 | try { |
michael@0 | 28 | eval(code); |
michael@0 | 29 | return true; |
michael@0 | 30 | } catch (exception) { |
michael@0 | 31 | return false; |
michael@0 | 32 | } |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | /* |
michael@0 | 36 | * returns(VALUE)(CODE) returns true if evaluating CODE (as eval code) |
michael@0 | 37 | * completes normally (rather than throwing an exception), yielding a value |
michael@0 | 38 | * strictly equal to VALUE. |
michael@0 | 39 | */ |
michael@0 | 40 | function returns(value) { |
michael@0 | 41 | return function(code) { |
michael@0 | 42 | try { |
michael@0 | 43 | return eval(code) === value; |
michael@0 | 44 | } catch (exception) { |
michael@0 | 45 | return false; |
michael@0 | 46 | } |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | /* |
michael@0 | 51 | * returnsCopyOf(VALUE)(CODE) returns true if evaluating CODE (as eval code) |
michael@0 | 52 | * completes normally (rather than throwing an exception), yielding a value |
michael@0 | 53 | * that is deepEqual to VALUE. |
michael@0 | 54 | */ |
michael@0 | 55 | function returnsCopyOf(value) { |
michael@0 | 56 | return function(code) { |
michael@0 | 57 | try { |
michael@0 | 58 | return deepEqual(eval(code), value); |
michael@0 | 59 | } catch (exception) { |
michael@0 | 60 | return false; |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | /* |
michael@0 | 66 | * raisesException(EXCEPTION)(CODE) returns true if evaluating CODE (as |
michael@0 | 67 | * eval code) throws an exception object that is an instance of EXCEPTION, |
michael@0 | 68 | * and returns false if it throws any other error or evaluates |
michael@0 | 69 | * successfully. For example: raises(TypeError)("0()") == true. |
michael@0 | 70 | */ |
michael@0 | 71 | function raisesException(exception) { |
michael@0 | 72 | return function (code) { |
michael@0 | 73 | try { |
michael@0 | 74 | eval(code); |
michael@0 | 75 | return false; |
michael@0 | 76 | } catch (actual) { |
michael@0 | 77 | return actual instanceof exception; |
michael@0 | 78 | } |
michael@0 | 79 | }; |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | /* |
michael@0 | 83 | * parsesSuccessfully(CODE) returns true if CODE parses as function |
michael@0 | 84 | * code without an error. |
michael@0 | 85 | */ |
michael@0 | 86 | function parsesSuccessfully(code) { |
michael@0 | 87 | try { |
michael@0 | 88 | Function(code); |
michael@0 | 89 | return true; |
michael@0 | 90 | } catch (exception) { |
michael@0 | 91 | return false; |
michael@0 | 92 | } |
michael@0 | 93 | }; |
michael@0 | 94 | |
michael@0 | 95 | /* |
michael@0 | 96 | * parseRaisesException(EXCEPTION)(CODE) returns true if parsing CODE |
michael@0 | 97 | * as function code raises EXCEPTION. |
michael@0 | 98 | */ |
michael@0 | 99 | function parseRaisesException(exception) { |
michael@0 | 100 | return function (code) { |
michael@0 | 101 | try { |
michael@0 | 102 | Function(code); |
michael@0 | 103 | return false; |
michael@0 | 104 | } catch (actual) { |
michael@0 | 105 | return actual instanceof exception; |
michael@0 | 106 | } |
michael@0 | 107 | }; |
michael@0 | 108 | }; |
michael@0 | 109 | |
michael@0 | 110 | /* |
michael@0 | 111 | * Return the result of applying uneval to VAL, and replacing all runs |
michael@0 | 112 | * of whitespace with a single horizontal space (poor man's |
michael@0 | 113 | * tokenization). |
michael@0 | 114 | */ |
michael@0 | 115 | function clean_uneval(val) { |
michael@0 | 116 | return uneval(val).replace(/\s+/g, ' '); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | /* |
michael@0 | 120 | * Return true if A is equal to B, where equality on arrays and objects |
michael@0 | 121 | * means that they have the same set of enumerable properties, the values |
michael@0 | 122 | * of each property are deep_equal, and their 'length' properties are |
michael@0 | 123 | * equal. Equality on other types is ==. |
michael@0 | 124 | */ |
michael@0 | 125 | function deepEqual(a, b) { |
michael@0 | 126 | if (typeof a != typeof b) |
michael@0 | 127 | return false; |
michael@0 | 128 | |
michael@0 | 129 | if (typeof a == 'object') { |
michael@0 | 130 | var props = {}; |
michael@0 | 131 | // For every property of a, does b have that property with an equal value? |
michael@0 | 132 | for (var prop in a) { |
michael@0 | 133 | if (!deepEqual(a[prop], b[prop])) |
michael@0 | 134 | return false; |
michael@0 | 135 | props[prop] = true; |
michael@0 | 136 | } |
michael@0 | 137 | // Are all of b's properties present on a? |
michael@0 | 138 | for (var prop in b) |
michael@0 | 139 | if (!props[prop]) |
michael@0 | 140 | return false; |
michael@0 | 141 | // length isn't enumerable, but we want to check it, too. |
michael@0 | 142 | return a.length == b.length; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | if (a === b) { |
michael@0 | 146 | // Distinguish 0 from -0, even though they are ===. |
michael@0 | 147 | return a !== 0 || 1/a === 1/b; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | // Treat NaNs as equal, even though NaN !== NaN. |
michael@0 | 151 | // NaNs are the only non-reflexive values, i.e., if a !== a, then a is a NaN. |
michael@0 | 152 | // isNaN is broken: it converts its argument to number, so isNaN("foo") => true |
michael@0 | 153 | return a !== a && b !== b; |
michael@0 | 154 | } |