| 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 |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include <ctype.h> // isspace. |
| 6 | |
| 7 | #include "vm/bootstrap_natives.h" |
| 8 | |
| 9 | #include "vm/exceptions.h" |
| 10 | #include "vm/native_entry.h" |
| 11 | #include "vm/object.h" |
| 12 | #include "vm/symbols.h" |
| 13 | |
| 14 | namespace dart { |
| 15 | |
| 16 | DEFINE_NATIVE_ENTRY(Math_doublePow, 0, 2) { |
| 17 | const double operand = |
| 18 | Double::CheckedHandle(zone, ptr: arguments->NativeArgAt(index: 0)).value(); |
| 19 | GET_NON_NULL_NATIVE_ARGUMENT(Double, exponent_object, |
| 20 | arguments->NativeArgAt(1)); |
| 21 | const double exponent = exponent_object.value(); |
| 22 | return Double::New(d: pow(x: operand, y: exponent)); |
| 23 | } |
| 24 | |
| 25 | DEFINE_NATIVE_ENTRY(Random_initialSeed, 0, 0) { |
| 26 | Random* rnd = isolate->random(); |
| 27 | uint64_t seed = rnd->NextUInt32(); |
| 28 | seed |= (static_cast<uint64_t>(rnd->NextUInt32()) << 32); |
| 29 | return Integer::New(value: seed); |
| 30 | } |
| 31 | |
| 32 | DEFINE_NATIVE_ENTRY(SecureRandom_getBytes, 0, 1) { |
| 33 | GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(0)); |
| 34 | const intptr_t n = count.Value(); |
| 35 | ASSERT((n > 0) && (n <= 8)); |
| 36 | uint8_t buffer[8]; |
| 37 | Dart_EntropySource entropy_source = Dart::entropy_source_callback(); |
| 38 | if ((entropy_source == nullptr) || !entropy_source(buffer, n)) { |
| 39 | const String& error = String::Handle(ptr: String::New( |
| 40 | cstr: "No source of cryptographically secure random numbers available." )); |
| 41 | const Array& args = Array::Handle(ptr: Array::New(len: 1)); |
| 42 | args.SetAt(index: 0, value: error); |
| 43 | Exceptions::ThrowByType(type: Exceptions::kUnsupported, arguments: args); |
| 44 | } |
| 45 | uint64_t result = 0; |
| 46 | for (intptr_t i = 0; i < n; i++) { |
| 47 | result = (result << 8) | buffer[i]; |
| 48 | } |
| 49 | return Integer::New(value: result); |
| 50 | } |
| 51 | |
| 52 | } // namespace dart |
| 53 | |