blob: 19b858b27ed90cce80cb5aac06b2a1ed8482bf87 [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
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +000042// The following declarations are shared with other native JS files.
43// They are all declared at this one spot to avoid redeclaration errors.
44var $Object = global.Object;
45var $Array = global.Array;
46var $String = global.String;
47var $Number = global.Number;
48var $Function = global.Function;
49var $Boolean = global.Boolean;
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +000050var $NaN = %GetRootNaN();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +000051var 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);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +000072 if (IS_SYMBOL(y)) return 1; // not equal
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000073 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
74 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
75 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
76 y = %ToPrimitive(y, NO_HINT);
77 }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +000078 } else if (IS_SYMBOL(x)) {
79 while (true) {
80 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1;
81 if (!IS_SPEC_OBJECT(y)) return 1; // not equal
82 y = %ToPrimitive(y, NO_HINT);
83 }
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000084 } else if (IS_BOOLEAN(x)) {
85 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
86 if (IS_NULL_OR_UNDEFINED(y)) return 1;
87 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
88 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +000089 if (IS_SYMBOL(y)) return 1; // not equal
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000090 // y is object.
91 x = %ToNumber(x);
92 y = %ToPrimitive(y, NO_HINT);
93 } else if (IS_NULL_OR_UNDEFINED(x)) {
94 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095 } else {
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000096 // x is an object.
ricow@chromium.org4980dff2010-07-19 08:33:45 +000097 if (IS_SPEC_OBJECT(y)) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +000098 return %_ObjectEquals(x, y) ? 0 : 1;
99 }
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000100 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
101 if (IS_BOOLEAN(y)) y = %ToNumber(y);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 x = %ToPrimitive(x, NO_HINT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 }
104 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000105}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107// ECMA-262, section 11.9.4, page 56.
108function STRICT_EQUALS(x) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109 if (IS_STRING(this)) {
110 if (!IS_STRING(x)) return 1; // not equal
111 return %StringEquals(this, x);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000112 }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000113
ager@chromium.org7c537e22008-10-16 08:43:32 +0000114 if (IS_NUMBER(this)) {
115 if (!IS_NUMBER(x)) return 1; // not equal
116 return %NumberEquals(this, x);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000117 }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000118
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000119 // If anything else gets here, we just do simple identity check.
120 // Objects (including functions), null, undefined and booleans were
121 // checked in the CompareStub, so there should be nothing left.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000122 return %_ObjectEquals(this, x) ? 0 : 1;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000123}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124
125
126// ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
127// the result when either (or both) the operands are NaN.
128function COMPARE(x, ncr) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000129 var left;
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000130 var right;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000131 // Fast cases for string, numbers and undefined compares.
132 if (IS_STRING(this)) {
133 if (IS_STRING(x)) return %_StringCompare(this, x);
134 if (IS_UNDEFINED(x)) return ncr;
135 left = this;
136 } else if (IS_NUMBER(this)) {
137 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
138 if (IS_UNDEFINED(x)) return ncr;
139 left = this;
140 } else if (IS_UNDEFINED(this)) {
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000141 if (!IS_UNDEFINED(x)) {
142 %ToPrimitive(x, NUMBER_HINT);
143 }
144 return ncr;
145 } else if (IS_UNDEFINED(x)) {
146 %ToPrimitive(this, NUMBER_HINT);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000147 return ncr;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000148 } else {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000149 left = %ToPrimitive(this, NUMBER_HINT);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000150 }
151
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000152 right = %ToPrimitive(x, NUMBER_HINT);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000153 if (IS_STRING(left) && IS_STRING(right)) {
154 return %_StringCompare(left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155 } else {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000156 var left_number = %ToNumber(left);
157 var right_number = %ToNumber(right);
158 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
159 return %NumberCompare(left_number, right_number, ncr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000161}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162
163
164
165/* -----------------------------------
166 - - - A r i t h m e t i c - - -
167 -----------------------------------
168*/
169
170// ECMA-262, section 11.6.1, page 50.
171function ADD(x) {
172 // Fast case: Check for number operands and do the addition.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000173 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000174 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000175
ager@chromium.org7c537e22008-10-16 08:43:32 +0000176 // Default implementation.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177 var a = %ToPrimitive(this, NO_HINT);
178 var b = %ToPrimitive(x, NO_HINT);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000179
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180 if (IS_STRING(a)) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000181 return %_StringAdd(a, %ToString(b));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 } else if (IS_STRING(b)) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000183 return %_StringAdd(%NonStringToString(a), b);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 } else {
185 return %NumberAdd(%ToNumber(a), %ToNumber(b));
186 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000187}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188
189
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000190// Left operand (this) is already a string.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000191function STRING_ADD_LEFT(y) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000192 if (!IS_STRING(y)) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000193 if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000194 y = %_ValueOf(y);
195 } else {
196 y = IS_NUMBER(y)
ager@chromium.org5c838252010-02-19 08:53:10 +0000197 ? %_NumberToString(y)
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000198 : %ToString(%ToPrimitive(y, NO_HINT));
199 }
200 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000201 return %_StringAdd(this, y);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000202}
203
204
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000205// Right operand (y) is already a string.
206function STRING_ADD_RIGHT(y) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000207 var x = this;
208 if (!IS_STRING(x)) {
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000209 if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000210 x = %_ValueOf(x);
211 } else {
212 x = IS_NUMBER(x)
ager@chromium.org5c838252010-02-19 08:53:10 +0000213 ? %_NumberToString(x)
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000214 : %ToString(%ToPrimitive(x, NO_HINT));
215 }
216 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000217 return %_StringAdd(x, y);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000218}
219
220
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221// ECMA-262, section 11.6.2, page 50.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000222function SUB(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 %NumberSub(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000226}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227
228
229// ECMA-262, section 11.5.1, page 48.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000230function MUL(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 %NumberMul(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000234}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235
236
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237// ECMA-262, section 11.5.2, page 49.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000238function DIV(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 %NumberDiv(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000242}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243
244
245// ECMA-262, section 11.5.3, page 49.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000246function MOD(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000247 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
248 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000249 return %NumberMod(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000250}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251
252
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253
254/* -------------------------------------------
255 - - - B i t o p e r a t i o n s - - -
256 -------------------------------------------
257*/
258
259// ECMA-262, section 11.10, page 57.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000260function BIT_OR(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000261 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
262 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000263 return %NumberOr(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000264}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265
266
267// ECMA-262, section 11.10, page 57.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000268function BIT_AND(y) {
269 var x;
270 if (IS_NUMBER(this)) {
271 x = this;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000272 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000273 } else {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000274 x = %NonNumberToNumber(this);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000275 // Make sure to convert the right operand to a number before
276 // bailing out in the fast case, but after converting the
277 // left operand. This ensures that valueOf methods on the right
278 // operand are always executed.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000279 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000280 // Optimize for the case where we end up AND'ing a value
281 // that doesn't convert to a number. This is common in
282 // certain benchmarks.
283 if (NUMBER_IS_NAN(x)) return 0;
284 }
285 return %NumberAnd(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.10, page 57.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000290function BIT_XOR(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000291 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
292 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000293 return %NumberXor(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000294}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295
296
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297// ECMA-262, section 11.7.1, page 51.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000298function SHL(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000299 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
300 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000301 return %NumberShl(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000302}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303
304
305// ECMA-262, section 11.7.2, page 51.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000306function SAR(y) {
307 var x;
308 if (IS_NUMBER(this)) {
309 x = this;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000310 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000311 } else {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000312 x = %NonNumberToNumber(this);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000313 // Make sure to convert the right operand to a number before
314 // bailing out in the fast case, but after converting the
315 // left operand. This ensures that valueOf methods on the right
316 // operand are always executed.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000317 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000318 // Optimize for the case where we end up shifting a value
319 // that doesn't convert to a number. This is common in
320 // certain benchmarks.
321 if (NUMBER_IS_NAN(x)) return 0;
322 }
323 return %NumberSar(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000324}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325
326
327// ECMA-262, section 11.7.3, page 52.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000328function SHR(y) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000329 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
330 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000331 return %NumberShr(x, y);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000332}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333
334
335
336/* -----------------------------
337 - - - H e l p e r s - - -
338 -----------------------------
339*/
340
341// ECMA-262, section 11.4.1, page 46.
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000342function DELETE(key, strict) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000343 return %DeleteProperty(%ToObject(this), %ToName(key), strict);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000344}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345
346
347// ECMA-262, section 11.8.7, page 54.
348function IN(x) {
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000349 if (!IS_SPEC_OBJECT(x)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350 throw %MakeTypeError('invalid_in_operator_use', [this, x]);
351 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000352 return %_IsNonNegativeSmi(this) ?
ulan@chromium.org750145a2013-03-07 15:14:13 +0000353 %HasElement(x, this) : %HasProperty(x, %ToName(this));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000354}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355
356
ager@chromium.org7c537e22008-10-16 08:43:32 +0000357// ECMA-262, section 11.8.6, page 54. To make the implementation more
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000358// efficient, the return value should be zero if the 'this' is an
ager@chromium.org7c537e22008-10-16 08:43:32 +0000359// instance of F, and non-zero if not. This makes it possible to avoid
360// an expensive ToBoolean conversion in the generated code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361function INSTANCE_OF(F) {
362 var V = this;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000363 if (!IS_SPEC_FUNCTION(F)) {
machenbach@chromium.org935a7792013-11-12 09:05:18 +0000364 throw %MakeTypeError('instanceof_function_expected', [F]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365 }
366
367 // If V is not an object, return false.
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000368 if (!IS_SPEC_OBJECT(V)) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000369 return 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370 }
371
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000372 // Check if function is bound, if so, get [[BoundFunction]] from it
373 // and use that instead of F.
374 var bindings = %BoundFunctionGetBindings(F);
375 if (bindings) {
376 F = bindings[kBoundFunctionIndex]; // Always a non-bound function.
377 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378 // 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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000389// Filter a given key against an object by checking if the object
390// has a property with the given key; return the key as a string if
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000391// it has. Otherwise returns 0 (smi). Used in for-in statements.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000392function FILTER_KEY(key) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000393 var string = %ToName(key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394 if (%HasProperty(this, string)) return string;
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000395 return 0;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000396}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000397
398
399function CALL_NON_FUNCTION() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000400 var delegate = %GetFunctionDelegate(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 if (!IS_FUNCTION(delegate)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000402 throw %MakeTypeError('called_non_callable', [typeof this]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000403 }
lrn@chromium.org34e60782011-09-15 07:25:40 +0000404 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000405}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406
407
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000408function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
ager@chromium.org5c838252010-02-19 08:53:10 +0000409 var delegate = %GetConstructorDelegate(this);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000410 if (!IS_FUNCTION(delegate)) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000411 throw %MakeTypeError('called_non_callable', [typeof this]);
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000412 }
lrn@chromium.org34e60782011-09-15 07:25:40 +0000413 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
414}
415
416
417function CALL_FUNCTION_PROXY() {
418 var arity = %_ArgumentsLength() - 1;
419 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg.
420 var trap = %GetCallTrap(proxy);
421 return %Apply(trap, this, arguments, 0, arity);
422}
423
424
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000425function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
426 var proxy = this;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000427 var trap = %GetConstructTrap(proxy);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000428 return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000429}
430
431
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432function APPLY_PREPARE(args) {
433 var length;
ager@chromium.org3e875802009-06-29 08:26:34 +0000434 // First check whether length is a positive Smi and args is an
435 // array. This is the fast case. If this fails, we do the slow case
436 // that takes care of more eventualities.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000437 if (IS_ARRAY(args)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438 length = args.length;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000439 if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
440 IS_SPEC_FUNCTION(this)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441 return length;
442 }
443 }
444
445 length = (args == null) ? 0 : %ToUint32(args.length);
446
447 // We can handle any number of apply arguments if the stack is
448 // big enough, but sanity check the value to avoid overflow when
449 // multiplying with pointer size.
450 if (length > 0x800000) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000451 throw %MakeRangeError('stack_overflow', []);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452 }
453
lrn@chromium.org34e60782011-09-15 07:25:40 +0000454 if (!IS_SPEC_FUNCTION(this)) {
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000455 throw %MakeTypeError('apply_non_function',
456 [ %ToString(this), typeof this ]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 }
458
459 // Make sure the arguments list has the right type.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000460 if (args != null && !IS_SPEC_OBJECT(args)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 throw %MakeTypeError('apply_wrong_args', []);
462 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000463
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 // Return the length which is the number of arguments to copy to the
465 // stack. It is guaranteed to be a small integer at this point.
466 return length;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000467}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468
469
470function APPLY_OVERFLOW(length) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000471 throw %MakeRangeError('stack_overflow', []);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000472}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473
474
475// Convert the receiver to an object - forward to ToObject.
476function TO_OBJECT() {
477 return %ToObject(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000478}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479
480
481// Convert the receiver to a number - forward to ToNumber.
482function TO_NUMBER() {
483 return %ToNumber(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000484}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485
486
487// Convert the receiver to a string - forward to ToString.
488function TO_STRING() {
489 return %ToString(this);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000490}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491
492
493/* -------------------------------------
494 - - - C o n v e r s i o n s - - -
495 -------------------------------------
496*/
497
498// ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
499// (1) for number hint, and (2) for string hint.
500function ToPrimitive(x, hint) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000501 // Fast case check.
502 if (IS_STRING(x)) return x;
503 // Normal behavior.
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000504 if (!IS_SPEC_OBJECT(x)) return x;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000505 if (IS_SYMBOL_WRAPPER(x)) return %_ValueOf(x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
507 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000508}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509
510
ager@chromium.org5c838252010-02-19 08:53:10 +0000511// ECMA-262, section 9.2, page 30
512function ToBoolean(x) {
513 if (IS_BOOLEAN(x)) return x;
514 if (IS_STRING(x)) return x.length != 0;
515 if (x == null) return false;
516 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
517 return true;
518}
519
520
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521// ECMA-262, section 9.3, page 31.
522function ToNumber(x) {
523 if (IS_NUMBER(x)) return x;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000524 if (IS_STRING(x)) {
525 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
526 : %StringToNumber(x);
527 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528 if (IS_BOOLEAN(x)) return x ? 1 : 0;
mstarzinger@chromium.org69008382013-10-18 10:34:25 +0000529 if (IS_UNDEFINED(x)) return NAN;
530 if (IS_SYMBOL(x)) return NAN;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000532}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000534function NonNumberToNumber(x) {
535 if (IS_STRING(x)) {
536 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
537 : %StringToNumber(x);
538 }
539 if (IS_BOOLEAN(x)) return x ? 1 : 0;
mstarzinger@chromium.org69008382013-10-18 10:34:25 +0000540 if (IS_UNDEFINED(x)) return NAN;
541 if (IS_SYMBOL(x)) return NAN;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000542 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
543}
544
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545
546// ECMA-262, section 9.8, page 35.
547function ToString(x) {
548 if (IS_STRING(x)) return x;
ager@chromium.org5c838252010-02-19 08:53:10 +0000549 if (IS_NUMBER(x)) return %_NumberToString(x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
551 if (IS_UNDEFINED(x)) return 'undefined';
552 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000553}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000555function NonStringToString(x) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000556 if (IS_NUMBER(x)) return %_NumberToString(x);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000557 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
558 if (IS_UNDEFINED(x)) return 'undefined';
559 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
560}
561
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562
ulan@chromium.org750145a2013-03-07 15:14:13 +0000563// ES6 symbols
564function ToName(x) {
565 return IS_SYMBOL(x) ? x : %ToString(x);
566}
567
568
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569// ECMA-262, section 9.9, page 36.
570function ToObject(x) {
571 if (IS_STRING(x)) return new $String(x);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000572 if (IS_SYMBOL(x)) return new $Symbol(x);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 if (IS_NUMBER(x)) return new $Number(x);
574 if (IS_BOOLEAN(x)) return new $Boolean(x);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000575 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000576 throw %MakeTypeError('undefined_or_null_to_object', []);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000577 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 return x;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000579}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580
581
582// ECMA-262, section 9.4, page 34.
583function ToInteger(x) {
584 if (%_IsSmi(x)) return x;
585 return %NumberToInteger(ToNumber(x));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000586}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587
588
589// ECMA-262, section 9.6, page 34.
590function ToUint32(x) {
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000591 if (%_IsSmi(x) && x >= 0) return x;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000592 return %NumberToJSUint32(ToNumber(x));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000593}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594
595
596// ECMA-262, section 9.5, page 34
597function ToInt32(x) {
598 if (%_IsSmi(x)) return x;
599 return %NumberToJSInt32(ToNumber(x));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000600}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601
602
ager@chromium.org5c838252010-02-19 08:53:10 +0000603// ES5, section 9.12
604function SameValue(x, y) {
605 if (typeof x != typeof y) return false;
ager@chromium.org5c838252010-02-19 08:53:10 +0000606 if (IS_NUMBER(x)) {
607 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000608 // x is +0 and y is -0 or vice versa.
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000609 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
610 return false;
611 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000612 }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000613 return x === y;
ager@chromium.org5c838252010-02-19 08:53:10 +0000614}
615
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616
617/* ---------------------------------
618 - - - U t i l i t i e s - - -
619 ---------------------------------
620*/
621
622// Returns if the given x is a primitive value - not an object or a
623// function.
624function IsPrimitive(x) {
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000625 // Even though the type of null is "object", null is still
626 // considered a primitive value. IS_SPEC_OBJECT handles this correctly
627 // (i.e., it will return false if x is null).
628 return !IS_SPEC_OBJECT(x);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000629}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630
631
632// ECMA-262, section 8.6.2.6, page 28.
633function DefaultNumber(x) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000634 var valueOf = x.valueOf;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000635 if (IS_SPEC_FUNCTION(valueOf)) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000636 var v = %_CallFunction(x, valueOf);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 if (%IsPrimitive(v)) return v;
638 }
639
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000640 var toString = x.toString;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000641 if (IS_SPEC_FUNCTION(toString)) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000642 var s = %_CallFunction(x, toString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 if (%IsPrimitive(s)) return s;
644 }
645
646 throw %MakeTypeError('cannot_convert_to_primitive', []);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000647}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649// ECMA-262, section 8.6.2.6, page 28.
650function DefaultString(x) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000651 var toString = x.toString;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000652 if (IS_SPEC_FUNCTION(toString)) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000653 var s = %_CallFunction(x, toString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 if (%IsPrimitive(s)) return s;
655 }
656
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000657 var valueOf = x.valueOf;
lrn@chromium.org34e60782011-09-15 07:25:40 +0000658 if (IS_SPEC_FUNCTION(valueOf)) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000659 var v = %_CallFunction(x, valueOf);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660 if (%IsPrimitive(v)) return v;
661 }
662
663 throw %MakeTypeError('cannot_convert_to_primitive', []);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000664}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000666function ToPositiveInteger(x, rangeErrorName) {
667 var i = TO_INTEGER(x);
668 if (i < 0) throw %MakeRangeError(rangeErrorName);
669 return i;
670}
671
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672
673// NOTE: Setting the prototype for Array must take place as early as
674// possible due to code generation for array literals. When
675// generating code for a array literal a boilerplate array is created
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000676// that is cloned when running the code. It is essential that the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677// boilerplate gets the right prototype.
678%FunctionSetPrototype($Array, new $Array(0));