Skip to content

Commit 0713a01

Browse files
igrrdevyte
authored andcommitted
Fix iostream related issues (#5047)
* add stubs for more exception throw calls Fixes #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 #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
1 parent 0da54d8 commit 0713a01

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

cores/esp8266/abi.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,24 @@ void __throw_out_of_range(const char* str)
127127
(void) str;
128128
panic();
129129
}
130+
131+
void __throw_bad_cast(void)
132+
{
133+
panic();
134+
}
135+
136+
void __throw_ios_failure(const char* str)
137+
{
138+
(void) str;
139+
panic();
140+
}
141+
142+
void __throw_runtime_error(const char* str)
143+
{
144+
(void) str;
145+
panic();
130146
}
147+
} // namespace std
131148

132149
// TODO: rebuild windows toolchain to make this unnecessary:
133150
void* __dso_handle;

cores/esp8266/libc_replacements.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,18 @@ int ICACHE_RAM_ATTR _read_r(struct _reent* unused, int file, char *ptr, int len)
8484

8585
int ICACHE_RAM_ATTR _write_r(struct _reent* r, int file, char *ptr, int len) {
8686
(void) r;
87+
int pos = len;
8788
if (file == STDOUT_FILENO) {
88-
while(len--) {
89+
while(pos--) {
8990
ets_putc(*ptr);
9091
++ptr;
9192
}
9293
}
9394
return len;
9495
}
9596

97+
int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) __attribute__((weak));
98+
9699
int ICACHE_RAM_ATTR _putc_r(struct _reent* r, int c, FILE* file) {
97100
(void) r;
98101
if (file->_file == STDOUT_FILENO) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <ESP8266WiFi.h>
2+
#include <BSTest.h>
3+
#include <sstream>
4+
#include <iostream>
5+
6+
BS_ENV_DECLARE();
7+
8+
void setup()
9+
{
10+
Serial.begin(115200);
11+
BS_RUN(Serial);
12+
}
13+
14+
TEST_CASE("can print to std::cout", "[iostream]")
15+
{
16+
std::stringstream test_stream("");
17+
test_stream << "hello stream";
18+
19+
// empty the RX buffer, just in case
20+
Serial.readString();
21+
22+
USC0(0) |= (1 << UCLBE); // enable loopback
23+
std::cout << test_stream.str().c_str() << std::endl;
24+
delay(100);
25+
USC0(0) &= ~(1 << UCLBE); // disable loopback
26+
27+
String result = Serial.readStringUntil('\n');
28+
29+
Serial.printf("result: '%s'\n", result.c_str());
30+
31+
CHECK(result == test_stream.str().c_str());
32+
}
33+
34+
void loop() { }

0 commit comments

Comments
 (0)