1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This files contains runtime support implemented in JavaScript.
7 // CAUTION: Some of the functions specified in this file are called
8 // directly from compiled code. These are the functions with names in
9 // ALL CAPS. The compiled code passes the first argument in 'this' and
10 // it does not push the function onto the stack. This means that you
11 // cannot use contexts in all these functions.
14 /* -----------------------------------
15 - - - C o m p a r i s o n - - -
16 -----------------------------------
19 // The following declarations are shared with other native JS files.
20 // They are all declared at this one spot to avoid redeclaration errors.
21 var $Object = global.Object;
22 var $Array = global.Array;
23 var $String = global.String;
24 var $Number = global.Number;
25 var $Function = global.Function;
26 var $Boolean = global.Boolean;
27 var $NaN = %GetRootNaN();
29 // ECMA-262 Section 11.9.3.
31 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
37 if (IS_NUMBER(y)) return %NumberEquals(x, y);
38 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
39 if (!IS_SPEC_OBJECT(y)) {
41 return %NumberEquals(x, %ToNumber(y));
43 y = %ToPrimitive(y, NO_HINT);
45 } else if (IS_STRING(x)) {
47 if (IS_STRING(y)) return %StringEquals(x, y);
48 if (IS_SYMBOL(y)) return 1; // not equal
49 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
50 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
51 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
52 y = %ToPrimitive(y, NO_HINT);
54 } else if (IS_SYMBOL(x)) {
55 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1;
56 return 1; // not equal
57 } else if (IS_BOOLEAN(x)) {
58 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
59 if (IS_NULL_OR_UNDEFINED(y)) return 1;
60 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
61 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
62 if (IS_SYMBOL(y)) return 1; // not equal
65 y = %ToPrimitive(y, NO_HINT);
66 } else if (IS_NULL_OR_UNDEFINED(x)) {
67 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
70 if (IS_SPEC_OBJECT(y)) {
71 return %_ObjectEquals(x, y) ? 0 : 1;
73 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
74 if (IS_SYMBOL(y)) return 1; // not equal
75 if (IS_BOOLEAN(y)) y = %ToNumber(y);
76 x = %ToPrimitive(x, NO_HINT);
81 // ECMA-262, section 11.9.4, page 56.
82 function STRICT_EQUALS(x) {
83 if (IS_STRING(this)) {
84 if (!IS_STRING(x)) return 1; // not equal
85 return %StringEquals(this, x);
88 if (IS_NUMBER(this)) {
89 if (!IS_NUMBER(x)) return 1; // not equal
90 return %NumberEquals(this, x);
93 // If anything else gets here, we just do simple identity check.
94 // Objects (including functions), null, undefined and booleans were
95 // checked in the CompareStub, so there should be nothing left.
96 return %_ObjectEquals(this, x) ? 0 : 1;
100 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
101 // the result when either (or both) the operands are NaN.
102 function COMPARE(x, ncr) {
105 // Fast cases for string, numbers and undefined compares.
106 if (IS_STRING(this)) {
107 if (IS_STRING(x)) return %_StringCompare(this, x);
108 if (IS_UNDEFINED(x)) return ncr;
110 } else if (IS_NUMBER(this)) {
111 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
112 if (IS_UNDEFINED(x)) return ncr;
114 } else if (IS_UNDEFINED(this)) {
115 if (!IS_UNDEFINED(x)) {
116 %ToPrimitive(x, NUMBER_HINT);
119 } else if (IS_UNDEFINED(x)) {
120 %ToPrimitive(this, NUMBER_HINT);
123 left = %ToPrimitive(this, NUMBER_HINT);
126 right = %ToPrimitive(x, NUMBER_HINT);
127 if (IS_STRING(left) && IS_STRING(right)) {
128 return %_StringCompare(left, right);
130 var left_number = %ToNumber(left);
131 var right_number = %ToNumber(right);
132 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
133 return %NumberCompare(left_number, right_number, ncr);
139 /* -----------------------------------
140 - - - A r i t h m e t i c - - -
141 -----------------------------------
144 // ECMA-262, section 11.6.1, page 50.
146 // Fast case: Check for number operands and do the addition.
147 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
148 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
150 // Default implementation.
151 var a = %ToPrimitive(this, NO_HINT);
152 var b = %ToPrimitive(x, NO_HINT);
155 return %_StringAdd(a, %ToString(b));
156 } else if (IS_STRING(b)) {
157 return %_StringAdd(%NonStringToString(a), b);
159 return %NumberAdd(%ToNumber(a), %ToNumber(b));
164 // Left operand (this) is already a string.
165 function STRING_ADD_LEFT(y) {
167 if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
171 ? %_NumberToString(y)
172 : %ToString(%ToPrimitive(y, NO_HINT));
175 return %_StringAdd(this, y);
179 // Right operand (y) is already a string.
180 function STRING_ADD_RIGHT(y) {
183 if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
187 ? %_NumberToString(x)
188 : %ToString(%ToPrimitive(x, NO_HINT));
191 return %_StringAdd(x, y);
195 // ECMA-262, section 11.6.2, page 50.
197 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
198 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
199 return %NumberSub(x, y);
203 // ECMA-262, section 11.5.1, page 48.
205 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
206 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
207 return %NumberMul(x, y);
211 // ECMA-262, section 11.5.2, page 49.
213 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
214 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
215 return %NumberDiv(x, y);
219 // ECMA-262, section 11.5.3, page 49.
221 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
222 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
223 return %NumberMod(x, y);
228 /* -------------------------------------------
229 - - - B i t o p e r a t i o n s - - -
230 -------------------------------------------
233 // ECMA-262, section 11.10, page 57.
235 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
236 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
237 return %NumberOr(x, y);
241 // ECMA-262, section 11.10, page 57.
242 function BIT_AND(y) {
244 if (IS_NUMBER(this)) {
246 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
248 x = %NonNumberToNumber(this);
249 // Make sure to convert the right operand to a number before
250 // bailing out in the fast case, but after converting the
251 // left operand. This ensures that valueOf methods on the right
252 // operand are always executed.
253 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
254 // Optimize for the case where we end up AND'ing a value
255 // that doesn't convert to a number. This is common in
256 // certain benchmarks.
257 if (NUMBER_IS_NAN(x)) return 0;
259 return %NumberAnd(x, y);
263 // ECMA-262, section 11.10, page 57.
264 function BIT_XOR(y) {
265 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
266 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
267 return %NumberXor(x, y);
271 // ECMA-262, section 11.7.1, page 51.
273 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
274 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
275 return %NumberShl(x, y);
279 // ECMA-262, section 11.7.2, page 51.
282 if (IS_NUMBER(this)) {
284 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
286 x = %NonNumberToNumber(this);
287 // Make sure to convert the right operand to a number before
288 // bailing out in the fast case, but after converting the
289 // left operand. This ensures that valueOf methods on the right
290 // operand are always executed.
291 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
292 // Optimize for the case where we end up shifting a value
293 // that doesn't convert to a number. This is common in
294 // certain benchmarks.
295 if (NUMBER_IS_NAN(x)) return 0;
297 return %NumberSar(x, y);
301 // ECMA-262, section 11.7.3, page 52.
303 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
304 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
305 return %NumberShr(x, y);
310 /* -----------------------------
311 - - - H e l p e r s - - -
312 -----------------------------
315 // ECMA-262, section 11.4.1, page 46.
316 function DELETE(key, strict) {
317 return %DeleteProperty(%ToObject(this), %ToName(key), strict);
321 // ECMA-262, section 11.8.7, page 54.
323 if (!IS_SPEC_OBJECT(x)) {
324 throw %MakeTypeError('invalid_in_operator_use', [this, x]);
326 return %_IsNonNegativeSmi(this) ?
327 %HasElement(x, this) : %HasProperty(x, %ToName(this));
331 // ECMA-262, section 11.8.6, page 54. To make the implementation more
332 // efficient, the return value should be zero if the 'this' is an
333 // instance of F, and non-zero if not. This makes it possible to avoid
334 // an expensive ToBoolean conversion in the generated code.
335 function INSTANCE_OF(F) {
337 if (!IS_SPEC_FUNCTION(F)) {
338 throw %MakeTypeError('instanceof_function_expected', [F]);
341 // If V is not an object, return false.
342 if (!IS_SPEC_OBJECT(V)) {
346 // Check if function is bound, if so, get [[BoundFunction]] from it
347 // and use that instead of F.
348 var bindings = %BoundFunctionGetBindings(F);
350 F = bindings[kBoundFunctionIndex]; // Always a non-bound function.
352 // Get the prototype of F; if it is not an object, throw an error.
354 if (!IS_SPEC_OBJECT(O)) {
355 throw %MakeTypeError('instanceof_nonobject_proto', [O]);
358 // Return whether or not O is in the prototype chain of V.
359 return %IsInPrototypeChain(O, V) ? 0 : 1;
363 // Filter a given key against an object by checking if the object
364 // has a property with the given key; return the key as a string if
365 // it has. Otherwise returns 0 (smi). Used in for-in statements.
366 function FILTER_KEY(key) {
367 var string = %ToName(key);
368 if (%HasProperty(this, string)) return string;
373 function CALL_NON_FUNCTION() {
374 var delegate = %GetFunctionDelegate(this);
375 if (!IS_FUNCTION(delegate)) {
376 throw %MakeTypeError('called_non_callable', [typeof this]);
378 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
382 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
383 var delegate = %GetConstructorDelegate(this);
384 if (!IS_FUNCTION(delegate)) {
385 throw %MakeTypeError('called_non_callable', [typeof this]);
387 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
391 function CALL_FUNCTION_PROXY() {
392 var arity = %_ArgumentsLength() - 1;
393 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg.
394 var trap = %GetCallTrap(proxy);
395 return %Apply(trap, this, arguments, 0, arity);
399 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
401 var trap = %GetConstructTrap(proxy);
402 return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
406 function APPLY_PREPARE(args) {
408 // First check whether length is a positive Smi and args is an
409 // array. This is the fast case. If this fails, we do the slow case
410 // that takes care of more eventualities.
411 if (IS_ARRAY(args)) {
412 length = args.length;
413 if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
414 IS_SPEC_FUNCTION(this)) {
419 length = (args == null) ? 0 : %ToUint32(args.length);
421 // We can handle any number of apply arguments if the stack is
422 // big enough, but sanity check the value to avoid overflow when
423 // multiplying with pointer size.
424 if (length > 0x800000) {
425 throw %MakeRangeError('stack_overflow', []);
428 if (!IS_SPEC_FUNCTION(this)) {
429 throw %MakeTypeError('apply_non_function',
430 [ %ToString(this), typeof this ]);
433 // Make sure the arguments list has the right type.
434 if (args != null && !IS_SPEC_OBJECT(args)) {
435 throw %MakeTypeError('apply_wrong_args', []);
438 // Return the length which is the number of arguments to copy to the
439 // stack. It is guaranteed to be a small integer at this point.
444 function STACK_OVERFLOW(length) {
445 throw %MakeRangeError('stack_overflow', []);
449 // Convert the receiver to an object - forward to ToObject.
450 function TO_OBJECT() {
451 return %ToObject(this);
455 // Convert the receiver to a number - forward to ToNumber.
456 function TO_NUMBER() {
457 return %ToNumber(this);
461 // Convert the receiver to a string - forward to ToString.
462 function TO_STRING() {
463 return %ToString(this);
467 /* -------------------------------------
468 - - - C o n v e r s i o n s - - -
469 -------------------------------------
472 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
473 // (1) for number hint, and (2) for string hint.
474 function ToPrimitive(x, hint) {
476 if (IS_STRING(x)) return x;
478 if (!IS_SPEC_OBJECT(x)) return x;
479 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError('symbol_to_primitive', []);
480 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
481 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
485 // ECMA-262, section 9.2, page 30
486 function ToBoolean(x) {
487 if (IS_BOOLEAN(x)) return x;
488 if (IS_STRING(x)) return x.length != 0;
489 if (x == null) return false;
490 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
495 // ECMA-262, section 9.3, page 31.
496 function ToNumber(x) {
497 if (IS_NUMBER(x)) return x;
499 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
500 : %StringToNumber(x);
502 if (IS_BOOLEAN(x)) return x ? 1 : 0;
503 if (IS_UNDEFINED(x)) return NAN;
504 if (IS_SYMBOL(x)) return NAN;
505 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
508 function NonNumberToNumber(x) {
510 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
511 : %StringToNumber(x);
513 if (IS_BOOLEAN(x)) return x ? 1 : 0;
514 if (IS_UNDEFINED(x)) return NAN;
515 if (IS_SYMBOL(x)) return NAN;
516 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
520 // ECMA-262, section 9.8, page 35.
521 function ToString(x) {
522 if (IS_STRING(x)) return x;
523 if (IS_NUMBER(x)) return %_NumberToString(x);
524 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
525 if (IS_UNDEFINED(x)) return 'undefined';
526 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
527 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
530 function NonStringToString(x) {
531 if (IS_NUMBER(x)) return %_NumberToString(x);
532 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
533 if (IS_UNDEFINED(x)) return 'undefined';
534 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
535 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
541 return IS_SYMBOL(x) ? x : %ToString(x);
545 // ECMA-262, section 9.9, page 36.
546 function ToObject(x) {
547 if (IS_STRING(x)) return new $String(x);
548 if (IS_NUMBER(x)) return new $Number(x);
549 if (IS_BOOLEAN(x)) return new $Boolean(x);
550 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x);
551 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
552 throw %MakeTypeError('undefined_or_null_to_object', []);
558 // ECMA-262, section 9.4, page 34.
559 function ToInteger(x) {
560 if (%_IsSmi(x)) return x;
561 return %NumberToInteger(ToNumber(x));
565 // ECMA-262, section 9.6, page 34.
566 function ToUint32(x) {
567 if (%_IsSmi(x) && x >= 0) return x;
568 return %NumberToJSUint32(ToNumber(x));
572 // ECMA-262, section 9.5, page 34
573 function ToInt32(x) {
574 if (%_IsSmi(x)) return x;
575 return %NumberToJSInt32(ToNumber(x));
580 function SameValue(x, y) {
581 if (typeof x != typeof y) return false;
583 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
584 // x is +0 and y is -0 or vice versa.
585 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
593 /* ---------------------------------
594 - - - U t i l i t i e s - - -
595 ---------------------------------
598 // Returns if the given x is a primitive value - not an object or a
600 function IsPrimitive(x) {
601 // Even though the type of null is "object", null is still
602 // considered a primitive value. IS_SPEC_OBJECT handles this correctly
603 // (i.e., it will return false if x is null).
604 return !IS_SPEC_OBJECT(x);
608 // ECMA-262, section 8.6.2.6, page 28.
609 function DefaultNumber(x) {
610 var valueOf = x.valueOf;
611 if (IS_SPEC_FUNCTION(valueOf)) {
612 var v = %_CallFunction(x, valueOf);
613 if (%IsPrimitive(v)) return v;
616 var toString = x.toString;
617 if (IS_SPEC_FUNCTION(toString)) {
618 var s = %_CallFunction(x, toString);
619 if (%IsPrimitive(s)) return s;
622 throw %MakeTypeError('cannot_convert_to_primitive', []);
625 // ECMA-262, section 8.6.2.6, page 28.
626 function DefaultString(x) {
627 var toString = x.toString;
628 if (IS_SPEC_FUNCTION(toString)) {
629 var s = %_CallFunction(x, toString);
630 if (%IsPrimitive(s)) return s;
633 var valueOf = x.valueOf;
634 if (IS_SPEC_FUNCTION(valueOf)) {
635 var v = %_CallFunction(x, valueOf);
636 if (%IsPrimitive(v)) return v;
639 throw %MakeTypeError('cannot_convert_to_primitive', []);
642 function ToPositiveInteger(x, rangeErrorName) {
643 var i = TO_INTEGER(x);
644 if (i < 0) throw MakeRangeError(rangeErrorName);
649 // NOTE: Setting the prototype for Array must take place as early as
650 // possible due to code generation for array literals. When
651 // generating code for a array literal a boilerplate array is created
652 // that is cloned when running the code. It is essential that the
653 // boilerplate gets the right prototype.
654 %FunctionSetPrototype($Array, new $Array(0));