OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include <ctype.h> // isspace. | 5 #include <ctype.h> // isspace. |
6 | 6 |
7 #include "vm/bootstrap_natives.h" | 7 #include "vm/bootstrap_natives.h" |
8 | 8 |
9 #include "vm/exceptions.h" | 9 #include "vm/exceptions.h" |
10 #include "vm/native_entry.h" | 10 #include "vm/native_entry.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 DEFINE_NATIVE_ENTRY(Math_doublePow, 2) { | 68 DEFINE_NATIVE_ENTRY(Math_doublePow, 2) { |
69 const double operand = | 69 const double operand = |
70 Double::CheckedHandle(arguments->NativeArgAt(0)).value(); | 70 Double::CheckedHandle(arguments->NativeArgAt(0)).value(); |
71 GET_NON_NULL_NATIVE_ARGUMENT(Double, exponent_object, | 71 GET_NON_NULL_NATIVE_ARGUMENT(Double, exponent_object, |
72 arguments->NativeArgAt(1)); | 72 arguments->NativeArgAt(1)); |
73 const double exponent = exponent_object.value(); | 73 const double exponent = exponent_object.value(); |
74 return Double::New(pow(operand, exponent)); | 74 return Double::New(pow(operand, exponent)); |
75 } | 75 } |
76 | 76 |
77 | |
78 // Returns the typed-data array store in '_Random._state' field. | 77 // Returns the typed-data array store in '_Random._state' field. |
79 static RawTypedData* GetRandomStateArray(const Instance& receiver) { | 78 static RawTypedData* GetRandomStateArray(const Instance& receiver) { |
80 const Class& random_class = Class::Handle(receiver.clazz()); | 79 const Class& random_class = Class::Handle(receiver.clazz()); |
81 const Field& state_field = | 80 const Field& state_field = |
82 Field::Handle(random_class.LookupFieldAllowPrivate(Symbols::_state())); | 81 Field::Handle(random_class.LookupFieldAllowPrivate(Symbols::_state())); |
83 ASSERT(!state_field.IsNull()); | 82 ASSERT(!state_field.IsNull()); |
84 const Instance& state_field_value = | 83 const Instance& state_field_value = |
85 Instance::Cast(Object::Handle(receiver.GetField(state_field))); | 84 Instance::Cast(Object::Handle(receiver.GetField(state_field))); |
86 ASSERT(!state_field_value.IsNull()); | 85 ASSERT(!state_field_value.IsNull()); |
87 ASSERT(state_field_value.IsTypedData()); | 86 ASSERT(state_field_value.IsTypedData()); |
88 const TypedData& array = TypedData::Cast(state_field_value); | 87 const TypedData& array = TypedData::Cast(state_field_value); |
89 ASSERT(array.Length() == 2); | 88 ASSERT(array.Length() == 2); |
90 ASSERT(array.ElementType() == kUint32ArrayElement); | 89 ASSERT(array.ElementType() == kUint32ArrayElement); |
91 return array.raw(); | 90 return array.raw(); |
92 } | 91 } |
93 | 92 |
94 | |
95 // Implements: | 93 // Implements: |
96 // var state = | 94 // var state = |
97 // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & (1 << 64) - 1); | 95 // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & (1 << 64) - 1); |
98 // _state[_kSTATE_LO] = state & (1 << 32) - 1); | 96 // _state[_kSTATE_LO] = state & (1 << 32) - 1); |
99 // _state[_kSTATE_HI] = state >> 32; | 97 // _state[_kSTATE_HI] = state >> 32; |
100 DEFINE_NATIVE_ENTRY(Random_nextState, 1) { | 98 DEFINE_NATIVE_ENTRY(Random_nextState, 1) { |
101 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0)); | 99 GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0)); |
102 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver)); | 100 const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver)); |
103 const uint64_t state_lo = array.GetUint32(0); | 101 const uint64_t state_lo = array.GetUint32(0); |
104 const uint64_t state_hi = array.GetUint32(array.ElementSizeInBytes()); | 102 const uint64_t state_hi = array.GetUint32(array.ElementSizeInBytes()); |
105 const uint64_t A = 0xffffda61; | 103 const uint64_t A = 0xffffda61; |
106 uint64_t state = (A * state_lo) + state_hi; | 104 uint64_t state = (A * state_lo) + state_hi; |
107 array.SetUint32(0, static_cast<uint32_t>(state)); | 105 array.SetUint32(0, static_cast<uint32_t>(state)); |
108 array.SetUint32(array.ElementSizeInBytes(), | 106 array.SetUint32(array.ElementSizeInBytes(), |
109 static_cast<uint32_t>(state >> 32)); | 107 static_cast<uint32_t>(state >> 32)); |
110 return Object::null(); | 108 return Object::null(); |
111 } | 109 } |
112 | 110 |
113 | |
114 RawTypedData* CreateRandomState(Zone* zone, uint64_t seed) { | 111 RawTypedData* CreateRandomState(Zone* zone, uint64_t seed) { |
115 const TypedData& result = | 112 const TypedData& result = |
116 TypedData::Handle(zone, TypedData::New(kTypedDataUint32ArrayCid, 2)); | 113 TypedData::Handle(zone, TypedData::New(kTypedDataUint32ArrayCid, 2)); |
117 result.SetUint32(0, static_cast<uint32_t>(seed)); | 114 result.SetUint32(0, static_cast<uint32_t>(seed)); |
118 result.SetUint32(result.ElementSizeInBytes(), | 115 result.SetUint32(result.ElementSizeInBytes(), |
119 static_cast<uint32_t>(seed >> 32)); | 116 static_cast<uint32_t>(seed >> 32)); |
120 return result.raw(); | 117 return result.raw(); |
121 } | 118 } |
122 | 119 |
123 | |
124 uint64_t mix64(uint64_t n) { | 120 uint64_t mix64(uint64_t n) { |
125 // Thomas Wang 64-bit mix. | 121 // Thomas Wang 64-bit mix. |
126 // http://www.concentric.net/~Ttwang/tech/inthash.htm | 122 // http://www.concentric.net/~Ttwang/tech/inthash.htm |
127 // via. http://web.archive.org/web/20071223173210/http://www.concentric.net/~T
twang/tech/inthash.htm | 123 // via. http://web.archive.org/web/20071223173210/http://www.concentric.net/~T
twang/tech/inthash.htm |
128 n = (~n) + (n << 21); // n = (n << 21) - n - 1; | 124 n = (~n) + (n << 21); // n = (n << 21) - n - 1; |
129 n = n ^ (n >> 24); | 125 n = n ^ (n >> 24); |
130 n = n * 265; // n = (n + (n << 3)) + (n << 8); | 126 n = n * 265; // n = (n + (n << 3)) + (n << 8); |
131 n = n ^ (n >> 14); | 127 n = n ^ (n >> 14); |
132 n = n * 21; // n = (n + (n << 2)) + (n << 4); | 128 n = n * 21; // n = (n + (n << 2)) + (n << 4); |
133 n = n ^ (n >> 28); | 129 n = n ^ (n >> 28); |
134 n = n + (n << 31); | 130 n = n + (n << 31); |
135 return n; | 131 return n; |
136 } | 132 } |
137 | 133 |
138 | |
139 // Implements: | 134 // Implements: |
140 // uint64_t hash = 0; | 135 // uint64_t hash = 0; |
141 // do { | 136 // do { |
142 // hash = hash * 1037 ^ mix64((uint64_t)seed); | 137 // hash = hash * 1037 ^ mix64((uint64_t)seed); |
143 // seed >>= 64; | 138 // seed >>= 64; |
144 // } while (seed != 0 && seed != -1); // Limits if seed positive or negative. | 139 // } while (seed != 0 && seed != -1); // Limits if seed positive or negative. |
145 // if (hash == 0) { | 140 // if (hash == 0) { |
146 // hash = 0x5A17; | 141 // hash = 0x5A17; |
147 // } | 142 // } |
148 // var result = new Uint32List(2); | 143 // var result = new Uint32List(2); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 } else { | 179 } else { |
185 seed = mix64(static_cast<uint64_t>(seed_int.AsInt64Value())); | 180 seed = mix64(static_cast<uint64_t>(seed_int.AsInt64Value())); |
186 } | 181 } |
187 | 182 |
188 if (seed == 0) { | 183 if (seed == 0) { |
189 seed = 0x5a17; | 184 seed = 0x5a17; |
190 } | 185 } |
191 return CreateRandomState(zone, seed); | 186 return CreateRandomState(zone, seed); |
192 } | 187 } |
193 | 188 |
194 | |
195 DEFINE_NATIVE_ENTRY(Random_initialSeed, 0) { | 189 DEFINE_NATIVE_ENTRY(Random_initialSeed, 0) { |
196 Random* rnd = isolate->random(); | 190 Random* rnd = isolate->random(); |
197 uint64_t seed = rnd->NextUInt32(); | 191 uint64_t seed = rnd->NextUInt32(); |
198 seed |= (static_cast<uint64_t>(rnd->NextUInt32()) << 32); | 192 seed |= (static_cast<uint64_t>(rnd->NextUInt32()) << 32); |
199 return CreateRandomState(zone, seed); | 193 return CreateRandomState(zone, seed); |
200 } | 194 } |
201 | 195 |
202 | |
203 DEFINE_NATIVE_ENTRY(SecureRandom_getBytes, 1) { | 196 DEFINE_NATIVE_ENTRY(SecureRandom_getBytes, 1) { |
204 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(0)); | 197 GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(0)); |
205 const intptr_t n = count.Value(); | 198 const intptr_t n = count.Value(); |
206 ASSERT((n > 0) && (n <= 8)); | 199 ASSERT((n > 0) && (n <= 8)); |
207 uint8_t buffer[8]; | 200 uint8_t buffer[8]; |
208 Dart_EntropySource entropy_source = Dart::entropy_source_callback(); | 201 Dart_EntropySource entropy_source = Dart::entropy_source_callback(); |
209 if ((entropy_source == NULL) || !entropy_source(buffer, n)) { | 202 if ((entropy_source == NULL) || !entropy_source(buffer, n)) { |
210 const String& error = String::Handle(String::New( | 203 const String& error = String::Handle(String::New( |
211 "No source of cryptographically secure random numbers available.")); | 204 "No source of cryptographically secure random numbers available.")); |
212 const Array& args = Array::Handle(Array::New(1)); | 205 const Array& args = Array::Handle(Array::New(1)); |
213 args.SetAt(0, error); | 206 args.SetAt(0, error); |
214 Exceptions::ThrowByType(Exceptions::kUnsupported, args); | 207 Exceptions::ThrowByType(Exceptions::kUnsupported, args); |
215 } | 208 } |
216 uint64_t result = 0; | 209 uint64_t result = 0; |
217 for (intptr_t i = 0; i < n; i++) { | 210 for (intptr_t i = 0; i < n; i++) { |
218 result = (result << 8) | buffer[i]; | 211 result = (result << 8) | buffer[i]; |
219 } | 212 } |
220 return Integer::New(result); | 213 return Integer::New(result); |
221 } | 214 } |
222 | 215 |
223 } // namespace dart | 216 } // namespace dart |
OLD | NEW |