Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/emrun_postjs.js
4128 views
1
/**
2
* @license
3
* Copyright 2013 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*
6
* This file gets implicatly injected as a `--post-js` file when
7
* emcc is run with `--emrun`
8
*/
9
10
if (typeof window == "object" && (typeof ENVIRONMENT_IS_PTHREAD == 'undefined' || !ENVIRONMENT_IS_PTHREAD)) {
11
var emrun_register_handlers = () => {
12
// When C code exit()s, we may still have remaining stdout and stderr
13
// messages in flight. In that case, we can't close the browser until all
14
// those XHRs have finished, so the following state variables track that all
15
// communication is done, after which we can close.
16
var emrun_num_post_messages_in_flight = 0;
17
var emrun_should_close_itself = false;
18
var postExit = (msg) => {
19
var http = new XMLHttpRequest();
20
// Don't do this immediately, this may race with the notification about
21
// the return code reaching the server. Send a *sync* xhr so that we know
22
// for sure that the server has gotten the return code before we continue.
23
http.open("POST", "stdio.html", false);
24
http.send(msg);
25
try {
26
// Try closing the current browser window, since it exit()ed itself.
27
// This can shut down the browser process and then emrun does not need
28
// to kill the whole browser process.
29
window.close();
30
} catch(e) {}
31
};
32
var post = (msg) => {
33
var http = new XMLHttpRequest();
34
++emrun_num_post_messages_in_flight;
35
http.onreadystatechange = () => {
36
if (http.readyState == 4 /*DONE*/) {
37
if (--emrun_num_post_messages_in_flight == 0 && emrun_should_close_itself) {
38
postExit('^exit^'+EXITSTATUS);
39
}
40
}
41
}
42
http.open("POST", "stdio.html", true);
43
http.send(msg);
44
};
45
// If the address contains localhost, or we are running the page from port
46
// 6931, we can assume we're running the test runner and should post stdout
47
// logs.
48
if (document.URL.search("localhost") != -1 || document.URL.search(":6931/") != -1) {
49
var emrun_http_sequence_number = 1;
50
var prevPrint = out;
51
var prevErr = err;
52
addOnExit(() => {
53
if (emrun_num_post_messages_in_flight == 0) {
54
postExit('^exit^'+EXITSTATUS);
55
} else {
56
emrun_should_close_itself = true;
57
}
58
});
59
out = (text) => {
60
post('^out^'+(emrun_http_sequence_number++)+'^'+encodeURIComponent(text));
61
prevPrint(text);
62
};
63
err = (text) => {
64
post('^err^'+(emrun_http_sequence_number++)+'^'+encodeURIComponent(text));
65
prevErr(text);
66
};
67
68
// Notify emrun web server that this browser has successfully launched the
69
// page. Note that we may need to wait for the server to be ready.
70
var tryToSendPageload = () => {
71
try {
72
post('^pageload^');
73
} catch (e) {
74
setTimeout(tryToSendPageload, 50);
75
}
76
};
77
tryToSendPageload();
78
}
79
};
80
81
// POSTs the given binary data represented as a (typed) array data back to the
82
// emrun-based web server.
83
// To use from C code, call e.g:
84
// EM_ASM({emrun_file_dump("file.dat", HEAPU8.subarray($0, $0 + $1));}, my_data_pointer, my_data_pointer_byte_length);
85
var emrun_file_dump = (filename, data) => {
86
var http = new XMLHttpRequest();
87
out(`Dumping out file "${filename}" with ${data.length} bytes of data.`);
88
http.open("POST", "stdio.html?file=" + filename, true);
89
http.send(data); // XXX this does not work in workers, for some odd reason (issue #2681)
90
};
91
92
if (typeof document != 'undefined') {
93
emrun_register_handlers();
94
}
95
}
96
97