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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | File Name: 11.10-1.js |
michael@0 | 9 | ECMA Section: 11.10-1 Binary Bitwise Operators: & |
michael@0 | 10 | Description: |
michael@0 | 11 | Semantics |
michael@0 | 12 | |
michael@0 | 13 | The production A : A @ B, where @ is one of the bitwise operators in the |
michael@0 | 14 | productions &, ^, | , is evaluated as follows: |
michael@0 | 15 | |
michael@0 | 16 | 1. Evaluate A. |
michael@0 | 17 | 2. Call GetValue(Result(1)). |
michael@0 | 18 | 3. Evaluate B. |
michael@0 | 19 | 4. Call GetValue(Result(3)). |
michael@0 | 20 | 5. Call ToInt32(Result(2)). |
michael@0 | 21 | 6. Call ToInt32(Result(4)). |
michael@0 | 22 | 7. Apply the bitwise operator @ to Result(5) and Result(6). The result is |
michael@0 | 23 | a signed 32 bit integer. |
michael@0 | 24 | 8. Return Result(7). |
michael@0 | 25 | |
michael@0 | 26 | Author: christine@netscape.com |
michael@0 | 27 | Date: 12 november 1997 |
michael@0 | 28 | */ |
michael@0 | 29 | var SECTION = "11.10-1"; |
michael@0 | 30 | var VERSION = "ECMA_1"; |
michael@0 | 31 | startTest(); |
michael@0 | 32 | |
michael@0 | 33 | writeHeaderToLog( SECTION + " Binary Bitwise Operators: &"); |
michael@0 | 34 | |
michael@0 | 35 | var shiftexp = 0; |
michael@0 | 36 | var addexp = 0; |
michael@0 | 37 | |
michael@0 | 38 | // for ( shiftpow = 0; shiftpow < 33; shiftpow++ ) { |
michael@0 | 39 | for ( shiftpow = 0; shiftpow < 1; shiftpow++ ) { |
michael@0 | 40 | shiftexp += Math.pow( 2, shiftpow ); |
michael@0 | 41 | |
michael@0 | 42 | for ( addpow = 0; addpow < 33; addpow++ ) { |
michael@0 | 43 | addexp += Math.pow(2, addpow); |
michael@0 | 44 | |
michael@0 | 45 | new TestCase( SECTION, |
michael@0 | 46 | shiftexp + " & " + addexp, |
michael@0 | 47 | And( shiftexp, addexp ), |
michael@0 | 48 | shiftexp & addexp ); |
michael@0 | 49 | } |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | test(); |
michael@0 | 53 | |
michael@0 | 54 | function ToInteger( n ) { |
michael@0 | 55 | n = Number( n ); |
michael@0 | 56 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 57 | |
michael@0 | 58 | if ( n != n ) { |
michael@0 | 59 | return 0; |
michael@0 | 60 | } |
michael@0 | 61 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { |
michael@0 | 62 | return n; |
michael@0 | 63 | } |
michael@0 | 64 | return ( sign * Math.floor(Math.abs(n)) ); |
michael@0 | 65 | } |
michael@0 | 66 | function ToInt32( n ) { |
michael@0 | 67 | n = Number( n ); |
michael@0 | 68 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 69 | |
michael@0 | 70 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 71 | return 0; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); |
michael@0 | 75 | n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; |
michael@0 | 76 | |
michael@0 | 77 | return ( n ); |
michael@0 | 78 | } |
michael@0 | 79 | function ToUint32( n ) { |
michael@0 | 80 | n = Number( n ); |
michael@0 | 81 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 82 | |
michael@0 | 83 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 84 | return 0; |
michael@0 | 85 | } |
michael@0 | 86 | n = sign * Math.floor( Math.abs(n) ) |
michael@0 | 87 | |
michael@0 | 88 | n = n % Math.pow(2,32); |
michael@0 | 89 | |
michael@0 | 90 | if ( n < 0 ){ |
michael@0 | 91 | n += Math.pow(2,32); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | return ( n ); |
michael@0 | 95 | } |
michael@0 | 96 | function ToUint16( n ) { |
michael@0 | 97 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 98 | |
michael@0 | 99 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 100 | return 0; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); |
michael@0 | 104 | |
michael@0 | 105 | if (n <0) { |
michael@0 | 106 | n += Math.pow(2,16); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | return ( n ); |
michael@0 | 110 | } |
michael@0 | 111 | function Mask( b, n ) { |
michael@0 | 112 | b = ToUint32BitString( b ); |
michael@0 | 113 | b = b.substring( b.length - n ); |
michael@0 | 114 | b = ToUint32Decimal( b ); |
michael@0 | 115 | return ( b ); |
michael@0 | 116 | } |
michael@0 | 117 | function ToUint32BitString( n ) { |
michael@0 | 118 | var b = ""; |
michael@0 | 119 | for ( p = 31; p >=0; p-- ) { |
michael@0 | 120 | if ( n >= Math.pow(2,p) ) { |
michael@0 | 121 | b += "1"; |
michael@0 | 122 | n -= Math.pow(2,p); |
michael@0 | 123 | } else { |
michael@0 | 124 | b += "0"; |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | return b; |
michael@0 | 128 | } |
michael@0 | 129 | function ToInt32BitString( n ) { |
michael@0 | 130 | var b = ""; |
michael@0 | 131 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 132 | |
michael@0 | 133 | b += ( sign == 1 ) ? "0" : "1"; |
michael@0 | 134 | |
michael@0 | 135 | for ( p = 30; p >=0; p-- ) { |
michael@0 | 136 | if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { |
michael@0 | 137 | b += ( sign == 1 ) ? "1" : "0"; |
michael@0 | 138 | n -= sign * Math.pow( 2, p ); |
michael@0 | 139 | } else { |
michael@0 | 140 | b += ( sign == 1 ) ? "0" : "1"; |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | return b; |
michael@0 | 145 | } |
michael@0 | 146 | function ToInt32Decimal( bin ) { |
michael@0 | 147 | var r = 0; |
michael@0 | 148 | var sign; |
michael@0 | 149 | |
michael@0 | 150 | if ( Number(bin.charAt(0)) == 0 ) { |
michael@0 | 151 | sign = 1; |
michael@0 | 152 | r = 0; |
michael@0 | 153 | } else { |
michael@0 | 154 | sign = -1; |
michael@0 | 155 | r = -(Math.pow(2,31)); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | for ( var j = 0; j < 31; j++ ) { |
michael@0 | 159 | r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | return r; |
michael@0 | 163 | } |
michael@0 | 164 | function ToUint32Decimal( bin ) { |
michael@0 | 165 | var r = 0; |
michael@0 | 166 | |
michael@0 | 167 | |
michael@0 | 168 | for ( l = bin.length; l < 32; l++ ) { |
michael@0 | 169 | bin = "0" + bin; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | for ( j = 0; j < 31; j++ ) { |
michael@0 | 173 | r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
michael@0 | 174 | |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | return r; |
michael@0 | 178 | } |
michael@0 | 179 | function And( s, a ) { |
michael@0 | 180 | s = ToInt32( s ); |
michael@0 | 181 | a = ToInt32( a ); |
michael@0 | 182 | |
michael@0 | 183 | var bs = ToInt32BitString( s ); |
michael@0 | 184 | var ba = ToInt32BitString( a ); |
michael@0 | 185 | |
michael@0 | 186 | var result = ""; |
michael@0 | 187 | |
michael@0 | 188 | for ( var bit = 0; bit < bs.length; bit++ ) { |
michael@0 | 189 | if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) { |
michael@0 | 190 | result += "1"; |
michael@0 | 191 | } else { |
michael@0 | 192 | result += "0"; |
michael@0 | 193 | } |
michael@0 | 194 | } |
michael@0 | 195 | return ToInt32Decimal(result); |
michael@0 | 196 | } |
michael@0 | 197 | function Xor( s, a ) { |
michael@0 | 198 | s = ToInt32( s ); |
michael@0 | 199 | a = ToInt32( a ); |
michael@0 | 200 | |
michael@0 | 201 | var bs = ToInt32BitString( s ); |
michael@0 | 202 | var ba = ToInt32BitString( a ); |
michael@0 | 203 | |
michael@0 | 204 | var result = ""; |
michael@0 | 205 | |
michael@0 | 206 | for ( var bit = 0; bit < bs.length; bit++ ) { |
michael@0 | 207 | if ( (bs.charAt(bit) == "1" && ba.charAt(bit) == "0") || |
michael@0 | 208 | (bs.charAt(bit) == "0" && ba.charAt(bit) == "1") |
michael@0 | 209 | ) { |
michael@0 | 210 | result += "1"; |
michael@0 | 211 | } else { |
michael@0 | 212 | result += "0"; |
michael@0 | 213 | } |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | return ToInt32Decimal(result); |
michael@0 | 217 | } |
michael@0 | 218 | function Or( s, a ) { |
michael@0 | 219 | s = ToInt32( s ); |
michael@0 | 220 | a = ToInt32( a ); |
michael@0 | 221 | |
michael@0 | 222 | var bs = ToInt32BitString( s ); |
michael@0 | 223 | var ba = ToInt32BitString( a ); |
michael@0 | 224 | |
michael@0 | 225 | var result = ""; |
michael@0 | 226 | |
michael@0 | 227 | for ( var bit = 0; bit < bs.length; bit++ ) { |
michael@0 | 228 | if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) { |
michael@0 | 229 | result += "1"; |
michael@0 | 230 | } else { |
michael@0 | 231 | result += "0"; |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | return ToInt32Decimal(result); |
michael@0 | 236 | } |