OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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/builtins.h" | 5 #include "src/builtins.h" |
6 | 6 |
7 #include "src/api-arguments.h" | 7 #include "src/api-arguments.h" |
8 #include "src/api-natives.h" | 8 #include "src/api-natives.h" |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/base/once.h" | 10 #include "src/base/once.h" |
(...skipping 4280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4291 Handle<Object> object = args.at<Object>(0); | 4291 Handle<Object> object = args.at<Object>(0); |
4292 Handle<String> result; | 4292 Handle<String> result; |
4293 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 4293 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
4294 isolate, result, Object::ObjectProtoToString(isolate, object)); | 4294 isolate, result, Object::ObjectProtoToString(isolate, object)); |
4295 return *result; | 4295 return *result; |
4296 } | 4296 } |
4297 | 4297 |
4298 // ----------------------------------------------------------------------------- | 4298 // ----------------------------------------------------------------------------- |
4299 // ES6 section 21.1 String Objects | 4299 // ES6 section 21.1 String Objects |
4300 | 4300 |
| 4301 namespace { |
| 4302 |
| 4303 bool ToUint16(Handle<Object> value, uint16_t* result) { |
| 4304 if (value->IsNumber() || Object::ToNumber(value).ToHandle(&value)) { |
| 4305 *result = DoubleToUint32(value->Number()); |
| 4306 return true; |
| 4307 } |
| 4308 return false; |
| 4309 } |
| 4310 |
| 4311 } // namespace |
| 4312 |
| 4313 // ES6 21.1.2.1 String.fromCharCode ( ...codeUnits ) |
| 4314 BUILTIN(StringFromCharCode) { |
| 4315 HandleScope scope(isolate); |
| 4316 // Check resulting string length. |
| 4317 int index = 0; |
| 4318 Handle<String> result; |
| 4319 int const length = args.length() - 1; |
| 4320 if (length == 0) return isolate->heap()->empty_string(); |
| 4321 DCHECK_LT(0, length); |
| 4322 // Load the first character code. |
| 4323 uint16_t code; |
| 4324 if (!ToUint16(args.at<Object>(1), &code)) return isolate->heap()->exception(); |
| 4325 // Assume that the resulting String contains only one byte characters. |
| 4326 if (code <= String::kMaxOneByteCharCodeU) { |
| 4327 // Check for single one-byte character fast case. |
| 4328 if (length == 1) { |
| 4329 return *isolate->factory()->LookupSingleCharacterStringFromCode(code); |
| 4330 } |
| 4331 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 4332 isolate, result, isolate->factory()->NewRawOneByteString(length)); |
| 4333 do { |
| 4334 Handle<SeqOneByteString>::cast(result)->Set(index, code); |
| 4335 if (++index == length) break; |
| 4336 if (!ToUint16(args.at<Object>(1 + index), &code)) { |
| 4337 return isolate->heap()->exception(); |
| 4338 } |
| 4339 } while (code <= String::kMaxOneByteCharCodeU); |
| 4340 } |
| 4341 // Check if all characters fit into the one byte range. |
| 4342 if (index < length) { |
| 4343 // Fallback to two byte string. |
| 4344 Handle<String> new_result; |
| 4345 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 4346 isolate, new_result, isolate->factory()->NewRawTwoByteString(length)); |
| 4347 for (int new_index = 0; new_index < index; ++new_index) { |
| 4348 uint16_t new_code = |
| 4349 Handle<SeqOneByteString>::cast(result)->Get(new_index); |
| 4350 Handle<SeqTwoByteString>::cast(new_result)->Set(new_index, new_code); |
| 4351 } |
| 4352 while (true) { |
| 4353 Handle<SeqTwoByteString>::cast(new_result)->Set(index, code); |
| 4354 if (++index == length) break; |
| 4355 if (!ToUint16(args.at<Object>(1 + index), &code)) { |
| 4356 return isolate->heap()->exception(); |
| 4357 } |
| 4358 } |
| 4359 result = new_result; |
| 4360 } |
| 4361 return *result; |
| 4362 } |
| 4363 |
4301 // ES6 section 21.1.3.1 String.prototype.charAt ( pos ) | 4364 // ES6 section 21.1.3.1 String.prototype.charAt ( pos ) |
4302 void Builtins::Generate_StringPrototypeCharAt(CodeStubAssembler* assembler) { | 4365 void Builtins::Generate_StringPrototypeCharAt(CodeStubAssembler* assembler) { |
4303 typedef CodeStubAssembler::Label Label; | 4366 typedef CodeStubAssembler::Label Label; |
4304 typedef compiler::Node Node; | 4367 typedef compiler::Node Node; |
4305 typedef CodeStubAssembler::Variable Variable; | 4368 typedef CodeStubAssembler::Variable Variable; |
4306 | 4369 |
4307 Node* receiver = assembler->Parameter(0); | 4370 Node* receiver = assembler->Parameter(0); |
4308 Node* position = assembler->Parameter(1); | 4371 Node* position = assembler->Parameter(1); |
4309 Node* context = assembler->Parameter(4); | 4372 Node* context = assembler->Parameter(4); |
4310 | 4373 |
(...skipping 1205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5516 BUILTIN_LIST_T(DEFINE_BUILTIN_ACCESSOR_T) | 5579 BUILTIN_LIST_T(DEFINE_BUILTIN_ACCESSOR_T) |
5517 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) | 5580 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) |
5518 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) | 5581 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) |
5519 #undef DEFINE_BUILTIN_ACCESSOR_C | 5582 #undef DEFINE_BUILTIN_ACCESSOR_C |
5520 #undef DEFINE_BUILTIN_ACCESSOR_A | 5583 #undef DEFINE_BUILTIN_ACCESSOR_A |
5521 #undef DEFINE_BUILTIN_ACCESSOR_T | 5584 #undef DEFINE_BUILTIN_ACCESSOR_T |
5522 #undef DEFINE_BUILTIN_ACCESSOR_H | 5585 #undef DEFINE_BUILTIN_ACCESSOR_H |
5523 | 5586 |
5524 } // namespace internal | 5587 } // namespace internal |
5525 } // namespace v8 | 5588 } // namespace v8 |
OLD | NEW |