Skip to content
Merged
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 @@ -23,7 +23,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 @@ -69,9 +68,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
26 changes: 26 additions & 0 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,32 @@ 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 (str.length() >= static_cast<size_t>(v8::String::kMaxLength))
[[unlikely]] {
// 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 @@ -719,6 +720,9 @@ inline v8::Maybe<void> FromV8Array(v8::Local<v8::Context> context,
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