| OLD | NEW |
| (Empty) |
| 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 var $NaN; | |
| 15 var $sameValue; | |
| 16 var $sameValueZero; | |
| 17 var $toPositiveInteger; | |
| 18 | |
| 19 var harmony_tolength = false; | |
| 20 | |
| 21 (function(global, utils) { | |
| 22 | |
| 23 %CheckIsBootstrapping(); | |
| 24 | |
| 25 var GlobalArray = global.Array; | |
| 26 var GlobalBoolean = global.Boolean; | |
| 27 var GlobalString = global.String; | |
| 28 var isConcatSpreadableSymbol = | |
| 29 utils.ImportNow("is_concat_spreadable_symbol"); | |
| 30 | |
| 31 // ---------------------------------------------------------------------------- | |
| 32 | |
| 33 /* ----------------------------- | |
| 34 - - - H e l p e r s - - - | |
| 35 ----------------------------- | |
| 36 */ | |
| 37 | |
| 38 function APPLY_PREPARE(args) { | |
| 39 var length; | |
| 40 | |
| 41 // First check that the receiver is callable. | |
| 42 if (!IS_CALLABLE(this)) { | |
| 43 throw %make_type_error(kApplyNonFunction, TO_STRING(this), typeof this); | |
| 44 } | |
| 45 | |
| 46 // First check whether length is a positive Smi and args is an | |
| 47 // array. This is the fast case. If this fails, we do the slow case | |
| 48 // that takes care of more eventualities. | |
| 49 if (IS_ARRAY(args)) { | |
| 50 length = args.length; | |
| 51 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength) { | |
| 52 return length; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 length = (args == null) ? 0 : TO_UINT32(args.length); | |
| 57 | |
| 58 // We can handle any number of apply arguments if the stack is | |
| 59 // big enough, but sanity check the value to avoid overflow when | |
| 60 // multiplying with pointer size. | |
| 61 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); | |
| 62 | |
| 63 // Make sure the arguments list has the right type. | |
| 64 if (args != null && !IS_SPEC_OBJECT(args)) { | |
| 65 throw %make_type_error(kWrongArgs, "Function.prototype.apply"); | |
| 66 } | |
| 67 | |
| 68 // Return the length which is the number of arguments to copy to the | |
| 69 // stack. It is guaranteed to be a small integer at this point. | |
| 70 return length; | |
| 71 } | |
| 72 | |
| 73 | |
| 74 function REFLECT_APPLY_PREPARE(args) { | |
| 75 var length; | |
| 76 | |
| 77 // First check that the receiver is callable. | |
| 78 if (!IS_CALLABLE(this)) { | |
| 79 throw %make_type_error(kApplyNonFunction, TO_STRING(this), typeof this); | |
| 80 } | |
| 81 | |
| 82 // First check whether length is a positive Smi and args is an | |
| 83 // array. This is the fast case. If this fails, we do the slow case | |
| 84 // that takes care of more eventualities. | |
| 85 if (IS_ARRAY(args)) { | |
| 86 length = args.length; | |
| 87 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength) { | |
| 88 return length; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 if (!IS_SPEC_OBJECT(args)) { | |
| 93 throw %make_type_error(kWrongArgs, "Reflect.apply"); | |
| 94 } | |
| 95 | |
| 96 length = TO_LENGTH(args.length); | |
| 97 | |
| 98 // We can handle any number of apply arguments if the stack is | |
| 99 // big enough, but sanity check the value to avoid overflow when | |
| 100 // multiplying with pointer size. | |
| 101 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); | |
| 102 | |
| 103 // Return the length which is the number of arguments to copy to the | |
| 104 // stack. It is guaranteed to be a small integer at this point. | |
| 105 return length; | |
| 106 } | |
| 107 | |
| 108 | |
| 109 function REFLECT_CONSTRUCT_PREPARE( | |
| 110 args, newTarget) { | |
| 111 var length; | |
| 112 var ctorOk = IS_CALLABLE(this) && %IsConstructor(this); | |
| 113 var newTargetOk = IS_CALLABLE(newTarget) && %IsConstructor(newTarget); | |
| 114 | |
| 115 // First check whether length is a positive Smi and args is an | |
| 116 // array. This is the fast case. If this fails, we do the slow case | |
| 117 // that takes care of more eventualities. | |
| 118 if (IS_ARRAY(args)) { | |
| 119 length = args.length; | |
| 120 if (%_IsSmi(length) && length >= 0 && length < kSafeArgumentsLength && | |
| 121 ctorOk && newTargetOk) { | |
| 122 return length; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 if (!ctorOk) { | |
| 127 if (!IS_CALLABLE(this)) { | |
| 128 throw %make_type_error(kCalledNonCallable, TO_STRING(this)); | |
| 129 } else { | |
| 130 throw %make_type_error(kNotConstructor, TO_STRING(this)); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 if (!newTargetOk) { | |
| 135 if (!IS_CALLABLE(newTarget)) { | |
| 136 throw %make_type_error(kCalledNonCallable, TO_STRING(newTarget)); | |
| 137 } else { | |
| 138 throw %make_type_error(kNotConstructor, TO_STRING(newTarget)); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 if (!IS_SPEC_OBJECT(args)) { | |
| 143 throw %make_type_error(kWrongArgs, "Reflect.construct"); | |
| 144 } | |
| 145 | |
| 146 length = TO_LENGTH(args.length); | |
| 147 | |
| 148 // We can handle any number of apply arguments if the stack is | |
| 149 // big enough, but sanity check the value to avoid overflow when | |
| 150 // multiplying with pointer size. | |
| 151 if (length > kSafeArgumentsLength) throw %make_range_error(kStackOverflow); | |
| 152 | |
| 153 // Return the length which is the number of arguments to copy to the | |
| 154 // stack. It is guaranteed to be a small integer at this point. | |
| 155 return length; | |
| 156 } | |
| 157 | |
| 158 | |
| 159 function CONCAT_ITERABLE_TO_ARRAY(iterable) { | |
| 160 return %concat_iterable_to_array(this, iterable); | |
| 161 }; | |
| 162 | |
| 163 | |
| 164 /* ------------------------------------- | |
| 165 - - - C o n v e r s i o n s - - - | |
| 166 ------------------------------------- | |
| 167 */ | |
| 168 | |
| 169 // ES5, section 9.12 | |
| 170 function SameValue(x, y) { | |
| 171 if (typeof x != typeof y) return false; | |
| 172 if (IS_NUMBER(x)) { | |
| 173 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; | |
| 174 // x is +0 and y is -0 or vice versa. | |
| 175 if (x === 0 && y === 0 && %_IsMinusZero(x) != %_IsMinusZero(y)) { | |
| 176 return false; | |
| 177 } | |
| 178 } | |
| 179 if (IS_SIMD_VALUE(x)) return %SimdSameValue(x, y); | |
| 180 return x === y; | |
| 181 } | |
| 182 | |
| 183 | |
| 184 // ES6, section 7.2.4 | |
| 185 function SameValueZero(x, y) { | |
| 186 if (typeof x != typeof y) return false; | |
| 187 if (IS_NUMBER(x)) { | |
| 188 if (NUMBER_IS_NAN(x) && NUMBER_IS_NAN(y)) return true; | |
| 189 } | |
| 190 if (IS_SIMD_VALUE(x)) return %SimdSameValueZero(x, y); | |
| 191 return x === y; | |
| 192 } | |
| 193 | |
| 194 | |
| 195 function ConcatIterableToArray(target, iterable) { | |
| 196 var index = target.length; | |
| 197 for (var element of iterable) { | |
| 198 %AddElement(target, index++, element); | |
| 199 } | |
| 200 return target; | |
| 201 } | |
| 202 | |
| 203 | |
| 204 /* --------------------------------- | |
| 205 - - - U t i l i t i e s - - - | |
| 206 --------------------------------- | |
| 207 */ | |
| 208 | |
| 209 | |
| 210 // ES6, draft 10-14-14, section 22.1.3.1.1 | |
| 211 function IsConcatSpreadable(O) { | |
| 212 if (!IS_SPEC_OBJECT(O)) return false; | |
| 213 var spreadable = O[isConcatSpreadableSymbol]; | |
| 214 if (IS_UNDEFINED(spreadable)) return IS_ARRAY(O); | |
| 215 return TO_BOOLEAN(spreadable); | |
| 216 } | |
| 217 | |
| 218 | |
| 219 function ToPositiveInteger(x, rangeErrorIndex) { | |
| 220 var i = TO_INTEGER_MAP_MINUS_ZERO(x); | |
| 221 if (i < 0) throw MakeRangeError(rangeErrorIndex); | |
| 222 return i; | |
| 223 } | |
| 224 | |
| 225 //---------------------------------------------------------------------------- | |
| 226 | |
| 227 // NOTE: Setting the prototype for Array must take place as early as | |
| 228 // possible due to code generation for array literals. When | |
| 229 // generating code for a array literal a boilerplate array is created | |
| 230 // that is cloned when running the code. It is essential that the | |
| 231 // boilerplate gets the right prototype. | |
| 232 %FunctionSetPrototype(GlobalArray, new GlobalArray(0)); | |
| 233 | |
| 234 // ---------------------------------------------------------------------------- | |
| 235 // Exports | |
| 236 | |
| 237 $NaN = %GetRootNaN(); | |
| 238 $sameValue = SameValue; | |
| 239 $sameValueZero = SameValueZero; | |
| 240 $toPositiveInteger = ToPositiveInteger; | |
| 241 | |
| 242 %InstallToContext([ | |
| 243 "apply_prepare_builtin", APPLY_PREPARE, | |
| 244 "concat_iterable_to_array_builtin", CONCAT_ITERABLE_TO_ARRAY, | |
| 245 "reflect_apply_prepare_builtin", REFLECT_APPLY_PREPARE, | |
| 246 "reflect_construct_prepare_builtin", REFLECT_CONSTRUCT_PREPARE, | |
| 247 ]); | |
| 248 | |
| 249 %InstallToContext([ | |
| 250 "concat_iterable_to_array", ConcatIterableToArray, | |
| 251 ]); | |
| 252 | |
| 253 }) | |
| OLD | NEW |