1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Fix iostream related issues (#5047)

* add stubs for more exception throw calls

Fixes https://github.com/esp8266/Arduino/issues/3358

* libc: make putc_r implementation weak

newlib provides its own implementation of _putc_r, which will call
_write_r (possibly after buffering). Make our implementation weak to
allow using the one from newlib.

Fixes https://github.com/esp8266/Arduino/issues/4630

* libc: fix incorrect return value of _write_r call

Should return number of bytes written, actually returned zero. This
resulted in std::cout going into failed state after the first write.

* tests: add test for output to std::cout
This commit is contained in:
Ivan Grokhotkov 2018-08-27 01:47:01 +08:00 committed by Develo
parent 0da54d88a9
commit 0713a01db8
3 changed files with 55 additions and 1 deletions

View File

@ -127,7 +127,24 @@ void __throw_out_of_range(const char* str)
(void) str; (void) str;
panic(); 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: // TODO: rebuild windows toolchain to make this unnecessary:
void* __dso_handle; void* __dso_handle;

View File

@ -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) { int ICACHE_RAM_ATTR _write_r(struct _reent* r, int file, char *ptr, int len) {
(void) r; (void) r;
int pos = len;
if (file == STDOUT_FILENO) { if (file == STDOUT_FILENO) {
while(len--) { while(pos--) {
ets_putc(*ptr); ets_putc(*ptr);
++ptr; ++ptr;
} }
@ -93,6 +94,8 @@ int ICACHE_RAM_ATTR _write_r(struct _reent* r, int file, char *ptr, int len) {
return 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) { int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) {
(void) r; (void) r;
if (file->_file == STDOUT_FILENO) { if (file->_file == STDOUT_FILENO) {

View File

@ -0,0 +1,34 @@
#include <ESP8266WiFi.h>
#include <BSTest.h>
#include <sstream>
#include <iostream>
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() { }