OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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/objects.h" | 5 #include "src/objects.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <iomanip> | 8 #include <iomanip> |
9 #include <memory> | 9 #include <memory> |
10 #include <sstream> | 10 #include <sstream> |
(...skipping 11577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
11588 } | 11588 } |
11589 } | 11589 } |
11590 if (r < 0) { | 11590 if (r < 0) { |
11591 result = ComparisonResult::kLessThan; | 11591 result = ComparisonResult::kLessThan; |
11592 } else if (r > 0) { | 11592 } else if (r > 0) { |
11593 result = ComparisonResult::kGreaterThan; | 11593 result = ComparisonResult::kGreaterThan; |
11594 } | 11594 } |
11595 return result; | 11595 return result; |
11596 } | 11596 } |
11597 | 11597 |
11598 int String::IndexOf(Isolate* isolate, Handle<String> sub, Handle<String> pat, | 11598 Object* String::IndexOf(Isolate* isolate, Handle<Object> receiver, |
11599 int start_index) { | 11599 Handle<Object> pattern, Handle<Object> position) { |
11600 DCHECK(0 <= start_index); | 11600 if (receiver->IsNull(isolate) || receiver->IsUndefined(isolate)) { |
11601 DCHECK(start_index <= sub->length()); | 11601 THROW_NEW_ERROR_RETURN_FAILURE( |
11602 isolate, NewTypeError(MessageTemplate::kCalledOnNullOrUndefined, | |
11603 isolate->factory()->NewStringFromAsciiChecked( | |
11604 "String.prototype.lastIndexOf"))); | |
Franzi
2016/09/20 17:03:27
Should this be String.prototpye.indexOf?
petermarshall
2016/09/22 13:54:24
It definitely should
| |
11605 } | |
11606 Handle<String> sub; | |
11607 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, sub, | |
11608 Object::ToString(isolate, receiver)); | |
11609 Handle<String> pat; | |
11610 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, pat, | |
11611 Object::ToString(isolate, pattern)); | |
11612 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, position, | |
11613 Object::ToInteger(isolate, position)); | |
11602 | 11614 |
11603 int pattern_length = pat->length(); | 11615 double position_number = std::max(position->Number(), 0.0); |
11604 if (pattern_length == 0) return start_index; | 11616 position_number = |
11617 std::min(position_number, static_cast<double>(sub->length())); | |
11618 uint32_t start_index = static_cast<uint32_t>(position_number); | |
11605 | 11619 |
11606 int subject_length = sub->length(); | 11620 uint32_t pattern_length = pat->length(); |
11607 if (start_index + pattern_length > subject_length) return -1; | 11621 if (pattern_length == 0) return Smi::FromInt(start_index); |
11622 | |
11623 uint32_t subject_length = sub->length(); | |
11624 if (start_index + pattern_length > subject_length) return Smi::FromInt(-1); | |
11608 | 11625 |
11609 sub = String::Flatten(sub); | 11626 sub = String::Flatten(sub); |
11610 pat = String::Flatten(pat); | 11627 pat = String::Flatten(pat); |
11611 | 11628 |
11629 int first_index = -1; | |
Franzi
2016/09/20 17:03:27
first_index is overwritten in any case, right? Can
petermarshall
2016/09/22 13:54:24
Yep that's a bit nicer
| |
11612 DisallowHeapAllocation no_gc; // ensure vectors stay valid | 11630 DisallowHeapAllocation no_gc; // ensure vectors stay valid |
11613 // Extract flattened substrings of cons strings before getting encoding. | 11631 // Extract flattened substrings of cons strings before getting encoding. |
11614 String::FlatContent seq_sub = sub->GetFlatContent(); | 11632 String::FlatContent seq_sub = sub->GetFlatContent(); |
11615 String::FlatContent seq_pat = pat->GetFlatContent(); | 11633 String::FlatContent seq_pat = pat->GetFlatContent(); |
11616 | 11634 |
11617 // dispatch on type of strings | 11635 // dispatch on type of strings |
11618 if (seq_pat.IsOneByte()) { | 11636 if (seq_pat.IsOneByte()) { |
11619 Vector<const uint8_t> pat_vector = seq_pat.ToOneByteVector(); | 11637 Vector<const uint8_t> pat_vector = seq_pat.ToOneByteVector(); |
11620 if (seq_sub.IsOneByte()) { | 11638 if (seq_sub.IsOneByte()) { |
11621 return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector, | 11639 first_index = SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector, |
11622 start_index); | 11640 start_index); |
11641 } else { | |
11642 first_index = SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, | |
11643 start_index); | |
11623 } | 11644 } |
11624 return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, | 11645 } else { |
11625 start_index); | 11646 Vector<const uc16> pat_vector = seq_pat.ToUC16Vector(); |
11647 if (seq_sub.IsOneByte()) { | |
Franzi
2016/09/20 17:03:27
Would it make sense to extract this if-else statem
petermarshall
2016/09/22 13:54:24
Could be a good idea, I'm not completely sure of a
| |
11648 first_index = SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector, | |
11649 start_index); | |
11650 } else { | |
11651 first_index = SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, | |
11652 start_index); | |
11653 } | |
11626 } | 11654 } |
11627 Vector<const uc16> pat_vector = seq_pat.ToUC16Vector(); | 11655 return Smi::FromInt(first_index); |
11628 if (seq_sub.IsOneByte()) { | |
11629 return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector, | |
11630 start_index); | |
11631 } | |
11632 return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, start_index); | |
11633 } | 11656 } |
11634 | 11657 |
11635 namespace { // for String.Prototype.lastIndexOf | 11658 namespace { // for String.Prototype.lastIndexOf |
11636 | 11659 |
11637 template <typename schar, typename pchar> | 11660 template <typename schar, typename pchar> |
11638 int StringMatchBackwards(Vector<const schar> subject, | 11661 int StringMatchBackwards(Vector<const schar> subject, |
11639 Vector<const pchar> pattern, int idx) { | 11662 Vector<const pchar> pattern, int idx) { |
11640 int pattern_length = pattern.length(); | 11663 int pattern_length = pattern.length(); |
11641 DCHECK(pattern_length >= 1); | 11664 DCHECK(pattern_length >= 1); |
11642 DCHECK(idx + pattern_length <= subject.length()); | 11665 DCHECK(idx + pattern_length <= subject.length()); |
(...skipping 7956 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
19599 | 19622 |
19600 Handle<Object> Module::LoadExport(Handle<Module> module, Handle<String> name) { | 19623 Handle<Object> Module::LoadExport(Handle<Module> module, Handle<String> name) { |
19601 Isolate* isolate = module->GetIsolate(); | 19624 Isolate* isolate = module->GetIsolate(); |
19602 Handle<ObjectHashTable> exports(module->exports(), isolate); | 19625 Handle<ObjectHashTable> exports(module->exports(), isolate); |
19603 Handle<Cell> cell(Cell::cast(exports->Lookup(name))); | 19626 Handle<Cell> cell(Cell::cast(exports->Lookup(name))); |
19604 return handle(cell->value(), isolate); | 19627 return handle(cell->value(), isolate); |
19605 } | 19628 } |
19606 | 19629 |
19607 } // namespace internal | 19630 } // namespace internal |
19608 } // namespace v8 | 19631 } // namespace v8 |
OLD | NEW |