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'.
12 /* -----------------------------------
13 - - - C o m p a r i s o n - - -
14 -----------------------------------
17 // The following declarations are shared with other native JS files.
18 // They are all declared at this one spot to avoid redeclaration errors.
19 var $Object = global.Object;
20 var $Array = global.Array;
21 var $String = global.String;
22 var $Number = global.Number;
23 var $Function = global.Function;
24 var $Boolean = global.Boolean;
25 var $NaN = %GetRootNaN();
27 // ECMA-262 Section 11.9.3.
29 if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
35 if (IS_NUMBER(y)) return %NumberEquals(x, y);
36 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
37 if (IS_SYMBOL(y)) return 1; // not equal
38 if (!IS_SPEC_OBJECT(y)) {
40 return %NumberEquals(x, %ToNumber(y));
42 y = %ToPrimitive(y, NO_HINT);
44 } else if (IS_STRING(x)) {
46 if (IS_STRING(y)) return %StringEquals(x, y);
47 if (IS_SYMBOL(y)) return 1; // not equal
48 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
49 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
50 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
51 y = %ToPrimitive(y, NO_HINT);
53 } else if (IS_SYMBOL(x)) {
54 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1;
55 return 1; // not equal
56 } else if (IS_BOOLEAN(x)) {
57 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
58 if (IS_NULL_OR_UNDEFINED(y)) return 1;
59 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
60 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
61 if (IS_SYMBOL(y)) return 1; // not equal
64 y = %ToPrimitive(y, NO_HINT);
65 } else if (IS_NULL_OR_UNDEFINED(x)) {
66 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
69 if (IS_SPEC_OBJECT(y)) {
70 return %_ObjectEquals(x, y) ? 0 : 1;
72 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
73 if (IS_SYMBOL(y)) return 1; // not equal
74 if (IS_BOOLEAN(y)) y = %ToNumber(y);
75 x = %ToPrimitive(x, NO_HINT);
80 // ECMA-262, section 11.9.4, page 56.
81 function STRICT_EQUALS(x) {
82 if (IS_STRING(this)) {
83 if (!IS_STRING(x)) return 1; // not equal
84 return %StringEquals(this, x);
87 if (IS_NUMBER(this)) {
88 if (!IS_NUMBER(x)) return 1; // not equal
89 return %NumberEquals(this, x);
92 // If anything else gets here, we just do simple identity check.
93 // Objects (including functions), null, undefined and booleans were
94 // checked in the CompareStub, so there should be nothing left.
95 return %_ObjectEquals(this, x) ? 0 : 1;
99 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
100 // the result when either (or both) the operands are NaN.
101 function COMPARE(x, ncr) {
104 // Fast cases for string, numbers and undefined compares.
105 if (IS_STRING(this)) {
106 if (IS_STRING(x)) return %_StringCompare(this, x);
107 if (IS_UNDEFINED(x)) return ncr;
109 } else if (IS_NUMBER(this)) {
110 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
111 if (IS_UNDEFINED(x)) return ncr;
113 } else if (IS_UNDEFINED(this)) {
114 if (!IS_UNDEFINED(x)) {
115 %ToPrimitive(x, NUMBER_HINT);
118 } else if (IS_UNDEFINED(x)) {
119 %ToPrimitive(this, NUMBER_HINT);
122 left = %ToPrimitive(this, NUMBER_HINT);
125 right = %ToPrimitive(x, NUMBER_HINT);
126 if (IS_STRING(left) && IS_STRING(right)) {
127 return %_StringCompare(left, right);
129 var left_number = %ToNumber(left);
130 var right_number = %ToNumber(right);
131 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
132 return %NumberCompare(left_number, right_number, ncr);
138 /* -----------------------------------
139 - - - A r i t h m e t i c - - -
140 -----------------------------------
143 // ECMA-262, section 11.6.1, page 50.
145 // Fast case: Check for number operands and do the addition.
146 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
147 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
149 // Default implementation.
150 var a = %ToPrimitive(this, NO_HINT);
151 var b = %ToPrimitive(x, NO_HINT);
154 return %_StringAdd(a, %ToString(b));
155 } else if (IS_STRING(b)) {
156 return %_StringAdd(%NonStringToString(a), b);
158 return %NumberAdd(%ToNumber(a), %ToNumber(b));
163 // Left operand (this) is already a string.
164 function STRING_ADD_LEFT(y) {
166 if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
170 ? %_NumberToString(y)
171 : %ToString(%ToPrimitive(y, NO_HINT));
174 return %_StringAdd(this, y);
178 // Right operand (y) is already a string.
179 function STRING_ADD_RIGHT(y) {
182 if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
186 ? %_NumberToString(x)
187 : %ToString(%ToPrimitive(x, NO_HINT));
190 return %_StringAdd(x, y);
194 // ECMA-262, section 11.6.2, page 50.
196 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
197 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
198 return %NumberSub(x, y);
202 // ECMA-262, section 11.5.1, page 48.
204 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
205 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
206 return %NumberMul(x, y);
210 // ECMA-262, section 11.5.2, page 49.
212 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
213 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
214 return %NumberDiv(x, y);
218 // ECMA-262, section 11.5.3, page 49.
220 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
221 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
222 return %NumberMod(x, y);
227 /* -------------------------------------------
228 - - - B i t o p e r a t i o n s - - -
229 -------------------------------------------
232 // ECMA-262, section 11.10, page 57.
234 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
235 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
236 return %NumberOr(x, y);
240 // ECMA-262, section 11.10, page 57.
241 function BIT_AND(y) {
243 if (IS_NUMBER(this)) {
245 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
247 x = %NonNumberToNumber(this);
248 // Make sure to convert the right operand to a number before
249 // bailing out in the fast case, but after converting the
250 // left operand. This ensures that valueOf methods on the right
251 // operand are always executed.
252 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
253 // Optimize for the case where we end up AND'ing a value
254 // that doesn't convert to a number. This is common in
255 // certain benchmarks.
256 if (NUMBER_IS_NAN(x)) return 0;
258 return %NumberAnd(x, y);
262 // ECMA-262, section 11.10, page 57.
263 function BIT_XOR(y) {
264 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
265 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
266 return %NumberXor(x, y);
270 // ECMA-262, section 11.7.1, page 51.
272 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
273 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
274 return %NumberShl(x, y);
278 // ECMA-262, section 11.7.2, page 51.
281 if (IS_NUMBER(this)) {
283 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
285 x = %NonNumberToNumber(this);
286 // Make sure to convert the right operand to a number before
287 // bailing out in the fast case, but after converting the
288 // left operand. This ensures that valueOf methods on the right
289 // operand are always executed.
290 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
291 // Optimize for the case where we end up shifting a value
292 // that doesn't convert to a number. This is common in
293 // certain benchmarks.
294 if (NUMBER_IS_NAN(x)) return 0;
296 return %NumberSar(x, y);
300 // ECMA-262, section 11.7.3, page 52.
302 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
303 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
304 return %NumberShr(x, y);
309 /* -----------------------------
310 - - - H e l p e r s - - -
311 -----------------------------
314 // ECMA-262, section 11.4.1, page 46.
315 function DELETE(key, strict) {
316 return %DeleteProperty(%ToObject(this), %ToName(key), strict);
320 // ECMA-262, section 11.8.7, page 54.
322 if (!IS_SPEC_OBJECT(x)) {
323 throw %MakeTypeError('invalid_in_operator_use', [this, x]);
325 if (%_IsNonNegativeSmi(this)) {
326 if (IS_ARRAY(x) && %_HasFastPackedElements(x)) {
327 return this < x.length;
329 return %HasElement(x, this);
331 return %HasProperty(x, %ToName(this));
335 // ECMA-262, section 11.8.6, page 54. To make the implementation more
336 // efficient, the return value should be zero if the 'this' is an
337 // instance of F, and non-zero if not. This makes it possible to avoid
338 // an expensive ToBoolean conversion in the generated code.
339 function INSTANCE_OF(F) {
341 if (!IS_SPEC_FUNCTION(F)) {
342 throw %MakeTypeError('instanceof_function_expected', [F]);
345 // If V is not an object, return false.
346 if (!IS_SPEC_OBJECT(V)) {
350 // Check if function is bound, if so, get [[BoundFunction]] from it
351 // and use that instead of F.
352 var bindings = %BoundFunctionGetBindings(F);
354 F = bindings[kBoundFunctionIndex]; // Always a non-bound function.
356 // Get the prototype of F; if it is not an object, throw an error.
358 if (!IS_SPEC_OBJECT(O)) {
359 throw %MakeTypeError('instanceof_nonobject_proto', [O]);
362 // Return whether or not O is in the prototype chain of V.
363 return %IsInPrototypeChain(O, V) ? 0 : 1;
367 // Filter a given key against an object by checking if the object
368 // has a property with the given key; return the key as a string if
369 // it has. Otherwise returns 0 (smi). Used in for-in statements.
370 function FILTER_KEY(key) {
371 var string = %ToName(key);
372 if (%HasProperty(this, string)) return string;
377 function CALL_NON_FUNCTION() {
378 var delegate = %GetFunctionDelegate(this);
379 if (!IS_FUNCTION(delegate)) {
380 throw %MakeTypeError('called_non_callable', [typeof this]);
382 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
386 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
387 var delegate = %GetConstructorDelegate(this);
388 if (!IS_FUNCTION(delegate)) {
389 throw %MakeTypeError('called_non_callable', [typeof this]);
391 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
395 function CALL_FUNCTION_PROXY() {
396 var arity = %_ArgumentsLength() - 1;
397 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg.
398 var trap = %GetCallTrap(proxy);
399 return %Apply(trap, this, arguments, 0, arity);
403 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
405 var trap = %GetConstructTrap(proxy);
406 return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
410 function APPLY_PREPARE(args) {
412 // First check whether length is a positive Smi and args is an
413 // array. This is the fast case. If this fails, we do the slow case
414 // that takes care of more eventualities.
415 if (IS_ARRAY(args)) {
416 length = args.length;
417 if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
418 IS_SPEC_FUNCTION(this)) {
423 length = (args == null) ? 0 : %ToUint32(args.length);
425 // We can handle any number of apply arguments if the stack is
426 // big enough, but sanity check the value to avoid overflow when
427 // multiplying with pointer size.
428 if (length > 0x800000) {
429 throw %MakeRangeError('stack_overflow', []);
432 if (!IS_SPEC_FUNCTION(this)) {
433 throw %MakeTypeError('apply_non_function',
434 [ %ToString(this), typeof this ]);
437 // Make sure the arguments list has the right type.
438 if (args != null && !IS_SPEC_OBJECT(args)) {
439 throw %MakeTypeError('apply_wrong_args', []);
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.
448 function STACK_OVERFLOW(length) {
449 throw %MakeRangeError('stack_overflow', []);
453 // Convert the receiver to an object - forward to ToObject.
454 function TO_OBJECT() {
455 return %ToObject(this);
459 // Convert the receiver to a number - forward to ToNumber.
460 function TO_NUMBER() {
461 return %ToNumber(this);
465 // Convert the receiver to a string - forward to ToString.
466 function TO_STRING() {
467 return %ToString(this);
471 /* -------------------------------------
472 - - - C o n v e r s i o n s - - -
473 -------------------------------------
476 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
477 // (1) for number hint, and (2) for string hint.
478 function ToPrimitive(x, hint) {
480 if (IS_STRING(x)) return x;
482 if (!IS_SPEC_OBJECT(x)) return x;
483 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError('symbol_to_primitive', []);
484 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
485 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
489 // ECMA-262, section 9.2, page 30
490 function ToBoolean(x) {
491 if (IS_BOOLEAN(x)) return x;
492 if (IS_STRING(x)) return x.length != 0;
493 if (x == null) return false;
494 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
499 // ECMA-262, section 9.3, page 31.
500 function ToNumber(x) {
501 if (IS_NUMBER(x)) return x;
503 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
504 : %StringToNumber(x);
506 if (IS_BOOLEAN(x)) return x ? 1 : 0;
507 if (IS_UNDEFINED(x)) return NAN;
508 if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
509 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
512 function NonNumberToNumber(x) {
514 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
515 : %StringToNumber(x);
517 if (IS_BOOLEAN(x)) return x ? 1 : 0;
518 if (IS_UNDEFINED(x)) return NAN;
519 if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
520 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
524 // ECMA-262, section 9.8, page 35.
525 function ToString(x) {
526 if (IS_STRING(x)) return x;
527 if (IS_NUMBER(x)) return %_NumberToString(x);
528 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
529 if (IS_UNDEFINED(x)) return 'undefined';
530 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
531 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
534 function NonStringToString(x) {
535 if (IS_NUMBER(x)) return %_NumberToString(x);
536 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
537 if (IS_UNDEFINED(x)) return 'undefined';
538 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
539 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
545 return IS_SYMBOL(x) ? x : %ToString(x);
549 // ECMA-262, section 9.9, page 36.
550 function ToObject(x) {
551 if (IS_STRING(x)) return new $String(x);
552 if (IS_NUMBER(x)) return new $Number(x);
553 if (IS_BOOLEAN(x)) return new $Boolean(x);
554 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x);
555 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
556 throw %MakeTypeError('undefined_or_null_to_object', []);
562 // ECMA-262, section 9.4, page 34.
563 function ToInteger(x) {
564 if (%_IsSmi(x)) return x;
565 return %NumberToInteger(ToNumber(x));
569 // ES6, draft 08-24-14, section 7.1.15
570 function ToLength(arg) {
571 arg = ToInteger(arg);
572 if (arg < 0) return 0;
573 return arg < $Number.MAX_SAFE_INTEGER ? arg : $Number.MAX_SAFE_INTEGER;
577 // ECMA-262, section 9.6, page 34.
578 function ToUint32(x) {
579 if (%_IsSmi(x) && x >= 0) return x;
580 return %NumberToJSUint32(ToNumber(x));
584 // ECMA-262, section 9.5, page 34
585 function ToInt32(x) {
586 if (%_IsSmi(x)) return x;
587 return %NumberToJSInt32(ToNumber(x));
592 function SameValue(x, y) {
593 if (typeof x != typeof y) return false;
595 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
596 // x is +0 and y is -0 or vice versa.
597 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
604 // ES6, section 7.2.4
605 function SameValueZero(x, y) {
606 if (typeof x != typeof y) return false;
608 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
614 /* ---------------------------------
615 - - - U t i l i t i e s - - -
616 ---------------------------------
619 // Returns if the given x is a primitive value - not an object or a
621 function IsPrimitive(x) {
622 // Even though the type of null is "object", null is still
623 // considered a primitive value. IS_SPEC_OBJECT handles this correctly
624 // (i.e., it will return false if x is null).
625 return !IS_SPEC_OBJECT(x);
629 // ES6, draft 10-14-14, section 22.1.3.1.1
630 function IsConcatSpreadable(O) {
631 if (!IS_SPEC_OBJECT(O)) return false;
632 var spreadable = O[symbolIsConcatSpreadable];
633 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O);
634 return ToBoolean(spreadable);
638 // ECMA-262, section 8.6.2.6, page 28.
639 function DefaultNumber(x) {
640 if (!IS_SYMBOL_WRAPPER(x)) {
641 var valueOf = x.valueOf;
642 if (IS_SPEC_FUNCTION(valueOf)) {
643 var v = %_CallFunction(x, valueOf);
644 if (%IsPrimitive(v)) return v;
647 var toString = x.toString;
648 if (IS_SPEC_FUNCTION(toString)) {
649 var s = %_CallFunction(x, toString);
650 if (%IsPrimitive(s)) return s;
653 throw %MakeTypeError('cannot_convert_to_primitive', []);
656 // ECMA-262, section 8.6.2.6, page 28.
657 function DefaultString(x) {
658 if (!IS_SYMBOL_WRAPPER(x)) {
659 var toString = x.toString;
660 if (IS_SPEC_FUNCTION(toString)) {
661 var s = %_CallFunction(x, toString);
662 if (%IsPrimitive(s)) return s;
665 var valueOf = x.valueOf;
666 if (IS_SPEC_FUNCTION(valueOf)) {
667 var v = %_CallFunction(x, valueOf);
668 if (%IsPrimitive(v)) return v;
671 throw %MakeTypeError('cannot_convert_to_primitive', []);
674 function ToPositiveInteger(x, rangeErrorName) {
675 var i = TO_INTEGER(x);
676 if (i < 0) throw MakeRangeError(rangeErrorName);
681 // NOTE: Setting the prototype for Array must take place as early as
682 // possible due to code generation for array literals. When
683 // generating code for a array literal a boilerplate array is created
684 // that is cloned when running the code. It is essential that the
685 // boilerplate gets the right prototype.
686 %FunctionSetPrototype($Array, new $Array(0));