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

Side by Side Diff: src/api.cc

Issue 1507823002: Improve style of V8 API code (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: remove unused <string> include Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« include/v8.h ('K') | « include/v8.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/api.h" 5 #include "src/api.h"
6 6
7 #include <string.h> // For memcpy, strlen. 7 #include <string.h> // For memcpy, strlen.
8 #ifdef V8_USE_ADDRESS_SANITIZER 8 #ifdef V8_USE_ADDRESS_SANITIZER
9 #include <sanitizer/asan_interface.h> 9 #include <sanitizer/asan_interface.h>
10 #endif // V8_USE_ADDRESS_SANITIZER 10 #endif // V8_USE_ADDRESS_SANITIZER
11 #include <cmath> // For isnan. 11 #include <cmath> // For isnan.
12 #include <limits>
13 #include <vector>
12 #include "include/v8-debug.h" 14 #include "include/v8-debug.h"
13 #include "include/v8-profiler.h" 15 #include "include/v8-profiler.h"
14 #include "include/v8-testing.h" 16 #include "include/v8-testing.h"
15 #include "src/api-natives.h" 17 #include "src/api-natives.h"
16 #include "src/assert-scope.h" 18 #include "src/assert-scope.h"
17 #include "src/background-parsing-task.h" 19 #include "src/background-parsing-task.h"
18 #include "src/base/functional.h" 20 #include "src/base/functional.h"
19 #include "src/base/platform/platform.h" 21 #include "src/base/platform/platform.h"
20 #include "src/base/platform/time.h" 22 #include "src/base/platform/time.h"
21 #include "src/base/utils/random-number-generator.h" 23 #include "src/base/utils/random-number-generator.h"
(...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 obj_.set(0, i::Smi::FromInt(0)); 877 obj_.set(0, i::Smi::FromInt(0));
876 } 878 }
877 879
878 880
879 int NeanderArray::length() { 881 int NeanderArray::length() {
880 return i::Smi::cast(obj_.get(0))->value(); 882 return i::Smi::cast(obj_.get(0))->value();
881 } 883 }
882 884
883 885
884 i::Object* NeanderArray::get(int offset) { 886 i::Object* NeanderArray::get(int offset) {
885 DCHECK(0 <= offset); 887 DCHECK_LE(0, offset);
886 DCHECK(offset < length()); 888 DCHECK_LT(offset, length());
887 return obj_.get(offset + 1); 889 return obj_.get(offset + 1);
888 } 890 }
889 891
890 892
891 // This method cannot easily return an error value, therefore it is necessary 893 // This method cannot easily return an error value, therefore it is necessary
892 // to check for a dead VM with ON_BAILOUT before calling it. To remind you 894 // to check for a dead VM with ON_BAILOUT before calling it. To remind you
893 // about this there is no HandleScope in this method. When you add one to the 895 // about this there is no HandleScope in this method. When you add one to the
894 // site calling this method you should check that you ensured the VM was not 896 // site calling this method you should check that you ensured the VM was not
895 // dead first. 897 // dead first.
896 void NeanderArray::add(i::Isolate* isolate, i::Handle<i::Object> value) { 898 void NeanderArray::add(i::Isolate* isolate, i::Handle<i::Object> value) {
(...skipping 3929 matching lines...) Expand 10 before | Expand all | Expand 10 after
4826 (state & kRightmostEdgeIsCalculated)); 4828 (state & kRightmostEdgeIsCalculated));
4827 if (EndsWithSurrogate(state) && StartsWithSurrogate(state)) { 4829 if (EndsWithSurrogate(state) && StartsWithSurrogate(state)) {
4828 *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates; 4830 *length -= unibrow::Utf8::kBytesSavedByCombiningSurrogates;
4829 } 4831 }
4830 *state_out = kInitialState | 4832 *state_out = kInitialState |
4831 (state & kLeftmostEdgeIsSurrogate ? kStartsWithTrailingSurrogate : 0) | 4833 (state & kLeftmostEdgeIsSurrogate ? kStartsWithTrailingSurrogate : 0) |
4832 (state & kRightmostEdgeIsSurrogate ? kEndsWithLeadingSurrogate : 0); 4834 (state & kRightmostEdgeIsSurrogate ? kEndsWithLeadingSurrogate : 0);
4833 } 4835 }
4834 4836
4835 static int Calculate(i::ConsString* current, uint8_t* state_out) { 4837 static int Calculate(i::ConsString* current, uint8_t* state_out) {
4836 using namespace internal; 4838 using internal::ConsString;
4837 int total_length = 0; 4839 int total_length = 0;
4838 uint8_t state = kInitialState; 4840 uint8_t state = kInitialState;
4839 while (true) { 4841 while (true) {
4840 i::String* left = current->first(); 4842 i::String* left = current->first();
4841 i::String* right = current->second(); 4843 i::String* right = current->second();
4842 uint8_t right_leaf_state; 4844 uint8_t right_leaf_state;
4843 uint8_t left_leaf_state; 4845 uint8_t left_leaf_state;
4844 int leaf_length; 4846 int leaf_length;
4845 ConsString* left_as_cons = 4847 ConsString* left_as_cons =
4846 Visitor::VisitFlat(left, &leaf_length, &left_leaf_state); 4848 Visitor::VisitFlat(left, &leaf_length, &left_leaf_state);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
4926 skip_capacity_check_(capacity == -1 || skip_capacity_check), 4928 skip_capacity_check_(capacity == -1 || skip_capacity_check),
4927 replace_invalid_utf8_(replace_invalid_utf8), 4929 replace_invalid_utf8_(replace_invalid_utf8),
4928 utf16_chars_read_(0) { 4930 utf16_chars_read_(0) {
4929 } 4931 }
4930 4932
4931 static int WriteEndCharacter(uint16_t character, 4933 static int WriteEndCharacter(uint16_t character,
4932 int last_character, 4934 int last_character,
4933 int remaining, 4935 int remaining,
4934 char* const buffer, 4936 char* const buffer,
4935 bool replace_invalid_utf8) { 4937 bool replace_invalid_utf8) {
4936 using namespace unibrow; 4938 DCHECK_GT(remaining, 0);
4937 DCHECK(remaining > 0);
4938 // We can't use a local buffer here because Encode needs to modify 4939 // We can't use a local buffer here because Encode needs to modify
4939 // previous characters in the stream. We know, however, that 4940 // previous characters in the stream. We know, however, that
4940 // exactly one character will be advanced. 4941 // exactly one character will be advanced.
4941 if (Utf16::IsSurrogatePair(last_character, character)) { 4942 if (unibrow::Utf16::IsSurrogatePair(last_character, character)) {
4942 int written = Utf8::Encode(buffer, 4943 int written = unibrow::Utf8::Encode(buffer, character, last_character,
4943 character, 4944 replace_invalid_utf8);
4944 last_character, 4945 DCHECK_EQ(written, 1);
4945 replace_invalid_utf8);
4946 DCHECK(written == 1);
4947 return written; 4946 return written;
4948 } 4947 }
4949 // Use a scratch buffer to check the required characters. 4948 // Use a scratch buffer to check the required characters.
4950 char temp_buffer[Utf8::kMaxEncodedSize]; 4949 char temp_buffer[unibrow::Utf8::kMaxEncodedSize];
4951 // Can't encode using last_character as gcc has array bounds issues. 4950 // Can't encode using last_character as gcc has array bounds issues.
4952 int written = Utf8::Encode(temp_buffer, 4951 int written = unibrow::Utf8::Encode(temp_buffer, character,
4953 character, 4952 unibrow::Utf16::kNoPreviousCharacter,
4954 Utf16::kNoPreviousCharacter, 4953 replace_invalid_utf8);
4955 replace_invalid_utf8);
4956 // Won't fit. 4954 // Won't fit.
4957 if (written > remaining) return 0; 4955 if (written > remaining) return 0;
4958 // Copy over the character from temp_buffer. 4956 // Copy over the character from temp_buffer.
4959 for (int j = 0; j < written; j++) { 4957 for (int j = 0; j < written; j++) {
4960 buffer[j] = temp_buffer[j]; 4958 buffer[j] = temp_buffer[j];
4961 } 4959 }
4962 return written; 4960 return written;
4963 } 4961 }
4964 4962
4965 // Visit writes out a group of code units (chars) of a v8::String to the 4963 // Visit writes out a group of code units (chars) of a v8::String to the
4966 // internal buffer_. This is done in two phases. The first phase calculates a 4964 // internal buffer_. This is done in two phases. The first phase calculates a
4967 // pesimistic estimate (writable_length) on how many code units can be safely 4965 // pesimistic estimate (writable_length) on how many code units can be safely
4968 // written without exceeding the buffer capacity and without writing the last 4966 // written without exceeding the buffer capacity and without writing the last
4969 // code unit (it could be a lead surrogate). The estimated number of code 4967 // code unit (it could be a lead surrogate). The estimated number of code
4970 // units is then written out in one go, and the reported byte usage is used 4968 // units is then written out in one go, and the reported byte usage is used
4971 // to correct the estimate. This is repeated until the estimate becomes <= 0 4969 // to correct the estimate. This is repeated until the estimate becomes <= 0
4972 // or all code units have been written out. The second phase writes out code 4970 // or all code units have been written out. The second phase writes out code
4973 // units until the buffer capacity is reached, would be exceeded by the next 4971 // units until the buffer capacity is reached, would be exceeded by the next
4974 // unit, or all units have been written out. 4972 // unit, or all units have been written out.
4975 template<typename Char> 4973 template<typename Char>
4976 void Visit(const Char* chars, const int length) { 4974 void Visit(const Char* chars, const int length) {
4977 using namespace unibrow;
4978 DCHECK(!early_termination_); 4975 DCHECK(!early_termination_);
4979 if (length == 0) return; 4976 if (length == 0) return;
4980 // Copy state to stack. 4977 // Copy state to stack.
4981 char* buffer = buffer_; 4978 char* buffer = buffer_;
4982 int last_character = 4979 int last_character = sizeof(Char) == 1
4983 sizeof(Char) == 1 ? Utf16::kNoPreviousCharacter : last_character_; 4980 ? unibrow::Utf16::kNoPreviousCharacter
4981 : last_character_;
4984 int i = 0; 4982 int i = 0;
4985 // Do a fast loop where there is no exit capacity check. 4983 // Do a fast loop where there is no exit capacity check.
4986 while (true) { 4984 while (true) {
4987 int fast_length; 4985 int fast_length;
4988 if (skip_capacity_check_) { 4986 if (skip_capacity_check_) {
4989 fast_length = length; 4987 fast_length = length;
4990 } else { 4988 } else {
4991 int remaining_capacity = capacity_ - static_cast<int>(buffer - start_); 4989 int remaining_capacity = capacity_ - static_cast<int>(buffer - start_);
4992 // Need enough space to write everything but one character. 4990 // Need enough space to write everything but one character.
4993 STATIC_ASSERT(Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit == 3); 4991 STATIC_ASSERT(unibrow::Utf16::kMaxExtraUtf8BytesForOneUtf16CodeUnit ==
4992 3);
4994 int max_size_per_char = sizeof(Char) == 1 ? 2 : 3; 4993 int max_size_per_char = sizeof(Char) == 1 ? 2 : 3;
4995 int writable_length = 4994 int writable_length =
4996 (remaining_capacity - max_size_per_char)/max_size_per_char; 4995 (remaining_capacity - max_size_per_char)/max_size_per_char;
4997 // Need to drop into slow loop. 4996 // Need to drop into slow loop.
4998 if (writable_length <= 0) break; 4997 if (writable_length <= 0) break;
4999 fast_length = i + writable_length; 4998 fast_length = i + writable_length;
5000 if (fast_length > length) fast_length = length; 4999 if (fast_length > length) fast_length = length;
5001 } 5000 }
5002 // Write the characters to the stream. 5001 // Write the characters to the stream.
5003 if (sizeof(Char) == 1) { 5002 if (sizeof(Char) == 1) {
5004 for (; i < fast_length; i++) { 5003 for (; i < fast_length; i++) {
5005 buffer += 5004 buffer += unibrow::Utf8::EncodeOneByte(
5006 Utf8::EncodeOneByte(buffer, static_cast<uint8_t>(*chars++)); 5005 buffer, static_cast<uint8_t>(*chars++));
5007 DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_); 5006 DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_);
5008 } 5007 }
5009 } else { 5008 } else {
5010 for (; i < fast_length; i++) { 5009 for (; i < fast_length; i++) {
5011 uint16_t character = *chars++; 5010 uint16_t character = *chars++;
5012 buffer += Utf8::Encode(buffer, 5011 buffer += unibrow::Utf8::Encode(buffer, character, last_character,
5013 character, 5012 replace_invalid_utf8_);
5014 last_character,
5015 replace_invalid_utf8_);
5016 last_character = character; 5013 last_character = character;
5017 DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_); 5014 DCHECK(capacity_ == -1 || (buffer - start_) <= capacity_);
5018 } 5015 }
5019 } 5016 }
5020 // Array is fully written. Exit. 5017 // Array is fully written. Exit.
5021 if (fast_length == length) { 5018 if (fast_length == length) {
5022 // Write state back out to object. 5019 // Write state back out to object.
5023 last_character_ = last_character; 5020 last_character_ = last_character;
5024 buffer_ = buffer; 5021 buffer_ = buffer;
5025 utf16_chars_read_ += length; 5022 utf16_chars_read_ += length;
5026 return; 5023 return;
5027 } 5024 }
5028 } 5025 }
5029 DCHECK(!skip_capacity_check_); 5026 DCHECK(!skip_capacity_check_);
5030 // Slow loop. Must check capacity on each iteration. 5027 // Slow loop. Must check capacity on each iteration.
5031 int remaining_capacity = capacity_ - static_cast<int>(buffer - start_); 5028 int remaining_capacity = capacity_ - static_cast<int>(buffer - start_);
5032 DCHECK(remaining_capacity >= 0); 5029 DCHECK_GE(remaining_capacity, 0);
5033 for (; i < length && remaining_capacity > 0; i++) { 5030 for (; i < length && remaining_capacity > 0; i++) {
5034 uint16_t character = *chars++; 5031 uint16_t character = *chars++;
5035 // remaining_capacity is <= 3 bytes at this point, so we do not write out 5032 // remaining_capacity is <= 3 bytes at this point, so we do not write out
5036 // an umatched lead surrogate. 5033 // an umatched lead surrogate.
5037 if (replace_invalid_utf8_ && Utf16::IsLeadSurrogate(character)) { 5034 if (replace_invalid_utf8_ && unibrow::Utf16::IsLeadSurrogate(character)) {
5038 early_termination_ = true; 5035 early_termination_ = true;
5039 break; 5036 break;
5040 } 5037 }
5041 int written = WriteEndCharacter(character, 5038 int written = WriteEndCharacter(character,
5042 last_character, 5039 last_character,
5043 remaining_capacity, 5040 remaining_capacity,
5044 buffer, 5041 buffer,
5045 replace_invalid_utf8_); 5042 replace_invalid_utf8_);
5046 if (written == 0) { 5043 if (written == 0) {
5047 early_termination_ = true; 5044 early_termination_ = true;
(...skipping 2977 matching lines...) Expand 10 before | Expand all | Expand 10 after
8025 return (profile->end_time() - base::TimeTicks()).InMicroseconds(); 8022 return (profile->end_time() - base::TimeTicks()).InMicroseconds();
8026 } 8023 }
8027 8024
8028 8025
8029 int CpuProfile::GetSamplesCount() const { 8026 int CpuProfile::GetSamplesCount() const {
8030 return reinterpret_cast<const i::CpuProfile*>(this)->samples_count(); 8027 return reinterpret_cast<const i::CpuProfile*>(this)->samples_count();
8031 } 8028 }
8032 8029
8033 8030
8034 void CpuProfiler::SetSamplingInterval(int us) { 8031 void CpuProfiler::SetSamplingInterval(int us) {
8035 DCHECK(us >= 0); 8032 DCHECK_GE(us, 0);
8036 return reinterpret_cast<i::CpuProfiler*>(this)->set_sampling_interval( 8033 return reinterpret_cast<i::CpuProfiler*>(this)->set_sampling_interval(
8037 base::TimeDelta::FromMicroseconds(us)); 8034 base::TimeDelta::FromMicroseconds(us));
8038 } 8035 }
8039 8036
8040 8037
8041 void CpuProfiler::StartProfiling(Local<String> title, bool record_samples) { 8038 void CpuProfiler::StartProfiling(Local<String> title, bool record_samples) {
8042 reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling( 8039 reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
8043 *Utils::OpenHandle(*title), record_samples); 8040 *Utils::OpenHandle(*title), record_samples);
8044 } 8041 }
8045 8042
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after
8521 Address callback_address = 8518 Address callback_address =
8522 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 8519 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
8523 VMState<EXTERNAL> state(isolate); 8520 VMState<EXTERNAL> state(isolate);
8524 ExternalCallbackScope call_scope(isolate, callback_address); 8521 ExternalCallbackScope call_scope(isolate, callback_address);
8525 callback(info); 8522 callback(info);
8526 } 8523 }
8527 8524
8528 8525
8529 } // namespace internal 8526 } // namespace internal
8530 } // namespace v8 8527 } // namespace v8
OLDNEW
« include/v8.h ('K') | « include/v8.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698