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_SYMBOL(y)) return 1; // not equal
40 if (!IS_SPEC_OBJECT(y)) {
42 return %NumberEquals(x, %ToNumber(y));
44 y = %ToPrimitive(y, NO_HINT);
46 } else if (IS_STRING(x)) {
48 if (IS_STRING(y)) return %StringEquals(x, y);
49 if (IS_SYMBOL(y)) return 1; // not equal
50 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
51 if (IS_BOOLEAN(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
52 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
53 y = %ToPrimitive(y, NO_HINT);
55 } else if (IS_SYMBOL(x)) {
56 if (IS_SYMBOL(y)) return %_ObjectEquals(x, y) ? 0 : 1;
57 return 1; // not equal
58 } else if (IS_BOOLEAN(x)) {
59 if (IS_BOOLEAN(y)) return %_ObjectEquals(x, y) ? 0 : 1;
60 if (IS_NULL_OR_UNDEFINED(y)) return 1;
61 if (IS_NUMBER(y)) return %NumberEquals(%ToNumber(x), y);
62 if (IS_STRING(y)) return %NumberEquals(%ToNumber(x), %ToNumber(y));
63 if (IS_SYMBOL(y)) return 1; // not equal
66 y = %ToPrimitive(y, NO_HINT);
67 } else if (IS_NULL_OR_UNDEFINED(x)) {
68 return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
71 if (IS_SPEC_OBJECT(y)) {
72 return %_ObjectEquals(x, y) ? 0 : 1;
74 if (IS_NULL_OR_UNDEFINED(y)) return 1; // not equal
75 if (IS_SYMBOL(y)) return 1; // not equal
76 if (IS_BOOLEAN(y)) y = %ToNumber(y);
77 x = %ToPrimitive(x, NO_HINT);
82 // ECMA-262, section 11.9.4, page 56.
83 function STRICT_EQUALS(x) {
84 if (IS_STRING(this)) {
85 if (!IS_STRING(x)) return 1; // not equal
86 return %StringEquals(this, x);
89 if (IS_NUMBER(this)) {
90 if (!IS_NUMBER(x)) return 1; // not equal
91 return %NumberEquals(this, x);
94 // If anything else gets here, we just do simple identity check.
95 // Objects (including functions), null, undefined and booleans were
96 // checked in the CompareStub, so there should be nothing left.
97 return %_ObjectEquals(this, x) ? 0 : 1;
101 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
102 // the result when either (or both) the operands are NaN.
103 function COMPARE(x, ncr) {
106 // Fast cases for string, numbers and undefined compares.
107 if (IS_STRING(this)) {
108 if (IS_STRING(x)) return %_StringCompare(this, x);
109 if (IS_UNDEFINED(x)) return ncr;
111 } else if (IS_NUMBER(this)) {
112 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
113 if (IS_UNDEFINED(x)) return ncr;
115 } else if (IS_UNDEFINED(this)) {
116 if (!IS_UNDEFINED(x)) {
117 %ToPrimitive(x, NUMBER_HINT);
120 } else if (IS_UNDEFINED(x)) {
121 %ToPrimitive(this, NUMBER_HINT);
124 left = %ToPrimitive(this, NUMBER_HINT);
127 right = %ToPrimitive(x, NUMBER_HINT);
128 if (IS_STRING(left) && IS_STRING(right)) {
129 return %_StringCompare(left, right);
131 var left_number = %ToNumber(left);
132 var right_number = %ToNumber(right);
133 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
134 return %NumberCompare(left_number, right_number, ncr);
140 /* -----------------------------------
141 - - - A r i t h m e t i c - - -
142 -----------------------------------
145 // ECMA-262, section 11.6.1, page 50.
147 // Fast case: Check for number operands and do the addition.
148 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
149 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
151 // Default implementation.
152 var a = %ToPrimitive(this, NO_HINT);
153 var b = %ToPrimitive(x, NO_HINT);
156 return %_StringAdd(a, %ToString(b));
157 } else if (IS_STRING(b)) {
158 return %_StringAdd(%NonStringToString(a), b);
160 return %NumberAdd(%ToNumber(a), %ToNumber(b));
165 // Left operand (this) is already a string.
166 function STRING_ADD_LEFT(y) {
168 if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
172 ? %_NumberToString(y)
173 : %ToString(%ToPrimitive(y, NO_HINT));
176 return %_StringAdd(this, y);
180 // Right operand (y) is already a string.
181 function STRING_ADD_RIGHT(y) {
184 if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
188 ? %_NumberToString(x)
189 : %ToString(%ToPrimitive(x, NO_HINT));
192 return %_StringAdd(x, y);
196 // ECMA-262, section 11.6.2, page 50.
198 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
199 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
200 return %NumberSub(x, y);
204 // ECMA-262, section 11.5.1, page 48.
206 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
207 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
208 return %NumberMul(x, y);
212 // ECMA-262, section 11.5.2, page 49.
214 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
215 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
216 return %NumberDiv(x, y);
220 // ECMA-262, section 11.5.3, page 49.
222 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
223 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
224 return %NumberMod(x, y);
229 /* -------------------------------------------
230 - - - B i t o p e r a t i o n s - - -
231 -------------------------------------------
234 // ECMA-262, section 11.10, page 57.
236 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
237 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
238 return %NumberOr(x, y);
242 // ECMA-262, section 11.10, page 57.
243 function BIT_AND(y) {
245 if (IS_NUMBER(this)) {
247 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
249 x = %NonNumberToNumber(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 = %NonNumberToNumber(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;
260 return %NumberAnd(x, y);
264 // ECMA-262, section 11.10, page 57.
265 function BIT_XOR(y) {
266 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
267 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
268 return %NumberXor(x, y);
272 // ECMA-262, section 11.7.1, page 51.
274 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
275 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
276 return %NumberShl(x, y);
280 // ECMA-262, section 11.7.2, page 51.
283 if (IS_NUMBER(this)) {
285 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
287 x = %NonNumberToNumber(this);
288 // Make sure to convert the right operand to a number before
289 // bailing out in the fast case, but after converting the
290 // left operand. This ensures that valueOf methods on the right
291 // operand are always executed.
292 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
293 // Optimize for the case where we end up shifting a value
294 // that doesn't convert to a number. This is common in
295 // certain benchmarks.
296 if (NUMBER_IS_NAN(x)) return 0;
298 return %NumberSar(x, y);
302 // ECMA-262, section 11.7.3, page 52.
304 var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
305 if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
306 return %NumberShr(x, y);
311 /* -----------------------------
312 - - - H e l p e r s - - -
313 -----------------------------
316 // ECMA-262, section 11.4.1, page 46.
317 function DELETE(key, strict) {
318 return %DeleteProperty(%ToObject(this), %ToName(key), strict);
322 // ECMA-262, section 11.8.7, page 54.
324 if (!IS_SPEC_OBJECT(x)) {
325 throw %MakeTypeError('invalid_in_operator_use', [this, x]);
327 return %_IsNonNegativeSmi(this) ?
328 %HasElement(x, this) : %HasProperty(x, %ToName(this));
332 // ECMA-262, section 11.8.6, page 54. To make the implementation more
333 // efficient, the return value should be zero if the 'this' is an
334 // instance of F, and non-zero if not. This makes it possible to avoid
335 // an expensive ToBoolean conversion in the generated code.
336 function INSTANCE_OF(F) {
338 if (!IS_SPEC_FUNCTION(F)) {
339 throw %MakeTypeError('instanceof_function_expected', [F]);
342 // If V is not an object, return false.
343 if (!IS_SPEC_OBJECT(V)) {
347 // Check if function is bound, if so, get [[BoundFunction]] from it
348 // and use that instead of F.
349 var bindings = %BoundFunctionGetBindings(F);
351 F = bindings[kBoundFunctionIndex]; // Always a non-bound function.
353 // Get the prototype of F; if it is not an object, throw an error.
355 if (!IS_SPEC_OBJECT(O)) {
356 throw %MakeTypeError('instanceof_nonobject_proto', [O]);
359 // Return whether or not O is in the prototype chain of V.
360 return %IsInPrototypeChain(O, V) ? 0 : 1;
364 // Filter a given key against an object by checking if the object
365 // has a property with the given key; return the key as a string if
366 // it has. Otherwise returns 0 (smi). Used in for-in statements.
367 function FILTER_KEY(key) {
368 var string = %ToName(key);
369 if (%HasProperty(this, string)) return string;
374 function CALL_NON_FUNCTION() {
375 var delegate = %GetFunctionDelegate(this);
376 if (!IS_FUNCTION(delegate)) {
377 throw %MakeTypeError('called_non_callable', [typeof this]);
379 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
383 function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
384 var delegate = %GetConstructorDelegate(this);
385 if (!IS_FUNCTION(delegate)) {
386 throw %MakeTypeError('called_non_callable', [typeof this]);
388 return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
392 function CALL_FUNCTION_PROXY() {
393 var arity = %_ArgumentsLength() - 1;
394 var proxy = %_Arguments(arity); // The proxy comes in as an additional arg.
395 var trap = %GetCallTrap(proxy);
396 return %Apply(trap, this, arguments, 0, arity);
400 function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
402 var trap = %GetConstructTrap(proxy);
403 return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
407 function APPLY_PREPARE(args) {
409 // First check whether length is a positive Smi and args is an
410 // array. This is the fast case. If this fails, we do the slow case
411 // that takes care of more eventualities.
412 if (IS_ARRAY(args)) {
413 length = args.length;
414 if (%_IsSmi(length) && length >= 0 && length < 0x800000 &&
415 IS_SPEC_FUNCTION(this)) {
420 length = (args == null) ? 0 : %ToUint32(args.length);
422 // We can handle any number of apply arguments if the stack is
423 // big enough, but sanity check the value to avoid overflow when
424 // multiplying with pointer size.
425 if (length > 0x800000) {
426 throw %MakeRangeError('stack_overflow', []);
429 if (!IS_SPEC_FUNCTION(this)) {
430 throw %MakeTypeError('apply_non_function',
431 [ %ToString(this), typeof this ]);
434 // Make sure the arguments list has the right type.
435 if (args != null && !IS_SPEC_OBJECT(args)) {
436 throw %MakeTypeError('apply_wrong_args', []);
439 // Return the length which is the number of arguments to copy to the
440 // stack. It is guaranteed to be a small integer at this point.
445 function STACK_OVERFLOW(length) {
446 throw %MakeRangeError('stack_overflow', []);
450 // Convert the receiver to an object - forward to ToObject.
451 function TO_OBJECT() {
452 return %ToObject(this);
456 // Convert the receiver to a number - forward to ToNumber.
457 function TO_NUMBER() {
458 return %ToNumber(this);
462 // Convert the receiver to a string - forward to ToString.
463 function TO_STRING() {
464 return %ToString(this);
468 /* -------------------------------------
469 - - - C o n v e r s i o n s - - -
470 -------------------------------------
473 // ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
474 // (1) for number hint, and (2) for string hint.
475 function ToPrimitive(x, hint) {
477 if (IS_STRING(x)) return x;
479 if (!IS_SPEC_OBJECT(x)) return x;
480 if (IS_SYMBOL_WRAPPER(x)) throw MakeTypeError('symbol_to_primitive', []);
481 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
482 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
486 // ECMA-262, section 9.2, page 30
487 function ToBoolean(x) {
488 if (IS_BOOLEAN(x)) return x;
489 if (IS_STRING(x)) return x.length != 0;
490 if (x == null) return false;
491 if (IS_NUMBER(x)) return !((x == 0) || NUMBER_IS_NAN(x));
496 // ECMA-262, section 9.3, page 31.
497 function ToNumber(x) {
498 if (IS_NUMBER(x)) return x;
500 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
501 : %StringToNumber(x);
503 if (IS_BOOLEAN(x)) return x ? 1 : 0;
504 if (IS_UNDEFINED(x)) return NAN;
505 if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
506 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
509 function NonNumberToNumber(x) {
511 return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
512 : %StringToNumber(x);
514 if (IS_BOOLEAN(x)) return x ? 1 : 0;
515 if (IS_UNDEFINED(x)) return NAN;
516 if (IS_SYMBOL(x)) throw MakeTypeError('symbol_to_number', []);
517 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
521 // ECMA-262, section 9.8, page 35.
522 function ToString(x) {
523 if (IS_STRING(x)) return x;
524 if (IS_NUMBER(x)) return %_NumberToString(x);
525 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
526 if (IS_UNDEFINED(x)) return 'undefined';
527 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
528 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
531 function NonStringToString(x) {
532 if (IS_NUMBER(x)) return %_NumberToString(x);
533 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
534 if (IS_UNDEFINED(x)) return 'undefined';
535 if (IS_SYMBOL(x)) throw %MakeTypeError('symbol_to_string', []);
536 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
542 return IS_SYMBOL(x) ? x : %ToString(x);
546 // ECMA-262, section 9.9, page 36.
547 function ToObject(x) {
548 if (IS_STRING(x)) return new $String(x);
549 if (IS_NUMBER(x)) return new $Number(x);
550 if (IS_BOOLEAN(x)) return new $Boolean(x);
551 if (IS_SYMBOL(x)) return %NewSymbolWrapper(x);
552 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
553 throw %MakeTypeError('undefined_or_null_to_object', []);
559 // ECMA-262, section 9.4, page 34.
560 function ToInteger(x) {
561 if (%_IsSmi(x)) return x;
562 return %NumberToInteger(ToNumber(x));
566 // ES6, draft 08-24-14, section 7.1.15
567 function ToLength(arg) {
568 arg = ToInteger(arg);
569 if (arg < 0) return 0;
570 return arg < $Number.MAX_SAFE_INTEGER ? arg : $Number.MAX_SAFE_INTEGER;
574 // ECMA-262, section 9.6, page 34.
575 function ToUint32(x) {
576 if (%_IsSmi(x) && x >= 0) return x;
577 return %NumberToJSUint32(ToNumber(x));
581 // ECMA-262, section 9.5, page 34
582 function ToInt32(x) {
583 if (%_IsSmi(x)) return x;
584 return %NumberToJSInt32(ToNumber(x));
589 function SameValue(x, y) {
590 if (typeof x != typeof y) return false;
592 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
593 // x is +0 and y is -0 or vice versa.
594 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) {
602 /* ---------------------------------
603 - - - U t i l i t i e s - - -
604 ---------------------------------
607 // Returns if the given x is a primitive value - not an object or a
609 function IsPrimitive(x) {
610 // Even though the type of null is "object", null is still
611 // considered a primitive value. IS_SPEC_OBJECT handles this correctly
612 // (i.e., it will return false if x is null).
613 return !IS_SPEC_OBJECT(x);
617 // ECMA-262, section 8.6.2.6, page 28.
618 function DefaultNumber(x) {
619 if (!IS_SYMBOL_WRAPPER(x)) {
620 var valueOf = x.valueOf;
621 if (IS_SPEC_FUNCTION(valueOf)) {
622 var v = %_CallFunction(x, valueOf);
623 if (%IsPrimitive(v)) return v;
626 var toString = x.toString;
627 if (IS_SPEC_FUNCTION(toString)) {
628 var s = %_CallFunction(x, toString);
629 if (%IsPrimitive(s)) return s;
632 throw %MakeTypeError('cannot_convert_to_primitive', []);
635 // ECMA-262, section 8.6.2.6, page 28.
636 function DefaultString(x) {
637 if (!IS_SYMBOL_WRAPPER(x)) {
638 var toString = x.toString;
639 if (IS_SPEC_FUNCTION(toString)) {
640 var s = %_CallFunction(x, toString);
641 if (%IsPrimitive(s)) return s;
644 var valueOf = x.valueOf;
645 if (IS_SPEC_FUNCTION(valueOf)) {
646 var v = %_CallFunction(x, valueOf);
647 if (%IsPrimitive(v)) return v;
650 throw %MakeTypeError('cannot_convert_to_primitive', []);
653 function ToPositiveInteger(x, rangeErrorName) {
654 var i = TO_INTEGER(x);
655 if (i < 0) throw MakeRangeError(rangeErrorName);
660 // NOTE: Setting the prototype for Array must take place as early as
661 // possible due to code generation for array literals. When
662 // generating code for a array literal a boilerplate array is created
663 // that is cloned when running the code. It is essential that the
664 // boilerplate gets the right prototype.
665 %FunctionSetPrototype($Array, new $Array(0));