Commit 1cf47195 authored by Ben Smith's avatar Ben Smith Committed by Commit Bot

Revert "[ast] Remove literal allocation from CallPrinter"

This reverts commit 908f08e4.

Reason for revert: Seems like it may be causing v8-fuzzer failures: https://ci.chromium.org/p/v8/builders/ci/V8%20Fuzzer/36017

Original change's description:
> [ast] Remove literal allocation from CallPrinter
> 
> Access literal data directly in CallPrinter, rather than allocating
> their values. This allows us to remove the isolate member from
> CallPrinter entirely.
> 
> Change-Id: Ib4203009c86b6778ee843e8956fc7cee2214841e
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2122019
> Reviewed-by: Toon Verwaest <verwaest@chromium.org>
> Commit-Queue: Leszek Swirski <leszeks@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#67471}

TBR=leszeks@chromium.org,verwaest@chromium.org

Change-Id: Ia7e0c95ee6ec58e5067d92c7517269fd334041a1
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2171929Reviewed-by: 's avatarBen Smith <binji@chromium.org>
Commit-Queue: Ben Smith <binji@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67480}
parent b9eda863
......@@ -20,6 +20,7 @@ namespace internal {
CallPrinter::CallPrinter(Isolate* isolate, bool is_user_js,
SpreadErrorInArgsHint error_in_spread_args)
: builder_(new IncrementalStringBuilder(isolate)) {
isolate_ = isolate;
position_ = 0;
num_prints_ = 0;
found_ = false;
......@@ -76,16 +77,10 @@ void CallPrinter::Print(const char* str) {
builder_->AppendCString(str);
}
void CallPrinter::Print(const AstRawString* str) {
void CallPrinter::Print(Handle<String> str) {
if (!found_ || done_) return;
num_prints_++;
if (str->is_one_byte()) {
builder_->AppendNString(reinterpret_cast<const char*>(str->raw_data()),
str->length());
} else {
builder_->AppendNString(reinterpret_cast<const uc16*>(str->raw_data()),
str->length());
}
builder_->AppendString(str);
}
void CallPrinter::VisitBlock(Block* node) {
......@@ -249,11 +244,17 @@ void CallPrinter::VisitConditional(Conditional* node) {
Find(node->else_expression());
}
void CallPrinter::VisitLiteral(Literal* node) { PrintLiteral(node, true); }
void CallPrinter::VisitLiteral(Literal* node) {
// TODO(adamk): Teach Literal how to print its values without
// allocating on the heap.
PrintLiteral(node->BuildValue(isolate_), true);
}
void CallPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
Print("/");
Print(node->raw_pattern());
PrintLiteral(node->pattern(), false);
Print("/");
if (node->flags() & RegExp::kGlobal) Print("g");
if (node->flags() & RegExp::kIgnoreCase) Print("i");
......@@ -294,7 +295,7 @@ void CallPrinter::VisitArrayLiteral(ArrayLiteral* node) {
void CallPrinter::VisitVariableProxy(VariableProxy* node) {
if (is_user_js_) {
Print(node->raw_name());
PrintLiteral(node->name(), false);
} else {
// Variable names of non-user code are meaningless due to minification.
Print("(var)");
......@@ -379,13 +380,16 @@ void CallPrinter::VisitOptionalChain(OptionalChain* node) {
void CallPrinter::VisitProperty(Property* node) {
Expression* key = node->key();
Literal* literal = key->AsLiteral();
if (literal != nullptr && literal->IsStringLiteral()) {
if (literal != nullptr &&
literal->BuildValue(isolate_)->IsInternalizedString()) {
Find(node->obj(), true);
if (node->is_optional_chain_link()) {
Print("?");
}
Print(".");
PrintLiteral(literal, false);
// TODO(adamk): Teach Literal how to print its values without
// allocating on the heap.
PrintLiteral(literal->BuildValue(isolate_), false);
} else {
Find(node->obj(), true);
if (node->is_optional_chain_link()) {
......@@ -574,45 +578,31 @@ void CallPrinter::FindArguments(const ZonePtrList<Expression>* arguments) {
}
}
void CallPrinter::PrintLiteral(Literal* literal, bool quote) {
switch (literal->type()) {
case Literal::kSmi:
case Literal::kHeapNumber: {
double double_value = literal->AsNumber();
char buffer_data[kDoubleToCStringMinBufferSize];
Vector<char> buffer(buffer_data, kDoubleToCStringMinBufferSize);
int smi_value;
if (DoubleToSmiInteger(double_value, &smi_value)) {
return Print(DoubleToCString(smi_value, buffer));
}
return Print(DoubleToCString(double_value, buffer));
}
case Literal::kBigInt:
return Print(literal->AsBigInt().c_str());
case Literal::kString:
return PrintLiteral(literal->AsRawString(), quote);
case Literal::kSymbol:
switch (literal->AsSymbol()) {
case AstSymbol::kHomeObjectSymbol:
return Print("HomeObjectSymbol");
}
UNREACHABLE();
case Literal::kBoolean:
return Print(literal->ToBooleanIsTrue() ? "true" : "false");
case Literal::kUndefined:
return Print("undefined");
case Literal::kNull:
return Print("null");
case Literal::kTheHole:
return;
void CallPrinter::PrintLiteral(Handle<Object> value, bool quote) {
if (value->IsString()) {
if (quote) Print("\"");
Print(Handle<String>::cast(value));
if (quote) Print("\"");
} else if (value->IsNull(isolate_)) {
Print("null");
} else if (value->IsTrue(isolate_)) {
Print("true");
} else if (value->IsFalse(isolate_)) {
Print("false");
} else if (value->IsUndefined(isolate_)) {
Print("undefined");
} else if (value->IsNumber()) {
Print(isolate_->factory()->NumberToString(value));
} else if (value->IsSymbol()) {
// Symbols can only occur as literals if they were inserted by the parser.
PrintLiteral(handle(Handle<Symbol>::cast(value)->description(), isolate_),
false);
}
}
void CallPrinter::PrintLiteral(const AstRawString* value, bool quote) {
if (quote) Print("\"");
Print(value);
if (quote) Print("\"");
PrintLiteral(value->string(), quote);
}
//-----------------------------------------------------------------------------
......
......@@ -53,10 +53,11 @@ class CallPrinter final : public AstVisitor<CallPrinter> {
private:
void Print(const char* str);
void Print(const AstRawString* str);
void Print(Handle<String> str);
void Find(AstNode* node, bool print = false);
Isolate* isolate_;
int num_prints_;
// Allocate the builder on the heap simply because it's forward declared.
std::unique_ptr<IncrementalStringBuilder> builder_;
......@@ -75,7 +76,7 @@ class CallPrinter final : public AstVisitor<CallPrinter> {
DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
protected:
void PrintLiteral(Literal* literal, bool quote);
void PrintLiteral(Handle<Object> value, bool quote);
void PrintLiteral(const AstRawString* value, bool quote);
void FindStatements(const ZonePtrList<Statement>* statements);
void FindArguments(const ZonePtrList<Expression>* arguments);
......
......@@ -147,25 +147,6 @@ class IncrementalStringBuilder {
}
}
V8_INLINE void AppendNString(const char* s, size_t len) {
const uint8_t* u = reinterpret_cast<const uint8_t*>(s);
const uint8_t* end = u + len;
if (encoding_ == String::ONE_BYTE_ENCODING) {
while (u != end) Append<uint8_t, uint8_t>(*(u++));
} else {
while (u != end) Append<uint8_t, uc16>(*(u++));
}
}
V8_INLINE void AppendNString(const uc16* s, size_t len) {
const uc16* end = s + len;
if (encoding_ == String::ONE_BYTE_ENCODING) {
while (s != end) Append<uc16, uint8_t>(*(s++));
} else {
while (s != end) Append<uc16, uc16>(*(s++));
}
}
V8_INLINE void AppendInt(int i) {
char buffer[kIntToCStringBufferSize];
const char* str =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment