| 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/bigint_operations.h" | 9 #include "vm/bigint_operations.h" |
| 10 #include "vm/exceptions.h" | 10 #include "vm/exceptions.h" |
| 11 #include "vm/native_entry.h" | 11 #include "vm/native_entry.h" |
| 12 #include "vm/object.h" | 12 #include "vm/object.h" |
| 13 #include "vm/random.h" | 13 #include "vm/random.h" |
| 14 #include "vm/scanner.h" | 14 #include "vm/scanner.h" |
| 15 #include "vm/symbols.h" | 15 #include "vm/symbols.h" |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 | 18 |
| 19 DEFINE_NATIVE_ENTRY(MathNatives_sqrt, 1) { | 19 DEFINE_NATIVE_ENTRY(Math_sqrt, 1) { |
| 20 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); | 20 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 21 return Double::New(sqrt(operand.value())); | 21 return Double::New(sqrt(operand.value())); |
| 22 } | 22 } |
| 23 | 23 |
| 24 DEFINE_NATIVE_ENTRY(MathNatives_sin, 1) { | 24 DEFINE_NATIVE_ENTRY(Math_sin, 1) { |
| 25 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); | 25 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 26 return Double::New(sin(operand.value())); | 26 return Double::New(sin(operand.value())); |
| 27 } | 27 } |
| 28 | 28 |
| 29 DEFINE_NATIVE_ENTRY(MathNatives_cos, 1) { | 29 DEFINE_NATIVE_ENTRY(Math_cos, 1) { |
| 30 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); | 30 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 31 return Double::New(cos(operand.value())); | 31 return Double::New(cos(operand.value())); |
| 32 } | 32 } |
| 33 | 33 |
| 34 DEFINE_NATIVE_ENTRY(MathNatives_tan, 1) { | 34 DEFINE_NATIVE_ENTRY(Math_tan, 1) { |
| 35 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); | 35 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 36 return Double::New(tan(operand.value())); | 36 return Double::New(tan(operand.value())); |
| 37 } | 37 } |
| 38 | 38 |
| 39 DEFINE_NATIVE_ENTRY(MathNatives_asin, 1) { | 39 DEFINE_NATIVE_ENTRY(Math_asin, 1) { |
| 40 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); | 40 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 41 return Double::New(asin(operand.value())); | 41 return Double::New(asin(operand.value())); |
| 42 } | 42 } |
| 43 | 43 |
| 44 DEFINE_NATIVE_ENTRY(MathNatives_acos, 1) { | 44 DEFINE_NATIVE_ENTRY(Math_acos, 1) { |
| 45 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); | 45 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 46 return Double::New(acos(operand.value())); | 46 return Double::New(acos(operand.value())); |
| 47 } | 47 } |
| 48 | 48 |
| 49 DEFINE_NATIVE_ENTRY(MathNatives_atan, 1) { | 49 DEFINE_NATIVE_ENTRY(Math_atan, 1) { |
| 50 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); | 50 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 51 return Double::New(atan(operand.value())); | 51 return Double::New(atan(operand.value())); |
| 52 } | 52 } |
| 53 | 53 |
| 54 DEFINE_NATIVE_ENTRY(MathNatives_atan2, 2) { | 54 DEFINE_NATIVE_ENTRY(Math_atan2, 2) { |
| 55 GET_NATIVE_ARGUMENT(Double, operand1, arguments->NativeArgAt(0)); | 55 GET_NATIVE_ARGUMENT(Double, operand1, arguments->NativeArgAt(0)); |
| 56 GET_NATIVE_ARGUMENT(Double, operand2, arguments->NativeArgAt(1)); | 56 GET_NATIVE_ARGUMENT(Double, operand2, arguments->NativeArgAt(1)); |
| 57 return Double::New(atan2(operand1.value(), operand2.value())); | 57 return Double::New(atan2(operand1.value(), operand2.value())); |
| 58 } | 58 } |
| 59 | 59 |
| 60 DEFINE_NATIVE_ENTRY(MathNatives_exp, 1) { | 60 DEFINE_NATIVE_ENTRY(Math_exp, 1) { |
| 61 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); | 61 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 62 return Double::New(exp(operand.value())); | 62 return Double::New(exp(operand.value())); |
| 63 } | 63 } |
| 64 | 64 |
| 65 DEFINE_NATIVE_ENTRY(MathNatives_log, 1) { | 65 DEFINE_NATIVE_ENTRY(Math_log, 1) { |
| 66 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); | 66 GET_NATIVE_ARGUMENT(Double, operand, arguments->NativeArgAt(0)); |
| 67 return Double::New(log(operand.value())); | 67 return Double::New(log(operand.value())); |
| 68 } | 68 } |
| 69 | 69 |
| 70 DEFINE_NATIVE_ENTRY(MathNatives_random, 0) { | 70 DEFINE_NATIVE_ENTRY(Math_random, 0) { |
| 71 return Double::New(static_cast<double>(Random::RandomInt32()-1)/0x80000000); | 71 return Double::New(static_cast<double>(Random::RandomInt32()-1)/0x80000000); |
| 72 } | 72 } |
| 73 | 73 |
| 74 | |
| 75 // TODO(srdjan): Investigate for performance hit; the integer and double parsing | |
| 76 // may not be efficient as we need to generate two extra growable arrays. | |
| 77 static bool IsValidLiteral(const Scanner::GrowableTokenStream& tokens, | |
| 78 Token::Kind literal_kind, | |
| 79 bool* is_positive, | |
| 80 String** value) { | |
| 81 if ((tokens.length() == 2) && | |
| 82 (tokens[0].kind == literal_kind) && | |
| 83 (tokens[1].kind == Token::kEOS)) { | |
| 84 *is_positive = true; | |
| 85 *value = tokens[0].literal; | |
| 86 return true; | |
| 87 } | |
| 88 if ((tokens.length() == 3) && | |
| 89 ((tokens[0].kind == Token::kTIGHTADD) || | |
| 90 (tokens[0].kind == Token::kSUB)) && | |
| 91 (tokens[1].kind == literal_kind) && | |
| 92 (tokens[2].kind == Token::kEOS)) { | |
| 93 // Check there is no space between "+/-" and number. | |
| 94 if ((tokens[0].offset + 1) != tokens[1].offset) { | |
| 95 return false; | |
| 96 } | |
| 97 *is_positive = tokens[0].kind == Token::kTIGHTADD; | |
| 98 *value = tokens[1].literal; | |
| 99 return true; | |
| 100 } | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 | |
| 105 DEFINE_NATIVE_ENTRY(MathNatives_parseInt, 1) { | |
| 106 GET_NATIVE_ARGUMENT(String, value, arguments->NativeArgAt(0)); | |
| 107 const String& dummy_key = String::Handle(Symbols::Empty()); | |
| 108 Scanner scanner(value, dummy_key); | |
| 109 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); | |
| 110 String* int_string; | |
| 111 bool is_positive; | |
| 112 if (IsValidLiteral(tokens, Token::kINTEGER, &is_positive, &int_string)) { | |
| 113 if (is_positive) { | |
| 114 return Integer::New(*int_string); | |
| 115 } else { | |
| 116 String& temp = String::Handle(); | |
| 117 temp = String::Concat(String::Handle(Symbols::New("-")), | |
| 118 *int_string); | |
| 119 return Integer::New(temp); | |
| 120 } | |
| 121 } else { | |
| 122 GrowableArray<const Object*> args; | |
| 123 args.Add(&value); | |
| 124 Exceptions::ThrowByType(Exceptions::kFormat, args); | |
| 125 return Object::null(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 | |
| 130 DEFINE_NATIVE_ENTRY(MathNatives_parseDouble, 1) { | |
| 131 GET_NATIVE_ARGUMENT(String, value, arguments->NativeArgAt(0)); | |
| 132 const String& dummy_key = String::Handle(Symbols::Empty()); | |
| 133 Scanner scanner(value, dummy_key); | |
| 134 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); | |
| 135 String* number_string; | |
| 136 bool is_positive; | |
| 137 if (IsValidLiteral(tokens, Token::kDOUBLE, &is_positive, &number_string)) { | |
| 138 const char* cstr = number_string->ToCString(); | |
| 139 char* p_end = NULL; | |
| 140 double double_value = strtod(cstr, &p_end); | |
| 141 ASSERT(p_end != cstr); | |
| 142 if (!is_positive) { | |
| 143 double_value = -double_value; | |
| 144 } | |
| 145 return Double::New(double_value); | |
| 146 } | |
| 147 | |
| 148 if (IsValidLiteral(tokens, Token::kINTEGER, &is_positive, &number_string)) { | |
| 149 Integer& res = Integer::Handle(Integer::New(*number_string)); | |
| 150 if (is_positive) { | |
| 151 return Double::New(res.AsDoubleValue()); | |
| 152 } else { | |
| 153 return Double::New(-res.AsDoubleValue()); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 // Infinity and nan. | |
| 158 if (IsValidLiteral(tokens, Token::kIDENT, &is_positive, &number_string)) { | |
| 159 if (number_string->Equals("NaN")) { | |
| 160 return Double::New(NAN); | |
| 161 } | |
| 162 if (number_string->Equals("Infinity")) { | |
| 163 if (is_positive) { | |
| 164 return Double::New(INFINITY); | |
| 165 } else { | |
| 166 return Double::New(-INFINITY); | |
| 167 } | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 GrowableArray<const Object*> args; | |
| 172 args.Add(&value); | |
| 173 Exceptions::ThrowByType(Exceptions::kFormat, args); | |
| 174 return Object::null(); | |
| 175 } | |
| 176 | |
| 177 } // namespace dart | 74 } // namespace dart |
| OLD | NEW |