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