"src/runtime-profiler.cc",
"src/runtime-profiler.h",
"src/runtime/runtime-i18n.cc",
+ "src/runtime/runtime-json.cc",
+ "src/runtime/runtime-regexp.cc",
+ "src/runtime/runtime-strings.cc",
+ "src/runtime/runtime-uri.cc",
"src/runtime/runtime-utils.h",
"src/runtime/runtime.cc",
"src/runtime/runtime.h",
+ "src/runtime/string-builder.h",
"src/safepoint-table.cc",
"src/safepoint-table.h",
"src/sampler.cc",
--- /dev/null
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/v8.h"
+
+#include "src/arguments.h"
+#include "src/json-parser.h"
+#include "src/json-stringifier.h"
+#include "src/runtime/runtime.h"
+#include "src/runtime/runtime-utils.h"
+
+namespace v8 {
+namespace internal {
+
+RUNTIME_FUNCTION(Runtime_QuoteJSONString) {
+ HandleScope scope(isolate);
+ CONVERT_ARG_HANDLE_CHECKED(String, string, 0);
+ DCHECK(args.length() == 1);
+ Handle<Object> result;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, result, BasicJsonStringifier::StringifyString(isolate, string));
+ return *result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_BasicJSONStringify) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 1);
+ CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
+ BasicJsonStringifier stringifier(isolate);
+ Handle<Object> result;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
+ stringifier.Stringify(object));
+ return *result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_ParseJson) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 1);
+ CONVERT_ARG_HANDLE_CHECKED(String, source, 0);
+
+ source = String::Flatten(source);
+ // Optimized fast case where we only have Latin1 characters.
+ Handle<Object> result;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
+ source->IsSeqOneByteString()
+ ? JsonParser<true>::Parse(source)
+ : JsonParser<false>::Parse(source));
+ return *result;
+}
+}
+} // namespace v8::internal
--- /dev/null
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/v8.h"
+
+#include "src/arguments.h"
+#include "src/jsregexp-inl.h"
+#include "src/jsregexp.h"
+#include "src/runtime/runtime.h"
+#include "src/runtime/runtime-utils.h"
+#include "src/runtime/string-builder.h"
+#include "src/string-search.h"
+
+namespace v8 {
+namespace internal {
+
+class CompiledReplacement {
+ public:
+ explicit CompiledReplacement(Zone* zone)
+ : parts_(1, zone), replacement_substrings_(0, zone), zone_(zone) {}
+
+ // Return whether the replacement is simple.
+ bool Compile(Handle<String> replacement, int capture_count,
+ int subject_length);
+
+ // Use Apply only if Compile returned false.
+ void Apply(ReplacementStringBuilder* builder, int match_from, int match_to,
+ int32_t* match);
+
+ // Number of distinct parts of the replacement pattern.
+ int parts() { return parts_.length(); }
+
+ Zone* zone() const { return zone_; }
+
+ private:
+ enum PartType {
+ SUBJECT_PREFIX = 1,
+ SUBJECT_SUFFIX,
+ SUBJECT_CAPTURE,
+ REPLACEMENT_SUBSTRING,
+ REPLACEMENT_STRING,
+ NUMBER_OF_PART_TYPES
+ };
+
+ struct ReplacementPart {
+ static inline ReplacementPart SubjectMatch() {
+ return ReplacementPart(SUBJECT_CAPTURE, 0);
+ }
+ static inline ReplacementPart SubjectCapture(int capture_index) {
+ return ReplacementPart(SUBJECT_CAPTURE, capture_index);
+ }
+ static inline ReplacementPart SubjectPrefix() {
+ return ReplacementPart(SUBJECT_PREFIX, 0);
+ }
+ static inline ReplacementPart SubjectSuffix(int subject_length) {
+ return ReplacementPart(SUBJECT_SUFFIX, subject_length);
+ }
+ static inline ReplacementPart ReplacementString() {
+ return ReplacementPart(REPLACEMENT_STRING, 0);
+ }
+ static inline ReplacementPart ReplacementSubString(int from, int to) {
+ DCHECK(from >= 0);
+ DCHECK(to > from);
+ return ReplacementPart(-from, to);
+ }
+
+ // If tag <= 0 then it is the negation of a start index of a substring of
+ // the replacement pattern, otherwise it's a value from PartType.
+ ReplacementPart(int tag, int data) : tag(tag), data(data) {
+ // Must be non-positive or a PartType value.
+ DCHECK(tag < NUMBER_OF_PART_TYPES);
+ }
+ // Either a value of PartType or a non-positive number that is
+ // the negation of an index into the replacement string.
+ int tag;
+ // The data value's interpretation depends on the value of tag:
+ // tag == SUBJECT_PREFIX ||
+ // tag == SUBJECT_SUFFIX: data is unused.
+ // tag == SUBJECT_CAPTURE: data is the number of the capture.
+ // tag == REPLACEMENT_SUBSTRING ||
+ // tag == REPLACEMENT_STRING: data is index into array of substrings
+ // of the replacement string.
+ // tag <= 0: Temporary representation of the substring of the replacement
+ // string ranging over -tag .. data.
+ // Is replaced by REPLACEMENT_{SUB,}STRING when we create the
+ // substring objects.
+ int data;
+ };
+
+ template <typename Char>
+ bool ParseReplacementPattern(ZoneList<ReplacementPart>* parts,
+ Vector<Char> characters, int capture_count,
+ int subject_length, Zone* zone) {
+ int length = characters.length();
+ int last = 0;
+ for (int i = 0; i < length; i++) {
+ Char c = characters[i];
+ if (c == '$') {
+ int next_index = i + 1;
+ if (next_index == length) { // No next character!
+ break;
+ }
+ Char c2 = characters[next_index];
+ switch (c2) {
+ case '$':
+ if (i > last) {
+ // There is a substring before. Include the first "$".
+ parts->Add(
+ ReplacementPart::ReplacementSubString(last, next_index),
+ zone);
+ last = next_index + 1; // Continue after the second "$".
+ } else {
+ // Let the next substring start with the second "$".
+ last = next_index;
+ }
+ i = next_index;
+ break;
+ case '`':
+ if (i > last) {
+ parts->Add(ReplacementPart::ReplacementSubString(last, i), zone);
+ }
+ parts->Add(ReplacementPart::SubjectPrefix(), zone);
+ i = next_index;
+ last = i + 1;
+ break;
+ case '\'':
+ if (i > last) {
+ parts->Add(ReplacementPart::ReplacementSubString(last, i), zone);
+ }
+ parts->Add(ReplacementPart::SubjectSuffix(subject_length), zone);
+ i = next_index;
+ last = i + 1;
+ break;
+ case '&':
+ if (i > last) {
+ parts->Add(ReplacementPart::ReplacementSubString(last, i), zone);
+ }
+ parts->Add(ReplacementPart::SubjectMatch(), zone);
+ i = next_index;
+ last = i + 1;
+ break;
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9': {
+ int capture_ref = c2 - '0';
+ if (capture_ref > capture_count) {
+ i = next_index;
+ continue;
+ }
+ int second_digit_index = next_index + 1;
+ if (second_digit_index < length) {
+ // Peek ahead to see if we have two digits.
+ Char c3 = characters[second_digit_index];
+ if ('0' <= c3 && c3 <= '9') { // Double digits.
+ int double_digit_ref = capture_ref * 10 + c3 - '0';
+ if (double_digit_ref <= capture_count) {
+ next_index = second_digit_index;
+ capture_ref = double_digit_ref;
+ }
+ }
+ }
+ if (capture_ref > 0) {
+ if (i > last) {
+ parts->Add(ReplacementPart::ReplacementSubString(last, i),
+ zone);
+ }
+ DCHECK(capture_ref <= capture_count);
+ parts->Add(ReplacementPart::SubjectCapture(capture_ref), zone);
+ last = next_index + 1;
+ }
+ i = next_index;
+ break;
+ }
+ default:
+ i = next_index;
+ break;
+ }
+ }
+ }
+ if (length > last) {
+ if (last == 0) {
+ // Replacement is simple. Do not use Apply to do the replacement.
+ return true;
+ } else {
+ parts->Add(ReplacementPart::ReplacementSubString(last, length), zone);
+ }
+ }
+ return false;
+ }
+
+ ZoneList<ReplacementPart> parts_;
+ ZoneList<Handle<String> > replacement_substrings_;
+ Zone* zone_;
+};
+
+
+bool CompiledReplacement::Compile(Handle<String> replacement, int capture_count,
+ int subject_length) {
+ {
+ DisallowHeapAllocation no_gc;
+ String::FlatContent content = replacement->GetFlatContent();
+ DCHECK(content.IsFlat());
+ bool simple = false;
+ if (content.IsOneByte()) {
+ simple = ParseReplacementPattern(&parts_, content.ToOneByteVector(),
+ capture_count, subject_length, zone());
+ } else {
+ DCHECK(content.IsTwoByte());
+ simple = ParseReplacementPattern(&parts_, content.ToUC16Vector(),
+ capture_count, subject_length, zone());
+ }
+ if (simple) return true;
+ }
+
+ Isolate* isolate = replacement->GetIsolate();
+ // Find substrings of replacement string and create them as String objects.
+ int substring_index = 0;
+ for (int i = 0, n = parts_.length(); i < n; i++) {
+ int tag = parts_[i].tag;
+ if (tag <= 0) { // A replacement string slice.
+ int from = -tag;
+ int to = parts_[i].data;
+ replacement_substrings_.Add(
+ isolate->factory()->NewSubString(replacement, from, to), zone());
+ parts_[i].tag = REPLACEMENT_SUBSTRING;
+ parts_[i].data = substring_index;
+ substring_index++;
+ } else if (tag == REPLACEMENT_STRING) {
+ replacement_substrings_.Add(replacement, zone());
+ parts_[i].data = substring_index;
+ substring_index++;
+ }
+ }
+ return false;
+}
+
+
+void CompiledReplacement::Apply(ReplacementStringBuilder* builder,
+ int match_from, int match_to, int32_t* match) {
+ DCHECK_LT(0, parts_.length());
+ for (int i = 0, n = parts_.length(); i < n; i++) {
+ ReplacementPart part = parts_[i];
+ switch (part.tag) {
+ case SUBJECT_PREFIX:
+ if (match_from > 0) builder->AddSubjectSlice(0, match_from);
+ break;
+ case SUBJECT_SUFFIX: {
+ int subject_length = part.data;
+ if (match_to < subject_length) {
+ builder->AddSubjectSlice(match_to, subject_length);
+ }
+ break;
+ }
+ case SUBJECT_CAPTURE: {
+ int capture = part.data;
+ int from = match[capture * 2];
+ int to = match[capture * 2 + 1];
+ if (from >= 0 && to > from) {
+ builder->AddSubjectSlice(from, to);
+ }
+ break;
+ }
+ case REPLACEMENT_SUBSTRING:
+ case REPLACEMENT_STRING:
+ builder->AddString(replacement_substrings_[part.data]);
+ break;
+ default:
+ UNREACHABLE();
+ }
+ }
+}
+
+
+void FindOneByteStringIndices(Vector<const uint8_t> subject, char pattern,
+ ZoneList<int>* indices, unsigned int limit,
+ Zone* zone) {
+ DCHECK(limit > 0);
+ // Collect indices of pattern in subject using memchr.
+ // Stop after finding at most limit values.
+ const uint8_t* subject_start = subject.start();
+ const uint8_t* subject_end = subject_start + subject.length();
+ const uint8_t* pos = subject_start;
+ while (limit > 0) {
+ pos = reinterpret_cast<const uint8_t*>(
+ memchr(pos, pattern, subject_end - pos));
+ if (pos == NULL) return;
+ indices->Add(static_cast<int>(pos - subject_start), zone);
+ pos++;
+ limit--;
+ }
+}
+
+
+void FindTwoByteStringIndices(const Vector<const uc16> subject, uc16 pattern,
+ ZoneList<int>* indices, unsigned int limit,
+ Zone* zone) {
+ DCHECK(limit > 0);
+ const uc16* subject_start = subject.start();
+ const uc16* subject_end = subject_start + subject.length();
+ for (const uc16* pos = subject_start; pos < subject_end && limit > 0; pos++) {
+ if (*pos == pattern) {
+ indices->Add(static_cast<int>(pos - subject_start), zone);
+ limit--;
+ }
+ }
+}
+
+
+template <typename SubjectChar, typename PatternChar>
+void FindStringIndices(Isolate* isolate, Vector<const SubjectChar> subject,
+ Vector<const PatternChar> pattern,
+ ZoneList<int>* indices, unsigned int limit, Zone* zone) {
+ DCHECK(limit > 0);
+ // Collect indices of pattern in subject.
+ // Stop after finding at most limit values.
+ int pattern_length = pattern.length();
+ int index = 0;
+ StringSearch<PatternChar, SubjectChar> search(isolate, pattern);
+ while (limit > 0) {
+ index = search.Search(subject, index);
+ if (index < 0) return;
+ indices->Add(index, zone);
+ index += pattern_length;
+ limit--;
+ }
+}
+
+
+void FindStringIndicesDispatch(Isolate* isolate, String* subject,
+ String* pattern, ZoneList<int>* indices,
+ unsigned int limit, Zone* zone) {
+ {
+ DisallowHeapAllocation no_gc;
+ String::FlatContent subject_content = subject->GetFlatContent();
+ String::FlatContent pattern_content = pattern->GetFlatContent();
+ DCHECK(subject_content.IsFlat());
+ DCHECK(pattern_content.IsFlat());
+ if (subject_content.IsOneByte()) {
+ Vector<const uint8_t> subject_vector = subject_content.ToOneByteVector();
+ if (pattern_content.IsOneByte()) {
+ Vector<const uint8_t> pattern_vector =
+ pattern_content.ToOneByteVector();
+ if (pattern_vector.length() == 1) {
+ FindOneByteStringIndices(subject_vector, pattern_vector[0], indices,
+ limit, zone);
+ } else {
+ FindStringIndices(isolate, subject_vector, pattern_vector, indices,
+ limit, zone);
+ }
+ } else {
+ FindStringIndices(isolate, subject_vector,
+ pattern_content.ToUC16Vector(), indices, limit, zone);
+ }
+ } else {
+ Vector<const uc16> subject_vector = subject_content.ToUC16Vector();
+ if (pattern_content.IsOneByte()) {
+ Vector<const uint8_t> pattern_vector =
+ pattern_content.ToOneByteVector();
+ if (pattern_vector.length() == 1) {
+ FindTwoByteStringIndices(subject_vector, pattern_vector[0], indices,
+ limit, zone);
+ } else {
+ FindStringIndices(isolate, subject_vector, pattern_vector, indices,
+ limit, zone);
+ }
+ } else {
+ Vector<const uc16> pattern_vector = pattern_content.ToUC16Vector();
+ if (pattern_vector.length() == 1) {
+ FindTwoByteStringIndices(subject_vector, pattern_vector[0], indices,
+ limit, zone);
+ } else {
+ FindStringIndices(isolate, subject_vector, pattern_vector, indices,
+ limit, zone);
+ }
+ }
+ }
+ }
+}
+
+
+template <typename ResultSeqString>
+MUST_USE_RESULT static Object* StringReplaceGlobalAtomRegExpWithString(
+ Isolate* isolate, Handle<String> subject, Handle<JSRegExp> pattern_regexp,
+ Handle<String> replacement, Handle<JSArray> last_match_info) {
+ DCHECK(subject->IsFlat());
+ DCHECK(replacement->IsFlat());
+
+ ZoneScope zone_scope(isolate->runtime_zone());
+ ZoneList<int> indices(8, zone_scope.zone());
+ DCHECK_EQ(JSRegExp::ATOM, pattern_regexp->TypeTag());
+ String* pattern =
+ String::cast(pattern_regexp->DataAt(JSRegExp::kAtomPatternIndex));
+ int subject_len = subject->length();
+ int pattern_len = pattern->length();
+ int replacement_len = replacement->length();
+
+ FindStringIndicesDispatch(isolate, *subject, pattern, &indices, 0xffffffff,
+ zone_scope.zone());
+
+ int matches = indices.length();
+ if (matches == 0) return *subject;
+
+ // Detect integer overflow.
+ int64_t result_len_64 = (static_cast<int64_t>(replacement_len) -
+ static_cast<int64_t>(pattern_len)) *
+ static_cast<int64_t>(matches) +
+ static_cast<int64_t>(subject_len);
+ int result_len;
+ if (result_len_64 > static_cast<int64_t>(String::kMaxLength)) {
+ STATIC_ASSERT(String::kMaxLength < kMaxInt);
+ result_len = kMaxInt; // Provoke exception.
+ } else {
+ result_len = static_cast<int>(result_len_64);
+ }
+
+ int subject_pos = 0;
+ int result_pos = 0;
+
+ MaybeHandle<SeqString> maybe_res;
+ if (ResultSeqString::kHasOneByteEncoding) {
+ maybe_res = isolate->factory()->NewRawOneByteString(result_len);
+ } else {
+ maybe_res = isolate->factory()->NewRawTwoByteString(result_len);
+ }
+ Handle<SeqString> untyped_res;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, untyped_res, maybe_res);
+ Handle<ResultSeqString> result = Handle<ResultSeqString>::cast(untyped_res);
+
+ for (int i = 0; i < matches; i++) {
+ // Copy non-matched subject content.
+ if (subject_pos < indices.at(i)) {
+ String::WriteToFlat(*subject, result->GetChars() + result_pos,
+ subject_pos, indices.at(i));
+ result_pos += indices.at(i) - subject_pos;
+ }
+
+ // Replace match.
+ if (replacement_len > 0) {
+ String::WriteToFlat(*replacement, result->GetChars() + result_pos, 0,
+ replacement_len);
+ result_pos += replacement_len;
+ }
+
+ subject_pos = indices.at(i) + pattern_len;
+ }
+ // Add remaining subject content at the end.
+ if (subject_pos < subject_len) {
+ String::WriteToFlat(*subject, result->GetChars() + result_pos, subject_pos,
+ subject_len);
+ }
+
+ int32_t match_indices[] = {indices.at(matches - 1),
+ indices.at(matches - 1) + pattern_len};
+ RegExpImpl::SetLastMatchInfo(last_match_info, subject, 0, match_indices);
+
+ return *result;
+}
+
+
+MUST_USE_RESULT static Object* StringReplaceGlobalRegExpWithString(
+ Isolate* isolate, Handle<String> subject, Handle<JSRegExp> regexp,
+ Handle<String> replacement, Handle<JSArray> last_match_info) {
+ DCHECK(subject->IsFlat());
+ DCHECK(replacement->IsFlat());
+
+ int capture_count = regexp->CaptureCount();
+ int subject_length = subject->length();
+
+ // CompiledReplacement uses zone allocation.
+ ZoneScope zone_scope(isolate->runtime_zone());
+ CompiledReplacement compiled_replacement(zone_scope.zone());
+ bool simple_replace =
+ compiled_replacement.Compile(replacement, capture_count, subject_length);
+
+ // Shortcut for simple non-regexp global replacements
+ if (regexp->TypeTag() == JSRegExp::ATOM && simple_replace) {
+ if (subject->HasOnlyOneByteChars() && replacement->HasOnlyOneByteChars()) {
+ return StringReplaceGlobalAtomRegExpWithString<SeqOneByteString>(
+ isolate, subject, regexp, replacement, last_match_info);
+ } else {
+ return StringReplaceGlobalAtomRegExpWithString<SeqTwoByteString>(
+ isolate, subject, regexp, replacement, last_match_info);
+ }
+ }
+
+ RegExpImpl::GlobalCache global_cache(regexp, subject, true, isolate);
+ if (global_cache.HasException()) return isolate->heap()->exception();
+
+ int32_t* current_match = global_cache.FetchNext();
+ if (current_match == NULL) {
+ if (global_cache.HasException()) return isolate->heap()->exception();
+ return *subject;
+ }
+
+ // Guessing the number of parts that the final result string is built
+ // from. Global regexps can match any number of times, so we guess
+ // conservatively.
+ int expected_parts = (compiled_replacement.parts() + 1) * 4 + 1;
+ ReplacementStringBuilder builder(isolate->heap(), subject, expected_parts);
+
+ // Number of parts added by compiled replacement plus preceeding
+ // string and possibly suffix after last match. It is possible for
+ // all components to use two elements when encoded as two smis.
+ const int parts_added_per_loop = 2 * (compiled_replacement.parts() + 2);
+
+ int prev = 0;
+
+ do {
+ builder.EnsureCapacity(parts_added_per_loop);
+
+ int start = current_match[0];
+ int end = current_match[1];
+
+ if (prev < start) {
+ builder.AddSubjectSlice(prev, start);
+ }
+
+ if (simple_replace) {
+ builder.AddString(replacement);
+ } else {
+ compiled_replacement.Apply(&builder, start, end, current_match);
+ }
+ prev = end;
+
+ current_match = global_cache.FetchNext();
+ } while (current_match != NULL);
+
+ if (global_cache.HasException()) return isolate->heap()->exception();
+
+ if (prev < subject_length) {
+ builder.EnsureCapacity(2);
+ builder.AddSubjectSlice(prev, subject_length);
+ }
+
+ RegExpImpl::SetLastMatchInfo(last_match_info, subject, capture_count,
+ global_cache.LastSuccessfulMatch());
+
+ Handle<String> result;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, builder.ToString());
+ return *result;
+}
+
+
+template <typename ResultSeqString>
+MUST_USE_RESULT static Object* StringReplaceGlobalRegExpWithEmptyString(
+ Isolate* isolate, Handle<String> subject, Handle<JSRegExp> regexp,
+ Handle<JSArray> last_match_info) {
+ DCHECK(subject->IsFlat());
+
+ // Shortcut for simple non-regexp global replacements
+ if (regexp->TypeTag() == JSRegExp::ATOM) {
+ Handle<String> empty_string = isolate->factory()->empty_string();
+ if (subject->IsOneByteRepresentation()) {
+ return StringReplaceGlobalAtomRegExpWithString<SeqOneByteString>(
+ isolate, subject, regexp, empty_string, last_match_info);
+ } else {
+ return StringReplaceGlobalAtomRegExpWithString<SeqTwoByteString>(
+ isolate, subject, regexp, empty_string, last_match_info);
+ }
+ }
+
+ RegExpImpl::GlobalCache global_cache(regexp, subject, true, isolate);
+ if (global_cache.HasException()) return isolate->heap()->exception();
+
+ int32_t* current_match = global_cache.FetchNext();
+ if (current_match == NULL) {
+ if (global_cache.HasException()) return isolate->heap()->exception();
+ return *subject;
+ }
+
+ int start = current_match[0];
+ int end = current_match[1];
+ int capture_count = regexp->CaptureCount();
+ int subject_length = subject->length();
+
+ int new_length = subject_length - (end - start);
+ if (new_length == 0) return isolate->heap()->empty_string();
+
+ Handle<ResultSeqString> answer;
+ if (ResultSeqString::kHasOneByteEncoding) {
+ answer = Handle<ResultSeqString>::cast(
+ isolate->factory()->NewRawOneByteString(new_length).ToHandleChecked());
+ } else {
+ answer = Handle<ResultSeqString>::cast(
+ isolate->factory()->NewRawTwoByteString(new_length).ToHandleChecked());
+ }
+
+ int prev = 0;
+ int position = 0;
+
+ do {
+ start = current_match[0];
+ end = current_match[1];
+ if (prev < start) {
+ // Add substring subject[prev;start] to answer string.
+ String::WriteToFlat(*subject, answer->GetChars() + position, prev, start);
+ position += start - prev;
+ }
+ prev = end;
+
+ current_match = global_cache.FetchNext();
+ } while (current_match != NULL);
+
+ if (global_cache.HasException()) return isolate->heap()->exception();
+
+ RegExpImpl::SetLastMatchInfo(last_match_info, subject, capture_count,
+ global_cache.LastSuccessfulMatch());
+
+ if (prev < subject_length) {
+ // Add substring subject[prev;length] to answer string.
+ String::WriteToFlat(*subject, answer->GetChars() + position, prev,
+ subject_length);
+ position += subject_length - prev;
+ }
+
+ if (position == 0) return isolate->heap()->empty_string();
+
+ // Shorten string and fill
+ int string_size = ResultSeqString::SizeFor(position);
+ int allocated_string_size = ResultSeqString::SizeFor(new_length);
+ int delta = allocated_string_size - string_size;
+
+ answer->set_length(position);
+ if (delta == 0) return *answer;
+
+ Address end_of_string = answer->address() + string_size;
+ Heap* heap = isolate->heap();
+
+ // The trimming is performed on a newly allocated object, which is on a
+ // fresly allocated page or on an already swept page. Hence, the sweeper
+ // thread can not get confused with the filler creation. No synchronization
+ // needed.
+ heap->CreateFillerObjectAt(end_of_string, delta);
+ heap->AdjustLiveBytes(answer->address(), -delta, Heap::FROM_MUTATOR);
+ return *answer;
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringReplaceGlobalRegExpWithString) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 4);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, replacement, 2);
+ CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 1);
+ CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 3);
+
+ RUNTIME_ASSERT(regexp->GetFlags().is_global());
+ RUNTIME_ASSERT(last_match_info->HasFastObjectElements());
+
+ subject = String::Flatten(subject);
+
+ if (replacement->length() == 0) {
+ if (subject->HasOnlyOneByteChars()) {
+ return StringReplaceGlobalRegExpWithEmptyString<SeqOneByteString>(
+ isolate, subject, regexp, last_match_info);
+ } else {
+ return StringReplaceGlobalRegExpWithEmptyString<SeqTwoByteString>(
+ isolate, subject, regexp, last_match_info);
+ }
+ }
+
+ replacement = String::Flatten(replacement);
+
+ return StringReplaceGlobalRegExpWithString(isolate, subject, regexp,
+ replacement, last_match_info);
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringSplit) {
+ HandleScope handle_scope(isolate);
+ DCHECK(args.length() == 3);
+ CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, pattern, 1);
+ CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]);
+ RUNTIME_ASSERT(limit > 0);
+
+ int subject_length = subject->length();
+ int pattern_length = pattern->length();
+ RUNTIME_ASSERT(pattern_length > 0);
+
+ if (limit == 0xffffffffu) {
+ Handle<Object> cached_answer(
+ RegExpResultsCache::Lookup(isolate->heap(), *subject, *pattern,
+ RegExpResultsCache::STRING_SPLIT_SUBSTRINGS),
+ isolate);
+ if (*cached_answer != Smi::FromInt(0)) {
+ // The cache FixedArray is a COW-array and can therefore be reused.
+ Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(
+ Handle<FixedArray>::cast(cached_answer));
+ return *result;
+ }
+ }
+
+ // The limit can be very large (0xffffffffu), but since the pattern
+ // isn't empty, we can never create more parts than ~half the length
+ // of the subject.
+
+ subject = String::Flatten(subject);
+ pattern = String::Flatten(pattern);
+
+ static const int kMaxInitialListCapacity = 16;
+
+ ZoneScope zone_scope(isolate->runtime_zone());
+
+ // Find (up to limit) indices of separator and end-of-string in subject
+ int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit);
+ ZoneList<int> indices(initial_capacity, zone_scope.zone());
+
+ FindStringIndicesDispatch(isolate, *subject, *pattern, &indices, limit,
+ zone_scope.zone());
+
+ if (static_cast<uint32_t>(indices.length()) < limit) {
+ indices.Add(subject_length, zone_scope.zone());
+ }
+
+ // The list indices now contains the end of each part to create.
+
+ // Create JSArray of substrings separated by separator.
+ int part_count = indices.length();
+
+ Handle<JSArray> result = isolate->factory()->NewJSArray(part_count);
+ JSObject::EnsureCanContainHeapObjectElements(result);
+ result->set_length(Smi::FromInt(part_count));
+
+ DCHECK(result->HasFastObjectElements());
+
+ if (part_count == 1 && indices.at(0) == subject_length) {
+ FixedArray::cast(result->elements())->set(0, *subject);
+ return *result;
+ }
+
+ Handle<FixedArray> elements(FixedArray::cast(result->elements()));
+ int part_start = 0;
+ for (int i = 0; i < part_count; i++) {
+ HandleScope local_loop_handle(isolate);
+ int part_end = indices.at(i);
+ Handle<String> substring =
+ isolate->factory()->NewProperSubString(subject, part_start, part_end);
+ elements->set(i, *substring);
+ part_start = part_end + pattern_length;
+ }
+
+ if (limit == 0xffffffffu) {
+ if (result->HasFastObjectElements()) {
+ RegExpResultsCache::Enter(isolate, subject, pattern, elements,
+ RegExpResultsCache::STRING_SPLIT_SUBSTRINGS);
+ }
+ }
+
+ return *result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_RegExpCompile) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 3);
+ CONVERT_ARG_HANDLE_CHECKED(JSRegExp, re, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, pattern, 1);
+ CONVERT_ARG_HANDLE_CHECKED(String, flags, 2);
+ Handle<Object> result;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
+ RegExpImpl::Compile(re, pattern, flags));
+ return *result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_RegExpExecRT) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 4);
+ CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, subject, 1);
+ CONVERT_INT32_ARG_CHECKED(index, 2);
+ CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 3);
+ // Due to the way the JS calls are constructed this must be less than the
+ // length of a string, i.e. it is always a Smi. We check anyway for security.
+ RUNTIME_ASSERT(index >= 0);
+ RUNTIME_ASSERT(index <= subject->length());
+ isolate->counters()->regexp_entry_runtime()->Increment();
+ Handle<Object> result;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, result,
+ RegExpImpl::Exec(regexp, subject, index, last_match_info));
+ return *result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_RegExpConstructResult) {
+ HandleScope handle_scope(isolate);
+ DCHECK(args.length() == 3);
+ CONVERT_SMI_ARG_CHECKED(size, 0);
+ RUNTIME_ASSERT(size >= 0 && size <= FixedArray::kMaxLength);
+ CONVERT_ARG_HANDLE_CHECKED(Object, index, 1);
+ CONVERT_ARG_HANDLE_CHECKED(Object, input, 2);
+ Handle<FixedArray> elements = isolate->factory()->NewFixedArray(size);
+ Handle<Map> regexp_map(isolate->native_context()->regexp_result_map());
+ Handle<JSObject> object =
+ isolate->factory()->NewJSObjectFromMap(regexp_map, NOT_TENURED, false);
+ Handle<JSArray> array = Handle<JSArray>::cast(object);
+ array->set_elements(*elements);
+ array->set_length(Smi::FromInt(size));
+ // Write in-object properties after the length of the array.
+ array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, *index);
+ array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, *input);
+ return *array;
+}
+
+
+RUNTIME_FUNCTION(Runtime_RegExpInitializeObject) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 6);
+ CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, source, 1);
+ // If source is the empty string we set it to "(?:)" instead as
+ // suggested by ECMA-262, 5th, section 15.10.4.1.
+ if (source->length() == 0) source = isolate->factory()->query_colon_string();
+
+ CONVERT_ARG_HANDLE_CHECKED(Object, global, 2);
+ if (!global->IsTrue()) global = isolate->factory()->false_value();
+
+ CONVERT_ARG_HANDLE_CHECKED(Object, ignoreCase, 3);
+ if (!ignoreCase->IsTrue()) ignoreCase = isolate->factory()->false_value();
+
+ CONVERT_ARG_HANDLE_CHECKED(Object, multiline, 4);
+ if (!multiline->IsTrue()) multiline = isolate->factory()->false_value();
+
+ CONVERT_ARG_HANDLE_CHECKED(Object, sticky, 5);
+ if (!sticky->IsTrue()) sticky = isolate->factory()->false_value();
+
+ Map* map = regexp->map();
+ Object* constructor = map->constructor();
+ if (!FLAG_harmony_regexps && constructor->IsJSFunction() &&
+ JSFunction::cast(constructor)->initial_map() == map) {
+ // If we still have the original map, set in-object properties directly.
+ regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, *source);
+ // Both true and false are immovable immortal objects so no need for write
+ // barrier.
+ regexp->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, *global,
+ SKIP_WRITE_BARRIER);
+ regexp->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, *ignoreCase,
+ SKIP_WRITE_BARRIER);
+ regexp->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, *multiline,
+ SKIP_WRITE_BARRIER);
+ regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
+ Smi::FromInt(0), SKIP_WRITE_BARRIER);
+ return *regexp;
+ }
+
+ // Map has changed, so use generic, but slower, method. We also end here if
+ // the --harmony-regexp flag is set, because the initial map does not have
+ // space for the 'sticky' flag, since it is from the snapshot, but must work
+ // both with and without --harmony-regexp. When sticky comes out from under
+ // the flag, we will be able to use the fast initial map.
+ PropertyAttributes final =
+ static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE);
+ PropertyAttributes writable =
+ static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
+ Handle<Object> zero(Smi::FromInt(0), isolate);
+ Factory* factory = isolate->factory();
+ JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->source_string(),
+ source, final).Check();
+ JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->global_string(),
+ global, final).Check();
+ JSObject::SetOwnPropertyIgnoreAttributes(
+ regexp, factory->ignore_case_string(), ignoreCase, final).Check();
+ JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->multiline_string(),
+ multiline, final).Check();
+ if (FLAG_harmony_regexps) {
+ JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->sticky_string(),
+ sticky, final).Check();
+ }
+ JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->last_index_string(),
+ zero, writable).Check();
+ return *regexp;
+}
+
+
+RUNTIME_FUNCTION(Runtime_MaterializeRegExpLiteral) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 4);
+ CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0);
+ CONVERT_SMI_ARG_CHECKED(index, 1);
+ CONVERT_ARG_HANDLE_CHECKED(String, pattern, 2);
+ CONVERT_ARG_HANDLE_CHECKED(String, flags, 3);
+
+ // Get the RegExp function from the context in the literals array.
+ // This is the RegExp function from the context in which the
+ // function was created. We do not use the RegExp function from the
+ // current native context because this might be the RegExp function
+ // from another context which we should not have access to.
+ Handle<JSFunction> constructor = Handle<JSFunction>(
+ JSFunction::NativeContextFromLiterals(*literals)->regexp_function());
+ // Compute the regular expression literal.
+ Handle<Object> regexp;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, regexp,
+ RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags));
+ literals->set(index, *regexp);
+ return *regexp;
+}
+
+
+// Only called from Runtime_RegExpExecMultiple so it doesn't need to maintain
+// separate last match info. See comment on that function.
+template <bool has_capture>
+static Object* SearchRegExpMultiple(Isolate* isolate, Handle<String> subject,
+ Handle<JSRegExp> regexp,
+ Handle<JSArray> last_match_array,
+ Handle<JSArray> result_array) {
+ DCHECK(subject->IsFlat());
+ DCHECK_NE(has_capture, regexp->CaptureCount() == 0);
+
+ int capture_count = regexp->CaptureCount();
+ int subject_length = subject->length();
+
+ static const int kMinLengthToCache = 0x1000;
+
+ if (subject_length > kMinLengthToCache) {
+ Handle<Object> cached_answer(
+ RegExpResultsCache::Lookup(isolate->heap(), *subject, regexp->data(),
+ RegExpResultsCache::REGEXP_MULTIPLE_INDICES),
+ isolate);
+ if (*cached_answer != Smi::FromInt(0)) {
+ Handle<FixedArray> cached_fixed_array =
+ Handle<FixedArray>(FixedArray::cast(*cached_answer));
+ // The cache FixedArray is a COW-array and can therefore be reused.
+ JSArray::SetContent(result_array, cached_fixed_array);
+ // The actual length of the result array is stored in the last element of
+ // the backing store (the backing FixedArray may have a larger capacity).
+ Object* cached_fixed_array_last_element =
+ cached_fixed_array->get(cached_fixed_array->length() - 1);
+ Smi* js_array_length = Smi::cast(cached_fixed_array_last_element);
+ result_array->set_length(js_array_length);
+ RegExpImpl::SetLastMatchInfo(last_match_array, subject, capture_count,
+ NULL);
+ return *result_array;
+ }
+ }
+
+ RegExpImpl::GlobalCache global_cache(regexp, subject, true, isolate);
+ if (global_cache.HasException()) return isolate->heap()->exception();
+
+ // Ensured in Runtime_RegExpExecMultiple.
+ DCHECK(result_array->HasFastObjectElements());
+ Handle<FixedArray> result_elements(
+ FixedArray::cast(result_array->elements()));
+ if (result_elements->length() < 16) {
+ result_elements = isolate->factory()->NewFixedArrayWithHoles(16);
+ }
+
+ FixedArrayBuilder builder(result_elements);
+
+ // Position to search from.
+ int match_start = -1;
+ int match_end = 0;
+ bool first = true;
+
+ // Two smis before and after the match, for very long strings.
+ static const int kMaxBuilderEntriesPerRegExpMatch = 5;
+
+ while (true) {
+ int32_t* current_match = global_cache.FetchNext();
+ if (current_match == NULL) break;
+ match_start = current_match[0];
+ builder.EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch);
+ if (match_end < match_start) {
+ ReplacementStringBuilder::AddSubjectSlice(&builder, match_end,
+ match_start);
+ }
+ match_end = current_match[1];
+ {
+ // Avoid accumulating new handles inside loop.
+ HandleScope temp_scope(isolate);
+ Handle<String> match;
+ if (!first) {
+ match = isolate->factory()->NewProperSubString(subject, match_start,
+ match_end);
+ } else {
+ match =
+ isolate->factory()->NewSubString(subject, match_start, match_end);
+ first = false;
+ }
+
+ if (has_capture) {
+ // Arguments array to replace function is match, captures, index and
+ // subject, i.e., 3 + capture count in total.
+ Handle<FixedArray> elements =
+ isolate->factory()->NewFixedArray(3 + capture_count);
+
+ elements->set(0, *match);
+ for (int i = 1; i <= capture_count; i++) {
+ int start = current_match[i * 2];
+ if (start >= 0) {
+ int end = current_match[i * 2 + 1];
+ DCHECK(start <= end);
+ Handle<String> substring =
+ isolate->factory()->NewSubString(subject, start, end);
+ elements->set(i, *substring);
+ } else {
+ DCHECK(current_match[i * 2 + 1] < 0);
+ elements->set(i, isolate->heap()->undefined_value());
+ }
+ }
+ elements->set(capture_count + 1, Smi::FromInt(match_start));
+ elements->set(capture_count + 2, *subject);
+ builder.Add(*isolate->factory()->NewJSArrayWithElements(elements));
+ } else {
+ builder.Add(*match);
+ }
+ }
+ }
+
+ if (global_cache.HasException()) return isolate->heap()->exception();
+
+ if (match_start >= 0) {
+ // Finished matching, with at least one match.
+ if (match_end < subject_length) {
+ ReplacementStringBuilder::AddSubjectSlice(&builder, match_end,
+ subject_length);
+ }
+
+ RegExpImpl::SetLastMatchInfo(last_match_array, subject, capture_count,
+ NULL);
+
+ if (subject_length > kMinLengthToCache) {
+ // Store the length of the result array into the last element of the
+ // backing FixedArray.
+ builder.EnsureCapacity(1);
+ Handle<FixedArray> fixed_array = builder.array();
+ fixed_array->set(fixed_array->length() - 1,
+ Smi::FromInt(builder.length()));
+ // Cache the result and turn the FixedArray into a COW array.
+ RegExpResultsCache::Enter(isolate, subject,
+ handle(regexp->data(), isolate), fixed_array,
+ RegExpResultsCache::REGEXP_MULTIPLE_INDICES);
+ }
+ return *builder.ToJSArray(result_array);
+ } else {
+ return isolate->heap()->null_value(); // No matches at all.
+ }
+}
+
+
+// This is only called for StringReplaceGlobalRegExpWithFunction. This sets
+// lastMatchInfoOverride to maintain the last match info, so we don't need to
+// set any other last match array info.
+RUNTIME_FUNCTION(Runtime_RegExpExecMultiple) {
+ HandleScope handles(isolate);
+ DCHECK(args.length() == 4);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, subject, 1);
+ CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0);
+ CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 2);
+ CONVERT_ARG_HANDLE_CHECKED(JSArray, result_array, 3);
+ RUNTIME_ASSERT(last_match_info->HasFastObjectElements());
+ RUNTIME_ASSERT(result_array->HasFastObjectElements());
+
+ subject = String::Flatten(subject);
+ RUNTIME_ASSERT(regexp->GetFlags().is_global());
+
+ if (regexp->CaptureCount() == 0) {
+ return SearchRegExpMultiple<false>(isolate, subject, regexp,
+ last_match_info, result_array);
+ } else {
+ return SearchRegExpMultiple<true>(isolate, subject, regexp, last_match_info,
+ result_array);
+ }
+}
+
+
+RUNTIME_FUNCTION(RuntimeReference_RegExpConstructResult) {
+ SealHandleScope shs(isolate);
+ return __RT_impl_Runtime_RegExpConstructResult(args, isolate);
+}
+
+
+RUNTIME_FUNCTION(RuntimeReference_RegExpExec) {
+ SealHandleScope shs(isolate);
+ return __RT_impl_Runtime_RegExpExecRT(args, isolate);
+}
+
+
+// Perform string match of pattern on subject, starting at start index.
+// Caller must ensure that 0 <= start_index <= sub->length(),
+// and should check that pat->length() + start_index <= sub->length().
+int Runtime::StringMatch(Isolate* isolate, Handle<String> sub,
+ Handle<String> pat, int start_index) {
+ DCHECK(0 <= start_index);
+ DCHECK(start_index <= sub->length());
+
+ int pattern_length = pat->length();
+ if (pattern_length == 0) return start_index;
+
+ int subject_length = sub->length();
+ if (start_index + pattern_length > subject_length) return -1;
+
+ sub = String::Flatten(sub);
+ pat = String::Flatten(pat);
+
+ DisallowHeapAllocation no_gc; // ensure vectors stay valid
+ // Extract flattened substrings of cons strings before getting encoding.
+ String::FlatContent seq_sub = sub->GetFlatContent();
+ String::FlatContent seq_pat = pat->GetFlatContent();
+
+ // dispatch on type of strings
+ if (seq_pat.IsOneByte()) {
+ Vector<const uint8_t> pat_vector = seq_pat.ToOneByteVector();
+ if (seq_sub.IsOneByte()) {
+ return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
+ start_index);
+ }
+ return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector,
+ start_index);
+ }
+ Vector<const uc16> pat_vector = seq_pat.ToUC16Vector();
+ if (seq_sub.IsOneByte()) {
+ return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
+ start_index);
+ }
+ return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, start_index);
+}
+}
+} // namespace v8::internal
--- /dev/null
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/v8.h"
+
+#include "src/arguments.h"
+#include "src/jsregexp-inl.h"
+#include "src/jsregexp.h"
+#include "src/runtime/runtime.h"
+#include "src/runtime/runtime-utils.h"
+#include "src/runtime/string-builder.h"
+#include "src/string-search.h"
+
+namespace v8 {
+namespace internal {
+
+
+// This may return an empty MaybeHandle if an exception is thrown or
+// we abort due to reaching the recursion limit.
+MaybeHandle<String> StringReplaceOneCharWithString(
+ Isolate* isolate, Handle<String> subject, Handle<String> search,
+ Handle<String> replace, bool* found, int recursion_limit) {
+ StackLimitCheck stackLimitCheck(isolate);
+ if (stackLimitCheck.HasOverflowed() || (recursion_limit == 0)) {
+ return MaybeHandle<String>();
+ }
+ recursion_limit--;
+ if (subject->IsConsString()) {
+ ConsString* cons = ConsString::cast(*subject);
+ Handle<String> first = Handle<String>(cons->first());
+ Handle<String> second = Handle<String>(cons->second());
+ Handle<String> new_first;
+ if (!StringReplaceOneCharWithString(isolate, first, search, replace, found,
+ recursion_limit).ToHandle(&new_first)) {
+ return MaybeHandle<String>();
+ }
+ if (*found) return isolate->factory()->NewConsString(new_first, second);
+
+ Handle<String> new_second;
+ if (!StringReplaceOneCharWithString(isolate, second, search, replace, found,
+ recursion_limit)
+ .ToHandle(&new_second)) {
+ return MaybeHandle<String>();
+ }
+ if (*found) return isolate->factory()->NewConsString(first, new_second);
+
+ return subject;
+ } else {
+ int index = Runtime::StringMatch(isolate, subject, search, 0);
+ if (index == -1) return subject;
+ *found = true;
+ Handle<String> first = isolate->factory()->NewSubString(subject, 0, index);
+ Handle<String> cons1;
+ ASSIGN_RETURN_ON_EXCEPTION(
+ isolate, cons1, isolate->factory()->NewConsString(first, replace),
+ String);
+ Handle<String> second =
+ isolate->factory()->NewSubString(subject, index + 1, subject->length());
+ return isolate->factory()->NewConsString(cons1, second);
+ }
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringReplaceOneCharWithString) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 3);
+ CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, search, 1);
+ CONVERT_ARG_HANDLE_CHECKED(String, replace, 2);
+
+ // If the cons string tree is too deep, we simply abort the recursion and
+ // retry with a flattened subject string.
+ const int kRecursionLimit = 0x1000;
+ bool found = false;
+ Handle<String> result;
+ if (StringReplaceOneCharWithString(isolate, subject, search, replace, &found,
+ kRecursionLimit).ToHandle(&result)) {
+ return *result;
+ }
+ if (isolate->has_pending_exception()) return isolate->heap()->exception();
+
+ subject = String::Flatten(subject);
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, result,
+ StringReplaceOneCharWithString(isolate, subject, search, replace, &found,
+ kRecursionLimit));
+ return *result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringIndexOf) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 3);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, sub, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, pat, 1);
+ CONVERT_ARG_HANDLE_CHECKED(Object, index, 2);
+
+ uint32_t start_index;
+ if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1);
+
+ RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length()));
+ int position = Runtime::StringMatch(isolate, sub, pat, start_index);
+ return Smi::FromInt(position);
+}
+
+
+template <typename schar, typename pchar>
+static int StringMatchBackwards(Vector<const schar> subject,
+ Vector<const pchar> pattern, int idx) {
+ int pattern_length = pattern.length();
+ DCHECK(pattern_length >= 1);
+ DCHECK(idx + pattern_length <= subject.length());
+
+ if (sizeof(schar) == 1 && sizeof(pchar) > 1) {
+ for (int i = 0; i < pattern_length; i++) {
+ uc16 c = pattern[i];
+ if (c > String::kMaxOneByteCharCode) {
+ return -1;
+ }
+ }
+ }
+
+ pchar pattern_first_char = pattern[0];
+ for (int i = idx; i >= 0; i--) {
+ if (subject[i] != pattern_first_char) continue;
+ int j = 1;
+ while (j < pattern_length) {
+ if (pattern[j] != subject[i + j]) {
+ break;
+ }
+ j++;
+ }
+ if (j == pattern_length) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringLastIndexOf) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 3);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, sub, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, pat, 1);
+ CONVERT_ARG_HANDLE_CHECKED(Object, index, 2);
+
+ uint32_t start_index;
+ if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1);
+
+ uint32_t pat_length = pat->length();
+ uint32_t sub_length = sub->length();
+
+ if (start_index + pat_length > sub_length) {
+ start_index = sub_length - pat_length;
+ }
+
+ if (pat_length == 0) {
+ return Smi::FromInt(start_index);
+ }
+
+ sub = String::Flatten(sub);
+ pat = String::Flatten(pat);
+
+ int position = -1;
+ DisallowHeapAllocation no_gc; // ensure vectors stay valid
+
+ String::FlatContent sub_content = sub->GetFlatContent();
+ String::FlatContent pat_content = pat->GetFlatContent();
+
+ if (pat_content.IsOneByte()) {
+ Vector<const uint8_t> pat_vector = pat_content.ToOneByteVector();
+ if (sub_content.IsOneByte()) {
+ position = StringMatchBackwards(sub_content.ToOneByteVector(), pat_vector,
+ start_index);
+ } else {
+ position = StringMatchBackwards(sub_content.ToUC16Vector(), pat_vector,
+ start_index);
+ }
+ } else {
+ Vector<const uc16> pat_vector = pat_content.ToUC16Vector();
+ if (sub_content.IsOneByte()) {
+ position = StringMatchBackwards(sub_content.ToOneByteVector(), pat_vector,
+ start_index);
+ } else {
+ position = StringMatchBackwards(sub_content.ToUC16Vector(), pat_vector,
+ start_index);
+ }
+ }
+
+ return Smi::FromInt(position);
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringLocaleCompare) {
+ HandleScope handle_scope(isolate);
+ DCHECK(args.length() == 2);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, str1, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, str2, 1);
+
+ if (str1.is_identical_to(str2)) return Smi::FromInt(0); // Equal.
+ int str1_length = str1->length();
+ int str2_length = str2->length();
+
+ // Decide trivial cases without flattening.
+ if (str1_length == 0) {
+ if (str2_length == 0) return Smi::FromInt(0); // Equal.
+ return Smi::FromInt(-str2_length);
+ } else {
+ if (str2_length == 0) return Smi::FromInt(str1_length);
+ }
+
+ int end = str1_length < str2_length ? str1_length : str2_length;
+
+ // No need to flatten if we are going to find the answer on the first
+ // character. At this point we know there is at least one character
+ // in each string, due to the trivial case handling above.
+ int d = str1->Get(0) - str2->Get(0);
+ if (d != 0) return Smi::FromInt(d);
+
+ str1 = String::Flatten(str1);
+ str2 = String::Flatten(str2);
+
+ DisallowHeapAllocation no_gc;
+ String::FlatContent flat1 = str1->GetFlatContent();
+ String::FlatContent flat2 = str2->GetFlatContent();
+
+ for (int i = 0; i < end; i++) {
+ if (flat1.Get(i) != flat2.Get(i)) {
+ return Smi::FromInt(flat1.Get(i) - flat2.Get(i));
+ }
+ }
+
+ return Smi::FromInt(str1_length - str2_length);
+}
+
+
+RUNTIME_FUNCTION(Runtime_SubString) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 3);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, string, 0);
+ int start, end;
+ // We have a fast integer-only case here to avoid a conversion to double in
+ // the common case where from and to are Smis.
+ if (args[1]->IsSmi() && args[2]->IsSmi()) {
+ CONVERT_SMI_ARG_CHECKED(from_number, 1);
+ CONVERT_SMI_ARG_CHECKED(to_number, 2);
+ start = from_number;
+ end = to_number;
+ } else {
+ CONVERT_DOUBLE_ARG_CHECKED(from_number, 1);
+ CONVERT_DOUBLE_ARG_CHECKED(to_number, 2);
+ start = FastD2IChecked(from_number);
+ end = FastD2IChecked(to_number);
+ }
+ RUNTIME_ASSERT(end >= start);
+ RUNTIME_ASSERT(start >= 0);
+ RUNTIME_ASSERT(end <= string->length());
+ isolate->counters()->sub_string_runtime()->Increment();
+
+ return *isolate->factory()->NewSubString(string, start, end);
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringAdd) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 2);
+ CONVERT_ARG_HANDLE_CHECKED(String, str1, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, str2, 1);
+ isolate->counters()->string_add_runtime()->Increment();
+ Handle<String> result;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, result, isolate->factory()->NewConsString(str1, str2));
+ return *result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_InternalizeString) {
+ HandleScope handles(isolate);
+ RUNTIME_ASSERT(args.length() == 1);
+ CONVERT_ARG_HANDLE_CHECKED(String, string, 0);
+ return *isolate->factory()->InternalizeString(string);
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringMatch) {
+ HandleScope handles(isolate);
+ DCHECK(args.length() == 3);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
+ CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 1);
+ CONVERT_ARG_HANDLE_CHECKED(JSArray, regexp_info, 2);
+
+ RUNTIME_ASSERT(regexp_info->HasFastObjectElements());
+
+ RegExpImpl::GlobalCache global_cache(regexp, subject, true, isolate);
+ if (global_cache.HasException()) return isolate->heap()->exception();
+
+ int capture_count = regexp->CaptureCount();
+
+ ZoneScope zone_scope(isolate->runtime_zone());
+ ZoneList<int> offsets(8, zone_scope.zone());
+
+ while (true) {
+ int32_t* match = global_cache.FetchNext();
+ if (match == NULL) break;
+ offsets.Add(match[0], zone_scope.zone()); // start
+ offsets.Add(match[1], zone_scope.zone()); // end
+ }
+
+ if (global_cache.HasException()) return isolate->heap()->exception();
+
+ if (offsets.length() == 0) {
+ // Not a single match.
+ return isolate->heap()->null_value();
+ }
+
+ RegExpImpl::SetLastMatchInfo(regexp_info, subject, capture_count,
+ global_cache.LastSuccessfulMatch());
+
+ int matches = offsets.length() / 2;
+ Handle<FixedArray> elements = isolate->factory()->NewFixedArray(matches);
+ Handle<String> substring =
+ isolate->factory()->NewSubString(subject, offsets.at(0), offsets.at(1));
+ elements->set(0, *substring);
+ for (int i = 1; i < matches; i++) {
+ HandleScope temp_scope(isolate);
+ int from = offsets.at(i * 2);
+ int to = offsets.at(i * 2 + 1);
+ Handle<String> substring =
+ isolate->factory()->NewProperSubString(subject, from, to);
+ elements->set(i, *substring);
+ }
+ Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(elements);
+ result->set_length(Smi::FromInt(matches));
+ return *result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringCharCodeAtRT) {
+ HandleScope handle_scope(isolate);
+ DCHECK(args.length() == 2);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
+ CONVERT_NUMBER_CHECKED(uint32_t, i, Uint32, args[1]);
+
+ // Flatten the string. If someone wants to get a char at an index
+ // in a cons string, it is likely that more indices will be
+ // accessed.
+ subject = String::Flatten(subject);
+
+ if (i >= static_cast<uint32_t>(subject->length())) {
+ return isolate->heap()->nan_value();
+ }
+
+ return Smi::FromInt(subject->Get(i));
+}
+
+
+RUNTIME_FUNCTION(Runtime_CharFromCode) {
+ HandleScope handlescope(isolate);
+ DCHECK(args.length() == 1);
+ if (args[0]->IsNumber()) {
+ CONVERT_NUMBER_CHECKED(uint32_t, code, Uint32, args[0]);
+ code &= 0xffff;
+ return *isolate->factory()->LookupSingleCharacterStringFromCode(code);
+ }
+ return isolate->heap()->empty_string();
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringCompare) {
+ HandleScope handle_scope(isolate);
+ DCHECK(args.length() == 2);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, x, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, y, 1);
+
+ isolate->counters()->string_compare_runtime()->Increment();
+
+ // A few fast case tests before we flatten.
+ if (x.is_identical_to(y)) return Smi::FromInt(EQUAL);
+ if (y->length() == 0) {
+ if (x->length() == 0) return Smi::FromInt(EQUAL);
+ return Smi::FromInt(GREATER);
+ } else if (x->length() == 0) {
+ return Smi::FromInt(LESS);
+ }
+
+ int d = x->Get(0) - y->Get(0);
+ if (d < 0)
+ return Smi::FromInt(LESS);
+ else if (d > 0)
+ return Smi::FromInt(GREATER);
+
+ // Slow case.
+ x = String::Flatten(x);
+ y = String::Flatten(y);
+
+ DisallowHeapAllocation no_gc;
+ Object* equal_prefix_result = Smi::FromInt(EQUAL);
+ int prefix_length = x->length();
+ if (y->length() < prefix_length) {
+ prefix_length = y->length();
+ equal_prefix_result = Smi::FromInt(GREATER);
+ } else if (y->length() > prefix_length) {
+ equal_prefix_result = Smi::FromInt(LESS);
+ }
+ int r;
+ String::FlatContent x_content = x->GetFlatContent();
+ String::FlatContent y_content = y->GetFlatContent();
+ if (x_content.IsOneByte()) {
+ Vector<const uint8_t> x_chars = x_content.ToOneByteVector();
+ if (y_content.IsOneByte()) {
+ Vector<const uint8_t> y_chars = y_content.ToOneByteVector();
+ r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
+ } else {
+ Vector<const uc16> y_chars = y_content.ToUC16Vector();
+ r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
+ }
+ } else {
+ Vector<const uc16> x_chars = x_content.ToUC16Vector();
+ if (y_content.IsOneByte()) {
+ Vector<const uint8_t> y_chars = y_content.ToOneByteVector();
+ r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
+ } else {
+ Vector<const uc16> y_chars = y_content.ToUC16Vector();
+ r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
+ }
+ }
+ Object* result;
+ if (r == 0) {
+ result = equal_prefix_result;
+ } else {
+ result = (r < 0) ? Smi::FromInt(LESS) : Smi::FromInt(GREATER);
+ }
+ return result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringBuilderConcat) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 3);
+ CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0);
+ int32_t array_length;
+ if (!args[1]->ToInt32(&array_length)) {
+ THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
+ }
+ CONVERT_ARG_HANDLE_CHECKED(String, special, 2);
+
+ size_t actual_array_length = 0;
+ RUNTIME_ASSERT(
+ TryNumberToSize(isolate, array->length(), &actual_array_length));
+ RUNTIME_ASSERT(array_length >= 0);
+ RUNTIME_ASSERT(static_cast<size_t>(array_length) <= actual_array_length);
+
+ // This assumption is used by the slice encoding in one or two smis.
+ DCHECK(Smi::kMaxValue >= String::kMaxLength);
+
+ RUNTIME_ASSERT(array->HasFastElements());
+ JSObject::EnsureCanContainHeapObjectElements(array);
+
+ int special_length = special->length();
+ if (!array->HasFastObjectElements()) {
+ return isolate->Throw(isolate->heap()->illegal_argument_string());
+ }
+
+ int length;
+ bool one_byte = special->HasOnlyOneByteChars();
+
+ {
+ DisallowHeapAllocation no_gc;
+ FixedArray* fixed_array = FixedArray::cast(array->elements());
+ if (fixed_array->length() < array_length) {
+ array_length = fixed_array->length();
+ }
+
+ if (array_length == 0) {
+ return isolate->heap()->empty_string();
+ } else if (array_length == 1) {
+ Object* first = fixed_array->get(0);
+ if (first->IsString()) return first;
+ }
+ length = StringBuilderConcatLength(special_length, fixed_array,
+ array_length, &one_byte);
+ }
+
+ if (length == -1) {
+ return isolate->Throw(isolate->heap()->illegal_argument_string());
+ }
+
+ if (one_byte) {
+ Handle<SeqOneByteString> answer;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, answer, isolate->factory()->NewRawOneByteString(length));
+ StringBuilderConcatHelper(*special, answer->GetChars(),
+ FixedArray::cast(array->elements()),
+ array_length);
+ return *answer;
+ } else {
+ Handle<SeqTwoByteString> answer;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, answer, isolate->factory()->NewRawTwoByteString(length));
+ StringBuilderConcatHelper(*special, answer->GetChars(),
+ FixedArray::cast(array->elements()),
+ array_length);
+ return *answer;
+ }
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringBuilderJoin) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 3);
+ CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0);
+ int32_t array_length;
+ if (!args[1]->ToInt32(&array_length)) {
+ THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
+ }
+ CONVERT_ARG_HANDLE_CHECKED(String, separator, 2);
+ RUNTIME_ASSERT(array->HasFastObjectElements());
+ RUNTIME_ASSERT(array_length >= 0);
+
+ Handle<FixedArray> fixed_array(FixedArray::cast(array->elements()));
+ if (fixed_array->length() < array_length) {
+ array_length = fixed_array->length();
+ }
+
+ if (array_length == 0) {
+ return isolate->heap()->empty_string();
+ } else if (array_length == 1) {
+ Object* first = fixed_array->get(0);
+ RUNTIME_ASSERT(first->IsString());
+ return first;
+ }
+
+ int separator_length = separator->length();
+ RUNTIME_ASSERT(separator_length > 0);
+ int max_nof_separators =
+ (String::kMaxLength + separator_length - 1) / separator_length;
+ if (max_nof_separators < (array_length - 1)) {
+ THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
+ }
+ int length = (array_length - 1) * separator_length;
+ for (int i = 0; i < array_length; i++) {
+ Object* element_obj = fixed_array->get(i);
+ RUNTIME_ASSERT(element_obj->IsString());
+ String* element = String::cast(element_obj);
+ int increment = element->length();
+ if (increment > String::kMaxLength - length) {
+ STATIC_ASSERT(String::kMaxLength < kMaxInt);
+ length = kMaxInt; // Provoke exception;
+ break;
+ }
+ length += increment;
+ }
+
+ Handle<SeqTwoByteString> answer;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, answer, isolate->factory()->NewRawTwoByteString(length));
+
+ DisallowHeapAllocation no_gc;
+
+ uc16* sink = answer->GetChars();
+#ifdef DEBUG
+ uc16* end = sink + length;
+#endif
+
+ RUNTIME_ASSERT(fixed_array->get(0)->IsString());
+ String* first = String::cast(fixed_array->get(0));
+ String* separator_raw = *separator;
+ int first_length = first->length();
+ String::WriteToFlat(first, sink, 0, first_length);
+ sink += first_length;
+
+ for (int i = 1; i < array_length; i++) {
+ DCHECK(sink + separator_length <= end);
+ String::WriteToFlat(separator_raw, sink, 0, separator_length);
+ sink += separator_length;
+
+ RUNTIME_ASSERT(fixed_array->get(i)->IsString());
+ String* element = String::cast(fixed_array->get(i));
+ int element_length = element->length();
+ DCHECK(sink + element_length <= end);
+ String::WriteToFlat(element, sink, 0, element_length);
+ sink += element_length;
+ }
+ DCHECK(sink == end);
+
+ // Use %_FastOneByteArrayJoin instead.
+ DCHECK(!answer->IsOneByteRepresentation());
+ return *answer;
+}
+
+template <typename Char>
+static void JoinSparseArrayWithSeparator(FixedArray* elements,
+ int elements_length,
+ uint32_t array_length,
+ String* separator,
+ Vector<Char> buffer) {
+ DisallowHeapAllocation no_gc;
+ int previous_separator_position = 0;
+ int separator_length = separator->length();
+ int cursor = 0;
+ for (int i = 0; i < elements_length; i += 2) {
+ int position = NumberToInt32(elements->get(i));
+ String* string = String::cast(elements->get(i + 1));
+ int string_length = string->length();
+ if (string->length() > 0) {
+ while (previous_separator_position < position) {
+ String::WriteToFlat<Char>(separator, &buffer[cursor], 0,
+ separator_length);
+ cursor += separator_length;
+ previous_separator_position++;
+ }
+ String::WriteToFlat<Char>(string, &buffer[cursor], 0, string_length);
+ cursor += string->length();
+ }
+ }
+ if (separator_length > 0) {
+ // Array length must be representable as a signed 32-bit number,
+ // otherwise the total string length would have been too large.
+ DCHECK(array_length <= 0x7fffffff); // Is int32_t.
+ int last_array_index = static_cast<int>(array_length - 1);
+ while (previous_separator_position < last_array_index) {
+ String::WriteToFlat<Char>(separator, &buffer[cursor], 0,
+ separator_length);
+ cursor += separator_length;
+ previous_separator_position++;
+ }
+ }
+ DCHECK(cursor <= buffer.length());
+}
+
+
+RUNTIME_FUNCTION(Runtime_SparseJoinWithSeparator) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 3);
+ CONVERT_ARG_HANDLE_CHECKED(JSArray, elements_array, 0);
+ CONVERT_NUMBER_CHECKED(uint32_t, array_length, Uint32, args[1]);
+ CONVERT_ARG_HANDLE_CHECKED(String, separator, 2);
+ // elements_array is fast-mode JSarray of alternating positions
+ // (increasing order) and strings.
+ RUNTIME_ASSERT(elements_array->HasFastSmiOrObjectElements());
+ // array_length is length of original array (used to add separators);
+ // separator is string to put between elements. Assumed to be non-empty.
+ RUNTIME_ASSERT(array_length > 0);
+
+ // Find total length of join result.
+ int string_length = 0;
+ bool is_one_byte = separator->IsOneByteRepresentation();
+ bool overflow = false;
+ CONVERT_NUMBER_CHECKED(int, elements_length, Int32, elements_array->length());
+ RUNTIME_ASSERT(elements_length <= elements_array->elements()->length());
+ RUNTIME_ASSERT((elements_length & 1) == 0); // Even length.
+ FixedArray* elements = FixedArray::cast(elements_array->elements());
+ for (int i = 0; i < elements_length; i += 2) {
+ RUNTIME_ASSERT(elements->get(i)->IsNumber());
+ CONVERT_NUMBER_CHECKED(uint32_t, position, Uint32, elements->get(i));
+ RUNTIME_ASSERT(position < array_length);
+ RUNTIME_ASSERT(elements->get(i + 1)->IsString());
+ }
+
+ {
+ DisallowHeapAllocation no_gc;
+ for (int i = 0; i < elements_length; i += 2) {
+ String* string = String::cast(elements->get(i + 1));
+ int length = string->length();
+ if (is_one_byte && !string->IsOneByteRepresentation()) {
+ is_one_byte = false;
+ }
+ if (length > String::kMaxLength ||
+ String::kMaxLength - length < string_length) {
+ overflow = true;
+ break;
+ }
+ string_length += length;
+ }
+ }
+
+ int separator_length = separator->length();
+ if (!overflow && separator_length > 0) {
+ if (array_length <= 0x7fffffffu) {
+ int separator_count = static_cast<int>(array_length) - 1;
+ int remaining_length = String::kMaxLength - string_length;
+ if ((remaining_length / separator_length) >= separator_count) {
+ string_length += separator_length * (array_length - 1);
+ } else {
+ // Not room for the separators within the maximal string length.
+ overflow = true;
+ }
+ } else {
+ // Nonempty separator and at least 2^31-1 separators necessary
+ // means that the string is too large to create.
+ STATIC_ASSERT(String::kMaxLength < 0x7fffffff);
+ overflow = true;
+ }
+ }
+ if (overflow) {
+ // Throw an exception if the resulting string is too large. See
+ // https://code.google.com/p/chromium/issues/detail?id=336820
+ // for details.
+ THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
+ }
+
+ if (is_one_byte) {
+ Handle<SeqOneByteString> result = isolate->factory()
+ ->NewRawOneByteString(string_length)
+ .ToHandleChecked();
+ JoinSparseArrayWithSeparator<uint8_t>(
+ FixedArray::cast(elements_array->elements()), elements_length,
+ array_length, *separator,
+ Vector<uint8_t>(result->GetChars(), string_length));
+ return *result;
+ } else {
+ Handle<SeqTwoByteString> result = isolate->factory()
+ ->NewRawTwoByteString(string_length)
+ .ToHandleChecked();
+ JoinSparseArrayWithSeparator<uc16>(
+ FixedArray::cast(elements_array->elements()), elements_length,
+ array_length, *separator,
+ Vector<uc16>(result->GetChars(), string_length));
+ return *result;
+ }
+}
+
+
+// Copies Latin1 characters to the given fixed array looking up
+// one-char strings in the cache. Gives up on the first char that is
+// not in the cache and fills the remainder with smi zeros. Returns
+// the length of the successfully copied prefix.
+static int CopyCachedOneByteCharsToArray(Heap* heap, const uint8_t* chars,
+ FixedArray* elements, int length) {
+ DisallowHeapAllocation no_gc;
+ FixedArray* one_byte_cache = heap->single_character_string_cache();
+ Object* undefined = heap->undefined_value();
+ int i;
+ WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc);
+ for (i = 0; i < length; ++i) {
+ Object* value = one_byte_cache->get(chars[i]);
+ if (value == undefined) break;
+ elements->set(i, value, mode);
+ }
+ if (i < length) {
+ DCHECK(Smi::FromInt(0) == 0);
+ memset(elements->data_start() + i, 0, kPointerSize * (length - i));
+ }
+#ifdef DEBUG
+ for (int j = 0; j < length; ++j) {
+ Object* element = elements->get(j);
+ DCHECK(element == Smi::FromInt(0) ||
+ (element->IsString() && String::cast(element)->LooksValid()));
+ }
+#endif
+ return i;
+}
+
+
+// Converts a String to JSArray.
+// For example, "foo" => ["f", "o", "o"].
+RUNTIME_FUNCTION(Runtime_StringToArray) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 2);
+ CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
+ CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]);
+
+ s = String::Flatten(s);
+ const int length = static_cast<int>(Min<uint32_t>(s->length(), limit));
+
+ Handle<FixedArray> elements;
+ int position = 0;
+ if (s->IsFlat() && s->IsOneByteRepresentation()) {
+ // Try using cached chars where possible.
+ elements = isolate->factory()->NewUninitializedFixedArray(length);
+
+ DisallowHeapAllocation no_gc;
+ String::FlatContent content = s->GetFlatContent();
+ if (content.IsOneByte()) {
+ Vector<const uint8_t> chars = content.ToOneByteVector();
+ // Note, this will initialize all elements (not only the prefix)
+ // to prevent GC from seeing partially initialized array.
+ position = CopyCachedOneByteCharsToArray(isolate->heap(), chars.start(),
+ *elements, length);
+ } else {
+ MemsetPointer(elements->data_start(), isolate->heap()->undefined_value(),
+ length);
+ }
+ } else {
+ elements = isolate->factory()->NewFixedArray(length);
+ }
+ for (int i = position; i < length; ++i) {
+ Handle<Object> str =
+ isolate->factory()->LookupSingleCharacterStringFromCode(s->Get(i));
+ elements->set(i, *str);
+ }
+
+#ifdef DEBUG
+ for (int i = 0; i < length; ++i) {
+ DCHECK(String::cast(elements->get(i))->length() == 1);
+ }
+#endif
+
+ return *isolate->factory()->NewJSArrayWithElements(elements);
+}
+
+
+static inline bool ToUpperOverflows(uc32 character) {
+ // y with umlauts and the micro sign are the only characters that stop
+ // fitting into one-byte when converting to uppercase.
+ static const uc32 yuml_code = 0xff;
+ static const uc32 micro_code = 0xb5;
+ return (character == yuml_code || character == micro_code);
+}
+
+
+template <class Converter>
+MUST_USE_RESULT static Object* ConvertCaseHelper(
+ Isolate* isolate, String* string, SeqString* result, int result_length,
+ unibrow::Mapping<Converter, 128>* mapping) {
+ DisallowHeapAllocation no_gc;
+ // We try this twice, once with the assumption that the result is no longer
+ // than the input and, if that assumption breaks, again with the exact
+ // length. This may not be pretty, but it is nicer than what was here before
+ // and I hereby claim my vaffel-is.
+ //
+ // NOTE: This assumes that the upper/lower case of an ASCII
+ // character is also ASCII. This is currently the case, but it
+ // might break in the future if we implement more context and locale
+ // dependent upper/lower conversions.
+ bool has_changed_character = false;
+
+ // Convert all characters to upper case, assuming that they will fit
+ // in the buffer
+ Access<ConsStringIteratorOp> op(isolate->runtime_state()->string_iterator());
+ StringCharacterStream stream(string, op.value());
+ unibrow::uchar chars[Converter::kMaxWidth];
+ // We can assume that the string is not empty
+ uc32 current = stream.GetNext();
+ bool ignore_overflow = Converter::kIsToLower || result->IsSeqTwoByteString();
+ for (int i = 0; i < result_length;) {
+ bool has_next = stream.HasMore();
+ uc32 next = has_next ? stream.GetNext() : 0;
+ int char_length = mapping->get(current, next, chars);
+ if (char_length == 0) {
+ // The case conversion of this character is the character itself.
+ result->Set(i, current);
+ i++;
+ } else if (char_length == 1 &&
+ (ignore_overflow || !ToUpperOverflows(current))) {
+ // Common case: converting the letter resulted in one character.
+ DCHECK(static_cast<uc32>(chars[0]) != current);
+ result->Set(i, chars[0]);
+ has_changed_character = true;
+ i++;
+ } else if (result_length == string->length()) {
+ bool overflows = ToUpperOverflows(current);
+ // We've assumed that the result would be as long as the
+ // input but here is a character that converts to several
+ // characters. No matter, we calculate the exact length
+ // of the result and try the whole thing again.
+ //
+ // Note that this leaves room for optimization. We could just
+ // memcpy what we already have to the result string. Also,
+ // the result string is the last object allocated we could
+ // "realloc" it and probably, in the vast majority of cases,
+ // extend the existing string to be able to hold the full
+ // result.
+ int next_length = 0;
+ if (has_next) {
+ next_length = mapping->get(next, 0, chars);
+ if (next_length == 0) next_length = 1;
+ }
+ int current_length = i + char_length + next_length;
+ while (stream.HasMore()) {
+ current = stream.GetNext();
+ overflows |= ToUpperOverflows(current);
+ // NOTE: we use 0 as the next character here because, while
+ // the next character may affect what a character converts to,
+ // it does not in any case affect the length of what it convert
+ // to.
+ int char_length = mapping->get(current, 0, chars);
+ if (char_length == 0) char_length = 1;
+ current_length += char_length;
+ if (current_length > String::kMaxLength) {
+ AllowHeapAllocation allocate_error_and_return;
+ THROW_NEW_ERROR_RETURN_FAILURE(isolate,
+ NewInvalidStringLengthError());
+ }
+ }
+ // Try again with the real length. Return signed if we need
+ // to allocate a two-byte string for to uppercase.
+ return (overflows && !ignore_overflow) ? Smi::FromInt(-current_length)
+ : Smi::FromInt(current_length);
+ } else {
+ for (int j = 0; j < char_length; j++) {
+ result->Set(i, chars[j]);
+ i++;
+ }
+ has_changed_character = true;
+ }
+ current = next;
+ }
+ if (has_changed_character) {
+ return result;
+ } else {
+ // If we didn't actually change anything in doing the conversion
+ // we simple return the result and let the converted string
+ // become garbage; there is no reason to keep two identical strings
+ // alive.
+ return string;
+ }
+}
+
+
+static const uintptr_t kOneInEveryByte = kUintptrAllBitsSet / 0xFF;
+static const uintptr_t kAsciiMask = kOneInEveryByte << 7;
+
+// Given a word and two range boundaries returns a word with high bit
+// set in every byte iff the corresponding input byte was strictly in
+// the range (m, n). All the other bits in the result are cleared.
+// This function is only useful when it can be inlined and the
+// boundaries are statically known.
+// Requires: all bytes in the input word and the boundaries must be
+// ASCII (less than 0x7F).
+static inline uintptr_t AsciiRangeMask(uintptr_t w, char m, char n) {
+ // Use strict inequalities since in edge cases the function could be
+ // further simplified.
+ DCHECK(0 < m && m < n);
+ // Has high bit set in every w byte less than n.
+ uintptr_t tmp1 = kOneInEveryByte * (0x7F + n) - w;
+ // Has high bit set in every w byte greater than m.
+ uintptr_t tmp2 = w + kOneInEveryByte * (0x7F - m);
+ return (tmp1 & tmp2 & (kOneInEveryByte * 0x80));
+}
+
+
+#ifdef DEBUG
+static bool CheckFastAsciiConvert(char* dst, const char* src, int length,
+ bool changed, bool is_to_lower) {
+ bool expected_changed = false;
+ for (int i = 0; i < length; i++) {
+ if (dst[i] == src[i]) continue;
+ expected_changed = true;
+ if (is_to_lower) {
+ DCHECK('A' <= src[i] && src[i] <= 'Z');
+ DCHECK(dst[i] == src[i] + ('a' - 'A'));
+ } else {
+ DCHECK('a' <= src[i] && src[i] <= 'z');
+ DCHECK(dst[i] == src[i] - ('a' - 'A'));
+ }
+ }
+ return (expected_changed == changed);
+}
+#endif
+
+
+template <class Converter>
+static bool FastAsciiConvert(char* dst, const char* src, int length,
+ bool* changed_out) {
+#ifdef DEBUG
+ char* saved_dst = dst;
+ const char* saved_src = src;
+#endif
+ DisallowHeapAllocation no_gc;
+ // We rely on the distance between upper and lower case letters
+ // being a known power of 2.
+ DCHECK('a' - 'A' == (1 << 5));
+ // Boundaries for the range of input characters than require conversion.
+ static const char lo = Converter::kIsToLower ? 'A' - 1 : 'a' - 1;
+ static const char hi = Converter::kIsToLower ? 'Z' + 1 : 'z' + 1;
+ bool changed = false;
+ uintptr_t or_acc = 0;
+ const char* const limit = src + length;
+
+ // dst is newly allocated and always aligned.
+ DCHECK(IsAligned(reinterpret_cast<intptr_t>(dst), sizeof(uintptr_t)));
+ // Only attempt processing one word at a time if src is also aligned.
+ if (IsAligned(reinterpret_cast<intptr_t>(src), sizeof(uintptr_t))) {
+ // Process the prefix of the input that requires no conversion one aligned
+ // (machine) word at a time.
+ while (src <= limit - sizeof(uintptr_t)) {
+ const uintptr_t w = *reinterpret_cast<const uintptr_t*>(src);
+ or_acc |= w;
+ if (AsciiRangeMask(w, lo, hi) != 0) {
+ changed = true;
+ break;
+ }
+ *reinterpret_cast<uintptr_t*>(dst) = w;
+ src += sizeof(uintptr_t);
+ dst += sizeof(uintptr_t);
+ }
+ // Process the remainder of the input performing conversion when
+ // required one word at a time.
+ while (src <= limit - sizeof(uintptr_t)) {
+ const uintptr_t w = *reinterpret_cast<const uintptr_t*>(src);
+ or_acc |= w;
+ uintptr_t m = AsciiRangeMask(w, lo, hi);
+ // The mask has high (7th) bit set in every byte that needs
+ // conversion and we know that the distance between cases is
+ // 1 << 5.
+ *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2);
+ src += sizeof(uintptr_t);
+ dst += sizeof(uintptr_t);
+ }
+ }
+ // Process the last few bytes of the input (or the whole input if
+ // unaligned access is not supported).
+ while (src < limit) {
+ char c = *src;
+ or_acc |= c;
+ if (lo < c && c < hi) {
+ c ^= (1 << 5);
+ changed = true;
+ }
+ *dst = c;
+ ++src;
+ ++dst;
+ }
+
+ if ((or_acc & kAsciiMask) != 0) return false;
+
+ DCHECK(CheckFastAsciiConvert(saved_dst, saved_src, length, changed,
+ Converter::kIsToLower));
+
+ *changed_out = changed;
+ return true;
+}
+
+
+template <class Converter>
+MUST_USE_RESULT static Object* ConvertCase(
+ Handle<String> s, Isolate* isolate,
+ unibrow::Mapping<Converter, 128>* mapping) {
+ s = String::Flatten(s);
+ int length = s->length();
+ // Assume that the string is not empty; we need this assumption later
+ if (length == 0) return *s;
+
+ // Simpler handling of ASCII strings.
+ //
+ // NOTE: This assumes that the upper/lower case of an ASCII
+ // character is also ASCII. This is currently the case, but it
+ // might break in the future if we implement more context and locale
+ // dependent upper/lower conversions.
+ if (s->IsOneByteRepresentationUnderneath()) {
+ // Same length as input.
+ Handle<SeqOneByteString> result =
+ isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
+ DisallowHeapAllocation no_gc;
+ String::FlatContent flat_content = s->GetFlatContent();
+ DCHECK(flat_content.IsFlat());
+ bool has_changed_character = false;
+ bool is_ascii = FastAsciiConvert<Converter>(
+ reinterpret_cast<char*>(result->GetChars()),
+ reinterpret_cast<const char*>(flat_content.ToOneByteVector().start()),
+ length, &has_changed_character);
+ // If not ASCII, we discard the result and take the 2 byte path.
+ if (is_ascii) return has_changed_character ? *result : *s;
+ }
+
+ Handle<SeqString> result; // Same length as input.
+ if (s->IsOneByteRepresentation()) {
+ result = isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
+ } else {
+ result = isolate->factory()->NewRawTwoByteString(length).ToHandleChecked();
+ }
+
+ Object* answer = ConvertCaseHelper(isolate, *s, *result, length, mapping);
+ if (answer->IsException() || answer->IsString()) return answer;
+
+ DCHECK(answer->IsSmi());
+ length = Smi::cast(answer)->value();
+ if (s->IsOneByteRepresentation() && length > 0) {
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, result, isolate->factory()->NewRawOneByteString(length));
+ } else {
+ if (length < 0) length = -length;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, result, isolate->factory()->NewRawTwoByteString(length));
+ }
+ return ConvertCaseHelper(isolate, *s, *result, length, mapping);
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringToLowerCase) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 1);
+ CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
+ return ConvertCase(s, isolate, isolate->runtime_state()->to_lower_mapping());
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringToUpperCase) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 1);
+ CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
+ return ConvertCase(s, isolate, isolate->runtime_state()->to_upper_mapping());
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringTrim) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 3);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, string, 0);
+ CONVERT_BOOLEAN_ARG_CHECKED(trimLeft, 1);
+ CONVERT_BOOLEAN_ARG_CHECKED(trimRight, 2);
+
+ string = String::Flatten(string);
+ int length = string->length();
+
+ int left = 0;
+ UnicodeCache* unicode_cache = isolate->unicode_cache();
+ if (trimLeft) {
+ while (left < length &&
+ unicode_cache->IsWhiteSpaceOrLineTerminator(string->Get(left))) {
+ left++;
+ }
+ }
+
+ int right = length;
+ if (trimRight) {
+ while (
+ right > left &&
+ unicode_cache->IsWhiteSpaceOrLineTerminator(string->Get(right - 1))) {
+ right--;
+ }
+ }
+
+ return *isolate->factory()->NewSubString(string, left, right);
+}
+
+
+RUNTIME_FUNCTION(Runtime_TruncateString) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 2);
+ CONVERT_ARG_HANDLE_CHECKED(SeqString, string, 0);
+ CONVERT_INT32_ARG_CHECKED(new_length, 1);
+ RUNTIME_ASSERT(new_length >= 0);
+ return *SeqString::Truncate(string, new_length);
+}
+
+
+RUNTIME_FUNCTION(Runtime_NewString) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 2);
+ CONVERT_INT32_ARG_CHECKED(length, 0);
+ CONVERT_BOOLEAN_ARG_CHECKED(is_one_byte, 1);
+ if (length == 0) return isolate->heap()->empty_string();
+ Handle<String> result;
+ if (is_one_byte) {
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, result, isolate->factory()->NewRawOneByteString(length));
+ } else {
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, result, isolate->factory()->NewRawTwoByteString(length));
+ }
+ return *result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_StringEquals) {
+ HandleScope handle_scope(isolate);
+ DCHECK(args.length() == 2);
+
+ CONVERT_ARG_HANDLE_CHECKED(String, x, 0);
+ CONVERT_ARG_HANDLE_CHECKED(String, y, 1);
+
+ bool not_equal = !String::Equals(x, y);
+ // This is slightly convoluted because the value that signifies
+ // equality is 0 and inequality is 1 so we have to negate the result
+ // from String::Equals.
+ DCHECK(not_equal == 0 || not_equal == 1);
+ STATIC_ASSERT(EQUAL == 0);
+ STATIC_ASSERT(NOT_EQUAL == 1);
+ return Smi::FromInt(not_equal);
+}
+
+
+RUNTIME_FUNCTION(RuntimeReference_StringCharFromCode) {
+ SealHandleScope shs(isolate);
+ return __RT_impl_Runtime_CharFromCode(args, isolate);
+}
+
+
+RUNTIME_FUNCTION(RuntimeReference_StringCharAt) {
+ SealHandleScope shs(isolate);
+ DCHECK(args.length() == 2);
+ if (!args[0]->IsString()) return Smi::FromInt(0);
+ if (!args[1]->IsNumber()) return Smi::FromInt(0);
+ if (std::isinf(args.number_at(1))) return isolate->heap()->empty_string();
+ Object* code = __RT_impl_Runtime_StringCharCodeAtRT(args, isolate);
+ if (code->IsNaN()) return isolate->heap()->empty_string();
+ return __RT_impl_Runtime_CharFromCode(Arguments(1, &code), isolate);
+}
+
+
+RUNTIME_FUNCTION(RuntimeReference_OneByteSeqStringSetChar) {
+ SealHandleScope shs(isolate);
+ DCHECK(args.length() == 3);
+ CONVERT_INT32_ARG_CHECKED(index, 0);
+ CONVERT_INT32_ARG_CHECKED(value, 1);
+ CONVERT_ARG_CHECKED(SeqOneByteString, string, 2);
+ string->SeqOneByteStringSet(index, value);
+ return string;
+}
+
+
+RUNTIME_FUNCTION(RuntimeReference_TwoByteSeqStringSetChar) {
+ SealHandleScope shs(isolate);
+ DCHECK(args.length() == 3);
+ CONVERT_INT32_ARG_CHECKED(index, 0);
+ CONVERT_INT32_ARG_CHECKED(value, 1);
+ CONVERT_ARG_CHECKED(SeqTwoByteString, string, 2);
+ string->SeqTwoByteStringSet(index, value);
+ return string;
+}
+
+
+RUNTIME_FUNCTION(RuntimeReference_StringCompare) {
+ SealHandleScope shs(isolate);
+ return __RT_impl_Runtime_StringCompare(args, isolate);
+}
+
+
+RUNTIME_FUNCTION(RuntimeReference_StringCharCodeAt) {
+ SealHandleScope shs(isolate);
+ DCHECK(args.length() == 2);
+ if (!args[0]->IsString()) return isolate->heap()->undefined_value();
+ if (!args[1]->IsNumber()) return isolate->heap()->undefined_value();
+ if (std::isinf(args.number_at(1))) return isolate->heap()->nan_value();
+ return __RT_impl_Runtime_StringCharCodeAtRT(args, isolate);
+}
+
+
+RUNTIME_FUNCTION(RuntimeReference_SubString) {
+ SealHandleScope shs(isolate);
+ return __RT_impl_Runtime_SubString(args, isolate);
+}
+
+
+RUNTIME_FUNCTION(RuntimeReference_StringAdd) {
+ SealHandleScope shs(isolate);
+ return __RT_impl_Runtime_StringAdd(args, isolate);
+}
+}
+} // namespace v8::internal
-// Copyright 2013 the V8 project authors. All rights reserved.
+// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef V8_URI_H_
-#define V8_URI_H_
-
#include "src/v8.h"
+#include "src/arguments.h"
#include "src/conversions.h"
+#include "src/runtime/runtime.h"
+#include "src/runtime/runtime-utils.h"
#include "src/string-search.h"
#include "src/utils.h"
+
namespace v8 {
namespace internal {
-
template <typename Char>
static INLINE(Vector<const Char> GetCharVector(Handle<String> string));
class URIUnescape : public AllStatic {
public:
- template<typename Char>
+ template <typename Char>
MUST_USE_RESULT static MaybeHandle<String> Unescape(Isolate* isolate,
Handle<String> source);
private:
static const signed char kHexValue['g'];
- template<typename Char>
- MUST_USE_RESULT static MaybeHandle<String> UnescapeSlow(
- Isolate* isolate, Handle<String> string, int start_index);
+ template <typename Char>
+ MUST_USE_RESULT static MaybeHandle<String> UnescapeSlow(Isolate* isolate,
+ Handle<String> string,
+ int start_index);
static INLINE(int TwoDigitHex(uint16_t character1, uint16_t character2));
template <typename Char>
- static INLINE(int UnescapeChar(Vector<const Char> vector,
- int i,
- int length,
+ static INLINE(int UnescapeChar(Vector<const Char> vector, int i, int length,
int* step));
};
const signed char URIUnescape::kHexValue[] = {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
- -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, 10, 11, 12, 13, 14, 15 };
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -0, 1, 2, 3, 4, 5,
+ 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15};
-template<typename Char>
+template <typename Char>
MaybeHandle<String> URIUnescape::Unescape(Isolate* isolate,
Handle<String> source) {
int index;
- { DisallowHeapAllocation no_allocation;
+ {
+ DisallowHeapAllocation no_allocation;
StringSearch<uint8_t, Char> search(isolate, STATIC_CHAR_VECTOR("%"));
index = search.Search(GetCharVector<Char>(source), 0);
if (index < 0) return source;
template <typename Char>
-MaybeHandle<String> URIUnescape::UnescapeSlow(
- Isolate* isolate, Handle<String> string, int start_index) {
+MaybeHandle<String> URIUnescape::UnescapeSlow(Isolate* isolate,
+ Handle<String> string,
+ int start_index) {
bool one_byte = true;
int length = string->length();
int unescaped_length = 0;
- { DisallowHeapAllocation no_allocation;
+ {
+ DisallowHeapAllocation no_allocation;
Vector<const Char> vector = GetCharVector<Char>(string);
for (int i = start_index; i < length; unescaped_length++) {
int step;
if (UnescapeChar(vector, i, length, &step) >
- String::kMaxOneByteCharCode) {
+ String::kMaxOneByteCharCode) {
one_byte = false;
}
i += step;
Handle<String> second_part;
DCHECK(unescaped_length <= String::kMaxLength);
if (one_byte) {
- Handle<SeqOneByteString> dest = isolate->factory()->NewRawOneByteString(
- unescaped_length).ToHandleChecked();
+ Handle<SeqOneByteString> dest = isolate->factory()
+ ->NewRawOneByteString(unescaped_length)
+ .ToHandleChecked();
DisallowHeapAllocation no_allocation;
Vector<const Char> vector = GetCharVector<Char>(string);
for (int i = start_index; i < length; dest_position++) {
}
second_part = dest;
} else {
- Handle<SeqTwoByteString> dest = isolate->factory()->NewRawTwoByteString(
- unescaped_length).ToHandleChecked();
+ Handle<SeqTwoByteString> dest = isolate->factory()
+ ->NewRawTwoByteString(unescaped_length)
+ .ToHandleChecked();
DisallowHeapAllocation no_allocation;
Vector<const Char> vector = GetCharVector<Char>(string);
for (int i = start_index; i < length; dest_position++) {
template <typename Char>
-int URIUnescape::UnescapeChar(Vector<const Char> vector,
- int i,
- int length,
+int URIUnescape::UnescapeChar(Vector<const Char> vector, int i, int length,
int* step) {
uint16_t character = vector[i];
int32_t hi = 0;
int32_t lo = 0;
- if (character == '%' &&
- i <= length - 6 &&
- vector[i + 1] == 'u' &&
- (hi = TwoDigitHex(vector[i + 2],
- vector[i + 3])) != -1 &&
- (lo = TwoDigitHex(vector[i + 4],
- vector[i + 5])) != -1) {
+ if (character == '%' && i <= length - 6 && vector[i + 1] == 'u' &&
+ (hi = TwoDigitHex(vector[i + 2], vector[i + 3])) != -1 &&
+ (lo = TwoDigitHex(vector[i + 4], vector[i + 5])) != -1) {
*step = 6;
return (hi << 8) + lo;
- } else if (character == '%' &&
- i <= length - 3 &&
- (lo = TwoDigitHex(vector[i + 1],
- vector[i + 2])) != -1) {
+ } else if (character == '%' && i <= length - 3 &&
+ (lo = TwoDigitHex(vector[i + 1], vector[i + 2])) != -1) {
*step = 3;
return lo;
} else {
class URIEscape : public AllStatic {
public:
- template<typename Char>
+ template <typename Char>
MUST_USE_RESULT static MaybeHandle<String> Escape(Isolate* isolate,
Handle<String> string);
// }
const char URIEscape::kNotEscaped[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
- 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-
-
-template<typename Char>
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
+ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+
+
+template <typename Char>
MaybeHandle<String> URIEscape::Escape(Isolate* isolate, Handle<String> string) {
DCHECK(string->IsFlat());
int escaped_length = 0;
int length = string->length();
- { DisallowHeapAllocation no_allocation;
+ {
+ DisallowHeapAllocation no_allocation;
Vector<const Char> vector = GetCharVector<Char>(string);
for (int i = 0; i < length; i++) {
uint16_t c = vector[i];
}
// We don't allow strings that are longer than a maximal length.
- DCHECK(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow.
+ DCHECK(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow.
if (escaped_length > String::kMaxLength) break; // Provoke exception.
}
}
Handle<SeqOneByteString> dest;
ASSIGN_RETURN_ON_EXCEPTION(
- isolate, dest,
- isolate->factory()->NewRawOneByteString(escaped_length),
+ isolate, dest, isolate->factory()->NewRawOneByteString(escaped_length),
String);
int dest_position = 0;
- { DisallowHeapAllocation no_allocation;
+ {
+ DisallowHeapAllocation no_allocation;
Vector<const Char> vector = GetCharVector<Char>(string);
for (int i = 0; i < length; i++) {
uint16_t c = vector[i];
if (c >= 256) {
dest->SeqOneByteStringSet(dest_position, '%');
- dest->SeqOneByteStringSet(dest_position+1, 'u');
- dest->SeqOneByteStringSet(dest_position+2, kHexChars[c >> 12]);
- dest->SeqOneByteStringSet(dest_position+3, kHexChars[(c >> 8) & 0xf]);
- dest->SeqOneByteStringSet(dest_position+4, kHexChars[(c >> 4) & 0xf]);
- dest->SeqOneByteStringSet(dest_position+5, kHexChars[c & 0xf]);
+ dest->SeqOneByteStringSet(dest_position + 1, 'u');
+ dest->SeqOneByteStringSet(dest_position + 2, kHexChars[c >> 12]);
+ dest->SeqOneByteStringSet(dest_position + 3, kHexChars[(c >> 8) & 0xf]);
+ dest->SeqOneByteStringSet(dest_position + 4, kHexChars[(c >> 4) & 0xf]);
+ dest->SeqOneByteStringSet(dest_position + 5, kHexChars[c & 0xf]);
dest_position += 6;
} else if (IsNotEscaped(c)) {
dest->SeqOneByteStringSet(dest_position, c);
dest_position++;
} else {
dest->SeqOneByteStringSet(dest_position, '%');
- dest->SeqOneByteStringSet(dest_position+1, kHexChars[c >> 4]);
- dest->SeqOneByteStringSet(dest_position+2, kHexChars[c & 0xf]);
+ dest->SeqOneByteStringSet(dest_position + 1, kHexChars[c >> 4]);
+ dest->SeqOneByteStringSet(dest_position + 2, kHexChars[c & 0xf]);
dest_position += 3;
}
}
return dest;
}
-} } // namespace v8::internal
-#endif // V8_URI_H_
+RUNTIME_FUNCTION(Runtime_URIEscape) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 1);
+ CONVERT_ARG_HANDLE_CHECKED(String, source, 0);
+ Handle<String> string = String::Flatten(source);
+ DCHECK(string->IsFlat());
+ Handle<String> result;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, result, string->IsOneByteRepresentationUnderneath()
+ ? URIEscape::Escape<uint8_t>(isolate, source)
+ : URIEscape::Escape<uc16>(isolate, source));
+ return *result;
+}
+
+
+RUNTIME_FUNCTION(Runtime_URIUnescape) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 1);
+ CONVERT_ARG_HANDLE_CHECKED(String, source, 0);
+ Handle<String> string = String::Flatten(source);
+ DCHECK(string->IsFlat());
+ Handle<String> result;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, result, string->IsOneByteRepresentationUnderneath()
+ ? URIUnescape::Unescape<uint8_t>(isolate, source)
+ : URIUnescape::Unescape<uc16>(isolate, source));
+ return *result;
+}
+}
+} // namespace v8::internal
#include "src/full-codegen.h"
#include "src/global-handles.h"
#include "src/isolate-inl.h"
-#include "src/json-parser.h"
-#include "src/json-stringifier.h"
-#include "src/jsregexp-inl.h"
-#include "src/jsregexp.h"
#include "src/liveedit.h"
#include "src/misc-intrinsics.h"
#include "src/parser.h"
#include "src/runtime-profiler.h"
#include "src/scopeinfo.h"
#include "src/smart-pointers.h"
-#include "src/string-search.h"
-#include "src/uri.h"
#include "src/utils.h"
#include "src/v8threads.h"
#include "src/vm-state-inl.h"
}
-RUNTIME_FUNCTION(Runtime_RegExpCompile) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 3);
- CONVERT_ARG_HANDLE_CHECKED(JSRegExp, re, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, pattern, 1);
- CONVERT_ARG_HANDLE_CHECKED(String, flags, 2);
- Handle<Object> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
- RegExpImpl::Compile(re, pattern, flags));
- return *result;
-}
-
-
RUNTIME_FUNCTION(Runtime_CreateApiFunction) {
HandleScope scope(isolate);
DCHECK(args.length() == 2);
}
-RUNTIME_FUNCTION(Runtime_RegExpExecRT) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 4);
- CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, subject, 1);
- CONVERT_INT32_ARG_CHECKED(index, 2);
- CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 3);
- // Due to the way the JS calls are constructed this must be less than the
- // length of a string, i.e. it is always a Smi. We check anyway for security.
- RUNTIME_ASSERT(index >= 0);
- RUNTIME_ASSERT(index <= subject->length());
- isolate->counters()->regexp_entry_runtime()->Increment();
- Handle<Object> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, result,
- RegExpImpl::Exec(regexp, subject, index, last_match_info));
- return *result;
-}
-
-
-RUNTIME_FUNCTION(Runtime_RegExpConstructResult) {
- HandleScope handle_scope(isolate);
- DCHECK(args.length() == 3);
- CONVERT_SMI_ARG_CHECKED(size, 0);
- RUNTIME_ASSERT(size >= 0 && size <= FixedArray::kMaxLength);
- CONVERT_ARG_HANDLE_CHECKED(Object, index, 1);
- CONVERT_ARG_HANDLE_CHECKED(Object, input, 2);
- Handle<FixedArray> elements = isolate->factory()->NewFixedArray(size);
- Handle<Map> regexp_map(isolate->native_context()->regexp_result_map());
- Handle<JSObject> object =
- isolate->factory()->NewJSObjectFromMap(regexp_map, NOT_TENURED, false);
- Handle<JSArray> array = Handle<JSArray>::cast(object);
- array->set_elements(*elements);
- array->set_length(Smi::FromInt(size));
- // Write in-object properties after the length of the array.
- array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, *index);
- array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, *input);
- return *array;
-}
-
-
-RUNTIME_FUNCTION(Runtime_RegExpInitializeObject) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 6);
- CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, source, 1);
- // If source is the empty string we set it to "(?:)" instead as
- // suggested by ECMA-262, 5th, section 15.10.4.1.
- if (source->length() == 0) source = isolate->factory()->query_colon_string();
-
- CONVERT_ARG_HANDLE_CHECKED(Object, global, 2);
- if (!global->IsTrue()) global = isolate->factory()->false_value();
-
- CONVERT_ARG_HANDLE_CHECKED(Object, ignoreCase, 3);
- if (!ignoreCase->IsTrue()) ignoreCase = isolate->factory()->false_value();
-
- CONVERT_ARG_HANDLE_CHECKED(Object, multiline, 4);
- if (!multiline->IsTrue()) multiline = isolate->factory()->false_value();
-
- CONVERT_ARG_HANDLE_CHECKED(Object, sticky, 5);
- if (!sticky->IsTrue()) sticky = isolate->factory()->false_value();
-
- Map* map = regexp->map();
- Object* constructor = map->constructor();
- if (!FLAG_harmony_regexps && constructor->IsJSFunction() &&
- JSFunction::cast(constructor)->initial_map() == map) {
- // If we still have the original map, set in-object properties directly.
- regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, *source);
- // Both true and false are immovable immortal objects so no need for write
- // barrier.
- regexp->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, *global,
- SKIP_WRITE_BARRIER);
- regexp->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, *ignoreCase,
- SKIP_WRITE_BARRIER);
- regexp->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, *multiline,
- SKIP_WRITE_BARRIER);
- regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
- Smi::FromInt(0), SKIP_WRITE_BARRIER);
- return *regexp;
- }
-
- // Map has changed, so use generic, but slower, method. We also end here if
- // the --harmony-regexp flag is set, because the initial map does not have
- // space for the 'sticky' flag, since it is from the snapshot, but must work
- // both with and without --harmony-regexp. When sticky comes out from under
- // the flag, we will be able to use the fast initial map.
- PropertyAttributes final =
- static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE);
- PropertyAttributes writable =
- static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
- Handle<Object> zero(Smi::FromInt(0), isolate);
- Factory* factory = isolate->factory();
- JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->source_string(),
- source, final).Check();
- JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->global_string(),
- global, final).Check();
- JSObject::SetOwnPropertyIgnoreAttributes(
- regexp, factory->ignore_case_string(), ignoreCase, final).Check();
- JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->multiline_string(),
- multiline, final).Check();
- if (FLAG_harmony_regexps) {
- JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->sticky_string(),
- sticky, final).Check();
- }
- JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->last_index_string(),
- zero, writable).Check();
- return *regexp;
-}
-
-
RUNTIME_FUNCTION(Runtime_FinishArrayPrototypeSetup) {
HandleScope scope(isolate);
DCHECK(args.length() == 1);
}
-RUNTIME_FUNCTION(Runtime_MaterializeRegExpLiteral) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 4);
- CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0);
- CONVERT_SMI_ARG_CHECKED(index, 1);
- CONVERT_ARG_HANDLE_CHECKED(String, pattern, 2);
- CONVERT_ARG_HANDLE_CHECKED(String, flags, 3);
-
- // Get the RegExp function from the context in the literals array.
- // This is the RegExp function from the context in which the
- // function was created. We do not use the RegExp function from the
- // current native context because this might be the RegExp function
- // from another context which we should not have access to.
- Handle<JSFunction> constructor = Handle<JSFunction>(
- JSFunction::NativeContextFromLiterals(*literals)->regexp_function());
- // Compute the regular expression literal.
- Handle<Object> regexp;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, regexp,
- RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags));
- literals->set(index, *regexp);
- return *regexp;
-}
-
-
RUNTIME_FUNCTION(Runtime_FunctionGetName) {
SealHandleScope shs(isolate);
DCHECK(args.length() == 1);
generator_object->set_continuation(JSGeneratorObject::kGeneratorExecuting);
FixedArray* operand_stack = generator_object->operand_stack();
- int operands_count = operand_stack->length();
- if (operands_count != 0) {
- frame->RestoreOperandStack(operand_stack,
- generator_object->stack_handler_index());
- generator_object->set_operand_stack(isolate->heap()->empty_fixed_array());
- generator_object->set_stack_handler_index(-1);
- }
-
- JSGeneratorObject::ResumeMode resume_mode =
- static_cast<JSGeneratorObject::ResumeMode>(resume_mode_int);
- switch (resume_mode) {
- case JSGeneratorObject::NEXT:
- return value;
- case JSGeneratorObject::THROW:
- return isolate->Throw(value);
- }
-
- UNREACHABLE();
- return isolate->ThrowIllegalOperation();
-}
-
-
-RUNTIME_FUNCTION(Runtime_ThrowGeneratorStateError) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 1);
- CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
- int continuation = generator->continuation();
- const char* message = continuation == JSGeneratorObject::kGeneratorClosed
- ? "generator_finished"
- : "generator_running";
- Vector<Handle<Object> > argv = HandleVector<Object>(NULL, 0);
- THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewError(message, argv));
-}
-
-
-RUNTIME_FUNCTION(Runtime_ObjectFreeze) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 1);
- CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
-
- // %ObjectFreeze is a fast path and these cases are handled elsewhere.
- RUNTIME_ASSERT(!object->HasSloppyArgumentsElements() &&
- !object->map()->is_observed() && !object->IsJSProxy());
-
- Handle<Object> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, JSObject::Freeze(object));
- return *result;
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringCharCodeAtRT) {
- HandleScope handle_scope(isolate);
- DCHECK(args.length() == 2);
-
- CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
- CONVERT_NUMBER_CHECKED(uint32_t, i, Uint32, args[1]);
-
- // Flatten the string. If someone wants to get a char at an index
- // in a cons string, it is likely that more indices will be
- // accessed.
- subject = String::Flatten(subject);
-
- if (i >= static_cast<uint32_t>(subject->length())) {
- return isolate->heap()->nan_value();
- }
-
- return Smi::FromInt(subject->Get(i));
-}
-
-
-RUNTIME_FUNCTION(Runtime_CharFromCode) {
- HandleScope handlescope(isolate);
- DCHECK(args.length() == 1);
- if (args[0]->IsNumber()) {
- CONVERT_NUMBER_CHECKED(uint32_t, code, Uint32, args[0]);
- code &= 0xffff;
- return *isolate->factory()->LookupSingleCharacterStringFromCode(code);
- }
- return isolate->heap()->empty_string();
-}
-
-
-class FixedArrayBuilder {
- public:
- explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity)
- : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)),
- length_(0),
- has_non_smi_elements_(false) {
- // Require a non-zero initial size. Ensures that doubling the size to
- // extend the array will work.
- DCHECK(initial_capacity > 0);
- }
-
- explicit FixedArrayBuilder(Handle<FixedArray> backing_store)
- : array_(backing_store), length_(0), has_non_smi_elements_(false) {
- // Require a non-zero initial size. Ensures that doubling the size to
- // extend the array will work.
- DCHECK(backing_store->length() > 0);
- }
-
- bool HasCapacity(int elements) {
- int length = array_->length();
- int required_length = length_ + elements;
- return (length >= required_length);
- }
-
- void EnsureCapacity(int elements) {
- int length = array_->length();
- int required_length = length_ + elements;
- if (length < required_length) {
- int new_length = length;
- do {
- new_length *= 2;
- } while (new_length < required_length);
- Handle<FixedArray> extended_array =
- array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length);
- array_->CopyTo(0, *extended_array, 0, length_);
- array_ = extended_array;
- }
- }
-
- void Add(Object* value) {
- DCHECK(!value->IsSmi());
- DCHECK(length_ < capacity());
- array_->set(length_, value);
- length_++;
- has_non_smi_elements_ = true;
- }
-
- void Add(Smi* value) {
- DCHECK(value->IsSmi());
- DCHECK(length_ < capacity());
- array_->set(length_, value);
- length_++;
- }
-
- Handle<FixedArray> array() { return array_; }
-
- int length() { return length_; }
-
- int capacity() { return array_->length(); }
-
- Handle<JSArray> ToJSArray(Handle<JSArray> target_array) {
- JSArray::SetContent(target_array, array_);
- target_array->set_length(Smi::FromInt(length_));
- return target_array;
- }
-
-
- private:
- Handle<FixedArray> array_;
- int length_;
- bool has_non_smi_elements_;
-};
-
-
-// Forward declarations.
-const int kStringBuilderConcatHelperLengthBits = 11;
-const int kStringBuilderConcatHelperPositionBits = 19;
-
-template <typename schar>
-static inline void StringBuilderConcatHelper(String*, schar*, FixedArray*, int);
-
-typedef BitField<int, 0, kStringBuilderConcatHelperLengthBits>
- StringBuilderSubstringLength;
-typedef BitField<int, kStringBuilderConcatHelperLengthBits,
- kStringBuilderConcatHelperPositionBits>
- StringBuilderSubstringPosition;
-
-
-class ReplacementStringBuilder {
- public:
- ReplacementStringBuilder(Heap* heap, Handle<String> subject,
- int estimated_part_count)
- : heap_(heap),
- array_builder_(heap->isolate(), estimated_part_count),
- subject_(subject),
- character_count_(0),
- is_one_byte_(subject->IsOneByteRepresentation()) {
- // Require a non-zero initial size. Ensures that doubling the size to
- // extend the array will work.
- DCHECK(estimated_part_count > 0);
- }
-
- static inline void AddSubjectSlice(FixedArrayBuilder* builder, int from,
- int to) {
- DCHECK(from >= 0);
- int length = to - from;
- DCHECK(length > 0);
- if (StringBuilderSubstringLength::is_valid(length) &&
- StringBuilderSubstringPosition::is_valid(from)) {
- int encoded_slice = StringBuilderSubstringLength::encode(length) |
- StringBuilderSubstringPosition::encode(from);
- builder->Add(Smi::FromInt(encoded_slice));
- } else {
- // Otherwise encode as two smis.
- builder->Add(Smi::FromInt(-length));
- builder->Add(Smi::FromInt(from));
- }
- }
-
-
- void EnsureCapacity(int elements) { array_builder_.EnsureCapacity(elements); }
-
-
- void AddSubjectSlice(int from, int to) {
- AddSubjectSlice(&array_builder_, from, to);
- IncrementCharacterCount(to - from);
- }
-
-
- void AddString(Handle<String> string) {
- int length = string->length();
- DCHECK(length > 0);
- AddElement(*string);
- if (!string->IsOneByteRepresentation()) {
- is_one_byte_ = false;
- }
- IncrementCharacterCount(length);
- }
-
-
- MaybeHandle<String> ToString() {
- Isolate* isolate = heap_->isolate();
- if (array_builder_.length() == 0) {
- return isolate->factory()->empty_string();
- }
-
- Handle<String> joined_string;
- if (is_one_byte_) {
- Handle<SeqOneByteString> seq;
- ASSIGN_RETURN_ON_EXCEPTION(
- isolate, seq,
- isolate->factory()->NewRawOneByteString(character_count_), String);
-
- DisallowHeapAllocation no_gc;
- uint8_t* char_buffer = seq->GetChars();
- StringBuilderConcatHelper(*subject_, char_buffer, *array_builder_.array(),
- array_builder_.length());
- joined_string = Handle<String>::cast(seq);
- } else {
- // Two-byte.
- Handle<SeqTwoByteString> seq;
- ASSIGN_RETURN_ON_EXCEPTION(
- isolate, seq,
- isolate->factory()->NewRawTwoByteString(character_count_), String);
-
- DisallowHeapAllocation no_gc;
- uc16* char_buffer = seq->GetChars();
- StringBuilderConcatHelper(*subject_, char_buffer, *array_builder_.array(),
- array_builder_.length());
- joined_string = Handle<String>::cast(seq);
- }
- return joined_string;
- }
-
-
- void IncrementCharacterCount(int by) {
- if (character_count_ > String::kMaxLength - by) {
- STATIC_ASSERT(String::kMaxLength < kMaxInt);
- character_count_ = kMaxInt;
- } else {
- character_count_ += by;
- }
- }
-
- private:
- void AddElement(Object* element) {
- DCHECK(element->IsSmi() || element->IsString());
- DCHECK(array_builder_.capacity() > array_builder_.length());
- array_builder_.Add(element);
- }
-
- Heap* heap_;
- FixedArrayBuilder array_builder_;
- Handle<String> subject_;
- int character_count_;
- bool is_one_byte_;
-};
-
-
-class CompiledReplacement {
- public:
- explicit CompiledReplacement(Zone* zone)
- : parts_(1, zone), replacement_substrings_(0, zone), zone_(zone) {}
-
- // Return whether the replacement is simple.
- bool Compile(Handle<String> replacement, int capture_count,
- int subject_length);
-
- // Use Apply only if Compile returned false.
- void Apply(ReplacementStringBuilder* builder, int match_from, int match_to,
- int32_t* match);
-
- // Number of distinct parts of the replacement pattern.
- int parts() { return parts_.length(); }
-
- Zone* zone() const { return zone_; }
-
- private:
- enum PartType {
- SUBJECT_PREFIX = 1,
- SUBJECT_SUFFIX,
- SUBJECT_CAPTURE,
- REPLACEMENT_SUBSTRING,
- REPLACEMENT_STRING,
- NUMBER_OF_PART_TYPES
- };
-
- struct ReplacementPart {
- static inline ReplacementPart SubjectMatch() {
- return ReplacementPart(SUBJECT_CAPTURE, 0);
- }
- static inline ReplacementPart SubjectCapture(int capture_index) {
- return ReplacementPart(SUBJECT_CAPTURE, capture_index);
- }
- static inline ReplacementPart SubjectPrefix() {
- return ReplacementPart(SUBJECT_PREFIX, 0);
- }
- static inline ReplacementPart SubjectSuffix(int subject_length) {
- return ReplacementPart(SUBJECT_SUFFIX, subject_length);
- }
- static inline ReplacementPart ReplacementString() {
- return ReplacementPart(REPLACEMENT_STRING, 0);
- }
- static inline ReplacementPart ReplacementSubString(int from, int to) {
- DCHECK(from >= 0);
- DCHECK(to > from);
- return ReplacementPart(-from, to);
- }
-
- // If tag <= 0 then it is the negation of a start index of a substring of
- // the replacement pattern, otherwise it's a value from PartType.
- ReplacementPart(int tag, int data) : tag(tag), data(data) {
- // Must be non-positive or a PartType value.
- DCHECK(tag < NUMBER_OF_PART_TYPES);
- }
- // Either a value of PartType or a non-positive number that is
- // the negation of an index into the replacement string.
- int tag;
- // The data value's interpretation depends on the value of tag:
- // tag == SUBJECT_PREFIX ||
- // tag == SUBJECT_SUFFIX: data is unused.
- // tag == SUBJECT_CAPTURE: data is the number of the capture.
- // tag == REPLACEMENT_SUBSTRING ||
- // tag == REPLACEMENT_STRING: data is index into array of substrings
- // of the replacement string.
- // tag <= 0: Temporary representation of the substring of the replacement
- // string ranging over -tag .. data.
- // Is replaced by REPLACEMENT_{SUB,}STRING when we create the
- // substring objects.
- int data;
- };
-
- template <typename Char>
- bool ParseReplacementPattern(ZoneList<ReplacementPart>* parts,
- Vector<Char> characters, int capture_count,
- int subject_length, Zone* zone) {
- int length = characters.length();
- int last = 0;
- for (int i = 0; i < length; i++) {
- Char c = characters[i];
- if (c == '$') {
- int next_index = i + 1;
- if (next_index == length) { // No next character!
- break;
- }
- Char c2 = characters[next_index];
- switch (c2) {
- case '$':
- if (i > last) {
- // There is a substring before. Include the first "$".
- parts->Add(
- ReplacementPart::ReplacementSubString(last, next_index),
- zone);
- last = next_index + 1; // Continue after the second "$".
- } else {
- // Let the next substring start with the second "$".
- last = next_index;
- }
- i = next_index;
- break;
- case '`':
- if (i > last) {
- parts->Add(ReplacementPart::ReplacementSubString(last, i), zone);
- }
- parts->Add(ReplacementPart::SubjectPrefix(), zone);
- i = next_index;
- last = i + 1;
- break;
- case '\'':
- if (i > last) {
- parts->Add(ReplacementPart::ReplacementSubString(last, i), zone);
- }
- parts->Add(ReplacementPart::SubjectSuffix(subject_length), zone);
- i = next_index;
- last = i + 1;
- break;
- case '&':
- if (i > last) {
- parts->Add(ReplacementPart::ReplacementSubString(last, i), zone);
- }
- parts->Add(ReplacementPart::SubjectMatch(), zone);
- i = next_index;
- last = i + 1;
- break;
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9': {
- int capture_ref = c2 - '0';
- if (capture_ref > capture_count) {
- i = next_index;
- continue;
- }
- int second_digit_index = next_index + 1;
- if (second_digit_index < length) {
- // Peek ahead to see if we have two digits.
- Char c3 = characters[second_digit_index];
- if ('0' <= c3 && c3 <= '9') { // Double digits.
- int double_digit_ref = capture_ref * 10 + c3 - '0';
- if (double_digit_ref <= capture_count) {
- next_index = second_digit_index;
- capture_ref = double_digit_ref;
- }
- }
- }
- if (capture_ref > 0) {
- if (i > last) {
- parts->Add(ReplacementPart::ReplacementSubString(last, i),
- zone);
- }
- DCHECK(capture_ref <= capture_count);
- parts->Add(ReplacementPart::SubjectCapture(capture_ref), zone);
- last = next_index + 1;
- }
- i = next_index;
- break;
- }
- default:
- i = next_index;
- break;
- }
- }
- }
- if (length > last) {
- if (last == 0) {
- // Replacement is simple. Do not use Apply to do the replacement.
- return true;
- } else {
- parts->Add(ReplacementPart::ReplacementSubString(last, length), zone);
- }
- }
- return false;
- }
-
- ZoneList<ReplacementPart> parts_;
- ZoneList<Handle<String> > replacement_substrings_;
- Zone* zone_;
-};
-
-
-bool CompiledReplacement::Compile(Handle<String> replacement, int capture_count,
- int subject_length) {
- {
- DisallowHeapAllocation no_gc;
- String::FlatContent content = replacement->GetFlatContent();
- DCHECK(content.IsFlat());
- bool simple = false;
- if (content.IsOneByte()) {
- simple = ParseReplacementPattern(&parts_, content.ToOneByteVector(),
- capture_count, subject_length, zone());
- } else {
- DCHECK(content.IsTwoByte());
- simple = ParseReplacementPattern(&parts_, content.ToUC16Vector(),
- capture_count, subject_length, zone());
- }
- if (simple) return true;
- }
-
- Isolate* isolate = replacement->GetIsolate();
- // Find substrings of replacement string and create them as String objects.
- int substring_index = 0;
- for (int i = 0, n = parts_.length(); i < n; i++) {
- int tag = parts_[i].tag;
- if (tag <= 0) { // A replacement string slice.
- int from = -tag;
- int to = parts_[i].data;
- replacement_substrings_.Add(
- isolate->factory()->NewSubString(replacement, from, to), zone());
- parts_[i].tag = REPLACEMENT_SUBSTRING;
- parts_[i].data = substring_index;
- substring_index++;
- } else if (tag == REPLACEMENT_STRING) {
- replacement_substrings_.Add(replacement, zone());
- parts_[i].data = substring_index;
- substring_index++;
- }
- }
- return false;
-}
-
-
-void CompiledReplacement::Apply(ReplacementStringBuilder* builder,
- int match_from, int match_to, int32_t* match) {
- DCHECK_LT(0, parts_.length());
- for (int i = 0, n = parts_.length(); i < n; i++) {
- ReplacementPart part = parts_[i];
- switch (part.tag) {
- case SUBJECT_PREFIX:
- if (match_from > 0) builder->AddSubjectSlice(0, match_from);
- break;
- case SUBJECT_SUFFIX: {
- int subject_length = part.data;
- if (match_to < subject_length) {
- builder->AddSubjectSlice(match_to, subject_length);
- }
- break;
- }
- case SUBJECT_CAPTURE: {
- int capture = part.data;
- int from = match[capture * 2];
- int to = match[capture * 2 + 1];
- if (from >= 0 && to > from) {
- builder->AddSubjectSlice(from, to);
- }
- break;
- }
- case REPLACEMENT_SUBSTRING:
- case REPLACEMENT_STRING:
- builder->AddString(replacement_substrings_[part.data]);
- break;
- default:
- UNREACHABLE();
- }
- }
-}
-
-
-void FindOneByteStringIndices(Vector<const uint8_t> subject, char pattern,
- ZoneList<int>* indices, unsigned int limit,
- Zone* zone) {
- DCHECK(limit > 0);
- // Collect indices of pattern in subject using memchr.
- // Stop after finding at most limit values.
- const uint8_t* subject_start = subject.start();
- const uint8_t* subject_end = subject_start + subject.length();
- const uint8_t* pos = subject_start;
- while (limit > 0) {
- pos = reinterpret_cast<const uint8_t*>(
- memchr(pos, pattern, subject_end - pos));
- if (pos == NULL) return;
- indices->Add(static_cast<int>(pos - subject_start), zone);
- pos++;
- limit--;
- }
-}
-
-
-void FindTwoByteStringIndices(const Vector<const uc16> subject, uc16 pattern,
- ZoneList<int>* indices, unsigned int limit,
- Zone* zone) {
- DCHECK(limit > 0);
- const uc16* subject_start = subject.start();
- const uc16* subject_end = subject_start + subject.length();
- for (const uc16* pos = subject_start; pos < subject_end && limit > 0; pos++) {
- if (*pos == pattern) {
- indices->Add(static_cast<int>(pos - subject_start), zone);
- limit--;
- }
- }
-}
-
-
-template <typename SubjectChar, typename PatternChar>
-void FindStringIndices(Isolate* isolate, Vector<const SubjectChar> subject,
- Vector<const PatternChar> pattern,
- ZoneList<int>* indices, unsigned int limit, Zone* zone) {
- DCHECK(limit > 0);
- // Collect indices of pattern in subject.
- // Stop after finding at most limit values.
- int pattern_length = pattern.length();
- int index = 0;
- StringSearch<PatternChar, SubjectChar> search(isolate, pattern);
- while (limit > 0) {
- index = search.Search(subject, index);
- if (index < 0) return;
- indices->Add(index, zone);
- index += pattern_length;
- limit--;
- }
-}
-
-
-void FindStringIndicesDispatch(Isolate* isolate, String* subject,
- String* pattern, ZoneList<int>* indices,
- unsigned int limit, Zone* zone) {
- {
- DisallowHeapAllocation no_gc;
- String::FlatContent subject_content = subject->GetFlatContent();
- String::FlatContent pattern_content = pattern->GetFlatContent();
- DCHECK(subject_content.IsFlat());
- DCHECK(pattern_content.IsFlat());
- if (subject_content.IsOneByte()) {
- Vector<const uint8_t> subject_vector = subject_content.ToOneByteVector();
- if (pattern_content.IsOneByte()) {
- Vector<const uint8_t> pattern_vector =
- pattern_content.ToOneByteVector();
- if (pattern_vector.length() == 1) {
- FindOneByteStringIndices(subject_vector, pattern_vector[0], indices,
- limit, zone);
- } else {
- FindStringIndices(isolate, subject_vector, pattern_vector, indices,
- limit, zone);
- }
- } else {
- FindStringIndices(isolate, subject_vector,
- pattern_content.ToUC16Vector(), indices, limit, zone);
- }
- } else {
- Vector<const uc16> subject_vector = subject_content.ToUC16Vector();
- if (pattern_content.IsOneByte()) {
- Vector<const uint8_t> pattern_vector =
- pattern_content.ToOneByteVector();
- if (pattern_vector.length() == 1) {
- FindTwoByteStringIndices(subject_vector, pattern_vector[0], indices,
- limit, zone);
- } else {
- FindStringIndices(isolate, subject_vector, pattern_vector, indices,
- limit, zone);
- }
- } else {
- Vector<const uc16> pattern_vector = pattern_content.ToUC16Vector();
- if (pattern_vector.length() == 1) {
- FindTwoByteStringIndices(subject_vector, pattern_vector[0], indices,
- limit, zone);
- } else {
- FindStringIndices(isolate, subject_vector, pattern_vector, indices,
- limit, zone);
- }
- }
- }
- }
-}
-
-
-template <typename ResultSeqString>
-MUST_USE_RESULT static Object* StringReplaceGlobalAtomRegExpWithString(
- Isolate* isolate, Handle<String> subject, Handle<JSRegExp> pattern_regexp,
- Handle<String> replacement, Handle<JSArray> last_match_info) {
- DCHECK(subject->IsFlat());
- DCHECK(replacement->IsFlat());
-
- ZoneScope zone_scope(isolate->runtime_zone());
- ZoneList<int> indices(8, zone_scope.zone());
- DCHECK_EQ(JSRegExp::ATOM, pattern_regexp->TypeTag());
- String* pattern =
- String::cast(pattern_regexp->DataAt(JSRegExp::kAtomPatternIndex));
- int subject_len = subject->length();
- int pattern_len = pattern->length();
- int replacement_len = replacement->length();
-
- FindStringIndicesDispatch(isolate, *subject, pattern, &indices, 0xffffffff,
- zone_scope.zone());
-
- int matches = indices.length();
- if (matches == 0) return *subject;
-
- // Detect integer overflow.
- int64_t result_len_64 = (static_cast<int64_t>(replacement_len) -
- static_cast<int64_t>(pattern_len)) *
- static_cast<int64_t>(matches) +
- static_cast<int64_t>(subject_len);
- int result_len;
- if (result_len_64 > static_cast<int64_t>(String::kMaxLength)) {
- STATIC_ASSERT(String::kMaxLength < kMaxInt);
- result_len = kMaxInt; // Provoke exception.
- } else {
- result_len = static_cast<int>(result_len_64);
- }
-
- int subject_pos = 0;
- int result_pos = 0;
-
- MaybeHandle<SeqString> maybe_res;
- if (ResultSeqString::kHasOneByteEncoding) {
- maybe_res = isolate->factory()->NewRawOneByteString(result_len);
- } else {
- maybe_res = isolate->factory()->NewRawTwoByteString(result_len);
- }
- Handle<SeqString> untyped_res;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, untyped_res, maybe_res);
- Handle<ResultSeqString> result = Handle<ResultSeqString>::cast(untyped_res);
-
- for (int i = 0; i < matches; i++) {
- // Copy non-matched subject content.
- if (subject_pos < indices.at(i)) {
- String::WriteToFlat(*subject, result->GetChars() + result_pos,
- subject_pos, indices.at(i));
- result_pos += indices.at(i) - subject_pos;
- }
-
- // Replace match.
- if (replacement_len > 0) {
- String::WriteToFlat(*replacement, result->GetChars() + result_pos, 0,
- replacement_len);
- result_pos += replacement_len;
- }
-
- subject_pos = indices.at(i) + pattern_len;
- }
- // Add remaining subject content at the end.
- if (subject_pos < subject_len) {
- String::WriteToFlat(*subject, result->GetChars() + result_pos, subject_pos,
- subject_len);
- }
-
- int32_t match_indices[] = {indices.at(matches - 1),
- indices.at(matches - 1) + pattern_len};
- RegExpImpl::SetLastMatchInfo(last_match_info, subject, 0, match_indices);
-
- return *result;
-}
-
-
-MUST_USE_RESULT static Object* StringReplaceGlobalRegExpWithString(
- Isolate* isolate, Handle<String> subject, Handle<JSRegExp> regexp,
- Handle<String> replacement, Handle<JSArray> last_match_info) {
- DCHECK(subject->IsFlat());
- DCHECK(replacement->IsFlat());
-
- int capture_count = regexp->CaptureCount();
- int subject_length = subject->length();
-
- // CompiledReplacement uses zone allocation.
- ZoneScope zone_scope(isolate->runtime_zone());
- CompiledReplacement compiled_replacement(zone_scope.zone());
- bool simple_replace =
- compiled_replacement.Compile(replacement, capture_count, subject_length);
-
- // Shortcut for simple non-regexp global replacements
- if (regexp->TypeTag() == JSRegExp::ATOM && simple_replace) {
- if (subject->HasOnlyOneByteChars() && replacement->HasOnlyOneByteChars()) {
- return StringReplaceGlobalAtomRegExpWithString<SeqOneByteString>(
- isolate, subject, regexp, replacement, last_match_info);
- } else {
- return StringReplaceGlobalAtomRegExpWithString<SeqTwoByteString>(
- isolate, subject, regexp, replacement, last_match_info);
- }
- }
-
- RegExpImpl::GlobalCache global_cache(regexp, subject, true, isolate);
- if (global_cache.HasException()) return isolate->heap()->exception();
-
- int32_t* current_match = global_cache.FetchNext();
- if (current_match == NULL) {
- if (global_cache.HasException()) return isolate->heap()->exception();
- return *subject;
- }
-
- // Guessing the number of parts that the final result string is built
- // from. Global regexps can match any number of times, so we guess
- // conservatively.
- int expected_parts = (compiled_replacement.parts() + 1) * 4 + 1;
- ReplacementStringBuilder builder(isolate->heap(), subject, expected_parts);
-
- // Number of parts added by compiled replacement plus preceeding
- // string and possibly suffix after last match. It is possible for
- // all components to use two elements when encoded as two smis.
- const int parts_added_per_loop = 2 * (compiled_replacement.parts() + 2);
-
- int prev = 0;
-
- do {
- builder.EnsureCapacity(parts_added_per_loop);
-
- int start = current_match[0];
- int end = current_match[1];
-
- if (prev < start) {
- builder.AddSubjectSlice(prev, start);
- }
-
- if (simple_replace) {
- builder.AddString(replacement);
- } else {
- compiled_replacement.Apply(&builder, start, end, current_match);
- }
- prev = end;
-
- current_match = global_cache.FetchNext();
- } while (current_match != NULL);
-
- if (global_cache.HasException()) return isolate->heap()->exception();
-
- if (prev < subject_length) {
- builder.EnsureCapacity(2);
- builder.AddSubjectSlice(prev, subject_length);
- }
-
- RegExpImpl::SetLastMatchInfo(last_match_info, subject, capture_count,
- global_cache.LastSuccessfulMatch());
-
- Handle<String> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, builder.ToString());
- return *result;
-}
-
-
-template <typename ResultSeqString>
-MUST_USE_RESULT static Object* StringReplaceGlobalRegExpWithEmptyString(
- Isolate* isolate, Handle<String> subject, Handle<JSRegExp> regexp,
- Handle<JSArray> last_match_info) {
- DCHECK(subject->IsFlat());
-
- // Shortcut for simple non-regexp global replacements
- if (regexp->TypeTag() == JSRegExp::ATOM) {
- Handle<String> empty_string = isolate->factory()->empty_string();
- if (subject->IsOneByteRepresentation()) {
- return StringReplaceGlobalAtomRegExpWithString<SeqOneByteString>(
- isolate, subject, regexp, empty_string, last_match_info);
- } else {
- return StringReplaceGlobalAtomRegExpWithString<SeqTwoByteString>(
- isolate, subject, regexp, empty_string, last_match_info);
- }
- }
-
- RegExpImpl::GlobalCache global_cache(regexp, subject, true, isolate);
- if (global_cache.HasException()) return isolate->heap()->exception();
-
- int32_t* current_match = global_cache.FetchNext();
- if (current_match == NULL) {
- if (global_cache.HasException()) return isolate->heap()->exception();
- return *subject;
- }
-
- int start = current_match[0];
- int end = current_match[1];
- int capture_count = regexp->CaptureCount();
- int subject_length = subject->length();
-
- int new_length = subject_length - (end - start);
- if (new_length == 0) return isolate->heap()->empty_string();
-
- Handle<ResultSeqString> answer;
- if (ResultSeqString::kHasOneByteEncoding) {
- answer = Handle<ResultSeqString>::cast(
- isolate->factory()->NewRawOneByteString(new_length).ToHandleChecked());
- } else {
- answer = Handle<ResultSeqString>::cast(
- isolate->factory()->NewRawTwoByteString(new_length).ToHandleChecked());
- }
-
- int prev = 0;
- int position = 0;
-
- do {
- start = current_match[0];
- end = current_match[1];
- if (prev < start) {
- // Add substring subject[prev;start] to answer string.
- String::WriteToFlat(*subject, answer->GetChars() + position, prev, start);
- position += start - prev;
- }
- prev = end;
-
- current_match = global_cache.FetchNext();
- } while (current_match != NULL);
-
- if (global_cache.HasException()) return isolate->heap()->exception();
-
- RegExpImpl::SetLastMatchInfo(last_match_info, subject, capture_count,
- global_cache.LastSuccessfulMatch());
-
- if (prev < subject_length) {
- // Add substring subject[prev;length] to answer string.
- String::WriteToFlat(*subject, answer->GetChars() + position, prev,
- subject_length);
- position += subject_length - prev;
- }
-
- if (position == 0) return isolate->heap()->empty_string();
-
- // Shorten string and fill
- int string_size = ResultSeqString::SizeFor(position);
- int allocated_string_size = ResultSeqString::SizeFor(new_length);
- int delta = allocated_string_size - string_size;
-
- answer->set_length(position);
- if (delta == 0) return *answer;
-
- Address end_of_string = answer->address() + string_size;
- Heap* heap = isolate->heap();
-
- // The trimming is performed on a newly allocated object, which is on a
- // fresly allocated page or on an already swept page. Hence, the sweeper
- // thread can not get confused with the filler creation. No synchronization
- // needed.
- heap->CreateFillerObjectAt(end_of_string, delta);
- heap->AdjustLiveBytes(answer->address(), -delta, Heap::FROM_MUTATOR);
- return *answer;
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringReplaceGlobalRegExpWithString) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 4);
-
- CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, replacement, 2);
- CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 1);
- CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 3);
-
- RUNTIME_ASSERT(regexp->GetFlags().is_global());
- RUNTIME_ASSERT(last_match_info->HasFastObjectElements());
-
- subject = String::Flatten(subject);
-
- if (replacement->length() == 0) {
- if (subject->HasOnlyOneByteChars()) {
- return StringReplaceGlobalRegExpWithEmptyString<SeqOneByteString>(
- isolate, subject, regexp, last_match_info);
- } else {
- return StringReplaceGlobalRegExpWithEmptyString<SeqTwoByteString>(
- isolate, subject, regexp, last_match_info);
- }
- }
-
- replacement = String::Flatten(replacement);
-
- return StringReplaceGlobalRegExpWithString(isolate, subject, regexp,
- replacement, last_match_info);
-}
-
-
-// This may return an empty MaybeHandle if an exception is thrown or
-// we abort due to reaching the recursion limit.
-MaybeHandle<String> StringReplaceOneCharWithString(
- Isolate* isolate, Handle<String> subject, Handle<String> search,
- Handle<String> replace, bool* found, int recursion_limit) {
- StackLimitCheck stackLimitCheck(isolate);
- if (stackLimitCheck.HasOverflowed() || (recursion_limit == 0)) {
- return MaybeHandle<String>();
- }
- recursion_limit--;
- if (subject->IsConsString()) {
- ConsString* cons = ConsString::cast(*subject);
- Handle<String> first = Handle<String>(cons->first());
- Handle<String> second = Handle<String>(cons->second());
- Handle<String> new_first;
- if (!StringReplaceOneCharWithString(isolate, first, search, replace, found,
- recursion_limit).ToHandle(&new_first)) {
- return MaybeHandle<String>();
- }
- if (*found) return isolate->factory()->NewConsString(new_first, second);
-
- Handle<String> new_second;
- if (!StringReplaceOneCharWithString(isolate, second, search, replace, found,
- recursion_limit)
- .ToHandle(&new_second)) {
- return MaybeHandle<String>();
- }
- if (*found) return isolate->factory()->NewConsString(first, new_second);
-
- return subject;
- } else {
- int index = Runtime::StringMatch(isolate, subject, search, 0);
- if (index == -1) return subject;
- *found = true;
- Handle<String> first = isolate->factory()->NewSubString(subject, 0, index);
- Handle<String> cons1;
- ASSIGN_RETURN_ON_EXCEPTION(
- isolate, cons1, isolate->factory()->NewConsString(first, replace),
- String);
- Handle<String> second =
- isolate->factory()->NewSubString(subject, index + 1, subject->length());
- return isolate->factory()->NewConsString(cons1, second);
- }
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringReplaceOneCharWithString) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 3);
- CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, search, 1);
- CONVERT_ARG_HANDLE_CHECKED(String, replace, 2);
-
- // If the cons string tree is too deep, we simply abort the recursion and
- // retry with a flattened subject string.
- const int kRecursionLimit = 0x1000;
- bool found = false;
- Handle<String> result;
- if (StringReplaceOneCharWithString(isolate, subject, search, replace, &found,
- kRecursionLimit).ToHandle(&result)) {
- return *result;
- }
- if (isolate->has_pending_exception()) return isolate->heap()->exception();
-
- subject = String::Flatten(subject);
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, result,
- StringReplaceOneCharWithString(isolate, subject, search, replace, &found,
- kRecursionLimit));
- return *result;
-}
-
-
-// Perform string match of pattern on subject, starting at start index.
-// Caller must ensure that 0 <= start_index <= sub->length(),
-// and should check that pat->length() + start_index <= sub->length().
-int Runtime::StringMatch(Isolate* isolate, Handle<String> sub,
- Handle<String> pat, int start_index) {
- DCHECK(0 <= start_index);
- DCHECK(start_index <= sub->length());
-
- int pattern_length = pat->length();
- if (pattern_length == 0) return start_index;
-
- int subject_length = sub->length();
- if (start_index + pattern_length > subject_length) return -1;
-
- sub = String::Flatten(sub);
- pat = String::Flatten(pat);
-
- DisallowHeapAllocation no_gc; // ensure vectors stay valid
- // Extract flattened substrings of cons strings before getting encoding.
- String::FlatContent seq_sub = sub->GetFlatContent();
- String::FlatContent seq_pat = pat->GetFlatContent();
-
- // dispatch on type of strings
- if (seq_pat.IsOneByte()) {
- Vector<const uint8_t> pat_vector = seq_pat.ToOneByteVector();
- if (seq_sub.IsOneByte()) {
- return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
- start_index);
- }
- return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector,
- start_index);
- }
- Vector<const uc16> pat_vector = seq_pat.ToUC16Vector();
- if (seq_sub.IsOneByte()) {
- return SearchString(isolate, seq_sub.ToOneByteVector(), pat_vector,
- start_index);
- }
- return SearchString(isolate, seq_sub.ToUC16Vector(), pat_vector, start_index);
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringIndexOf) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 3);
-
- CONVERT_ARG_HANDLE_CHECKED(String, sub, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, pat, 1);
- CONVERT_ARG_HANDLE_CHECKED(Object, index, 2);
-
- uint32_t start_index;
- if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1);
-
- RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length()));
- int position = Runtime::StringMatch(isolate, sub, pat, start_index);
- return Smi::FromInt(position);
-}
-
-
-template <typename schar, typename pchar>
-static int StringMatchBackwards(Vector<const schar> subject,
- Vector<const pchar> pattern, int idx) {
- int pattern_length = pattern.length();
- DCHECK(pattern_length >= 1);
- DCHECK(idx + pattern_length <= subject.length());
-
- if (sizeof(schar) == 1 && sizeof(pchar) > 1) {
- for (int i = 0; i < pattern_length; i++) {
- uc16 c = pattern[i];
- if (c > String::kMaxOneByteCharCode) {
- return -1;
- }
- }
- }
-
- pchar pattern_first_char = pattern[0];
- for (int i = idx; i >= 0; i--) {
- if (subject[i] != pattern_first_char) continue;
- int j = 1;
- while (j < pattern_length) {
- if (pattern[j] != subject[i + j]) {
- break;
- }
- j++;
- }
- if (j == pattern_length) {
- return i;
- }
- }
- return -1;
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringLastIndexOf) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 3);
-
- CONVERT_ARG_HANDLE_CHECKED(String, sub, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, pat, 1);
- CONVERT_ARG_HANDLE_CHECKED(Object, index, 2);
-
- uint32_t start_index;
- if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1);
-
- uint32_t pat_length = pat->length();
- uint32_t sub_length = sub->length();
-
- if (start_index + pat_length > sub_length) {
- start_index = sub_length - pat_length;
- }
-
- if (pat_length == 0) {
- return Smi::FromInt(start_index);
- }
-
- sub = String::Flatten(sub);
- pat = String::Flatten(pat);
-
- int position = -1;
- DisallowHeapAllocation no_gc; // ensure vectors stay valid
-
- String::FlatContent sub_content = sub->GetFlatContent();
- String::FlatContent pat_content = pat->GetFlatContent();
-
- if (pat_content.IsOneByte()) {
- Vector<const uint8_t> pat_vector = pat_content.ToOneByteVector();
- if (sub_content.IsOneByte()) {
- position = StringMatchBackwards(sub_content.ToOneByteVector(), pat_vector,
- start_index);
- } else {
- position = StringMatchBackwards(sub_content.ToUC16Vector(), pat_vector,
- start_index);
- }
- } else {
- Vector<const uc16> pat_vector = pat_content.ToUC16Vector();
- if (sub_content.IsOneByte()) {
- position = StringMatchBackwards(sub_content.ToOneByteVector(), pat_vector,
- start_index);
- } else {
- position = StringMatchBackwards(sub_content.ToUC16Vector(), pat_vector,
- start_index);
- }
- }
-
- return Smi::FromInt(position);
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringLocaleCompare) {
- HandleScope handle_scope(isolate);
- DCHECK(args.length() == 2);
-
- CONVERT_ARG_HANDLE_CHECKED(String, str1, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, str2, 1);
-
- if (str1.is_identical_to(str2)) return Smi::FromInt(0); // Equal.
- int str1_length = str1->length();
- int str2_length = str2->length();
-
- // Decide trivial cases without flattening.
- if (str1_length == 0) {
- if (str2_length == 0) return Smi::FromInt(0); // Equal.
- return Smi::FromInt(-str2_length);
- } else {
- if (str2_length == 0) return Smi::FromInt(str1_length);
- }
-
- int end = str1_length < str2_length ? str1_length : str2_length;
-
- // No need to flatten if we are going to find the answer on the first
- // character. At this point we know there is at least one character
- // in each string, due to the trivial case handling above.
- int d = str1->Get(0) - str2->Get(0);
- if (d != 0) return Smi::FromInt(d);
-
- str1 = String::Flatten(str1);
- str2 = String::Flatten(str2);
-
- DisallowHeapAllocation no_gc;
- String::FlatContent flat1 = str1->GetFlatContent();
- String::FlatContent flat2 = str2->GetFlatContent();
-
- for (int i = 0; i < end; i++) {
- if (flat1.Get(i) != flat2.Get(i)) {
- return Smi::FromInt(flat1.Get(i) - flat2.Get(i));
- }
- }
-
- return Smi::FromInt(str1_length - str2_length);
-}
-
-
-RUNTIME_FUNCTION(Runtime_SubString) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 3);
-
- CONVERT_ARG_HANDLE_CHECKED(String, string, 0);
- int start, end;
- // We have a fast integer-only case here to avoid a conversion to double in
- // the common case where from and to are Smis.
- if (args[1]->IsSmi() && args[2]->IsSmi()) {
- CONVERT_SMI_ARG_CHECKED(from_number, 1);
- CONVERT_SMI_ARG_CHECKED(to_number, 2);
- start = from_number;
- end = to_number;
- } else {
- CONVERT_DOUBLE_ARG_CHECKED(from_number, 1);
- CONVERT_DOUBLE_ARG_CHECKED(to_number, 2);
- start = FastD2IChecked(from_number);
- end = FastD2IChecked(to_number);
- }
- RUNTIME_ASSERT(end >= start);
- RUNTIME_ASSERT(start >= 0);
- RUNTIME_ASSERT(end <= string->length());
- isolate->counters()->sub_string_runtime()->Increment();
-
- return *isolate->factory()->NewSubString(string, start, end);
-}
-
-
-RUNTIME_FUNCTION(Runtime_InternalizeString) {
- HandleScope handles(isolate);
- RUNTIME_ASSERT(args.length() == 1);
- CONVERT_ARG_HANDLE_CHECKED(String, string, 0);
- return *isolate->factory()->InternalizeString(string);
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringMatch) {
- HandleScope handles(isolate);
- DCHECK(args.length() == 3);
-
- CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
- CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 1);
- CONVERT_ARG_HANDLE_CHECKED(JSArray, regexp_info, 2);
-
- RUNTIME_ASSERT(regexp_info->HasFastObjectElements());
-
- RegExpImpl::GlobalCache global_cache(regexp, subject, true, isolate);
- if (global_cache.HasException()) return isolate->heap()->exception();
-
- int capture_count = regexp->CaptureCount();
-
- ZoneScope zone_scope(isolate->runtime_zone());
- ZoneList<int> offsets(8, zone_scope.zone());
-
- while (true) {
- int32_t* match = global_cache.FetchNext();
- if (match == NULL) break;
- offsets.Add(match[0], zone_scope.zone()); // start
- offsets.Add(match[1], zone_scope.zone()); // end
- }
-
- if (global_cache.HasException()) return isolate->heap()->exception();
-
- if (offsets.length() == 0) {
- // Not a single match.
- return isolate->heap()->null_value();
- }
-
- RegExpImpl::SetLastMatchInfo(regexp_info, subject, capture_count,
- global_cache.LastSuccessfulMatch());
-
- int matches = offsets.length() / 2;
- Handle<FixedArray> elements = isolate->factory()->NewFixedArray(matches);
- Handle<String> substring =
- isolate->factory()->NewSubString(subject, offsets.at(0), offsets.at(1));
- elements->set(0, *substring);
- for (int i = 1; i < matches; i++) {
- HandleScope temp_scope(isolate);
- int from = offsets.at(i * 2);
- int to = offsets.at(i * 2 + 1);
- Handle<String> substring =
- isolate->factory()->NewProperSubString(subject, from, to);
- elements->set(i, *substring);
- }
- Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(elements);
- result->set_length(Smi::FromInt(matches));
- return *result;
-}
-
-
-// Only called from Runtime_RegExpExecMultiple so it doesn't need to maintain
-// separate last match info. See comment on that function.
-template <bool has_capture>
-static Object* SearchRegExpMultiple(Isolate* isolate, Handle<String> subject,
- Handle<JSRegExp> regexp,
- Handle<JSArray> last_match_array,
- Handle<JSArray> result_array) {
- DCHECK(subject->IsFlat());
- DCHECK_NE(has_capture, regexp->CaptureCount() == 0);
-
- int capture_count = regexp->CaptureCount();
- int subject_length = subject->length();
-
- static const int kMinLengthToCache = 0x1000;
-
- if (subject_length > kMinLengthToCache) {
- Handle<Object> cached_answer(
- RegExpResultsCache::Lookup(isolate->heap(), *subject, regexp->data(),
- RegExpResultsCache::REGEXP_MULTIPLE_INDICES),
- isolate);
- if (*cached_answer != Smi::FromInt(0)) {
- Handle<FixedArray> cached_fixed_array =
- Handle<FixedArray>(FixedArray::cast(*cached_answer));
- // The cache FixedArray is a COW-array and can therefore be reused.
- JSArray::SetContent(result_array, cached_fixed_array);
- // The actual length of the result array is stored in the last element of
- // the backing store (the backing FixedArray may have a larger capacity).
- Object* cached_fixed_array_last_element =
- cached_fixed_array->get(cached_fixed_array->length() - 1);
- Smi* js_array_length = Smi::cast(cached_fixed_array_last_element);
- result_array->set_length(js_array_length);
- RegExpImpl::SetLastMatchInfo(last_match_array, subject, capture_count,
- NULL);
- return *result_array;
- }
- }
-
- RegExpImpl::GlobalCache global_cache(regexp, subject, true, isolate);
- if (global_cache.HasException()) return isolate->heap()->exception();
-
- // Ensured in Runtime_RegExpExecMultiple.
- DCHECK(result_array->HasFastObjectElements());
- Handle<FixedArray> result_elements(
- FixedArray::cast(result_array->elements()));
- if (result_elements->length() < 16) {
- result_elements = isolate->factory()->NewFixedArrayWithHoles(16);
- }
-
- FixedArrayBuilder builder(result_elements);
-
- // Position to search from.
- int match_start = -1;
- int match_end = 0;
- bool first = true;
-
- // Two smis before and after the match, for very long strings.
- static const int kMaxBuilderEntriesPerRegExpMatch = 5;
-
- while (true) {
- int32_t* current_match = global_cache.FetchNext();
- if (current_match == NULL) break;
- match_start = current_match[0];
- builder.EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch);
- if (match_end < match_start) {
- ReplacementStringBuilder::AddSubjectSlice(&builder, match_end,
- match_start);
- }
- match_end = current_match[1];
- {
- // Avoid accumulating new handles inside loop.
- HandleScope temp_scope(isolate);
- Handle<String> match;
- if (!first) {
- match = isolate->factory()->NewProperSubString(subject, match_start,
- match_end);
- } else {
- match =
- isolate->factory()->NewSubString(subject, match_start, match_end);
- first = false;
- }
-
- if (has_capture) {
- // Arguments array to replace function is match, captures, index and
- // subject, i.e., 3 + capture count in total.
- Handle<FixedArray> elements =
- isolate->factory()->NewFixedArray(3 + capture_count);
-
- elements->set(0, *match);
- for (int i = 1; i <= capture_count; i++) {
- int start = current_match[i * 2];
- if (start >= 0) {
- int end = current_match[i * 2 + 1];
- DCHECK(start <= end);
- Handle<String> substring =
- isolate->factory()->NewSubString(subject, start, end);
- elements->set(i, *substring);
- } else {
- DCHECK(current_match[i * 2 + 1] < 0);
- elements->set(i, isolate->heap()->undefined_value());
- }
- }
- elements->set(capture_count + 1, Smi::FromInt(match_start));
- elements->set(capture_count + 2, *subject);
- builder.Add(*isolate->factory()->NewJSArrayWithElements(elements));
- } else {
- builder.Add(*match);
- }
- }
+ int operands_count = operand_stack->length();
+ if (operands_count != 0) {
+ frame->RestoreOperandStack(operand_stack,
+ generator_object->stack_handler_index());
+ generator_object->set_operand_stack(isolate->heap()->empty_fixed_array());
+ generator_object->set_stack_handler_index(-1);
}
- if (global_cache.HasException()) return isolate->heap()->exception();
+ JSGeneratorObject::ResumeMode resume_mode =
+ static_cast<JSGeneratorObject::ResumeMode>(resume_mode_int);
+ switch (resume_mode) {
+ case JSGeneratorObject::NEXT:
+ return value;
+ case JSGeneratorObject::THROW:
+ return isolate->Throw(value);
+ }
- if (match_start >= 0) {
- // Finished matching, with at least one match.
- if (match_end < subject_length) {
- ReplacementStringBuilder::AddSubjectSlice(&builder, match_end,
- subject_length);
- }
+ UNREACHABLE();
+ return isolate->ThrowIllegalOperation();
+}
- RegExpImpl::SetLastMatchInfo(last_match_array, subject, capture_count,
- NULL);
- if (subject_length > kMinLengthToCache) {
- // Store the length of the result array into the last element of the
- // backing FixedArray.
- builder.EnsureCapacity(1);
- Handle<FixedArray> fixed_array = builder.array();
- fixed_array->set(fixed_array->length() - 1,
- Smi::FromInt(builder.length()));
- // Cache the result and turn the FixedArray into a COW array.
- RegExpResultsCache::Enter(isolate, subject,
- handle(regexp->data(), isolate), fixed_array,
- RegExpResultsCache::REGEXP_MULTIPLE_INDICES);
- }
- return *builder.ToJSArray(result_array);
- } else {
- return isolate->heap()->null_value(); // No matches at all.
- }
+RUNTIME_FUNCTION(Runtime_ThrowGeneratorStateError) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 1);
+ CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, generator, 0);
+ int continuation = generator->continuation();
+ const char* message = continuation == JSGeneratorObject::kGeneratorClosed
+ ? "generator_finished"
+ : "generator_running";
+ Vector<Handle<Object> > argv = HandleVector<Object>(NULL, 0);
+ THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewError(message, argv));
}
-// This is only called for StringReplaceGlobalRegExpWithFunction. This sets
-// lastMatchInfoOverride to maintain the last match info, so we don't need to
-// set any other last match array info.
-RUNTIME_FUNCTION(Runtime_RegExpExecMultiple) {
- HandleScope handles(isolate);
- DCHECK(args.length() == 4);
-
- CONVERT_ARG_HANDLE_CHECKED(String, subject, 1);
- CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0);
- CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 2);
- CONVERT_ARG_HANDLE_CHECKED(JSArray, result_array, 3);
- RUNTIME_ASSERT(last_match_info->HasFastObjectElements());
- RUNTIME_ASSERT(result_array->HasFastObjectElements());
+RUNTIME_FUNCTION(Runtime_ObjectFreeze) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 1);
+ CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
- subject = String::Flatten(subject);
- RUNTIME_ASSERT(regexp->GetFlags().is_global());
+ // %ObjectFreeze is a fast path and these cases are handled elsewhere.
+ RUNTIME_ASSERT(!object->HasSloppyArgumentsElements() &&
+ !object->map()->is_observed() && !object->IsJSProxy());
- if (regexp->CaptureCount() == 0) {
- return SearchRegExpMultiple<false>(isolate, subject, regexp,
- last_match_info, result_array);
- } else {
- return SearchRegExpMultiple<true>(isolate, subject, regexp, last_match_info,
- result_array);
- }
+ Handle<Object> result;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result, JSObject::Freeze(object));
+ return *result;
}
}
}
- // Slower case.
- int flags = ALLOW_HEX;
- if (FLAG_harmony_numeric_literals) {
- // The current spec draft has not updated "ToNumber Applied to the String
- // Type", https://bugs.ecmascript.org/show_bug.cgi?id=1584
- flags |= ALLOW_OCTAL | ALLOW_BINARY;
- }
-
- return *isolate->factory()->NewNumber(
- StringToDouble(isolate->unicode_cache(), *subject, flags));
-}
-
-
-RUNTIME_FUNCTION(Runtime_NewString) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 2);
- CONVERT_INT32_ARG_CHECKED(length, 0);
- CONVERT_BOOLEAN_ARG_CHECKED(is_one_byte, 1);
- if (length == 0) return isolate->heap()->empty_string();
- Handle<String> result;
- if (is_one_byte) {
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, result, isolate->factory()->NewRawOneByteString(length));
- } else {
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, result, isolate->factory()->NewRawTwoByteString(length));
- }
- return *result;
-}
-
-
-RUNTIME_FUNCTION(Runtime_TruncateString) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 2);
- CONVERT_ARG_HANDLE_CHECKED(SeqString, string, 0);
- CONVERT_INT32_ARG_CHECKED(new_length, 1);
- RUNTIME_ASSERT(new_length >= 0);
- return *SeqString::Truncate(string, new_length);
-}
-
-
-RUNTIME_FUNCTION(Runtime_URIEscape) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 1);
- CONVERT_ARG_HANDLE_CHECKED(String, source, 0);
- Handle<String> string = String::Flatten(source);
- DCHECK(string->IsFlat());
- Handle<String> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, result, string->IsOneByteRepresentationUnderneath()
- ? URIEscape::Escape<uint8_t>(isolate, source)
- : URIEscape::Escape<uc16>(isolate, source));
- return *result;
-}
-
-
-RUNTIME_FUNCTION(Runtime_URIUnescape) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 1);
- CONVERT_ARG_HANDLE_CHECKED(String, source, 0);
- Handle<String> string = String::Flatten(source);
- DCHECK(string->IsFlat());
- Handle<String> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, result, string->IsOneByteRepresentationUnderneath()
- ? URIUnescape::Unescape<uint8_t>(isolate, source)
- : URIUnescape::Unescape<uc16>(isolate, source));
- return *result;
-}
-
-
-RUNTIME_FUNCTION(Runtime_QuoteJSONString) {
- HandleScope scope(isolate);
- CONVERT_ARG_HANDLE_CHECKED(String, string, 0);
- DCHECK(args.length() == 1);
- Handle<Object> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, result, BasicJsonStringifier::StringifyString(isolate, string));
- return *result;
-}
-
-
-RUNTIME_FUNCTION(Runtime_BasicJSONStringify) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 1);
- CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
- BasicJsonStringifier stringifier(isolate);
- Handle<Object> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
- stringifier.Stringify(object));
- return *result;
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringParseInt) {
- HandleScope handle_scope(isolate);
- DCHECK(args.length() == 2);
- CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
- CONVERT_NUMBER_CHECKED(int, radix, Int32, args[1]);
- RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36));
-
- subject = String::Flatten(subject);
- double value;
-
- {
- DisallowHeapAllocation no_gc;
- String::FlatContent flat = subject->GetFlatContent();
-
- // ECMA-262 section 15.1.2.3, empty string is NaN
- if (flat.IsOneByte()) {
- value =
- StringToInt(isolate->unicode_cache(), flat.ToOneByteVector(), radix);
- } else {
- value = StringToInt(isolate->unicode_cache(), flat.ToUC16Vector(), radix);
- }
- }
-
- return *isolate->factory()->NewNumber(value);
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringParseFloat) {
- HandleScope shs(isolate);
- DCHECK(args.length() == 1);
- CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
-
- subject = String::Flatten(subject);
- double value = StringToDouble(isolate->unicode_cache(), *subject,
- ALLOW_TRAILING_JUNK, base::OS::nan_value());
-
- return *isolate->factory()->NewNumber(value);
-}
-
-
-static inline bool ToUpperOverflows(uc32 character) {
- // y with umlauts and the micro sign are the only characters that stop
- // fitting into one-byte when converting to uppercase.
- static const uc32 yuml_code = 0xff;
- static const uc32 micro_code = 0xb5;
- return (character == yuml_code || character == micro_code);
-}
-
-
-template <class Converter>
-MUST_USE_RESULT static Object* ConvertCaseHelper(
- Isolate* isolate, String* string, SeqString* result, int result_length,
- unibrow::Mapping<Converter, 128>* mapping) {
- DisallowHeapAllocation no_gc;
- // We try this twice, once with the assumption that the result is no longer
- // than the input and, if that assumption breaks, again with the exact
- // length. This may not be pretty, but it is nicer than what was here before
- // and I hereby claim my vaffel-is.
- //
- // NOTE: This assumes that the upper/lower case of an ASCII
- // character is also ASCII. This is currently the case, but it
- // might break in the future if we implement more context and locale
- // dependent upper/lower conversions.
- bool has_changed_character = false;
-
- // Convert all characters to upper case, assuming that they will fit
- // in the buffer
- Access<ConsStringIteratorOp> op(isolate->runtime_state()->string_iterator());
- StringCharacterStream stream(string, op.value());
- unibrow::uchar chars[Converter::kMaxWidth];
- // We can assume that the string is not empty
- uc32 current = stream.GetNext();
- bool ignore_overflow = Converter::kIsToLower || result->IsSeqTwoByteString();
- for (int i = 0; i < result_length;) {
- bool has_next = stream.HasMore();
- uc32 next = has_next ? stream.GetNext() : 0;
- int char_length = mapping->get(current, next, chars);
- if (char_length == 0) {
- // The case conversion of this character is the character itself.
- result->Set(i, current);
- i++;
- } else if (char_length == 1 &&
- (ignore_overflow || !ToUpperOverflows(current))) {
- // Common case: converting the letter resulted in one character.
- DCHECK(static_cast<uc32>(chars[0]) != current);
- result->Set(i, chars[0]);
- has_changed_character = true;
- i++;
- } else if (result_length == string->length()) {
- bool overflows = ToUpperOverflows(current);
- // We've assumed that the result would be as long as the
- // input but here is a character that converts to several
- // characters. No matter, we calculate the exact length
- // of the result and try the whole thing again.
- //
- // Note that this leaves room for optimization. We could just
- // memcpy what we already have to the result string. Also,
- // the result string is the last object allocated we could
- // "realloc" it and probably, in the vast majority of cases,
- // extend the existing string to be able to hold the full
- // result.
- int next_length = 0;
- if (has_next) {
- next_length = mapping->get(next, 0, chars);
- if (next_length == 0) next_length = 1;
- }
- int current_length = i + char_length + next_length;
- while (stream.HasMore()) {
- current = stream.GetNext();
- overflows |= ToUpperOverflows(current);
- // NOTE: we use 0 as the next character here because, while
- // the next character may affect what a character converts to,
- // it does not in any case affect the length of what it convert
- // to.
- int char_length = mapping->get(current, 0, chars);
- if (char_length == 0) char_length = 1;
- current_length += char_length;
- if (current_length > String::kMaxLength) {
- AllowHeapAllocation allocate_error_and_return;
- THROW_NEW_ERROR_RETURN_FAILURE(isolate,
- NewInvalidStringLengthError());
- }
- }
- // Try again with the real length. Return signed if we need
- // to allocate a two-byte string for to uppercase.
- return (overflows && !ignore_overflow) ? Smi::FromInt(-current_length)
- : Smi::FromInt(current_length);
- } else {
- for (int j = 0; j < char_length; j++) {
- result->Set(i, chars[j]);
- i++;
- }
- has_changed_character = true;
- }
- current = next;
- }
- if (has_changed_character) {
- return result;
- } else {
- // If we didn't actually change anything in doing the conversion
- // we simple return the result and let the converted string
- // become garbage; there is no reason to keep two identical strings
- // alive.
- return string;
- }
-}
-
-
-namespace {
-
-static const uintptr_t kOneInEveryByte = kUintptrAllBitsSet / 0xFF;
-static const uintptr_t kAsciiMask = kOneInEveryByte << 7;
-
-// Given a word and two range boundaries returns a word with high bit
-// set in every byte iff the corresponding input byte was strictly in
-// the range (m, n). All the other bits in the result are cleared.
-// This function is only useful when it can be inlined and the
-// boundaries are statically known.
-// Requires: all bytes in the input word and the boundaries must be
-// ASCII (less than 0x7F).
-static inline uintptr_t AsciiRangeMask(uintptr_t w, char m, char n) {
- // Use strict inequalities since in edge cases the function could be
- // further simplified.
- DCHECK(0 < m && m < n);
- // Has high bit set in every w byte less than n.
- uintptr_t tmp1 = kOneInEveryByte * (0x7F + n) - w;
- // Has high bit set in every w byte greater than m.
- uintptr_t tmp2 = w + kOneInEveryByte * (0x7F - m);
- return (tmp1 & tmp2 & (kOneInEveryByte * 0x80));
-}
-
-
-#ifdef DEBUG
-static bool CheckFastAsciiConvert(char* dst, const char* src, int length,
- bool changed, bool is_to_lower) {
- bool expected_changed = false;
- for (int i = 0; i < length; i++) {
- if (dst[i] == src[i]) continue;
- expected_changed = true;
- if (is_to_lower) {
- DCHECK('A' <= src[i] && src[i] <= 'Z');
- DCHECK(dst[i] == src[i] + ('a' - 'A'));
- } else {
- DCHECK('a' <= src[i] && src[i] <= 'z');
- DCHECK(dst[i] == src[i] - ('a' - 'A'));
- }
- }
- return (expected_changed == changed);
-}
-#endif
-
-
-template <class Converter>
-static bool FastAsciiConvert(char* dst, const char* src, int length,
- bool* changed_out) {
-#ifdef DEBUG
- char* saved_dst = dst;
- const char* saved_src = src;
-#endif
- DisallowHeapAllocation no_gc;
- // We rely on the distance between upper and lower case letters
- // being a known power of 2.
- DCHECK('a' - 'A' == (1 << 5));
- // Boundaries for the range of input characters than require conversion.
- static const char lo = Converter::kIsToLower ? 'A' - 1 : 'a' - 1;
- static const char hi = Converter::kIsToLower ? 'Z' + 1 : 'z' + 1;
- bool changed = false;
- uintptr_t or_acc = 0;
- const char* const limit = src + length;
-
- // dst is newly allocated and always aligned.
- DCHECK(IsAligned(reinterpret_cast<intptr_t>(dst), sizeof(uintptr_t)));
- // Only attempt processing one word at a time if src is also aligned.
- if (IsAligned(reinterpret_cast<intptr_t>(src), sizeof(uintptr_t))) {
- // Process the prefix of the input that requires no conversion one aligned
- // (machine) word at a time.
- while (src <= limit - sizeof(uintptr_t)) {
- const uintptr_t w = *reinterpret_cast<const uintptr_t*>(src);
- or_acc |= w;
- if (AsciiRangeMask(w, lo, hi) != 0) {
- changed = true;
- break;
- }
- *reinterpret_cast<uintptr_t*>(dst) = w;
- src += sizeof(uintptr_t);
- dst += sizeof(uintptr_t);
- }
- // Process the remainder of the input performing conversion when
- // required one word at a time.
- while (src <= limit - sizeof(uintptr_t)) {
- const uintptr_t w = *reinterpret_cast<const uintptr_t*>(src);
- or_acc |= w;
- uintptr_t m = AsciiRangeMask(w, lo, hi);
- // The mask has high (7th) bit set in every byte that needs
- // conversion and we know that the distance between cases is
- // 1 << 5.
- *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2);
- src += sizeof(uintptr_t);
- dst += sizeof(uintptr_t);
- }
- }
- // Process the last few bytes of the input (or the whole input if
- // unaligned access is not supported).
- while (src < limit) {
- char c = *src;
- or_acc |= c;
- if (lo < c && c < hi) {
- c ^= (1 << 5);
- changed = true;
- }
- *dst = c;
- ++src;
- ++dst;
- }
-
- if ((or_acc & kAsciiMask) != 0) return false;
-
- DCHECK(CheckFastAsciiConvert(saved_dst, saved_src, length, changed,
- Converter::kIsToLower));
-
- *changed_out = changed;
- return true;
-}
-
-} // namespace
-
-
-template <class Converter>
-MUST_USE_RESULT static Object* ConvertCase(
- Handle<String> s, Isolate* isolate,
- unibrow::Mapping<Converter, 128>* mapping) {
- s = String::Flatten(s);
- int length = s->length();
- // Assume that the string is not empty; we need this assumption later
- if (length == 0) return *s;
-
- // Simpler handling of ASCII strings.
- //
- // NOTE: This assumes that the upper/lower case of an ASCII
- // character is also ASCII. This is currently the case, but it
- // might break in the future if we implement more context and locale
- // dependent upper/lower conversions.
- if (s->IsOneByteRepresentationUnderneath()) {
- // Same length as input.
- Handle<SeqOneByteString> result =
- isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
- DisallowHeapAllocation no_gc;
- String::FlatContent flat_content = s->GetFlatContent();
- DCHECK(flat_content.IsFlat());
- bool has_changed_character = false;
- bool is_ascii = FastAsciiConvert<Converter>(
- reinterpret_cast<char*>(result->GetChars()),
- reinterpret_cast<const char*>(flat_content.ToOneByteVector().start()),
- length, &has_changed_character);
- // If not ASCII, we discard the result and take the 2 byte path.
- if (is_ascii) return has_changed_character ? *result : *s;
- }
-
- Handle<SeqString> result; // Same length as input.
- if (s->IsOneByteRepresentation()) {
- result = isolate->factory()->NewRawOneByteString(length).ToHandleChecked();
- } else {
- result = isolate->factory()->NewRawTwoByteString(length).ToHandleChecked();
- }
-
- Object* answer = ConvertCaseHelper(isolate, *s, *result, length, mapping);
- if (answer->IsException() || answer->IsString()) return answer;
-
- DCHECK(answer->IsSmi());
- length = Smi::cast(answer)->value();
- if (s->IsOneByteRepresentation() && length > 0) {
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, result, isolate->factory()->NewRawOneByteString(length));
- } else {
- if (length < 0) length = -length;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, result, isolate->factory()->NewRawTwoByteString(length));
- }
- return ConvertCaseHelper(isolate, *s, *result, length, mapping);
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringToLowerCase) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 1);
- CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
- return ConvertCase(s, isolate, isolate->runtime_state()->to_lower_mapping());
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringToUpperCase) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 1);
- CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
- return ConvertCase(s, isolate, isolate->runtime_state()->to_upper_mapping());
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringTrim) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 3);
-
- CONVERT_ARG_HANDLE_CHECKED(String, string, 0);
- CONVERT_BOOLEAN_ARG_CHECKED(trimLeft, 1);
- CONVERT_BOOLEAN_ARG_CHECKED(trimRight, 2);
-
- string = String::Flatten(string);
- int length = string->length();
-
- int left = 0;
- UnicodeCache* unicode_cache = isolate->unicode_cache();
- if (trimLeft) {
- while (left < length &&
- unicode_cache->IsWhiteSpaceOrLineTerminator(string->Get(left))) {
- left++;
- }
- }
-
- int right = length;
- if (trimRight) {
- while (
- right > left &&
- unicode_cache->IsWhiteSpaceOrLineTerminator(string->Get(right - 1))) {
- right--;
- }
- }
-
- return *isolate->factory()->NewSubString(string, left, right);
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringSplit) {
- HandleScope handle_scope(isolate);
- DCHECK(args.length() == 3);
- CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, pattern, 1);
- CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]);
- RUNTIME_ASSERT(limit > 0);
-
- int subject_length = subject->length();
- int pattern_length = pattern->length();
- RUNTIME_ASSERT(pattern_length > 0);
-
- if (limit == 0xffffffffu) {
- Handle<Object> cached_answer(
- RegExpResultsCache::Lookup(isolate->heap(), *subject, *pattern,
- RegExpResultsCache::STRING_SPLIT_SUBSTRINGS),
- isolate);
- if (*cached_answer != Smi::FromInt(0)) {
- // The cache FixedArray is a COW-array and can therefore be reused.
- Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(
- Handle<FixedArray>::cast(cached_answer));
- return *result;
- }
- }
-
- // The limit can be very large (0xffffffffu), but since the pattern
- // isn't empty, we can never create more parts than ~half the length
- // of the subject.
-
- subject = String::Flatten(subject);
- pattern = String::Flatten(pattern);
-
- static const int kMaxInitialListCapacity = 16;
-
- ZoneScope zone_scope(isolate->runtime_zone());
-
- // Find (up to limit) indices of separator and end-of-string in subject
- int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit);
- ZoneList<int> indices(initial_capacity, zone_scope.zone());
-
- FindStringIndicesDispatch(isolate, *subject, *pattern, &indices, limit,
- zone_scope.zone());
-
- if (static_cast<uint32_t>(indices.length()) < limit) {
- indices.Add(subject_length, zone_scope.zone());
- }
-
- // The list indices now contains the end of each part to create.
-
- // Create JSArray of substrings separated by separator.
- int part_count = indices.length();
-
- Handle<JSArray> result = isolate->factory()->NewJSArray(part_count);
- JSObject::EnsureCanContainHeapObjectElements(result);
- result->set_length(Smi::FromInt(part_count));
-
- DCHECK(result->HasFastObjectElements());
-
- if (part_count == 1 && indices.at(0) == subject_length) {
- FixedArray::cast(result->elements())->set(0, *subject);
- return *result;
- }
-
- Handle<FixedArray> elements(FixedArray::cast(result->elements()));
- int part_start = 0;
- for (int i = 0; i < part_count; i++) {
- HandleScope local_loop_handle(isolate);
- int part_end = indices.at(i);
- Handle<String> substring =
- isolate->factory()->NewProperSubString(subject, part_start, part_end);
- elements->set(i, *substring);
- part_start = part_end + pattern_length;
- }
-
- if (limit == 0xffffffffu) {
- if (result->HasFastObjectElements()) {
- RegExpResultsCache::Enter(isolate, subject, pattern, elements,
- RegExpResultsCache::STRING_SPLIT_SUBSTRINGS);
- }
- }
-
- return *result;
-}
-
-
-// Copies Latin1 characters to the given fixed array looking up
-// one-char strings in the cache. Gives up on the first char that is
-// not in the cache and fills the remainder with smi zeros. Returns
-// the length of the successfully copied prefix.
-static int CopyCachedOneByteCharsToArray(Heap* heap, const uint8_t* chars,
- FixedArray* elements, int length) {
- DisallowHeapAllocation no_gc;
- FixedArray* one_byte_cache = heap->single_character_string_cache();
- Object* undefined = heap->undefined_value();
- int i;
- WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc);
- for (i = 0; i < length; ++i) {
- Object* value = one_byte_cache->get(chars[i]);
- if (value == undefined) break;
- elements->set(i, value, mode);
- }
- if (i < length) {
- DCHECK(Smi::FromInt(0) == 0);
- memset(elements->data_start() + i, 0, kPointerSize * (length - i));
- }
-#ifdef DEBUG
- for (int j = 0; j < length; ++j) {
- Object* element = elements->get(j);
- DCHECK(element == Smi::FromInt(0) ||
- (element->IsString() && String::cast(element)->LooksValid()));
+ // Slower case.
+ int flags = ALLOW_HEX;
+ if (FLAG_harmony_numeric_literals) {
+ // The current spec draft has not updated "ToNumber Applied to the String
+ // Type", https://bugs.ecmascript.org/show_bug.cgi?id=1584
+ flags |= ALLOW_OCTAL | ALLOW_BINARY;
}
-#endif
- return i;
+
+ return *isolate->factory()->NewNumber(
+ StringToDouble(isolate->unicode_cache(), *subject, flags));
}
-// Converts a String to JSArray.
-// For example, "foo" => ["f", "o", "o"].
-RUNTIME_FUNCTION(Runtime_StringToArray) {
- HandleScope scope(isolate);
+RUNTIME_FUNCTION(Runtime_StringParseInt) {
+ HandleScope handle_scope(isolate);
DCHECK(args.length() == 2);
- CONVERT_ARG_HANDLE_CHECKED(String, s, 0);
- CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]);
-
- s = String::Flatten(s);
- const int length = static_cast<int>(Min<uint32_t>(s->length(), limit));
+ CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
+ CONVERT_NUMBER_CHECKED(int, radix, Int32, args[1]);
+ RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36));
- Handle<FixedArray> elements;
- int position = 0;
- if (s->IsFlat() && s->IsOneByteRepresentation()) {
- // Try using cached chars where possible.
- elements = isolate->factory()->NewUninitializedFixedArray(length);
+ subject = String::Flatten(subject);
+ double value;
+ {
DisallowHeapAllocation no_gc;
- String::FlatContent content = s->GetFlatContent();
- if (content.IsOneByte()) {
- Vector<const uint8_t> chars = content.ToOneByteVector();
- // Note, this will initialize all elements (not only the prefix)
- // to prevent GC from seeing partially initialized array.
- position = CopyCachedOneByteCharsToArray(isolate->heap(), chars.start(),
- *elements, length);
+ String::FlatContent flat = subject->GetFlatContent();
+
+ // ECMA-262 section 15.1.2.3, empty string is NaN
+ if (flat.IsOneByte()) {
+ value =
+ StringToInt(isolate->unicode_cache(), flat.ToOneByteVector(), radix);
} else {
- MemsetPointer(elements->data_start(), isolate->heap()->undefined_value(),
- length);
+ value = StringToInt(isolate->unicode_cache(), flat.ToUC16Vector(), radix);
}
- } else {
- elements = isolate->factory()->NewFixedArray(length);
- }
- for (int i = position; i < length; ++i) {
- Handle<Object> str =
- isolate->factory()->LookupSingleCharacterStringFromCode(s->Get(i));
- elements->set(i, *str);
}
-#ifdef DEBUG
- for (int i = 0; i < length; ++i) {
- DCHECK(String::cast(elements->get(i))->length() == 1);
- }
-#endif
+ return *isolate->factory()->NewNumber(value);
+}
- return *isolate->factory()->NewJSArrayWithElements(elements);
+
+RUNTIME_FUNCTION(Runtime_StringParseFloat) {
+ HandleScope shs(isolate);
+ DCHECK(args.length() == 1);
+ CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
+
+ subject = String::Flatten(subject);
+ double value = StringToDouble(isolate->unicode_cache(), *subject,
+ ALLOW_TRAILING_JUNK, base::OS::nan_value());
+
+ return *isolate->factory()->NewNumber(value);
}
}
-bool Runtime::IsUpperCaseChar(RuntimeState* runtime_state, uint16_t ch) {
- unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth];
- int char_length = runtime_state->to_upper_mapping()->get(ch, 0, chars);
- return char_length == 0;
-}
-
-
RUNTIME_FUNCTION(Runtime_NumberToStringRT) {
HandleScope scope(isolate);
DCHECK(args.length() == 1);
}
-RUNTIME_FUNCTION(Runtime_StringAdd) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 2);
- CONVERT_ARG_HANDLE_CHECKED(String, str1, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, str2, 1);
- isolate->counters()->string_add_runtime()->Increment();
- Handle<String> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, result, isolate->factory()->NewConsString(str1, str2));
- return *result;
-}
-
-
-template <typename sinkchar>
-static inline void StringBuilderConcatHelper(String* special, sinkchar* sink,
- FixedArray* fixed_array,
- int array_length) {
- DisallowHeapAllocation no_gc;
- int position = 0;
- for (int i = 0; i < array_length; i++) {
- Object* element = fixed_array->get(i);
- if (element->IsSmi()) {
- // Smi encoding of position and length.
- int encoded_slice = Smi::cast(element)->value();
- int pos;
- int len;
- if (encoded_slice > 0) {
- // Position and length encoded in one smi.
- pos = StringBuilderSubstringPosition::decode(encoded_slice);
- len = StringBuilderSubstringLength::decode(encoded_slice);
- } else {
- // Position and length encoded in two smis.
- Object* obj = fixed_array->get(++i);
- DCHECK(obj->IsSmi());
- pos = Smi::cast(obj)->value();
- len = -encoded_slice;
- }
- String::WriteToFlat(special, sink + position, pos, pos + len);
- position += len;
- } else {
- String* string = String::cast(element);
- int element_length = string->length();
- String::WriteToFlat(string, sink + position, 0, element_length);
- position += element_length;
- }
- }
-}
-
-
-// Returns the result length of the concatenation.
-// On illegal argument, -1 is returned.
-static inline int StringBuilderConcatLength(int special_length,
- FixedArray* fixed_array,
- int array_length, bool* one_byte) {
- DisallowHeapAllocation no_gc;
- int position = 0;
- for (int i = 0; i < array_length; i++) {
- int increment = 0;
- Object* elt = fixed_array->get(i);
- if (elt->IsSmi()) {
- // Smi encoding of position and length.
- int smi_value = Smi::cast(elt)->value();
- int pos;
- int len;
- if (smi_value > 0) {
- // Position and length encoded in one smi.
- pos = StringBuilderSubstringPosition::decode(smi_value);
- len = StringBuilderSubstringLength::decode(smi_value);
- } else {
- // Position and length encoded in two smis.
- len = -smi_value;
- // Get the position and check that it is a positive smi.
- i++;
- if (i >= array_length) return -1;
- Object* next_smi = fixed_array->get(i);
- if (!next_smi->IsSmi()) return -1;
- pos = Smi::cast(next_smi)->value();
- if (pos < 0) return -1;
- }
- DCHECK(pos >= 0);
- DCHECK(len >= 0);
- if (pos > special_length || len > special_length - pos) return -1;
- increment = len;
- } else if (elt->IsString()) {
- String* element = String::cast(elt);
- int element_length = element->length();
- increment = element_length;
- if (*one_byte && !element->HasOnlyOneByteChars()) {
- *one_byte = false;
- }
- } else {
- return -1;
- }
- if (increment > String::kMaxLength - position) {
- return kMaxInt; // Provoke throw on allocation.
- }
- position += increment;
- }
- return position;
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringBuilderConcat) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 3);
- CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0);
- int32_t array_length;
- if (!args[1]->ToInt32(&array_length)) {
- THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
- }
- CONVERT_ARG_HANDLE_CHECKED(String, special, 2);
-
- size_t actual_array_length = 0;
- RUNTIME_ASSERT(
- TryNumberToSize(isolate, array->length(), &actual_array_length));
- RUNTIME_ASSERT(array_length >= 0);
- RUNTIME_ASSERT(static_cast<size_t>(array_length) <= actual_array_length);
-
- // This assumption is used by the slice encoding in one or two smis.
- DCHECK(Smi::kMaxValue >= String::kMaxLength);
-
- RUNTIME_ASSERT(array->HasFastElements());
- JSObject::EnsureCanContainHeapObjectElements(array);
-
- int special_length = special->length();
- if (!array->HasFastObjectElements()) {
- return isolate->Throw(isolate->heap()->illegal_argument_string());
- }
-
- int length;
- bool one_byte = special->HasOnlyOneByteChars();
-
- {
- DisallowHeapAllocation no_gc;
- FixedArray* fixed_array = FixedArray::cast(array->elements());
- if (fixed_array->length() < array_length) {
- array_length = fixed_array->length();
- }
-
- if (array_length == 0) {
- return isolate->heap()->empty_string();
- } else if (array_length == 1) {
- Object* first = fixed_array->get(0);
- if (first->IsString()) return first;
- }
- length = StringBuilderConcatLength(special_length, fixed_array,
- array_length, &one_byte);
- }
-
- if (length == -1) {
- return isolate->Throw(isolate->heap()->illegal_argument_string());
- }
-
- if (one_byte) {
- Handle<SeqOneByteString> answer;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, answer, isolate->factory()->NewRawOneByteString(length));
- StringBuilderConcatHelper(*special, answer->GetChars(),
- FixedArray::cast(array->elements()),
- array_length);
- return *answer;
- } else {
- Handle<SeqTwoByteString> answer;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, answer, isolate->factory()->NewRawTwoByteString(length));
- StringBuilderConcatHelper(*special, answer->GetChars(),
- FixedArray::cast(array->elements()),
- array_length);
- return *answer;
- }
-}
-
-
-RUNTIME_FUNCTION(Runtime_StringBuilderJoin) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 3);
- CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0);
- int32_t array_length;
- if (!args[1]->ToInt32(&array_length)) {
- THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
- }
- CONVERT_ARG_HANDLE_CHECKED(String, separator, 2);
- RUNTIME_ASSERT(array->HasFastObjectElements());
- RUNTIME_ASSERT(array_length >= 0);
-
- Handle<FixedArray> fixed_array(FixedArray::cast(array->elements()));
- if (fixed_array->length() < array_length) {
- array_length = fixed_array->length();
- }
-
- if (array_length == 0) {
- return isolate->heap()->empty_string();
- } else if (array_length == 1) {
- Object* first = fixed_array->get(0);
- RUNTIME_ASSERT(first->IsString());
- return first;
- }
-
- int separator_length = separator->length();
- RUNTIME_ASSERT(separator_length > 0);
- int max_nof_separators =
- (String::kMaxLength + separator_length - 1) / separator_length;
- if (max_nof_separators < (array_length - 1)) {
- THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
- }
- int length = (array_length - 1) * separator_length;
- for (int i = 0; i < array_length; i++) {
- Object* element_obj = fixed_array->get(i);
- RUNTIME_ASSERT(element_obj->IsString());
- String* element = String::cast(element_obj);
- int increment = element->length();
- if (increment > String::kMaxLength - length) {
- STATIC_ASSERT(String::kMaxLength < kMaxInt);
- length = kMaxInt; // Provoke exception;
- break;
- }
- length += increment;
- }
-
- Handle<SeqTwoByteString> answer;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
- isolate, answer, isolate->factory()->NewRawTwoByteString(length));
-
- DisallowHeapAllocation no_gc;
-
- uc16* sink = answer->GetChars();
-#ifdef DEBUG
- uc16* end = sink + length;
-#endif
-
- RUNTIME_ASSERT(fixed_array->get(0)->IsString());
- String* first = String::cast(fixed_array->get(0));
- String* separator_raw = *separator;
- int first_length = first->length();
- String::WriteToFlat(first, sink, 0, first_length);
- sink += first_length;
-
- for (int i = 1; i < array_length; i++) {
- DCHECK(sink + separator_length <= end);
- String::WriteToFlat(separator_raw, sink, 0, separator_length);
- sink += separator_length;
-
- RUNTIME_ASSERT(fixed_array->get(i)->IsString());
- String* element = String::cast(fixed_array->get(i));
- int element_length = element->length();
- DCHECK(sink + element_length <= end);
- String::WriteToFlat(element, sink, 0, element_length);
- sink += element_length;
- }
- DCHECK(sink == end);
-
- // Use %_FastOneByteArrayJoin instead.
- DCHECK(!answer->IsOneByteRepresentation());
- return *answer;
-}
-
-template <typename Char>
-static void JoinSparseArrayWithSeparator(FixedArray* elements,
- int elements_length,
- uint32_t array_length,
- String* separator,
- Vector<Char> buffer) {
- DisallowHeapAllocation no_gc;
- int previous_separator_position = 0;
- int separator_length = separator->length();
- int cursor = 0;
- for (int i = 0; i < elements_length; i += 2) {
- int position = NumberToInt32(elements->get(i));
- String* string = String::cast(elements->get(i + 1));
- int string_length = string->length();
- if (string->length() > 0) {
- while (previous_separator_position < position) {
- String::WriteToFlat<Char>(separator, &buffer[cursor], 0,
- separator_length);
- cursor += separator_length;
- previous_separator_position++;
- }
- String::WriteToFlat<Char>(string, &buffer[cursor], 0, string_length);
- cursor += string->length();
- }
- }
- if (separator_length > 0) {
- // Array length must be representable as a signed 32-bit number,
- // otherwise the total string length would have been too large.
- DCHECK(array_length <= 0x7fffffff); // Is int32_t.
- int last_array_index = static_cast<int>(array_length - 1);
- while (previous_separator_position < last_array_index) {
- String::WriteToFlat<Char>(separator, &buffer[cursor], 0,
- separator_length);
- cursor += separator_length;
- previous_separator_position++;
- }
- }
- DCHECK(cursor <= buffer.length());
-}
-
-
-RUNTIME_FUNCTION(Runtime_SparseJoinWithSeparator) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 3);
- CONVERT_ARG_HANDLE_CHECKED(JSArray, elements_array, 0);
- CONVERT_NUMBER_CHECKED(uint32_t, array_length, Uint32, args[1]);
- CONVERT_ARG_HANDLE_CHECKED(String, separator, 2);
- // elements_array is fast-mode JSarray of alternating positions
- // (increasing order) and strings.
- RUNTIME_ASSERT(elements_array->HasFastSmiOrObjectElements());
- // array_length is length of original array (used to add separators);
- // separator is string to put between elements. Assumed to be non-empty.
- RUNTIME_ASSERT(array_length > 0);
-
- // Find total length of join result.
- int string_length = 0;
- bool is_one_byte = separator->IsOneByteRepresentation();
- bool overflow = false;
- CONVERT_NUMBER_CHECKED(int, elements_length, Int32, elements_array->length());
- RUNTIME_ASSERT(elements_length <= elements_array->elements()->length());
- RUNTIME_ASSERT((elements_length & 1) == 0); // Even length.
- FixedArray* elements = FixedArray::cast(elements_array->elements());
- for (int i = 0; i < elements_length; i += 2) {
- RUNTIME_ASSERT(elements->get(i)->IsNumber());
- CONVERT_NUMBER_CHECKED(uint32_t, position, Uint32, elements->get(i));
- RUNTIME_ASSERT(position < array_length);
- RUNTIME_ASSERT(elements->get(i + 1)->IsString());
- }
-
- {
- DisallowHeapAllocation no_gc;
- for (int i = 0; i < elements_length; i += 2) {
- String* string = String::cast(elements->get(i + 1));
- int length = string->length();
- if (is_one_byte && !string->IsOneByteRepresentation()) {
- is_one_byte = false;
- }
- if (length > String::kMaxLength ||
- String::kMaxLength - length < string_length) {
- overflow = true;
- break;
- }
- string_length += length;
- }
- }
-
- int separator_length = separator->length();
- if (!overflow && separator_length > 0) {
- if (array_length <= 0x7fffffffu) {
- int separator_count = static_cast<int>(array_length) - 1;
- int remaining_length = String::kMaxLength - string_length;
- if ((remaining_length / separator_length) >= separator_count) {
- string_length += separator_length * (array_length - 1);
- } else {
- // Not room for the separators within the maximal string length.
- overflow = true;
- }
- } else {
- // Nonempty separator and at least 2^31-1 separators necessary
- // means that the string is too large to create.
- STATIC_ASSERT(String::kMaxLength < 0x7fffffff);
- overflow = true;
- }
- }
- if (overflow) {
- // Throw an exception if the resulting string is too large. See
- // https://code.google.com/p/chromium/issues/detail?id=336820
- // for details.
- THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewInvalidStringLengthError());
- }
-
- if (is_one_byte) {
- Handle<SeqOneByteString> result = isolate->factory()
- ->NewRawOneByteString(string_length)
- .ToHandleChecked();
- JoinSparseArrayWithSeparator<uint8_t>(
- FixedArray::cast(elements_array->elements()), elements_length,
- array_length, *separator,
- Vector<uint8_t>(result->GetChars(), string_length));
- return *result;
- } else {
- Handle<SeqTwoByteString> result = isolate->factory()
- ->NewRawTwoByteString(string_length)
- .ToHandleChecked();
- JoinSparseArrayWithSeparator<uc16>(
- FixedArray::cast(elements_array->elements()), elements_length,
- array_length, *separator,
- Vector<uc16>(result->GetChars(), string_length));
- return *result;
- }
-}
-
-
RUNTIME_FUNCTION(Runtime_NumberOr) {
HandleScope scope(isolate);
DCHECK(args.length() == 2);
}
-RUNTIME_FUNCTION(Runtime_StringEquals) {
- HandleScope handle_scope(isolate);
- DCHECK(args.length() == 2);
-
- CONVERT_ARG_HANDLE_CHECKED(String, x, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, y, 1);
-
- bool not_equal = !String::Equals(x, y);
- // This is slightly convoluted because the value that signifies
- // equality is 0 and inequality is 1 so we have to negate the result
- // from String::Equals.
- DCHECK(not_equal == 0 || not_equal == 1);
- STATIC_ASSERT(EQUAL == 0);
- STATIC_ASSERT(NOT_EQUAL == 1);
- return Smi::FromInt(not_equal);
-}
-
-
RUNTIME_FUNCTION(Runtime_NumberCompare) {
SealHandleScope shs(isolate);
DCHECK(args.length() == 3);
}
-RUNTIME_FUNCTION(Runtime_StringCompare) {
- HandleScope handle_scope(isolate);
- DCHECK(args.length() == 2);
-
- CONVERT_ARG_HANDLE_CHECKED(String, x, 0);
- CONVERT_ARG_HANDLE_CHECKED(String, y, 1);
-
- isolate->counters()->string_compare_runtime()->Increment();
-
- // A few fast case tests before we flatten.
- if (x.is_identical_to(y)) return Smi::FromInt(EQUAL);
- if (y->length() == 0) {
- if (x->length() == 0) return Smi::FromInt(EQUAL);
- return Smi::FromInt(GREATER);
- } else if (x->length() == 0) {
- return Smi::FromInt(LESS);
- }
-
- int d = x->Get(0) - y->Get(0);
- if (d < 0)
- return Smi::FromInt(LESS);
- else if (d > 0)
- return Smi::FromInt(GREATER);
-
- // Slow case.
- x = String::Flatten(x);
- y = String::Flatten(y);
- DisallowHeapAllocation no_gc;
- Object* equal_prefix_result = Smi::FromInt(EQUAL);
- int prefix_length = x->length();
- if (y->length() < prefix_length) {
- prefix_length = y->length();
- equal_prefix_result = Smi::FromInt(GREATER);
- } else if (y->length() > prefix_length) {
- equal_prefix_result = Smi::FromInt(LESS);
- }
- int r;
- String::FlatContent x_content = x->GetFlatContent();
- String::FlatContent y_content = y->GetFlatContent();
- if (x_content.IsOneByte()) {
- Vector<const uint8_t> x_chars = x_content.ToOneByteVector();
- if (y_content.IsOneByte()) {
- Vector<const uint8_t> y_chars = y_content.ToOneByteVector();
- r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
- } else {
- Vector<const uc16> y_chars = y_content.ToUC16Vector();
- r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
- }
- } else {
- Vector<const uc16> x_chars = x_content.ToUC16Vector();
- if (y_content.IsOneByte()) {
- Vector<const uint8_t> y_chars = y_content.ToOneByteVector();
- r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
- } else {
- Vector<const uc16> y_chars = y_content.ToUC16Vector();
- r = CompareChars(x_chars.start(), y_chars.start(), prefix_length);
- }
- }
- Object* result;
- if (r == 0) {
- result = equal_prefix_result;
- } else {
- result = (r < 0) ? Smi::FromInt(LESS) : Smi::FromInt(GREATER);
- }
- return result;
-}
#define RUNTIME_UNARY_MATH(Name, name) \
}
-RUNTIME_FUNCTION(Runtime_ParseJson) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 1);
- CONVERT_ARG_HANDLE_CHECKED(String, source, 0);
-
- source = String::Flatten(source);
- // Optimized fast case where we only have Latin1 characters.
- Handle<Object> result;
- ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
- source->IsSeqOneByteString()
- ? JsonParser<true>::Parse(source)
- : JsonParser<false>::Parse(source));
- return *result;
-}
-
-
bool CodeGenerationFromStringsAllowed(Isolate* isolate,
Handle<Context> context) {
DCHECK(context->allow_code_gen_from_strings()->IsFalse());
}
-RUNTIME_FUNCTION(RuntimeReference_StringCharFromCode) {
- SealHandleScope shs(isolate);
- return __RT_impl_Runtime_CharFromCode(args, isolate);
-}
-
-
-RUNTIME_FUNCTION(RuntimeReference_StringCharAt) {
- SealHandleScope shs(isolate);
- DCHECK(args.length() == 2);
- if (!args[0]->IsString()) return Smi::FromInt(0);
- if (!args[1]->IsNumber()) return Smi::FromInt(0);
- if (std::isinf(args.number_at(1))) return isolate->heap()->empty_string();
- Object* code = __RT_impl_Runtime_StringCharCodeAtRT(args, isolate);
- if (code->IsNaN()) return isolate->heap()->empty_string();
- return __RT_impl_Runtime_CharFromCode(Arguments(1, &code), isolate);
-}
-
-
-RUNTIME_FUNCTION(RuntimeReference_OneByteSeqStringSetChar) {
- SealHandleScope shs(isolate);
- DCHECK(args.length() == 3);
- CONVERT_INT32_ARG_CHECKED(index, 0);
- CONVERT_INT32_ARG_CHECKED(value, 1);
- CONVERT_ARG_CHECKED(SeqOneByteString, string, 2);
- string->SeqOneByteStringSet(index, value);
- return string;
-}
-
-
-RUNTIME_FUNCTION(RuntimeReference_TwoByteSeqStringSetChar) {
- SealHandleScope shs(isolate);
- DCHECK(args.length() == 3);
- CONVERT_INT32_ARG_CHECKED(index, 0);
- CONVERT_INT32_ARG_CHECKED(value, 1);
- CONVERT_ARG_CHECKED(SeqTwoByteString, string, 2);
- string->SeqTwoByteStringSet(index, value);
- return string;
-}
-
-
RUNTIME_FUNCTION(RuntimeReference_ObjectEquals) {
SealHandleScope shs(isolate);
DCHECK(args.length() == 2);
}
-RUNTIME_FUNCTION(RuntimeReference_StringCharCodeAt) {
- SealHandleScope shs(isolate);
- DCHECK(args.length() == 2);
- if (!args[0]->IsString()) return isolate->heap()->undefined_value();
- if (!args[1]->IsNumber()) return isolate->heap()->undefined_value();
- if (std::isinf(args.number_at(1))) return isolate->heap()->nan_value();
- return __RT_impl_Runtime_StringCharCodeAtRT(args, isolate);
-}
-
-
-RUNTIME_FUNCTION(RuntimeReference_StringAdd) {
- SealHandleScope shs(isolate);
- return __RT_impl_Runtime_StringAdd(args, isolate);
-}
-
-
-RUNTIME_FUNCTION(RuntimeReference_SubString) {
- SealHandleScope shs(isolate);
- return __RT_impl_Runtime_SubString(args, isolate);
-}
-
-
-RUNTIME_FUNCTION(RuntimeReference_StringCompare) {
- SealHandleScope shs(isolate);
- return __RT_impl_Runtime_StringCompare(args, isolate);
-}
-
-
-RUNTIME_FUNCTION(RuntimeReference_RegExpExec) {
- SealHandleScope shs(isolate);
- return __RT_impl_Runtime_RegExpExecRT(args, isolate);
-}
-
-
-RUNTIME_FUNCTION(RuntimeReference_RegExpConstructResult) {
- SealHandleScope shs(isolate);
- return __RT_impl_Runtime_RegExpConstructResult(args, isolate);
-}
-
-
RUNTIME_FUNCTION(RuntimeReference_GetFromCache) {
HandleScope scope(isolate);
DCHECK(args.length() == 2);
static int StringMatch(Isolate* isolate, Handle<String> sub,
Handle<String> pat, int index);
- static bool IsUpperCaseChar(RuntimeState* runtime_state, uint16_t ch);
-
// TODO(1240886): Some of the following methods are *not* handle safe, but
// accept handle arguments. This seems fragile.
--- /dev/null
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef V8_STRING_BUILDER_H_
+#define V8_STRING_BUILDER_H_
+
+namespace v8 {
+namespace internal {
+
+const int kStringBuilderConcatHelperLengthBits = 11;
+const int kStringBuilderConcatHelperPositionBits = 19;
+
+typedef BitField<int, 0, kStringBuilderConcatHelperLengthBits>
+ StringBuilderSubstringLength;
+typedef BitField<int, kStringBuilderConcatHelperLengthBits,
+ kStringBuilderConcatHelperPositionBits>
+ StringBuilderSubstringPosition;
+
+
+template <typename sinkchar>
+static inline void StringBuilderConcatHelper(String* special, sinkchar* sink,
+ FixedArray* fixed_array,
+ int array_length) {
+ DisallowHeapAllocation no_gc;
+ int position = 0;
+ for (int i = 0; i < array_length; i++) {
+ Object* element = fixed_array->get(i);
+ if (element->IsSmi()) {
+ // Smi encoding of position and length.
+ int encoded_slice = Smi::cast(element)->value();
+ int pos;
+ int len;
+ if (encoded_slice > 0) {
+ // Position and length encoded in one smi.
+ pos = StringBuilderSubstringPosition::decode(encoded_slice);
+ len = StringBuilderSubstringLength::decode(encoded_slice);
+ } else {
+ // Position and length encoded in two smis.
+ Object* obj = fixed_array->get(++i);
+ DCHECK(obj->IsSmi());
+ pos = Smi::cast(obj)->value();
+ len = -encoded_slice;
+ }
+ String::WriteToFlat(special, sink + position, pos, pos + len);
+ position += len;
+ } else {
+ String* string = String::cast(element);
+ int element_length = string->length();
+ String::WriteToFlat(string, sink + position, 0, element_length);
+ position += element_length;
+ }
+ }
+}
+
+
+// Returns the result length of the concatenation.
+// On illegal argument, -1 is returned.
+static inline int StringBuilderConcatLength(int special_length,
+ FixedArray* fixed_array,
+ int array_length, bool* one_byte) {
+ DisallowHeapAllocation no_gc;
+ int position = 0;
+ for (int i = 0; i < array_length; i++) {
+ int increment = 0;
+ Object* elt = fixed_array->get(i);
+ if (elt->IsSmi()) {
+ // Smi encoding of position and length.
+ int smi_value = Smi::cast(elt)->value();
+ int pos;
+ int len;
+ if (smi_value > 0) {
+ // Position and length encoded in one smi.
+ pos = StringBuilderSubstringPosition::decode(smi_value);
+ len = StringBuilderSubstringLength::decode(smi_value);
+ } else {
+ // Position and length encoded in two smis.
+ len = -smi_value;
+ // Get the position and check that it is a positive smi.
+ i++;
+ if (i >= array_length) return -1;
+ Object* next_smi = fixed_array->get(i);
+ if (!next_smi->IsSmi()) return -1;
+ pos = Smi::cast(next_smi)->value();
+ if (pos < 0) return -1;
+ }
+ DCHECK(pos >= 0);
+ DCHECK(len >= 0);
+ if (pos > special_length || len > special_length - pos) return -1;
+ increment = len;
+ } else if (elt->IsString()) {
+ String* element = String::cast(elt);
+ int element_length = element->length();
+ increment = element_length;
+ if (*one_byte && !element->HasOnlyOneByteChars()) {
+ *one_byte = false;
+ }
+ } else {
+ return -1;
+ }
+ if (increment > String::kMaxLength - position) {
+ return kMaxInt; // Provoke throw on allocation.
+ }
+ position += increment;
+ }
+ return position;
+}
+
+
+class FixedArrayBuilder {
+ public:
+ explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity)
+ : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)),
+ length_(0),
+ has_non_smi_elements_(false) {
+ // Require a non-zero initial size. Ensures that doubling the size to
+ // extend the array will work.
+ DCHECK(initial_capacity > 0);
+ }
+
+ explicit FixedArrayBuilder(Handle<FixedArray> backing_store)
+ : array_(backing_store), length_(0), has_non_smi_elements_(false) {
+ // Require a non-zero initial size. Ensures that doubling the size to
+ // extend the array will work.
+ DCHECK(backing_store->length() > 0);
+ }
+
+ bool HasCapacity(int elements) {
+ int length = array_->length();
+ int required_length = length_ + elements;
+ return (length >= required_length);
+ }
+
+ void EnsureCapacity(int elements) {
+ int length = array_->length();
+ int required_length = length_ + elements;
+ if (length < required_length) {
+ int new_length = length;
+ do {
+ new_length *= 2;
+ } while (new_length < required_length);
+ Handle<FixedArray> extended_array =
+ array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length);
+ array_->CopyTo(0, *extended_array, 0, length_);
+ array_ = extended_array;
+ }
+ }
+
+ void Add(Object* value) {
+ DCHECK(!value->IsSmi());
+ DCHECK(length_ < capacity());
+ array_->set(length_, value);
+ length_++;
+ has_non_smi_elements_ = true;
+ }
+
+ void Add(Smi* value) {
+ DCHECK(value->IsSmi());
+ DCHECK(length_ < capacity());
+ array_->set(length_, value);
+ length_++;
+ }
+
+ Handle<FixedArray> array() { return array_; }
+
+ int length() { return length_; }
+
+ int capacity() { return array_->length(); }
+
+ Handle<JSArray> ToJSArray(Handle<JSArray> target_array) {
+ JSArray::SetContent(target_array, array_);
+ target_array->set_length(Smi::FromInt(length_));
+ return target_array;
+ }
+
+
+ private:
+ Handle<FixedArray> array_;
+ int length_;
+ bool has_non_smi_elements_;
+};
+
+
+class ReplacementStringBuilder {
+ public:
+ ReplacementStringBuilder(Heap* heap, Handle<String> subject,
+ int estimated_part_count)
+ : heap_(heap),
+ array_builder_(heap->isolate(), estimated_part_count),
+ subject_(subject),
+ character_count_(0),
+ is_one_byte_(subject->IsOneByteRepresentation()) {
+ // Require a non-zero initial size. Ensures that doubling the size to
+ // extend the array will work.
+ DCHECK(estimated_part_count > 0);
+ }
+
+ static inline void AddSubjectSlice(FixedArrayBuilder* builder, int from,
+ int to) {
+ DCHECK(from >= 0);
+ int length = to - from;
+ DCHECK(length > 0);
+ if (StringBuilderSubstringLength::is_valid(length) &&
+ StringBuilderSubstringPosition::is_valid(from)) {
+ int encoded_slice = StringBuilderSubstringLength::encode(length) |
+ StringBuilderSubstringPosition::encode(from);
+ builder->Add(Smi::FromInt(encoded_slice));
+ } else {
+ // Otherwise encode as two smis.
+ builder->Add(Smi::FromInt(-length));
+ builder->Add(Smi::FromInt(from));
+ }
+ }
+
+
+ void EnsureCapacity(int elements) { array_builder_.EnsureCapacity(elements); }
+
+
+ void AddSubjectSlice(int from, int to) {
+ AddSubjectSlice(&array_builder_, from, to);
+ IncrementCharacterCount(to - from);
+ }
+
+
+ void AddString(Handle<String> string) {
+ int length = string->length();
+ DCHECK(length > 0);
+ AddElement(*string);
+ if (!string->IsOneByteRepresentation()) {
+ is_one_byte_ = false;
+ }
+ IncrementCharacterCount(length);
+ }
+
+
+ MaybeHandle<String> ToString() {
+ Isolate* isolate = heap_->isolate();
+ if (array_builder_.length() == 0) {
+ return isolate->factory()->empty_string();
+ }
+
+ Handle<String> joined_string;
+ if (is_one_byte_) {
+ Handle<SeqOneByteString> seq;
+ ASSIGN_RETURN_ON_EXCEPTION(
+ isolate, seq,
+ isolate->factory()->NewRawOneByteString(character_count_), String);
+
+ DisallowHeapAllocation no_gc;
+ uint8_t* char_buffer = seq->GetChars();
+ StringBuilderConcatHelper(*subject_, char_buffer, *array_builder_.array(),
+ array_builder_.length());
+ joined_string = Handle<String>::cast(seq);
+ } else {
+ // Two-byte.
+ Handle<SeqTwoByteString> seq;
+ ASSIGN_RETURN_ON_EXCEPTION(
+ isolate, seq,
+ isolate->factory()->NewRawTwoByteString(character_count_), String);
+
+ DisallowHeapAllocation no_gc;
+ uc16* char_buffer = seq->GetChars();
+ StringBuilderConcatHelper(*subject_, char_buffer, *array_builder_.array(),
+ array_builder_.length());
+ joined_string = Handle<String>::cast(seq);
+ }
+ return joined_string;
+ }
+
+
+ void IncrementCharacterCount(int by) {
+ if (character_count_ > String::kMaxLength - by) {
+ STATIC_ASSERT(String::kMaxLength < kMaxInt);
+ character_count_ = kMaxInt;
+ } else {
+ character_count_ += by;
+ }
+ }
+
+ private:
+ void AddElement(Object* element) {
+ DCHECK(element->IsSmi() || element->IsString());
+ DCHECK(array_builder_.capacity() > array_builder_.length());
+ array_builder_.Add(element);
+ }
+
+ Heap* heap_;
+ FixedArrayBuilder array_builder_;
+ Handle<String> subject_;
+ int character_count_;
+ bool is_one_byte_;
+};
+}
+} // namespace v8::internal
+
+#endif // V8_STRING_BUILDER_H_
'../../src/runtime-profiler.cc',
'../../src/runtime-profiler.h',
'../../src/runtime/runtime-i18n.cc',
+ '../../src/runtime/runtime-json.cc',
+ '../../src/runtime/runtime-regexp.cc',
+ '../../src/runtime/runtime-strings.cc',
+ '../../src/runtime/runtime-uri.cc',
'../../src/runtime/runtime-utils.h',
'../../src/runtime/runtime.cc',
'../../src/runtime/runtime.h',
+ '../../src/runtime/string-builder.h',
'../../src/safepoint-table.cc',
'../../src/safepoint-table.h',
'../../src/sampler.cc',