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'. 10 11 12 // The following declarations are shared with other native JS files. 13 // They are all declared at this one spot to avoid redeclaration errors. 14 15 (function(global, utils) { 16 17 %CheckIsBootstrapping(); 18 19 var GlobalArray = global.Array; 20 var GlobalBoolean = global.Boolean; 21 var GlobalString = global.String; 22 var MakeRangeError; 23 var MakeTypeError; 24 var speciesSymbol; 25 26 utils.Import(function(from) { 27 MakeRangeError = from.MakeRangeError; 28 MakeTypeError = from.MakeTypeError; 29 speciesSymbol = from.species_symbol; 30 }); 31 32 // ---------------------------------------------------------------------------- 33 34 35 /* --------------------------------- 36 - - - U t i l i t i e s - - - 37 --------------------------------- 38 */ 39 40 41 function ToPositiveInteger(x, rangeErrorIndex) { 42 var i = TO_INTEGER(x) + 0; 43 if (i < 0) throw MakeRangeError(rangeErrorIndex); 44 return i; 45 } 46 47 48 function MaxSimple(a, b) { 49 return a > b ? a : b; 50 } 51 52 53 function MinSimple(a, b) { 54 return a > b ? b : a; 55 } 56 57 58 %SetForceInlineFlag(MaxSimple); 59 %SetForceInlineFlag(MinSimple); 60 61 62 // ES2015 7.3.20 63 function SpeciesConstructor(object, defaultConstructor) { 64 var constructor = object.constructor; 65 if (IS_UNDEFINED(constructor)) { 66 return defaultConstructor; 67 } 68 if (!IS_RECEIVER(constructor)) { 69 throw MakeTypeError(kConstructorNotReceiver); 70 } 71 var species = constructor[speciesSymbol]; 72 if (IS_NULL_OR_UNDEFINED(species)) { 73 return defaultConstructor; 74 } 75 if (%IsConstructor(species)) { 76 return species; 77 } 78 throw MakeTypeError(kSpeciesNotConstructor); 79 } 80 81 //---------------------------------------------------------------------------- 82 83 // NOTE: Setting the prototype for Array must take place as early as 84 // possible due to code generation for array literals. When 85 // generating code for a array literal a boilerplate array is created 86 // that is cloned when running the code. It is essential that the 87 // boilerplate gets the right prototype. 88 %FunctionSetPrototype(GlobalArray, new GlobalArray(0)); 89 90 // ---------------------------------------------------------------------------- 91 // Exports 92 93 utils.Export(function(to) { 94 to.MaxSimple = MaxSimple; 95 to.MinSimple = MinSimple; 96 to.ToPositiveInteger = ToPositiveInteger; 97 to.SpeciesConstructor = SpeciesConstructor; 98 }); 99 100 }) 101