Skip to content

Commit f79c3cb

Browse files
committed
Rename function to ToV8Value and mirror std::string_view overload
1 parent 5600ff1 commit f79c3cb

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

src/inspector_js_api.cc

Lines changed: 2 additions & 1 deletion
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/util-inl.h

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,6 @@ inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
214214
.ToLocalChecked();
215215
}
216216

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

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

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

src/util.h

Lines changed: 3 additions & 4 deletions
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)