| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 FLAG_harmony_species; | 
 | 20 | var GlobalArray = global.Array; | 
 | 21 | var GlobalBoolean = global.Boolean; | 
 | 22 | var GlobalString = global.String; | 
 | 23 | var MakeRangeError; | 
 | 24 | var MakeTypeError; | 
 | 25 | var speciesSymbol; | 
 | 26 |  | 
 | 27 | utils.Import(function(from) { | 
 | 28 |   MakeRangeError = from.MakeRangeError; | 
 | 29 |   MakeTypeError = from.MakeTypeError; | 
 | 30 |   speciesSymbol = from.species_symbol; | 
 | 31 | }); | 
 | 32 |  | 
 | 33 | utils.ImportFromExperimental(function(from) { | 
 | 34 |   FLAG_harmony_species = from.FLAG_harmony_species; | 
 | 35 | }); | 
 | 36 |  | 
 | 37 | // ---------------------------------------------------------------------------- | 
 | 38 |  | 
| Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 39 |  | 
 | 40 | /* --------------------------------- | 
 | 41 |    - - -   U t i l i t i e s   - - - | 
 | 42 |    --------------------------------- | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 43 | */ | 
 | 44 |  | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 45 | function ConcatIterableToArray(target, iterable) { | 
 | 46 |    var index = target.length; | 
 | 47 |    for (var element of iterable) { | 
 | 48 |      AddIndexedProperty(target, index++, element); | 
 | 49 |    } | 
 | 50 |    return target; | 
 | 51 | } | 
 | 52 |  | 
 | 53 |  | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 54 | // This function should be called rather than %AddElement in contexts where the | 
 | 55 | // argument might not be less than 2**32-1. ES2015 ToLength semantics mean that | 
 | 56 | // this is a concern at basically all callsites. | 
 | 57 | function AddIndexedProperty(obj, index, value) { | 
 | 58 |   if (index === TO_UINT32(index) && index !== kMaxUint32) { | 
 | 59 |     %AddElement(obj, index, value); | 
 | 60 |   } else { | 
 | 61 |     %AddNamedProperty(obj, TO_STRING(index), value, NONE); | 
 | 62 |   } | 
 | 63 | } | 
 | 64 | %SetForceInlineFlag(AddIndexedProperty); | 
 | 65 |  | 
 | 66 |  | 
 | 67 | function ToPositiveInteger(x, rangeErrorIndex) { | 
 | 68 |   var i = TO_INTEGER_MAP_MINUS_ZERO(x); | 
 | 69 |   if (i < 0) throw MakeRangeError(rangeErrorIndex); | 
 | 70 |   return i; | 
 | 71 | } | 
 | 72 |  | 
 | 73 |  | 
 | 74 | function MaxSimple(a, b) { | 
 | 75 |   return a > b ? a : b; | 
 | 76 | } | 
 | 77 |  | 
 | 78 |  | 
 | 79 | function MinSimple(a, b) { | 
 | 80 |   return a > b ? b : a; | 
 | 81 | } | 
 | 82 |  | 
 | 83 |  | 
 | 84 | %SetForceInlineFlag(MaxSimple); | 
 | 85 | %SetForceInlineFlag(MinSimple); | 
 | 86 |  | 
 | 87 |  | 
 | 88 | // ES2015 7.3.20 | 
 | 89 | // For the fallback with --harmony-species off, there are two possible choices: | 
 | 90 | //  - "conservative": return defaultConstructor | 
 | 91 | //  - "not conservative": return object.constructor | 
 | 92 | // This fallback path is only needed in the transition to ES2015, and the | 
 | 93 | // choice is made simply to preserve the previous behavior so that we don't | 
 | 94 | // have a three-step upgrade: old behavior, unspecified intermediate behavior, | 
 | 95 | // and ES2015. | 
 | 96 | // In some cases, we were "conservative" (e.g., ArrayBuffer, RegExp), and in | 
 | 97 | // other cases we were "not conservative (e.g., TypedArray, Promise). | 
 | 98 | function SpeciesConstructor(object, defaultConstructor, conservative) { | 
 | 99 |   if (FLAG_harmony_species) { | 
 | 100 |     var constructor = object.constructor; | 
 | 101 |     if (IS_UNDEFINED(constructor)) { | 
 | 102 |       return defaultConstructor; | 
 | 103 |     } | 
 | 104 |     if (!IS_RECEIVER(constructor)) { | 
 | 105 |       throw MakeTypeError(kConstructorNotReceiver); | 
 | 106 |     } | 
 | 107 |     var species = constructor[speciesSymbol]; | 
 | 108 |     if (IS_NULL_OR_UNDEFINED(species)) { | 
 | 109 |       return defaultConstructor; | 
 | 110 |     } | 
 | 111 |     if (%IsConstructor(species)) { | 
 | 112 |       return species; | 
 | 113 |     } | 
 | 114 |     throw MakeTypeError(kSpeciesNotConstructor); | 
 | 115 |   } else { | 
 | 116 |     return conservative ? defaultConstructor : object.constructor; | 
 | 117 |   } | 
 | 118 | } | 
 | 119 |  | 
 | 120 | //---------------------------------------------------------------------------- | 
 | 121 |  | 
 | 122 | // NOTE: Setting the prototype for Array must take place as early as | 
 | 123 | // possible due to code generation for array literals.  When | 
 | 124 | // generating code for a array literal a boilerplate array is created | 
 | 125 | // that is cloned when running the code.  It is essential that the | 
 | 126 | // boilerplate gets the right prototype. | 
 | 127 | %FunctionSetPrototype(GlobalArray, new GlobalArray(0)); | 
 | 128 |  | 
 | 129 | // ---------------------------------------------------------------------------- | 
 | 130 | // Exports | 
 | 131 |  | 
 | 132 | utils.Export(function(to) { | 
 | 133 |   to.AddIndexedProperty = AddIndexedProperty; | 
 | 134 |   to.MaxSimple = MaxSimple; | 
 | 135 |   to.MinSimple = MinSimple; | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 136 |   to.ToPositiveInteger = ToPositiveInteger; | 
 | 137 |   to.SpeciesConstructor = SpeciesConstructor; | 
 | 138 | }); | 
 | 139 |  | 
 | 140 | %InstallToContext([ | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 141 |   "concat_iterable_to_array", ConcatIterableToArray, | 
 | 142 | ]); | 
 | 143 |  | 
 | 144 | }) |