blob: 105749a7597fffe592b87670d540c671fa9a2eac [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
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.
44const $Object = global.Object;
45const $Array = global.Array;
46const $String = global.String;
47const $Number = global.Number;
48const $Function = global.Function;
49const $Boolean = global.Boolean;
50const $NaN = 0/0;
51
52
53// ECMA-262, section 11.9.1, page 55.
54function EQUALS(y) {
55 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
56 var x = this;
57
58 // NOTE: We use iteration instead of recursion, because it is
59 // difficult to call EQUALS with the correct setting of 'this' in
60 // an efficient way.
61 while (true) {
62 if (IS_NUMBER(x)) {
63 if (y == null) return 1; // not equal
64 return %NumberEquals(x, %ToNumber(y));
65 } else if (IS_STRING(x)) {
66 if (IS_STRING(y)) return %StringEquals(x, y);
67 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
68 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
69 if (y == null) return 1; // not equal
70 y = %ToPrimitive(y, NO_HINT);
71 } else if (IS_BOOLEAN(x)) {
72 if (IS_BOOLEAN(y)) {
73 return %_ObjectEquals(x, y) ? 0 : 1;
74 }
75 if (y == null) return 1; // not equal
76 return %NumberEquals(%ToNumber(x), %ToNumber(y));
77 } else if (x == null) {
78 // NOTE: This checks for both null and undefined.
79 return (y == null) ? 0 : 1;
80 } else {
81 // x is not a number, boolean, null or undefined.
82 if (y == null) return 1; // not equal
83 if (IS_OBJECT(y)) {
84 return %_ObjectEquals(x, y) ? 0 : 1;
85 }
86 if (IS_FUNCTION(y)) {
87 return %_ObjectEquals(x, y) ? 0 : 1;
88 }
89
90 x = %ToPrimitive(x, NO_HINT);
91 }
92 }
93}
94
95// ECMA-262, section 11.9.4, page 56.
96function STRICT_EQUALS(x) {
97 if (IS_STRING(this)) {
98 if (!IS_STRING(x)) return 1; // not equal
99 return %StringEquals(this, x);
100 }
101
102 if (IS_NUMBER(this)) {
103 if (!IS_NUMBER(x)) return 1; // not equal
104 return %NumberEquals(this, x);
105 }
106
107 // If anything else gets here, we just do simple identity check.
108 // Objects (including functions), null, undefined and booleans were
109 // checked in the CompareStub, so there should be nothing left.
110 return %_ObjectEquals(this, x) ? 0 : 1;
111}
112
113
114// ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
115// the result when either (or both) the operands are NaN.
116function COMPARE(x, ncr) {
117 // Fast case for numbers and strings.
118 if (IS_NUMBER(this) && IS_NUMBER(x)) {
119 return %NumberCompare(this, x, ncr);
120 }
121 if (IS_STRING(this) && IS_STRING(x)) {
122 return %StringCompare(this, x);
123 }
124
125 // Default implementation.
126 var a = %ToPrimitive(this, NUMBER_HINT);
127 var b = %ToPrimitive(x, NUMBER_HINT);
128 if (IS_STRING(a) && IS_STRING(b)) {
129 return %StringCompare(a, b);
130 } else {
Steve Blockd0582a62009-12-15 09:54:21 +0000131 var a_number = %ToNumber(a);
132 var b_number = %ToNumber(b);
133 if (NUMBER_IS_NAN(a_number) || NUMBER_IS_NAN(b_number)) return ncr;
134 return %NumberCompare(a_number, b_number, ncr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000135 }
136}
137
138
139
140/* -----------------------------------
141 - - - A r i t h m e t i c - - -
142 -----------------------------------
143*/
144
145// ECMA-262, section 11.6.1, page 50.
146function ADD(x) {
147 // Fast case: Check for number operands and do the addition.
148 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
Steve Blockd0582a62009-12-15 09:54:21 +0000149 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000150
151 // Default implementation.
152 var a = %ToPrimitive(this, NO_HINT);
153 var b = %ToPrimitive(x, NO_HINT);
154
155 if (IS_STRING(a)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000156 return %_StringAdd(a, %ToString(b));
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 } else if (IS_STRING(b)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000158 return %_StringAdd(%ToString(a), b);
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 } else {
160 return %NumberAdd(%ToNumber(a), %ToNumber(b));
161 }
162}
163
164
165// Left operand (this) is already a string.
166function STRING_ADD_LEFT(y) {
167 if (!IS_STRING(y)) {
168 if (IS_STRING_WRAPPER(y)) {
169 y = %_ValueOf(y);
170 } else {
171 y = IS_NUMBER(y)
172 ? %NumberToString(y)
173 : %ToString(%ToPrimitive(y, NO_HINT));
174 }
175 }
Steve Blockd0582a62009-12-15 09:54:21 +0000176 return %_StringAdd(this, y);
Steve Blocka7e24c12009-10-30 11:49:00 +0000177}
178
179
180// Right operand (y) is already a string.
181function STRING_ADD_RIGHT(y) {
182 var x = this;
183 if (!IS_STRING(x)) {
184 if (IS_STRING_WRAPPER(x)) {
185 x = %_ValueOf(x);
186 } else {
187 x = IS_NUMBER(x)
188 ? %NumberToString(x)
189 : %ToString(%ToPrimitive(x, NO_HINT));
190 }
191 }
Steve Blockd0582a62009-12-15 09:54:21 +0000192 return %_StringAdd(x, y);
Steve Blocka7e24c12009-10-30 11:49:00 +0000193}
194
195
196// ECMA-262, section 11.6.2, page 50.
197function SUB(y) {
198 var x = IS_NUMBER(this) ? this : %ToNumber(this);
199 if (!IS_NUMBER(y)) y = %ToNumber(y);
200 return %NumberSub(x, y);
201}
202
203
204// ECMA-262, section 11.5.1, page 48.
205function MUL(y) {
206 var x = IS_NUMBER(this) ? this : %ToNumber(this);
207 if (!IS_NUMBER(y)) y = %ToNumber(y);
208 return %NumberMul(x, y);
209}
210
211
212// ECMA-262, section 11.5.2, page 49.
213function DIV(y) {
214 var x = IS_NUMBER(this) ? this : %ToNumber(this);
215 if (!IS_NUMBER(y)) y = %ToNumber(y);
216 return %NumberDiv(x, y);
217}
218
219
220// ECMA-262, section 11.5.3, page 49.
221function MOD(y) {
222 var x = IS_NUMBER(this) ? this : %ToNumber(this);
223 if (!IS_NUMBER(y)) y = %ToNumber(y);
224 return %NumberMod(x, y);
225}
226
227
228
229/* -------------------------------------------
230 - - - B i t o p e r a t i o n s - - -
231 -------------------------------------------
232*/
233
234// ECMA-262, section 11.10, page 57.
235function BIT_OR(y) {
236 var x = IS_NUMBER(this) ? this : %ToNumber(this);
237 if (!IS_NUMBER(y)) y = %ToNumber(y);
238 return %NumberOr(x, y);
239}
240
241
242// ECMA-262, section 11.10, page 57.
243function BIT_AND(y) {
244 var x;
245 if (IS_NUMBER(this)) {
246 x = this;
247 if (!IS_NUMBER(y)) y = %ToNumber(y);
248 } else {
249 x = %ToNumber(this);
250 // Make sure to convert the right operand to a number before
251 // bailing out in the fast case, but after converting the
252 // left operand. This ensures that valueOf methods on the right
253 // operand are always executed.
254 if (!IS_NUMBER(y)) y = %ToNumber(y);
255 // Optimize for the case where we end up AND'ing a value
256 // that doesn't convert to a number. This is common in
257 // certain benchmarks.
258 if (NUMBER_IS_NAN(x)) return 0;
259 }
260 return %NumberAnd(x, y);
261}
262
263
264// ECMA-262, section 11.10, page 57.
265function BIT_XOR(y) {
266 var x = IS_NUMBER(this) ? this : %ToNumber(this);
267 if (!IS_NUMBER(y)) y = %ToNumber(y);
268 return %NumberXor(x, y);
269}
270
271
272// ECMA-262, section 11.4.7, page 47.
273function UNARY_MINUS() {
274 var x = IS_NUMBER(this) ? this : %ToNumber(this);
275 return %NumberUnaryMinus(x);
276}
277
278
279// ECMA-262, section 11.4.8, page 48.
280function BIT_NOT() {
281 var x = IS_NUMBER(this) ? this : %ToNumber(this);
282 return %NumberNot(x);
283}
284
285
286// ECMA-262, section 11.7.1, page 51.
287function SHL(y) {
288 var x = IS_NUMBER(this) ? this : %ToNumber(this);
289 if (!IS_NUMBER(y)) y = %ToNumber(y);
290 return %NumberShl(x, y);
291}
292
293
294// ECMA-262, section 11.7.2, page 51.
295function SAR(y) {
296 var x;
297 if (IS_NUMBER(this)) {
298 x = this;
299 if (!IS_NUMBER(y)) y = %ToNumber(y);
300 } else {
301 x = %ToNumber(this);
302 // Make sure to convert the right operand to a number before
303 // bailing out in the fast case, but after converting the
304 // left operand. This ensures that valueOf methods on the right
305 // operand are always executed.
306 if (!IS_NUMBER(y)) y = %ToNumber(y);
307 // Optimize for the case where we end up shifting a value
308 // that doesn't convert to a number. This is common in
309 // certain benchmarks.
310 if (NUMBER_IS_NAN(x)) return 0;
311 }
312 return %NumberSar(x, y);
313}
314
315
316// ECMA-262, section 11.7.3, page 52.
317function SHR(y) {
318 var x = IS_NUMBER(this) ? this : %ToNumber(this);
319 if (!IS_NUMBER(y)) y = %ToNumber(y);
320 return %NumberShr(x, y);
321}
322
323
324
325/* -----------------------------
326 - - - H e l p e r s - - -
327 -----------------------------
328*/
329
330// ECMA-262, section 11.4.1, page 46.
331function DELETE(key) {
332 return %DeleteProperty(%ToObject(this), %ToString(key));
333}
334
335
336// ECMA-262, section 11.8.7, page 54.
337function IN(x) {
338 if (x == null || (!IS_OBJECT(x) && !IS_FUNCTION(x))) {
339 throw %MakeTypeError('invalid_in_operator_use', [this, x]);
340 }
341 return %_IsNonNegativeSmi(this) ? %HasElement(x, this) : %HasProperty(x, %ToString(this));
342}
343
344
345// ECMA-262, section 11.8.6, page 54. To make the implementation more
346// efficient, the return value should be zero if the 'this' is an
347// instance of F, and non-zero if not. This makes it possible to avoid
348// an expensive ToBoolean conversion in the generated code.
349function INSTANCE_OF(F) {
350 var V = this;
351 if (!IS_FUNCTION(F)) {
352 throw %MakeTypeError('instanceof_function_expected', [V]);
353 }
354
355 // If V is not an object, return false.
356 if (IS_NULL(V) || (!IS_OBJECT(V) && !IS_FUNCTION(V))) {
357 return 1;
358 }
359
360 // Get the prototype of F; if it is not an object, throw an error.
361 var O = F.prototype;
362 if (IS_NULL(O) || (!IS_OBJECT(O) && !IS_FUNCTION(O))) {
363 throw %MakeTypeError('instanceof_nonobject_proto', [O]);
364 }
365
366 // Return whether or not O is in the prototype chain of V.
367 return %IsInPrototypeChain(O, V) ? 0 : 1;
368}
369
370
371// Get an array of property keys for the given object. Used in
372// for-in statements.
373function GET_KEYS() {
374 return %GetPropertyNames(this);
375}
376
377
378// Filter a given key against an object by checking if the object
379// has a property with the given key; return the key as a string if
380// it has. Otherwise returns null. Used in for-in statements.
381function FILTER_KEY(key) {
382 var string = %ToString(key);
383 if (%HasProperty(this, string)) return string;
384 return null;
385}
386
387
388function CALL_NON_FUNCTION() {
389 var callee = %GetCalledFunction();
390 var delegate = %GetFunctionDelegate(callee);
391 if (!IS_FUNCTION(delegate)) {
392 throw %MakeTypeError('called_non_callable', [typeof callee]);
393 }
394
395 var parameters = %NewArguments(delegate);
396 return delegate.apply(callee, parameters);
397}
398
399
400function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
401 var callee = %GetCalledFunction();
402 var delegate = %GetConstructorDelegate(callee);
403 if (!IS_FUNCTION(delegate)) {
404 throw %MakeTypeError('called_non_callable', [typeof callee]);
405 }
406
407 var parameters = %NewArguments(delegate);
408 return delegate.apply(callee, parameters);
409}
410
411
412function APPLY_PREPARE(args) {
413 var length;
414 // First check whether length is a positive Smi and args is an
415 // array. This is the fast case. If this fails, we do the slow case
416 // that takes care of more eventualities.
417 if (IS_ARRAY(args)) {
418 length = args.length;
419 if (%_IsSmi(length) && length >= 0 && length < 0x800000 && IS_FUNCTION(this)) {
420 return length;
421 }
422 }
423
424 length = (args == null) ? 0 : %ToUint32(args.length);
425
426 // We can handle any number of apply arguments if the stack is
427 // big enough, but sanity check the value to avoid overflow when
428 // multiplying with pointer size.
429 if (length > 0x800000) {
430 throw %MakeRangeError('apply_overflow', [length]);
431 }
432
433 if (!IS_FUNCTION(this)) {
434 throw %MakeTypeError('apply_non_function', [ %ToString(this), typeof this ]);
435 }
436
437 // Make sure the arguments list has the right type.
438 if (args != null && !IS_ARRAY(args) && !IS_ARGUMENTS(args)) {
439 throw %MakeTypeError('apply_wrong_args', []);
440 }
441
442 // Return the length which is the number of arguments to copy to the
443 // stack. It is guaranteed to be a small integer at this point.
444 return length;
445}
446
447
448function APPLY_OVERFLOW(length) {
449 throw %MakeRangeError('apply_overflow', [length]);
450}
451
452
453// Convert the receiver to an object - forward to ToObject.
454function TO_OBJECT() {
455 return %ToObject(this);
456}
457
458
459// Convert the receiver to a number - forward to ToNumber.
460function TO_NUMBER() {
461 return %ToNumber(this);
462}
463
464
465// Convert the receiver to a string - forward to ToString.
466function TO_STRING() {
467 return %ToString(this);
468}
469
470
471/* -------------------------------------
472 - - - C o n v e r s i o n s - - -
473 -------------------------------------
474*/
475
476// ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
477// (1) for number hint, and (2) for string hint.
478function ToPrimitive(x, hint) {
479 // Fast case check.
480 if (IS_STRING(x)) return x;
481 // Normal behavior.
482 if (!IS_OBJECT(x) && !IS_FUNCTION(x)) return x;
483 if (x == null) return x; // check for null, undefined
484 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
485 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
486}
487
488
489// ECMA-262, section 9.3, page 31.
490function ToNumber(x) {
491 if (IS_NUMBER(x)) return x;
492 if (IS_STRING(x)) return %StringToNumber(x);
493 if (IS_BOOLEAN(x)) return x ? 1 : 0;
494 if (IS_UNDEFINED(x)) return $NaN;
495 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
496}
497
498
499// ECMA-262, section 9.8, page 35.
500function ToString(x) {
501 if (IS_STRING(x)) return x;
502 if (IS_NUMBER(x)) return %NumberToString(x);
503 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
504 if (IS_UNDEFINED(x)) return 'undefined';
505 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
506}
507
508
509// ... where did this come from?
510function ToBoolean(x) {
511 if (IS_BOOLEAN(x)) return x;
512 if (IS_STRING(x)) return x.length != 0;
513 if (x == null) return false;
514 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
515 return true;
516}
517
518
519// ECMA-262, section 9.9, page 36.
520function ToObject(x) {
521 if (IS_STRING(x)) return new $String(x);
522 if (IS_NUMBER(x)) return new $Number(x);
523 if (IS_BOOLEAN(x)) return new $Boolean(x);
524 if (x == null) throw %MakeTypeError('null_to_object', []);
525 return x;
526}
527
528
529// ECMA-262, section 9.4, page 34.
530function ToInteger(x) {
531 if (%_IsSmi(x)) return x;
532 return %NumberToInteger(ToNumber(x));
533}
534
535
536// ECMA-262, section 9.6, page 34.
537function ToUint32(x) {
538 if (%_IsSmi(x) && x >= 0) return x;
539 return %NumberToJSUint32(ToNumber(x));
540}
541
542
543// ECMA-262, section 9.5, page 34
544function ToInt32(x) {
545 if (%_IsSmi(x)) return x;
546 return %NumberToJSInt32(ToNumber(x));
547}
548
549
550
551/* ---------------------------------
552 - - - U t i l i t i e s - - -
553 ---------------------------------
554*/
555
556// Returns if the given x is a primitive value - not an object or a
557// function.
558function IsPrimitive(x) {
559 if (!IS_OBJECT(x) && !IS_FUNCTION(x)) {
560 return true;
561 } else {
562 // Even though the type of null is "object", null is still
563 // considered a primitive value.
564 return IS_NULL(x);
565 }
566}
567
568
569// ECMA-262, section 8.6.2.6, page 28.
570function DefaultNumber(x) {
571 if (IS_FUNCTION(x.valueOf)) {
572 var v = x.valueOf();
573 if (%IsPrimitive(v)) return v;
574 }
575
576 if (IS_FUNCTION(x.toString)) {
577 var s = x.toString();
578 if (%IsPrimitive(s)) return s;
579 }
580
581 throw %MakeTypeError('cannot_convert_to_primitive', []);
582}
583
584
585// ECMA-262, section 8.6.2.6, page 28.
586function DefaultString(x) {
587 if (IS_FUNCTION(x.toString)) {
588 var s = x.toString();
589 if (%IsPrimitive(s)) return s;
590 }
591
592 if (IS_FUNCTION(x.valueOf)) {
593 var v = x.valueOf();
594 if (%IsPrimitive(v)) return v;
595 }
596
597 throw %MakeTypeError('cannot_convert_to_primitive', []);
598}
599
600
601// NOTE: Setting the prototype for Array must take place as early as
602// possible due to code generation for array literals. When
603// generating code for a array literal a boilerplate array is created
604// that is cloned when running the code. It is essiential that the
605// boilerplate gets the right prototype.
606%FunctionSetPrototype($Array, new $Array(0));