Skip to content

Commit

Permalink
Bug 1823326 - Part 9: Wrap JSStringBuilder to support syntax parser. …
Browse files Browse the repository at this point in the history
…r=bthrall

Syntax parser does not need string allocation.

Differential Revision: https://phabricator.services.mozilla.com/D174567
  • Loading branch information
arai-a committed Apr 20, 2023
1 parent 77c54c8 commit 99b4441
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
25 changes: 18 additions & 7 deletions js/src/vm/JSONParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ JSONToken JSONTokenizer<CharT, ParserT, StringBuilderT>::readString() {
* of unescaped characters into a temporary buffer, then an escaped
* character, and repeat until the entire string is consumed.
*/
JSStringBuilder buffer(parser->handler.cx);
StringBuilderT builder(parser->handler.cx);
do {
if (start < current && !buffer.append(start.get(), current.get())) {
if (start < current && !builder.append(start.get(), current.get())) {
return token(JSONToken::OOM);
}

Expand All @@ -111,7 +111,7 @@ JSONToken JSONTokenizer<CharT, ParserT, StringBuilderT>::readString() {

char16_t c = *current++;
if (c == '"') {
return stringToken<ST>(buffer);
return stringToken<ST>(builder);
}

if (c != '\\') {
Expand Down Expand Up @@ -183,7 +183,7 @@ JSONToken JSONTokenizer<CharT, ParserT, StringBuilderT>::readString() {
error("bad escaped character");
return token(JSONToken::Error);
}
if (!buffer.append(c)) {
if (!builder.append(c)) {
return token(JSONToken::OOM);
}

Expand Down Expand Up @@ -617,10 +617,10 @@ inline bool JSONFullParseHandler<CharT>::setStringValue(CharPtr start,
template <typename CharT>
template <JSONStringType ST>
inline bool JSONFullParseHandler<CharT>::setStringValue(
JSStringBuilder& builder) {
StringBuilder& builder) {
JSLinearString* str = (ST == JSONStringType::PropertyName)
? builder.finishAtom()
: builder.finishString();
? builder.buffer.finishAtom()
: builder.buffer.finishString();
if (!str) {
return false;
}
Expand Down Expand Up @@ -773,6 +773,17 @@ void JSONParser<CharT>::error(const char* msg) {
handler.reportError(msg, lineNumber, columnNumber);
}

template <typename CharT>
bool JSONFullParseHandler<CharT>::StringBuilder::append(char16_t c) {
return buffer.append(c);
}

template <typename CharT>
bool JSONFullParseHandler<CharT>::StringBuilder::append(const CharT* begin,
const CharT* end) {
return buffer.append(begin, end);
}

template <typename CharT>
JSONParser<CharT>::~JSONParser() {
for (size_t i = 0; i < stack.length(); i++) {
Expand Down
22 changes: 18 additions & 4 deletions js/src/vm/JSONParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
#include "jspubtd.h"

#include "ds/IdValuePair.h"
#include "util/StringBuffer.h"
#include "vm/StringType.h"

namespace js {

class JSStringBuilder;

enum class JSONToken {
String,
Number,
Expand Down Expand Up @@ -268,6 +267,16 @@ class MOZ_STACK_CLASS JSONFullParseHandler
using CharPtr = mozilla::RangedPtr<const CharT>;

public:
class StringBuilder {
public:
JSStringBuilder buffer;

explicit StringBuilder(JSContext* cx) : buffer(cx) {}

bool append(char16_t c);
bool append(const CharT* begin, const CharT* end);
};

JSONFullParseHandler(JSContext* cx, ParseType parseType)
: Base(cx, parseType) {}

Expand All @@ -280,17 +289,22 @@ class MOZ_STACK_CLASS JSONFullParseHandler
template <JSONStringType ST>
inline bool setStringValue(CharPtr start, size_t length);
template <JSONStringType ST>
inline bool setStringValue(JSStringBuilder& builder);
inline bool setStringValue(StringBuilder& builder);

void reportError(const char* msg, const char* lineString,
const char* columnString);
};

template <typename CharT>
class MOZ_STACK_CLASS JSONParser {
using Tokenizer = JSONTokenizer<CharT, JSONParser<CharT>, JSStringBuilder>;
using Handler = JSONFullParseHandler<CharT>;

public:
using StringBuilder = typename Handler::StringBuilder;

private:
using Tokenizer = JSONTokenizer<CharT, JSONParser<CharT>, StringBuilder>;

public:
Handler handler;
Tokenizer tokenizer;
Expand Down

0 comments on commit 99b4441

Please sign in to comment.