Skip to content
Closed
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
14 changes: 11 additions & 3 deletions src/node_builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,23 @@ void BuiltinLoader::GetNatives(Local<Name> property,
auto source = env->builtin_loader()->source_.read();
for (auto const& x : *source) {
Local<String> key = OneByteString(isolate, x.first);
if (out->Set(context, key, x.second.ToStringChecked(isolate)).IsNothing()) {
Local<String> value;
if (!x.second.ToString(isolate).ToLocal(&value)) {
return;
}
if (out->Set(context, key, value).IsNothing()) {
return;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion, but if we add support for UnionBytes in ToV8Value(), this entirely loop could be replaced by a ToV8Value() call

info.GetReturnValue().Set(out);
}

Local<String> BuiltinLoader::GetConfigString(Isolate* isolate) {
return config_.ToStringChecked(isolate);
Local<String> config_str;
if (!config_.ToString(isolate).ToLocal(&config_str)) {
return {};
}
return config_str;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not do this – this is dangerous and strictly worse than the previous code, because a return type of Local<String> (as opposed to MaybeLocal<String>) typically means that the value won't be empty, so the caller might assume that it's always safe to dereference the handle.

}

BuiltinLoader::BuiltinCategories BuiltinLoader::GetBuiltinCategories() const {
Expand Down Expand Up @@ -203,7 +211,7 @@ MaybeLocal<String> BuiltinLoader::LoadBuiltinSource(Isolate* isolate,
fprintf(stderr, "Cannot find native builtin: \"%s\".\n", id);
ABORT();
}
return source_it->second.ToStringChecked(isolate);
return source_it->second.ToString(isolate);
#else // !NODE_BUILTIN_MODULES_PATH
std::string filename = OnDiskFileName(id);

Expand Down
4 changes: 2 additions & 2 deletions src/node_union_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ using StaticExternalTwoByteResource =

// Similar to a v8::String, but it's independent from Isolates
// and can be materialized in Isolates as external Strings
// via ToStringChecked.
// via ToString.
class UnionBytes {
public:
explicit UnionBytes(StaticExternalOneByteResource* one_byte_resource)
Expand All @@ -67,7 +67,7 @@ class UnionBytes {

bool is_one_byte() const { return one_byte_resource_ != nullptr; }

v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const;
v8::MaybeLocal<v8::String> ToString(v8::Isolate* isolate) const;

private:
StaticExternalOneByteResource* one_byte_resource_;
Expand Down
10 changes: 5 additions & 5 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "node_snapshot_builder.h"
#include "node_v8_platform-inl.h"
#include "string_bytes.h"
#include "v8-local-handle.h"
#include "v8-value.h"

#ifdef _WIN32
Expand Down Expand Up @@ -91,6 +92,7 @@ using v8::Context;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Template;
Expand Down Expand Up @@ -725,13 +727,11 @@ void SetConstructorFunction(Isolate* isolate,
that->Set(name, tmpl);
}

Local<String> UnionBytes::ToStringChecked(Isolate* isolate) const {
MaybeLocal<String> UnionBytes::ToString(Isolate* isolate) const {
if (is_one_byte()) {
return String::NewExternalOneByte(isolate, one_byte_resource_)
.ToLocalChecked();
return String::NewExternalOneByte(isolate, one_byte_resource_);
} else {
return String::NewExternalTwoByte(isolate, two_byte_resource_)
.ToLocalChecked();
return String::NewExternalTwoByte(isolate, two_byte_resource_);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change kind of only brings us halfway towards the right semantics, because the comment in this snippet also applies here:

node/src/util-inl.h

Lines 347 to 357 in 1527825

if (str.size() >= 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>();
}
return v8::String::NewFromUtf8(
isolate, str.data(), v8::NewStringType::kNormal, str.size())
.FromMaybe(v8::Local<v8::String>());
}

}
}

Expand Down
Loading