v8/test/inspector/utils.cc
Clemens Backes 3640583fcc [inspector][fuzzer] Extract functionality for reuse
This CL extracts some functionality from inspector-test.cc to be reused
by the inspector fuzzer.

It also puts all functions in the v8::internal namespace, and adds
separate functions in v8::internal to be called by ::main such that
we have direct access to the full namespace there.

R=szuend@chromium.org

Bug: chromium:1142437
Change-Id: I671317822bdc1c721334469811893affcc460f8f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2501847
Reviewed-by: Simon Zünd <szuend@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70821}
2020-10-28 08:37:25 +00:00

83 lines
2.8 KiB
C++

// Copyright 2020 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 "test/inspector/utils.h"
#include <vector>
#include "include/v8-inspector.h"
#include "include/v8.h"
namespace v8 {
namespace internal {
std::vector<uint8_t> ToBytes(v8::Isolate* isolate, v8::Local<v8::String> str) {
std::vector<uint8_t> buffer(str->Length());
str->WriteOneByte(isolate, buffer.data(), 0, str->Length());
return buffer;
}
v8::Local<v8::String> ToV8String(v8::Isolate* isolate, const char* str) {
return v8::String::NewFromUtf8(isolate, str).ToLocalChecked();
}
v8::Local<v8::String> ToV8String(v8::Isolate* isolate,
const std::vector<uint8_t>& bytes) {
return v8::String::NewFromOneByte(isolate, bytes.data(),
v8::NewStringType::kNormal,
static_cast<int>(bytes.size()))
.ToLocalChecked();
}
v8::Local<v8::String> ToV8String(v8::Isolate* isolate,
const std::string& buffer) {
int length = static_cast<int>(buffer.size());
return v8::String::NewFromUtf8(isolate, buffer.data(),
v8::NewStringType::kNormal, length)
.ToLocalChecked();
}
v8::Local<v8::String> ToV8String(v8::Isolate* isolate,
const std::vector<uint16_t>& buffer) {
int length = static_cast<int>(buffer.size());
return v8::String::NewFromTwoByte(isolate, buffer.data(),
v8::NewStringType::kNormal, length)
.ToLocalChecked();
}
v8::Local<v8::String> ToV8String(v8::Isolate* isolate,
const v8_inspector::StringView& string) {
if (string.is8Bit()) {
return v8::String::NewFromOneByte(isolate, string.characters8(),
v8::NewStringType::kNormal,
static_cast<int>(string.length()))
.ToLocalChecked();
}
return v8::String::NewFromTwoByte(isolate, string.characters16(),
v8::NewStringType::kNormal,
static_cast<int>(string.length()))
.ToLocalChecked();
}
std::vector<uint16_t> ToVector(v8::Isolate* isolate,
v8::Local<v8::String> str) {
std::vector<uint16_t> buffer(str->Length());
str->Write(isolate, buffer.data(), 0, str->Length());
return buffer;
}
std::vector<uint16_t> ToVector(const v8_inspector::StringView& string) {
std::vector<uint16_t> buffer(string.length());
for (size_t i = 0; i < string.length(); i++) {
if (string.is8Bit())
buffer[i] = string.characters8()[i];
else
buffer[i] = string.characters16()[i];
}
return buffer;
}
} // namespace internal
} // namespace v8