| 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 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 | 17 |
| 18 DEFINE_NATIVE_ENTRY(MathNatives_sqrt, 1) { | 18 DEFINE_NATIVE_ENTRY(MathNatives_sqrt, 1) { |
| 19 const double operand = Double::CheckedHandle(arguments->At(0)).value(); | 19 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); |
| 20 arguments->SetReturn(Double::Handle(Double::New(sqrt(operand)))); | 20 arguments->SetReturn(Double::Handle(Double::New(sqrt(operand.value())))); |
| 21 } | 21 } |
| 22 | 22 |
| 23 DEFINE_NATIVE_ENTRY(MathNatives_sin, 1) { | 23 DEFINE_NATIVE_ENTRY(MathNatives_sin, 1) { |
| 24 const double operand = Double::CheckedHandle(arguments->At(0)).value(); | 24 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); |
| 25 arguments->SetReturn(Double::Handle(Double::New(sin(operand)))); | 25 arguments->SetReturn(Double::Handle(Double::New(sin(operand.value())))); |
| 26 } | 26 } |
| 27 | 27 |
| 28 DEFINE_NATIVE_ENTRY(MathNatives_cos, 1) { | 28 DEFINE_NATIVE_ENTRY(MathNatives_cos, 1) { |
| 29 const double operand = Double::CheckedHandle(arguments->At(0)).value(); | 29 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); |
| 30 arguments->SetReturn(Double::Handle(Double::New(cos(operand)))); | 30 arguments->SetReturn(Double::Handle(Double::New(cos(operand.value())))); |
| 31 } | 31 } |
| 32 | 32 |
| 33 DEFINE_NATIVE_ENTRY(MathNatives_tan, 1) { | 33 DEFINE_NATIVE_ENTRY(MathNatives_tan, 1) { |
| 34 const double operand = Double::CheckedHandle(arguments->At(0)).value(); | 34 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); |
| 35 arguments->SetReturn(Double::Handle(Double::New(tan(operand)))); | 35 arguments->SetReturn(Double::Handle(Double::New(tan(operand.value())))); |
| 36 } | 36 } |
| 37 | 37 |
| 38 DEFINE_NATIVE_ENTRY(MathNatives_asin, 1) { | 38 DEFINE_NATIVE_ENTRY(MathNatives_asin, 1) { |
| 39 const double operand = Double::CheckedHandle(arguments->At(0)).value(); | 39 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); |
| 40 arguments->SetReturn(Double::Handle(Double::New(asin(operand)))); | 40 arguments->SetReturn(Double::Handle(Double::New(asin(operand.value())))); |
| 41 } | 41 } |
| 42 | 42 |
| 43 DEFINE_NATIVE_ENTRY(MathNatives_acos, 1) { | 43 DEFINE_NATIVE_ENTRY(MathNatives_acos, 1) { |
| 44 const double operand = Double::CheckedHandle(arguments->At(0)).value(); | 44 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); |
| 45 arguments->SetReturn(Double::Handle(Double::New(acos(operand)))); | 45 arguments->SetReturn(Double::Handle(Double::New(acos(operand.value())))); |
| 46 } | 46 } |
| 47 | 47 |
| 48 DEFINE_NATIVE_ENTRY(MathNatives_atan, 1) { | 48 DEFINE_NATIVE_ENTRY(MathNatives_atan, 1) { |
| 49 const double operand = Double::CheckedHandle(arguments->At(0)).value(); | 49 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); |
| 50 arguments->SetReturn(Double::Handle(Double::New(atan(operand)))); | 50 arguments->SetReturn(Double::Handle(Double::New(atan(operand.value())))); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // It is not possible to call the native MathNatives_atan2. Somehow this leads | 53 // It is not possible to call the native MathNatives_atan2. Somehow this leads |
| 54 // to a dynamic error "native function 'MathNatives_atan2' cannot be found". | 54 // to a dynamic error "native function 'MathNatives_atan2' cannot be found". |
| 55 DEFINE_NATIVE_ENTRY(MathNatives_2atan, 2) { | 55 DEFINE_NATIVE_ENTRY(MathNatives_2atan, 2) { |
| 56 const double operand1 = Double::CheckedHandle(arguments->At(0)).value(); | 56 GET_NATIVE_ARGUMENT(Double, operand1, arguments->At(0)); |
| 57 const double operand2 = Double::CheckedHandle(arguments->At(1)).value(); | 57 GET_NATIVE_ARGUMENT(Double, operand2, arguments->At(1)); |
| 58 arguments->SetReturn(Double::Handle(Double::New(atan2(operand1, operand2)))); | 58 arguments->SetReturn(Double::Handle(Double::New( |
| 59 atan2(operand1.value(), operand2.value())))); |
| 59 } | 60 } |
| 60 | 61 |
| 61 DEFINE_NATIVE_ENTRY(MathNatives_exp, 1) { | 62 DEFINE_NATIVE_ENTRY(MathNatives_exp, 1) { |
| 62 const double operand = Double::CheckedHandle(arguments->At(0)).value(); | 63 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); |
| 63 arguments->SetReturn(Double::Handle(Double::New(exp(operand)))); | 64 arguments->SetReturn(Double::Handle(Double::New(exp(operand.value())))); |
| 64 } | 65 } |
| 65 | 66 |
| 66 DEFINE_NATIVE_ENTRY(MathNatives_log, 1) { | 67 DEFINE_NATIVE_ENTRY(MathNatives_log, 1) { |
| 67 const double operand = Double::CheckedHandle(arguments->At(0)).value(); | 68 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); |
| 68 arguments->SetReturn(Double::Handle(Double::New(log(operand)))); | 69 arguments->SetReturn(Double::Handle(Double::New(log(operand.value())))); |
| 69 } | 70 } |
| 70 | 71 |
| 71 DEFINE_NATIVE_ENTRY(MathNatives_random, 0) { | 72 DEFINE_NATIVE_ENTRY(MathNatives_random, 0) { |
| 72 arguments->SetReturn(Double::Handle(Double:: | 73 arguments->SetReturn(Double::Handle(Double:: |
| 73 New(static_cast<double>(Random::RandomInt32()-1)/0x80000000))); | 74 New(static_cast<double>(Random::RandomInt32()-1)/0x80000000))); |
| 74 } | 75 } |
| 75 | 76 |
| 76 | 77 |
| 77 // TODO(srdjan): Investigate for performance hit; the integer and double parsing | 78 // TODO(srdjan): Investigate for performance hit; the integer and double parsing |
| 78 // may not be efficient as we need to generate two extra growable arrays. | 79 // may not be efficient as we need to generate two extra growable arrays. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 97 } | 98 } |
| 98 *is_positive = tokens[0].kind == Token::kADD; | 99 *is_positive = tokens[0].kind == Token::kADD; |
| 99 *value = tokens[1].literal; | 100 *value = tokens[1].literal; |
| 100 return true; | 101 return true; |
| 101 } | 102 } |
| 102 return false; | 103 return false; |
| 103 } | 104 } |
| 104 | 105 |
| 105 | 106 |
| 106 DEFINE_NATIVE_ENTRY(MathNatives_parseInt, 1) { | 107 DEFINE_NATIVE_ENTRY(MathNatives_parseInt, 1) { |
| 107 const String& value = String::CheckedHandle(arguments->At(0)); | 108 GET_NATIVE_ARGUMENT(String, value, arguments->At(0)); |
| 108 Scanner scanner(value, String::Handle()); | 109 Scanner scanner(value, String::Handle()); |
| 109 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); | 110 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); |
| 110 String* int_string; | 111 String* int_string; |
| 111 bool is_positive; | 112 bool is_positive; |
| 112 if (IsValidLiteral(tokens, Token::kINTEGER, &is_positive, &int_string)) { | 113 if (IsValidLiteral(tokens, Token::kINTEGER, &is_positive, &int_string)) { |
| 113 Integer& result = Integer::Handle(); | 114 Integer& result = Integer::Handle(); |
| 114 if (is_positive) { | 115 if (is_positive) { |
| 115 result = Integer::New(*int_string); | 116 result = Integer::New(*int_string); |
| 116 } else { | 117 } else { |
| 117 String& temp = String::Handle(); | 118 String& temp = String::Handle(); |
| 118 temp = String::Concat(String::Handle(String::NewSymbol("-")), | 119 temp = String::Concat(String::Handle(String::NewSymbol("-")), |
| 119 *int_string); | 120 *int_string); |
| 120 result = Integer::New(temp); | 121 result = Integer::New(temp); |
| 121 } | 122 } |
| 122 arguments->SetReturn(result); | 123 arguments->SetReturn(result); |
| 123 } else { | 124 } else { |
| 124 GrowableArray<const Object*> args; | 125 GrowableArray<const Object*> args; |
| 125 args.Add(&value); | 126 args.Add(&value); |
| 126 Exceptions::ThrowByType(Exceptions::kBadNumberFormat, args); | 127 Exceptions::ThrowByType(Exceptions::kBadNumberFormat, args); |
| 127 } | 128 } |
| 128 } | 129 } |
| 129 | 130 |
| 130 | 131 |
| 131 DEFINE_NATIVE_ENTRY(MathNatives_parseDouble, 1) { | 132 DEFINE_NATIVE_ENTRY(MathNatives_parseDouble, 1) { |
| 132 const String& value = String::CheckedHandle(arguments->At(0)); | 133 GET_NATIVE_ARGUMENT(String, value, arguments->At(0)); |
| 133 Scanner scanner(value, String::Handle()); | 134 Scanner scanner(value, String::Handle()); |
| 134 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); | 135 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); |
| 135 String* number_string; | 136 String* number_string; |
| 136 bool is_positive; | 137 bool is_positive; |
| 137 if (IsValidLiteral(tokens, Token::kDOUBLE, &is_positive, &number_string)) { | 138 if (IsValidLiteral(tokens, Token::kDOUBLE, &is_positive, &number_string)) { |
| 138 const char* cstr = number_string->ToCString(); | 139 const char* cstr = number_string->ToCString(); |
| 139 char* p_end = NULL; | 140 char* p_end = NULL; |
| 140 double double_value = strtod(cstr, &p_end); | 141 double double_value = strtod(cstr, &p_end); |
| 141 ASSERT(p_end != cstr); | 142 ASSERT(p_end != cstr); |
| 142 if (!is_positive) { | 143 if (!is_positive) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 172 return; | 173 return; |
| 173 } | 174 } |
| 174 } | 175 } |
| 175 | 176 |
| 176 GrowableArray<const Object*> args; | 177 GrowableArray<const Object*> args; |
| 177 args.Add(&value); | 178 args.Add(&value); |
| 178 Exceptions::ThrowByType(Exceptions::kBadNumberFormat, args); | 179 Exceptions::ThrowByType(Exceptions::kBadNumberFormat, args); |
| 179 } | 180 } |
| 180 | 181 |
| 181 } // namespace dart | 182 } // namespace dart |
| OLD | NEW |