Skip to content

Commit 66345c0

Browse files
committed
Rename function to ToV8Value and mirror std::string_view overload
1 parent 8d52133 commit 66345c0

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

src/inspector_js_api.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class JSBindingsConnection : public BaseObject {
7575
HandleScope handle_scope(isolate);
7676
Context::Scope context_scope(env_->context());
7777
Local<Value> argument;
78-
if (!StringViewToV8String(isolate, message).ToLocal(&argument)) return;
78+
if (!ToV8Value(env_->context(), message, isolate).ToLocal(&argument))
79+
return;
7980
connection_->OnMessage(argument);
8081
}
8182

src/inspector_profiler.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ void V8ProfilerConnection::V8ProfilerSessionDelegate::SendMessageToFrontend(
8787

8888
const char* type = connection_->type();
8989
// Convert StringView to a Local<String>.
90-
Local<String> message_str;
91-
if (!StringViewToV8String(isolate, message).ToLocal(&message_str)) {
90+
Local<Value> message_str;
91+
if (!ToV8Value(context, message, isolate).ToLocal(&message_str) ||
92+
!message_str->IsString()) {
9293
fprintf(
9394
stderr, "Failed to convert %s profile message to V8 string\n", type);
9495
return;
@@ -100,7 +101,8 @@ void V8ProfilerConnection::V8ProfilerSessionDelegate::SendMessageToFrontend(
100101
type);
101102

102103
Local<Value> parsed;
103-
if (!v8::JSON::Parse(context, message_str).ToLocal(&parsed) ||
104+
if (!v8::JSON::Parse(context, message_str.As<v8::String>())
105+
.ToLocal(&parsed) ||
104106
!parsed->IsObject()) {
105107
fprintf(stderr, "Failed to parse %s profile result as JSON object\n", type);
106108
return;

src/util-inl.h

+25-14
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,6 @@ inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
213213
.ToLocalChecked();
214214
}
215215

216-
v8::MaybeLocal<v8::String> StringViewToV8String(
217-
v8::Isolate* isolate, v8_inspector::StringView string) {
218-
if (string.is8Bit()) {
219-
return v8::String::NewFromOneByte(isolate,
220-
string.characters8(),
221-
v8::NewStringType::kNormal,
222-
string.length());
223-
}
224-
return v8::String::NewFromTwoByte(isolate,
225-
string.characters16(),
226-
v8::NewStringType::kNormal,
227-
string.length());
228-
}
229-
230216
void SwapBytes16(char* data, size_t nbytes) {
231217
CHECK_EQ(nbytes % 2, 0);
232218

@@ -454,6 +440,31 @@ v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
454440
.FromMaybe(v8::Local<v8::String>());
455441
}
456442

443+
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
444+
v8_inspector::StringView str,
445+
v8::Isolate* isolate) {
446+
if (isolate == nullptr) isolate = context->GetIsolate();
447+
if (UNLIKELY(str.length() >= static_cast<size_t>(v8::String::kMaxLength))) {
448+
// V8 only has a TODO comment about adding an exception when the maximum
449+
// string size is exceeded.
450+
ThrowErrStringTooLong(isolate);
451+
return v8::MaybeLocal<v8::Value>();
452+
}
453+
454+
if (str.is8Bit()) {
455+
return v8::String::NewFromOneByte(isolate,
456+
str.characters8(),
457+
v8::NewStringType::kNormal,
458+
str.length())
459+
.FromMaybe(v8::Local<v8::String>());
460+
}
461+
return v8::String::NewFromTwoByte(isolate,
462+
str.characters16(),
463+
v8::NewStringType::kNormal,
464+
str.length())
465+
.FromMaybe(v8::Local<v8::String>());
466+
}
467+
457468
template <typename T>
458469
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
459470
const std::vector<T>& vec,

src/util.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,6 @@ inline v8::Local<v8::String> FIXED_ONE_BYTE_STRING(
354354
return OneByteString(isolate, arr.data(), N - 1);
355355
}
356356

357-
// Convenience wrapper to handle both one- and two-byte inspector strings.
358-
inline v8::MaybeLocal<v8::String> StringViewToV8String(
359-
v8::Isolate* isolate, v8_inspector::StringView string);
360-
361357
// Swaps bytes in place. nbytes is the number of bytes to swap and must be a
362358
// multiple of the word size (checked by function).
363359
inline void SwapBytes16(char* data, size_t nbytes);
@@ -717,6 +713,9 @@ std::vector<std::string_view> SplitString(const std::string_view in,
717713
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
718714
std::string_view str,
719715
v8::Isolate* isolate = nullptr);
716+
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
717+
v8_inspector::StringView str,
718+
v8::Isolate* isolate);
720719
template <typename T, typename test_for_number =
721720
typename std::enable_if<std::numeric_limits<T>::is_specialized, bool>::type>
722721
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,

0 commit comments

Comments
 (0)