Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: Fix inefficient usage of v8_inspector::StringView #52372

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/inspector_js_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::NewStringType;
using v8::Object;
using v8::String;
using v8::Uint32;
Expand Down Expand Up @@ -76,9 +75,8 @@ class JSBindingsConnection : public BaseObject {
HandleScope handle_scope(isolate);
Context::Scope context_scope(env_->context());
Local<Value> argument;
if (!String::NewFromTwoByte(isolate, message.characters16(),
NewStringType::kNormal,
message.length()).ToLocal(&argument)) return;
if (!ToV8Value(env_->context(), message, isolate).ToLocal(&argument))
return;
connection_->OnMessage(argument);
}

Expand Down
25 changes: 25 additions & 0 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,31 @@ v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
.FromMaybe(v8::Local<v8::String>());
}

v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
v8_inspector::StringView str,
v8::Isolate* isolate) {
if (isolate == nullptr) isolate = context->GetIsolate();
if (UNLIKELY(str.length() >= static_cast<size_t>(v8::String::kMaxLength))) {
// V8 only has a TODO comment about adding an exception when the maximum
// string size is exceeded.
ThrowErrStringTooLong(isolate);
return v8::MaybeLocal<v8::Value>();
}

if (str.is8Bit()) {
return v8::String::NewFromOneByte(isolate,
str.characters8(),
v8::NewStringType::kNormal,
str.length())
.FromMaybe(v8::Local<v8::String>());
}
return v8::String::NewFromTwoByte(isolate,
str.characters16(),
v8::NewStringType::kNormal,
str.length())
.FromMaybe(v8::Local<v8::String>());
}

template <typename T>
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
const std::vector<T>& vec,
Expand Down
4 changes: 4 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "uv.h"
#include "v8-inspector.h"
#include "v8.h"

#include "node.h"
Expand Down Expand Up @@ -715,6 +716,9 @@ std::vector<std::string_view> SplitString(const std::string_view in,
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
std::string_view str,
v8::Isolate* isolate = nullptr);
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
v8_inspector::StringView str,
v8::Isolate* isolate);
template <typename T, typename test_for_number =
typename std::enable_if<std::numeric_limits<T>::is_specialized, bool>::type>
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
Expand Down
Loading