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

Side by Side Diff: src/runtime/runtime-numbers.cc

Issue 1306303003: [es6] Implement spec compliant ToPrimitive in the runtime. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Address Michis comments. Created 5 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
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/runtime/runtime-utils.h" 5 #include "src/runtime/runtime-utils.h"
6 6
7 #include "src/arguments.h" 7 #include "src/arguments.h"
8 #include "src/base/bits.h" 8 #include "src/base/bits.h"
9 #include "src/bootstrapper.h" 9 #include "src/bootstrapper.h"
10 #include "src/codegen.h" 10 #include "src/codegen.h"
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 105
106 RUNTIME_FUNCTION(Runtime_IsValidSmi) { 106 RUNTIME_FUNCTION(Runtime_IsValidSmi) {
107 SealHandleScope shs(isolate); 107 SealHandleScope shs(isolate);
108 DCHECK(args.length() == 1); 108 DCHECK(args.length() == 1);
109 109
110 CONVERT_NUMBER_CHECKED(int32_t, number, Int32, args[0]); 110 CONVERT_NUMBER_CHECKED(int32_t, number, Int32, args[0]);
111 return isolate->heap()->ToBoolean(Smi::IsValid(number)); 111 return isolate->heap()->ToBoolean(Smi::IsValid(number));
112 } 112 }
113 113
114 114
115 static bool AreDigits(const uint8_t* s, int from, int to) {
116 for (int i = from; i < to; i++) {
117 if (s[i] < '0' || s[i] > '9') return false;
118 }
119
120 return true;
121 }
122
123
124 static int ParseDecimalInteger(const uint8_t* s, int from, int to) {
125 DCHECK(to - from < 10); // Overflow is not possible.
126 DCHECK(from < to);
127 int d = s[from] - '0';
128
129 for (int i = from + 1; i < to; i++) {
130 d = 10 * d + (s[i] - '0');
131 }
132
133 return d;
134 }
135
136
137 RUNTIME_FUNCTION(Runtime_StringToNumber) { 115 RUNTIME_FUNCTION(Runtime_StringToNumber) {
138 HandleScope handle_scope(isolate); 116 HandleScope handle_scope(isolate);
139 DCHECK(args.length() == 1); 117 DCHECK_EQ(1, args.length());
140 CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); 118 CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
141 subject = String::Flatten(subject); 119 return *String::ToNumber(subject);
142
143 // Fast case: short integer or some sorts of junk values.
144 if (subject->IsSeqOneByteString()) {
145 int len = subject->length();
146 if (len == 0) return Smi::FromInt(0);
147
148 DisallowHeapAllocation no_gc;
149 uint8_t const* data = Handle<SeqOneByteString>::cast(subject)->GetChars();
150 bool minus = (data[0] == '-');
151 int start_pos = (minus ? 1 : 0);
152
153 if (start_pos == len) {
154 return isolate->heap()->nan_value();
155 } else if (data[start_pos] > '9') {
156 // Fast check for a junk value. A valid string may start from a
157 // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit
158 // or the 'I' character ('Infinity'). All of that have codes not greater
159 // than '9' except 'I' and &nbsp;.
160 if (data[start_pos] != 'I' && data[start_pos] != 0xa0) {
161 return isolate->heap()->nan_value();
162 }
163 } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) {
164 // The maximal/minimal smi has 10 digits. If the string has less digits
165 // we know it will fit into the smi-data type.
166 int d = ParseDecimalInteger(data, start_pos, len);
167 if (minus) {
168 if (d == 0) return isolate->heap()->minus_zero_value();
169 d = -d;
170 } else if (!subject->HasHashCode() && len <= String::kMaxArrayIndexSize &&
171 (len == 1 || data[0] != '0')) {
172 // String hash is not calculated yet but all the data are present.
173 // Update the hash field to speed up sequential convertions.
174 uint32_t hash = StringHasher::MakeArrayIndexHash(d, len);
175 #ifdef DEBUG
176 subject->Hash(); // Force hash calculation.
177 DCHECK_EQ(static_cast<int>(subject->hash_field()),
178 static_cast<int>(hash));
179 #endif
180 subject->set_hash_field(hash);
181 }
182 return Smi::FromInt(d);
183 }
184 }
185
186 // Slower case.
187 int flags = ALLOW_HEX | ALLOW_OCTAL | ALLOW_BINARY;
188 return *isolate->factory()->NewNumber(
189 StringToDouble(isolate->unicode_cache(), subject, flags));
190 } 120 }
191 121
192 122
193 RUNTIME_FUNCTION(Runtime_StringParseInt) { 123 RUNTIME_FUNCTION(Runtime_StringParseInt) {
194 HandleScope handle_scope(isolate); 124 HandleScope handle_scope(isolate);
195 DCHECK(args.length() == 2); 125 DCHECK(args.length() == 2);
196 CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); 126 CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
197 CONVERT_NUMBER_CHECKED(int, radix, Int32, args[1]); 127 CONVERT_NUMBER_CHECKED(int, radix, Int32, args[1]);
198 RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36)); 128 RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36));
199 129
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 } 486 }
557 487
558 488
559 RUNTIME_FUNCTION(Runtime_GetRootNaN) { 489 RUNTIME_FUNCTION(Runtime_GetRootNaN) {
560 SealHandleScope shs(isolate); 490 SealHandleScope shs(isolate);
561 DCHECK(args.length() == 0); 491 DCHECK(args.length() == 0);
562 return isolate->heap()->nan_value(); 492 return isolate->heap()->nan_value();
563 } 493 }
564 } // namespace internal 494 } // namespace internal
565 } // namespace v8 495 } // namespace v8
OLDNEW
« src/objects.cc ('K') | « src/runtime/runtime-i18n.cc ('k') | src/runtime/runtime-object.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698