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

Side by Side Diff: src/builtins/builtins-string.cc

Issue 2313393005: [builtins] Move StringLocaleCompare to a builtin. (Closed)
Patch Set: Change dcheck to dcheck_eq Created 4 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
« no previous file with comments | « src/builtins/builtins.h ('k') | src/js/string.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 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/builtins.h" 5 #include "src/builtins/builtins.h"
6 #include "src/builtins/builtins-utils.h" 6 #include "src/builtins/builtins-utils.h"
7 7
8 #include "src/code-factory.h" 8 #include "src/code-factory.h"
9 9
10 namespace v8 { 10 namespace v8 {
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 assembler->Return(assembler->NaNConstant()); 470 assembler->Return(assembler->NaNConstant());
471 assembler->Bind(&if_positioninbounds); 471 assembler->Bind(&if_positioninbounds);
472 } 472 }
473 473
474 // Load the character at the {position} from the {receiver}. 474 // Load the character at the {position} from the {receiver}.
475 Node* value = assembler->StringCharCodeAt(receiver, position); 475 Node* value = assembler->StringCharCodeAt(receiver, position);
476 Node* result = assembler->SmiFromWord32(value); 476 Node* result = assembler->SmiFromWord32(value);
477 assembler->Return(result); 477 assembler->Return(result);
478 } 478 }
479 479
480 // ES6 section 21.1.3.10 String.prototype.localeCompare ( that )
481 //
482 // This function is implementation specific. For now, we do not
483 // do anything locale specific.
484 // If internationalization is enabled, then i18n.js will override this function
485 // and provide the proper functionality, so this is just a fallback.
486 BUILTIN(StringPrototypeLocaleCompare) {
487 HandleScope handle_scope(isolate);
488 DCHECK_EQ(2, args.length());
489
490 TO_THIS_STRING(str1, "String.prototype.localeCompare");
491 Handle<String> str2;
492 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
493 isolate, str2, Object::ToString(isolate, args.at<Object>(1)));
494
495 if (str1.is_identical_to(str2)) return Smi::FromInt(0); // Equal.
496 int str1_length = str1->length();
497 int str2_length = str2->length();
498
499 // Decide trivial cases without flattening.
500 if (str1_length == 0) {
501 if (str2_length == 0) return Smi::FromInt(0); // Equal.
502 return Smi::FromInt(-str2_length);
503 } else {
504 if (str2_length == 0) return Smi::FromInt(str1_length);
505 }
506
507 int end = str1_length < str2_length ? str1_length : str2_length;
508
509 // No need to flatten if we are going to find the answer on the first
510 // character. At this point we know there is at least one character
511 // in each string, due to the trivial case handling above.
512 int d = str1->Get(0) - str2->Get(0);
513 if (d != 0) return Smi::FromInt(d);
514
515 str1 = String::Flatten(str1);
516 str2 = String::Flatten(str2);
517
518 DisallowHeapAllocation no_gc;
519 String::FlatContent flat1 = str1->GetFlatContent();
520 String::FlatContent flat2 = str2->GetFlatContent();
521
522 for (int i = 0; i < end; i++) {
523 if (flat1.Get(i) != flat2.Get(i)) {
524 return Smi::FromInt(flat1.Get(i) - flat2.Get(i));
525 }
526 }
527
528 return Smi::FromInt(str1_length - str2_length);
529 }
530
480 // ES6 section 21.1.3.12 String.prototype.normalize ( [form] ) 531 // ES6 section 21.1.3.12 String.prototype.normalize ( [form] )
481 // 532 //
482 // Simply checks the argument is valid and returns the string itself. 533 // Simply checks the argument is valid and returns the string itself.
483 // If internationalization is enabled, then i18n.js will override this function 534 // If internationalization is enabled, then i18n.js will override this function
484 // and provide the proper functionality, so this is just a fallback. 535 // and provide the proper functionality, so this is just a fallback.
485 BUILTIN(StringPrototypeNormalize) { 536 BUILTIN(StringPrototypeNormalize) {
486 HandleScope handle_scope(isolate); 537 HandleScope handle_scope(isolate);
487 TO_THIS_STRING(string, "String.prototype.normalize"); 538 TO_THIS_STRING(string, "String.prototype.normalize");
488 539
489 Handle<Object> form_input = args.atOrUndefined(isolate, 1); 540 Handle<Object> form_input = args.atOrUndefined(isolate, 1);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 Node* receiver = assembler->Parameter(0); 602 Node* receiver = assembler->Parameter(0);
552 Node* context = assembler->Parameter(3); 603 Node* context = assembler->Parameter(3);
553 604
554 Node* result = assembler->ToThisValue( 605 Node* result = assembler->ToThisValue(
555 context, receiver, PrimitiveType::kString, "String.prototype.valueOf"); 606 context, receiver, PrimitiveType::kString, "String.prototype.valueOf");
556 assembler->Return(result); 607 assembler->Return(result);
557 } 608 }
558 609
559 } // namespace internal 610 } // namespace internal
560 } // namespace v8 611 } // namespace v8
OLDNEW
« no previous file with comments | « src/builtins/builtins.h ('k') | src/js/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698