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