Skip to content

Commit

Permalink
[string] deprecate String::Utf8Length
Browse files Browse the repository at this point in the history
Deprecate String::Utf8Length in favor of a new, similar function that
takes the Isolate used for the String::Flatten call as an argument.

BUG: v8:7786

Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Change-Id: Icaf04b272679fd853e9cdbe6c7088f63e9aacb95
Reviewed-on: https://chromium-review.googlesource.com/1124724
Commit-Queue: Dan Elphick <delphick@chromium.org>
Reviewed-by: Yang Guo <yangguo@chromium.org>
Reviewed-by: Dan Elphick <delphick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54476}
  • Loading branch information
danelphick authored and Commit Bot committed Jul 16, 2018
1 parent 804a693 commit 3dd5c6f
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 43 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -150,6 +150,7 @@ Teddy Katz <teddy.katz@gmail.com>
Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Tobias Burnus <burnus@net-b.de>
Tobias Nießen <tniessen@tnie.de>
Ujjwal Sharma <usharma1998@gmail.com>
Victor Costan <costan@gmail.com>
Vlad Burlik <vladbph@gmail.com>
Vladimir Krivosheev <develar@gmail.com>
Expand Down
6 changes: 5 additions & 1 deletion include/v8.h
Expand Up @@ -2699,7 +2699,9 @@ class V8_EXPORT String : public Name {
* Returns the number of bytes in the UTF-8 encoded
* representation of this string.
*/
int Utf8Length() const;
V8_DEPRECATE_SOON("Use Isolate version instead", int Utf8Length() const);

int Utf8Length(Isolate* isolate) const;

/**
* Returns whether this string is known to contain only one byte data,
Expand Down Expand Up @@ -6986,6 +6988,8 @@ struct JitCodeEvent {
// New location of instructions. Only valid for CODE_MOVED.
void* new_code_start;
};

Isolate* isolate;
};

/**
Expand Down
13 changes: 8 additions & 5 deletions src/api.cc
Expand Up @@ -5602,10 +5602,14 @@ bool String::ContainsOnlyOneByte() const {
return helper.Check(*str);
}

// TODO(v8:7786): Deprecate this function and pass the isolate in instead.
int String::Utf8Length() const {
i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
return Utf8Length(reinterpret_cast<Isolate*>(isolate));
}

int String::Utf8Length(Isolate* isolate) const {
i::Handle<i::String> str = Utils::OpenHandle(this);
str = i::String::Flatten(str->GetIsolate(), str);
str = i::String::Flatten(reinterpret_cast<i::Isolate*>(isolate), str);
int length = str->length();
if (length == 0) return 0;
i::DisallowHeapAllocation no_gc;
Expand All @@ -5627,7 +5631,6 @@ int String::Utf8Length() const {
return utf8_length;
}


class Utf8WriterVisitor {
public:
Utf8WriterVisitor(
Expand Down Expand Up @@ -5846,7 +5849,7 @@ int String::WriteUtf8(Isolate* v8_isolate, char* buffer, int capacity,
if (success) return writer.CompleteWrite(write_null, nchars_ref);
} else if (capacity >= string_length) {
// First check that the buffer is large enough.
int utf8_bytes = Utf8Length();
int utf8_bytes = Utf8Length(reinterpret_cast<Isolate*>(isolate));
if (utf8_bytes <= capacity) {
// one-byte fast path.
if (utf8_bytes == string_length) {
Expand Down Expand Up @@ -9235,7 +9238,7 @@ String::Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Local<v8::Value> obj)
TryCatch try_catch(isolate);
Local<String> str;
if (!obj->ToString(context).ToLocal(&str)) return;
length_ = str->Utf8Length();
length_ = str->Utf8Length(isolate);
str_ = i::NewArray<char>(length_ + 1);
str->WriteUtf8(str_);
}
Expand Down
6 changes: 3 additions & 3 deletions src/d8.cc
Expand Up @@ -425,8 +425,8 @@ base::OnceType Shell::quit_once_ = V8_ONCE_INIT;
// Dummy external source stream which returns the whole source in one go.
class DummySourceStream : public v8::ScriptCompiler::ExternalSourceStream {
public:
explicit DummySourceStream(Local<String> source) : done_(false) {
source_length_ = source->Utf8Length();
DummySourceStream(Local<String> source, Isolate* isolate) : done_(false) {
source_length_ = source->Utf8Length(isolate);
source_buffer_.reset(new uint8_t[source_length_]);
source->WriteUtf8(reinterpret_cast<char*>(source_buffer_.get()),
source_length_);
Expand All @@ -453,7 +453,7 @@ class BackgroundCompileThread : public base::Thread {
BackgroundCompileThread(Isolate* isolate, Local<String> source)
: base::Thread(GetThreadOptions("BackgroundCompileThread")),
source_(source),
streamed_source_(new DummySourceStream(source),
streamed_source_(new DummySourceStream(source, isolate),
v8::ScriptCompiler::StreamedSource::UTF8),
task_(v8::ScriptCompiler::StartStreamingScript(isolate,
&streamed_source_)) {}
Expand Down
36 changes: 21 additions & 15 deletions src/log.cc
Expand Up @@ -191,8 +191,8 @@ class CodeEventLogger::NameBuffer {
uc16 utf16_buffer[kUtf16BufferSize];
};


CodeEventLogger::CodeEventLogger() : name_buffer_(new NameBuffer) { }
CodeEventLogger::CodeEventLogger(Isolate* isolate)
: isolate_(isolate), name_buffer_(new NameBuffer) {}

CodeEventLogger::~CodeEventLogger() { delete name_buffer_; }

Expand Down Expand Up @@ -267,7 +267,7 @@ void CodeEventLogger::RegExpCodeCreateEvent(AbstractCode* code,
// Linux perf tool logging support
class PerfBasicLogger : public CodeEventLogger {
public:
PerfBasicLogger();
explicit PerfBasicLogger(Isolate* isolate);
~PerfBasicLogger() override;

void CodeMoveEvent(AbstractCode* from, Address to) override {}
Expand All @@ -293,7 +293,8 @@ const char PerfBasicLogger::kFilenameFormatString[] = "/tmp/perf-%d.map";
// Extra space for the PID in the filename
const int PerfBasicLogger::kFilenameBufferPadding = 16;

PerfBasicLogger::PerfBasicLogger() : perf_output_handle_(nullptr) {
PerfBasicLogger::PerfBasicLogger(Isolate* isolate)
: CodeEventLogger(isolate), perf_output_handle_(nullptr) {
// Open the perf JIT dump file.
int bufferSize = sizeof(kFilenameFormatString) + kFilenameBufferPadding;
ScopedVector<char> perf_dump_name(bufferSize);
Expand Down Expand Up @@ -492,7 +493,7 @@ void ExternalCodeEventListener::RegExpCodeCreateEvent(AbstractCode* code,
// Low-level logging support.
class LowLevelLogger : public CodeEventLogger {
public:
explicit LowLevelLogger(const char* file_name);
LowLevelLogger(Isolate* isolate, const char* file_name);
~LowLevelLogger() override;

void CodeMoveEvent(AbstractCode* from, Address to) override;
Expand Down Expand Up @@ -546,7 +547,8 @@ class LowLevelLogger : public CodeEventLogger {

const char LowLevelLogger::kLogExt[] = ".ll";

LowLevelLogger::LowLevelLogger(const char* name) : ll_output_handle_(nullptr) {
LowLevelLogger::LowLevelLogger(Isolate* isolate, const char* name)
: CodeEventLogger(isolate), ll_output_handle_(nullptr) {
// Open the low-level log file.
size_t len = strlen(name);
ScopedVector<char> ll_name(static_cast<int>(len + sizeof(kLogExt)));
Expand Down Expand Up @@ -637,7 +639,7 @@ void LowLevelLogger::CodeMovingGCEvent() {

class JitLogger : public CodeEventLogger {
public:
explicit JitLogger(JitCodeEventHandler code_event_handler);
JitLogger(Isolate* isolate, JitCodeEventHandler code_event_handler);

void CodeMoveEvent(AbstractCode* from, Address to) override;
void CodeDisableOptEvent(AbstractCode* code,
Expand All @@ -659,10 +661,8 @@ class JitLogger : public CodeEventLogger {
base::Mutex logger_mutex_;
};


JitLogger::JitLogger(JitCodeEventHandler code_event_handler)
: code_event_handler_(code_event_handler) {
}
JitLogger::JitLogger(Isolate* isolate, JitCodeEventHandler code_event_handler)
: CodeEventLogger(isolate), code_event_handler_(code_event_handler) {}

void JitLogger::LogRecordedBuffer(AbstractCode* code,
SharedFunctionInfo* shared, const char* name,
Expand All @@ -682,6 +682,7 @@ void JitLogger::LogRecordedBuffer(AbstractCode* code,
event.script = ToApiHandle<v8::UnboundScript>(shared_function_handle);
event.name.str = name;
event.name.len = length;
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);
code_event_handler_(&event);
}

Expand All @@ -695,6 +696,7 @@ void JitLogger::LogRecordedBuffer(const wasm::WasmCode* code, const char* name,
event.code_len = code->instructions().length();
event.name.str = name;
event.name.len = length;
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);
code_event_handler_(&event);
}

Expand All @@ -713,6 +715,7 @@ void JitLogger::CodeMoveEvent(AbstractCode* from, Address to) {

// Calculate the new start address of the instructions.
event.new_code_start = reinterpret_cast<void*>(to + header_size);
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);

code_event_handler_(&event);
}
Expand All @@ -729,6 +732,7 @@ void JitLogger::AddCodeLinePosInfoEvent(
event.line_info.offset = pc_offset;
event.line_info.pos = position;
event.line_info.position_type = position_type;
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);

code_event_handler_(&event);
}
Expand All @@ -738,6 +742,7 @@ void* JitLogger::StartCodePosInfoEvent() {
JitCodeEvent event;
memset(&event, 0, sizeof(event));
event.type = JitCodeEvent::CODE_START_LINE_INFO_RECORDING;
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);

code_event_handler_(&event);
return event.user_data;
Expand All @@ -750,6 +755,7 @@ void JitLogger::EndCodePosInfoEvent(Address start_address,
event.type = JitCodeEvent::CODE_END_LINE_INFO_RECORDING;
event.code_start = reinterpret_cast<void*>(start_address);
event.user_data = jit_handler_data;
event.isolate = reinterpret_cast<v8::Isolate*>(isolate_);

code_event_handler_(&event);
}
Expand Down Expand Up @@ -1967,17 +1973,17 @@ bool Logger::SetUp(Isolate* isolate) {
log_ = new Log(this, log_file_name.str().c_str());

if (FLAG_perf_basic_prof) {
perf_basic_logger_ = new PerfBasicLogger();
perf_basic_logger_ = new PerfBasicLogger(isolate);
AddCodeEventListener(perf_basic_logger_);
}

if (FLAG_perf_prof) {
perf_jit_logger_ = new PerfJitLogger();
perf_jit_logger_ = new PerfJitLogger(isolate);
AddCodeEventListener(perf_jit_logger_);
}

if (FLAG_ll_prof) {
ll_logger_ = new LowLevelLogger(log_file_name.str().c_str());
ll_logger_ = new LowLevelLogger(isolate, log_file_name.str().c_str());
AddCodeEventListener(ll_logger_);
}

Expand Down Expand Up @@ -2012,7 +2018,7 @@ void Logger::SetCodeEventHandler(uint32_t options,
}

if (event_handler) {
jit_logger_ = new JitLogger(event_handler);
jit_logger_ = new JitLogger(isolate_, event_handler);
AddCodeEventListener(jit_logger_);
if (options & kJitCodeEventEnumExisting) {
HandleScope scope(isolate_);
Expand Down
6 changes: 5 additions & 1 deletion src/log.h
Expand Up @@ -414,7 +414,7 @@ class TimerEventScope {

class CodeEventLogger : public CodeEventListener {
public:
CodeEventLogger();
explicit CodeEventLogger(Isolate* isolate);
~CodeEventLogger() override;

void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code,
Expand All @@ -438,6 +438,9 @@ class CodeEventLogger : public CodeEventListener {
void CodeDeoptEvent(Code* code, DeoptimizeKind kind, Address pc,
int fp_to_sp_delta) override {}

protected:
Isolate* isolate_;

private:
class NameBuffer;

Expand All @@ -450,6 +453,7 @@ class CodeEventLogger : public CodeEventListener {
};

struct CodeEvent {
Isolate* isolate_;
uintptr_t code_start_address;
size_t code_size;
Handle<String> function_name;
Expand Down
2 changes: 1 addition & 1 deletion src/perf-jit.cc
Expand Up @@ -164,7 +164,7 @@ void PerfJitLogger::CloseMarkerFile(void* marker_address) {
munmap(marker_address, page_size);
}

PerfJitLogger::PerfJitLogger() {
PerfJitLogger::PerfJitLogger(Isolate* isolate) : CodeEventLogger(isolate) {
base::LockGuard<base::RecursiveMutex> guard_file(file_mutex_.Pointer());

reference_count_++;
Expand Down
4 changes: 3 additions & 1 deletion src/perf-jit.h
Expand Up @@ -38,7 +38,7 @@ namespace internal {
// Linux perf tool logging support
class PerfJitLogger : public CodeEventLogger {
public:
PerfJitLogger();
explicit PerfJitLogger(Isolate* isolate);
virtual ~PerfJitLogger();

void CodeMoveEvent(AbstractCode* from, Address to) override;
Expand Down Expand Up @@ -118,6 +118,8 @@ class PerfJitLogger : public CodeEventLogger {
// PerfJitLogger is only implemented on Linux
class PerfJitLogger : public CodeEventLogger {
public:
explicit PerfJitLogger(Isolate* isolate) : CodeEventLogger(isolate) {}

void CodeMoveEvent(AbstractCode* from, Address to) override {
UNIMPLEMENTED();
}
Expand Down
3 changes: 1 addition & 2 deletions src/snapshot/serializer.h
Expand Up @@ -20,7 +20,7 @@ namespace internal {

class CodeAddressMap : public CodeEventLogger {
public:
explicit CodeAddressMap(Isolate* isolate) : isolate_(isolate) {
explicit CodeAddressMap(Isolate* isolate) : CodeEventLogger(isolate) {
isolate->logger()->AddCodeEventListener(this);
}

Expand Down Expand Up @@ -125,7 +125,6 @@ class CodeAddressMap : public CodeEventLogger {
}

NameMap address_to_name_map_;
Isolate* isolate_;
};

template <class AllocatorT = DefaultSerializerAllocator>
Expand Down
3 changes: 2 additions & 1 deletion src/third_party/vtune/vtune-jit.cc
Expand Up @@ -182,7 +182,8 @@ void VTUNEJITInterface::event_handler(const v8::JitCodeEvent* event) {
if ((*script->GetScriptName())->IsString()) {
Local<String> script_name =
Local<String>::Cast(script->GetScriptName());
temp_file_name = new char[script_name->Utf8Length() + 1];
temp_file_name =
new char[script_name->Utf8Length(event->isolate) + 1];
script_name->WriteUtf8(temp_file_name);
jmethod.source_file_name = temp_file_name;
}
Expand Down
11 changes: 6 additions & 5 deletions test/cctest/test-api.cc
Expand Up @@ -373,7 +373,7 @@ THREADED_TEST(HulIgennem) {
v8::HandleScope scope(isolate);
v8::Local<v8::Primitive> undef = v8::Undefined(isolate);
Local<String> undef_str = undef->ToString(env.local()).ToLocalChecked();
char* value = i::NewArray<char>(undef_str->Utf8Length() + 1);
char* value = i::NewArray<char>(undef_str->Utf8Length(isolate) + 1);
undef_str->WriteUtf8(value);
CHECK_EQ(0, strcmp(value, "undefined"));
i::DeleteArray(value);
Expand Down Expand Up @@ -8271,12 +8271,12 @@ static int StrNCmp16(uint16_t* a, uint16_t* b, int n) {
}

int GetUtf8Length(v8::Isolate* isolate, Local<String> str) {
int len = str->Utf8Length();
int len = str->Utf8Length(isolate);
if (len < 0) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
i::Handle<i::String> istr(v8::Utils::OpenHandle(*str));
i::String::Flatten(i_isolate, istr);
len = str->Utf8Length();
len = str->Utf8Length(isolate);
}
return len;
}
Expand Down Expand Up @@ -15676,7 +15676,8 @@ THREADED_TEST(MorphCompositeStringTest) {
{
LocalContext env;
i::Factory* factory = CcTest::i_isolate()->factory();
v8::HandleScope scope(env->GetIsolate());
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope scope(isolate);
OneByteVectorResource one_byte_resource(
i::Vector<const char>(c_string, i::StrLength(c_string)));
UC16VectorResource uc16_resource(
Expand Down Expand Up @@ -15708,7 +15709,7 @@ THREADED_TEST(MorphCompositeStringTest) {
// This should UTF-8 without flattening, since everything is ASCII.
Local<String> cons =
v8_compile("cons")->Run(env.local()).ToLocalChecked().As<String>();
CHECK_EQ(128, cons->Utf8Length());
CHECK_EQ(128, cons->Utf8Length(isolate));
int nchars = -1;
CHECK_EQ(129, cons->WriteUtf8(utf_buffer, -1, &nchars));
CHECK_EQ(128, nchars);
Expand Down

0 comments on commit 3dd5c6f

Please sign in to comment.