OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/assembler.h" | 8 #include "src/assembler.h" |
9 #include "src/base/utils/random-number-generator.h" | 9 #include "src/base/utils/random-number-generator.h" |
10 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 | 221 |
222 CONVERT_DOUBLE_ARG_CHECKED(x, 0); | 222 CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
223 lazily_initialize_fast_sqrt(isolate); | 223 lazily_initialize_fast_sqrt(isolate); |
224 return *isolate->factory()->NewNumber(fast_sqrt(x, isolate)); | 224 return *isolate->factory()->NewNumber(fast_sqrt(x, isolate)); |
225 } | 225 } |
226 | 226 |
227 | 227 |
228 RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) { | 228 RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) { |
229 HandleScope scope(isolate); | 229 HandleScope scope(isolate); |
230 DCHECK(args.length() == 1); | 230 DCHECK(args.length() == 1); |
231 // Random numbers in the snapshot are not really that random. | 231 if (isolate->serializer_enabled()) { |
232 CHECK(!isolate->serializer_enabled()); | 232 // Random numbers in the snapshot are not really that random. And we cannot |
| 233 // return a typed array as it cannot be serialized. To make calling |
| 234 // Math.random possible when creating a custom startup snapshot, we simply |
| 235 // return a normal array with a single random number. |
| 236 Handle<HeapNumber> random_number = isolate->factory()->NewHeapNumber( |
| 237 isolate->random_number_generator()->NextDouble()); |
| 238 Handle<FixedArray> array_backing = isolate->factory()->NewFixedArray(1); |
| 239 array_backing->set(0, *random_number); |
| 240 return *isolate->factory()->NewJSArrayWithElements(array_backing); |
| 241 } |
| 242 |
233 static const int kState0Offset = 0; | 243 static const int kState0Offset = 0; |
234 static const int kState1Offset = 1; | 244 static const int kState1Offset = 1; |
235 static const int kRandomBatchSize = 64; | 245 static const int kRandomBatchSize = 64; |
236 CONVERT_ARG_HANDLE_CHECKED(Object, maybe_typed_array, 0); | 246 CONVERT_ARG_HANDLE_CHECKED(Object, maybe_typed_array, 0); |
237 Handle<JSTypedArray> typed_array; | 247 Handle<JSTypedArray> typed_array; |
238 // Allocate typed array if it does not yet exist. | 248 // Allocate typed array if it does not yet exist. |
239 if (maybe_typed_array->IsJSTypedArray()) { | 249 if (maybe_typed_array->IsJSTypedArray()) { |
240 typed_array = Handle<JSTypedArray>::cast(maybe_typed_array); | 250 typed_array = Handle<JSTypedArray>::cast(maybe_typed_array); |
241 } else { | 251 } else { |
242 static const int kByteLength = kRandomBatchSize * kDoubleSize; | 252 static const int kByteLength = kRandomBatchSize * kDoubleSize; |
(...skipping 22 matching lines...) Expand all Loading... |
265 base::RandomNumberGenerator::XorShift128(&state0, &state1); | 275 base::RandomNumberGenerator::XorShift128(&state0, &state1); |
266 array[i] = base::RandomNumberGenerator::ToDouble(state0, state1); | 276 array[i] = base::RandomNumberGenerator::ToDouble(state0, state1); |
267 } | 277 } |
268 // Persist current state. | 278 // Persist current state. |
269 array[kState0Offset] = uint64_to_double(state0); | 279 array[kState0Offset] = uint64_to_double(state0); |
270 array[kState1Offset] = uint64_to_double(state1); | 280 array[kState1Offset] = uint64_to_double(state1); |
271 return *typed_array; | 281 return *typed_array; |
272 } | 282 } |
273 } // namespace internal | 283 } // namespace internal |
274 } // namespace v8 | 284 } // namespace v8 |
OLD | NEW |