| 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(MathNatives_sqrt, 1) { |
| 20 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(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(MathNatives_sin, 1) { |
| 25 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(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(MathNatives_cos, 1) { |
| 30 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(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(MathNatives_tan, 1) { |
| 35 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(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(MathNatives_asin, 1) { |
| 40 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(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(MathNatives_acos, 1) { |
| 45 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(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(MathNatives_atan, 1) { |
| 50 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(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(MathNatives_atan2, 2) { |
| 55 GET_NATIVE_ARGUMENT(Double, operand1, arguments->At(0)); | 55 GET_NATIVE_ARGUMENT(Double, operand1, arguments->NativeArgAt(0)); |
| 56 GET_NATIVE_ARGUMENT(Double, operand2, arguments->At(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(MathNatives_exp, 1) { |
| 61 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(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(MathNatives_log, 1) { |
| 66 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(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(MathNatives_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 | 74 |
| 75 // TODO(srdjan): Investigate for performance hit; the integer and double parsing | 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. | 76 // may not be efficient as we need to generate two extra growable arrays. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 96 } | 96 } |
| 97 *is_positive = tokens[0].kind == Token::kTIGHTADD; | 97 *is_positive = tokens[0].kind == Token::kTIGHTADD; |
| 98 *value = tokens[1].literal; | 98 *value = tokens[1].literal; |
| 99 return true; | 99 return true; |
| 100 } | 100 } |
| 101 return false; | 101 return false; |
| 102 } | 102 } |
| 103 | 103 |
| 104 | 104 |
| 105 DEFINE_NATIVE_ENTRY(MathNatives_parseInt, 1) { | 105 DEFINE_NATIVE_ENTRY(MathNatives_parseInt, 1) { |
| 106 GET_NATIVE_ARGUMENT(String, value, arguments->At(0)); | 106 GET_NATIVE_ARGUMENT(String, value, arguments->NativeArgAt(0)); |
| 107 const String& dummy_key = String::Handle(Symbols::Empty()); | 107 const String& dummy_key = String::Handle(Symbols::Empty()); |
| 108 Scanner scanner(value, dummy_key); | 108 Scanner scanner(value, dummy_key); |
| 109 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); | 109 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); |
| 110 String* int_string; | 110 String* int_string; |
| 111 bool is_positive; | 111 bool is_positive; |
| 112 if (IsValidLiteral(tokens, Token::kINTEGER, &is_positive, &int_string)) { | 112 if (IsValidLiteral(tokens, Token::kINTEGER, &is_positive, &int_string)) { |
| 113 if (is_positive) { | 113 if (is_positive) { |
| 114 return Integer::New(*int_string); | 114 return Integer::New(*int_string); |
| 115 } else { | 115 } else { |
| 116 String& temp = String::Handle(); | 116 String& temp = String::Handle(); |
| 117 temp = String::Concat(String::Handle(Symbols::New("-")), | 117 temp = String::Concat(String::Handle(Symbols::New("-")), |
| 118 *int_string); | 118 *int_string); |
| 119 return Integer::New(temp); | 119 return Integer::New(temp); |
| 120 } | 120 } |
| 121 } else { | 121 } else { |
| 122 GrowableArray<const Object*> args; | 122 GrowableArray<const Object*> args; |
| 123 args.Add(&value); | 123 args.Add(&value); |
| 124 Exceptions::ThrowByType(Exceptions::kFormat, args); | 124 Exceptions::ThrowByType(Exceptions::kFormat, args); |
| 125 return Object::null(); | 125 return Object::null(); |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 | 128 |
| 129 | 129 |
| 130 DEFINE_NATIVE_ENTRY(MathNatives_parseDouble, 1) { | 130 DEFINE_NATIVE_ENTRY(MathNatives_parseDouble, 1) { |
| 131 GET_NATIVE_ARGUMENT(String, value, arguments->At(0)); | 131 GET_NATIVE_ARGUMENT(String, value, arguments->NativeArgAt(0)); |
| 132 const String& dummy_key = String::Handle(Symbols::Empty()); | 132 const String& dummy_key = String::Handle(Symbols::Empty()); |
| 133 Scanner scanner(value, dummy_key); | 133 Scanner scanner(value, dummy_key); |
| 134 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); | 134 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); |
| 135 String* number_string; | 135 String* number_string; |
| 136 bool is_positive; | 136 bool is_positive; |
| 137 if (IsValidLiteral(tokens, Token::kDOUBLE, &is_positive, &number_string)) { | 137 if (IsValidLiteral(tokens, Token::kDOUBLE, &is_positive, &number_string)) { |
| 138 const char* cstr = number_string->ToCString(); | 138 const char* cstr = number_string->ToCString(); |
| 139 char* p_end = NULL; | 139 char* p_end = NULL; |
| 140 double double_value = strtod(cstr, &p_end); | 140 double double_value = strtod(cstr, &p_end); |
| 141 ASSERT(p_end != cstr); | 141 ASSERT(p_end != cstr); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 168 } | 168 } |
| 169 } | 169 } |
| 170 | 170 |
| 171 GrowableArray<const Object*> args; | 171 GrowableArray<const Object*> args; |
| 172 args.Add(&value); | 172 args.Add(&value); |
| 173 Exceptions::ThrowByType(Exceptions::kFormat, args); | 173 Exceptions::ThrowByType(Exceptions::kFormat, args); |
| 174 return Object::null(); | 174 return Object::null(); |
| 175 } | 175 } |
| 176 | 176 |
| 177 } // namespace dart | 177 } // namespace dart |
| OLD | NEW |