Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: lib/math.cc

Issue 10874072: Use the return value of vm native methods to set the return value, (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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->At(0));
21 arguments->SetReturn(Double::Handle(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->At(0));
26 arguments->SetReturn(Double::Handle(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->At(0));
31 arguments->SetReturn(Double::Handle(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->At(0));
36 arguments->SetReturn(Double::Handle(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->At(0));
41 arguments->SetReturn(Double::Handle(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->At(0));
46 arguments->SetReturn(Double::Handle(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->At(0));
51 arguments->SetReturn(Double::Handle(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->At(0));
56 GET_NATIVE_ARGUMENT(Double, operand2, arguments->At(1)); 56 GET_NATIVE_ARGUMENT(Double, operand2, arguments->At(1));
57 arguments->SetReturn(Double::Handle(Double::New( 57 return Double::New(atan2(operand1.value(), operand2.value()));
58 atan2(operand1.value(), operand2.value()))));
59 } 58 }
60 59
61 DEFINE_NATIVE_ENTRY(MathNatives_exp, 1) { 60 DEFINE_NATIVE_ENTRY(MathNatives_exp, 1) {
62 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); 61 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0));
63 arguments->SetReturn(Double::Handle(Double::New(exp(operand.value())))); 62 return Double::New(exp(operand.value()));
64 } 63 }
65 64
66 DEFINE_NATIVE_ENTRY(MathNatives_log, 1) { 65 DEFINE_NATIVE_ENTRY(MathNatives_log, 1) {
67 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0)); 66 GET_NATIVE_ARGUMENT(Double, operand, arguments->At(0));
68 arguments->SetReturn(Double::Handle(Double::New(log(operand.value())))); 67 return Double::New(log(operand.value()));
69 } 68 }
70 69
71 DEFINE_NATIVE_ENTRY(MathNatives_random, 0) { 70 DEFINE_NATIVE_ENTRY(MathNatives_random, 0) {
72 arguments->SetReturn(Double::Handle(Double:: 71 return Double::New(static_cast<double>(Random::RandomInt32()-1)/0x80000000);
73 New(static_cast<double>(Random::RandomInt32()-1)/0x80000000)));
74 } 72 }
75 73
76 74
77 // TODO(srdjan): Investigate for performance hit; the integer and double parsing 75 // 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. 76 // may not be efficient as we need to generate two extra growable arrays.
79 static bool IsValidLiteral(const Scanner::GrowableTokenStream& tokens, 77 static bool IsValidLiteral(const Scanner::GrowableTokenStream& tokens,
80 Token::Kind literal_kind, 78 Token::Kind literal_kind,
81 bool* is_positive, 79 bool* is_positive,
82 String** value) { 80 String** value) {
83 if ((tokens.length() == 2) && 81 if ((tokens.length() == 2) &&
(...skipping 20 matching lines...) Expand all
104 } 102 }
105 103
106 104
107 DEFINE_NATIVE_ENTRY(MathNatives_parseInt, 1) { 105 DEFINE_NATIVE_ENTRY(MathNatives_parseInt, 1) {
108 GET_NATIVE_ARGUMENT(String, value, arguments->At(0)); 106 GET_NATIVE_ARGUMENT(String, value, arguments->At(0));
109 Scanner scanner(value, String::Handle()); 107 Scanner scanner(value, String::Handle());
110 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); 108 const Scanner::GrowableTokenStream& tokens = scanner.GetStream();
111 String* int_string; 109 String* int_string;
112 bool is_positive; 110 bool is_positive;
113 if (IsValidLiteral(tokens, Token::kINTEGER, &is_positive, &int_string)) { 111 if (IsValidLiteral(tokens, Token::kINTEGER, &is_positive, &int_string)) {
114 Integer& result = Integer::Handle();
115 if (is_positive) { 112 if (is_positive) {
116 result = Integer::New(*int_string); 113 return Integer::New(*int_string);
117 } else { 114 } else {
118 String& temp = String::Handle(); 115 String& temp = String::Handle();
119 temp = String::Concat(String::Handle(Symbols::New("-")), 116 temp = String::Concat(String::Handle(Symbols::New("-")),
120 *int_string); 117 *int_string);
121 result = Integer::New(temp); 118 return Integer::New(temp);
122 } 119 }
123 arguments->SetReturn(result);
124 } else { 120 } else {
125 GrowableArray<const Object*> args; 121 GrowableArray<const Object*> args;
126 args.Add(&value); 122 args.Add(&value);
127 Exceptions::ThrowByType(Exceptions::kFormat, args); 123 Exceptions::ThrowByType(Exceptions::kFormat, args);
124 return Object::null();
128 } 125 }
129 } 126 }
130 127
131 128
132 DEFINE_NATIVE_ENTRY(MathNatives_parseDouble, 1) { 129 DEFINE_NATIVE_ENTRY(MathNatives_parseDouble, 1) {
133 GET_NATIVE_ARGUMENT(String, value, arguments->At(0)); 130 GET_NATIVE_ARGUMENT(String, value, arguments->At(0));
134 Scanner scanner(value, String::Handle()); 131 Scanner scanner(value, String::Handle());
135 const Scanner::GrowableTokenStream& tokens = scanner.GetStream(); 132 const Scanner::GrowableTokenStream& tokens = scanner.GetStream();
136 String* number_string; 133 String* number_string;
137 bool is_positive; 134 bool is_positive;
138 if (IsValidLiteral(tokens, Token::kDOUBLE, &is_positive, &number_string)) { 135 if (IsValidLiteral(tokens, Token::kDOUBLE, &is_positive, &number_string)) {
139 const char* cstr = number_string->ToCString(); 136 const char* cstr = number_string->ToCString();
140 char* p_end = NULL; 137 char* p_end = NULL;
141 double double_value = strtod(cstr, &p_end); 138 double double_value = strtod(cstr, &p_end);
142 ASSERT(p_end != cstr); 139 ASSERT(p_end != cstr);
143 if (!is_positive) { 140 if (!is_positive) {
144 double_value = -double_value; 141 double_value = -double_value;
145 } 142 }
146 Double& result = Double::Handle(Double::New(double_value)); 143 return Double::New(double_value);
147 arguments->SetReturn(result);
148 return;
149 } 144 }
150 145
151 if (IsValidLiteral(tokens, Token::kINTEGER, &is_positive, &number_string)) { 146 if (IsValidLiteral(tokens, Token::kINTEGER, &is_positive, &number_string)) {
152 Integer& res = Integer::Handle(Integer::New(*number_string)); 147 Integer& res = Integer::Handle(Integer::New(*number_string));
153 if (is_positive) { 148 if (is_positive) {
154 arguments->SetReturn(Double::Handle(Double::New(res.AsDoubleValue()))); 149 return Double::New(res.AsDoubleValue());
155 } else { 150 } else {
156 arguments->SetReturn(Double::Handle(Double::New(-res.AsDoubleValue()))); 151 return Double::New(-res.AsDoubleValue());
157 } 152 }
158 return;
159 } 153 }
160 154
161 // Infinity and nan. 155 // Infinity and nan.
162 if (IsValidLiteral(tokens, Token::kIDENT, &is_positive, &number_string)) { 156 if (IsValidLiteral(tokens, Token::kIDENT, &is_positive, &number_string)) {
163 if (number_string->Equals("NaN")) { 157 if (number_string->Equals("NaN")) {
164 arguments->SetReturn(Double::Handle(Double::New(NAN))); 158 return Double::New(NAN);
165 return;
166 } 159 }
167 if (number_string->Equals("Infinity")) { 160 if (number_string->Equals("Infinity")) {
168 if (is_positive) { 161 if (is_positive) {
169 arguments->SetReturn(Double::Handle(Double::New(INFINITY))); 162 return Double::New(INFINITY);
170 } else { 163 } else {
171 arguments->SetReturn(Double::Handle(Double::New(-INFINITY))); 164 return Double::New(-INFINITY);
172 } 165 }
173 return;
174 } 166 }
175 } 167 }
176 168
177 GrowableArray<const Object*> args; 169 GrowableArray<const Object*> args;
178 args.Add(&value); 170 args.Add(&value);
179 Exceptions::ThrowByType(Exceptions::kFormat, args); 171 Exceptions::ThrowByType(Exceptions::kFormat, args);
172 return Object::null();
180 } 173 }
181 174
182 } // namespace dart 175 } // namespace dart
OLDNEW
« no previous file with comments | « lib/isolate.cc ('k') | lib/mirrors.cc » ('j') | vm/bootstrap_natives.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698