| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 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 (function(global, utils) { | |
| 6 "use strict"; | |
| 7 | |
| 8 %CheckIsBootstrapping(); | |
| 9 | |
| 10 // ------------------------------------------------------------------- | |
| 11 // Imports | |
| 12 | |
| 13 // The first two slots are reserved to persist PRNG state. | |
| 14 define kRandomNumberStart = 2; | |
| 15 | |
| 16 var GlobalMath = global.Math; | |
| 17 var NaN = %GetRootNaN(); | |
| 18 var nextRandomIndex = 0; | |
| 19 var randomNumbers = UNDEFINED; | |
| 20 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | |
| 21 | |
| 22 //------------------------------------------------------------------- | |
| 23 // ECMA 262 - 15.8.2.14 | |
| 24 function MathRandom() { | |
| 25 // While creating a startup snapshot, %GenerateRandomNumbers returns a | |
| 26 // normal array containing a single random number, and has to be called for | |
| 27 // every new random number. | |
| 28 // Otherwise, it returns a pre-populated typed array of random numbers. The | |
| 29 // first two elements are reserved for the PRNG state. | |
| 30 if (nextRandomIndex <= kRandomNumberStart) { | |
| 31 randomNumbers = %GenerateRandomNumbers(randomNumbers); | |
| 32 if (%_IsTypedArray(randomNumbers)) { | |
| 33 nextRandomIndex = %_TypedArrayGetLength(randomNumbers); | |
| 34 } else { | |
| 35 nextRandomIndex = randomNumbers.length; | |
| 36 } | |
| 37 } | |
| 38 return randomNumbers[--nextRandomIndex]; | |
| 39 } | |
| 40 | |
| 41 // ------------------------------------------------------------------- | |
| 42 | |
| 43 %AddNamedProperty(GlobalMath, toStringTagSymbol, "Math", READ_ONLY | DONT_ENUM); | |
| 44 | |
| 45 // Set up non-enumerable functions of the Math object and | |
| 46 // set their names. | |
| 47 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ | |
| 48 "random", MathRandom, | |
| 49 ]); | |
| 50 | |
| 51 %SetForceInlineFlag(MathRandom); | |
| 52 | |
| 53 // ------------------------------------------------------------------- | |
| 54 // Exports | |
| 55 | |
| 56 utils.Export(function(to) { | |
| 57 to.MathRandom = MathRandom; | |
| 58 }); | |
| 59 | |
| 60 }) | |
| OLD | NEW |