Skip to content

Commit

Permalink
Bug 1794063 - Create an SEprinter for printing via printf_stderr r=jonco
Browse files Browse the repository at this point in the history
  • Loading branch information
hotsphink committed Jan 12, 2024
1 parent f109889 commit dba58f4
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions js/public/Printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define js_Printer_h

#include "mozilla/Attributes.h"
#include "mozilla/glue/Debug.h"
#include "mozilla/Range.h"

#include <stdarg.h>
Expand Down Expand Up @@ -272,10 +273,11 @@ class JS_PUBLIC_API StringPrinter : public GenericPrinter {
// success.
char* reserve(size_t len);

// Puts |len| characters from |s| at the current position and
// return true on success, false on failure.
// Puts |len| characters from |s| at the current position. May OOM, which must
// be checked by testing the return value of releaseJS() at the end of
// printing.
virtual void put(const char* s, size_t len) final;
using GenericPrinter::put; // pick up |inline bool put(const char* s);|
using GenericPrinter::put; // pick up |put(const char* s);|

virtual bool canPutFromIndex() const final { return true; }
virtual void putFromIndex(size_t index, size_t length) final {
Expand Down Expand Up @@ -337,10 +339,24 @@ class JS_PUBLIC_API Fprinter final : public GenericPrinter {
void flush() override;
void finish();

// Puts |len| characters from |s| at the current position and
// return true on success, false on failure.
virtual void put(const char* s, size_t len) override;
using GenericPrinter::put; // pick up |inline bool put(const char* s);|
// Puts |len| characters from |s| at the current position. Errors may be
// detected with hadOutOfMemory() (which will be set for any fwrite() error,
// not just OOM.)
void put(const char* s, size_t len) override;
using GenericPrinter::put; // pick up |put(const char* s);|
};

// SEprinter, print using printf_stderr (goes to Android log, Windows debug,
// else just stderr).
class SEprinter final : public GenericPrinter {
public:
constexpr SEprinter() {}

// Puts |len| characters from |s| at the current position. Ignores errors.
virtual void put(const char* s, size_t len) override {
printf_stderr("%.*s", int(len), s);
}
using GenericPrinter::put; // pick up |put(const char* s);|
};

// LSprinter, is similar to Sprinter except that instead of using an
Expand Down Expand Up @@ -373,10 +389,9 @@ class JS_PUBLIC_API LSprinter final : public GenericPrinter {
// Drop the current string, and let them be free with the LifoAlloc.
void clear();

// Puts |len| characters from |s| at the current position and
// return true on success, false on failure.
// Puts |len| characters from |s| at the current position.
virtual void put(const char* s, size_t len) override;
using GenericPrinter::put; // pick up |inline bool put(const char* s);|
using GenericPrinter::put; // pick up |put(const char* s);|
};

// Escaping printers work like any other printer except that any added character
Expand Down

0 comments on commit dba58f4

Please sign in to comment.