OLD | NEW |
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 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 Handle<String> valid_forms = | 464 Handle<String> valid_forms = |
465 isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD"); | 465 isolate->factory()->NewStringFromStaticChars("NFC, NFD, NFKC, NFKD"); |
466 THROW_NEW_ERROR_RETURN_FAILURE( | 466 THROW_NEW_ERROR_RETURN_FAILURE( |
467 isolate, | 467 isolate, |
468 NewRangeError(MessageTemplate::kNormalizationForm, valid_forms)); | 468 NewRangeError(MessageTemplate::kNormalizationForm, valid_forms)); |
469 } | 469 } |
470 | 470 |
471 return *string; | 471 return *string; |
472 } | 472 } |
473 | 473 |
474 namespace { | |
475 | |
476 compiler::Node* ToSmiBetweenZeroAnd(CodeStubAssembler* a, | |
477 compiler::Node* context, | |
478 compiler::Node* value, | |
479 compiler::Node* limit) { | |
480 typedef CodeStubAssembler::Label Label; | |
481 typedef compiler::Node Node; | |
482 typedef CodeStubAssembler::Variable Variable; | |
483 | |
484 Label out(a); | |
485 Variable var_result(a, MachineRepresentation::kTagged); | |
486 | |
487 Node* const value_int = | |
488 a->ToInteger(context, value, CodeStubAssembler::kTruncateMinusZero); | |
489 | |
490 Label if_issmi(a), if_isnotsmi(a, Label::kDeferred); | |
491 a->Branch(a->WordIsSmi(value_int), &if_issmi, &if_isnotsmi); | |
492 | |
493 a->Bind(&if_issmi); | |
494 { | |
495 Label if_isinbounds(a), if_isoutofbounds(a, Label::kDeferred); | |
496 a->Branch(a->SmiAbove(value_int, limit), &if_isoutofbounds, &if_isinbounds); | |
497 | |
498 a->Bind(&if_isinbounds); | |
499 { | |
500 var_result.Bind(value_int); | |
501 a->Goto(&out); | |
502 } | |
503 | |
504 a->Bind(&if_isoutofbounds); | |
505 { | |
506 Node* const zero = a->SmiConstant(Smi::FromInt(0)); | |
507 var_result.Bind(a->Select(a->SmiLessThan(value_int, zero), zero, limit)); | |
508 a->Goto(&out); | |
509 } | |
510 } | |
511 | |
512 a->Bind(&if_isnotsmi); | |
513 { | |
514 // {value} is a heap number - in this case, it is definitely out of bounds. | |
515 a->Assert(a->WordEqual(a->LoadMap(value_int), a->HeapNumberMapConstant())); | |
516 | |
517 Node* const float_zero = a->Float64Constant(0.); | |
518 Node* const smi_zero = a->SmiConstant(Smi::FromInt(0)); | |
519 Node* const value_float = a->LoadHeapNumberValue(value_int); | |
520 var_result.Bind(a->Select(a->Float64LessThan(value_float, float_zero), | |
521 smi_zero, limit)); | |
522 a->Goto(&out); | |
523 } | |
524 | |
525 a->Bind(&out); | |
526 return var_result.value(); | |
527 } | |
528 | |
529 } // namespace | |
530 | |
531 // ES6 section 21.1.3.19 String.prototype.substring ( start, end ) | |
532 void Builtins::Generate_StringPrototypeSubstring(CodeStubAssembler* a) { | |
533 typedef CodeStubAssembler::Label Label; | |
534 typedef compiler::Node Node; | |
535 typedef CodeStubAssembler::Variable Variable; | |
536 | |
537 Label out(a); | |
538 | |
539 Variable var_start(a, MachineRepresentation::kTagged); | |
540 Variable var_end(a, MachineRepresentation::kTagged); | |
541 | |
542 Node* const receiver = a->Parameter(0); | |
543 Node* const start = a->Parameter(1); | |
544 Node* const end = a->Parameter(2); | |
545 Node* const context = a->Parameter(5); | |
546 | |
547 // Check that {receiver} is coercible to Object and convert it to a String. | |
548 Node* const string = | |
549 a->ToThisString(context, receiver, "String.prototype.substring"); | |
550 | |
551 Node* const length = a->LoadStringLength(string); | |
552 | |
553 // Conversion and bounds-checks for {start}. | |
554 var_start.Bind(ToSmiBetweenZeroAnd(a, context, start, length)); | |
555 | |
556 // Conversion and bounds-checks for {end}. | |
557 { | |
558 var_end.Bind(length); | |
559 a->GotoIf(a->WordEqual(end, a->UndefinedConstant()), &out); | |
560 | |
561 var_end.Bind(ToSmiBetweenZeroAnd(a, context, end, length)); | |
562 | |
563 Label if_endislessthanstart(a); | |
564 a->Branch(a->SmiLessThan(var_end.value(), var_start.value()), | |
565 &if_endislessthanstart, &out); | |
566 | |
567 a->Bind(&if_endislessthanstart); | |
568 { | |
569 Node* const tmp = var_end.value(); | |
570 var_end.Bind(var_start.value()); | |
571 var_start.Bind(tmp); | |
572 a->Goto(&out); | |
573 } | |
574 } | |
575 | |
576 a->Bind(&out); | |
577 { | |
578 Node* result = | |
579 a->SubString(context, string, var_start.value(), var_end.value()); | |
580 a->Return(result); | |
581 } | |
582 } | |
583 | |
584 // ES6 section 21.1.3.25 String.prototype.toString () | 474 // ES6 section 21.1.3.25 String.prototype.toString () |
585 void Builtins::Generate_StringPrototypeToString(CodeStubAssembler* assembler) { | 475 void Builtins::Generate_StringPrototypeToString(CodeStubAssembler* assembler) { |
586 typedef compiler::Node Node; | 476 typedef compiler::Node Node; |
587 | 477 |
588 Node* receiver = assembler->Parameter(0); | 478 Node* receiver = assembler->Parameter(0); |
589 Node* context = assembler->Parameter(3); | 479 Node* context = assembler->Parameter(3); |
590 | 480 |
591 Node* result = assembler->ToThisValue( | 481 Node* result = assembler->ToThisValue( |
592 context, receiver, PrimitiveType::kString, "String.prototype.toString"); | 482 context, receiver, PrimitiveType::kString, "String.prototype.toString"); |
593 assembler->Return(result); | 483 assembler->Return(result); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
675 } | 565 } |
676 | 566 |
677 iterator->set_string(isolate->heap()->empty_string()); | 567 iterator->set_string(isolate->heap()->empty_string()); |
678 | 568 |
679 return *isolate->factory()->NewJSIteratorResult( | 569 return *isolate->factory()->NewJSIteratorResult( |
680 isolate->factory()->undefined_value(), true); | 570 isolate->factory()->undefined_value(), true); |
681 } | 571 } |
682 | 572 |
683 } // namespace internal | 573 } // namespace internal |
684 } // namespace v8 | 574 } // namespace v8 |
OLD | NEW |