• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5// This files contains runtime support implemented in JavaScript.
6
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.
12
13
14/* -----------------------------------
15   - - -   C o m p a r i s o n   - - -
16   -----------------------------------
17*/
18
19// The following declarations are shared with other native JS files.
20// They are all declared at this one spot to avoid redeclaration errors.
21var $Object = global.Object;
22var $Array = global.Array;
23var $String = global.String;
24var $Number = global.Number;
25var $Function = global.Function;
26var $Boolean = global.Boolean;
27var $NaN = %GetRootNaN();
28
29// ECMA-262 Section 11.9.3.
30function EQUALS(y) {
31  if (IS_STRING(this) && IS_STRING(y)) return %StringEquals(this, y);
32  var x = this;
33
34  while (true) {
35    if (IS_NUMBER(x)) {
36      while (true) {
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)) {
40          // String or boolean.
41          return %NumberEquals(x, %ToNumber(y));
42        }
43        y = %ToPrimitive(y, NO_HINT);
44      }
45    } else if (IS_STRING(x)) {
46      while (true) {
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);
53      }
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
63      // y is object.
64      x = %ToNumber(x);
65      y = %ToPrimitive(y, NO_HINT);
66    } else if (IS_NULL_OR_UNDEFINED(x)) {
67      return IS_NULL_OR_UNDEFINED(y) ? 0 : 1;
68    } else {
69      // x is an object.
70      if (IS_SPEC_OBJECT(y)) {
71        return %_ObjectEquals(x, y) ? 0 : 1;
72      }
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);
77    }
78  }
79}
80
81// ECMA-262, section 11.9.4, page 56.
82function STRICT_EQUALS(x) {
83  if (IS_STRING(this)) {
84    if (!IS_STRING(x)) return 1;  // not equal
85    return %StringEquals(this, x);
86  }
87
88  if (IS_NUMBER(this)) {
89    if (!IS_NUMBER(x)) return 1;  // not equal
90    return %NumberEquals(this, x);
91  }
92
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;
97}
98
99
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.
102function COMPARE(x, ncr) {
103  var left;
104  var right;
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;
109    left = this;
110  } else if (IS_NUMBER(this)) {
111    if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
112    if (IS_UNDEFINED(x)) return ncr;
113    left = this;
114  } else if (IS_UNDEFINED(this)) {
115    if (!IS_UNDEFINED(x)) {
116      %ToPrimitive(x, NUMBER_HINT);
117    }
118    return ncr;
119  } else if (IS_UNDEFINED(x)) {
120    %ToPrimitive(this, NUMBER_HINT);
121    return ncr;
122  } else {
123    left = %ToPrimitive(this, NUMBER_HINT);
124  }
125
126  right = %ToPrimitive(x, NUMBER_HINT);
127  if (IS_STRING(left) && IS_STRING(right)) {
128    return %_StringCompare(left, right);
129  } else {
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);
134  }
135}
136
137
138
139/* -----------------------------------
140   - - -   A r i t h m e t i c   - - -
141   -----------------------------------
142*/
143
144// ECMA-262, section 11.6.1, page 50.
145function ADD(x) {
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);
149
150  // Default implementation.
151  var a = %ToPrimitive(this, NO_HINT);
152  var b = %ToPrimitive(x, NO_HINT);
153
154  if (IS_STRING(a)) {
155    return %_StringAdd(a, %ToString(b));
156  } else if (IS_STRING(b)) {
157    return %_StringAdd(%NonStringToString(a), b);
158  } else {
159    return %NumberAdd(%ToNumber(a), %ToNumber(b));
160  }
161}
162
163
164// Left operand (this) is already a string.
165function STRING_ADD_LEFT(y) {
166  if (!IS_STRING(y)) {
167    if (IS_STRING_WRAPPER(y) && %_IsStringWrapperSafeForDefaultValueOf(y)) {
168      y = %_ValueOf(y);
169    } else {
170      y = IS_NUMBER(y)
171          ? %_NumberToString(y)
172          : %ToString(%ToPrimitive(y, NO_HINT));
173    }
174  }
175  return %_StringAdd(this, y);
176}
177
178
179// Right operand (y) is already a string.
180function STRING_ADD_RIGHT(y) {
181  var x = this;
182  if (!IS_STRING(x)) {
183    if (IS_STRING_WRAPPER(x) && %_IsStringWrapperSafeForDefaultValueOf(x)) {
184      x = %_ValueOf(x);
185    } else {
186      x = IS_NUMBER(x)
187          ? %_NumberToString(x)
188          : %ToString(%ToPrimitive(x, NO_HINT));
189    }
190  }
191  return %_StringAdd(x, y);
192}
193
194
195// ECMA-262, section 11.6.2, page 50.
196function SUB(y) {
197  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
198  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
199  return %NumberSub(x, y);
200}
201
202
203// ECMA-262, section 11.5.1, page 48.
204function MUL(y) {
205  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
206  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
207  return %NumberMul(x, y);
208}
209
210
211// ECMA-262, section 11.5.2, page 49.
212function DIV(y) {
213  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
214  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
215  return %NumberDiv(x, y);
216}
217
218
219// ECMA-262, section 11.5.3, page 49.
220function MOD(y) {
221  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
222  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
223  return %NumberMod(x, y);
224}
225
226
227
228/* -------------------------------------------
229   - - -   B i t   o p e r a t i o n s   - - -
230   -------------------------------------------
231*/
232
233// ECMA-262, section 11.10, page 57.
234function BIT_OR(y) {
235  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
236  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
237  return %NumberOr(x, y);
238}
239
240
241// ECMA-262, section 11.10, page 57.
242function BIT_AND(y) {
243  var x;
244  if (IS_NUMBER(this)) {
245    x = this;
246    if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
247  } else {
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;
258  }
259  return %NumberAnd(x, y);
260}
261
262
263// ECMA-262, section 11.10, page 57.
264function 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);
268}
269
270
271// ECMA-262, section 11.7.1, page 51.
272function SHL(y) {
273  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
274  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
275  return %NumberShl(x, y);
276}
277
278
279// ECMA-262, section 11.7.2, page 51.
280function SAR(y) {
281  var x;
282  if (IS_NUMBER(this)) {
283    x = this;
284    if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
285  } else {
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;
296  }
297  return %NumberSar(x, y);
298}
299
300
301// ECMA-262, section 11.7.3, page 52.
302function SHR(y) {
303  var x = IS_NUMBER(this) ? this : %NonNumberToNumber(this);
304  if (!IS_NUMBER(y)) y = %NonNumberToNumber(y);
305  return %NumberShr(x, y);
306}
307
308
309
310/* -----------------------------
311   - - -   H e l p e r s   - - -
312   -----------------------------
313*/
314
315// ECMA-262, section 11.4.1, page 46.
316function DELETE(key, strict) {
317  return %DeleteProperty(%ToObject(this), %ToName(key), strict);
318}
319
320
321// ECMA-262, section 11.8.7, page 54.
322function IN(x) {
323  if (!IS_SPEC_OBJECT(x)) {
324    throw %MakeTypeError('invalid_in_operator_use', [this, x]);
325  }
326  return %_IsNonNegativeSmi(this) ?
327    %HasElement(x, this) : %HasProperty(x, %ToName(this));
328}
329
330
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.
335function INSTANCE_OF(F) {
336  var V = this;
337  if (!IS_SPEC_FUNCTION(F)) {
338    throw %MakeTypeError('instanceof_function_expected', [F]);
339  }
340
341  // If V is not an object, return false.
342  if (!IS_SPEC_OBJECT(V)) {
343    return 1;
344  }
345
346  // Check if function is bound, if so, get [[BoundFunction]] from it
347  // and use that instead of F.
348  var bindings = %BoundFunctionGetBindings(F);
349  if (bindings) {
350    F = bindings[kBoundFunctionIndex];  // Always a non-bound function.
351  }
352  // Get the prototype of F; if it is not an object, throw an error.
353  var O = F.prototype;
354  if (!IS_SPEC_OBJECT(O)) {
355    throw %MakeTypeError('instanceof_nonobject_proto', [O]);
356  }
357
358  // Return whether or not O is in the prototype chain of V.
359  return %IsInPrototypeChain(O, V) ? 0 : 1;
360}
361
362
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.
366function FILTER_KEY(key) {
367  var string = %ToName(key);
368  if (%HasProperty(this, string)) return string;
369  return 0;
370}
371
372
373function CALL_NON_FUNCTION() {
374  var delegate = %GetFunctionDelegate(this);
375  if (!IS_FUNCTION(delegate)) {
376    throw %MakeTypeError('called_non_callable', [typeof this]);
377  }
378  return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
379}
380
381
382function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
383  var delegate = %GetConstructorDelegate(this);
384  if (!IS_FUNCTION(delegate)) {
385    throw %MakeTypeError('called_non_callable', [typeof this]);
386  }
387  return %Apply(delegate, this, arguments, 0, %_ArgumentsLength());
388}
389
390
391function 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);
396}
397
398
399function CALL_FUNCTION_PROXY_AS_CONSTRUCTOR() {
400  var proxy = this;
401  var trap = %GetConstructTrap(proxy);
402  return %Apply(trap, this, arguments, 0, %_ArgumentsLength());
403}
404
405
406function APPLY_PREPARE(args) {
407  var length;
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)) {
415      return length;
416    }
417  }
418
419  length = (args == null) ? 0 : %ToUint32(args.length);
420
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', []);
426  }
427
428  if (!IS_SPEC_FUNCTION(this)) {
429    throw %MakeTypeError('apply_non_function',
430                         [ %ToString(this), typeof this ]);
431  }
432
433  // Make sure the arguments list has the right type.
434  if (args != null && !IS_SPEC_OBJECT(args)) {
435    throw %MakeTypeError('apply_wrong_args', []);
436  }
437
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.
440  return length;
441}
442
443
444function STACK_OVERFLOW(length) {
445  throw %MakeRangeError('stack_overflow', []);
446}
447
448
449// Convert the receiver to an object - forward to ToObject.
450function TO_OBJECT() {
451  return %ToObject(this);
452}
453
454
455// Convert the receiver to a number - forward to ToNumber.
456function TO_NUMBER() {
457  return %ToNumber(this);
458}
459
460
461// Convert the receiver to a string - forward to ToString.
462function TO_STRING() {
463  return %ToString(this);
464}
465
466
467/* -------------------------------------
468   - - -   C o n v e r s i o n s   - - -
469   -------------------------------------
470*/
471
472// ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
473// (1) for number hint, and (2) for string hint.
474function ToPrimitive(x, hint) {
475  // Fast case check.
476  if (IS_STRING(x)) return x;
477  // Normal behavior.
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);
482}
483
484
485// ECMA-262, section 9.2, page 30
486function 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));
491  return true;
492}
493
494
495// ECMA-262, section 9.3, page 31.
496function ToNumber(x) {
497  if (IS_NUMBER(x)) return x;
498  if (IS_STRING(x)) {
499    return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
500                                    : %StringToNumber(x);
501  }
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));
506}
507
508function NonNumberToNumber(x) {
509  if (IS_STRING(x)) {
510    return %_HasCachedArrayIndex(x) ? %_GetCachedArrayIndex(x)
511                                    : %StringToNumber(x);
512  }
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));
517}
518
519
520// ECMA-262, section 9.8, page 35.
521function 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));
528}
529
530function 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));
536}
537
538
539// ES6 symbols
540function ToName(x) {
541  return IS_SYMBOL(x) ? x : %ToString(x);
542}
543
544
545// ECMA-262, section 9.9, page 36.
546function 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', []);
553  }
554  return x;
555}
556
557
558// ECMA-262, section 9.4, page 34.
559function ToInteger(x) {
560  if (%_IsSmi(x)) return x;
561  return %NumberToInteger(ToNumber(x));
562}
563
564
565// ECMA-262, section 9.6, page 34.
566function ToUint32(x) {
567  if (%_IsSmi(x) && x >= 0) return x;
568  return %NumberToJSUint32(ToNumber(x));
569}
570
571
572// ECMA-262, section 9.5, page 34
573function ToInt32(x) {
574  if (%_IsSmi(x)) return x;
575  return %NumberToJSInt32(ToNumber(x));
576}
577
578
579// ES5, section 9.12
580function SameValue(x, y) {
581  if (typeof x != typeof y) return false;
582  if (IS_NUMBER(x)) {
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)) {
586      return false;
587    }
588  }
589  return x === y;
590}
591
592
593/* ---------------------------------
594   - - -   U t i l i t i e s   - - -
595   ---------------------------------
596*/
597
598// Returns if the given x is a primitive value - not an object or a
599// function.
600function 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);
605}
606
607
608// ECMA-262, section 8.6.2.6, page 28.
609function 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;
614  }
615
616  var toString = x.toString;
617  if (IS_SPEC_FUNCTION(toString)) {
618    var s = %_CallFunction(x, toString);
619    if (%IsPrimitive(s)) return s;
620  }
621
622  throw %MakeTypeError('cannot_convert_to_primitive', []);
623}
624
625// ECMA-262, section 8.6.2.6, page 28.
626function 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;
631  }
632
633  var valueOf = x.valueOf;
634  if (IS_SPEC_FUNCTION(valueOf)) {
635    var v = %_CallFunction(x, valueOf);
636    if (%IsPrimitive(v)) return v;
637  }
638
639  throw %MakeTypeError('cannot_convert_to_primitive', []);
640}
641
642function ToPositiveInteger(x, rangeErrorName) {
643  var i = TO_INTEGER(x);
644  if (i < 0) throw MakeRangeError(rangeErrorName);
645  return i;
646}
647
648
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));
655