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
5 changes: 5 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "ir/utils.h"
#include "pass.h"
#include "shell-interface.h"
#include "support/colors.h"
#include "wasm-binary.h"
#include "wasm-builder.h"
#include "wasm-interpreter.h"
Expand Down Expand Up @@ -3824,6 +3825,10 @@ BinaryenGetFunctionTypeBySignature(BinaryenModuleRef module,
return NULL;
}

void BinaryenSetColorsEnabled(int enabled) { Colors::setEnabled(enabled); }

int BinaryenAreColorsEnabled() { return Colors::isEnabled(); }

#ifdef __EMSCRIPTEN__
// Override atexit - we don't need any global ctors to actually run, and
// otherwise we get clutter in the output in debug builds
Expand Down
5 changes: 5 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,11 @@ BinaryenGetFunctionTypeBySignature(BinaryenModuleRef module,
BinaryenType* paramTypes,
BinaryenIndex numParams);

// Enable or disable coloring for the WASM printer
void BinaryenSetColorsEnabled(int enabled);

// Query whether color is enable for the WASM printer
int BinaryenAreColorsEnabled();
#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ static void dumpWast(Name name, Module* wasm) {
fullName += std::to_string(getpid()) + '-';
#endif
fullName += numstr + "-" + name.str + ".wasm";
Colors::disable();
Colors::setEnabled(false);
ModuleWriter writer;
writer.setBinary(false); // TODO: add an option for binary
writer.write(*wasm, fullName);
Expand Down
9 changes: 5 additions & 4 deletions src/support/colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
#include <ostream>

namespace {
bool colors_disabled = false;
bool colors_enabled = true;
} // anonymous namespace

void Colors::disable() { colors_disabled = true; }
void Colors::setEnabled(bool enabled) { colors_enabled = enabled; }
bool Colors::isEnabled() { return colors_enabled; }

#if defined(__linux__) || defined(__APPLE__)
#include <unistd.h>
Expand All @@ -34,7 +35,7 @@ void Colors::outputColorCode(std::ostream& stream, const char* colorCode) {
(isatty(STDOUT_FILENO) &&
(!getenv("COLORS") || getenv("COLORS")[0] != '0')); // implicit
}();
if (has_color && !colors_disabled) {
if (has_color && colors_enabled) {
stream << colorCode;
}
}
Expand All @@ -50,7 +51,7 @@ void Colors::outputColorCode(std::ostream& stream, const WORD& colorCode) {
}();
static HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
static HANDLE hStderr = GetStdHandle(STD_ERROR_HANDLE);
if (has_color && !colors_disabled)
if (has_color && colors_enabled)
SetConsoleTextAttribute(&stream == &std::cout ? hStdout : hStderr,
colorCode);
}
Expand Down
3 changes: 2 additions & 1 deletion src/support/colors.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#include <iosfwd>

namespace Colors {
void disable();
void setEnabled(bool enabled);
bool isEnabled();

#if defined(__linux__) || defined(__APPLE__)
void outputColorCode(std::ostream& stream, const char* colorCode);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/asm2wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main(int argc, const char* argv[]) {
Options::Arguments::One,
[](Options* o, const std::string& argument) {
o->extra["output"] = argument;
Colors::disable();
Colors::setEnabled(false);
})
.add(
"--mapped-globals",
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main(int argc, const char* argv[]) {
Options::Arguments::One,
[](Options* o, const std::string& argument) {
o->extra["output"] = argument;
Colors::disable();
Colors::setEnabled(false);
})
.add("--validate",
"-v",
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm-ctor-eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ int main(int argc, const char* argv[]) {
Options::Arguments::One,
[](Options* o, const std::string& argument) {
o->extra["output"] = argument;
Colors::disable();
Colors::setEnabled(false);
})
.add("--emit-text",
"-S",
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm-dis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int main(int argc, const char* argv[]) {
Options::Arguments::One,
[](Options* o, const std::string& argument) {
o->extra["output"] = argument;
Colors::disable();
Colors::setEnabled(false);
})
.add(
"--source-map",
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm-emscripten-finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int main(int argc, const char* argv[]) {
Options::Arguments::One,
[&outfile](Options*, const std::string& argument) {
outfile = argument;
Colors::disable();
Colors::setEnabled(false);
})
.add("--debuginfo",
"-g",
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm-metadce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ int main(int argc, const char* argv[]) {
Options::Arguments::One,
[](Options* o, const std::string& argument) {
o->extra["output"] = argument;
Colors::disable();
Colors::setEnabled(false);
})
.add("--emit-text",
"-S",
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int main(int argc, const char* argv[]) {
Options::Arguments::One,
[](Options* o, const std::string& argument) {
o->extra["output"] = argument;
Colors::disable();
Colors::setEnabled(false);
})
.add("--emit-text",
"-S",
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm-reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ int main(int argc, const char* argv[]) {
}

if (!binary) {
Colors::disable();
Colors::setEnabled(false);
}

std::cerr << "|wasm-reduce\n";
Expand Down
2 changes: 1 addition & 1 deletion src/tools/wasm2js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ int main(int argc, const char* argv[]) {
Options::Arguments::One,
[](Options* o, const std::string& argument) {
o->extra["output"] = argument;
Colors::disable();
Colors::setEnabled(false);
})
.add("--allow-asserts",
"",
Expand Down
16 changes: 16 additions & 0 deletions test/example/c-api-kitchen-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,21 @@ void test_tracing() {
BinaryenSetAPITracing(0);
}

void test_color_status() {
int i;

// save old state
const int old_state = BinaryenAreColorsEnabled();

// Check that we can set the state to both {0, 1}
for(i = 0; i <= 1; i++){
BinaryenSetColorsEnabled(i);
assert(BinaryenAreColorsEnabled() == i);
}

BinaryenSetColorsEnabled(old_state);
}

int main() {
test_types();
test_core();
Expand All @@ -822,6 +837,7 @@ int main() {
test_interpret();
test_nonvalid();
test_tracing();
test_color_status();

return 0;
}