blob: 3e4d473c27f6c55f369888e0fb2b82e59bef99d8 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
Kristian Monsen25f61362010-05-21 11:50:48 +010083 if (IS_SPEC_OBJECT_OR_NULL(y)) {
Steve Blocka7e24c12009-10-30 11:49:00 +000084 return %_ObjectEquals(x, y) ? 0 : 1;
85 }
86
87 x = %ToPrimitive(x, NO_HINT);
88 }
89 }
90}
91
92// ECMA-262, section 11.9.4, page 56.
93function STRICT_EQUALS(x) {
94 if (IS_STRING(this)) {
95 if (!IS_STRING(x)) return 1; // not equal
96 return %StringEquals(this, x);
97 }
98
99 if (IS_NUMBER(this)) {
100 if (!IS_NUMBER(x)) return 1; // not equal
101 return %NumberEquals(this, x);
102 }
103
104 // If anything else gets here, we just do simple identity check.
105 // Objects (including functions), null, undefined and booleans were
106 // checked in the CompareStub, so there should be nothing left.
107 return %_ObjectEquals(this, x) ? 0 : 1;
108}
109
110
111// ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
112// the result when either (or both) the operands are NaN.
113function COMPARE(x, ncr) {
Leon Clarkee46be812010-01-19 14:06:41 +0000114 var left;
115
116 // Fast cases for string, numbers and undefined compares.
117 if (IS_STRING(this)) {
118 if (IS_STRING(x)) return %_StringCompare(this, x);
119 if (IS_UNDEFINED(x)) return ncr;
120 left = this;
121 } else if (IS_NUMBER(this)) {
122 if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
123 if (IS_UNDEFINED(x)) return ncr;
124 left = this;
125 } else if (IS_UNDEFINED(this)) {
126 return ncr;
127 } else {
128 if (IS_UNDEFINED(x)) return ncr;
129 left = %ToPrimitive(this, NUMBER_HINT);
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 }
131
132 // Default implementation.
Leon Clarkee46be812010-01-19 14:06:41 +0000133 var right = %ToPrimitive(x, NUMBER_HINT);
134 if (IS_STRING(left) && IS_STRING(right)) {
135 return %_StringCompare(left, right);
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 } else {
Leon Clarkee46be812010-01-19 14:06:41 +0000137 var left_number = %ToNumber(left);
138 var right_number = %ToNumber(right);
139 if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
140 return %NumberCompare(left_number, right_number, ncr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000141 }
142}
143
144
145
146/* -----------------------------------
147 - - - A r i t h m e t i c - - -
148 -----------------------------------
149*/
150
151// ECMA-262, section 11.6.1, page 50.
152function ADD(x) {
153 // Fast case: Check for number operands and do the addition.
154 if (IS_NUMBER(this) && IS_NUMBER(x)) return %NumberAdd(this, x);
Steve Blockd0582a62009-12-15 09:54:21 +0000155 if (IS_STRING(this) && IS_STRING(x)) return %_StringAdd(this, x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000156
157 // Default implementation.
158 var a = %ToPrimitive(this, NO_HINT);
159 var b = %ToPrimitive(x, NO_HINT);
160
161 if (IS_STRING(a)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000162 return %_StringAdd(a, %ToString(b));
Steve Blocka7e24c12009-10-30 11:49:00 +0000163 } else if (IS_STRING(b)) {
Steve Blockd0582a62009-12-15 09:54:21 +0000164 return %_StringAdd(%ToString(a), b);
Steve Blocka7e24c12009-10-30 11:49:00 +0000165 } else {
166 return %NumberAdd(%ToNumber(a), %ToNumber(b));
167 }
168}
169
170
171// Left operand (this) is already a string.
172function STRING_ADD_LEFT(y) {
173 if (!IS_STRING(y)) {
174 if (IS_STRING_WRAPPER(y)) {
175 y = %_ValueOf(y);
176 } else {
177 y = IS_NUMBER(y)
Andrei Popescu402d9372010-02-26 13:31:12 +0000178 ? %_NumberToString(y)
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 : %ToString(%ToPrimitive(y, NO_HINT));
180 }
181 }
Steve Blockd0582a62009-12-15 09:54:21 +0000182 return %_StringAdd(this, y);
Steve Blocka7e24c12009-10-30 11:49:00 +0000183}
184
185
186// Right operand (y) is already a string.
187function STRING_ADD_RIGHT(y) {
188 var x = this;
189 if (!IS_STRING(x)) {
190 if (IS_STRING_WRAPPER(x)) {
191 x = %_ValueOf(x);
192 } else {
193 x = IS_NUMBER(x)
Andrei Popescu402d9372010-02-26 13:31:12 +0000194 ? %_NumberToString(x)
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 : %ToString(%ToPrimitive(x, NO_HINT));
196 }
197 }
Steve Blockd0582a62009-12-15 09:54:21 +0000198 return %_StringAdd(x, y);
Steve Blocka7e24c12009-10-30 11:49:00 +0000199}
200
201
202// ECMA-262, section 11.6.2, page 50.
203function SUB(y) {
204 var x = IS_NUMBER(this) ? this : %ToNumber(this);
205 if (!IS_NUMBER(y)) y = %ToNumber(y);
206 return %NumberSub(x, y);
207}
208
209
210// ECMA-262, section 11.5.1, page 48.
211function MUL(y) {
212 var x = IS_NUMBER(this) ? this : %ToNumber(this);
213 if (!IS_NUMBER(y)) y = %ToNumber(y);
214 return %NumberMul(x, y);
215}
216
217
218// ECMA-262, section 11.5.2, page 49.
219function DIV(y) {
220 var x = IS_NUMBER(this) ? this : %ToNumber(this);
221 if (!IS_NUMBER(y)) y = %ToNumber(y);
222 return %NumberDiv(x, y);
223}
224
225
226// ECMA-262, section 11.5.3, page 49.
227function MOD(y) {
228 var x = IS_NUMBER(this) ? this : %ToNumber(this);
229 if (!IS_NUMBER(y)) y = %ToNumber(y);
230 return %NumberMod(x, y);
231}
232
233
234
235/* -------------------------------------------
236 - - - B i t o p e r a t i o n s - - -
237 -------------------------------------------
238*/
239
240// ECMA-262, section 11.10, page 57.
241function BIT_OR(y) {
242 var x = IS_NUMBER(this) ? this : %ToNumber(this);
243 if (!IS_NUMBER(y)) y = %ToNumber(y);
244 return %NumberOr(x, y);
245}
246
247
248// ECMA-262, section 11.10, page 57.
249function BIT_AND(y) {
250 var x;
251 if (IS_NUMBER(this)) {
252 x = this;
253 if (!IS_NUMBER(y)) y = %ToNumber(y);
254 } else {
255 x = %ToNumber(this);
256 // Make sure to convert the right operand to a number before
257 // bailing out in the fast case, but after converting the
258 // left operand. This ensures that valueOf methods on the right
259 // operand are always executed.
260 if (!IS_NUMBER(y)) y = %ToNumber(y);
261 // Optimize for the case where we end up AND'ing a value
262 // that doesn't convert to a number. This is common in
263 // certain benchmarks.
264 if (NUMBER_IS_NAN(x)) return 0;
265 }
266 return %NumberAnd(x, y);
267}
268
269
270// ECMA-262, section 11.10, page 57.
271function BIT_XOR(y) {
272 var x = IS_NUMBER(this) ? this : %ToNumber(this);
273 if (!IS_NUMBER(y)) y = %ToNumber(y);
274 return %NumberXor(x, y);
275}
276
277
278// ECMA-262, section 11.4.7, page 47.
279function UNARY_MINUS() {
280 var x = IS_NUMBER(this) ? this : %ToNumber(this);
281 return %NumberUnaryMinus(x);
282}
283
284
285// ECMA-262, section 11.4.8, page 48.
286function BIT_NOT() {
287 var x = IS_NUMBER(this) ? this : %ToNumber(this);
288 return %NumberNot(x);
289}
290
291
292// ECMA-262, section 11.7.1, page 51.
293function SHL(y) {
294 var x = IS_NUMBER(this) ? this : %ToNumber(this);
295 if (!IS_NUMBER(y)) y = %ToNumber(y);
296 return %NumberShl(x, y);
297}
298
299
300// ECMA-262, section 11.7.2, page 51.
301function SAR(y) {
302 var x;
303 if (IS_NUMBER(this)) {
304 x = this;
305 if (!IS_NUMBER(y)) y = %ToNumber(y);
306 } else {
307 x = %ToNumber(this);
308 // Make sure to convert the right operand to a number before
309 // bailing out in the fast case, but after converting the
310 // left operand. This ensures that valueOf methods on the right
311 // operand are always executed.
312 if (!IS_NUMBER(y)) y = %ToNumber(y);
313 // Optimize for the case where we end up shifting a value
314 // that doesn't convert to a number. This is common in
315 // certain benchmarks.
316 if (NUMBER_IS_NAN(x)) return 0;
317 }
318 return %NumberSar(x, y);
319}
320
321
322// ECMA-262, section 11.7.3, page 52.
323function SHR(y) {
324 var x = IS_NUMBER(this) ? this : %ToNumber(this);
325 if (!IS_NUMBER(y)) y = %ToNumber(y);
326 return %NumberShr(x, y);
327}
328
329
330
331/* -----------------------------
332 - - - H e l p e r s - - -
333 -----------------------------
334*/
335
336// ECMA-262, section 11.4.1, page 46.
337function DELETE(key) {
338 return %DeleteProperty(%ToObject(this), %ToString(key));
339}
340
341
342// ECMA-262, section 11.8.7, page 54.
343function IN(x) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100344 if (x == null || !IS_SPEC_OBJECT_OR_NULL(x)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 throw %MakeTypeError('invalid_in_operator_use', [this, x]);
346 }
347 return %_IsNonNegativeSmi(this) ? %HasElement(x, this) : %HasProperty(x, %ToString(this));
348}
349
350
351// ECMA-262, section 11.8.6, page 54. To make the implementation more
352// efficient, the return value should be zero if the 'this' is an
353// instance of F, and non-zero if not. This makes it possible to avoid
354// an expensive ToBoolean conversion in the generated code.
355function INSTANCE_OF(F) {
356 var V = this;
357 if (!IS_FUNCTION(F)) {
358 throw %MakeTypeError('instanceof_function_expected', [V]);
359 }
360
361 // If V is not an object, return false.
Kristian Monsen25f61362010-05-21 11:50:48 +0100362 if (IS_NULL(V) || !IS_SPEC_OBJECT_OR_NULL(V)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000363 return 1;
364 }
365
366 // Get the prototype of F; if it is not an object, throw an error.
367 var O = F.prototype;
Kristian Monsen25f61362010-05-21 11:50:48 +0100368 if (IS_NULL(O) || !IS_SPEC_OBJECT_OR_NULL(O)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000369 throw %MakeTypeError('instanceof_nonobject_proto', [O]);
370 }
371
372 // Return whether or not O is in the prototype chain of V.
373 return %IsInPrototypeChain(O, V) ? 0 : 1;
374}
375
376
377// Get an array of property keys for the given object. Used in
378// for-in statements.
379function GET_KEYS() {
380 return %GetPropertyNames(this);
381}
382
383
384// Filter a given key against an object by checking if the object
385// has a property with the given key; return the key as a string if
386// it has. Otherwise returns null. Used in for-in statements.
387function FILTER_KEY(key) {
388 var string = %ToString(key);
389 if (%HasProperty(this, string)) return string;
390 return null;
391}
392
393
394function CALL_NON_FUNCTION() {
Andrei Popescu402d9372010-02-26 13:31:12 +0000395 var delegate = %GetFunctionDelegate(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000396 if (!IS_FUNCTION(delegate)) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000397 throw %MakeTypeError('called_non_callable', [typeof this]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000398 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000399 return delegate.apply(this, arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000400}
401
402
403function CALL_NON_FUNCTION_AS_CONSTRUCTOR() {
Andrei Popescu402d9372010-02-26 13:31:12 +0000404 var delegate = %GetConstructorDelegate(this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 if (!IS_FUNCTION(delegate)) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000406 throw %MakeTypeError('called_non_callable', [typeof this]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000408 return delegate.apply(this, arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000409}
410
411
412function APPLY_PREPARE(args) {
413 var length;
414 // First check whether length is a positive Smi and args is an
415 // array. This is the fast case. If this fails, we do the slow case
416 // that takes care of more eventualities.
417 if (IS_ARRAY(args)) {
418 length = args.length;
419 if (%_IsSmi(length) && length >= 0 && length < 0x800000 && IS_FUNCTION(this)) {
420 return length;
421 }
422 }
423
424 length = (args == null) ? 0 : %ToUint32(args.length);
425
426 // We can handle any number of apply arguments if the stack is
427 // big enough, but sanity check the value to avoid overflow when
428 // multiplying with pointer size.
429 if (length > 0x800000) {
430 throw %MakeRangeError('apply_overflow', [length]);
431 }
432
433 if (!IS_FUNCTION(this)) {
434 throw %MakeTypeError('apply_non_function', [ %ToString(this), typeof this ]);
435 }
436
437 // Make sure the arguments list has the right type.
438 if (args != null && !IS_ARRAY(args) && !IS_ARGUMENTS(args)) {
439 throw %MakeTypeError('apply_wrong_args', []);
440 }
441
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.
444 return length;
445}
446
447
448function APPLY_OVERFLOW(length) {
449 throw %MakeRangeError('apply_overflow', [length]);
450}
451
452
453// Convert the receiver to an object - forward to ToObject.
454function TO_OBJECT() {
455 return %ToObject(this);
456}
457
458
459// Convert the receiver to a number - forward to ToNumber.
460function TO_NUMBER() {
461 return %ToNumber(this);
462}
463
464
465// Convert the receiver to a string - forward to ToString.
466function TO_STRING() {
467 return %ToString(this);
468}
469
470
Steve Blocka7e24c12009-10-30 11:49:00 +0000471/* -------------------------------------
472 - - - C o n v e r s i o n s - - -
473 -------------------------------------
474*/
475
476// ECMA-262, section 9.1, page 30. Use null/undefined for no hint,
477// (1) for number hint, and (2) for string hint.
478function ToPrimitive(x, hint) {
479 // Fast case check.
480 if (IS_STRING(x)) return x;
481 // Normal behavior.
Kristian Monsen25f61362010-05-21 11:50:48 +0100482 if (!IS_SPEC_OBJECT_OR_NULL(x)) return x;
Steve Blocka7e24c12009-10-30 11:49:00 +0000483 if (x == null) return x; // check for null, undefined
484 if (hint == NO_HINT) hint = (IS_DATE(x)) ? STRING_HINT : NUMBER_HINT;
485 return (hint == NUMBER_HINT) ? %DefaultNumber(x) : %DefaultString(x);
486}
487
488
Andrei Popescu31002712010-02-23 13:46:05 +0000489// ECMA-262, section 9.2, page 30
490function 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));
495 return true;
496}
497
498
Steve Blocka7e24c12009-10-30 11:49:00 +0000499// ECMA-262, section 9.3, page 31.
500function ToNumber(x) {
501 if (IS_NUMBER(x)) return x;
502 if (IS_STRING(x)) return %StringToNumber(x);
503 if (IS_BOOLEAN(x)) return x ? 1 : 0;
504 if (IS_UNDEFINED(x)) return $NaN;
505 return (IS_NULL(x)) ? 0 : ToNumber(%DefaultNumber(x));
506}
507
508
509// ECMA-262, section 9.8, page 35.
510function ToString(x) {
511 if (IS_STRING(x)) return x;
Andrei Popescu402d9372010-02-26 13:31:12 +0000512 if (IS_NUMBER(x)) return %_NumberToString(x);
513 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
514 if (IS_UNDEFINED(x)) return 'undefined';
515 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
516}
517
518function NonStringToString(x) {
Steve Block6ded16b2010-05-10 14:33:55 +0100519 if (IS_NUMBER(x)) return %_NumberToString(x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000520 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
521 if (IS_UNDEFINED(x)) return 'undefined';
522 return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
523}
524
525
Steve Blocka7e24c12009-10-30 11:49:00 +0000526// ECMA-262, section 9.9, page 36.
527function ToObject(x) {
528 if (IS_STRING(x)) return new $String(x);
529 if (IS_NUMBER(x)) return new $Number(x);
530 if (IS_BOOLEAN(x)) return new $Boolean(x);
Leon Clarked91b9f72010-01-27 17:25:45 +0000531 if (IS_NULL_OR_UNDEFINED(x) && !IS_UNDETECTABLE(x)) {
532 throw %MakeTypeError('null_to_object', []);
533 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000534 return x;
535}
536
537
538// ECMA-262, section 9.4, page 34.
539function ToInteger(x) {
540 if (%_IsSmi(x)) return x;
541 return %NumberToInteger(ToNumber(x));
542}
543
544
545// ECMA-262, section 9.6, page 34.
546function ToUint32(x) {
547 if (%_IsSmi(x) && x >= 0) return x;
548 return %NumberToJSUint32(ToNumber(x));
549}
550
551
552// ECMA-262, section 9.5, page 34
553function ToInt32(x) {
554 if (%_IsSmi(x)) return x;
555 return %NumberToJSInt32(ToNumber(x));
556}
557
558
Andrei Popescu31002712010-02-23 13:46:05 +0000559// ES5, section 9.12
560function SameValue(x, y) {
561 if (typeof x != typeof y) return false;
Andrei Popescu31002712010-02-23 13:46:05 +0000562 if (IS_NUMBER(x)) {
563 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true;
Leon Clarkef7060e22010-06-03 12:02:55 +0100564 // x is +0 and y is -0 or vice versa.
565 if (x === 0 && y === 0 && (1 / x) != (1 / y)) {
Andrei Popescu31002712010-02-23 13:46:05 +0000566 return false;
567 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100568 return x === y;
Andrei Popescu31002712010-02-23 13:46:05 +0000569 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100570 return x === y
Andrei Popescu31002712010-02-23 13:46:05 +0000571}
572
Steve Blocka7e24c12009-10-30 11:49:00 +0000573
574/* ---------------------------------
575 - - - U t i l i t i e s - - -
576 ---------------------------------
577*/
578
579// Returns if the given x is a primitive value - not an object or a
580// function.
581function IsPrimitive(x) {
Kristian Monsen25f61362010-05-21 11:50:48 +0100582 if (!IS_SPEC_OBJECT_OR_NULL(x)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000583 return true;
584 } else {
585 // Even though the type of null is "object", null is still
586 // considered a primitive value.
587 return IS_NULL(x);
588 }
589}
590
591
592// ECMA-262, section 8.6.2.6, page 28.
593function DefaultNumber(x) {
594 if (IS_FUNCTION(x.valueOf)) {
595 var v = x.valueOf();
596 if (%IsPrimitive(v)) return v;
597 }
598
599 if (IS_FUNCTION(x.toString)) {
600 var s = x.toString();
601 if (%IsPrimitive(s)) return s;
602 }
603
604 throw %MakeTypeError('cannot_convert_to_primitive', []);
605}
606
607
608// ECMA-262, section 8.6.2.6, page 28.
609function DefaultString(x) {
610 if (IS_FUNCTION(x.toString)) {
611 var s = x.toString();
612 if (%IsPrimitive(s)) return s;
613 }
614
615 if (IS_FUNCTION(x.valueOf)) {
616 var v = x.valueOf();
617 if (%IsPrimitive(v)) return v;
618 }
619
620 throw %MakeTypeError('cannot_convert_to_primitive', []);
621}
622
623
624// NOTE: Setting the prototype for Array must take place as early as
625// possible due to code generation for array literals. When
626// generating code for a array literal a boilerplate array is created
627// that is cloned when running the code. It is essiential that the
628// boilerplate gets the right prototype.
629%FunctionSetPrototype($Array, new $Array(0));