diff --git a/cores/esp8266/abi.cpp b/cores/esp8266/abi.cpp index 8001c185a..aa4d29f99 100644 --- a/cores/esp8266/abi.cpp +++ b/cores/esp8266/abi.cpp @@ -127,7 +127,24 @@ void __throw_out_of_range(const char* str) (void) str; panic(); } + +void __throw_bad_cast(void) +{ + panic(); } +void __throw_ios_failure(const char* str) +{ + (void) str; + panic(); +} + +void __throw_runtime_error(const char* str) +{ + (void) str; + panic(); +} +} // namespace std + // TODO: rebuild windows toolchain to make this unnecessary: void* __dso_handle; diff --git a/cores/esp8266/libc_replacements.c b/cores/esp8266/libc_replacements.c index fe111ba4c..fc6c2f142 100644 --- a/cores/esp8266/libc_replacements.c +++ b/cores/esp8266/libc_replacements.c @@ -84,8 +84,9 @@ int ICACHE_RAM_ATTR _read_r(struct _reent* unused, int file, char *ptr, int len) int ICACHE_RAM_ATTR _write_r(struct _reent* r, int file, char *ptr, int len) { (void) r; + int pos = len; if (file == STDOUT_FILENO) { - while(len--) { + while(pos--) { ets_putc(*ptr); ++ptr; } @@ -93,6 +94,8 @@ int ICACHE_RAM_ATTR _write_r(struct _reent* r, int file, char *ptr, int len) { return len; } +int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) __attribute__((weak)); + int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) { (void) r; if (file->_file == STDOUT_FILENO) { diff --git a/tests/device/test_iostream/test_iostream.ino b/tests/device/test_iostream/test_iostream.ino new file mode 100644 index 000000000..b644882bf --- /dev/null +++ b/tests/device/test_iostream/test_iostream.ino @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +BS_ENV_DECLARE(); + +void setup() +{ + Serial.begin(115200); + BS_RUN(Serial); +} + +TEST_CASE("can print to std::cout", "[iostream]") +{ + std::stringstream test_stream(""); + test_stream << "hello stream"; + + // empty the RX buffer, just in case + Serial.readString(); + + USC0(0) |= (1 << UCLBE); // enable loopback + std::cout << test_stream.str().c_str() << std::endl; + delay(100); + USC0(0) &= ~(1 << UCLBE); // disable loopback + + String result = Serial.readStringUntil('\n'); + + Serial.printf("result: '%s'\n", result.c_str()); + + CHECK(result == test_stream.str().c_str()); +} + +void loop() { }