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

Unified Diff: runtime/lib/math.cc

Issue 199263005: - Avoid exposing the 64-bit state of the PRNG to Dart code. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 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 | runtime/lib/math_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/math.cc
===================================================================
--- runtime/lib/math.cc (revision 33759)
+++ runtime/lib/math.cc (working copy)
@@ -111,6 +111,16 @@
}
+RawTypedData* CreateRandomState(Isolate* isolate, uint64_t seed) {
+ const TypedData& result = TypedData::Handle(
+ isolate, TypedData::New(kTypedDataUint32ArrayCid, 2));
+ result.SetUint32(0, static_cast<uint32_t>(seed));
+ result.SetUint32(result.ElementSizeInBytes(),
+ static_cast<uint32_t>(seed >> 32));
+ return result.raw();
+}
+
+
uint64_t mix64(uint64_t n) {
// Thomas Wang 64-bit mix.
// http://www.concentric.net/~Ttwang/tech/inthash.htm
@@ -135,14 +145,12 @@
// if (hash == 0) {
// hash = 0x5A17;
// }
-// _state[kSTATE_LO] = hash & _MASK_32;
-// _state[kSTATE_HI] = hash >> 32;
-DEFINE_NATIVE_ENTRY(Random_setupSeed, 2) {
- GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0));
- GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(1));
- const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver));
- ASSERT(!seed_int.IsNull());
- ASSERT(!array.IsNull());
+// var result = new Uint32List(2);
+// result[kSTATE_LO] = seed & _MASK_32;
+// result[kSTATE_HI] = seed >> 32;
+// return result;
+DEFINE_NATIVE_ENTRY(Random_setupSeed, 1) {
+ GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(0));
uint64_t seed = 0;
if (seed_int.IsBigint()) {
const Bigint& mask64 = Bigint::Handle(
@@ -172,18 +180,15 @@
if (seed == 0) {
seed = 0x5a17;
}
- array.SetUint32(0, static_cast<uint32_t>(seed));
- array.SetUint32(array.ElementSizeInBytes(),
- static_cast<uint32_t>(seed >> 32));
- return Object::null();
+ return CreateRandomState(isolate, seed);
}
DEFINE_NATIVE_ENTRY(Random_initialSeed, 0) {
Random* rnd = isolate->random();
- int64_t seed = rnd->NextUInt32();
- seed |= (static_cast<int64_t>(rnd->NextUInt32())) << 32;
- return Integer::New(seed);
+ uint64_t seed = rnd->NextUInt32();
+ seed |= (static_cast<uint64_t>(rnd->NextUInt32())) << 32;
siva 2014/03/17 21:19:04 I would write this as : seed |= (static_cast<uint
+ return CreateRandomState(isolate, seed);
}
} // namespace dart
« no previous file with comments | « no previous file | runtime/lib/math_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698