Skip to content

Commit f4e6a1c

Browse files
committed
Merge pull request #2771 from juj/implement_fflush
implement_fflush
2 parents ab883fd + 15bb2cf commit f4e6a1c

File tree

5 files changed

+196
-5
lines changed

5 files changed

+196
-5
lines changed

src/library.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,10 @@ LibraryManager.library = {
24392439
fflush: function(stream) {
24402440
// int fflush(FILE *stream);
24412441
// http://pubs.opengroup.org/onlinepubs/000095399/functions/fflush.html
2442-
// we don't currently perform any user-space buffering of data
2442+
stream = FS.getStreamFromPtr(stream);
2443+
if (stream.stream_ops.flush) {
2444+
stream.stream_ops.flush(stream);
2445+
}
24432446
},
24442447
fgetc__deps: ['$FS', 'fread'],
24452448
fgetc__postset: '_fgetc.ret = allocate([0], "i8", ALLOC_STATIC);',

src/library_tty.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ mergeInto(LibraryManager.library, {
4141
},
4242
close: function(stream) {
4343
// flush any pending line data
44-
if (stream.tty.output.length) {
45-
stream.tty.ops.put_char(stream.tty, {{{ charCode('\n') }}});
46-
}
44+
stream.tty.ops.flush(stream.tty);
45+
},
46+
flush: function(stream) {
47+
stream.tty.ops.flush(stream.tty);
4748
},
4849
read: function(stream, buffer, offset, length, pos /* ignored */) {
4950
if (!stream.tty || !stream.tty.ops.get_char) {
@@ -123,6 +124,12 @@ mergeInto(LibraryManager.library, {
123124
}
124125
return tty.input.shift();
125126
},
127+
flush: function(tty) {
128+
if (tty.output && tty.output.length > 0) {
129+
Module['print'](tty.output.join(''));
130+
tty.output = [];
131+
}
132+
},
126133
put_char: function(tty, val) {
127134
if (val === null || val === {{{ charCode('\n') }}}) {
128135
Module['print'](tty.output.join(''));
@@ -140,7 +147,13 @@ mergeInto(LibraryManager.library, {
140147
} else {
141148
tty.output.push(TTY.utf8.processCChar(val));
142149
}
143-
}
150+
},
151+
flush: function(tty) {
152+
if (tty.output && tty.output.length > 0) {
153+
Module['printErr'](tty.output.join(''));
154+
tty.output = [];
155+
}
156+
}
144157
}
145158
}
146159
});

tests/test_browser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,9 @@ def test_webgl_context_attributes(self):
11381138
def test_emscripten_get_now(self):
11391139
self.btest('emscripten_get_now.cpp', '1')
11401140

1141+
def test_fflush(self):
1142+
self.btest('test_fflush.cpp', '0', args=['-s', 'NO_EXIT_RUNTIME=1', '--shell-file', path_from_root('tests', 'test_fflush.html')])
1143+
11411144
def test_file_db(self):
11421145
secret = str(time.time())
11431146
open('moar.txt', 'w').write(secret)

tests/test_fflush.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdio.h>
2+
3+
int main()
4+
{
5+
printf("hello!");
6+
fflush(stdout);
7+
fprintf(stderr, "hello from stderr too!");
8+
fflush(stderr);
9+
}

tests/test_fflush.html

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<!doctype html>
2+
<html lang="en-us">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6+
<title>Emscripten-Generated Code</title>
7+
<style>
8+
.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
9+
textarea.emscripten { font-family: monospace; width: 80%; }
10+
div.emscripten { text-align: center; }
11+
div.emscripten_border { border: 1px solid black; }
12+
/* the canvas *must not* have any border or padding, or mouse coords will be wrong */
13+
canvas.emscripten { border: 0px none; }
14+
15+
.spinner {
16+
height: 50px;
17+
width: 50px;
18+
margin: 0px auto;
19+
-webkit-animation: rotation .8s linear infinite;
20+
-moz-animation: rotation .8s linear infinite;
21+
-o-animation: rotation .8s linear infinite;
22+
animation: rotation 0.8s linear infinite;
23+
border-left: 10px solid rgb(0,150,240);
24+
border-right: 10px solid rgb(0,150,240);
25+
border-bottom: 10px solid rgb(0,150,240);
26+
border-top: 10px solid rgb(100,0,200);
27+
border-radius: 100%;
28+
background-color: rgb(200,100,250);
29+
}
30+
@-webkit-keyframes rotation {
31+
from {-webkit-transform: rotate(0deg);}
32+
to {-webkit-transform: rotate(360deg);}
33+
}
34+
@-moz-keyframes rotation {
35+
from {-moz-transform: rotate(0deg);}
36+
to {-moz-transform: rotate(360deg);}
37+
}
38+
@-o-keyframes rotation {
39+
from {-o-transform: rotate(0deg);}
40+
to {-o-transform: rotate(360deg);}
41+
}
42+
@keyframes rotation {
43+
from {transform: rotate(0deg);}
44+
to {transform: rotate(360deg);}
45+
}
46+
47+
</style>
48+
</head>
49+
<body>
50+
<hr/>
51+
<figure style="overflow:visible;" id="spinner"><div class="spinner"></div><center style="margin-top:0.5em"><strong>emscripten</strong></center></figure>
52+
<div class="emscripten" id="status">Downloading...</div>
53+
<div class="emscripten">
54+
<progress value="0" max="100" id="progress" hidden=1></progress>
55+
</div>
56+
<div class="emscripten_border">
57+
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
58+
</div>
59+
<hr/>
60+
<div class="emscripten">
61+
<input type="checkbox" id="resize">Resize canvas
62+
<input type="checkbox" id="pointerLock" checked>Lock/hide mouse pointer
63+
&nbsp;&nbsp;&nbsp;
64+
<input type="button" value="Fullscreen" onclick="Module.requestFullScreen(document.getElementById('pointerLock').checked,
65+
document.getElementById('resize').checked)">
66+
</div>
67+
68+
<hr/>
69+
<textarea class="emscripten" id="output" rows="8"></textarea>
70+
<hr>
71+
<script type='text/javascript'>
72+
var statusElement = document.getElementById('status');
73+
var progressElement = document.getElementById('progress');
74+
var spinnerElement = document.getElementById('spinner');
75+
76+
var Module = {
77+
gotStdout: false,
78+
gotStderr: false,
79+
preRun: [],
80+
postRun: [],
81+
82+
reportSuccess: function() {
83+
var xhr = new XMLHttpRequest();
84+
xhr.open('GET', 'http://localhost:8888/report_result?0');
85+
xhr.send();
86+
setTimeout(function() { window.close() }, 1000);
87+
},
88+
89+
print: (function() {
90+
var element = document.getElementById('output');
91+
if (element) element.value = ''; // clear browser cache
92+
return function(text) {
93+
text = Array.prototype.slice.call(arguments).join(' ');
94+
// These replacements are necessary if you render to raw HTML
95+
//text = text.replace(/&/g, "&amp;");
96+
//text = text.replace(/</g, "&lt;");
97+
//text = text.replace(/>/g, "&gt;");
98+
//text = text.replace('\n', '<br>', 'g');
99+
console.log(text);
100+
if (text == 'hello!') { Module.gotStdout = true; if (Module.gotStderr) Module.reportSuccess(); }
101+
if (element) {
102+
element.value += text + "\n";
103+
element.scrollTop = element.scrollHeight; // focus on bottom
104+
}
105+
};
106+
})(),
107+
printErr: function(text) {
108+
text = Array.prototype.slice.call(arguments).join(' ');
109+
if (text == 'hello from stderr too!') { Module.gotStderr = true; if (Module.gotStdout) Module.reportSuccess(); }
110+
if (0) { // XXX disabled for safety typeof dump == 'function') {
111+
dump(text + '\n'); // fast, straight to the real console
112+
} else {
113+
console.error(text);
114+
}
115+
},
116+
canvas: (function() {
117+
var canvas = document.getElementById('canvas');
118+
119+
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
120+
// application robust, you may want to override this behavior before shipping!
121+
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
122+
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
123+
124+
return canvas;
125+
})(),
126+
setStatus: function(text) {
127+
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
128+
if (text === Module.setStatus.text) return;
129+
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
130+
var now = Date.now();
131+
if (m && now - Date.now() < 30) return; // if this is a progress update, skip it if too soon
132+
if (m) {
133+
text = m[1];
134+
progressElement.value = parseInt(m[2])*100;
135+
progressElement.max = parseInt(m[4])*100;
136+
progressElement.hidden = false;
137+
spinnerElement.hidden = false;
138+
} else {
139+
progressElement.value = null;
140+
progressElement.max = null;
141+
progressElement.hidden = true;
142+
if (!text) spinnerElement.hidden = true;
143+
}
144+
statusElement.innerHTML = text;
145+
},
146+
totalDependencies: 0,
147+
monitorRunDependencies: function(left) {
148+
this.totalDependencies = Math.max(this.totalDependencies, left);
149+
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
150+
}
151+
};
152+
Module.setStatus('Downloading...');
153+
window.onerror = function() {
154+
Module.setStatus('Exception thrown, see JavaScript console');
155+
spinnerElement.style.display = 'none';
156+
Module.setStatus = function(text) {
157+
if (text) Module.printErr('[post-exception status] ' + text);
158+
};
159+
};
160+
</script>
161+
{{{ SCRIPT }}}
162+
</body>
163+
</html>

0 commit comments

Comments
 (0)