diff options
-rw-r--r-- | src/binaryen-c.cpp | 5 | ||||
-rw-r--r-- | src/binaryen-c.h | 5 | ||||
-rw-r--r-- | src/passes/pass.cpp | 2 | ||||
-rw-r--r-- | src/support/colors.cpp | 9 | ||||
-rw-r--r-- | src/support/colors.h | 3 | ||||
-rw-r--r-- | src/tools/asm2wasm.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-as.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-ctor-eval.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-dis.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-emscripten-finalize.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-metadce.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-opt.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-reduce.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm2js.cpp | 2 | ||||
-rw-r--r-- | test/example/c-api-kitchen-sink.c | 16 |
15 files changed, 43 insertions, 15 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 37d2c2142..be06cce83 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -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" @@ -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 diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 310ca3a2a..820b23cca 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -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 diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index bb1a062e7..84914f819 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -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); diff --git a/src/support/colors.cpp b/src/support/colors.cpp index 6d06b69fd..d8a2c7f52 100644 --- a/src/support/colors.cpp +++ b/src/support/colors.cpp @@ -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> @@ -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; } } @@ -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); } diff --git a/src/support/colors.h b/src/support/colors.h index 68a251969..f2c446025 100644 --- a/src/support/colors.h +++ b/src/support/colors.h @@ -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); diff --git a/src/tools/asm2wasm.cpp b/src/tools/asm2wasm.cpp index b1196850b..0077b9448 100644 --- a/src/tools/asm2wasm.cpp +++ b/src/tools/asm2wasm.cpp @@ -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", diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp index d45f10f81..2203aa83c 100644 --- a/src/tools/wasm-as.cpp +++ b/src/tools/wasm-as.cpp @@ -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", diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index 6a967ece7..293c675d1 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -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", diff --git a/src/tools/wasm-dis.cpp b/src/tools/wasm-dis.cpp index 1dbe97656..6c1ed1524 100644 --- a/src/tools/wasm-dis.cpp +++ b/src/tools/wasm-dis.cpp @@ -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", diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp index e524e4732..90183ab48 100644 --- a/src/tools/wasm-emscripten-finalize.cpp +++ b/src/tools/wasm-emscripten-finalize.cpp @@ -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", diff --git a/src/tools/wasm-metadce.cpp b/src/tools/wasm-metadce.cpp index c87da8c9a..879919c84 100644 --- a/src/tools/wasm-metadce.cpp +++ b/src/tools/wasm-metadce.cpp @@ -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", diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp index 6d78a209b..d692d598a 100644 --- a/src/tools/wasm-opt.cpp +++ b/src/tools/wasm-opt.cpp @@ -88,7 +88,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", diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index cea415a47..36b988091 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -1113,7 +1113,7 @@ int main(int argc, const char* argv[]) { } if (!binary) { - Colors::disable(); + Colors::setEnabled(false); } std::cerr << "|wasm-reduce\n"; diff --git a/src/tools/wasm2js.cpp b/src/tools/wasm2js.cpp index 731efbee0..8e6ab074f 100644 --- a/src/tools/wasm2js.cpp +++ b/src/tools/wasm2js.cpp @@ -815,7 +815,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", "", diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 9e996a881..1761ab0bb 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -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(); @@ -822,6 +837,7 @@ int main() { test_interpret(); test_nonvalid(); test_tracing(); + test_color_status(); return 0; } |