blob: 61deb9baa0cf39fc3ddc42fb2ea4c5f893debbe3 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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.
44const $Object = global.Object;
45const $Array = global.Array;
46const $String = global.String;
47const $Number = global.Number;
48const $Function = global.Function;
49const $Boolean = global.Boolean;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000050const $NaN = 0/0;
fschneider@chromium.org1805e212011-09-05 10:49:12 +000051const builtins = this;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000053// ECMA-262 Section 11.9.3.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054function EQUALS(y) {
ager@chromium.org7c537e22008-10-16 08:43:32 +000055 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056 var x = this;
57
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058 while (true) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059 if (IS_NUMBER(x)) {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000060 while (true) {
61 if (IS_NUMBER(y)) return %NumberEquals(x, y);
62 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
63 if (!IS_SPEC_OBJECT(y)) {
64 // String or boolean.
65 return %NumberEquals(x, %ToNumber(y));
66 }
67 y = %ToPrimitive(y, NO_HINT);
ager@chromium.org9258b6b2008-09-11 09:11:10 +000068 }
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000069 } else if (IS_STRING(x)) {
70 while (true) {
71 if (IS_STRING(y)) return %StringEquals(x, y);
72 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
73 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
74 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
75 y = %ToPrimitive(y, NO_HINT);
76 }
77 } else if (IS_BOOLEAN(x)) {
78 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
79 if (IS_NULL_OR_UNDEFINED(y)) return 1;
80 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
81 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
82 // y is object.
83 x = %ToNumber(x);
84 y = %ToPrimitive(y, NO_HINT);
85 } else if (IS_NULL_OR_UNDEFINED(x)) {
86 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087 } else {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000088 // x is an object.
ricow@chromium.org4980dff2010-07-19 08:33:45 +000089 if (IS_SPEC_OBJECT(y)) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +000090 return %_ObjectEquals(x, y) ? 0 : 1;
91 }
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000092 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
93 if (IS_BOOLEAN(y)) y = %ToNumber(y);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094 x = %ToPrimitive(x, NO_HINT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095 }
96 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000097}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099// ECMA-262, section 11.9.4, page 56.
100function STRICT_EQUALS(x) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101 if (IS_STRING(this)) {
102 if (!IS_STRING(x)) return 1; // not equal
103 return %StringEquals(this, x);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000104 }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000105
ager@chromium.org7c537e22008-10-16 08:43:32 +0000106 if (IS_NUMBER(this)) {
107 if (!IS_NUMBER(x)) return 1; // not equal
108 return %NumberEquals(this, x);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000109 }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000110
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000111 // If anything else gets here, we just do simple identity check.
112 // Objects (including functions), null, undefined and booleans were
113 // checked in the CompareStub, so there should be nothing left.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000114 return %_ObjectEquals(this, x) ? 0 : 1;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000115}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116
117
118// ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
119// the result when either (or both) the operands are NaN.
120function COMPARE(x, ncr) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000121 var left;
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000122 var right;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000123 // Fast cases for string, numbers and undefined compares.
124 if (IS_STRING(this)) {
125 if (IS_STRING(x)) return %_StringCompare(this, x);
126 if (IS_UNDEFINED(x)) return ncr;
127 left = this;
128 } else if (IS_NUMBER(this)) {
129 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
130 if (IS_UNDEFINED(x)) return ncr;
131 left = this;
132 } else if (IS_UNDEFINED(this)) {
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000133 if (!IS_UNDEFINED(x)) {
134 %ToPrimitive(x, NUMBER_HINT);
135 }
136 return ncr;
137 } else if (IS_UNDEFINED(x)) {
138 %ToPrimitive(this, NUMBER_HINT);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000139 return ncr;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000140 } else {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000141 left = %ToPrimitive(this, NUMBER_HINT);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000142 }
143
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000144 right = %ToPrimitive(x, NUMBER_HINT);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000145 if (IS_STRING(left) && IS_STRING(right)) {
146 return %_StringCompare(left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 } else {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000148 var left_number = %ToNumber(left);
149 var right_number = %ToNumber(right);
150 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
151 return %NumberCompare(left_number, right_number, ncr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000153}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154
155
156
157/* -----------------------------------
158 - - - A r i t h m e t i c - - -
159 -----------------------------------
160*/
161
162// ECMA-262, section 11.6.1, page 50.
163function ADD(x) {
164 // Fast case: Check for number operands and do the addition.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000165 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000166 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000167
ager@chromium.org7c537e22008-10-16 08:43:32 +0000168 // Default implementation.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 var a = %ToPrimitive(this, NO_HINT);
170 var b = %ToPrimitive(x, NO_HINT);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000171
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 if (IS_STRING(a)) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000173 return %_StringAdd(a, %ToString(b));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 } else if (IS_STRING(b)) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000175 return %_StringAdd(%NonStringToString(a), b);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176 } else {
177 return %NumberAdd(%ToNumber(a), %ToNumber(b));
178 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000179}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
181
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000182// Left operand (this) is already a string.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000183function STRING_ADD_LEFT(y) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000184 if (!IS_STRING(y)) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000185 if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000186 y = %_ValueOf(y);
187 } else {
188 y = IS_NUMBER(y)
ager@chromium.org5c838252010-02-19 08:53:10 +0000189 ? %_NumberToString(y)
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000190 : %ToString(%ToPrimitive(y, NO_HINT));
191 }
192 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000193 return %_StringAdd(this, y);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000194}
195
196
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000197// Right operand (y) is already a string.
198function STRING_ADD_RIGHT(y) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000199 var x = this;
200 if (!IS_STRING(x)) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000201 if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000202 x = %_ValueOf(x);
203 } else {
204 x = IS_NUMBER(x)
ager@chromium.org5c838252010-02-19 08:53:10 +0000205 ? %_NumberToString(x)
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000206 : %ToString(%ToPrimitive(x, NO_HINT));
207 }
208 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000209 return %_StringAdd(x, y);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000210}
211
212
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213// ECMA-262, section 11.6.2, page 50.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000214function SUB(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000215 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
216 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000217 return %NumberSub(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000218}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
220
221// ECMA-262, section 11.5.1, page 48.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000222function MUL(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000223 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
224 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000225 return %NumberMul(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000226}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227
228
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229// ECMA-262, section 11.5.2, page 49.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000230function DIV(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000231 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
232 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000233 return %NumberDiv(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000234}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235
236
237// ECMA-262, section 11.5.3, page 49.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000238function MOD(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000239 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
240 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000241 return %NumberMod(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000242}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243
244
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245
246/* -------------------------------------------
247 - - - B i t o p e r a t i o n s - - -
248 -------------------------------------------
249*/
250
251// ECMA-262, section 11.10, page 57.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000252function BIT_OR(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000253 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
254 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000255 return %NumberOr(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000256}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257
258
259// ECMA-262, section 11.10, page 57.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000260function BIT_AND(y) {
261 var x;
262 if (IS_NUMBER(this)) {
263 x = this;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000264 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000265 } else {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000266 x = %NonNumberToNumber(this);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000267 // Make sure to convert the right operand to a number before
268 // bailing out in the fast case, but after converting the
269 // left operand. This ensures that valueOf methods on the right
270 // operand are always executed.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000271 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000272 // Optimize for the case where we end up AND'ing a value
273 // that doesn't convert to a number. This is common in
274 // certain benchmarks.
275 if (NUMBER_IS_NAN(x)) return 0;
276 }
277 return %NumberAnd(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000278}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279
280
281// ECMA-262, section 11.10, page 57.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000282function BIT_XOR(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000283 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
284 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000285 return %NumberXor(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000286}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287
288
289// ECMA-262, section 11.4.7, page 47.
290function UNARY_MINUS() {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000291 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000292 return %NumberUnaryMinus(x);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000293}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294
295
296// ECMA-262, section 11.4.8, page 48.
297function BIT_NOT() {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000298 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000299 return %NumberNot(x);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000300}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301
302
303// ECMA-262, section 11.7.1, page 51.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000304function SHL(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000305 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
306 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000307 return %NumberShl(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000308}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309
310
311// ECMA-262, section 11.7.2, page 51.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000312function SAR(y) {
313 var x;
314 if (IS_NUMBER(this)) {
315 x = this;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000316 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000317 } else {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000318 x = %NonNumberToNumber(this);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000319 // Make sure to convert the right operand to a number before
320 // bailing out in the fast case, but after converting the
321 // left operand. This ensures that valueOf methods on the right
322 // operand are always executed.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000323 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000324 // Optimize for the case where we end up shifting a value
325 // that doesn't convert to a number. This is common in
326 // certain benchmarks.
327 if (NUMBER_IS_NAN(x)) return 0;
328 }
329 return %NumberSar(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000330}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331
332
333// ECMA-262, section 11.7.3, page 52.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000334function SHR(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000335 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
336 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000337 return %NumberShr(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000338}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000339
340
341
342/* -----------------------------
343 - - - H e l p e r s - - -
344 -----------------------------
345*/
346
347// ECMA-262, section 11.4.1, page 46.
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000348function DELETE(key, strict) {
349 return %DeleteProperty(%ToObject(this), %ToString(key), strict);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000350}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351
352
353// ECMA-262, section 11.8.7, page 54.
354function IN(x) {
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000355 if (!IS_SPEC_OBJECT(x)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356 throw %MakeTypeError('invalid_in_operator_use', [this, x]);
357 }
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000358 return %_IsNonNegativeSmi(this) && !%IsJSProxy(x) ?
359 %HasElement(x, this) : %HasProperty(x, %ToString(this));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000360}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361
362
ager@chromium.org7c537e22008-10-16 08:43:32 +0000363// ECMA-262, section 11.8.6, page 54. To make the implementation more
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000364// efficient, the return value should be zero if the 'this' is an
ager@chromium.org7c537e22008-10-16 08:43:32 +0000365// instance of F, and non-zero if not. This makes it possible to avoid
366// an expensive ToBoolean conversion in the generated code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367function INSTANCE_OF(F) {
368 var V = this;
369 if (!IS_FUNCTION(F)) {
370 throw %MakeTypeError('instanceof_function_expected', [V]);
371 }
372
373 // If V is not an object, return false.
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000374 if (!IS_SPEC_OBJECT(V)) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000375 return 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376 }
377
378 // Get the prototype of F; if it is not an object, throw an error.
379 var O = F.prototype;
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000380 if (!IS_SPEC_OBJECT(O)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 throw %MakeTypeError('instanceof_nonobject_proto', [O]);
382 }
383
384 // Return whether or not O is in the prototype chain of V.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000385 return %IsInPrototypeChain(O, V) ? 0 : 1;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000386}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000387
388
389// Get an array of property keys for the given object. Used in
390// for-in statements.
391function GET_KEYS() {
392 return %GetPropertyNames(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000393}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394
395
396// Filter a given key against an object by checking if the object
397// has a property with the given key; return the key as a string if
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000398// it has. Otherwise returns 0 (smi). Used in for-in statements.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399function FILTER_KEY(key) {
400 var string = %ToString(key);
401 if (%HasProperty(this, string)) return string;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000402 return 0;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000403}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404
405
406function CALL_NON_FUNCTION() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000407 var delegate = %GetFunctionDelegate(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408 if (!IS_FUNCTION(delegate)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000409 throw %MakeTypeError('called_non_callable', [typeof this]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000410 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000411 return delegate.apply(this, arguments);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000412}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413
414
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000415function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000416 var delegate = %GetConstructorDelegate(this);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000417 if (!IS_FUNCTION(delegate)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000418 throw %MakeTypeError('called_non_callable', [typeof this]);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000419 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000420 return delegate.apply(this, arguments);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000421}
422
423
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000424function APPLY_PREPARE(args) {
425 var length;
ager@chromium.org3e875802009-06-29 08:26:34 +0000426 // First check whether length is a positive Smi and args is an
427 // array. This is the fast case. If this fails, we do the slow case
428 // that takes care of more eventualities.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000429 if (IS_ARRAY(args)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430 length = args.length;
431 if (%_IsSmi(length) && length >= 0 && length < 0x800000 && IS_FUNCTION(this)) {
432 return length;
433 }
434 }
435
436 length = (args == null) ? 0 : %ToUint32(args.length);
437
438 // We can handle any number of apply arguments if the stack is
439 // big enough, but sanity check the value to avoid overflow when
440 // multiplying with pointer size.
441 if (length > 0x800000) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000442 throw %MakeRangeError('stack_overflow', []);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000443 }
444
445 if (!IS_FUNCTION(this)) {
446 throw %MakeTypeError('apply_non_function', [ %ToString(this), typeof this ]);
447 }
448
449 // Make sure the arguments list has the right type.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000450 if (args != null && !IS_ARRAY(args) && !IS_ARGUMENTS(args)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451 throw %MakeTypeError('apply_wrong_args', []);
452 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000453
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000454 // Return the length which is the number of arguments to copy to the
455 // stack. It is guaranteed to be a small integer at this point.
456 return length;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000457}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458
459
460function APPLY_OVERFLOW(length) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000461 throw %MakeRangeError('stack_overflow', []);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000462}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463
464
465// Convert the receiver to an object - forward to ToObject.
466function TO_OBJECT() {
467 return %ToObject(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000468}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469
470
471// Convert the receiver to a number - forward to ToNumber.
472function TO_NUMBER() {
473 return %ToNumber(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000474}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
476
477// Convert the receiver to a string - forward to ToString.
478function TO_STRING() {
479 return %ToString(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000480}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481
482
483/* -------------------------------------
484 - - - C o n v e r s i o n s - - -
485 -------------------------------------
486*/
487
488// ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
489// (1) for number hint, and (2) for string hint.
490function ToPrimitive(x, hint) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000491 // Fast case check.
492 if (IS_STRING(x)) return x;
493 // Normal behavior.
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000494 if (!IS_SPEC_OBJECT(x)) return x;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000495 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
496 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000497}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498
499
ager@chromium.org5c838252010-02-19 08:53:10 +0000500// ECMA-262, section 9.2, page 30
501function ToBoolean(x) {
502 if (IS_BOOLEAN(x)) return x;
503 if (IS_STRING(x)) return x.length != 0;
504 if (x == null) return false;
505 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
506 return true;
507}
508
509
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510// ECMA-262, section 9.3, page 31.
511function ToNumber(x) {
512 if (IS_NUMBER(x)) return x;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000513 if (IS_STRING(x)) {
514 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
515 : %StringToNumber(x);
516 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517 if (IS_BOOLEAN(x)) return x ? 1 : 0;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000518 if (IS_UNDEFINED(x)) return $NaN;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000520}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000522function NonNumberToNumber(x) {
523 if (IS_STRING(x)) {
524 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
525 : %StringToNumber(x);
526 }
527 if (IS_BOOLEAN(x)) return x ? 1 : 0;
528 if (IS_UNDEFINED(x)) return $NaN;
529 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
530}
531
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532
533// ECMA-262, section 9.8, page 35.
534function ToString(x) {
535 if (IS_STRING(x)) return x;
ager@chromium.org5c838252010-02-19 08:53:10 +0000536 if (IS_NUMBER(x)) return %_NumberToString(x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
538 if (IS_UNDEFINED(x)) return 'undefined';
539 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000540}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000542function NonStringToString(x) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000543 if (IS_NUMBER(x)) return %_NumberToString(x);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000544 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
545 if (IS_UNDEFINED(x)) return 'undefined';
546 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
547}
548
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550// ECMA-262, section 9.9, page 36.
551function ToObject(x) {
552 if (IS_STRING(x)) return new $String(x);
553 if (IS_NUMBER(x)) return new $Number(x);
554 if (IS_BOOLEAN(x)) return new $Boolean(x);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000555 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
556 throw %MakeTypeError('null_to_object', []);
557 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000558 return x;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000559}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560
561
562// ECMA-262, section 9.4, page 34.
563function ToInteger(x) {
564 if (%_IsSmi(x)) return x;
565 return %NumberToInteger(ToNumber(x));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000566}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567
568
569// ECMA-262, section 9.6, page 34.
570function ToUint32(x) {
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000571 if (%_IsSmi(x) && x >= 0) return x;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 return %NumberToJSUint32(ToNumber(x));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000573}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574
575
576// ECMA-262, section 9.5, page 34
577function ToInt32(x) {
578 if (%_IsSmi(x)) return x;
579 return %NumberToJSInt32(ToNumber(x));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000580}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581
582
ager@chromium.org5c838252010-02-19 08:53:10 +0000583// ES5, section 9.12
584function SameValue(x, y) {
585 if (typeof x != typeof y) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +0000586 if (IS_NUMBER(x)) {
587 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000588 // x is +0 and y is -0 or vice versa.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000589 if (x === 0 && y === 0 && (1 / x) != (1 / y)) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +0000590 }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000591 return x === y;
ager@chromium.org5c838252010-02-19 08:53:10 +0000592}
593
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594
595/* ---------------------------------
596 - - - U t i l i t i e s - - -
597 ---------------------------------
598*/
599
600// Returns if the given x is a primitive value - not an object or a
601// function.
602function IsPrimitive(x) {
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000603 // Even though the type of null is "object", null is still
604 // considered a primitive value. IS_SPEC_OBJECT handles this correctly
605 // (i.e., it will return false if x is null).
606 return !IS_SPEC_OBJECT(x);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000607}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608
609
610// ECMA-262, section 8.6.2.6, page 28.
611function DefaultNumber(x) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000612 var valueOf = x.valueOf;
613 if (IS_FUNCTION(valueOf)) {
614 var v = %_CallFunction(x, valueOf);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615 if (%IsPrimitive(v)) return v;
616 }
617
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000618 var toString = x.toString;
619 if (IS_FUNCTION(toString)) {
620 var s = %_CallFunction(x, toString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 if (%IsPrimitive(s)) return s;
622 }
623
624 throw %MakeTypeError('cannot_convert_to_primitive', []);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000625}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626
627
628// ECMA-262, section 8.6.2.6, page 28.
629function DefaultString(x) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000630 var toString = x.toString;
631 if (IS_FUNCTION(toString)) {
632 var s = %_CallFunction(x, toString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633 if (%IsPrimitive(s)) return s;
634 }
635
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000636 var valueOf = x.valueOf;
637 if (IS_FUNCTION(valueOf)) {
638 var v = %_CallFunction(x, valueOf);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 if (%IsPrimitive(v)) return v;
640 }
641
642 throw %MakeTypeError('cannot_convert_to_primitive', []);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000643}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644
645
646// NOTE: Setting the prototype for Array must take place as early as
647// possible due to code generation for array literals. When
648// generating code for a array literal a boilerplate array is created
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000649// that is cloned when running the code. It is essential that the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650// boilerplate gets the right prototype.
651%FunctionSetPrototype($Array, new $Array(0));