ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // This files contains runtime support implemented in JavaScript. |
| 29 | |
| 30 | // CAUTION: Some of the functions specified in this file are called |
| 31 | // directly from compiled code. These are the functions with names in |
| 32 | // ALL CAPS. The compiled code passes the first argument in 'this' and |
| 33 | // it does not push the function onto the stack. This means that you |
| 34 | // cannot use contexts in all these functions. |
| 35 | |
| 36 | |
| 37 | /* ----------------------------------- |
| 38 | - - - C o m p a r i s o n - - - |
| 39 | ----------------------------------- |
| 40 | */ |
| 41 | |
| 42 | // The following const declarations are shared with other native JS files. |
| 43 | // They are all declared at this one spot to avoid const redeclaration errors. |
| 44 | const $Object = global.Object; |
| 45 | const $Array = global.Array; |
| 46 | const $String = global.String; |
| 47 | const $Number = global.Number; |
| 48 | const $Function = global.Function; |
| 49 | const $Boolean = global.Boolean; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 50 | const $NaN = 0/0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 52 | // ECMA-262 Section 11.9.3. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | function EQUALS(y) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 54 | if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 55 | var x = this; |
| 56 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 57 | while (true) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 58 | if (IS_NUMBER(x)) { |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 59 | while (true) { |
| 60 | if (IS_NUMBER(y)) return %NumberEquals(x, y); |
| 61 | if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
| 62 | if (!IS_SPEC_OBJECT(y)) { |
| 63 | // String or boolean. |
| 64 | return %NumberEquals(x, %ToNumber(y)); |
| 65 | } |
| 66 | y = %ToPrimitive(y, NO_HINT); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 67 | } |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 68 | } else if (IS_STRING(x)) { |
| 69 | while (true) { |
| 70 | if (IS_STRING(y)) return %StringEquals(x, y); |
| 71 | if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); |
| 72 | if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); |
| 73 | if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
| 74 | y = %ToPrimitive(y, NO_HINT); |
| 75 | } |
| 76 | } else if (IS_BOOLEAN(x)) { |
| 77 | if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1; |
| 78 | if (IS_NULL_OR_UNDEFINED(y)) return 1; |
| 79 | if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y); |
| 80 | if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y)); |
| 81 | // y is object. |
| 82 | x = %ToNumber(x); |
| 83 | y = %ToPrimitive(y, NO_HINT); |
| 84 | } else if (IS_NULL_OR_UNDEFINED(x)) { |
| 85 | return IS_NULL_OR_UNDEFINED(y) ? 0 : 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 86 | } else { |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 87 | // x is an object. |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 88 | if (IS_SPEC_OBJECT(y)) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 89 | return %_ObjectEquals(x, y) ? 0 : 1; |
| 90 | } |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 91 | if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal |
| 92 | if (IS_BOOLEAN(y)) y = %ToNumber(y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | x = %ToPrimitive(x, NO_HINT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 96 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 97 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | // ECMA-262, section 11.9.4, page 56. |
| 99 | function STRICT_EQUALS(x) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | if (IS_STRING(this)) { |
| 101 | if (!IS_STRING(x)) return 1; // not equal |
| 102 | return %StringEquals(this, x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 103 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 104 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 105 | if (IS_NUMBER(this)) { |
| 106 | if (!IS_NUMBER(x)) return 1; // not equal |
| 107 | return %NumberEquals(this, x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 108 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 109 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 110 | // If anything else gets here, we just do simple identity check. |
| 111 | // Objects (including functions), null, undefined and booleans were |
| 112 | // checked in the CompareStub, so there should be nothing left. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 113 | return %_ObjectEquals(this, x) ? 0 : 1; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 114 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as |
| 118 | // the result when either (or both) the operands are NaN. |
| 119 | function COMPARE(x, ncr) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 120 | var left; |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 121 | var right; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 122 | // Fast cases for string, numbers and undefined compares. |
| 123 | if (IS_STRING(this)) { |
| 124 | if (IS_STRING(x)) return %_StringCompare(this, x); |
| 125 | if (IS_UNDEFINED(x)) return ncr; |
| 126 | left = this; |
| 127 | } else if (IS_NUMBER(this)) { |
| 128 | if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr); |
| 129 | if (IS_UNDEFINED(x)) return ncr; |
| 130 | left = this; |
| 131 | } else if (IS_UNDEFINED(this)) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 132 | if (!IS_UNDEFINED(x)) { |
| 133 | %ToPrimitive(x, NUMBER_HINT); |
| 134 | } |
| 135 | return ncr; |
| 136 | } else if (IS_UNDEFINED(x)) { |
| 137 | %ToPrimitive(this, NUMBER_HINT); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 138 | return ncr; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 139 | } else { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 140 | left = %ToPrimitive(this, NUMBER_HINT); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 141 | } |
| 142 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 143 | right = %ToPrimitive(x, NUMBER_HINT); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 144 | if (IS_STRING(left) && IS_STRING(right)) { |
| 145 | return %_StringCompare(left, right); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 146 | } else { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 147 | var left_number = %ToNumber(left); |
| 148 | var right_number = %ToNumber(right); |
| 149 | if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr; |
| 150 | return %NumberCompare(left_number, right_number, ncr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 152 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | |
| 154 | |
| 155 | |
| 156 | /* ----------------------------------- |
| 157 | - - - A r i t h m e t i c - - - |
| 158 | ----------------------------------- |
| 159 | */ |
| 160 | |
| 161 | // ECMA-262, section 11.6.1, page 50. |
| 162 | function ADD(x) { |
| 163 | // Fast case: Check for number operands and do the addition. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 164 | if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 165 | if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 166 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 167 | // Default implementation. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | var a = %ToPrimitive(this, NO_HINT); |
| 169 | var b = %ToPrimitive(x, NO_HINT); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 170 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 171 | if (IS_STRING(a)) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 172 | return %_StringAdd(a, %ToString(b)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 173 | } else if (IS_STRING(b)) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 174 | return %_StringAdd(%NonStringToString(a), b); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | } else { |
| 176 | return %NumberAdd(%ToNumber(a), %ToNumber(b)); |
| 177 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 178 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 179 | |
| 180 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 181 | // Left operand (this) is already a string. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 182 | function STRING_ADD_LEFT(y) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 183 | if (!IS_STRING(y)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 184 | if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 185 | y = %_ValueOf(y); |
| 186 | } else { |
| 187 | y = IS_NUMBER(y) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 188 | ? %_NumberToString(y) |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 189 | : %ToString(%ToPrimitive(y, NO_HINT)); |
| 190 | } |
| 191 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 192 | return %_StringAdd(this, y); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 196 | // Right operand (y) is already a string. |
| 197 | function STRING_ADD_RIGHT(y) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 198 | var x = this; |
| 199 | if (!IS_STRING(x)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 200 | if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 201 | x = %_ValueOf(x); |
| 202 | } else { |
| 203 | x = IS_NUMBER(x) |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 204 | ? %_NumberToString(x) |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 205 | : %ToString(%ToPrimitive(x, NO_HINT)); |
| 206 | } |
| 207 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 208 | return %_StringAdd(x, y); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 212 | // ECMA-262, section 11.6.2, page 50. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 213 | function SUB(y) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 214 | var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); |
| 215 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 216 | return %NumberSub(x, y); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 217 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 218 | |
| 219 | |
| 220 | // ECMA-262, section 11.5.1, page 48. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 221 | function MUL(y) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 222 | var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); |
| 223 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 224 | return %NumberMul(x, y); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 225 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | |
| 227 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | // ECMA-262, section 11.5.2, page 49. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 229 | function DIV(y) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 230 | var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); |
| 231 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 232 | return %NumberDiv(x, y); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 233 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 234 | |
| 235 | |
| 236 | // ECMA-262, section 11.5.3, page 49. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 237 | function MOD(y) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 238 | var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); |
| 239 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 240 | return %NumberMod(x, y); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 241 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | |
| 243 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | |
| 245 | /* ------------------------------------------- |
| 246 | - - - B i t o p e r a t i o n s - - - |
| 247 | ------------------------------------------- |
| 248 | */ |
| 249 | |
| 250 | // ECMA-262, section 11.10, page 57. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 251 | function BIT_OR(y) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 252 | var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); |
| 253 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 254 | return %NumberOr(x, y); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 255 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 256 | |
| 257 | |
| 258 | // ECMA-262, section 11.10, page 57. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 259 | function BIT_AND(y) { |
| 260 | var x; |
| 261 | if (IS_NUMBER(this)) { |
| 262 | x = this; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 263 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 264 | } else { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 265 | x = %NonNumberToNumber(this); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 266 | // Make sure to convert the right operand to a number before |
| 267 | // bailing out in the fast case, but after converting the |
| 268 | // left operand. This ensures that valueOf methods on the right |
| 269 | // operand are always executed. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 270 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 271 | // Optimize for the case where we end up AND'ing a value |
| 272 | // that doesn't convert to a number. This is common in |
| 273 | // certain benchmarks. |
| 274 | if (NUMBER_IS_NAN(x)) return 0; |
| 275 | } |
| 276 | return %NumberAnd(x, y); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 277 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | |
| 279 | |
| 280 | // ECMA-262, section 11.10, page 57. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 281 | function BIT_XOR(y) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 282 | var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); |
| 283 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 284 | return %NumberXor(x, y); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 285 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 286 | |
| 287 | |
| 288 | // ECMA-262, section 11.4.7, page 47. |
| 289 | function UNARY_MINUS() { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 290 | var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 291 | return %NumberUnaryMinus(x); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 292 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 293 | |
| 294 | |
| 295 | // ECMA-262, section 11.4.8, page 48. |
| 296 | function BIT_NOT() { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 297 | var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 298 | return %NumberNot(x); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 299 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | |
| 301 | |
| 302 | // ECMA-262, section 11.7.1, page 51. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 303 | function SHL(y) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 304 | var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); |
| 305 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 306 | return %NumberShl(x, y); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 307 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 308 | |
| 309 | |
| 310 | // ECMA-262, section 11.7.2, page 51. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 311 | function SAR(y) { |
| 312 | var x; |
| 313 | if (IS_NUMBER(this)) { |
| 314 | x = this; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 315 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 316 | } else { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 317 | x = %NonNumberToNumber(this); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 318 | // Make sure to convert the right operand to a number before |
| 319 | // bailing out in the fast case, but after converting the |
| 320 | // left operand. This ensures that valueOf methods on the right |
| 321 | // operand are always executed. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 322 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 323 | // Optimize for the case where we end up shifting a value |
| 324 | // that doesn't convert to a number. This is common in |
| 325 | // certain benchmarks. |
| 326 | if (NUMBER_IS_NAN(x)) return 0; |
| 327 | } |
| 328 | return %NumberSar(x, y); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 329 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 330 | |
| 331 | |
| 332 | // ECMA-262, section 11.7.3, page 52. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 333 | function SHR(y) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 334 | var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this); |
| 335 | if (!IS_NUMBER(y)) y = %NonNumberToNumber(y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 336 | return %NumberShr(x, y); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 337 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 338 | |
| 339 | |
| 340 | |
| 341 | /* ----------------------------- |
| 342 | - - - H e l p e r s - - - |
| 343 | ----------------------------- |
| 344 | */ |
| 345 | |
| 346 | // ECMA-262, section 11.4.1, page 46. |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 347 | function DELETE(key, strict) { |
| 348 | return %DeleteProperty(%ToObject(this), %ToString(key), strict); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 349 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 350 | |
| 351 | |
| 352 | // ECMA-262, section 11.8.7, page 54. |
| 353 | function IN(x) { |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 354 | if (!IS_SPEC_OBJECT(x)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 355 | throw %MakeTypeError('invalid_in_operator_use', [this, x]); |
| 356 | } |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 357 | return %_IsNonNegativeSmi(this) && !%IsJSProxy(x) ? |
| 358 | %HasElement(x, this) : %HasProperty(x, %ToString(this)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 359 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 360 | |
| 361 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 362 | // ECMA-262, section 11.8.6, page 54. To make the implementation more |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 363 | // efficient, the return value should be zero if the 'this' is an |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 364 | // instance of F, and non-zero if not. This makes it possible to avoid |
| 365 | // an expensive ToBoolean conversion in the generated code. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 366 | function INSTANCE_OF(F) { |
| 367 | var V = this; |
| 368 | if (!IS_FUNCTION(F)) { |
| 369 | throw %MakeTypeError('instanceof_function_expected', [V]); |
| 370 | } |
| 371 | |
| 372 | // If V is not an object, return false. |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 373 | if (!IS_SPEC_OBJECT(V)) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 374 | return 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // Get the prototype of F; if it is not an object, throw an error. |
| 378 | var O = F.prototype; |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 379 | if (!IS_SPEC_OBJECT(O)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 380 | throw %MakeTypeError('instanceof_nonobject_proto', [O]); |
| 381 | } |
| 382 | |
| 383 | // Return whether or not O is in the prototype chain of V. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 384 | return %IsInPrototypeChain(O, V) ? 0 : 1; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 385 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 386 | |
| 387 | |
| 388 | // Get an array of property keys for the given object. Used in |
| 389 | // for-in statements. |
| 390 | function GET_KEYS() { |
| 391 | return %GetPropertyNames(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 392 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | |
| 394 | |
| 395 | // Filter a given key against an object by checking if the object |
| 396 | // has a property with the given key; return the key as a string if |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 397 | // it has. Otherwise returns 0 (smi). Used in for-in statements. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 398 | function FILTER_KEY(key) { |
| 399 | var string = %ToString(key); |
| 400 | if (%HasProperty(this, string)) return string; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 401 | return 0; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 402 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 403 | |
| 404 | |
| 405 | function CALL_NON_FUNCTION() { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 406 | var delegate = %GetFunctionDelegate(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 407 | if (!IS_FUNCTION(delegate)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 408 | throw %MakeTypeError('called_non_callable', [typeof this]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 409 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 410 | return delegate.apply(this, arguments); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 411 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 412 | |
| 413 | |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 414 | function CALL_NON_FUNCTION_AS_CONSTRUCTOR() { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 415 | var delegate = %GetConstructorDelegate(this); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 416 | if (!IS_FUNCTION(delegate)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 417 | throw %MakeTypeError('called_non_callable', [typeof this]); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 418 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 419 | return delegate.apply(this, arguments); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 423 | function APPLY_PREPARE(args) { |
| 424 | var length; |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 425 | // First check whether length is a positive Smi and args is an |
| 426 | // array. This is the fast case. If this fails, we do the slow case |
| 427 | // that takes care of more eventualities. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 428 | if (IS_ARRAY(args)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 429 | length = args.length; |
| 430 | if (%_IsSmi(length) && length >= 0 && length < 0x800000 && IS_FUNCTION(this)) { |
| 431 | return length; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | length = (args == null) ? 0 : %ToUint32(args.length); |
| 436 | |
| 437 | // We can handle any number of apply arguments if the stack is |
| 438 | // big enough, but sanity check the value to avoid overflow when |
| 439 | // multiplying with pointer size. |
| 440 | if (length > 0x800000) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 441 | throw %MakeRangeError('stack_overflow', []); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | if (!IS_FUNCTION(this)) { |
| 445 | throw %MakeTypeError('apply_non_function', [ %ToString(this), typeof this ]); |
| 446 | } |
| 447 | |
| 448 | // Make sure the arguments list has the right type. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 449 | if (args != null && !IS_ARRAY(args) && !IS_ARGUMENTS(args)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 450 | throw %MakeTypeError('apply_wrong_args', []); |
| 451 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 452 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 453 | // Return the length which is the number of arguments to copy to the |
| 454 | // stack. It is guaranteed to be a small integer at this point. |
| 455 | return length; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 456 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 457 | |
| 458 | |
| 459 | function APPLY_OVERFLOW(length) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 460 | throw %MakeRangeError('stack_overflow', []); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 461 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 462 | |
| 463 | |
| 464 | // Convert the receiver to an object - forward to ToObject. |
| 465 | function TO_OBJECT() { |
| 466 | return %ToObject(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 467 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 468 | |
| 469 | |
| 470 | // Convert the receiver to a number - forward to ToNumber. |
| 471 | function TO_NUMBER() { |
| 472 | return %ToNumber(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 473 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 474 | |
| 475 | |
| 476 | // Convert the receiver to a string - forward to ToString. |
| 477 | function TO_STRING() { |
| 478 | return %ToString(this); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 479 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 480 | |
| 481 | |
| 482 | /* ------------------------------------- |
| 483 | - - - C o n v e r s i o n s - - - |
| 484 | ------------------------------------- |
| 485 | */ |
| 486 | |
| 487 | // ECMA-262, section 9.1, page 30. Use null/undefined for no hint, |
| 488 | // (1) for number hint, and (2) for string hint. |
| 489 | function ToPrimitive(x, hint) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 490 | // Fast case check. |
| 491 | if (IS_STRING(x)) return x; |
| 492 | // Normal behavior. |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 493 | if (!IS_SPEC_OBJECT(x)) return x; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 494 | if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT; |
| 495 | return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 496 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 497 | |
| 498 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 499 | // ECMA-262, section 9.2, page 30 |
| 500 | function ToBoolean(x) { |
| 501 | if (IS_BOOLEAN(x)) return x; |
| 502 | if (IS_STRING(x)) return x.length != 0; |
| 503 | if (x == null) return false; |
| 504 | if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x)); |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 509 | // ECMA-262, section 9.3, page 31. |
| 510 | function ToNumber(x) { |
| 511 | if (IS_NUMBER(x)) return x; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 512 | if (IS_STRING(x)) { |
| 513 | return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) |
| 514 | : %StringToNumber(x); |
| 515 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 516 | if (IS_BOOLEAN(x)) return x ? 1 : 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 517 | if (IS_UNDEFINED(x)) return $NaN; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 519 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 520 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 521 | function NonNumberToNumber(x) { |
| 522 | if (IS_STRING(x)) { |
| 523 | return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x) |
| 524 | : %StringToNumber(x); |
| 525 | } |
| 526 | if (IS_BOOLEAN(x)) return x ? 1 : 0; |
| 527 | if (IS_UNDEFINED(x)) return $NaN; |
| 528 | return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x)); |
| 529 | } |
| 530 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 531 | |
| 532 | // ECMA-262, section 9.8, page 35. |
| 533 | function ToString(x) { |
| 534 | if (IS_STRING(x)) return x; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 535 | if (IS_NUMBER(x)) return %_NumberToString(x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 536 | if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
| 537 | if (IS_UNDEFINED(x)) return 'undefined'; |
| 538 | return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 539 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 540 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 541 | function NonStringToString(x) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 542 | if (IS_NUMBER(x)) return %_NumberToString(x); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 543 | if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
| 544 | if (IS_UNDEFINED(x)) return 'undefined'; |
| 545 | return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x)); |
| 546 | } |
| 547 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 548 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 549 | // ECMA-262, section 9.9, page 36. |
| 550 | function ToObject(x) { |
| 551 | if (IS_STRING(x)) return new $String(x); |
| 552 | if (IS_NUMBER(x)) return new $Number(x); |
| 553 | if (IS_BOOLEAN(x)) return new $Boolean(x); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 554 | if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) { |
| 555 | throw %MakeTypeError('null_to_object', []); |
| 556 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 557 | return x; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 558 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | |
| 560 | |
| 561 | // ECMA-262, section 9.4, page 34. |
| 562 | function ToInteger(x) { |
| 563 | if (%_IsSmi(x)) return x; |
| 564 | return %NumberToInteger(ToNumber(x)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 565 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 566 | |
| 567 | |
| 568 | // ECMA-262, section 9.6, page 34. |
| 569 | function ToUint32(x) { |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 570 | if (%_IsSmi(x) && x >= 0) return x; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | return %NumberToJSUint32(ToNumber(x)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 572 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 573 | |
| 574 | |
| 575 | // ECMA-262, section 9.5, page 34 |
| 576 | function ToInt32(x) { |
| 577 | if (%_IsSmi(x)) return x; |
| 578 | return %NumberToJSInt32(ToNumber(x)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 579 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 580 | |
| 581 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 582 | // ES5, section 9.12 |
| 583 | function SameValue(x, y) { |
| 584 | if (typeof x != typeof y) return false; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 585 | if (IS_NUMBER(x)) { |
| 586 | if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 587 | // x is +0 and y is -0 or vice versa. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 588 | if (x === 0 && y === 0 && (1 / x) != (1 / y)) return false; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 589 | } |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 590 | return x === y; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 591 | } |
| 592 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 593 | |
| 594 | /* --------------------------------- |
| 595 | - - - U t i l i t i e s - - - |
| 596 | --------------------------------- |
| 597 | */ |
| 598 | |
| 599 | // Returns if the given x is a primitive value - not an object or a |
| 600 | // function. |
| 601 | function IsPrimitive(x) { |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 602 | // Even though the type of null is "object", null is still |
| 603 | // considered a primitive value. IS_SPEC_OBJECT handles this correctly |
| 604 | // (i.e., it will return false if x is null). |
| 605 | return !IS_SPEC_OBJECT(x); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 606 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 607 | |
| 608 | |
| 609 | // ECMA-262, section 8.6.2.6, page 28. |
| 610 | function DefaultNumber(x) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 611 | var valueOf = x.valueOf; |
| 612 | if (IS_FUNCTION(valueOf)) { |
| 613 | var v = %_CallFunction(x, valueOf); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 614 | if (%IsPrimitive(v)) return v; |
| 615 | } |
| 616 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 617 | var toString = x.toString; |
| 618 | if (IS_FUNCTION(toString)) { |
| 619 | var s = %_CallFunction(x, toString); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | if (%IsPrimitive(s)) return s; |
| 621 | } |
| 622 | |
| 623 | throw %MakeTypeError('cannot_convert_to_primitive', []); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 624 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | |
| 626 | |
| 627 | // ECMA-262, section 8.6.2.6, page 28. |
| 628 | function DefaultString(x) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 629 | var toString = x.toString; |
| 630 | if (IS_FUNCTION(toString)) { |
| 631 | var s = %_CallFunction(x, toString); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 632 | if (%IsPrimitive(s)) return s; |
| 633 | } |
| 634 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 635 | var valueOf = x.valueOf; |
| 636 | if (IS_FUNCTION(valueOf)) { |
| 637 | var v = %_CallFunction(x, valueOf); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 638 | if (%IsPrimitive(v)) return v; |
| 639 | } |
| 640 | |
| 641 | throw %MakeTypeError('cannot_convert_to_primitive', []); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 642 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 643 | |
| 644 | |
| 645 | // NOTE: Setting the prototype for Array must take place as early as |
| 646 | // possible due to code generation for array literals. When |
| 647 | // generating code for a array literal a boilerplate array is created |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 648 | // that is cloned when running the code. It is essential that the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 649 | // boilerplate gets the right prototype. |
| 650 | %FunctionSetPrototype($Array, new $Array(0)); |