| 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_sqrt, 0, 1) { |
| 17 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 18 | return Double::New(sqrt(operand.value())); |
| 19 | } |
| 20 | |
| 21 | DEFINE_NATIVE_ENTRY(Math_sin, 0, 1) { |
| 22 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 23 | return Double::New(sin(operand.value())); |
| 24 | } |
| 25 | |
| 26 | DEFINE_NATIVE_ENTRY(Math_cos, 0, 1) { |
| 27 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 28 | return Double::New(cos(operand.value())); |
| 29 | } |
| 30 | |
| 31 | DEFINE_NATIVE_ENTRY(Math_tan, 0, 1) { |
| 32 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 33 | return Double::New(tan(operand.value())); |
| 34 | } |
| 35 | |
| 36 | DEFINE_NATIVE_ENTRY(Math_asin, 0, 1) { |
| 37 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 38 | return Double::New(asin(operand.value())); |
| 39 | } |
| 40 | |
| 41 | DEFINE_NATIVE_ENTRY(Math_acos, 0, 1) { |
| 42 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 43 | return Double::New(acos(operand.value())); |
| 44 | } |
| 45 | |
| 46 | DEFINE_NATIVE_ENTRY(Math_atan, 0, 1) { |
| 47 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 48 | return Double::New(atan(operand.value())); |
| 49 | } |
| 50 | |
| 51 | DEFINE_NATIVE_ENTRY(Math_atan2, 0, 2) { |
| 52 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand1, arguments->NativeArgAt(0)); |
| 53 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand2, arguments->NativeArgAt(1)); |
| 54 | return Double::New(atan2_ieee(operand1.value(), operand2.value())); |
| 55 | } |
| 56 | |
| 57 | DEFINE_NATIVE_ENTRY(Math_exp, 0, 1) { |
| 58 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 59 | return Double::New(exp(operand.value())); |
| 60 | } |
| 61 | |
| 62 | DEFINE_NATIVE_ENTRY(Math_log, 0, 1) { |
| 63 | GET_NON_NULL_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 64 | return Double::New(log(operand.value())); |
| 65 | } |
| 66 | |
| 67 | DEFINE_NATIVE_ENTRY(Math_doublePow, 0, 2) { |
| 68 | const double operand = |
| 69 | Double::CheckedHandle(zone, arguments->NativeArgAt(0)).value(); |
| 70 | GET_NON_NULL_NATIVE_ARGUMENT(Double, exponent_object, |
| 71 | arguments->NativeArgAt(1)); |
| 72 | const double exponent = exponent_object.value(); |
| 73 | return Double::New(pow(operand, exponent)); |
| 74 | } |
| 75 | |
| 76 | // Returns the typed-data array store in '_Random._state' field. |
| 77 | static TypedDataPtr GetRandomStateArray(const Instance& receiver) { |
| 78 | const Class& random_class = Class::Handle(receiver.clazz()); |
| 79 | const Field& state_field = |
| 80 | Field::Handle(random_class.LookupFieldAllowPrivate(Symbols::_state())); |
| 81 | ASSERT(!state_field.IsNull()); |
| 82 | const Instance& state_field_value = |
| 83 | Instance::Cast(Object::Handle(receiver.GetField(state_field))); |
| 84 | ASSERT(!state_field_value.IsNull()); |
| 85 | ASSERT(state_field_value.IsTypedData()); |
| 86 | const TypedData& array = TypedData::Cast(state_field_value); |
| 87 | ASSERT(array.Length() == 2); |
| 88 | ASSERT(array.ElementType() == kUint32ArrayElement); |
| 89 | return array.raw(); |
| 90 | } |
| 91 | |
| 92 | // Implements: |
| 93 | // var state = |
| 94 | // ((_A * (_state[_kSTATE_LO])) + _state[_kSTATE_HI]) & (1 << 64) - 1); |
| 95 | // _state[_kSTATE_LO] = state & (1 << 32) - 1); |
| 96 | // _state[_kSTATE_HI] = state >> 32; |
| 97 | DEFINE_NATIVE_ENTRY(Random_nextState, 0, 1) { |
| 98 | GET_NON_NULL_NATIVE_ARGUMENT(Instance, receiver, arguments->NativeArgAt(0)); |
| 99 | const TypedData& array = TypedData::Handle(GetRandomStateArray(receiver)); |
| 100 | const uint64_t state_lo = array.GetUint32(0); |
| 101 | const uint64_t state_hi = array.GetUint32(array.ElementSizeInBytes()); |
| 102 | const uint64_t A = 0xffffda61; |
| 103 | uint64_t state = (A * state_lo) + state_hi; |
| 104 | array.SetUint32(0, static_cast<uint32_t>(state)); |
| 105 | array.SetUint32(array.ElementSizeInBytes(), |
| 106 | static_cast<uint32_t>(state >> 32)); |
| 107 | return Object::null(); |
| 108 | } |
| 109 | |
| 110 | TypedDataPtr CreateRandomState(Zone* zone, uint64_t seed) { |
| 111 | const TypedData& result = |
| 112 | TypedData::Handle(zone, TypedData::New(kTypedDataUint32ArrayCid, 2)); |
| 113 | result.SetUint32(0, static_cast<uint32_t>(seed)); |
| 114 | result.SetUint32(result.ElementSizeInBytes(), |
| 115 | static_cast<uint32_t>(seed >> 32)); |
| 116 | return result.raw(); |
| 117 | } |
| 118 | |
| 119 | uint64_t mix64(uint64_t n) { |
| 120 | // Thomas Wang 64-bit mix. |
| 121 | // http://www.concentric.net/~Ttwang/tech/inthash.htm |
| 122 | // via. http://web.archive.org/web/20071223173210/http://www.concentric.net/~Ttwang/tech/inthash.htm |
| 123 | n = (~n) + (n << 21); // n = (n << 21) - n - 1; |
| 124 | n = n ^ (n >> 24); |
| 125 | n = n * 265; // n = (n + (n << 3)) + (n << 8); |
| 126 | n = n ^ (n >> 14); |
| 127 | n = n * 21; // n = (n + (n << 2)) + (n << 4); |
| 128 | n = n ^ (n >> 28); |
| 129 | n = n + (n << 31); |
| 130 | return n; |
| 131 | } |
| 132 | |
| 133 | // Implements: |
| 134 | // uint64_t hash = 0; |
| 135 | // do { |
| 136 | // hash = hash * 1037 ^ mix64((uint64_t)seed); |
| 137 | // seed >>= 64; |
| 138 | // } while (seed != 0 && seed != -1); // Limits if seed positive or negative. |
| 139 | // if (hash == 0) { |
| 140 | // hash = 0x5A17; |
| 141 | // } |
| 142 | // var result = new Uint32List(2); |
| 143 | // result[_kSTATE_LO] = seed & ((1 << 32) - 1); |
| 144 | // result[_kSTATE_HI] = seed >> 32; |
| 145 | // return result; |
| 146 | DEFINE_NATIVE_ENTRY(Random_setupSeed, 0, 1) { |
| 147 | GET_NON_NULL_NATIVE_ARGUMENT(Integer, seed_int, arguments->NativeArgAt(0)); |
| 148 | uint64_t seed = mix64(static_cast<uint64_t>(seed_int.AsInt64Value())); |
| 149 | |
| 150 | if (seed == 0) { |
| 151 | seed = 0x5a17; |
| 152 | } |
| 153 | return CreateRandomState(zone, seed); |
| 154 | } |
| 155 | |
| 156 | DEFINE_NATIVE_ENTRY(Random_initialSeed, 0, 0) { |
| 157 | Random* rnd = isolate->random(); |
| 158 | uint64_t seed = rnd->NextUInt32(); |
| 159 | seed |= (static_cast<uint64_t>(rnd->NextUInt32()) << 32); |
| 160 | return CreateRandomState(zone, seed); |
| 161 | } |
| 162 | |
| 163 | DEFINE_NATIVE_ENTRY(SecureRandom_getBytes, 0, 1) { |
| 164 | GET_NON_NULL_NATIVE_ARGUMENT(Smi, count, arguments->NativeArgAt(0)); |
| 165 | const intptr_t n = count.Value(); |
| 166 | ASSERT((n > 0) && (n <= 8)); |
| 167 | uint8_t buffer[8]; |
| 168 | Dart_EntropySource entropy_source = Dart::entropy_source_callback(); |
| 169 | if ((entropy_source == NULL) || !entropy_source(buffer, n)) { |
| 170 | const String& error = String::Handle(String::New( |
| 171 | "No source of cryptographically secure random numbers available." )); |
| 172 | const Array& args = Array::Handle(Array::New(1)); |
| 173 | args.SetAt(0, error); |
| 174 | Exceptions::ThrowByType(Exceptions::kUnsupported, args); |
| 175 | } |
| 176 | uint64_t result = 0; |
| 177 | for (intptr_t i = 0; i < n; i++) { |
| 178 | result = (result << 8) | buffer[i]; |
| 179 | } |
| 180 | return Integer::New(result); |
| 181 | } |
| 182 | |
| 183 | } // namespace dart |
| 184 | |