Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Unified Diff: src/js/math.js

Issue 1780173002: Allow Math.random to be called when creating a custom startup snapshot. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@testserialize
Patch Set: fix Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/runtime/runtime-maths.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/js/math.js
diff --git a/src/js/math.js b/src/js/math.js
index fbe783bf7758b36daaa86ae10ad502c5db72cc81..121d188101037fbda683c2e160364ec77a8cfbc8 100644
--- a/src/js/math.js
+++ b/src/js/math.js
@@ -10,7 +10,6 @@
// -------------------------------------------------------------------
// Imports
-define kRandomBatchSize = 64;
// The first two slots are reserved to persist PRNG state.
define kRandomNumberStart = 2;
@@ -19,7 +18,7 @@ var GlobalMath = global.Math;
var GlobalObject = global.Object;
var InternalArray = utils.InternalArray;
var NaN = %GetRootNaN();
-var nextRandomIndex = kRandomBatchSize;
+var nextRandomIndex = 0;
var randomNumbers = UNDEFINED;
var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol");
@@ -67,19 +66,24 @@ function MathPowJS(x, y) {
// ECMA 262 - 15.8.2.14
function MathRandom() {
- if (nextRandomIndex >= kRandomBatchSize) {
+ // While creating a startup snapshot, %GenerateRandomNumbers returns a
+ // normal array containing a single random number, and has to be called for
+ // every new random number.
+ // Otherwise, it returns a pre-populated typed array of random numbers. The
+ // first two elements are reserved for the PRNG state.
+ if (nextRandomIndex <= kRandomNumberStart) {
randomNumbers = %GenerateRandomNumbers(randomNumbers);
- nextRandomIndex = kRandomNumberStart;
+ nextRandomIndex = randomNumbers.length;
}
- return randomNumbers[nextRandomIndex++];
+ return randomNumbers[--nextRandomIndex];
}
function MathRandomRaw() {
- if (nextRandomIndex >= kRandomBatchSize) {
+ if (nextRandomIndex <= kRandomNumberStart) {
randomNumbers = %GenerateRandomNumbers(randomNumbers);
- nextRandomIndex = kRandomNumberStart;
+ nextRandomIndex = randomNumbers.length;
}
- return %_DoubleLo(randomNumbers[nextRandomIndex++]) & 0x3FFFFFFF;
+ return %_DoubleLo(randomNumbers[--nextRandomIndex]) & 0x3FFFFFFF;
}
// ECMA 262 - 15.8.2.15
« no previous file with comments | « no previous file | src/runtime/runtime-maths.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698