1// Copyright 2014 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// Test that Array builtins can be called on primitive values. 6var values = [ 23, 4.2, true, false, 0/0 ]; 7for (var i = 0; i < values.length; ++i) { 8 var v = values[i]; 9 Array.prototype.join.call(v); 10 Array.prototype.pop.call(v); 11 Array.prototype.push.call(v); 12 Array.prototype.reverse.call(v); 13 Array.prototype.shift.call(v); 14 Array.prototype.slice.call(v); 15 Array.prototype.splice.call(v); 16 Array.prototype.unshift.call(v); 17} 18 19// Test that ToObject on primitive values is only called once. 20var length_receiver, element_receiver; 21function length() { length_receiver = this; return 2; } 22function element() { element_receiver = this; return "x"; } 23Object.defineProperty(Number.prototype, "length", { get:length, set:length }); 24Object.defineProperty(Number.prototype, "0", { get:element, set:element }); 25Object.defineProperty(Number.prototype, "1", { get:element, set:element }); 26Object.defineProperty(Number.prototype, "2", { get:element, set:element }); 27function test_receiver(expected, call_string) { 28 assertDoesNotThrow(call_string); 29 assertEquals(new Number(expected), length_receiver); 30 assertSame(length_receiver, element_receiver); 31} 32 33test_receiver(11, "Array.prototype.join.call(11)") 34test_receiver(23, "Array.prototype.pop.call(23)"); 35test_receiver(42, "Array.prototype.push.call(42, 'y')"); 36test_receiver(49, "Array.prototype.reverse.call(49)"); 37test_receiver(65, "Array.prototype.shift.call(65)"); 38test_receiver(77, "Array.prototype.slice.call(77, 1)"); 39test_receiver(88, "Array.prototype.splice.call(88, 1, 1)"); 40test_receiver(99, "Array.prototype.unshift.call(99, 'z')"); 41