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