diff --git a/package-lock.json b/package-lock.json index 432f9944ea5..55890ac83ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13213,6 +13213,10 @@ "resolved": "packages/php-wasm/node", "link": true }, + "node_modules/@php-wasm/node-5-2": { + "resolved": "packages/php-wasm/node-builds/5-2", + "link": true + }, "node_modules/@php-wasm/node-7-4": { "resolved": "packages/php-wasm/node-builds/7-4", "link": true @@ -13265,6 +13269,10 @@ "resolved": "packages/php-wasm/web", "link": true }, + "node_modules/@php-wasm/web-5-2": { + "resolved": "packages/php-wasm/web-builds/5-2", + "link": true + }, "node_modules/@php-wasm/web-7-4": { "resolved": "packages/php-wasm/web-builds/7-4", "link": true @@ -53796,6 +53804,15 @@ "npm": ">=10.2.3" } }, + "packages/php-wasm/node-builds/5-2": { + "name": "@php-wasm/node-5-2", + "version": "3.1.19", + "license": "GPL-2.0-or-later", + "engines": { + "node": ">=20.10.0", + "npm": ">=10.2.3" + } + }, "packages/php-wasm/node-builds/7-4": { "name": "@php-wasm/node-7-4", "version": "3.1.19", @@ -53908,6 +53925,15 @@ "npm": ">=10.2.3" } }, + "packages/php-wasm/web-builds/5-2": { + "name": "@php-wasm/web-5-2", + "version": "3.1.19", + "license": "GPL-2.0-or-later", + "engines": { + "node": ">=20.10.0", + "npm": ">=10.2.3" + } + }, "packages/php-wasm/web-builds/7-4": { "name": "@php-wasm/web-7-4", "version": "3.1.19", diff --git a/packages/php-wasm/compile/build.js b/packages/php-wasm/compile/build.js index b5002daaed5..b60f6f67242 100644 --- a/packages/php-wasm/compile/build.js +++ b/packages/php-wasm/compile/build.js @@ -287,12 +287,17 @@ await cleanupOldMinorVersions(); // Build the base image await asyncSpawn('make', ['base-image'], { cwd: sourceDir, stdio: 'inherit' }); +const phpVersionForDockerfile = getArg('PHP_VERSION').replace('PHP_VERSION=', ''); +const dockerfile = phpVersionForDockerfile.startsWith('5.2') + ? 'php/Dockerfile-5-2' + : 'php/Dockerfile'; + await asyncSpawn( 'docker', [ 'build', '-f', - 'php/Dockerfile', + dockerfile, '..', '--tag=php-wasm', '--progress=plain', diff --git a/packages/php-wasm/compile/php-post-message-to-js/post_message_to_js.c b/packages/php-wasm/compile/php-post-message-to-js/post_message_to_js.c index 539d60cc27b..218bbb058cb 100644 --- a/packages/php-wasm/compile/php-post-message-to-js/post_message_to_js.c +++ b/packages/php-wasm/compile/php-post-message-to-js/post_message_to_js.c @@ -3,6 +3,11 @@ #include "ext/standard/info.h" #include +/* PHP_FE_END was added in PHP 5.3.7; provide fallback for PHP 5.2 */ +#ifndef PHP_FE_END +#define PHP_FE_END {NULL, NULL, NULL} +#endif + /** * Provided by php_wasm.c: */ @@ -14,16 +19,22 @@ PHP_FUNCTION(post_message_to_js) char *data; int data_len; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &data, &data_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == FAILURE) { return; } char *response; size_t response_len = js_module_onMessage(data, &response); - if (response_len != -1) { + if (response_len != (size_t)-1) { +#if PHP_MAJOR_VERSION >= 7 zend_string *return_string = zend_string_init(response, response_len, 0); free(response); RETURN_NEW_STR(return_string); +#else + RETVAL_STRINGL(response, response_len, 1); + free(response); + return; +#endif } else { RETURN_NULL(); } @@ -39,6 +50,12 @@ PHP_MINFO_FUNCTION(post_message_to_js) } /* }}} */ +/* {{{ arginfo */ +ZEND_BEGIN_ARG_INFO_EX(arginfo_post_message_to_js, 0, 1, 1) +ZEND_ARG_INFO(0, data) +ZEND_END_ARG_INFO() +/* }}} */ + /* {{{ post_message_to_js_functions[] */ const zend_function_entry post_message_to_js_functions[] = { PHP_FE(post_message_to_js, arginfo_post_message_to_js) diff --git a/packages/php-wasm/compile/php-post-message-to-js/post_message_to_js.h b/packages/php-wasm/compile/php-post-message-to-js/post_message_to_js.h index 67fddb9f39e..357dc6d3296 100644 --- a/packages/php-wasm/compile/php-post-message-to-js/post_message_to_js.h +++ b/packages/php-wasm/compile/php-post-message-to-js/post_message_to_js.h @@ -4,10 +4,6 @@ extern zend_module_entry post_message_to_js_module_entry; #define phpext_post_message_to_js_ptr &post_message_to_js_module_entry -ZEND_BEGIN_ARG_INFO_EX(arginfo_post_message_to_js, 0, 1, 1) -ZEND_ARG_INFO(0, data) -ZEND_END_ARG_INFO() - PHP_FUNCTION(post_message_to_js); #define PHP_POST_MESSAGE_TO_JS_VERSION "1.0.0" diff --git a/packages/php-wasm/compile/php-wasm-dns-polyfill/dns_polyfill.c b/packages/php-wasm/compile/php-wasm-dns-polyfill/dns_polyfill.c index 2c96cd96bc9..d51154ce1ae 100644 --- a/packages/php-wasm/compile/php-wasm-dns-polyfill/dns_polyfill.c +++ b/packages/php-wasm/compile/php-wasm-dns-polyfill/dns_polyfill.c @@ -5,6 +5,11 @@ #include "dns_polyfill.h" #include "ext/standard/info.h" +/* ZEND_FE_END was added in PHP 5.3.7; provide fallback for PHP 5.2 */ +#ifndef ZEND_FE_END +#define ZEND_FE_END {NULL, NULL, NULL} +#endif + #ifdef HAVE_SYS_SOCKET_H #include #endif @@ -119,21 +124,27 @@ PHP_FUNCTION(dns_check_record) HEADER *hp; querybuf answer = {0}; char *hostname; +#if PHP_MAJOR_VERSION >= 7 size_t hostname_len; size_t rectype_len = 0; zend_string *rectype = NULL; +#else + int hostname_len; + int rectype_len = 0; + char *rectype = NULL; +#endif int type = DNS_T_MX, i; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &hostname, &hostname_len, &rectype, &rectype_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &hostname, &hostname_len, &rectype, &rectype_len) == FAILURE) { return; } if (hostname_len == 0) { - php_error_docref(NULL, E_WARNING, "Host cannot be empty"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Host cannot be empty"); RETURN_FALSE; } - php_error_docref(NULL, E_WARNING, "dns_check_record() always returns false in PHP.wasm."); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "dns_check_record() always returns false in PHP.wasm."); RETURN_FALSE; } @@ -143,8 +154,13 @@ PHP_FUNCTION(dns_check_record) PHP_FUNCTION(dns_get_record) { char *hostname; +#if PHP_MAJOR_VERSION >= 7 size_t hostname_len; zend_long type_param = PHP_DNS_ANY; +#else + int hostname_len; + long type_param = PHP_DNS_ANY; +#endif zval *authns = NULL, *addtl = NULL; int type_to_fetch; int dns_errno; @@ -155,7 +171,7 @@ PHP_FUNCTION(dns_get_record) int type, first_query = 1, store_results = 1; zend_bool raw = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lz!z!b", + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lz!z!b", &hostname, &hostname_len, &type_param, &authns, &addtl, &raw) == FAILURE) { return; } @@ -173,7 +189,7 @@ PHP_FUNCTION(dns_get_record) } } - php_error_docref(NULL, E_WARNING, "dns_get_record() always returns an empty array in PHP.wasm."); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "dns_get_record() always returns an empty array in PHP.wasm."); /* Initialize the return array */ array_init(return_value); @@ -186,7 +202,11 @@ PHP_FUNCTION(dns_get_record) PHP_FUNCTION(dns_get_mx) { char *hostname; +#if PHP_MAJOR_VERSION >= 7 size_t hostname_len; +#else + int hostname_len; +#endif zval *mx_list, *weight_list = NULL; int count, qdc; u_short type, weight; @@ -196,12 +216,18 @@ PHP_FUNCTION(dns_get_mx) uint8_t *cp, *end; int i; +#if PHP_MAJOR_VERSION >= 7 ZEND_PARSE_PARAMETERS_START(2, 3) Z_PARAM_STRING(hostname, hostname_len) Z_PARAM_ZVAL(mx_list) Z_PARAM_OPTIONAL Z_PARAM_ZVAL(weight_list) ZEND_PARSE_PARAMETERS_END(); +#else + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|z", &hostname, &hostname_len, &mx_list, &weight_list) == FAILURE) { + return; + } +#endif array_init(mx_list); if (!mx_list) { @@ -215,7 +241,7 @@ PHP_FUNCTION(dns_get_mx) } } - php_error_docref(NULL, E_WARNING, "dns_get_mx() always returns an empty array in PHP.wasm."); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "dns_get_mx() always returns an empty array in PHP.wasm."); RETURN_FALSE; } @@ -256,6 +282,31 @@ PHP_MSHUTDOWN_FUNCTION(dns_polyfill) return SUCCESS; } +/* {{{ arginfo definitions */ +ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_check_record, 0, 0, 1) +ZEND_ARG_INFO(0, host) +ZEND_ARG_INFO(0, type) +ZEND_END_ARG_INFO() + +#define arginfo_checkdnsrr arginfo_dns_check_record + +ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_record, 0, 0, 1) +ZEND_ARG_INFO(0, hostname) +ZEND_ARG_INFO(0, type) +ZEND_ARG_ARRAY_INFO(1, authns, 1) +ZEND_ARG_ARRAY_INFO(1, addtl, 1) +ZEND_ARG_INFO(0, raw) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_mx, 0, 0, 2) +ZEND_ARG_INFO(0, hostname) +ZEND_ARG_INFO(1, mxhosts) +ZEND_ARG_INFO(1, weight) +ZEND_END_ARG_INFO() + +#define arginfo_getmxrr arginfo_dns_get_mx +/* }}} */ + /* {{{ dns_polyfill_functions[] */ const zend_function_entry dns_polyfill_functions[] = { ZEND_FE(dns_get_mx, arginfo_dns_get_mx) diff --git a/packages/php-wasm/compile/php-wasm-dns-polyfill/dns_polyfill.h b/packages/php-wasm/compile/php-wasm-dns-polyfill/dns_polyfill.h index 4ca49fd5096..8a4070693ab 100644 --- a/packages/php-wasm/compile/php-wasm-dns-polyfill/dns_polyfill.h +++ b/packages/php-wasm/compile/php-wasm-dns-polyfill/dns_polyfill.h @@ -53,33 +53,8 @@ extern zend_module_entry dns_polyfill_module_entry; #define phpext_dns_polyfill_ptr &dns_polyfill_module_entry -ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_check_record, 0, 0, 1) -ZEND_ARG_INFO(0, host) -ZEND_ARG_INFO(0, type) -ZEND_END_ARG_INFO() - -#define arginfo_checkdnsrr arginfo_dns_check_record - PHP_FUNCTION(dns_check_record); - -ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_record, 0, 0, 1) -ZEND_ARG_INFO(0, hostname) -ZEND_ARG_INFO(0, type) -ZEND_ARG_ARRAY_INFO(1, authns, 1) -ZEND_ARG_ARRAY_INFO(1, addtl, 1) -ZEND_ARG_INFO(0, raw) -ZEND_END_ARG_INFO() - PHP_FUNCTION(dns_get_record); - -ZEND_BEGIN_ARG_INFO_EX(arginfo_dns_get_mx, 0, 0, 2) -ZEND_ARG_INFO(0, hostname) -ZEND_ARG_INFO(1, mxhosts) /* ARRAY_INFO(1, mxhosts, 1) */ -ZEND_ARG_INFO(1, weight) /* ARRAY_INFO(1, weight, 1) */ -ZEND_END_ARG_INFO() - -#define arginfo_getmxrr arginfo_dns_get_mx - PHP_FUNCTION(dns_get_mx); #define PHP_DNS_POLYFILL_VERSION "1.0.0" diff --git a/packages/php-wasm/compile/php-wasm-memory-storage/wasm_memory_storage.c b/packages/php-wasm/compile/php-wasm-memory-storage/wasm_memory_storage.c index f38661df318..1379168f33c 100644 --- a/packages/php-wasm/compile/php-wasm-memory-storage/wasm_memory_storage.c +++ b/packages/php-wasm/compile/php-wasm-memory-storage/wasm_memory_storage.c @@ -22,6 +22,8 @@ #include "Zend/zend_alloc.h" #include "php_wasm_memory_storage.h" +#if PHP_MAJOR_VERSION >= 7 + /** * Allocate a chunk of memory. * @@ -112,6 +114,22 @@ PHP_MSHUTDOWN_FUNCTION(wasm_memory_storage) return SUCCESS; } +#else /* PHP < 7 */ + +/* Custom memory storage is not available in PHP 5.x. + * Provide no-op MINIT/MSHUTDOWN. */ +PHP_MINIT_FUNCTION(wasm_memory_storage) +{ + return SUCCESS; +} + +PHP_MSHUTDOWN_FUNCTION(wasm_memory_storage) +{ + return SUCCESS; +} + +#endif /* PHP_MAJOR_VERSION >= 7 */ + /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(wasm_memory_storage) { diff --git a/packages/php-wasm/compile/php/Dockerfile-5-2 b/packages/php-wasm/compile/php/Dockerfile-5-2 new file mode 100644 index 00000000000..a876033ad78 --- /dev/null +++ b/packages/php-wasm/compile/php/Dockerfile-5-2 @@ -0,0 +1,2499 @@ +FROM playground-php-wasm:base + +# ----------------------------------------------------------------- +# PHP 5.2-specific Dockerfile +# +# This is a standalone Dockerfile for building PHP 5.2 as a WASM +# module. It mirrors the structure of the main Dockerfile but with +# PHP 5.2-specific adjustments throughout (autoconf 2.13, different +# configure flags, no OPcache/mysqlnd/imagick/fibers/phar, etc.). +# ----------------------------------------------------------------- + +ARG WITH_JSPI="no" +ENV PKG_CONFIG_PATH=/root/lib/lib/pkgconfig +ENV PATH="/root/lib/bin:${PATH}" + +ARG PHP_VERSION + +# OPcache was bundled starting in PHP 5.5; accepted but unused here. +ARG WITH_OPCACHE + +# Clone PHP from a tag [ php-$PHP_VERSION ] +RUN git clone https://github.com/php/php-src.git php-src \ + --branch php-$PHP_VERSION \ + --single-branch \ + --depth 1; + +# PHP 5.2's git repo doesn't include pre-generated parser/scanner C files +# (flex/bison outputs). Download them from the release tarball. +RUN wget -q -O /tmp/php.tar.bz2 "https://museum.php.net/php5/php-${PHP_VERSION}.tar.bz2" && \ + cd /root/php-src && \ + tar xjf /tmp/php.tar.bz2 --strip-components=1 \ + "php-${PHP_VERSION}/Zend/zend_language_scanner.c" \ + "php-${PHP_VERSION}/Zend/zend_language_parser.c" \ + "php-${PHP_VERSION}/Zend/zend_language_parser.h" \ + "php-${PHP_VERSION}/Zend/zend_ini_scanner.c" \ + "php-${PHP_VERSION}/Zend/zend_ini_parser.c" \ + "php-${PHP_VERSION}/Zend/zend_ini_parser.h" \ + "php-${PHP_VERSION}/ext/standard/var_unserializer.c" \ + "php-${PHP_VERSION}/ext/standard/url_scanner_ex.c" \ + "php-${PHP_VERSION}/ext/date/lib/parse_date.c" && \ + rm /tmp/php.tar.bz2 && \ + touch Zend/zend_language_scanner.c Zend/zend_language_parser.c \ + Zend/zend_language_parser.h Zend/zend_ini_scanner.c \ + Zend/zend_ini_parser.c Zend/zend_ini_parser.h \ + ext/standard/var_unserializer.c ext/standard/url_scanner_ex.c \ + ext/date/lib/parse_date.c + +# Work around memory leak due to PHP using Emscripten's incomplete mmap/munmap support +COPY ./compile/php-wasm-memory-storage /root/php-src/ext/wasm_memory_storage + +# Polyfill for dns functions +COPY ./compile/php-wasm-dns-polyfill/ /root/php-src/ext/dns_polyfill + +# Polyfill for post_message_to_js functions +COPY ./compile/php-post-message-to-js/ /root/php-src/ext/post_message_to_js + +# Copy the opcache_module.c and php_opcache.h files. +COPY ./compile/opcache/ /root/php-src/ext/opcache + +# Bring in the libraries +RUN mkdir -p /root/lib/include /root/lib/lib /libs +COPY ./compile/libiconv/ /root/builds/libiconv +COPY ./compile/libcurl/ /root/builds/libcurl +COPY ./compile/libopenssl/ /root/builds/libopenssl +COPY ./compile/libpng16/ /root/builds/libpng16 +COPY ./compile/libjpeg/ /root/builds/libjpeg +COPY ./compile/libwebp/ /root/builds/libwebp +COPY ./compile/libaom/ /root/builds/libaom +COPY ./compile/libavif/ /root/builds/libavif +COPY ./compile/libsqlite3/ /root/builds/libsqlite3 +COPY ./compile/libxml2/ /root/builds/libxml2 +COPY ./compile/libz/ /root/builds/libz +COPY ./compile/oniguruma/ /root/builds/oniguruma +COPY ./compile/libImageMagick/ /root/builds/libImageMagick +COPY ./compile/libgd/ /root/builds/libgd + +RUN if [ "$WITH_JSPI" = "yes" ]; then \ + find /root/builds -path '*/jspi/dist/root/lib' -type d -exec cp -r {}/. /root/lib \; ; \ +else \ + find /root/builds -path '*/asyncify/dist/root/lib' -type d -exec cp -r {}/. /root/lib \; ; \ +fi + +COPY ./compile/libzip/ /root/builds-libzip/ +RUN mkdir -p /libs/libzip +RUN if [ "$WITH_JSPI" = "yes" ]; then \ + cp -r /root/builds-libzip/jspi/dist/1.2.0/root/lib /libs/libzip/1.2.0; \ + cp -r /root/builds-libzip/jspi/dist/1.9.2/root/lib /libs/libzip/1.9.2; \ +else \ + cp -r /root/builds-libzip/asyncify/dist/1.2.0/root/lib /libs/libzip/1.2.0; \ + cp -r /root/builds-libzip/asyncify/dist/1.9.2/root/lib /libs/libzip/1.9.2; \ +fi + +COPY ./compile/libopenssl/ /root/builds-libopenssl/ +RUN mkdir -p /libs/libopenssl +RUN if [ "$WITH_JSPI" = "yes" ]; then \ + cp -r /root/builds-libopenssl/jspi/dist/root/lib /libs/libopenssl; \ +else \ + cp -r /root/builds-libopenssl/asyncify/dist/root/lib /libs/libopenssl; \ +fi + +# OPcache was bundled starting in PHP 5.5; always disable for PHP 5.2. +RUN echo -n ' --disable-opcache ' >> /root/.php-configure-flags + +# PHP 5.2 requires autoconf 2.13, updated config.guess/config.sub, and a +# flex wrapper that reports version 2.5.4 (expected by PHP 5.2's buildconf). +RUN cd /tmp && wget -q https://ftp.gnu.org/gnu/autoconf/autoconf-2.13.tar.gz && \ + tar xzf autoconf-2.13.tar.gz && cd autoconf-2.13 && \ + ./configure --program-suffix=-2.13 --quiet && make -s && make install && \ + cd / && rm -rf /tmp/autoconf-2.13* && \ + wget -q -O /root/php-src/config.guess 'https://git.savannah.gnu.org/cgit/config.git/plain/config.guess' && \ + wget -q -O /root/php-src/config.sub 'https://git.savannah.gnu.org/cgit/config.git/plain/config.sub' && \ + chmod +x /root/php-src/config.guess /root/php-src/config.sub && \ + mv /usr/bin/flex /usr/bin/flex.real && \ + printf '#!/bin/bash\nfor a in "$@"; do case "$a" in -V|-v|--version) echo "flex 2.5.4"; exit 0;; esac; done\n/usr/bin/flex.real "$@"\n' > /usr/bin/flex && \ + chmod +x /usr/bin/flex && \ + cd /root/php-src && PHP_AUTOCONF=autoconf-2.13 ./buildconf --force + +# Build PHP + +# The PHP extensions to build: +ARG WITH_CURL +ARG WITH_FILEINFO +ARG WITH_LIBXML +ARG WITH_SOAP +ARG WITH_LIBZIP +ARG WITH_EXIF +ARG WITH_GD +ARG WITH_MBSTRING +ARG WITH_MBREGEX +ARG WITH_CLI_SAPI +ARG WITH_SQLITE +ARG WITH_MYSQL +ARG WITH_OPENSSL +ARG WITH_ICONV +ARG WITH_SOURCEMAPS +ARG OUTPUT_DIR_ON_HOST +ARG WITH_DEBUG +ARG DEBUG_DWARF_COMPILATION_DIR +ARG WITH_WS_NETWORKING_PROXY +ARG WITH_IMAGICK + +# The platform to build for: web or node +ARG EMSCRIPTEN_ENVIRONMENT=web + +RUN touch /root/.configure-flags && \ + touch /root/.emcc-php-wasm-flags && \ + touch /root/.emcc-php-wasm-sources && \ + touch /root/.JS_ABI_EXPORTS && \ + touch /root/.WASM_ABI_EXPORTS + +RUN apt install -y bison; + +# Add libzip and zlib files if needed +# PHP 5.x has a bundled zip extension that uses --enable-zip (not --with-zip) +RUN if [ "$WITH_LIBZIP" = "yes" ]; then \ + cp -r /libs/libzip/1.2.0/* /root/lib; \ + echo -n ' --with-zlib --with-zlib-dir=/root/lib --enable-zip ' >> /root/.php-configure-flags; \ + echo -n ' /root/lib/lib/libz.a ' >> /root/.emcc-php-wasm-sources; \ + fi; + +# Add ncurses if needed and libedit if needed +RUN if [ "$WITH_CLI_SAPI" = "yes" ]; \ + then \ + # --enable-phar is handled separately (PHP 5.2 has no ext/phar) + echo -n ' --enable-cli=static ' >> /root/.php-configure-flags && \ + # Add CLI source files that exist in this PHP version. + # PHP 5.2 ships getopt.c under sapi/cli/; on PHP 5.3+ it + # lives in main/ and is already compiled into libphp.a. + for f in php_cli_process_title.c ps_title.c php_http_parser.c php_cli_server.c php_cli.c getopt.c; do \ + if [ -f "/root/php-src/sapi/cli/$f" ]; then \ + echo -n " /root/php-src/sapi/cli/$f " >> /root/.emcc-php-wasm-sources; \ + fi; \ + done && \ + echo "_run_cli" >> /root/.JS_ABI_EXPORTS && echo "_wasm_add_cli_arg" >> /root/.JS_ABI_EXPORTS && \ + echo -n ' -DWITH_CLI_SAPI=1 ' >> /root/.emcc-php-wasm-flags && \ + if grep -q 'GET_SHELL_CB' /root/php-src/ext/readline/readline_cli.c 2>/dev/null; then \ + /root/replace.sh 's/GET_SHELL_CB\(cb\);/(cb) = php_cli_get_shell_callbacks();/g' /root/php-src/ext/readline/readline_cli.c; \ + fi; \ + # Avoid a symbol conflict between php_cli.c and php_embed.c + if grep -q 'HARDCODED_INI' /root/php-src/sapi/embed/php_embed.c 2>/dev/null; then \ + /root/replace.sh 's/\bHARDCODED_INI\b/HARDCODED_EMBED_INI/g' /root/php-src/sapi/embed/php_embed.c; \ + fi \ + else \ + echo -n ' --disable-cli ' >> /root/.php-configure-flags; \ + fi; + +# Add Libxml2 if needed +RUN if [ "$WITH_LIBXML" = "yes" ]; \ + then \ + set -euxo pipefail;\ + echo -n ' --enable-libxml --with-libxml --with-libxml-dir=/root/lib --enable-dom --enable-xml --enable-simplexml --enable-xmlreader --enable-xmlwriter ' >> /root/.php-configure-flags && \ + echo -n ' /root/lib/lib/libxml2.a ' >> /root/.emcc-php-wasm-sources && \ + # Libxml check is wrong in PHP < 7.4.0. + # In the regular cc it's just a warning, but in the emscripten's emcc that's an error: + perl -pi.bak -e 's/char xmlInitParser/void xmlInitParser/g' /root/php-src/configure; \ + # On PHP < 7.1.0, the dom_iterators.c file implicitly converts *char to const *char causing emcc error + if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "0" ]]; then \ + /root/replace.sh 's/xmlHashScan\(ht, itemHashScanner, iter\);/xmlHashScan(ht, (xmlHashScanner)itemHashScanner, iter);/g' /root/php-src/ext/dom/dom_iterators.c; \ + fi; \ + else \ + echo -n ' --disable-libxml --without-libxml --disable-dom --disable-xml --disable-simplexml --disable-xmlreader --disable-xmlwriter' >> /root/.php-configure-flags; \ + fi + +# Add SOAP if needed (requires libxml) +RUN if [ "$WITH_SOAP" = "yes" ]; \ + then \ + echo -n ' --enable-soap' >> /root/.php-configure-flags; \ + else \ + echo -n ' --disable-soap' >> /root/.php-configure-flags; \ + fi + +# Add sqlite3 if needed +# ext/sqlite3 was added in PHP 5.3. PHP 5.2 only has ext/pdo_sqlite. +RUN if [ "$WITH_SQLITE" = "yes" ]; \ + then \ + echo -n ' --enable-pdo --with-pdo-sqlite=/root/lib ' >> /root/.php-configure-flags; \ + echo -n ' /root/lib/lib/libsqlite3.a ' >> /root/.emcc-php-wasm-sources; \ + else \ + echo -n ' --disable-pdo ' >> /root/.php-configure-flags; \ + fi + +# Add GD if needed +RUN if [ "$WITH_GD" = "yes" ]; \ + then \ + # Use external libgd for PHP 8.1+; fall back to bundled gd for <= 8.0 (and 7.x) to avoid missing symbols + if [[ "${PHP_VERSION:0:1}" -gt "8" || ( "${PHP_VERSION:0:1}" -eq "8" && "${PHP_VERSION:2:1}" -ge "1" ) ]]; then \ + echo -n ' --with-external-gd=/root/lib --enable-gd ' >> /root/.php-configure-flags; \ + echo -n ' /root/lib/lib/libgd.a /root/lib/lib/libjpeg.a /root/lib/lib/libpng16.a /root/lib/lib/libwebp.a /root/lib/lib/libsharpyuv.a /root/lib/lib/libavif.a ' >> /root/.emcc-php-wasm-sources; \ + if [[ "${PHP_VERSION:0:1}" -eq "8" && "${PHP_VERSION:2:1}" -ge "1" ]]; then \ + echo -n ' --with-avif ' >> /root/.php-configure-flags; \ + fi; \ + else \ + # Bundled gd for PHP 7.4 and 8.0 + echo -n ' --enable-gd ' >> /root/.php-configure-flags; \ + if [[ "${PHP_VERSION:0:1}" -le "7" && "${PHP_VERSION:2:1}" -le "4" ]]; then \ + /root/replace.sh 's/static long php_jpeg_emit_message/static void php_jpeg_emit_message/g' /root/php-src/ext/gd/libgd/gd_jpeg.c; \ + sed -i '84s/.*return 1;.*//' /root/php-src/ext/gd/libgd/gd_jpeg.c; \ + /root/replace.sh 's/cinfo.err->emit_message = \(void \(*\)\(j_common_ptr,int\)\) php_jpeg_emit_message;/cinfo.err->emit_message = php_jpeg_emit_message;/g' /root/php-src/ext/gd/libgd/gd_jpeg.c; \ + fi; \ + echo -n ' /root/lib/lib/libjpeg.a /root/lib/lib/libpng16.a /root/lib/lib/libwebp.a /root/lib/lib/libsharpyuv.a ' >> /root/.emcc-php-wasm-sources; \ + fi; \ + echo -n ' --with-png-dir=/root/lib --with-jpeg --with-webp ' >> /root/.php-configure-flags; \ + fi; + +# Add openssl if needed. +# PHP 5.2's openssl.c is patched via php5.2-openssl-compat.patch to work +# with OpenSSL 1.1.0+ opaque structs. PHP 5.2 takes the directory via +# --with-openssl[=DIR] (no separate --with-openssl-dir flag). +RUN if [ "$WITH_OPENSSL" = "yes" ]; \ + then \ + cp -r /libs/libopenssl/* /root/lib; \ + echo -n ' --with-openssl=/root/lib ' >> /root/.php-configure-flags; \ + echo -n ' /root/lib/lib/libcrypto.a /root/lib/lib/libssl.a ' >> /root/.emcc-php-wasm-sources; \ + fi; + +# Remove fileinfo if needed +RUN if [ "$WITH_FILEINFO" = "yes" ]; \ + then \ + echo -n ' --enable-fileinfo' >> /root/.php-configure-flags; \ + else \ + # light bundle should compile without fileinfo and libmagic + echo -n ' --disable-fileinfo' >> /root/.php-configure-flags; \ + fi; + +# Add iconv if needed +RUN if [ "$WITH_ICONV" = "yes" ]; \ + then \ + echo -n ' --with-iconv=/root/lib ' >> /root/.php-configure-flags; \ + echo -n ' /root/lib/lib/libiconv.a ' >> /root/.emcc-php-wasm-sources; \ + # PHP >= 8.0 posix is no longer supported. + # PHP ≤= 7.4 posix is supported. + else \ + echo -n ' --without-iconv ' >> /root/.php-configure-flags; \ + fi; + +# Add curl if needed +# PHP 5.x uses curl-config to detect cURL instead of pkg-config. +# Provide a shim so configure can find the pre-built library. +RUN if [ "$WITH_CURL" = "yes" ]; \ + then \ + echo -n ' --with-curl=/root/lib ' >> /root/.php-configure-flags; \ + echo -n ' /root/lib/lib/libcurl.a ' >> /root/.emcc-php-wasm-sources; \ + printf '#!/bin/sh\ncase "$1" in\n--version) echo "7.80.0" ;;\n--cflags) echo "-I/root/lib/include" ;;\n--libs) echo "-L/root/lib/lib -lcurl" ;;\n--prefix) echo "/root/lib" ;;\nesac\n' > /usr/local/bin/curl-config && \ + chmod +x /usr/local/bin/curl-config; \ + fi; + +# Add mbstring if needed +# mbstring is required for the exif extension +RUN if [ "$WITH_MBSTRING" = "yes" ] || [ "$WITH_EXIF" = "yes" ]; \ + then echo -n ' --enable-mbstring ' >> /root/.php-configure-flags; \ + else echo -n ' --disable-mbstring ' >> /root/.php-configure-flags; \ + fi; + +# Add exif if needed +RUN if [ "$WITH_EXIF" = "yes" ]; \ + then \ + echo -n ' --enable-exif ' >> /root/.php-configure-flags; \ + else \ + echo -n ' --disable-exif ' >> /root/.php-configure-flags; \ + fi; + +# Add mbregex if needed +RUN if [ "$WITH_MBREGEX" = "yes" ] && [ "${PHP_VERSION:0:3}" != "7.0" ]; \ + then \ + echo -n ' --enable-mbregex --with-onig=/root/lib ' >> /root/.php-configure-flags; \ + echo -n ' /root/lib/lib/libonig.a ' >> /root/.emcc-php-wasm-sources; \ + else \ + echo -n ' --disable-mbregex ' >> /root/.php-configure-flags; \ + fi; + +# Get and patch PHP +# Only PHP 5.2-specific patches; chunk-alloc-zend-assert patches are for PHP 7+. +COPY ./compile/php/php*.patch /root/ +COPY ./compile/php/apply-mysqlnd-patch.sh /root/ +RUN cd /root && \ + git apply --no-index /root/php${PHP_VERSION:0:3}*.patch -v && \ + chmod +x /root/apply-mysqlnd-patch.sh && \ + /root/apply-mysqlnd-patch.sh && \ + touch php-src/patched + +# mysqlnd was introduced in PHP 5.3; skip MySQL support entirely. +# imagick 3.7+ requires PHP 7+; skip entirely. +# ext/phar was bundled starting in PHP 5.3; skip entirely. + +# Patch configure for PHP 5.2 (autoconf 2.13) + emcc compatibility. +# emcc produces .js/.wasm instead of plain binaries. Force the compiler +# test to pass by patching the generated configure script. +RUN sed -i 's/ac_cv_prog_cc_works=no/ac_cv_prog_cc_works=yes/g' /root/php-src/configure && \ + sed -i 's/ac_cv_prog_cc_cross=no/ac_cv_prog_cc_cross=yes/g' /root/php-src/configure + +# Build the patched PHP +WORKDIR /root/php-src +RUN source /root/emsdk/emsdk_env.sh && \ + CURL_CFLAGS="-I/root/lib/include" \ + WEBP_LIBS="-L/root/lib/lib -lwebp -lsharpyuv" \ + WEBP_CFLAGS="-I/root/lib/include" \ + CURL_LIBS="-I/root/lib/lib -L/root/lib/lib" \ + JPEG_CFLAGS="-I/root/lib/include" \ + JPEG_LIBS="-L/root/lib/lib -ljpeg" \ + PNG_CFLAGS="-I/root/lib/include" \ + PNG_LIBS="-L/root/lib/lib -lpng16 -lz" \ + AVIF_CFLAGS="-I/root/lib/include" \ + AVIF_LIBS="-L/root/lib/lib -lavif" \ + GDLIB_CFLAGS="-I/root/lib/include" \ + GDLIB_LIBS="-L/root/lib/lib -lgd -lpng16 -lz -ljpeg -lwebp -lsharpyuv -lavif" \ + PKG_CONFIG_PATH="/root/lib/lib/pkgconfig:${PKG_CONFIG_PATH}" \ + emconfigure ./configure \ + # PKG_CONFIG_PATH=... as a configure argument requires autoconf 2.59+; + # PHP 5.2 uses autoconf 2.13, so it's passed only as env var above. + # + # --disable-fiber-asm: Fibers are PHP 8.1+, N/A for PHP 5.2. + # --enable-phar: ext/phar is PHP 5.3+, N/A for PHP 5.2. + # --enable-json for PHP < 8.0: + --enable-json \ + --enable-embed=static \ + --with-layout=GNU \ + --disable-cgi \ + --disable-opcache \ + --disable-posix \ + --enable-hash \ + --enable-static \ + --enable-shared \ + --enable-session \ + --enable-filter \ + --enable-calendar \ + --disable-rpath \ + --disable-phpdbg \ + --without-pear \ + --with-valgrind=no \ + --without-pcre-jit \ + --enable-bcmath \ + --enable-ctype \ + --enable-tokenizer \ + --enable-wasm_memory_storage \ + --enable-dns_polyfill \ + --enable-post_message_to_js \ + $(cat /root/.php-configure-flags) + +# Silence the errors "munmap() failed: [28] Invalid argument" +# @TODO: Identify the root cause behind these errors and fix them properly +RUN echo '#define ZEND_MM_ERROR 0' >> /root/php-src/main/php_config.h; + +# PHP 5.2: type size defines (configure can't run test programs under emcc) + linker fix +# for duplicate yytext symbols from multiple flex-generated scanners. +RUN echo '/* WASM type sizes (ILP32) */' >> /root/php-src/main/php_config.h && \ + echo '#undef SIZEOF_SHORT' >> /root/php-src/main/php_config.h && \ + echo '#define SIZEOF_SHORT 2' >> /root/php-src/main/php_config.h && \ + echo '#undef SIZEOF_INT' >> /root/php-src/main/php_config.h && \ + echo '#define SIZEOF_INT 4' >> /root/php-src/main/php_config.h && \ + echo '#undef SIZEOF_LONG' >> /root/php-src/main/php_config.h && \ + echo '#define SIZEOF_LONG 4' >> /root/php-src/main/php_config.h && \ + echo '#undef SIZEOF_LONG_LONG' >> /root/php-src/main/php_config.h && \ + echo '#define SIZEOF_LONG_LONG 8' >> /root/php-src/main/php_config.h && \ + echo '#undef SIZEOF_SIZE_T' >> /root/php-src/main/php_config.h && \ + echo '#define SIZEOF_SIZE_T 4' >> /root/php-src/main/php_config.h && \ + echo -n ' -Wl,--allow-multiple-definition ' >> /root/.emcc-php-wasm-flags + +# Force GD JPEG/PNG/WebP/AVIF support for external GD builds (PHP 8.1+) — N/A for PHP 5.2. + +# PHP 5.x's configure detects "old" readdir_r (2-arg) but Emscripten provides +# the modern 3-arg version. Undefine the old variant so reentrancy.c compiles. +RUN if grep -q 'HAVE_OLD_READDIR_R' /root/php-src/main/php_config.h 2>/dev/null; then \ + echo '#undef HAVE_OLD_READDIR_R' >> /root/php-src/main/php_config.h; \ + fi + +# PHP 5.x bundles PCRE with JIT hardcoded in config.h. Disable it for WASM. +RUN if [ -f /root/php-src/ext/pcre/pcrelib/config.h ] && grep -q '^#define SUPPORT_JIT' /root/php-src/ext/pcre/pcrelib/config.h; then \ + /root/replace.sh 's/^#define SUPPORT_JIT/#undef SUPPORT_JIT/g' /root/php-src/ext/pcre/pcrelib/config.h; \ + fi + +# 64 bit long support {{{ +# PHP uses custom typedefs for 64 bit integers in many places, e.g. +# timelib_long is int64_t when __x86_64__ is defined and int32_t otherwise. +# +# We're defining __x86_64__ in all emcc calls in this project, which +# works well, but also has some side effects in the PHP codebase. +# +# Specifically, PHP has a lot of inlined asm for 64bit processors that is used +# when __x86_64__ is defined. Inline asm won't build to WebAssembly so we need to +# disable every instance of inlined asm. +RUN echo '#undef HAVE_ASM_GOTO' >> /root/php-src/main/php_config.h; +RUN echo '#define HAVE_ASM_GOTO 0' >> /root/php-src/main/php_config.h; +# Disable asm arithmetic. +# PHP 5.x uses __GNUC__ && __x86_64__ guards for inline asm in zend_operators.h +# and zend_alloc.c instead of ZEND_USE_ASM_ARITHMETIC (introduced in PHP 7.x). +RUN /root/replace.sh 's/defined\(__GNUC__\) && defined\(__x86_64__\)/0/g' /root/php-src/Zend/zend_operators.h; \ + /root/replace.sh 's/defined\(__GNUC__\) && defined\(__i386__\)/0/g' /root/php-src/Zend/zend_operators.h || true +RUN /root/replace.sh 's/defined\(__GNUC__\) && defined\(__x86_64__\)/0/g' /root/php-src/Zend/zend_alloc.c; \ + /root/replace.sh 's/defined\(__GNUC__\) && defined\(__i386__\)/0/g' /root/php-src/Zend/zend_alloc.c || true +RUN if [ -f /root/php-src/Zend/zend_multiply.h ]; then \ + /root/replace.sh 's/defined\(__GNUC__\)/0/g' /root/php-src/Zend/zend_multiply.h; \ + fi +# zend_cpuinfo.c doesn't exist in PHP 5.2 +# }}} + +# With HAVE_UNISTD_H=1 PHP complains about the missing getdtablesize() function +RUN /root/replace.sh 's/define php_sleep sleep/define php_sleep wasm_sleep/g' /root/php-src/main/php.h +RUN echo 'extern unsigned int wasm_sleep(unsigned int time);' >> /root/php-src/main/php.h; + +RUN /root/replace.sh 's/define HAVE_UNISTD_H 1/define HAVE_UNISTD_H 0/g' /root/php-src/main/php_config.h + +# Rename the original php_pollfd_for() implementation so that we can link our own version. +# php_pollfd_for() was added in PHP 5.3+. +RUN if grep -q 'php_pollfd_for' /root/php-src/main/php_network.h 2>/dev/null; then \ + /root/replace.sh 's/static inline int php_pollfd_for\(/int php_pollfd_for(php_socket_t fd, int events, struct timeval *timeouttv); static inline int __real_php_pollfd_for(/g' /root/php-src/main/php_network.h; \ + fi + +RUN echo 'extern ssize_t wasm_read(int fd, void *buf, size_t count);' >> /root/php-src/main/php.h; +RUN /root/replace.sh 's/ret = read/ret = wasm_read/g' /root/php-src/main/streams/plain_wrapper.c + +# Provide a custom implementation of the php_exec() function that handles spawning +# the process inside exec(), passthru(), system(), etc. +# We effectively remove the php_exec() implementation from the build by renaming it +# to an unused identifier "php_exec_old", and then we mark php_exec as extern. +# The function signature varies between PHP versions. +RUN if grep -q 'PHPAPI int php_exec' /root/php-src/ext/standard/exec.c 2>/dev/null; then \ + /root/replace.sh 's/PHPAPI int php_exec(.+)$/PHPAPI extern int php_exec\1; int php_exec_old\1/g' /root/php-src/ext/standard/exec.c; \ + elif grep -q 'int php_exec' /root/php-src/ext/standard/exec.c 2>/dev/null; then \ + /root/replace.sh 's/^int php_exec(.+)$/extern int php_exec\1; int php_exec_old\1/g' /root/php-src/ext/standard/exec.c; \ + fi + +# Provide a custom implementation of the VCWD_POPEN() function that handles spawning +# the process inside PHP_FUNCTION(popen). +RUN if grep -q 'VCWD_POPEN' /root/php-src/Zend/zend_virtual_cwd.h 2>/dev/null; then \ + /root/replace.sh 's/#define VCWD_POPEN.+/#define VCWD_POPEN(command, type) wasm_popen(command,type)/g' /root/php-src/Zend/zend_virtual_cwd.h; \ + fi +RUN echo 'extern FILE *wasm_popen(const char *cmd, const char *mode);' >> /root/php-src/Zend/zend_virtual_cwd.h + +# Provide a custom implementation of the shutdown() function. +# php_cli_server.c exists in PHP 5.4+ (built-in server added in 5.4) +RUN if [ -f /root/php-src/sapi/cli/php_cli_server.c ]; then \ + perl -pi.bak -e $'s/(\\s+)shutdown\\(/$1 wasm_shutdown(/g' /root/php-src/sapi/cli/php_cli_server.c; \ + perl -pi.bak -e $'s/(\\s+)closesocket\\(/$1 wasm_close(/g' /root/php-src/sapi/cli/php_cli_server.c; \ + fi + +# Provide custom implementation fo the shutdown() function. +# This is used by intl ( which links C++ code ) and need C linkage. +RUN cat >> /root/php-src/main/php_config.h <> /root/php-src/main/php.h +#ifdef __cplusplus +extern "C" { +#endif + +extern char **environ; + +#ifdef __cplusplus +} +#endif +EOF + +# Don't ship PHP_FUNCTION(proc_open) with the PHP build +# so that we can ship a patched version with php_wasm.c +RUN echo '' > /root/php-src/ext/standard/proc_open.h; +RUN echo '' > /root/php-src/ext/standard/proc_open.c; + +ARG OUTPUT_DIR_ON_HOST +ARG DEBUG_DWARF_COMPILATION_DIR +RUN source /root/emsdk/emsdk_env.sh && \ + # We're compiling PHP as emscripten's side module... + export JSPI_FLAGS=$(if [ "$WITH_JSPI" = "yes" ]; then echo "-sSUPPORT_LONGJMP=wasm -fwasm-exceptions"; else echo ""; fi) && \ + export PHP_VERSION_ESCAPED="${PHP_VERSION//./_}" && \ + # PHP 5.x has function pointer type mismatches, deprecated non-prototype + # function definitions, and implicit function declarations that are + # errors in modern clang/emcc. + # PHP 5.2's pre-generated flex scanners (zend_language_scanner.c and + # zend_ini_scanner.c) both define a global `yytext` symbol without a + # per-scanner prefix. Let wasm-ld keep the first definition seen + # instead of erroring out. + export PHP5_FLAGS="-Wno-incompatible-function-pointer-types -Wno-deprecated-non-prototype -Wno-implicit-function-declaration -Wno-incompatible-pointer-types -Wno-implicit-int -Wl,--allow-multiple-definition" && \ + EMCC_FLAGS=" -D__x86_64__ -sSIDE_MODULE -Dsetsockopt=wasm_setsockopt -Dphp_exec=wasm_php_exec \ + $JSPI_FLAGS $PHP5_FLAGS \ + -fdebug-compilation-dir=${DEBUG_DWARF_COMPILATION_DIR}/ \ + -fdebug-prefix-map=/root/php-src/=${OUTPUT_DIR_ON_HOST}/${PHP_VERSION_ESCAPED}/php-src/ " \ + # ...which means we must skip all the libraries - they will be provided in the final linking step. + emmake make -j14 + +RUN cp -v /root/php-src/.libs/libphp*.la /root/lib/libphp.la +RUN cp -v /root/php-src/.libs/libphp*.a /root/lib/libphp.a + +COPY ./compile/php/php_wasm.c /root/ +COPY ./compile/php/proc_open* /root/ + +ARG WITH_SOURCEMAPS +ARG OUTPUT_DIR_ON_HOST +ARG DEBUG_DWARF_COMPILATION_DIR +ARG WITH_DEBUG +RUN set -euxo pipefail; \ + if [ "$EMSCRIPTEN_ENVIRONMENT" = "node" ]; then \ + # Add nodefs when building for node.js + echo -n ' -lnodefs.js ' >> /root/.emcc-php-wasm-flags; \ + fi; \ + if [ "${WITH_SOURCEMAPS}" = "yes" ] || [ "${WITH_DEBUG}" = "yes" ]; then \ + echo -n ' -g3 ' >> /root/.emcc-php-wasm-flags; \ + # Make source file paths relative to the location of the .wasm file + # so we can debug without additional config in host OS. + export PHP_VERSION_ESCAPED="${PHP_VERSION//./_}"; \ + echo -n " -fdebug-compilation-dir=${DEBUG_DWARF_COMPILATION_DIR}/ " \ + "-fdebug-prefix-map=/root/php_wasm.c=${DEBUG_DWARF_COMPILATION_DIR}/compile/php/php_wasm.c " \ + "-fdebug-prefix-map=/root/php-src/=${OUTPUT_DIR_ON_HOST}/${PHP_VERSION_ESCAPED}/php-src/ " \ + "-fdebug-prefix-map=./=${OUTPUT_DIR_ON_HOST}/${PHP_VERSION_ESCAPED}/php-src/ " \ + "-fdebug-prefix-map=/emsdk/emscripten/==${OUTPUT_DIR_ON_HOST}/${PHP_VERSION_ESCAPED}/emscripten/ " \ + "-fdebug-prefix-map=/root/emsdk/upstream/emscripten/=${OUTPUT_DIR_ON_HOST}/${PHP_VERSION_ESCAPED}/emscripten/ " \ + >> /root/.emcc-php-wasm-flags; \ + if [ "${WITH_SOURCEMAPS}" = "yes" ]; then \ + echo -n ' -gsource-map' >> /root/.emcc-php-wasm-flags; \ + # Ensure source maps are loaded from the correct URL on the web. + # Without this, browsers will try the wasm:// protocol and never load the source map. + if [ "$EMSCRIPTEN_ENVIRONMENT" = "web" ]; then \ + echo -n " --source-map-base http://127.0.0.1:5400/@fs${OUTPUT_DIR_ON_HOST}/${PHP_VERSION_ESCAPED}/" >> /root/.emcc-php-wasm-flags; \ + fi; \ + fi; \ + else \ + # Preserve symbol names in node.js build – the bundle size doesn't matter as much + # as on the web, and this makes debugging **much** easier. + if [ "$EMSCRIPTEN_ENVIRONMENT" = "node" ]; then \ + echo -n ' -g2 ' >> /root/.emcc-php-wasm-flags; \ + fi; \ + fi; + +# Add ws networking proxy support if needed +RUN if [ "$WITH_WS_NETWORKING_PROXY" = "yes" ]; \ + then \ + echo -n ' -lwebsocket.js ' >> /root/.emcc-php-wasm-flags; \ + fi + +# Emscripten supports yielding from sync functions to JavaScript event loop, but all +# the synchronous functions doing that must be explicitly listed here. This is an +# exhaustive list that was created by compiling PHP with ASYNCIFY, running code that +# uses networking, observing the error, and listing the missing functions. +# +# If you a get an error similar to the one below, you need to add all the function on +# the stack to the "ASYNCIFY_ONLY" list below (in this case, it's php_mysqlnd_net_open_tcp_or_unix_pub): +# +# RuntimeError: unreachable +# at php_mysqlnd_net_open_tcp_or_unix_pub (:wasm-function[9341]:0x5e42b8) +# at byn$fpcast-emu$php_mysqlnd_net_open_tcp_or_unix_pub (:wasm-function[17222]:0x7795e9) +# at php_mysqlnd_net_connect_ex_pub (:wasm-function[9338]:0x5e3f02) +# +# Node cuts the trace short by default so use the --stack-trace-limit=50 CLI flag +# to get the entire stack. +# +# ------- +# +# Related: Any errors like Fatal error: Cannot redeclare function ... +# are caused by dispatching a PHP request while an execution is paused +# due to an async call – it means the same PHP files are loaded before +# the previous request, where they're already loaded, is concluded. +# TODO: Consider reverting asyncify additions before merging this PR if file locking doesn't support asyncify. +RUN export ASYNCIFY_IMPORTS=$'[\n\ +"invoke_i",\n\ +"invoke_ii",\n\ +"invoke_iii",\n\ +"invoke_iiii",\n\ +"invoke_iiiii",\n\ +"invoke_iiiiii",\n\ +"invoke_iiiiiii",\n\ +"invoke_iiiiiiii",\n\ +"invoke_iiiiiiiii",\n\ +"invoke_iiiiiiiiii",\n\ +"invoke_v",\n\ +"invoke_vi",\n\ +"invoke_vii",\n\ +"invoke_viidii",\n\ +"invoke_viii",\n\ +"invoke_viiii",\n\ +"invoke_viiiii",\n\ +"invoke_viiiiii",\n\ +"invoke_viiiiiii",\n\ +"invoke_viiiiiiiii",\n\ +"invoke_i",\ +"invoke_ii",\ +"invoke_iii",\ +"invoke_iiii",\ +"invoke_iiiii",\ +"invoke_iiiiii",\ +"invoke_iiiiiii",\ +"invoke_iiiiiiii",\ +"invoke_iiiiiiiiii",\ +"invoke_iij",\ +"invoke_iiji",\ +"invoke_iiij",\ +"invoke_iijii",\ +"invoke_iijiji",\ +"invoke_jii",\ +"invoke_jiii",\ +"invoke_viijii",\ +"invoke_vji",\ +"js_open_process",\n\ +"_js_open_process",\n\ +"_asyncjs__js_open_process",\n\ +"js_popen_to_file",\n\ +"_js_popen_to_file",\n\ +"_asyncjs__js_popen_to_file",\n\ +"__syscall_fcntl64",\n\ +"___syscall_fcntl64",\n\ +"_asyncjs___syscall_fcntl64",\n\ +"js_release_file_locks",\n\ +"_js_release_file_locks",\n\ +"_async_js_release_file_locks",\n\ +"js_flock",\n\ +"_js_flock",\n\ +"_async_js_flock",\n\ +"js_fd_read",\n\ +"_js_fd_read",\n\ +"fd_close",\n\ +"_fd_close",\n\ +"_asyncjs__fd_close",\n\ +"close",\n\ +"_close",\n\ +"js_module_onMessage",\n\ +"zend_hash_str_find",\n\ +"_js_module_onMessage",\n\ +"_asyncjs__js_module_onMessage",\n\ +"js_waitpid",\n\ +"_js_waitpid",\n\ +"_asyncjs__js_waitpid",\n\ +"wasm_poll_socket",\n\ +"_wasm_poll_socket",\n\ +"_asyncjs__wasm_poll_socket",\n\ +"_wasm_shutdown",\n\ +"_asyncjs__wasm_shutdown",\n\ +"recv",\n\ +"_recv",\n\ +"setsockopt",\n\ +"_setsockopt",\n\ +"wasm_connect",\n\ +"_wasm_connect"]'; \ + echo -n " -s ASYNCIFY_IMPORTS=$ASYNCIFY_IMPORTS " | tr -d "\n" >> /root/.emcc-php-asyncify-flags; \ + export ASYNCIFY_ONLY_UNPREFIXED=$'"null.",\ +"cookieread",\ +"ImageToFile",\ +"ReadBlob",\ +"ReadBlobStream",\ +"ReadImage",\ +"SetImageInfo",\ +"MagickReadImageFile",\ +"PHPWasm_Magick_ImageToFile",\ +"PHPWasm_Magick_ReadBlob",\ +"PHPWasm_Magick_ReadBlobStream",\ +"PHPWasm_Magick_ReadImage",\ +"PHPWasm_Magick_SetImageInfo",\ +"zm_shutdown_imagick",\ +"php_imagick_read_file",\ +"zim_Imagick___construct",\ +"printf_core",\ +"__vfprintf_internal",\ +"vfprintf",\ +"vsnprintf",\ +"__xmlRaiseError",\ +"__xmlLoaderErr",\ +"zim_SoapClient___construct",\ +"zim_SoapClient_SoapClient",\ +"get_sdl",\ +"get_http_headers",\ +"zim_SoapClient___call",\ +"soap_client_call_impl",\ +"zim_SoapClient___doRequest",\ +"make_http_soap_request",\ +"load_wsdl_ex",\ +"soap_xmlParseFile",\ +"xmlCreateURLParserCtxt",\ +"xmlNewInputFromFile",\ +"xmlDefaultExternalEntityLoader",\ +"_php_libxml_pre_ext_ent_loader",\ +"xmlLoadExternalEntity",\ +"xmlParserInputBufferCreateFilename",\ +"php_libxml_input_buffer_create_filename",\ +"php_libxml_streams_IO_open_write_wrapper",\ +"php_libxml_streams_IO_open_read_wrapper",\ +"php_libxml_streams_IO_open_wrapper",\ +"__stdio_close",\ +"zend_stream_stdio_closer",\ +"zend_destroy_file_handle",\ +"php_init_config",\ +"zend_register_constant",\ +"zend_register_ini_entries_ex",\ +"php_module_startup",\ +"wasm_sapi_module_startup",\ +"__fseeko_unlocked",\ +"__ftello_unlocked",\ +"__funcs_on_exit",\ +"__fwritex",\ +"__netlink_enumerate",\ +"__overflow",\ +"__synccall",\ +"__toread",\ +"__uflow",\ +"__vfprintf_internal",\ +"__wrap_select",\ +"__zend_malloc",\ +"_call_user_function_ex",\ +"_call_user_function_impl",\ +"_emalloc_large",\ +"_erealloc",\ +"_erealloc2",\ +"_estrdup",\ +"_get_zval_cv_lookup",\ +"_is_numeric_string_ex",\ +"_mysqlnd_init_ps_subsystem",\ +"_mysqlnd_run_command",\ +"_pcre2_memctl_malloc_8",\ +"_pdo_mysql_error",\ +"_php_error_log_ex",\ +"_php_import_environment_variables",\ +"_php_stream_cast",\ +"_php_stream_copy_to_mem",\ +"_php_stream_copy_to_stream_ex",\ +"_php_stream_eof",\ +"_php_stream_fill_read_buffer",\ +"_php_stream_filter_flush",\ +"_php_stream_flush",\ +"_php_stream_free_enclosed",\ +"_php_stream_free",\ +"_php_stream_get_line",\ +"_php_stream_mkdir",\ +"_php_stream_open_wrapper_ex",\ +"_php_stream_opendir",\ +"_php_stream_passthru",\ +"_php_stream_read",\ +"_php_stream_rmdir",\ +"_php_stream_seek",\ +"_php_stream_set_option",\ +"_php_stream_stat_path",\ +"_php_stream_stat",\ +"_php_stream_sync",\ +"_php_stream_truncate_set_size",\ +"_php_stream_write_buffer",\ +"_php_stream_write_filtered",\ +"_php_stream_write",\ +"_php_stream_xport_create",\ +"_php_image_create_from",\ +"zif_imagecreatefromavif",\ +"_safe_emalloc",\ +"_try_convert_to_string",\ +"_zend_hash_init",\ +"_zend_new_array_0",\ +"_zend_new_array",\ +"_zend_observe_fcall_begin",\ +"_zend_observer_class_linked_notify",\ +"_zend_observer_error_notify",\ +"_zend_observer_function_declared_notify",\ +"_zval_dtor_func_for_ptr",\ +"_zval_dtor_func",\ +"_zval_ptr_dtor",\ +"add_property_resource_ex",\ +"autoVacuumCommit",\ +"bsearch",\ +"btreeCellSizeCheck",\ +"btreeEndTransaction",\ +"btreeParseCell",\ +"call_extract_if_dead",\ +"call",\ +"callCollNeeded",\ +"callFinaliser",\ +"checkTreePage",\ +"cleanup_unfinished_calls",\ +"clearDatabasePage",\ +"cli_register_file_handles",\ +"cli",\ +"close_stmt_and_copy_errors",\ +"close",\ +"__stdio_close",\ +"closeUnixFile",\ +"compile_file",\ +"createCollation",\ +"createFunctionApi",\ +"createModule",\ +"ctype_fallback",\ +"Curl_client_write",\ +"Curl_conncache_foreach",\ +"curl_easy_perform",\ +"Curl_http_connect",\ +"Curl_httpchunk_read",\ +"curl_write",\ +"Curl_is_connected",\ +"curl_multi_perform",\ +"curl_multi_poll",\ +"curl_multi_wait",\ +"Curl_multi_wait",\ +"Curl_ossl_connect_nonblocking",\ +"Curl_ossl_seed",\ +"Curl_poll",\ +"Curl_proxy_connect",\ +"Curl_readwrite",\ +"Curl_setup_conn",\ +"Curl_socket_check",\ +"Curl_ssl_connect_nonblocking",\ +"Curl_wait_ms",\ +"curlx_nonblock",\ +"dbh_free",\ +"dbh_get_gc",\ +"decrement_function",\ +"deflate",\ +"deflateEnd",\ +"deflateInit2_",\ +"defragmentPage",\ +"destroy_op_array",\ +"do_cli_server",\ +"do_cli",\ +"do_fetch_common",\ +"do_fetch",\ +"dotlockCheckReservedLock",\ +"dotlockLock",\ +"dotlockUnlock",\ +"doWalCallbacks",\ +"dynCall_dd",\ +"dynCall_ddd",\ +"dynCall_di",\ +"dynCall_dii",\ +"dynCall_i",\ +"dynCall_ii",\ +"dynCall_iidiiii",\ +"dynCall_iii",\ +"dynCall_iiid",\ +"dynCall_iiii",\ +"dynCall_iiiii",\ +"dynCall_iiiiii",\ +"dynCall_iiiiiii",\ +"dynCall_iiiiiiii",\ +"dynCall_iiiiiiiii",\ +"dynCall_iiiiiiiiii",\ +"dynCall_iiiiiiiiiii",\ +"dynCall_iiiiiiiiiiii",\ +"dynCall_iiiiiiiiiiij",\ +"dynCall_iiiiiij",\ +"dynCall_iiiiijii",\ +"dynCall_iiiij",\ +"dynCall_iiiiji",\ +"dynCall_iiiijii",\ +"dynCall_iiiijji",\ +"dynCall_iiij",\ +"dynCall_iiiji",\ +"dynCall_iij",\ +"dynCall_iiji",\ +"dynCall_iijii",\ +"dynCall_iijiji",\ +"dynCall_iijj",\ +"dynCall_ij",\ +"dynCall_j",\ +"dynCall_ji",\ +"dynCall_jii",\ +"dynCall_jiii",\ +"dynCall_jiiii",\ +"dynCall_jiiiji",\ +"dynCall_jiij",\ +"dynCall_jiiji",\ +"dynCall_jiji",\ +"dynCall_jijj",\ +"dynCall_jj",\ +"dynCall_v",\ +"dynCall_vi",\ +"dynCall_vid",\ +"dynCall_vii",\ +"dynCall_viidii",\ +"dynCall_viii",\ +"dynCall_viiii",\ +"dynCall_viiiii",\ +"dynCall_viiiiii",\ +"dynCall_viiiiiii",\ +"dynCall_viiiiiiii",\ +"dynCall_viiji",\ +"dynCall_viijii",\ +"dynCall_vij",\ +"dynCall_vijii",\ +"dynCall_vji",\ +"dynCall_vjiii",\ +"easy_perform",\ +"easy_transfer",\ +"RAND_poll",\ +"rand_bytes",\ +"RAND_bytes",\ +"SSL_CTX_new",\ +"rand_nopseudo_bytes",\ +"rand_enough",\ +"Curl_ossl_seed",\ +"zif_curl_multi_exec",\ +"zif_curl_multi_select",\ +"ossl_connect_common",\ +"Curl_ossl_connect_nonblocking",\ +"Curl_ssl_connect_nonblocking",\ +"https_connecting",\ +"Curl_readwrite",\ +"zend_fetch_debug_backtrace",\ +"zend_default_exception_new",\ +"zend_throw_exception_zstr",\ +"zend_throw_exception",\ +"zend_type_error",\ +"zend_generator_iterator_move_forward",\ +"ZEND_FE_FETCH_R_SPEC_VAR_HANDLER",\ +"zend_generator_resume",\ +"zend_generator_iterator_rewind",\ +"zend_fe_reset_iterator",\ +"ZEND_FE_RESET_R_SPEC_VAR_HANDLER",\ +"zend_user_it_get_new_iterator",\ +"ZEND_ADD_ARRAY_UNPACK_SPEC_HANDLER",\ +"ZEND_COUNT_SPEC_CV_UNUSED_HANDLER",\ +"__fwritex",\ +"read",\ +"zif_sleep",\ +"zif_stream_get_contents",\ +"zif_stream_select",\ +"_php_stream_fill_read_buffer",\ +"_php_stream_read",\ +"php_sqlite3_object_free_storage",\ +"php_sqlite3_error",\ +"sqlite3_reset",\ +"php_stream_read_to_str",\ +"php_userstreamop_read",\ +"zif_fread",\ +"wasm_read",\ +"php_stdiop_read",\ +"fwrite",\ +"zif_fwrite",\ +"php_stdiop_write",\ +"zif_array_filter",\ +"zend_unclean_zval_ptr_dtor",\ +"zend_call_known_instance_method_with_2_params",\ +"ZEND_CALL_TRAMPOLINE_SPEC_OBSERVER_HANDLER",\ +"zend_fetch_dimension_address_read_R",\ +"_zval_dtor_func_for_ptr",\ +"ZEND_ASSIGN_DIM_SPEC_CV_CONST_HANDLER",\ +"zend_fetch_dimension_address_read",\ +"php_if_fopen",\ +"_php_stream_cast",\ +"php_stream_temp_cast",\ +"php_stream_temp_flush",\ +"_php_stream_flush",\ +"php_stream_flush",\ +"_php_stream_write",\ +"php_stream_write",\ +"_php_stream_free",\ +"_php_stream_free_enclosed",\ +"stream_resource_persistent_dtor",\ +"stream_resource_regular_dtor",\ +"zend_std_has_dimension",\ +"zend_isset_isempty_dim_prop_obj_handler_SPEC_CV_CONST",\ +"zend_assign_to_object",\ +"zif_file_put_contents",\ +"ZEND_ASSIGN_OBJ_SPEC_CV_CONST_HANDLER",\ +"ZEND_FETCH_OBJ_R_SPEC_CV_CV_HANDLER",\ +"zend_std_call_user_call",\ +"zend_objects_store_del_ref_by_handle_ex",\ +"zend_objects_store_del_ref",\ +"_zval_dtor_func",\ +"_zval_ptr_dtor",\ +"zend_hash_del_key_or_index",\ +"zend_delete_variable",\ +"ZEND_UNSET_VAR_SPEC_CV_UNUSED_HANDLER",\ +"zend_std_unset_dimension",\ +"ZEND_UNSET_DIM_SPEC_CV_CONST_HANDLER",\ +"zend_std_read_dimension",\ +"zend_fetch_dimension_address_read_R_slow",\ +"zend_call_method",\ +"zend_assign_to_object_dim",\ +"ZEND_ASSIGN_DIM_SPEC_CV_CONST_OP_DATA_CONST_HANDLER",\ +"zend_std_write_dimension",\ +"zend_isset_dim_slow",\ +"ZEND_ISSET_ISEMPTY_DIM_OBJ_SPEC_CV_CONST_HANDLER",\ +"zend_std_write_property",\ +"ZEND_ASSIGN_OBJ_SPEC_CV_CONST_OP_DATA_CONST_HANDLER",\ +"zend_objects_store_del",\ +"ZEND_UNSET_CV_SPEC_CV_UNUSED_HANDLER",\ +"ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER",\ +"ZEND_DO_FCALL_BY_NAME_SPEC_OBSERVER_HANDLER",\ +"ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_UNUSED_HANDLER",\ +"ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_USED_HANDLER",\ +"ZEND_DO_FCALL_SPEC_CONST_HANDLER",\ +"ZEND_DO_FCALL_SPEC_HANDLER",\ +"ZEND_DO_FCALL_SPEC_OBSERVER_HANDLER",\ +"ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER",\ +"ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER",\ +"ZEND_DO_ICALL_SPEC_HANDLER",\ +"ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER",\ +"ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER",\ +"ZEND_DO_UCALL_SPEC_OBSERVER_HANDLER",\ +"ZEND_FETCH_OBJ_FUNC_ARG_SPEC_CV_CONST_HANDLER",\ +"ZEND_FETCH_OBJ_R_SPEC_CV_CONST_HANDLER",\ +"ZEND_FETCH_OBJ_R_SPEC_TMPVAR_CONST_HANDLER",\ +"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_CV_CONST_HANDLER",\ +"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_CV_HANDLER",\ +"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_CV_TMPVAR_HANDLER",\ +"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_TMPVAR_CONST_HANDLER",\ +"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_TMPVAR_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_VAR_CV_OP_DATA_CV_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_VAR_CONST_OP_DATA_CV_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_VAR_CONST_OP_DATA_VAR_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_VAR_CONST_OP_DATA_TMP_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_VAR_CONST_OP_DATA_CONST_HANDLER",\ +"ZEND_ASSIGN_SPEC_CV_VAR_RETVAL_UNUSED_HANDLER",\ +"cli",\ +"__wrap_usleep",\ +"wasm_sleep",\ +"wasm_php_stream_flush",\ +"wasm_php_stream_read",\ +"php_crc32_stream_bulk_update",\ +"phar_parse_zipfile",\ +"GetDelegateInfo",\ +"ReadBlob",\ +"ReadBlobStream",\ +"ImageToFile",\ +"SetImageInfo",\ +"PHPWasm_Magick_ReadImage",\ +"MagickReadImageFile",\ +"FormatLocaleString",\ +"GetConfigurePaths",\ +"GetConfigureOptions",\ +"IsDelegateCacheInstantiated",\ +"get_http_body",\ +"wasm_php_exec",\ +"wasm_sapi_handle_request",\ +"wasm_sapi_request_shutdown",\ +"execute_internal",\ +"_call_user_function_ex",\ +"_call_user_function_impl",\ +"_mysqlnd_run_command",\ +"_php_stream_copy_to_mem",\ +"_php_stream_copy_to_stream_ex",\ +"_php_stream_eof",\ +"_php_stream_fill_read_buffer",\ +"_php_stream_free",\ +"_php_stream_get_line",\ +"_php_stream_open_wrapper_ex",\ +"_php_stream_read",\ +"_php_stream_set_option",\ +"_php_stream_write",\ +"_php_stream_xport_create",\ +"zif_stream_socket_enable_crypto",\ +"do_cli",\ +"do_cli_server",\ +"execute_ex",\ +"EVP_PKEY_encrypt",\ +"exif_error_docref",\ +"fclose",\ +"fcntl",\ +"fcntlSizeHint",\ +"fetch_value",\ +"fflush",\ +"file_handle_dtor",\ +"fileHasMovedunixCheckReservedLock",\ +"fillInUnixFile",\ +"find_implicit_binds_recursively",\ +"findInodeInfo",\ +"findReusableFd",\ +"flock",\ +"fread",\ +"functionDestroy",\ +"fwrite",\ +"get_http_body",\ +"get_line",\ +"getFileMode",\ +"gzip_unencode_write",\ +"hasHotJournal",\ +"https_connecting",\ +"inflate",\ +"inflate_fast",\ +"inflate_stream",\ +"inflate_table",\ +"iter_wrapper_free",\ +"iter_wrapper_get_gc",\ +"lex_scan",\ +"list_entry_destructor",\ +"zend_hash_index_del",\ +"zend_list_free",\ +"lockBtree",\ +"lockTrace",\ +"main",\ +"mallocWithAlarm",\ +"match",\ +"modifyPagePointer",\ +"module_destructor",\ +"module_destructor_zval",\ +"zend_destroy_modules",\ +"php_module_shutdown",\ +"mul_function_slow",\ +"multi_runsingle",\ +"multipart_buffer_read",\ +"mysql_handle_begin",\ +"mysql_handle_closer",\ +"mysql_handle_commit",\ +"mysql_handle_doer",\ +"mysql_handle_preparer",\ +"mysql_handle_quoter",\ +"mysql_handle_rollback",\ +"mysql_stmt_execute",\ +"mysqli_commit_or_rollback_libmysql",\ +"mysqli_common_connect",\ +"mysqlnd_caching_sha2_handle_server_response",\ +"mysqlnd_com_handshake_run",\ +"mysqlnd_com_init_db_run",\ +"mysqlnd_com_stmt_execute_run",\ +"mysqlnd_com_stmt_prepare_run",\ +"mysqlnd_com_set_option_run",\ +"mysqlnd_com_ping_run",\ +"mysqlnd_com_set_option_run",\ +"mysqlnd_com_debug_run",\ +"mysqlnd_com_init_db_run",\ +"mysqlnd_com_ping_run",\ +"mysqlnd_com_statistics_run",\ +"mysqlnd_com_process_kill_run",\ +"mysqlnd_com_refresh_run",\ +"mysqlnd_com_quit_run",\ +"mysqlnd_com_query_run",\ +"mysqlnd_com_change_user_run",\ +"mysqlnd_com_reap_result_run",\ +"mysqlnd_com_stmt_prepare_run",\ +"mysqlnd_com_stmt_reset_run",\ +"mysqlnd_com_stmt_send_long_data_run",\ +"mysqlnd_com_stmt_close_run",\ +"mysqlnd_com_enable_ssl_run",\ +"mysqlnd_com_handshake_run",\ +"mysqlnd_connect",\ +"mysqlnd_connection_connect",\ +"mysqlnd_mysqlnd_command_handshake_pub",\ +"mysqlnd_mysqlnd_command_init_db_pub",\ +"mysqlnd_mysqlnd_command_ping_pub",\ +"mysqlnd_mysqlnd_command_reap_result_pub",\ +"mysqlnd_mysqlnd_command_set_option_pub",\ +"mysqlnd_mysqlnd_conn_connect_pub",\ +"mysqlnd_mysqlnd_conn_data_connect_handshake_pub",\ +"mysqlnd_mysqlnd_conn_data_connect_pub",\ +"mysqlnd_mysqlnd_conn_data_next_result_pub",\ +"mysqlnd_mysqlnd_conn_data_ping_pub",\ +"mysqlnd_mysqlnd_conn_data_query_pub",\ +"mysqlnd_mysqlnd_conn_data_reap_query_pub",\ +"mysqlnd_mysqlnd_conn_data_select_db_pub",\ +"mysqlnd_mysqlnd_conn_data_set_charset_pub",\ +"mysqlnd_mysqlnd_conn_data_set_server_option_pub",\ +"mysqlnd_mysqlnd_conn_data_store_result_pub",\ +"mysqlnd_mysqlnd_conn_data_tx_commit_or_rollback_pub",\ +"mysqlnd_mysqlnd_pfc_receive_pub",\ +"mysqlnd_mysqlnd_pfc_send_pub",\ +"mysqlnd_mysqlnd_protocol_send_command_handle_EOF_pub",\ +"mysqlnd_mysqlnd_protocol_send_command_handle_OK_pub",\ +"mysqlnd_mysqlnd_protocol_send_command_handle_response_pub",\ +"mysqlnd_mysqlnd_protocol_send_command_pub",\ +"mysqlnd_mysqlnd_res_store_result_fetch_data_pub",\ +"mysqlnd_mysqlnd_res_store_result_pub",\ +"mysqlnd_mysqlnd_stmt_execute_pub",\ +"mysqlnd_mysqlnd_stmt_prepare_pub",\ +"mysqlnd_mysqlnd_stmt_send_execute_pub",\ +"mysqlnd_mysqlnd_vio_connect_pub",\ +"mysqlnd_mysqlnd_vio_network_read_pub",\ +"mysqlnd_mysqlnd_vio_network_write_pub",\ +"mysqlnd_mysqlnd_vio_open_tcp_or_unix_pub",\ +"mysqlnd_query_read_result_set_header",\ +"mysqlnd_read_body_name",\ +"mysqlnd_read_buffer_is_empty",\ +"mysqlnd_read_buffer",\ +"mysqlnd_read_header_name",\ +"mysqlnd_read_header",\ +"mysqlnd_read_packet_header_and_body",\ +"mysqlnd_run_authentication",\ +"mysqlnd_stmt_execute_parse_response",\ +"mysqlnd_stmt_execute",\ +"nodeAcquire",\ +"nodeNew",\ +"nodeRelease",\ +"nodeWrite",\ +"object_and_properties_init",\ +"object_common",\ +"object_custom",\ +"object_init_ex",\ +"open_file_for_scanning",\ +"openDatabase",\ +"osLocaltime",\ +"ossl_connect_common",\ +"page_wait_on_lock",\ +"pager_end_transaction",\ +"pager_playback_one_page",\ +"pager_unlock",\ +"pager_wait_on_lock",\ +"pagerLockDb",\ +"pagerUndoCallback",\ +"pagerUnlockAndRollback",\ +"pagerUnlockDb",\ +"pagerUnlockIfUnused",\ +"param_dtor",\ +"parentWrite",\ +"pcacheUnpin",\ +"persistent_stream_open_function",\ +"pdo_dbh_attribute_set",\ +"pdo_dbh_free_storage",\ +"pdo_dbstmt_free_storage",\ +"pdo_handle_error",\ +"pdo_hash_methods",\ +"pdo_mysql_check_liveness",\ +"pdo_mysql_fill_stmt_from_result",\ +"pdo_mysql_get_attribute",\ +"pdo_mysql_handle_factory",\ +"pdo_mysql_in_transaction",\ +"pdo_mysql_last_insert_id",\ +"pdo_mysql_request_shutdown",\ +"pdo_mysql_set_attribute",\ +"pdo_mysql_stmt_after_execute_prepared",\ +"pdo_mysql_stmt_col_meta",\ +"pdo_mysql_stmt_cursor_closer",\ +"pdo_mysql_stmt_describe",\ +"pdo_mysql_stmt_dtor",\ +"pdo_mysql_stmt_execute_prepared",\ +"pdo_mysql_stmt_execute",\ +"pdo_mysql_stmt_fetch",\ +"pdo_mysql_stmt_get_col",\ +"pdo_mysql_stmt_next_rowset",\ +"pdo_mysql_stmt_param_hook",\ +"pdo_parse_params",\ +"pdo_sqlite_stmt_dtor",\ +"pdo_sqlite_stmt_execute",\ +"pdo_sqlite_stmt_fetch",\ +"pdo_stmt_describe_columns",\ +"pdo_stmt_do_next_rowset",\ +"pdo_stmt_init",\ +"phar_compile_file",\ +"phar_file_get_contents",\ +"phar_fopen",\ +"phar_parse_zipfile",\ +"php_call_shutdown_functions",\ +"php_cli_server_do_event_for_each_fd_callback",\ +"php_cli_server_poller_poll",\ +"php_cli_server_recv_event_read_request",\ +"php_crc32_stream_bulk_update",\ +"php_error_docref",\ +"php_exec_ex",\ +"php_exec",\ +"php_execute_script_ex",\ +"php_execute_script",\ +"php_fopen_primary_script",\ +"php_fsockopen_stream",\ +"php_getimagesize_from_any",\ +"php_if_fopen",\ +"php_mysqlnd_auth_response_read",\ +"php_mysqlnd_cached_sha2_result_read",\ +"php_mysqlnd_cmd_write",\ +"php_mysqlnd_conn_connect_pub",\ +"php_mysqlnd_conn_data_connect_handshake_pub",\ +"php_mysqlnd_conn_data_connect_pub",\ +"php_mysqlnd_conn_data_next_result_pub",\ +"php_mysqlnd_conn_data_query_pub",\ +"php_mysqlnd_conn_data_reap_query_pub",\ +"php_mysqlnd_conn_data_select_db_pub",\ +"php_mysqlnd_conn_data_set_charset_pub",\ +"php_mysqlnd_conn_data_simple_command_pub",\ +"php_mysqlnd_conn_data_simple_command_send_request_pub",\ +"php_mysqlnd_conn_data_store_result_pub",\ +"php_mysqlnd_conn_data_tx_commit_or_rollback_pub",\ +"php_mysqlnd_eof_read",\ +"php_mysqlnd_greet_read",\ +"php_mysqlnd_net_connect_ex_pub",\ +"php_mysqlnd_net_network_read_ex_pub",\ +"php_mysqlnd_net_network_write_ex_pub",\ +"php_mysqlnd_net_open_tcp_or_unix_pub",\ +"php_mysqlnd_net_receive_ex_pub",\ +"php_mysqlnd_net_send_ex_pub",\ +"php_mysqlnd_ok_read",\ +"php_mysqlnd_prepare_read",\ +"php_mysqlnd_res_meta_read_metadata_pub",\ +"php_mysqlnd_res_read_result_metadata_pub",\ +"php_mysqlnd_res_store_result_fetch_data_pub",\ +"php_mysqlnd_res_store_result_pub",\ +"php_mysqlnd_rowp_read",\ +"php_mysqlnd_rset_field_read",\ +"php_mysqlnd_rset_header_read",\ +"php_mysqlnd_sha256_pk_request_response_read",\ +"php_mysqlnd_stmt_execute_pub",\ +"php_mysqlnd_stmt_prepare_pub",\ +"php_network_accept_incoming",\ +"php_network_connect_socket_to_host",\ +"php_network_connect_socket",\ +"php_openssl_enable_crypto",\ +"php_openssl_sockop_close",\ +"php_openssl_sockop_io",\ +"php_openssl_sockop_read",\ +"php_openssl_sockop_set_option",\ +"php_openssl_sockop_write",\ +"php_pcre_replace_func_impl",\ +"php_pdo_free_statement",\ +"php_pollfd_for",\ +"php_replace_in_subject_func",\ +"php_request_shutdown",\ +"php_sock_stream_wait_for_data",\ +"php_sockop_read_close",\ +"php_sockop_read",\ +"php_sockop_set_option",\ +"php_sockop_write",\ +"php_stdiop_read",\ +"php_stdiop_set_option",\ +"php_stdiop_write",\ +"php_stream_bucket_attach",\ +"php_stream_context_free",\ +"php_stream_filter_append_ex",\ +"php_stream_filter_create",\ +"php_stream_filter_free",\ +"php_stream_filter_register_factory",\ +"php_stream_filter_remove",\ +"php_stream_flush",\ +"php_stream_notification_free",\ +"php_stream_notification_notify",\ +"php_stream_open_for_zend",\ +"php_stream_read_to_str",\ +"php_stream_temp_cast",\ +"php_stream_temp_flush",\ +"php_stream_url_wrap_http_ex",\ +"php_stream_url_wrap_http",\ +"php_stream_write",\ +"php_stream_xport_crypto_enable",\ +"php_stream_xport_crypto_setup",\ +"php_stream_xport_register",\ +"php_tcp_sockop_set_option",\ +"php_tick_iterator",\ +"php_url_encode_hash_ex",\ +"php_userstreamop_read",\ +"php_userstreamop_set_option",\ +"php_var_dump",\ +"php_var_serialize_intern",\ +"php_var_unserialize_internal",\ +"php_verror",\ +"php_wasm_init",\ +"php_zend_stream_closer",\ +"php_zip_extract_file",\ +"php_zip_get_properties",\ +"php_zip_has_property",\ +"php_zip_read_property",\ +"php_zip_register_prop_handler",\ +"php_zlib_filter_create",\ +"php_zlib_output_compression_start",\ +"php_zval_filter",\ +"pkey_rsa_encrypt",\ +"plist_entry_destructor",\ +"pop_arg",\ +"populateCellCache",\ +"posixUnlock",\ +"pow_function",\ +"preg_replace_func_impl",\ +"printf_core",\ +"proc_open_rsrc_dtor",\ +"ptrmapPutOvflPtr",\ +"raise",\ +"rand_bytes",\ +"RAND_bytes",\ +"rand_enough",\ +"rand_nopseudo_bytes",\ +"RAND_poll",\ +"rand_status",\ +"RAND_status",\ +"rc_dtor_func",\ +"read_file",\ +"read",\ +"readline_shell_run",\ +"really_register_bound_param",\ +"rebuild_object_properties",\ +"reflection_method_invoke",\ +"Reinsert",\ +"releasePageOne",\ +"renderLogMsg",\ +"resetCursor",\ +"result_current_field_read",\ +"result_field_count_read",\ +"result_lengths_read",\ +"result_num_rows_read",\ +"rfc1867_post_handler",\ +"robust_close",\ +"robust_ftruncate",\ +"robust_open",\ +"robustFchown",\ +"rowidWrite",\ +"RSA_padding_add_PKCS1_OAEP_mgf1",\ +"rtreeCallbackConstraint",\ +"rtreeInsertCell",\ +"run_cli",\ +"schemaIsValid",\ +"seekAndRead",\ +"seekAndWriteFd",\ +"selectExpander",\ +"shutdown_destructors",\ +"shutdown_executor",\ +"sifttrinkle",\ +"singleipconnect",\ +"slow_index_convert",\ +"smart_str_append_printf",\ +"smart_str_erealloc",\ +"spl_append_it_fetch",\ +"spl_append_it_next_iterator",\ +"spl_caching_it_next",\ +"spl_dual_it_construct",\ +"spl_dual_it_fetch",\ +"spl_dual_it_free",\ +"spl_dual_it_get_method",\ +"spl_filesystem_object_clone",\ +"spl_filesystem_object_free_storage",\ +"spl_filter_it_fetch",\ +"spl_heap_object_free_storage",\ +"spl_heap_object_new_ex",\ +"spl_iterator_apply",\ +"spl_iterator_to_array_apply",\ +"spl_iterator_to_values_apply",\ +"spl_limit_it_seek",\ +"spl_ptr_heap_delete_top",\ +"spl_ptr_heap_insert",\ +"spl_recursive_it_get_current_data",\ +"spl_recursive_it_get_current_key",\ +"spl_recursive_it_get_method",\ +"spl_recursive_it_it_construct",\ +"spl_recursive_it_move_forward_ex",\ +"spl_recursive_it_rewind_ex",\ +"spl_recursive_it_valid",\ +"SplitNode",\ +"splitNodeStartree",\ +"sqlite_handle_closer",\ +"sqlite_handle_doer",\ +"sqlite_handle_preparer",\ +"sqlite3_autovacuum_pages",\ +"sqlite3_backup_step",\ +"sqlite3_bind_pointer",\ +"sqlite3_exec",\ +"sqlite3_finalize",\ +"sqlite3_free",\ +"sqlite3_malloc64",\ +"sqlite3_msize",\ +"sqlite3_mutex_alloc",\ +"sqlite3_mutex_enter",\ +"sqlite3_mutex_free",\ +"sqlite3_mutex_leave",\ +"sqlite3_mutex_try",\ +"sqlite3_prepare_v2",\ +"sqlite3_set_auxdata",\ +"sqlite3_step",\ +"sqlite3AuthCheck",\ +"sqlite3AuthReadCol",\ +"sqlite3AutoLoadExtensions",\ +"sqlite3BeginBenignMalloc",\ +"sqlite3BtreeBeginTrans",\ +"sqlite3BtreeClose",\ +"sqlite3BtreeCommit",\ +"sqlite3BtreeCommitPhaseTwo",\ +"sqlite3BtreeDelete",\ +"sqlite3BtreeIndexMoveto",\ +"sqlite3BtreeInsert",\ +"sqlite3BtreeRollback",\ +"sqlite3Close",\ +"sqlite3DbMallocSize",\ +"sqlite3EndBenignMalloc",\ +"sqlite3FaultSim",\ +"sqlite3Fts3CacheDeferredDoclists",\ +"sqlite3Fts3HashFindElem",\ +"sqlite3Fts3HashInsert",\ +"sqlite3Fts3InitTokenizer",\ +"sqlite3Fts3Offsets",\ +"sqlite3Fts3OpenTokenizer",\ +"sqlite3Fts5AuxInit",\ +"sqlite3Fts5ConfigFree",\ +"sqlite3Fts5ExprFirst",\ +"sqlite3Fts5ExprNext",\ +"sqlite3Fts5GetTokenizer",\ +"sqlite3Fts5IndexQuery",\ +"sqlite3Fts5ParserAlloc",\ +"sqlite3Fts5ParserFree",\ +"sqlite3Fts5Tokenize",\ +"sqlite3Fts5TokenizerInit",\ +"sqlite3Init",\ +"sqlite3InitOne",\ +"sqlite3Insert",\ +"sqlite3InvokeBusyHandler",\ +"sqlite3IsShadowTableOf",\ +"sqlite3LeaveMutexAndCloseZombie",\ +"sqlite3LoadExtension",\ +"sqlite3LocateTable",\ +"sqlite3LocateTableItem",\ +"sqlite3LockAndPrepare",\ +"sqlite3Malloc",\ +"sqlite3MallocInit",\ +"sqlite3MallocSize",\ +"sqlite3MarkAllShadowTablesOf",\ +"sqlite3MutexAlloc",\ +"sqlite3MutexInitsqlite3_db_status",\ +"sqlite3OsAccess",\ +"sqlite3OsCheckReservedLock",\ +"sqlite3OsClose",\ +"sqlite3OsCurrentTimeInt64",\ +"sqlite3OsDelete",\ +"sqlite3OsDeviceCharacteristics",\ +"sqlite3OsDlClose",\ +"sqlite3OsDlError",\ +"sqlite3OsDlOpensqlite3OsDlSym",\ +"sqlite3OsFileControl",\ +"sqlite3OsFileControlHint",\ +"sqlite3OsFileSize",\ +"sqlite3OsFullPathname",\ +"sqlite3OsGetLastError",\ +"sqlite3OsLock",\ +"sqlite3OsOpen",\ +"sqlite3OsRandomness",\ +"sqlite3OsRead",\ +"sqlite3OsSectorSize",\ +"sqlite3OsShmBarrier",\ +"sqlite3OsShmLock",\ +"sqlite3OsShmMap",\ +"sqlite3OsShmUnmap",\ +"sqlite3OsSleep",\ +"sqlite3OsSync",\ +"sqlite3OsTruncate",\ +"sqlite3OsUnlock",\ +"sqlite3OsWrite",\ +"sqlite3PagerBegin",\ +"sqlite3PagerClose",\ +"sqlite3PagerCommitPhaseTwo",\ +"sqlite3PagerExclusiveLock",\ +"sqlite3PagerGet",\ +"sqlite3PagerRollback",\ +"sqlite3PagerSharedLock",\ +"sqlite3PagerUnrefPageOne",\ +"sqlite3ParseObjectReset",\ +"sqlite3Parser",\ +"sqlite3ParserAddCleanup",\ +"sqlite3PcacheClose",\ +"sqlite3PcacheDrop",\ +"sqlite3PcacheFetch",\ +"sqlite3PcacheFetchStress",\ +"sqlite3PcacheInitialize",\ +"sqlite3PcacheMove",\ +"sqlite3PcachePagecount",\ +"sqlite3PcacheSetCachesize",\ +"sqlite3PcacheSetPageSize",\ +"sqlite3PcacheShrink",\ +"sqlite3PcacheTruncate",\ +"sqlite3Prepare",\ +"sqlite3ReadSchema",\ +"sqlite3Realloc",\ +"sqlite3RollbackAll",\ +"sqlite3RunParser",\ +"sqlite3Select",\ +"sqlite3SelectPrep",\ +"sqlite3SrcListLookup",\ +"sqlite3StartTable",\ +"sqlite3Step",\ +"sqlite3ThreadCreate",\ +"sqlite3VdbeDeleteAuxData",\ +"sqlite3VdbeExec",\ +"sqlite3VdbeFreeCursorNN",\ +"sqlite3VdbeHalt",\ +"sqlite3VdbeMemAggValue",\ +"sqlite3VdbeMemFinalizevdbeCommit",\ +"sqlite3VdbeMemGrow",\ +"sqlite3VdbeMemSetStr",\ +"sqlite3VdbeReset",\ +"sqlite3VtabBegin",\ +"sqlite3VtabCallDestroy",\ +"sqlite3VtabModuleUnref",\ +"sqlite3VtabOverloadFunction",\ +"sqlite3VtabSavepoint",\ +"sqlite3VtabSync",\ +"sqlite3VtabUnlock",\ +"sqlite3WalkSelect",\ +"sqlite3WalUndo",\ +"SSL_CTX_new",\ +"stem",\ +"stmt_affected_rows_read",\ +"stmt_errno_read",\ +"stmt_error_read",\ +"stmt_field_count_read",\ +"stmt_insert_id_read",\ +"stmt_num_rows_read",\ +"stmt_param_count_read",\ +"stmt_sqlstate_read",\ +"stream_array_from_fd_set",\ +"stream_resource_persistent_dtor",\ +"stream_resource_regular_dtor",\ +"strfilter_convert_append_bucket",\ +"strfilter_convert_dtor",\ +"sub_function_slow",\ +"syscall",\ +"tsrm_realpath",\ +"unixAccess",\ +"unixCheckReservedLock",\ +"unixClose",\ +"unixDelete",\ +"unixDlSym",\ +"unixFcntlExternalReader",\ +"unixFileLock",\ +"unixFileSize",\ +"unixFullPathname",\ +"unixGetTempname",\ +"unixLock",\ +"unixLockSharedMemory",\ +"unixOpen",\ +"unixOpenSharedMemory",\ +"unixShmMap",\ +"unixShmPurge",\ +"unixShmRegionPerMap",\ +"unixShmSystemLock",\ +"unixShmUnmap",\ +"unixSync",\ +"unencode_write",\ +"unixTempFileDir",\ +"unixUnlock",\ +"unlockBtreeIfUnused",\ +"UpdateDefaultFilter",\ +"updateMapping",\ +"updatewindow",\ +"user_shutdown_function_call",\ +"userfilter_filter",\ +"var_destroy",\ +"_efree_large",\ +"_php_stream_make_seekable",\ +"_zval_undefined_op1",\ +"concat_function",\ +"zend_get_properties_for",\ +"ZEND_CALL_TRAMPOLINE_SPEC_HANDLER",\ +"ZEND_CONCAT_SPEC_CONST_CV_HANDLER",\ +"ZEND_CLONE_SPEC_CV_HANDLER",\ +"ZEND_ECHO_SPEC_CV_HANDLER",\ +"php_output_op",\ +"php_output_write",\ +"php_printf_to_smart_str",\ +"php_unserialize_with_options",\ +"php_var_unserialize_destroy",\ +"xbuf_format_converter",\ +"zend_hash_destroy",\ +"zend_std_cast_object_tostring",\ +"zend_std_get_debug_info",\ +"zend_std_get_properties_for",\ +"zif_serialize",\ +"zif_unserialize",\ +"zif_var_dump",\ +"vdbeCommit",\ +"vdbeCompareMemString",\ +"vdbeMemClearExternAndSetNull",\ +"vdbeMergeEngineCompare",\ +"vdbeMergeEngineStep",\ +"vdbeSorterMerge",\ +"verifyDbFile",\ +"virtual_chdir_file",\ +"virtual_file_ex",\ +"vtabBestIndex",\ +"vtabCallConstructor",\ +"walBusyLock",\ +"walkExpr",\ +"wasm_php_exec",\ +"wasm_php_stream_flush",\ +"wasm_php_stream_read",\ +"wasm_popen",\ +"wasm_read",\ +"wasm_sapi_handle_request",\ +"wasm_sapi_request_shutdown",\ +"wasm_sleep",\ +"wrapper_cmp",\ +"yy_reduce",\ +"zend_activate_auto_globals",\ +"zend_activate_modules",\ +"ZEND_ADD_ARRAY_UNPACK_SPEC_HANDLER",\ +"zend_add_class_name_literal",\ +"zend_add_const_name_literal",\ +"zend_add_func_name_literal",\ +"zend_add_helper_SPEC",\ +"zend_add_literal",\ +"zend_add_ns_func_name_literal",\ +"zend_alter_ini_entry_ex",\ +"zend_argument_count_error",\ +"zend_array_destroy",\ +"zend_array_dup",\ +"zend_array_key_exists_error",\ +"zend_array_key_exists_fast",\ +"ZEND_ASSIGN_DIM_SPEC_CV_CONST_HANDLER",\ +"ZEND_ASSIGN_DIM_SPEC_CV_CONST_OP_DATA_CONST_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_CV_CONST_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_CV_CONST_OP_DATA_CONST_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_VAR_CONST_OP_DATA_CONST_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_VAR_CONST_OP_DATA_CV_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_VAR_CONST_OP_DATA_TMP_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_VAR_CONST_OP_DATA_VAR_HANDLER",\ +"ZEND_ASSIGN_OBJ_SPEC_VAR_CV_OP_DATA_CV_HANDLER",\ +"zend_assign_to_object_dim",\ +"zend_assign_to_object",\ +"zend_ast_apply",\ +"zend_ast_evaluate_inner",\ +"zend_attach_symbol_table",\ +"ZEND_BEGIN_SILENCE_SPEC_HANDLER",\ +"zend_binary_assign_op_dim_slow",\ +"zend_binary_assign_op_obj_dim",\ +"zend_binary_assign_op_typed_prop",\ +"zend_binary_assign_op_typed_ref",\ +"zend_bind_class_in_slot",\ +"zend_call_destructors",\ +"zend_call_function",\ +"zend_call_known_function",\ +"zend_call_known_instance_method_with_2_params",\ +"zend_call_method_if_exists",\ +"zend_call_method",\ +"zend_close_rsrc",\ +"zend_close_rsrc_list",\ +"ZEND_COUNT_SPEC_CV_UNUSED_HANDLER",\ +"zend_deactivate",\ +"zend_default_exception_new",\ +"zend_delete_variable",\ +"zend_deprecated_function",\ +"zend_destroy_rsrc_list",\ +"ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER",\ +"ZEND_DO_FCALL_BY_NAME_SPEC_OBSERVER_HANDLER",\ +"ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_UNUSED_HANDLER",\ +"ZEND_DO_FCALL_BY_NAME_SPEC_RETVAL_USED_HANDLER",\ +"zend_do_fcall_common_helper_SPEC",\ +"ZEND_DO_FCALL_SPEC_CONST_HANDLER",\ +"ZEND_DO_FCALL_SPEC_HANDLER",\ +"ZEND_DO_FCALL_SPEC_OBSERVER_HANDLER",\ +"ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER",\ +"ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER",\ +"ZEND_DO_ICALL_SPEC_HANDLER",\ +"ZEND_DO_ICALL_SPEC_RETVAL_UNUSED_HANDLER",\ +"ZEND_DO_ICALL_SPEC_RETVAL_USED_HANDLER",\ +"ZEND_DO_ICALL_SPEC_OBSERVER_HANDLER",\ +"ZEND_DO_UCALL_SPEC_OBSERVER_HANDLER",\ +"zend_emit_op_tmp",\ +"zend_emit_op",\ +"zend_enum_add_case_cstr",\ +"zend_error_noreturn",\ +"zend_error_va_list",\ +"zend_error_va",\ +"zend_error_zstr_at",\ +"zend_error_zstr",\ +"zend_error",\ +"zend_eval_const_expr",\ +"zend_eval_string_ex",\ +"zend_eval_string",\ +"zend_eval_stringl",\ +"zend_exception_error",\ +"zend_exception_restore",\ +"zend_execute_script",\ +"zend_execute_scripts",\ +"zend_execute",\ +"zend_extension_statement_handler",\ +"ZEND_EXT_STMT_SPEC_HANDLER",\ +"ZEND_FE_FETCH_R_SPEC_VAR_HANDLER",\ +"zend_fe_reset_iterator",\ +"ZEND_FE_RESET_R_SPEC_VAR_HANDLER",\ +"zend_fetch_class_by_name",\ +"zend_fetch_debug_backtrace",\ +"ZEND_FETCH_DIM_R_SPEC_CV_CONST_HANDLER",\ +"zend_fetch_dimension_address_read_R_slow",\ +"zend_fetch_dimension_address_read_R",\ +"zend_fetch_dimension_address_read",\ +"ZEND_FETCH_OBJ_FUNC_ARG_SPEC_CV_CONST_HANDLER",\ +"ZEND_FETCH_OBJ_R_SPEC_CV_CONST_HANDLER",\ +"ZEND_FETCH_OBJ_R_SPEC_CV_CV_HANDLER",\ +"ZEND_FETCH_OBJ_R_SPEC_TMPVAR_CONST_HANDLER",\ +"zend_fiber_switch_context",\ +"zend_fiber_trampoline",\ +"zend_file_handle_dtor",\ +"zend_find_array_dim_slow",\ +"zend_free_extra_named_params",\ +"zend_generate_anon_class_name",\ +"zend_generator_close",\ +"ZEND_GENERATOR_CREATE_SPEC_HANDLER",\ +"zend_generator_get_next_delegated_value",\ +"zend_generator_iterator_move_forward",\ +"zend_generator_iterator_rewind",\ +"zend_generator_resume",\ +"ZEND_GENERATOR_RETURN_SPEC_CONST_HANDLER",\ +"ZEND_GENERATOR_RETURN_SPEC_CV_HANDLER",\ +"ZEND_GENERATOR_RETURN_SPEC_OBSERVER_HANDLER",\ +"ZEND_GENERATOR_RETURN_SPEC_TMP_HANDLER",\ +"ZEND_GENERATOR_RETURN_SPEC_VAR_HANDLER",\ +"zend_hash_apply",\ +"zend_hash_del_key_or_index",\ +"zend_hash_graceful_reverse_destroy",\ +"zend_hash_reverse_apply",\ +"ZEND_INCLUDE_OR_EVAL_SPEC_CV_HANDLER",\ +"ZEND_INCLUDE_OR_EVAL_SPEC_OBSERVER_HANDLER",\ +"zend_include_or_eval",\ +"compile_filename",\ +"zend_internal_type_error",\ +"zend_interrupt_helper_SPEC",\ +"zend_invalid_class_constant_type_error",\ +"zend_invalid_method_call",\ +"zend_isset_dim_slow",\ +"ZEND_ISSET_ISEMPTY_DIM_OBJ_SPEC_CV_CONST_HANDLER",\ +"zend_isset_isempty_dim_prop_obj_handler_SPEC_CV_CONST",\ +"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_CV_CONST_HANDLER",\ +"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_CV_HANDLER",\ +"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_CV_TMPVAR_HANDLER",\ +"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_TMPVAR_CONST_HANDLER",\ +"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_TMPVAR_HANDLER",\ +"zend_iterator_dtor",\ +"zend_leave_helper_SPEC",\ +"zend_lex_tstring",\ +"zend_list_close",\ +"zend_llist_apply_with_argument",\ +"zend_llist_apply_with_arguments",\ +"zend_llist_apply_with_del",\ +"zend_llist_apply",\ +"zend_llist_clean",\ +"zend_llist_del_element",\ +"zend_llist_destroy",\ +"zend_load_extension_handle",\ +"zend_lookup_class_ex",\ +"zend_merge_properties",\ +"zend_message_dispatcher",\ +"zend_mm_alloc_huge",\ +"zend_mm_alloc_pages_efree_160",\ +"zend_mm_free_huge",\ +"zend_mm_free_pages",\ +"zend_mm_gc",\ +"zend_mm_realloc_huge",\ +"zend_mm_shutdown_emalloc_24",\ +"zend_multibyte_check_lexer_compatibility",\ +"ZEND_NEW_SPEC_CONST_UNUSED_HANDLER",\ +"zend_non_static_method_call",\ +"zend_objects_destroy_object",\ +"zend_objects_store_call_destructors",\ +"zend_objects_store_del_ref_by_handle_ex",\ +"zend_objects_store_del_ref",\ +"zend_objects_store_del",\ +"zend_objects_store_free_object_storage",\ +"zend_observer_fcall_begin_prechecked",\ +"zend_observer_fcall_begin",\ +"zend_observer_fcall_end",\ +"zend_observer_fcall_end_prechecked",\ +"zend_parse_arg_number_or_str_slow",\ +"zend_parse_arg_str_weak",\ +"zend_parse_arg",\ +"zend_parse_method_parameters",\ +"zend_parse_parameters",\ +"zend_parse_va_args",\ +"zend_post_deactivate_modules",\ +"zend_post_startup",\ +"zend_prepare_string_for_scanning",\ +"zend_print_zval_r_to_buf",\ +"zend_print_zval_r",\ +"zend_print_zval",\ +"zend_ptr_stack_clean",\ +"zend_ptr_stack_reverse_apply",\ +"zend_quick_get_constant",\ +"zend_read_property_ex",\ +"zend_read_property",\ +"zend_readonly_property_modification_error",\ +"zend_rebuild_symbol_table",\ +"zend_ref_add_type_source",\ +"zend_scan_escape_string",\ +"zend_separate_class_constants_table",\ +"zend_set_memory_limit",\ +"zend_set_timeout",\ +"zend_short_circuiting_commit",\ +"zend_shutdown_executor_values",\ +"zend_signal_handler",\ +"zend_sort_4",\ +"zend_sort",\ +"zend_spprintf",\ +"zend_stack_apply_with_argument",\ +"zend_stack_clean",\ +"zend_startup_module_ex",\ +"zend_startup",\ +"zend_std_call_getter",\ +"zend_std_call_user_call",\ +"zend_std_compare_objects",\ +"zend_std_has_dimension",\ +"zend_std_has_property",\ +"zend_std_read_dimension",\ +"zend_std_read_property",\ +"zend_std_unset_dimension",\ +"zend_std_write_dimension",\ +"zend_std_write_property",\ +"zend_stream_fixup",\ +"zend_stream_open",\ +"zend_strip",\ +"zend_strtod",\ +"zend_symtable_to_proptable",\ +"zend_this_not_in_object_context_helper_SPEC",\ +"zend_throw_error",\ +"zend_throw_exception_ex",\ +"zend_throw_exception_internal",\ +"zend_throw_exception_object",\ +"zend_throw_exception_zstr",\ +"zend_throw_exception",\ +"zend_throw_non_object_error",\ +"zend_throw_unwind_exit",\ +"ZEND_TICKS_SPEC_HANDLER",\ +"zend_timeout_handler",\ +"zend_trace_to_string",\ +"zend_try_compile_cv",\ +"zend_try_ct_eval_binary_op",\ +"zend_try_ct_eval_unary_op",\ +"zend_type_error",\ +"zend_undefined_index",\ +"ZEND_UNSET_CV_SPEC_CV_UNUSED_HANDLER",\ +"ZEND_UNSET_DIM_SPEC_CV_CONST_HANDLER",\ +"ZEND_UNSET_DIM_SPEC_CV_CV_HANDLER",\ +"ZEND_UNSET_DIM_SPEC_CV_TMPVAR_HANDLER",\ +"ZEND_UNSET_DIM_SPEC_VAR_CONST_HANDLER",\ +"ZEND_UNSET_DIM_SPEC_VAR_CV_HANDLER",\ +"ZEND_UNSET_DIM_SPEC_VAR_TMPVAR_HANDLER",\ +"ZEND_UNSET_OBJ_SPEC_CV_CONST_HANDLER",\ +"ZEND_UNSET_OBJ_SPEC_CV_CV_HANDLER",\ +"ZEND_UNSET_OBJ_SPEC_CV_TMPVAR_HANDLER",\ +"ZEND_UNSET_OBJ_SPEC_UNUSED_CONST_HANDLER",\ +"ZEND_UNSET_OBJ_SPEC_UNUSED_CV_HANDLER",\ +"ZEND_UNSET_OBJ_SPEC_UNUSED_TMPVAR_HANDLER",\ +"ZEND_UNSET_OBJ_SPEC_VAR_CONST_HANDLER",\ +"ZEND_UNSET_OBJ_SPEC_VAR_CV_HANDLER",\ +"ZEND_UNSET_OBJ_SPEC_VAR_TMPVAR_HANDLER",\ +"zend_unset_property",\ +"ZEND_UNSET_VAR_SPEC_CV_UNUSED_HANDLER",\ +"zend_update_class_constant",\ +"zend_update_class_constants",\ +"zend_update_property_ex",\ +"zend_update_property_long",\ +"zend_update_property_str",\ +"zend_update_property_string",\ +"zend_update_property",\ +"zend_use_new_element_for_string",\ +"zend_use_tmp_in_write_context_helper_SPEC",\ +"zend_use_undef_in_read_context_helper_SPEC",\ +"zend_user_it_get_current_data",\ +"zend_user_it_get_new_iterator",\ +"ZEND_USER_OPCODE_SPEC_HANDLER",\ +"ZEND_INCLUDE_OR_EVAL_SPEC_CONST_HANDLER",\ +"ZEND_INCLUDE_OR_EVAL_SPEC_TMPVAR_HANDLER",\ +"zend_value_error",\ +"zend_verify_ref_array_assignable",\ +"zend_vm_stack_extend",\ +"zend_vspprintf",\ +"zend_vstrpprintf",\ +"zend_wrong_assign_to_variable_reference",\ +"zend_wrong_clone_call",\ +"zend_wrong_property_read",\ +"ZEND_YIELD_FROM_SPEC_CV_HANDLER",\ +"ZEND_YIELD_FROM_SPEC_TMPVAR_HANDLER",\ +"zendi_smart_streq",\ +"zendi_try_get_long",\ +"zif_array_filter",\ +"zif_array_map",\ +"zif_array_product",\ +"zif_array_reduce",\ +"zif_array_sum",\ +"zif_array_unique",\ +"zif_cal_days_in_month",\ +"zif_cal_from_jd",\ +"zif_cal_to_jd",\ +"zif_call_user_func_array",\ +"zif_call_user_func",\ +"zif_chmod",\ +"zif_count",\ +"zif_curl_exec",\ +"zif_curl_multi_exec",\ +"zif_curl_multi_select",\ +"zif_current",\ +"zif_debug_print_backtrace",\ +"zif_define",\ +"zif_end",\ +"zif_exec",\ +"zif_exif_imagetype",\ +"zif_exif_read_data",\ +"zif_exif_thumbnail",\ +"zif_fclose",\ +"zif_feof",\ +"zif_file_get_contents",\ +"zif_file_put_contents",\ +"zif_fopen",\ +"zif_fread",\ +"zif_fsockopen",\ +"zif_fwrite",\ +"zif_gc_collect_cycles",\ +"zif_get_mangled_object_vars",\ +"zif_get_object_vars",\ +"zif_getenv",\ +"zif_getimagesize",\ +"zif_hash_copy",\ +"zif_hash_final",\ +"zif_hash_hkdf",\ +"zif_hash_init",\ +"zif_hash_pbkdf2",\ +"zif_hash_update_file",\ +"zif_hash_update_stream",\ +"zif_hash_update",\ +"zif_http_build_query",\ +"zif_iterator_apply",\ +"zif_iterator_count",\ +"zif_json_encode",\ +"zif_key",\ +"zif_lcg_value",\ +"zif_method_exists",\ +"zif_mysqli_begin_transaction",\ +"zif_mysqli_connect",\ +"zif_mysqli_data_seek",\ +"zif_mysqli_errno",\ +"zif_mysqli_error",\ +"zif_mysqli_fetch_fields",\ +"zif_mysqli_fetch_object",\ +"zif_mysqli_fetch_row",\ +"zif_mysqli_get_charset",\ +"zif_mysqli_link_construct",\ +"zif_mysqli_multi_query",\ +"zif_mysqli_next_result",\ +"zif_mysqli_ping",\ +"zif_mysqli_prepare",\ +"zif_mysqli_query",\ +"zif_mysqli_real_connect",\ +"zif_mysqli_reap_async_query",\ +"zif_mysqli_select_db",\ +"zif_mysqli_set_charset",\ +"zif_mysqli_stmt_execute",\ +"zif_mysqli_stmt_fetch",\ +"zif_next",\ +"zif_passthru",\ +"zif_phar_file_get_contents",\ +"zif_phar_fopen",\ +"zif_popen",\ +"zif_post_message_to_js",\ +"zif_preg_replace_callback",\ +"zif_prev",\ +"zif_proc_close",\ +"zif_proc_open",\ +"zif_property_exists",\ +"zif_rand",\ +"zif_rename",\ +"zif_reset",\ +"zif_shell_exec",\ +"zif_sleep",\ +"zif_spl_autoload_call",\ +"zif_spl_autoload",\ +"zif_str_shuffle",\ +"zif_stream_get_contents",\ +"zif_stream_resolve_include_path",\ +"zif_stream_select",\ +"zif_stream_socket_client",\ +"zif_stream_socket_enable_crypto",\ +"zif_system",\ +"zif_touch",\ +"zif_unlink",\ +"zif_wasm_popen",\ +"zim_AppendIterator_append",\ +"zim_AppendIterator_current",\ +"zim_AppendIterator_next",\ +"zim_AppendIterator_rewind",\ +"zim_CachingIterator_hasNext",\ +"zim_CachingIterator_rewind",\ +"zim_DateInterval___wakeup",\ +"zim_DatePeriod___wakeup",\ +"zim_DateTime___wakeup",\ +"zim_DateTimeImmutable___wakeup",\ +"zim_DateTimeZone___wakeup",\ +"zim_FilterIterator_next",\ +"zim_FilterIterator_rewind",\ +"zim_HashContext___serialize",\ +"zim_HashContext___unserialize",\ +"zim_mysqli___construct",\ +"zim_mysqli_result___construct",\ +"zim_mysqli_stmt___construct",\ +"zim_NoRewindIterator_current",\ +"zim_NoRewindIterator_key",\ +"zim_NoRewindIterator_next",\ +"zim_NoRewindIterator_valid",\ +"zim_PDO___construct",\ +"zim_PDO_beginTransaction",\ +"zim_PDO_commit",\ +"zim_PDO_commitTransaction",\ +"zim_PDO_dbh_constructor",\ +"zim_PDO_errorInfo",\ +"zim_PDO_exec",\ +"zim_PDO_getAttribute",\ +"zim_PDO_inTransaction",\ +"zim_PDO_lastInsertId",\ +"zim_PDO_prepare",\ +"zim_PDO_query",\ +"zim_PDO_quote",\ +"zim_PDO_rollBack",\ +"zim_PDOStatement_closeCursor",\ +"zim_PDOStatement_errorInfo",\ +"zim_PDOStatement_execute",\ +"zim_PDOStatement_fetch",\ +"zim_PDOStatement_fetchAll",\ +"zim_PDOStatement_getAttribute",\ +"zim_PDOStatement_getColumnMeta",\ +"zim_PDOStatement_setAttribute",\ +"zim_reflection_class_newInstanceArgs",\ +"zim_reflection_method_invoke",\ +"zim_reflection_method_invokeArgs",\ +"zim_ReflectionClass_newInstanceArgs",\ +"zim_ReflectionMethod_invoke",\ +"zim_ReflectionMethod_invokeArgs",\ +"zim_SessionHandler_create_sid",\ +"zim_SessionHandler_destroy",\ +"zim_SessionHandler_gc",\ +"zim_SessionHandler_read",\ +"zim_SessionHandler_write",\ +"zim_SplObjectStorage___debugInfo",\ +"zim_sqlite3_backup",\ +"zim_SQLite3_backup",\ +"zim_SQLite3_exec",\ +"zim_sqlite3_exec",\ +"zim_sqlite3_query",\ +"zim_sqlite3_querySingle",\ +"zim_sqlite3result_fetchArray",\ +"php_cli_startup",\ +"php_stdiop_close",\ +"zend_parse_ini_file",\ +"php_embed_startup",\ +"zip_source_file_common_new",\ +"zip_source_function_create",\ +"zip_source_layered_create",\ +"zm_startup_basic",\ +"zm_startup_core",\ +"zm_startup_date",\ +"zm_startup_dir",\ +"zm_startup_hash",\ +"zm_startup_json",\ +"zm_startup_mysqli",\ +"zm_startup_password",\ +"zm_startup_pdo",\ +"zm_startup_random",\ +"zm_startup_reflection",\ +"zm_startup_session",\ +"zm_startup_spl_array",\ +"zm_startup_spl_directory",\ +"zm_startup_spl_dllist",\ +"zm_startup_spl_exceptions",\ +"zm_startup_spl_fixedarray",\ +"zm_startup_spl_heap",\ +"zm_startup_spl_iterators",\ +"zm_startup_spl_observer",\ +"zm_startup_sqlite3",\ +"zm_startup_tokenizer",\ +"zm_startup_user_filters",\ +"zm_startup_zlib",\ +"zval_copy_ctor_func",\ +"zval_get_double_func",\ +"zval_get_long_func",\ +"zval_get_string_func",\ +"zval_make_interned_string",\ +"zval_ptr_dtor",\ +"zval_try_get_string_func",\ +"zval_undefined_cv",\ +"zval_update_constant_ex"';\ + export ASYNCIFY_ONLY="$ASYNCIFY_ONLY_UNPREFIXED"; \ + + echo -n ' -s ASYNCIFY_ONLY=['$ASYNCIFY_ONLY'] '| tr -d "\n" >> /root/.emcc-php-asyncify-flags; + + +# List global and exports symbols +COPY ./compile/php/exported-functions.list /root/exported-functions.list +RUN cat /root/exported-functions.list >> /root/.JS_ABI_EXPORTS + +# WORKAROUND: Asyncify builds do not currently include __c_longjmp, +# but we require it for JSPI builds. Including the symbol in our +# main exported-functions.list will cause Asyncify build errors, +# so we dynamically add it here instead. +RUN if [ "$EMSCRIPTEN_ENVIRONMENT" = "node" ] && [ "$WITH_JSPI" = "yes" ]; then \ + echo '___c_longjmp' >> /root/.JS_ABI_EXPORTS; \ + fi; + +COPY ./${EMSCRIPTEN_ENVIRONMENT}-builds/ /root/${EMSCRIPTEN_ENVIRONMENT}-builds +COPY ./compile/shared/ /root/shared/ +RUN set -eux; \ + PHP_VERSION_DIR="$(echo "$PHP_VERSION" | cut -d. -f1,2 | tr . -)"; \ + ASYNCIFY_OR_JSPI_DIR=$( [ "$WITH_JSPI" = "yes" ] && echo "jspi" || echo "asyncify" ); \ + EXTENSIONS_DIR_PATH="/root/${EMSCRIPTEN_ENVIRONMENT}-builds/${PHP_VERSION_DIR}/${ASYNCIFY_OR_JSPI_DIR}/extensions"; \ + for path in "$EXTENSIONS_DIR_PATH"/*; do \ + extension="${path##*/}"; \ + # List globals symbols [ env func and globals without invoke_ and libc++(_Z) prefixed ones ] + /root/emsdk/upstream/bin/wasm-dis "$EXTENSIONS_DIR_PATH/${extension}/${extension}.so" \ + | sed -n 's/^[[:space:]]*(import "env" "\([^"]*\)"[[:space:]]*(\(func\|global\).*/\1/p' \ + | sed '/^invoke_/d' \ + | sed '/^_Z/d' \ + | sed 's/^/_/' \ + | sort \ + >> /root/.JS_ABI_EXPORTS; \ + # List exports symbols [ GOT.mem and GOT.func globals ] + /root/emsdk/upstream/bin/wasm-dis "$EXTENSIONS_DIR_PATH/${extension}/${extension}.so" \ + | sed -n 's/^[[:space:]]*(import "\(GOT\.mem\|GOT\.func\)" "\([^"]*\)"[[:space:]]*(global.*/\2/p' \ + | sort \ + >> /root/.WASM_ABI_EXPORTS; \ + # Inject extension-specific exported functions + if [ -f /root/shared/${extension}/exported-functions.list ]; then \ + cat /root/shared/${extension}/exported-functions.list >> /root/.JS_ABI_EXPORTS; \ + fi; \ + done + +RUN set -eux; \ + sort -u /root/.JS_ABI_EXPORTS | \ + grep -v '^[[:space:]]*$' | \ + sed 's/^/ "/; s/$/",/' | \ + sed '$ s/,$//' | \ + { echo '['; cat; echo ']'; } \ + > /root/.exported-functions + +RUN set -eux; \ + sort -u /root/.WASM_ABI_EXPORTS | \ + grep -v '^[[:space:]]*$' | \ + sed 's/^/-Wl,--export=/' \ + > /root/.wasm-exports + + +# Build the final .wasm file +RUN mkdir /root/output +COPY ./compile/php/phpwasm-emscripten-library.js /root/phpwasm-emscripten-library.js +COPY ./compile/php/phpwasm-emscripten-library-dynamic-linking.js /root/phpwasm-emscripten-library-dynamic-linking.js +COPY ./compile/php/phpwasm-emscripten-library-file-locking-for-node.js /root/phpwasm-emscripten-library-file-locking-for-node.js +COPY ./compile/php/phpwasm-emscripten-library-dns-lookup-for-node.js /root/phpwasm-emscripten-library-dns-lookup-for-node.js +COPY ./compile/php/phpwasm-emscripten-library-known-undefined-functions.js /root/phpwasm-emscripten-library-known-undefined-functions.js +ARG WITH_SOURCEMAPS +ARG WITH_DEBUG +ARG STACK_SIZE +RUN set -euxo pipefail; \ + mkdir -p /build/output; \ + source /root/emsdk/emsdk_env.sh; \ + if [ "$WITH_JSPI" = "yes" ]; then \ + # Both imports and exports are required for inter-module communication with wrapped methods, e.g., wasm_recv. + export ASYNCIFY_FLAGS=" -s ASYNCIFY=2 -sSUPPORT_LONGJMP=wasm -fwasm-exceptions -sJSPI_IMPORTS=js_open_process,js_fd_read,js_waitpid,js_process_status,js_create_input_device,wasm_setsockopt,wasm_shutdown,wasm_close,wasm_recv,wasm_connect,__syscall_fcntl64,js_flock,js_release_file_locks,js_waitpid -sJSPI_EXPORTS=php_wasm_init,wasm_sleep,wasm_read,emscripten_sleep,wasm_sapi_handle_request,wasm_sapi_request_shutdown,wasm_poll_socket,wrap_select,__wrap_select,select,php_pollfd_for,fflush,wasm_popen,wasm_read,wasm_php_exec,run_cli,wasm_recv,wasm_connect,__wasm_call_ctors,__errno_location,__funcs_on_exit -s EXPORTED_RUNTIME_METHODS=HEAPU32,HEAPU8,ccall,PROXYFS,wasmExports "; \ + echo '#define PLAYGROUND_JSPI 1' > /root/php_wasm_asyncify.h; \ + else \ + export ASYNCIFY_FLAGS=" -s ASYNCIFY=1 -s ASYNCIFY_IGNORE_INDIRECT=1 -s EXPORTED_RUNTIME_METHODS=HEAPU32,HEAPU8,ccall,PROXYFS,wasmExports,UTF8ToString,lengthBytesUTF8,stringToUTF8 $(cat /root/.emcc-php-asyncify-flags) "; \ + echo '' > /root/php_wasm_asyncify.h; \ + fi; \ + export OPTIMIZATION_FLAGS="-O3"; \ + if [ "${WITH_SOURCEMAPS}" = "yes" ] || [ "${WITH_DEBUG}" = "yes" ]; then \ + export OPTIMIZATION_FLAGS="-O0"; \ + fi; \ + PLATFORM_SPECIFIC_ARGS=''; \ + ENVIRONMENT="$EMSCRIPTEN_ENVIRONMENT"; \ + if [ "$EMSCRIPTEN_ENVIRONMENT" = "node" ]; then \ + PLATFORM_SPECIFIC_ARGS="$PLATFORM_SPECIFIC_ARGS -DPHP_WASM_FILE_LOCKING_SUPPORT=1"; \ + PLATFORM_SPECIFIC_ARGS="$PLATFORM_SPECIFIC_ARGS --js-library /root/phpwasm-emscripten-library-file-locking-for-node.js --js-library /root/phpwasm-emscripten-library-dns-lookup-for-node.js -Wl,--wrap=getpid "; \ + else \ + # Prevents "document is not defined" errors in Web Workers when using MAIN_MODULE. + # By default, Emscripten assumes MAIN_MODULE runs only in a web environment, + # so it injects WebGL and other browser-only APIs. Explicitly adding "worker" + # ensures the runtime is compatible with Web Workers as well. + ENVIRONMENT="$EMSCRIPTEN_ENVIRONMENT,worker"; \ + fi; \ + emcc $OPTIMIZATION_FLAGS \ + --js-library /root/phpwasm-emscripten-library.js \ + --js-library /root/phpwasm-emscripten-library-dynamic-linking.js \ + --js-library /root/phpwasm-emscripten-library-known-undefined-functions.js \ + $PLATFORM_SPECIFIC_ARGS \ + -I . \ + -I ext \ + -I ext/json \ + -I Zend \ + -I main \ + -I TSRM/ \ + -I/root/lib -I/root/lib/include \ + -L/root/lib -L/root/lib/lib/ \ + -D__x86_64__\ + -lproxyfs.js \ + $ASYNCIFY_FLAGS \ + $(cat /root/.emcc-php-wasm-flags) \ + -s MAIN_MODULE=2 \ + -s EXPORTED_FUNCTIONS="@/root/.exported-functions" \ + -s WASM_BIGINT=1 \ + -s MIN_NODE_VERSION=200900 \ + -s INITIAL_MEMORY=64MB \ + -s ALLOW_MEMORY_GROWTH=1 \ + -s STACK_SIZE="$STACK_SIZE" \ + -s ASSERTIONS=0 \ + -s ERROR_ON_UNDEFINED_SYMBOLS=1 \ + -s NODEJS_CATCH_EXIT=0 \ + -s NODEJS_CATCH_REJECTION=0 \ + -s INVOKE_RUN=0 \ + -o /build/output/php.js \ + -s EXIT_RUNTIME=1 \ + -Wl,@/root/.wasm-exports \ + -Wl,--wrap=usleep \ + -Wl,--wrap=select \ + /root/lib/libphp.a \ + /root/proc_open.c \ + /root/php_wasm.c \ + $(cat /root/.emcc-php-wasm-sources) \ + -s ENVIRONMENT=$ENVIRONMENT \ + -s FORCE_FILESYSTEM=1 \ + -s EXPORT_NAME="'PHPLoader'" + # Emscripten complains it can't find some Asyncify functions + # listed in ASYNCIFY_IMPORTS. The culprit is those functions + # are actually used and delisting them breaks php_pollfd_for(). + # Sooo... just ignore Emscripten's warnings. + +# # At the moment terminfo is baked into the repository in +# # src/php-cli/terminfo, but if we ever need to update it, we can +# # use the terminfo database produced by the ncurses make script: +# RUN if [ "$EMSCRIPTEN_ENVIRONMENT" = "node" ]; then \ +# mkdir -p /root/output/terminfo/x && \ +# cp -v /root/lib/share/terminfo/x/xterm* /root/output/terminfo/x/; \ +# fi + +# Postprocess the build php.js module: +COPY ./compile/php/esm-node-boilerplate.js /root/esm-node-boilerplate.js +COPY ./compile/php/esm-prefix.js /root/esm-prefix.js +COPY ./compile/php/esm-suffix.js /root/esm-suffix.js +RUN set -euxo pipefail; \ + cp -rfv /build/output/* /root/output/; \ + # Figure out the target file names and URLs + # The .js and .wasm filenames should reflect the build configuration, e.g.: + # * `php-7.4.node.js` and `php-7.4.node.wasm` + # * `php-8.0.js` and `php-8.0.wasm` + # In addition, the `.wasm` file URL should have a "cache busting" query string on the + # web, so that web browsers will reload it when the file contents change. + # Precompute export variables: + export FILE_SIZE=$(stat -c%s "/root/output/php.wasm") && \ + export PHP_VERSION_ESCAPED="${PHP_VERSION//./_}" && \ + export FILENAME_PREFIX="php_${PHP_VERSION_ESCAPED:0:3}"; \ + export EXT_PREFIX=""; \ + export JS_FILENAME="${FILENAME_PREFIX}${EXT_PREFIX}.js"; \ + export WASM_FILENAME="${FILENAME_PREFIX}${EXT_PREFIX}.wasm"; \ + # Make the php.wasm URL configurable via the dependencyFilename loader argument: + /root/replace.sh $'s/["\']php\.wasm[\'"]/dependencyFilename/g' /root/output/php.js; \ + # Patch a "property undefined" error + # Emscripten produces an if that checks a stream.stream_ops.poll property. However, + # stream.stream_ops is sometimes undefined and the check fails. Let's adjust it to + # tolerate a null stream.stream_ops value. + /root/replace.sh "s/if\s*\(stream\.stream_ops\.poll\)/if (stream.stream_ops?.poll)/g" /root/output/php.js || true; \ + # Emscripten allows only directories to be mounted, but in Playground we support mounting files, directories, and symlinks. + # For file mounting to work, we need to remove the directory check. + /root/replace-across-lines.sh 's/(\s*)if\s*\(\s*!FS\.isDir\(node\.mode\)\s*\)\s*\{\s*throw\s*new\s*FS\.ErrnoError\(54\)\s*;?\s*\}(\s*)/$1$2/s' /root/output/php.js; \ + # Make Emscripten websockets configurable + # Emscripten makes the Websocket proxy connect to a fixed URL. + # This assumes the traffic is always forwarded to the same target. + # However, we want to support arbitrary targets, so we need to + # replace the hardcoded websocket target URL with a dynamic callback. + /root/replace.sh $'s/if\s*\(\s*SOCKFS\.websocketArgs\[[\'"]url[\'"]\]\s*\)/if("function"===typeof SOCKFS.websocketArgs["url"]) {\nurl = SOCKFS.websocketArgs["url"](...arguments);\n}else if ("string" === typeof SOCKFS.websocketArgs["url"])/g' \ + /root/output/php.js; \ + # Enable custom WebSocket constructors to support socket options. + /root/replace.sh "s/ws\s*=\s*new WebSocketConstructor/if (Module['websocket']['decorator']) {WebSocketConstructor = Module['websocket']['decorator'](WebSocketConstructor);}ws = new WebSocketConstructor/g" /root/output/php.js && \ + if [ "$EMSCRIPTEN_ENVIRONMENT" = "node" ]; then \ + if [ "$WITH_WS_NETWORKING_PROXY" = "yes" ]; then \ + /root/replace.sh "s/sock\.server\s*=\s*new WebSocketServer/if (Module['websocket']['serverDecorator']) {WebSocketServer = Module['websocket']['serverDecorator'](WebSocketServer);}sock.server = new WebSocketServer/g" /root/output/php.js; \ + fi; \ + fi; \ + # PIPEFS: return 0 (success) instead of 6 (EWOULDBLOCK) when reached end of data + # and the other end of the pipe is closed. + /root/replace-across-lines.sh 's/if\s*\(\s*currentLength\s*==\s*0\s*\)\s*\{.{0,200}throw new FS.ErrnoError\(\s*6\s*\)/if(currentLength==0){if(pipe.refcnt<2){return 0;}throw new FS.ErrnoError(6)/sg' /root/output/php.js; \ + # Add MSG_PEEK flag support in recvfrom + # + # Emscripten ignores the flags argument to ___syscall_recvfrom. + # However, PHP relies on passing the MSG_PEEK (== integer 2) flag when polling + # the stream for data. + # MSG_PEEK enables reading bytes without moving the stream pointer forward. + # Without the patch below, PHP consumes the first byte from the top of the + # response stream, typically "H" in "HTTP/1.1 200 OK", and then fails after + # reading the remaining "TTP/1.1 200 OK" and not recognizing it as a valid + # status line. + # We need to patch the syscall to support the MSG_PEEK flag. + if [ "$WITH_WS_NETWORKING_PROXY" = "yes" ]; then \ + /root/replace.sh 's/sock\.sock_ops\.recvmsg\(sock,\s*len\);/sock.sock_ops.recvmsg(sock, len, typeof flags !== "undefined" ? flags : 0);/g' /root/output/php.js; \ + /root/replace.sh 's/recvmsg\(sock,\s*length\)\s*{/recvmsg(sock, length, flags) {/g' /root/output/php.js; \ + /root/replace.sh 's/if\s*\(sock\.type\s*===\s*1\s*&&\s*bytesRead\s*<\s*queuedLength\)/if (flags&2) {bytesRead = 0;} if (sock.type === 1 && bytesRead < queuedLength)/g' /root/output/php.js; \ + fi ; \ + # Replace the hardcoded ENVIRONMENT variable with a dynamic computation + # + # The JavaScript code of the web loader and web worker loader is identical, + # but Emscripten forces running different code paths by setting + # + # ENVIRONMENT_IS_WEB = true; ENVIRONMENT_IS_WORKER = false + # + # This project supports both environments and would have to maintain two + # separate copies of the code. Instead, we use a dynamic computation of the + # environment, based on the `RuntimeName` variable, which is an argument to the + # wrapper function. + /root/replace.sh $'s/ENVIRONMENT_IS_([A-Z]+)\s*=[^;]*;/ENVIRONMENT_IS_$1=RuntimeName==="$1";/g' /root/output/php.js; \ + /root/replace.sh 's/var ENV\s*=\s*\{/var ENV = PHPLoader.ENV || {/g' /root/output/php.js; \ + # Expose required functions to the module. + # Emscripten 4.0.10 removed the runtime exposure of `_malloc` and `_free`, + # so we rebind them from `wasmExports` and attach them to PHPLoader. + /root/replace.sh "s/_wasm_free\s*=\s*Module\[[\"']_wasm_free[\"']\]/_wasm_free = PHPLoader['free'] = Module['_wasm_free']/g" /root/output/php.js; \ + /root/replace.sh "s/_malloc\s*=\s*Module\[[\"']_malloc[\"']\]/_malloc = PHPLoader['malloc'] = Module['_malloc']/g" /root/output/php.js; \ + # Add alias to wasmImports for dynamically loaded extensions. + # The memcached extension imports wasm_recv and wasm_setsockopt, + # but the main module only exposes wasm_setsockopt in wasmImports. + # This alias ensure that dynamically loaded extensions can + # correctly resolve the required import. + if [ "$EMSCRIPTEN_ENVIRONMENT" = "node" ]; then \ + # Patch wasm_recv export alongside wasm_setsockopt. The pattern + # may not match in PHP 5.x builds, so allow the replace to fail. + /root/replace.sh 's/wasm_setsockopt:\s*_wasm_setsockopt,/wasm_setsockopt: _wasm_setsockopt,\n \/** \@export *\/ wasm_recv: _wasm_recv,/g' /root/output/php.js || true; \ + fi; \ + # Patch Emscripten exit status to include a stack trace + # Override Emscripten's default ExitStatus class which gets + # thrown on failure. Unfortunately, the default object is not + # a subclass of Error and does not provide any stack trace. + # + # This is a deliberate behavior on Emscripten's end to prevent + # memory leaks after the program exits. See: + # + # https://github.com/emscripten-core/emscripten/pull/9108 + # + # In case of WordPress Playground, the worker in which the PHP + # runs will typically exit after the PHP program finishes, so + # we don't have to worry about memory leaks. + /root/replace-across-lines.sh "s/(class\\s+ExitStatus\\s*{\\s*name\\s*=\\s*['\"]ExitStatus['\"];\\s*constructor\\s*\(\\s*status\\s*\)\\s*{\\s*this\\.message\\s*=\\s*\`Program terminated with exit\(\\\${status}\)\`;\s*this\\.status\\s*=\\s*status;?\\s*}\\s*})/\$1\nExitStatus = class PHPExitStatus extends Error {\n constructor\(status\) {\n super\(status\);\n this.name = 'ExitStatus';\n this.message = 'Program terminated with exit\(' + status + '\)';\n this.status = status;\n }\n};/" /root/output/php.js; \ + # Restore quit handling to Emscripten + # Quit callback customization was removed from Emscripten by: + # https://github.com/emscripten-core/emscripten/pull/22371 + # But the alternative `onExit()` handling is conditional and has different semantics. + # To avoid bugs due to this change, let's just restore support for quit callback customization. + /root/replace.sh "s/(if\s*\(Module\[['\"]thisProgram['\"]\]\)\s*thisProgram\s*=\s*Module\[['\"]thisProgram['\"]\];)/\$1\nif (Module[\"quit\"]) quit_=Module[\"quit\"];/" /root/output/php.js; \ + # Cache Asyncify sleep data allocation to prevent memory growth corruption + # + # Emscripten's handleSleep() calls _malloc() on every async operation and + # _free() on every rewind. If _malloc triggers memory.grow(), the ArrayBuffer + # detaches and corrupts state. Fix: allocate once, reuse on subsequent sleeps. + # See: https://github.com/emscripten-core/emscripten/issues/21108 + if [ "$WITH_JSPI" != "yes" ]; then \ + /root/replace.sh \ + 's/Asyncify\.currData\s*=\s*Asyncify\.allocateData\(\)/if (!Asyncify._cachedData) { Asyncify._cachedData = Asyncify.allocateData(); } else { Asyncify.setDataHeader(Asyncify._cachedData, Asyncify._cachedData + 12, Asyncify.StackSize); Asyncify.setDataRewindFunc(Asyncify._cachedData); } Asyncify.currData = Asyncify._cachedData/' \ + /root/output/php.js; \ + /root/replace-across-lines.sh \ + 's/runAndAbortIfError\(_asyncify_stop_rewind\);\s*_free\(Asyncify\.currData\);/runAndAbortIfError(_asyncify_stop_rewind);/s' \ + /root/output/php.js; \ + fi; \ + # Node.js: Create a require() function and add 'currentDirPath' declaration + # because Emscripten generates Node.js code that requires them. + if [ "$EMSCRIPTEN_ENVIRONMENT" = "node" ]; then \ + cat /root/esm-node-boilerplate.js >> /root/output/php-module.js; \ + fi; \ + # Turn the php.js file into an ES module + # Manually turn the output into a esm module instead of relying on -s MODULARIZE=1. + # which pollutes the global namespace and does not play well with import() mechanics. + if [ "$EMSCRIPTEN_ENVIRONMENT" = "node" ]; then \ + echo "const dependencyFilename = path.join(currentDirPath, '${PHP_VERSION_ESCAPED}', '$WASM_FILENAME');" >> /root/output/php-module.js;\ + /root/replace.sh "s/scriptDirectory\s*=\s*__dirname/scriptDirectory = currentDirPath/" /root/output/php.js; \ + else \ + echo "import dependencyFilename from './${PHP_VERSION_ESCAPED}/$WASM_FILENAME'; " >> /root/output/php-module.js; \ + fi; \ + echo "export { dependencyFilename }; " >> /root/output/php-module.js && \ + echo "export const dependenciesTotalSize = $FILE_SIZE; " >> /root/output/php-module.js && \ + echo "const phpVersionString = '$PHP_VERSION';" >> /root/output/php-module.js && \ + cat /root/esm-prefix.js >> /root/output/php-module.js && \ + cat /root/output/php.js >> /root/output/php-module.js && \ + cat /root/esm-suffix.js >> /root/output/php-module.js && \ + \ + # Remove the old php.js file + rm /root/output/php.js && \ + \ + # Rename the build files to their final names + mv /root/output/php-module.js "/root/output/$JS_FILENAME"; \ + mkdir -p /root/output/${PHP_VERSION_ESCAPED}/; \ + mv /root/output/php.wasm "/root/output/${PHP_VERSION_ESCAPED}/$WASM_FILENAME"; \ + if [ "${WITH_SOURCEMAPS}" = "yes" ]; then \ + # Make the source map paths relative to the .wasm file, not the build directory + sed -i 's#"\.\./\.\./root/#"#g' /root/output/php.wasm.map; \ + mv /root/output/php.wasm.map "/root/output/${PHP_VERSION_ESCAPED}/php.wasm.map"; \ + fi; \ + if [ "${WITH_SOURCEMAPS}" = "yes" ] || [ "${WITH_DEBUG}" = "yes" ]; then \ + # Make PHP source available for use with step debugger + rm -rf /root/php-src/.git; \ + cp -r /root/php-src /root/output/"${PHP_VERSION_ESCAPED}"/php-src; \ + # Copy emscripten source files + mkdir -p /root/output/"${PHP_VERSION_ESCAPED}"/emscripten; \ + cp -r /root/emsdk/upstream/emscripten/cache /root/output/"${PHP_VERSION_ESCAPED}"/emscripten/; \ + cp -r /root/emsdk/upstream/emscripten/system /root/output/"${PHP_VERSION_ESCAPED}"/emscripten/; \ + fi; diff --git a/packages/php-wasm/compile/php/apply-mysqlnd-patch.sh b/packages/php-wasm/compile/php/apply-mysqlnd-patch.sh index 50e5c888960..dc968bf1980 100644 --- a/packages/php-wasm/compile/php/apply-mysqlnd-patch.sh +++ b/packages/php-wasm/compile/php/apply-mysqlnd-patch.sh @@ -9,8 +9,8 @@ TARGET_FILE="php-src/ext/mysqlnd/mysqlnd_connection.c" if [ ! -f "$TARGET_FILE" ]; then - echo "Error: $TARGET_FILE not found" - exit 1 + echo "Skipping mysqlnd patch: $TARGET_FILE not found (may not exist in this PHP version)" + exit 0 fi if grep -q "effective_host" "$TARGET_FILE"; then @@ -31,6 +31,13 @@ else if [ -f "$TARGET_FILE.bak" ]; then mv "$TARGET_FILE.bak" "$TARGET_FILE" fi - echo "Failed to apply mysqlnd patch to $TARGET_FILE" + echo "Warning: Could not apply mysqlnd patch to $TARGET_FILE (pattern not found, may be a different PHP version)" + # Older PHP (<7) has different mysqlnd code and we tolerate pattern + # misses there. On PHP 7+ the pattern is expected to match; fail + # loudly so a silent fallback to Unix sockets never ships. + PHP_MAJOR_VERSION="${PHP_VERSION%%.*}" + if [ "${PHP_MAJOR_VERSION:-0}" -lt 7 ]; then + exit 0 + fi exit 1 fi diff --git a/packages/php-wasm/compile/php/php5.2-openssl-compat.patch b/packages/php-wasm/compile/php/php5.2-openssl-compat.patch new file mode 100644 index 00000000000..98ee823a442 --- /dev/null +++ b/packages/php-wasm/compile/php/php5.2-openssl-compat.patch @@ -0,0 +1,613 @@ +diff --git a/php-src/ext/openssl/openssl.c b/php-src/ext/openssl/openssl.c +--- a/php-src/ext/openssl/openssl.c ++++ b/php-src/ext/openssl/openssl.c +@@ -46,6 +46,195 @@ + #include + #include + ++/* OpenSSL 1.1.0 compatibility: opaque structs require accessor functions. ++ * ++ * OpenSSL 1.1.0 made EVP_PKEY, RSA, DSA, DH, EVP_MD_CTX, EVP_CIPHER_CTX, ++ * X509, X509_EXTENSION structs opaque. The multi-return accessor functions ++ * (RSA_get0_key, DSA_get0_pqg, etc.) were added in 1.1.0. The single-value ++ * accessors (RSA_get0_n, etc.) were added in 1.1.1. We provide shims for ++ * both directions so the patched code works with any OpenSSL version. */ ++ ++#if OPENSSL_VERSION_NUMBER >= 0x10100000L ++ ++/* EVP_dss1() was removed in OpenSSL 1.1.0. It returned EVP_sha1() ++ * with DSA key type handling, but in 1.1.0 the key type is implicit. */ ++#define EVP_dss1() EVP_sha1() ++ ++/* RAND_egd() was removed in OpenSSL 1.1.0 (EGD is long obsolete). ++ * Stub it to always return 0 (not an EGD socket). */ ++static int RAND_egd(const char *path) { (void)path; return 0; } ++ ++/* OpenSSL >= 1.1.0 has the multi-return accessors but the single-value ++ * helpers (RSA_get0_n, RSA_get0_e, RSA_get0_d) only arrived in 1.1.1. ++ * Provide them unconditionally for 1.1.0. */ ++#if OPENSSL_VERSION_NUMBER < 0x10101000L ++static const BIGNUM *RSA_get0_n(const RSA *r) { const BIGNUM *v = NULL; RSA_get0_key(r, &v, NULL, NULL); return v; } ++static const BIGNUM *RSA_get0_e(const RSA *r) { const BIGNUM *v = NULL; RSA_get0_key(r, NULL, &v, NULL); return v; } ++static const BIGNUM *RSA_get0_d(const RSA *r) { const BIGNUM *v = NULL; RSA_get0_key(r, NULL, NULL, &v); return v; } ++#endif ++ ++#else /* OpenSSL < 1.1.0 - provide all accessor shims */ ++ ++static int EVP_PKEY_id(const EVP_PKEY *pkey) { return pkey->type; } ++static int EVP_PKEY_base_id(const EVP_PKEY *pkey) { return EVP_PKEY_type(pkey->type); } ++ ++static RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey) { return pkey->pkey.rsa; } ++static DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey) { return pkey->pkey.dsa; } ++static DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey) { return pkey->pkey.dh; } ++#ifdef HAVE_EVP_PKEY_EC ++static EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) { return pkey->pkey.ec; } ++#endif ++ ++static const BIGNUM *RSA_get0_n(const RSA *r) { return r->n; } ++static const BIGNUM *RSA_get0_e(const RSA *r) { return r->e; } ++static const BIGNUM *RSA_get0_d(const RSA *r) { return r->d; } ++ ++static void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) ++{ ++ if (n) *n = r->n; ++ if (e) *e = r->e; ++ if (d) *d = r->d; ++} ++ ++static void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) ++{ ++ if (p) *p = r->p; ++ if (q) *q = r->q; ++} ++ ++static void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp) ++{ ++ if (dmp1) *dmp1 = r->dmp1; ++ if (dmq1) *dmq1 = r->dmq1; ++ if (iqmp) *iqmp = r->iqmp; ++} ++ ++static int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) ++{ ++ if (n) { BN_free(r->n); r->n = n; } ++ if (e) { BN_free(r->e); r->e = e; } ++ if (d) { BN_free(r->d); r->d = d; } ++ return 1; ++} ++ ++static int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) ++{ ++ if (p) { BN_free(r->p); r->p = p; } ++ if (q) { BN_free(r->q); r->q = q; } ++ return 1; ++} ++ ++static int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) ++{ ++ if (dmp1) { BN_free(r->dmp1); r->dmp1 = dmp1; } ++ if (dmq1) { BN_free(r->dmq1); r->dmq1 = dmq1; } ++ if (iqmp) { BN_free(r->iqmp); r->iqmp = iqmp; } ++ return 1; ++} ++ ++static void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) ++{ ++ if (p) *p = d->p; ++ if (q) *q = d->q; ++ if (g) *g = d->g; ++} ++ ++static void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key) ++{ ++ if (pub_key) *pub_key = d->pub_key; ++ if (priv_key) *priv_key = d->priv_key; ++} ++ ++static int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) ++{ ++ if (p) { BN_free(d->p); d->p = p; } ++ if (q) { BN_free(d->q); d->q = q; } ++ if (g) { BN_free(d->g); d->g = g; } ++ return 1; ++} ++ ++static int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) ++{ ++ if (pub_key) { BN_free(d->pub_key); d->pub_key = pub_key; } ++ if (priv_key) { BN_free(d->priv_key); d->priv_key = priv_key; } ++ return 1; ++} ++ ++static void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) ++{ ++ if (p) *p = dh->p; ++ if (q) *q = dh->q; ++ if (g) *g = dh->g; ++} ++ ++static void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key) ++{ ++ if (pub_key) *pub_key = dh->pub_key; ++ if (priv_key) *priv_key = dh->priv_key; ++} ++ ++static int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) ++{ ++ if (p) { BN_free(dh->p); dh->p = p; } ++ if (q) { BN_free(dh->q); dh->q = q; } ++ if (g) { BN_free(dh->g); dh->g = g; } ++ return 1; ++} ++ ++static int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) ++{ ++ if (pub_key) { BN_free(dh->pub_key); dh->pub_key = pub_key; } ++ if (priv_key) { BN_free(dh->priv_key); dh->priv_key = priv_key; } ++ return 1; ++} ++ ++static ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ext) ++{ ++ return ext->value; ++} ++ ++static const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x) ++{ ++ return ASN1_STRING_data((ASN1_STRING *)x); ++} ++ ++static EVP_MD_CTX *EVP_MD_CTX_new(void) ++{ ++ EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX)); ++ if (ctx) memset(ctx, 0, sizeof(EVP_MD_CTX)); ++ return ctx; ++} ++ ++static void EVP_MD_CTX_free(EVP_MD_CTX *ctx) ++{ ++ if (ctx) { ++ EVP_MD_CTX_cleanup(ctx); ++ OPENSSL_free(ctx); ++ } ++} ++ ++static EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) ++{ ++ EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX)); ++ if (ctx) memset(ctx, 0, sizeof(EVP_CIPHER_CTX)); ++ return ctx; ++} ++ ++static void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) ++{ ++ if (ctx) { ++ EVP_CIPHER_CTX_cleanup(ctx); ++ OPENSSL_free(ctx); ++ } ++} ++ ++static void EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) ++{ ++ EVP_CIPHER_CTX_cleanup(ctx); ++} ++ ++#endif /* OPENSSL_VERSION_NUMBER >= 0x10100000L */ ++ + /* Common */ + #include + +@@ -316,7 +501,7 @@ + add_next_index_stringl(subentries, (char *)to_add, to_add_len, 1); + } + } else { +- to_add = ASN1_STRING_data(str); ++ to_add = (unsigned char *)ASN1_STRING_get0_data(str); + to_add_len = ASN1_STRING_length(str); + add_next_index_stringl(subentries, (char *)to_add, to_add_len, 1); + } +@@ -796,7 +981,9 @@ + */ + PHP_MSHUTDOWN_FUNCTION(openssl) + { ++#if OPENSSL_VERSION_NUMBER < 0x10100000L + EVP_cleanup(); ++#endif + + php_unregister_url_stream_wrapper("https" TSRMLS_CC); + php_unregister_url_stream_wrapper("ftps" TSRMLS_CC); +@@ -1041,8 +1228,13 @@ + } + array_init(return_value); + +- if (cert->name) { +- add_assoc_string(return_value, "name", cert->name, 1); ++ { ++ char *cert_name = X509_NAME_oneline( ++ X509_get_subject_name(cert), NULL, 0); ++ if (cert_name) { ++ add_assoc_string(return_value, "name", cert_name, 1); ++ OPENSSL_free(cert_name); ++ } + } + /* add_assoc_bool(return_value, "valid", cert->valid); */ + +@@ -2492,13 +2684,20 @@ + { + assert(pkey != NULL); + +- switch (pkey->type) { ++ switch (EVP_PKEY_id(pkey)) { + #ifndef NO_RSA + case EVP_PKEY_RSA: + case EVP_PKEY_RSA2: +- assert(pkey->pkey.rsa != NULL); +- if (pkey->pkey.rsa != NULL && (NULL == pkey->pkey.rsa->p || NULL == pkey->pkey.rsa->q)) { +- return 0; ++ { ++ RSA *rsa = EVP_PKEY_get0_RSA(pkey); ++ const BIGNUM *p, *q; ++ assert(rsa != NULL); ++ if (rsa != NULL) { ++ RSA_get0_factors(rsa, &p, &q); ++ if (NULL == p || NULL == q) { ++ return 0; ++ } ++ } + } + break; + #endif +@@ -2508,19 +2707,33 @@ + case EVP_PKEY_DSA2: + case EVP_PKEY_DSA3: + case EVP_PKEY_DSA4: +- assert(pkey->pkey.dsa != NULL); +- +- if (NULL == pkey->pkey.dsa->p || NULL == pkey->pkey.dsa->q || NULL == pkey->pkey.dsa->priv_key){ +- return 0; ++ { ++ DSA *dsa = EVP_PKEY_get0_DSA(pkey); ++ const BIGNUM *p, *q, *priv_key; ++ assert(dsa != NULL); ++ if (dsa != NULL) { ++ DSA_get0_pqg(dsa, &p, &q, NULL); ++ DSA_get0_key(dsa, NULL, &priv_key); ++ if (NULL == p || NULL == q || NULL == priv_key) { ++ return 0; ++ } ++ } + } + break; + #endif + #ifndef NO_DH + case EVP_PKEY_DH: +- assert(pkey->pkey.dh != NULL); +- +- if (NULL == pkey->pkey.dh->p || NULL == pkey->pkey.dh->priv_key) { +- return 0; ++ { ++ DH *dh = EVP_PKEY_get0_DH(pkey); ++ const BIGNUM *p, *priv_key; ++ assert(dh != NULL); ++ if (dh != NULL) { ++ DH_get0_pqg(dh, &p, NULL, NULL); ++ DH_get0_key(dh, NULL, &priv_key); ++ if (NULL == p || NULL == priv_key) { ++ return 0; ++ } ++ } + } + break; + #endif +@@ -2757,7 +2970,7 @@ + /*TODO: Use the real values once the openssl constants are used + * See the enum at the top of this file + */ +- switch (EVP_PKEY_type(pkey->type)) { ++ switch (EVP_PKEY_base_id(pkey)) { + case EVP_PKEY_RSA: + case EVP_PKEY_RSA2: + ktype = OPENSSL_KEYTYPE_RSA; +@@ -3269,13 +3482,13 @@ + cryptedlen = EVP_PKEY_size(pkey); + cryptedbuf = emalloc(cryptedlen + 1); + +- switch (pkey->type) { ++ switch (EVP_PKEY_id(pkey)) { + case EVP_PKEY_RSA: + case EVP_PKEY_RSA2: +- successful = (RSA_private_encrypt(data_len, +- (unsigned char *)data, +- cryptedbuf, +- pkey->pkey.rsa, ++ successful = (RSA_private_encrypt(data_len, ++ (unsigned char *)data, ++ cryptedbuf, ++ EVP_PKEY_get0_RSA(pkey), + padding) == cryptedlen); + break; + default: +@@ -3327,13 +3540,13 @@ + cryptedlen = EVP_PKEY_size(pkey); + crypttemp = emalloc(cryptedlen + 1); + +- switch (pkey->type) { ++ switch (EVP_PKEY_id(pkey)) { + case EVP_PKEY_RSA: + case EVP_PKEY_RSA2: +- cryptedlen = RSA_private_decrypt(data_len, +- (unsigned char *)data, +- crypttemp, +- pkey->pkey.rsa, ++ cryptedlen = RSA_private_decrypt(data_len, ++ (unsigned char *)data, ++ crypttemp, ++ EVP_PKEY_get0_RSA(pkey), + padding); + if (cryptedlen != -1) { + cryptedbuf = emalloc(cryptedlen + 1); +@@ -3392,13 +3605,13 @@ + cryptedlen = EVP_PKEY_size(pkey); + cryptedbuf = emalloc(cryptedlen + 1); + +- switch (pkey->type) { ++ switch (EVP_PKEY_id(pkey)) { + case EVP_PKEY_RSA: + case EVP_PKEY_RSA2: +- successful = (RSA_public_encrypt(data_len, +- (unsigned char *)data, +- cryptedbuf, +- pkey->pkey.rsa, ++ successful = (RSA_public_encrypt(data_len, ++ (unsigned char *)data, ++ cryptedbuf, ++ EVP_PKEY_get0_RSA(pkey), + padding) == cryptedlen); + break; + default: +@@ -3451,13 +3664,13 @@ + cryptedlen = EVP_PKEY_size(pkey); + crypttemp = emalloc(cryptedlen + 1); + +- switch (pkey->type) { ++ switch (EVP_PKEY_id(pkey)) { + case EVP_PKEY_RSA: + case EVP_PKEY_RSA2: +- cryptedlen = RSA_public_decrypt(data_len, +- (unsigned char *)data, +- crypttemp, +- pkey->pkey.rsa, ++ cryptedlen = RSA_public_decrypt(data_len, ++ (unsigned char *)data, ++ crypttemp, ++ EVP_PKEY_get0_RSA(pkey), + padding); + if (cryptedlen != -1) { + cryptedbuf = emalloc(cryptedlen + 1); +@@ -3521,7 +3734,7 @@ + long keyresource = -1; + char * data; + int data_len; +- EVP_MD_CTX md_ctx; ++ EVP_MD_CTX *md_ctx; + long signature_algo = OPENSSL_ALGO_SHA1; + EVP_MD *mdtype; + +@@ -3543,9 +3756,14 @@ + siglen = EVP_PKEY_size(pkey); + sigbuf = emalloc(siglen + 1); + +- EVP_SignInit(&md_ctx, mdtype); +- EVP_SignUpdate(&md_ctx, data, data_len); +- if (EVP_SignFinal (&md_ctx, sigbuf,(unsigned int *)&siglen, pkey)) { ++ md_ctx = EVP_MD_CTX_new(); ++ if (!md_ctx) { ++ efree(sigbuf); ++ RETURN_FALSE; ++ } ++ EVP_SignInit(md_ctx, mdtype); ++ EVP_SignUpdate(md_ctx, data, data_len); ++ if (EVP_SignFinal(md_ctx, sigbuf, (unsigned int *)&siglen, pkey)) { + zval_dtor(signature); + sigbuf[siglen] = '\0'; + ZVAL_STRINGL(signature, (char *)sigbuf, siglen, 0); +@@ -3554,9 +3772,7 @@ + efree(sigbuf); + RETVAL_FALSE; + } +-#if OPENSSL_VERSION_NUMBER >= 0x0090700fL +- EVP_MD_CTX_cleanup(&md_ctx); +-#endif ++ EVP_MD_CTX_free(md_ctx); + if (keyresource == -1) { + EVP_PKEY_free(pkey); + } +@@ -3570,7 +3786,7 @@ + zval **key; + EVP_PKEY *pkey; + int err; +- EVP_MD_CTX md_ctx; ++ EVP_MD_CTX *md_ctx; + EVP_MD *mdtype; + long keyresource = -1; + char * data; int data_len; +@@ -3593,12 +3809,14 @@ + RETURN_FALSE; + } + +- EVP_VerifyInit (&md_ctx, mdtype); +- EVP_VerifyUpdate (&md_ctx, data, data_len); +- err = EVP_VerifyFinal (&md_ctx, (unsigned char *)signature, signature_len, pkey); +-#if OPENSSL_VERSION_NUMBER >= 0x0090700fL +- EVP_MD_CTX_cleanup(&md_ctx); +-#endif ++ md_ctx = EVP_MD_CTX_new(); ++ if (!md_ctx) { ++ RETURN_FALSE; ++ } ++ EVP_VerifyInit (md_ctx, mdtype); ++ EVP_VerifyUpdate (md_ctx, data, data_len); ++ err = EVP_VerifyFinal (md_ctx, (unsigned char *)signature, signature_len, pkey); ++ EVP_MD_CTX_free(md_ctx); + + if (keyresource == -1) { + EVP_PKEY_free(pkey); +@@ -3619,7 +3837,7 @@ + int i, len1, len2, *eksl, nkeys; + unsigned char *buf = NULL, **eks; + char * data; int data_len; +- EVP_CIPHER_CTX ctx; ++ EVP_CIPHER_CTX *ctx = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szza/", &data, &data_len, &sealdata, &ekeys, &pubkeys) == FAILURE) { + return; +@@ -3655,26 +3873,31 @@ + i++; + } + +- if (!EVP_EncryptInit(&ctx,EVP_rc4(),NULL,NULL)) { ++ ctx = EVP_CIPHER_CTX_new(); ++ if (!ctx) { + RETVAL_FALSE; + goto clean_exit; + } ++ if (!EVP_EncryptInit(ctx,EVP_rc4(),NULL,NULL)) { ++ RETVAL_FALSE; ++ goto clean_exit; ++ } + + #if 0 + /* Need this if allow ciphers that require initialization vector */ +- ivlen = EVP_CIPHER_CTX_iv_length(&ctx); ++ ivlen = EVP_CIPHER_CTX_iv_length(ctx); + iv = ivlen ? emalloc(ivlen + 1) : NULL; + #endif + /* allocate one byte extra to make room for \0 */ +- buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(&ctx)); ++ buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(ctx)); + +- if (!EVP_SealInit(&ctx, EVP_rc4(), eks, eksl, NULL, pkeys, nkeys) || !EVP_SealUpdate(&ctx, buf, &len1, (unsigned char *)data, data_len)) { ++ if (!EVP_SealInit(ctx, EVP_rc4(), eks, eksl, NULL, pkeys, nkeys) || !EVP_SealUpdate(ctx, buf, &len1, (unsigned char *)data, data_len)) { + RETVAL_FALSE; + efree(buf); + goto clean_exit; + } + +- EVP_SealFinal(&ctx, buf + len1, &len2); ++ EVP_SealFinal(ctx, buf + len1, &len2); + + if (len1 + len2 > 0) { + zval_dtor(sealdata); +@@ -3705,11 +3928,14 @@ + RETVAL_LONG(len1 + len2); + + clean_exit: ++ if (ctx) { ++ EVP_CIPHER_CTX_free(ctx); ++ } + for (i=0; issl_handle) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "SSL/TLS already set-up for this stream"); + return -1; +@@ -332,14 +332,18 @@ + sslsock->is_client = 1; + method = SSLv23_client_method(); + break; ++#if OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(OPENSSL_NO_SSL2) + case STREAM_CRYPTO_METHOD_SSLv2_CLIENT: + sslsock->is_client = 1; + method = SSLv2_client_method(); + break; ++#endif ++#ifndef OPENSSL_NO_SSL3 + case STREAM_CRYPTO_METHOD_SSLv3_CLIENT: + sslsock->is_client = 1; + method = SSLv3_client_method(); + break; ++#endif + case STREAM_CRYPTO_METHOD_TLS_CLIENT: + sslsock->is_client = 1; + method = TLSv1_client_method(); +@@ -348,14 +352,18 @@ + sslsock->is_client = 0; + method = SSLv23_server_method(); + break; ++#ifndef OPENSSL_NO_SSL3 + case STREAM_CRYPTO_METHOD_SSLv3_SERVER: + sslsock->is_client = 0; + method = SSLv3_server_method(); + break; ++#endif ++#if OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(OPENSSL_NO_SSL2) + case STREAM_CRYPTO_METHOD_SSLv2_SERVER: + sslsock->is_client = 0; + method = SSLv2_server_method(); + break; ++#endif + case STREAM_CRYPTO_METHOD_TLS_SERVER: + sslsock->is_client = 0; + method = TLSv1_server_method(); diff --git a/packages/php-wasm/compile/php/php5.2.patch b/packages/php-wasm/compile/php/php5.2.patch new file mode 100644 index 00000000000..f1c4ec32572 --- /dev/null +++ b/packages/php-wasm/compile/php/php5.2.patch @@ -0,0 +1,60 @@ +--- a/php-src/ext/standard/file.c ++++ b/php-src/ext/standard/file.c +@@ -1776,6 +1776,20 @@ + goto safe_to_copy; + break; + case 0: ++ // Fix for https://github.com/WordPress/wordpress-playground/issues/54: ++ // Problem: Calling copy() on an empty source file crashes the JavaScript ++ // runtime. ++ // Solution: Avoid copying empty files. Just create an empty ++ // destination file and return. ++ if (src_s.sb.st_size == 0) { ++ char *opened_path = NULL; ++ php_stream *stream = php_stream_open_wrapper(dest, "w", REPORT_ERRORS, &opened_path); ++ if (stream) { ++ php_stream_close(stream); ++ return SUCCESS; ++ } ++ return FAILURE; ++ } + break; + default: /* failed to stat file, does not exist? */ + return ret; +--- a/php-src/ext/dom/documenttype.c ++++ b/php-src/ext/dom/documenttype.c +@@ -215,7 +215,7 @@ + if (buff != NULL) { + xmlNodeDumpOutput (buff, NULL, (xmlNodePtr) intsubset, 0, 0, NULL); + xmlOutputBufferFlush(buff); +- ZVAL_STRINGL(*retval, buff->buffer->content, buff->buffer->use, 1); ++ ZVAL_STRINGL(*retval, (char *) xmlOutputBufferGetContent(buff), xmlOutputBufferGetSize(buff), 1); + (void)xmlOutputBufferClose(buff); + return SUCCESS; + } +--- a/php-src/ext/dom/node.c ++++ b/php-src/ext/dom/node.c +@@ -1950,9 +1950,9 @@ + if (buf == NULL || ret < 0) { + RETVAL_FALSE; + } else { + if (mode == 0) { +- ret = buf->buffer->use; ++ ret = xmlOutputBufferGetSize(buf); + if (ret > 0) { +- RETVAL_STRINGL((char *) buf->buffer->content, ret, 1); ++ RETVAL_STRINGL((char *) xmlOutputBufferGetContent(buf), ret, 1); + } else { + RETVAL_EMPTY_STRING(); + } +--- a/php-src/ext/simplexml/simplexml.c ++++ b/php-src/ext/simplexml/simplexml.c +@@ -1343,7 +1343,7 @@ + + xmlNodeDumpOutput(outbuf, (xmlDocPtr) sxe->document->ptr, node, 0, 0, ((xmlDocPtr) sxe->document->ptr)->encoding); + xmlOutputBufferFlush(outbuf); +- RETVAL_STRINGL((char *)outbuf->buffer->content, outbuf->buffer->use, 1); ++ RETVAL_STRINGL((char *) xmlOutputBufferGetContent(outbuf), xmlOutputBufferGetSize(outbuf), 1); + xmlOutputBufferClose(outbuf); + } + } else { diff --git a/packages/php-wasm/compile/php/php_wasm.c b/packages/php-wasm/compile/php/php_wasm.c index 4dcdf90b33a..7721c95d4de 100644 --- a/packages/php-wasm/compile/php/php_wasm.c +++ b/packages/php-wasm/compile/php/php_wasm.c @@ -20,7 +20,9 @@ #include "zend_globals_macros.h" #include "zend_exceptions.h" +#if PHP_MAJOR_VERSION >= 6 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3) #include "zend_closures.h" +#endif #include "zend_hash.h" #include "rfc1867.h" #include "SAPI.h" @@ -609,15 +611,26 @@ static size_t handle_line(int type, zval *array, char *buf, size_t bufl) if (type == 1) { PHPWRITE(buf, bufl); +#if PHP_MAJOR_VERSION >= 6 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) if (php_output_get_level() < 1) { sapi_flush(); } +#else + if (OG(ob_nesting_level) < 1) + { + sapi_flush(); + } +#endif } else if (type == 2) { bufl = strip_trailing_whitespace(buf, bufl); +#if PHP_MAJOR_VERSION >= 7 add_next_index_stringl(array, buf, bufl); +#else + add_next_index_stringl(array, buf, bufl, 1); +#endif } return bufl; } @@ -720,7 +733,11 @@ EMSCRIPTEN_KEEPALIVE int wasm_php_exec(int type, const char *cmd, zval *array, z /* Return last line from the shell command */ bufl = strip_trailing_whitespace(buf, bufl); +#if PHP_MAJOR_VERSION >= 7 RETVAL_STRINGL(buf, bufl); +#else + RETVAL_STRINGL(buf, bufl, 1); +#endif } else { /* should return NULL, but for BC we return "" */ @@ -883,8 +900,13 @@ int wasm_sapi_module_startup(sapi_module_struct *sapi_module); int wasm_sapi_shutdown_wrapper(sapi_module_struct *sapi_globals); void wasm_sapi_module_shutdown(); static int wasm_sapi_deactivate(TSRMLS_D); +#if PHP_MAJOR_VERSION >= 7 static size_t wasm_sapi_ub_write(const char *str, size_t str_length TSRMLS_DC); static size_t wasm_sapi_read_post_body(char *buffer, size_t count_bytes); +#else +static int wasm_sapi_ub_write(const char *str, unsigned int str_length TSRMLS_DC); +static int wasm_sapi_read_post_body(char *buffer, unsigned int count_bytes); +#endif #if PHP_MAJOR_VERSION >= 8 static void wasm_sapi_log_message(const char *message TSRMLS_DC, int syslog_type_int); #else @@ -983,18 +1005,25 @@ int stdout_replacement; int stderr_replacement; #if WITH_CLI_SAPI == 1 +/* php_cli_process_title was introduced in PHP 5.5. On PHP 5.2 there is + * no such header and the Dockerfile already skips php_cli_process_title.c + * from the CLI source list. */ +#if PHP_MAJOR_VERSION >= 6 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 5) #include "sapi/cli/php_cli_process_title.h" #if PHP_MAJOR_VERSION >= 8 #include "sapi/cli/php_cli_process_title_arginfo.h" #endif +#endif extern int wasm_shutdown(int sockfd, int how); extern int wasm_close(int sockfd); static const zend_function_entry additional_functions[] = { ZEND_FE(dl, arginfo_dl) +#if PHP_MAJOR_VERSION >= 6 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 5) PHP_FE(cli_set_process_title, arginfo_cli_set_process_title) PHP_FE(cli_get_process_title, arginfo_cli_get_process_title) +#endif {NULL, NULL, NULL} }; @@ -1395,7 +1424,11 @@ static char *wasm_sapi_read_cookies(TSRMLS_D) * buffer: the buffer to read the request body into * count_bytes: the number of bytes to read */ +#if PHP_MAJOR_VERSION >= 7 static size_t wasm_sapi_read_post_body(char *buffer, size_t count_bytes) +#else +static int wasm_sapi_read_post_body(char *buffer, unsigned int count_bytes) +#endif { if (wasm_server_context == NULL || wasm_server_context->request_body == NULL) { @@ -1779,7 +1812,11 @@ static inline size_t wasm_sapi_single_write(const char *str, uint str_length) * str: the string to write. * str_length: the length of the string. */ +#if PHP_MAJOR_VERSION >= 7 static size_t wasm_sapi_ub_write(const char *str, size_t str_length TSRMLS_DC) +#else +static int wasm_sapi_ub_write(const char *str, unsigned int str_length TSRMLS_DC) +#endif { const char *ptr = str; uint remaining = str_length; @@ -1925,7 +1962,9 @@ int php_wasm_init() } php_sapi_started = 1; +#if PHP_MAJOR_VERSION >= 6 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3) php_wasm_sapi_module.additional_functions = additional_functions; +#endif if (php_wasm_sapi_module.startup(&php_wasm_sapi_module) == FAILURE) { return FAILURE; diff --git a/packages/php-wasm/compile/php/proc_open.c b/packages/php-wasm/compile/php/proc_open.c index 6cc1d49e718..170522feb4d 100644 --- a/packages/php-wasm/compile/php/proc_open.c +++ b/packages/php-wasm/compile/php/proc_open.c @@ -34,7 +34,11 @@ #include "php_globals.h" #include "SAPI.h" #include "main/php_network.h" +#if PHP_MAJOR_VERSION >= 7 #include "zend_smart_string.h" +#else +#include "ext/standard/php_smart_str.h" +#endif #if HAVE_SYS_WAIT_H #include @@ -52,6 +56,9 @@ static int le_proc_open; +#if PHP_MAJOR_VERSION >= 7 +/* ============ PHP 7+ implementation ============ */ + /* {{{ _php_array_to_envp */ static php_process_env_t _php_array_to_envp(zval *environment, int is_persistent) { @@ -713,3 +720,82 @@ PHP_FUNCTION(proc_open) RETURN_FALSE; } /* }}} */ + +#else /* PHP_MAJOR_VERSION < 7 */ +/* ============ PHP 5.x stub implementation ============ */ +/* proc_open and related functions are provided as stubs for PHP 5.x + * because process spawning doesn't truly work in WebAssembly anyway. + * The functions need to exist to satisfy the linker. */ + +static void proc_open_rsrc_dtor_legacy(zend_rsrc_list_entry *rsrc TSRMLS_DC); + +PHP_MINIT_FUNCTION(proc_open) +{ + le_proc_open = zend_register_list_destructors_ex( + proc_open_rsrc_dtor_legacy, NULL, "process", module_number); + return SUCCESS; +} + +/* This destructor is never actually invoked because PHP_FUNCTION(proc_open) + * always returns FALSE (process spawning is stubbed in WASM). It exists + * solely because zend_register_list_destructors_ex requires a callback. */ +static void proc_open_rsrc_dtor_legacy(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + struct php_process_handle *proc = (struct php_process_handle*)rsrc->ptr; + if (proc) { + /* Capture is_persistent before we free the containing struct — + * pefree(proc, proc->is_persistent) would read a freed field. */ + int is_persistent = proc->is_persistent; + if (proc->pipes) { + pefree(proc->pipes, is_persistent); + } + if (proc->command) { + pefree(proc->command, is_persistent); + } + if (proc->env.envarray) { + pefree(proc->env.envarray, is_persistent); + } + if (proc->env.envp) { + pefree(proc->env.envp, is_persistent); + } + pefree(proc, is_persistent); + } +} + +PHP_FUNCTION(proc_terminate) +{ + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "proc_terminate is not supported in this WASM build"); + RETURN_FALSE; +} + +PHP_FUNCTION(proc_close) +{ + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "proc_close is not supported in this WASM build"); + RETURN_LONG(-1); +} + +PHP_FUNCTION(proc_get_status) +{ + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "proc_get_status is not supported in this WASM build"); + array_init(return_value); + add_assoc_string(return_value, "command", "", 1); + add_assoc_long(return_value, "pid", 0); + add_assoc_bool(return_value, "running", 0); + add_assoc_bool(return_value, "signaled", 0); + add_assoc_bool(return_value, "stopped", 0); + add_assoc_long(return_value, "exitcode", -1); + add_assoc_long(return_value, "termsig", 0); + add_assoc_long(return_value, "stopsig", 0); +} + +PHP_FUNCTION(proc_open) +{ + php_error_docref(NULL TSRMLS_CC, E_WARNING, + "proc_open is not fully supported in this WASM build"); + RETURN_FALSE; +} + +#endif /* PHP_MAJOR_VERSION >= 7 */ diff --git a/packages/php-wasm/compile/php/proc_open.h b/packages/php-wasm/compile/php/proc_open.h index b1bfd1e7dc5..a708e121eca 100644 --- a/packages/php-wasm/compile/php/proc_open.h +++ b/packages/php-wasm/compile/php/proc_open.h @@ -16,6 +16,11 @@ +----------------------------------------------------------------------+ */ +#ifndef PHP_WASM_PROC_OPEN_H +#define PHP_WASM_PROC_OPEN_H + +#include "php.h" + typedef int php_file_descriptor_t; typedef pid_t php_process_id_t; @@ -31,7 +36,11 @@ typedef struct _php_process_env { struct php_process_handle { php_process_id_t child; int npipes; +#if PHP_MAJOR_VERSION >= 7 zend_resource **pipes; +#else + void **pipes; +#endif char *command; int is_persistent; php_process_env_t env; @@ -43,3 +52,5 @@ extern int js_open_process(const char *command, char **args, int args_length, in extern char *js_create_input_device(int procopen_call_nb); static int procopen_call_id = 0; + +#endif /* PHP_WASM_PROC_OPEN_H */ diff --git a/packages/php-wasm/node-builds/5-2/asyncify/5_2_17/php_5_2.wasm b/packages/php-wasm/node-builds/5-2/asyncify/5_2_17/php_5_2.wasm new file mode 100755 index 00000000000..b326c7f4aac Binary files /dev/null and b/packages/php-wasm/node-builds/5-2/asyncify/5_2_17/php_5_2.wasm differ diff --git a/packages/php-wasm/node-builds/5-2/asyncify/php_5_2.js b/packages/php-wasm/node-builds/5-2/asyncify/php_5_2.js new file mode 100644 index 00000000000..a789edc09d7 --- /dev/null +++ b/packages/php-wasm/node-builds/5-2/asyncify/php_5_2.js @@ -0,0 +1,10991 @@ +// Emscripten generates code for Node.js that uses the `require` function. +// We need to explicitly create a require function to avoid errors when running +// this code in Node.js as an ES module. +import { createRequire } from 'module'; +const require = createRequire(import.meta.url); +// Note: The path and url modules are currently needed by code injected by the php-wasm Dockerfile. +import path from 'path'; +import { fileURLToPath } from 'url'; + +// Determine the current directory path. In CJS mode, __dirname is available. +// In ESM mode, we derive it from import.meta.url. +const currentDirPath = + typeof __dirname !== 'undefined' + ? __dirname + : path.dirname(fileURLToPath(import.meta.url)); +const dependencyFilename = path.join(currentDirPath, '5_2_17', 'php_5_2.wasm'); +export { dependencyFilename }; +export const dependenciesTotalSize = 10268418; +const phpVersionString = '5.2.17'; +export function init(RuntimeName, PHPLoader) { + // The rest of the code comes from the built php.js file and esm-suffix.js + // include: shell.js + // include: minimum_runtime_check.js + // end include: minimum_runtime_check.js + // The Module object: Our interface to the outside world. We import + // and export values on it. There are various ways Module can be used: + // 1. Not defined. We create it here + // 2. A function parameter, function(moduleArg) => Promise + // 3. pre-run appended it, var Module = {}; ..generated code.. + // 4. External script tag defines var Module. + // We need to check if Module already exists (e.g. case 3 above). + // Substitution will be replaced with actual code on later stage of the build, + // this way Closure Compiler will not mangle it (e.g. case 4. above). + // Note that if you want to run closure, and also to use Module + // after the generated code, you will need to define var Module = {}; + // before the code. Then that object will be used in the code, and you + // can continue to use Module afterwards as well. + var Module = typeof PHPLoader != 'undefined' ? PHPLoader : {}; + + var ENVIRONMENT_IS_WORKER = RuntimeName === 'WORKER'; + + var ENVIRONMENT_IS_NODE = RuntimeName === 'NODE'; + + // --pre-jses are emitted after the Module integration code, so that they can + // refer to Module (if they choose; they can also define Module) + var arguments_ = []; + + var thisProgram = './this.program'; + + var quit_ = (status, toThrow) => { + throw toThrow; + }; + + var _scriptName; + + if (typeof __filename != 'undefined') { + // Node + _scriptName = __filename; + } else /*no-op*/ { + } + + // `/` should be present at the end if `scriptDirectory` is not empty + var scriptDirectory = ''; + + function locateFile(path) { + if (Module['locateFile']) { + return Module['locateFile'](path, scriptDirectory); + } + return scriptDirectory + path; + } + + // Hooks that are implemented differently in different runtime environments. + var readAsync, readBinary; + + if (ENVIRONMENT_IS_NODE) { + // These modules will usually be used on Node.js. Load them eagerly to avoid + // the complexity of lazy-loading. + var fs = require('fs'); + scriptDirectory = currentDirPath + '/'; + // include: node_shell_read.js + readBinary = (filename) => { + // We need to re-wrap `file://` strings to URLs. + filename = isFileURI(filename) ? new URL(filename) : filename; + var ret = fs.readFileSync(filename); + return ret; + }; + readAsync = async (filename, binary = true) => { + // See the comment in the `readBinary` function. + filename = isFileURI(filename) ? new URL(filename) : filename; + var ret = fs.readFileSync(filename, binary ? undefined : 'utf8'); + return ret; + }; + // end include: node_shell_read.js + if (process.argv.length > 1) { + thisProgram = process.argv[1].replace(/\\/g, '/'); + } + arguments_ = process.argv.slice(2); + // MODULARIZE will export the module in the proper place outside, we don't need to export here + if (typeof module != 'undefined') { + module['exports'] = Module; + } + quit_ = (status, toThrow) => { + process.exitCode = status; + throw toThrow; + }; + } else // Note that this includes Node.js workers when relevant (pthreads is enabled). + // Node.js workers are detected as a combination of ENVIRONMENT_IS_WORKER and + // ENVIRONMENT_IS_NODE. + { + } + + var out = console.log.bind(console); + + var err = console.error.bind(console); + + // end include: shell.js + // include: preamble.js + // === Preamble library stuff === + // Documentation for the public APIs defined in this file must be updated in: + // site/source/docs/api_reference/preamble.js.rst + // A prebuilt local version of the documentation is available at: + // site/build/text/docs/api_reference/preamble.js.txt + // You can also build docs locally as HTML or other formats in site/ + // An online HTML version (which may be of a different version of Emscripten) + // is up at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html + var dynamicLibraries = []; + + var wasmBinary; + + // Wasm globals + //======================================== + // Runtime essentials + //======================================== + // whether we are quitting the application. no code should run after this. + // set in exit() and abort() + var ABORT = false; + + // set by exit() and abort(). Passed to 'onExit' handler. + // NOTE: This is also used as the process return code code in shell environments + // but only when noExitRuntime is false. + var EXITSTATUS; + + /** + * Indicates whether filename is delivered via file protocol (as opposed to http/https) + * @noinline + */ var isFileURI = (filename) => filename.startsWith('file://'); + + // include: runtime_common.js + // include: runtime_stack_check.js + // end include: runtime_stack_check.js + // include: runtime_exceptions.js + // end include: runtime_exceptions.js + // include: runtime_debug.js + // end include: runtime_debug.js + // Memory management + var /** @type {!Int8Array} */ HEAP8, + /** @type {!Uint8Array} */ HEAPU8, + /** @type {!Int16Array} */ HEAP16, + /** @type {!Uint16Array} */ HEAPU16, + /** @type {!Int32Array} */ HEAP32, + /** @type {!Uint32Array} */ HEAPU32, + /** @type {!Float32Array} */ HEAPF32, + /** @type {!Float64Array} */ HEAPF64; + + // BigInt64Array type is not correctly defined in closure + var /** not-@type {!BigInt64Array} */ HEAP64, + /* BigUint64Array type is not correctly defined in closure +/** not-@type {!BigUint64Array} */ HEAPU64; + + var runtimeInitialized = false; + + var runtimeExited = false; + + function updateMemoryViews() { + var b = wasmMemory.buffer; + HEAP8 = new Int8Array(b); + HEAP16 = new Int16Array(b); + Module['HEAPU8'] = HEAPU8 = new Uint8Array(b); + HEAPU16 = new Uint16Array(b); + HEAP32 = new Int32Array(b); + Module['HEAPU32'] = HEAPU32 = new Uint32Array(b); + HEAPF32 = new Float32Array(b); + HEAPF64 = new Float64Array(b); + HEAP64 = new BigInt64Array(b); + HEAPU64 = new BigUint64Array(b); + } + + // include: memoryprofiler.js + // end include: memoryprofiler.js + // end include: runtime_common.js + var __RELOC_FUNCS__ = []; + + function preRun() { + if (Module['preRun']) { + if (typeof Module['preRun'] == 'function') + Module['preRun'] = [Module['preRun']]; + while (Module['preRun'].length) { + addOnPreRun(Module['preRun'].shift()); + } + } + // Begin ATPRERUNS hooks + callRuntimeCallbacks(onPreRuns); + } + + function initRuntime() { + runtimeInitialized = true; + callRuntimeCallbacks(__RELOC_FUNCS__); + // Begin ATINITS hooks + callRuntimeCallbacks(onInits); + if (!Module['noFSInit'] && !FS.initialized) FS.init(); + TTY.init(); + SOCKFS.root = FS.mount(SOCKFS, {}, null); + PIPEFS.root = FS.mount(PIPEFS, {}, null); + // End ATINITS hooks + wasmExports['__wasm_call_ctors'](); + // Begin ATPOSTCTORS hooks + callRuntimeCallbacks(onPostCtors); + FS.ignorePermissions = false; + } + + function preMain() {} + + function exitRuntime() { + // PThreads reuse the runtime from the main thread. + ___funcs_on_exit(); + // Native atexit() functions + // Begin ATEXITS hooks + FS.quit(); + TTY.shutdown(); + // End ATEXITS hooks + runtimeExited = true; + } + + function postRun() { + // PThreads reuse the runtime from the main thread. + if (Module['postRun']) { + if (typeof Module['postRun'] == 'function') + Module['postRun'] = [Module['postRun']]; + while (Module['postRun'].length) { + addOnPostRun(Module['postRun'].shift()); + } + } + // Begin ATPOSTRUNS hooks + callRuntimeCallbacks(onPostRuns); + } + + /** @param {string|number=} what */ function abort(what) { + Module['onAbort']?.(what); + what = 'Aborted(' + what + ')'; + // TODO(sbc): Should we remove printing and leave it up to whoever + // catches the exception? + err(what); + ABORT = true; + what += '. Build with -sASSERTIONS for more info.'; + // Use a wasm runtime error, because a JS error might be seen as a foreign + // exception, which means we'd run destructors on it. We need the error to + // simply make the program stop. + // FIXME This approach does not work in Wasm EH because it currently does not assume + // all RuntimeErrors are from traps; it decides whether a RuntimeError is from + // a trap or not based on a hidden field within the object. So at the moment + // we don't have a way of throwing a wasm trap from JS. TODO Make a JS API that + // allows this in the wasm spec. + // Suppress closure compiler warning here. Closure compiler's builtin extern + // definition for WebAssembly.RuntimeError claims it takes no arguments even + // though it can. + // TODO(https://github.com/google/closure-compiler/pull/3913): Remove if/when upstream closure gets fixed. + /** @suppress {checkTypes} */ var e = new WebAssembly.RuntimeError( + what + ); + // Throw the error whether or not MODULARIZE is set because abort is used + // in code paths apart from instantiation where an exception is expected + // to be thrown when abort is called. + throw e; + } + + var wasmBinaryFile; + + function findWasmBinary() { + return locateFile(dependencyFilename); + } + + function getBinarySync(file) { + if (file == wasmBinaryFile && wasmBinary) { + return new Uint8Array(wasmBinary); + } + if (readBinary) { + return readBinary(file); + } + // Throwing a plain string here, even though it not normally adviables since + // this gets turning into an `abort` in instantiateArrayBuffer. + throw 'both async and sync fetching of the wasm failed'; + } + + async function getWasmBinary(binaryFile) { + // If we don't have the binary yet, load it asynchronously using readAsync. + if (!wasmBinary) { + // Fetch the binary using readAsync + try { + var response = await readAsync(binaryFile); + return new Uint8Array(response); + } catch {} + } + // Otherwise, getBinarySync should be able to get it synchronously + return getBinarySync(binaryFile); + } + + async function instantiateArrayBuffer(binaryFile, imports) { + try { + var binary = await getWasmBinary(binaryFile); + var instance = await WebAssembly.instantiate(binary, imports); + return instance; + } catch (reason) { + err(`failed to asynchronously prepare wasm: ${reason}`); + abort(reason); + } + } + + async function instantiateAsync(binary, binaryFile, imports) { + if (!binary && !ENVIRONMENT_IS_NODE) { + try { + var response = fetch(binaryFile, { + credentials: 'same-origin', + }); + var instantiationResult = + await WebAssembly.instantiateStreaming(response, imports); + return instantiationResult; + } catch (reason) { + // We expect the most common failure cause to be a bad MIME type for the binary, + // in which case falling back to ArrayBuffer instantiation should work. + err(`wasm streaming compile failed: ${reason}`); + err('falling back to ArrayBuffer instantiation'); + } + } + return instantiateArrayBuffer(binaryFile, imports); + } + + function getWasmImports() { + // prepare imports + var imports = { + env: wasmImports, + wasi_snapshot_preview1: wasmImports, + 'GOT.mem': new Proxy(wasmImports, GOTHandler), + 'GOT.func': new Proxy(wasmImports, GOTHandler), + }; + return imports; + } + + // Create the wasm instance. + // Receives the wasm imports, returns the exports. + async function createWasm() { + // Load the wasm module and create an instance of using native support in the JS engine. + // handle a generated wasm instance, receiving its exports and + // performing other necessary setup + /** @param {WebAssembly.Module=} module*/ function receiveInstance( + instance, + module + ) { + wasmExports = instance.exports; + // No relocation needed here.. but calling this just so that updateGOT is + // called. + var origExports = (wasmExports = relocateExports(wasmExports)); + wasmExports = Asyncify.instrumentWasmExports(wasmExports); + mergeLibSymbols(wasmExports, 'main'); + var metadata = getDylinkMetadata(module); + if (metadata.neededDynlibs) { + dynamicLibraries = + metadata.neededDynlibs.concat(dynamicLibraries); + } + assignWasmExports(wasmExports); + updateGOT(origExports); + Module['wasmExports'] = wasmExports; + LDSO.init(); + loadDylibs(); + updateMemoryViews(); + removeRunDependency('wasm-instantiate'); + return wasmExports; + } + addRunDependency('wasm-instantiate'); + // Prefer streaming instantiation if available. + function receiveInstantiationResult(result) { + // 'result' is a ResultObject object which has both the module and instance. + // receiveInstance() will swap in the exports (to Module.asm) so they can be called + return receiveInstance(result['instance'], result['module']); + } + var info = getWasmImports(); + // User shell pages can write their own Module.instantiateWasm = function(imports, successCallback) callback + // to manually instantiate the Wasm module themselves. This allows pages to + // run the instantiation parallel to any other async startup actions they are + // performing. + // Also pthreads and wasm workers initialize the wasm instance through this + // path. + if (Module['instantiateWasm']) { + return new Promise((resolve, reject) => { + Module['instantiateWasm'](info, (inst, mod) => { + resolve(receiveInstance(inst, mod)); + }); + }); + } + wasmBinaryFile ??= findWasmBinary(); + var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info); + var exports = receiveInstantiationResult(result); + return exports; + } + + // With MAIN_MODULE + ASYNCIFY the normal method of placing stub functions in + // wasmImports for as-yet-undefined symbols doesn't work since ASYNCIFY then + // wraps these stub functions and we can't then replace them directly. Instead + // the stub functions call into `asyncifyStubs` which gets populated by the + // dynamic linker as symbols are loaded. + var asyncifyStubs = {}; + + // end include: preamble.js + // Begin JS library code + class ExitStatus { + name = 'ExitStatus'; + constructor(status) { + this.message = `Program terminated with exit(${status})`; + this.status = status; + } + } + ExitStatus = class PHPExitStatus extends Error { + constructor(status) { + super(status); + this.name = 'ExitStatus'; + this.message = 'Program terminated with exit(' + status + ')'; + this.status = status; + } + }; + + var GOT = {}; + + var currentModuleWeakSymbols = new Set([]); + + var GOTHandler = { + get(obj, symName) { + var rtn = GOT[symName]; + if (!rtn) { + rtn = GOT[symName] = new WebAssembly.Global( + { + value: 'i32', + mutable: true, + }, + -1 + ); + } + if (!currentModuleWeakSymbols.has(symName)) { + // Any non-weak reference to a symbol marks it as `required`, which + // enabled `reportUndefinedSymbols` to report undefined symbol errors + // correctly. + rtn.required = true; + } + return rtn; + }, + }; + + var callRuntimeCallbacks = (callbacks) => { + while (callbacks.length > 0) { + // Pass the module as the first argument. + callbacks.shift()(Module); + } + }; + + var onPostRuns = []; + + var addOnPostRun = (cb) => onPostRuns.push(cb); + + var onPreRuns = []; + + var addOnPreRun = (cb) => onPreRuns.push(cb); + + var runDependencies = 0; + + var dependenciesFulfilled = null; + + var removeRunDependency = (id) => { + runDependencies--; + Module['monitorRunDependencies']?.(runDependencies); + if (runDependencies == 0) { + if (dependenciesFulfilled) { + var callback = dependenciesFulfilled; + dependenciesFulfilled = null; + callback(); + } + } + }; + + var addRunDependency = (id) => { + runDependencies++; + Module['monitorRunDependencies']?.(runDependencies); + }; + + var dynCalls = {}; + + var dynCallLegacy = (sig, ptr, args) => { + sig = sig.replace(/p/g, 'i'); + var f = dynCalls[sig]; + return f(ptr, ...args); + }; + + var dynCall = (sig, ptr, args = [], promising = false) => { + var rtn = dynCallLegacy(sig, ptr, args); + function convert(rtn) { + return rtn; + } + return convert(rtn); + }; + + var UTF8Decoder = globalThis.TextDecoder && new TextDecoder(); + + var findStringEnd = (heapOrArray, idx, maxBytesToRead, ignoreNul) => { + var maxIdx = idx + maxBytesToRead; + if (ignoreNul) return maxIdx; + // TextDecoder needs to know the byte length in advance, it doesn't stop on + // null terminator by itself. + // As a tiny code save trick, compare idx against maxIdx using a negation, + // so that maxBytesToRead=undefined/NaN means Infinity. + while (heapOrArray[idx] && !(idx >= maxIdx)) ++idx; + return idx; + }; + + /** + * Given a pointer 'idx' to a null-terminated UTF8-encoded string in the given + * array that contains uint8 values, returns a copy of that string as a + * Javascript String object. + * heapOrArray is either a regular array, or a JavaScript typed array view. + * @param {number=} idx + * @param {number=} maxBytesToRead + * @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character. + * @return {string} + */ var UTF8ArrayToString = ( + heapOrArray, + idx = 0, + maxBytesToRead, + ignoreNul + ) => { + var endPtr = findStringEnd(heapOrArray, idx, maxBytesToRead, ignoreNul); + // When using conditional TextDecoder, skip it for short strings as the overhead of the native call is not worth it. + if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) { + return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr)); + } + var str = ''; + while (idx < endPtr) { + // For UTF8 byte structure, see: + // http://en.wikipedia.org/wiki/UTF-8#Description + // https://www.ietf.org/rfc/rfc2279.txt + // https://tools.ietf.org/html/rfc3629 + var u0 = heapOrArray[idx++]; + if (!(u0 & 128)) { + str += String.fromCharCode(u0); + continue; + } + var u1 = heapOrArray[idx++] & 63; + if ((u0 & 224) == 192) { + str += String.fromCharCode(((u0 & 31) << 6) | u1); + continue; + } + var u2 = heapOrArray[idx++] & 63; + if ((u0 & 240) == 224) { + u0 = ((u0 & 15) << 12) | (u1 << 6) | u2; + } else { + u0 = + ((u0 & 7) << 18) | + (u1 << 12) | + (u2 << 6) | + (heapOrArray[idx++] & 63); + } + if (u0 < 65536) { + str += String.fromCharCode(u0); + } else { + var ch = u0 - 65536; + str += String.fromCharCode( + 55296 | (ch >> 10), + 56320 | (ch & 1023) + ); + } + } + return str; + }; + + var getDylinkMetadata = (binary) => { + var offset = 0; + var end = 0; + function getU8() { + return binary[offset++]; + } + function getLEB() { + var ret = 0; + var mul = 1; + while (1) { + var byte = binary[offset++]; + ret += (byte & 127) * mul; + mul *= 128; + if (!(byte & 128)) break; + } + return ret; + } + function getString() { + var len = getLEB(); + offset += len; + return UTF8ArrayToString(binary, offset - len, len); + } + function getStringList() { + var count = getLEB(); + var rtn = []; + while (count--) rtn.push(getString()); + return rtn; + } + /** @param {string=} message */ function failIf(condition, message) { + if (condition) throw new Error(message); + } + if (binary instanceof WebAssembly.Module) { + var dylinkSection = WebAssembly.Module.customSections( + binary, + 'dylink.0' + ); + failIf(dylinkSection.length === 0, 'need dylink section'); + binary = new Uint8Array(dylinkSection[0]); + end = binary.length; + } else { + var int32View = new Uint32Array( + new Uint8Array(binary.subarray(0, 24)).buffer + ); + var magicNumberFound = int32View[0] == 1836278016; + failIf(!magicNumberFound, 'need to see wasm magic number'); + // \0asm + // we should see the dylink custom section right after the magic number and wasm version + failIf(binary[8] !== 0, 'need the dylink section to be first'); + offset = 9; + var section_size = getLEB(); + //section size + end = offset + section_size; + var name = getString(); + failIf(name !== 'dylink.0'); + } + var customSection = { + neededDynlibs: [], + tlsExports: new Set(), + weakImports: new Set(), + runtimePaths: [], + }; + var WASM_DYLINK_MEM_INFO = 1; + var WASM_DYLINK_NEEDED = 2; + var WASM_DYLINK_EXPORT_INFO = 3; + var WASM_DYLINK_IMPORT_INFO = 4; + var WASM_DYLINK_RUNTIME_PATH = 5; + var WASM_SYMBOL_TLS = 256; + var WASM_SYMBOL_BINDING_MASK = 3; + var WASM_SYMBOL_BINDING_WEAK = 1; + while (offset < end) { + var subsectionType = getU8(); + var subsectionSize = getLEB(); + if (subsectionType === WASM_DYLINK_MEM_INFO) { + customSection.memorySize = getLEB(); + customSection.memoryAlign = getLEB(); + customSection.tableSize = getLEB(); + customSection.tableAlign = getLEB(); + } else if (subsectionType === WASM_DYLINK_NEEDED) { + customSection.neededDynlibs = getStringList(); + } else if (subsectionType === WASM_DYLINK_EXPORT_INFO) { + var count = getLEB(); + while (count--) { + var symname = getString(); + var flags = getLEB(); + if (flags & WASM_SYMBOL_TLS) { + customSection.tlsExports.add(symname); + } + } + } else if (subsectionType === WASM_DYLINK_IMPORT_INFO) { + var count = getLEB(); + while (count--) { + var modname = getString(); + var symname = getString(); + var flags = getLEB(); + if ( + (flags & WASM_SYMBOL_BINDING_MASK) == + WASM_SYMBOL_BINDING_WEAK + ) { + customSection.weakImports.add(symname); + } + } + } else if (subsectionType === WASM_DYLINK_RUNTIME_PATH) { + customSection.runtimePaths = getStringList(); + } else { + // unknown subsection + offset += subsectionSize; + } + } + return customSection; + }; + + var newDSO = (name, handle, syms) => { + var dso = { + refcount: Infinity, + name, + exports: syms, + global: true, + }; + LDSO.loadedLibsByName[name] = dso; + if (handle != undefined) { + LDSO.loadedLibsByHandle[handle] = dso; + } + return dso; + }; + + var LDSO = { + loadedLibsByName: {}, + loadedLibsByHandle: {}, + init() { + newDSO('__main__', 0, wasmImports); + }, + }; + + var alignMemory = (size, alignment) => + Math.ceil(size / alignment) * alignment; + + var getMemory = (size) => { + // After the runtime is initialized, we must only use sbrk() normally. + if (runtimeInitialized) { + // Currently we don't support freeing of static data when modules are + // unloaded via dlclose. This function is tagged as `noleakcheck` to + // avoid having this reported as leak. + return _calloc(size, 1); + } + var ret = ___heap_base; + // Keep __heap_base stack aligned. + var end = ret + alignMemory(size, 16); + ___heap_base = end; + // After allocating the memory from the start of the heap we need to ensure + // that once the program starts it doesn't use this region. In relocatable + // mode we can just update the __heap_base symbol that we are exporting to + // the main module. + // When not relocatable `__heap_base` is fixed and exported by the main + // module, but we can update the `sbrk_ptr` value instead. We call + // `_emscripten_get_sbrk_ptr` knowing that it is safe to call prior to + // runtime initialization (unlike, the higher level sbrk function) + var sbrk_ptr = _emscripten_get_sbrk_ptr(); + HEAPU32[sbrk_ptr >> 2] = end; + return ret; + }; + + var isInternalSym = (symName) => + [ + 'memory', + '__memory_base', + '__table_base', + '__stack_pointer', + '__indirect_function_table', + '__cpp_exception', + '__c_longjmp', + '__wasm_apply_data_relocs', + '__dso_handle', + '__tls_size', + '__tls_align', + '__set_stack_limits', + '_emscripten_tls_init', + '__wasm_init_tls', + '__wasm_call_ctors', + '__start_em_asm', + '__stop_em_asm', + '__start_em_js', + '__stop_em_js', + ].includes(symName) || symName.startsWith('__em_js__'); + + var wasmTableMirror = []; + + var getWasmTableEntry = (funcPtr) => { + var func = wasmTableMirror[funcPtr]; + if (!func) { + /** @suppress {checkTypes} */ wasmTableMirror[funcPtr] = func = + wasmTable.get(funcPtr); + } + return func; + }; + + var updateTableMap = (offset, count) => { + if (functionsInTableMap) { + for (var i = offset; i < offset + count; i++) { + var item = getWasmTableEntry(i); + // Ignore null values. + if (item) { + functionsInTableMap.set(item, i); + } + } + } + }; + + var functionsInTableMap; + + var getFunctionAddress = (func) => { + // First, create the map if this is the first use. + if (!functionsInTableMap) { + functionsInTableMap = new WeakMap(); + updateTableMap(0, wasmTable.length); + } + return functionsInTableMap.get(func) || 0; + }; + + var freeTableIndexes = []; + + var getEmptyTableSlot = () => { + // Reuse a free index if there is one, otherwise grow. + if (freeTableIndexes.length) { + return freeTableIndexes.pop(); + } + // Grow the table + return wasmTable['grow'](1); + }; + + var setWasmTableEntry = (idx, func) => { + /** @suppress {checkTypes} */ wasmTable.set(idx, func); + // With ABORT_ON_WASM_EXCEPTIONS wasmTable.get is overridden to return wrapped + // functions so we need to call it here to retrieve the potential wrapper correctly + // instead of just storing 'func' directly into wasmTableMirror + /** @suppress {checkTypes} */ wasmTableMirror[idx] = wasmTable.get(idx); + }; + + var uleb128EncodeWithLen = (arr) => { + const n = arr.length; + // Note: this LEB128 length encoding produces extra byte for n < 128, + // but we don't care as it's only used in a temporary representation. + return [(n % 128) | 128, n >> 7, ...arr]; + }; + + var wasmTypeCodes = { + i: 127, + // i32 + p: 127, + // i32 + j: 126, + // i64 + f: 125, + // f32 + d: 124, + // f64 + e: 111, + }; + + var generateTypePack = (types) => + uleb128EncodeWithLen( + Array.from(types, (type) => { + var code = wasmTypeCodes[type]; + return code; + }) + ); + + var convertJsFunctionToWasm = (func, sig) => { + // Rest of the module is static + var bytes = Uint8Array.of( + 0, + 97, + 115, + 109, // magic ("\0asm") + 1, + 0, + 0, + 0, // version: 1 + 1, // Type section code + // The module is static, with the exception of the type section, which is + // generated based on the signature passed in. + ...uleb128EncodeWithLen([ + 1, // count: 1 + 96, // param types + ...generateTypePack(sig.slice(1)), // return types (for now only supporting [] if `void` and single [T] otherwise) + ...generateTypePack(sig[0] === 'v' ? '' : sig[0]), + ]), // The rest of the module is static + 2, + 7, // import section + // (import "e" "f" (func 0 (type 0))) + 1, + 1, + 101, + 1, + 102, + 0, + 0, + 7, + 5, // export section + // (export "f" (func 0 (type 0))) + 1, + 1, + 102, + 0, + 0 + ); + // We can compile this wasm module synchronously because it is very small. + // This accepts an import (at "e.f"), that it reroutes to an export (at "f") + var module = new WebAssembly.Module(bytes); + var instance = new WebAssembly.Instance(module, { + e: { + f: func, + }, + }); + var wrappedFunc = instance.exports['f']; + return wrappedFunc; + }; + + /** @param {string=} sig */ var addFunction = (func, sig) => { + // Check if the function is already in the table, to ensure each function + // gets a unique index. + var rtn = getFunctionAddress(func); + if (rtn) { + return rtn; + } + // It's not in the table, add it now. + var ret = getEmptyTableSlot(); + // Set the new value. + try { + // Attempting to call this with JS function will cause of table.set() to fail + setWasmTableEntry(ret, func); + } catch (err) { + if (!(err instanceof TypeError)) { + throw err; + } + var wrapped = convertJsFunctionToWasm(func, sig); + setWasmTableEntry(ret, wrapped); + } + functionsInTableMap.set(func, ret); + return ret; + }; + + /** @param {boolean=} replace */ var updateGOT = (exports, replace) => { + for (var symName in exports) { + if (isInternalSym(symName)) { + continue; + } + var value = exports[symName]; + var existingEntry = GOT[symName] && GOT[symName].value != -1; + if (replace || !existingEntry) { + var newValue; + if (typeof value == 'function') { + newValue = addFunction(value); + } else if (typeof value == 'number') { + newValue = value; + } else { + // The GOT can only contain addresses (i.e data addresses or function + // addresses so we currently ignore other types export here. + continue; + } + GOT[symName] ??= new WebAssembly.Global({ + value: 'i32', + mutable: true, + }); + GOT[symName].value = newValue; + } + } + }; + + var isImmutableGlobal = (val) => { + if (val instanceof WebAssembly.Global) { + try { + val.value = val.value; + } catch { + return true; + } + } + return false; + }; + + var relocateExports = (exports, memoryBase = 0) => { + function relocateExport(name, value) { + // Detect immuable wasm global exports. These represent data addresses + // which are relative to `memoryBase` + if (isImmutableGlobal(value)) { + return value.value + memoryBase; + } + // Return unmodified value (no relocation required). + return value; + } + var relocated = {}; + for (var e in exports) { + relocated[e] = relocateExport(e, exports[e]); + } + return relocated; + }; + + var isSymbolDefined = (symName) => { + // Ignore 'stub' symbols that are auto-generated as part of the original + // `wasmImports` used to instantiate the main module. + var existing = wasmImports[symName]; + if (!existing || existing.stub) { + return false; + } + // Even if a symbol exists in wasmImports, and is not itself a stub, it + // could be an ASYNCIFY wrapper function that wraps a stub function. + if (symName in asyncifyStubs && !asyncifyStubs[symName]) { + return false; + } + return true; + }; + + var createNamedFunction = (name, func) => + Object.defineProperty(func, 'name', { + value: name, + }); + + var stackSave = () => _emscripten_stack_get_current(); + + var stackRestore = (val) => __emscripten_stack_restore(val); + + var createInvokeFunction = + (sig) => + (ptr, ...args) => { + var sp = stackSave(); + try { + return dynCall(sig, ptr, args); + } catch (e) { + stackRestore(sp); + // Create a try-catch guard that rethrows the Emscripten EH exception. + // Exceptions thrown from C++ will be a pointer (number) and longjmp + // will throw the number Infinity. Use the compact and fast "e !== e+0" + // test to check if e was not a Number. + if (e !== e + 0) throw e; + _setThrew(1, 0); + // In theory this if statement could be done on + // creating the function, but I just added this to + // save wasting code space as it only happens on exception. + if (sig[0] == 'j') return 0n; + } + }; + + var resolveGlobalSymbol = (symName, direct = false) => { + var sym; + if (isSymbolDefined(symName)) { + sym = wasmImports[symName]; + } else if (symName.startsWith('invoke_')) { + // Create (and cache) new invoke_ functions on demand. + sym = wasmImports[symName] = createNamedFunction( + symName, + createInvokeFunction(symName.split('_')[1]) + ); + } + return { + sym, + name: symName, + }; + }; + + var onPostCtors = []; + + var addOnPostCtor = (cb) => onPostCtors.push(cb); + + /** + * Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the + * emscripten HEAP, returns a copy of that string as a Javascript String object. + * + * @param {number} ptr + * @param {number=} maxBytesToRead - An optional length that specifies the + * maximum number of bytes to read. You can omit this parameter to scan the + * string until the first 0 byte. If maxBytesToRead is passed, and the string + * at [ptr, ptr+maxBytesToReadr[ contains a null byte in the middle, then the + * string will cut short at that byte index. + * @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character. + * @return {string} + */ var UTF8ToString = (ptr, maxBytesToRead, ignoreNul) => + ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead, ignoreNul) : ''; + + /** + * @param {string=} libName + * @param {Object=} localScope + * @param {number=} handle + */ var loadWebAssemblyModule = ( + binary, + flags, + libName, + localScope, + handle + ) => { + var metadata = getDylinkMetadata(binary); + // loadModule loads the wasm module after all its dependencies have been loaded. + // can be called both sync/async. + function loadModule() { + // alignments are powers of 2 + var memAlign = Math.pow(2, metadata.memoryAlign); + // prepare memory + var memoryBase = metadata.memorySize + ? alignMemory( + getMemory(metadata.memorySize + memAlign), + memAlign + ) + : 0; + // TODO: add to cleanups + var tableBase = metadata.tableSize ? wasmTable.length : 0; + if (handle) { + HEAP8[handle + 8] = 1; + HEAPU32[(handle + 12) >> 2] = memoryBase; + HEAP32[(handle + 16) >> 2] = metadata.memorySize; + HEAPU32[(handle + 20) >> 2] = tableBase; + HEAP32[(handle + 24) >> 2] = metadata.tableSize; + } + if (metadata.tableSize) { + wasmTable.grow(metadata.tableSize); + } + // This is the export map that we ultimately return. We declare it here + // so it can be used within resolveSymbol. We resolve symbols against + // this local symbol map in the case there they are not present on the + // global Module object. We need this fallback because Modules sometime + // need to import their own symbols + var moduleExports; + function resolveSymbol(sym) { + var resolved = resolveGlobalSymbol(sym).sym; + if (!resolved && localScope) { + resolved = localScope[sym]; + } + if (!resolved) { + resolved = moduleExports[sym]; + } + return resolved; + } + // TODO kill ↓↓↓ (except "symbols local to this module", it will likely be + // not needed if we require that if A wants symbols from B it has to link + // to B explicitly: similarly to -Wl,--no-undefined) + // wasm dynamic libraries are pure wasm, so they cannot assist in + // their own loading. When side module A wants to import something + // provided by a side module B that is loaded later, we need to + // add a layer of indirection, but worse, we can't even tell what + // to add the indirection for, without inspecting what A's imports + // are. To do that here, we use a JS proxy (another option would + // be to inspect the binary directly). + var proxyHandler = { + get(stubs, prop) { + // symbols that should be local to this module + switch (prop) { + case '__memory_base': + return memoryBase; + + case '__table_base': + return tableBase; + } + if (prop in wasmImports && !wasmImports[prop].stub) { + // No stub needed, symbol already exists in symbol table + var res = wasmImports[prop]; + // Asyncify wraps exports, and we need to look through those wrappers. + if (res.orig) { + res = res.orig; + } + return res; + } + // Return a stub function that will resolve the symbol + // when first called. + if (!(prop in stubs)) { + var resolved; + stubs[prop] = (...args) => { + resolved ||= resolveSymbol(prop); + return resolved(...args); + }; + } + return stubs[prop]; + }, + }; + var proxy = new Proxy({}, proxyHandler); + currentModuleWeakSymbols = metadata.weakImports; + var info = { + 'GOT.mem': new Proxy({}, GOTHandler), + 'GOT.func': new Proxy({}, GOTHandler), + env: proxy, + wasi_snapshot_preview1: proxy, + }; + function postInstantiation(module, instance) { + // add new entries to functionsInTableMap + updateTableMap(tableBase, metadata.tableSize); + moduleExports = relocateExports(instance.exports, memoryBase); + updateGOT(moduleExports); + moduleExports = Asyncify.instrumentWasmExports(moduleExports); + if (!flags.allowUndefined) { + reportUndefinedSymbols(); + } + function addEmAsm(addr, body) { + var args = []; + for (var arity = 0; ; arity++) { + var argName = '$' + arity; + if (!body.includes(argName)) break; + args.push(argName); + } + args = args.join(','); + var func = `(${args}) => { ${body} };`; + ASM_CONSTS[start] = eval(func); + } + // Add any EM_ASM function that exist in the side module + if ('__start_em_asm' in moduleExports) { + var start = moduleExports['__start_em_asm']; + var stop = moduleExports['__stop_em_asm']; + while (start < stop) { + var jsString = UTF8ToString(start); + addEmAsm(start, jsString); + start = HEAPU8.indexOf(0, start) + 1; + } + } + function addEmJs(name, cSig, body) { + // The signature here is a C signature (e.g. "(int foo, char* bar)"). + // See `create_em_js` in emcc.py` for the build-time version of this + // code. + var jsArgs = []; + cSig = cSig.slice(1, -1); + if (cSig != 'void') { + cSig = cSig.split(','); + for (var arg of cSig) { + var jsArg = arg.split(' ').pop(); + jsArgs.push(jsArg.replace('*', '')); + } + } + var func = `(${jsArgs}) => ${body};`; + moduleExports[name] = eval(func); + } + for (var name in moduleExports) { + if (name.startsWith('__em_js__')) { + var start = moduleExports[name]; + var jsString = UTF8ToString(start); + // EM_JS strings are stored in the data section in the form + // SIG<::>BODY. + var [sig, body] = jsString.split('<::>'); + addEmJs(name.replace('__em_js__', ''), sig, body); + delete moduleExports[name]; + } + } + // initialize the module + var applyRelocs = moduleExports['__wasm_apply_data_relocs']; + if (applyRelocs) { + if (runtimeInitialized) { + applyRelocs(); + } else { + __RELOC_FUNCS__.push(applyRelocs); + } + } + var init = moduleExports['__wasm_call_ctors']; + if (init) { + if (runtimeInitialized) { + init(); + } else { + // we aren't ready to run compiled code yet + addOnPostCtor(init); + } + } + return moduleExports; + } + if (flags.loadAsync) { + return (async () => { + var instance; + if (binary instanceof WebAssembly.Module) { + instance = new WebAssembly.Instance(binary, info); + } else { + // Destructuring assignment without declaration has to be wrapped + // with parens or parser will treat the l-value as an object + // literal instead. + ({ module: binary, instance } = + await WebAssembly.instantiate(binary, info)); + } + return postInstantiation(binary, instance); + })(); + } + var module = + binary instanceof WebAssembly.Module + ? binary + : new WebAssembly.Module(binary); + var instance = new WebAssembly.Instance(module, info); + return postInstantiation(module, instance); + } + // We need to set rpath in flags based on the current library's rpath. + // We can't mutate flags or else if a depends on b and c and b depends on d, + // then c will be loaded with b's rpath instead of a's. + flags = { + ...flags, + rpath: { + parentLibPath: libName, + paths: metadata.runtimePaths, + }, + }; + // now load needed libraries and the module itself. + if (flags.loadAsync) { + return metadata.neededDynlibs + .reduce( + (chain, dynNeeded) => + chain.then(() => + loadDynamicLibrary(dynNeeded, flags, localScope) + ), + Promise.resolve() + ) + .then(loadModule); + } + for (var needed of metadata.neededDynlibs) { + loadDynamicLibrary(needed, flags, localScope); + } + return loadModule(); + }; + + var mergeLibSymbols = (exports, libName) => { + registerDynCallSymbols(exports); + // add symbols into global namespace TODO: weak linking etc. + for (var [sym, exp] of Object.entries(exports)) { + // When RTLD_GLOBAL is enabled, the symbols defined by this shared object + // will be made available for symbol resolution of subsequently loaded + // shared objects. + // We should copy the symbols (which include methods and variables) from + // SIDE_MODULE to MAIN_MODULE. + const setImport = (target) => { + if (target in asyncifyStubs) { + asyncifyStubs[target] = exp; + } + if (!isSymbolDefined(target)) { + wasmImports[target] = exp; + } + }; + setImport(sym); + // Special case for handling of main symbol: If a side module exports + // `main` that also acts a definition for `__main_argc_argv` and vice + // versa. + const main_alias = '__main_argc_argv'; + if (sym == 'main') { + setImport(main_alias); + } + if (sym == main_alias) { + setImport('main'); + } + } + }; + + var asyncLoad = async (url) => { + var arrayBuffer = await readAsync(url); + return new Uint8Array(arrayBuffer); + }; + + var preloadPlugins = []; + + var registerWasmPlugin = () => { + // Use string keys here for public methods to avoid minification since the + // plugin consumer also uses string keys. + var wasmPlugin = { + promiseChainEnd: Promise.resolve(), + canHandle: (name) => + !Module['noWasmDecoding'] && name.endsWith('.so'), + handle: async ( + byteArray, + name // loadWebAssemblyModule can not load modules out-of-order, so rather + ) => + // than just running the promises in parallel, this makes a chain of + // promises to run in series. + (wasmPlugin.promiseChainEnd = wasmPlugin.promiseChainEnd.then( + async () => { + try { + var exports = await loadWebAssemblyModule( + byteArray, + { + loadAsync: true, + nodelete: true, + }, + name, + {} + ); + } catch (error) { + throw new Error( + `failed to instantiate wasm: ${name}: ${error}` + ); + } + preloadedWasm[name] = exports; + return byteArray; + } + )), + }; + preloadPlugins.push(wasmPlugin); + }; + + var preloadedWasm = {}; + + var PATH = { + isAbs: (path) => path.charAt(0) === '/', + splitPath: (filename) => { + var splitPathRe = + /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; + return splitPathRe.exec(filename).slice(1); + }, + normalizeArray: (parts, allowAboveRoot) => { + // if the path tries to go above the root, `up` ends up > 0 + var up = 0; + for (var i = parts.length - 1; i >= 0; i--) { + var last = parts[i]; + if (last === '.') { + parts.splice(i, 1); + } else if (last === '..') { + parts.splice(i, 1); + up++; + } else if (up) { + parts.splice(i, 1); + up--; + } + } + // if the path is allowed to go above the root, restore leading ..s + if (allowAboveRoot) { + for (; up; up--) { + parts.unshift('..'); + } + } + return parts; + }, + normalize: (path) => { + var isAbsolute = PATH.isAbs(path), + trailingSlash = path.slice(-1) === '/'; + // Normalize the path + path = PATH.normalizeArray( + path.split('/').filter((p) => !!p), + !isAbsolute + ).join('/'); + if (!path && !isAbsolute) { + path = '.'; + } + if (path && trailingSlash) { + path += '/'; + } + return (isAbsolute ? '/' : '') + path; + }, + dirname: (path) => { + var result = PATH.splitPath(path), + root = result[0], + dir = result[1]; + if (!root && !dir) { + // No dirname whatsoever + return '.'; + } + if (dir) { + // It has a dirname, strip trailing slash + dir = dir.slice(0, -1); + } + return root + dir; + }, + basename: (path) => path && path.match(/([^\/]+|\/)\/*$/)[1], + join: (...paths) => PATH.normalize(paths.join('/')), + join2: (l, r) => PATH.normalize(l + '/' + r), + }; + + var replaceORIGIN = (parentLibName, rpath) => { + if (rpath.startsWith('$ORIGIN')) { + // TODO: what to do if we only know the relative path of the file? It will return "." here. + var origin = PATH.dirname(parentLibName); + return rpath.replace('$ORIGIN', origin); + } + return rpath; + }; + + var withStackSave = (f) => { + var stack = stackSave(); + var ret = f(); + stackRestore(stack); + return ret; + }; + + var stackAlloc = (sz) => __emscripten_stack_alloc(sz); + + var lengthBytesUTF8 = (str) => { + var len = 0; + for (var i = 0; i < str.length; ++i) { + // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code + // unit, not a Unicode code point of the character! So decode + // UTF16->UTF32->UTF8. + // See http://unicode.org/faq/utf_bom.html#utf16-3 + var c = str.charCodeAt(i); + // possibly a lead surrogate + if (c <= 127) { + len++; + } else if (c <= 2047) { + len += 2; + } else if (c >= 55296 && c <= 57343) { + len += 4; + ++i; + } else { + len += 3; + } + } + return len; + }; + + var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => { + // Parameter maxBytesToWrite is not optional. Negative values, 0, null, + // undefined and false each don't write out any bytes. + if (!(maxBytesToWrite > 0)) return 0; + var startIdx = outIdx; + var endIdx = outIdx + maxBytesToWrite - 1; + // -1 for string null terminator. + for (var i = 0; i < str.length; ++i) { + // For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description + // and https://www.ietf.org/rfc/rfc2279.txt + // and https://tools.ietf.org/html/rfc3629 + var u = str.codePointAt(i); + if (u <= 127) { + if (outIdx >= endIdx) break; + heap[outIdx++] = u; + } else if (u <= 2047) { + if (outIdx + 1 >= endIdx) break; + heap[outIdx++] = 192 | (u >> 6); + heap[outIdx++] = 128 | (u & 63); + } else if (u <= 65535) { + if (outIdx + 2 >= endIdx) break; + heap[outIdx++] = 224 | (u >> 12); + heap[outIdx++] = 128 | ((u >> 6) & 63); + heap[outIdx++] = 128 | (u & 63); + } else { + if (outIdx + 3 >= endIdx) break; + heap[outIdx++] = 240 | (u >> 18); + heap[outIdx++] = 128 | ((u >> 12) & 63); + heap[outIdx++] = 128 | ((u >> 6) & 63); + heap[outIdx++] = 128 | (u & 63); + // Gotcha: if codePoint is over 0xFFFF, it is represented as a surrogate pair in UTF-16. + // We need to manually skip over the second code unit for correct iteration. + i++; + } + } + // Null-terminate the pointer to the buffer. + heap[outIdx] = 0; + return outIdx - startIdx; + }; + + var stringToUTF8 = (str, outPtr, maxBytesToWrite) => + stringToUTF8Array(str, HEAPU8, outPtr, maxBytesToWrite); + + var stringToUTF8OnStack = (str) => { + var size = lengthBytesUTF8(str) + 1; + var ret = stackAlloc(size); + stringToUTF8(str, ret, size); + return ret; + }; + + var initRandomFill = () => (view) => crypto.getRandomValues(view); + + var randomFill = (view) => { + // Lazily init on the first invocation. + (randomFill = initRandomFill())(view); + }; + + var PATH_FS = { + resolve: (...args) => { + var resolvedPath = '', + resolvedAbsolute = false; + for (var i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) { + var path = i >= 0 ? args[i] : FS.cwd(); + // Skip empty and invalid entries + if (typeof path != 'string') { + throw new TypeError( + 'Arguments to path.resolve must be strings' + ); + } else if (!path) { + return ''; + } + resolvedPath = path + '/' + resolvedPath; + resolvedAbsolute = PATH.isAbs(path); + } + // At this point the path should be resolved to a full absolute path, but + // handle relative paths to be safe (might happen when process.cwd() fails) + resolvedPath = PATH.normalizeArray( + resolvedPath.split('/').filter((p) => !!p), + !resolvedAbsolute + ).join('/'); + return (resolvedAbsolute ? '/' : '') + resolvedPath || '.'; + }, + relative: (from, to) => { + from = PATH_FS.resolve(from).slice(1); + to = PATH_FS.resolve(to).slice(1); + function trim(arr) { + var start = 0; + for (; start < arr.length; start++) { + if (arr[start] !== '') break; + } + var end = arr.length - 1; + for (; end >= 0; end--) { + if (arr[end] !== '') break; + } + if (start > end) return []; + return arr.slice(start, end - start + 1); + } + var fromParts = trim(from.split('/')); + var toParts = trim(to.split('/')); + var length = Math.min(fromParts.length, toParts.length); + var samePartsLength = length; + for (var i = 0; i < length; i++) { + if (fromParts[i] !== toParts[i]) { + samePartsLength = i; + break; + } + } + var outputParts = []; + for (var i = samePartsLength; i < fromParts.length; i++) { + outputParts.push('..'); + } + outputParts = outputParts.concat(toParts.slice(samePartsLength)); + return outputParts.join('/'); + }, + }; + + var FS_stdin_getChar_buffer = []; + + /** @type {function(string, boolean=, number=)} */ var intArrayFromString = + (stringy, dontAddNull, length) => { + var len = length > 0 ? length : lengthBytesUTF8(stringy) + 1; + var u8array = new Array(len); + var numBytesWritten = stringToUTF8Array( + stringy, + u8array, + 0, + u8array.length + ); + if (dontAddNull) u8array.length = numBytesWritten; + return u8array; + }; + + var FS_stdin_getChar = () => { + if (!FS_stdin_getChar_buffer.length) { + var result = null; + if (ENVIRONMENT_IS_NODE) { + // we will read data by chunks of BUFSIZE + var BUFSIZE = 256; + var buf = Buffer.alloc(BUFSIZE); + var bytesRead = 0; + // For some reason we must suppress a closure warning here, even though + // fd definitely exists on process.stdin, and is even the proper way to + // get the fd of stdin, + // https://github.com/nodejs/help/issues/2136#issuecomment-523649904 + // This started to happen after moving this logic out of library_tty.js, + // so it is related to the surrounding code in some unclear manner. + /** @suppress {missingProperties} */ var fd = process.stdin.fd; + try { + bytesRead = fs.readSync(fd, buf, 0, BUFSIZE); + } catch (e) { + // Cross-platform differences: on Windows, reading EOF throws an + // exception, but on other OSes, reading EOF returns 0. Uniformize + // behavior by treating the EOF exception to return 0. + if (e.toString().includes('EOF')) bytesRead = 0; + else throw e; + } + if (bytesRead > 0) { + result = buf.slice(0, bytesRead).toString('utf-8'); + } + } else { + } + if (!result) { + return null; + } + FS_stdin_getChar_buffer = intArrayFromString(result, true); + } + return FS_stdin_getChar_buffer.shift(); + }; + + var TTY = { + ttys: [], + init() {}, + shutdown() {}, + register(dev, ops) { + TTY.ttys[dev] = { + input: [], + output: [], + ops, + }; + FS.registerDevice(dev, TTY.stream_ops); + }, + stream_ops: { + open(stream) { + var tty = TTY.ttys[stream.node.rdev]; + if (!tty) { + throw new FS.ErrnoError(43); + } + stream.tty = tty; + stream.seekable = false; + }, + close(stream) { + // flush any pending line data + stream.tty.ops.fsync(stream.tty); + }, + fsync(stream) { + stream.tty.ops.fsync(stream.tty); + }, + read(stream, buffer, offset, length, pos) { + if (!stream.tty || !stream.tty.ops.get_char) { + throw new FS.ErrnoError(60); + } + var bytesRead = 0; + for (var i = 0; i < length; i++) { + var result; + try { + result = stream.tty.ops.get_char(stream.tty); + } catch (e) { + throw new FS.ErrnoError(29); + } + if (result === undefined && bytesRead === 0) { + throw new FS.ErrnoError(6); + } + if (result === null || result === undefined) break; + bytesRead++; + buffer[offset + i] = result; + } + if (bytesRead) { + stream.node.atime = Date.now(); + } + return bytesRead; + }, + write(stream, buffer, offset, length, pos) { + if (!stream.tty || !stream.tty.ops.put_char) { + throw new FS.ErrnoError(60); + } + try { + for (var i = 0; i < length; i++) { + stream.tty.ops.put_char(stream.tty, buffer[offset + i]); + } + } catch (e) { + throw new FS.ErrnoError(29); + } + if (length) { + stream.node.mtime = stream.node.ctime = Date.now(); + } + return i; + }, + }, + default_tty_ops: { + get_char(tty) { + return FS_stdin_getChar(); + }, + put_char(tty, val) { + if (val === null || val === 10) { + out(UTF8ArrayToString(tty.output)); + tty.output = []; + } else { + if (val != 0) tty.output.push(val); + } + }, + fsync(tty) { + if (tty.output?.length > 0) { + out(UTF8ArrayToString(tty.output)); + tty.output = []; + } + }, + ioctl_tcgets(tty) { + // typical setting + return { + c_iflag: 25856, + c_oflag: 5, + c_cflag: 191, + c_lflag: 35387, + c_cc: [ + 3, 28, 127, 21, 4, 0, 1, 0, 17, 19, 26, 0, 18, 15, 23, + 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ], + }; + }, + ioctl_tcsets(tty, optional_actions, data) { + // currently just ignore + return 0; + }, + ioctl_tiocgwinsz(tty) { + return [24, 80]; + }, + }, + default_tty1_ops: { + put_char(tty, val) { + if (val === null || val === 10) { + err(UTF8ArrayToString(tty.output)); + tty.output = []; + } else { + if (val != 0) tty.output.push(val); + } + }, + fsync(tty) { + if (tty.output?.length > 0) { + err(UTF8ArrayToString(tty.output)); + tty.output = []; + } + }, + }, + }; + + var zeroMemory = (ptr, size) => HEAPU8.fill(0, ptr, ptr + size); + + var mmapAlloc = (size) => { + size = alignMemory(size, 65536); + var ptr = _emscripten_builtin_memalign(65536, size); + if (ptr) zeroMemory(ptr, size); + return ptr; + }; + + var MEMFS = { + ops_table: null, + mount(mount) { + return MEMFS.createNode(null, '/', 16895, 0); + }, + createNode(parent, name, mode, dev) { + if (FS.isBlkdev(mode) || FS.isFIFO(mode)) { + // no supported + throw new FS.ErrnoError(63); + } + MEMFS.ops_table ||= { + dir: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + lookup: MEMFS.node_ops.lookup, + mknod: MEMFS.node_ops.mknod, + rename: MEMFS.node_ops.rename, + unlink: MEMFS.node_ops.unlink, + rmdir: MEMFS.node_ops.rmdir, + readdir: MEMFS.node_ops.readdir, + symlink: MEMFS.node_ops.symlink, + }, + stream: { + llseek: MEMFS.stream_ops.llseek, + }, + }, + file: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + }, + stream: { + llseek: MEMFS.stream_ops.llseek, + read: MEMFS.stream_ops.read, + write: MEMFS.stream_ops.write, + mmap: MEMFS.stream_ops.mmap, + msync: MEMFS.stream_ops.msync, + }, + }, + link: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + readlink: MEMFS.node_ops.readlink, + }, + stream: {}, + }, + chrdev: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + }, + stream: FS.chrdev_stream_ops, + }, + }; + var node = FS.createNode(parent, name, mode, dev); + if (FS.isDir(node.mode)) { + node.node_ops = MEMFS.ops_table.dir.node; + node.stream_ops = MEMFS.ops_table.dir.stream; + node.contents = {}; + } else if (FS.isFile(node.mode)) { + node.node_ops = MEMFS.ops_table.file.node; + node.stream_ops = MEMFS.ops_table.file.stream; + node.usedBytes = 0; + // The actual number of bytes used in the typed array, as opposed to contents.length which gives the whole capacity. + // When the byte data of the file is populated, this will point to either a typed array, or a normal JS array. Typed arrays are preferred + // for performance, and used by default. However, typed arrays are not resizable like normal JS arrays are, so there is a small disk size + // penalty involved for appending file writes that continuously grow a file similar to std::vector capacity vs used -scheme. + node.contents = null; + } else if (FS.isLink(node.mode)) { + node.node_ops = MEMFS.ops_table.link.node; + node.stream_ops = MEMFS.ops_table.link.stream; + } else if (FS.isChrdev(node.mode)) { + node.node_ops = MEMFS.ops_table.chrdev.node; + node.stream_ops = MEMFS.ops_table.chrdev.stream; + } + node.atime = node.mtime = node.ctime = Date.now(); + // add the new node to the parent + if (parent) { + parent.contents[name] = node; + parent.atime = parent.mtime = parent.ctime = node.atime; + } + return node; + }, + getFileDataAsTypedArray(node) { + if (!node.contents) return new Uint8Array(0); + if (node.contents.subarray) + return node.contents.subarray(0, node.usedBytes); + // Make sure to not return excess unused bytes. + return new Uint8Array(node.contents); + }, + expandFileStorage(node, newCapacity) { + var prevCapacity = node.contents ? node.contents.length : 0; + if (prevCapacity >= newCapacity) return; + // No need to expand, the storage was already large enough. + // Don't expand strictly to the given requested limit if it's only a very small increase, but instead geometrically grow capacity. + // For small filesizes (<1MB), perform size*2 geometric increase, but for large sizes, do a much more conservative size*1.125 increase to + // avoid overshooting the allocation cap by a very large margin. + var CAPACITY_DOUBLING_MAX = 1024 * 1024; + newCapacity = Math.max( + newCapacity, + (prevCapacity * + (prevCapacity < CAPACITY_DOUBLING_MAX ? 2 : 1.125)) >>> + 0 + ); + if (prevCapacity != 0) newCapacity = Math.max(newCapacity, 256); + // At minimum allocate 256b for each file when expanding. + var oldContents = node.contents; + node.contents = new Uint8Array(newCapacity); + // Allocate new storage. + if (node.usedBytes > 0) + node.contents.set(oldContents.subarray(0, node.usedBytes), 0); + }, + resizeFileStorage(node, newSize) { + if (node.usedBytes == newSize) return; + if (newSize == 0) { + node.contents = null; + // Fully decommit when requesting a resize to zero. + node.usedBytes = 0; + } else { + var oldContents = node.contents; + node.contents = new Uint8Array(newSize); + // Allocate new storage. + if (oldContents) { + node.contents.set( + oldContents.subarray( + 0, + Math.min(newSize, node.usedBytes) + ) + ); + } + node.usedBytes = newSize; + } + }, + node_ops: { + getattr(node) { + var attr = {}; + // device numbers reuse inode numbers. + attr.dev = FS.isChrdev(node.mode) ? node.id : 1; + attr.ino = node.id; + attr.mode = node.mode; + attr.nlink = 1; + attr.uid = 0; + attr.gid = 0; + attr.rdev = node.rdev; + if (FS.isDir(node.mode)) { + attr.size = 4096; + } else if (FS.isFile(node.mode)) { + attr.size = node.usedBytes; + } else if (FS.isLink(node.mode)) { + attr.size = node.link.length; + } else { + attr.size = 0; + } + attr.atime = new Date(node.atime); + attr.mtime = new Date(node.mtime); + attr.ctime = new Date(node.ctime); + // NOTE: In our implementation, st_blocks = Math.ceil(st_size/st_blksize), + // but this is not required by the standard. + attr.blksize = 4096; + attr.blocks = Math.ceil(attr.size / attr.blksize); + return attr; + }, + setattr(node, attr) { + for (const key of ['mode', 'atime', 'mtime', 'ctime']) { + if (attr[key] != null) { + node[key] = attr[key]; + } + } + if (attr.size !== undefined) { + MEMFS.resizeFileStorage(node, attr.size); + } + }, + lookup(parent, name) { + // This error may happen quite a bit. To avoid overhead we reuse it (and + // suffer a lack of stack info). + if (!MEMFS.doesNotExistError) { + MEMFS.doesNotExistError = new FS.ErrnoError(44); + /** @suppress {checkTypes} */ MEMFS.doesNotExistError.stack = + ''; + } + throw MEMFS.doesNotExistError; + }, + mknod(parent, name, mode, dev) { + return MEMFS.createNode(parent, name, mode, dev); + }, + rename(old_node, new_dir, new_name) { + var new_node; + try { + new_node = FS.lookupNode(new_dir, new_name); + } catch (e) {} + if (new_node) { + if (FS.isDir(old_node.mode)) { + // if we're overwriting a directory at new_name, make sure it's empty. + for (var i in new_node.contents) { + throw new FS.ErrnoError(55); + } + } + FS.hashRemoveNode(new_node); + } + // do the internal rewiring + delete old_node.parent.contents[old_node.name]; + new_dir.contents[new_name] = old_node; + old_node.name = new_name; + new_dir.ctime = + new_dir.mtime = + old_node.parent.ctime = + old_node.parent.mtime = + Date.now(); + }, + unlink(parent, name) { + delete parent.contents[name]; + parent.ctime = parent.mtime = Date.now(); + }, + rmdir(parent, name) { + var node = FS.lookupNode(parent, name); + for (var i in node.contents) { + throw new FS.ErrnoError(55); + } + delete parent.contents[name]; + parent.ctime = parent.mtime = Date.now(); + }, + readdir(node) { + return ['.', '..', ...Object.keys(node.contents)]; + }, + symlink(parent, newname, oldpath) { + var node = MEMFS.createNode(parent, newname, 511 | 40960, 0); + node.link = oldpath; + return node; + }, + readlink(node) { + if (!FS.isLink(node.mode)) { + throw new FS.ErrnoError(28); + } + return node.link; + }, + }, + stream_ops: { + read(stream, buffer, offset, length, position) { + var contents = stream.node.contents; + if (position >= stream.node.usedBytes) return 0; + var size = Math.min(stream.node.usedBytes - position, length); + if (size > 8 && contents.subarray) { + // non-trivial, and typed array + buffer.set( + contents.subarray(position, position + size), + offset + ); + } else { + for (var i = 0; i < size; i++) + buffer[offset + i] = contents[position + i]; + } + return size; + }, + write(stream, buffer, offset, length, position, canOwn) { + // If the buffer is located in main memory (HEAP), and if + // memory can grow, we can't hold on to references of the + // memory buffer, as they may get invalidated. That means we + // need to do copy its contents. + if (buffer.buffer === HEAP8.buffer) { + canOwn = false; + } + if (!length) return 0; + var node = stream.node; + node.mtime = node.ctime = Date.now(); + if ( + buffer.subarray && + (!node.contents || node.contents.subarray) + ) { + // This write is from a typed array to a typed array? + if (canOwn) { + node.contents = buffer.subarray( + offset, + offset + length + ); + node.usedBytes = length; + return length; + } else if (node.usedBytes === 0 && position === 0) { + // If this is a simple first write to an empty file, do a fast set since we don't need to care about old data. + node.contents = buffer.slice(offset, offset + length); + node.usedBytes = length; + return length; + } else if (position + length <= node.usedBytes) { + // Writing to an already allocated and used subrange of the file? + node.contents.set( + buffer.subarray(offset, offset + length), + position + ); + return length; + } + } + // Appending to an existing file and we need to reallocate, or source data did not come as a typed array. + MEMFS.expandFileStorage(node, position + length); + if (node.contents.subarray && buffer.subarray) { + // Use typed array write which is available. + node.contents.set( + buffer.subarray(offset, offset + length), + position + ); + } else { + for (var i = 0; i < length; i++) { + node.contents[position + i] = buffer[offset + i]; + } + } + node.usedBytes = Math.max(node.usedBytes, position + length); + return length; + }, + llseek(stream, offset, whence) { + var position = offset; + if (whence === 1) { + position += stream.position; + } else if (whence === 2) { + if (FS.isFile(stream.node.mode)) { + position += stream.node.usedBytes; + } + } + if (position < 0) { + throw new FS.ErrnoError(28); + } + return position; + }, + mmap(stream, length, position, prot, flags) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError(43); + } + var ptr; + var allocated; + var contents = stream.node.contents; + // Only make a new copy when MAP_PRIVATE is specified. + if ( + !(flags & 2) && + contents && + contents.buffer === HEAP8.buffer + ) { + // We can't emulate MAP_SHARED when the file is not backed by the + // buffer we're mapping to (e.g. the HEAP buffer). + allocated = false; + ptr = contents.byteOffset; + } else { + allocated = true; + ptr = mmapAlloc(length); + if (!ptr) { + throw new FS.ErrnoError(48); + } + if (contents) { + // Try to avoid unnecessary slices. + if ( + position > 0 || + position + length < contents.length + ) { + if (contents.subarray) { + contents = contents.subarray( + position, + position + length + ); + } else { + contents = Array.prototype.slice.call( + contents, + position, + position + length + ); + } + } + HEAP8.set(contents, ptr); + } + } + return { + ptr, + allocated, + }; + }, + msync(stream, buffer, offset, length, mmapFlags) { + MEMFS.stream_ops.write( + stream, + buffer, + 0, + length, + offset, + false + ); + // should we check if bytesWritten and length are the same? + return 0; + }, + }, + }; + + var FS_modeStringToFlags = (str) => { + var flagModes = { + r: 0, + 'r+': 2, + w: 512 | 64 | 1, + 'w+': 512 | 64 | 2, + a: 1024 | 64 | 1, + 'a+': 1024 | 64 | 2, + }; + var flags = flagModes[str]; + if (typeof flags == 'undefined') { + throw new Error(`Unknown file open mode: ${str}`); + } + return flags; + }; + + var FS_getMode = (canRead, canWrite) => { + var mode = 0; + if (canRead) mode |= 292 | 73; + if (canWrite) mode |= 146; + return mode; + }; + + var ERRNO_CODES = { + EPERM: 63, + ENOENT: 44, + ESRCH: 71, + EINTR: 27, + EIO: 29, + ENXIO: 60, + E2BIG: 1, + ENOEXEC: 45, + EBADF: 8, + ECHILD: 12, + EAGAIN: 6, + EWOULDBLOCK: 6, + ENOMEM: 48, + EACCES: 2, + EFAULT: 21, + ENOTBLK: 105, + EBUSY: 10, + EEXIST: 20, + EXDEV: 75, + ENODEV: 43, + ENOTDIR: 54, + EISDIR: 31, + EINVAL: 28, + ENFILE: 41, + EMFILE: 33, + ENOTTY: 59, + ETXTBSY: 74, + EFBIG: 22, + ENOSPC: 51, + ESPIPE: 70, + EROFS: 69, + EMLINK: 34, + EPIPE: 64, + EDOM: 18, + ERANGE: 68, + ENOMSG: 49, + EIDRM: 24, + ECHRNG: 106, + EL2NSYNC: 156, + EL3HLT: 107, + EL3RST: 108, + ELNRNG: 109, + EUNATCH: 110, + ENOCSI: 111, + EL2HLT: 112, + EDEADLK: 16, + ENOLCK: 46, + EBADE: 113, + EBADR: 114, + EXFULL: 115, + ENOANO: 104, + EBADRQC: 103, + EBADSLT: 102, + EDEADLOCK: 16, + EBFONT: 101, + ENOSTR: 100, + ENODATA: 116, + ETIME: 117, + ENOSR: 118, + ENONET: 119, + ENOPKG: 120, + EREMOTE: 121, + ENOLINK: 47, + EADV: 122, + ESRMNT: 123, + ECOMM: 124, + EPROTO: 65, + EMULTIHOP: 36, + EDOTDOT: 125, + EBADMSG: 9, + ENOTUNIQ: 126, + EBADFD: 127, + EREMCHG: 128, + ELIBACC: 129, + ELIBBAD: 130, + ELIBSCN: 131, + ELIBMAX: 132, + ELIBEXEC: 133, + ENOSYS: 52, + ENOTEMPTY: 55, + ENAMETOOLONG: 37, + ELOOP: 32, + EOPNOTSUPP: 138, + EPFNOSUPPORT: 139, + ECONNRESET: 15, + ENOBUFS: 42, + EAFNOSUPPORT: 5, + EPROTOTYPE: 67, + ENOTSOCK: 57, + ENOPROTOOPT: 50, + ESHUTDOWN: 140, + ECONNREFUSED: 14, + EADDRINUSE: 3, + ECONNABORTED: 13, + ENETUNREACH: 40, + ENETDOWN: 38, + ETIMEDOUT: 73, + EHOSTDOWN: 142, + EHOSTUNREACH: 23, + EINPROGRESS: 26, + EALREADY: 7, + EDESTADDRREQ: 17, + EMSGSIZE: 35, + EPROTONOSUPPORT: 66, + ESOCKTNOSUPPORT: 137, + EADDRNOTAVAIL: 4, + ENETRESET: 39, + EISCONN: 30, + ENOTCONN: 53, + ETOOMANYREFS: 141, + EUSERS: 136, + EDQUOT: 19, + ESTALE: 72, + ENOTSUP: 138, + ENOMEDIUM: 148, + EILSEQ: 25, + EOVERFLOW: 61, + ECANCELED: 11, + ENOTRECOVERABLE: 56, + EOWNERDEAD: 62, + ESTRPIPE: 135, + }; + + var NODEFS = { + isWindows: false, + staticInit() { + NODEFS.isWindows = !!process.platform.match(/^win/); + var flags = process.binding('constants')['fs']; + NODEFS.flagsForNodeMap = { + 1024: flags['O_APPEND'], + 64: flags['O_CREAT'], + 128: flags['O_EXCL'], + 256: flags['O_NOCTTY'], + 0: flags['O_RDONLY'], + 2: flags['O_RDWR'], + 4096: flags['O_SYNC'], + 512: flags['O_TRUNC'], + 1: flags['O_WRONLY'], + 131072: flags['O_NOFOLLOW'], + }; + }, + convertNodeCode(e) { + var code = e.code; + return ERRNO_CODES[code]; + }, + tryFSOperation(f) { + try { + return f(); + } catch (e) { + if (!e.code) throw e; + // node under windows can return code 'UNKNOWN' here: + // https://github.com/emscripten-core/emscripten/issues/15468 + if (e.code === 'UNKNOWN') throw new FS.ErrnoError(28); + throw new FS.ErrnoError(NODEFS.convertNodeCode(e)); + } + }, + mount(mount) { + return NODEFS.createNode( + null, + '/', + NODEFS.getMode(mount.opts.root), + 0 + ); + }, + createNode(parent, name, mode, dev) { + if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) { + throw new FS.ErrnoError(28); + } + var node = FS.createNode(parent, name, mode); + node.node_ops = NODEFS.node_ops; + node.stream_ops = NODEFS.stream_ops; + return node; + }, + getMode(path) { + return NODEFS.tryFSOperation(() => { + var mode = fs.lstatSync(path).mode; + if (NODEFS.isWindows) { + // Windows does not report the 'x' permission bit, so propagate read + // bits to execute bits. + mode |= (mode & 292) >> 2; + } + return mode; + }); + }, + realPath(node) { + var parts = []; + while (node.parent !== node) { + parts.push(node.name); + node = node.parent; + } + parts.push(node.mount.opts.root); + parts.reverse(); + return PATH.join(...parts); + }, + flagsForNode(flags) { + flags &= ~2097152; + // Ignore this flag from musl, otherwise node.js fails to open the file. + flags &= ~2048; + // Ignore this flag from musl, otherwise node.js fails to open the file. + flags &= ~32768; + // Ignore this flag from musl, otherwise node.js fails to open the file. + flags &= ~524288; + // Some applications may pass it; it makes no sense for a single process. + flags &= ~65536; + // Node.js doesn't need this passed in, it errors. + var newFlags = 0; + for (var k in NODEFS.flagsForNodeMap) { + if (flags & k) { + newFlags |= NODEFS.flagsForNodeMap[k]; + flags ^= k; + } + } + if (flags) { + throw new FS.ErrnoError(28); + } + return newFlags; + }, + getattr(func, node) { + var stat = NODEFS.tryFSOperation(func); + if (NODEFS.isWindows) { + // node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake + // them with default blksize of 4096. + // See http://support.microsoft.com/kb/140365 + if (!stat.blksize) { + stat.blksize = 4096; + } + if (!stat.blocks) { + stat.blocks = + ((stat.size + stat.blksize - 1) / stat.blksize) | 0; + } + // Windows does not report the 'x' permission bit, so propagate read + // bits to execute bits. + stat.mode |= (stat.mode & 292) >> 2; + } + return { + dev: stat.dev, + ino: node.id, + mode: stat.mode, + nlink: stat.nlink, + uid: stat.uid, + gid: stat.gid, + rdev: stat.rdev, + size: stat.size, + atime: stat.atime, + mtime: stat.mtime, + ctime: stat.ctime, + blksize: stat.blksize, + blocks: stat.blocks, + }; + }, + setattr(arg, node, attr, chmod, utimes, truncate, stat) { + NODEFS.tryFSOperation(() => { + if (attr.mode !== undefined) { + var mode = attr.mode; + if (NODEFS.isWindows) { + // Windows only supports S_IREAD / S_IWRITE (S_IRUSR / S_IWUSR) + // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/chmod-wchmod + mode &= 384; + } + chmod(arg, mode); + // update the common node structure mode as well + node.mode = attr.mode; + } + if (typeof (attr.atime ?? attr.mtime) === 'number') { + // Unfortunately, we have to stat the current value if we don't want + // to change it. On top of that, since the times don't round trip + // this will only keep the value nearly unchanged not exactly + // unchanged. See: + // https://github.com/nodejs/node/issues/56492 + var atime = new Date(attr.atime ?? stat(arg).atime); + var mtime = new Date(attr.mtime ?? stat(arg).mtime); + utimes(arg, atime, mtime); + } + if (attr.size !== undefined) { + truncate(arg, attr.size); + } + }); + }, + node_ops: { + getattr(node) { + var path = NODEFS.realPath(node); + return NODEFS.getattr(() => fs.lstatSync(path), node); + }, + setattr(node, attr) { + var path = NODEFS.realPath(node); + if (attr.mode != null && attr.dontFollow) { + throw new FS.ErrnoError(52); + } + NODEFS.setattr( + path, + node, + attr, + fs.chmodSync, + fs.utimesSync, + fs.truncateSync, + fs.lstatSync + ); + }, + lookup(parent, name) { + var path = PATH.join2(NODEFS.realPath(parent), name); + var mode = NODEFS.getMode(path); + return NODEFS.createNode(parent, name, mode); + }, + mknod(parent, name, mode, dev) { + var node = NODEFS.createNode(parent, name, mode, dev); + // create the backing node for this in the fs root as well + var path = NODEFS.realPath(node); + NODEFS.tryFSOperation(() => { + if (FS.isDir(node.mode)) { + fs.mkdirSync(path, node.mode); + } else { + fs.writeFileSync(path, '', { + mode: node.mode, + }); + } + }); + return node; + }, + rename(oldNode, newDir, newName) { + var oldPath = NODEFS.realPath(oldNode); + var newPath = PATH.join2(NODEFS.realPath(newDir), newName); + try { + FS.unlink(newPath); + } catch (e) {} + NODEFS.tryFSOperation(() => fs.renameSync(oldPath, newPath)); + oldNode.name = newName; + }, + unlink(parent, name) { + var path = PATH.join2(NODEFS.realPath(parent), name); + NODEFS.tryFSOperation(() => fs.unlinkSync(path)); + }, + rmdir(parent, name) { + var path = PATH.join2(NODEFS.realPath(parent), name); + NODEFS.tryFSOperation(() => fs.rmdirSync(path)); + }, + readdir(node) { + var path = NODEFS.realPath(node); + return NODEFS.tryFSOperation(() => fs.readdirSync(path)); + }, + symlink(parent, newName, oldPath) { + var newPath = PATH.join2(NODEFS.realPath(parent), newName); + NODEFS.tryFSOperation(() => fs.symlinkSync(oldPath, newPath)); + }, + readlink(node) { + var path = NODEFS.realPath(node); + return NODEFS.tryFSOperation(() => fs.readlinkSync(path)); + }, + statfs(path) { + var stats = NODEFS.tryFSOperation(() => fs.statfsSync(path)); + // Node.js doesn't provide frsize (fragment size). Set it to bsize (block size) + // as they're often the same in many file systems. May not be accurate for all. + stats.frsize = stats.bsize; + return stats; + }, + }, + stream_ops: { + getattr(stream) { + return NODEFS.getattr( + () => fs.fstatSync(stream.nfd), + stream.node + ); + }, + setattr(stream, attr) { + NODEFS.setattr( + stream.nfd, + stream.node, + attr, + fs.fchmodSync, + fs.futimesSync, + fs.ftruncateSync, + fs.fstatSync + ); + }, + open(stream) { + var path = NODEFS.realPath(stream.node); + NODEFS.tryFSOperation(() => { + stream.shared.refcount = 1; + stream.nfd = fs.openSync( + path, + NODEFS.flagsForNode(stream.flags) + ); + }); + }, + close(stream) { + NODEFS.tryFSOperation(() => { + if (stream.nfd && --stream.shared.refcount === 0) { + fs.closeSync(stream.nfd); + } + }); + }, + dup(stream) { + stream.shared.refcount++; + }, + read(stream, buffer, offset, length, position) { + return NODEFS.tryFSOperation(() => + fs.readSync( + stream.nfd, + new Int8Array(buffer.buffer, offset, length), + 0, + length, + position + ) + ); + }, + write(stream, buffer, offset, length, position) { + return NODEFS.tryFSOperation(() => + fs.writeSync( + stream.nfd, + new Int8Array(buffer.buffer, offset, length), + 0, + length, + position + ) + ); + }, + llseek(stream, offset, whence) { + var position = offset; + if (whence === 1) { + position += stream.position; + } else if (whence === 2) { + if (FS.isFile(stream.node.mode)) { + NODEFS.tryFSOperation(() => { + var stat = fs.fstatSync(stream.nfd); + position += stat.size; + }); + } + } + if (position < 0) { + throw new FS.ErrnoError(28); + } + return position; + }, + mmap(stream, length, position, prot, flags) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError(43); + } + var ptr = mmapAlloc(length); + NODEFS.stream_ops.read(stream, HEAP8, ptr, length, position); + return { + ptr, + allocated: true, + }; + }, + msync(stream, buffer, offset, length, mmapFlags) { + NODEFS.stream_ops.write( + stream, + buffer, + 0, + length, + offset, + false + ); + // should we check if bytesWritten and length are the same? + return 0; + }, + }, + }; + + var PROXYFS = { + mount(mount) { + return PROXYFS.createNode( + null, + '/', + mount.opts.fs.lstat(mount.opts.root).mode, + 0 + ); + }, + createNode(parent, name, mode, dev) { + if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) { + throw new FS.ErrnoError(ERRNO_CODES.EINVAL); + } + var node = FS.createNode(parent, name, mode); + node.node_ops = PROXYFS.node_ops; + node.stream_ops = PROXYFS.stream_ops; + return node; + }, + realPath(node) { + var parts = []; + while (node.parent !== node) { + parts.push(node.name); + node = node.parent; + } + parts.push(node.mount.opts.root); + parts.reverse(); + return PATH.join(...parts); + }, + node_ops: { + getattr(node) { + var path = PROXYFS.realPath(node); + var stat; + try { + stat = node.mount.opts.fs.lstat(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + return { + dev: stat.dev, + ino: stat.ino, + mode: stat.mode, + nlink: stat.nlink, + uid: stat.uid, + gid: stat.gid, + rdev: stat.rdev, + size: stat.size, + atime: stat.atime, + mtime: stat.mtime, + ctime: stat.ctime, + blksize: stat.blksize, + blocks: stat.blocks, + }; + }, + setattr(node, attr) { + var path = PROXYFS.realPath(node); + try { + if (attr.mode !== undefined) { + node.mount.opts.fs.chmod(path, attr.mode); + // update the common node structure mode as well + node.mode = attr.mode; + } + if (attr.atime || attr.mtime) { + var atime = new Date(attr.atime || attr.mtime); + var mtime = new Date(attr.mtime || attr.atime); + node.mount.opts.fs.utime(path, atime, mtime); + } + if (attr.size !== undefined) { + node.mount.opts.fs.truncate(path, attr.size); + } + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + lookup(parent, name) { + try { + var path = PATH.join2(PROXYFS.realPath(parent), name); + var mode = parent.mount.opts.fs.lstat(path).mode; + var node = PROXYFS.createNode(parent, name, mode); + return node; + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + mknod(parent, name, mode, dev) { + var node = PROXYFS.createNode(parent, name, mode, dev); + // create the backing node for this in the fs root as well + var path = PROXYFS.realPath(node); + try { + if (FS.isDir(node.mode)) { + node.mount.opts.fs.mkdir(path, node.mode); + } else { + node.mount.opts.fs.writeFile(path, '', { + mode: node.mode, + }); + } + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + return node; + }, + rename(oldNode, newDir, newName) { + var oldPath = PROXYFS.realPath(oldNode); + var newPath = PATH.join2(PROXYFS.realPath(newDir), newName); + try { + oldNode.mount.opts.fs.rename(oldPath, newPath); + oldNode.name = newName; + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + unlink(parent, name) { + var path = PATH.join2(PROXYFS.realPath(parent), name); + try { + parent.mount.opts.fs.unlink(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + rmdir(parent, name) { + var path = PATH.join2(PROXYFS.realPath(parent), name); + try { + parent.mount.opts.fs.rmdir(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + readdir(node) { + var path = PROXYFS.realPath(node); + try { + return node.mount.opts.fs.readdir(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + symlink(parent, newName, oldPath) { + var newPath = PATH.join2(PROXYFS.realPath(parent), newName); + try { + parent.mount.opts.fs.symlink(oldPath, newPath); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + readlink(node) { + var path = PROXYFS.realPath(node); + try { + return node.mount.opts.fs.readlink(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + }, + stream_ops: { + open(stream) { + var path = PROXYFS.realPath(stream.node); + try { + stream.nfd = stream.node.mount.opts.fs.open( + path, + stream.flags + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + close(stream) { + try { + stream.node.mount.opts.fs.close(stream.nfd); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + read(stream, buffer, offset, length, position) { + try { + return stream.node.mount.opts.fs.read( + stream.nfd, + buffer, + offset, + length, + position + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + write(stream, buffer, offset, length, position) { + try { + return stream.node.mount.opts.fs.write( + stream.nfd, + buffer, + offset, + length, + position + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + llseek(stream, offset, whence) { + var position = offset; + if (whence === 1) { + position += stream.position; + } else if (whence === 2) { + if (FS.isFile(stream.node.mode)) { + try { + var stat = stream.node.node_ops.getattr( + stream.node + ); + position += stat.size; + } catch (e) { + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + } + } + if (position < 0) { + throw new FS.ErrnoError(ERRNO_CODES.EINVAL); + } + return position; + }, + }, + }; + + var FS_createDataFile = (...args) => FS.createDataFile(...args); + + var getUniqueRunDependency = (id) => id; + + var FS_handledByPreloadPlugin = async (byteArray, fullname) => { + // Ensure plugins are ready. + if (typeof Browser != 'undefined') Browser.init(); + for (var plugin of preloadPlugins) { + if (plugin['canHandle'](fullname)) { + return plugin['handle'](byteArray, fullname); + } + } + // In no plugin handled this file then return the original/unmodified + // byteArray. + return byteArray; + }; + + var FS_preloadFile = async ( + parent, + name, + url, + canRead, + canWrite, + dontCreateFile, + canOwn, + preFinish + ) => { + // TODO we should allow people to just pass in a complete filename instead + // of parent and name being that we just join them anyways + var fullname = name + ? PATH_FS.resolve(PATH.join2(parent, name)) + : parent; + var dep = getUniqueRunDependency(`cp ${fullname}`); + // might have several active requests for the same fullname + addRunDependency(dep); + try { + var byteArray = url; + if (typeof url == 'string') { + byteArray = await asyncLoad(url); + } + byteArray = await FS_handledByPreloadPlugin(byteArray, fullname); + preFinish?.(); + if (!dontCreateFile) { + FS_createDataFile( + parent, + name, + byteArray, + canRead, + canWrite, + canOwn + ); + } + } finally { + removeRunDependency(dep); + } + }; + + var FS_createPreloadedFile = ( + parent, + name, + url, + canRead, + canWrite, + onload, + onerror, + dontCreateFile, + canOwn, + preFinish + ) => { + FS_preloadFile( + parent, + name, + url, + canRead, + canWrite, + dontCreateFile, + canOwn, + preFinish + ) + .then(onload) + .catch(onerror); + }; + + var FS = { + root: null, + mounts: [], + devices: {}, + streams: [], + nextInode: 1, + nameTable: null, + currentPath: '/', + initialized: false, + ignorePermissions: true, + filesystems: null, + syncFSRequests: 0, + readFiles: {}, + ErrnoError: class { + name = 'ErrnoError'; + // We set the `name` property to be able to identify `FS.ErrnoError` + // - the `name` is a standard ECMA-262 property of error objects. Kind of good to have it anyway. + // - when using PROXYFS, an error can come from an underlying FS + // as different FS objects have their own FS.ErrnoError each, + // the test `err instanceof FS.ErrnoError` won't detect an error coming from another filesystem, causing bugs. + // we'll use the reliable test `err.name == "ErrnoError"` instead + constructor(errno) { + this.errno = errno; + } + }, + FSStream: class { + shared = {}; + get object() { + return this.node; + } + set object(val) { + this.node = val; + } + get isRead() { + return (this.flags & 2097155) !== 1; + } + get isWrite() { + return (this.flags & 2097155) !== 0; + } + get isAppend() { + return this.flags & 1024; + } + get flags() { + return this.shared.flags; + } + set flags(val) { + this.shared.flags = val; + } + get position() { + return this.shared.position; + } + set position(val) { + this.shared.position = val; + } + }, + FSNode: class { + node_ops = {}; + stream_ops = {}; + readMode = 292 | 73; + writeMode = 146; + mounted = null; + constructor(parent, name, mode, rdev) { + if (!parent) { + parent = this; + } + this.parent = parent; + this.mount = parent.mount; + this.id = FS.nextInode++; + this.name = name; + this.mode = mode; + this.rdev = rdev; + this.atime = this.mtime = this.ctime = Date.now(); + } + get read() { + return (this.mode & this.readMode) === this.readMode; + } + set read(val) { + val + ? (this.mode |= this.readMode) + : (this.mode &= ~this.readMode); + } + get write() { + return (this.mode & this.writeMode) === this.writeMode; + } + set write(val) { + val + ? (this.mode |= this.writeMode) + : (this.mode &= ~this.writeMode); + } + get isFolder() { + return FS.isDir(this.mode); + } + get isDevice() { + return FS.isChrdev(this.mode); + } + }, + lookupPath(path, opts = {}) { + if (!path) { + throw new FS.ErrnoError(44); + } + opts.follow_mount ??= true; + if (!PATH.isAbs(path)) { + path = FS.cwd() + '/' + path; + } + // limit max consecutive symlinks to 40 (SYMLOOP_MAX). + linkloop: for (var nlinks = 0; nlinks < 40; nlinks++) { + // split the absolute path + var parts = path.split('/').filter((p) => !!p); + // start at the root + var current = FS.root; + var current_path = '/'; + for (var i = 0; i < parts.length; i++) { + var islast = i === parts.length - 1; + if (islast && opts.parent) { + // stop resolving + break; + } + if (parts[i] === '.') { + continue; + } + if (parts[i] === '..') { + current_path = PATH.dirname(current_path); + if (FS.isRoot(current)) { + path = + current_path + + '/' + + parts.slice(i + 1).join('/'); + // We're making progress here, don't let many consecutive ..'s + // lead to ELOOP + nlinks--; + continue linkloop; + } else { + current = current.parent; + } + continue; + } + current_path = PATH.join2(current_path, parts[i]); + try { + current = FS.lookupNode(current, parts[i]); + } catch (e) { + // if noent_okay is true, suppress a ENOENT in the last component + // and return an object with an undefined node. This is needed for + // resolving symlinks in the path when creating a file. + if (e?.errno === 44 && islast && opts.noent_okay) { + return { + path: current_path, + }; + } + throw e; + } + // jump to the mount's root node if this is a mountpoint + if ( + FS.isMountpoint(current) && + (!islast || opts.follow_mount) + ) { + current = current.mounted.root; + } + // by default, lookupPath will not follow a symlink if it is the final path component. + // setting opts.follow = true will override this behavior. + if (FS.isLink(current.mode) && (!islast || opts.follow)) { + if (!current.node_ops.readlink) { + throw new FS.ErrnoError(52); + } + var link = current.node_ops.readlink(current); + if (!PATH.isAbs(link)) { + link = PATH.dirname(current_path) + '/' + link; + } + path = link + '/' + parts.slice(i + 1).join('/'); + continue linkloop; + } + } + return { + path: current_path, + node: current, + }; + } + throw new FS.ErrnoError(32); + }, + getPath(node) { + var path; + while (true) { + if (FS.isRoot(node)) { + var mount = node.mount.mountpoint; + if (!path) return mount; + return mount[mount.length - 1] !== '/' + ? `${mount}/${path}` + : mount + path; + } + path = path ? `${node.name}/${path}` : node.name; + node = node.parent; + } + }, + hashName(parentid, name) { + var hash = 0; + for (var i = 0; i < name.length; i++) { + hash = ((hash << 5) - hash + name.charCodeAt(i)) | 0; + } + return ((parentid + hash) >>> 0) % FS.nameTable.length; + }, + hashAddNode(node) { + var hash = FS.hashName(node.parent.id, node.name); + node.name_next = FS.nameTable[hash]; + FS.nameTable[hash] = node; + }, + hashRemoveNode(node) { + var hash = FS.hashName(node.parent.id, node.name); + if (FS.nameTable[hash] === node) { + FS.nameTable[hash] = node.name_next; + } else { + var current = FS.nameTable[hash]; + while (current) { + if (current.name_next === node) { + current.name_next = node.name_next; + break; + } + current = current.name_next; + } + } + }, + lookupNode(parent, name) { + var errCode = FS.mayLookup(parent); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + var hash = FS.hashName(parent.id, name); + for (var node = FS.nameTable[hash]; node; node = node.name_next) { + var nodeName = node.name; + if (node.parent.id === parent.id && nodeName === name) { + return node; + } + } + // if we failed to find it in the cache, call into the VFS + return FS.lookup(parent, name); + }, + createNode(parent, name, mode, rdev) { + var node = new FS.FSNode(parent, name, mode, rdev); + FS.hashAddNode(node); + return node; + }, + destroyNode(node) { + FS.hashRemoveNode(node); + }, + isRoot(node) { + return node === node.parent; + }, + isMountpoint(node) { + return !!node.mounted; + }, + isFile(mode) { + return (mode & 61440) === 32768; + }, + isDir(mode) { + return (mode & 61440) === 16384; + }, + isLink(mode) { + return (mode & 61440) === 40960; + }, + isChrdev(mode) { + return (mode & 61440) === 8192; + }, + isBlkdev(mode) { + return (mode & 61440) === 24576; + }, + isFIFO(mode) { + return (mode & 61440) === 4096; + }, + isSocket(mode) { + return (mode & 49152) === 49152; + }, + flagsToPermissionString(flag) { + var perms = ['r', 'w', 'rw'][flag & 3]; + if (flag & 512) { + perms += 'w'; + } + return perms; + }, + nodePermissions(node, perms) { + if (FS.ignorePermissions) { + return 0; + } + // return 0 if any user, group or owner bits are set. + if (perms.includes('r') && !(node.mode & 292)) { + return 2; + } else if (perms.includes('w') && !(node.mode & 146)) { + return 2; + } else if (perms.includes('x') && !(node.mode & 73)) { + return 2; + } + return 0; + }, + mayLookup(dir) { + if (!FS.isDir(dir.mode)) return 54; + var errCode = FS.nodePermissions(dir, 'x'); + if (errCode) return errCode; + if (!dir.node_ops.lookup) return 2; + return 0; + }, + mayCreate(dir, name) { + if (!FS.isDir(dir.mode)) { + return 54; + } + try { + var node = FS.lookupNode(dir, name); + return 20; + } catch (e) {} + return FS.nodePermissions(dir, 'wx'); + }, + mayDelete(dir, name, isdir) { + var node; + try { + node = FS.lookupNode(dir, name); + } catch (e) { + return e.errno; + } + var errCode = FS.nodePermissions(dir, 'wx'); + if (errCode) { + return errCode; + } + if (isdir) { + if (!FS.isDir(node.mode)) { + return 54; + } + if (FS.isRoot(node) || FS.getPath(node) === FS.cwd()) { + return 10; + } + } else { + if (FS.isDir(node.mode)) { + return 31; + } + } + return 0; + }, + mayOpen(node, flags) { + if (!node) { + return 44; + } + if (FS.isLink(node.mode)) { + return 32; + } else if (FS.isDir(node.mode)) { + if ( + FS.flagsToPermissionString(flags) !== 'r' || + flags & (512 | 64) + ) { + // TODO: check for O_SEARCH? (== search for dir only) + return 31; + } + } + return FS.nodePermissions(node, FS.flagsToPermissionString(flags)); + }, + checkOpExists(op, err) { + if (!op) { + throw new FS.ErrnoError(err); + } + return op; + }, + MAX_OPEN_FDS: 4096, + nextfd() { + for (var fd = 0; fd <= FS.MAX_OPEN_FDS; fd++) { + if (!FS.streams[fd]) { + return fd; + } + } + throw new FS.ErrnoError(33); + }, + getStreamChecked(fd) { + var stream = FS.getStream(fd); + if (!stream) { + throw new FS.ErrnoError(8); + } + return stream; + }, + getStream: (fd) => FS.streams[fd], + createStream(stream, fd = -1) { + // clone it, so we can return an instance of FSStream + stream = Object.assign(new FS.FSStream(), stream); + if (fd == -1) { + fd = FS.nextfd(); + } + stream.fd = fd; + FS.streams[fd] = stream; + return stream; + }, + closeStream(fd) { + FS.streams[fd] = null; + }, + dupStream(origStream, fd = -1) { + var stream = FS.createStream(origStream, fd); + stream.stream_ops?.dup?.(stream); + return stream; + }, + doSetAttr(stream, node, attr) { + var setattr = stream?.stream_ops.setattr; + var arg = setattr ? stream : node; + setattr ??= node.node_ops.setattr; + FS.checkOpExists(setattr, 63); + setattr(arg, attr); + }, + chrdev_stream_ops: { + open(stream) { + var device = FS.getDevice(stream.node.rdev); + // override node's stream ops with the device's + stream.stream_ops = device.stream_ops; + // forward the open call + stream.stream_ops.open?.(stream); + }, + llseek() { + throw new FS.ErrnoError(70); + }, + }, + major: (dev) => dev >> 8, + minor: (dev) => dev & 255, + makedev: (ma, mi) => (ma << 8) | mi, + registerDevice(dev, ops) { + FS.devices[dev] = { + stream_ops: ops, + }; + }, + getDevice: (dev) => FS.devices[dev], + getMounts(mount) { + var mounts = []; + var check = [mount]; + while (check.length) { + var m = check.pop(); + mounts.push(m); + check.push(...m.mounts); + } + return mounts; + }, + syncfs(populate, callback) { + if (typeof populate == 'function') { + callback = populate; + populate = false; + } + FS.syncFSRequests++; + if (FS.syncFSRequests > 1) { + err( + `warning: ${FS.syncFSRequests} FS.syncfs operations in flight at once, probably just doing extra work` + ); + } + var mounts = FS.getMounts(FS.root.mount); + var completed = 0; + function doCallback(errCode) { + FS.syncFSRequests--; + return callback(errCode); + } + function done(errCode) { + if (errCode) { + if (!done.errored) { + done.errored = true; + return doCallback(errCode); + } + return; + } + if (++completed >= mounts.length) { + doCallback(null); + } + } + // sync all mounts + for (var mount of mounts) { + if (mount.type.syncfs) { + mount.type.syncfs(mount, populate, done); + } else { + done(null); + } + } + }, + mount(type, opts, mountpoint) { + var root = mountpoint === '/'; + var pseudo = !mountpoint; + var node; + if (root && FS.root) { + throw new FS.ErrnoError(10); + } else if (!root && !pseudo) { + var lookup = FS.lookupPath(mountpoint, { + follow_mount: false, + }); + mountpoint = lookup.path; + // use the absolute path + node = lookup.node; + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + } + var mount = { + type, + opts, + mountpoint, + mounts: [], + }; + // create a root node for the fs + var mountRoot = type.mount(mount); + mountRoot.mount = mount; + mount.root = mountRoot; + if (root) { + FS.root = mountRoot; + } else if (node) { + // set as a mountpoint + node.mounted = mount; + // add the new mount to the current mount's children + if (node.mount) { + node.mount.mounts.push(mount); + } + } + return mountRoot; + }, + unmount(mountpoint) { + var lookup = FS.lookupPath(mountpoint, { + follow_mount: false, + }); + if (!FS.isMountpoint(lookup.node)) { + throw new FS.ErrnoError(28); + } + // destroy the nodes for this mount, and all its child mounts + var node = lookup.node; + var mount = node.mounted; + var mounts = FS.getMounts(mount); + for (var [hash, current] of Object.entries(FS.nameTable)) { + while (current) { + var next = current.name_next; + if (mounts.includes(current.mount)) { + FS.destroyNode(current); + } + current = next; + } + } + // no longer a mountpoint + node.mounted = null; + // remove this mount from the child mounts + var idx = node.mount.mounts.indexOf(mount); + node.mount.mounts.splice(idx, 1); + }, + lookup(parent, name) { + return parent.node_ops.lookup(parent, name); + }, + mknod(path, mode, dev) { + var lookup = FS.lookupPath(path, { + parent: true, + }); + var parent = lookup.node; + var name = PATH.basename(path); + if (!name) { + throw new FS.ErrnoError(28); + } + if (name === '.' || name === '..') { + throw new FS.ErrnoError(20); + } + var errCode = FS.mayCreate(parent, name); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.mknod) { + throw new FS.ErrnoError(63); + } + return parent.node_ops.mknod(parent, name, mode, dev); + }, + statfs(path) { + return FS.statfsNode( + FS.lookupPath(path, { + follow: true, + }).node + ); + }, + statfsStream(stream) { + // We keep a separate statfsStream function because noderawfs overrides + // it. In noderawfs, stream.node is sometimes null. Instead, we need to + // look at stream.path. + return FS.statfsNode(stream.node); + }, + statfsNode(node) { + // NOTE: None of the defaults here are true. We're just returning safe and + // sane values. Currently nodefs and rawfs replace these defaults, + // other file systems leave them alone. + var rtn = { + bsize: 4096, + frsize: 4096, + blocks: 1e6, + bfree: 5e5, + bavail: 5e5, + files: FS.nextInode, + ffree: FS.nextInode - 1, + fsid: 42, + flags: 2, + namelen: 255, + }; + if (node.node_ops.statfs) { + Object.assign(rtn, node.node_ops.statfs(node.mount.opts.root)); + } + return rtn; + }, + create(path, mode = 438) { + mode &= 4095; + mode |= 32768; + return FS.mknod(path, mode, 0); + }, + mkdir(path, mode = 511) { + mode &= 511 | 512; + mode |= 16384; + return FS.mknod(path, mode, 0); + }, + mkdirTree(path, mode) { + var dirs = path.split('/'); + var d = ''; + for (var dir of dirs) { + if (!dir) continue; + if (d || PATH.isAbs(path)) d += '/'; + d += dir; + try { + FS.mkdir(d, mode); + } catch (e) { + if (e.errno != 20) throw e; + } + } + }, + mkdev(path, mode, dev) { + if (typeof dev == 'undefined') { + dev = mode; + mode = 438; + } + mode |= 8192; + return FS.mknod(path, mode, dev); + }, + symlink(oldpath, newpath) { + if (!PATH_FS.resolve(oldpath)) { + throw new FS.ErrnoError(44); + } + var lookup = FS.lookupPath(newpath, { + parent: true, + }); + var parent = lookup.node; + if (!parent) { + throw new FS.ErrnoError(44); + } + var newname = PATH.basename(newpath); + var errCode = FS.mayCreate(parent, newname); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.symlink) { + throw new FS.ErrnoError(63); + } + return parent.node_ops.symlink(parent, newname, oldpath); + }, + rename(old_path, new_path) { + var old_dirname = PATH.dirname(old_path); + var new_dirname = PATH.dirname(new_path); + var old_name = PATH.basename(old_path); + var new_name = PATH.basename(new_path); + // parents must exist + var lookup, old_dir, new_dir; + // let the errors from non existent directories percolate up + lookup = FS.lookupPath(old_path, { + parent: true, + }); + old_dir = lookup.node; + lookup = FS.lookupPath(new_path, { + parent: true, + }); + new_dir = lookup.node; + if (!old_dir || !new_dir) throw new FS.ErrnoError(44); + // need to be part of the same mount + if (old_dir.mount !== new_dir.mount) { + throw new FS.ErrnoError(75); + } + // source must exist + var old_node = FS.lookupNode(old_dir, old_name); + // old path should not be an ancestor of the new path + var relative = PATH_FS.relative(old_path, new_dirname); + if (relative.charAt(0) !== '.') { + throw new FS.ErrnoError(28); + } + // new path should not be an ancestor of the old path + relative = PATH_FS.relative(new_path, old_dirname); + if (relative.charAt(0) !== '.') { + throw new FS.ErrnoError(55); + } + // see if the new path already exists + var new_node; + try { + new_node = FS.lookupNode(new_dir, new_name); + } catch (e) {} + // early out if nothing needs to change + if (old_node === new_node) { + return; + } + // we'll need to delete the old entry + var isdir = FS.isDir(old_node.mode); + var errCode = FS.mayDelete(old_dir, old_name, isdir); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + // need delete permissions if we'll be overwriting. + // need create permissions if new doesn't already exist. + errCode = new_node + ? FS.mayDelete(new_dir, new_name, isdir) + : FS.mayCreate(new_dir, new_name); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!old_dir.node_ops.rename) { + throw new FS.ErrnoError(63); + } + if ( + FS.isMountpoint(old_node) || + (new_node && FS.isMountpoint(new_node)) + ) { + throw new FS.ErrnoError(10); + } + // if we are going to change the parent, check write permissions + if (new_dir !== old_dir) { + errCode = FS.nodePermissions(old_dir, 'w'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + } + // remove the node from the lookup hash + FS.hashRemoveNode(old_node); + // do the underlying fs rename + try { + old_dir.node_ops.rename(old_node, new_dir, new_name); + // update old node (we do this here to avoid each backend + // needing to) + old_node.parent = new_dir; + } catch (e) { + throw e; + } finally { + // add the node back to the hash (in case node_ops.rename + // changed its name) + FS.hashAddNode(old_node); + } + }, + rmdir(path) { + var lookup = FS.lookupPath(path, { + parent: true, + }); + var parent = lookup.node; + var name = PATH.basename(path); + var node = FS.lookupNode(parent, name); + var errCode = FS.mayDelete(parent, name, true); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.rmdir) { + throw new FS.ErrnoError(63); + } + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + parent.node_ops.rmdir(parent, name); + FS.destroyNode(node); + }, + readdir(path) { + var lookup = FS.lookupPath(path, { + follow: true, + }); + var node = lookup.node; + var readdir = FS.checkOpExists(node.node_ops.readdir, 54); + return readdir(node); + }, + unlink(path) { + var lookup = FS.lookupPath(path, { + parent: true, + }); + var parent = lookup.node; + if (!parent) { + throw new FS.ErrnoError(44); + } + var name = PATH.basename(path); + var node = FS.lookupNode(parent, name); + var errCode = FS.mayDelete(parent, name, false); + if (errCode) { + // According to POSIX, we should map EISDIR to EPERM, but + // we instead do what Linux does (and we must, as we use + // the musl linux libc). + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.unlink) { + throw new FS.ErrnoError(63); + } + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + parent.node_ops.unlink(parent, name); + FS.destroyNode(node); + }, + readlink(path) { + var lookup = FS.lookupPath(path); + var link = lookup.node; + if (!link) { + throw new FS.ErrnoError(44); + } + if (!link.node_ops.readlink) { + throw new FS.ErrnoError(28); + } + return link.node_ops.readlink(link); + }, + stat(path, dontFollow) { + var lookup = FS.lookupPath(path, { + follow: !dontFollow, + }); + var node = lookup.node; + var getattr = FS.checkOpExists(node.node_ops.getattr, 63); + return getattr(node); + }, + fstat(fd) { + var stream = FS.getStreamChecked(fd); + var node = stream.node; + var getattr = stream.stream_ops.getattr; + var arg = getattr ? stream : node; + getattr ??= node.node_ops.getattr; + FS.checkOpExists(getattr, 63); + return getattr(arg); + }, + lstat(path) { + return FS.stat(path, true); + }, + doChmod(stream, node, mode, dontFollow) { + FS.doSetAttr(stream, node, { + mode: (mode & 4095) | (node.mode & ~4095), + ctime: Date.now(), + dontFollow, + }); + }, + chmod(path, mode, dontFollow) { + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { + follow: !dontFollow, + }); + node = lookup.node; + } else { + node = path; + } + FS.doChmod(null, node, mode, dontFollow); + }, + lchmod(path, mode) { + FS.chmod(path, mode, true); + }, + fchmod(fd, mode) { + var stream = FS.getStreamChecked(fd); + FS.doChmod(stream, stream.node, mode, false); + }, + doChown(stream, node, dontFollow) { + FS.doSetAttr(stream, node, { + timestamp: Date.now(), + dontFollow, + }); + }, + chown(path, uid, gid, dontFollow) { + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { + follow: !dontFollow, + }); + node = lookup.node; + } else { + node = path; + } + FS.doChown(null, node, dontFollow); + }, + lchown(path, uid, gid) { + FS.chown(path, uid, gid, true); + }, + fchown(fd, uid, gid) { + var stream = FS.getStreamChecked(fd); + FS.doChown(stream, stream.node, false); + }, + doTruncate(stream, node, len) { + if (FS.isDir(node.mode)) { + throw new FS.ErrnoError(31); + } + if (!FS.isFile(node.mode)) { + throw new FS.ErrnoError(28); + } + var errCode = FS.nodePermissions(node, 'w'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + FS.doSetAttr(stream, node, { + size: len, + timestamp: Date.now(), + }); + }, + truncate(path, len) { + if (len < 0) { + throw new FS.ErrnoError(28); + } + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { + follow: true, + }); + node = lookup.node; + } else { + node = path; + } + FS.doTruncate(null, node, len); + }, + ftruncate(fd, len) { + var stream = FS.getStreamChecked(fd); + if (len < 0 || (stream.flags & 2097155) === 0) { + throw new FS.ErrnoError(28); + } + FS.doTruncate(stream, stream.node, len); + }, + utime(path, atime, mtime) { + var lookup = FS.lookupPath(path, { + follow: true, + }); + var node = lookup.node; + var setattr = FS.checkOpExists(node.node_ops.setattr, 63); + setattr(node, { + atime, + mtime, + }); + }, + open(path, flags, mode = 438) { + if (path === '') { + throw new FS.ErrnoError(44); + } + flags = + typeof flags == 'string' ? FS_modeStringToFlags(flags) : flags; + if (flags & 64) { + mode = (mode & 4095) | 32768; + } else { + mode = 0; + } + var node; + var isDirPath; + if (typeof path == 'object') { + node = path; + } else { + isDirPath = path.endsWith('/'); + // noent_okay makes it so that if the final component of the path + // doesn't exist, lookupPath returns `node: undefined`. `path` will be + // updated to point to the target of all symlinks. + var lookup = FS.lookupPath(path, { + follow: !(flags & 131072), + noent_okay: true, + }); + node = lookup.node; + path = lookup.path; + } + // perhaps we need to create the node + var created = false; + if (flags & 64) { + if (node) { + // if O_CREAT and O_EXCL are set, error out if the node already exists + if (flags & 128) { + throw new FS.ErrnoError(20); + } + } else if (isDirPath) { + throw new FS.ErrnoError(31); + } else { + // node doesn't exist, try to create it + // Ignore the permission bits here to ensure we can `open` this new + // file below. We use chmod below the apply the permissions once the + // file is open. + node = FS.mknod(path, mode | 511, 0); + created = true; + } + } + if (!node) { + throw new FS.ErrnoError(44); + } + // can't truncate a device + if (FS.isChrdev(node.mode)) { + flags &= ~512; + } + // if asked only for a directory, then this must be one + if (flags & 65536 && !FS.isDir(node.mode)) { + throw new FS.ErrnoError(54); + } + // check permissions, if this is not a file we just created now (it is ok to + // create and write to a file with read-only permissions; it is read-only + // for later use) + if (!created) { + var errCode = FS.mayOpen(node, flags); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + } + // do truncation if necessary + if (flags & 512 && !created) { + FS.truncate(node, 0); + } + // we've already handled these, don't pass down to the underlying vfs + flags &= ~(128 | 512 | 131072); + // register the stream with the filesystem + var stream = FS.createStream({ + node, + path: FS.getPath(node), + // we want the absolute path to the node + flags, + seekable: true, + position: 0, + stream_ops: node.stream_ops, + // used by the file family libc calls (fopen, fwrite, ferror, etc.) + ungotten: [], + error: false, + }); + // call the new stream's open function + if (stream.stream_ops.open) { + stream.stream_ops.open(stream); + } + if (created) { + FS.chmod(node, mode & 511); + } + if (Module['logReadFiles'] && !(flags & 1)) { + if (!(path in FS.readFiles)) { + FS.readFiles[path] = 1; + } + } + return stream; + }, + close(stream) { + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if (stream.getdents) stream.getdents = null; + // free readdir state + try { + if (stream.stream_ops.close) { + stream.stream_ops.close(stream); + } + } catch (e) { + throw e; + } finally { + FS.closeStream(stream.fd); + } + stream.fd = null; + }, + isClosed(stream) { + return stream.fd === null; + }, + llseek(stream, offset, whence) { + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if (!stream.seekable || !stream.stream_ops.llseek) { + throw new FS.ErrnoError(70); + } + if (whence != 0 && whence != 1 && whence != 2) { + throw new FS.ErrnoError(28); + } + stream.position = stream.stream_ops.llseek(stream, offset, whence); + stream.ungotten = []; + return stream.position; + }, + read(stream, buffer, offset, length, position) { + if (length < 0 || position < 0) { + throw new FS.ErrnoError(28); + } + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if ((stream.flags & 2097155) === 1) { + throw new FS.ErrnoError(8); + } + if (FS.isDir(stream.node.mode)) { + throw new FS.ErrnoError(31); + } + if (!stream.stream_ops.read) { + throw new FS.ErrnoError(28); + } + var seeking = typeof position != 'undefined'; + if (!seeking) { + position = stream.position; + } else if (!stream.seekable) { + throw new FS.ErrnoError(70); + } + var bytesRead = stream.stream_ops.read( + stream, + buffer, + offset, + length, + position + ); + if (!seeking) stream.position += bytesRead; + return bytesRead; + }, + write(stream, buffer, offset, length, position, canOwn) { + if (length < 0 || position < 0) { + throw new FS.ErrnoError(28); + } + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if ((stream.flags & 2097155) === 0) { + throw new FS.ErrnoError(8); + } + if (FS.isDir(stream.node.mode)) { + throw new FS.ErrnoError(31); + } + if (!stream.stream_ops.write) { + throw new FS.ErrnoError(28); + } + if (stream.seekable && stream.flags & 1024) { + // seek to the end before writing in append mode + FS.llseek(stream, 0, 2); + } + var seeking = typeof position != 'undefined'; + if (!seeking) { + position = stream.position; + } else if (!stream.seekable) { + throw new FS.ErrnoError(70); + } + var bytesWritten = stream.stream_ops.write( + stream, + buffer, + offset, + length, + position, + canOwn + ); + if (!seeking) stream.position += bytesWritten; + return bytesWritten; + }, + mmap(stream, length, position, prot, flags) { + // User requests writing to file (prot & PROT_WRITE != 0). + // Checking if we have permissions to write to the file unless + // MAP_PRIVATE flag is set. According to POSIX spec it is possible + // to write to file opened in read-only mode with MAP_PRIVATE flag, + // as all modifications will be visible only in the memory of + // the current process. + if ( + (prot & 2) !== 0 && + (flags & 2) === 0 && + (stream.flags & 2097155) !== 2 + ) { + throw new FS.ErrnoError(2); + } + if ((stream.flags & 2097155) === 1) { + throw new FS.ErrnoError(2); + } + if (!stream.stream_ops.mmap) { + throw new FS.ErrnoError(43); + } + if (!length) { + throw new FS.ErrnoError(28); + } + return stream.stream_ops.mmap( + stream, + length, + position, + prot, + flags + ); + }, + msync(stream, buffer, offset, length, mmapFlags) { + if (!stream.stream_ops.msync) { + return 0; + } + return stream.stream_ops.msync( + stream, + buffer, + offset, + length, + mmapFlags + ); + }, + ioctl(stream, cmd, arg) { + if (!stream.stream_ops.ioctl) { + throw new FS.ErrnoError(59); + } + return stream.stream_ops.ioctl(stream, cmd, arg); + }, + readFile(path, opts = {}) { + opts.flags = opts.flags || 0; + opts.encoding = opts.encoding || 'binary'; + if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') { + abort(`Invalid encoding type "${opts.encoding}"`); + } + var stream = FS.open(path, opts.flags); + var stat = FS.stat(path); + var length = stat.size; + var buf = new Uint8Array(length); + FS.read(stream, buf, 0, length, 0); + if (opts.encoding === 'utf8') { + buf = UTF8ArrayToString(buf); + } + FS.close(stream); + return buf; + }, + writeFile(path, data, opts = {}) { + opts.flags = opts.flags || 577; + var stream = FS.open(path, opts.flags, opts.mode); + if (typeof data == 'string') { + data = new Uint8Array(intArrayFromString(data, true)); + } + if (ArrayBuffer.isView(data)) { + FS.write( + stream, + data, + 0, + data.byteLength, + undefined, + opts.canOwn + ); + } else { + abort('Unsupported data type'); + } + FS.close(stream); + }, + cwd: () => FS.currentPath, + chdir(path) { + var lookup = FS.lookupPath(path, { + follow: true, + }); + if (lookup.node === null) { + throw new FS.ErrnoError(44); + } + if (!FS.isDir(lookup.node.mode)) { + throw new FS.ErrnoError(54); + } + var errCode = FS.nodePermissions(lookup.node, 'x'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + FS.currentPath = lookup.path; + }, + createDefaultDirectories() { + FS.mkdir('/tmp'); + FS.mkdir('/home'); + FS.mkdir('/home/web_user'); + }, + createDefaultDevices() { + // create /dev + FS.mkdir('/dev'); + // setup /dev/null + FS.registerDevice(FS.makedev(1, 3), { + read: () => 0, + write: (stream, buffer, offset, length, pos) => length, + llseek: () => 0, + }); + FS.mkdev('/dev/null', FS.makedev(1, 3)); + // setup /dev/tty and /dev/tty1 + // stderr needs to print output using err() rather than out() + // so we register a second tty just for it. + TTY.register(FS.makedev(5, 0), TTY.default_tty_ops); + TTY.register(FS.makedev(6, 0), TTY.default_tty1_ops); + FS.mkdev('/dev/tty', FS.makedev(5, 0)); + FS.mkdev('/dev/tty1', FS.makedev(6, 0)); + // setup /dev/[u]random + // use a buffer to avoid overhead of individual crypto calls per byte + var randomBuffer = new Uint8Array(1024), + randomLeft = 0; + var randomByte = () => { + if (randomLeft === 0) { + randomFill(randomBuffer); + randomLeft = randomBuffer.byteLength; + } + return randomBuffer[--randomLeft]; + }; + FS.createDevice('/dev', 'random', randomByte); + FS.createDevice('/dev', 'urandom', randomByte); + // we're not going to emulate the actual shm device, + // just create the tmp dirs that reside in it commonly + FS.mkdir('/dev/shm'); + FS.mkdir('/dev/shm/tmp'); + }, + createSpecialDirectories() { + // create /proc/self/fd which allows /proc/self/fd/6 => readlink gives the + // name of the stream for fd 6 (see test_unistd_ttyname) + FS.mkdir('/proc'); + var proc_self = FS.mkdir('/proc/self'); + FS.mkdir('/proc/self/fd'); + FS.mount( + { + mount() { + var node = FS.createNode(proc_self, 'fd', 16895, 73); + node.stream_ops = { + llseek: MEMFS.stream_ops.llseek, + }; + node.node_ops = { + lookup(parent, name) { + var fd = +name; + var stream = FS.getStreamChecked(fd); + var ret = { + parent: null, + mount: { + mountpoint: 'fake', + }, + node_ops: { + readlink: () => stream.path, + }, + id: fd + 1, + }; + ret.parent = ret; + // make it look like a simple root node + return ret; + }, + readdir() { + return Array.from(FS.streams.entries()) + .filter(([k, v]) => v) + .map(([k, v]) => k.toString()); + }, + }; + return node; + }, + }, + {}, + '/proc/self/fd' + ); + }, + createStandardStreams(input, output, error) { + // TODO deprecate the old functionality of a single + // input / output callback and that utilizes FS.createDevice + // and instead require a unique set of stream ops + // by default, we symlink the standard streams to the + // default tty devices. however, if the standard streams + // have been overwritten we create a unique device for + // them instead. + if (input) { + FS.createDevice('/dev', 'stdin', input); + } else { + FS.symlink('/dev/tty', '/dev/stdin'); + } + if (output) { + FS.createDevice('/dev', 'stdout', null, output); + } else { + FS.symlink('/dev/tty', '/dev/stdout'); + } + if (error) { + FS.createDevice('/dev', 'stderr', null, error); + } else { + FS.symlink('/dev/tty1', '/dev/stderr'); + } + // open default streams for the stdin, stdout and stderr devices + var stdin = FS.open('/dev/stdin', 0); + var stdout = FS.open('/dev/stdout', 1); + var stderr = FS.open('/dev/stderr', 1); + }, + staticInit() { + FS.nameTable = new Array(4096); + FS.mount(MEMFS, {}, '/'); + FS.createDefaultDirectories(); + FS.createDefaultDevices(); + FS.createSpecialDirectories(); + FS.filesystems = { + MEMFS: MEMFS, + NODEFS: NODEFS, + PROXYFS: PROXYFS, + }; + }, + init(input, output, error) { + FS.initialized = true; + // Allow Module.stdin etc. to provide defaults, if none explicitly passed to us here + input ??= Module['stdin']; + output ??= Module['stdout']; + error ??= Module['stderr']; + FS.createStandardStreams(input, output, error); + }, + quit() { + FS.initialized = false; + // force-flush all streams, so we get musl std streams printed out + _fflush(0); + // close all of our streams + for (var stream of FS.streams) { + if (stream) { + FS.close(stream); + } + } + }, + findObject(path, dontResolveLastLink) { + var ret = FS.analyzePath(path, dontResolveLastLink); + if (!ret.exists) { + return null; + } + return ret.object; + }, + analyzePath(path, dontResolveLastLink) { + // operate from within the context of the symlink's target + try { + var lookup = FS.lookupPath(path, { + follow: !dontResolveLastLink, + }); + path = lookup.path; + } catch (e) {} + var ret = { + isRoot: false, + exists: false, + error: 0, + name: null, + path: null, + object: null, + parentExists: false, + parentPath: null, + parentObject: null, + }; + try { + var lookup = FS.lookupPath(path, { + parent: true, + }); + ret.parentExists = true; + ret.parentPath = lookup.path; + ret.parentObject = lookup.node; + ret.name = PATH.basename(path); + lookup = FS.lookupPath(path, { + follow: !dontResolveLastLink, + }); + ret.exists = true; + ret.path = lookup.path; + ret.object = lookup.node; + ret.name = lookup.node.name; + ret.isRoot = lookup.path === '/'; + } catch (e) { + ret.error = e.errno; + } + return ret; + }, + createPath(parent, path, canRead, canWrite) { + parent = typeof parent == 'string' ? parent : FS.getPath(parent); + var parts = path.split('/').reverse(); + while (parts.length) { + var part = parts.pop(); + if (!part) continue; + var current = PATH.join2(parent, part); + try { + FS.mkdir(current); + } catch (e) { + if (e.errno != 20) throw e; + } + parent = current; + } + return current; + }, + createFile(parent, name, properties, canRead, canWrite) { + var path = PATH.join2( + typeof parent == 'string' ? parent : FS.getPath(parent), + name + ); + var mode = FS_getMode(canRead, canWrite); + return FS.create(path, mode); + }, + createDataFile(parent, name, data, canRead, canWrite, canOwn) { + var path = name; + if (parent) { + parent = + typeof parent == 'string' ? parent : FS.getPath(parent); + path = name ? PATH.join2(parent, name) : parent; + } + var mode = FS_getMode(canRead, canWrite); + var node = FS.create(path, mode); + if (data) { + if (typeof data == 'string') { + var arr = new Array(data.length); + for (var i = 0, len = data.length; i < len; ++i) + arr[i] = data.charCodeAt(i); + data = arr; + } + // make sure we can write to the file + FS.chmod(node, mode | 146); + var stream = FS.open(node, 577); + FS.write(stream, data, 0, data.length, 0, canOwn); + FS.close(stream); + FS.chmod(node, mode); + } + }, + createDevice(parent, name, input, output) { + var path = PATH.join2( + typeof parent == 'string' ? parent : FS.getPath(parent), + name + ); + var mode = FS_getMode(!!input, !!output); + FS.createDevice.major ??= 64; + var dev = FS.makedev(FS.createDevice.major++, 0); + // Create a fake device that a set of stream ops to emulate + // the old behavior. + FS.registerDevice(dev, { + open(stream) { + stream.seekable = false; + }, + close(stream) { + // flush any pending line data + if (output?.buffer?.length) { + output(10); + } + }, + read(stream, buffer, offset, length, pos) { + var bytesRead = 0; + for (var i = 0; i < length; i++) { + var result; + try { + result = input(); + } catch (e) { + throw new FS.ErrnoError(29); + } + if (result === undefined && bytesRead === 0) { + throw new FS.ErrnoError(6); + } + if (result === null || result === undefined) break; + bytesRead++; + buffer[offset + i] = result; + } + if (bytesRead) { + stream.node.atime = Date.now(); + } + return bytesRead; + }, + write(stream, buffer, offset, length, pos) { + for (var i = 0; i < length; i++) { + try { + output(buffer[offset + i]); + } catch (e) { + throw new FS.ErrnoError(29); + } + } + if (length) { + stream.node.mtime = stream.node.ctime = Date.now(); + } + return i; + }, + }); + return FS.mkdev(path, mode, dev); + }, + forceLoadFile(obj) { + if (obj.isDevice || obj.isFolder || obj.link || obj.contents) + return true; + if (globalThis.XMLHttpRequest) { + abort( + 'Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.' + ); + } else { + // Command-line. + try { + obj.contents = readBinary(obj.url); + } catch (e) { + throw new FS.ErrnoError(29); + } + } + }, + createLazyFile(parent, name, url, canRead, canWrite) { + // Lazy chunked Uint8Array (implements get and length from Uint8Array). + // Actual getting is abstracted away for eventual reuse. + class LazyUint8Array { + lengthKnown = false; + chunks = []; + // Loaded chunks. Index is the chunk number + get(idx) { + if (idx > this.length - 1 || idx < 0) { + return undefined; + } + var chunkOffset = idx % this.chunkSize; + var chunkNum = (idx / this.chunkSize) | 0; + return this.getter(chunkNum)[chunkOffset]; + } + setDataGetter(getter) { + this.getter = getter; + } + cacheLength() { + // Find length + var xhr = new XMLHttpRequest(); + xhr.open('HEAD', url, false); + xhr.send(null); + if ( + !( + (xhr.status >= 200 && xhr.status < 300) || + xhr.status === 304 + ) + ) + abort( + "Couldn't load " + url + '. Status: ' + xhr.status + ); + var datalength = Number( + xhr.getResponseHeader('Content-length') + ); + var header; + var hasByteServing = + (header = xhr.getResponseHeader('Accept-Ranges')) && + header === 'bytes'; + var usesGzip = + (header = xhr.getResponseHeader('Content-Encoding')) && + header === 'gzip'; + var chunkSize = 1024 * 1024; + // Chunk size in bytes + if (!hasByteServing) chunkSize = datalength; + // Function to get a range from the remote URL. + var doXHR = (from, to) => { + if (from > to) + abort( + 'invalid range (' + + from + + ', ' + + to + + ') or no bytes requested!' + ); + if (to > datalength - 1) + abort( + 'only ' + + datalength + + ' bytes available! programmer error!' + ); + // TODO: Use mozResponseArrayBuffer, responseStream, etc. if available. + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, false); + if (datalength !== chunkSize) + xhr.setRequestHeader( + 'Range', + 'bytes=' + from + '-' + to + ); + // Some hints to the browser that we want binary data. + xhr.responseType = 'arraybuffer'; + if (xhr.overrideMimeType) { + xhr.overrideMimeType( + 'text/plain; charset=x-user-defined' + ); + } + xhr.send(null); + if ( + !( + (xhr.status >= 200 && xhr.status < 300) || + xhr.status === 304 + ) + ) + abort( + "Couldn't load " + + url + + '. Status: ' + + xhr.status + ); + if (xhr.response !== undefined) { + return new Uint8Array( + /** @type{Array} */ (xhr.response || []) + ); + } + return intArrayFromString(xhr.responseText || '', true); + }; + var lazyArray = this; + lazyArray.setDataGetter((chunkNum) => { + var start = chunkNum * chunkSize; + var end = (chunkNum + 1) * chunkSize - 1; + // including this byte + end = Math.min(end, datalength - 1); + // if datalength-1 is selected, this is the last block + if (typeof lazyArray.chunks[chunkNum] == 'undefined') { + lazyArray.chunks[chunkNum] = doXHR(start, end); + } + if (typeof lazyArray.chunks[chunkNum] == 'undefined') + abort('doXHR failed!'); + return lazyArray.chunks[chunkNum]; + }); + if (usesGzip || !datalength) { + // if the server uses gzip or doesn't supply the length, we have to download the whole file to get the (uncompressed) length + chunkSize = datalength = 1; + // this will force getter(0)/doXHR do download the whole file + datalength = this.getter(0).length; + chunkSize = datalength; + out( + 'LazyFiles on gzip forces download of the whole file when length is accessed' + ); + } + this._length = datalength; + this._chunkSize = chunkSize; + this.lengthKnown = true; + } + get length() { + if (!this.lengthKnown) { + this.cacheLength(); + } + return this._length; + } + get chunkSize() { + if (!this.lengthKnown) { + this.cacheLength(); + } + return this._chunkSize; + } + } + if (globalThis.XMLHttpRequest) { + if (!ENVIRONMENT_IS_WORKER) + abort( + 'Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc' + ); + var lazyArray = new LazyUint8Array(); + var properties = { + isDevice: false, + contents: lazyArray, + }; + } else { + var properties = { + isDevice: false, + url, + }; + } + var node = FS.createFile( + parent, + name, + properties, + canRead, + canWrite + ); + // This is a total hack, but I want to get this lazy file code out of the + // core of MEMFS. If we want to keep this lazy file concept I feel it should + // be its own thin LAZYFS proxying calls to MEMFS. + if (properties.contents) { + node.contents = properties.contents; + } else if (properties.url) { + node.contents = null; + node.url = properties.url; + } + // Add a function that defers querying the file size until it is asked the first time. + Object.defineProperties(node, { + usedBytes: { + get: function () { + return this.contents.length; + }, + }, + }); + // override each stream op with one that tries to force load the lazy file first + var stream_ops = {}; + for (const [key, fn] of Object.entries(node.stream_ops)) { + stream_ops[key] = (...args) => { + FS.forceLoadFile(node); + return fn(...args); + }; + } + function writeChunks(stream, buffer, offset, length, position) { + var contents = stream.node.contents; + if (position >= contents.length) return 0; + var size = Math.min(contents.length - position, length); + if (contents.slice) { + // normal array + for (var i = 0; i < size; i++) { + buffer[offset + i] = contents[position + i]; + } + } else { + for (var i = 0; i < size; i++) { + // LazyUint8Array from sync binary XHR + buffer[offset + i] = contents.get(position + i); + } + } + return size; + } + // use a custom read function + stream_ops.read = (stream, buffer, offset, length, position) => { + FS.forceLoadFile(node); + return writeChunks(stream, buffer, offset, length, position); + }; + // use a custom mmap function + stream_ops.mmap = (stream, length, position, prot, flags) => { + FS.forceLoadFile(node); + var ptr = mmapAlloc(length); + if (!ptr) { + throw new FS.ErrnoError(48); + } + writeChunks(stream, HEAP8, ptr, length, position); + return { + ptr, + allocated: true, + }; + }; + node.stream_ops = stream_ops; + return node; + }, + }; + + var findLibraryFS = (libName, rpath) => { + // If we're preloading a dynamic library, the runtime is not ready to call + // __wasmfs_identify or __emscripten_find_dylib. So just quit out. + // This means that DT_NEEDED for the main module and transitive dependencies + // of it won't work with this code path. Similarly, it means that calling + // loadDynamicLibrary in a preRun hook can't use this code path. + if (!runtimeInitialized) { + return undefined; + } + if (PATH.isAbs(libName)) { + try { + FS.lookupPath(libName); + return libName; + } catch (e) { + return undefined; + } + } + var rpathResolved = (rpath?.paths || []).map((p) => + replaceORIGIN(rpath?.parentLibPath, p) + ); + return withStackSave(() => { + // In dylink.c we use: `char buf[2*NAME_MAX+2];` and NAME_MAX is 255. + // So we use the same size here. + var bufSize = 2 * 255 + 2; + var buf = stackAlloc(bufSize); + var rpathC = stringToUTF8OnStack(rpathResolved.join(':')); + var libNameC = stringToUTF8OnStack(libName); + var resLibNameC = __emscripten_find_dylib( + buf, + rpathC, + libNameC, + bufSize + ); + return resLibNameC ? UTF8ToString(resLibNameC) : undefined; + }); + }; + + var registerDynCallSymbols = (exports) => { + for (var [sym, exp] of Object.entries(exports)) { + if (sym.startsWith('dynCall_')) { + var sig = sym.substring(8); + if (!dynCalls.hasOwnProperty(sig)) { + dynCalls[sig] = exp; + } + } + } + }; + + /** + * @param {number=} handle + * @param {Object=} localScope + */ function loadDynamicLibrary( + libName, + flags = { + global: true, + nodelete: true, + }, + localScope, + handle + ) { + // when loadDynamicLibrary did not have flags, libraries were loaded + // globally & permanently + var dso = LDSO.loadedLibsByName[libName]; + if (dso) { + // the library is being loaded or has been loaded already. + if (!flags.global) { + if (localScope) { + Object.assign(localScope, dso.exports); + } + registerDynCallSymbols(dso.exports); + } else if (!dso.global) { + // The library was previously loaded only locally but not + // we have a request with global=true. + dso.global = true; + mergeLibSymbols(dso.exports, libName); + } + // same for "nodelete" + if (flags.nodelete && dso.refcount !== Infinity) { + dso.refcount = Infinity; + } + dso.refcount++; + if (handle) { + LDSO.loadedLibsByHandle[handle] = dso; + } + return flags.loadAsync ? Promise.resolve(true) : true; + } + // allocate new DSO + dso = newDSO(libName, handle, 'loading'); + dso.refcount = flags.nodelete ? Infinity : 1; + dso.global = flags.global; + // libName -> libData + function loadLibData() { + // for wasm, we can use fetch for async, but for fs mode we can only imitate it + if (handle) { + var data = HEAPU32[(handle + 28) >> 2]; + var dataSize = HEAPU32[(handle + 32) >> 2]; + if (data && dataSize) { + var libData = HEAP8.slice(data, data + dataSize); + return flags.loadAsync ? Promise.resolve(libData) : libData; + } + } + var f = findLibraryFS(libName, flags.rpath); + if (f) { + var libData = FS.readFile(f, { + encoding: 'binary', + }); + return flags.loadAsync ? Promise.resolve(libData) : libData; + } + var libFile = locateFile(libName); + if (flags.loadAsync) { + return asyncLoad(libFile); + } + // load the binary synchronously + if (!readBinary) { + throw new Error( + `${libFile}: file not found, and synchronous loading of external files is not available` + ); + } + return readBinary(libFile); + } + // libName -> exports + function getExports() { + // lookup preloaded cache first + var preloaded = preloadedWasm[libName]; + if (preloaded) { + return flags.loadAsync ? Promise.resolve(preloaded) : preloaded; + } + // module not preloaded - load lib data and create new module from it + if (flags.loadAsync) { + return loadLibData().then((libData) => + loadWebAssemblyModule( + libData, + flags, + libName, + localScope, + handle + ) + ); + } + return loadWebAssemblyModule( + loadLibData(), + flags, + libName, + localScope, + handle + ); + } + // module for lib is loaded - update the dso & global namespace + function moduleLoaded(exports) { + if (dso.global) { + mergeLibSymbols(exports, libName); + } else if (localScope) { + Object.assign(localScope, exports); + registerDynCallSymbols(exports); + } + dso.exports = exports; + } + if (flags.loadAsync) { + return getExports().then((exports) => { + moduleLoaded(exports); + return true; + }); + } + moduleLoaded(getExports()); + return true; + } + + var reportUndefinedSymbols = () => { + for (var [symName, entry] of Object.entries(GOT)) { + if (entry.value == -1) { + var value = resolveGlobalSymbol(symName, true).sym; + if (!value && !entry.required) { + // Ignore undefined symbols that are imported as weak. + entry.value = 0; + continue; + } + if (typeof value == 'function') { + /** @suppress {checkTypes} */ entry.value = addFunction( + value, + value.sig + ); + } else if (typeof value == 'number') { + entry.value = value; + } else { + throw new Error( + `bad export type for '${symName}': ${typeof value} (${value})` + ); + } + } + } + }; + + var loadDylibs = async () => { + if (!dynamicLibraries.length) { + reportUndefinedSymbols(); + return; + } + addRunDependency('loadDylibs'); + // Load binaries asynchronously + for (var lib of dynamicLibraries) { + await loadDynamicLibrary(lib, { + loadAsync: true, + global: true, + nodelete: true, + allowUndefined: true, + }); + } + // we got them all, wonderful + reportUndefinedSymbols(); + removeRunDependency('loadDylibs'); + }; + + var noExitRuntime = false; + + var ___assert_fail = (condition, filename, line, func) => + abort( + `Assertion failed: ${UTF8ToString(condition)}, at: ` + + [ + filename ? UTF8ToString(filename) : 'unknown filename', + line, + func ? UTF8ToString(func) : 'unknown function', + ] + ); + + ___assert_fail.sig = 'vppip'; + + var ___asyncify_data = new WebAssembly.Global( + { + value: 'i32', + mutable: true, + }, + 0 + ); + + var ___asyncify_state = new WebAssembly.Global( + { + value: 'i32', + mutable: true, + }, + 0 + ); + + var ___call_sighandler = (fp, sig) => ((a1) => dynCall_vi(fp, a1))(sig); + + ___call_sighandler.sig = 'vpi'; + + var SOCKFS = { + websocketArgs: {}, + callbacks: {}, + on(event, callback) { + SOCKFS.callbacks[event] = callback; + }, + emit(event, param) { + SOCKFS.callbacks[event]?.(param); + }, + mount(mount) { + // The incomming Module['websocket'] can be used for configuring + // configuring subprotocol/url, etc + SOCKFS.websocketArgs = Module['websocket'] || {}; + // Add the Event registration mechanism to the exported websocket configuration + // object so we can register network callbacks from native JavaScript too. + // For more documentation see system/include/emscripten/emscripten.h + (Module['websocket'] ??= {})['on'] = SOCKFS.on; + return FS.createNode(null, '/', 16895, 0); + }, + createSocket(family, type, protocol) { + // Emscripten only supports AF_INET + if (family != 2) { + throw new FS.ErrnoError(5); + } + type &= ~526336; + // Some applications may pass it; it makes no sense for a single process. + // Emscripten only supports SOCK_STREAM and SOCK_DGRAM + if (type != 1 && type != 2) { + throw new FS.ErrnoError(28); + } + var streaming = type == 1; + if (streaming && protocol && protocol != 6) { + throw new FS.ErrnoError(66); + } + // create our internal socket structure + var sock = { + family, + type, + protocol, + server: null, + error: null, + // Used in getsockopt for SOL_SOCKET/SO_ERROR test + peers: {}, + pending: [], + recv_queue: [], + sock_ops: SOCKFS.websocket_sock_ops, + }; + // create the filesystem node to store the socket structure + var name = SOCKFS.nextname(); + var node = FS.createNode(SOCKFS.root, name, 49152, 0); + node.sock = sock; + // and the wrapping stream that enables library functions such + // as read and write to indirectly interact with the socket + var stream = FS.createStream({ + path: name, + node, + flags: 2, + seekable: false, + stream_ops: SOCKFS.stream_ops, + }); + // map the new stream to the socket structure (sockets have a 1:1 + // relationship with a stream) + sock.stream = stream; + return sock; + }, + getSocket(fd) { + var stream = FS.getStream(fd); + if (!stream || !FS.isSocket(stream.node.mode)) { + return null; + } + return stream.node.sock; + }, + stream_ops: { + poll(stream) { + var sock = stream.node.sock; + return sock.sock_ops.poll(sock); + }, + ioctl(stream, request, varargs) { + var sock = stream.node.sock; + return sock.sock_ops.ioctl(sock, request, varargs); + }, + read(stream, buffer, offset, length, position) { + var sock = stream.node.sock; + var msg = sock.sock_ops.recvmsg(sock, length); + if (!msg) { + // socket is closed + return 0; + } + buffer.set(msg.buffer, offset); + return msg.buffer.length; + }, + write(stream, buffer, offset, length, position) { + var sock = stream.node.sock; + return sock.sock_ops.sendmsg(sock, buffer, offset, length); + }, + close(stream) { + var sock = stream.node.sock; + sock.sock_ops.close(sock); + }, + }, + nextname() { + if (!SOCKFS.nextname.current) { + SOCKFS.nextname.current = 0; + } + return `socket[${SOCKFS.nextname.current++}]`; + }, + websocket_sock_ops: { + createPeer(sock, addr, port) { + var ws; + if (typeof addr == 'object') { + ws = addr; + addr = null; + port = null; + } + if (ws) { + // for sockets that've already connected (e.g. we're the server) + // we can inspect the _socket property for the address + if (ws._socket) { + addr = ws._socket.remoteAddress; + port = ws._socket.remotePort; + } else { + var result = /ws[s]?:\/\/([^:]+):(\d+)/.exec(ws.url); + if (!result) { + throw new Error( + 'WebSocket URL must be in the format ws(s)://address:port' + ); + } + addr = result[1]; + port = parseInt(result[2], 10); + } + } else { + // create the actual websocket object and connect + try { + // The default value is 'ws://' the replace is needed because the compiler replaces '//' comments with '#' + // comments without checking context, so we'd end up with ws:#, the replace swaps the '#' for '//' again. + var url = 'ws://'.replace('#', '//'); + // Make the WebSocket subprotocol (Sec-WebSocket-Protocol) default to binary if no configuration is set. + var subProtocols = 'binary'; + // The default value is 'binary' + // The default WebSocket options + var opts = undefined; + // Fetch runtime WebSocket URL config. + if ('function' === typeof SOCKFS.websocketArgs['url']) { + url = SOCKFS.websocketArgs['url'](...arguments); + } else if ( + 'string' === typeof SOCKFS.websocketArgs['url'] + ) { + url = SOCKFS.websocketArgs['url']; + } + // Fetch runtime WebSocket subprotocol config. + if (SOCKFS.websocketArgs['subprotocol']) { + subProtocols = SOCKFS.websocketArgs['subprotocol']; + } else if ( + SOCKFS.websocketArgs['subprotocol'] === null + ) { + subProtocols = 'null'; + } + if (url === 'ws://' || url === 'wss://') { + // Is the supplied URL config just a prefix, if so complete it. + var parts = addr.split('/'); + url = + url + + parts[0] + + ':' + + port + + '/' + + parts.slice(1).join('/'); + } + if (subProtocols !== 'null') { + // The regex trims the string (removes spaces at the beginning and end, then splits the string by + // , into an Array. Whitespace removal is important for Websockify and ws. + subProtocols = subProtocols + .replace(/^ +| +$/g, '') + .split(/ *, */); + opts = subProtocols; + } + // If node we use the ws library. + var WebSocketConstructor; + if (ENVIRONMENT_IS_NODE) { + WebSocketConstructor = + /** @type{(typeof WebSocket)} */ ( + require('ws') + ); + } else { + WebSocketConstructor = WebSocket; + } + if (Module['websocket']['decorator']) { + WebSocketConstructor = + Module['websocket']['decorator']( + WebSocketConstructor + ); + } + ws = new WebSocketConstructor(url, opts); + ws.binaryType = 'arraybuffer'; + } catch (e) { + throw new FS.ErrnoError(23); + } + } + var peer = { + addr, + port, + socket: ws, + msg_send_queue: [], + }; + SOCKFS.websocket_sock_ops.addPeer(sock, peer); + SOCKFS.websocket_sock_ops.handlePeerEvents(sock, peer); + // if this is a bound dgram socket, send the port number first to allow + // us to override the ephemeral port reported to us by remotePort on the + // remote end. + if (sock.type === 2 && typeof sock.sport != 'undefined') { + peer.msg_send_queue.push( + new Uint8Array([ + 255, + 255, + 255, + 255, + 'p'.charCodeAt(0), + 'o'.charCodeAt(0), + 'r'.charCodeAt(0), + 't'.charCodeAt(0), + (sock.sport & 65280) >> 8, + sock.sport & 255, + ]) + ); + } + return peer; + }, + getPeer(sock, addr, port) { + return sock.peers[addr + ':' + port]; + }, + addPeer(sock, peer) { + sock.peers[peer.addr + ':' + peer.port] = peer; + }, + removePeer(sock, peer) { + delete sock.peers[peer.addr + ':' + peer.port]; + }, + handlePeerEvents(sock, peer) { + var first = true; + var handleOpen = function () { + sock.connecting = false; + SOCKFS.emit('open', sock.stream.fd); + try { + var queued = peer.msg_send_queue.shift(); + while (queued) { + peer.socket.send(queued); + queued = peer.msg_send_queue.shift(); + } + } catch (e) { + // not much we can do here in the way of proper error handling as we've already + // lied and said this data was sent. shut it down. + peer.socket.close(); + } + }; + function handleMessage(data) { + if (typeof data == 'string') { + var encoder = new TextEncoder(); + // should be utf-8 + data = encoder.encode(data); + } else { + if (data.byteLength == 0) { + // An empty ArrayBuffer will emit a pseudo disconnect event + // as recv/recvmsg will return zero which indicates that a socket + // has performed a shutdown although the connection has not been disconnected yet. + return; + } + data = new Uint8Array(data); + } + // if this is the port message, override the peer's port with it + var wasfirst = first; + first = false; + if ( + wasfirst && + data.length === 10 && + data[0] === 255 && + data[1] === 255 && + data[2] === 255 && + data[3] === 255 && + data[4] === 'p'.charCodeAt(0) && + data[5] === 'o'.charCodeAt(0) && + data[6] === 'r'.charCodeAt(0) && + data[7] === 't'.charCodeAt(0) + ) { + // update the peer's port and it's key in the peer map + var newport = (data[8] << 8) | data[9]; + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + peer.port = newport; + SOCKFS.websocket_sock_ops.addPeer(sock, peer); + return; + } + sock.recv_queue.push({ + addr: peer.addr, + port: peer.port, + data, + }); + SOCKFS.emit('message', sock.stream.fd); + } + if (ENVIRONMENT_IS_NODE) { + peer.socket.on('open', handleOpen); + peer.socket.on('message', function (data, isBinary) { + if (!isBinary) { + return; + } + handleMessage(new Uint8Array(data).buffer); + }); + peer.socket.on('close', function () { + SOCKFS.emit('close', sock.stream.fd); + }); + peer.socket.on('error', function (error) { + // Although the ws library may pass errors that may be more descriptive than + // ECONNREFUSED they are not necessarily the expected error code e.g. + // ENOTFOUND on getaddrinfo seems to be node.js specific, so using ECONNREFUSED + // is still probably the most useful thing to do. + sock.error = 14; + // Used in getsockopt for SOL_SOCKET/SO_ERROR test. + SOCKFS.emit('error', [ + sock.stream.fd, + sock.error, + 'ECONNREFUSED: Connection refused', + ]); + }); + } else { + peer.socket.onopen = handleOpen; + peer.socket.onclose = function () { + SOCKFS.emit('close', sock.stream.fd); + }; + peer.socket.onmessage = function peer_socket_onmessage( + event + ) { + handleMessage(event.data); + }; + peer.socket.onerror = function (error) { + // The WebSocket spec only allows a 'simple event' to be thrown on error, + // so we only really know as much as ECONNREFUSED. + sock.error = 14; + // Used in getsockopt for SOL_SOCKET/SO_ERROR test. + SOCKFS.emit('error', [ + sock.stream.fd, + sock.error, + 'ECONNREFUSED: Connection refused', + ]); + }; + } + }, + poll(sock) { + if (sock.type === 1 && sock.server) { + // listen sockets should only say they're available for reading + // if there are pending clients. + return sock.pending.length ? 64 | 1 : 0; + } + var mask = 0; + var dest = + sock.type === 1 // we only care about the socket state for connection-based sockets + ? SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ) + : null; + if ( + sock.recv_queue.length || + !dest || // connection-less sockets are always ready to read + (dest && dest.socket.readyState === dest.socket.CLOSING) || + (dest && dest.socket.readyState === dest.socket.CLOSED) + ) { + // let recv return 0 once closed + mask |= 64 | 1; + } + if ( + !dest || // connection-less sockets are always ready to write + (dest && dest.socket.readyState === dest.socket.OPEN) + ) { + mask |= 4; + } + if ( + (dest && dest.socket.readyState === dest.socket.CLOSING) || + (dest && dest.socket.readyState === dest.socket.CLOSED) + ) { + // When an non-blocking connect fails mark the socket as writable. + // Its up to the calling code to then use getsockopt with SO_ERROR to + // retrieve the error. + // See https://man7.org/linux/man-pages/man2/connect.2.html + if (sock.connecting) { + mask |= 4; + } else { + mask |= 16; + } + } + return mask; + }, + ioctl(sock, request, arg) { + switch (request) { + case 21531: + var bytes = 0; + if (sock.recv_queue.length) { + bytes = sock.recv_queue[0].data.length; + } + HEAP32[arg >> 2] = bytes; + return 0; + + case 21537: + var on = HEAP32[arg >> 2]; + if (on) { + sock.stream.flags |= 2048; + } else { + sock.stream.flags &= ~2048; + } + return 0; + + default: + return 28; + } + }, + close(sock) { + // if we've spawned a listen server, close it + if (sock.server) { + try { + sock.server.close(); + } catch (e) {} + sock.server = null; + } + // close any peer connections + for (var peer of Object.values(sock.peers)) { + try { + peer.socket.close(); + } catch (e) {} + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + } + return 0; + }, + bind(sock, addr, port) { + if ( + typeof sock.saddr != 'undefined' || + typeof sock.sport != 'undefined' + ) { + throw new FS.ErrnoError(28); + } + sock.saddr = addr; + sock.sport = port; + // in order to emulate dgram sockets, we need to launch a listen server when + // binding on a connection-less socket + // note: this is only required on the server side + if (sock.type === 2) { + // close the existing server if it exists + if (sock.server) { + sock.server.close(); + sock.server = null; + } + // swallow error operation not supported error that occurs when binding in the + // browser where this isn't supported + try { + sock.sock_ops.listen(sock, 0); + } catch (e) { + if (!(e.name === 'ErrnoError')) throw e; + if (e.errno !== 138) throw e; + } + } + }, + connect(sock, addr, port) { + if (sock.server) { + throw new FS.ErrnoError(138); + } + // TODO autobind + // if (!sock.addr && sock.type == 2) { + // } + // early out if we're already connected / in the middle of connecting + if ( + typeof sock.daddr != 'undefined' && + typeof sock.dport != 'undefined' + ) { + var dest = SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ); + if (dest) { + if (dest.socket.readyState === dest.socket.CONNECTING) { + throw new FS.ErrnoError(7); + } else { + throw new FS.ErrnoError(30); + } + } + } + // add the socket to our peer list and set our + // destination address / port to match + var peer = SOCKFS.websocket_sock_ops.createPeer( + sock, + addr, + port + ); + sock.daddr = peer.addr; + sock.dport = peer.port; + // because we cannot synchronously block to wait for the WebSocket + // connection to complete, we return here pretending that the connection + // was a success. + sock.connecting = true; + }, + listen(sock, backlog) { + if (!ENVIRONMENT_IS_NODE) { + throw new FS.ErrnoError(138); + } + if (sock.server) { + throw new FS.ErrnoError(28); + } + var WebSocketServer = require('ws').Server; + var host = sock.saddr; + if (Module['websocket']['serverDecorator']) { + WebSocketServer = + Module['websocket']['serverDecorator'](WebSocketServer); + } + sock.server = new WebSocketServer({ + host, + port: sock.sport, + }); + SOCKFS.emit('listen', sock.stream.fd); + // Send Event with listen fd. + sock.server.on('connection', function (ws) { + if (sock.type === 1) { + var newsock = SOCKFS.createSocket( + sock.family, + sock.type, + sock.protocol + ); + // create a peer on the new socket + var peer = SOCKFS.websocket_sock_ops.createPeer( + newsock, + ws + ); + newsock.daddr = peer.addr; + newsock.dport = peer.port; + // push to queue for accept to pick up + sock.pending.push(newsock); + SOCKFS.emit('connection', newsock.stream.fd); + } else { + // create a peer on the listen socket so calling sendto + // with the listen socket and an address will resolve + // to the correct client + SOCKFS.websocket_sock_ops.createPeer(sock, ws); + SOCKFS.emit('connection', sock.stream.fd); + } + }); + sock.server.on('close', function () { + SOCKFS.emit('close', sock.stream.fd); + sock.server = null; + }); + sock.server.on('error', function (error) { + // Although the ws library may pass errors that may be more descriptive than + // ECONNREFUSED they are not necessarily the expected error code e.g. + // ENOTFOUND on getaddrinfo seems to be node.js specific, so using EHOSTUNREACH + // is still probably the most useful thing to do. This error shouldn't + // occur in a well written app as errors should get trapped in the compiled + // app's own getaddrinfo call. + sock.error = 23; + // Used in getsockopt for SOL_SOCKET/SO_ERROR test. + SOCKFS.emit('error', [ + sock.stream.fd, + sock.error, + 'EHOSTUNREACH: Host is unreachable', + ]); + }); + }, + accept(listensock) { + if (!listensock.server || !listensock.pending.length) { + throw new FS.ErrnoError(28); + } + var newsock = listensock.pending.shift(); + newsock.stream.flags = listensock.stream.flags; + return newsock; + }, + getname(sock, peer) { + var addr, port; + if (peer) { + if (sock.daddr === undefined || sock.dport === undefined) { + throw new FS.ErrnoError(53); + } + addr = sock.daddr; + port = sock.dport; + } else { + // TODO saddr and sport will be set for bind()'d UDP sockets, but what + // should we be returning for TCP sockets that've been connect()'d? + addr = sock.saddr || 0; + port = sock.sport || 0; + } + return { + addr, + port, + }; + }, + sendmsg(sock, buffer, offset, length, addr, port) { + if (sock.type === 2) { + // connection-less sockets will honor the message address, + // and otherwise fall back to the bound destination address + if (addr === undefined || port === undefined) { + addr = sock.daddr; + port = sock.dport; + } + // if there was no address to fall back to, error out + if (addr === undefined || port === undefined) { + throw new FS.ErrnoError(17); + } + } else { + // connection-based sockets will only use the bound + addr = sock.daddr; + port = sock.dport; + } + // find the peer for the destination address + var dest = SOCKFS.websocket_sock_ops.getPeer(sock, addr, port); + // early out if not connected with a connection-based socket + if (sock.type === 1) { + if ( + !dest || + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + throw new FS.ErrnoError(53); + } + } + // create a copy of the incoming data to send, as the WebSocket API + // doesn't work entirely with an ArrayBufferView, it'll just send + // the entire underlying buffer + if (ArrayBuffer.isView(buffer)) { + offset += buffer.byteOffset; + buffer = buffer.buffer; + } + var data = buffer.slice(offset, offset + length); + // if we don't have a cached connectionless UDP datagram connection, or + // the TCP socket is still connecting, queue the message to be sent upon + // connect, and lie, saying the data was sent now. + if (!dest || dest.socket.readyState !== dest.socket.OPEN) { + // if we're not connected, open a new connection + if (sock.type === 2) { + if ( + !dest || + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + dest = SOCKFS.websocket_sock_ops.createPeer( + sock, + addr, + port + ); + } + } + dest.msg_send_queue.push(data); + return length; + } + try { + // send the actual data + dest.socket.send(data); + return length; + } catch (e) { + throw new FS.ErrnoError(28); + } + }, + recvmsg(sock, length, flags) { + // http://pubs.opengroup.org/onlinepubs/7908799/xns/recvmsg.html + if (sock.type === 1 && sock.server) { + // tcp servers should not be recv()'ing on the listen socket + throw new FS.ErrnoError(53); + } + var queued = sock.recv_queue.shift(); + if (!queued) { + if (sock.type === 1) { + var dest = SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ); + if (!dest) { + // if we have a destination address but are not connected, error out + throw new FS.ErrnoError(53); + } + if ( + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + // return null if the socket has closed + return null; + } + // else, our socket is in a valid state but truly has nothing available + throw new FS.ErrnoError(6); + } + throw new FS.ErrnoError(6); + } + // queued.data will be an ArrayBuffer if it's unadulterated, but if it's + // requeued TCP data it'll be an ArrayBufferView + var queuedLength = queued.data.byteLength || queued.data.length; + var queuedOffset = queued.data.byteOffset || 0; + var queuedBuffer = queued.data.buffer || queued.data; + var bytesRead = Math.min(length, queuedLength); + var res = { + buffer: new Uint8Array( + queuedBuffer, + queuedOffset, + bytesRead + ), + addr: queued.addr, + port: queued.port, + }; + // push back any unread data for TCP connections + if (flags & 2) { + bytesRead = 0; + } + if (sock.type === 1 && bytesRead < queuedLength) { + var bytesRemaining = queuedLength - bytesRead; + queued.data = new Uint8Array( + queuedBuffer, + queuedOffset + bytesRead, + bytesRemaining + ); + sock.recv_queue.unshift(queued); + } + return res; + }, + }, + }; + + var getSocketFromFD = (fd) => { + var socket = SOCKFS.getSocket(fd); + if (!socket) throw new FS.ErrnoError(8); + return socket; + }; + + var inetPton4 = (str) => { + var b = str.split('.'); + for (var i = 0; i < 4; i++) { + var tmp = Number(b[i]); + if (isNaN(tmp)) return null; + b[i] = tmp; + } + return (b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24)) >>> 0; + }; + + var inetPton6 = (str) => { + var words; + var w, offset, z; + /* http://home.deds.nl/~aeron/regex/ */ var valid6regx = + /^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i; + var parts = []; + if (!valid6regx.test(str)) { + return null; + } + if (str === '::') { + return [0, 0, 0, 0, 0, 0, 0, 0]; + } + // Z placeholder to keep track of zeros when splitting the string on ":" + if (str.startsWith('::')) { + str = str.replace('::', 'Z:'); + } else { + str = str.replace('::', ':Z:'); + } + if (str.indexOf('.') > 0) { + // parse IPv4 embedded stress + str = str.replace(new RegExp('[.]', 'g'), ':'); + words = str.split(':'); + words[words.length - 4] = + Number(words[words.length - 4]) + + Number(words[words.length - 3]) * 256; + words[words.length - 3] = + Number(words[words.length - 2]) + + Number(words[words.length - 1]) * 256; + words = words.slice(0, words.length - 2); + } else { + words = str.split(':'); + } + offset = 0; + z = 0; + for (w = 0; w < words.length; w++) { + if (typeof words[w] == 'string') { + if (words[w] === 'Z') { + // compressed zeros - write appropriate number of zero words + for (z = 0; z < 8 - words.length + 1; z++) { + parts[w + z] = 0; + } + offset = z - 1; + } else { + // parse hex to field to 16-bit value and write it in network byte-order + parts[w + offset] = _htons(parseInt(words[w], 16)); + } + } else { + // parsed IPv4 words + parts[w + offset] = words[w]; + } + } + return [ + (parts[1] << 16) | parts[0], + (parts[3] << 16) | parts[2], + (parts[5] << 16) | parts[4], + (parts[7] << 16) | parts[6], + ]; + }; + + /** @param {number=} addrlen */ var writeSockaddr = ( + sa, + family, + addr, + port, + addrlen + ) => { + switch (family) { + case 2: + addr = inetPton4(addr); + zeroMemory(sa, 16); + if (addrlen) { + HEAP32[addrlen >> 2] = 16; + } + HEAP16[sa >> 1] = family; + HEAP32[(sa + 4) >> 2] = addr; + HEAP16[(sa + 2) >> 1] = _htons(port); + break; + + case 10: + addr = inetPton6(addr); + zeroMemory(sa, 28); + if (addrlen) { + HEAP32[addrlen >> 2] = 28; + } + HEAP32[sa >> 2] = family; + HEAP32[(sa + 8) >> 2] = addr[0]; + HEAP32[(sa + 12) >> 2] = addr[1]; + HEAP32[(sa + 16) >> 2] = addr[2]; + HEAP32[(sa + 20) >> 2] = addr[3]; + HEAP16[(sa + 2) >> 1] = _htons(port); + break; + + default: + return 5; + } + return 0; + }; + + var DNS = { + address_map: { + id: 1, + addrs: {}, + names: {}, + }, + lookup_name(name) { + // If the name is already a valid ipv4 / ipv6 address, don't generate a fake one. + var res = inetPton4(name); + if (res !== null) { + return name; + } + res = inetPton6(name); + if (res !== null) { + return name; + } + // See if this name is already mapped. + var addr; + if (DNS.address_map.addrs[name]) { + addr = DNS.address_map.addrs[name]; + } else { + var id = DNS.address_map.id++; + addr = '172.29.' + (id & 255) + '.' + (id & 65280); + DNS.address_map.names[addr] = name; + DNS.address_map.addrs[name] = addr; + } + return addr; + }, + lookup_addr(addr) { + if (DNS.address_map.names[addr]) { + return DNS.address_map.names[addr]; + } + return null; + }, + }; + + function ___syscall_accept4(fd, addr, addrlen, flags, d1, d2) { + try { + var sock = getSocketFromFD(fd); + var newsock = sock.sock_ops.accept(sock); + if (addr) { + var errno = writeSockaddr( + addr, + newsock.family, + DNS.lookup_name(newsock.daddr), + newsock.dport, + addrlen + ); + } + return newsock.stream.fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_accept4.sig = 'iippiii'; + + var inetNtop4 = (addr) => + (addr & 255) + + '.' + + ((addr >> 8) & 255) + + '.' + + ((addr >> 16) & 255) + + '.' + + ((addr >> 24) & 255); + + var inetNtop6 = (ints) => { + // ref: http://www.ietf.org/rfc/rfc2373.txt - section 2.5.4 + // Format for IPv4 compatible and mapped 128-bit IPv6 Addresses + // 128-bits are split into eight 16-bit words + // stored in network byte order (big-endian) + // | 80 bits | 16 | 32 bits | + // +-----------------------------------------------------------------+ + // | 10 bytes | 2 | 4 bytes | + // +--------------------------------------+--------------------------+ + // + 5 words | 1 | 2 words | + // +--------------------------------------+--------------------------+ + // |0000..............................0000|0000| IPv4 ADDRESS | (compatible) + // +--------------------------------------+----+---------------------+ + // |0000..............................0000|FFFF| IPv4 ADDRESS | (mapped) + // +--------------------------------------+----+---------------------+ + var str = ''; + var word = 0; + var longest = 0; + var lastzero = 0; + var zstart = 0; + var len = 0; + var i = 0; + var parts = [ + ints[0] & 65535, + ints[0] >> 16, + ints[1] & 65535, + ints[1] >> 16, + ints[2] & 65535, + ints[2] >> 16, + ints[3] & 65535, + ints[3] >> 16, + ]; + // Handle IPv4-compatible, IPv4-mapped, loopback and any/unspecified addresses + var hasipv4 = true; + var v4part = ''; + // check if the 10 high-order bytes are all zeros (first 5 words) + for (i = 0; i < 5; i++) { + if (parts[i] !== 0) { + hasipv4 = false; + break; + } + } + if (hasipv4) { + // low-order 32-bits store an IPv4 address (bytes 13 to 16) (last 2 words) + v4part = inetNtop4(parts[6] | (parts[7] << 16)); + // IPv4-mapped IPv6 address if 16-bit value (bytes 11 and 12) == 0xFFFF (6th word) + if (parts[5] === -1) { + str = '::ffff:'; + str += v4part; + return str; + } + // IPv4-compatible IPv6 address if 16-bit value (bytes 11 and 12) == 0x0000 (6th word) + if (parts[5] === 0) { + str = '::'; + //special case IPv6 addresses + if (v4part === '0.0.0.0') v4part = ''; + // any/unspecified address + if (v4part === '0.0.0.1') v4part = '1'; + // loopback address + str += v4part; + return str; + } + } + // Handle all other IPv6 addresses + // first run to find the longest contiguous zero words + for (word = 0; word < 8; word++) { + if (parts[word] === 0) { + if (word - lastzero > 1) { + len = 0; + } + lastzero = word; + len++; + } + if (len > longest) { + longest = len; + zstart = word - longest + 1; + } + } + for (word = 0; word < 8; word++) { + if (longest > 1) { + // compress contiguous zeros - to produce "::" + if ( + parts[word] === 0 && + word >= zstart && + word < zstart + longest + ) { + if (word === zstart) { + str += ':'; + if (zstart === 0) str += ':'; + } + continue; + } + } + // converts 16-bit words from big-endian to little-endian before converting to hex string + str += Number(_ntohs(parts[word] & 65535)).toString(16); + str += word < 7 ? ':' : ''; + } + return str; + }; + + var readSockaddr = (sa, salen) => { + // family / port offsets are common to both sockaddr_in and sockaddr_in6 + var family = HEAP16[sa >> 1]; + var port = _ntohs(HEAPU16[(sa + 2) >> 1]); + var addr; + switch (family) { + case 2: + if (salen !== 16) { + return { + errno: 28, + }; + } + addr = HEAP32[(sa + 4) >> 2]; + addr = inetNtop4(addr); + break; + + case 10: + if (salen !== 28) { + return { + errno: 28, + }; + } + addr = [ + HEAP32[(sa + 8) >> 2], + HEAP32[(sa + 12) >> 2], + HEAP32[(sa + 16) >> 2], + HEAP32[(sa + 20) >> 2], + ]; + addr = inetNtop6(addr); + break; + + default: + return { + errno: 5, + }; + } + return { + family, + addr, + port, + }; + }; + + var getSocketAddress = (addrp, addrlen) => { + var info = readSockaddr(addrp, addrlen); + if (info.errno) throw new FS.ErrnoError(info.errno); + info.addr = DNS.lookup_addr(info.addr) || info.addr; + return info; + }; + + function ___syscall_bind(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + var info = getSocketAddress(addr, addrlen); + sock.sock_ops.bind(sock, info.addr, info.port); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_bind.sig = 'iippiii'; + + var SYSCALLS = { + DEFAULT_POLLMASK: 5, + calculateAt(dirfd, path, allowEmpty) { + if (PATH.isAbs(path)) { + return path; + } + // relative path + var dir; + if (dirfd === -100) { + dir = FS.cwd(); + } else { + var dirstream = SYSCALLS.getStreamFromFD(dirfd); + dir = dirstream.path; + } + if (path.length == 0) { + if (!allowEmpty) { + throw new FS.ErrnoError(44); + } + return dir; + } + return dir + '/' + path; + }, + writeStat(buf, stat) { + HEAPU32[buf >> 2] = stat.dev; + HEAPU32[(buf + 4) >> 2] = stat.mode; + HEAPU32[(buf + 8) >> 2] = stat.nlink; + HEAPU32[(buf + 12) >> 2] = stat.uid; + HEAPU32[(buf + 16) >> 2] = stat.gid; + HEAPU32[(buf + 20) >> 2] = stat.rdev; + HEAP64[(buf + 24) >> 3] = BigInt(stat.size); + HEAP32[(buf + 32) >> 2] = 4096; + HEAP32[(buf + 36) >> 2] = stat.blocks; + var atime = stat.atime.getTime(); + var mtime = stat.mtime.getTime(); + var ctime = stat.ctime.getTime(); + HEAP64[(buf + 40) >> 3] = BigInt(Math.floor(atime / 1e3)); + HEAPU32[(buf + 48) >> 2] = (atime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 56) >> 3] = BigInt(Math.floor(mtime / 1e3)); + HEAPU32[(buf + 64) >> 2] = (mtime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 72) >> 3] = BigInt(Math.floor(ctime / 1e3)); + HEAPU32[(buf + 80) >> 2] = (ctime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 88) >> 3] = BigInt(stat.ino); + return 0; + }, + writeStatFs(buf, stats) { + HEAPU32[(buf + 4) >> 2] = stats.bsize; + HEAPU32[(buf + 60) >> 2] = stats.bsize; + HEAP64[(buf + 8) >> 3] = BigInt(stats.blocks); + HEAP64[(buf + 16) >> 3] = BigInt(stats.bfree); + HEAP64[(buf + 24) >> 3] = BigInt(stats.bavail); + HEAP64[(buf + 32) >> 3] = BigInt(stats.files); + HEAP64[(buf + 40) >> 3] = BigInt(stats.ffree); + HEAPU32[(buf + 48) >> 2] = stats.fsid; + HEAPU32[(buf + 64) >> 2] = stats.flags; + // ST_NOSUID + HEAPU32[(buf + 56) >> 2] = stats.namelen; + }, + doMsync(addr, stream, len, flags, offset) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError(43); + } + if (flags & 2) { + // MAP_PRIVATE calls need not to be synced back to underlying fs + return 0; + } + var buffer = HEAPU8.slice(addr, addr + len); + FS.msync(stream, buffer, offset, len, flags); + }, + getStreamFromFD(fd) { + var stream = FS.getStreamChecked(fd); + return stream; + }, + varargs: undefined, + getStr(ptr) { + var ret = UTF8ToString(ptr); + return ret; + }, + }; + + function ___syscall_chdir(path) { + try { + path = SYSCALLS.getStr(path); + FS.chdir(path); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_chdir.sig = 'ip'; + + function ___syscall_chmod(path, mode) { + try { + path = SYSCALLS.getStr(path); + FS.chmod(path, mode); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_chmod.sig = 'ipi'; + + var allocateUTF8OnStack = (...args) => stringToUTF8OnStack(...args); + + var onInits = []; + + var addOnInit = (cb) => onInits.push(cb); + + function _js_getpid() { + return PHPLoader.processId ?? 42; + } + + function _js_wasm_trace(format, ...args) { + if (PHPLoader.trace instanceof Function) { + PHPLoader.trace(_js_getpid(), format, ...args); + } + } + + var PHPWASM = { + O_APPEND: 1024, + O_NONBLOCK: 2048, + POLLHUP: 16, + SETFL_MASK: 3072, + init: function () { + // TODO: Move this to a library function that is made an onInit callback by the `__postset` suffix. + if (PHPLoader.bindUserSpace) { + /** + * We need to add an onInit callback to bind the user-space API + * because some dependencies like wasmImports and wasmExports + * are not yet assigned. + */ addOnInit(() => { + if (typeof PHPLoader.processId !== 'number') { + throw new Error( + 'PHPLoader.processId must be set before init' + ); + } + Module['userSpace'] = PHPLoader.bindUserSpace({ + pid: PHPLoader.processId, + constants: { + F_GETFL: Number('3'), + O_ACCMODE: Number('2097155'), + O_RDONLY: Number('0'), + O_WRONLY: Number('1'), + O_APPEND: Number('1024'), + O_NONBLOCK: Number('2048'), + F_SETFL: Number('4'), + F_GETLK: Number('12'), + F_SETLK: Number('13'), + F_SETLKW: Number('14'), + SEEK_SET: Number('0'), + SEEK_CUR: Number('1'), + SEEK_END: Number('2'), + F_GETFL: Number('3'), + O_ACCMODE: Number('2097155'), + O_RDONLY: Number('0'), + O_WRONLY: Number('1'), + O_APPEND: Number('1024'), + O_NONBLOCK: Number('2048'), + F_SETFL: Number('4'), + F_GETLK: Number('12'), + F_SETLK: Number('13'), + F_SETLKW: Number('14'), + SEEK_SET: Number('0'), + SEEK_CUR: Number('1'), + SEEK_END: Number('2'), + // From: + // https://github.com/emscripten-core/emscripten/blob/66d2137b0381ac35f7e2346b2d6a90abd0f1211a/system/lib/libc/musl/include/fcntl.h#L58-L60 + F_RDLCK: 0, + F_WRLCK: 1, + F_UNLCK: 2, + // From: + // https://github.com/emscripten-core/emscripten/blob/81bbaa42a7827d88a71bd89701245052c622428c/system/lib/libc/musl/include/sys/file.h#L7-L10 + LOCK_SH: 1, + LOCK_EX: 2, + LOCK_NB: 4, + // Non-blocking lock + LOCK_UN: 8, + }, + errnoCodes: ERRNO_CODES, + // Use get/set closures instead of exposing + // typed arrays directly. After memory.grow(), + // Emscripten's updateMemoryViews() reassigns + // the module-scoped HEAP* variables. Closures + // always reference the current value, so + // accesses are never stale. The get/set + // interface also prevents callers from + // capturing a typed array reference that + // could become stale. + memory: { + HEAP8: { + get(offset) { + return HEAP8[offset]; + }, + set(offset, value) { + HEAP8[offset] = value; + }, + }, + HEAPU8: { + get(offset) { + return HEAPU8[offset]; + }, + set(offset, value) { + HEAPU8[offset] = value; + }, + }, + HEAP16: { + get(offset) { + return HEAP16[offset]; + }, + set(offset, value) { + HEAP16[offset] = value; + }, + }, + HEAPU16: { + get(offset) { + return HEAPU16[offset]; + }, + set(offset, value) { + HEAPU16[offset] = value; + }, + }, + HEAP32: { + get(offset) { + return HEAP32[offset]; + }, + set(offset, value) { + HEAP32[offset] = value; + }, + }, + HEAPU32: { + get(offset) { + return HEAPU32[offset]; + }, + set(offset, value) { + HEAPU32[offset] = value; + }, + }, + HEAPF32: { + get(offset) { + return HEAPF32[offset]; + }, + set(offset, value) { + HEAPF32[offset] = value; + }, + }, + HEAP64: { + get(offset) { + return HEAP64[offset]; + }, + set(offset, value) { + HEAP64[offset] = value; + }, + }, + HEAPU64: { + get(offset) { + return HEAPU64[offset]; + }, + set(offset, value) { + HEAPU64[offset] = value; + }, + }, + HEAPF64: { + get(offset) { + return HEAPF64[offset]; + }, + set(offset, value) { + HEAPF64[offset] = value; + }, + }, + }, + wasmImports: Object.assign( + {}, + wasmImports, + typeof _builtin_fd_close === 'function' + ? { + builtin_fd_close: _builtin_fd_close, + } + : {}, + typeof _builtin_fcntl64 === 'function' + ? { + builtin_fcntl64: _builtin_fcntl64, + } + : {} + ), + wasmExports, + syscalls: SYSCALLS, + FS, + PROXYFS, + NODEFS, + }); + }); + } + Module['ENV'] = Module['ENV'] || {}; + // Ensure a platform-level bin directory for a fallback `php` binary. + Module['ENV']['PATH'] = [ + Module['ENV']['PATH'], + '/internal/shared/bin', + ] + .filter(Boolean) + .join(':'); + // The /request directory is required by the C module. It's where the + // stdout, stderr, and headers information are written for the JavaScript + // code to read later on. This is per-request state that is isolated to a + // single PHP process. + FS.mkdir('/request'); + // The /internal directory is shared amongst all PHP processes + // and contains the php.ini, constants definitions, etc. + FS.mkdir('/internal'); + if (PHPLoader.nativeInternalDirPath) { + FS.mount( + FS.filesystems.NODEFS, + { + root: PHPLoader.nativeInternalDirPath, + }, + '/internal' + ); + } + // The files from the shared directory are shared between all the + // PHP processes managed by PHPProcessManager. + FS.mkdirTree('/internal/shared'); + // The files from the preload directory are preloaded using the + // auto_prepend_file php.ini directive. + FS.mkdirTree('/internal/shared/preload'); + // Platform-level bin directory for a fallback `php` binary. Without it, + // PHP may not populate the PHP_BINARY constant. + FS.mkdirTree('/internal/shared/bin'); + const originalOnRuntimeInitialized = Module['onRuntimeInitialized']; + Module['onRuntimeInitialized'] = () => { + const { node: phpBinaryNode } = FS.lookupPath( + '/internal/shared/bin/php', + { + noent_okay: true, + } + ); + if (!phpBinaryNode) { + // Dummy PHP binary for PHP to populate the PHP_BINARY constant. + FS.writeFile( + '/internal/shared/bin/php', + new TextEncoder().encode('#!/bin/sh\nphp "$@"') + ); + // It must be executable to be used by PHP. + FS.chmod('/internal/shared/bin/php', 493); + } + originalOnRuntimeInitialized(); + }; + // Create stdout and stderr devices. We can't just use Emscripten's + // default stdout and stderr devices because they stop processing data + // on the first null byte. However, when dealing with binary data, + // null bytes are valid and common. + FS.registerDevice(FS.makedev(64, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onStdout(chunk); + return length; + }, + }); + FS.mkdev('/request/stdout', FS.makedev(64, 0)); + FS.registerDevice(FS.makedev(63, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onStderr(chunk); + return length; + }, + }); + FS.mkdev('/request/stderr', FS.makedev(63, 0)); + FS.registerDevice(FS.makedev(62, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onHeaders(chunk); + return length; + }, + }); + FS.mkdev('/request/headers', FS.makedev(62, 0)); + // Handle events. + PHPWASM.EventEmitter = ENVIRONMENT_IS_NODE + ? require('events').EventEmitter + : class EventEmitter { + constructor() { + this.listeners = {}; + } + emit(eventName, data) { + if (this.listeners[eventName]) { + this.listeners[eventName].forEach( + (callback) => { + callback(data); + } + ); + } + } + once(eventName, callback) { + const self = this; + function removedCallback() { + callback(...arguments); + self.removeListener(eventName, removedCallback); + } + this.on(eventName, removedCallback); + } + removeAllListeners(eventName) { + if (eventName) { + delete this.listeners[eventName]; + } else { + this.listeners = {}; + } + } + removeListener(eventName, callback) { + if (this.listeners[eventName]) { + const idx = + this.listeners[eventName].indexOf(callback); + if (idx !== -1) { + this.listeners[eventName].splice(idx, 1); + } + } + } + }; + PHPWASM.processTable = {}; + PHPWASM.input_devices = {}; + const originalWrite = TTY.stream_ops.write; + TTY.stream_ops.write = function (stream, ...rest) { + const retval = originalWrite(stream, ...rest); + // Implicit flush since PHP's fflush() doesn't seem to trigger the fsync event + // @TODO: Fix this at the wasm level + stream.tty.ops.fsync(stream.tty); + return retval; + }; + const originalPutChar = TTY.stream_ops.put_char; + TTY.stream_ops.put_char = function (tty, val) { + /** + * Buffer newlines that Emscripten normally ignores. + * + * Emscripten doesn't do it by default because its default + * print function is console.log that implicitly adds a newline. We are overwriting + * it with an environment-specific function that outputs exaclty what it was given, + * e.g. in Node.js it's process.stdout.write(). Therefore, we need to mak sure + * all the newlines make it to the output buffer. + */ if (val === 10) tty.output.push(val); + return originalPutChar(tty, val); + }; + }, + onHeaders: function (chunk) { + if (Module['onHeaders']) { + Module['onHeaders'](chunk); + return; + } + console.log('headers', { + chunk, + }); + }, + onStdout: function (chunk) { + if (Module['onStdout']) { + Module['onStdout'](chunk); + return; + } + if (ENVIRONMENT_IS_NODE) { + process.stdout.write(chunk); + } else { + console.log('stdout', { + chunk, + }); + } + }, + onStderr: function (chunk) { + if (Module['onStderr']) { + Module['onStderr'](chunk); + return; + } + if (ENVIRONMENT_IS_NODE) { + process.stderr.write(chunk); + } else { + console.warn('stderr', { + chunk, + }); + } + }, + getAllWebSockets: function (sock) { + const webSockets = new Set(); + if (sock.server) { + sock.server.clients.forEach((ws) => { + webSockets.add(ws); + }); + } + for (const peer of PHPWASM.getAllPeers(sock)) { + webSockets.add(peer.socket); + } + return Array.from(webSockets); + }, + getAllPeers: function (sock) { + const peers = new Set(); + if (sock.server) { + sock.pending + .filter((pending) => pending.peers) + .forEach((pending) => { + for (const peer of Object.values(pending.peers)) { + peers.add(peer); + } + }); + } + if (sock.peers) { + for (const peer of Object.values(sock.peers)) { + peers.add(peer); + } + } + return Array.from(peers); + }, + awaitData: function (ws) { + return PHPWASM.awaitEvent(ws, 'message'); + }, + awaitConnection: function (ws) { + if (ws.OPEN === ws.readyState) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'open'); + }, + awaitClose: function (ws) { + if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'close'); + }, + awaitError: function (ws) { + if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'error'); + }, + awaitEvent: function (ws, event) { + let resolve; + const listener = () => { + resolve(); + }; + const promise = new Promise(function (_resolve) { + resolve = _resolve; + ws.once(event, listener); + }); + const cancel = () => { + ws.removeListener(event, listener); + // Rejecting the promises bubbles up and kills the entire + // node process. Let's resolve them on the next tick instead + // to give the caller some space to unbind any handlers. + setTimeout(resolve); + }; + return [promise, cancel]; + }, + noop: function () {}, + spawnProcess: function (command, args, options) { + if (Module['spawnProcess']) { + const spawned = Module['spawnProcess']( + command, + args, + /** + * We're providing the same extra options we would pass to child_process.spawn(). + * + * Why? + * + * spawnProcess() follows the same interface as child_process.spawn() + * and some consumers pass `child_process.spawn` directly to php.setSpawnHandler() + */ { + ...options, + shell: true, + stdio: ['pipe', 'pipe', 'pipe'], + } + ); + if (spawned && !('then' in spawned) && 'on' in spawned) { + /** + * If we get the child process directly, return it immediately. + * Delaying it to the next tick via Promise.resolve() would create + * a race condition where it might emit some events before the + * caller has a chance to bind event listeners to them. + * + * Without this condition, this callback would be at least flaky: + * + * php.setSpawnHandler(require('child_process').spawn); + */ return spawned; + } + return Promise.resolve(spawned).then(function (spawned) { + if (!spawned || !spawned.on) { + throw new Error( + 'spawnProcess() must return an EventEmitter but returned a different type.' + ); + } + return spawned; + }); + } + const e = new Error( + 'popen(), proc_open() etc. are unsupported on this PHP instance. Call php.setSpawnHandler() ' + + 'and provide a callback to handle spawning processes, or disable a popen(), proc_open() ' + + 'and similar functions via php.ini.' + ); + e.code = 'SPAWN_UNSUPPORTED'; + throw e; + }, + shutdownSocket: function (socketd, how) { + // This implementation only supports websockets at the moment + const sock = getSocketFromFD(socketd); + const peer = Object.values(sock.peers)[0]; + if (!peer) { + return -1; + } + try { + peer.socket.close(); + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + return 0; + } catch (e) { + console.log('Socket shutdown error', e); + return -1; + } + }, + }; + + function _wasm_connect(sockfd, addr, addrlen) { + /** + * Use a synchronous connect() call when Asyncify is used. + * + * The async version was originally introduced to support the Memcached and Redis extensions, + * and both are only available with JSPI. Asyncify is too difficult to maintain and + * it's not getting that upgrade. + */ if (!('Suspending' in WebAssembly)) { + var sock = getSocketFromFD(sockfd); + var info = getSocketAddress(addr, addrlen); + sock.sock_ops.connect(sock, info.addr, info.port); + return 0; + } + return Asyncify.handleSleep((wakeUp) => { + // Get the socket + let sock; + try { + sock = getSocketFromFD(sockfd); + } catch (e) { + wakeUp(-ERRNO_CODES.EBADF); + return; + } + if (!sock) { + wakeUp(-ERRNO_CODES.EBADF); + return; + } + // Parse the address + let info; + try { + info = getSocketAddress(addr, addrlen); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + wakeUp(-ERRNO_CODES.EFAULT); + return; + } + wakeUp(-e.errno); + return; + } + // Perform the connect (this creates the WebSocket but doesn't wait) + try { + sock.sock_ops.connect(sock, info.addr, info.port); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + wakeUp(-e.errno); + return; + } + // Get all websockets for this socket + const webSockets = PHPWASM.getAllWebSockets(sock); + if (!webSockets.length) { + // No WebSocket yet, this shouldn't happen after connect + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + const ws = webSockets[0]; + // If already connected, return success + if (ws.readyState === ws.OPEN) { + wakeUp(0); + return; + } + // If already closed or closing, return error + if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) { + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + // Wait for the connection to be established + const timeout = 3e4; + // 30 second timeout + let resolved = false; + const timeoutId = setTimeout(() => { + if (!resolved) { + resolved = true; + wakeUp(-ERRNO_CODES.ETIMEDOUT); + } + }, timeout); + const handleOpen = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('error', handleError); + ws.removeEventListener('close', handleClose); + wakeUp(0); + } + }; + const handleError = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('open', handleOpen); + ws.removeEventListener('close', handleClose); + wakeUp(-ERRNO_CODES.ECONNREFUSED); + } + }; + const handleClose = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('open', handleOpen); + ws.removeEventListener('error', handleError); + wakeUp(-ERRNO_CODES.ECONNREFUSED); + } + }; + ws.addEventListener('open', handleOpen); + ws.addEventListener('error', handleError); + ws.addEventListener('close', handleClose); + }); + } + + function ___syscall_connect(sockfd, addr, addrlen, d1, d2, d3) { + return _wasm_connect(sockfd, addr, addrlen); + } + + ___syscall_connect.sig = 'iippiii'; + + function ___syscall_dup(fd) { + try { + var old = SYSCALLS.getStreamFromFD(fd); + return FS.dupStream(old).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_dup.sig = 'ii'; + + function ___syscall_dup3(fd, newfd, flags) { + try { + var old = SYSCALLS.getStreamFromFD(fd); + if (old.fd === newfd) return -28; + // Check newfd is within range of valid open file descriptors. + if (newfd < 0 || newfd >= FS.MAX_OPEN_FDS) return -8; + var existing = FS.getStream(newfd); + if (existing) FS.close(existing); + return FS.dupStream(old, newfd).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_dup3.sig = 'iiii'; + + function ___syscall_faccessat(dirfd, path, amode, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (amode & ~7) { + // need a valid mode + return -28; + } + var lookup = FS.lookupPath(path, { + follow: true, + }); + var node = lookup.node; + if (!node) { + return -44; + } + var perms = ''; + if (amode & 4) perms += 'r'; + if (amode & 2) perms += 'w'; + if (amode & 1) perms += 'x'; + if (perms && FS.nodePermissions(node, perms)) { + return -2; + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_faccessat.sig = 'iipii'; + + function ___syscall_fchmod(fd, mode) { + try { + FS.fchmod(fd, mode); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_fchmod.sig = 'iii'; + + function ___syscall_fchown32(fd, owner, group) { + try { + FS.fchown(fd, owner, group); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_fchown32.sig = 'iiii'; + + function ___syscall_fchownat(dirfd, path, owner, group, flags) { + try { + path = SYSCALLS.getStr(path); + var nofollow = flags & 256; + flags = flags & ~256; + path = SYSCALLS.calculateAt(dirfd, path); + (nofollow ? FS.lchown : FS.chown)(path, owner, group); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_fchownat.sig = 'iipiii'; + + var syscallGetVarargI = () => { + // the `+` prepended here is necessary to convince the JSCompiler that varargs is indeed a number. + var ret = HEAP32[+SYSCALLS.varargs >> 2]; + SYSCALLS.varargs += 4; + return ret; + }; + + var syscallGetVarargP = syscallGetVarargI; + + function _fd_close(fd) { + if (typeof Module['userSpace'] === 'undefined') { + return _builtin_fd_close(fd); + } + return Module['userSpace'].fd_close(fd); + } + + _fd_close.sig = 'ii'; + + function _builtin_fd_close(fd) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + FS.close(stream); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + + function _builtin_fcntl64(fd, cmd, varargs) { + SYSCALLS.varargs = varargs; + try { + var stream = SYSCALLS.getStreamFromFD(fd); + switch (cmd) { + case 0: { + var arg = syscallGetVarargI(); + if (arg < 0) { + return -28; + } + while (FS.streams[arg]) { + arg++; + } + var newStream; + newStream = FS.dupStream(stream, arg); + return newStream.fd; + } + + case 1: + case 2: + return 0; + + // FD_CLOEXEC makes no sense for a single process. + case 3: + return stream.flags; + + case 4: { + var arg = syscallGetVarargI(); + stream.flags |= arg; + return 0; + } + + case 12: { + var arg = syscallGetVarargP(); + var offset = 0; + // We're always unlocked. + HEAP16[(arg + offset) >> 1] = 2; + return 0; + } + + case 13: + case 14: + // Pretend that the locking is successful. These are process-level locks, + // and Emscripten programs are a single process. If we supported linking a + // filesystem between programs, we'd need to do more here. + // See https://github.com/emscripten-core/emscripten/issues/23697 + return 0; + } + return -28; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + function ___syscall_fcntl64(fd, cmd, varargs) { + if (typeof Module['userSpace'] === 'undefined') { + return _builtin_fcntl64(fd, cmd, varargs); + } + return Module['userSpace'].fcntl64(fd, cmd, varargs); + } + + ___syscall_fcntl64.sig = 'iiip'; + + function ___syscall_fstat64(fd, buf) { + try { + return SYSCALLS.writeStat(buf, FS.fstat(fd)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_fstat64.sig = 'iip'; + + var INT53_MAX = 9007199254740992; + + var INT53_MIN = -9007199254740992; + + var bigintToI53Checked = (num) => + num < INT53_MIN || num > INT53_MAX ? NaN : Number(num); + + function ___syscall_ftruncate64(fd, length) { + length = bigintToI53Checked(length); + try { + if (isNaN(length)) return -61; + FS.ftruncate(fd, length); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_ftruncate64.sig = 'iij'; + + function ___syscall_getcwd(buf, size) { + try { + if (size === 0) return -28; + var cwd = FS.cwd(); + var cwdLengthInBytes = lengthBytesUTF8(cwd) + 1; + if (size < cwdLengthInBytes) return -68; + stringToUTF8(cwd, buf, size); + return cwdLengthInBytes; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_getcwd.sig = 'ipp'; + + function ___syscall_getdents64(fd, dirp, count) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + stream.getdents ||= FS.readdir(stream.path); + var struct_size = 280; + var pos = 0; + var off = FS.llseek(stream, 0, 1); + var startIdx = Math.floor(off / struct_size); + var endIdx = Math.min( + stream.getdents.length, + startIdx + Math.floor(count / struct_size) + ); + for (var idx = startIdx; idx < endIdx; idx++) { + var id; + var type; + var name = stream.getdents[idx]; + if (name === '.') { + id = stream.node.id; + type = 4; + } else if (name === '..') { + var lookup = FS.lookupPath(stream.path, { + parent: true, + }); + id = lookup.node.id; + type = 4; + } else { + var child; + try { + child = FS.lookupNode(stream.node, name); + } catch (e) { + // If the entry is not a directory, file, or symlink, nodefs + // lookupNode will raise EINVAL. Skip these and continue. + if (e?.errno === 28) { + continue; + } + throw e; + } + id = child.id; + type = FS.isChrdev(child.mode) + ? 2 // DT_CHR, character device. + : FS.isDir(child.mode) + ? 4 // DT_DIR, directory. + : FS.isLink(child.mode) + ? 10 // DT_LNK, symbolic link. + : 8; + } + HEAP64[(dirp + pos) >> 3] = BigInt(id); + HEAP64[(dirp + pos + 8) >> 3] = BigInt((idx + 1) * struct_size); + HEAP16[(dirp + pos + 16) >> 1] = 280; + HEAP8[dirp + pos + 18] = type; + stringToUTF8(name, dirp + pos + 19, 256); + pos += struct_size; + } + FS.llseek(stream, idx * struct_size, 0); + return pos; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_getdents64.sig = 'iipp'; + + function ___syscall_getpeername(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + if (!sock.daddr) { + return -53; + } + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(sock.daddr), + sock.dport, + addrlen + ); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_getpeername.sig = 'iippiii'; + + function ___syscall_getsockname(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + // TODO: sock.saddr should never be undefined, see TODO in websocket_sock_ops.getname + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(sock.saddr || '0.0.0.0'), + sock.sport, + addrlen + ); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_getsockname.sig = 'iippiii'; + + function ___syscall_getsockopt(fd, level, optname, optval, optlen, d1) { + try { + var sock = getSocketFromFD(fd); + // Minimal getsockopt aimed at resolving https://github.com/emscripten-core/emscripten/issues/2211 + // so only supports SOL_SOCKET with SO_ERROR. + if (level === 1) { + if (optname === 4) { + HEAP32[optval >> 2] = sock.error; + HEAP32[optlen >> 2] = 4; + sock.error = null; + // Clear the error (The SO_ERROR option obtains and then clears this field). + return 0; + } + } + return -50; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_getsockopt.sig = 'iiiippi'; + + function ___syscall_ioctl(fd, op, varargs) { + SYSCALLS.varargs = varargs; + try { + var stream = SYSCALLS.getStreamFromFD(fd); + switch (op) { + case 21509: { + if (!stream.tty) return -59; + return 0; + } + + case 21505: { + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tcgets) { + var termios = stream.tty.ops.ioctl_tcgets(stream); + var argp = syscallGetVarargP(); + HEAP32[argp >> 2] = termios.c_iflag || 0; + HEAP32[(argp + 4) >> 2] = termios.c_oflag || 0; + HEAP32[(argp + 8) >> 2] = termios.c_cflag || 0; + HEAP32[(argp + 12) >> 2] = termios.c_lflag || 0; + for (var i = 0; i < 32; i++) { + HEAP8[argp + i + 17] = termios.c_cc[i] || 0; + } + return 0; + } + return 0; + } + + case 21510: + case 21511: + case 21512: { + if (!stream.tty) return -59; + return 0; + } + + case 21506: + case 21507: + case 21508: { + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tcsets) { + var argp = syscallGetVarargP(); + var c_iflag = HEAP32[argp >> 2]; + var c_oflag = HEAP32[(argp + 4) >> 2]; + var c_cflag = HEAP32[(argp + 8) >> 2]; + var c_lflag = HEAP32[(argp + 12) >> 2]; + var c_cc = []; + for (var i = 0; i < 32; i++) { + c_cc.push(HEAP8[argp + i + 17]); + } + return stream.tty.ops.ioctl_tcsets(stream.tty, op, { + c_iflag, + c_oflag, + c_cflag, + c_lflag, + c_cc, + }); + } + return 0; + } + + case 21519: { + if (!stream.tty) return -59; + var argp = syscallGetVarargP(); + HEAP32[argp >> 2] = 0; + return 0; + } + + case 21520: { + if (!stream.tty) return -59; + return -28; + } + + case 21537: + case 21531: { + var argp = syscallGetVarargP(); + return FS.ioctl(stream, op, argp); + } + + case 21523: { + // TODO: in theory we should write to the winsize struct that gets + // passed in, but for now musl doesn't read anything on it + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tiocgwinsz) { + var winsize = stream.tty.ops.ioctl_tiocgwinsz( + stream.tty + ); + var argp = syscallGetVarargP(); + HEAP16[argp >> 1] = winsize[0]; + HEAP16[(argp + 2) >> 1] = winsize[1]; + } + return 0; + } + + case 21524: { + // TODO: technically, this ioctl call should change the window size. + // but, since emscripten doesn't have any concept of a terminal window + // yet, we'll just silently throw it away as we do TIOCGWINSZ + if (!stream.tty) return -59; + return 0; + } + + case 21515: { + if (!stream.tty) return -59; + return 0; + } + + default: + return -28; + } + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_ioctl.sig = 'iiip'; + + function ___syscall_listen(fd, backlog) { + try { + var sock = getSocketFromFD(fd); + sock.sock_ops.listen(sock, backlog); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_listen.sig = 'iiiiiii'; + + function ___syscall_lstat64(path, buf) { + try { + path = SYSCALLS.getStr(path); + return SYSCALLS.writeStat(buf, FS.lstat(path)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_lstat64.sig = 'ipp'; + + function ___syscall_mkdirat(dirfd, path, mode) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + FS.mkdir(path, mode, 0); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_mkdirat.sig = 'iipi'; + + function ___syscall_newfstatat(dirfd, path, buf, flags) { + try { + path = SYSCALLS.getStr(path); + var nofollow = flags & 256; + var allowEmpty = flags & 4096; + flags = flags & ~6400; + path = SYSCALLS.calculateAt(dirfd, path, allowEmpty); + return SYSCALLS.writeStat( + buf, + nofollow ? FS.lstat(path) : FS.stat(path) + ); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_newfstatat.sig = 'iippi'; + + function ___syscall_openat(dirfd, path, flags, varargs) { + SYSCALLS.varargs = varargs; + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + var mode = varargs ? syscallGetVarargI() : 0; + return FS.open(path, flags, mode).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_openat.sig = 'iipip'; + + var PIPEFS = { + BUCKET_BUFFER_SIZE: 8192, + mount(mount) { + // Do not pollute the real root directory or its child nodes with pipes + // Looks like it is OK to create another pseudo-root node not linked to the FS.root hierarchy this way + return FS.createNode(null, '/', 16384 | 511, 0); + }, + createPipe() { + var pipe = { + buckets: [], + // refcnt 2 because pipe has a read end and a write end. We need to be + // able to read from the read end after write end is closed. + refcnt: 2, + timestamp: new Date(), + }; + pipe.buckets.push({ + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: 0, + roffset: 0, + }); + var rName = PIPEFS.nextname(); + var wName = PIPEFS.nextname(); + var rNode = FS.createNode(PIPEFS.root, rName, 4096, 0); + var wNode = FS.createNode(PIPEFS.root, wName, 4096, 0); + rNode.pipe = pipe; + wNode.pipe = pipe; + var readableStream = FS.createStream({ + path: rName, + node: rNode, + flags: 0, + seekable: false, + stream_ops: PIPEFS.stream_ops, + }); + rNode.stream = readableStream; + var writableStream = FS.createStream({ + path: wName, + node: wNode, + flags: 1, + seekable: false, + stream_ops: PIPEFS.stream_ops, + }); + wNode.stream = writableStream; + return { + readable_fd: readableStream.fd, + writable_fd: writableStream.fd, + }; + }, + stream_ops: { + getattr(stream) { + var node = stream.node; + var timestamp = node.pipe.timestamp; + return { + dev: 14, + ino: node.id, + mode: 4480, + nlink: 1, + uid: 0, + gid: 0, + rdev: 0, + size: 0, + atime: timestamp, + mtime: timestamp, + ctime: timestamp, + blksize: 4096, + blocks: 0, + }; + }, + poll(stream) { + var pipe = stream.node.pipe; + if ((stream.flags & 2097155) === 1) { + return 256 | 4; + } + for (var bucket of pipe.buckets) { + if (bucket.offset - bucket.roffset > 0) { + return 64 | 1; + } + } + return 0; + }, + dup(stream) { + stream.node.pipe.refcnt++; + }, + ioctl(stream, request, varargs) { + return 28; + }, + fsync(stream) { + return 28; + }, + read(stream, buffer, offset, length, position) { + var pipe = stream.node.pipe; + var currentLength = 0; + for (var bucket of pipe.buckets) { + currentLength += bucket.offset - bucket.roffset; + } + var data = buffer.subarray(offset, offset + length); + if (length <= 0) { + return 0; + } + if (currentLength == 0) { + if (pipe.refcnt < 2) { + return 0; + } + throw new FS.ErrnoError(6); + } + var toRead = Math.min(currentLength, length); + var totalRead = toRead; + var toRemove = 0; + for (var bucket of pipe.buckets) { + var bucketSize = bucket.offset - bucket.roffset; + if (toRead <= bucketSize) { + var tmpSlice = bucket.buffer.subarray( + bucket.roffset, + bucket.offset + ); + if (toRead < bucketSize) { + tmpSlice = tmpSlice.subarray(0, toRead); + bucket.roffset += toRead; + } else { + toRemove++; + } + data.set(tmpSlice); + break; + } else { + var tmpSlice = bucket.buffer.subarray( + bucket.roffset, + bucket.offset + ); + data.set(tmpSlice); + data = data.subarray(tmpSlice.byteLength); + toRead -= tmpSlice.byteLength; + toRemove++; + } + } + if (toRemove && toRemove == pipe.buckets.length) { + // Do not generate excessive garbage in use cases such as + // write several bytes, read everything, write several bytes, read everything... + toRemove--; + pipe.buckets[toRemove].offset = 0; + pipe.buckets[toRemove].roffset = 0; + } + pipe.buckets.splice(0, toRemove); + return totalRead; + }, + write(stream, buffer, offset, length, position) { + var pipe = stream.node.pipe; + var data = buffer.subarray(offset, offset + length); + var dataLen = data.byteLength; + if (dataLen <= 0) { + return 0; + } + var currBucket = null; + if (pipe.buckets.length == 0) { + currBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: 0, + roffset: 0, + }; + pipe.buckets.push(currBucket); + } else { + currBucket = pipe.buckets[pipe.buckets.length - 1]; + } + var freeBytesInCurrBuffer = + PIPEFS.BUCKET_BUFFER_SIZE - currBucket.offset; + if (freeBytesInCurrBuffer >= dataLen) { + currBucket.buffer.set(data, currBucket.offset); + currBucket.offset += dataLen; + return dataLen; + } else if (freeBytesInCurrBuffer > 0) { + currBucket.buffer.set( + data.subarray(0, freeBytesInCurrBuffer), + currBucket.offset + ); + currBucket.offset += freeBytesInCurrBuffer; + data = data.subarray( + freeBytesInCurrBuffer, + data.byteLength + ); + } + var numBuckets = + (data.byteLength / PIPEFS.BUCKET_BUFFER_SIZE) | 0; + var remElements = data.byteLength % PIPEFS.BUCKET_BUFFER_SIZE; + for (var i = 0; i < numBuckets; i++) { + var newBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: PIPEFS.BUCKET_BUFFER_SIZE, + roffset: 0, + }; + pipe.buckets.push(newBucket); + newBucket.buffer.set( + data.subarray(0, PIPEFS.BUCKET_BUFFER_SIZE) + ); + data = data.subarray( + PIPEFS.BUCKET_BUFFER_SIZE, + data.byteLength + ); + } + if (remElements > 0) { + var newBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: data.byteLength, + roffset: 0, + }; + pipe.buckets.push(newBucket); + newBucket.buffer.set(data); + } + return dataLen; + }, + close(stream) { + var pipe = stream.node.pipe; + pipe.refcnt--; + if (pipe.refcnt === 0) { + pipe.buckets = null; + } + }, + }, + nextname() { + if (!PIPEFS.nextname.current) { + PIPEFS.nextname.current = 0; + } + return 'pipe[' + PIPEFS.nextname.current++ + ']'; + }, + }; + + function ___syscall_pipe(fdPtr) { + try { + if (fdPtr == 0) { + throw new FS.ErrnoError(21); + } + var res = PIPEFS.createPipe(); + HEAP32[fdPtr >> 2] = res.readable_fd; + HEAP32[(fdPtr + 4) >> 2] = res.writable_fd; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_pipe.sig = 'ip'; + + function ___syscall_readlinkat(dirfd, path, buf, bufsize) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (bufsize <= 0) return -28; + var ret = FS.readlink(path); + var len = Math.min(bufsize, lengthBytesUTF8(ret)); + var endChar = HEAP8[buf + len]; + stringToUTF8(ret, buf, bufsize + 1); + // readlink is one of the rare functions that write out a C string, but does never append a null to the output buffer(!) + // stringToUTF8() always appends a null byte, so restore the character under the null byte after the write. + HEAP8[buf + len] = endChar; + return len; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_readlinkat.sig = 'iippp'; + + function ___syscall_recvfrom(fd, buf, len, flags, addr, addrlen) { + try { + var sock = getSocketFromFD(fd); + var msg = sock.sock_ops.recvmsg( + sock, + len, + typeof flags !== 'undefined' ? flags : 0 + ); + if (!msg) return 0; + // socket is closed + if (addr) { + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(msg.addr), + msg.port, + addrlen + ); + } + HEAPU8.set(msg.buffer, buf); + return msg.buffer.byteLength; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_recvfrom.sig = 'iippipp'; + + function ___syscall_renameat(olddirfd, oldpath, newdirfd, newpath) { + try { + oldpath = SYSCALLS.getStr(oldpath); + newpath = SYSCALLS.getStr(newpath); + oldpath = SYSCALLS.calculateAt(olddirfd, oldpath); + newpath = SYSCALLS.calculateAt(newdirfd, newpath); + FS.rename(oldpath, newpath); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_renameat.sig = 'iipip'; + + function ___syscall_rmdir(path) { + try { + path = SYSCALLS.getStr(path); + FS.rmdir(path); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_rmdir.sig = 'ip'; + + function ___syscall_sendto(fd, message, length, flags, addr, addr_len) { + try { + var sock = getSocketFromFD(fd); + if (!addr) { + // send, no address provided + return FS.write(sock.stream, HEAP8, message, length); + } + var dest = getSocketAddress(addr, addr_len); + // sendto an address + return sock.sock_ops.sendmsg( + sock, + HEAP8, + message, + length, + dest.addr, + dest.port + ); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_sendto.sig = 'iippipp'; + + function ___syscall_socket(domain, type, protocol) { + try { + var sock = SOCKFS.createSocket(domain, type, protocol); + return sock.stream.fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_socket.sig = 'iiiiiii'; + + function ___syscall_stat64(path, buf) { + try { + path = SYSCALLS.getStr(path); + return SYSCALLS.writeStat(buf, FS.stat(path)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_stat64.sig = 'ipp'; + + function ___syscall_statfs64(path, size, buf) { + try { + SYSCALLS.writeStatFs(buf, FS.statfs(SYSCALLS.getStr(path))); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_statfs64.sig = 'ippp'; + + function ___syscall_symlinkat(target, dirfd, linkpath) { + try { + target = SYSCALLS.getStr(target); + linkpath = SYSCALLS.getStr(linkpath); + linkpath = SYSCALLS.calculateAt(dirfd, linkpath); + FS.symlink(target, linkpath); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_symlinkat.sig = 'ipip'; + + function ___syscall_unlinkat(dirfd, path, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (!flags) { + FS.unlink(path); + } else if (flags === 512) { + FS.rmdir(path); + } else { + return -28; + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_unlinkat.sig = 'iipi'; + + var readI53FromI64 = (ptr) => + HEAPU32[ptr >> 2] + HEAP32[(ptr + 4) >> 2] * 4294967296; + + function ___syscall_utimensat(dirfd, path, times, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path, true); + var now = Date.now(), + atime, + mtime; + if (!times) { + atime = now; + mtime = now; + } else { + var seconds = readI53FromI64(times); + var nanoseconds = HEAP32[(times + 8) >> 2]; + if (nanoseconds == 1073741823) { + atime = now; + } else if (nanoseconds == 1073741822) { + atime = null; + } else { + atime = seconds * 1e3 + nanoseconds / (1e3 * 1e3); + } + times += 16; + seconds = readI53FromI64(times); + nanoseconds = HEAP32[(times + 8) >> 2]; + if (nanoseconds == 1073741823) { + mtime = now; + } else if (nanoseconds == 1073741822) { + mtime = null; + } else { + mtime = seconds * 1e3 + nanoseconds / (1e3 * 1e3); + } + } + // null here means UTIME_OMIT was passed. If both were set to UTIME_OMIT then + // we can skip the call completely. + if ((mtime ?? atime) !== null) { + FS.utime(path, atime, mtime); + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_utimensat.sig = 'iippi'; + + var __abort_js = () => abort(''); + + __abort_js.sig = 'v'; + + var dlSetError = (msg) => { + var sp = stackSave(); + var cmsg = stringToUTF8OnStack(msg); + ___dl_seterr(cmsg, 0); + stackRestore(sp); + }; + + var dlopenInternal = (handle, jsflags) => { + // void *dlopen(const char *file, int mode); + // http://pubs.opengroup.org/onlinepubs/009695399/functions/dlopen.html + var filename = UTF8ToString(handle + 36); + var flags = HEAP32[(handle + 4) >> 2]; + filename = PATH.normalize(filename); + var global = Boolean(flags & 256); + var localScope = global ? null : {}; + // We don't care about RTLD_NOW and RTLD_LAZY. + var combinedFlags = { + global, + nodelete: Boolean(flags & 4096), + loadAsync: jsflags.loadAsync, + }; + if (jsflags.loadAsync) { + return loadDynamicLibrary( + filename, + combinedFlags, + localScope, + handle + ); + } + try { + return loadDynamicLibrary( + filename, + combinedFlags, + localScope, + handle + ); + } catch (e) { + dlSetError(`could not load dynamic lib: ${filename}\n${e}`); + return 0; + } + }; + + function __dlopen_js(handle) { + var jsflags = { + loadAsync: false, + }; + return dlopenInternal(handle, jsflags); + } + + __dlopen_js.sig = 'pp'; + + var __dlsym_js = (handle, symbol, symbolIndex) => { + // void *dlsym(void *restrict handle, const char *restrict name); + // http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html + symbol = UTF8ToString(symbol); + var result; + var newSymIndex; + var lib = LDSO.loadedLibsByHandle[handle]; + newSymIndex = Object.keys(lib.exports).indexOf(symbol); + if (newSymIndex == -1 || lib.exports[symbol].stub) { + dlSetError( + `Tried to lookup unknown symbol "${symbol}" in dynamic lib: ${lib.name}` + ); + return 0; + } + result = lib.exports[symbol]; + if (typeof result == 'function') { + // Asyncify wraps exports, and we need to look through those wrappers. + if (result.orig) { + result = result.orig; + } + var addr = getFunctionAddress(result); + if (addr) { + result = addr; + } else { + // Insert the function into the wasm table. If its a direct wasm + // function the second argument will not be needed. If its a JS + // function we rely on the `sig` attribute being set based on the + // `__sig` specified in library JS file. + result = addFunction(result, result.sig); + HEAPU32[symbolIndex >> 2] = newSymIndex; + } + } + return result; + }; + + __dlsym_js.sig = 'pppp'; + + var __emscripten_lookup_name = (name) => { + // uint32_t _emscripten_lookup_name(const char *name); + var nameString = UTF8ToString(name); + return inetPton4(DNS.lookup_name(nameString)); + }; + + __emscripten_lookup_name.sig = 'ip'; + + var runtimeKeepaliveCounter = 0; + + var __emscripten_runtime_keepalive_clear = () => { + noExitRuntime = false; + runtimeKeepaliveCounter = 0; + }; + + __emscripten_runtime_keepalive_clear.sig = 'v'; + + var __emscripten_throw_longjmp = () => { + throw Infinity; + }; + + __emscripten_throw_longjmp.sig = 'v'; + + function __gmtime_js(time, tmPtr) { + time = bigintToI53Checked(time); + var date = new Date(time * 1e3); + HEAP32[tmPtr >> 2] = date.getUTCSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getUTCMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getUTCHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getUTCDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getUTCMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getUTCFullYear() - 1900; + HEAP32[(tmPtr + 24) >> 2] = date.getUTCDay(); + var start = Date.UTC(date.getUTCFullYear(), 0, 1, 0, 0, 0, 0); + var yday = ((date.getTime() - start) / (1e3 * 60 * 60 * 24)) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + } + + __gmtime_js.sig = 'vjp'; + + var isLeapYear = (year) => + year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); + + var MONTH_DAYS_LEAP_CUMULATIVE = [ + 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, + ]; + + var MONTH_DAYS_REGULAR_CUMULATIVE = [ + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, + ]; + + var ydayFromDate = (date) => { + var leap = isLeapYear(date.getFullYear()); + var monthDaysCumulative = leap + ? MONTH_DAYS_LEAP_CUMULATIVE + : MONTH_DAYS_REGULAR_CUMULATIVE; + var yday = monthDaysCumulative[date.getMonth()] + date.getDate() - 1; + // -1 since it's days since Jan 1 + return yday; + }; + + function __localtime_js(time, tmPtr) { + time = bigintToI53Checked(time); + var date = new Date(time * 1e3); + HEAP32[tmPtr >> 2] = date.getSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getFullYear() - 1900; + HEAP32[(tmPtr + 24) >> 2] = date.getDay(); + var yday = ydayFromDate(date) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + HEAP32[(tmPtr + 36) >> 2] = -(date.getTimezoneOffset() * 60); + // Attention: DST is in December in South, and some regions don't have DST at all. + var start = new Date(date.getFullYear(), 0, 1); + var summerOffset = new Date( + date.getFullYear(), + 6, + 1 + ).getTimezoneOffset(); + var winterOffset = start.getTimezoneOffset(); + var dst = + (summerOffset != winterOffset && + date.getTimezoneOffset() == + Math.min(winterOffset, summerOffset)) | 0; + HEAP32[(tmPtr + 32) >> 2] = dst; + } + + __localtime_js.sig = 'vjp'; + + var __mktime_js = function (tmPtr) { + var ret = (() => { + var date = new Date( + HEAP32[(tmPtr + 20) >> 2] + 1900, + HEAP32[(tmPtr + 16) >> 2], + HEAP32[(tmPtr + 12) >> 2], + HEAP32[(tmPtr + 8) >> 2], + HEAP32[(tmPtr + 4) >> 2], + HEAP32[tmPtr >> 2], + 0 + ); + // There's an ambiguous hour when the time goes back; the tm_isdst field is + // used to disambiguate it. Date() basically guesses, so we fix it up if it + // guessed wrong, or fill in tm_isdst with the guess if it's -1. + var dst = HEAP32[(tmPtr + 32) >> 2]; + var guessedOffset = date.getTimezoneOffset(); + var start = new Date(date.getFullYear(), 0, 1); + var summerOffset = new Date( + date.getFullYear(), + 6, + 1 + ).getTimezoneOffset(); + var winterOffset = start.getTimezoneOffset(); + var dstOffset = Math.min(winterOffset, summerOffset); + // DST is in December in South + if (dst < 0) { + // Attention: some regions don't have DST at all. + HEAP32[(tmPtr + 32) >> 2] = Number( + summerOffset != winterOffset && dstOffset == guessedOffset + ); + } else if (dst > 0 != (dstOffset == guessedOffset)) { + var nonDstOffset = Math.max(winterOffset, summerOffset); + var trueOffset = dst > 0 ? dstOffset : nonDstOffset; + // Don't try setMinutes(date.getMinutes() + ...) -- it's messed up. + date.setTime( + date.getTime() + (trueOffset - guessedOffset) * 6e4 + ); + } + HEAP32[(tmPtr + 24) >> 2] = date.getDay(); + var yday = ydayFromDate(date) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + // To match expected behavior, update fields from date + HEAP32[tmPtr >> 2] = date.getSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getYear(); + var timeMs = date.getTime(); + if (isNaN(timeMs)) { + return -1; + } + // Return time in microseconds + return timeMs / 1e3; + })(); + return BigInt(ret); + }; + + __mktime_js.sig = 'jp'; + + function __mmap_js(len, prot, flags, fd, offset, allocated, addr) { + offset = bigintToI53Checked(offset); + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var res = FS.mmap(stream, len, offset, prot, flags); + var ptr = res.ptr; + HEAP32[allocated >> 2] = res.allocated; + HEAPU32[addr >> 2] = ptr; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + __mmap_js.sig = 'ipiiijpp'; + + function __munmap_js(addr, len, prot, flags, fd, offset) { + offset = bigintToI53Checked(offset); + try { + var stream = SYSCALLS.getStreamFromFD(fd); + if (prot & 2) { + SYSCALLS.doMsync(addr, stream, len, flags, offset); + } + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + __munmap_js.sig = 'ippiiij'; + + var timers = {}; + + var handleException = (e) => { + // Certain exception types we do not treat as errors since they are used for + // internal control flow. + // 1. ExitStatus, which is thrown by exit() + // 2. "unwind", which is thrown by emscripten_unwind_to_js_event_loop() and others + // that wish to return to JS event loop. + if (e instanceof ExitStatus || e == 'unwind') { + return EXITSTATUS; + } + quit_(1, e); + }; + + var keepRuntimeAlive = () => noExitRuntime || runtimeKeepaliveCounter > 0; + + var _proc_exit = (code) => { + EXITSTATUS = code; + if (!keepRuntimeAlive()) { + Module['onExit']?.(code); + ABORT = true; + } + quit_(code, new ExitStatus(code)); + }; + + _proc_exit.sig = 'vi'; + + /** @param {boolean|number=} implicit */ var exitJS = ( + status, + implicit + ) => { + EXITSTATUS = status; + if (!keepRuntimeAlive()) { + exitRuntime(); + } + _proc_exit(status); + }; + + var _exit = exitJS; + + _exit.sig = 'vi'; + + var maybeExit = () => { + if (runtimeExited) { + return; + } + if (!keepRuntimeAlive()) { + try { + _exit(EXITSTATUS); + } catch (e) { + handleException(e); + } + } + }; + + var callUserCallback = (func) => { + if (runtimeExited || ABORT) { + return; + } + try { + func(); + maybeExit(); + } catch (e) { + handleException(e); + } + }; + + var _emscripten_get_now = () => performance.now(); + + _emscripten_get_now.sig = 'd'; + + var __setitimer_js = (which, timeout_ms) => { + // First, clear any existing timer. + if (timers[which]) { + clearTimeout(timers[which].id); + delete timers[which]; + } + // A timeout of zero simply cancels the current timeout so we have nothing + // more to do. + if (!timeout_ms) return 0; + var id = setTimeout(() => { + delete timers[which]; + callUserCallback(() => + __emscripten_timeout(which, _emscripten_get_now()) + ); + }, timeout_ms); + timers[which] = { + id, + timeout_ms, + }; + return 0; + }; + + __setitimer_js.sig = 'iid'; + + var __tzset_js = (timezone, daylight, std_name, dst_name) => { + // TODO: Use (malleable) environment variables instead of system settings. + var currentYear = new Date().getFullYear(); + var winter = new Date(currentYear, 0, 1); + var summer = new Date(currentYear, 6, 1); + var winterOffset = winter.getTimezoneOffset(); + var summerOffset = summer.getTimezoneOffset(); + // Local standard timezone offset. Local standard time is not adjusted for + // daylight savings. This code uses the fact that getTimezoneOffset returns + // a greater value during Standard Time versus Daylight Saving Time (DST). + // Thus it determines the expected output during Standard Time, and it + // compares whether the output of the given date the same (Standard) or less + // (DST). + var stdTimezoneOffset = Math.max(winterOffset, summerOffset); + // timezone is specified as seconds west of UTC ("The external variable + // `timezone` shall be set to the difference, in seconds, between + // Coordinated Universal Time (UTC) and local standard time."), the same + // as returned by stdTimezoneOffset. + // See http://pubs.opengroup.org/onlinepubs/009695399/functions/tzset.html + HEAPU32[timezone >> 2] = stdTimezoneOffset * 60; + HEAP32[daylight >> 2] = Number(winterOffset != summerOffset); + var extractZone = (timezoneOffset) => { + // Why inverse sign? + // Read here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset + var sign = timezoneOffset >= 0 ? '-' : '+'; + var absOffset = Math.abs(timezoneOffset); + var hours = String(Math.floor(absOffset / 60)).padStart(2, '0'); + var minutes = String(absOffset % 60).padStart(2, '0'); + return `UTC${sign}${hours}${minutes}`; + }; + var winterName = extractZone(winterOffset); + var summerName = extractZone(summerOffset); + if (summerOffset < winterOffset) { + // Northern hemisphere + stringToUTF8(winterName, std_name, 17); + stringToUTF8(summerName, dst_name, 17); + } else { + stringToUTF8(winterName, dst_name, 17); + stringToUTF8(summerName, std_name, 17); + } + }; + + __tzset_js.sig = 'vpppp'; + + var _emscripten_date_now = () => Date.now(); + + _emscripten_date_now.sig = 'd'; + + var nowIsMonotonic = 1; + + var checkWasiClock = (clock_id) => clock_id >= 0 && clock_id <= 3; + + function _clock_time_get(clk_id, ignored_precision, ptime) { + ignored_precision = bigintToI53Checked(ignored_precision); + if (!checkWasiClock(clk_id)) { + return 28; + } + var now; + // all wasi clocks but realtime are monotonic + if (clk_id === 0) { + now = _emscripten_date_now(); + } else if (nowIsMonotonic) { + now = _emscripten_get_now(); + } else { + return 52; + } + // "now" is in ms, and wasi times are in ns. + var nsec = Math.round(now * 1e3 * 1e3); + HEAP64[ptime >> 3] = BigInt(nsec); + return 0; + } + + _clock_time_get.sig = 'iijp'; + + var getHeapMax = () => + // Stay one Wasm page short of 4GB: while e.g. Chrome is able to allocate + // full 4GB Wasm memories, the size will wrap back to 0 bytes in Wasm side + // for any code that deals with heap sizes, which would require special + // casing all heap size related code to treat 0 specially. + 2147483648; + + var _emscripten_get_heap_max = () => getHeapMax(); + + _emscripten_get_heap_max.sig = 'p'; + + var growMemory = (size) => { + var oldHeapSize = wasmMemory.buffer.byteLength; + var pages = ((size - oldHeapSize + 65535) / 65536) | 0; + try { + // round size grow request up to wasm page size (fixed 64KB per spec) + wasmMemory.grow(pages); + // .grow() takes a delta compared to the previous size + updateMemoryViews(); + return 1; + } catch (e) {} + }; + + var _emscripten_resize_heap = (requestedSize) => { + var oldSize = HEAPU8.length; + // With CAN_ADDRESS_2GB or MEMORY64, pointers are already unsigned. + requestedSize >>>= 0; + // With multithreaded builds, races can happen (another thread might increase the size + // in between), so return a failure, and let the caller retry. + // Memory resize rules: + // 1. Always increase heap size to at least the requested size, rounded up + // to next page multiple. + // 2a. If MEMORY_GROWTH_LINEAR_STEP == -1, excessively resize the heap + // geometrically: increase the heap size according to + // MEMORY_GROWTH_GEOMETRIC_STEP factor (default +20%), At most + // overreserve by MEMORY_GROWTH_GEOMETRIC_CAP bytes (default 96MB). + // 2b. If MEMORY_GROWTH_LINEAR_STEP != -1, excessively resize the heap + // linearly: increase the heap size by at least + // MEMORY_GROWTH_LINEAR_STEP bytes. + // 3. Max size for the heap is capped at 2048MB-WASM_PAGE_SIZE, or by + // MAXIMUM_MEMORY, or by ASAN limit, depending on which is smallest + // 4. If we were unable to allocate as much memory, it may be due to + // over-eager decision to excessively reserve due to (3) above. + // Hence if an allocation fails, cut down on the amount of excess + // growth, in an attempt to succeed to perform a smaller allocation. + // A limit is set for how much we can grow. We should not exceed that + // (the wasm binary specifies it, so if we tried, we'd fail anyhow). + var maxHeapSize = getHeapMax(); + if (requestedSize > maxHeapSize) { + return false; + } + // Loop through potential heap size increases. If we attempt a too eager + // reservation that fails, cut down on the attempted size and reserve a + // smaller bump instead. (max 3 times, chosen somewhat arbitrarily) + for (var cutDown = 1; cutDown <= 4; cutDown *= 2) { + var overGrownHeapSize = oldSize * (1 + 0.2 / cutDown); + // ensure geometric growth + // but limit overreserving (default to capping at +96MB overgrowth at most) + overGrownHeapSize = Math.min( + overGrownHeapSize, + requestedSize + 100663296 + ); + var newSize = Math.min( + maxHeapSize, + alignMemory(Math.max(requestedSize, overGrownHeapSize), 65536) + ); + var replacement = growMemory(newSize); + if (replacement) { + return true; + } + } + return false; + }; + + _emscripten_resize_heap.sig = 'ip'; + + var runtimeKeepalivePush = () => { + runtimeKeepaliveCounter += 1; + }; + + runtimeKeepalivePush.sig = 'v'; + + var runtimeKeepalivePop = () => { + runtimeKeepaliveCounter -= 1; + }; + + runtimeKeepalivePop.sig = 'v'; + + /** @param {number=} timeout */ var safeSetTimeout = (func, timeout) => { + runtimeKeepalivePush(); + return setTimeout(() => { + runtimeKeepalivePop(); + callUserCallback(func); + }, timeout); + }; + + var _emscripten_sleep = (ms) => + Asyncify.handleSleep((wakeUp) => safeSetTimeout(wakeUp, ms)); + + _emscripten_sleep.sig = 'vi'; + + _emscripten_sleep.isAsync = true; + + var ENV = PHPLoader.ENV || {}; + + var getExecutableName = () => thisProgram || './this.program'; + + var getEnvStrings = () => { + if (!getEnvStrings.strings) { + // Default values. + // Browser language detection #8751 + var lang = + ( + (typeof navigator == 'object' && navigator.language) || + 'C' + ).replace('-', '_') + '.UTF-8'; + var env = { + USER: 'web_user', + LOGNAME: 'web_user', + PATH: '/', + PWD: '/', + HOME: '/home/web_user', + LANG: lang, + _: getExecutableName(), + }; + // Apply the user-provided values, if any. + for (var x in ENV) { + // x is a key in ENV; if ENV[x] is undefined, that means it was + // explicitly set to be so. We allow user code to do that to + // force variables with default values to remain unset. + if (ENV[x] === undefined) delete env[x]; + else env[x] = ENV[x]; + } + var strings = []; + for (var x in env) { + strings.push(`${x}=${env[x]}`); + } + getEnvStrings.strings = strings; + } + return getEnvStrings.strings; + }; + + var _environ_get = (__environ, environ_buf) => { + var bufSize = 0; + var envp = 0; + for (var string of getEnvStrings()) { + var ptr = environ_buf + bufSize; + HEAPU32[(__environ + envp) >> 2] = ptr; + bufSize += stringToUTF8(string, ptr, Infinity) + 1; + envp += 4; + } + return 0; + }; + + _environ_get.sig = 'ipp'; + + var _environ_sizes_get = (penviron_count, penviron_buf_size) => { + var strings = getEnvStrings(); + HEAPU32[penviron_count >> 2] = strings.length; + var bufSize = 0; + for (var string of strings) { + bufSize += lengthBytesUTF8(string) + 1; + } + HEAPU32[penviron_buf_size >> 2] = bufSize; + return 0; + }; + + _environ_sizes_get.sig = 'ipp'; + + function _fd_fdstat_get(fd, pbuf) { + try { + var rightsBase = 0; + var rightsInheriting = 0; + var flags = 0; + { + var stream = SYSCALLS.getStreamFromFD(fd); + // All character devices are terminals (other things a Linux system would + // assume is a character device, like the mouse, we have special APIs for). + var type = stream.tty + ? 2 + : FS.isDir(stream.mode) + ? 3 + : FS.isLink(stream.mode) + ? 7 + : 4; + } + HEAP8[pbuf] = type; + HEAP16[(pbuf + 2) >> 1] = flags; + HEAP64[(pbuf + 8) >> 3] = BigInt(rightsBase); + HEAP64[(pbuf + 16) >> 3] = BigInt(rightsInheriting); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + + _fd_fdstat_get.sig = 'iip'; + + /** @param {number=} offset */ var doReadv = ( + stream, + iov, + iovcnt, + offset + ) => { + var ret = 0; + for (var i = 0; i < iovcnt; i++) { + var ptr = HEAPU32[iov >> 2]; + var len = HEAPU32[(iov + 4) >> 2]; + iov += 8; + var curr = FS.read(stream, HEAP8, ptr, len, offset); + if (curr < 0) return -1; + ret += curr; + if (curr < len) break; + // nothing more to read + if (typeof offset != 'undefined') { + offset += curr; + } + } + return ret; + }; + + function _fd_read(fd, iov, iovcnt, pnum) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var num = doReadv(stream, iov, iovcnt); + HEAPU32[pnum >> 2] = num; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + + _fd_read.sig = 'iippp'; + + function _fd_seek(fd, offset, whence, newOffset) { + offset = bigintToI53Checked(offset); + try { + if (isNaN(offset)) return 61; + var stream = SYSCALLS.getStreamFromFD(fd); + FS.llseek(stream, offset, whence); + HEAP64[newOffset >> 3] = BigInt(stream.position); + if (stream.getdents && offset === 0 && whence === 0) + stream.getdents = null; + // reset readdir state + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + + _fd_seek.sig = 'iijip'; + + var _fd_sync = function (fd) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + return Asyncify.handleSleep((wakeUp) => { + var mount = stream.node.mount; + if (!mount.type.syncfs) { + // We write directly to the file system, so there's nothing to do here. + wakeUp(0); + return; + } + mount.type.syncfs(mount, false, (err) => { + wakeUp(err ? 29 : 0); + }); + }); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + }; + + _fd_sync.sig = 'ii'; + + _fd_sync.isAsync = true; + + /** @param {number=} offset */ var doWritev = ( + stream, + iov, + iovcnt, + offset + ) => { + var ret = 0; + for (var i = 0; i < iovcnt; i++) { + var ptr = HEAPU32[iov >> 2]; + var len = HEAPU32[(iov + 4) >> 2]; + iov += 8; + var curr = FS.write(stream, HEAP8, ptr, len, offset); + if (curr < 0) return -1; + ret += curr; + if (curr < len) { + // No more space to write. + break; + } + if (typeof offset != 'undefined') { + offset += curr; + } + } + return ret; + }; + + function _fd_write(fd, iov, iovcnt, pnum) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var num = doWritev(stream, iov, iovcnt); + HEAPU32[pnum >> 2] = num; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + + _fd_write.sig = 'iippp'; + + var _getaddrinfo = (node, service, hint, out) => { + var addr = 0; + var port = 0; + var flags = 0; + var family = 0; + var type = 0; + var proto = 0; + var ai; + function allocaddrinfo(family, type, proto, canon, addr, port) { + var sa, salen, ai; + var errno; + salen = family === 10 ? 28 : 16; + addr = family === 10 ? inetNtop6(addr) : inetNtop4(addr); + sa = _malloc(salen); + errno = writeSockaddr(sa, family, addr, port); + ai = _malloc(32); + HEAP32[(ai + 4) >> 2] = family; + HEAP32[(ai + 8) >> 2] = type; + HEAP32[(ai + 12) >> 2] = proto; + HEAPU32[(ai + 24) >> 2] = canon; + HEAPU32[(ai + 20) >> 2] = sa; + if (family === 10) { + HEAP32[(ai + 16) >> 2] = 28; + } else { + HEAP32[(ai + 16) >> 2] = 16; + } + HEAP32[(ai + 28) >> 2] = 0; + return ai; + } + if (hint) { + flags = HEAP32[hint >> 2]; + family = HEAP32[(hint + 4) >> 2]; + type = HEAP32[(hint + 8) >> 2]; + proto = HEAP32[(hint + 12) >> 2]; + } + if (type && !proto) { + proto = type === 2 ? 17 : 6; + } + if (!type && proto) { + type = proto === 17 ? 2 : 1; + } + // If type or proto are set to zero in hints we should really be returning multiple addrinfo values, but for + // now default to a TCP STREAM socket so we can at least return a sensible addrinfo given NULL hints. + if (proto === 0) { + proto = 6; + } + if (type === 0) { + type = 1; + } + if (!node && !service) { + return -2; + } + if (flags & ~(1 | 2 | 4 | 1024 | 8 | 16 | 32)) { + return -1; + } + if (hint !== 0 && HEAP32[hint >> 2] & 2 && !node) { + return -1; + } + if (flags & 32) { + // TODO + return -2; + } + if (type !== 0 && type !== 1 && type !== 2) { + return -7; + } + if (family !== 0 && family !== 2 && family !== 10) { + return -6; + } + if (service) { + service = UTF8ToString(service); + port = parseInt(service, 10); + if (isNaN(port)) { + if (flags & 1024) { + return -2; + } + // TODO support resolving well-known service names from: + // http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt + return -8; + } + } + if (!node) { + if (family === 0) { + family = 2; + } + if ((flags & 1) === 0) { + if (family === 2) { + addr = _htonl(2130706433); + } else { + addr = [0, 0, 0, _htonl(1)]; + } + } + ai = allocaddrinfo(family, type, proto, null, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + } + // try as a numeric address + node = UTF8ToString(node); + addr = inetPton4(node); + if (addr !== null) { + // incoming node is a valid ipv4 address + if (family === 0 || family === 2) { + family = 2; + } else if (family === 10 && flags & 8) { + addr = [0, 0, _htonl(65535), addr]; + family = 10; + } else { + return -2; + } + } else { + addr = inetPton6(node); + if (addr !== null) { + // incoming node is a valid ipv6 address + if (family === 0 || family === 10) { + family = 10; + } else { + return -2; + } + } + } + if (addr != null) { + ai = allocaddrinfo(family, type, proto, node, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + } + if (flags & 4) { + return -2; + } + // try as a hostname + // resolve the hostname to a temporary fake address + node = DNS.lookup_name(node); + addr = inetPton4(node); + if (family === 0) { + family = 2; + } else if (family === 10) { + addr = [0, 0, _htonl(65535), addr]; + } + ai = allocaddrinfo(family, type, proto, null, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + }; + + _getaddrinfo.sig = 'ipppp'; + + var _getnameinfo = (sa, salen, node, nodelen, serv, servlen, flags) => { + var info = readSockaddr(sa, salen); + if (info.errno) { + return -6; + } + var port = info.port; + var addr = info.addr; + var overflowed = false; + if (node && nodelen) { + var lookup; + if (flags & 1 || !(lookup = DNS.lookup_addr(addr))) { + if (flags & 8) { + return -2; + } + } else { + addr = lookup; + } + var numBytesWrittenExclNull = stringToUTF8(addr, node, nodelen); + if (numBytesWrittenExclNull + 1 >= nodelen) { + overflowed = true; + } + } + if (serv && servlen) { + port = '' + port; + var numBytesWrittenExclNull = stringToUTF8(port, serv, servlen); + if (numBytesWrittenExclNull + 1 >= servlen) { + overflowed = true; + } + } + if (overflowed) { + // Note: even when we overflow, getnameinfo() is specced to write out the truncated results. + return -12; + } + return 0; + }; + + _getnameinfo.sig = 'ipipipii'; + + var Protocols = { + list: [], + map: {}, + }; + + var stringToAscii = (str, buffer) => { + for (var i = 0; i < str.length; ++i) { + HEAP8[buffer++] = str.charCodeAt(i); + } + // Null-terminate the string + HEAP8[buffer] = 0; + }; + + var _setprotoent = (stayopen) => { + // void setprotoent(int stayopen); + // Allocate and populate a protoent structure given a name, protocol number and array of aliases + function allocprotoent(name, proto, aliases) { + // write name into buffer + var nameBuf = _malloc(name.length + 1); + stringToAscii(name, nameBuf); + // write aliases into buffer + var j = 0; + var length = aliases.length; + var aliasListBuf = _malloc((length + 1) * 4); + // Use length + 1 so we have space for the terminating NULL ptr. + for (var i = 0; i < length; i++, j += 4) { + var alias = aliases[i]; + var aliasBuf = _malloc(alias.length + 1); + stringToAscii(alias, aliasBuf); + HEAPU32[(aliasListBuf + j) >> 2] = aliasBuf; + } + HEAPU32[(aliasListBuf + j) >> 2] = 0; + // Terminating NULL pointer. + // generate protoent + var pe = _malloc(12); + HEAPU32[pe >> 2] = nameBuf; + HEAPU32[(pe + 4) >> 2] = aliasListBuf; + HEAP32[(pe + 8) >> 2] = proto; + return pe; + } + // Populate the protocol 'database'. The entries are limited to tcp and udp, though it is fairly trivial + // to add extra entries from /etc/protocols if desired - though not sure if that'd actually be useful. + var list = Protocols.list; + var map = Protocols.map; + if (list.length === 0) { + var entry = allocprotoent('tcp', 6, ['TCP']); + list.push(entry); + map['tcp'] = map['6'] = entry; + entry = allocprotoent('udp', 17, ['UDP']); + list.push(entry); + map['udp'] = map['17'] = entry; + } + _setprotoent.index = 0; + }; + + _setprotoent.sig = 'vi'; + + var _getprotobyname = (name) => { + // struct protoent *getprotobyname(const char *); + name = UTF8ToString(name); + _setprotoent(true); + var result = Protocols.map[name]; + return result; + }; + + _getprotobyname.sig = 'pp'; + + var _getprotobynumber = (number) => { + // struct protoent *getprotobynumber(int proto); + _setprotoent(true); + var result = Protocols.map[number]; + return result; + }; + + _getprotobynumber.sig = 'pi'; + + function _js_flock(fd, op) { + if (typeof Module['userSpace'] === 'undefined') { + // In the absence of a real locking facility, + // return success by default as Emscripten does. + return 0; + } + return Module['userSpace'].flock(fd, op); + } + + function _js_open_process( + command, + argsPtr, + argsLength, + descriptorsPtr, + descriptorsLength, + cwdPtr, + cwdLength, + envPtr, + envLength + ) { + if (!command) { + ___errno_location(ERRNO_CODES.EINVAL); + return -1; + } + const cmdstr = UTF8ToString(command); + if (!cmdstr.length) { + ___errno_location(ERRNO_CODES.EINVAL); + return -1; + } + let argsArray = []; + if (argsLength) { + for (var i = 0; i < argsLength; i++) { + const charPointer = argsPtr + i * 4; + argsArray.push(UTF8ToString(HEAPU32[charPointer >> 2])); + } + } + const cwdstr = cwdPtr ? UTF8ToString(cwdPtr) : FS.cwd(); + let envObject = null; + if (envLength) { + envObject = {}; + for (var i = 0; i < envLength; i++) { + const envPointer = envPtr + i * 4; + const envEntry = UTF8ToString(HEAPU32[envPointer >> 2]); + const splitAt = envEntry.indexOf('='); + if (splitAt === -1) { + continue; + } + const key = envEntry.substring(0, splitAt); + const value = envEntry.substring(splitAt + 1); + envObject[key] = value; + } + } + var std = {}; + // Extracts an array of available descriptors that should be dispatched to streams. + // On the C side, the descriptors are expressed as `**int` so we must go read + // each of the `descriptorsLength` `*int` pointers and convert the associated data into + // a JavaScript object { descriptor : { child : fd, parent : fd } }. + for (var i = 0; i < descriptorsLength; i++) { + const descriptorPtr = HEAPU32[(descriptorsPtr + i * 4) >> 2]; + std[HEAPU32[descriptorPtr >> 2]] = { + child: HEAPU32[(descriptorPtr + 4) >> 2], + parent: HEAPU32[(descriptorPtr + 8) >> 2], + }; + // swap parent and child descs until we rebuild PHP 7.4 + if (i === 0) { + HEAPU32[(descriptorPtr + 8) >> 2] = + std[HEAPU32[descriptorPtr >> 2]].parent; + HEAPU32[(descriptorPtr + 4) >> 2] = + std[HEAPU32[descriptorPtr >> 2]].child; + } + } + return Asyncify.handleAsync(async () => { + let cp; + try { + const options = {}; + if (cwdstr !== null) { + options.cwd = cwdstr; + } + if (envObject !== null) { + options.env = envObject; + } + cp = PHPWASM.spawnProcess(cmdstr, argsArray, options); + if (cp instanceof Promise) { + cp = await cp; + } + } catch (e) { + if (e.code === 'SPAWN_UNSUPPORTED') { + ___errno_location(ERRNO_CODES.ENOSYS); + return -1; + } + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) + throw e; + ___errno_location(e.code); + return -1; + } + const ProcInfo = { + pid: cp.pid, + exited: false, + }; + PHPWASM.processTable[ProcInfo.pid] = ProcInfo; + const stdinParentFd = std[0]?.parent, + stdinChildFd = std[0]?.child, + stdoutChildFd = std[1]?.child, + stdoutParentFd = std[1]?.parent, + stderrChildFd = std[2]?.child, + stderrParentFd = std[2]?.parent; + const detachPipeDataListeners = []; + cp.on('exit', function (code) { + for (const detach of detachPipeDataListeners) { + detach(); + } + for (const fd of [ + // The child process exited. Let's clean up its output streams: + stdoutChildFd, + stderrChildFd, + stdinChildFd, + ]) { + if (FS.streams[fd] && !FS.isClosed(FS.streams[fd])) { + FS.close(FS.streams[fd]); + } + } + ProcInfo.exitCode = code; + ProcInfo.exited = true; + }); + // Pass data from child process's stdout to PHP's end of the stdout pipe. + if (stdoutChildFd) { + const stdoutStream = SYSCALLS.getStreamFromFD(stdoutChildFd); + let stdoutAt = 0; + const onStdoutData = function (data) { + try { + stdoutStream.stream_ops.write( + stdoutStream, + data, + 0, + data.length, + stdoutAt + ); + stdoutAt += data.length; + } catch { + // PHP may close the child pipe before Node finishes + // draining already-buffered stdout data. Late chunks are + // no longer deliverable, so detach the listener and stop. + cp.stdout.off('data', onStdoutData); + } + }; + cp.stdout.on('data', onStdoutData); + detachPipeDataListeners.push(() => + cp.stdout.off('data', onStdoutData) + ); + } + // Pass data from child process's stderr to PHP's end of the stdout pipe. + if (stderrChildFd) { + const stderrStream = SYSCALLS.getStreamFromFD(stderrChildFd); + let stderrAt = 0; + const onStderrData = function (data) { + try { + stderrStream.stream_ops.write( + stderrStream, + data, + 0, + data.length, + stderrAt + ); + stderrAt += data.length; + } catch { + cp.stderr.off('data', onStderrData); + } + }; + cp.stderr.on('data', onStderrData); + detachPipeDataListeners.push(() => + cp.stderr.off('data', onStderrData) + ); + } + /** + * Wait until the child process has been spawned. + * Unfortunately there is no Node.js API to check whether + * the process has already been spawned. We can only listen + * to the 'spawn' event and if it has already been spawned, + * listen to the 'exit' event. + */ try { + await new Promise((resolve, reject) => { + /** + * There was no `await` between the `spawnProcess` call + * and the `await` below so the process haven't had a chance + * to run any of the exit-related callbacks yet. + * + * Good. + * + * Let's listen to all the lifecycle events and resolve + * the promise when the process starts or immediately crashes. + */ let resolved = false; + cp.on('spawn', () => { + if (resolved) return; + resolved = true; + resolve(); + }); + cp.on('error', (e) => { + if (resolved) return; + resolved = true; + reject(e); + }); + cp.on('exit', function (code) { + if (resolved) return; + resolved = true; + if (code === 0) { + resolve(); + } else { + reject( + new Error(`Process exited with code ${code}`) + ); + } + }); + /** + * If the process haven't even started after 5 seconds, something + * is wrong. Perhaps we're missing an event listener, or perhaps + * the `spawnProcess` implementation failed to dispatch the relevant + * event. Either way, let's crash to avoid blocking the proc_open() + * call indefinitely. + */ setTimeout(() => { + if (resolved) return; + resolved = true; + reject(new Error('Process timed out')); + }, 5e3); + }); + } catch (e) { + // Process already started. Even if it exited early, PHP still + // needs to know about the pid and clean up the resources. + console.error(e); + return ProcInfo.pid; + } + // Now we want to pass data from the STDIN source supplied by PHP + // to the child process. + if (stdinChildFd) { + // We're in a kernel function used instead of fork(). + // We are the ones responsible for pumping the data from the stdinChildFd + // into the child process. There is no concurrent task operating on the + // piped data or polling the file descriptors, etc. Nothing will ever + // read from the stdinChildFd if we don't do it here. + // Well, let's do it! We'll periodically read from the child end of the + // data pipe and push what we get into the child process. + let stdinStream; + try { + stdinStream = SYSCALLS.getStreamFromFD(stdinChildFd); + } catch (e) { + ___errno_location(ERRNO_CODES.EBADF); + return ProcInfo.pid; + } + if (!stdinStream?.node) { + return ProcInfo.pid; + } + // Pipe the entire stdinStream to cp.stdin + const CHUNK_SIZE = 1024; + const iov = _malloc(16); + // Space for iovec structure + const pnum = _malloc(4); + // Space for number of bytes read + const buffer = _malloc(CHUNK_SIZE); + // Set up iovec structure pointing to our buffer + HEAPU32[iov >> 2] = buffer; + // iov_base + HEAPU32[(iov + 4) >> 2] = CHUNK_SIZE; + // iov_len + function pump() { + try { + while (true) { + if (cp.killed) { + stopPumpingAndCloseStdin(); + return; + } + const result = js_fd_read( + stdinChildFd, + iov, + 1, + pnum, + false + ); + const bytesRead = HEAPU32[pnum >> 2]; + if (result === 0 && bytesRead > 0) { + const wrote = HEAPU8.subarray( + buffer, + buffer + bytesRead + ); + cp.stdin.write(wrote); + } else if (result === 0 && bytesRead === 0) { + // result === 0 and bytesRead === 0 means the file descriptor + // is at EOF. Let's close the stdin stream and clean up. + stopPumpingAndCloseStdin(); + break; + } else if (result === ERRNO_CODES.EAGAIN) { + // The file descriptor is not ready for reading. + // Let's break out of the loop. setInterval will invoke + // this function again soon. + break; + } else { + throw new FS.ErrnoError(result); + } + } + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) { + throw e; + } + ___errno_location(e.errno); + stopPumpingAndCloseStdin(); + } + } + function stopPumpingAndCloseStdin() { + clearInterval(interval); + if (!cp.stdin.closed) { + cp.stdin.end(); + } + _wasm_free(buffer); + _wasm_free(iov); + _wasm_free(pnum); + } + // pump() can never alter the result of this function. + // Even when it fails, we still return the pid. + // Why? + // Because the process already started. We wouldn't backtrack + // with fork(), we won't backtrack here. Let's give PHP the pid, + // and let it think it's the parent process. It will clean up the + // resources as needed. + // stdin may be non-blocking – let's check for updates periodically. + // If we exhaust it at any point, pump() will self-terminate. + // Note handling any failures, closing the descriptor, etc. will not + // happen synchronously when PHP calls fclose($pipes[0]) or proc_close(). + // It will all happen asynchronously on the next tick. It seems off, + // but there doesn't seem to be a better way: cp.stdin.write() and + // cp.stdin.end() are both async APIs and they both accept onCompleted + // callbacks. + const interval = setInterval(pump, 20); + pump(); + } + return ProcInfo.pid; + }); + } + + function _js_release_file_locks() { + if (typeof Module['userSpace'] === 'undefined') { + return; + } + return Module['userSpace'].js_release_file_locks(); + } + + var arraySum = (array, index) => { + var sum = 0; + for (var i = 0; i <= index; sum += array[i++]) {} + return sum; + }; + + var MONTH_DAYS_LEAP = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + + var MONTH_DAYS_REGULAR = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + + var addDays = (date, days) => { + var newDate = new Date(date.getTime()); + while (days > 0) { + var leap = isLeapYear(newDate.getFullYear()); + var currentMonth = newDate.getMonth(); + var daysInCurrentMonth = ( + leap ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR + )[currentMonth]; + if (days > daysInCurrentMonth - newDate.getDate()) { + // we spill over to next month + days -= daysInCurrentMonth - newDate.getDate() + 1; + newDate.setDate(1); + if (currentMonth < 11) { + newDate.setMonth(currentMonth + 1); + } else { + newDate.setMonth(0); + newDate.setFullYear(newDate.getFullYear() + 1); + } + } else { + // we stay in current month + newDate.setDate(newDate.getDate() + days); + return newDate; + } + } + return newDate; + }; + + var _strptime = (buf, format, tm) => { + // char *strptime(const char *restrict buf, const char *restrict format, struct tm *restrict tm); + // http://pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html + var pattern = UTF8ToString(format); + // escape special characters + // TODO: not sure we really need to escape all of these in JS regexps + var SPECIAL_CHARS = '\\!@#$^&*()+=-[]/{}|:<>?,.'; + for (var i = 0, ii = SPECIAL_CHARS.length; i < ii; ++i) { + pattern = pattern.replace( + new RegExp('\\' + SPECIAL_CHARS[i], 'g'), + '\\' + SPECIAL_CHARS[i] + ); + } + // reduce number of matchers + var EQUIVALENT_MATCHERS = { + A: '%a', + B: '%b', + c: '%a %b %d %H:%M:%S %Y', + D: '%m\\/%d\\/%y', + e: '%d', + F: '%Y-%m-%d', + h: '%b', + R: '%H\\:%M', + r: '%I\\:%M\\:%S\\s%p', + T: '%H\\:%M\\:%S', + x: '%m\\/%d\\/(?:%y|%Y)', + X: '%H\\:%M\\:%S', + }; + // TODO: take care of locale + var DATE_PATTERNS = { + /* weekday name */ a: '(?:Sun(?:day)?)|(?:Mon(?:day)?)|(?:Tue(?:sday)?)|(?:Wed(?:nesday)?)|(?:Thu(?:rsday)?)|(?:Fri(?:day)?)|(?:Sat(?:urday)?)', + /* month name */ b: '(?:Jan(?:uary)?)|(?:Feb(?:ruary)?)|(?:Mar(?:ch)?)|(?:Apr(?:il)?)|May|(?:Jun(?:e)?)|(?:Jul(?:y)?)|(?:Aug(?:ust)?)|(?:Sep(?:tember)?)|(?:Oct(?:ober)?)|(?:Nov(?:ember)?)|(?:Dec(?:ember)?)', + /* century */ C: '\\d\\d', + /* day of month */ d: '0[1-9]|[1-9](?!\\d)|1\\d|2\\d|30|31', + /* hour (24hr) */ H: '\\d(?!\\d)|[0,1]\\d|20|21|22|23', + /* hour (12hr) */ I: '\\d(?!\\d)|0\\d|10|11|12', + /* day of year */ j: '00[1-9]|0?[1-9](?!\\d)|0?[1-9]\\d(?!\\d)|[1,2]\\d\\d|3[0-6]\\d', + /* month */ m: '0[1-9]|[1-9](?!\\d)|10|11|12', + /* minutes */ M: '0\\d|\\d(?!\\d)|[1-5]\\d', + /* whitespace */ n: ' ', + /* AM/PM */ p: 'AM|am|PM|pm|A\\.M\\.|a\\.m\\.|P\\.M\\.|p\\.m\\.', + /* seconds */ S: '0\\d|\\d(?!\\d)|[1-5]\\d|60', + /* week number */ U: '0\\d|\\d(?!\\d)|[1-4]\\d|50|51|52|53', + /* week number */ W: '0\\d|\\d(?!\\d)|[1-4]\\d|50|51|52|53', + /* weekday number */ w: '[0-6]', + /* 2-digit year */ y: '\\d\\d', + /* 4-digit year */ Y: '\\d\\d\\d\\d', + /* whitespace */ t: ' ', + /* time zone */ z: 'Z|(?:[\\+\\-]\\d\\d:?(?:\\d\\d)?)', + }; + var MONTH_NUMBERS = { + JAN: 0, + FEB: 1, + MAR: 2, + APR: 3, + MAY: 4, + JUN: 5, + JUL: 6, + AUG: 7, + SEP: 8, + OCT: 9, + NOV: 10, + DEC: 11, + }; + var DAY_NUMBERS_SUN_FIRST = { + SUN: 0, + MON: 1, + TUE: 2, + WED: 3, + THU: 4, + FRI: 5, + SAT: 6, + }; + var DAY_NUMBERS_MON_FIRST = { + MON: 0, + TUE: 1, + WED: 2, + THU: 3, + FRI: 4, + SAT: 5, + SUN: 6, + }; + var capture = []; + var pattern_out = pattern + .replace(/%(.)/g, (m, c) => EQUIVALENT_MATCHERS[c] || m) + .replace(/%(.)/g, (_, c) => { + let pat = DATE_PATTERNS[c]; + if (pat) { + capture.push(c); + return `(${pat})`; + } else { + return c; + } + }) + .replace( + // any number of space or tab characters match zero or more spaces + /\s+/g, + '\\s*' + ); + var matches = new RegExp('^' + pattern_out, 'i').exec( + UTF8ToString(buf) + ); + function initDate() { + function fixup(value, min, max) { + return typeof value != 'number' || isNaN(value) + ? min + : value >= min + ? value <= max + ? value + : max + : min; + } + return { + year: fixup(HEAP32[(tm + 20) >> 2] + 1900, 1970, 9999), + month: fixup(HEAP32[(tm + 16) >> 2], 0, 11), + day: fixup(HEAP32[(tm + 12) >> 2], 1, 31), + hour: fixup(HEAP32[(tm + 8) >> 2], 0, 23), + min: fixup(HEAP32[(tm + 4) >> 2], 0, 59), + sec: fixup(HEAP32[tm >> 2], 0, 59), + gmtoff: 0, + }; + } + if (matches) { + var date = initDate(); + var value; + var getMatch = (symbol) => { + var pos = capture.indexOf(symbol); + // check if symbol appears in regexp + if (pos >= 0) { + // return matched value or null (falsy!) for non-matches + return matches[pos + 1]; + } + return; + }; + // seconds + if ((value = getMatch('S'))) { + date.sec = Number(value); + } + // minutes + if ((value = getMatch('M'))) { + date.min = Number(value); + } + // hours + if ((value = getMatch('H'))) { + // 24h clock + date.hour = Number(value); + } else if ((value = getMatch('I'))) { + // AM/PM clock + var hour = Number(value); + if ((value = getMatch('p'))) { + hour += value.toUpperCase()[0] === 'P' ? 12 : 0; + } + date.hour = hour; + } + // year + if ((value = getMatch('Y'))) { + // parse from four-digit year + date.year = Number(value); + } else if ((value = getMatch('y'))) { + // parse from two-digit year... + var year = Number(value); + if ((value = getMatch('C'))) { + // ...and century + year += Number(value) * 100; + } else { + // ...and rule-of-thumb + year += year < 69 ? 2e3 : 1900; + } + date.year = year; + } + // month + if ((value = getMatch('m'))) { + // parse from month number + date.month = Number(value) - 1; + } else if ((value = getMatch('b'))) { + // parse from month name + date.month = + MONTH_NUMBERS[value.substring(0, 3).toUpperCase()] || 0; + } + // day + if ((value = getMatch('d'))) { + // get day of month directly + date.day = Number(value); + } else if ((value = getMatch('j'))) { + // get day of month from day of year ... + var day = Number(value); + var leapYear = isLeapYear(date.year); + for (var month = 0; month < 12; ++month) { + var daysUntilMonth = arraySum( + leapYear ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR, + month - 1 + ); + if ( + day <= + daysUntilMonth + + (leapYear ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR)[ + month + ] + ) { + date.day = day - daysUntilMonth; + } + } + } else if ((value = getMatch('a'))) { + // get day of month from weekday ... + var weekDay = value.substring(0, 3).toUpperCase(); + if ((value = getMatch('U'))) { + // ... and week number (Sunday being first day of week) + // Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. + // All days in a new year preceding the first Sunday are considered to be in week 0. + var weekDayNumber = DAY_NUMBERS_SUN_FIRST[weekDay]; + var weekNumber = Number(value); + // January 1st + var janFirst = new Date(date.year, 0, 1); + var endDate; + if (janFirst.getDay() === 0) { + // Jan 1st is a Sunday, and, hence in the 1st CW + endDate = addDays( + janFirst, + weekDayNumber + 7 * (weekNumber - 1) + ); + } else { + // Jan 1st is not a Sunday, and, hence still in the 0th CW + endDate = addDays( + janFirst, + 7 - + janFirst.getDay() + + weekDayNumber + + 7 * (weekNumber - 1) + ); + } + date.day = endDate.getDate(); + date.month = endDate.getMonth(); + } else if ((value = getMatch('W'))) { + // ... and week number (Monday being first day of week) + // Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. + // All days in a new year preceding the first Monday are considered to be in week 0. + var weekDayNumber = DAY_NUMBERS_MON_FIRST[weekDay]; + var weekNumber = Number(value); + // January 1st + var janFirst = new Date(date.year, 0, 1); + var endDate; + if (janFirst.getDay() === 1) { + // Jan 1st is a Monday, and, hence in the 1st CW + endDate = addDays( + janFirst, + weekDayNumber + 7 * (weekNumber - 1) + ); + } else { + // Jan 1st is not a Monday, and, hence still in the 0th CW + endDate = addDays( + janFirst, + 7 - + janFirst.getDay() + + 1 + + weekDayNumber + + 7 * (weekNumber - 1) + ); + } + date.day = endDate.getDate(); + date.month = endDate.getMonth(); + } + } + // time zone + if ((value = getMatch('z'))) { + // GMT offset as either 'Z' or +-HH:MM or +-HH or +-HHMM + if (value.toLowerCase() === 'z') { + date.gmtoff = 0; + } else { + var match = value.match(/^((?:\-|\+)\d\d):?(\d\d)?/); + date.gmtoff = match[1] * 3600; + if (match[2]) { + date.gmtoff += + date.gmtoff > 0 ? match[2] * 60 : -match[2] * 60; + } + } + } + /* + tm_sec int seconds after the minute 0-61* + tm_min int minutes after the hour 0-59 + tm_hour int hours since midnight 0-23 + tm_mday int day of the month 1-31 + tm_mon int months since January 0-11 + tm_year int years since 1900 + tm_wday int days since Sunday 0-6 + tm_yday int days since January 1 0-365 + tm_isdst int Daylight Saving Time flag + tm_gmtoff long offset from GMT (seconds) + */ var fullDate = new Date( + date.year, + date.month, + date.day, + date.hour, + date.min, + date.sec, + 0 + ); + HEAP32[tm >> 2] = fullDate.getSeconds(); + HEAP32[(tm + 4) >> 2] = fullDate.getMinutes(); + HEAP32[(tm + 8) >> 2] = fullDate.getHours(); + HEAP32[(tm + 12) >> 2] = fullDate.getDate(); + HEAP32[(tm + 16) >> 2] = fullDate.getMonth(); + HEAP32[(tm + 20) >> 2] = fullDate.getFullYear() - 1900; + HEAP32[(tm + 24) >> 2] = fullDate.getDay(); + HEAP32[(tm + 28) >> 2] = + arraySum( + isLeapYear(fullDate.getFullYear()) + ? MONTH_DAYS_LEAP + : MONTH_DAYS_REGULAR, + fullDate.getMonth() - 1 + ) + + fullDate.getDate() - + 1; + HEAP32[(tm + 32) >> 2] = 0; + HEAP32[(tm + 36) >> 2] = date.gmtoff; + // we need to convert the matched sequence into an integer array to take care of UTF-8 characters > 0x7F + // TODO: not sure that intArrayFromString handles all unicode characters correctly + return buf + lengthBytesUTF8(matches[0]); + } + return 0; + }; + + _strptime.sig = 'pppp'; + + function _wasm_setsockopt( + socketd, + level, + optionName, + optionValuePtr, + optionLen + ) { + const optionValue = HEAPU8[optionValuePtr]; + const SOL_SOCKET = 1; + const SO_KEEPALIVE = 9; + const SO_RCVTIMEO = 66; + const SO_SNDTIMEO = 67; + const IPPROTO_TCP = 6; + const TCP_NODELAY = 1; + // Options that we can forward to the WebSocket proxy + const isForwardable = + (level === SOL_SOCKET && optionName === SO_KEEPALIVE) || + (level === IPPROTO_TCP && optionName === TCP_NODELAY); + // Options that we acknowledge but don't actually implement + // (WebSocket connections handle timeouts differently) + const isIgnorable = + level === SOL_SOCKET && + (optionName === SO_RCVTIMEO || optionName === SO_SNDTIMEO); + if (!isForwardable && !isIgnorable) { + console.warn( + `Unsupported socket option: ${level}, ${optionName}, ${optionValue}` + ); + return -1; + } + // For ignorable options, just return success + if (isIgnorable) { + return 0; + } + const ws = PHPWASM.getAllWebSockets(socketd)[0]; + if (!ws) { + return -1; + } + ws.setSocketOpt(level, optionName, optionValuePtr); + return 0; + } + + var runAndAbortIfError = (func) => { + try { + return func(); + } catch (e) { + abort(e); + } + }; + + var Asyncify = { + instrumentWasmImports(imports) { + var importPattern = + /^(invoke_i|invoke_ii|invoke_iii|invoke_iiii|invoke_iiiii|invoke_iiiiii|invoke_iiiiiii|invoke_iiiiiiii|invoke_iiiiiiiii|invoke_iiiiiiiiii|invoke_v|invoke_vi|invoke_vii|invoke_viidii|invoke_viii|invoke_viiii|invoke_viiiii|invoke_viiiiii|invoke_viiiiiii|invoke_viiiiiiiii|invoke_i|invoke_ii|invoke_iii|invoke_iiii|invoke_iiiii|invoke_iiiiii|invoke_iiiiiii|invoke_iiiiiiii|invoke_iiiiiiiiii|invoke_iij|invoke_iiji|invoke_iiij|invoke_iijii|invoke_iijiji|invoke_jii|invoke_jiii|invoke_viijii|invoke_vji|js_open_process|_js_open_process|_asyncjs__js_open_process|js_popen_to_file|_js_popen_to_file|_asyncjs__js_popen_to_file|__syscall_fcntl64|___syscall_fcntl64|_asyncjs___syscall_fcntl64|js_release_file_locks|_js_release_file_locks|_async_js_release_file_locks|js_flock|_js_flock|_async_js_flock|js_fd_read|_js_fd_read|fd_close|_fd_close|_asyncjs__fd_close|close|_close|js_module_onMessage|zend_hash_str_find|_js_module_onMessage|_asyncjs__js_module_onMessage|js_waitpid|_js_waitpid|_asyncjs__js_waitpid|wasm_poll_socket|_wasm_poll_socket|_asyncjs__wasm_poll_socket|_wasm_shutdown|_asyncjs__wasm_shutdown|recv|_recv|setsockopt|_setsockopt|wasm_connect|_wasm_connect|__asyncjs__.*)$/; + for (let [x, original] of Object.entries(imports)) { + if (typeof original == 'function') { + let isAsyncifyImport = + original.isAsync || importPattern.test(x); + } + } + }, + instrumentFunction(original) { + var wrapper = (...args) => { + Asyncify.exportCallStack.push(original); + try { + return original(...args); + } finally { + if (!ABORT) { + var top = Asyncify.exportCallStack.pop(); + Asyncify.maybeStopUnwind(); + } + } + }; + Asyncify.funcWrappers.set(original, wrapper); + wrapper.orig = original; + return wrapper; + }, + instrumentWasmExports(exports) { + var ret = {}; + for (let [x, original] of Object.entries(exports)) { + if (typeof original == 'function') { + var wrapper = Asyncify.instrumentFunction(original); + ret[x] = wrapper; + } else { + ret[x] = original; + } + } + return ret; + }, + State: { + Normal: 0, + Unwinding: 1, + Rewinding: 2, + Disabled: 3, + }, + state: 0, + StackSize: 4096, + currData: null, + handleSleepReturnValue: 0, + exportCallStack: [], + callstackFuncToId: new Map(), + callStackIdToFunc: new Map(), + funcWrappers: new Map(), + callStackId: 0, + asyncPromiseHandlers: null, + sleepCallbacks: [], + getCallStackId(func) { + if (!Asyncify.callstackFuncToId.has(func)) { + var id = Asyncify.callStackId++; + Asyncify.callstackFuncToId.set(func, id); + Asyncify.callStackIdToFunc.set(id, func); + } + return Asyncify.callstackFuncToId.get(func); + }, + maybeStopUnwind() { + if ( + Asyncify.currData && + Asyncify.state === Asyncify.State.Unwinding && + Asyncify.exportCallStack.length === 0 + ) { + // We just finished unwinding. + // Be sure to set the state before calling any other functions to avoid + // possible infinite recursion here (For example in debug pthread builds + // the dbg() function itself can call back into WebAssembly to get the + // current pthread_self() pointer). + Asyncify.state = Asyncify.State.Normal; + runtimeKeepalivePush(); + // Keep the runtime alive so that a re-wind can be done later. + runAndAbortIfError(_asyncify_stop_unwind); + if (typeof Fibers != 'undefined') { + Fibers.trampoline(); + } + } + }, + whenDone() { + return new Promise((resolve, reject) => { + Asyncify.asyncPromiseHandlers = { + resolve, + reject, + }; + }); + }, + allocateData() { + // An asyncify data structure has three fields: + // 0 current stack pos + // 4 max stack pos + // 8 id of function at bottom of the call stack (callStackIdToFunc[id] == wasm func) + // The Asyncify ABI only interprets the first two fields, the rest is for the runtime. + // We also embed a stack in the same memory region here, right next to the structure. + // This struct is also defined as asyncify_data_t in emscripten/fiber.h + var ptr = _malloc(12 + Asyncify.StackSize); + Asyncify.setDataHeader(ptr, ptr + 12, Asyncify.StackSize); + Asyncify.setDataRewindFunc(ptr); + return ptr; + }, + setDataHeader(ptr, stack, stackSize) { + HEAPU32[ptr >> 2] = stack; + HEAPU32[(ptr + 4) >> 2] = stack + stackSize; + }, + setDataRewindFunc(ptr) { + var bottomOfCallStack = Asyncify.exportCallStack[0]; + var rewindId = Asyncify.getCallStackId(bottomOfCallStack); + HEAP32[(ptr + 8) >> 2] = rewindId; + }, + getDataRewindFunc(ptr) { + var id = HEAP32[(ptr + 8) >> 2]; + var func = Asyncify.callStackIdToFunc.get(id); + return func; + }, + doRewind(ptr) { + var original = Asyncify.getDataRewindFunc(ptr); + var func = Asyncify.funcWrappers.get(original); + // Once we have rewound and the stack we no longer need to artificially + // keep the runtime alive. + runtimeKeepalivePop(); + return func(); + }, + handleSleep(startAsync) { + if (ABORT) return; + if (Asyncify.state === Asyncify.State.Normal) { + // Prepare to sleep. Call startAsync, and see what happens: + // if the code decided to call our callback synchronously, + // then no async operation was in fact begun, and we don't + // need to do anything. + var reachedCallback = false; + var reachedAfterCallback = false; + startAsync((handleSleepReturnValue = 0) => { + if (ABORT) return; + Asyncify.handleSleepReturnValue = handleSleepReturnValue; + reachedCallback = true; + if (!reachedAfterCallback) { + // We are happening synchronously, so no need for async. + return; + } + Asyncify.state = Asyncify.State.Rewinding; + runAndAbortIfError(() => + _asyncify_start_rewind(Asyncify.currData) + ); + if (typeof MainLoop != 'undefined' && MainLoop.func) { + MainLoop.resume(); + } + var asyncWasmReturnValue, + isError = false; + try { + asyncWasmReturnValue = Asyncify.doRewind( + Asyncify.currData + ); + } catch (err) { + asyncWasmReturnValue = err; + isError = true; + } + // Track whether the return value was handled by any promise handlers. + var handled = false; + if (!Asyncify.currData) { + // All asynchronous execution has finished. + // `asyncWasmReturnValue` now contains the final + // return value of the exported async WASM function. + // Note: `asyncWasmReturnValue` is distinct from + // `Asyncify.handleSleepReturnValue`. + // `Asyncify.handleSleepReturnValue` contains the return + // value of the last C function to have executed + // `Asyncify.handleSleep()`, where as `asyncWasmReturnValue` + // contains the return value of the exported WASM function + // that may have called C functions that + // call `Asyncify.handleSleep()`. + var asyncPromiseHandlers = + Asyncify.asyncPromiseHandlers; + if (asyncPromiseHandlers) { + Asyncify.asyncPromiseHandlers = null; + (isError + ? asyncPromiseHandlers.reject + : asyncPromiseHandlers.resolve)( + asyncWasmReturnValue + ); + handled = true; + } + } + if (isError && !handled) { + // If there was an error and it was not handled by now, we have no choice but to + // rethrow that error into the global scope where it can be caught only by + // `onerror` or `onunhandledpromiserejection`. + throw asyncWasmReturnValue; + } + }); + reachedAfterCallback = true; + if (!reachedCallback) { + // A true async operation was begun; start a sleep. + Asyncify.state = Asyncify.State.Unwinding; + // TODO: reuse, don't alloc/free every sleep + if (!Asyncify._cachedData) { + Asyncify._cachedData = Asyncify.allocateData(); + } else { + Asyncify.setDataHeader( + Asyncify._cachedData, + Asyncify._cachedData + 12, + Asyncify.StackSize + ); + Asyncify.setDataRewindFunc(Asyncify._cachedData); + } + Asyncify.currData = Asyncify._cachedData; + if (typeof MainLoop != 'undefined' && MainLoop.func) { + MainLoop.pause(); + } + runAndAbortIfError(() => + _asyncify_start_unwind(Asyncify.currData) + ); + } + } else if (Asyncify.state === Asyncify.State.Rewinding) { + // Stop a resume. + Asyncify.state = Asyncify.State.Normal; + runAndAbortIfError(_asyncify_stop_rewind); + Asyncify.currData = null; + // Call all sleep callbacks now that the sleep-resume is all done. + Asyncify.sleepCallbacks.forEach(callUserCallback); + } else { + abort(`invalid state: ${Asyncify.state}`); + } + return Asyncify.handleSleepReturnValue; + }, + handleAsync: (startAsync) => + Asyncify.handleSleep((wakeUp) => { + // TODO: add error handling as a second param when handleSleep implements it. + startAsync().then(wakeUp); + }), + }; + + var getCFunc = (ident) => { + var func = Module['_' + ident]; + // closure exported function + return func; + }; + + var writeArrayToMemory = (array, buffer) => { + HEAP8.set(array, buffer); + }; + + /** + * @param {string|null=} returnType + * @param {Array=} argTypes + * @param {Array=} args + * @param {Object=} opts + */ var ccall = (ident, returnType, argTypes, args, opts) => { + // For fast lookup of conversion functions + var toC = { + string: (str) => { + var ret = 0; + if (str !== null && str !== undefined && str !== 0) { + // null string + ret = stringToUTF8OnStack(str); + } + return ret; + }, + array: (arr) => { + var ret = stackAlloc(arr.length); + writeArrayToMemory(arr, ret); + return ret; + }, + }; + function convertReturnValue(ret) { + if (returnType === 'string') { + return UTF8ToString(ret); + } + if (returnType === 'boolean') return Boolean(ret); + return ret; + } + var func = getCFunc(ident); + var cArgs = []; + var stack = 0; + if (args) { + for (var i = 0; i < args.length; i++) { + var converter = toC[argTypes[i]]; + if (converter) { + if (stack === 0) stack = stackSave(); + cArgs[i] = converter(args[i]); + } else { + cArgs[i] = args[i]; + } + } + } + // Data for a previous async operation that was in flight before us. + var previousAsync = Asyncify.currData; + var ret = func(...cArgs); + function onDone(ret) { + runtimeKeepalivePop(); + if (stack !== 0) stackRestore(stack); + return convertReturnValue(ret); + } + var asyncMode = opts?.async; + // Keep the runtime alive through all calls. Note that this call might not be + // async, but for simplicity we push and pop in all calls. + runtimeKeepalivePush(); + if (Asyncify.currData != previousAsync) { + // This is a new async operation. The wasm is paused and has unwound its stack. + // We need to return a Promise that resolves the return value + // once the stack is rewound and execution finishes. + return Asyncify.whenDone().then(onDone); + } + ret = onDone(ret); + // If this is an async ccall, ensure we return a promise + if (asyncMode) return Promise.resolve(ret); + return ret; + }; + + var FS_createPath = (...args) => FS.createPath(...args); + + var FS_unlink = (...args) => FS.unlink(...args); + + var FS_createLazyFile = (...args) => FS.createLazyFile(...args); + + var FS_createDevice = (...args) => FS.createDevice(...args); + + registerWasmPlugin(); + + FS.createPreloadedFile = FS_createPreloadedFile; + + FS.preloadFile = FS_preloadFile; + + FS.staticInit(); + + if (ENVIRONMENT_IS_NODE) { + NODEFS.staticInit(); + } + + PHPWASM.init(); + + // End JS library code + // include: postlibrary.js + // This file is included after the automatically-generated JS library code + // but before the wasm module is created. + { + // Begin ATMODULES hooks + if (Module['preloadPlugins']) preloadPlugins = Module['preloadPlugins']; + if (Module['noExitRuntime']) noExitRuntime = Module['noExitRuntime']; + if (Module['print']) out = Module['print']; + if (Module['printErr']) err = Module['printErr']; + if (Module['dynamicLibraries']) + dynamicLibraries = Module['dynamicLibraries']; + if (Module['wasmBinary']) wasmBinary = Module['wasmBinary']; + // End ATMODULES hooks + if (Module['arguments']) arguments_ = Module['arguments']; + if (Module['thisProgram']) thisProgram = Module['thisProgram']; + if (Module['quit']) quit_ = Module['quit']; + if (Module['preInit']) { + if (typeof Module['preInit'] == 'function') + Module['preInit'] = [Module['preInit']]; + while (Module['preInit'].length > 0) { + Module['preInit'].shift()(); + } + } + } + + // Begin runtime exports + Module['wasmExports'] = wasmExports; + + Module['addRunDependency'] = addRunDependency; + + Module['removeRunDependency'] = removeRunDependency; + + Module['ccall'] = ccall; + + Module['UTF8ToString'] = UTF8ToString; + + Module['stringToUTF8'] = stringToUTF8; + + Module['lengthBytesUTF8'] = lengthBytesUTF8; + + Module['FS_preloadFile'] = FS_preloadFile; + + Module['FS_unlink'] = FS_unlink; + + Module['FS_createPath'] = FS_createPath; + + Module['FS_createDevice'] = FS_createDevice; + + Module['FS_createDataFile'] = FS_createDataFile; + + Module['FS_createLazyFile'] = FS_createLazyFile; + + Module['PROXYFS'] = PROXYFS; + + // End runtime exports + // Begin JS library exports + Module['UTF8ToString'] = UTF8ToString; + + Module['lengthBytesUTF8'] = lengthBytesUTF8; + + Module['stringToUTF8'] = stringToUTF8; + + Module['FS'] = FS; + + Module['_exit'] = _exit; + + Module['_emscripten_sleep'] = _emscripten_sleep; + + // End JS library exports + // end include: postlibrary.js + var ASM_CONSTS = {}; + + function js_popen_to_file(command, mode, exitCodePtr) { + const returnCallback = (resolver) => Asyncify.handleSleep(resolver); + if (!command) return 1; + const cmdstr = UTF8ToString(command); + if (!cmdstr.length) return 0; + const modestr = UTF8ToString(mode); + if (!modestr.length) return 0; + if (modestr === 'w') { + console.error('popen($cmd, "w") is not implemented yet'); + } + return returnCallback(async (wakeUp) => { + let cp; + try { + cp = PHPWASM.spawnProcess(cmdstr, []); + if (cp instanceof Promise) { + cp = await cp; + } + } catch (e) { + console.error(e); + if (e.code === 'SPAWN_UNSUPPORTED') { + return 1; + } + throw e; + } + const outByteArrays = []; + cp.stdout.on('data', function (data) { + outByteArrays.push(data); + }); + const outputPath = '/tmp/popen_output'; + cp.on('exit', function (exitCode) { + const outBytes = new Uint8Array( + outByteArrays.reduce((acc, curr) => acc + curr.length, 0) + ); + let offset = 0; + for (const byteArray of outByteArrays) { + outBytes.set(byteArray, offset); + offset += byteArray.length; + } + FS.writeFile(outputPath, outBytes); + HEAPU8[exitCodePtr] = exitCode; + wakeUp(allocateUTF8OnStack(outputPath)); + }); + }); + } + + js_popen_to_file.sig = 'iiii'; + + function wasm_poll_socket(socketd, events, timeout) { + const returnCallback = (resolver) => Asyncify.handleSleep(resolver); + const POLLIN = 1; + const POLLPRI = 2; + const POLLOUT = 4; + const POLLERR = 8; + const POLLHUP = 16; + const POLLNVAL = 32; + return returnCallback((wakeUp) => { + const polls = []; + const stream = FS.getStream(socketd); + if (FS.isSocket(stream?.node.mode)) { + const sock = getSocketFromFD(socketd); + if (!sock) { + wakeUp(0); + return; + } + const lookingFor = new Set(); + if (events & POLLIN || events & POLLPRI) { + if (sock.server) { + for (const client of sock.pending) { + if ((client.recv_queue || []).length > 0) { + wakeUp(1); + return; + } + } + } else if ((sock.recv_queue || []).length > 0) { + wakeUp(1); + return; + } + } + const webSockets = PHPWASM.getAllWebSockets(sock); + if (!webSockets.length) { + wakeUp(0); + return; + } + for (const ws of webSockets) { + if (events & POLLIN || events & POLLPRI) { + polls.push(PHPWASM.awaitData(ws)); + lookingFor.add('POLLIN'); + } + if (events & POLLOUT) { + polls.push(PHPWASM.awaitConnection(ws)); + lookingFor.add('POLLOUT'); + } + if ( + events & POLLHUP || + events & POLLIN || + events & POLLOUT || + events & POLLERR + ) { + polls.push(PHPWASM.awaitClose(ws)); + lookingFor.add('POLLHUP'); + } + if (events & POLLERR || events & POLLNVAL) { + polls.push(PHPWASM.awaitError(ws)); + lookingFor.add('POLLERR'); + } + } + } else if (stream?.stream_ops?.poll) { + let interrupted = false; + async function poll() { + try { + while (true) { + var mask = POLLNVAL; + mask = SYSCALLS.DEFAULT_POLLMASK; + if (FS.isClosed(stream)) { + return ERRNO_CODES.EBADF; + } + if (stream.stream_ops?.poll) { + mask = stream.stream_ops.poll(stream, -1); + } + mask &= events | POLLERR | POLLHUP; + if (mask) { + return mask; + } + if (interrupted) { + return ERRNO_CODES.ETIMEDOUT; + } + await new Promise((resolve) => + setTimeout(resolve, 10) + ); + } + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) + throw e; + return -e.errno; + } + } + polls.push([ + poll(), + () => { + interrupted = true; + }, + ]); + } else { + setTimeout(function () { + wakeUp(1); + }, timeout); + return; + } + if (polls.length === 0) { + console.warn( + 'Unsupported poll event ' + + events + + ', defaulting to setTimeout().' + ); + setTimeout(function () { + wakeUp(0); + }, timeout); + return; + } + const promises = polls.map(([promise]) => promise); + const clearPolling = () => polls.forEach(([, clear]) => clear()); + let awaken = false; + let timeoutId; + Promise.race(promises).then(function (results) { + if (!awaken) { + awaken = true; + wakeUp(1); + if (timeoutId) { + clearTimeout(timeoutId); + } + clearPolling(); + } + }); + if (timeout !== -1) { + timeoutId = setTimeout(function () { + if (!awaken) { + awaken = true; + wakeUp(0); + clearPolling(); + } + }, timeout); + } + }); + } + + wasm_poll_socket.sig = 'iiii'; + + function js_fd_read(fd, iov, iovcnt, pnum) { + const returnCallback = (resolver) => Asyncify.handleSleep(resolver); + const pollAsync = arguments[4] === undefined ? true : !!arguments[4]; + if ( + Asyncify?.State?.Normal === undefined || + Asyncify?.state === Asyncify?.State?.Normal + ) { + var stream; + try { + stream = SYSCALLS.getStreamFromFD(fd); + HEAPU32[pnum >> 2] = doReadv(stream, iov, iovcnt); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + throw e; + } + if ( + e.errno !== ERRNO_CODES.EWOULDBLOCK && + e.errno !== ERRNO_CODES.EAGAIN + ) { + return e.errno; + } + const nonBlocking = stream.flags & PHPWASM.O_NONBLOCK; + if (nonBlocking) { + return e.errno; + } + } + } + if (false === pollAsync) { + return ERRNO_CODES.EWOULDBLOCK; + } + return returnCallback(async (wakeUp) => { + var retries = 0; + var interval = 50; + var timeout = 5e3; + var maxRetries = timeout / interval; + while (true) { + var returnCode; + var stream; + let num; + try { + stream = SYSCALLS.getStreamFromFD(fd); + num = doReadv(stream, iov, iovcnt); + returnCode = 0; + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) { + console.error(e); + throw e; + } + returnCode = e.errno; + } + if (returnCode === 0) { + HEAPU32[pnum >> 2] = num; + return wakeUp(0); + } + if ( + ++retries > maxRetries || + !stream || + FS.isClosed(stream) || + returnCode !== ERRNO_CODES.EWOULDBLOCK || + ('pipe' in stream.node && stream.node.pipe.refcnt < 2) + ) { + HEAPU32[pnum >> 2] = num; + return wakeUp(returnCode); + } + await new Promise((resolve) => setTimeout(resolve, interval)); + } + }); + } + + js_fd_read.sig = 'iiiii'; + + function __asyncjs__js_module_onMessage(data, response_buffer) { + return Asyncify.handleAsync(async () => { + if (Module['onMessage']) { + const dataStr = UTF8ToString(data); + return Module['onMessage'](dataStr) + .then((response) => { + const responseBytes = + typeof response === 'string' + ? new TextEncoder().encode(response) + : response; + const responseSize = responseBytes.byteLength; + const responsePtr = _malloc(responseSize + 1); + HEAPU8.set(responseBytes, responsePtr); + HEAPU8[responsePtr + responseSize] = 0; + HEAPU8[response_buffer] = responsePtr; + HEAPU8[response_buffer + 1] = responsePtr >> 8; + HEAPU8[response_buffer + 2] = responsePtr >> 16; + HEAPU8[response_buffer + 3] = responsePtr >> 24; + return responseSize; + }) + .catch((e) => { + console.error(e); + return -1; + }); + } + }); + } + + __asyncjs__js_module_onMessage.sig = 'iii'; + + // Imports from the Wasm binary. + var _calloc, + _malloc, + _free, + ___errno_location, + _wasm_sleep, + _ntohs, + _htons, + _htonl, + _wasm_read, + _fflush, + _flock, + _php_pollfd_for, + _wasm_php_exec, + ___wrap_usleep, + _wasm_popen, + ___wrap_select, + _wasm_set_sapi_name, + _wasm_set_phpini_path, + _wasm_add_cli_arg, + _run_cli, + _wasm_add_SERVER_entry, + _wasm_add_ENV_entry, + _wasm_set_query_string, + _wasm_set_path_translated, + _wasm_set_skip_shebang, + _wasm_set_request_uri, + _wasm_set_request_method, + _wasm_set_request_host, + _wasm_set_content_type, + _wasm_set_request_body, + _wasm_set_content_length, + _wasm_set_cookies, + _wasm_set_request_port, + _wasm_sapi_request_shutdown, + _wasm_sapi_handle_request, + _php_wasm_init, + _wasm_free, + _wasm_get_end_offset, + ___wrap_getpid, + _wasm_trace, + _initgroups, + ___funcs_on_exit, + ___dl_seterr, + __emscripten_find_dylib, + _emscripten_builtin_memalign, + __emscripten_timeout, + _emscripten_get_sbrk_ptr, + _setThrew, + __emscripten_stack_restore, + __emscripten_stack_alloc, + _emscripten_stack_get_current, + __ZNSt3__211__call_onceERVmPvPFvS2_E, + __ZNSt3__218condition_variable10notify_allEv, + __ZNSt3__25mutex4lockEv, + __ZNSt3__25mutex6unlockEv, + dynCall_viii, + dynCall_vi, + dynCall_ii, + dynCall_iii, + dynCall_iiiii, + dynCall_viiii, + dynCall_vii, + dynCall_viiiii, + dynCall_iiii, + dynCall_iiiiii, + dynCall_v, + dynCall_i, + dynCall_iiiiiiii, + dynCall_iijii, + dynCall_iiiiiii, + dynCall_iiji, + dynCall_iiiiiiiiii, + dynCall_viiiiiiii, + dynCall_viiiiiii, + dynCall_iiiiiiiii, + dynCall_viiiiiiiii, + dynCall_viiiiii, + dynCall_ij, + dynCall_iiiij, + dynCall_vijii, + dynCall_iij, + dynCall_iiiiiij, + dynCall_iiid, + dynCall_iiij, + dynCall_dii, + dynCall_jii, + dynCall_ji, + dynCall_vid, + dynCall_vij, + dynCall_di, + dynCall_iiiiijii, + dynCall_j, + dynCall_jj, + dynCall_jiij, + dynCall_iiiiji, + dynCall_iiiijii, + dynCall_viiji, + dynCall_viijii, + dynCall_iiiiiiiiiii, + dynCall_iiiijji, + dynCall_dd, + dynCall_ddd, + dynCall_jiji, + dynCall_iidiiii, + _asyncify_start_unwind, + _asyncify_stop_unwind, + _asyncify_start_rewind, + _asyncify_stop_rewind, + memory, + __indirect_function_table, + wasmTable, + wasmMemory; + + function assignWasmExports(wasmExports) { + _calloc = wasmExports['calloc']; + _malloc = + PHPLoader['malloc'] = + Module['_malloc'] = + wasmExports['malloc']; + _free = wasmExports['free']; + ___errno_location = Module['___errno_location'] = + wasmExports['__errno_location']; + _wasm_sleep = Module['_wasm_sleep'] = wasmExports['wasm_sleep']; + _ntohs = wasmExports['ntohs']; + _htons = wasmExports['htons']; + _htonl = wasmExports['htonl']; + _wasm_read = Module['_wasm_read'] = wasmExports['wasm_read']; + _fflush = wasmExports['fflush']; + _flock = Module['_flock'] = wasmExports['flock']; + _php_pollfd_for = Module['_php_pollfd_for'] = + wasmExports['php_pollfd_for']; + _wasm_php_exec = Module['_wasm_php_exec'] = + wasmExports['wasm_php_exec']; + ___wrap_usleep = Module['___wrap_usleep'] = + wasmExports['__wrap_usleep']; + _wasm_popen = Module['_wasm_popen'] = wasmExports['wasm_popen']; + ___wrap_select = Module['___wrap_select'] = + wasmExports['__wrap_select']; + _wasm_set_sapi_name = Module['_wasm_set_sapi_name'] = + wasmExports['wasm_set_sapi_name']; + _wasm_set_phpini_path = Module['_wasm_set_phpini_path'] = + wasmExports['wasm_set_phpini_path']; + _wasm_add_cli_arg = Module['_wasm_add_cli_arg'] = + wasmExports['wasm_add_cli_arg']; + _run_cli = Module['_run_cli'] = wasmExports['run_cli']; + _wasm_add_SERVER_entry = Module['_wasm_add_SERVER_entry'] = + wasmExports['wasm_add_SERVER_entry']; + _wasm_add_ENV_entry = Module['_wasm_add_ENV_entry'] = + wasmExports['wasm_add_ENV_entry']; + _wasm_set_query_string = Module['_wasm_set_query_string'] = + wasmExports['wasm_set_query_string']; + _wasm_set_path_translated = Module['_wasm_set_path_translated'] = + wasmExports['wasm_set_path_translated']; + _wasm_set_skip_shebang = Module['_wasm_set_skip_shebang'] = + wasmExports['wasm_set_skip_shebang']; + _wasm_set_request_uri = Module['_wasm_set_request_uri'] = + wasmExports['wasm_set_request_uri']; + _wasm_set_request_method = Module['_wasm_set_request_method'] = + wasmExports['wasm_set_request_method']; + _wasm_set_request_host = Module['_wasm_set_request_host'] = + wasmExports['wasm_set_request_host']; + _wasm_set_content_type = Module['_wasm_set_content_type'] = + wasmExports['wasm_set_content_type']; + _wasm_set_request_body = Module['_wasm_set_request_body'] = + wasmExports['wasm_set_request_body']; + _wasm_set_content_length = Module['_wasm_set_content_length'] = + wasmExports['wasm_set_content_length']; + _wasm_set_cookies = Module['_wasm_set_cookies'] = + wasmExports['wasm_set_cookies']; + _wasm_set_request_port = Module['_wasm_set_request_port'] = + wasmExports['wasm_set_request_port']; + _wasm_sapi_request_shutdown = Module['_wasm_sapi_request_shutdown'] = + wasmExports['wasm_sapi_request_shutdown']; + _wasm_sapi_handle_request = Module['_wasm_sapi_handle_request'] = + wasmExports['wasm_sapi_handle_request']; + _php_wasm_init = Module['_php_wasm_init'] = + wasmExports['php_wasm_init']; + _wasm_free = + PHPLoader['free'] = + Module['_wasm_free'] = + wasmExports['wasm_free']; + _wasm_get_end_offset = Module['_wasm_get_end_offset'] = + wasmExports['wasm_get_end_offset']; + ___wrap_getpid = Module['___wrap_getpid'] = + wasmExports['__wrap_getpid']; + _wasm_trace = Module['_wasm_trace'] = wasmExports['wasm_trace']; + _initgroups = Module['_initgroups'] = wasmExports['initgroups']; + ___funcs_on_exit = wasmExports['__funcs_on_exit']; + ___dl_seterr = wasmExports['__dl_seterr']; + __emscripten_find_dylib = wasmExports['_emscripten_find_dylib']; + _emscripten_builtin_memalign = + wasmExports['emscripten_builtin_memalign']; + __emscripten_timeout = wasmExports['_emscripten_timeout']; + _emscripten_get_sbrk_ptr = wasmExports['emscripten_get_sbrk_ptr']; + _setThrew = wasmExports['setThrew']; + __emscripten_stack_restore = wasmExports['_emscripten_stack_restore']; + __emscripten_stack_alloc = wasmExports['_emscripten_stack_alloc']; + _emscripten_stack_get_current = + wasmExports['emscripten_stack_get_current']; + __ZNSt3__211__call_onceERVmPvPFvS2_E = Module[ + '__ZNSt3__211__call_onceERVmPvPFvS2_E' + ] = wasmExports['_ZNSt3__211__call_onceERVmPvPFvS2_E']; + __ZNSt3__218condition_variable10notify_allEv = Module[ + '__ZNSt3__218condition_variable10notify_allEv' + ] = wasmExports['_ZNSt3__218condition_variable10notify_allEv']; + __ZNSt3__25mutex4lockEv = Module['__ZNSt3__25mutex4lockEv'] = + wasmExports['_ZNSt3__25mutex4lockEv']; + __ZNSt3__25mutex6unlockEv = Module['__ZNSt3__25mutex6unlockEv'] = + wasmExports['_ZNSt3__25mutex6unlockEv']; + dynCall_viii = dynCalls['viii'] = wasmExports['dynCall_viii']; + dynCall_vi = dynCalls['vi'] = wasmExports['dynCall_vi']; + dynCall_ii = dynCalls['ii'] = wasmExports['dynCall_ii']; + dynCall_iii = dynCalls['iii'] = wasmExports['dynCall_iii']; + dynCall_iiiii = dynCalls['iiiii'] = wasmExports['dynCall_iiiii']; + dynCall_viiii = dynCalls['viiii'] = wasmExports['dynCall_viiii']; + dynCall_vii = dynCalls['vii'] = wasmExports['dynCall_vii']; + dynCall_viiiii = dynCalls['viiiii'] = wasmExports['dynCall_viiiii']; + dynCall_iiii = dynCalls['iiii'] = wasmExports['dynCall_iiii']; + dynCall_iiiiii = dynCalls['iiiiii'] = wasmExports['dynCall_iiiiii']; + dynCall_v = dynCalls['v'] = wasmExports['dynCall_v']; + dynCall_i = dynCalls['i'] = wasmExports['dynCall_i']; + dynCall_iiiiiiii = dynCalls['iiiiiiii'] = + wasmExports['dynCall_iiiiiiii']; + dynCall_iijii = dynCalls['iijii'] = wasmExports['dynCall_iijii']; + dynCall_iiiiiii = dynCalls['iiiiiii'] = wasmExports['dynCall_iiiiiii']; + dynCall_iiji = dynCalls['iiji'] = wasmExports['dynCall_iiji']; + dynCall_iiiiiiiiii = dynCalls['iiiiiiiiii'] = + wasmExports['dynCall_iiiiiiiiii']; + dynCall_viiiiiiii = dynCalls['viiiiiiii'] = + wasmExports['dynCall_viiiiiiii']; + dynCall_viiiiiii = dynCalls['viiiiiii'] = + wasmExports['dynCall_viiiiiii']; + dynCall_iiiiiiiii = dynCalls['iiiiiiiii'] = + wasmExports['dynCall_iiiiiiiii']; + dynCall_viiiiiiiii = dynCalls['viiiiiiiii'] = + wasmExports['dynCall_viiiiiiiii']; + dynCall_viiiiii = dynCalls['viiiiii'] = wasmExports['dynCall_viiiiii']; + dynCall_ij = dynCalls['ij'] = wasmExports['dynCall_ij']; + dynCall_iiiij = dynCalls['iiiij'] = wasmExports['dynCall_iiiij']; + dynCall_vijii = dynCalls['vijii'] = wasmExports['dynCall_vijii']; + dynCall_iij = dynCalls['iij'] = wasmExports['dynCall_iij']; + dynCall_iiiiiij = dynCalls['iiiiiij'] = wasmExports['dynCall_iiiiiij']; + dynCall_iiid = dynCalls['iiid'] = wasmExports['dynCall_iiid']; + dynCall_iiij = dynCalls['iiij'] = wasmExports['dynCall_iiij']; + dynCall_dii = dynCalls['dii'] = wasmExports['dynCall_dii']; + dynCall_jii = dynCalls['jii'] = wasmExports['dynCall_jii']; + dynCall_ji = dynCalls['ji'] = wasmExports['dynCall_ji']; + dynCall_vid = dynCalls['vid'] = wasmExports['dynCall_vid']; + dynCall_vij = dynCalls['vij'] = wasmExports['dynCall_vij']; + dynCall_di = dynCalls['di'] = wasmExports['dynCall_di']; + dynCall_iiiiijii = dynCalls['iiiiijii'] = + wasmExports['dynCall_iiiiijii']; + dynCall_j = dynCalls['j'] = wasmExports['dynCall_j']; + dynCall_jj = dynCalls['jj'] = wasmExports['dynCall_jj']; + dynCall_jiij = dynCalls['jiij'] = wasmExports['dynCall_jiij']; + dynCall_iiiiji = dynCalls['iiiiji'] = wasmExports['dynCall_iiiiji']; + dynCall_iiiijii = dynCalls['iiiijii'] = wasmExports['dynCall_iiiijii']; + dynCall_viiji = dynCalls['viiji'] = wasmExports['dynCall_viiji']; + dynCall_viijii = dynCalls['viijii'] = wasmExports['dynCall_viijii']; + dynCall_iiiiiiiiiii = dynCalls['iiiiiiiiiii'] = + wasmExports['dynCall_iiiiiiiiiii']; + dynCall_iiiijji = dynCalls['iiiijji'] = wasmExports['dynCall_iiiijji']; + dynCall_dd = dynCalls['dd'] = wasmExports['dynCall_dd']; + dynCall_ddd = dynCalls['ddd'] = wasmExports['dynCall_ddd']; + dynCall_jiji = dynCalls['jiji'] = wasmExports['dynCall_jiji']; + dynCall_iidiiii = dynCalls['iidiiii'] = wasmExports['dynCall_iidiiii']; + _asyncify_start_unwind = wasmExports['asyncify_start_unwind']; + _asyncify_stop_unwind = wasmExports['asyncify_stop_unwind']; + _asyncify_start_rewind = wasmExports['asyncify_start_rewind']; + _asyncify_stop_rewind = wasmExports['asyncify_stop_rewind']; + memory = wasmMemory = wasmExports['memory']; + __indirect_function_table = wasmTable = + wasmExports['__indirect_function_table']; + } + + var ___heap_base = 5122080; + + var wasmImports = { + /** @export */ __assert_fail: ___assert_fail, + /** @export */ __asyncify_data: ___asyncify_data, + /** @export */ __asyncify_state: ___asyncify_state, + /** @export */ __asyncjs__js_module_onMessage, + /** @export */ __call_sighandler: ___call_sighandler, + /** @export */ __syscall_accept4: ___syscall_accept4, + /** @export */ __syscall_bind: ___syscall_bind, + /** @export */ __syscall_chdir: ___syscall_chdir, + /** @export */ __syscall_chmod: ___syscall_chmod, + /** @export */ __syscall_connect: ___syscall_connect, + /** @export */ __syscall_dup: ___syscall_dup, + /** @export */ __syscall_dup3: ___syscall_dup3, + /** @export */ __syscall_faccessat: ___syscall_faccessat, + /** @export */ __syscall_fchmod: ___syscall_fchmod, + /** @export */ __syscall_fchown32: ___syscall_fchown32, + /** @export */ __syscall_fchownat: ___syscall_fchownat, + /** @export */ __syscall_fcntl64: ___syscall_fcntl64, + /** @export */ __syscall_fstat64: ___syscall_fstat64, + /** @export */ __syscall_ftruncate64: ___syscall_ftruncate64, + /** @export */ __syscall_getcwd: ___syscall_getcwd, + /** @export */ __syscall_getdents64: ___syscall_getdents64, + /** @export */ __syscall_getpeername: ___syscall_getpeername, + /** @export */ __syscall_getsockname: ___syscall_getsockname, + /** @export */ __syscall_getsockopt: ___syscall_getsockopt, + /** @export */ __syscall_ioctl: ___syscall_ioctl, + /** @export */ __syscall_listen: ___syscall_listen, + /** @export */ __syscall_lstat64: ___syscall_lstat64, + /** @export */ __syscall_mkdirat: ___syscall_mkdirat, + /** @export */ __syscall_newfstatat: ___syscall_newfstatat, + /** @export */ __syscall_openat: ___syscall_openat, + /** @export */ __syscall_pipe: ___syscall_pipe, + /** @export */ __syscall_readlinkat: ___syscall_readlinkat, + /** @export */ __syscall_recvfrom: ___syscall_recvfrom, + /** @export */ __syscall_renameat: ___syscall_renameat, + /** @export */ __syscall_rmdir: ___syscall_rmdir, + /** @export */ __syscall_sendto: ___syscall_sendto, + /** @export */ __syscall_socket: ___syscall_socket, + /** @export */ __syscall_stat64: ___syscall_stat64, + /** @export */ __syscall_statfs64: ___syscall_statfs64, + /** @export */ __syscall_symlinkat: ___syscall_symlinkat, + /** @export */ __syscall_unlinkat: ___syscall_unlinkat, + /** @export */ __syscall_utimensat: ___syscall_utimensat, + /** @export */ _abort_js: __abort_js, + /** @export */ _dlopen_js: __dlopen_js, + /** @export */ _dlsym_js: __dlsym_js, + /** @export */ _emscripten_lookup_name: __emscripten_lookup_name, + /** @export */ _emscripten_runtime_keepalive_clear: + __emscripten_runtime_keepalive_clear, + /** @export */ _emscripten_throw_longjmp: __emscripten_throw_longjmp, + /** @export */ _gmtime_js: __gmtime_js, + /** @export */ _localtime_js: __localtime_js, + /** @export */ _mktime_js: __mktime_js, + /** @export */ _mmap_js: __mmap_js, + /** @export */ _munmap_js: __munmap_js, + /** @export */ _setitimer_js: __setitimer_js, + /** @export */ _tzset_js: __tzset_js, + /** @export */ clock_time_get: _clock_time_get, + /** @export */ emscripten_date_now: _emscripten_date_now, + /** @export */ emscripten_get_heap_max: _emscripten_get_heap_max, + /** @export */ emscripten_get_now: _emscripten_get_now, + /** @export */ emscripten_resize_heap: _emscripten_resize_heap, + /** @export */ emscripten_sleep: _emscripten_sleep, + /** @export */ environ_get: _environ_get, + /** @export */ environ_sizes_get: _environ_sizes_get, + /** @export */ exit: _exit, + /** @export */ fd_close: _fd_close, + /** @export */ fd_fdstat_get: _fd_fdstat_get, + /** @export */ fd_read: _fd_read, + /** @export */ fd_seek: _fd_seek, + /** @export */ fd_sync: _fd_sync, + /** @export */ fd_write: _fd_write, + /** @export */ getaddrinfo: _getaddrinfo, + /** @export */ getnameinfo: _getnameinfo, + /** @export */ getprotobyname: _getprotobyname, + /** @export */ getprotobynumber: _getprotobynumber, + /** @export */ invoke_i, + /** @export */ invoke_ii, + /** @export */ invoke_iii, + /** @export */ invoke_iiii, + /** @export */ invoke_iiiii, + /** @export */ invoke_iiiiii, + /** @export */ invoke_iiiiiii, + /** @export */ invoke_iiiiiiii, + /** @export */ invoke_iiiiiiiii, + /** @export */ invoke_iiiiiiiiii, + /** @export */ invoke_jii, + /** @export */ invoke_v, + /** @export */ invoke_vi, + /** @export */ invoke_vii, + /** @export */ invoke_viii, + /** @export */ invoke_viiii, + /** @export */ invoke_viiiii, + /** @export */ invoke_viiiiiii, + /** @export */ js_fd_read, + /** @export */ js_flock: _js_flock, + /** @export */ js_getpid: _js_getpid, + /** @export */ js_open_process: _js_open_process, + /** @export */ js_popen_to_file, + /** @export */ js_release_file_locks: _js_release_file_locks, + /** @export */ js_wasm_trace: _js_wasm_trace, + /** @export */ proc_exit: _proc_exit, + /** @export */ strptime: _strptime, + /** @export */ wasm_poll_socket, + /** @export */ wasm_setsockopt: _wasm_setsockopt, + }; + + function invoke_vii(index, a1, a2) { + var sp = stackSave(); + try { + dynCalls['vii'](index, a1, a2); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_vi(index, a1) { + var sp = stackSave(); + try { + dynCalls['vi'](index, a1); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_ii(index, a1) { + var sp = stackSave(); + try { + return dynCalls['ii'](index, a1); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_viii(index, a1, a2, a3) { + var sp = stackSave(); + try { + dynCalls['viii'](index, a1, a2, a3); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_iiiiiiii(index, a1, a2, a3, a4, a5, a6, a7) { + var sp = stackSave(); + try { + return dynCalls['iiiiiiii'](index, a1, a2, a3, a4, a5, a6, a7); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_v(index) { + var sp = stackSave(); + try { + dynCalls['v'](index); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_i(index) { + var sp = stackSave(); + try { + return dynCalls['i'](index); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_iiiii(index, a1, a2, a3, a4) { + var sp = stackSave(); + try { + return dynCalls['iiiii'](index, a1, a2, a3, a4); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_iiii(index, a1, a2, a3) { + var sp = stackSave(); + try { + return dynCalls['iiii'](index, a1, a2, a3); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_iiiiiii(index, a1, a2, a3, a4, a5, a6) { + var sp = stackSave(); + try { + return dynCalls['iiiiiii'](index, a1, a2, a3, a4, a5, a6); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_viiii(index, a1, a2, a3, a4) { + var sp = stackSave(); + try { + dynCalls['viiii'](index, a1, a2, a3, a4); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_iii(index, a1, a2) { + var sp = stackSave(); + try { + return dynCalls['iii'](index, a1, a2); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_viiiii(index, a1, a2, a3, a4, a5) { + var sp = stackSave(); + try { + dynCalls['viiiii'](index, a1, a2, a3, a4, a5); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_viiiiiii(index, a1, a2, a3, a4, a5, a6, a7) { + var sp = stackSave(); + try { + dynCalls['viiiiiii'](index, a1, a2, a3, a4, a5, a6, a7); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_iiiiii(index, a1, a2, a3, a4, a5) { + var sp = stackSave(); + try { + return dynCalls['iiiiii'](index, a1, a2, a3, a4, a5); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_iiiiiiiii(index, a1, a2, a3, a4, a5, a6, a7, a8) { + var sp = stackSave(); + try { + return dynCalls['iiiiiiiii'](index, a1, a2, a3, a4, a5, a6, a7, a8); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_iiiiiiiiii(index, a1, a2, a3, a4, a5, a6, a7, a8, a9) { + var sp = stackSave(); + try { + return dynCalls['iiiiiiiiii']( + index, + a1, + a2, + a3, + a4, + a5, + a6, + a7, + a8, + a9 + ); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + + function invoke_jii(index, a1, a2) { + var sp = stackSave(); + try { + return dynCalls['jii'](index, a1, a2); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + return 0n; + } + } + + // include: postamble.js + // === Auto-generated postamble setup entry stuff === + function callMain(args = []) { + var entryFunction = resolveGlobalSymbol('main').sym; + // Main modules can't tell if they have main() at compile time, since it may + // arrive from a dynamic library. + if (!entryFunction) return; + args.unshift(thisProgram); + var argc = args.length; + var argv = stackAlloc((argc + 1) * 4); + var argv_ptr = argv; + for (var arg of args) { + HEAPU32[argv_ptr >> 2] = stringToUTF8OnStack(arg); + argv_ptr += 4; + } + HEAPU32[argv_ptr >> 2] = 0; + try { + var ret = entryFunction(argc, argv); + // if we're not running an evented main loop, it's time to exit + exitJS(ret, /* implicit = */ true); + return ret; + } catch (e) { + return handleException(e); + } + } + + function run(args = arguments_) { + if (runDependencies > 0) { + dependenciesFulfilled = run; + return; + } + preRun(); + // a preRun added a dependency, run will be called later + if (runDependencies > 0) { + dependenciesFulfilled = run; + return; + } + function doRun() { + // run may have just been called through dependencies being fulfilled just in this very frame, + // or while the async setStatus time below was happening + Module['calledRun'] = true; + if (ABORT) return; + initRuntime(); + preMain(); + Module['onRuntimeInitialized']?.(); + var noInitialRun = Module['noInitialRun'] || true; + if (!noInitialRun) callMain(args); + postRun(); + } + if (Module['setStatus']) { + Module['setStatus']('Running...'); + setTimeout(() => { + setTimeout(() => Module['setStatus'](''), 1); + doRun(); + }, 1); + } else { + doRun(); + } + } + + var wasmExports; + + // With async instantation wasmExports is assigned asynchronously when the + // instance is received. + createWasm(); + + run(); + /** + * Emscripten resolves `localhost` to a random IP address. Let's + * make it always resolve to 127.0.0.1. + */ + DNS.address_map.addrs.localhost = '127.0.0.1'; + + /** + * Debugging Asyncify errors is tricky because the stack trace is lost when the + * error is thrown. This code saves the stack trace in a global variable + * so that it can be inspected later. + */ + PHPLoader.debug = 'debug' in PHPLoader ? PHPLoader.debug : true; + if (PHPLoader.debug && typeof Asyncify !== 'undefined') { + const originalHandleSleep = Asyncify.handleSleep; + Asyncify.handleSleep = function (startAsync) { + if (!ABORT) { + Module['lastAsyncifyStackSource'] = new Error(); + } + return originalHandleSleep(startAsync); + }; + } + + /** + * Data dependencies call removeRunDependency() when they are loaded. + * The synchronous call stack then continues to run. If an error occurs + * in PHP initialization, e.g. Out Of Memory error, it will not be + * caught by any try/catch. This override propagates the failure to + * PHPLoader.onAbort() so that it can be handled. + */ + const originalRemoveRunDependency = PHPLoader['removeRunDependency']; + PHPLoader['removeRunDependency'] = function (...args) { + try { + originalRemoveRunDependency(...args); + } catch (e) { + PHPLoader['onAbort'](e); + } + }; + + if (typeof NODEFS === 'object') { + // We override NODEFS.createNode() to add an `isSharedFS` flag to all NODEFS + // nodes. This way we can tell whether file-locking is needed and possible + // for an FS node, even if wrapped with PROXYFS. + const originalNodeFsCreateNode = NODEFS.createNode; + NODEFS.createNode = function createNodeWithSharedFlag() { + const node = originalNodeFsCreateNode.apply(NODEFS, arguments); + node.isSharedFS = true; + return node; + }; + + var originalHashAddNode = FS.hashAddNode; + FS.hashAddNode = function hashAddNodeIfNotSharedFS(node) { + if (node?.isSharedFS) { + // Avoid caching shared VFS nodes so multiple instances + // can access the same underlying filesystem without + // conflicting caches. + return; + } + return originalHashAddNode.apply(FS, arguments); + }; + } + + /** + * Expose the PHP version so the PHP class can make version-specific + * adjustments to `php.ini`. + */ + PHPLoader['phpVersion'] = (() => { + const [major, minor, patch] = phpVersionString.split('.').map(Number); + return { major, minor, patch }; + })(); + + return PHPLoader; + + // Close the opening bracket from esm-prefix.js: +} diff --git a/packages/php-wasm/node-builds/5-2/build.js b/packages/php-wasm/node-builds/5-2/build.js new file mode 100644 index 00000000000..4cc7fec181d --- /dev/null +++ b/packages/php-wasm/node-builds/5-2/build.js @@ -0,0 +1,90 @@ +import esbuild from 'esbuild'; +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const projectRoot = path.resolve(__dirname, '..', '..', '..', '..'); +const packagePath = path.join(projectRoot, 'packages/php-wasm/node-builds/5-2'); +const distPath = path.join( + projectRoot, + 'dist/packages/php-wasm/node-builds/5-2' +); + +try { + fs.mkdirSync(distPath, { recursive: true }); +} catch (e) { + // Ignore +} + +/** + * Plugin to rewrite imports to work from the dist directory. + * Dynamic imports need to be preserved as external and paths adjusted. + */ +const externalPathPlugin = { + name: 'external-path', + setup(build) { + // Mark PHP loader files as external and rewrite their paths + build.onResolve( + { filter: /\.\.\/(?:jspi|asyncify)\/.*\.js$/ }, + (args) => { + const newPath = args.path.replace('../', './'); + return { path: newPath, external: true }; + } + ); + // Mark extension .so files as external and rewrite paths + build.onResolve( + { filter: /\.\.\/(?:jspi|asyncify)\/extensions\/.*\.so\?url$/ }, + (args) => { + const newPath = args.path.replace('../', './'); + return { path: newPath, external: true }; + } + ); + }, +}; + +async function build() { + // CommonJS build + await esbuild.build({ + entryPoints: [`${packagePath}/src/index.ts`], + supported: { 'dynamic-import': true }, + outExtension: { '.js': '.cjs' }, + outdir: distPath, + platform: 'node', + assetNames: '[name]', + chunkNames: '[name]', + logOverride: { + 'direct-eval': 'silent', + 'commonjs-variable-in-esm': 'silent', + }, + format: 'cjs', + bundle: true, + tsconfig: `${packagePath}/tsconfig.json`, + external: ['@php-wasm/*', 'wasm-feature-detect'], + loader: { '.wasm': 'file', '.so': 'file' }, + plugins: [externalPathPlugin], + }); + + // ESM build + await esbuild.build({ + entryPoints: [`${packagePath}/src/index.ts`], + outdir: distPath, + platform: 'node', + assetNames: '[name]', + chunkNames: '[name]', + logOverride: { + 'direct-eval': 'silent', + 'commonjs-variable-in-esm': 'silent', + }, + packages: 'external', + bundle: true, + tsconfig: `${packagePath}/tsconfig.json`, + external: ['@php-wasm/*', 'wasm-feature-detect'], + supported: { 'dynamic-import': true, 'top-level-await': true }, + format: 'esm', + loader: { '.wasm': 'file', '.so': 'file' }, + plugins: [externalPathPlugin], + }); +} +build(); diff --git a/packages/php-wasm/node-builds/5-2/jspi/5_2_17/php_5_2.wasm b/packages/php-wasm/node-builds/5-2/jspi/5_2_17/php_5_2.wasm new file mode 100755 index 00000000000..737ffc007a0 Binary files /dev/null and b/packages/php-wasm/node-builds/5-2/jspi/5_2_17/php_5_2.wasm differ diff --git a/packages/php-wasm/node-builds/5-2/jspi/php_5_2.js b/packages/php-wasm/node-builds/5-2/jspi/php_5_2.js new file mode 100644 index 00000000000..80f7a630f5e --- /dev/null +++ b/packages/php-wasm/node-builds/5-2/jspi/php_5_2.js @@ -0,0 +1,10392 @@ +// Emscripten generates code for Node.js that uses the `require` function. +// We need to explicitly create a require function to avoid errors when running +// this code in Node.js as an ES module. +import { createRequire } from 'module'; +const require = createRequire(import.meta.url); +// Note: The path and url modules are currently needed by code injected by the php-wasm Dockerfile. +import path from 'path'; +import { fileURLToPath } from 'url'; + +// Determine the current directory path. In CJS mode, __dirname is available. +// In ESM mode, we derive it from import.meta.url. +const currentDirPath = + typeof __dirname !== 'undefined' + ? __dirname + : path.dirname(fileURLToPath(import.meta.url)); +const dependencyFilename = path.join(currentDirPath, '5_2_17', 'php_5_2.wasm'); +export { dependencyFilename }; +export const dependenciesTotalSize = 9914905; +const phpVersionString = '5.2.17'; +export function init(RuntimeName, PHPLoader) { + // The rest of the code comes from the built php.js file and esm-suffix.js + // include: shell.js + // include: minimum_runtime_check.js + // end include: minimum_runtime_check.js + // The Module object: Our interface to the outside world. We import + // and export values on it. There are various ways Module can be used: + // 1. Not defined. We create it here + // 2. A function parameter, function(moduleArg) => Promise + // 3. pre-run appended it, var Module = {}; ..generated code.. + // 4. External script tag defines var Module. + // We need to check if Module already exists (e.g. case 3 above). + // Substitution will be replaced with actual code on later stage of the build, + // this way Closure Compiler will not mangle it (e.g. case 4. above). + // Note that if you want to run closure, and also to use Module + // after the generated code, you will need to define var Module = {}; + // before the code. Then that object will be used in the code, and you + // can continue to use Module afterwards as well. + var Module = typeof PHPLoader != 'undefined' ? PHPLoader : {}; + + var ENVIRONMENT_IS_WORKER = RuntimeName === 'WORKER'; + + var ENVIRONMENT_IS_NODE = RuntimeName === 'NODE'; + + // --pre-jses are emitted after the Module integration code, so that they can + // refer to Module (if they choose; they can also define Module) + var arguments_ = []; + + var thisProgram = './this.program'; + + var quit_ = (status, toThrow) => { + throw toThrow; + }; + + var _scriptName; + + if (typeof __filename != 'undefined') { + // Node + _scriptName = __filename; + } else /*no-op*/ { + } + + // `/` should be present at the end if `scriptDirectory` is not empty + var scriptDirectory = ''; + + function locateFile(path) { + if (Module['locateFile']) { + return Module['locateFile'](path, scriptDirectory); + } + return scriptDirectory + path; + } + + // Hooks that are implemented differently in different runtime environments. + var readAsync, readBinary; + + if (ENVIRONMENT_IS_NODE) { + // These modules will usually be used on Node.js. Load them eagerly to avoid + // the complexity of lazy-loading. + var fs = require('fs'); + scriptDirectory = currentDirPath + '/'; + // include: node_shell_read.js + readBinary = (filename) => { + // We need to re-wrap `file://` strings to URLs. + filename = isFileURI(filename) ? new URL(filename) : filename; + var ret = fs.readFileSync(filename); + return ret; + }; + readAsync = async (filename, binary = true) => { + // See the comment in the `readBinary` function. + filename = isFileURI(filename) ? new URL(filename) : filename; + var ret = fs.readFileSync(filename, binary ? undefined : 'utf8'); + return ret; + }; + // end include: node_shell_read.js + if (process.argv.length > 1) { + thisProgram = process.argv[1].replace(/\\/g, '/'); + } + arguments_ = process.argv.slice(2); + // MODULARIZE will export the module in the proper place outside, we don't need to export here + if (typeof module != 'undefined') { + module['exports'] = Module; + } + quit_ = (status, toThrow) => { + process.exitCode = status; + throw toThrow; + }; + } else // Note that this includes Node.js workers when relevant (pthreads is enabled). + // Node.js workers are detected as a combination of ENVIRONMENT_IS_WORKER and + // ENVIRONMENT_IS_NODE. + { + } + + var out = console.log.bind(console); + + var err = console.error.bind(console); + + // end include: shell.js + // include: preamble.js + // === Preamble library stuff === + // Documentation for the public APIs defined in this file must be updated in: + // site/source/docs/api_reference/preamble.js.rst + // A prebuilt local version of the documentation is available at: + // site/build/text/docs/api_reference/preamble.js.txt + // You can also build docs locally as HTML or other formats in site/ + // An online HTML version (which may be of a different version of Emscripten) + // is up at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html + var dynamicLibraries = []; + + var wasmBinary; + + // Wasm globals + //======================================== + // Runtime essentials + //======================================== + // whether we are quitting the application. no code should run after this. + // set in exit() and abort() + var ABORT = false; + + // set by exit() and abort(). Passed to 'onExit' handler. + // NOTE: This is also used as the process return code code in shell environments + // but only when noExitRuntime is false. + var EXITSTATUS; + + /** + * Indicates whether filename is delivered via file protocol (as opposed to http/https) + * @noinline + */ var isFileURI = (filename) => filename.startsWith('file://'); + + // include: runtime_common.js + // include: runtime_stack_check.js + // end include: runtime_stack_check.js + // include: runtime_exceptions.js + // end include: runtime_exceptions.js + // include: runtime_debug.js + // end include: runtime_debug.js + // Memory management + var /** @type {!Int8Array} */ HEAP8, + /** @type {!Uint8Array} */ HEAPU8, + /** @type {!Int16Array} */ HEAP16, + /** @type {!Uint16Array} */ HEAPU16, + /** @type {!Int32Array} */ HEAP32, + /** @type {!Uint32Array} */ HEAPU32, + /** @type {!Float32Array} */ HEAPF32, + /** @type {!Float64Array} */ HEAPF64; + + // BigInt64Array type is not correctly defined in closure + var /** not-@type {!BigInt64Array} */ HEAP64, + /* BigUint64Array type is not correctly defined in closure +/** not-@type {!BigUint64Array} */ HEAPU64; + + var runtimeInitialized = false; + + var runtimeExited = false; + + function updateMemoryViews() { + var b = wasmMemory.buffer; + HEAP8 = new Int8Array(b); + HEAP16 = new Int16Array(b); + Module['HEAPU8'] = HEAPU8 = new Uint8Array(b); + HEAPU16 = new Uint16Array(b); + HEAP32 = new Int32Array(b); + Module['HEAPU32'] = HEAPU32 = new Uint32Array(b); + HEAPF32 = new Float32Array(b); + HEAPF64 = new Float64Array(b); + HEAP64 = new BigInt64Array(b); + HEAPU64 = new BigUint64Array(b); + } + + // include: memoryprofiler.js + // end include: memoryprofiler.js + // end include: runtime_common.js + var __RELOC_FUNCS__ = []; + + function preRun() { + if (Module['preRun']) { + if (typeof Module['preRun'] == 'function') + Module['preRun'] = [Module['preRun']]; + while (Module['preRun'].length) { + addOnPreRun(Module['preRun'].shift()); + } + } + // Begin ATPRERUNS hooks + callRuntimeCallbacks(onPreRuns); + } + + function initRuntime() { + runtimeInitialized = true; + callRuntimeCallbacks(__RELOC_FUNCS__); + // Begin ATINITS hooks + callRuntimeCallbacks(onInits); + if (!Module['noFSInit'] && !FS.initialized) FS.init(); + TTY.init(); + SOCKFS.root = FS.mount(SOCKFS, {}, null); + PIPEFS.root = FS.mount(PIPEFS, {}, null); + // End ATINITS hooks + wasmExports['__wasm_call_ctors'](); + // Begin ATPOSTCTORS hooks + callRuntimeCallbacks(onPostCtors); + FS.ignorePermissions = false; + } + + function preMain() {} + + function exitRuntime() { + // PThreads reuse the runtime from the main thread. + ___funcs_on_exit(); + // Native atexit() functions + // Begin ATEXITS hooks + FS.quit(); + TTY.shutdown(); + // End ATEXITS hooks + runtimeExited = true; + } + + function postRun() { + // PThreads reuse the runtime from the main thread. + if (Module['postRun']) { + if (typeof Module['postRun'] == 'function') + Module['postRun'] = [Module['postRun']]; + while (Module['postRun'].length) { + addOnPostRun(Module['postRun'].shift()); + } + } + // Begin ATPOSTRUNS hooks + callRuntimeCallbacks(onPostRuns); + } + + /** @param {string|number=} what */ function abort(what) { + Module['onAbort']?.(what); + what = 'Aborted(' + what + ')'; + // TODO(sbc): Should we remove printing and leave it up to whoever + // catches the exception? + err(what); + ABORT = true; + what += '. Build with -sASSERTIONS for more info.'; + // Use a wasm runtime error, because a JS error might be seen as a foreign + // exception, which means we'd run destructors on it. We need the error to + // simply make the program stop. + // FIXME This approach does not work in Wasm EH because it currently does not assume + // all RuntimeErrors are from traps; it decides whether a RuntimeError is from + // a trap or not based on a hidden field within the object. So at the moment + // we don't have a way of throwing a wasm trap from JS. TODO Make a JS API that + // allows this in the wasm spec. + // Suppress closure compiler warning here. Closure compiler's builtin extern + // definition for WebAssembly.RuntimeError claims it takes no arguments even + // though it can. + // TODO(https://github.com/google/closure-compiler/pull/3913): Remove if/when upstream closure gets fixed. + // See above, in the meantime, we resort to wasm code for trapping. + // In case abort() is called before the module is initialized, wasmExports + // and its exported '__trap' function is not available, in which case we throw + // a RuntimeError. + // We trap instead of throwing RuntimeError to prevent infinite-looping in + // Wasm EH code (because RuntimeError is considered as a foreign exception and + // caught by 'catch_all'), but in case throwing RuntimeError is fine because + // the module has not even been instantiated, even less running. + if (runtimeInitialized) { + ___trap(); + } + /** @suppress {checkTypes} */ var e = new WebAssembly.RuntimeError( + what + ); + // Throw the error whether or not MODULARIZE is set because abort is used + // in code paths apart from instantiation where an exception is expected + // to be thrown when abort is called. + throw e; + } + + var wasmBinaryFile; + + function findWasmBinary() { + return locateFile(dependencyFilename); + } + + function getBinarySync(file) { + if (file == wasmBinaryFile && wasmBinary) { + return new Uint8Array(wasmBinary); + } + if (readBinary) { + return readBinary(file); + } + // Throwing a plain string here, even though it not normally adviables since + // this gets turning into an `abort` in instantiateArrayBuffer. + throw 'both async and sync fetching of the wasm failed'; + } + + async function getWasmBinary(binaryFile) { + // If we don't have the binary yet, load it asynchronously using readAsync. + if (!wasmBinary) { + // Fetch the binary using readAsync + try { + var response = await readAsync(binaryFile); + return new Uint8Array(response); + } catch {} + } + // Otherwise, getBinarySync should be able to get it synchronously + return getBinarySync(binaryFile); + } + + async function instantiateArrayBuffer(binaryFile, imports) { + try { + var binary = await getWasmBinary(binaryFile); + var instance = await WebAssembly.instantiate(binary, imports); + return instance; + } catch (reason) { + err(`failed to asynchronously prepare wasm: ${reason}`); + abort(reason); + } + } + + async function instantiateAsync(binary, binaryFile, imports) { + if (!binary && !ENVIRONMENT_IS_NODE) { + try { + var response = fetch(binaryFile, { + credentials: 'same-origin', + }); + var instantiationResult = + await WebAssembly.instantiateStreaming(response, imports); + return instantiationResult; + } catch (reason) { + // We expect the most common failure cause to be a bad MIME type for the binary, + // in which case falling back to ArrayBuffer instantiation should work. + err(`wasm streaming compile failed: ${reason}`); + err('falling back to ArrayBuffer instantiation'); + } + } + return instantiateArrayBuffer(binaryFile, imports); + } + + function getWasmImports() { + // instrumenting imports is used in asyncify in two ways: to add assertions + // that check for proper import use, and for ASYNCIFY=2 we use them to set up + // the Promise API on the import side. + Asyncify.instrumentWasmImports(wasmImports); + // prepare imports + var imports = { + env: wasmImports, + wasi_snapshot_preview1: wasmImports, + 'GOT.mem': new Proxy(wasmImports, GOTHandler), + 'GOT.func': new Proxy(wasmImports, GOTHandler), + }; + return imports; + } + + // Create the wasm instance. + // Receives the wasm imports, returns the exports. + async function createWasm() { + // Load the wasm module and create an instance of using native support in the JS engine. + // handle a generated wasm instance, receiving its exports and + // performing other necessary setup + /** @param {WebAssembly.Module=} module*/ function receiveInstance( + instance, + module + ) { + wasmExports = instance.exports; + // No relocation needed here.. but calling this just so that updateGOT is + // called. + var origExports = (wasmExports = relocateExports(wasmExports)); + wasmExports = Asyncify.instrumentWasmExports(wasmExports); + mergeLibSymbols(wasmExports, 'main'); + var metadata = getDylinkMetadata(module); + if (metadata.neededDynlibs) { + dynamicLibraries = + metadata.neededDynlibs.concat(dynamicLibraries); + } + assignWasmExports(wasmExports); + updateGOT(origExports); + Module['wasmExports'] = wasmExports; + LDSO.init(); + loadDylibs(); + updateMemoryViews(); + removeRunDependency('wasm-instantiate'); + return wasmExports; + } + addRunDependency('wasm-instantiate'); + // Prefer streaming instantiation if available. + function receiveInstantiationResult(result) { + // 'result' is a ResultObject object which has both the module and instance. + // receiveInstance() will swap in the exports (to Module.asm) so they can be called + return receiveInstance(result['instance'], result['module']); + } + var info = getWasmImports(); + // User shell pages can write their own Module.instantiateWasm = function(imports, successCallback) callback + // to manually instantiate the Wasm module themselves. This allows pages to + // run the instantiation parallel to any other async startup actions they are + // performing. + // Also pthreads and wasm workers initialize the wasm instance through this + // path. + if (Module['instantiateWasm']) { + return new Promise((resolve, reject) => { + Module['instantiateWasm'](info, (inst, mod) => { + resolve(receiveInstance(inst, mod)); + }); + }); + } + wasmBinaryFile ??= findWasmBinary(); + var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info); + var exports = receiveInstantiationResult(result); + return exports; + } + + // With MAIN_MODULE + ASYNCIFY the normal method of placing stub functions in + // wasmImports for as-yet-undefined symbols doesn't work since ASYNCIFY then + // wraps these stub functions and we can't then replace them directly. Instead + // the stub functions call into `asyncifyStubs` which gets populated by the + // dynamic linker as symbols are loaded. + var asyncifyStubs = {}; + + // end include: preamble.js + // Begin JS library code + class ExitStatus { + name = 'ExitStatus'; + constructor(status) { + this.message = `Program terminated with exit(${status})`; + this.status = status; + } + } + ExitStatus = class PHPExitStatus extends Error { + constructor(status) { + super(status); + this.name = 'ExitStatus'; + this.message = 'Program terminated with exit(' + status + ')'; + this.status = status; + } + }; + + var GOT = {}; + + var currentModuleWeakSymbols = new Set([]); + + var GOTHandler = { + get(obj, symName) { + var rtn = GOT[symName]; + if (!rtn) { + rtn = GOT[symName] = new WebAssembly.Global( + { + value: 'i32', + mutable: true, + }, + -1 + ); + } + if (!currentModuleWeakSymbols.has(symName)) { + // Any non-weak reference to a symbol marks it as `required`, which + // enabled `reportUndefinedSymbols` to report undefined symbol errors + // correctly. + rtn.required = true; + } + return rtn; + }, + }; + + var callRuntimeCallbacks = (callbacks) => { + while (callbacks.length > 0) { + // Pass the module as the first argument. + callbacks.shift()(Module); + } + }; + + var onPostRuns = []; + + var addOnPostRun = (cb) => onPostRuns.push(cb); + + var onPreRuns = []; + + var addOnPreRun = (cb) => onPreRuns.push(cb); + + var runDependencies = 0; + + var dependenciesFulfilled = null; + + var removeRunDependency = (id) => { + runDependencies--; + Module['monitorRunDependencies']?.(runDependencies); + if (runDependencies == 0) { + if (dependenciesFulfilled) { + var callback = dependenciesFulfilled; + dependenciesFulfilled = null; + callback(); + } + } + }; + + var addRunDependency = (id) => { + runDependencies++; + Module['monitorRunDependencies']?.(runDependencies); + }; + + var UTF8Decoder = globalThis.TextDecoder && new TextDecoder(); + + var findStringEnd = (heapOrArray, idx, maxBytesToRead, ignoreNul) => { + var maxIdx = idx + maxBytesToRead; + if (ignoreNul) return maxIdx; + // TextDecoder needs to know the byte length in advance, it doesn't stop on + // null terminator by itself. + // As a tiny code save trick, compare idx against maxIdx using a negation, + // so that maxBytesToRead=undefined/NaN means Infinity. + while (heapOrArray[idx] && !(idx >= maxIdx)) ++idx; + return idx; + }; + + /** + * Given a pointer 'idx' to a null-terminated UTF8-encoded string in the given + * array that contains uint8 values, returns a copy of that string as a + * Javascript String object. + * heapOrArray is either a regular array, or a JavaScript typed array view. + * @param {number=} idx + * @param {number=} maxBytesToRead + * @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character. + * @return {string} + */ var UTF8ArrayToString = ( + heapOrArray, + idx = 0, + maxBytesToRead, + ignoreNul + ) => { + var endPtr = findStringEnd(heapOrArray, idx, maxBytesToRead, ignoreNul); + // When using conditional TextDecoder, skip it for short strings as the overhead of the native call is not worth it. + if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) { + return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr)); + } + var str = ''; + while (idx < endPtr) { + // For UTF8 byte structure, see: + // http://en.wikipedia.org/wiki/UTF-8#Description + // https://www.ietf.org/rfc/rfc2279.txt + // https://tools.ietf.org/html/rfc3629 + var u0 = heapOrArray[idx++]; + if (!(u0 & 128)) { + str += String.fromCharCode(u0); + continue; + } + var u1 = heapOrArray[idx++] & 63; + if ((u0 & 224) == 192) { + str += String.fromCharCode(((u0 & 31) << 6) | u1); + continue; + } + var u2 = heapOrArray[idx++] & 63; + if ((u0 & 240) == 224) { + u0 = ((u0 & 15) << 12) | (u1 << 6) | u2; + } else { + u0 = + ((u0 & 7) << 18) | + (u1 << 12) | + (u2 << 6) | + (heapOrArray[idx++] & 63); + } + if (u0 < 65536) { + str += String.fromCharCode(u0); + } else { + var ch = u0 - 65536; + str += String.fromCharCode( + 55296 | (ch >> 10), + 56320 | (ch & 1023) + ); + } + } + return str; + }; + + var getDylinkMetadata = (binary) => { + var offset = 0; + var end = 0; + function getU8() { + return binary[offset++]; + } + function getLEB() { + var ret = 0; + var mul = 1; + while (1) { + var byte = binary[offset++]; + ret += (byte & 127) * mul; + mul *= 128; + if (!(byte & 128)) break; + } + return ret; + } + function getString() { + var len = getLEB(); + offset += len; + return UTF8ArrayToString(binary, offset - len, len); + } + function getStringList() { + var count = getLEB(); + var rtn = []; + while (count--) rtn.push(getString()); + return rtn; + } + /** @param {string=} message */ function failIf(condition, message) { + if (condition) throw new Error(message); + } + if (binary instanceof WebAssembly.Module) { + var dylinkSection = WebAssembly.Module.customSections( + binary, + 'dylink.0' + ); + failIf(dylinkSection.length === 0, 'need dylink section'); + binary = new Uint8Array(dylinkSection[0]); + end = binary.length; + } else { + var int32View = new Uint32Array( + new Uint8Array(binary.subarray(0, 24)).buffer + ); + var magicNumberFound = int32View[0] == 1836278016; + failIf(!magicNumberFound, 'need to see wasm magic number'); + // \0asm + // we should see the dylink custom section right after the magic number and wasm version + failIf(binary[8] !== 0, 'need the dylink section to be first'); + offset = 9; + var section_size = getLEB(); + //section size + end = offset + section_size; + var name = getString(); + failIf(name !== 'dylink.0'); + } + var customSection = { + neededDynlibs: [], + tlsExports: new Set(), + weakImports: new Set(), + runtimePaths: [], + }; + var WASM_DYLINK_MEM_INFO = 1; + var WASM_DYLINK_NEEDED = 2; + var WASM_DYLINK_EXPORT_INFO = 3; + var WASM_DYLINK_IMPORT_INFO = 4; + var WASM_DYLINK_RUNTIME_PATH = 5; + var WASM_SYMBOL_TLS = 256; + var WASM_SYMBOL_BINDING_MASK = 3; + var WASM_SYMBOL_BINDING_WEAK = 1; + while (offset < end) { + var subsectionType = getU8(); + var subsectionSize = getLEB(); + if (subsectionType === WASM_DYLINK_MEM_INFO) { + customSection.memorySize = getLEB(); + customSection.memoryAlign = getLEB(); + customSection.tableSize = getLEB(); + customSection.tableAlign = getLEB(); + } else if (subsectionType === WASM_DYLINK_NEEDED) { + customSection.neededDynlibs = getStringList(); + } else if (subsectionType === WASM_DYLINK_EXPORT_INFO) { + var count = getLEB(); + while (count--) { + var symname = getString(); + var flags = getLEB(); + if (flags & WASM_SYMBOL_TLS) { + customSection.tlsExports.add(symname); + } + } + } else if (subsectionType === WASM_DYLINK_IMPORT_INFO) { + var count = getLEB(); + while (count--) { + var modname = getString(); + var symname = getString(); + var flags = getLEB(); + if ( + (flags & WASM_SYMBOL_BINDING_MASK) == + WASM_SYMBOL_BINDING_WEAK + ) { + customSection.weakImports.add(symname); + } + } + } else if (subsectionType === WASM_DYLINK_RUNTIME_PATH) { + customSection.runtimePaths = getStringList(); + } else { + // unknown subsection + offset += subsectionSize; + } + } + return customSection; + }; + + var newDSO = (name, handle, syms) => { + var dso = { + refcount: Infinity, + name, + exports: syms, + global: true, + }; + LDSO.loadedLibsByName[name] = dso; + if (handle != undefined) { + LDSO.loadedLibsByHandle[handle] = dso; + } + return dso; + }; + + var LDSO = { + loadedLibsByName: {}, + loadedLibsByHandle: {}, + init() { + newDSO('__main__', 0, wasmImports); + }, + }; + + var alignMemory = (size, alignment) => + Math.ceil(size / alignment) * alignment; + + var getMemory = (size) => { + // After the runtime is initialized, we must only use sbrk() normally. + if (runtimeInitialized) { + // Currently we don't support freeing of static data when modules are + // unloaded via dlclose. This function is tagged as `noleakcheck` to + // avoid having this reported as leak. + return _calloc(size, 1); + } + var ret = ___heap_base; + // Keep __heap_base stack aligned. + var end = ret + alignMemory(size, 16); + ___heap_base = end; + // After allocating the memory from the start of the heap we need to ensure + // that once the program starts it doesn't use this region. In relocatable + // mode we can just update the __heap_base symbol that we are exporting to + // the main module. + // When not relocatable `__heap_base` is fixed and exported by the main + // module, but we can update the `sbrk_ptr` value instead. We call + // `_emscripten_get_sbrk_ptr` knowing that it is safe to call prior to + // runtime initialization (unlike, the higher level sbrk function) + var sbrk_ptr = _emscripten_get_sbrk_ptr(); + HEAPU32[sbrk_ptr >> 2] = end; + return ret; + }; + + var isInternalSym = (symName) => + [ + 'memory', + '__memory_base', + '__table_base', + '__stack_pointer', + '__indirect_function_table', + '__cpp_exception', + '__c_longjmp', + '__wasm_apply_data_relocs', + '__dso_handle', + '__tls_size', + '__tls_align', + '__set_stack_limits', + '_emscripten_tls_init', + '__wasm_init_tls', + '__wasm_call_ctors', + '__start_em_asm', + '__stop_em_asm', + '__start_em_js', + '__stop_em_js', + ].includes(symName) || symName.startsWith('__em_js__'); + + var wasmTableMirror = []; + + var getWasmTableEntry = (funcPtr) => { + var func = wasmTableMirror[funcPtr]; + if (!func) { + /** @suppress {checkTypes} */ wasmTableMirror[funcPtr] = func = + wasmTable.get(funcPtr); + if (Asyncify.isAsyncExport(func)) { + wasmTableMirror[funcPtr] = func = + Asyncify.makeAsyncFunction(func); + } + } + return func; + }; + + var updateTableMap = (offset, count) => { + if (functionsInTableMap) { + for (var i = offset; i < offset + count; i++) { + var item = getWasmTableEntry(i); + // Ignore null values. + if (item) { + functionsInTableMap.set(item, i); + } + } + } + }; + + var functionsInTableMap; + + var getFunctionAddress = (func) => { + // First, create the map if this is the first use. + if (!functionsInTableMap) { + functionsInTableMap = new WeakMap(); + updateTableMap(0, wasmTable.length); + } + return functionsInTableMap.get(func) || 0; + }; + + var freeTableIndexes = []; + + var getEmptyTableSlot = () => { + // Reuse a free index if there is one, otherwise grow. + if (freeTableIndexes.length) { + return freeTableIndexes.pop(); + } + // Grow the table + return wasmTable['grow'](1); + }; + + var setWasmTableEntry = (idx, func) => { + /** @suppress {checkTypes} */ wasmTable.set(idx, func); + // With ABORT_ON_WASM_EXCEPTIONS wasmTable.get is overridden to return wrapped + // functions so we need to call it here to retrieve the potential wrapper correctly + // instead of just storing 'func' directly into wasmTableMirror + /** @suppress {checkTypes} */ wasmTableMirror[idx] = wasmTable.get(idx); + }; + + var uleb128EncodeWithLen = (arr) => { + const n = arr.length; + // Note: this LEB128 length encoding produces extra byte for n < 128, + // but we don't care as it's only used in a temporary representation. + return [(n % 128) | 128, n >> 7, ...arr]; + }; + + var wasmTypeCodes = { + i: 127, + // i32 + p: 127, + // i32 + j: 126, + // i64 + f: 125, + // f32 + d: 124, + // f64 + e: 111, + }; + + var generateTypePack = (types) => + uleb128EncodeWithLen( + Array.from(types, (type) => { + var code = wasmTypeCodes[type]; + return code; + }) + ); + + var convertJsFunctionToWasm = (func, sig) => { + // Rest of the module is static + var bytes = Uint8Array.of( + 0, + 97, + 115, + 109, // magic ("\0asm") + 1, + 0, + 0, + 0, // version: 1 + 1, // Type section code + // The module is static, with the exception of the type section, which is + // generated based on the signature passed in. + ...uleb128EncodeWithLen([ + 1, // count: 1 + 96, // param types + ...generateTypePack(sig.slice(1)), // return types (for now only supporting [] if `void` and single [T] otherwise) + ...generateTypePack(sig[0] === 'v' ? '' : sig[0]), + ]), // The rest of the module is static + 2, + 7, // import section + // (import "e" "f" (func 0 (type 0))) + 1, + 1, + 101, + 1, + 102, + 0, + 0, + 7, + 5, // export section + // (export "f" (func 0 (type 0))) + 1, + 1, + 102, + 0, + 0 + ); + // We can compile this wasm module synchronously because it is very small. + // This accepts an import (at "e.f"), that it reroutes to an export (at "f") + var module = new WebAssembly.Module(bytes); + var instance = new WebAssembly.Instance(module, { + e: { + f: func, + }, + }); + var wrappedFunc = instance.exports['f']; + return wrappedFunc; + }; + + /** @param {string=} sig */ var addFunction = (func, sig) => { + // Check if the function is already in the table, to ensure each function + // gets a unique index. + var rtn = getFunctionAddress(func); + if (rtn) { + return rtn; + } + // It's not in the table, add it now. + var ret = getEmptyTableSlot(); + // Set the new value. + try { + // Attempting to call this with JS function will cause of table.set() to fail + setWasmTableEntry(ret, func); + } catch (err) { + if (!(err instanceof TypeError)) { + throw err; + } + var wrapped = convertJsFunctionToWasm(func, sig); + setWasmTableEntry(ret, wrapped); + } + functionsInTableMap.set(func, ret); + return ret; + }; + + /** @param {boolean=} replace */ var updateGOT = (exports, replace) => { + for (var symName in exports) { + if (isInternalSym(symName)) { + continue; + } + var value = exports[symName]; + var existingEntry = GOT[symName] && GOT[symName].value != -1; + if (replace || !existingEntry) { + var newValue; + if (typeof value == 'function') { + newValue = addFunction(value); + } else if (typeof value == 'number') { + newValue = value; + } else { + // The GOT can only contain addresses (i.e data addresses or function + // addresses so we currently ignore other types export here. + continue; + } + GOT[symName] ??= new WebAssembly.Global({ + value: 'i32', + mutable: true, + }); + GOT[symName].value = newValue; + } + } + }; + + var isImmutableGlobal = (val) => { + if (val instanceof WebAssembly.Global) { + try { + val.value = val.value; + } catch { + return true; + } + } + return false; + }; + + var relocateExports = (exports, memoryBase = 0) => { + function relocateExport(name, value) { + // Detect immuable wasm global exports. These represent data addresses + // which are relative to `memoryBase` + if (isImmutableGlobal(value)) { + return value.value + memoryBase; + } + // Return unmodified value (no relocation required). + return value; + } + var relocated = {}; + for (var e in exports) { + relocated[e] = relocateExport(e, exports[e]); + } + return relocated; + }; + + var isSymbolDefined = (symName) => { + // Ignore 'stub' symbols that are auto-generated as part of the original + // `wasmImports` used to instantiate the main module. + var existing = wasmImports[symName]; + if (!existing || existing.stub) { + return false; + } + // Even if a symbol exists in wasmImports, and is not itself a stub, it + // could be an ASYNCIFY wrapper function that wraps a stub function. + if (symName in asyncifyStubs && !asyncifyStubs[symName]) { + return false; + } + return true; + }; + + var resolveGlobalSymbol = (symName, direct = false) => { + var sym; + if (isSymbolDefined(symName)) { + sym = wasmImports[symName]; + } + return { + sym, + name: symName, + }; + }; + + var onPostCtors = []; + + var addOnPostCtor = (cb) => onPostCtors.push(cb); + + /** + * Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the + * emscripten HEAP, returns a copy of that string as a Javascript String object. + * + * @param {number} ptr + * @param {number=} maxBytesToRead - An optional length that specifies the + * maximum number of bytes to read. You can omit this parameter to scan the + * string until the first 0 byte. If maxBytesToRead is passed, and the string + * at [ptr, ptr+maxBytesToReadr[ contains a null byte in the middle, then the + * string will cut short at that byte index. + * @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character. + * @return {string} + */ var UTF8ToString = (ptr, maxBytesToRead, ignoreNul) => + ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead, ignoreNul) : ''; + + /** + * @param {string=} libName + * @param {Object=} localScope + * @param {number=} handle + */ var loadWebAssemblyModule = ( + binary, + flags, + libName, + localScope, + handle + ) => { + var metadata = getDylinkMetadata(binary); + // loadModule loads the wasm module after all its dependencies have been loaded. + // can be called both sync/async. + function loadModule() { + // alignments are powers of 2 + var memAlign = Math.pow(2, metadata.memoryAlign); + // prepare memory + var memoryBase = metadata.memorySize + ? alignMemory( + getMemory(metadata.memorySize + memAlign), + memAlign + ) + : 0; + // TODO: add to cleanups + var tableBase = metadata.tableSize ? wasmTable.length : 0; + if (handle) { + HEAP8[handle + 8] = 1; + HEAPU32[(handle + 12) >> 2] = memoryBase; + HEAP32[(handle + 16) >> 2] = metadata.memorySize; + HEAPU32[(handle + 20) >> 2] = tableBase; + HEAP32[(handle + 24) >> 2] = metadata.tableSize; + } + if (metadata.tableSize) { + wasmTable.grow(metadata.tableSize); + } + // This is the export map that we ultimately return. We declare it here + // so it can be used within resolveSymbol. We resolve symbols against + // this local symbol map in the case there they are not present on the + // global Module object. We need this fallback because Modules sometime + // need to import their own symbols + var moduleExports; + function resolveSymbol(sym) { + var resolved = resolveGlobalSymbol(sym).sym; + if (!resolved && localScope) { + resolved = localScope[sym]; + } + if (!resolved) { + resolved = moduleExports[sym]; + } + return resolved; + } + // TODO kill ↓↓↓ (except "symbols local to this module", it will likely be + // not needed if we require that if A wants symbols from B it has to link + // to B explicitly: similarly to -Wl,--no-undefined) + // wasm dynamic libraries are pure wasm, so they cannot assist in + // their own loading. When side module A wants to import something + // provided by a side module B that is loaded later, we need to + // add a layer of indirection, but worse, we can't even tell what + // to add the indirection for, without inspecting what A's imports + // are. To do that here, we use a JS proxy (another option would + // be to inspect the binary directly). + var proxyHandler = { + get(stubs, prop) { + // symbols that should be local to this module + switch (prop) { + case '__memory_base': + return memoryBase; + + case '__table_base': + return tableBase; + } + if (prop in wasmImports && !wasmImports[prop].stub) { + // No stub needed, symbol already exists in symbol table + var res = wasmImports[prop]; + // Asyncify wraps exports, and we need to look through those wrappers. + if (res.orig) { + res = res.orig; + } + return res; + } + // Return a stub function that will resolve the symbol + // when first called. + if (!(prop in stubs)) { + var resolved; + stubs[prop] = (...args) => { + resolved ||= resolveSymbol(prop); + return resolved(...args); + }; + } + return stubs[prop]; + }, + }; + var proxy = new Proxy({}, proxyHandler); + currentModuleWeakSymbols = metadata.weakImports; + var info = { + 'GOT.mem': new Proxy({}, GOTHandler), + 'GOT.func': new Proxy({}, GOTHandler), + env: proxy, + wasi_snapshot_preview1: proxy, + }; + function postInstantiation(module, instance) { + // add new entries to functionsInTableMap + updateTableMap(tableBase, metadata.tableSize); + moduleExports = relocateExports(instance.exports, memoryBase); + updateGOT(moduleExports); + moduleExports = Asyncify.instrumentWasmExports(moduleExports); + if (!flags.allowUndefined) { + reportUndefinedSymbols(); + } + function addEmAsm(addr, body) { + var args = []; + for (var arity = 0; ; arity++) { + var argName = '$' + arity; + if (!body.includes(argName)) break; + args.push(argName); + } + args = args.join(','); + var func = `(${args}) => { ${body} };`; + ASM_CONSTS[start] = eval(func); + } + // Add any EM_ASM function that exist in the side module + if ('__start_em_asm' in moduleExports) { + var start = moduleExports['__start_em_asm']; + var stop = moduleExports['__stop_em_asm']; + while (start < stop) { + var jsString = UTF8ToString(start); + addEmAsm(start, jsString); + start = HEAPU8.indexOf(0, start) + 1; + } + } + function addEmJs(name, cSig, body) { + // The signature here is a C signature (e.g. "(int foo, char* bar)"). + // See `create_em_js` in emcc.py` for the build-time version of this + // code. + var jsArgs = []; + cSig = cSig.slice(1, -1); + if (cSig != 'void') { + cSig = cSig.split(','); + for (var arg of cSig) { + var jsArg = arg.split(' ').pop(); + jsArgs.push(jsArg.replace('*', '')); + } + } + var func = `(${jsArgs}) => ${body};`; + moduleExports[name] = eval(func); + } + for (var name in moduleExports) { + if (name.startsWith('__em_js__')) { + var start = moduleExports[name]; + var jsString = UTF8ToString(start); + // EM_JS strings are stored in the data section in the form + // SIG<::>BODY. + var [sig, body] = jsString.split('<::>'); + addEmJs(name.replace('__em_js__', ''), sig, body); + delete moduleExports[name]; + } + } + // initialize the module + var applyRelocs = moduleExports['__wasm_apply_data_relocs']; + if (applyRelocs) { + if (runtimeInitialized) { + applyRelocs(); + } else { + __RELOC_FUNCS__.push(applyRelocs); + } + } + var init = moduleExports['__wasm_call_ctors']; + if (init) { + if (runtimeInitialized) { + init(); + } else { + // we aren't ready to run compiled code yet + addOnPostCtor(init); + } + } + return moduleExports; + } + if (flags.loadAsync) { + return (async () => { + var instance; + if (binary instanceof WebAssembly.Module) { + instance = new WebAssembly.Instance(binary, info); + } else { + // Destructuring assignment without declaration has to be wrapped + // with parens or parser will treat the l-value as an object + // literal instead. + ({ module: binary, instance } = + await WebAssembly.instantiate(binary, info)); + } + return postInstantiation(binary, instance); + })(); + } + var module = + binary instanceof WebAssembly.Module + ? binary + : new WebAssembly.Module(binary); + var instance = new WebAssembly.Instance(module, info); + return postInstantiation(module, instance); + } + // We need to set rpath in flags based on the current library's rpath. + // We can't mutate flags or else if a depends on b and c and b depends on d, + // then c will be loaded with b's rpath instead of a's. + flags = { + ...flags, + rpath: { + parentLibPath: libName, + paths: metadata.runtimePaths, + }, + }; + // now load needed libraries and the module itself. + if (flags.loadAsync) { + return metadata.neededDynlibs + .reduce( + (chain, dynNeeded) => + chain.then(() => + loadDynamicLibrary(dynNeeded, flags, localScope) + ), + Promise.resolve() + ) + .then(loadModule); + } + for (var needed of metadata.neededDynlibs) { + loadDynamicLibrary(needed, flags, localScope); + } + return loadModule(); + }; + + var mergeLibSymbols = (exports, libName) => { + // add symbols into global namespace TODO: weak linking etc. + for (var [sym, exp] of Object.entries(exports)) { + // When RTLD_GLOBAL is enabled, the symbols defined by this shared object + // will be made available for symbol resolution of subsequently loaded + // shared objects. + // We should copy the symbols (which include methods and variables) from + // SIDE_MODULE to MAIN_MODULE. + const setImport = (target) => { + if (target in asyncifyStubs) { + asyncifyStubs[target] = exp; + } + if (!isSymbolDefined(target)) { + wasmImports[target] = exp; + } + }; + setImport(sym); + // Special case for handling of main symbol: If a side module exports + // `main` that also acts a definition for `__main_argc_argv` and vice + // versa. + const main_alias = '__main_argc_argv'; + if (sym == 'main') { + setImport(main_alias); + } + if (sym == main_alias) { + setImport('main'); + } + } + }; + + var asyncLoad = async (url) => { + var arrayBuffer = await readAsync(url); + return new Uint8Array(arrayBuffer); + }; + + var preloadPlugins = []; + + var registerWasmPlugin = () => { + // Use string keys here for public methods to avoid minification since the + // plugin consumer also uses string keys. + var wasmPlugin = { + promiseChainEnd: Promise.resolve(), + canHandle: (name) => + !Module['noWasmDecoding'] && name.endsWith('.so'), + handle: async ( + byteArray, + name // loadWebAssemblyModule can not load modules out-of-order, so rather + ) => + // than just running the promises in parallel, this makes a chain of + // promises to run in series. + (wasmPlugin.promiseChainEnd = wasmPlugin.promiseChainEnd.then( + async () => { + try { + var exports = await loadWebAssemblyModule( + byteArray, + { + loadAsync: true, + nodelete: true, + }, + name, + {} + ); + } catch (error) { + throw new Error( + `failed to instantiate wasm: ${name}: ${error}` + ); + } + preloadedWasm[name] = exports; + return byteArray; + } + )), + }; + preloadPlugins.push(wasmPlugin); + }; + + var preloadedWasm = {}; + + var PATH = { + isAbs: (path) => path.charAt(0) === '/', + splitPath: (filename) => { + var splitPathRe = + /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; + return splitPathRe.exec(filename).slice(1); + }, + normalizeArray: (parts, allowAboveRoot) => { + // if the path tries to go above the root, `up` ends up > 0 + var up = 0; + for (var i = parts.length - 1; i >= 0; i--) { + var last = parts[i]; + if (last === '.') { + parts.splice(i, 1); + } else if (last === '..') { + parts.splice(i, 1); + up++; + } else if (up) { + parts.splice(i, 1); + up--; + } + } + // if the path is allowed to go above the root, restore leading ..s + if (allowAboveRoot) { + for (; up; up--) { + parts.unshift('..'); + } + } + return parts; + }, + normalize: (path) => { + var isAbsolute = PATH.isAbs(path), + trailingSlash = path.slice(-1) === '/'; + // Normalize the path + path = PATH.normalizeArray( + path.split('/').filter((p) => !!p), + !isAbsolute + ).join('/'); + if (!path && !isAbsolute) { + path = '.'; + } + if (path && trailingSlash) { + path += '/'; + } + return (isAbsolute ? '/' : '') + path; + }, + dirname: (path) => { + var result = PATH.splitPath(path), + root = result[0], + dir = result[1]; + if (!root && !dir) { + // No dirname whatsoever + return '.'; + } + if (dir) { + // It has a dirname, strip trailing slash + dir = dir.slice(0, -1); + } + return root + dir; + }, + basename: (path) => path && path.match(/([^\/]+|\/)\/*$/)[1], + join: (...paths) => PATH.normalize(paths.join('/')), + join2: (l, r) => PATH.normalize(l + '/' + r), + }; + + var replaceORIGIN = (parentLibName, rpath) => { + if (rpath.startsWith('$ORIGIN')) { + // TODO: what to do if we only know the relative path of the file? It will return "." here. + var origin = PATH.dirname(parentLibName); + return rpath.replace('$ORIGIN', origin); + } + return rpath; + }; + + var stackSave = () => _emscripten_stack_get_current(); + + var stackRestore = (val) => __emscripten_stack_restore(val); + + var withStackSave = (f) => { + var stack = stackSave(); + var ret = f(); + stackRestore(stack); + return ret; + }; + + var stackAlloc = (sz) => __emscripten_stack_alloc(sz); + + var lengthBytesUTF8 = (str) => { + var len = 0; + for (var i = 0; i < str.length; ++i) { + // Gotcha: charCodeAt returns a 16-bit word that is a UTF-16 encoded code + // unit, not a Unicode code point of the character! So decode + // UTF16->UTF32->UTF8. + // See http://unicode.org/faq/utf_bom.html#utf16-3 + var c = str.charCodeAt(i); + // possibly a lead surrogate + if (c <= 127) { + len++; + } else if (c <= 2047) { + len += 2; + } else if (c >= 55296 && c <= 57343) { + len += 4; + ++i; + } else { + len += 3; + } + } + return len; + }; + + var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => { + // Parameter maxBytesToWrite is not optional. Negative values, 0, null, + // undefined and false each don't write out any bytes. + if (!(maxBytesToWrite > 0)) return 0; + var startIdx = outIdx; + var endIdx = outIdx + maxBytesToWrite - 1; + // -1 for string null terminator. + for (var i = 0; i < str.length; ++i) { + // For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description + // and https://www.ietf.org/rfc/rfc2279.txt + // and https://tools.ietf.org/html/rfc3629 + var u = str.codePointAt(i); + if (u <= 127) { + if (outIdx >= endIdx) break; + heap[outIdx++] = u; + } else if (u <= 2047) { + if (outIdx + 1 >= endIdx) break; + heap[outIdx++] = 192 | (u >> 6); + heap[outIdx++] = 128 | (u & 63); + } else if (u <= 65535) { + if (outIdx + 2 >= endIdx) break; + heap[outIdx++] = 224 | (u >> 12); + heap[outIdx++] = 128 | ((u >> 6) & 63); + heap[outIdx++] = 128 | (u & 63); + } else { + if (outIdx + 3 >= endIdx) break; + heap[outIdx++] = 240 | (u >> 18); + heap[outIdx++] = 128 | ((u >> 12) & 63); + heap[outIdx++] = 128 | ((u >> 6) & 63); + heap[outIdx++] = 128 | (u & 63); + // Gotcha: if codePoint is over 0xFFFF, it is represented as a surrogate pair in UTF-16. + // We need to manually skip over the second code unit for correct iteration. + i++; + } + } + // Null-terminate the pointer to the buffer. + heap[outIdx] = 0; + return outIdx - startIdx; + }; + + var stringToUTF8 = (str, outPtr, maxBytesToWrite) => + stringToUTF8Array(str, HEAPU8, outPtr, maxBytesToWrite); + + var stringToUTF8OnStack = (str) => { + var size = lengthBytesUTF8(str) + 1; + var ret = stackAlloc(size); + stringToUTF8(str, ret, size); + return ret; + }; + + var initRandomFill = () => (view) => crypto.getRandomValues(view); + + var randomFill = (view) => { + // Lazily init on the first invocation. + (randomFill = initRandomFill())(view); + }; + + var PATH_FS = { + resolve: (...args) => { + var resolvedPath = '', + resolvedAbsolute = false; + for (var i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) { + var path = i >= 0 ? args[i] : FS.cwd(); + // Skip empty and invalid entries + if (typeof path != 'string') { + throw new TypeError( + 'Arguments to path.resolve must be strings' + ); + } else if (!path) { + return ''; + } + resolvedPath = path + '/' + resolvedPath; + resolvedAbsolute = PATH.isAbs(path); + } + // At this point the path should be resolved to a full absolute path, but + // handle relative paths to be safe (might happen when process.cwd() fails) + resolvedPath = PATH.normalizeArray( + resolvedPath.split('/').filter((p) => !!p), + !resolvedAbsolute + ).join('/'); + return (resolvedAbsolute ? '/' : '') + resolvedPath || '.'; + }, + relative: (from, to) => { + from = PATH_FS.resolve(from).slice(1); + to = PATH_FS.resolve(to).slice(1); + function trim(arr) { + var start = 0; + for (; start < arr.length; start++) { + if (arr[start] !== '') break; + } + var end = arr.length - 1; + for (; end >= 0; end--) { + if (arr[end] !== '') break; + } + if (start > end) return []; + return arr.slice(start, end - start + 1); + } + var fromParts = trim(from.split('/')); + var toParts = trim(to.split('/')); + var length = Math.min(fromParts.length, toParts.length); + var samePartsLength = length; + for (var i = 0; i < length; i++) { + if (fromParts[i] !== toParts[i]) { + samePartsLength = i; + break; + } + } + var outputParts = []; + for (var i = samePartsLength; i < fromParts.length; i++) { + outputParts.push('..'); + } + outputParts = outputParts.concat(toParts.slice(samePartsLength)); + return outputParts.join('/'); + }, + }; + + var FS_stdin_getChar_buffer = []; + + /** @type {function(string, boolean=, number=)} */ var intArrayFromString = + (stringy, dontAddNull, length) => { + var len = length > 0 ? length : lengthBytesUTF8(stringy) + 1; + var u8array = new Array(len); + var numBytesWritten = stringToUTF8Array( + stringy, + u8array, + 0, + u8array.length + ); + if (dontAddNull) u8array.length = numBytesWritten; + return u8array; + }; + + var FS_stdin_getChar = () => { + if (!FS_stdin_getChar_buffer.length) { + var result = null; + if (ENVIRONMENT_IS_NODE) { + // we will read data by chunks of BUFSIZE + var BUFSIZE = 256; + var buf = Buffer.alloc(BUFSIZE); + var bytesRead = 0; + // For some reason we must suppress a closure warning here, even though + // fd definitely exists on process.stdin, and is even the proper way to + // get the fd of stdin, + // https://github.com/nodejs/help/issues/2136#issuecomment-523649904 + // This started to happen after moving this logic out of library_tty.js, + // so it is related to the surrounding code in some unclear manner. + /** @suppress {missingProperties} */ var fd = process.stdin.fd; + try { + bytesRead = fs.readSync(fd, buf, 0, BUFSIZE); + } catch (e) { + // Cross-platform differences: on Windows, reading EOF throws an + // exception, but on other OSes, reading EOF returns 0. Uniformize + // behavior by treating the EOF exception to return 0. + if (e.toString().includes('EOF')) bytesRead = 0; + else throw e; + } + if (bytesRead > 0) { + result = buf.slice(0, bytesRead).toString('utf-8'); + } + } else { + } + if (!result) { + return null; + } + FS_stdin_getChar_buffer = intArrayFromString(result, true); + } + return FS_stdin_getChar_buffer.shift(); + }; + + var TTY = { + ttys: [], + init() {}, + shutdown() {}, + register(dev, ops) { + TTY.ttys[dev] = { + input: [], + output: [], + ops, + }; + FS.registerDevice(dev, TTY.stream_ops); + }, + stream_ops: { + open(stream) { + var tty = TTY.ttys[stream.node.rdev]; + if (!tty) { + throw new FS.ErrnoError(43); + } + stream.tty = tty; + stream.seekable = false; + }, + close(stream) { + // flush any pending line data + stream.tty.ops.fsync(stream.tty); + }, + fsync(stream) { + stream.tty.ops.fsync(stream.tty); + }, + read(stream, buffer, offset, length, pos) { + if (!stream.tty || !stream.tty.ops.get_char) { + throw new FS.ErrnoError(60); + } + var bytesRead = 0; + for (var i = 0; i < length; i++) { + var result; + try { + result = stream.tty.ops.get_char(stream.tty); + } catch (e) { + throw new FS.ErrnoError(29); + } + if (result === undefined && bytesRead === 0) { + throw new FS.ErrnoError(6); + } + if (result === null || result === undefined) break; + bytesRead++; + buffer[offset + i] = result; + } + if (bytesRead) { + stream.node.atime = Date.now(); + } + return bytesRead; + }, + write(stream, buffer, offset, length, pos) { + if (!stream.tty || !stream.tty.ops.put_char) { + throw new FS.ErrnoError(60); + } + try { + for (var i = 0; i < length; i++) { + stream.tty.ops.put_char(stream.tty, buffer[offset + i]); + } + } catch (e) { + throw new FS.ErrnoError(29); + } + if (length) { + stream.node.mtime = stream.node.ctime = Date.now(); + } + return i; + }, + }, + default_tty_ops: { + get_char(tty) { + return FS_stdin_getChar(); + }, + put_char(tty, val) { + if (val === null || val === 10) { + out(UTF8ArrayToString(tty.output)); + tty.output = []; + } else { + if (val != 0) tty.output.push(val); + } + }, + fsync(tty) { + if (tty.output?.length > 0) { + out(UTF8ArrayToString(tty.output)); + tty.output = []; + } + }, + ioctl_tcgets(tty) { + // typical setting + return { + c_iflag: 25856, + c_oflag: 5, + c_cflag: 191, + c_lflag: 35387, + c_cc: [ + 3, 28, 127, 21, 4, 0, 1, 0, 17, 19, 26, 0, 18, 15, 23, + 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ], + }; + }, + ioctl_tcsets(tty, optional_actions, data) { + // currently just ignore + return 0; + }, + ioctl_tiocgwinsz(tty) { + return [24, 80]; + }, + }, + default_tty1_ops: { + put_char(tty, val) { + if (val === null || val === 10) { + err(UTF8ArrayToString(tty.output)); + tty.output = []; + } else { + if (val != 0) tty.output.push(val); + } + }, + fsync(tty) { + if (tty.output?.length > 0) { + err(UTF8ArrayToString(tty.output)); + tty.output = []; + } + }, + }, + }; + + var zeroMemory = (ptr, size) => HEAPU8.fill(0, ptr, ptr + size); + + var mmapAlloc = (size) => { + size = alignMemory(size, 65536); + var ptr = _emscripten_builtin_memalign(65536, size); + if (ptr) zeroMemory(ptr, size); + return ptr; + }; + + var MEMFS = { + ops_table: null, + mount(mount) { + return MEMFS.createNode(null, '/', 16895, 0); + }, + createNode(parent, name, mode, dev) { + if (FS.isBlkdev(mode) || FS.isFIFO(mode)) { + // no supported + throw new FS.ErrnoError(63); + } + MEMFS.ops_table ||= { + dir: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + lookup: MEMFS.node_ops.lookup, + mknod: MEMFS.node_ops.mknod, + rename: MEMFS.node_ops.rename, + unlink: MEMFS.node_ops.unlink, + rmdir: MEMFS.node_ops.rmdir, + readdir: MEMFS.node_ops.readdir, + symlink: MEMFS.node_ops.symlink, + }, + stream: { + llseek: MEMFS.stream_ops.llseek, + }, + }, + file: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + }, + stream: { + llseek: MEMFS.stream_ops.llseek, + read: MEMFS.stream_ops.read, + write: MEMFS.stream_ops.write, + mmap: MEMFS.stream_ops.mmap, + msync: MEMFS.stream_ops.msync, + }, + }, + link: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + readlink: MEMFS.node_ops.readlink, + }, + stream: {}, + }, + chrdev: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + }, + stream: FS.chrdev_stream_ops, + }, + }; + var node = FS.createNode(parent, name, mode, dev); + if (FS.isDir(node.mode)) { + node.node_ops = MEMFS.ops_table.dir.node; + node.stream_ops = MEMFS.ops_table.dir.stream; + node.contents = {}; + } else if (FS.isFile(node.mode)) { + node.node_ops = MEMFS.ops_table.file.node; + node.stream_ops = MEMFS.ops_table.file.stream; + node.usedBytes = 0; + // The actual number of bytes used in the typed array, as opposed to contents.length which gives the whole capacity. + // When the byte data of the file is populated, this will point to either a typed array, or a normal JS array. Typed arrays are preferred + // for performance, and used by default. However, typed arrays are not resizable like normal JS arrays are, so there is a small disk size + // penalty involved for appending file writes that continuously grow a file similar to std::vector capacity vs used -scheme. + node.contents = null; + } else if (FS.isLink(node.mode)) { + node.node_ops = MEMFS.ops_table.link.node; + node.stream_ops = MEMFS.ops_table.link.stream; + } else if (FS.isChrdev(node.mode)) { + node.node_ops = MEMFS.ops_table.chrdev.node; + node.stream_ops = MEMFS.ops_table.chrdev.stream; + } + node.atime = node.mtime = node.ctime = Date.now(); + // add the new node to the parent + if (parent) { + parent.contents[name] = node; + parent.atime = parent.mtime = parent.ctime = node.atime; + } + return node; + }, + getFileDataAsTypedArray(node) { + if (!node.contents) return new Uint8Array(0); + if (node.contents.subarray) + return node.contents.subarray(0, node.usedBytes); + // Make sure to not return excess unused bytes. + return new Uint8Array(node.contents); + }, + expandFileStorage(node, newCapacity) { + var prevCapacity = node.contents ? node.contents.length : 0; + if (prevCapacity >= newCapacity) return; + // No need to expand, the storage was already large enough. + // Don't expand strictly to the given requested limit if it's only a very small increase, but instead geometrically grow capacity. + // For small filesizes (<1MB), perform size*2 geometric increase, but for large sizes, do a much more conservative size*1.125 increase to + // avoid overshooting the allocation cap by a very large margin. + var CAPACITY_DOUBLING_MAX = 1024 * 1024; + newCapacity = Math.max( + newCapacity, + (prevCapacity * + (prevCapacity < CAPACITY_DOUBLING_MAX ? 2 : 1.125)) >>> + 0 + ); + if (prevCapacity != 0) newCapacity = Math.max(newCapacity, 256); + // At minimum allocate 256b for each file when expanding. + var oldContents = node.contents; + node.contents = new Uint8Array(newCapacity); + // Allocate new storage. + if (node.usedBytes > 0) + node.contents.set(oldContents.subarray(0, node.usedBytes), 0); + }, + resizeFileStorage(node, newSize) { + if (node.usedBytes == newSize) return; + if (newSize == 0) { + node.contents = null; + // Fully decommit when requesting a resize to zero. + node.usedBytes = 0; + } else { + var oldContents = node.contents; + node.contents = new Uint8Array(newSize); + // Allocate new storage. + if (oldContents) { + node.contents.set( + oldContents.subarray( + 0, + Math.min(newSize, node.usedBytes) + ) + ); + } + node.usedBytes = newSize; + } + }, + node_ops: { + getattr(node) { + var attr = {}; + // device numbers reuse inode numbers. + attr.dev = FS.isChrdev(node.mode) ? node.id : 1; + attr.ino = node.id; + attr.mode = node.mode; + attr.nlink = 1; + attr.uid = 0; + attr.gid = 0; + attr.rdev = node.rdev; + if (FS.isDir(node.mode)) { + attr.size = 4096; + } else if (FS.isFile(node.mode)) { + attr.size = node.usedBytes; + } else if (FS.isLink(node.mode)) { + attr.size = node.link.length; + } else { + attr.size = 0; + } + attr.atime = new Date(node.atime); + attr.mtime = new Date(node.mtime); + attr.ctime = new Date(node.ctime); + // NOTE: In our implementation, st_blocks = Math.ceil(st_size/st_blksize), + // but this is not required by the standard. + attr.blksize = 4096; + attr.blocks = Math.ceil(attr.size / attr.blksize); + return attr; + }, + setattr(node, attr) { + for (const key of ['mode', 'atime', 'mtime', 'ctime']) { + if (attr[key] != null) { + node[key] = attr[key]; + } + } + if (attr.size !== undefined) { + MEMFS.resizeFileStorage(node, attr.size); + } + }, + lookup(parent, name) { + // This error may happen quite a bit. To avoid overhead we reuse it (and + // suffer a lack of stack info). + if (!MEMFS.doesNotExistError) { + MEMFS.doesNotExistError = new FS.ErrnoError(44); + /** @suppress {checkTypes} */ MEMFS.doesNotExistError.stack = + ''; + } + throw MEMFS.doesNotExistError; + }, + mknod(parent, name, mode, dev) { + return MEMFS.createNode(parent, name, mode, dev); + }, + rename(old_node, new_dir, new_name) { + var new_node; + try { + new_node = FS.lookupNode(new_dir, new_name); + } catch (e) {} + if (new_node) { + if (FS.isDir(old_node.mode)) { + // if we're overwriting a directory at new_name, make sure it's empty. + for (var i in new_node.contents) { + throw new FS.ErrnoError(55); + } + } + FS.hashRemoveNode(new_node); + } + // do the internal rewiring + delete old_node.parent.contents[old_node.name]; + new_dir.contents[new_name] = old_node; + old_node.name = new_name; + new_dir.ctime = + new_dir.mtime = + old_node.parent.ctime = + old_node.parent.mtime = + Date.now(); + }, + unlink(parent, name) { + delete parent.contents[name]; + parent.ctime = parent.mtime = Date.now(); + }, + rmdir(parent, name) { + var node = FS.lookupNode(parent, name); + for (var i in node.contents) { + throw new FS.ErrnoError(55); + } + delete parent.contents[name]; + parent.ctime = parent.mtime = Date.now(); + }, + readdir(node) { + return ['.', '..', ...Object.keys(node.contents)]; + }, + symlink(parent, newname, oldpath) { + var node = MEMFS.createNode(parent, newname, 511 | 40960, 0); + node.link = oldpath; + return node; + }, + readlink(node) { + if (!FS.isLink(node.mode)) { + throw new FS.ErrnoError(28); + } + return node.link; + }, + }, + stream_ops: { + read(stream, buffer, offset, length, position) { + var contents = stream.node.contents; + if (position >= stream.node.usedBytes) return 0; + var size = Math.min(stream.node.usedBytes - position, length); + if (size > 8 && contents.subarray) { + // non-trivial, and typed array + buffer.set( + contents.subarray(position, position + size), + offset + ); + } else { + for (var i = 0; i < size; i++) + buffer[offset + i] = contents[position + i]; + } + return size; + }, + write(stream, buffer, offset, length, position, canOwn) { + // If the buffer is located in main memory (HEAP), and if + // memory can grow, we can't hold on to references of the + // memory buffer, as they may get invalidated. That means we + // need to do copy its contents. + if (buffer.buffer === HEAP8.buffer) { + canOwn = false; + } + if (!length) return 0; + var node = stream.node; + node.mtime = node.ctime = Date.now(); + if ( + buffer.subarray && + (!node.contents || node.contents.subarray) + ) { + // This write is from a typed array to a typed array? + if (canOwn) { + node.contents = buffer.subarray( + offset, + offset + length + ); + node.usedBytes = length; + return length; + } else if (node.usedBytes === 0 && position === 0) { + // If this is a simple first write to an empty file, do a fast set since we don't need to care about old data. + node.contents = buffer.slice(offset, offset + length); + node.usedBytes = length; + return length; + } else if (position + length <= node.usedBytes) { + // Writing to an already allocated and used subrange of the file? + node.contents.set( + buffer.subarray(offset, offset + length), + position + ); + return length; + } + } + // Appending to an existing file and we need to reallocate, or source data did not come as a typed array. + MEMFS.expandFileStorage(node, position + length); + if (node.contents.subarray && buffer.subarray) { + // Use typed array write which is available. + node.contents.set( + buffer.subarray(offset, offset + length), + position + ); + } else { + for (var i = 0; i < length; i++) { + node.contents[position + i] = buffer[offset + i]; + } + } + node.usedBytes = Math.max(node.usedBytes, position + length); + return length; + }, + llseek(stream, offset, whence) { + var position = offset; + if (whence === 1) { + position += stream.position; + } else if (whence === 2) { + if (FS.isFile(stream.node.mode)) { + position += stream.node.usedBytes; + } + } + if (position < 0) { + throw new FS.ErrnoError(28); + } + return position; + }, + mmap(stream, length, position, prot, flags) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError(43); + } + var ptr; + var allocated; + var contents = stream.node.contents; + // Only make a new copy when MAP_PRIVATE is specified. + if ( + !(flags & 2) && + contents && + contents.buffer === HEAP8.buffer + ) { + // We can't emulate MAP_SHARED when the file is not backed by the + // buffer we're mapping to (e.g. the HEAP buffer). + allocated = false; + ptr = contents.byteOffset; + } else { + allocated = true; + ptr = mmapAlloc(length); + if (!ptr) { + throw new FS.ErrnoError(48); + } + if (contents) { + // Try to avoid unnecessary slices. + if ( + position > 0 || + position + length < contents.length + ) { + if (contents.subarray) { + contents = contents.subarray( + position, + position + length + ); + } else { + contents = Array.prototype.slice.call( + contents, + position, + position + length + ); + } + } + HEAP8.set(contents, ptr); + } + } + return { + ptr, + allocated, + }; + }, + msync(stream, buffer, offset, length, mmapFlags) { + MEMFS.stream_ops.write( + stream, + buffer, + 0, + length, + offset, + false + ); + // should we check if bytesWritten and length are the same? + return 0; + }, + }, + }; + + var FS_modeStringToFlags = (str) => { + var flagModes = { + r: 0, + 'r+': 2, + w: 512 | 64 | 1, + 'w+': 512 | 64 | 2, + a: 1024 | 64 | 1, + 'a+': 1024 | 64 | 2, + }; + var flags = flagModes[str]; + if (typeof flags == 'undefined') { + throw new Error(`Unknown file open mode: ${str}`); + } + return flags; + }; + + var FS_getMode = (canRead, canWrite) => { + var mode = 0; + if (canRead) mode |= 292 | 73; + if (canWrite) mode |= 146; + return mode; + }; + + var ERRNO_CODES = { + EPERM: 63, + ENOENT: 44, + ESRCH: 71, + EINTR: 27, + EIO: 29, + ENXIO: 60, + E2BIG: 1, + ENOEXEC: 45, + EBADF: 8, + ECHILD: 12, + EAGAIN: 6, + EWOULDBLOCK: 6, + ENOMEM: 48, + EACCES: 2, + EFAULT: 21, + ENOTBLK: 105, + EBUSY: 10, + EEXIST: 20, + EXDEV: 75, + ENODEV: 43, + ENOTDIR: 54, + EISDIR: 31, + EINVAL: 28, + ENFILE: 41, + EMFILE: 33, + ENOTTY: 59, + ETXTBSY: 74, + EFBIG: 22, + ENOSPC: 51, + ESPIPE: 70, + EROFS: 69, + EMLINK: 34, + EPIPE: 64, + EDOM: 18, + ERANGE: 68, + ENOMSG: 49, + EIDRM: 24, + ECHRNG: 106, + EL2NSYNC: 156, + EL3HLT: 107, + EL3RST: 108, + ELNRNG: 109, + EUNATCH: 110, + ENOCSI: 111, + EL2HLT: 112, + EDEADLK: 16, + ENOLCK: 46, + EBADE: 113, + EBADR: 114, + EXFULL: 115, + ENOANO: 104, + EBADRQC: 103, + EBADSLT: 102, + EDEADLOCK: 16, + EBFONT: 101, + ENOSTR: 100, + ENODATA: 116, + ETIME: 117, + ENOSR: 118, + ENONET: 119, + ENOPKG: 120, + EREMOTE: 121, + ENOLINK: 47, + EADV: 122, + ESRMNT: 123, + ECOMM: 124, + EPROTO: 65, + EMULTIHOP: 36, + EDOTDOT: 125, + EBADMSG: 9, + ENOTUNIQ: 126, + EBADFD: 127, + EREMCHG: 128, + ELIBACC: 129, + ELIBBAD: 130, + ELIBSCN: 131, + ELIBMAX: 132, + ELIBEXEC: 133, + ENOSYS: 52, + ENOTEMPTY: 55, + ENAMETOOLONG: 37, + ELOOP: 32, + EOPNOTSUPP: 138, + EPFNOSUPPORT: 139, + ECONNRESET: 15, + ENOBUFS: 42, + EAFNOSUPPORT: 5, + EPROTOTYPE: 67, + ENOTSOCK: 57, + ENOPROTOOPT: 50, + ESHUTDOWN: 140, + ECONNREFUSED: 14, + EADDRINUSE: 3, + ECONNABORTED: 13, + ENETUNREACH: 40, + ENETDOWN: 38, + ETIMEDOUT: 73, + EHOSTDOWN: 142, + EHOSTUNREACH: 23, + EINPROGRESS: 26, + EALREADY: 7, + EDESTADDRREQ: 17, + EMSGSIZE: 35, + EPROTONOSUPPORT: 66, + ESOCKTNOSUPPORT: 137, + EADDRNOTAVAIL: 4, + ENETRESET: 39, + EISCONN: 30, + ENOTCONN: 53, + ETOOMANYREFS: 141, + EUSERS: 136, + EDQUOT: 19, + ESTALE: 72, + ENOTSUP: 138, + ENOMEDIUM: 148, + EILSEQ: 25, + EOVERFLOW: 61, + ECANCELED: 11, + ENOTRECOVERABLE: 56, + EOWNERDEAD: 62, + ESTRPIPE: 135, + }; + + var NODEFS = { + isWindows: false, + staticInit() { + NODEFS.isWindows = !!process.platform.match(/^win/); + var flags = process.binding('constants')['fs']; + NODEFS.flagsForNodeMap = { + 1024: flags['O_APPEND'], + 64: flags['O_CREAT'], + 128: flags['O_EXCL'], + 256: flags['O_NOCTTY'], + 0: flags['O_RDONLY'], + 2: flags['O_RDWR'], + 4096: flags['O_SYNC'], + 512: flags['O_TRUNC'], + 1: flags['O_WRONLY'], + 131072: flags['O_NOFOLLOW'], + }; + }, + convertNodeCode(e) { + var code = e.code; + return ERRNO_CODES[code]; + }, + tryFSOperation(f) { + try { + return f(); + } catch (e) { + if (!e.code) throw e; + // node under windows can return code 'UNKNOWN' here: + // https://github.com/emscripten-core/emscripten/issues/15468 + if (e.code === 'UNKNOWN') throw new FS.ErrnoError(28); + throw new FS.ErrnoError(NODEFS.convertNodeCode(e)); + } + }, + mount(mount) { + return NODEFS.createNode( + null, + '/', + NODEFS.getMode(mount.opts.root), + 0 + ); + }, + createNode(parent, name, mode, dev) { + if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) { + throw new FS.ErrnoError(28); + } + var node = FS.createNode(parent, name, mode); + node.node_ops = NODEFS.node_ops; + node.stream_ops = NODEFS.stream_ops; + return node; + }, + getMode(path) { + return NODEFS.tryFSOperation(() => { + var mode = fs.lstatSync(path).mode; + if (NODEFS.isWindows) { + // Windows does not report the 'x' permission bit, so propagate read + // bits to execute bits. + mode |= (mode & 292) >> 2; + } + return mode; + }); + }, + realPath(node) { + var parts = []; + while (node.parent !== node) { + parts.push(node.name); + node = node.parent; + } + parts.push(node.mount.opts.root); + parts.reverse(); + return PATH.join(...parts); + }, + flagsForNode(flags) { + flags &= ~2097152; + // Ignore this flag from musl, otherwise node.js fails to open the file. + flags &= ~2048; + // Ignore this flag from musl, otherwise node.js fails to open the file. + flags &= ~32768; + // Ignore this flag from musl, otherwise node.js fails to open the file. + flags &= ~524288; + // Some applications may pass it; it makes no sense for a single process. + flags &= ~65536; + // Node.js doesn't need this passed in, it errors. + var newFlags = 0; + for (var k in NODEFS.flagsForNodeMap) { + if (flags & k) { + newFlags |= NODEFS.flagsForNodeMap[k]; + flags ^= k; + } + } + if (flags) { + throw new FS.ErrnoError(28); + } + return newFlags; + }, + getattr(func, node) { + var stat = NODEFS.tryFSOperation(func); + if (NODEFS.isWindows) { + // node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake + // them with default blksize of 4096. + // See http://support.microsoft.com/kb/140365 + if (!stat.blksize) { + stat.blksize = 4096; + } + if (!stat.blocks) { + stat.blocks = + ((stat.size + stat.blksize - 1) / stat.blksize) | 0; + } + // Windows does not report the 'x' permission bit, so propagate read + // bits to execute bits. + stat.mode |= (stat.mode & 292) >> 2; + } + return { + dev: stat.dev, + ino: node.id, + mode: stat.mode, + nlink: stat.nlink, + uid: stat.uid, + gid: stat.gid, + rdev: stat.rdev, + size: stat.size, + atime: stat.atime, + mtime: stat.mtime, + ctime: stat.ctime, + blksize: stat.blksize, + blocks: stat.blocks, + }; + }, + setattr(arg, node, attr, chmod, utimes, truncate, stat) { + NODEFS.tryFSOperation(() => { + if (attr.mode !== undefined) { + var mode = attr.mode; + if (NODEFS.isWindows) { + // Windows only supports S_IREAD / S_IWRITE (S_IRUSR / S_IWUSR) + // https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/chmod-wchmod + mode &= 384; + } + chmod(arg, mode); + // update the common node structure mode as well + node.mode = attr.mode; + } + if (typeof (attr.atime ?? attr.mtime) === 'number') { + // Unfortunately, we have to stat the current value if we don't want + // to change it. On top of that, since the times don't round trip + // this will only keep the value nearly unchanged not exactly + // unchanged. See: + // https://github.com/nodejs/node/issues/56492 + var atime = new Date(attr.atime ?? stat(arg).atime); + var mtime = new Date(attr.mtime ?? stat(arg).mtime); + utimes(arg, atime, mtime); + } + if (attr.size !== undefined) { + truncate(arg, attr.size); + } + }); + }, + node_ops: { + getattr(node) { + var path = NODEFS.realPath(node); + return NODEFS.getattr(() => fs.lstatSync(path), node); + }, + setattr(node, attr) { + var path = NODEFS.realPath(node); + if (attr.mode != null && attr.dontFollow) { + throw new FS.ErrnoError(52); + } + NODEFS.setattr( + path, + node, + attr, + fs.chmodSync, + fs.utimesSync, + fs.truncateSync, + fs.lstatSync + ); + }, + lookup(parent, name) { + var path = PATH.join2(NODEFS.realPath(parent), name); + var mode = NODEFS.getMode(path); + return NODEFS.createNode(parent, name, mode); + }, + mknod(parent, name, mode, dev) { + var node = NODEFS.createNode(parent, name, mode, dev); + // create the backing node for this in the fs root as well + var path = NODEFS.realPath(node); + NODEFS.tryFSOperation(() => { + if (FS.isDir(node.mode)) { + fs.mkdirSync(path, node.mode); + } else { + fs.writeFileSync(path, '', { + mode: node.mode, + }); + } + }); + return node; + }, + rename(oldNode, newDir, newName) { + var oldPath = NODEFS.realPath(oldNode); + var newPath = PATH.join2(NODEFS.realPath(newDir), newName); + try { + FS.unlink(newPath); + } catch (e) {} + NODEFS.tryFSOperation(() => fs.renameSync(oldPath, newPath)); + oldNode.name = newName; + }, + unlink(parent, name) { + var path = PATH.join2(NODEFS.realPath(parent), name); + NODEFS.tryFSOperation(() => fs.unlinkSync(path)); + }, + rmdir(parent, name) { + var path = PATH.join2(NODEFS.realPath(parent), name); + NODEFS.tryFSOperation(() => fs.rmdirSync(path)); + }, + readdir(node) { + var path = NODEFS.realPath(node); + return NODEFS.tryFSOperation(() => fs.readdirSync(path)); + }, + symlink(parent, newName, oldPath) { + var newPath = PATH.join2(NODEFS.realPath(parent), newName); + NODEFS.tryFSOperation(() => fs.symlinkSync(oldPath, newPath)); + }, + readlink(node) { + var path = NODEFS.realPath(node); + return NODEFS.tryFSOperation(() => fs.readlinkSync(path)); + }, + statfs(path) { + var stats = NODEFS.tryFSOperation(() => fs.statfsSync(path)); + // Node.js doesn't provide frsize (fragment size). Set it to bsize (block size) + // as they're often the same in many file systems. May not be accurate for all. + stats.frsize = stats.bsize; + return stats; + }, + }, + stream_ops: { + getattr(stream) { + return NODEFS.getattr( + () => fs.fstatSync(stream.nfd), + stream.node + ); + }, + setattr(stream, attr) { + NODEFS.setattr( + stream.nfd, + stream.node, + attr, + fs.fchmodSync, + fs.futimesSync, + fs.ftruncateSync, + fs.fstatSync + ); + }, + open(stream) { + var path = NODEFS.realPath(stream.node); + NODEFS.tryFSOperation(() => { + stream.shared.refcount = 1; + stream.nfd = fs.openSync( + path, + NODEFS.flagsForNode(stream.flags) + ); + }); + }, + close(stream) { + NODEFS.tryFSOperation(() => { + if (stream.nfd && --stream.shared.refcount === 0) { + fs.closeSync(stream.nfd); + } + }); + }, + dup(stream) { + stream.shared.refcount++; + }, + read(stream, buffer, offset, length, position) { + return NODEFS.tryFSOperation(() => + fs.readSync( + stream.nfd, + new Int8Array(buffer.buffer, offset, length), + 0, + length, + position + ) + ); + }, + write(stream, buffer, offset, length, position) { + return NODEFS.tryFSOperation(() => + fs.writeSync( + stream.nfd, + new Int8Array(buffer.buffer, offset, length), + 0, + length, + position + ) + ); + }, + llseek(stream, offset, whence) { + var position = offset; + if (whence === 1) { + position += stream.position; + } else if (whence === 2) { + if (FS.isFile(stream.node.mode)) { + NODEFS.tryFSOperation(() => { + var stat = fs.fstatSync(stream.nfd); + position += stat.size; + }); + } + } + if (position < 0) { + throw new FS.ErrnoError(28); + } + return position; + }, + mmap(stream, length, position, prot, flags) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError(43); + } + var ptr = mmapAlloc(length); + NODEFS.stream_ops.read(stream, HEAP8, ptr, length, position); + return { + ptr, + allocated: true, + }; + }, + msync(stream, buffer, offset, length, mmapFlags) { + NODEFS.stream_ops.write( + stream, + buffer, + 0, + length, + offset, + false + ); + // should we check if bytesWritten and length are the same? + return 0; + }, + }, + }; + + var PROXYFS = { + mount(mount) { + return PROXYFS.createNode( + null, + '/', + mount.opts.fs.lstat(mount.opts.root).mode, + 0 + ); + }, + createNode(parent, name, mode, dev) { + if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) { + throw new FS.ErrnoError(ERRNO_CODES.EINVAL); + } + var node = FS.createNode(parent, name, mode); + node.node_ops = PROXYFS.node_ops; + node.stream_ops = PROXYFS.stream_ops; + return node; + }, + realPath(node) { + var parts = []; + while (node.parent !== node) { + parts.push(node.name); + node = node.parent; + } + parts.push(node.mount.opts.root); + parts.reverse(); + return PATH.join(...parts); + }, + node_ops: { + getattr(node) { + var path = PROXYFS.realPath(node); + var stat; + try { + stat = node.mount.opts.fs.lstat(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + return { + dev: stat.dev, + ino: stat.ino, + mode: stat.mode, + nlink: stat.nlink, + uid: stat.uid, + gid: stat.gid, + rdev: stat.rdev, + size: stat.size, + atime: stat.atime, + mtime: stat.mtime, + ctime: stat.ctime, + blksize: stat.blksize, + blocks: stat.blocks, + }; + }, + setattr(node, attr) { + var path = PROXYFS.realPath(node); + try { + if (attr.mode !== undefined) { + node.mount.opts.fs.chmod(path, attr.mode); + // update the common node structure mode as well + node.mode = attr.mode; + } + if (attr.atime || attr.mtime) { + var atime = new Date(attr.atime || attr.mtime); + var mtime = new Date(attr.mtime || attr.atime); + node.mount.opts.fs.utime(path, atime, mtime); + } + if (attr.size !== undefined) { + node.mount.opts.fs.truncate(path, attr.size); + } + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + lookup(parent, name) { + try { + var path = PATH.join2(PROXYFS.realPath(parent), name); + var mode = parent.mount.opts.fs.lstat(path).mode; + var node = PROXYFS.createNode(parent, name, mode); + return node; + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + mknod(parent, name, mode, dev) { + var node = PROXYFS.createNode(parent, name, mode, dev); + // create the backing node for this in the fs root as well + var path = PROXYFS.realPath(node); + try { + if (FS.isDir(node.mode)) { + node.mount.opts.fs.mkdir(path, node.mode); + } else { + node.mount.opts.fs.writeFile(path, '', { + mode: node.mode, + }); + } + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + return node; + }, + rename(oldNode, newDir, newName) { + var oldPath = PROXYFS.realPath(oldNode); + var newPath = PATH.join2(PROXYFS.realPath(newDir), newName); + try { + oldNode.mount.opts.fs.rename(oldPath, newPath); + oldNode.name = newName; + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + unlink(parent, name) { + var path = PATH.join2(PROXYFS.realPath(parent), name); + try { + parent.mount.opts.fs.unlink(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + rmdir(parent, name) { + var path = PATH.join2(PROXYFS.realPath(parent), name); + try { + parent.mount.opts.fs.rmdir(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + readdir(node) { + var path = PROXYFS.realPath(node); + try { + return node.mount.opts.fs.readdir(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + symlink(parent, newName, oldPath) { + var newPath = PATH.join2(PROXYFS.realPath(parent), newName); + try { + parent.mount.opts.fs.symlink(oldPath, newPath); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + readlink(node) { + var path = PROXYFS.realPath(node); + try { + return node.mount.opts.fs.readlink(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + }, + stream_ops: { + open(stream) { + var path = PROXYFS.realPath(stream.node); + try { + stream.nfd = stream.node.mount.opts.fs.open( + path, + stream.flags + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + close(stream) { + try { + stream.node.mount.opts.fs.close(stream.nfd); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + read(stream, buffer, offset, length, position) { + try { + return stream.node.mount.opts.fs.read( + stream.nfd, + buffer, + offset, + length, + position + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + write(stream, buffer, offset, length, position) { + try { + return stream.node.mount.opts.fs.write( + stream.nfd, + buffer, + offset, + length, + position + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + llseek(stream, offset, whence) { + var position = offset; + if (whence === 1) { + position += stream.position; + } else if (whence === 2) { + if (FS.isFile(stream.node.mode)) { + try { + var stat = stream.node.node_ops.getattr( + stream.node + ); + position += stat.size; + } catch (e) { + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + } + } + if (position < 0) { + throw new FS.ErrnoError(ERRNO_CODES.EINVAL); + } + return position; + }, + }, + }; + + var FS_createDataFile = (...args) => FS.createDataFile(...args); + + var getUniqueRunDependency = (id) => id; + + var FS_handledByPreloadPlugin = async (byteArray, fullname) => { + // Ensure plugins are ready. + if (typeof Browser != 'undefined') Browser.init(); + for (var plugin of preloadPlugins) { + if (plugin['canHandle'](fullname)) { + return plugin['handle'](byteArray, fullname); + } + } + // In no plugin handled this file then return the original/unmodified + // byteArray. + return byteArray; + }; + + var FS_preloadFile = async ( + parent, + name, + url, + canRead, + canWrite, + dontCreateFile, + canOwn, + preFinish + ) => { + // TODO we should allow people to just pass in a complete filename instead + // of parent and name being that we just join them anyways + var fullname = name + ? PATH_FS.resolve(PATH.join2(parent, name)) + : parent; + var dep = getUniqueRunDependency(`cp ${fullname}`); + // might have several active requests for the same fullname + addRunDependency(dep); + try { + var byteArray = url; + if (typeof url == 'string') { + byteArray = await asyncLoad(url); + } + byteArray = await FS_handledByPreloadPlugin(byteArray, fullname); + preFinish?.(); + if (!dontCreateFile) { + FS_createDataFile( + parent, + name, + byteArray, + canRead, + canWrite, + canOwn + ); + } + } finally { + removeRunDependency(dep); + } + }; + + var FS_createPreloadedFile = ( + parent, + name, + url, + canRead, + canWrite, + onload, + onerror, + dontCreateFile, + canOwn, + preFinish + ) => { + FS_preloadFile( + parent, + name, + url, + canRead, + canWrite, + dontCreateFile, + canOwn, + preFinish + ) + .then(onload) + .catch(onerror); + }; + + var FS = { + root: null, + mounts: [], + devices: {}, + streams: [], + nextInode: 1, + nameTable: null, + currentPath: '/', + initialized: false, + ignorePermissions: true, + filesystems: null, + syncFSRequests: 0, + readFiles: {}, + ErrnoError: class { + name = 'ErrnoError'; + // We set the `name` property to be able to identify `FS.ErrnoError` + // - the `name` is a standard ECMA-262 property of error objects. Kind of good to have it anyway. + // - when using PROXYFS, an error can come from an underlying FS + // as different FS objects have their own FS.ErrnoError each, + // the test `err instanceof FS.ErrnoError` won't detect an error coming from another filesystem, causing bugs. + // we'll use the reliable test `err.name == "ErrnoError"` instead + constructor(errno) { + this.errno = errno; + } + }, + FSStream: class { + shared = {}; + get object() { + return this.node; + } + set object(val) { + this.node = val; + } + get isRead() { + return (this.flags & 2097155) !== 1; + } + get isWrite() { + return (this.flags & 2097155) !== 0; + } + get isAppend() { + return this.flags & 1024; + } + get flags() { + return this.shared.flags; + } + set flags(val) { + this.shared.flags = val; + } + get position() { + return this.shared.position; + } + set position(val) { + this.shared.position = val; + } + }, + FSNode: class { + node_ops = {}; + stream_ops = {}; + readMode = 292 | 73; + writeMode = 146; + mounted = null; + constructor(parent, name, mode, rdev) { + if (!parent) { + parent = this; + } + this.parent = parent; + this.mount = parent.mount; + this.id = FS.nextInode++; + this.name = name; + this.mode = mode; + this.rdev = rdev; + this.atime = this.mtime = this.ctime = Date.now(); + } + get read() { + return (this.mode & this.readMode) === this.readMode; + } + set read(val) { + val + ? (this.mode |= this.readMode) + : (this.mode &= ~this.readMode); + } + get write() { + return (this.mode & this.writeMode) === this.writeMode; + } + set write(val) { + val + ? (this.mode |= this.writeMode) + : (this.mode &= ~this.writeMode); + } + get isFolder() { + return FS.isDir(this.mode); + } + get isDevice() { + return FS.isChrdev(this.mode); + } + }, + lookupPath(path, opts = {}) { + if (!path) { + throw new FS.ErrnoError(44); + } + opts.follow_mount ??= true; + if (!PATH.isAbs(path)) { + path = FS.cwd() + '/' + path; + } + // limit max consecutive symlinks to 40 (SYMLOOP_MAX). + linkloop: for (var nlinks = 0; nlinks < 40; nlinks++) { + // split the absolute path + var parts = path.split('/').filter((p) => !!p); + // start at the root + var current = FS.root; + var current_path = '/'; + for (var i = 0; i < parts.length; i++) { + var islast = i === parts.length - 1; + if (islast && opts.parent) { + // stop resolving + break; + } + if (parts[i] === '.') { + continue; + } + if (parts[i] === '..') { + current_path = PATH.dirname(current_path); + if (FS.isRoot(current)) { + path = + current_path + + '/' + + parts.slice(i + 1).join('/'); + // We're making progress here, don't let many consecutive ..'s + // lead to ELOOP + nlinks--; + continue linkloop; + } else { + current = current.parent; + } + continue; + } + current_path = PATH.join2(current_path, parts[i]); + try { + current = FS.lookupNode(current, parts[i]); + } catch (e) { + // if noent_okay is true, suppress a ENOENT in the last component + // and return an object with an undefined node. This is needed for + // resolving symlinks in the path when creating a file. + if (e?.errno === 44 && islast && opts.noent_okay) { + return { + path: current_path, + }; + } + throw e; + } + // jump to the mount's root node if this is a mountpoint + if ( + FS.isMountpoint(current) && + (!islast || opts.follow_mount) + ) { + current = current.mounted.root; + } + // by default, lookupPath will not follow a symlink if it is the final path component. + // setting opts.follow = true will override this behavior. + if (FS.isLink(current.mode) && (!islast || opts.follow)) { + if (!current.node_ops.readlink) { + throw new FS.ErrnoError(52); + } + var link = current.node_ops.readlink(current); + if (!PATH.isAbs(link)) { + link = PATH.dirname(current_path) + '/' + link; + } + path = link + '/' + parts.slice(i + 1).join('/'); + continue linkloop; + } + } + return { + path: current_path, + node: current, + }; + } + throw new FS.ErrnoError(32); + }, + getPath(node) { + var path; + while (true) { + if (FS.isRoot(node)) { + var mount = node.mount.mountpoint; + if (!path) return mount; + return mount[mount.length - 1] !== '/' + ? `${mount}/${path}` + : mount + path; + } + path = path ? `${node.name}/${path}` : node.name; + node = node.parent; + } + }, + hashName(parentid, name) { + var hash = 0; + for (var i = 0; i < name.length; i++) { + hash = ((hash << 5) - hash + name.charCodeAt(i)) | 0; + } + return ((parentid + hash) >>> 0) % FS.nameTable.length; + }, + hashAddNode(node) { + var hash = FS.hashName(node.parent.id, node.name); + node.name_next = FS.nameTable[hash]; + FS.nameTable[hash] = node; + }, + hashRemoveNode(node) { + var hash = FS.hashName(node.parent.id, node.name); + if (FS.nameTable[hash] === node) { + FS.nameTable[hash] = node.name_next; + } else { + var current = FS.nameTable[hash]; + while (current) { + if (current.name_next === node) { + current.name_next = node.name_next; + break; + } + current = current.name_next; + } + } + }, + lookupNode(parent, name) { + var errCode = FS.mayLookup(parent); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + var hash = FS.hashName(parent.id, name); + for (var node = FS.nameTable[hash]; node; node = node.name_next) { + var nodeName = node.name; + if (node.parent.id === parent.id && nodeName === name) { + return node; + } + } + // if we failed to find it in the cache, call into the VFS + return FS.lookup(parent, name); + }, + createNode(parent, name, mode, rdev) { + var node = new FS.FSNode(parent, name, mode, rdev); + FS.hashAddNode(node); + return node; + }, + destroyNode(node) { + FS.hashRemoveNode(node); + }, + isRoot(node) { + return node === node.parent; + }, + isMountpoint(node) { + return !!node.mounted; + }, + isFile(mode) { + return (mode & 61440) === 32768; + }, + isDir(mode) { + return (mode & 61440) === 16384; + }, + isLink(mode) { + return (mode & 61440) === 40960; + }, + isChrdev(mode) { + return (mode & 61440) === 8192; + }, + isBlkdev(mode) { + return (mode & 61440) === 24576; + }, + isFIFO(mode) { + return (mode & 61440) === 4096; + }, + isSocket(mode) { + return (mode & 49152) === 49152; + }, + flagsToPermissionString(flag) { + var perms = ['r', 'w', 'rw'][flag & 3]; + if (flag & 512) { + perms += 'w'; + } + return perms; + }, + nodePermissions(node, perms) { + if (FS.ignorePermissions) { + return 0; + } + // return 0 if any user, group or owner bits are set. + if (perms.includes('r') && !(node.mode & 292)) { + return 2; + } else if (perms.includes('w') && !(node.mode & 146)) { + return 2; + } else if (perms.includes('x') && !(node.mode & 73)) { + return 2; + } + return 0; + }, + mayLookup(dir) { + if (!FS.isDir(dir.mode)) return 54; + var errCode = FS.nodePermissions(dir, 'x'); + if (errCode) return errCode; + if (!dir.node_ops.lookup) return 2; + return 0; + }, + mayCreate(dir, name) { + if (!FS.isDir(dir.mode)) { + return 54; + } + try { + var node = FS.lookupNode(dir, name); + return 20; + } catch (e) {} + return FS.nodePermissions(dir, 'wx'); + }, + mayDelete(dir, name, isdir) { + var node; + try { + node = FS.lookupNode(dir, name); + } catch (e) { + return e.errno; + } + var errCode = FS.nodePermissions(dir, 'wx'); + if (errCode) { + return errCode; + } + if (isdir) { + if (!FS.isDir(node.mode)) { + return 54; + } + if (FS.isRoot(node) || FS.getPath(node) === FS.cwd()) { + return 10; + } + } else { + if (FS.isDir(node.mode)) { + return 31; + } + } + return 0; + }, + mayOpen(node, flags) { + if (!node) { + return 44; + } + if (FS.isLink(node.mode)) { + return 32; + } else if (FS.isDir(node.mode)) { + if ( + FS.flagsToPermissionString(flags) !== 'r' || + flags & (512 | 64) + ) { + // TODO: check for O_SEARCH? (== search for dir only) + return 31; + } + } + return FS.nodePermissions(node, FS.flagsToPermissionString(flags)); + }, + checkOpExists(op, err) { + if (!op) { + throw new FS.ErrnoError(err); + } + return op; + }, + MAX_OPEN_FDS: 4096, + nextfd() { + for (var fd = 0; fd <= FS.MAX_OPEN_FDS; fd++) { + if (!FS.streams[fd]) { + return fd; + } + } + throw new FS.ErrnoError(33); + }, + getStreamChecked(fd) { + var stream = FS.getStream(fd); + if (!stream) { + throw new FS.ErrnoError(8); + } + return stream; + }, + getStream: (fd) => FS.streams[fd], + createStream(stream, fd = -1) { + // clone it, so we can return an instance of FSStream + stream = Object.assign(new FS.FSStream(), stream); + if (fd == -1) { + fd = FS.nextfd(); + } + stream.fd = fd; + FS.streams[fd] = stream; + return stream; + }, + closeStream(fd) { + FS.streams[fd] = null; + }, + dupStream(origStream, fd = -1) { + var stream = FS.createStream(origStream, fd); + stream.stream_ops?.dup?.(stream); + return stream; + }, + doSetAttr(stream, node, attr) { + var setattr = stream?.stream_ops.setattr; + var arg = setattr ? stream : node; + setattr ??= node.node_ops.setattr; + FS.checkOpExists(setattr, 63); + setattr(arg, attr); + }, + chrdev_stream_ops: { + open(stream) { + var device = FS.getDevice(stream.node.rdev); + // override node's stream ops with the device's + stream.stream_ops = device.stream_ops; + // forward the open call + stream.stream_ops.open?.(stream); + }, + llseek() { + throw new FS.ErrnoError(70); + }, + }, + major: (dev) => dev >> 8, + minor: (dev) => dev & 255, + makedev: (ma, mi) => (ma << 8) | mi, + registerDevice(dev, ops) { + FS.devices[dev] = { + stream_ops: ops, + }; + }, + getDevice: (dev) => FS.devices[dev], + getMounts(mount) { + var mounts = []; + var check = [mount]; + while (check.length) { + var m = check.pop(); + mounts.push(m); + check.push(...m.mounts); + } + return mounts; + }, + syncfs(populate, callback) { + if (typeof populate == 'function') { + callback = populate; + populate = false; + } + FS.syncFSRequests++; + if (FS.syncFSRequests > 1) { + err( + `warning: ${FS.syncFSRequests} FS.syncfs operations in flight at once, probably just doing extra work` + ); + } + var mounts = FS.getMounts(FS.root.mount); + var completed = 0; + function doCallback(errCode) { + FS.syncFSRequests--; + return callback(errCode); + } + function done(errCode) { + if (errCode) { + if (!done.errored) { + done.errored = true; + return doCallback(errCode); + } + return; + } + if (++completed >= mounts.length) { + doCallback(null); + } + } + // sync all mounts + for (var mount of mounts) { + if (mount.type.syncfs) { + mount.type.syncfs(mount, populate, done); + } else { + done(null); + } + } + }, + mount(type, opts, mountpoint) { + var root = mountpoint === '/'; + var pseudo = !mountpoint; + var node; + if (root && FS.root) { + throw new FS.ErrnoError(10); + } else if (!root && !pseudo) { + var lookup = FS.lookupPath(mountpoint, { + follow_mount: false, + }); + mountpoint = lookup.path; + // use the absolute path + node = lookup.node; + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + } + var mount = { + type, + opts, + mountpoint, + mounts: [], + }; + // create a root node for the fs + var mountRoot = type.mount(mount); + mountRoot.mount = mount; + mount.root = mountRoot; + if (root) { + FS.root = mountRoot; + } else if (node) { + // set as a mountpoint + node.mounted = mount; + // add the new mount to the current mount's children + if (node.mount) { + node.mount.mounts.push(mount); + } + } + return mountRoot; + }, + unmount(mountpoint) { + var lookup = FS.lookupPath(mountpoint, { + follow_mount: false, + }); + if (!FS.isMountpoint(lookup.node)) { + throw new FS.ErrnoError(28); + } + // destroy the nodes for this mount, and all its child mounts + var node = lookup.node; + var mount = node.mounted; + var mounts = FS.getMounts(mount); + for (var [hash, current] of Object.entries(FS.nameTable)) { + while (current) { + var next = current.name_next; + if (mounts.includes(current.mount)) { + FS.destroyNode(current); + } + current = next; + } + } + // no longer a mountpoint + node.mounted = null; + // remove this mount from the child mounts + var idx = node.mount.mounts.indexOf(mount); + node.mount.mounts.splice(idx, 1); + }, + lookup(parent, name) { + return parent.node_ops.lookup(parent, name); + }, + mknod(path, mode, dev) { + var lookup = FS.lookupPath(path, { + parent: true, + }); + var parent = lookup.node; + var name = PATH.basename(path); + if (!name) { + throw new FS.ErrnoError(28); + } + if (name === '.' || name === '..') { + throw new FS.ErrnoError(20); + } + var errCode = FS.mayCreate(parent, name); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.mknod) { + throw new FS.ErrnoError(63); + } + return parent.node_ops.mknod(parent, name, mode, dev); + }, + statfs(path) { + return FS.statfsNode( + FS.lookupPath(path, { + follow: true, + }).node + ); + }, + statfsStream(stream) { + // We keep a separate statfsStream function because noderawfs overrides + // it. In noderawfs, stream.node is sometimes null. Instead, we need to + // look at stream.path. + return FS.statfsNode(stream.node); + }, + statfsNode(node) { + // NOTE: None of the defaults here are true. We're just returning safe and + // sane values. Currently nodefs and rawfs replace these defaults, + // other file systems leave them alone. + var rtn = { + bsize: 4096, + frsize: 4096, + blocks: 1e6, + bfree: 5e5, + bavail: 5e5, + files: FS.nextInode, + ffree: FS.nextInode - 1, + fsid: 42, + flags: 2, + namelen: 255, + }; + if (node.node_ops.statfs) { + Object.assign(rtn, node.node_ops.statfs(node.mount.opts.root)); + } + return rtn; + }, + create(path, mode = 438) { + mode &= 4095; + mode |= 32768; + return FS.mknod(path, mode, 0); + }, + mkdir(path, mode = 511) { + mode &= 511 | 512; + mode |= 16384; + return FS.mknod(path, mode, 0); + }, + mkdirTree(path, mode) { + var dirs = path.split('/'); + var d = ''; + for (var dir of dirs) { + if (!dir) continue; + if (d || PATH.isAbs(path)) d += '/'; + d += dir; + try { + FS.mkdir(d, mode); + } catch (e) { + if (e.errno != 20) throw e; + } + } + }, + mkdev(path, mode, dev) { + if (typeof dev == 'undefined') { + dev = mode; + mode = 438; + } + mode |= 8192; + return FS.mknod(path, mode, dev); + }, + symlink(oldpath, newpath) { + if (!PATH_FS.resolve(oldpath)) { + throw new FS.ErrnoError(44); + } + var lookup = FS.lookupPath(newpath, { + parent: true, + }); + var parent = lookup.node; + if (!parent) { + throw new FS.ErrnoError(44); + } + var newname = PATH.basename(newpath); + var errCode = FS.mayCreate(parent, newname); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.symlink) { + throw new FS.ErrnoError(63); + } + return parent.node_ops.symlink(parent, newname, oldpath); + }, + rename(old_path, new_path) { + var old_dirname = PATH.dirname(old_path); + var new_dirname = PATH.dirname(new_path); + var old_name = PATH.basename(old_path); + var new_name = PATH.basename(new_path); + // parents must exist + var lookup, old_dir, new_dir; + // let the errors from non existent directories percolate up + lookup = FS.lookupPath(old_path, { + parent: true, + }); + old_dir = lookup.node; + lookup = FS.lookupPath(new_path, { + parent: true, + }); + new_dir = lookup.node; + if (!old_dir || !new_dir) throw new FS.ErrnoError(44); + // need to be part of the same mount + if (old_dir.mount !== new_dir.mount) { + throw new FS.ErrnoError(75); + } + // source must exist + var old_node = FS.lookupNode(old_dir, old_name); + // old path should not be an ancestor of the new path + var relative = PATH_FS.relative(old_path, new_dirname); + if (relative.charAt(0) !== '.') { + throw new FS.ErrnoError(28); + } + // new path should not be an ancestor of the old path + relative = PATH_FS.relative(new_path, old_dirname); + if (relative.charAt(0) !== '.') { + throw new FS.ErrnoError(55); + } + // see if the new path already exists + var new_node; + try { + new_node = FS.lookupNode(new_dir, new_name); + } catch (e) {} + // early out if nothing needs to change + if (old_node === new_node) { + return; + } + // we'll need to delete the old entry + var isdir = FS.isDir(old_node.mode); + var errCode = FS.mayDelete(old_dir, old_name, isdir); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + // need delete permissions if we'll be overwriting. + // need create permissions if new doesn't already exist. + errCode = new_node + ? FS.mayDelete(new_dir, new_name, isdir) + : FS.mayCreate(new_dir, new_name); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!old_dir.node_ops.rename) { + throw new FS.ErrnoError(63); + } + if ( + FS.isMountpoint(old_node) || + (new_node && FS.isMountpoint(new_node)) + ) { + throw new FS.ErrnoError(10); + } + // if we are going to change the parent, check write permissions + if (new_dir !== old_dir) { + errCode = FS.nodePermissions(old_dir, 'w'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + } + // remove the node from the lookup hash + FS.hashRemoveNode(old_node); + // do the underlying fs rename + try { + old_dir.node_ops.rename(old_node, new_dir, new_name); + // update old node (we do this here to avoid each backend + // needing to) + old_node.parent = new_dir; + } catch (e) { + throw e; + } finally { + // add the node back to the hash (in case node_ops.rename + // changed its name) + FS.hashAddNode(old_node); + } + }, + rmdir(path) { + var lookup = FS.lookupPath(path, { + parent: true, + }); + var parent = lookup.node; + var name = PATH.basename(path); + var node = FS.lookupNode(parent, name); + var errCode = FS.mayDelete(parent, name, true); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.rmdir) { + throw new FS.ErrnoError(63); + } + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + parent.node_ops.rmdir(parent, name); + FS.destroyNode(node); + }, + readdir(path) { + var lookup = FS.lookupPath(path, { + follow: true, + }); + var node = lookup.node; + var readdir = FS.checkOpExists(node.node_ops.readdir, 54); + return readdir(node); + }, + unlink(path) { + var lookup = FS.lookupPath(path, { + parent: true, + }); + var parent = lookup.node; + if (!parent) { + throw new FS.ErrnoError(44); + } + var name = PATH.basename(path); + var node = FS.lookupNode(parent, name); + var errCode = FS.mayDelete(parent, name, false); + if (errCode) { + // According to POSIX, we should map EISDIR to EPERM, but + // we instead do what Linux does (and we must, as we use + // the musl linux libc). + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.unlink) { + throw new FS.ErrnoError(63); + } + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + parent.node_ops.unlink(parent, name); + FS.destroyNode(node); + }, + readlink(path) { + var lookup = FS.lookupPath(path); + var link = lookup.node; + if (!link) { + throw new FS.ErrnoError(44); + } + if (!link.node_ops.readlink) { + throw new FS.ErrnoError(28); + } + return link.node_ops.readlink(link); + }, + stat(path, dontFollow) { + var lookup = FS.lookupPath(path, { + follow: !dontFollow, + }); + var node = lookup.node; + var getattr = FS.checkOpExists(node.node_ops.getattr, 63); + return getattr(node); + }, + fstat(fd) { + var stream = FS.getStreamChecked(fd); + var node = stream.node; + var getattr = stream.stream_ops.getattr; + var arg = getattr ? stream : node; + getattr ??= node.node_ops.getattr; + FS.checkOpExists(getattr, 63); + return getattr(arg); + }, + lstat(path) { + return FS.stat(path, true); + }, + doChmod(stream, node, mode, dontFollow) { + FS.doSetAttr(stream, node, { + mode: (mode & 4095) | (node.mode & ~4095), + ctime: Date.now(), + dontFollow, + }); + }, + chmod(path, mode, dontFollow) { + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { + follow: !dontFollow, + }); + node = lookup.node; + } else { + node = path; + } + FS.doChmod(null, node, mode, dontFollow); + }, + lchmod(path, mode) { + FS.chmod(path, mode, true); + }, + fchmod(fd, mode) { + var stream = FS.getStreamChecked(fd); + FS.doChmod(stream, stream.node, mode, false); + }, + doChown(stream, node, dontFollow) { + FS.doSetAttr(stream, node, { + timestamp: Date.now(), + dontFollow, + }); + }, + chown(path, uid, gid, dontFollow) { + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { + follow: !dontFollow, + }); + node = lookup.node; + } else { + node = path; + } + FS.doChown(null, node, dontFollow); + }, + lchown(path, uid, gid) { + FS.chown(path, uid, gid, true); + }, + fchown(fd, uid, gid) { + var stream = FS.getStreamChecked(fd); + FS.doChown(stream, stream.node, false); + }, + doTruncate(stream, node, len) { + if (FS.isDir(node.mode)) { + throw new FS.ErrnoError(31); + } + if (!FS.isFile(node.mode)) { + throw new FS.ErrnoError(28); + } + var errCode = FS.nodePermissions(node, 'w'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + FS.doSetAttr(stream, node, { + size: len, + timestamp: Date.now(), + }); + }, + truncate(path, len) { + if (len < 0) { + throw new FS.ErrnoError(28); + } + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { + follow: true, + }); + node = lookup.node; + } else { + node = path; + } + FS.doTruncate(null, node, len); + }, + ftruncate(fd, len) { + var stream = FS.getStreamChecked(fd); + if (len < 0 || (stream.flags & 2097155) === 0) { + throw new FS.ErrnoError(28); + } + FS.doTruncate(stream, stream.node, len); + }, + utime(path, atime, mtime) { + var lookup = FS.lookupPath(path, { + follow: true, + }); + var node = lookup.node; + var setattr = FS.checkOpExists(node.node_ops.setattr, 63); + setattr(node, { + atime, + mtime, + }); + }, + open(path, flags, mode = 438) { + if (path === '') { + throw new FS.ErrnoError(44); + } + flags = + typeof flags == 'string' ? FS_modeStringToFlags(flags) : flags; + if (flags & 64) { + mode = (mode & 4095) | 32768; + } else { + mode = 0; + } + var node; + var isDirPath; + if (typeof path == 'object') { + node = path; + } else { + isDirPath = path.endsWith('/'); + // noent_okay makes it so that if the final component of the path + // doesn't exist, lookupPath returns `node: undefined`. `path` will be + // updated to point to the target of all symlinks. + var lookup = FS.lookupPath(path, { + follow: !(flags & 131072), + noent_okay: true, + }); + node = lookup.node; + path = lookup.path; + } + // perhaps we need to create the node + var created = false; + if (flags & 64) { + if (node) { + // if O_CREAT and O_EXCL are set, error out if the node already exists + if (flags & 128) { + throw new FS.ErrnoError(20); + } + } else if (isDirPath) { + throw new FS.ErrnoError(31); + } else { + // node doesn't exist, try to create it + // Ignore the permission bits here to ensure we can `open` this new + // file below. We use chmod below the apply the permissions once the + // file is open. + node = FS.mknod(path, mode | 511, 0); + created = true; + } + } + if (!node) { + throw new FS.ErrnoError(44); + } + // can't truncate a device + if (FS.isChrdev(node.mode)) { + flags &= ~512; + } + // if asked only for a directory, then this must be one + if (flags & 65536 && !FS.isDir(node.mode)) { + throw new FS.ErrnoError(54); + } + // check permissions, if this is not a file we just created now (it is ok to + // create and write to a file with read-only permissions; it is read-only + // for later use) + if (!created) { + var errCode = FS.mayOpen(node, flags); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + } + // do truncation if necessary + if (flags & 512 && !created) { + FS.truncate(node, 0); + } + // we've already handled these, don't pass down to the underlying vfs + flags &= ~(128 | 512 | 131072); + // register the stream with the filesystem + var stream = FS.createStream({ + node, + path: FS.getPath(node), + // we want the absolute path to the node + flags, + seekable: true, + position: 0, + stream_ops: node.stream_ops, + // used by the file family libc calls (fopen, fwrite, ferror, etc.) + ungotten: [], + error: false, + }); + // call the new stream's open function + if (stream.stream_ops.open) { + stream.stream_ops.open(stream); + } + if (created) { + FS.chmod(node, mode & 511); + } + if (Module['logReadFiles'] && !(flags & 1)) { + if (!(path in FS.readFiles)) { + FS.readFiles[path] = 1; + } + } + return stream; + }, + close(stream) { + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if (stream.getdents) stream.getdents = null; + // free readdir state + try { + if (stream.stream_ops.close) { + stream.stream_ops.close(stream); + } + } catch (e) { + throw e; + } finally { + FS.closeStream(stream.fd); + } + stream.fd = null; + }, + isClosed(stream) { + return stream.fd === null; + }, + llseek(stream, offset, whence) { + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if (!stream.seekable || !stream.stream_ops.llseek) { + throw new FS.ErrnoError(70); + } + if (whence != 0 && whence != 1 && whence != 2) { + throw new FS.ErrnoError(28); + } + stream.position = stream.stream_ops.llseek(stream, offset, whence); + stream.ungotten = []; + return stream.position; + }, + read(stream, buffer, offset, length, position) { + if (length < 0 || position < 0) { + throw new FS.ErrnoError(28); + } + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if ((stream.flags & 2097155) === 1) { + throw new FS.ErrnoError(8); + } + if (FS.isDir(stream.node.mode)) { + throw new FS.ErrnoError(31); + } + if (!stream.stream_ops.read) { + throw new FS.ErrnoError(28); + } + var seeking = typeof position != 'undefined'; + if (!seeking) { + position = stream.position; + } else if (!stream.seekable) { + throw new FS.ErrnoError(70); + } + var bytesRead = stream.stream_ops.read( + stream, + buffer, + offset, + length, + position + ); + if (!seeking) stream.position += bytesRead; + return bytesRead; + }, + write(stream, buffer, offset, length, position, canOwn) { + if (length < 0 || position < 0) { + throw new FS.ErrnoError(28); + } + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if ((stream.flags & 2097155) === 0) { + throw new FS.ErrnoError(8); + } + if (FS.isDir(stream.node.mode)) { + throw new FS.ErrnoError(31); + } + if (!stream.stream_ops.write) { + throw new FS.ErrnoError(28); + } + if (stream.seekable && stream.flags & 1024) { + // seek to the end before writing in append mode + FS.llseek(stream, 0, 2); + } + var seeking = typeof position != 'undefined'; + if (!seeking) { + position = stream.position; + } else if (!stream.seekable) { + throw new FS.ErrnoError(70); + } + var bytesWritten = stream.stream_ops.write( + stream, + buffer, + offset, + length, + position, + canOwn + ); + if (!seeking) stream.position += bytesWritten; + return bytesWritten; + }, + mmap(stream, length, position, prot, flags) { + // User requests writing to file (prot & PROT_WRITE != 0). + // Checking if we have permissions to write to the file unless + // MAP_PRIVATE flag is set. According to POSIX spec it is possible + // to write to file opened in read-only mode with MAP_PRIVATE flag, + // as all modifications will be visible only in the memory of + // the current process. + if ( + (prot & 2) !== 0 && + (flags & 2) === 0 && + (stream.flags & 2097155) !== 2 + ) { + throw new FS.ErrnoError(2); + } + if ((stream.flags & 2097155) === 1) { + throw new FS.ErrnoError(2); + } + if (!stream.stream_ops.mmap) { + throw new FS.ErrnoError(43); + } + if (!length) { + throw new FS.ErrnoError(28); + } + return stream.stream_ops.mmap( + stream, + length, + position, + prot, + flags + ); + }, + msync(stream, buffer, offset, length, mmapFlags) { + if (!stream.stream_ops.msync) { + return 0; + } + return stream.stream_ops.msync( + stream, + buffer, + offset, + length, + mmapFlags + ); + }, + ioctl(stream, cmd, arg) { + if (!stream.stream_ops.ioctl) { + throw new FS.ErrnoError(59); + } + return stream.stream_ops.ioctl(stream, cmd, arg); + }, + readFile(path, opts = {}) { + opts.flags = opts.flags || 0; + opts.encoding = opts.encoding || 'binary'; + if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') { + abort(`Invalid encoding type "${opts.encoding}"`); + } + var stream = FS.open(path, opts.flags); + var stat = FS.stat(path); + var length = stat.size; + var buf = new Uint8Array(length); + FS.read(stream, buf, 0, length, 0); + if (opts.encoding === 'utf8') { + buf = UTF8ArrayToString(buf); + } + FS.close(stream); + return buf; + }, + writeFile(path, data, opts = {}) { + opts.flags = opts.flags || 577; + var stream = FS.open(path, opts.flags, opts.mode); + if (typeof data == 'string') { + data = new Uint8Array(intArrayFromString(data, true)); + } + if (ArrayBuffer.isView(data)) { + FS.write( + stream, + data, + 0, + data.byteLength, + undefined, + opts.canOwn + ); + } else { + abort('Unsupported data type'); + } + FS.close(stream); + }, + cwd: () => FS.currentPath, + chdir(path) { + var lookup = FS.lookupPath(path, { + follow: true, + }); + if (lookup.node === null) { + throw new FS.ErrnoError(44); + } + if (!FS.isDir(lookup.node.mode)) { + throw new FS.ErrnoError(54); + } + var errCode = FS.nodePermissions(lookup.node, 'x'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + FS.currentPath = lookup.path; + }, + createDefaultDirectories() { + FS.mkdir('/tmp'); + FS.mkdir('/home'); + FS.mkdir('/home/web_user'); + }, + createDefaultDevices() { + // create /dev + FS.mkdir('/dev'); + // setup /dev/null + FS.registerDevice(FS.makedev(1, 3), { + read: () => 0, + write: (stream, buffer, offset, length, pos) => length, + llseek: () => 0, + }); + FS.mkdev('/dev/null', FS.makedev(1, 3)); + // setup /dev/tty and /dev/tty1 + // stderr needs to print output using err() rather than out() + // so we register a second tty just for it. + TTY.register(FS.makedev(5, 0), TTY.default_tty_ops); + TTY.register(FS.makedev(6, 0), TTY.default_tty1_ops); + FS.mkdev('/dev/tty', FS.makedev(5, 0)); + FS.mkdev('/dev/tty1', FS.makedev(6, 0)); + // setup /dev/[u]random + // use a buffer to avoid overhead of individual crypto calls per byte + var randomBuffer = new Uint8Array(1024), + randomLeft = 0; + var randomByte = () => { + if (randomLeft === 0) { + randomFill(randomBuffer); + randomLeft = randomBuffer.byteLength; + } + return randomBuffer[--randomLeft]; + }; + FS.createDevice('/dev', 'random', randomByte); + FS.createDevice('/dev', 'urandom', randomByte); + // we're not going to emulate the actual shm device, + // just create the tmp dirs that reside in it commonly + FS.mkdir('/dev/shm'); + FS.mkdir('/dev/shm/tmp'); + }, + createSpecialDirectories() { + // create /proc/self/fd which allows /proc/self/fd/6 => readlink gives the + // name of the stream for fd 6 (see test_unistd_ttyname) + FS.mkdir('/proc'); + var proc_self = FS.mkdir('/proc/self'); + FS.mkdir('/proc/self/fd'); + FS.mount( + { + mount() { + var node = FS.createNode(proc_self, 'fd', 16895, 73); + node.stream_ops = { + llseek: MEMFS.stream_ops.llseek, + }; + node.node_ops = { + lookup(parent, name) { + var fd = +name; + var stream = FS.getStreamChecked(fd); + var ret = { + parent: null, + mount: { + mountpoint: 'fake', + }, + node_ops: { + readlink: () => stream.path, + }, + id: fd + 1, + }; + ret.parent = ret; + // make it look like a simple root node + return ret; + }, + readdir() { + return Array.from(FS.streams.entries()) + .filter(([k, v]) => v) + .map(([k, v]) => k.toString()); + }, + }; + return node; + }, + }, + {}, + '/proc/self/fd' + ); + }, + createStandardStreams(input, output, error) { + // TODO deprecate the old functionality of a single + // input / output callback and that utilizes FS.createDevice + // and instead require a unique set of stream ops + // by default, we symlink the standard streams to the + // default tty devices. however, if the standard streams + // have been overwritten we create a unique device for + // them instead. + if (input) { + FS.createDevice('/dev', 'stdin', input); + } else { + FS.symlink('/dev/tty', '/dev/stdin'); + } + if (output) { + FS.createDevice('/dev', 'stdout', null, output); + } else { + FS.symlink('/dev/tty', '/dev/stdout'); + } + if (error) { + FS.createDevice('/dev', 'stderr', null, error); + } else { + FS.symlink('/dev/tty1', '/dev/stderr'); + } + // open default streams for the stdin, stdout and stderr devices + var stdin = FS.open('/dev/stdin', 0); + var stdout = FS.open('/dev/stdout', 1); + var stderr = FS.open('/dev/stderr', 1); + }, + staticInit() { + FS.nameTable = new Array(4096); + FS.mount(MEMFS, {}, '/'); + FS.createDefaultDirectories(); + FS.createDefaultDevices(); + FS.createSpecialDirectories(); + FS.filesystems = { + MEMFS: MEMFS, + NODEFS: NODEFS, + PROXYFS: PROXYFS, + }; + }, + init(input, output, error) { + FS.initialized = true; + // Allow Module.stdin etc. to provide defaults, if none explicitly passed to us here + input ??= Module['stdin']; + output ??= Module['stdout']; + error ??= Module['stderr']; + FS.createStandardStreams(input, output, error); + }, + quit() { + FS.initialized = false; + // force-flush all streams, so we get musl std streams printed out + _fflush(0); + // close all of our streams + for (var stream of FS.streams) { + if (stream) { + FS.close(stream); + } + } + }, + findObject(path, dontResolveLastLink) { + var ret = FS.analyzePath(path, dontResolveLastLink); + if (!ret.exists) { + return null; + } + return ret.object; + }, + analyzePath(path, dontResolveLastLink) { + // operate from within the context of the symlink's target + try { + var lookup = FS.lookupPath(path, { + follow: !dontResolveLastLink, + }); + path = lookup.path; + } catch (e) {} + var ret = { + isRoot: false, + exists: false, + error: 0, + name: null, + path: null, + object: null, + parentExists: false, + parentPath: null, + parentObject: null, + }; + try { + var lookup = FS.lookupPath(path, { + parent: true, + }); + ret.parentExists = true; + ret.parentPath = lookup.path; + ret.parentObject = lookup.node; + ret.name = PATH.basename(path); + lookup = FS.lookupPath(path, { + follow: !dontResolveLastLink, + }); + ret.exists = true; + ret.path = lookup.path; + ret.object = lookup.node; + ret.name = lookup.node.name; + ret.isRoot = lookup.path === '/'; + } catch (e) { + ret.error = e.errno; + } + return ret; + }, + createPath(parent, path, canRead, canWrite) { + parent = typeof parent == 'string' ? parent : FS.getPath(parent); + var parts = path.split('/').reverse(); + while (parts.length) { + var part = parts.pop(); + if (!part) continue; + var current = PATH.join2(parent, part); + try { + FS.mkdir(current); + } catch (e) { + if (e.errno != 20) throw e; + } + parent = current; + } + return current; + }, + createFile(parent, name, properties, canRead, canWrite) { + var path = PATH.join2( + typeof parent == 'string' ? parent : FS.getPath(parent), + name + ); + var mode = FS_getMode(canRead, canWrite); + return FS.create(path, mode); + }, + createDataFile(parent, name, data, canRead, canWrite, canOwn) { + var path = name; + if (parent) { + parent = + typeof parent == 'string' ? parent : FS.getPath(parent); + path = name ? PATH.join2(parent, name) : parent; + } + var mode = FS_getMode(canRead, canWrite); + var node = FS.create(path, mode); + if (data) { + if (typeof data == 'string') { + var arr = new Array(data.length); + for (var i = 0, len = data.length; i < len; ++i) + arr[i] = data.charCodeAt(i); + data = arr; + } + // make sure we can write to the file + FS.chmod(node, mode | 146); + var stream = FS.open(node, 577); + FS.write(stream, data, 0, data.length, 0, canOwn); + FS.close(stream); + FS.chmod(node, mode); + } + }, + createDevice(parent, name, input, output) { + var path = PATH.join2( + typeof parent == 'string' ? parent : FS.getPath(parent), + name + ); + var mode = FS_getMode(!!input, !!output); + FS.createDevice.major ??= 64; + var dev = FS.makedev(FS.createDevice.major++, 0); + // Create a fake device that a set of stream ops to emulate + // the old behavior. + FS.registerDevice(dev, { + open(stream) { + stream.seekable = false; + }, + close(stream) { + // flush any pending line data + if (output?.buffer?.length) { + output(10); + } + }, + read(stream, buffer, offset, length, pos) { + var bytesRead = 0; + for (var i = 0; i < length; i++) { + var result; + try { + result = input(); + } catch (e) { + throw new FS.ErrnoError(29); + } + if (result === undefined && bytesRead === 0) { + throw new FS.ErrnoError(6); + } + if (result === null || result === undefined) break; + bytesRead++; + buffer[offset + i] = result; + } + if (bytesRead) { + stream.node.atime = Date.now(); + } + return bytesRead; + }, + write(stream, buffer, offset, length, pos) { + for (var i = 0; i < length; i++) { + try { + output(buffer[offset + i]); + } catch (e) { + throw new FS.ErrnoError(29); + } + } + if (length) { + stream.node.mtime = stream.node.ctime = Date.now(); + } + return i; + }, + }); + return FS.mkdev(path, mode, dev); + }, + forceLoadFile(obj) { + if (obj.isDevice || obj.isFolder || obj.link || obj.contents) + return true; + if (globalThis.XMLHttpRequest) { + abort( + 'Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.' + ); + } else { + // Command-line. + try { + obj.contents = readBinary(obj.url); + } catch (e) { + throw new FS.ErrnoError(29); + } + } + }, + createLazyFile(parent, name, url, canRead, canWrite) { + // Lazy chunked Uint8Array (implements get and length from Uint8Array). + // Actual getting is abstracted away for eventual reuse. + class LazyUint8Array { + lengthKnown = false; + chunks = []; + // Loaded chunks. Index is the chunk number + get(idx) { + if (idx > this.length - 1 || idx < 0) { + return undefined; + } + var chunkOffset = idx % this.chunkSize; + var chunkNum = (idx / this.chunkSize) | 0; + return this.getter(chunkNum)[chunkOffset]; + } + setDataGetter(getter) { + this.getter = getter; + } + cacheLength() { + // Find length + var xhr = new XMLHttpRequest(); + xhr.open('HEAD', url, false); + xhr.send(null); + if ( + !( + (xhr.status >= 200 && xhr.status < 300) || + xhr.status === 304 + ) + ) + abort( + "Couldn't load " + url + '. Status: ' + xhr.status + ); + var datalength = Number( + xhr.getResponseHeader('Content-length') + ); + var header; + var hasByteServing = + (header = xhr.getResponseHeader('Accept-Ranges')) && + header === 'bytes'; + var usesGzip = + (header = xhr.getResponseHeader('Content-Encoding')) && + header === 'gzip'; + var chunkSize = 1024 * 1024; + // Chunk size in bytes + if (!hasByteServing) chunkSize = datalength; + // Function to get a range from the remote URL. + var doXHR = (from, to) => { + if (from > to) + abort( + 'invalid range (' + + from + + ', ' + + to + + ') or no bytes requested!' + ); + if (to > datalength - 1) + abort( + 'only ' + + datalength + + ' bytes available! programmer error!' + ); + // TODO: Use mozResponseArrayBuffer, responseStream, etc. if available. + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, false); + if (datalength !== chunkSize) + xhr.setRequestHeader( + 'Range', + 'bytes=' + from + '-' + to + ); + // Some hints to the browser that we want binary data. + xhr.responseType = 'arraybuffer'; + if (xhr.overrideMimeType) { + xhr.overrideMimeType( + 'text/plain; charset=x-user-defined' + ); + } + xhr.send(null); + if ( + !( + (xhr.status >= 200 && xhr.status < 300) || + xhr.status === 304 + ) + ) + abort( + "Couldn't load " + + url + + '. Status: ' + + xhr.status + ); + if (xhr.response !== undefined) { + return new Uint8Array( + /** @type{Array} */ (xhr.response || []) + ); + } + return intArrayFromString(xhr.responseText || '', true); + }; + var lazyArray = this; + lazyArray.setDataGetter((chunkNum) => { + var start = chunkNum * chunkSize; + var end = (chunkNum + 1) * chunkSize - 1; + // including this byte + end = Math.min(end, datalength - 1); + // if datalength-1 is selected, this is the last block + if (typeof lazyArray.chunks[chunkNum] == 'undefined') { + lazyArray.chunks[chunkNum] = doXHR(start, end); + } + if (typeof lazyArray.chunks[chunkNum] == 'undefined') + abort('doXHR failed!'); + return lazyArray.chunks[chunkNum]; + }); + if (usesGzip || !datalength) { + // if the server uses gzip or doesn't supply the length, we have to download the whole file to get the (uncompressed) length + chunkSize = datalength = 1; + // this will force getter(0)/doXHR do download the whole file + datalength = this.getter(0).length; + chunkSize = datalength; + out( + 'LazyFiles on gzip forces download of the whole file when length is accessed' + ); + } + this._length = datalength; + this._chunkSize = chunkSize; + this.lengthKnown = true; + } + get length() { + if (!this.lengthKnown) { + this.cacheLength(); + } + return this._length; + } + get chunkSize() { + if (!this.lengthKnown) { + this.cacheLength(); + } + return this._chunkSize; + } + } + if (globalThis.XMLHttpRequest) { + if (!ENVIRONMENT_IS_WORKER) + abort( + 'Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc' + ); + var lazyArray = new LazyUint8Array(); + var properties = { + isDevice: false, + contents: lazyArray, + }; + } else { + var properties = { + isDevice: false, + url, + }; + } + var node = FS.createFile( + parent, + name, + properties, + canRead, + canWrite + ); + // This is a total hack, but I want to get this lazy file code out of the + // core of MEMFS. If we want to keep this lazy file concept I feel it should + // be its own thin LAZYFS proxying calls to MEMFS. + if (properties.contents) { + node.contents = properties.contents; + } else if (properties.url) { + node.contents = null; + node.url = properties.url; + } + // Add a function that defers querying the file size until it is asked the first time. + Object.defineProperties(node, { + usedBytes: { + get: function () { + return this.contents.length; + }, + }, + }); + // override each stream op with one that tries to force load the lazy file first + var stream_ops = {}; + for (const [key, fn] of Object.entries(node.stream_ops)) { + stream_ops[key] = (...args) => { + FS.forceLoadFile(node); + return fn(...args); + }; + } + function writeChunks(stream, buffer, offset, length, position) { + var contents = stream.node.contents; + if (position >= contents.length) return 0; + var size = Math.min(contents.length - position, length); + if (contents.slice) { + // normal array + for (var i = 0; i < size; i++) { + buffer[offset + i] = contents[position + i]; + } + } else { + for (var i = 0; i < size; i++) { + // LazyUint8Array from sync binary XHR + buffer[offset + i] = contents.get(position + i); + } + } + return size; + } + // use a custom read function + stream_ops.read = (stream, buffer, offset, length, position) => { + FS.forceLoadFile(node); + return writeChunks(stream, buffer, offset, length, position); + }; + // use a custom mmap function + stream_ops.mmap = (stream, length, position, prot, flags) => { + FS.forceLoadFile(node); + var ptr = mmapAlloc(length); + if (!ptr) { + throw new FS.ErrnoError(48); + } + writeChunks(stream, HEAP8, ptr, length, position); + return { + ptr, + allocated: true, + }; + }; + node.stream_ops = stream_ops; + return node; + }, + }; + + var findLibraryFS = (libName, rpath) => { + // If we're preloading a dynamic library, the runtime is not ready to call + // __wasmfs_identify or __emscripten_find_dylib. So just quit out. + // This means that DT_NEEDED for the main module and transitive dependencies + // of it won't work with this code path. Similarly, it means that calling + // loadDynamicLibrary in a preRun hook can't use this code path. + if (!runtimeInitialized) { + return undefined; + } + if (PATH.isAbs(libName)) { + try { + FS.lookupPath(libName); + return libName; + } catch (e) { + return undefined; + } + } + var rpathResolved = (rpath?.paths || []).map((p) => + replaceORIGIN(rpath?.parentLibPath, p) + ); + return withStackSave(() => { + // In dylink.c we use: `char buf[2*NAME_MAX+2];` and NAME_MAX is 255. + // So we use the same size here. + var bufSize = 2 * 255 + 2; + var buf = stackAlloc(bufSize); + var rpathC = stringToUTF8OnStack(rpathResolved.join(':')); + var libNameC = stringToUTF8OnStack(libName); + var resLibNameC = __emscripten_find_dylib( + buf, + rpathC, + libNameC, + bufSize + ); + return resLibNameC ? UTF8ToString(resLibNameC) : undefined; + }); + }; + + /** + * @param {number=} handle + * @param {Object=} localScope + */ function loadDynamicLibrary( + libName, + flags = { + global: true, + nodelete: true, + }, + localScope, + handle + ) { + // when loadDynamicLibrary did not have flags, libraries were loaded + // globally & permanently + var dso = LDSO.loadedLibsByName[libName]; + if (dso) { + // the library is being loaded or has been loaded already. + if (!flags.global) { + if (localScope) { + Object.assign(localScope, dso.exports); + } + } else if (!dso.global) { + // The library was previously loaded only locally but not + // we have a request with global=true. + dso.global = true; + mergeLibSymbols(dso.exports, libName); + } + // same for "nodelete" + if (flags.nodelete && dso.refcount !== Infinity) { + dso.refcount = Infinity; + } + dso.refcount++; + if (handle) { + LDSO.loadedLibsByHandle[handle] = dso; + } + return flags.loadAsync ? Promise.resolve(true) : true; + } + // allocate new DSO + dso = newDSO(libName, handle, 'loading'); + dso.refcount = flags.nodelete ? Infinity : 1; + dso.global = flags.global; + // libName -> libData + function loadLibData() { + // for wasm, we can use fetch for async, but for fs mode we can only imitate it + if (handle) { + var data = HEAPU32[(handle + 28) >> 2]; + var dataSize = HEAPU32[(handle + 32) >> 2]; + if (data && dataSize) { + var libData = HEAP8.slice(data, data + dataSize); + return flags.loadAsync ? Promise.resolve(libData) : libData; + } + } + var f = findLibraryFS(libName, flags.rpath); + if (f) { + var libData = FS.readFile(f, { + encoding: 'binary', + }); + return flags.loadAsync ? Promise.resolve(libData) : libData; + } + var libFile = locateFile(libName); + if (flags.loadAsync) { + return asyncLoad(libFile); + } + // load the binary synchronously + if (!readBinary) { + throw new Error( + `${libFile}: file not found, and synchronous loading of external files is not available` + ); + } + return readBinary(libFile); + } + // libName -> exports + function getExports() { + // lookup preloaded cache first + var preloaded = preloadedWasm[libName]; + if (preloaded) { + return flags.loadAsync ? Promise.resolve(preloaded) : preloaded; + } + // module not preloaded - load lib data and create new module from it + if (flags.loadAsync) { + return loadLibData().then((libData) => + loadWebAssemblyModule( + libData, + flags, + libName, + localScope, + handle + ) + ); + } + return loadWebAssemblyModule( + loadLibData(), + flags, + libName, + localScope, + handle + ); + } + // module for lib is loaded - update the dso & global namespace + function moduleLoaded(exports) { + if (dso.global) { + mergeLibSymbols(exports, libName); + } else if (localScope) { + Object.assign(localScope, exports); + } + dso.exports = exports; + } + if (flags.loadAsync) { + return getExports().then((exports) => { + moduleLoaded(exports); + return true; + }); + } + moduleLoaded(getExports()); + return true; + } + + var reportUndefinedSymbols = () => { + for (var [symName, entry] of Object.entries(GOT)) { + if (entry.value == -1) { + var value = resolveGlobalSymbol(symName, true).sym; + if (!value && !entry.required) { + // Ignore undefined symbols that are imported as weak. + entry.value = 0; + continue; + } + if (typeof value == 'function') { + /** @suppress {checkTypes} */ entry.value = addFunction( + value, + value.sig + ); + } else if (typeof value == 'number') { + entry.value = value; + } else { + throw new Error( + `bad export type for '${symName}': ${typeof value} (${value})` + ); + } + } + } + }; + + var loadDylibs = async () => { + if (!dynamicLibraries.length) { + reportUndefinedSymbols(); + return; + } + addRunDependency('loadDylibs'); + // Load binaries asynchronously + for (var lib of dynamicLibraries) { + await loadDynamicLibrary(lib, { + loadAsync: true, + global: true, + nodelete: true, + allowUndefined: true, + }); + } + // we got them all, wonderful + reportUndefinedSymbols(); + removeRunDependency('loadDylibs'); + }; + + var noExitRuntime = false; + + var ___assert_fail = (condition, filename, line, func) => + abort( + `Assertion failed: ${UTF8ToString(condition)}, at: ` + + [ + filename ? UTF8ToString(filename) : 'unknown filename', + line, + func ? UTF8ToString(func) : 'unknown function', + ] + ); + + ___assert_fail.sig = 'vppip'; + + var ___call_sighandler = (fp, sig) => getWasmTableEntry(fp)(sig); + + ___call_sighandler.sig = 'vpi'; + + var SOCKFS = { + websocketArgs: {}, + callbacks: {}, + on(event, callback) { + SOCKFS.callbacks[event] = callback; + }, + emit(event, param) { + SOCKFS.callbacks[event]?.(param); + }, + mount(mount) { + // The incomming Module['websocket'] can be used for configuring + // configuring subprotocol/url, etc + SOCKFS.websocketArgs = Module['websocket'] || {}; + // Add the Event registration mechanism to the exported websocket configuration + // object so we can register network callbacks from native JavaScript too. + // For more documentation see system/include/emscripten/emscripten.h + (Module['websocket'] ??= {})['on'] = SOCKFS.on; + return FS.createNode(null, '/', 16895, 0); + }, + createSocket(family, type, protocol) { + // Emscripten only supports AF_INET + if (family != 2) { + throw new FS.ErrnoError(5); + } + type &= ~526336; + // Some applications may pass it; it makes no sense for a single process. + // Emscripten only supports SOCK_STREAM and SOCK_DGRAM + if (type != 1 && type != 2) { + throw new FS.ErrnoError(28); + } + var streaming = type == 1; + if (streaming && protocol && protocol != 6) { + throw new FS.ErrnoError(66); + } + // create our internal socket structure + var sock = { + family, + type, + protocol, + server: null, + error: null, + // Used in getsockopt for SOL_SOCKET/SO_ERROR test + peers: {}, + pending: [], + recv_queue: [], + sock_ops: SOCKFS.websocket_sock_ops, + }; + // create the filesystem node to store the socket structure + var name = SOCKFS.nextname(); + var node = FS.createNode(SOCKFS.root, name, 49152, 0); + node.sock = sock; + // and the wrapping stream that enables library functions such + // as read and write to indirectly interact with the socket + var stream = FS.createStream({ + path: name, + node, + flags: 2, + seekable: false, + stream_ops: SOCKFS.stream_ops, + }); + // map the new stream to the socket structure (sockets have a 1:1 + // relationship with a stream) + sock.stream = stream; + return sock; + }, + getSocket(fd) { + var stream = FS.getStream(fd); + if (!stream || !FS.isSocket(stream.node.mode)) { + return null; + } + return stream.node.sock; + }, + stream_ops: { + poll(stream) { + var sock = stream.node.sock; + return sock.sock_ops.poll(sock); + }, + ioctl(stream, request, varargs) { + var sock = stream.node.sock; + return sock.sock_ops.ioctl(sock, request, varargs); + }, + read(stream, buffer, offset, length, position) { + var sock = stream.node.sock; + var msg = sock.sock_ops.recvmsg(sock, length); + if (!msg) { + // socket is closed + return 0; + } + buffer.set(msg.buffer, offset); + return msg.buffer.length; + }, + write(stream, buffer, offset, length, position) { + var sock = stream.node.sock; + return sock.sock_ops.sendmsg(sock, buffer, offset, length); + }, + close(stream) { + var sock = stream.node.sock; + sock.sock_ops.close(sock); + }, + }, + nextname() { + if (!SOCKFS.nextname.current) { + SOCKFS.nextname.current = 0; + } + return `socket[${SOCKFS.nextname.current++}]`; + }, + websocket_sock_ops: { + createPeer(sock, addr, port) { + var ws; + if (typeof addr == 'object') { + ws = addr; + addr = null; + port = null; + } + if (ws) { + // for sockets that've already connected (e.g. we're the server) + // we can inspect the _socket property for the address + if (ws._socket) { + addr = ws._socket.remoteAddress; + port = ws._socket.remotePort; + } else { + var result = /ws[s]?:\/\/([^:]+):(\d+)/.exec(ws.url); + if (!result) { + throw new Error( + 'WebSocket URL must be in the format ws(s)://address:port' + ); + } + addr = result[1]; + port = parseInt(result[2], 10); + } + } else { + // create the actual websocket object and connect + try { + // The default value is 'ws://' the replace is needed because the compiler replaces '//' comments with '#' + // comments without checking context, so we'd end up with ws:#, the replace swaps the '#' for '//' again. + var url = 'ws://'.replace('#', '//'); + // Make the WebSocket subprotocol (Sec-WebSocket-Protocol) default to binary if no configuration is set. + var subProtocols = 'binary'; + // The default value is 'binary' + // The default WebSocket options + var opts = undefined; + // Fetch runtime WebSocket URL config. + if ('function' === typeof SOCKFS.websocketArgs['url']) { + url = SOCKFS.websocketArgs['url'](...arguments); + } else if ( + 'string' === typeof SOCKFS.websocketArgs['url'] + ) { + url = SOCKFS.websocketArgs['url']; + } + // Fetch runtime WebSocket subprotocol config. + if (SOCKFS.websocketArgs['subprotocol']) { + subProtocols = SOCKFS.websocketArgs['subprotocol']; + } else if ( + SOCKFS.websocketArgs['subprotocol'] === null + ) { + subProtocols = 'null'; + } + if (url === 'ws://' || url === 'wss://') { + // Is the supplied URL config just a prefix, if so complete it. + var parts = addr.split('/'); + url = + url + + parts[0] + + ':' + + port + + '/' + + parts.slice(1).join('/'); + } + if (subProtocols !== 'null') { + // The regex trims the string (removes spaces at the beginning and end, then splits the string by + // , into an Array. Whitespace removal is important for Websockify and ws. + subProtocols = subProtocols + .replace(/^ +| +$/g, '') + .split(/ *, */); + opts = subProtocols; + } + // If node we use the ws library. + var WebSocketConstructor; + if (ENVIRONMENT_IS_NODE) { + WebSocketConstructor = + /** @type{(typeof WebSocket)} */ ( + require('ws') + ); + } else { + WebSocketConstructor = WebSocket; + } + if (Module['websocket']['decorator']) { + WebSocketConstructor = + Module['websocket']['decorator']( + WebSocketConstructor + ); + } + ws = new WebSocketConstructor(url, opts); + ws.binaryType = 'arraybuffer'; + } catch (e) { + throw new FS.ErrnoError(23); + } + } + var peer = { + addr, + port, + socket: ws, + msg_send_queue: [], + }; + SOCKFS.websocket_sock_ops.addPeer(sock, peer); + SOCKFS.websocket_sock_ops.handlePeerEvents(sock, peer); + // if this is a bound dgram socket, send the port number first to allow + // us to override the ephemeral port reported to us by remotePort on the + // remote end. + if (sock.type === 2 && typeof sock.sport != 'undefined') { + peer.msg_send_queue.push( + new Uint8Array([ + 255, + 255, + 255, + 255, + 'p'.charCodeAt(0), + 'o'.charCodeAt(0), + 'r'.charCodeAt(0), + 't'.charCodeAt(0), + (sock.sport & 65280) >> 8, + sock.sport & 255, + ]) + ); + } + return peer; + }, + getPeer(sock, addr, port) { + return sock.peers[addr + ':' + port]; + }, + addPeer(sock, peer) { + sock.peers[peer.addr + ':' + peer.port] = peer; + }, + removePeer(sock, peer) { + delete sock.peers[peer.addr + ':' + peer.port]; + }, + handlePeerEvents(sock, peer) { + var first = true; + var handleOpen = function () { + sock.connecting = false; + SOCKFS.emit('open', sock.stream.fd); + try { + var queued = peer.msg_send_queue.shift(); + while (queued) { + peer.socket.send(queued); + queued = peer.msg_send_queue.shift(); + } + } catch (e) { + // not much we can do here in the way of proper error handling as we've already + // lied and said this data was sent. shut it down. + peer.socket.close(); + } + }; + function handleMessage(data) { + if (typeof data == 'string') { + var encoder = new TextEncoder(); + // should be utf-8 + data = encoder.encode(data); + } else { + if (data.byteLength == 0) { + // An empty ArrayBuffer will emit a pseudo disconnect event + // as recv/recvmsg will return zero which indicates that a socket + // has performed a shutdown although the connection has not been disconnected yet. + return; + } + data = new Uint8Array(data); + } + // if this is the port message, override the peer's port with it + var wasfirst = first; + first = false; + if ( + wasfirst && + data.length === 10 && + data[0] === 255 && + data[1] === 255 && + data[2] === 255 && + data[3] === 255 && + data[4] === 'p'.charCodeAt(0) && + data[5] === 'o'.charCodeAt(0) && + data[6] === 'r'.charCodeAt(0) && + data[7] === 't'.charCodeAt(0) + ) { + // update the peer's port and it's key in the peer map + var newport = (data[8] << 8) | data[9]; + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + peer.port = newport; + SOCKFS.websocket_sock_ops.addPeer(sock, peer); + return; + } + sock.recv_queue.push({ + addr: peer.addr, + port: peer.port, + data, + }); + SOCKFS.emit('message', sock.stream.fd); + } + if (ENVIRONMENT_IS_NODE) { + peer.socket.on('open', handleOpen); + peer.socket.on('message', function (data, isBinary) { + if (!isBinary) { + return; + } + handleMessage(new Uint8Array(data).buffer); + }); + peer.socket.on('close', function () { + SOCKFS.emit('close', sock.stream.fd); + }); + peer.socket.on('error', function (error) { + // Although the ws library may pass errors that may be more descriptive than + // ECONNREFUSED they are not necessarily the expected error code e.g. + // ENOTFOUND on getaddrinfo seems to be node.js specific, so using ECONNREFUSED + // is still probably the most useful thing to do. + sock.error = 14; + // Used in getsockopt for SOL_SOCKET/SO_ERROR test. + SOCKFS.emit('error', [ + sock.stream.fd, + sock.error, + 'ECONNREFUSED: Connection refused', + ]); + }); + } else { + peer.socket.onopen = handleOpen; + peer.socket.onclose = function () { + SOCKFS.emit('close', sock.stream.fd); + }; + peer.socket.onmessage = function peer_socket_onmessage( + event + ) { + handleMessage(event.data); + }; + peer.socket.onerror = function (error) { + // The WebSocket spec only allows a 'simple event' to be thrown on error, + // so we only really know as much as ECONNREFUSED. + sock.error = 14; + // Used in getsockopt for SOL_SOCKET/SO_ERROR test. + SOCKFS.emit('error', [ + sock.stream.fd, + sock.error, + 'ECONNREFUSED: Connection refused', + ]); + }; + } + }, + poll(sock) { + if (sock.type === 1 && sock.server) { + // listen sockets should only say they're available for reading + // if there are pending clients. + return sock.pending.length ? 64 | 1 : 0; + } + var mask = 0; + var dest = + sock.type === 1 // we only care about the socket state for connection-based sockets + ? SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ) + : null; + if ( + sock.recv_queue.length || + !dest || // connection-less sockets are always ready to read + (dest && dest.socket.readyState === dest.socket.CLOSING) || + (dest && dest.socket.readyState === dest.socket.CLOSED) + ) { + // let recv return 0 once closed + mask |= 64 | 1; + } + if ( + !dest || // connection-less sockets are always ready to write + (dest && dest.socket.readyState === dest.socket.OPEN) + ) { + mask |= 4; + } + if ( + (dest && dest.socket.readyState === dest.socket.CLOSING) || + (dest && dest.socket.readyState === dest.socket.CLOSED) + ) { + // When an non-blocking connect fails mark the socket as writable. + // Its up to the calling code to then use getsockopt with SO_ERROR to + // retrieve the error. + // See https://man7.org/linux/man-pages/man2/connect.2.html + if (sock.connecting) { + mask |= 4; + } else { + mask |= 16; + } + } + return mask; + }, + ioctl(sock, request, arg) { + switch (request) { + case 21531: + var bytes = 0; + if (sock.recv_queue.length) { + bytes = sock.recv_queue[0].data.length; + } + HEAP32[arg >> 2] = bytes; + return 0; + + case 21537: + var on = HEAP32[arg >> 2]; + if (on) { + sock.stream.flags |= 2048; + } else { + sock.stream.flags &= ~2048; + } + return 0; + + default: + return 28; + } + }, + close(sock) { + // if we've spawned a listen server, close it + if (sock.server) { + try { + sock.server.close(); + } catch (e) {} + sock.server = null; + } + // close any peer connections + for (var peer of Object.values(sock.peers)) { + try { + peer.socket.close(); + } catch (e) {} + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + } + return 0; + }, + bind(sock, addr, port) { + if ( + typeof sock.saddr != 'undefined' || + typeof sock.sport != 'undefined' + ) { + throw new FS.ErrnoError(28); + } + sock.saddr = addr; + sock.sport = port; + // in order to emulate dgram sockets, we need to launch a listen server when + // binding on a connection-less socket + // note: this is only required on the server side + if (sock.type === 2) { + // close the existing server if it exists + if (sock.server) { + sock.server.close(); + sock.server = null; + } + // swallow error operation not supported error that occurs when binding in the + // browser where this isn't supported + try { + sock.sock_ops.listen(sock, 0); + } catch (e) { + if (!(e.name === 'ErrnoError')) throw e; + if (e.errno !== 138) throw e; + } + } + }, + connect(sock, addr, port) { + if (sock.server) { + throw new FS.ErrnoError(138); + } + // TODO autobind + // if (!sock.addr && sock.type == 2) { + // } + // early out if we're already connected / in the middle of connecting + if ( + typeof sock.daddr != 'undefined' && + typeof sock.dport != 'undefined' + ) { + var dest = SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ); + if (dest) { + if (dest.socket.readyState === dest.socket.CONNECTING) { + throw new FS.ErrnoError(7); + } else { + throw new FS.ErrnoError(30); + } + } + } + // add the socket to our peer list and set our + // destination address / port to match + var peer = SOCKFS.websocket_sock_ops.createPeer( + sock, + addr, + port + ); + sock.daddr = peer.addr; + sock.dport = peer.port; + // because we cannot synchronously block to wait for the WebSocket + // connection to complete, we return here pretending that the connection + // was a success. + sock.connecting = true; + }, + listen(sock, backlog) { + if (!ENVIRONMENT_IS_NODE) { + throw new FS.ErrnoError(138); + } + if (sock.server) { + throw new FS.ErrnoError(28); + } + var WebSocketServer = require('ws').Server; + var host = sock.saddr; + if (Module['websocket']['serverDecorator']) { + WebSocketServer = + Module['websocket']['serverDecorator'](WebSocketServer); + } + sock.server = new WebSocketServer({ + host, + port: sock.sport, + }); + SOCKFS.emit('listen', sock.stream.fd); + // Send Event with listen fd. + sock.server.on('connection', function (ws) { + if (sock.type === 1) { + var newsock = SOCKFS.createSocket( + sock.family, + sock.type, + sock.protocol + ); + // create a peer on the new socket + var peer = SOCKFS.websocket_sock_ops.createPeer( + newsock, + ws + ); + newsock.daddr = peer.addr; + newsock.dport = peer.port; + // push to queue for accept to pick up + sock.pending.push(newsock); + SOCKFS.emit('connection', newsock.stream.fd); + } else { + // create a peer on the listen socket so calling sendto + // with the listen socket and an address will resolve + // to the correct client + SOCKFS.websocket_sock_ops.createPeer(sock, ws); + SOCKFS.emit('connection', sock.stream.fd); + } + }); + sock.server.on('close', function () { + SOCKFS.emit('close', sock.stream.fd); + sock.server = null; + }); + sock.server.on('error', function (error) { + // Although the ws library may pass errors that may be more descriptive than + // ECONNREFUSED they are not necessarily the expected error code e.g. + // ENOTFOUND on getaddrinfo seems to be node.js specific, so using EHOSTUNREACH + // is still probably the most useful thing to do. This error shouldn't + // occur in a well written app as errors should get trapped in the compiled + // app's own getaddrinfo call. + sock.error = 23; + // Used in getsockopt for SOL_SOCKET/SO_ERROR test. + SOCKFS.emit('error', [ + sock.stream.fd, + sock.error, + 'EHOSTUNREACH: Host is unreachable', + ]); + }); + }, + accept(listensock) { + if (!listensock.server || !listensock.pending.length) { + throw new FS.ErrnoError(28); + } + var newsock = listensock.pending.shift(); + newsock.stream.flags = listensock.stream.flags; + return newsock; + }, + getname(sock, peer) { + var addr, port; + if (peer) { + if (sock.daddr === undefined || sock.dport === undefined) { + throw new FS.ErrnoError(53); + } + addr = sock.daddr; + port = sock.dport; + } else { + // TODO saddr and sport will be set for bind()'d UDP sockets, but what + // should we be returning for TCP sockets that've been connect()'d? + addr = sock.saddr || 0; + port = sock.sport || 0; + } + return { + addr, + port, + }; + }, + sendmsg(sock, buffer, offset, length, addr, port) { + if (sock.type === 2) { + // connection-less sockets will honor the message address, + // and otherwise fall back to the bound destination address + if (addr === undefined || port === undefined) { + addr = sock.daddr; + port = sock.dport; + } + // if there was no address to fall back to, error out + if (addr === undefined || port === undefined) { + throw new FS.ErrnoError(17); + } + } else { + // connection-based sockets will only use the bound + addr = sock.daddr; + port = sock.dport; + } + // find the peer for the destination address + var dest = SOCKFS.websocket_sock_ops.getPeer(sock, addr, port); + // early out if not connected with a connection-based socket + if (sock.type === 1) { + if ( + !dest || + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + throw new FS.ErrnoError(53); + } + } + // create a copy of the incoming data to send, as the WebSocket API + // doesn't work entirely with an ArrayBufferView, it'll just send + // the entire underlying buffer + if (ArrayBuffer.isView(buffer)) { + offset += buffer.byteOffset; + buffer = buffer.buffer; + } + var data = buffer.slice(offset, offset + length); + // if we don't have a cached connectionless UDP datagram connection, or + // the TCP socket is still connecting, queue the message to be sent upon + // connect, and lie, saying the data was sent now. + if (!dest || dest.socket.readyState !== dest.socket.OPEN) { + // if we're not connected, open a new connection + if (sock.type === 2) { + if ( + !dest || + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + dest = SOCKFS.websocket_sock_ops.createPeer( + sock, + addr, + port + ); + } + } + dest.msg_send_queue.push(data); + return length; + } + try { + // send the actual data + dest.socket.send(data); + return length; + } catch (e) { + throw new FS.ErrnoError(28); + } + }, + recvmsg(sock, length, flags) { + // http://pubs.opengroup.org/onlinepubs/7908799/xns/recvmsg.html + if (sock.type === 1 && sock.server) { + // tcp servers should not be recv()'ing on the listen socket + throw new FS.ErrnoError(53); + } + var queued = sock.recv_queue.shift(); + if (!queued) { + if (sock.type === 1) { + var dest = SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ); + if (!dest) { + // if we have a destination address but are not connected, error out + throw new FS.ErrnoError(53); + } + if ( + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + // return null if the socket has closed + return null; + } + // else, our socket is in a valid state but truly has nothing available + throw new FS.ErrnoError(6); + } + throw new FS.ErrnoError(6); + } + // queued.data will be an ArrayBuffer if it's unadulterated, but if it's + // requeued TCP data it'll be an ArrayBufferView + var queuedLength = queued.data.byteLength || queued.data.length; + var queuedOffset = queued.data.byteOffset || 0; + var queuedBuffer = queued.data.buffer || queued.data; + var bytesRead = Math.min(length, queuedLength); + var res = { + buffer: new Uint8Array( + queuedBuffer, + queuedOffset, + bytesRead + ), + addr: queued.addr, + port: queued.port, + }; + // push back any unread data for TCP connections + if (flags & 2) { + bytesRead = 0; + } + if (sock.type === 1 && bytesRead < queuedLength) { + var bytesRemaining = queuedLength - bytesRead; + queued.data = new Uint8Array( + queuedBuffer, + queuedOffset + bytesRead, + bytesRemaining + ); + sock.recv_queue.unshift(queued); + } + return res; + }, + }, + }; + + var getSocketFromFD = (fd) => { + var socket = SOCKFS.getSocket(fd); + if (!socket) throw new FS.ErrnoError(8); + return socket; + }; + + var inetPton4 = (str) => { + var b = str.split('.'); + for (var i = 0; i < 4; i++) { + var tmp = Number(b[i]); + if (isNaN(tmp)) return null; + b[i] = tmp; + } + return (b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24)) >>> 0; + }; + + var inetPton6 = (str) => { + var words; + var w, offset, z; + /* http://home.deds.nl/~aeron/regex/ */ var valid6regx = + /^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i; + var parts = []; + if (!valid6regx.test(str)) { + return null; + } + if (str === '::') { + return [0, 0, 0, 0, 0, 0, 0, 0]; + } + // Z placeholder to keep track of zeros when splitting the string on ":" + if (str.startsWith('::')) { + str = str.replace('::', 'Z:'); + } else { + str = str.replace('::', ':Z:'); + } + if (str.indexOf('.') > 0) { + // parse IPv4 embedded stress + str = str.replace(new RegExp('[.]', 'g'), ':'); + words = str.split(':'); + words[words.length - 4] = + Number(words[words.length - 4]) + + Number(words[words.length - 3]) * 256; + words[words.length - 3] = + Number(words[words.length - 2]) + + Number(words[words.length - 1]) * 256; + words = words.slice(0, words.length - 2); + } else { + words = str.split(':'); + } + offset = 0; + z = 0; + for (w = 0; w < words.length; w++) { + if (typeof words[w] == 'string') { + if (words[w] === 'Z') { + // compressed zeros - write appropriate number of zero words + for (z = 0; z < 8 - words.length + 1; z++) { + parts[w + z] = 0; + } + offset = z - 1; + } else { + // parse hex to field to 16-bit value and write it in network byte-order + parts[w + offset] = _htons(parseInt(words[w], 16)); + } + } else { + // parsed IPv4 words + parts[w + offset] = words[w]; + } + } + return [ + (parts[1] << 16) | parts[0], + (parts[3] << 16) | parts[2], + (parts[5] << 16) | parts[4], + (parts[7] << 16) | parts[6], + ]; + }; + + /** @param {number=} addrlen */ var writeSockaddr = ( + sa, + family, + addr, + port, + addrlen + ) => { + switch (family) { + case 2: + addr = inetPton4(addr); + zeroMemory(sa, 16); + if (addrlen) { + HEAP32[addrlen >> 2] = 16; + } + HEAP16[sa >> 1] = family; + HEAP32[(sa + 4) >> 2] = addr; + HEAP16[(sa + 2) >> 1] = _htons(port); + break; + + case 10: + addr = inetPton6(addr); + zeroMemory(sa, 28); + if (addrlen) { + HEAP32[addrlen >> 2] = 28; + } + HEAP32[sa >> 2] = family; + HEAP32[(sa + 8) >> 2] = addr[0]; + HEAP32[(sa + 12) >> 2] = addr[1]; + HEAP32[(sa + 16) >> 2] = addr[2]; + HEAP32[(sa + 20) >> 2] = addr[3]; + HEAP16[(sa + 2) >> 1] = _htons(port); + break; + + default: + return 5; + } + return 0; + }; + + var DNS = { + address_map: { + id: 1, + addrs: {}, + names: {}, + }, + lookup_name(name) { + // If the name is already a valid ipv4 / ipv6 address, don't generate a fake one. + var res = inetPton4(name); + if (res !== null) { + return name; + } + res = inetPton6(name); + if (res !== null) { + return name; + } + // See if this name is already mapped. + var addr; + if (DNS.address_map.addrs[name]) { + addr = DNS.address_map.addrs[name]; + } else { + var id = DNS.address_map.id++; + addr = '172.29.' + (id & 255) + '.' + (id & 65280); + DNS.address_map.names[addr] = name; + DNS.address_map.addrs[name] = addr; + } + return addr; + }, + lookup_addr(addr) { + if (DNS.address_map.names[addr]) { + return DNS.address_map.names[addr]; + } + return null; + }, + }; + + function ___syscall_accept4(fd, addr, addrlen, flags, d1, d2) { + try { + var sock = getSocketFromFD(fd); + var newsock = sock.sock_ops.accept(sock); + if (addr) { + var errno = writeSockaddr( + addr, + newsock.family, + DNS.lookup_name(newsock.daddr), + newsock.dport, + addrlen + ); + } + return newsock.stream.fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_accept4.sig = 'iippiii'; + + var inetNtop4 = (addr) => + (addr & 255) + + '.' + + ((addr >> 8) & 255) + + '.' + + ((addr >> 16) & 255) + + '.' + + ((addr >> 24) & 255); + + var inetNtop6 = (ints) => { + // ref: http://www.ietf.org/rfc/rfc2373.txt - section 2.5.4 + // Format for IPv4 compatible and mapped 128-bit IPv6 Addresses + // 128-bits are split into eight 16-bit words + // stored in network byte order (big-endian) + // | 80 bits | 16 | 32 bits | + // +-----------------------------------------------------------------+ + // | 10 bytes | 2 | 4 bytes | + // +--------------------------------------+--------------------------+ + // + 5 words | 1 | 2 words | + // +--------------------------------------+--------------------------+ + // |0000..............................0000|0000| IPv4 ADDRESS | (compatible) + // +--------------------------------------+----+---------------------+ + // |0000..............................0000|FFFF| IPv4 ADDRESS | (mapped) + // +--------------------------------------+----+---------------------+ + var str = ''; + var word = 0; + var longest = 0; + var lastzero = 0; + var zstart = 0; + var len = 0; + var i = 0; + var parts = [ + ints[0] & 65535, + ints[0] >> 16, + ints[1] & 65535, + ints[1] >> 16, + ints[2] & 65535, + ints[2] >> 16, + ints[3] & 65535, + ints[3] >> 16, + ]; + // Handle IPv4-compatible, IPv4-mapped, loopback and any/unspecified addresses + var hasipv4 = true; + var v4part = ''; + // check if the 10 high-order bytes are all zeros (first 5 words) + for (i = 0; i < 5; i++) { + if (parts[i] !== 0) { + hasipv4 = false; + break; + } + } + if (hasipv4) { + // low-order 32-bits store an IPv4 address (bytes 13 to 16) (last 2 words) + v4part = inetNtop4(parts[6] | (parts[7] << 16)); + // IPv4-mapped IPv6 address if 16-bit value (bytes 11 and 12) == 0xFFFF (6th word) + if (parts[5] === -1) { + str = '::ffff:'; + str += v4part; + return str; + } + // IPv4-compatible IPv6 address if 16-bit value (bytes 11 and 12) == 0x0000 (6th word) + if (parts[5] === 0) { + str = '::'; + //special case IPv6 addresses + if (v4part === '0.0.0.0') v4part = ''; + // any/unspecified address + if (v4part === '0.0.0.1') v4part = '1'; + // loopback address + str += v4part; + return str; + } + } + // Handle all other IPv6 addresses + // first run to find the longest contiguous zero words + for (word = 0; word < 8; word++) { + if (parts[word] === 0) { + if (word - lastzero > 1) { + len = 0; + } + lastzero = word; + len++; + } + if (len > longest) { + longest = len; + zstart = word - longest + 1; + } + } + for (word = 0; word < 8; word++) { + if (longest > 1) { + // compress contiguous zeros - to produce "::" + if ( + parts[word] === 0 && + word >= zstart && + word < zstart + longest + ) { + if (word === zstart) { + str += ':'; + if (zstart === 0) str += ':'; + } + continue; + } + } + // converts 16-bit words from big-endian to little-endian before converting to hex string + str += Number(_ntohs(parts[word] & 65535)).toString(16); + str += word < 7 ? ':' : ''; + } + return str; + }; + + var readSockaddr = (sa, salen) => { + // family / port offsets are common to both sockaddr_in and sockaddr_in6 + var family = HEAP16[sa >> 1]; + var port = _ntohs(HEAPU16[(sa + 2) >> 1]); + var addr; + switch (family) { + case 2: + if (salen !== 16) { + return { + errno: 28, + }; + } + addr = HEAP32[(sa + 4) >> 2]; + addr = inetNtop4(addr); + break; + + case 10: + if (salen !== 28) { + return { + errno: 28, + }; + } + addr = [ + HEAP32[(sa + 8) >> 2], + HEAP32[(sa + 12) >> 2], + HEAP32[(sa + 16) >> 2], + HEAP32[(sa + 20) >> 2], + ]; + addr = inetNtop6(addr); + break; + + default: + return { + errno: 5, + }; + } + return { + family, + addr, + port, + }; + }; + + var getSocketAddress = (addrp, addrlen) => { + var info = readSockaddr(addrp, addrlen); + if (info.errno) throw new FS.ErrnoError(info.errno); + info.addr = DNS.lookup_addr(info.addr) || info.addr; + return info; + }; + + function ___syscall_bind(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + var info = getSocketAddress(addr, addrlen); + sock.sock_ops.bind(sock, info.addr, info.port); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_bind.sig = 'iippiii'; + + var SYSCALLS = { + DEFAULT_POLLMASK: 5, + calculateAt(dirfd, path, allowEmpty) { + if (PATH.isAbs(path)) { + return path; + } + // relative path + var dir; + if (dirfd === -100) { + dir = FS.cwd(); + } else { + var dirstream = SYSCALLS.getStreamFromFD(dirfd); + dir = dirstream.path; + } + if (path.length == 0) { + if (!allowEmpty) { + throw new FS.ErrnoError(44); + } + return dir; + } + return dir + '/' + path; + }, + writeStat(buf, stat) { + HEAPU32[buf >> 2] = stat.dev; + HEAPU32[(buf + 4) >> 2] = stat.mode; + HEAPU32[(buf + 8) >> 2] = stat.nlink; + HEAPU32[(buf + 12) >> 2] = stat.uid; + HEAPU32[(buf + 16) >> 2] = stat.gid; + HEAPU32[(buf + 20) >> 2] = stat.rdev; + HEAP64[(buf + 24) >> 3] = BigInt(stat.size); + HEAP32[(buf + 32) >> 2] = 4096; + HEAP32[(buf + 36) >> 2] = stat.blocks; + var atime = stat.atime.getTime(); + var mtime = stat.mtime.getTime(); + var ctime = stat.ctime.getTime(); + HEAP64[(buf + 40) >> 3] = BigInt(Math.floor(atime / 1e3)); + HEAPU32[(buf + 48) >> 2] = (atime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 56) >> 3] = BigInt(Math.floor(mtime / 1e3)); + HEAPU32[(buf + 64) >> 2] = (mtime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 72) >> 3] = BigInt(Math.floor(ctime / 1e3)); + HEAPU32[(buf + 80) >> 2] = (ctime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 88) >> 3] = BigInt(stat.ino); + return 0; + }, + writeStatFs(buf, stats) { + HEAPU32[(buf + 4) >> 2] = stats.bsize; + HEAPU32[(buf + 60) >> 2] = stats.bsize; + HEAP64[(buf + 8) >> 3] = BigInt(stats.blocks); + HEAP64[(buf + 16) >> 3] = BigInt(stats.bfree); + HEAP64[(buf + 24) >> 3] = BigInt(stats.bavail); + HEAP64[(buf + 32) >> 3] = BigInt(stats.files); + HEAP64[(buf + 40) >> 3] = BigInt(stats.ffree); + HEAPU32[(buf + 48) >> 2] = stats.fsid; + HEAPU32[(buf + 64) >> 2] = stats.flags; + // ST_NOSUID + HEAPU32[(buf + 56) >> 2] = stats.namelen; + }, + doMsync(addr, stream, len, flags, offset) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError(43); + } + if (flags & 2) { + // MAP_PRIVATE calls need not to be synced back to underlying fs + return 0; + } + var buffer = HEAPU8.slice(addr, addr + len); + FS.msync(stream, buffer, offset, len, flags); + }, + getStreamFromFD(fd) { + var stream = FS.getStreamChecked(fd); + return stream; + }, + varargs: undefined, + getStr(ptr) { + var ret = UTF8ToString(ptr); + return ret; + }, + }; + + function ___syscall_chdir(path) { + try { + path = SYSCALLS.getStr(path); + FS.chdir(path); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_chdir.sig = 'ip'; + + function ___syscall_chmod(path, mode) { + try { + path = SYSCALLS.getStr(path); + FS.chmod(path, mode); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_chmod.sig = 'ipi'; + + var allocateUTF8OnStack = (...args) => stringToUTF8OnStack(...args); + + var onInits = []; + + var addOnInit = (cb) => onInits.push(cb); + + function _js_getpid() { + return PHPLoader.processId ?? 42; + } + + function _js_wasm_trace(format, ...args) { + if (PHPLoader.trace instanceof Function) { + PHPLoader.trace(_js_getpid(), format, ...args); + } + } + + var PHPWASM = { + O_APPEND: 1024, + O_NONBLOCK: 2048, + POLLHUP: 16, + SETFL_MASK: 3072, + init: function () { + // TODO: Move this to a library function that is made an onInit callback by the `__postset` suffix. + if (PHPLoader.bindUserSpace) { + /** + * We need to add an onInit callback to bind the user-space API + * because some dependencies like wasmImports and wasmExports + * are not yet assigned. + */ addOnInit(() => { + if (typeof PHPLoader.processId !== 'number') { + throw new Error( + 'PHPLoader.processId must be set before init' + ); + } + Module['userSpace'] = PHPLoader.bindUserSpace({ + pid: PHPLoader.processId, + constants: { + F_GETFL: Number('3'), + O_ACCMODE: Number('2097155'), + O_RDONLY: Number('0'), + O_WRONLY: Number('1'), + O_APPEND: Number('1024'), + O_NONBLOCK: Number('2048'), + F_SETFL: Number('4'), + F_GETLK: Number('12'), + F_SETLK: Number('13'), + F_SETLKW: Number('14'), + SEEK_SET: Number('0'), + SEEK_CUR: Number('1'), + SEEK_END: Number('2'), + F_GETFL: Number('3'), + O_ACCMODE: Number('2097155'), + O_RDONLY: Number('0'), + O_WRONLY: Number('1'), + O_APPEND: Number('1024'), + O_NONBLOCK: Number('2048'), + F_SETFL: Number('4'), + F_GETLK: Number('12'), + F_SETLK: Number('13'), + F_SETLKW: Number('14'), + SEEK_SET: Number('0'), + SEEK_CUR: Number('1'), + SEEK_END: Number('2'), + // From: + // https://github.com/emscripten-core/emscripten/blob/66d2137b0381ac35f7e2346b2d6a90abd0f1211a/system/lib/libc/musl/include/fcntl.h#L58-L60 + F_RDLCK: 0, + F_WRLCK: 1, + F_UNLCK: 2, + // From: + // https://github.com/emscripten-core/emscripten/blob/81bbaa42a7827d88a71bd89701245052c622428c/system/lib/libc/musl/include/sys/file.h#L7-L10 + LOCK_SH: 1, + LOCK_EX: 2, + LOCK_NB: 4, + // Non-blocking lock + LOCK_UN: 8, + }, + errnoCodes: ERRNO_CODES, + // Use get/set closures instead of exposing + // typed arrays directly. After memory.grow(), + // Emscripten's updateMemoryViews() reassigns + // the module-scoped HEAP* variables. Closures + // always reference the current value, so + // accesses are never stale. The get/set + // interface also prevents callers from + // capturing a typed array reference that + // could become stale. + memory: { + HEAP8: { + get(offset) { + return HEAP8[offset]; + }, + set(offset, value) { + HEAP8[offset] = value; + }, + }, + HEAPU8: { + get(offset) { + return HEAPU8[offset]; + }, + set(offset, value) { + HEAPU8[offset] = value; + }, + }, + HEAP16: { + get(offset) { + return HEAP16[offset]; + }, + set(offset, value) { + HEAP16[offset] = value; + }, + }, + HEAPU16: { + get(offset) { + return HEAPU16[offset]; + }, + set(offset, value) { + HEAPU16[offset] = value; + }, + }, + HEAP32: { + get(offset) { + return HEAP32[offset]; + }, + set(offset, value) { + HEAP32[offset] = value; + }, + }, + HEAPU32: { + get(offset) { + return HEAPU32[offset]; + }, + set(offset, value) { + HEAPU32[offset] = value; + }, + }, + HEAPF32: { + get(offset) { + return HEAPF32[offset]; + }, + set(offset, value) { + HEAPF32[offset] = value; + }, + }, + HEAP64: { + get(offset) { + return HEAP64[offset]; + }, + set(offset, value) { + HEAP64[offset] = value; + }, + }, + HEAPU64: { + get(offset) { + return HEAPU64[offset]; + }, + set(offset, value) { + HEAPU64[offset] = value; + }, + }, + HEAPF64: { + get(offset) { + return HEAPF64[offset]; + }, + set(offset, value) { + HEAPF64[offset] = value; + }, + }, + }, + wasmImports: Object.assign( + {}, + wasmImports, + typeof _builtin_fd_close === 'function' + ? { + builtin_fd_close: _builtin_fd_close, + } + : {}, + typeof _builtin_fcntl64 === 'function' + ? { + builtin_fcntl64: _builtin_fcntl64, + } + : {} + ), + wasmExports, + syscalls: SYSCALLS, + FS, + PROXYFS, + NODEFS, + }); + }); + } + Module['ENV'] = Module['ENV'] || {}; + // Ensure a platform-level bin directory for a fallback `php` binary. + Module['ENV']['PATH'] = [ + Module['ENV']['PATH'], + '/internal/shared/bin', + ] + .filter(Boolean) + .join(':'); + // The /request directory is required by the C module. It's where the + // stdout, stderr, and headers information are written for the JavaScript + // code to read later on. This is per-request state that is isolated to a + // single PHP process. + FS.mkdir('/request'); + // The /internal directory is shared amongst all PHP processes + // and contains the php.ini, constants definitions, etc. + FS.mkdir('/internal'); + if (PHPLoader.nativeInternalDirPath) { + FS.mount( + FS.filesystems.NODEFS, + { + root: PHPLoader.nativeInternalDirPath, + }, + '/internal' + ); + } + // The files from the shared directory are shared between all the + // PHP processes managed by PHPProcessManager. + FS.mkdirTree('/internal/shared'); + // The files from the preload directory are preloaded using the + // auto_prepend_file php.ini directive. + FS.mkdirTree('/internal/shared/preload'); + // Platform-level bin directory for a fallback `php` binary. Without it, + // PHP may not populate the PHP_BINARY constant. + FS.mkdirTree('/internal/shared/bin'); + const originalOnRuntimeInitialized = Module['onRuntimeInitialized']; + Module['onRuntimeInitialized'] = () => { + const { node: phpBinaryNode } = FS.lookupPath( + '/internal/shared/bin/php', + { + noent_okay: true, + } + ); + if (!phpBinaryNode) { + // Dummy PHP binary for PHP to populate the PHP_BINARY constant. + FS.writeFile( + '/internal/shared/bin/php', + new TextEncoder().encode('#!/bin/sh\nphp "$@"') + ); + // It must be executable to be used by PHP. + FS.chmod('/internal/shared/bin/php', 493); + } + originalOnRuntimeInitialized(); + }; + // Create stdout and stderr devices. We can't just use Emscripten's + // default stdout and stderr devices because they stop processing data + // on the first null byte. However, when dealing with binary data, + // null bytes are valid and common. + FS.registerDevice(FS.makedev(64, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onStdout(chunk); + return length; + }, + }); + FS.mkdev('/request/stdout', FS.makedev(64, 0)); + FS.registerDevice(FS.makedev(63, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onStderr(chunk); + return length; + }, + }); + FS.mkdev('/request/stderr', FS.makedev(63, 0)); + FS.registerDevice(FS.makedev(62, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onHeaders(chunk); + return length; + }, + }); + FS.mkdev('/request/headers', FS.makedev(62, 0)); + // Handle events. + PHPWASM.EventEmitter = ENVIRONMENT_IS_NODE + ? require('events').EventEmitter + : class EventEmitter { + constructor() { + this.listeners = {}; + } + emit(eventName, data) { + if (this.listeners[eventName]) { + this.listeners[eventName].forEach( + (callback) => { + callback(data); + } + ); + } + } + once(eventName, callback) { + const self = this; + function removedCallback() { + callback(...arguments); + self.removeListener(eventName, removedCallback); + } + this.on(eventName, removedCallback); + } + removeAllListeners(eventName) { + if (eventName) { + delete this.listeners[eventName]; + } else { + this.listeners = {}; + } + } + removeListener(eventName, callback) { + if (this.listeners[eventName]) { + const idx = + this.listeners[eventName].indexOf(callback); + if (idx !== -1) { + this.listeners[eventName].splice(idx, 1); + } + } + } + }; + PHPWASM.processTable = {}; + PHPWASM.input_devices = {}; + const originalWrite = TTY.stream_ops.write; + TTY.stream_ops.write = function (stream, ...rest) { + const retval = originalWrite(stream, ...rest); + // Implicit flush since PHP's fflush() doesn't seem to trigger the fsync event + // @TODO: Fix this at the wasm level + stream.tty.ops.fsync(stream.tty); + return retval; + }; + const originalPutChar = TTY.stream_ops.put_char; + TTY.stream_ops.put_char = function (tty, val) { + /** + * Buffer newlines that Emscripten normally ignores. + * + * Emscripten doesn't do it by default because its default + * print function is console.log that implicitly adds a newline. We are overwriting + * it with an environment-specific function that outputs exaclty what it was given, + * e.g. in Node.js it's process.stdout.write(). Therefore, we need to mak sure + * all the newlines make it to the output buffer. + */ if (val === 10) tty.output.push(val); + return originalPutChar(tty, val); + }; + }, + onHeaders: function (chunk) { + if (Module['onHeaders']) { + Module['onHeaders'](chunk); + return; + } + console.log('headers', { + chunk, + }); + }, + onStdout: function (chunk) { + if (Module['onStdout']) { + Module['onStdout'](chunk); + return; + } + if (ENVIRONMENT_IS_NODE) { + process.stdout.write(chunk); + } else { + console.log('stdout', { + chunk, + }); + } + }, + onStderr: function (chunk) { + if (Module['onStderr']) { + Module['onStderr'](chunk); + return; + } + if (ENVIRONMENT_IS_NODE) { + process.stderr.write(chunk); + } else { + console.warn('stderr', { + chunk, + }); + } + }, + getAllWebSockets: function (sock) { + const webSockets = new Set(); + if (sock.server) { + sock.server.clients.forEach((ws) => { + webSockets.add(ws); + }); + } + for (const peer of PHPWASM.getAllPeers(sock)) { + webSockets.add(peer.socket); + } + return Array.from(webSockets); + }, + getAllPeers: function (sock) { + const peers = new Set(); + if (sock.server) { + sock.pending + .filter((pending) => pending.peers) + .forEach((pending) => { + for (const peer of Object.values(pending.peers)) { + peers.add(peer); + } + }); + } + if (sock.peers) { + for (const peer of Object.values(sock.peers)) { + peers.add(peer); + } + } + return Array.from(peers); + }, + awaitData: function (ws) { + return PHPWASM.awaitEvent(ws, 'message'); + }, + awaitConnection: function (ws) { + if (ws.OPEN === ws.readyState) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'open'); + }, + awaitClose: function (ws) { + if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'close'); + }, + awaitError: function (ws) { + if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'error'); + }, + awaitEvent: function (ws, event) { + let resolve; + const listener = () => { + resolve(); + }; + const promise = new Promise(function (_resolve) { + resolve = _resolve; + ws.once(event, listener); + }); + const cancel = () => { + ws.removeListener(event, listener); + // Rejecting the promises bubbles up and kills the entire + // node process. Let's resolve them on the next tick instead + // to give the caller some space to unbind any handlers. + setTimeout(resolve); + }; + return [promise, cancel]; + }, + noop: function () {}, + spawnProcess: function (command, args, options) { + if (Module['spawnProcess']) { + const spawned = Module['spawnProcess']( + command, + args, + /** + * We're providing the same extra options we would pass to child_process.spawn(). + * + * Why? + * + * spawnProcess() follows the same interface as child_process.spawn() + * and some consumers pass `child_process.spawn` directly to php.setSpawnHandler() + */ { + ...options, + shell: true, + stdio: ['pipe', 'pipe', 'pipe'], + } + ); + if (spawned && !('then' in spawned) && 'on' in spawned) { + /** + * If we get the child process directly, return it immediately. + * Delaying it to the next tick via Promise.resolve() would create + * a race condition where it might emit some events before the + * caller has a chance to bind event listeners to them. + * + * Without this condition, this callback would be at least flaky: + * + * php.setSpawnHandler(require('child_process').spawn); + */ return spawned; + } + return Promise.resolve(spawned).then(function (spawned) { + if (!spawned || !spawned.on) { + throw new Error( + 'spawnProcess() must return an EventEmitter but returned a different type.' + ); + } + return spawned; + }); + } + const e = new Error( + 'popen(), proc_open() etc. are unsupported on this PHP instance. Call php.setSpawnHandler() ' + + 'and provide a callback to handle spawning processes, or disable a popen(), proc_open() ' + + 'and similar functions via php.ini.' + ); + e.code = 'SPAWN_UNSUPPORTED'; + throw e; + }, + shutdownSocket: function (socketd, how) { + // This implementation only supports websockets at the moment + const sock = getSocketFromFD(socketd); + const peer = Object.values(sock.peers)[0]; + if (!peer) { + return -1; + } + try { + peer.socket.close(); + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + return 0; + } catch (e) { + console.log('Socket shutdown error', e); + return -1; + } + }, + }; + + function _wasm_connect(sockfd, addr, addrlen) { + /** + * Use a synchronous connect() call when Asyncify is used. + * + * The async version was originally introduced to support the Memcached and Redis extensions, + * and both are only available with JSPI. Asyncify is too difficult to maintain and + * it's not getting that upgrade. + */ if (!('Suspending' in WebAssembly)) { + var sock = getSocketFromFD(sockfd); + var info = getSocketAddress(addr, addrlen); + sock.sock_ops.connect(sock, info.addr, info.port); + return 0; + } + return Asyncify.handleSleep((wakeUp) => { + // Get the socket + let sock; + try { + sock = getSocketFromFD(sockfd); + } catch (e) { + wakeUp(-ERRNO_CODES.EBADF); + return; + } + if (!sock) { + wakeUp(-ERRNO_CODES.EBADF); + return; + } + // Parse the address + let info; + try { + info = getSocketAddress(addr, addrlen); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + wakeUp(-ERRNO_CODES.EFAULT); + return; + } + wakeUp(-e.errno); + return; + } + // Perform the connect (this creates the WebSocket but doesn't wait) + try { + sock.sock_ops.connect(sock, info.addr, info.port); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + wakeUp(-e.errno); + return; + } + // Get all websockets for this socket + const webSockets = PHPWASM.getAllWebSockets(sock); + if (!webSockets.length) { + // No WebSocket yet, this shouldn't happen after connect + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + const ws = webSockets[0]; + // If already connected, return success + if (ws.readyState === ws.OPEN) { + wakeUp(0); + return; + } + // If already closed or closing, return error + if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) { + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + // Wait for the connection to be established + const timeout = 3e4; + // 30 second timeout + let resolved = false; + const timeoutId = setTimeout(() => { + if (!resolved) { + resolved = true; + wakeUp(-ERRNO_CODES.ETIMEDOUT); + } + }, timeout); + const handleOpen = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('error', handleError); + ws.removeEventListener('close', handleClose); + wakeUp(0); + } + }; + const handleError = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('open', handleOpen); + ws.removeEventListener('close', handleClose); + wakeUp(-ERRNO_CODES.ECONNREFUSED); + } + }; + const handleClose = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('open', handleOpen); + ws.removeEventListener('error', handleError); + wakeUp(-ERRNO_CODES.ECONNREFUSED); + } + }; + ws.addEventListener('open', handleOpen); + ws.addEventListener('error', handleError); + ws.addEventListener('close', handleClose); + }); + } + + function ___syscall_connect(sockfd, addr, addrlen, d1, d2, d3) { + return _wasm_connect(sockfd, addr, addrlen); + } + + ___syscall_connect.sig = 'iippiii'; + + function ___syscall_dup(fd) { + try { + var old = SYSCALLS.getStreamFromFD(fd); + return FS.dupStream(old).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_dup.sig = 'ii'; + + function ___syscall_dup3(fd, newfd, flags) { + try { + var old = SYSCALLS.getStreamFromFD(fd); + if (old.fd === newfd) return -28; + // Check newfd is within range of valid open file descriptors. + if (newfd < 0 || newfd >= FS.MAX_OPEN_FDS) return -8; + var existing = FS.getStream(newfd); + if (existing) FS.close(existing); + return FS.dupStream(old, newfd).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_dup3.sig = 'iiii'; + + function ___syscall_faccessat(dirfd, path, amode, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (amode & ~7) { + // need a valid mode + return -28; + } + var lookup = FS.lookupPath(path, { + follow: true, + }); + var node = lookup.node; + if (!node) { + return -44; + } + var perms = ''; + if (amode & 4) perms += 'r'; + if (amode & 2) perms += 'w'; + if (amode & 1) perms += 'x'; + if (perms && FS.nodePermissions(node, perms)) { + return -2; + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_faccessat.sig = 'iipii'; + + function ___syscall_fchmod(fd, mode) { + try { + FS.fchmod(fd, mode); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_fchmod.sig = 'iii'; + + function ___syscall_fchown32(fd, owner, group) { + try { + FS.fchown(fd, owner, group); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_fchown32.sig = 'iiii'; + + function ___syscall_fchownat(dirfd, path, owner, group, flags) { + try { + path = SYSCALLS.getStr(path); + var nofollow = flags & 256; + flags = flags & ~256; + path = SYSCALLS.calculateAt(dirfd, path); + (nofollow ? FS.lchown : FS.chown)(path, owner, group); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_fchownat.sig = 'iipiii'; + + var syscallGetVarargI = () => { + // the `+` prepended here is necessary to convince the JSCompiler that varargs is indeed a number. + var ret = HEAP32[+SYSCALLS.varargs >> 2]; + SYSCALLS.varargs += 4; + return ret; + }; + + var syscallGetVarargP = syscallGetVarargI; + + function _fd_close(fd) { + if (typeof Module['userSpace'] === 'undefined') { + return _builtin_fd_close(fd); + } + return Module['userSpace'].fd_close(fd); + } + + _fd_close.sig = 'ii'; + + function _builtin_fd_close(fd) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + FS.close(stream); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + + function _builtin_fcntl64(fd, cmd, varargs) { + SYSCALLS.varargs = varargs; + try { + var stream = SYSCALLS.getStreamFromFD(fd); + switch (cmd) { + case 0: { + var arg = syscallGetVarargI(); + if (arg < 0) { + return -28; + } + while (FS.streams[arg]) { + arg++; + } + var newStream; + newStream = FS.dupStream(stream, arg); + return newStream.fd; + } + + case 1: + case 2: + return 0; + + // FD_CLOEXEC makes no sense for a single process. + case 3: + return stream.flags; + + case 4: { + var arg = syscallGetVarargI(); + stream.flags |= arg; + return 0; + } + + case 12: { + var arg = syscallGetVarargP(); + var offset = 0; + // We're always unlocked. + HEAP16[(arg + offset) >> 1] = 2; + return 0; + } + + case 13: + case 14: + // Pretend that the locking is successful. These are process-level locks, + // and Emscripten programs are a single process. If we supported linking a + // filesystem between programs, we'd need to do more here. + // See https://github.com/emscripten-core/emscripten/issues/23697 + return 0; + } + return -28; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + function ___syscall_fcntl64(fd, cmd, varargs) { + if (typeof Module['userSpace'] === 'undefined') { + return _builtin_fcntl64(fd, cmd, varargs); + } + return Module['userSpace'].fcntl64(fd, cmd, varargs); + } + + ___syscall_fcntl64.sig = 'iiip'; + + function ___syscall_fstat64(fd, buf) { + try { + return SYSCALLS.writeStat(buf, FS.fstat(fd)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_fstat64.sig = 'iip'; + + var INT53_MAX = 9007199254740992; + + var INT53_MIN = -9007199254740992; + + var bigintToI53Checked = (num) => + num < INT53_MIN || num > INT53_MAX ? NaN : Number(num); + + function ___syscall_ftruncate64(fd, length) { + length = bigintToI53Checked(length); + try { + if (isNaN(length)) return -61; + FS.ftruncate(fd, length); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_ftruncate64.sig = 'iij'; + + function ___syscall_getcwd(buf, size) { + try { + if (size === 0) return -28; + var cwd = FS.cwd(); + var cwdLengthInBytes = lengthBytesUTF8(cwd) + 1; + if (size < cwdLengthInBytes) return -68; + stringToUTF8(cwd, buf, size); + return cwdLengthInBytes; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_getcwd.sig = 'ipp'; + + function ___syscall_getdents64(fd, dirp, count) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + stream.getdents ||= FS.readdir(stream.path); + var struct_size = 280; + var pos = 0; + var off = FS.llseek(stream, 0, 1); + var startIdx = Math.floor(off / struct_size); + var endIdx = Math.min( + stream.getdents.length, + startIdx + Math.floor(count / struct_size) + ); + for (var idx = startIdx; idx < endIdx; idx++) { + var id; + var type; + var name = stream.getdents[idx]; + if (name === '.') { + id = stream.node.id; + type = 4; + } else if (name === '..') { + var lookup = FS.lookupPath(stream.path, { + parent: true, + }); + id = lookup.node.id; + type = 4; + } else { + var child; + try { + child = FS.lookupNode(stream.node, name); + } catch (e) { + // If the entry is not a directory, file, or symlink, nodefs + // lookupNode will raise EINVAL. Skip these and continue. + if (e?.errno === 28) { + continue; + } + throw e; + } + id = child.id; + type = FS.isChrdev(child.mode) + ? 2 // DT_CHR, character device. + : FS.isDir(child.mode) + ? 4 // DT_DIR, directory. + : FS.isLink(child.mode) + ? 10 // DT_LNK, symbolic link. + : 8; + } + HEAP64[(dirp + pos) >> 3] = BigInt(id); + HEAP64[(dirp + pos + 8) >> 3] = BigInt((idx + 1) * struct_size); + HEAP16[(dirp + pos + 16) >> 1] = 280; + HEAP8[dirp + pos + 18] = type; + stringToUTF8(name, dirp + pos + 19, 256); + pos += struct_size; + } + FS.llseek(stream, idx * struct_size, 0); + return pos; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_getdents64.sig = 'iipp'; + + function ___syscall_getpeername(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + if (!sock.daddr) { + return -53; + } + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(sock.daddr), + sock.dport, + addrlen + ); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_getpeername.sig = 'iippiii'; + + function ___syscall_getsockname(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + // TODO: sock.saddr should never be undefined, see TODO in websocket_sock_ops.getname + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(sock.saddr || '0.0.0.0'), + sock.sport, + addrlen + ); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_getsockname.sig = 'iippiii'; + + function ___syscall_getsockopt(fd, level, optname, optval, optlen, d1) { + try { + var sock = getSocketFromFD(fd); + // Minimal getsockopt aimed at resolving https://github.com/emscripten-core/emscripten/issues/2211 + // so only supports SOL_SOCKET with SO_ERROR. + if (level === 1) { + if (optname === 4) { + HEAP32[optval >> 2] = sock.error; + HEAP32[optlen >> 2] = 4; + sock.error = null; + // Clear the error (The SO_ERROR option obtains and then clears this field). + return 0; + } + } + return -50; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_getsockopt.sig = 'iiiippi'; + + function ___syscall_ioctl(fd, op, varargs) { + SYSCALLS.varargs = varargs; + try { + var stream = SYSCALLS.getStreamFromFD(fd); + switch (op) { + case 21509: { + if (!stream.tty) return -59; + return 0; + } + + case 21505: { + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tcgets) { + var termios = stream.tty.ops.ioctl_tcgets(stream); + var argp = syscallGetVarargP(); + HEAP32[argp >> 2] = termios.c_iflag || 0; + HEAP32[(argp + 4) >> 2] = termios.c_oflag || 0; + HEAP32[(argp + 8) >> 2] = termios.c_cflag || 0; + HEAP32[(argp + 12) >> 2] = termios.c_lflag || 0; + for (var i = 0; i < 32; i++) { + HEAP8[argp + i + 17] = termios.c_cc[i] || 0; + } + return 0; + } + return 0; + } + + case 21510: + case 21511: + case 21512: { + if (!stream.tty) return -59; + return 0; + } + + case 21506: + case 21507: + case 21508: { + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tcsets) { + var argp = syscallGetVarargP(); + var c_iflag = HEAP32[argp >> 2]; + var c_oflag = HEAP32[(argp + 4) >> 2]; + var c_cflag = HEAP32[(argp + 8) >> 2]; + var c_lflag = HEAP32[(argp + 12) >> 2]; + var c_cc = []; + for (var i = 0; i < 32; i++) { + c_cc.push(HEAP8[argp + i + 17]); + } + return stream.tty.ops.ioctl_tcsets(stream.tty, op, { + c_iflag, + c_oflag, + c_cflag, + c_lflag, + c_cc, + }); + } + return 0; + } + + case 21519: { + if (!stream.tty) return -59; + var argp = syscallGetVarargP(); + HEAP32[argp >> 2] = 0; + return 0; + } + + case 21520: { + if (!stream.tty) return -59; + return -28; + } + + case 21537: + case 21531: { + var argp = syscallGetVarargP(); + return FS.ioctl(stream, op, argp); + } + + case 21523: { + // TODO: in theory we should write to the winsize struct that gets + // passed in, but for now musl doesn't read anything on it + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tiocgwinsz) { + var winsize = stream.tty.ops.ioctl_tiocgwinsz( + stream.tty + ); + var argp = syscallGetVarargP(); + HEAP16[argp >> 1] = winsize[0]; + HEAP16[(argp + 2) >> 1] = winsize[1]; + } + return 0; + } + + case 21524: { + // TODO: technically, this ioctl call should change the window size. + // but, since emscripten doesn't have any concept of a terminal window + // yet, we'll just silently throw it away as we do TIOCGWINSZ + if (!stream.tty) return -59; + return 0; + } + + case 21515: { + if (!stream.tty) return -59; + return 0; + } + + default: + return -28; + } + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_ioctl.sig = 'iiip'; + + function ___syscall_listen(fd, backlog) { + try { + var sock = getSocketFromFD(fd); + sock.sock_ops.listen(sock, backlog); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_listen.sig = 'iiiiiii'; + + function ___syscall_lstat64(path, buf) { + try { + path = SYSCALLS.getStr(path); + return SYSCALLS.writeStat(buf, FS.lstat(path)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_lstat64.sig = 'ipp'; + + function ___syscall_mkdirat(dirfd, path, mode) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + FS.mkdir(path, mode, 0); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_mkdirat.sig = 'iipi'; + + function ___syscall_newfstatat(dirfd, path, buf, flags) { + try { + path = SYSCALLS.getStr(path); + var nofollow = flags & 256; + var allowEmpty = flags & 4096; + flags = flags & ~6400; + path = SYSCALLS.calculateAt(dirfd, path, allowEmpty); + return SYSCALLS.writeStat( + buf, + nofollow ? FS.lstat(path) : FS.stat(path) + ); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_newfstatat.sig = 'iippi'; + + function ___syscall_openat(dirfd, path, flags, varargs) { + SYSCALLS.varargs = varargs; + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + var mode = varargs ? syscallGetVarargI() : 0; + return FS.open(path, flags, mode).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_openat.sig = 'iipip'; + + var PIPEFS = { + BUCKET_BUFFER_SIZE: 8192, + mount(mount) { + // Do not pollute the real root directory or its child nodes with pipes + // Looks like it is OK to create another pseudo-root node not linked to the FS.root hierarchy this way + return FS.createNode(null, '/', 16384 | 511, 0); + }, + createPipe() { + var pipe = { + buckets: [], + // refcnt 2 because pipe has a read end and a write end. We need to be + // able to read from the read end after write end is closed. + refcnt: 2, + timestamp: new Date(), + }; + pipe.buckets.push({ + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: 0, + roffset: 0, + }); + var rName = PIPEFS.nextname(); + var wName = PIPEFS.nextname(); + var rNode = FS.createNode(PIPEFS.root, rName, 4096, 0); + var wNode = FS.createNode(PIPEFS.root, wName, 4096, 0); + rNode.pipe = pipe; + wNode.pipe = pipe; + var readableStream = FS.createStream({ + path: rName, + node: rNode, + flags: 0, + seekable: false, + stream_ops: PIPEFS.stream_ops, + }); + rNode.stream = readableStream; + var writableStream = FS.createStream({ + path: wName, + node: wNode, + flags: 1, + seekable: false, + stream_ops: PIPEFS.stream_ops, + }); + wNode.stream = writableStream; + return { + readable_fd: readableStream.fd, + writable_fd: writableStream.fd, + }; + }, + stream_ops: { + getattr(stream) { + var node = stream.node; + var timestamp = node.pipe.timestamp; + return { + dev: 14, + ino: node.id, + mode: 4480, + nlink: 1, + uid: 0, + gid: 0, + rdev: 0, + size: 0, + atime: timestamp, + mtime: timestamp, + ctime: timestamp, + blksize: 4096, + blocks: 0, + }; + }, + poll(stream) { + var pipe = stream.node.pipe; + if ((stream.flags & 2097155) === 1) { + return 256 | 4; + } + for (var bucket of pipe.buckets) { + if (bucket.offset - bucket.roffset > 0) { + return 64 | 1; + } + } + return 0; + }, + dup(stream) { + stream.node.pipe.refcnt++; + }, + ioctl(stream, request, varargs) { + return 28; + }, + fsync(stream) { + return 28; + }, + read(stream, buffer, offset, length, position) { + var pipe = stream.node.pipe; + var currentLength = 0; + for (var bucket of pipe.buckets) { + currentLength += bucket.offset - bucket.roffset; + } + var data = buffer.subarray(offset, offset + length); + if (length <= 0) { + return 0; + } + if (currentLength == 0) { + if (pipe.refcnt < 2) { + return 0; + } + throw new FS.ErrnoError(6); + } + var toRead = Math.min(currentLength, length); + var totalRead = toRead; + var toRemove = 0; + for (var bucket of pipe.buckets) { + var bucketSize = bucket.offset - bucket.roffset; + if (toRead <= bucketSize) { + var tmpSlice = bucket.buffer.subarray( + bucket.roffset, + bucket.offset + ); + if (toRead < bucketSize) { + tmpSlice = tmpSlice.subarray(0, toRead); + bucket.roffset += toRead; + } else { + toRemove++; + } + data.set(tmpSlice); + break; + } else { + var tmpSlice = bucket.buffer.subarray( + bucket.roffset, + bucket.offset + ); + data.set(tmpSlice); + data = data.subarray(tmpSlice.byteLength); + toRead -= tmpSlice.byteLength; + toRemove++; + } + } + if (toRemove && toRemove == pipe.buckets.length) { + // Do not generate excessive garbage in use cases such as + // write several bytes, read everything, write several bytes, read everything... + toRemove--; + pipe.buckets[toRemove].offset = 0; + pipe.buckets[toRemove].roffset = 0; + } + pipe.buckets.splice(0, toRemove); + return totalRead; + }, + write(stream, buffer, offset, length, position) { + var pipe = stream.node.pipe; + var data = buffer.subarray(offset, offset + length); + var dataLen = data.byteLength; + if (dataLen <= 0) { + return 0; + } + var currBucket = null; + if (pipe.buckets.length == 0) { + currBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: 0, + roffset: 0, + }; + pipe.buckets.push(currBucket); + } else { + currBucket = pipe.buckets[pipe.buckets.length - 1]; + } + var freeBytesInCurrBuffer = + PIPEFS.BUCKET_BUFFER_SIZE - currBucket.offset; + if (freeBytesInCurrBuffer >= dataLen) { + currBucket.buffer.set(data, currBucket.offset); + currBucket.offset += dataLen; + return dataLen; + } else if (freeBytesInCurrBuffer > 0) { + currBucket.buffer.set( + data.subarray(0, freeBytesInCurrBuffer), + currBucket.offset + ); + currBucket.offset += freeBytesInCurrBuffer; + data = data.subarray( + freeBytesInCurrBuffer, + data.byteLength + ); + } + var numBuckets = + (data.byteLength / PIPEFS.BUCKET_BUFFER_SIZE) | 0; + var remElements = data.byteLength % PIPEFS.BUCKET_BUFFER_SIZE; + for (var i = 0; i < numBuckets; i++) { + var newBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: PIPEFS.BUCKET_BUFFER_SIZE, + roffset: 0, + }; + pipe.buckets.push(newBucket); + newBucket.buffer.set( + data.subarray(0, PIPEFS.BUCKET_BUFFER_SIZE) + ); + data = data.subarray( + PIPEFS.BUCKET_BUFFER_SIZE, + data.byteLength + ); + } + if (remElements > 0) { + var newBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: data.byteLength, + roffset: 0, + }; + pipe.buckets.push(newBucket); + newBucket.buffer.set(data); + } + return dataLen; + }, + close(stream) { + var pipe = stream.node.pipe; + pipe.refcnt--; + if (pipe.refcnt === 0) { + pipe.buckets = null; + } + }, + }, + nextname() { + if (!PIPEFS.nextname.current) { + PIPEFS.nextname.current = 0; + } + return 'pipe[' + PIPEFS.nextname.current++ + ']'; + }, + }; + + function ___syscall_pipe(fdPtr) { + try { + if (fdPtr == 0) { + throw new FS.ErrnoError(21); + } + var res = PIPEFS.createPipe(); + HEAP32[fdPtr >> 2] = res.readable_fd; + HEAP32[(fdPtr + 4) >> 2] = res.writable_fd; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_pipe.sig = 'ip'; + + function ___syscall_readlinkat(dirfd, path, buf, bufsize) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (bufsize <= 0) return -28; + var ret = FS.readlink(path); + var len = Math.min(bufsize, lengthBytesUTF8(ret)); + var endChar = HEAP8[buf + len]; + stringToUTF8(ret, buf, bufsize + 1); + // readlink is one of the rare functions that write out a C string, but does never append a null to the output buffer(!) + // stringToUTF8() always appends a null byte, so restore the character under the null byte after the write. + HEAP8[buf + len] = endChar; + return len; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_readlinkat.sig = 'iippp'; + + function ___syscall_recvfrom(fd, buf, len, flags, addr, addrlen) { + try { + var sock = getSocketFromFD(fd); + var msg = sock.sock_ops.recvmsg( + sock, + len, + typeof flags !== 'undefined' ? flags : 0 + ); + if (!msg) return 0; + // socket is closed + if (addr) { + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(msg.addr), + msg.port, + addrlen + ); + } + HEAPU8.set(msg.buffer, buf); + return msg.buffer.byteLength; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_recvfrom.sig = 'iippipp'; + + function ___syscall_renameat(olddirfd, oldpath, newdirfd, newpath) { + try { + oldpath = SYSCALLS.getStr(oldpath); + newpath = SYSCALLS.getStr(newpath); + oldpath = SYSCALLS.calculateAt(olddirfd, oldpath); + newpath = SYSCALLS.calculateAt(newdirfd, newpath); + FS.rename(oldpath, newpath); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_renameat.sig = 'iipip'; + + function ___syscall_rmdir(path) { + try { + path = SYSCALLS.getStr(path); + FS.rmdir(path); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_rmdir.sig = 'ip'; + + function ___syscall_sendto(fd, message, length, flags, addr, addr_len) { + try { + var sock = getSocketFromFD(fd); + if (!addr) { + // send, no address provided + return FS.write(sock.stream, HEAP8, message, length); + } + var dest = getSocketAddress(addr, addr_len); + // sendto an address + return sock.sock_ops.sendmsg( + sock, + HEAP8, + message, + length, + dest.addr, + dest.port + ); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_sendto.sig = 'iippipp'; + + function ___syscall_socket(domain, type, protocol) { + try { + var sock = SOCKFS.createSocket(domain, type, protocol); + return sock.stream.fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_socket.sig = 'iiiiiii'; + + function ___syscall_stat64(path, buf) { + try { + path = SYSCALLS.getStr(path); + return SYSCALLS.writeStat(buf, FS.stat(path)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_stat64.sig = 'ipp'; + + function ___syscall_statfs64(path, size, buf) { + try { + SYSCALLS.writeStatFs(buf, FS.statfs(SYSCALLS.getStr(path))); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_statfs64.sig = 'ippp'; + + function ___syscall_symlinkat(target, dirfd, linkpath) { + try { + target = SYSCALLS.getStr(target); + linkpath = SYSCALLS.getStr(linkpath); + linkpath = SYSCALLS.calculateAt(dirfd, linkpath); + FS.symlink(target, linkpath); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_symlinkat.sig = 'ipip'; + + function ___syscall_unlinkat(dirfd, path, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (!flags) { + FS.unlink(path); + } else if (flags === 512) { + FS.rmdir(path); + } else { + return -28; + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_unlinkat.sig = 'iipi'; + + var readI53FromI64 = (ptr) => + HEAPU32[ptr >> 2] + HEAP32[(ptr + 4) >> 2] * 4294967296; + + function ___syscall_utimensat(dirfd, path, times, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path, true); + var now = Date.now(), + atime, + mtime; + if (!times) { + atime = now; + mtime = now; + } else { + var seconds = readI53FromI64(times); + var nanoseconds = HEAP32[(times + 8) >> 2]; + if (nanoseconds == 1073741823) { + atime = now; + } else if (nanoseconds == 1073741822) { + atime = null; + } else { + atime = seconds * 1e3 + nanoseconds / (1e3 * 1e3); + } + times += 16; + seconds = readI53FromI64(times); + nanoseconds = HEAP32[(times + 8) >> 2]; + if (nanoseconds == 1073741823) { + mtime = now; + } else if (nanoseconds == 1073741822) { + mtime = null; + } else { + mtime = seconds * 1e3 + nanoseconds / (1e3 * 1e3); + } + } + // null here means UTIME_OMIT was passed. If both were set to UTIME_OMIT then + // we can skip the call completely. + if ((mtime ?? atime) !== null) { + FS.utime(path, atime, mtime); + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + ___syscall_utimensat.sig = 'iippi'; + + var __abort_js = () => abort(''); + + __abort_js.sig = 'v'; + + var dlSetError = (msg) => { + var sp = stackSave(); + var cmsg = stringToUTF8OnStack(msg); + ___dl_seterr(cmsg, 0); + stackRestore(sp); + }; + + var dlopenInternal = (handle, jsflags) => { + // void *dlopen(const char *file, int mode); + // http://pubs.opengroup.org/onlinepubs/009695399/functions/dlopen.html + var filename = UTF8ToString(handle + 36); + var flags = HEAP32[(handle + 4) >> 2]; + filename = PATH.normalize(filename); + var global = Boolean(flags & 256); + var localScope = global ? null : {}; + // We don't care about RTLD_NOW and RTLD_LAZY. + var combinedFlags = { + global, + nodelete: Boolean(flags & 4096), + loadAsync: jsflags.loadAsync, + }; + if (jsflags.loadAsync) { + return loadDynamicLibrary( + filename, + combinedFlags, + localScope, + handle + ); + } + try { + return loadDynamicLibrary( + filename, + combinedFlags, + localScope, + handle + ); + } catch (e) { + dlSetError(`could not load dynamic lib: ${filename}\n${e}`); + return 0; + } + }; + + function __dlopen_js(handle) { + var jsflags = { + loadAsync: false, + }; + return dlopenInternal(handle, jsflags); + } + + __dlopen_js.sig = 'pp'; + + var __dlsym_js = (handle, symbol, symbolIndex) => { + // void *dlsym(void *restrict handle, const char *restrict name); + // http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html + symbol = UTF8ToString(symbol); + var result; + var newSymIndex; + var lib = LDSO.loadedLibsByHandle[handle]; + newSymIndex = Object.keys(lib.exports).indexOf(symbol); + if (newSymIndex == -1 || lib.exports[symbol].stub) { + dlSetError( + `Tried to lookup unknown symbol "${symbol}" in dynamic lib: ${lib.name}` + ); + return 0; + } + result = lib.exports[symbol]; + if (typeof result == 'function') { + // Asyncify wraps exports, and we need to look through those wrappers. + if (result.orig) { + result = result.orig; + } + var addr = getFunctionAddress(result); + if (addr) { + result = addr; + } else { + // Insert the function into the wasm table. If its a direct wasm + // function the second argument will not be needed. If its a JS + // function we rely on the `sig` attribute being set based on the + // `__sig` specified in library JS file. + result = addFunction(result, result.sig); + HEAPU32[symbolIndex >> 2] = newSymIndex; + } + } + return result; + }; + + __dlsym_js.sig = 'pppp'; + + var __emscripten_lookup_name = (name) => { + // uint32_t _emscripten_lookup_name(const char *name); + var nameString = UTF8ToString(name); + return inetPton4(DNS.lookup_name(nameString)); + }; + + __emscripten_lookup_name.sig = 'ip'; + + var runtimeKeepaliveCounter = 0; + + var __emscripten_runtime_keepalive_clear = () => { + noExitRuntime = false; + runtimeKeepaliveCounter = 0; + }; + + __emscripten_runtime_keepalive_clear.sig = 'v'; + + function __gmtime_js(time, tmPtr) { + time = bigintToI53Checked(time); + var date = new Date(time * 1e3); + HEAP32[tmPtr >> 2] = date.getUTCSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getUTCMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getUTCHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getUTCDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getUTCMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getUTCFullYear() - 1900; + HEAP32[(tmPtr + 24) >> 2] = date.getUTCDay(); + var start = Date.UTC(date.getUTCFullYear(), 0, 1, 0, 0, 0, 0); + var yday = ((date.getTime() - start) / (1e3 * 60 * 60 * 24)) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + } + + __gmtime_js.sig = 'vjp'; + + var isLeapYear = (year) => + year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); + + var MONTH_DAYS_LEAP_CUMULATIVE = [ + 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, + ]; + + var MONTH_DAYS_REGULAR_CUMULATIVE = [ + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, + ]; + + var ydayFromDate = (date) => { + var leap = isLeapYear(date.getFullYear()); + var monthDaysCumulative = leap + ? MONTH_DAYS_LEAP_CUMULATIVE + : MONTH_DAYS_REGULAR_CUMULATIVE; + var yday = monthDaysCumulative[date.getMonth()] + date.getDate() - 1; + // -1 since it's days since Jan 1 + return yday; + }; + + function __localtime_js(time, tmPtr) { + time = bigintToI53Checked(time); + var date = new Date(time * 1e3); + HEAP32[tmPtr >> 2] = date.getSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getFullYear() - 1900; + HEAP32[(tmPtr + 24) >> 2] = date.getDay(); + var yday = ydayFromDate(date) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + HEAP32[(tmPtr + 36) >> 2] = -(date.getTimezoneOffset() * 60); + // Attention: DST is in December in South, and some regions don't have DST at all. + var start = new Date(date.getFullYear(), 0, 1); + var summerOffset = new Date( + date.getFullYear(), + 6, + 1 + ).getTimezoneOffset(); + var winterOffset = start.getTimezoneOffset(); + var dst = + (summerOffset != winterOffset && + date.getTimezoneOffset() == + Math.min(winterOffset, summerOffset)) | 0; + HEAP32[(tmPtr + 32) >> 2] = dst; + } + + __localtime_js.sig = 'vjp'; + + var __mktime_js = function (tmPtr) { + var ret = (() => { + var date = new Date( + HEAP32[(tmPtr + 20) >> 2] + 1900, + HEAP32[(tmPtr + 16) >> 2], + HEAP32[(tmPtr + 12) >> 2], + HEAP32[(tmPtr + 8) >> 2], + HEAP32[(tmPtr + 4) >> 2], + HEAP32[tmPtr >> 2], + 0 + ); + // There's an ambiguous hour when the time goes back; the tm_isdst field is + // used to disambiguate it. Date() basically guesses, so we fix it up if it + // guessed wrong, or fill in tm_isdst with the guess if it's -1. + var dst = HEAP32[(tmPtr + 32) >> 2]; + var guessedOffset = date.getTimezoneOffset(); + var start = new Date(date.getFullYear(), 0, 1); + var summerOffset = new Date( + date.getFullYear(), + 6, + 1 + ).getTimezoneOffset(); + var winterOffset = start.getTimezoneOffset(); + var dstOffset = Math.min(winterOffset, summerOffset); + // DST is in December in South + if (dst < 0) { + // Attention: some regions don't have DST at all. + HEAP32[(tmPtr + 32) >> 2] = Number( + summerOffset != winterOffset && dstOffset == guessedOffset + ); + } else if (dst > 0 != (dstOffset == guessedOffset)) { + var nonDstOffset = Math.max(winterOffset, summerOffset); + var trueOffset = dst > 0 ? dstOffset : nonDstOffset; + // Don't try setMinutes(date.getMinutes() + ...) -- it's messed up. + date.setTime( + date.getTime() + (trueOffset - guessedOffset) * 6e4 + ); + } + HEAP32[(tmPtr + 24) >> 2] = date.getDay(); + var yday = ydayFromDate(date) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + // To match expected behavior, update fields from date + HEAP32[tmPtr >> 2] = date.getSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getYear(); + var timeMs = date.getTime(); + if (isNaN(timeMs)) { + return -1; + } + // Return time in microseconds + return timeMs / 1e3; + })(); + return BigInt(ret); + }; + + __mktime_js.sig = 'jp'; + + function __mmap_js(len, prot, flags, fd, offset, allocated, addr) { + offset = bigintToI53Checked(offset); + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var res = FS.mmap(stream, len, offset, prot, flags); + var ptr = res.ptr; + HEAP32[allocated >> 2] = res.allocated; + HEAPU32[addr >> 2] = ptr; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + __mmap_js.sig = 'ipiiijpp'; + + function __munmap_js(addr, len, prot, flags, fd, offset) { + offset = bigintToI53Checked(offset); + try { + var stream = SYSCALLS.getStreamFromFD(fd); + if (prot & 2) { + SYSCALLS.doMsync(addr, stream, len, flags, offset); + } + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + + __munmap_js.sig = 'ippiiij'; + + var timers = {}; + + var handleException = (e) => { + // Certain exception types we do not treat as errors since they are used for + // internal control flow. + // 1. ExitStatus, which is thrown by exit() + // 2. "unwind", which is thrown by emscripten_unwind_to_js_event_loop() and others + // that wish to return to JS event loop. + if (e instanceof ExitStatus || e == 'unwind') { + return EXITSTATUS; + } + quit_(1, e); + }; + + var keepRuntimeAlive = () => noExitRuntime || runtimeKeepaliveCounter > 0; + + var _proc_exit = (code) => { + EXITSTATUS = code; + if (!keepRuntimeAlive()) { + Module['onExit']?.(code); + ABORT = true; + } + quit_(code, new ExitStatus(code)); + }; + + _proc_exit.sig = 'vi'; + + /** @param {boolean|number=} implicit */ var exitJS = ( + status, + implicit + ) => { + EXITSTATUS = status; + if (!keepRuntimeAlive()) { + exitRuntime(); + } + _proc_exit(status); + }; + + var _exit = exitJS; + + _exit.sig = 'vi'; + + var maybeExit = () => { + if (runtimeExited) { + return; + } + if (!keepRuntimeAlive()) { + try { + _exit(EXITSTATUS); + } catch (e) { + handleException(e); + } + } + }; + + var callUserCallback = (func) => { + if (runtimeExited || ABORT) { + return; + } + try { + func(); + maybeExit(); + } catch (e) { + handleException(e); + } + }; + + var _emscripten_get_now = () => performance.now(); + + _emscripten_get_now.sig = 'd'; + + var __setitimer_js = (which, timeout_ms) => { + // First, clear any existing timer. + if (timers[which]) { + clearTimeout(timers[which].id); + delete timers[which]; + } + // A timeout of zero simply cancels the current timeout so we have nothing + // more to do. + if (!timeout_ms) return 0; + var id = setTimeout(() => { + delete timers[which]; + callUserCallback(() => + __emscripten_timeout(which, _emscripten_get_now()) + ); + }, timeout_ms); + timers[which] = { + id, + timeout_ms, + }; + return 0; + }; + + __setitimer_js.sig = 'iid'; + + var __tzset_js = (timezone, daylight, std_name, dst_name) => { + // TODO: Use (malleable) environment variables instead of system settings. + var currentYear = new Date().getFullYear(); + var winter = new Date(currentYear, 0, 1); + var summer = new Date(currentYear, 6, 1); + var winterOffset = winter.getTimezoneOffset(); + var summerOffset = summer.getTimezoneOffset(); + // Local standard timezone offset. Local standard time is not adjusted for + // daylight savings. This code uses the fact that getTimezoneOffset returns + // a greater value during Standard Time versus Daylight Saving Time (DST). + // Thus it determines the expected output during Standard Time, and it + // compares whether the output of the given date the same (Standard) or less + // (DST). + var stdTimezoneOffset = Math.max(winterOffset, summerOffset); + // timezone is specified as seconds west of UTC ("The external variable + // `timezone` shall be set to the difference, in seconds, between + // Coordinated Universal Time (UTC) and local standard time."), the same + // as returned by stdTimezoneOffset. + // See http://pubs.opengroup.org/onlinepubs/009695399/functions/tzset.html + HEAPU32[timezone >> 2] = stdTimezoneOffset * 60; + HEAP32[daylight >> 2] = Number(winterOffset != summerOffset); + var extractZone = (timezoneOffset) => { + // Why inverse sign? + // Read here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset + var sign = timezoneOffset >= 0 ? '-' : '+'; + var absOffset = Math.abs(timezoneOffset); + var hours = String(Math.floor(absOffset / 60)).padStart(2, '0'); + var minutes = String(absOffset % 60).padStart(2, '0'); + return `UTC${sign}${hours}${minutes}`; + }; + var winterName = extractZone(winterOffset); + var summerName = extractZone(summerOffset); + if (summerOffset < winterOffset) { + // Northern hemisphere + stringToUTF8(winterName, std_name, 17); + stringToUTF8(summerName, dst_name, 17); + } else { + stringToUTF8(winterName, dst_name, 17); + stringToUTF8(summerName, std_name, 17); + } + }; + + __tzset_js.sig = 'vpppp'; + + var _emscripten_date_now = () => Date.now(); + + _emscripten_date_now.sig = 'd'; + + var nowIsMonotonic = 1; + + var checkWasiClock = (clock_id) => clock_id >= 0 && clock_id <= 3; + + function _clock_time_get(clk_id, ignored_precision, ptime) { + ignored_precision = bigintToI53Checked(ignored_precision); + if (!checkWasiClock(clk_id)) { + return 28; + } + var now; + // all wasi clocks but realtime are monotonic + if (clk_id === 0) { + now = _emscripten_date_now(); + } else if (nowIsMonotonic) { + now = _emscripten_get_now(); + } else { + return 52; + } + // "now" is in ms, and wasi times are in ns. + var nsec = Math.round(now * 1e3 * 1e3); + HEAP64[ptime >> 3] = BigInt(nsec); + return 0; + } + + _clock_time_get.sig = 'iijp'; + + var getHeapMax = () => + // Stay one Wasm page short of 4GB: while e.g. Chrome is able to allocate + // full 4GB Wasm memories, the size will wrap back to 0 bytes in Wasm side + // for any code that deals with heap sizes, which would require special + // casing all heap size related code to treat 0 specially. + 2147483648; + + var _emscripten_get_heap_max = () => getHeapMax(); + + _emscripten_get_heap_max.sig = 'p'; + + var growMemory = (size) => { + var oldHeapSize = wasmMemory.buffer.byteLength; + var pages = ((size - oldHeapSize + 65535) / 65536) | 0; + try { + // round size grow request up to wasm page size (fixed 64KB per spec) + wasmMemory.grow(pages); + // .grow() takes a delta compared to the previous size + updateMemoryViews(); + return 1; + } catch (e) {} + }; + + var _emscripten_resize_heap = (requestedSize) => { + var oldSize = HEAPU8.length; + // With CAN_ADDRESS_2GB or MEMORY64, pointers are already unsigned. + requestedSize >>>= 0; + // With multithreaded builds, races can happen (another thread might increase the size + // in between), so return a failure, and let the caller retry. + // Memory resize rules: + // 1. Always increase heap size to at least the requested size, rounded up + // to next page multiple. + // 2a. If MEMORY_GROWTH_LINEAR_STEP == -1, excessively resize the heap + // geometrically: increase the heap size according to + // MEMORY_GROWTH_GEOMETRIC_STEP factor (default +20%), At most + // overreserve by MEMORY_GROWTH_GEOMETRIC_CAP bytes (default 96MB). + // 2b. If MEMORY_GROWTH_LINEAR_STEP != -1, excessively resize the heap + // linearly: increase the heap size by at least + // MEMORY_GROWTH_LINEAR_STEP bytes. + // 3. Max size for the heap is capped at 2048MB-WASM_PAGE_SIZE, or by + // MAXIMUM_MEMORY, or by ASAN limit, depending on which is smallest + // 4. If we were unable to allocate as much memory, it may be due to + // over-eager decision to excessively reserve due to (3) above. + // Hence if an allocation fails, cut down on the amount of excess + // growth, in an attempt to succeed to perform a smaller allocation. + // A limit is set for how much we can grow. We should not exceed that + // (the wasm binary specifies it, so if we tried, we'd fail anyhow). + var maxHeapSize = getHeapMax(); + if (requestedSize > maxHeapSize) { + return false; + } + // Loop through potential heap size increases. If we attempt a too eager + // reservation that fails, cut down on the attempted size and reserve a + // smaller bump instead. (max 3 times, chosen somewhat arbitrarily) + for (var cutDown = 1; cutDown <= 4; cutDown *= 2) { + var overGrownHeapSize = oldSize * (1 + 0.2 / cutDown); + // ensure geometric growth + // but limit overreserving (default to capping at +96MB overgrowth at most) + overGrownHeapSize = Math.min( + overGrownHeapSize, + requestedSize + 100663296 + ); + var newSize = Math.min( + maxHeapSize, + alignMemory(Math.max(requestedSize, overGrownHeapSize), 65536) + ); + var replacement = growMemory(newSize); + if (replacement) { + return true; + } + } + return false; + }; + + _emscripten_resize_heap.sig = 'ip'; + + var runtimeKeepalivePush = () => { + runtimeKeepaliveCounter += 1; + }; + + runtimeKeepalivePush.sig = 'v'; + + var runtimeKeepalivePop = () => { + runtimeKeepaliveCounter -= 1; + }; + + runtimeKeepalivePop.sig = 'v'; + + /** @param {number=} timeout */ var safeSetTimeout = (func, timeout) => { + runtimeKeepalivePush(); + return setTimeout(() => { + runtimeKeepalivePop(); + callUserCallback(func); + }, timeout); + }; + + var _emscripten_sleep = (ms) => + Asyncify.handleSleep((wakeUp) => safeSetTimeout(wakeUp, ms)); + + _emscripten_sleep.sig = 'vi'; + + _emscripten_sleep.isAsync = true; + + var ENV = PHPLoader.ENV || {}; + + var getExecutableName = () => thisProgram || './this.program'; + + var getEnvStrings = () => { + if (!getEnvStrings.strings) { + // Default values. + // Browser language detection #8751 + var lang = + ( + (typeof navigator == 'object' && navigator.language) || + 'C' + ).replace('-', '_') + '.UTF-8'; + var env = { + USER: 'web_user', + LOGNAME: 'web_user', + PATH: '/', + PWD: '/', + HOME: '/home/web_user', + LANG: lang, + _: getExecutableName(), + }; + // Apply the user-provided values, if any. + for (var x in ENV) { + // x is a key in ENV; if ENV[x] is undefined, that means it was + // explicitly set to be so. We allow user code to do that to + // force variables with default values to remain unset. + if (ENV[x] === undefined) delete env[x]; + else env[x] = ENV[x]; + } + var strings = []; + for (var x in env) { + strings.push(`${x}=${env[x]}`); + } + getEnvStrings.strings = strings; + } + return getEnvStrings.strings; + }; + + var _environ_get = (__environ, environ_buf) => { + var bufSize = 0; + var envp = 0; + for (var string of getEnvStrings()) { + var ptr = environ_buf + bufSize; + HEAPU32[(__environ + envp) >> 2] = ptr; + bufSize += stringToUTF8(string, ptr, Infinity) + 1; + envp += 4; + } + return 0; + }; + + _environ_get.sig = 'ipp'; + + var _environ_sizes_get = (penviron_count, penviron_buf_size) => { + var strings = getEnvStrings(); + HEAPU32[penviron_count >> 2] = strings.length; + var bufSize = 0; + for (var string of strings) { + bufSize += lengthBytesUTF8(string) + 1; + } + HEAPU32[penviron_buf_size >> 2] = bufSize; + return 0; + }; + + _environ_sizes_get.sig = 'ipp'; + + function _fd_fdstat_get(fd, pbuf) { + try { + var rightsBase = 0; + var rightsInheriting = 0; + var flags = 0; + { + var stream = SYSCALLS.getStreamFromFD(fd); + // All character devices are terminals (other things a Linux system would + // assume is a character device, like the mouse, we have special APIs for). + var type = stream.tty + ? 2 + : FS.isDir(stream.mode) + ? 3 + : FS.isLink(stream.mode) + ? 7 + : 4; + } + HEAP8[pbuf] = type; + HEAP16[(pbuf + 2) >> 1] = flags; + HEAP64[(pbuf + 8) >> 3] = BigInt(rightsBase); + HEAP64[(pbuf + 16) >> 3] = BigInt(rightsInheriting); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + + _fd_fdstat_get.sig = 'iip'; + + /** @param {number=} offset */ var doReadv = ( + stream, + iov, + iovcnt, + offset + ) => { + var ret = 0; + for (var i = 0; i < iovcnt; i++) { + var ptr = HEAPU32[iov >> 2]; + var len = HEAPU32[(iov + 4) >> 2]; + iov += 8; + var curr = FS.read(stream, HEAP8, ptr, len, offset); + if (curr < 0) return -1; + ret += curr; + if (curr < len) break; + // nothing more to read + if (typeof offset != 'undefined') { + offset += curr; + } + } + return ret; + }; + + function _fd_read(fd, iov, iovcnt, pnum) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var num = doReadv(stream, iov, iovcnt); + HEAPU32[pnum >> 2] = num; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + + _fd_read.sig = 'iippp'; + + function _fd_seek(fd, offset, whence, newOffset) { + offset = bigintToI53Checked(offset); + try { + if (isNaN(offset)) return 61; + var stream = SYSCALLS.getStreamFromFD(fd); + FS.llseek(stream, offset, whence); + HEAP64[newOffset >> 3] = BigInt(stream.position); + if (stream.getdents && offset === 0 && whence === 0) + stream.getdents = null; + // reset readdir state + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + + _fd_seek.sig = 'iijip'; + + var _fd_sync = function (fd) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + return Asyncify.handleSleep((wakeUp) => { + var mount = stream.node.mount; + if (!mount.type.syncfs) { + // We write directly to the file system, so there's nothing to do here. + wakeUp(0); + return; + } + mount.type.syncfs(mount, false, (err) => { + wakeUp(err ? 29 : 0); + }); + }); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + }; + + _fd_sync.sig = 'ii'; + + _fd_sync.isAsync = true; + + /** @param {number=} offset */ var doWritev = ( + stream, + iov, + iovcnt, + offset + ) => { + var ret = 0; + for (var i = 0; i < iovcnt; i++) { + var ptr = HEAPU32[iov >> 2]; + var len = HEAPU32[(iov + 4) >> 2]; + iov += 8; + var curr = FS.write(stream, HEAP8, ptr, len, offset); + if (curr < 0) return -1; + ret += curr; + if (curr < len) { + // No more space to write. + break; + } + if (typeof offset != 'undefined') { + offset += curr; + } + } + return ret; + }; + + function _fd_write(fd, iov, iovcnt, pnum) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var num = doWritev(stream, iov, iovcnt); + HEAPU32[pnum >> 2] = num; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + + _fd_write.sig = 'iippp'; + + var _getaddrinfo = (node, service, hint, out) => { + var addr = 0; + var port = 0; + var flags = 0; + var family = 0; + var type = 0; + var proto = 0; + var ai; + function allocaddrinfo(family, type, proto, canon, addr, port) { + var sa, salen, ai; + var errno; + salen = family === 10 ? 28 : 16; + addr = family === 10 ? inetNtop6(addr) : inetNtop4(addr); + sa = _malloc(salen); + errno = writeSockaddr(sa, family, addr, port); + ai = _malloc(32); + HEAP32[(ai + 4) >> 2] = family; + HEAP32[(ai + 8) >> 2] = type; + HEAP32[(ai + 12) >> 2] = proto; + HEAPU32[(ai + 24) >> 2] = canon; + HEAPU32[(ai + 20) >> 2] = sa; + if (family === 10) { + HEAP32[(ai + 16) >> 2] = 28; + } else { + HEAP32[(ai + 16) >> 2] = 16; + } + HEAP32[(ai + 28) >> 2] = 0; + return ai; + } + if (hint) { + flags = HEAP32[hint >> 2]; + family = HEAP32[(hint + 4) >> 2]; + type = HEAP32[(hint + 8) >> 2]; + proto = HEAP32[(hint + 12) >> 2]; + } + if (type && !proto) { + proto = type === 2 ? 17 : 6; + } + if (!type && proto) { + type = proto === 17 ? 2 : 1; + } + // If type or proto are set to zero in hints we should really be returning multiple addrinfo values, but for + // now default to a TCP STREAM socket so we can at least return a sensible addrinfo given NULL hints. + if (proto === 0) { + proto = 6; + } + if (type === 0) { + type = 1; + } + if (!node && !service) { + return -2; + } + if (flags & ~(1 | 2 | 4 | 1024 | 8 | 16 | 32)) { + return -1; + } + if (hint !== 0 && HEAP32[hint >> 2] & 2 && !node) { + return -1; + } + if (flags & 32) { + // TODO + return -2; + } + if (type !== 0 && type !== 1 && type !== 2) { + return -7; + } + if (family !== 0 && family !== 2 && family !== 10) { + return -6; + } + if (service) { + service = UTF8ToString(service); + port = parseInt(service, 10); + if (isNaN(port)) { + if (flags & 1024) { + return -2; + } + // TODO support resolving well-known service names from: + // http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt + return -8; + } + } + if (!node) { + if (family === 0) { + family = 2; + } + if ((flags & 1) === 0) { + if (family === 2) { + addr = _htonl(2130706433); + } else { + addr = [0, 0, 0, _htonl(1)]; + } + } + ai = allocaddrinfo(family, type, proto, null, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + } + // try as a numeric address + node = UTF8ToString(node); + addr = inetPton4(node); + if (addr !== null) { + // incoming node is a valid ipv4 address + if (family === 0 || family === 2) { + family = 2; + } else if (family === 10 && flags & 8) { + addr = [0, 0, _htonl(65535), addr]; + family = 10; + } else { + return -2; + } + } else { + addr = inetPton6(node); + if (addr !== null) { + // incoming node is a valid ipv6 address + if (family === 0 || family === 10) { + family = 10; + } else { + return -2; + } + } + } + if (addr != null) { + ai = allocaddrinfo(family, type, proto, node, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + } + if (flags & 4) { + return -2; + } + // try as a hostname + // resolve the hostname to a temporary fake address + node = DNS.lookup_name(node); + addr = inetPton4(node); + if (family === 0) { + family = 2; + } else if (family === 10) { + addr = [0, 0, _htonl(65535), addr]; + } + ai = allocaddrinfo(family, type, proto, null, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + }; + + _getaddrinfo.sig = 'ipppp'; + + var _getnameinfo = (sa, salen, node, nodelen, serv, servlen, flags) => { + var info = readSockaddr(sa, salen); + if (info.errno) { + return -6; + } + var port = info.port; + var addr = info.addr; + var overflowed = false; + if (node && nodelen) { + var lookup; + if (flags & 1 || !(lookup = DNS.lookup_addr(addr))) { + if (flags & 8) { + return -2; + } + } else { + addr = lookup; + } + var numBytesWrittenExclNull = stringToUTF8(addr, node, nodelen); + if (numBytesWrittenExclNull + 1 >= nodelen) { + overflowed = true; + } + } + if (serv && servlen) { + port = '' + port; + var numBytesWrittenExclNull = stringToUTF8(port, serv, servlen); + if (numBytesWrittenExclNull + 1 >= servlen) { + overflowed = true; + } + } + if (overflowed) { + // Note: even when we overflow, getnameinfo() is specced to write out the truncated results. + return -12; + } + return 0; + }; + + _getnameinfo.sig = 'ipipipii'; + + var Protocols = { + list: [], + map: {}, + }; + + var stringToAscii = (str, buffer) => { + for (var i = 0; i < str.length; ++i) { + HEAP8[buffer++] = str.charCodeAt(i); + } + // Null-terminate the string + HEAP8[buffer] = 0; + }; + + var _setprotoent = (stayopen) => { + // void setprotoent(int stayopen); + // Allocate and populate a protoent structure given a name, protocol number and array of aliases + function allocprotoent(name, proto, aliases) { + // write name into buffer + var nameBuf = _malloc(name.length + 1); + stringToAscii(name, nameBuf); + // write aliases into buffer + var j = 0; + var length = aliases.length; + var aliasListBuf = _malloc((length + 1) * 4); + // Use length + 1 so we have space for the terminating NULL ptr. + for (var i = 0; i < length; i++, j += 4) { + var alias = aliases[i]; + var aliasBuf = _malloc(alias.length + 1); + stringToAscii(alias, aliasBuf); + HEAPU32[(aliasListBuf + j) >> 2] = aliasBuf; + } + HEAPU32[(aliasListBuf + j) >> 2] = 0; + // Terminating NULL pointer. + // generate protoent + var pe = _malloc(12); + HEAPU32[pe >> 2] = nameBuf; + HEAPU32[(pe + 4) >> 2] = aliasListBuf; + HEAP32[(pe + 8) >> 2] = proto; + return pe; + } + // Populate the protocol 'database'. The entries are limited to tcp and udp, though it is fairly trivial + // to add extra entries from /etc/protocols if desired - though not sure if that'd actually be useful. + var list = Protocols.list; + var map = Protocols.map; + if (list.length === 0) { + var entry = allocprotoent('tcp', 6, ['TCP']); + list.push(entry); + map['tcp'] = map['6'] = entry; + entry = allocprotoent('udp', 17, ['UDP']); + list.push(entry); + map['udp'] = map['17'] = entry; + } + _setprotoent.index = 0; + }; + + _setprotoent.sig = 'vi'; + + var _getprotobyname = (name) => { + // struct protoent *getprotobyname(const char *); + name = UTF8ToString(name); + _setprotoent(true); + var result = Protocols.map[name]; + return result; + }; + + _getprotobyname.sig = 'pp'; + + var _getprotobynumber = (number) => { + // struct protoent *getprotobynumber(int proto); + _setprotoent(true); + var result = Protocols.map[number]; + return result; + }; + + _getprotobynumber.sig = 'pi'; + + function _js_flock(fd, op) { + if (typeof Module['userSpace'] === 'undefined') { + // In the absence of a real locking facility, + // return success by default as Emscripten does. + return 0; + } + return Module['userSpace'].flock(fd, op); + } + + function _js_open_process( + command, + argsPtr, + argsLength, + descriptorsPtr, + descriptorsLength, + cwdPtr, + cwdLength, + envPtr, + envLength + ) { + if (!command) { + ___errno_location(ERRNO_CODES.EINVAL); + return -1; + } + const cmdstr = UTF8ToString(command); + if (!cmdstr.length) { + ___errno_location(ERRNO_CODES.EINVAL); + return -1; + } + let argsArray = []; + if (argsLength) { + for (var i = 0; i < argsLength; i++) { + const charPointer = argsPtr + i * 4; + argsArray.push(UTF8ToString(HEAPU32[charPointer >> 2])); + } + } + const cwdstr = cwdPtr ? UTF8ToString(cwdPtr) : FS.cwd(); + let envObject = null; + if (envLength) { + envObject = {}; + for (var i = 0; i < envLength; i++) { + const envPointer = envPtr + i * 4; + const envEntry = UTF8ToString(HEAPU32[envPointer >> 2]); + const splitAt = envEntry.indexOf('='); + if (splitAt === -1) { + continue; + } + const key = envEntry.substring(0, splitAt); + const value = envEntry.substring(splitAt + 1); + envObject[key] = value; + } + } + var std = {}; + // Extracts an array of available descriptors that should be dispatched to streams. + // On the C side, the descriptors are expressed as `**int` so we must go read + // each of the `descriptorsLength` `*int` pointers and convert the associated data into + // a JavaScript object { descriptor : { child : fd, parent : fd } }. + for (var i = 0; i < descriptorsLength; i++) { + const descriptorPtr = HEAPU32[(descriptorsPtr + i * 4) >> 2]; + std[HEAPU32[descriptorPtr >> 2]] = { + child: HEAPU32[(descriptorPtr + 4) >> 2], + parent: HEAPU32[(descriptorPtr + 8) >> 2], + }; + // swap parent and child descs until we rebuild PHP 7.4 + if (i === 0) { + HEAPU32[(descriptorPtr + 8) >> 2] = + std[HEAPU32[descriptorPtr >> 2]].parent; + HEAPU32[(descriptorPtr + 4) >> 2] = + std[HEAPU32[descriptorPtr >> 2]].child; + } + } + return Asyncify.handleAsync(async () => { + let cp; + try { + const options = {}; + if (cwdstr !== null) { + options.cwd = cwdstr; + } + if (envObject !== null) { + options.env = envObject; + } + cp = PHPWASM.spawnProcess(cmdstr, argsArray, options); + if (cp instanceof Promise) { + cp = await cp; + } + } catch (e) { + if (e.code === 'SPAWN_UNSUPPORTED') { + ___errno_location(ERRNO_CODES.ENOSYS); + return -1; + } + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) + throw e; + ___errno_location(e.code); + return -1; + } + const ProcInfo = { + pid: cp.pid, + exited: false, + }; + PHPWASM.processTable[ProcInfo.pid] = ProcInfo; + const stdinParentFd = std[0]?.parent, + stdinChildFd = std[0]?.child, + stdoutChildFd = std[1]?.child, + stdoutParentFd = std[1]?.parent, + stderrChildFd = std[2]?.child, + stderrParentFd = std[2]?.parent; + const detachPipeDataListeners = []; + cp.on('exit', function (code) { + for (const detach of detachPipeDataListeners) { + detach(); + } + for (const fd of [ + // The child process exited. Let's clean up its output streams: + stdoutChildFd, + stderrChildFd, + stdinChildFd, + ]) { + if (FS.streams[fd] && !FS.isClosed(FS.streams[fd])) { + FS.close(FS.streams[fd]); + } + } + ProcInfo.exitCode = code; + ProcInfo.exited = true; + }); + // Pass data from child process's stdout to PHP's end of the stdout pipe. + if (stdoutChildFd) { + const stdoutStream = SYSCALLS.getStreamFromFD(stdoutChildFd); + let stdoutAt = 0; + const onStdoutData = function (data) { + try { + stdoutStream.stream_ops.write( + stdoutStream, + data, + 0, + data.length, + stdoutAt + ); + stdoutAt += data.length; + } catch { + // PHP may close the child pipe before Node finishes + // draining already-buffered stdout data. Late chunks are + // no longer deliverable, so detach the listener and stop. + cp.stdout.off('data', onStdoutData); + } + }; + cp.stdout.on('data', onStdoutData); + detachPipeDataListeners.push(() => + cp.stdout.off('data', onStdoutData) + ); + } + // Pass data from child process's stderr to PHP's end of the stdout pipe. + if (stderrChildFd) { + const stderrStream = SYSCALLS.getStreamFromFD(stderrChildFd); + let stderrAt = 0; + const onStderrData = function (data) { + try { + stderrStream.stream_ops.write( + stderrStream, + data, + 0, + data.length, + stderrAt + ); + stderrAt += data.length; + } catch { + cp.stderr.off('data', onStderrData); + } + }; + cp.stderr.on('data', onStderrData); + detachPipeDataListeners.push(() => + cp.stderr.off('data', onStderrData) + ); + } + /** + * Wait until the child process has been spawned. + * Unfortunately there is no Node.js API to check whether + * the process has already been spawned. We can only listen + * to the 'spawn' event and if it has already been spawned, + * listen to the 'exit' event. + */ try { + await new Promise((resolve, reject) => { + /** + * There was no `await` between the `spawnProcess` call + * and the `await` below so the process haven't had a chance + * to run any of the exit-related callbacks yet. + * + * Good. + * + * Let's listen to all the lifecycle events and resolve + * the promise when the process starts or immediately crashes. + */ let resolved = false; + cp.on('spawn', () => { + if (resolved) return; + resolved = true; + resolve(); + }); + cp.on('error', (e) => { + if (resolved) return; + resolved = true; + reject(e); + }); + cp.on('exit', function (code) { + if (resolved) return; + resolved = true; + if (code === 0) { + resolve(); + } else { + reject( + new Error(`Process exited with code ${code}`) + ); + } + }); + /** + * If the process haven't even started after 5 seconds, something + * is wrong. Perhaps we're missing an event listener, or perhaps + * the `spawnProcess` implementation failed to dispatch the relevant + * event. Either way, let's crash to avoid blocking the proc_open() + * call indefinitely. + */ setTimeout(() => { + if (resolved) return; + resolved = true; + reject(new Error('Process timed out')); + }, 5e3); + }); + } catch (e) { + // Process already started. Even if it exited early, PHP still + // needs to know about the pid and clean up the resources. + console.error(e); + return ProcInfo.pid; + } + // Now we want to pass data from the STDIN source supplied by PHP + // to the child process. + if (stdinChildFd) { + // We're in a kernel function used instead of fork(). + // We are the ones responsible for pumping the data from the stdinChildFd + // into the child process. There is no concurrent task operating on the + // piped data or polling the file descriptors, etc. Nothing will ever + // read from the stdinChildFd if we don't do it here. + // Well, let's do it! We'll periodically read from the child end of the + // data pipe and push what we get into the child process. + let stdinStream; + try { + stdinStream = SYSCALLS.getStreamFromFD(stdinChildFd); + } catch (e) { + ___errno_location(ERRNO_CODES.EBADF); + return ProcInfo.pid; + } + if (!stdinStream?.node) { + return ProcInfo.pid; + } + // Pipe the entire stdinStream to cp.stdin + const CHUNK_SIZE = 1024; + const iov = _malloc(16); + // Space for iovec structure + const pnum = _malloc(4); + // Space for number of bytes read + const buffer = _malloc(CHUNK_SIZE); + // Set up iovec structure pointing to our buffer + HEAPU32[iov >> 2] = buffer; + // iov_base + HEAPU32[(iov + 4) >> 2] = CHUNK_SIZE; + // iov_len + function pump() { + try { + while (true) { + if (cp.killed) { + stopPumpingAndCloseStdin(); + return; + } + const result = js_fd_read( + stdinChildFd, + iov, + 1, + pnum, + false + ); + const bytesRead = HEAPU32[pnum >> 2]; + if (result === 0 && bytesRead > 0) { + const wrote = HEAPU8.subarray( + buffer, + buffer + bytesRead + ); + cp.stdin.write(wrote); + } else if (result === 0 && bytesRead === 0) { + // result === 0 and bytesRead === 0 means the file descriptor + // is at EOF. Let's close the stdin stream and clean up. + stopPumpingAndCloseStdin(); + break; + } else if (result === ERRNO_CODES.EAGAIN) { + // The file descriptor is not ready for reading. + // Let's break out of the loop. setInterval will invoke + // this function again soon. + break; + } else { + throw new FS.ErrnoError(result); + } + } + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) { + throw e; + } + ___errno_location(e.errno); + stopPumpingAndCloseStdin(); + } + } + function stopPumpingAndCloseStdin() { + clearInterval(interval); + if (!cp.stdin.closed) { + cp.stdin.end(); + } + _wasm_free(buffer); + _wasm_free(iov); + _wasm_free(pnum); + } + // pump() can never alter the result of this function. + // Even when it fails, we still return the pid. + // Why? + // Because the process already started. We wouldn't backtrack + // with fork(), we won't backtrack here. Let's give PHP the pid, + // and let it think it's the parent process. It will clean up the + // resources as needed. + // stdin may be non-blocking – let's check for updates periodically. + // If we exhaust it at any point, pump() will self-terminate. + // Note handling any failures, closing the descriptor, etc. will not + // happen synchronously when PHP calls fclose($pipes[0]) or proc_close(). + // It will all happen asynchronously on the next tick. It seems off, + // but there doesn't seem to be a better way: cp.stdin.write() and + // cp.stdin.end() are both async APIs and they both accept onCompleted + // callbacks. + const interval = setInterval(pump, 20); + pump(); + } + return ProcInfo.pid; + }); + } + + function _js_release_file_locks() { + if (typeof Module['userSpace'] === 'undefined') { + return; + } + return Module['userSpace'].js_release_file_locks(); + } + + var arraySum = (array, index) => { + var sum = 0; + for (var i = 0; i <= index; sum += array[i++]) {} + return sum; + }; + + var MONTH_DAYS_LEAP = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + + var MONTH_DAYS_REGULAR = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + + var addDays = (date, days) => { + var newDate = new Date(date.getTime()); + while (days > 0) { + var leap = isLeapYear(newDate.getFullYear()); + var currentMonth = newDate.getMonth(); + var daysInCurrentMonth = ( + leap ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR + )[currentMonth]; + if (days > daysInCurrentMonth - newDate.getDate()) { + // we spill over to next month + days -= daysInCurrentMonth - newDate.getDate() + 1; + newDate.setDate(1); + if (currentMonth < 11) { + newDate.setMonth(currentMonth + 1); + } else { + newDate.setMonth(0); + newDate.setFullYear(newDate.getFullYear() + 1); + } + } else { + // we stay in current month + newDate.setDate(newDate.getDate() + days); + return newDate; + } + } + return newDate; + }; + + var _strptime = (buf, format, tm) => { + // char *strptime(const char *restrict buf, const char *restrict format, struct tm *restrict tm); + // http://pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html + var pattern = UTF8ToString(format); + // escape special characters + // TODO: not sure we really need to escape all of these in JS regexps + var SPECIAL_CHARS = '\\!@#$^&*()+=-[]/{}|:<>?,.'; + for (var i = 0, ii = SPECIAL_CHARS.length; i < ii; ++i) { + pattern = pattern.replace( + new RegExp('\\' + SPECIAL_CHARS[i], 'g'), + '\\' + SPECIAL_CHARS[i] + ); + } + // reduce number of matchers + var EQUIVALENT_MATCHERS = { + A: '%a', + B: '%b', + c: '%a %b %d %H:%M:%S %Y', + D: '%m\\/%d\\/%y', + e: '%d', + F: '%Y-%m-%d', + h: '%b', + R: '%H\\:%M', + r: '%I\\:%M\\:%S\\s%p', + T: '%H\\:%M\\:%S', + x: '%m\\/%d\\/(?:%y|%Y)', + X: '%H\\:%M\\:%S', + }; + // TODO: take care of locale + var DATE_PATTERNS = { + /* weekday name */ a: '(?:Sun(?:day)?)|(?:Mon(?:day)?)|(?:Tue(?:sday)?)|(?:Wed(?:nesday)?)|(?:Thu(?:rsday)?)|(?:Fri(?:day)?)|(?:Sat(?:urday)?)', + /* month name */ b: '(?:Jan(?:uary)?)|(?:Feb(?:ruary)?)|(?:Mar(?:ch)?)|(?:Apr(?:il)?)|May|(?:Jun(?:e)?)|(?:Jul(?:y)?)|(?:Aug(?:ust)?)|(?:Sep(?:tember)?)|(?:Oct(?:ober)?)|(?:Nov(?:ember)?)|(?:Dec(?:ember)?)', + /* century */ C: '\\d\\d', + /* day of month */ d: '0[1-9]|[1-9](?!\\d)|1\\d|2\\d|30|31', + /* hour (24hr) */ H: '\\d(?!\\d)|[0,1]\\d|20|21|22|23', + /* hour (12hr) */ I: '\\d(?!\\d)|0\\d|10|11|12', + /* day of year */ j: '00[1-9]|0?[1-9](?!\\d)|0?[1-9]\\d(?!\\d)|[1,2]\\d\\d|3[0-6]\\d', + /* month */ m: '0[1-9]|[1-9](?!\\d)|10|11|12', + /* minutes */ M: '0\\d|\\d(?!\\d)|[1-5]\\d', + /* whitespace */ n: ' ', + /* AM/PM */ p: 'AM|am|PM|pm|A\\.M\\.|a\\.m\\.|P\\.M\\.|p\\.m\\.', + /* seconds */ S: '0\\d|\\d(?!\\d)|[1-5]\\d|60', + /* week number */ U: '0\\d|\\d(?!\\d)|[1-4]\\d|50|51|52|53', + /* week number */ W: '0\\d|\\d(?!\\d)|[1-4]\\d|50|51|52|53', + /* weekday number */ w: '[0-6]', + /* 2-digit year */ y: '\\d\\d', + /* 4-digit year */ Y: '\\d\\d\\d\\d', + /* whitespace */ t: ' ', + /* time zone */ z: 'Z|(?:[\\+\\-]\\d\\d:?(?:\\d\\d)?)', + }; + var MONTH_NUMBERS = { + JAN: 0, + FEB: 1, + MAR: 2, + APR: 3, + MAY: 4, + JUN: 5, + JUL: 6, + AUG: 7, + SEP: 8, + OCT: 9, + NOV: 10, + DEC: 11, + }; + var DAY_NUMBERS_SUN_FIRST = { + SUN: 0, + MON: 1, + TUE: 2, + WED: 3, + THU: 4, + FRI: 5, + SAT: 6, + }; + var DAY_NUMBERS_MON_FIRST = { + MON: 0, + TUE: 1, + WED: 2, + THU: 3, + FRI: 4, + SAT: 5, + SUN: 6, + }; + var capture = []; + var pattern_out = pattern + .replace(/%(.)/g, (m, c) => EQUIVALENT_MATCHERS[c] || m) + .replace(/%(.)/g, (_, c) => { + let pat = DATE_PATTERNS[c]; + if (pat) { + capture.push(c); + return `(${pat})`; + } else { + return c; + } + }) + .replace( + // any number of space or tab characters match zero or more spaces + /\s+/g, + '\\s*' + ); + var matches = new RegExp('^' + pattern_out, 'i').exec( + UTF8ToString(buf) + ); + function initDate() { + function fixup(value, min, max) { + return typeof value != 'number' || isNaN(value) + ? min + : value >= min + ? value <= max + ? value + : max + : min; + } + return { + year: fixup(HEAP32[(tm + 20) >> 2] + 1900, 1970, 9999), + month: fixup(HEAP32[(tm + 16) >> 2], 0, 11), + day: fixup(HEAP32[(tm + 12) >> 2], 1, 31), + hour: fixup(HEAP32[(tm + 8) >> 2], 0, 23), + min: fixup(HEAP32[(tm + 4) >> 2], 0, 59), + sec: fixup(HEAP32[tm >> 2], 0, 59), + gmtoff: 0, + }; + } + if (matches) { + var date = initDate(); + var value; + var getMatch = (symbol) => { + var pos = capture.indexOf(symbol); + // check if symbol appears in regexp + if (pos >= 0) { + // return matched value or null (falsy!) for non-matches + return matches[pos + 1]; + } + return; + }; + // seconds + if ((value = getMatch('S'))) { + date.sec = Number(value); + } + // minutes + if ((value = getMatch('M'))) { + date.min = Number(value); + } + // hours + if ((value = getMatch('H'))) { + // 24h clock + date.hour = Number(value); + } else if ((value = getMatch('I'))) { + // AM/PM clock + var hour = Number(value); + if ((value = getMatch('p'))) { + hour += value.toUpperCase()[0] === 'P' ? 12 : 0; + } + date.hour = hour; + } + // year + if ((value = getMatch('Y'))) { + // parse from four-digit year + date.year = Number(value); + } else if ((value = getMatch('y'))) { + // parse from two-digit year... + var year = Number(value); + if ((value = getMatch('C'))) { + // ...and century + year += Number(value) * 100; + } else { + // ...and rule-of-thumb + year += year < 69 ? 2e3 : 1900; + } + date.year = year; + } + // month + if ((value = getMatch('m'))) { + // parse from month number + date.month = Number(value) - 1; + } else if ((value = getMatch('b'))) { + // parse from month name + date.month = + MONTH_NUMBERS[value.substring(0, 3).toUpperCase()] || 0; + } + // day + if ((value = getMatch('d'))) { + // get day of month directly + date.day = Number(value); + } else if ((value = getMatch('j'))) { + // get day of month from day of year ... + var day = Number(value); + var leapYear = isLeapYear(date.year); + for (var month = 0; month < 12; ++month) { + var daysUntilMonth = arraySum( + leapYear ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR, + month - 1 + ); + if ( + day <= + daysUntilMonth + + (leapYear ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR)[ + month + ] + ) { + date.day = day - daysUntilMonth; + } + } + } else if ((value = getMatch('a'))) { + // get day of month from weekday ... + var weekDay = value.substring(0, 3).toUpperCase(); + if ((value = getMatch('U'))) { + // ... and week number (Sunday being first day of week) + // Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. + // All days in a new year preceding the first Sunday are considered to be in week 0. + var weekDayNumber = DAY_NUMBERS_SUN_FIRST[weekDay]; + var weekNumber = Number(value); + // January 1st + var janFirst = new Date(date.year, 0, 1); + var endDate; + if (janFirst.getDay() === 0) { + // Jan 1st is a Sunday, and, hence in the 1st CW + endDate = addDays( + janFirst, + weekDayNumber + 7 * (weekNumber - 1) + ); + } else { + // Jan 1st is not a Sunday, and, hence still in the 0th CW + endDate = addDays( + janFirst, + 7 - + janFirst.getDay() + + weekDayNumber + + 7 * (weekNumber - 1) + ); + } + date.day = endDate.getDate(); + date.month = endDate.getMonth(); + } else if ((value = getMatch('W'))) { + // ... and week number (Monday being first day of week) + // Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. + // All days in a new year preceding the first Monday are considered to be in week 0. + var weekDayNumber = DAY_NUMBERS_MON_FIRST[weekDay]; + var weekNumber = Number(value); + // January 1st + var janFirst = new Date(date.year, 0, 1); + var endDate; + if (janFirst.getDay() === 1) { + // Jan 1st is a Monday, and, hence in the 1st CW + endDate = addDays( + janFirst, + weekDayNumber + 7 * (weekNumber - 1) + ); + } else { + // Jan 1st is not a Monday, and, hence still in the 0th CW + endDate = addDays( + janFirst, + 7 - + janFirst.getDay() + + 1 + + weekDayNumber + + 7 * (weekNumber - 1) + ); + } + date.day = endDate.getDate(); + date.month = endDate.getMonth(); + } + } + // time zone + if ((value = getMatch('z'))) { + // GMT offset as either 'Z' or +-HH:MM or +-HH or +-HHMM + if (value.toLowerCase() === 'z') { + date.gmtoff = 0; + } else { + var match = value.match(/^((?:\-|\+)\d\d):?(\d\d)?/); + date.gmtoff = match[1] * 3600; + if (match[2]) { + date.gmtoff += + date.gmtoff > 0 ? match[2] * 60 : -match[2] * 60; + } + } + } + /* + tm_sec int seconds after the minute 0-61* + tm_min int minutes after the hour 0-59 + tm_hour int hours since midnight 0-23 + tm_mday int day of the month 1-31 + tm_mon int months since January 0-11 + tm_year int years since 1900 + tm_wday int days since Sunday 0-6 + tm_yday int days since January 1 0-365 + tm_isdst int Daylight Saving Time flag + tm_gmtoff long offset from GMT (seconds) + */ var fullDate = new Date( + date.year, + date.month, + date.day, + date.hour, + date.min, + date.sec, + 0 + ); + HEAP32[tm >> 2] = fullDate.getSeconds(); + HEAP32[(tm + 4) >> 2] = fullDate.getMinutes(); + HEAP32[(tm + 8) >> 2] = fullDate.getHours(); + HEAP32[(tm + 12) >> 2] = fullDate.getDate(); + HEAP32[(tm + 16) >> 2] = fullDate.getMonth(); + HEAP32[(tm + 20) >> 2] = fullDate.getFullYear() - 1900; + HEAP32[(tm + 24) >> 2] = fullDate.getDay(); + HEAP32[(tm + 28) >> 2] = + arraySum( + isLeapYear(fullDate.getFullYear()) + ? MONTH_DAYS_LEAP + : MONTH_DAYS_REGULAR, + fullDate.getMonth() - 1 + ) + + fullDate.getDate() - + 1; + HEAP32[(tm + 32) >> 2] = 0; + HEAP32[(tm + 36) >> 2] = date.gmtoff; + // we need to convert the matched sequence into an integer array to take care of UTF-8 characters > 0x7F + // TODO: not sure that intArrayFromString handles all unicode characters correctly + return buf + lengthBytesUTF8(matches[0]); + } + return 0; + }; + + _strptime.sig = 'pppp'; + + function _wasm_setsockopt( + socketd, + level, + optionName, + optionValuePtr, + optionLen + ) { + const optionValue = HEAPU8[optionValuePtr]; + const SOL_SOCKET = 1; + const SO_KEEPALIVE = 9; + const SO_RCVTIMEO = 66; + const SO_SNDTIMEO = 67; + const IPPROTO_TCP = 6; + const TCP_NODELAY = 1; + // Options that we can forward to the WebSocket proxy + const isForwardable = + (level === SOL_SOCKET && optionName === SO_KEEPALIVE) || + (level === IPPROTO_TCP && optionName === TCP_NODELAY); + // Options that we acknowledge but don't actually implement + // (WebSocket connections handle timeouts differently) + const isIgnorable = + level === SOL_SOCKET && + (optionName === SO_RCVTIMEO || optionName === SO_SNDTIMEO); + if (!isForwardable && !isIgnorable) { + console.warn( + `Unsupported socket option: ${level}, ${optionName}, ${optionValue}` + ); + return -1; + } + // For ignorable options, just return success + if (isIgnorable) { + return 0; + } + const ws = PHPWASM.getAllWebSockets(socketd)[0]; + if (!ws) { + return -1; + } + ws.setSocketOpt(level, optionName, optionValuePtr); + return 0; + } + + var Asyncify = { + instrumentWasmImports(imports) { + var importPattern = + /^(js_open_process|js_fd_read|js_waitpid|js_process_status|js_create_input_device|wasm_setsockopt|wasm_shutdown|wasm_close|wasm_recv|wasm_connect|__syscall_fcntl64|js_flock|js_release_file_locks|js_waitpid|invoke_.*|__asyncjs__.*)$/; + for (let [x, original] of Object.entries(imports)) { + if (typeof original == 'function') { + let isAsyncifyImport = + original.isAsync || importPattern.test(x); + // Wrap async imports with a suspending WebAssembly function. + if (isAsyncifyImport) { + imports[x] = original = new WebAssembly.Suspending( + original + ); + } + } + } + }, + instrumentFunction(original) { + var wrapper = (...args) => original(...args); + wrapper.orig = original; + return wrapper; + }, + instrumentWasmExports(exports) { + var exportPattern = + /^(php_wasm_init|wasm_sleep|wasm_read|emscripten_sleep|wasm_sapi_handle_request|wasm_sapi_request_shutdown|wasm_poll_socket|wrap_select|__wrap_select|select|php_pollfd_for|fflush|wasm_popen|wasm_read|wasm_php_exec|run_cli|wasm_recv|wasm_connect|__wasm_call_ctors|__errno_location|__funcs_on_exit|main|__main_argc_argv)$/; + Asyncify.asyncExports = new Set(); + var ret = {}; + for (let [x, original] of Object.entries(exports)) { + if (typeof original == 'function') { + // Wrap all exports with a promising WebAssembly function. + let isAsyncifyExport = exportPattern.test(x); + if (isAsyncifyExport) { + Asyncify.asyncExports.add(original); + original = Asyncify.makeAsyncFunction(original); + } + var wrapper = Asyncify.instrumentFunction(original); + ret[x] = wrapper; + } else { + ret[x] = original; + } + } + return ret; + }, + asyncExports: null, + isAsyncExport(func) { + return Asyncify.asyncExports?.has(func); + }, + handleAsync: async (startAsync) => { + runtimeKeepalivePush(); + try { + return await startAsync(); + } finally { + runtimeKeepalivePop(); + } + }, + handleSleep: (startAsync) => + Asyncify.handleAsync(() => new Promise(startAsync)), + makeAsyncFunction(original) { + return WebAssembly.promising(original); + }, + }; + + var getCFunc = (ident) => { + var func = Module['_' + ident]; + // closure exported function + return func; + }; + + var writeArrayToMemory = (array, buffer) => { + HEAP8.set(array, buffer); + }; + + /** + * @param {string|null=} returnType + * @param {Array=} argTypes + * @param {Array=} args + * @param {Object=} opts + */ var ccall = (ident, returnType, argTypes, args, opts) => { + // For fast lookup of conversion functions + var toC = { + string: (str) => { + var ret = 0; + if (str !== null && str !== undefined && str !== 0) { + // null string + ret = stringToUTF8OnStack(str); + } + return ret; + }, + array: (arr) => { + var ret = stackAlloc(arr.length); + writeArrayToMemory(arr, ret); + return ret; + }, + }; + function convertReturnValue(ret) { + if (returnType === 'string') { + return UTF8ToString(ret); + } + if (returnType === 'boolean') return Boolean(ret); + return ret; + } + var func = getCFunc(ident); + var cArgs = []; + var stack = 0; + if (args) { + for (var i = 0; i < args.length; i++) { + var converter = toC[argTypes[i]]; + if (converter) { + if (stack === 0) stack = stackSave(); + cArgs[i] = converter(args[i]); + } else { + cArgs[i] = args[i]; + } + } + } + var ret = func(...cArgs); + function onDone(ret) { + if (stack !== 0) stackRestore(stack); + return convertReturnValue(ret); + } + var asyncMode = opts?.async; + if (asyncMode) return ret.then(onDone); + ret = onDone(ret); + return ret; + }; + + var FS_createPath = (...args) => FS.createPath(...args); + + var FS_unlink = (...args) => FS.unlink(...args); + + var FS_createLazyFile = (...args) => FS.createLazyFile(...args); + + var FS_createDevice = (...args) => FS.createDevice(...args); + + registerWasmPlugin(); + + FS.createPreloadedFile = FS_createPreloadedFile; + + FS.preloadFile = FS_preloadFile; + + FS.staticInit(); + + if (ENVIRONMENT_IS_NODE) { + NODEFS.staticInit(); + } + + PHPWASM.init(); + + // End JS library code + // include: postlibrary.js + // This file is included after the automatically-generated JS library code + // but before the wasm module is created. + { + // Begin ATMODULES hooks + if (Module['preloadPlugins']) preloadPlugins = Module['preloadPlugins']; + if (Module['noExitRuntime']) noExitRuntime = Module['noExitRuntime']; + if (Module['print']) out = Module['print']; + if (Module['printErr']) err = Module['printErr']; + if (Module['dynamicLibraries']) + dynamicLibraries = Module['dynamicLibraries']; + if (Module['wasmBinary']) wasmBinary = Module['wasmBinary']; + // End ATMODULES hooks + if (Module['arguments']) arguments_ = Module['arguments']; + if (Module['thisProgram']) thisProgram = Module['thisProgram']; + if (Module['quit']) quit_ = Module['quit']; + if (Module['preInit']) { + if (typeof Module['preInit'] == 'function') + Module['preInit'] = [Module['preInit']]; + while (Module['preInit'].length > 0) { + Module['preInit'].shift()(); + } + } + } + + // Begin runtime exports + Module['wasmExports'] = wasmExports; + + Module['addRunDependency'] = addRunDependency; + + Module['removeRunDependency'] = removeRunDependency; + + Module['ccall'] = ccall; + + Module['FS_preloadFile'] = FS_preloadFile; + + Module['FS_unlink'] = FS_unlink; + + Module['FS_createPath'] = FS_createPath; + + Module['FS_createDevice'] = FS_createDevice; + + Module['FS_createDataFile'] = FS_createDataFile; + + Module['FS_createLazyFile'] = FS_createLazyFile; + + Module['PROXYFS'] = PROXYFS; + + // End runtime exports + // Begin JS library exports + Module['UTF8ToString'] = UTF8ToString; + + Module['lengthBytesUTF8'] = lengthBytesUTF8; + + Module['stringToUTF8'] = stringToUTF8; + + Module['FS'] = FS; + + Module['_exit'] = _exit; + + Module['_emscripten_sleep'] = _emscripten_sleep; + + // End JS library exports + // end include: postlibrary.js + var ASM_CONSTS = {}; + + function __asyncjs__js_popen_to_file(command, mode, exitCodePtr) { + return Asyncify.handleAsync(async () => { + const returnCallback = (resolver) => new Promise(resolver); + if (!command) return 1; + const cmdstr = UTF8ToString(command); + if (!cmdstr.length) return 0; + const modestr = UTF8ToString(mode); + if (!modestr.length) return 0; + if (modestr === 'w') { + console.error('popen($cmd, "w") is not implemented yet'); + } + return returnCallback(async (wakeUp) => { + let cp; + try { + cp = PHPWASM.spawnProcess(cmdstr, []); + if (cp instanceof Promise) { + cp = await cp; + } + } catch (e) { + console.error(e); + if (e.code === 'SPAWN_UNSUPPORTED') { + return 1; + } + throw e; + } + const outByteArrays = []; + cp.stdout.on('data', function (data) { + outByteArrays.push(data); + }); + const outputPath = '/tmp/popen_output'; + cp.on('exit', function (exitCode) { + const outBytes = new Uint8Array( + outByteArrays.reduce( + (acc, curr) => acc + curr.length, + 0 + ) + ); + let offset = 0; + for (const byteArray of outByteArrays) { + outBytes.set(byteArray, offset); + offset += byteArray.length; + } + FS.writeFile(outputPath, outBytes); + HEAPU8[exitCodePtr] = exitCode; + wakeUp(allocateUTF8OnStack(outputPath)); + }); + }); + }); + } + + __asyncjs__js_popen_to_file.sig = 'iiii'; + + function __asyncjs__wasm_poll_socket(socketd, events, timeout) { + return Asyncify.handleAsync(async () => { + const returnCallback = (resolver) => new Promise(resolver); + const POLLIN = 1; + const POLLPRI = 2; + const POLLOUT = 4; + const POLLERR = 8; + const POLLHUP = 16; + const POLLNVAL = 32; + return returnCallback((wakeUp) => { + const polls = []; + const stream = FS.getStream(socketd); + if (FS.isSocket(stream?.node.mode)) { + const sock = getSocketFromFD(socketd); + if (!sock) { + wakeUp(0); + return; + } + const lookingFor = new Set(); + if (events & POLLIN || events & POLLPRI) { + if (sock.server) { + for (const client of sock.pending) { + if ((client.recv_queue || []).length > 0) { + wakeUp(1); + return; + } + } + } else if ((sock.recv_queue || []).length > 0) { + wakeUp(1); + return; + } + } + const webSockets = PHPWASM.getAllWebSockets(sock); + if (!webSockets.length) { + wakeUp(0); + return; + } + for (const ws of webSockets) { + if (events & POLLIN || events & POLLPRI) { + polls.push(PHPWASM.awaitData(ws)); + lookingFor.add('POLLIN'); + } + if (events & POLLOUT) { + polls.push(PHPWASM.awaitConnection(ws)); + lookingFor.add('POLLOUT'); + } + if ( + events & POLLHUP || + events & POLLIN || + events & POLLOUT || + events & POLLERR + ) { + polls.push(PHPWASM.awaitClose(ws)); + lookingFor.add('POLLHUP'); + } + if (events & POLLERR || events & POLLNVAL) { + polls.push(PHPWASM.awaitError(ws)); + lookingFor.add('POLLERR'); + } + } + } else if (stream?.stream_ops?.poll) { + let interrupted = false; + async function poll() { + try { + while (true) { + var mask = POLLNVAL; + mask = SYSCALLS.DEFAULT_POLLMASK; + if (FS.isClosed(stream)) { + return ERRNO_CODES.EBADF; + } + if (stream.stream_ops?.poll) { + mask = stream.stream_ops.poll(stream, -1); + } + mask &= events | POLLERR | POLLHUP; + if (mask) { + return mask; + } + if (interrupted) { + return ERRNO_CODES.ETIMEDOUT; + } + await new Promise((resolve) => + setTimeout(resolve, 10) + ); + } + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) + throw e; + return -e.errno; + } + } + polls.push([ + poll(), + () => { + interrupted = true; + }, + ]); + } else { + setTimeout(function () { + wakeUp(1); + }, timeout); + return; + } + if (polls.length === 0) { + console.warn( + 'Unsupported poll event ' + + events + + ', defaulting to setTimeout().' + ); + setTimeout(function () { + wakeUp(0); + }, timeout); + return; + } + const promises = polls.map(([promise]) => promise); + const clearPolling = () => + polls.forEach(([, clear]) => clear()); + let awaken = false; + let timeoutId; + Promise.race(promises).then(function (results) { + if (!awaken) { + awaken = true; + wakeUp(1); + if (timeoutId) { + clearTimeout(timeoutId); + } + clearPolling(); + } + }); + if (timeout !== -1) { + timeoutId = setTimeout(function () { + if (!awaken) { + awaken = true; + wakeUp(0); + clearPolling(); + } + }, timeout); + } + }); + }); + } + + __asyncjs__wasm_poll_socket.sig = 'iiii'; + + function js_fd_read(fd, iov, iovcnt, pnum) { + const returnCallback = (resolver) => new Promise(resolver); + const pollAsync = arguments[4] === undefined ? true : !!arguments[4]; + if ( + Asyncify?.State?.Normal === undefined || + Asyncify?.state === Asyncify?.State?.Normal + ) { + var stream; + try { + stream = SYSCALLS.getStreamFromFD(fd); + HEAPU32[pnum >> 2] = doReadv(stream, iov, iovcnt); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + throw e; + } + if ( + e.errno !== ERRNO_CODES.EWOULDBLOCK && + e.errno !== ERRNO_CODES.EAGAIN + ) { + return e.errno; + } + const nonBlocking = stream.flags & PHPWASM.O_NONBLOCK; + if (nonBlocking) { + return e.errno; + } + } + } + if (false === pollAsync) { + return ERRNO_CODES.EWOULDBLOCK; + } + return returnCallback(async (wakeUp) => { + var retries = 0; + var interval = 50; + var timeout = 5e3; + var maxRetries = timeout / interval; + while (true) { + var returnCode; + var stream; + let num; + try { + stream = SYSCALLS.getStreamFromFD(fd); + num = doReadv(stream, iov, iovcnt); + returnCode = 0; + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) { + console.error(e); + throw e; + } + returnCode = e.errno; + } + if (returnCode === 0) { + HEAPU32[pnum >> 2] = num; + return wakeUp(0); + } + if ( + ++retries > maxRetries || + !stream || + FS.isClosed(stream) || + returnCode !== ERRNO_CODES.EWOULDBLOCK || + ('pipe' in stream.node && stream.node.pipe.refcnt < 2) + ) { + HEAPU32[pnum >> 2] = num; + return wakeUp(returnCode); + } + await new Promise((resolve) => setTimeout(resolve, interval)); + } + }); + } + + js_fd_read.sig = 'iiiii'; + + function __asyncjs__js_module_onMessage(data, response_buffer) { + return Asyncify.handleAsync(async () => { + if (Module['onMessage']) { + const dataStr = UTF8ToString(data); + return Module['onMessage'](dataStr) + .then((response) => { + const responseBytes = + typeof response === 'string' + ? new TextEncoder().encode(response) + : response; + const responseSize = responseBytes.byteLength; + const responsePtr = _malloc(responseSize + 1); + HEAPU8.set(responseBytes, responsePtr); + HEAPU8[responsePtr + responseSize] = 0; + HEAPU8[response_buffer] = responsePtr; + HEAPU8[response_buffer + 1] = responsePtr >> 8; + HEAPU8[response_buffer + 2] = responsePtr >> 16; + HEAPU8[response_buffer + 3] = responsePtr >> 24; + return responseSize; + }) + .catch((e) => { + console.error(e); + return -1; + }); + } + }); + } + + __asyncjs__js_module_onMessage.sig = 'iii'; + + // Imports from the Wasm binary. + var _calloc, + _malloc, + ___errno_location, + _wasm_sleep, + _ntohs, + _htons, + _htonl, + _wasm_read, + _fflush, + _flock, + _php_pollfd_for, + _wasm_php_exec, + ___wrap_usleep, + _wasm_popen, + ___wrap_select, + _wasm_set_sapi_name, + _wasm_set_phpini_path, + _wasm_add_cli_arg, + _run_cli, + _wasm_add_SERVER_entry, + _wasm_add_ENV_entry, + _wasm_set_query_string, + _wasm_set_path_translated, + _wasm_set_skip_shebang, + _wasm_set_request_uri, + _wasm_set_request_method, + _wasm_set_request_host, + _wasm_set_content_type, + _wasm_set_request_body, + _wasm_set_content_length, + _wasm_set_cookies, + _wasm_set_request_port, + _wasm_sapi_request_shutdown, + _wasm_sapi_handle_request, + _php_wasm_init, + _wasm_free, + _wasm_get_end_offset, + ___wrap_getpid, + _wasm_trace, + _initgroups, + ___funcs_on_exit, + ___dl_seterr, + __emscripten_find_dylib, + _emscripten_builtin_memalign, + __emscripten_timeout, + _emscripten_get_sbrk_ptr, + ___trap, + __emscripten_stack_restore, + __emscripten_stack_alloc, + _emscripten_stack_get_current, + __ZNSt3__211__call_onceERVmPvPFvS2_E, + __ZNSt3__218condition_variable10notify_allEv, + __ZNSt3__25mutex4lockEv, + __ZNSt3__25mutex6unlockEv, + memory, + __indirect_function_table, + ___c_longjmp, + wasmTable, + wasmMemory; + + function assignWasmExports(wasmExports) { + _calloc = wasmExports['calloc']; + _malloc = + PHPLoader['malloc'] = + Module['_malloc'] = + wasmExports['malloc']; + ___errno_location = Module['___errno_location'] = + wasmExports['__errno_location']; + _wasm_sleep = Module['_wasm_sleep'] = wasmExports['wasm_sleep']; + _ntohs = wasmExports['ntohs']; + _htons = wasmExports['htons']; + _htonl = wasmExports['htonl']; + _wasm_read = Module['_wasm_read'] = wasmExports['wasm_read']; + _fflush = wasmExports['fflush']; + _flock = Module['_flock'] = wasmExports['flock']; + _php_pollfd_for = Module['_php_pollfd_for'] = + wasmExports['php_pollfd_for']; + _wasm_php_exec = Module['_wasm_php_exec'] = + wasmExports['wasm_php_exec']; + ___wrap_usleep = Module['___wrap_usleep'] = + wasmExports['__wrap_usleep']; + _wasm_popen = Module['_wasm_popen'] = wasmExports['wasm_popen']; + ___wrap_select = Module['___wrap_select'] = + wasmExports['__wrap_select']; + _wasm_set_sapi_name = Module['_wasm_set_sapi_name'] = + wasmExports['wasm_set_sapi_name']; + _wasm_set_phpini_path = Module['_wasm_set_phpini_path'] = + wasmExports['wasm_set_phpini_path']; + _wasm_add_cli_arg = Module['_wasm_add_cli_arg'] = + wasmExports['wasm_add_cli_arg']; + _run_cli = Module['_run_cli'] = wasmExports['run_cli']; + _wasm_add_SERVER_entry = Module['_wasm_add_SERVER_entry'] = + wasmExports['wasm_add_SERVER_entry']; + _wasm_add_ENV_entry = Module['_wasm_add_ENV_entry'] = + wasmExports['wasm_add_ENV_entry']; + _wasm_set_query_string = Module['_wasm_set_query_string'] = + wasmExports['wasm_set_query_string']; + _wasm_set_path_translated = Module['_wasm_set_path_translated'] = + wasmExports['wasm_set_path_translated']; + _wasm_set_skip_shebang = Module['_wasm_set_skip_shebang'] = + wasmExports['wasm_set_skip_shebang']; + _wasm_set_request_uri = Module['_wasm_set_request_uri'] = + wasmExports['wasm_set_request_uri']; + _wasm_set_request_method = Module['_wasm_set_request_method'] = + wasmExports['wasm_set_request_method']; + _wasm_set_request_host = Module['_wasm_set_request_host'] = + wasmExports['wasm_set_request_host']; + _wasm_set_content_type = Module['_wasm_set_content_type'] = + wasmExports['wasm_set_content_type']; + _wasm_set_request_body = Module['_wasm_set_request_body'] = + wasmExports['wasm_set_request_body']; + _wasm_set_content_length = Module['_wasm_set_content_length'] = + wasmExports['wasm_set_content_length']; + _wasm_set_cookies = Module['_wasm_set_cookies'] = + wasmExports['wasm_set_cookies']; + _wasm_set_request_port = Module['_wasm_set_request_port'] = + wasmExports['wasm_set_request_port']; + _wasm_sapi_request_shutdown = Module['_wasm_sapi_request_shutdown'] = + wasmExports['wasm_sapi_request_shutdown']; + _wasm_sapi_handle_request = Module['_wasm_sapi_handle_request'] = + wasmExports['wasm_sapi_handle_request']; + _php_wasm_init = Module['_php_wasm_init'] = + wasmExports['php_wasm_init']; + _wasm_free = + PHPLoader['free'] = + Module['_wasm_free'] = + wasmExports['wasm_free']; + _wasm_get_end_offset = Module['_wasm_get_end_offset'] = + wasmExports['wasm_get_end_offset']; + ___wrap_getpid = Module['___wrap_getpid'] = + wasmExports['__wrap_getpid']; + _wasm_trace = Module['_wasm_trace'] = wasmExports['wasm_trace']; + _initgroups = Module['_initgroups'] = wasmExports['initgroups']; + ___funcs_on_exit = wasmExports['__funcs_on_exit']; + ___dl_seterr = wasmExports['__dl_seterr']; + __emscripten_find_dylib = wasmExports['_emscripten_find_dylib']; + _emscripten_builtin_memalign = + wasmExports['emscripten_builtin_memalign']; + __emscripten_timeout = wasmExports['_emscripten_timeout']; + _emscripten_get_sbrk_ptr = wasmExports['emscripten_get_sbrk_ptr']; + ___trap = wasmExports['__trap']; + __emscripten_stack_restore = wasmExports['_emscripten_stack_restore']; + __emscripten_stack_alloc = wasmExports['_emscripten_stack_alloc']; + _emscripten_stack_get_current = + wasmExports['emscripten_stack_get_current']; + __ZNSt3__211__call_onceERVmPvPFvS2_E = Module[ + '__ZNSt3__211__call_onceERVmPvPFvS2_E' + ] = wasmExports['_ZNSt3__211__call_onceERVmPvPFvS2_E']; + __ZNSt3__218condition_variable10notify_allEv = Module[ + '__ZNSt3__218condition_variable10notify_allEv' + ] = wasmExports['_ZNSt3__218condition_variable10notify_allEv']; + __ZNSt3__25mutex4lockEv = Module['__ZNSt3__25mutex4lockEv'] = + wasmExports['_ZNSt3__25mutex4lockEv']; + __ZNSt3__25mutex6unlockEv = Module['__ZNSt3__25mutex6unlockEv'] = + wasmExports['_ZNSt3__25mutex6unlockEv']; + memory = wasmMemory = wasmExports['memory']; + __indirect_function_table = wasmTable = + wasmExports['__indirect_function_table']; + ___c_longjmp = Module['___c_longjmp'] = wasmExports['__c_longjmp']; + } + + var ___heap_base = 5123904; + + var wasmImports = { + /** @export */ __assert_fail: ___assert_fail, + /** @export */ __asyncjs__js_module_onMessage, + /** @export */ __asyncjs__js_popen_to_file, + /** @export */ __asyncjs__wasm_poll_socket, + /** @export */ __call_sighandler: ___call_sighandler, + /** @export */ __syscall_accept4: ___syscall_accept4, + /** @export */ __syscall_bind: ___syscall_bind, + /** @export */ __syscall_chdir: ___syscall_chdir, + /** @export */ __syscall_chmod: ___syscall_chmod, + /** @export */ __syscall_connect: ___syscall_connect, + /** @export */ __syscall_dup: ___syscall_dup, + /** @export */ __syscall_dup3: ___syscall_dup3, + /** @export */ __syscall_faccessat: ___syscall_faccessat, + /** @export */ __syscall_fchmod: ___syscall_fchmod, + /** @export */ __syscall_fchown32: ___syscall_fchown32, + /** @export */ __syscall_fchownat: ___syscall_fchownat, + /** @export */ __syscall_fcntl64: ___syscall_fcntl64, + /** @export */ __syscall_fstat64: ___syscall_fstat64, + /** @export */ __syscall_ftruncate64: ___syscall_ftruncate64, + /** @export */ __syscall_getcwd: ___syscall_getcwd, + /** @export */ __syscall_getdents64: ___syscall_getdents64, + /** @export */ __syscall_getpeername: ___syscall_getpeername, + /** @export */ __syscall_getsockname: ___syscall_getsockname, + /** @export */ __syscall_getsockopt: ___syscall_getsockopt, + /** @export */ __syscall_ioctl: ___syscall_ioctl, + /** @export */ __syscall_listen: ___syscall_listen, + /** @export */ __syscall_lstat64: ___syscall_lstat64, + /** @export */ __syscall_mkdirat: ___syscall_mkdirat, + /** @export */ __syscall_newfstatat: ___syscall_newfstatat, + /** @export */ __syscall_openat: ___syscall_openat, + /** @export */ __syscall_pipe: ___syscall_pipe, + /** @export */ __syscall_readlinkat: ___syscall_readlinkat, + /** @export */ __syscall_recvfrom: ___syscall_recvfrom, + /** @export */ __syscall_renameat: ___syscall_renameat, + /** @export */ __syscall_rmdir: ___syscall_rmdir, + /** @export */ __syscall_sendto: ___syscall_sendto, + /** @export */ __syscall_socket: ___syscall_socket, + /** @export */ __syscall_stat64: ___syscall_stat64, + /** @export */ __syscall_statfs64: ___syscall_statfs64, + /** @export */ __syscall_symlinkat: ___syscall_symlinkat, + /** @export */ __syscall_unlinkat: ___syscall_unlinkat, + /** @export */ __syscall_utimensat: ___syscall_utimensat, + /** @export */ _abort_js: __abort_js, + /** @export */ _dlopen_js: __dlopen_js, + /** @export */ _dlsym_js: __dlsym_js, + /** @export */ _emscripten_lookup_name: __emscripten_lookup_name, + /** @export */ _emscripten_runtime_keepalive_clear: + __emscripten_runtime_keepalive_clear, + /** @export */ _gmtime_js: __gmtime_js, + /** @export */ _localtime_js: __localtime_js, + /** @export */ _mktime_js: __mktime_js, + /** @export */ _mmap_js: __mmap_js, + /** @export */ _munmap_js: __munmap_js, + /** @export */ _setitimer_js: __setitimer_js, + /** @export */ _tzset_js: __tzset_js, + /** @export */ clock_time_get: _clock_time_get, + /** @export */ emscripten_date_now: _emscripten_date_now, + /** @export */ emscripten_get_heap_max: _emscripten_get_heap_max, + /** @export */ emscripten_get_now: _emscripten_get_now, + /** @export */ emscripten_resize_heap: _emscripten_resize_heap, + /** @export */ emscripten_sleep: _emscripten_sleep, + /** @export */ environ_get: _environ_get, + /** @export */ environ_sizes_get: _environ_sizes_get, + /** @export */ exit: _exit, + /** @export */ fd_close: _fd_close, + /** @export */ fd_fdstat_get: _fd_fdstat_get, + /** @export */ fd_read: _fd_read, + /** @export */ fd_seek: _fd_seek, + /** @export */ fd_sync: _fd_sync, + /** @export */ fd_write: _fd_write, + /** @export */ getaddrinfo: _getaddrinfo, + /** @export */ getnameinfo: _getnameinfo, + /** @export */ getprotobyname: _getprotobyname, + /** @export */ getprotobynumber: _getprotobynumber, + /** @export */ js_fd_read, + /** @export */ js_flock: _js_flock, + /** @export */ js_getpid: _js_getpid, + /** @export */ js_open_process: _js_open_process, + /** @export */ js_release_file_locks: _js_release_file_locks, + /** @export */ js_wasm_trace: _js_wasm_trace, + /** @export */ proc_exit: _proc_exit, + /** @export */ strptime: _strptime, + /** @export */ wasm_setsockopt: _wasm_setsockopt, + }; + + // include: postamble.js + // === Auto-generated postamble setup entry stuff === + async function callMain(args = []) { + var entryFunction = resolveGlobalSymbol('main').sym; + // Main modules can't tell if they have main() at compile time, since it may + // arrive from a dynamic library. + if (!entryFunction) return; + args.unshift(thisProgram); + var argc = args.length; + var argv = stackAlloc((argc + 1) * 4); + var argv_ptr = argv; + for (var arg of args) { + HEAPU32[argv_ptr >> 2] = stringToUTF8OnStack(arg); + argv_ptr += 4; + } + HEAPU32[argv_ptr >> 2] = 0; + try { + var ret = entryFunction(argc, argv); + // The current spec of JSPI returns a promise only if the function suspends + // and a plain value otherwise. This will likely change: + // https://github.com/WebAssembly/js-promise-integration/issues/11 + ret = await ret; + // if we're not running an evented main loop, it's time to exit + exitJS(ret, /* implicit = */ true); + return ret; + } catch (e) { + return handleException(e); + } + } + + function run(args = arguments_) { + if (runDependencies > 0) { + dependenciesFulfilled = run; + return; + } + preRun(); + // a preRun added a dependency, run will be called later + if (runDependencies > 0) { + dependenciesFulfilled = run; + return; + } + async function doRun() { + // run may have just been called through dependencies being fulfilled just in this very frame, + // or while the async setStatus time below was happening + Module['calledRun'] = true; + if (ABORT) return; + initRuntime(); + preMain(); + Module['onRuntimeInitialized']?.(); + var noInitialRun = Module['noInitialRun'] || true; + if (!noInitialRun) await callMain(args); + postRun(); + } + if (Module['setStatus']) { + Module['setStatus']('Running...'); + setTimeout(() => { + setTimeout(() => Module['setStatus'](''), 1); + doRun(); + }, 1); + } else { + doRun(); + } + } + + var wasmExports; + + // With async instantation wasmExports is assigned asynchronously when the + // instance is received. + createWasm(); + + run(); + /** + * Emscripten resolves `localhost` to a random IP address. Let's + * make it always resolve to 127.0.0.1. + */ + DNS.address_map.addrs.localhost = '127.0.0.1'; + + /** + * Debugging Asyncify errors is tricky because the stack trace is lost when the + * error is thrown. This code saves the stack trace in a global variable + * so that it can be inspected later. + */ + PHPLoader.debug = 'debug' in PHPLoader ? PHPLoader.debug : true; + if (PHPLoader.debug && typeof Asyncify !== 'undefined') { + const originalHandleSleep = Asyncify.handleSleep; + Asyncify.handleSleep = function (startAsync) { + if (!ABORT) { + Module['lastAsyncifyStackSource'] = new Error(); + } + return originalHandleSleep(startAsync); + }; + } + + /** + * Data dependencies call removeRunDependency() when they are loaded. + * The synchronous call stack then continues to run. If an error occurs + * in PHP initialization, e.g. Out Of Memory error, it will not be + * caught by any try/catch. This override propagates the failure to + * PHPLoader.onAbort() so that it can be handled. + */ + const originalRemoveRunDependency = PHPLoader['removeRunDependency']; + PHPLoader['removeRunDependency'] = function (...args) { + try { + originalRemoveRunDependency(...args); + } catch (e) { + PHPLoader['onAbort'](e); + } + }; + + if (typeof NODEFS === 'object') { + // We override NODEFS.createNode() to add an `isSharedFS` flag to all NODEFS + // nodes. This way we can tell whether file-locking is needed and possible + // for an FS node, even if wrapped with PROXYFS. + const originalNodeFsCreateNode = NODEFS.createNode; + NODEFS.createNode = function createNodeWithSharedFlag() { + const node = originalNodeFsCreateNode.apply(NODEFS, arguments); + node.isSharedFS = true; + return node; + }; + + var originalHashAddNode = FS.hashAddNode; + FS.hashAddNode = function hashAddNodeIfNotSharedFS(node) { + if (node?.isSharedFS) { + // Avoid caching shared VFS nodes so multiple instances + // can access the same underlying filesystem without + // conflicting caches. + return; + } + return originalHashAddNode.apply(FS, arguments); + }; + } + + /** + * Expose the PHP version so the PHP class can make version-specific + * adjustments to `php.ini`. + */ + PHPLoader['phpVersion'] = (() => { + const [major, minor, patch] = phpVersionString.split('.').map(Number); + return { major, minor, patch }; + })(); + + return PHPLoader; + + // Close the opening bracket from esm-prefix.js: +} diff --git a/packages/php-wasm/node-builds/5-2/package.json b/packages/php-wasm/node-builds/5-2/package.json new file mode 100644 index 00000000000..ecfd37a56c2 --- /dev/null +++ b/packages/php-wasm/node-builds/5-2/package.json @@ -0,0 +1,38 @@ +{ + "name": "@php-wasm/node-5-2", + "version": "3.1.19", + "description": "PHP 5.2 WebAssembly binaries for node (legacy)", + "repository": { + "type": "git", + "url": "https://github.com/WordPress/wordpress-playground" + }, + "homepage": "https://developer.wordpress.org/playground", + "author": "The WordPress contributors", + "contributors": [ + { + "name": "Adam Zielinski", + "email": "adam@adamziel.com", + "url": "https://github.com/adamziel" + } + ], + "exports": { + ".": { + "import": "./index.js", + "require": "./index.cjs" + }, + "./package.json": "./package.json" + }, + "publishConfig": { + "access": "public", + "directory": "../../../../dist/packages/php-wasm/node-builds/5-2" + }, + "type": "module", + "main": "./index.cjs", + "module": "./index.js", + "types": "index.d.ts", + "license": "GPL-2.0-or-later", + "engines": { + "node": ">=20.10.0", + "npm": ">=10.2.3" + } +} diff --git a/packages/php-wasm/node-builds/5-2/project.json b/packages/php-wasm/node-builds/5-2/project.json new file mode 100644 index 00000000000..c14ab676c76 --- /dev/null +++ b/packages/php-wasm/node-builds/5-2/project.json @@ -0,0 +1,61 @@ +{ + "name": "php-wasm-node-5-2", + "$schema": "../../../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "packages/php-wasm/node-builds/5-2/src", + "projectType": "library", + "implicitDependencies": ["php-wasm-compile"], + "targets": { + "build": { + "executor": "nx:noop", + "dependsOn": ["build:copy-assets"] + }, + "build:mkdir": { + "executor": "nx:run-commands", + "options": { + "commands": ["mkdir -p dist/packages/php-wasm/node-builds/5-2"], + "parallel": false + } + }, + "build:package-json": { + "executor": "@wp-playground/nx-extensions:package-json", + "options": { + "tsConfig": "packages/php-wasm/node-builds/5-2/tsconfig.lib.json", + "outputPath": "dist/packages/php-wasm/node-builds/5-2", + "buildTarget": "php-wasm-node-5-2:build:bundle:production" + }, + "dependsOn": ["build:mkdir"] + }, + "build:bundle": { + "executor": "nx:run-commands", + "options": { + "command": "node packages/php-wasm/node-builds/5-2/build.js", + "parallel": false + }, + "dependsOn": ["build:mkdir"] + }, + "build:copy-assets": { + "executor": "nx:run-commands", + "options": { + "commands": [ + "cp -rf packages/php-wasm/node-builds/5-2/jspi dist/packages/php-wasm/node-builds/5-2/", + "cp -rf packages/php-wasm/node-builds/5-2/asyncify dist/packages/php-wasm/node-builds/5-2/" + ], + "parallel": false + }, + "dependsOn": ["build:package-json"] + }, + "publish": { + "executor": "nx:run-commands", + "options": { + "command": "node tools/scripts/publish.mjs php-wasm-node-5-2 {args.ver} {args.tag}", + "parallel": false + }, + "dependsOn": ["build"] + }, + "package-for-self-hosting": { + "executor": "@wp-playground/nx-extensions:package-for-self-hosting", + "dependsOn": ["build"] + } + }, + "tags": ["scope:php-binaries"] +} diff --git a/packages/php-wasm/node-builds/5-2/src/index.ts b/packages/php-wasm/node-builds/5-2/src/index.ts new file mode 100644 index 00000000000..89f00dffb2a --- /dev/null +++ b/packages/php-wasm/node-builds/5-2/src/index.ts @@ -0,0 +1,51 @@ +import type { PHPLoaderModule } from '@php-wasm/universal'; +import { jspi } from 'wasm-feature-detect'; +import { fileURLToPath } from 'node:url'; +import { dirname, join } from 'node:path'; +import { existsSync } from 'node:fs'; + +// Determine the current directory path. In CJS mode, __dirname is available. +// In ESM mode, we derive it from import.meta.url. +// We use a type assertion to avoid TypeScript errors about __dirname in ESM. +declare const __dirname: string | undefined; +const currentDirPath = + typeof __dirname !== 'undefined' + ? __dirname + : dirname(fileURLToPath(import.meta.url)); +// In development, the file is in src/ so we need to go up one level. +// In the built package, the file is at the package root. +// Detect by checking if jspi/ exists in the current directory. +// Unused on legacy 5.2 today (no JSPI-gated extensions), but kept so +// future cherry-picks from newer version packages apply cleanly. +const packageRoot = existsSync(join(currentDirPath, 'jspi')) + ? currentDirPath + : dirname(currentDirPath); +void packageRoot; + +export async function getPHPLoaderModule(): Promise { + if (await jspi()) { + // @ts-ignore + return await import('../jspi/php_5_2.js'); + } else { + // @ts-ignore + return await import('../asyncify/php_5_2.js'); + } +} + +export async function getIntlExtensionPath(): Promise { + throw new Error('The intl extension is not available for PHP 5.2.'); +} + +export async function getXdebugExtensionPath(): Promise { + throw new Error('The Xdebug extension is not available for PHP 5.2.'); +} + +export async function getRedisExtensionPath(): Promise { + throw new Error('The Redis extension is not available for PHP 5.2.'); +} + +export async function getMemcachedExtensionPath(): Promise { + throw new Error('The Memcached extension is not available for PHP 5.2.'); +} + +export { jspi }; diff --git a/packages/php-wasm/node-builds/5-2/tsconfig.json b/packages/php-wasm/node-builds/5-2/tsconfig.json new file mode 100644 index 00000000000..b943cbfe62a --- /dev/null +++ b/packages/php-wasm/node-builds/5-2/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "../../../../tsconfig.base.json", + "compilerOptions": { + "module": "ESNext", + "moduleResolution": "bundler", + "allowJs": true, + "checkJs": false + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + } + ] +} diff --git a/packages/php-wasm/node-builds/5-2/tsconfig.lib.json b/packages/php-wasm/node-builds/5-2/tsconfig.lib.json new file mode 100644 index 00000000000..3561dce32f8 --- /dev/null +++ b/packages/php-wasm/node-builds/5-2/tsconfig.lib.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../../dist/packages/php-wasm/node-builds/5-2", + "declaration": true, + "types": ["node"] + }, + "include": ["src/**/*.ts"], + "exclude": ["**/*.spec.ts", "**/*.test.ts"] +} diff --git a/packages/php-wasm/node/src/lib/extensions/memcached/get-memcached-extension-module.ts b/packages/php-wasm/node/src/lib/extensions/memcached/get-memcached-extension-module.ts index 8a300e8a2c0..61a04104ab3 100644 --- a/packages/php-wasm/node/src/lib/extensions/memcached/get-memcached-extension-module.ts +++ b/packages/php-wasm/node/src/lib/extensions/memcached/get-memcached-extension-module.ts @@ -16,25 +16,39 @@ export async function getMemcachedExtensionModule( switch (version) { case '8.5': // @ts-ignore - return (await import('@php-wasm/node-8-5')).getMemcachedExtensionPath(); + return ( + await import('@php-wasm/node-8-5') + ).getMemcachedExtensionPath(); case '8.4': // @ts-ignore - return (await import('@php-wasm/node-8-4')).getMemcachedExtensionPath(); + return ( + await import('@php-wasm/node-8-4') + ).getMemcachedExtensionPath(); case '8.3': // @ts-ignore - return (await import('@php-wasm/node-8-3')).getMemcachedExtensionPath(); + return ( + await import('@php-wasm/node-8-3') + ).getMemcachedExtensionPath(); case '8.2': // @ts-ignore - return (await import('@php-wasm/node-8-2')).getMemcachedExtensionPath(); + return ( + await import('@php-wasm/node-8-2') + ).getMemcachedExtensionPath(); case '8.1': // @ts-ignore - return (await import('@php-wasm/node-8-1')).getMemcachedExtensionPath(); + return ( + await import('@php-wasm/node-8-1') + ).getMemcachedExtensionPath(); case '8.0': // @ts-ignore - return (await import('@php-wasm/node-8-0')).getMemcachedExtensionPath(); + return ( + await import('@php-wasm/node-8-0') + ).getMemcachedExtensionPath(); case '7.4': // @ts-ignore - return (await import('@php-wasm/node-7-4')).getMemcachedExtensionPath(); + return ( + await import('@php-wasm/node-7-4') + ).getMemcachedExtensionPath(); } throw new Error(`Unsupported PHP version ${version}`); } diff --git a/packages/php-wasm/node/src/lib/get-php-loader-module.ts b/packages/php-wasm/node/src/lib/get-php-loader-module.ts index 7c736466072..c3fb1881588 100644 --- a/packages/php-wasm/node/src/lib/get-php-loader-module.ts +++ b/packages/php-wasm/node/src/lib/get-php-loader-module.ts @@ -1,5 +1,5 @@ import { LatestSupportedPHPVersion } from '@php-wasm/universal'; -import type { PHPLoaderModule, SupportedPHPVersion } from '@php-wasm/universal'; +import type { AllPHPVersion, PHPLoaderModule } from '@php-wasm/universal'; /** * Loads the PHP loader module for the given PHP version. @@ -14,7 +14,7 @@ import type { PHPLoaderModule, SupportedPHPVersion } from '@php-wasm/universal'; * @returns The PHP loader module. */ export async function getPHPLoaderModule( - version: SupportedPHPVersion | string = LatestSupportedPHPVersion + version: AllPHPVersion = LatestSupportedPHPVersion ): Promise { try { switch (version) { @@ -53,6 +53,11 @@ export async function getPHPLoaderModule( return ( await import('@php-wasm/node-7-4') ).getPHPLoaderModule(); + case '5.2': + // @ts-ignore + return ( + await import('@php-wasm/node-5-2') + ).getPHPLoaderModule(); } throw new Error(`Unsupported PHP version ${version}`); diff --git a/packages/php-wasm/node/src/lib/load-runtime.ts b/packages/php-wasm/node/src/lib/load-runtime.ts index b23f5f40dec..6bf92ca982e 100644 --- a/packages/php-wasm/node/src/lib/load-runtime.ts +++ b/packages/php-wasm/node/src/lib/load-runtime.ts @@ -1,4 +1,5 @@ import { + type AllPHPVersion, type SupportedPHPVersion, type EmscriptenOptions, type PHPRuntime, @@ -6,6 +7,8 @@ import { loadPHPRuntime, FSHelpers, FileLockManagerComposite, + createLegacyPhpIniPreRunStep, + isLegacyPHPVersion, ProcessIdAllocator, } from '@php-wasm/universal'; import type { WasmUserSpaceAPI, WasmUserSpaceContext } from './wasm-user-space'; @@ -98,7 +101,7 @@ const dangerousDefaultProcessIdAllocator = (process.env as any).VITEST * @see load */ export async function loadNodeRuntime( - phpVersion: SupportedPHPVersion, + phpVersion: AllPHPVersion, options: PHPLoaderOptionsForNode = {} ) { const processId = @@ -110,6 +113,8 @@ export async function loadNodeRuntime( ? dangerousDefaultProcessIdAllocator!.claim() : undefined); + const isLegacy = isLegacyPHPVersion(phpVersion); + let emscriptenOptions: EmscriptenOptions = { /** * Emscripten default behavior is to kill the process when @@ -134,6 +139,18 @@ export async function loadNodeRuntime( }, ...(options.emscriptenOptions || {}), processId, + // For legacy PHP: pre-create php.ini via a preRun step. See + // createLegacyPhpIniPreRunStep for why this must run before + // the PHP SAPI starts. Merge with any caller-provided preRun + // hooks (the spread above may have set them). + ...(isLegacy + ? { + preRun: [ + createLegacyPhpIniPreRunStep(), + ...((options.emscriptenOptions as any)?.preRun ?? []), + ], + } + : {}), onRuntimeInitialized: (phpRuntime: PHPRuntime) => { /** * When users mount a directory using the `mount` function, @@ -287,24 +304,46 @@ export async function loadNodeRuntime( }, }; - if (options?.withXdebug) { - emscriptenOptions = await withXdebug( - phpVersion, - emscriptenOptions, - typeof options.withXdebug === 'object' ? options.withXdebug : {} + if ( + isLegacy && + (options?.withXdebug || + options?.withIntl || + options?.withRedis || + options?.withMemcached) + ) { + throw new Error( + `Extensions (xdebug, intl, redis, memcached) are not ` + + `available for legacy PHP ${phpVersion}.` ); } - if (options?.withIntl === true) { - emscriptenOptions = await withIntl(phpVersion, emscriptenOptions); - } - - if (options?.withRedis === true) { - emscriptenOptions = await withRedis(phpVersion, emscriptenOptions); - } - - if (options?.withMemcached === true) { - emscriptenOptions = await withMemcached(phpVersion, emscriptenOptions); + if (!isLegacy) { + const modernVersion = phpVersion as SupportedPHPVersion; + if (options?.withXdebug) { + emscriptenOptions = await withXdebug( + modernVersion, + emscriptenOptions, + typeof options.withXdebug === 'object' ? options.withXdebug : {} + ); + } + if (options?.withIntl === true) { + emscriptenOptions = await withIntl( + modernVersion, + emscriptenOptions + ); + } + if (options?.withRedis === true) { + emscriptenOptions = await withRedis( + modernVersion, + emscriptenOptions + ); + } + if (options?.withMemcached === true) { + emscriptenOptions = await withMemcached( + modernVersion, + emscriptenOptions + ); + } } emscriptenOptions = await withNetworking(emscriptenOptions); diff --git a/packages/php-wasm/supported-php-versions.mjs b/packages/php-wasm/supported-php-versions.mjs index 3ca0bb78244..f32806feb42 100644 --- a/packages/php-wasm/supported-php-versions.mjs +++ b/packages/php-wasm/supported-php-versions.mjs @@ -54,5 +54,11 @@ export const phpVersions = [ loaderFilename: 'php_7_4.js', wasmFilename: 'php_7_4.wasm', lastRelease: '7.4.33', - } + }, + { + version: '5.2', + loaderFilename: 'php_5_2.js', + wasmFilename: 'php_5_2.wasm', + lastRelease: '5.2.17', + }, ]; diff --git a/packages/php-wasm/universal/src/lib/index.ts b/packages/php-wasm/universal/src/lib/index.ts index 34a8c0b47e9..ad3fa8989e2 100644 --- a/packages/php-wasm/universal/src/lib/index.ts +++ b/packages/php-wasm/universal/src/lib/index.ts @@ -46,11 +46,23 @@ export { PHPResponse, StreamedPHPResponse } from './php-response'; export type { PHPResponseData } from './php-response'; export type { ErrnoError } from './rethrow-file-system-error'; export { + AllPHPVersions, + isLegacyPHPVersion, LatestSupportedPHPVersion, + LegacyPHPVersions, SupportedPHPVersions, SupportedPHPVersionsList, } from './supported-php-versions'; -export type { SupportedPHPVersion } from './supported-php-versions'; +export type { + AllPHPVersion, + LegacyPHPVersion, + SupportedPHPVersion, +} from './supported-php-versions'; +export { + createLegacyPhpIniPreRunStep, + LEGACY_PHP_INI_CONTENT, + LEGACY_PHP_INI_PATH, +} from './legacy-php-ini'; export { PHP, __private__dont__use, PHPExecutionFailureError } from './php'; export type { MountHandler, UnmountFunction } from './php'; export { loadPHPRuntime, popLoadedRuntime } from './load-php-runtime'; diff --git a/packages/php-wasm/universal/src/lib/legacy-php-ini.ts b/packages/php-wasm/universal/src/lib/legacy-php-ini.ts new file mode 100644 index 00000000000..476ff6aae9a --- /dev/null +++ b/packages/php-wasm/universal/src/lib/legacy-php-ini.ts @@ -0,0 +1,62 @@ +/** + * Pre-boot php.ini content for legacy PHP builds (currently 5.2). + * + * Why pre-create a php.ini before the SAPI starts: + * + * - ini_get_all() crashes on legacy PHP WASM (null function pointer in + * the Asyncify instrumentation) so we disable it via + * `disable_functions`, which PHP only reads during module startup. + * - OPcache's shared-memory allocator fails in our WASM environment, + * so opcache must be disabled before php_module_startup runs. + * + * Both settings are processed by php_module_startup before PHP reads + * any user code, so setting them at runtime via `ini_set()` is too + * late. The Emscripten preRun hook below writes the ini file before + * PHP.initializeRuntime invokes __wasm_call_ctors, which in turn + * invokes wasm_sapi_module_startup → php_module_startup. + * + * PHP.initializeRuntime will skip writing its own default php.ini + * when the file already exists. + */ + +export const LEGACY_PHP_INI_PATH = '/internal/shared/php.ini'; + +export const LEGACY_PHP_INI_CONTENT = [ + 'auto_prepend_file=/internal/shared/auto_prepend_file.php', + 'memory_limit=256M', + 'ignore_repeated_errors = 1', + 'error_reporting = E_ALL', + 'display_errors = 1', + 'html_errors = 1', + 'display_startup_errors = On', + 'log_errors = 1', + 'always_populate_raw_post_data = -1', + 'upload_max_filesize = 2000M', + 'post_max_size = 2000M', + 'allow_url_fopen = On', + 'allow_url_include = Off', + 'session.save_path = /home/web_user', + 'implicit_flush = 1', + 'output_buffering = 0', + 'max_execution_time = 0', + 'max_input_time = -1', + 'disable_functions = ini_get_all', + 'opcache.enable = 0', + 'opcache.enable_cli = 0', +].join('\n'); + +/** + * Returns an Emscripten preRun callback that writes + * {@link LEGACY_PHP_INI_CONTENT} to {@link LEGACY_PHP_INI_PATH}. + */ +export function createLegacyPhpIniPreRunStep(): (module: { + FS: { + mkdirTree: (path: string) => void; + writeFile: (path: string, content: string) => void; + }; +}) => void { + return (module) => { + module.FS.mkdirTree('/internal/shared'); + module.FS.writeFile(LEGACY_PHP_INI_PATH, LEGACY_PHP_INI_CONTENT); + }; +} diff --git a/packages/php-wasm/universal/src/lib/proxy-file-system.ts b/packages/php-wasm/universal/src/lib/proxy-file-system.ts index 23d9c537790..507b9b389df 100644 --- a/packages/php-wasm/universal/src/lib/proxy-file-system.ts +++ b/packages/php-wasm/universal/src/lib/proxy-file-system.ts @@ -1,5 +1,24 @@ import type { PHP } from './php'; +/** + * Reads the major PHP version from an instance's Emscripten runtime and + * returns true for anything older than PHP 7. Used to decide whether the + * PROXYFS mmap patch should apply — see {@link proxyFileSystem} for the + * legacy-PHP rationale. Falls back to `false` when the runtime hasn't + * populated phpVersion yet, matching the pre-refactor default behaviour. + */ +function isLegacyPhpInstance(phpInstance: PHP): boolean { + // We can't import __private__dont__use because Playground CLI is + // built as ESM and php-wasm-node is built as CJS — the imported + // symbols would differ in the production build. Use + // getOwnPropertySymbols()[0] like the rest of this file. + const __private__symbol = Object.getOwnPropertySymbols(phpInstance)[0]; + // @ts-ignore + const runtime = phpInstance[__private__symbol]; + const major: number | undefined = runtime?.phpVersion?.major; + return typeof major === 'number' && major < 7; +} + /** * Adds mmap support to PROXYFS for memory-mapping files across PHP instances. * @@ -136,8 +155,20 @@ function ensureProxyFSHasMmapSupport(phpInstance: PHP) { * For example, mounting /wordpress from the parent instance into a child worker allows * both to access the same WordPress installation without copying the entire directory. * - * The function automatically patches PROXYFS with mmap support before mounting, ensuring - * libraries like ICU can memory-map data files through the proxied filesystem. + * The function automatically patches PROXYFS with mmap support before mounting on + * PHP 7+, so libraries like ICU can memory-map data files through the proxied + * filesystem. + * + * Legacy PHP (< 7) skips the mmap patch. PHP 5.x's zend_compile_file + * calls mmap() via zend_stream_fixup and trusts fstat() for the buffer + * size. When a file is pre-populated in MEMFS before PROXYFS is mounted + * over that path (e.g. php.ini written by preRun), fstat() can return + * the stale MEMFS size instead of the PROXYFS size, causing the parser + * to read past the real EOF and report spurious syntax errors. Without + * the mmap patch, Emscripten returns ENOSYS from mmap() and PHP falls + * back to a read-based path that handles size mismatches gracefully. + * PHP 7+ removed the mmap path from zend_stream_fixup entirely, so the + * patch is both safe and needed there (for ICU/intl). * * Mounts are registered via php.mount() so they survive runtime rotation. * When the replica's WASM module is hot-swapped, hotSwapPHPRuntime() @@ -152,6 +183,7 @@ export async function proxyFileSystem( replica: PHP, paths: string[] ) { + const replicaIsLegacy = isLegacyPhpInstance(replica); // We can't just import the symbol from the library because // Playground CLI is built as ESM and php-wasm-node is built as // CJS and the imported symbols will differ in the production build. @@ -164,7 +196,9 @@ export async function proxyFileSystem( // after runtime rotation in hotSwapPHPRuntime(). replica.mkdir(path); await replica.mount(path, (php: PHP) => { - ensureProxyFSHasMmapSupport(php); + if (!replicaIsLegacy) { + ensureProxyFSHasMmapSupport(php); + } const replicaSymbol = Object.getOwnPropertySymbols(php)[0]; // @ts-ignore php[replicaSymbol].FS.mount( diff --git a/packages/php-wasm/universal/src/lib/supported-php-versions.ts b/packages/php-wasm/universal/src/lib/supported-php-versions.ts index 64050475b1c..2f962206912 100644 --- a/packages/php-wasm/universal/src/lib/supported-php-versions.ts +++ b/packages/php-wasm/universal/src/lib/supported-php-versions.ts @@ -10,3 +10,23 @@ export const SupportedPHPVersions = [ export const LatestSupportedPHPVersion = SupportedPHPVersions[0]; export const SupportedPHPVersionsList = SupportedPHPVersions as any as string[]; export type SupportedPHPVersion = (typeof SupportedPHPVersions)[number]; + +export const LegacyPHPVersions = ['5.2'] as const; +export type LegacyPHPVersion = (typeof LegacyPHPVersions)[number]; + +/** + * Type guard for legacy PHP versions. Lets callers narrow a string + * to `LegacyPHPVersion` without the `as readonly string[]` cast that + * would otherwise be required to satisfy `Array.prototype.includes`. + */ +export function isLegacyPHPVersion( + version: string | undefined +): version is LegacyPHPVersion { + return (LegacyPHPVersions as readonly string[]).includes(version ?? ''); +} + +export const AllPHPVersions = [ + ...SupportedPHPVersions, + ...LegacyPHPVersions, +] as const; +export type AllPHPVersion = SupportedPHPVersion | LegacyPHPVersion; diff --git a/packages/php-wasm/web-builds/5-2/asyncify/5_2_17/php_5_2.wasm b/packages/php-wasm/web-builds/5-2/asyncify/5_2_17/php_5_2.wasm new file mode 100755 index 00000000000..4d911cb9a7f Binary files /dev/null and b/packages/php-wasm/web-builds/5-2/asyncify/5_2_17/php_5_2.wasm differ diff --git a/packages/php-wasm/web-builds/5-2/asyncify/php_5_2.js b/packages/php-wasm/web-builds/5-2/asyncify/php_5_2.js new file mode 100644 index 00000000000..c9a3d9b95b8 --- /dev/null +++ b/packages/php-wasm/web-builds/5-2/asyncify/php_5_2.js @@ -0,0 +1,8749 @@ +import dependencyFilename from './5_2_17/php_5_2.wasm'; +export { dependencyFilename }; +export const dependenciesTotalSize = 9781068; +const phpVersionString = '5.2.17'; +export function init(RuntimeName, PHPLoader) { + // The rest of the code comes from the built php.js file and esm-suffix.js + var Module = typeof PHPLoader != 'undefined' ? PHPLoader : {}; + var ENVIRONMENT_IS_WEB = RuntimeName === 'WEB'; + var ENVIRONMENT_IS_WORKER = RuntimeName === 'WORKER'; + var ENVIRONMENT_IS_NODE = RuntimeName === 'NODE'; + var arguments_ = []; + var thisProgram = './this.program'; + var quit_ = (status, toThrow) => { + throw toThrow; + }; + var _scriptName = globalThis.document?.currentScript?.src; + if (ENVIRONMENT_IS_WORKER) { + _scriptName = self.location.href; + } + var scriptDirectory = ''; + function locateFile(path) { + if (Module['locateFile']) { + return Module['locateFile'](path, scriptDirectory); + } + return scriptDirectory + path; + } + var readAsync, readBinary; + if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { + try { + scriptDirectory = new URL('.', _scriptName).href; + } catch {} + { + if (ENVIRONMENT_IS_WORKER) { + readBinary = (url) => { + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, false); + xhr.responseType = 'arraybuffer'; + xhr.send(null); + return new Uint8Array(xhr.response); + }; + } + readAsync = async (url) => { + var response = await fetch(url, { credentials: 'same-origin' }); + if (response.ok) { + return response.arrayBuffer(); + } + throw new Error(response.status + ' : ' + response.url); + }; + } + } else { + } + var out = console.log.bind(console); + var err = console.error.bind(console); + var dynamicLibraries = []; + var wasmBinary; + var ABORT = false; + var EXITSTATUS; + var HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64; + var HEAP64, HEAPU64; + var runtimeInitialized = false; + var runtimeExited = false; + function updateMemoryViews() { + var b = wasmMemory.buffer; + HEAP8 = new Int8Array(b); + HEAP16 = new Int16Array(b); + Module['HEAPU8'] = HEAPU8 = new Uint8Array(b); + HEAPU16 = new Uint16Array(b); + HEAP32 = new Int32Array(b); + Module['HEAPU32'] = HEAPU32 = new Uint32Array(b); + HEAPF32 = new Float32Array(b); + HEAPF64 = new Float64Array(b); + HEAP64 = new BigInt64Array(b); + HEAPU64 = new BigUint64Array(b); + } + var __RELOC_FUNCS__ = []; + function preRun() { + if (Module['preRun']) { + if (typeof Module['preRun'] == 'function') + Module['preRun'] = [Module['preRun']]; + while (Module['preRun'].length) { + addOnPreRun(Module['preRun'].shift()); + } + } + callRuntimeCallbacks(onPreRuns); + } + function initRuntime() { + runtimeInitialized = true; + callRuntimeCallbacks(__RELOC_FUNCS__); + callRuntimeCallbacks(onInits); + if (!Module['noFSInit'] && !FS.initialized) FS.init(); + TTY.init(); + SOCKFS.root = FS.mount(SOCKFS, {}, null); + PIPEFS.root = FS.mount(PIPEFS, {}, null); + wasmExports['__wasm_call_ctors'](); + callRuntimeCallbacks(onPostCtors); + FS.ignorePermissions = false; + } + function preMain() {} + function exitRuntime() { + ___funcs_on_exit(); + FS.quit(); + TTY.shutdown(); + runtimeExited = true; + } + function postRun() { + if (Module['postRun']) { + if (typeof Module['postRun'] == 'function') + Module['postRun'] = [Module['postRun']]; + while (Module['postRun'].length) { + addOnPostRun(Module['postRun'].shift()); + } + } + callRuntimeCallbacks(onPostRuns); + } + function abort(what) { + Module['onAbort']?.(what); + what = 'Aborted(' + what + ')'; + err(what); + ABORT = true; + what += '. Build with -sASSERTIONS for more info.'; + var e = new WebAssembly.RuntimeError(what); + throw e; + } + var wasmBinaryFile; + function findWasmBinary() { + return locateFile(dependencyFilename); + } + function getBinarySync(file) { + if (file == wasmBinaryFile && wasmBinary) { + return new Uint8Array(wasmBinary); + } + if (readBinary) { + return readBinary(file); + } + throw 'both async and sync fetching of the wasm failed'; + } + async function getWasmBinary(binaryFile) { + if (!wasmBinary) { + try { + var response = await readAsync(binaryFile); + return new Uint8Array(response); + } catch {} + } + return getBinarySync(binaryFile); + } + async function instantiateArrayBuffer(binaryFile, imports) { + try { + var binary = await getWasmBinary(binaryFile); + var instance = await WebAssembly.instantiate(binary, imports); + return instance; + } catch (reason) { + err(`failed to asynchronously prepare wasm: ${reason}`); + abort(reason); + } + } + async function instantiateAsync(binary, binaryFile, imports) { + if (!binary) { + try { + var response = fetch(binaryFile, { + credentials: 'same-origin', + }); + var instantiationResult = + await WebAssembly.instantiateStreaming(response, imports); + return instantiationResult; + } catch (reason) { + err(`wasm streaming compile failed: ${reason}`); + err('falling back to ArrayBuffer instantiation'); + } + } + return instantiateArrayBuffer(binaryFile, imports); + } + function getWasmImports() { + var imports = { + env: wasmImports, + wasi_snapshot_preview1: wasmImports, + 'GOT.mem': new Proxy(wasmImports, GOTHandler), + 'GOT.func': new Proxy(wasmImports, GOTHandler), + }; + return imports; + } + async function createWasm() { + function receiveInstance(instance, module) { + wasmExports = instance.exports; + var origExports = (wasmExports = relocateExports(wasmExports)); + wasmExports = Asyncify.instrumentWasmExports(wasmExports); + mergeLibSymbols(wasmExports, 'main'); + var metadata = getDylinkMetadata(module); + if (metadata.neededDynlibs) { + dynamicLibraries = + metadata.neededDynlibs.concat(dynamicLibraries); + } + assignWasmExports(wasmExports); + updateGOT(origExports); + Module['wasmExports'] = wasmExports; + LDSO.init(); + loadDylibs(); + updateMemoryViews(); + removeRunDependency('wasm-instantiate'); + return wasmExports; + } + addRunDependency('wasm-instantiate'); + function receiveInstantiationResult(result) { + return receiveInstance(result['instance'], result['module']); + } + var info = getWasmImports(); + if (Module['instantiateWasm']) { + return new Promise((resolve, reject) => { + Module['instantiateWasm'](info, (inst, mod) => { + resolve(receiveInstance(inst, mod)); + }); + }); + } + wasmBinaryFile ??= findWasmBinary(); + var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info); + var exports = receiveInstantiationResult(result); + return exports; + } + var asyncifyStubs = {}; + class ExitStatus { + name = 'ExitStatus'; + constructor(status) { + this.message = `Program terminated with exit(${status})`; + this.status = status; + } + } + ExitStatus = class PHPExitStatus extends Error { + constructor(status) { + super(status); + this.name = 'ExitStatus'; + this.message = 'Program terminated with exit(' + status + ')'; + this.status = status; + } + }; + var GOT = {}; + var currentModuleWeakSymbols = new Set([]); + var GOTHandler = { + get(obj, symName) { + var rtn = GOT[symName]; + if (!rtn) { + rtn = GOT[symName] = new WebAssembly.Global( + { value: 'i32', mutable: true }, + -1 + ); + } + if (!currentModuleWeakSymbols.has(symName)) { + rtn.required = true; + } + return rtn; + }, + }; + var callRuntimeCallbacks = (callbacks) => { + while (callbacks.length > 0) { + callbacks.shift()(Module); + } + }; + var onPostRuns = []; + var addOnPostRun = (cb) => onPostRuns.push(cb); + var onPreRuns = []; + var addOnPreRun = (cb) => onPreRuns.push(cb); + var runDependencies = 0; + var dependenciesFulfilled = null; + var removeRunDependency = (id) => { + runDependencies--; + Module['monitorRunDependencies']?.(runDependencies); + if (runDependencies == 0) { + if (dependenciesFulfilled) { + var callback = dependenciesFulfilled; + dependenciesFulfilled = null; + callback(); + } + } + }; + var addRunDependency = (id) => { + runDependencies++; + Module['monitorRunDependencies']?.(runDependencies); + }; + var dynCalls = {}; + var dynCallLegacy = (sig, ptr, args) => { + sig = sig.replace(/p/g, 'i'); + var f = dynCalls[sig]; + return f(ptr, ...args); + }; + var dynCall = (sig, ptr, args = [], promising = false) => { + var rtn = dynCallLegacy(sig, ptr, args); + function convert(rtn) { + return rtn; + } + return convert(rtn); + }; + var UTF8Decoder = globalThis.TextDecoder && new TextDecoder(); + var findStringEnd = (heapOrArray, idx, maxBytesToRead, ignoreNul) => { + var maxIdx = idx + maxBytesToRead; + if (ignoreNul) return maxIdx; + while (heapOrArray[idx] && !(idx >= maxIdx)) ++idx; + return idx; + }; + var UTF8ArrayToString = ( + heapOrArray, + idx = 0, + maxBytesToRead, + ignoreNul + ) => { + var endPtr = findStringEnd(heapOrArray, idx, maxBytesToRead, ignoreNul); + if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) { + return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr)); + } + var str = ''; + while (idx < endPtr) { + var u0 = heapOrArray[idx++]; + if (!(u0 & 128)) { + str += String.fromCharCode(u0); + continue; + } + var u1 = heapOrArray[idx++] & 63; + if ((u0 & 224) == 192) { + str += String.fromCharCode(((u0 & 31) << 6) | u1); + continue; + } + var u2 = heapOrArray[idx++] & 63; + if ((u0 & 240) == 224) { + u0 = ((u0 & 15) << 12) | (u1 << 6) | u2; + } else { + u0 = + ((u0 & 7) << 18) | + (u1 << 12) | + (u2 << 6) | + (heapOrArray[idx++] & 63); + } + if (u0 < 65536) { + str += String.fromCharCode(u0); + } else { + var ch = u0 - 65536; + str += String.fromCharCode( + 55296 | (ch >> 10), + 56320 | (ch & 1023) + ); + } + } + return str; + }; + var getDylinkMetadata = (binary) => { + var offset = 0; + var end = 0; + function getU8() { + return binary[offset++]; + } + function getLEB() { + var ret = 0; + var mul = 1; + while (1) { + var byte = binary[offset++]; + ret += (byte & 127) * mul; + mul *= 128; + if (!(byte & 128)) break; + } + return ret; + } + function getString() { + var len = getLEB(); + offset += len; + return UTF8ArrayToString(binary, offset - len, len); + } + function getStringList() { + var count = getLEB(); + var rtn = []; + while (count--) rtn.push(getString()); + return rtn; + } + function failIf(condition, message) { + if (condition) throw new Error(message); + } + if (binary instanceof WebAssembly.Module) { + var dylinkSection = WebAssembly.Module.customSections( + binary, + 'dylink.0' + ); + failIf(dylinkSection.length === 0, 'need dylink section'); + binary = new Uint8Array(dylinkSection[0]); + end = binary.length; + } else { + var int32View = new Uint32Array( + new Uint8Array(binary.subarray(0, 24)).buffer + ); + var magicNumberFound = int32View[0] == 1836278016; + failIf(!magicNumberFound, 'need to see wasm magic number'); + failIf(binary[8] !== 0, 'need the dylink section to be first'); + offset = 9; + var section_size = getLEB(); + end = offset + section_size; + var name = getString(); + failIf(name !== 'dylink.0'); + } + var customSection = { + neededDynlibs: [], + tlsExports: new Set(), + weakImports: new Set(), + runtimePaths: [], + }; + var WASM_DYLINK_MEM_INFO = 1; + var WASM_DYLINK_NEEDED = 2; + var WASM_DYLINK_EXPORT_INFO = 3; + var WASM_DYLINK_IMPORT_INFO = 4; + var WASM_DYLINK_RUNTIME_PATH = 5; + var WASM_SYMBOL_TLS = 256; + var WASM_SYMBOL_BINDING_MASK = 3; + var WASM_SYMBOL_BINDING_WEAK = 1; + while (offset < end) { + var subsectionType = getU8(); + var subsectionSize = getLEB(); + if (subsectionType === WASM_DYLINK_MEM_INFO) { + customSection.memorySize = getLEB(); + customSection.memoryAlign = getLEB(); + customSection.tableSize = getLEB(); + customSection.tableAlign = getLEB(); + } else if (subsectionType === WASM_DYLINK_NEEDED) { + customSection.neededDynlibs = getStringList(); + } else if (subsectionType === WASM_DYLINK_EXPORT_INFO) { + var count = getLEB(); + while (count--) { + var symname = getString(); + var flags = getLEB(); + if (flags & WASM_SYMBOL_TLS) { + customSection.tlsExports.add(symname); + } + } + } else if (subsectionType === WASM_DYLINK_IMPORT_INFO) { + var count = getLEB(); + while (count--) { + var modname = getString(); + var symname = getString(); + var flags = getLEB(); + if ( + (flags & WASM_SYMBOL_BINDING_MASK) == + WASM_SYMBOL_BINDING_WEAK + ) { + customSection.weakImports.add(symname); + } + } + } else if (subsectionType === WASM_DYLINK_RUNTIME_PATH) { + customSection.runtimePaths = getStringList(); + } else { + offset += subsectionSize; + } + } + return customSection; + }; + var newDSO = (name, handle, syms) => { + var dso = { refcount: Infinity, name, exports: syms, global: true }; + LDSO.loadedLibsByName[name] = dso; + if (handle != undefined) { + LDSO.loadedLibsByHandle[handle] = dso; + } + return dso; + }; + var LDSO = { + loadedLibsByName: {}, + loadedLibsByHandle: {}, + init() { + newDSO('__main__', 0, wasmImports); + }, + }; + var alignMemory = (size, alignment) => + Math.ceil(size / alignment) * alignment; + var getMemory = (size) => { + if (runtimeInitialized) { + return _calloc(size, 1); + } + var ret = ___heap_base; + var end = ret + alignMemory(size, 16); + ___heap_base = end; + var sbrk_ptr = _emscripten_get_sbrk_ptr(); + HEAPU32[sbrk_ptr >> 2] = end; + return ret; + }; + var isInternalSym = (symName) => + [ + 'memory', + '__memory_base', + '__table_base', + '__stack_pointer', + '__indirect_function_table', + '__cpp_exception', + '__c_longjmp', + '__wasm_apply_data_relocs', + '__dso_handle', + '__tls_size', + '__tls_align', + '__set_stack_limits', + '_emscripten_tls_init', + '__wasm_init_tls', + '__wasm_call_ctors', + '__start_em_asm', + '__stop_em_asm', + '__start_em_js', + '__stop_em_js', + ].includes(symName) || symName.startsWith('__em_js__'); + var wasmTableMirror = []; + var getWasmTableEntry = (funcPtr) => { + var func = wasmTableMirror[funcPtr]; + if (!func) { + wasmTableMirror[funcPtr] = func = wasmTable.get(funcPtr); + } + return func; + }; + var updateTableMap = (offset, count) => { + if (functionsInTableMap) { + for (var i = offset; i < offset + count; i++) { + var item = getWasmTableEntry(i); + if (item) { + functionsInTableMap.set(item, i); + } + } + } + }; + var functionsInTableMap; + var getFunctionAddress = (func) => { + if (!functionsInTableMap) { + functionsInTableMap = new WeakMap(); + updateTableMap(0, wasmTable.length); + } + return functionsInTableMap.get(func) || 0; + }; + var freeTableIndexes = []; + var getEmptyTableSlot = () => { + if (freeTableIndexes.length) { + return freeTableIndexes.pop(); + } + return wasmTable['grow'](1); + }; + var setWasmTableEntry = (idx, func) => { + wasmTable.set(idx, func); + wasmTableMirror[idx] = wasmTable.get(idx); + }; + var uleb128EncodeWithLen = (arr) => { + const n = arr.length; + return [(n % 128) | 128, n >> 7, ...arr]; + }; + var wasmTypeCodes = { i: 127, p: 127, j: 126, f: 125, d: 124, e: 111 }; + var generateTypePack = (types) => + uleb128EncodeWithLen( + Array.from(types, (type) => { + var code = wasmTypeCodes[type]; + return code; + }) + ); + var convertJsFunctionToWasm = (func, sig) => { + var bytes = Uint8Array.of( + 0, + 97, + 115, + 109, + 1, + 0, + 0, + 0, + 1, + ...uleb128EncodeWithLen([ + 1, + 96, + ...generateTypePack(sig.slice(1)), + ...generateTypePack(sig[0] === 'v' ? '' : sig[0]), + ]), + 2, + 7, + 1, + 1, + 101, + 1, + 102, + 0, + 0, + 7, + 5, + 1, + 1, + 102, + 0, + 0 + ); + var module = new WebAssembly.Module(bytes); + var instance = new WebAssembly.Instance(module, { e: { f: func } }); + var wrappedFunc = instance.exports['f']; + return wrappedFunc; + }; + var addFunction = (func, sig) => { + var rtn = getFunctionAddress(func); + if (rtn) { + return rtn; + } + var ret = getEmptyTableSlot(); + try { + setWasmTableEntry(ret, func); + } catch (err) { + if (!(err instanceof TypeError)) { + throw err; + } + var wrapped = convertJsFunctionToWasm(func, sig); + setWasmTableEntry(ret, wrapped); + } + functionsInTableMap.set(func, ret); + return ret; + }; + var updateGOT = (exports, replace) => { + for (var symName in exports) { + if (isInternalSym(symName)) { + continue; + } + var value = exports[symName]; + var existingEntry = GOT[symName] && GOT[symName].value != -1; + if (replace || !existingEntry) { + var newValue; + if (typeof value == 'function') { + newValue = addFunction(value); + } else if (typeof value == 'number') { + newValue = value; + } else { + continue; + } + GOT[symName] ??= new WebAssembly.Global({ + value: 'i32', + mutable: true, + }); + GOT[symName].value = newValue; + } + } + }; + var isImmutableGlobal = (val) => { + if (val instanceof WebAssembly.Global) { + try { + val.value = val.value; + } catch { + return true; + } + } + return false; + }; + var relocateExports = (exports, memoryBase = 0) => { + function relocateExport(name, value) { + if (isImmutableGlobal(value)) { + return value.value + memoryBase; + } + return value; + } + var relocated = {}; + for (var e in exports) { + relocated[e] = relocateExport(e, exports[e]); + } + return relocated; + }; + var isSymbolDefined = (symName) => { + var existing = wasmImports[symName]; + if (!existing || existing.stub) { + return false; + } + if (symName in asyncifyStubs && !asyncifyStubs[symName]) { + return false; + } + return true; + }; + var createNamedFunction = (name, func) => + Object.defineProperty(func, 'name', { value: name }); + var stackSave = () => _emscripten_stack_get_current(); + var stackRestore = (val) => __emscripten_stack_restore(val); + var createInvokeFunction = + (sig) => + (ptr, ...args) => { + var sp = stackSave(); + try { + return dynCall(sig, ptr, args); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + if (sig[0] == 'j') return 0n; + } + }; + var resolveGlobalSymbol = (symName, direct = false) => { + var sym; + if (isSymbolDefined(symName)) { + sym = wasmImports[symName]; + } else if (symName.startsWith('invoke_')) { + sym = wasmImports[symName] = createNamedFunction( + symName, + createInvokeFunction(symName.split('_')[1]) + ); + } + return { sym, name: symName }; + }; + var onPostCtors = []; + var addOnPostCtor = (cb) => onPostCtors.push(cb); + var UTF8ToString = (ptr, maxBytesToRead, ignoreNul) => + ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead, ignoreNul) : ''; + var loadWebAssemblyModule = ( + binary, + flags, + libName, + localScope, + handle + ) => { + var metadata = getDylinkMetadata(binary); + function loadModule() { + var memAlign = Math.pow(2, metadata.memoryAlign); + var memoryBase = metadata.memorySize + ? alignMemory( + getMemory(metadata.memorySize + memAlign), + memAlign + ) + : 0; + var tableBase = metadata.tableSize ? wasmTable.length : 0; + if (handle) { + HEAP8[handle + 8] = 1; + HEAPU32[(handle + 12) >> 2] = memoryBase; + HEAP32[(handle + 16) >> 2] = metadata.memorySize; + HEAPU32[(handle + 20) >> 2] = tableBase; + HEAP32[(handle + 24) >> 2] = metadata.tableSize; + } + if (metadata.tableSize) { + wasmTable.grow(metadata.tableSize); + } + var moduleExports; + function resolveSymbol(sym) { + var resolved = resolveGlobalSymbol(sym).sym; + if (!resolved && localScope) { + resolved = localScope[sym]; + } + if (!resolved) { + resolved = moduleExports[sym]; + } + return resolved; + } + var proxyHandler = { + get(stubs, prop) { + switch (prop) { + case '__memory_base': + return memoryBase; + case '__table_base': + return tableBase; + } + if (prop in wasmImports && !wasmImports[prop].stub) { + var res = wasmImports[prop]; + if (res.orig) { + res = res.orig; + } + return res; + } + if (!(prop in stubs)) { + var resolved; + stubs[prop] = (...args) => { + resolved ||= resolveSymbol(prop); + return resolved(...args); + }; + } + return stubs[prop]; + }, + }; + var proxy = new Proxy({}, proxyHandler); + currentModuleWeakSymbols = metadata.weakImports; + var info = { + 'GOT.mem': new Proxy({}, GOTHandler), + 'GOT.func': new Proxy({}, GOTHandler), + env: proxy, + wasi_snapshot_preview1: proxy, + }; + function postInstantiation(module, instance) { + updateTableMap(tableBase, metadata.tableSize); + moduleExports = relocateExports(instance.exports, memoryBase); + updateGOT(moduleExports); + moduleExports = Asyncify.instrumentWasmExports(moduleExports); + if (!flags.allowUndefined) { + reportUndefinedSymbols(); + } + function addEmAsm(addr, body) { + var args = []; + for (var arity = 0; ; arity++) { + var argName = '$' + arity; + if (!body.includes(argName)) break; + args.push(argName); + } + args = args.join(','); + var func = `(${args}) => { ${body} };`; + ASM_CONSTS[start] = eval(func); + } + if ('__start_em_asm' in moduleExports) { + var start = moduleExports['__start_em_asm']; + var stop = moduleExports['__stop_em_asm']; + while (start < stop) { + var jsString = UTF8ToString(start); + addEmAsm(start, jsString); + start = HEAPU8.indexOf(0, start) + 1; + } + } + function addEmJs(name, cSig, body) { + var jsArgs = []; + cSig = cSig.slice(1, -1); + if (cSig != 'void') { + cSig = cSig.split(','); + for (var arg of cSig) { + var jsArg = arg.split(' ').pop(); + jsArgs.push(jsArg.replace('*', '')); + } + } + var func = `(${jsArgs}) => ${body};`; + moduleExports[name] = eval(func); + } + for (var name in moduleExports) { + if (name.startsWith('__em_js__')) { + var start = moduleExports[name]; + var jsString = UTF8ToString(start); + var [sig, body] = jsString.split('<::>'); + addEmJs(name.replace('__em_js__', ''), sig, body); + delete moduleExports[name]; + } + } + var applyRelocs = moduleExports['__wasm_apply_data_relocs']; + if (applyRelocs) { + if (runtimeInitialized) { + applyRelocs(); + } else { + __RELOC_FUNCS__.push(applyRelocs); + } + } + var init = moduleExports['__wasm_call_ctors']; + if (init) { + if (runtimeInitialized) { + init(); + } else { + addOnPostCtor(init); + } + } + return moduleExports; + } + if (flags.loadAsync) { + return (async () => { + var instance; + if (binary instanceof WebAssembly.Module) { + instance = new WebAssembly.Instance(binary, info); + } else { + ({ module: binary, instance } = + await WebAssembly.instantiate(binary, info)); + } + return postInstantiation(binary, instance); + })(); + } + var module = + binary instanceof WebAssembly.Module + ? binary + : new WebAssembly.Module(binary); + var instance = new WebAssembly.Instance(module, info); + return postInstantiation(module, instance); + } + flags = { + ...flags, + rpath: { parentLibPath: libName, paths: metadata.runtimePaths }, + }; + if (flags.loadAsync) { + return metadata.neededDynlibs + .reduce( + (chain, dynNeeded) => + chain.then(() => + loadDynamicLibrary(dynNeeded, flags, localScope) + ), + Promise.resolve() + ) + .then(loadModule); + } + for (var needed of metadata.neededDynlibs) { + loadDynamicLibrary(needed, flags, localScope); + } + return loadModule(); + }; + var mergeLibSymbols = (exports, libName) => { + registerDynCallSymbols(exports); + for (var [sym, exp] of Object.entries(exports)) { + const setImport = (target) => { + if (target in asyncifyStubs) { + asyncifyStubs[target] = exp; + } + if (!isSymbolDefined(target)) { + wasmImports[target] = exp; + } + }; + setImport(sym); + const main_alias = '__main_argc_argv'; + if (sym == 'main') { + setImport(main_alias); + } + if (sym == main_alias) { + setImport('main'); + } + } + }; + var asyncLoad = async (url) => { + var arrayBuffer = await readAsync(url); + return new Uint8Array(arrayBuffer); + }; + var preloadPlugins = []; + var registerWasmPlugin = () => { + var wasmPlugin = { + promiseChainEnd: Promise.resolve(), + canHandle: (name) => + !Module['noWasmDecoding'] && name.endsWith('.so'), + handle: async (byteArray, name) => + (wasmPlugin.promiseChainEnd = wasmPlugin.promiseChainEnd.then( + async () => { + try { + var exports = await loadWebAssemblyModule( + byteArray, + { loadAsync: true, nodelete: true }, + name, + {} + ); + } catch (error) { + throw new Error( + `failed to instantiate wasm: ${name}: ${error}` + ); + } + preloadedWasm[name] = exports; + return byteArray; + } + )), + }; + preloadPlugins.push(wasmPlugin); + }; + var preloadedWasm = {}; + var PATH = { + isAbs: (path) => path.charAt(0) === '/', + splitPath: (filename) => { + var splitPathRe = + /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; + return splitPathRe.exec(filename).slice(1); + }, + normalizeArray: (parts, allowAboveRoot) => { + var up = 0; + for (var i = parts.length - 1; i >= 0; i--) { + var last = parts[i]; + if (last === '.') { + parts.splice(i, 1); + } else if (last === '..') { + parts.splice(i, 1); + up++; + } else if (up) { + parts.splice(i, 1); + up--; + } + } + if (allowAboveRoot) { + for (; up; up--) { + parts.unshift('..'); + } + } + return parts; + }, + normalize: (path) => { + var isAbsolute = PATH.isAbs(path), + trailingSlash = path.slice(-1) === '/'; + path = PATH.normalizeArray( + path.split('/').filter((p) => !!p), + !isAbsolute + ).join('/'); + if (!path && !isAbsolute) { + path = '.'; + } + if (path && trailingSlash) { + path += '/'; + } + return (isAbsolute ? '/' : '') + path; + }, + dirname: (path) => { + var result = PATH.splitPath(path), + root = result[0], + dir = result[1]; + if (!root && !dir) { + return '.'; + } + if (dir) { + dir = dir.slice(0, -1); + } + return root + dir; + }, + basename: (path) => path && path.match(/([^\/]+|\/)\/*$/)[1], + join: (...paths) => PATH.normalize(paths.join('/')), + join2: (l, r) => PATH.normalize(l + '/' + r), + }; + var replaceORIGIN = (parentLibName, rpath) => { + if (rpath.startsWith('$ORIGIN')) { + var origin = PATH.dirname(parentLibName); + return rpath.replace('$ORIGIN', origin); + } + return rpath; + }; + var withStackSave = (f) => { + var stack = stackSave(); + var ret = f(); + stackRestore(stack); + return ret; + }; + var stackAlloc = (sz) => __emscripten_stack_alloc(sz); + var lengthBytesUTF8 = (str) => { + var len = 0; + for (var i = 0; i < str.length; ++i) { + var c = str.charCodeAt(i); + if (c <= 127) { + len++; + } else if (c <= 2047) { + len += 2; + } else if (c >= 55296 && c <= 57343) { + len += 4; + ++i; + } else { + len += 3; + } + } + return len; + }; + var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => { + if (!(maxBytesToWrite > 0)) return 0; + var startIdx = outIdx; + var endIdx = outIdx + maxBytesToWrite - 1; + for (var i = 0; i < str.length; ++i) { + var u = str.codePointAt(i); + if (u <= 127) { + if (outIdx >= endIdx) break; + heap[outIdx++] = u; + } else if (u <= 2047) { + if (outIdx + 1 >= endIdx) break; + heap[outIdx++] = 192 | (u >> 6); + heap[outIdx++] = 128 | (u & 63); + } else if (u <= 65535) { + if (outIdx + 2 >= endIdx) break; + heap[outIdx++] = 224 | (u >> 12); + heap[outIdx++] = 128 | ((u >> 6) & 63); + heap[outIdx++] = 128 | (u & 63); + } else { + if (outIdx + 3 >= endIdx) break; + heap[outIdx++] = 240 | (u >> 18); + heap[outIdx++] = 128 | ((u >> 12) & 63); + heap[outIdx++] = 128 | ((u >> 6) & 63); + heap[outIdx++] = 128 | (u & 63); + i++; + } + } + heap[outIdx] = 0; + return outIdx - startIdx; + }; + var stringToUTF8 = (str, outPtr, maxBytesToWrite) => + stringToUTF8Array(str, HEAPU8, outPtr, maxBytesToWrite); + var stringToUTF8OnStack = (str) => { + var size = lengthBytesUTF8(str) + 1; + var ret = stackAlloc(size); + stringToUTF8(str, ret, size); + return ret; + }; + var initRandomFill = () => (view) => crypto.getRandomValues(view); + var randomFill = (view) => { + (randomFill = initRandomFill())(view); + }; + var PATH_FS = { + resolve: (...args) => { + var resolvedPath = '', + resolvedAbsolute = false; + for (var i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) { + var path = i >= 0 ? args[i] : FS.cwd(); + if (typeof path != 'string') { + throw new TypeError( + 'Arguments to path.resolve must be strings' + ); + } else if (!path) { + return ''; + } + resolvedPath = path + '/' + resolvedPath; + resolvedAbsolute = PATH.isAbs(path); + } + resolvedPath = PATH.normalizeArray( + resolvedPath.split('/').filter((p) => !!p), + !resolvedAbsolute + ).join('/'); + return (resolvedAbsolute ? '/' : '') + resolvedPath || '.'; + }, + relative: (from, to) => { + from = PATH_FS.resolve(from).slice(1); + to = PATH_FS.resolve(to).slice(1); + function trim(arr) { + var start = 0; + for (; start < arr.length; start++) { + if (arr[start] !== '') break; + } + var end = arr.length - 1; + for (; end >= 0; end--) { + if (arr[end] !== '') break; + } + if (start > end) return []; + return arr.slice(start, end - start + 1); + } + var fromParts = trim(from.split('/')); + var toParts = trim(to.split('/')); + var length = Math.min(fromParts.length, toParts.length); + var samePartsLength = length; + for (var i = 0; i < length; i++) { + if (fromParts[i] !== toParts[i]) { + samePartsLength = i; + break; + } + } + var outputParts = []; + for (var i = samePartsLength; i < fromParts.length; i++) { + outputParts.push('..'); + } + outputParts = outputParts.concat(toParts.slice(samePartsLength)); + return outputParts.join('/'); + }, + }; + var FS_stdin_getChar_buffer = []; + var intArrayFromString = (stringy, dontAddNull, length) => { + var len = length > 0 ? length : lengthBytesUTF8(stringy) + 1; + var u8array = new Array(len); + var numBytesWritten = stringToUTF8Array( + stringy, + u8array, + 0, + u8array.length + ); + if (dontAddNull) u8array.length = numBytesWritten; + return u8array; + }; + var FS_stdin_getChar = () => { + if (!FS_stdin_getChar_buffer.length) { + var result = null; + if (globalThis.window?.prompt) { + result = window.prompt('Input: '); + if (result !== null) { + result += '\n'; + } + } else { + } + if (!result) { + return null; + } + FS_stdin_getChar_buffer = intArrayFromString(result, true); + } + return FS_stdin_getChar_buffer.shift(); + }; + var TTY = { + ttys: [], + init() {}, + shutdown() {}, + register(dev, ops) { + TTY.ttys[dev] = { input: [], output: [], ops }; + FS.registerDevice(dev, TTY.stream_ops); + }, + stream_ops: { + open(stream) { + var tty = TTY.ttys[stream.node.rdev]; + if (!tty) { + throw new FS.ErrnoError(43); + } + stream.tty = tty; + stream.seekable = false; + }, + close(stream) { + stream.tty.ops.fsync(stream.tty); + }, + fsync(stream) { + stream.tty.ops.fsync(stream.tty); + }, + read(stream, buffer, offset, length, pos) { + if (!stream.tty || !stream.tty.ops.get_char) { + throw new FS.ErrnoError(60); + } + var bytesRead = 0; + for (var i = 0; i < length; i++) { + var result; + try { + result = stream.tty.ops.get_char(stream.tty); + } catch (e) { + throw new FS.ErrnoError(29); + } + if (result === undefined && bytesRead === 0) { + throw new FS.ErrnoError(6); + } + if (result === null || result === undefined) break; + bytesRead++; + buffer[offset + i] = result; + } + if (bytesRead) { + stream.node.atime = Date.now(); + } + return bytesRead; + }, + write(stream, buffer, offset, length, pos) { + if (!stream.tty || !stream.tty.ops.put_char) { + throw new FS.ErrnoError(60); + } + try { + for (var i = 0; i < length; i++) { + stream.tty.ops.put_char(stream.tty, buffer[offset + i]); + } + } catch (e) { + throw new FS.ErrnoError(29); + } + if (length) { + stream.node.mtime = stream.node.ctime = Date.now(); + } + return i; + }, + }, + default_tty_ops: { + get_char(tty) { + return FS_stdin_getChar(); + }, + put_char(tty, val) { + if (val === null || val === 10) { + out(UTF8ArrayToString(tty.output)); + tty.output = []; + } else { + if (val != 0) tty.output.push(val); + } + }, + fsync(tty) { + if (tty.output?.length > 0) { + out(UTF8ArrayToString(tty.output)); + tty.output = []; + } + }, + ioctl_tcgets(tty) { + return { + c_iflag: 25856, + c_oflag: 5, + c_cflag: 191, + c_lflag: 35387, + c_cc: [ + 3, 28, 127, 21, 4, 0, 1, 0, 17, 19, 26, 0, 18, 15, 23, + 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ], + }; + }, + ioctl_tcsets(tty, optional_actions, data) { + return 0; + }, + ioctl_tiocgwinsz(tty) { + return [24, 80]; + }, + }, + default_tty1_ops: { + put_char(tty, val) { + if (val === null || val === 10) { + err(UTF8ArrayToString(tty.output)); + tty.output = []; + } else { + if (val != 0) tty.output.push(val); + } + }, + fsync(tty) { + if (tty.output?.length > 0) { + err(UTF8ArrayToString(tty.output)); + tty.output = []; + } + }, + }, + }; + var zeroMemory = (ptr, size) => HEAPU8.fill(0, ptr, ptr + size); + var mmapAlloc = (size) => { + size = alignMemory(size, 65536); + var ptr = _emscripten_builtin_memalign(65536, size); + if (ptr) zeroMemory(ptr, size); + return ptr; + }; + var MEMFS = { + ops_table: null, + mount(mount) { + return MEMFS.createNode(null, '/', 16895, 0); + }, + createNode(parent, name, mode, dev) { + if (FS.isBlkdev(mode) || FS.isFIFO(mode)) { + throw new FS.ErrnoError(63); + } + MEMFS.ops_table ||= { + dir: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + lookup: MEMFS.node_ops.lookup, + mknod: MEMFS.node_ops.mknod, + rename: MEMFS.node_ops.rename, + unlink: MEMFS.node_ops.unlink, + rmdir: MEMFS.node_ops.rmdir, + readdir: MEMFS.node_ops.readdir, + symlink: MEMFS.node_ops.symlink, + }, + stream: { llseek: MEMFS.stream_ops.llseek }, + }, + file: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + }, + stream: { + llseek: MEMFS.stream_ops.llseek, + read: MEMFS.stream_ops.read, + write: MEMFS.stream_ops.write, + mmap: MEMFS.stream_ops.mmap, + msync: MEMFS.stream_ops.msync, + }, + }, + link: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + readlink: MEMFS.node_ops.readlink, + }, + stream: {}, + }, + chrdev: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + }, + stream: FS.chrdev_stream_ops, + }, + }; + var node = FS.createNode(parent, name, mode, dev); + if (FS.isDir(node.mode)) { + node.node_ops = MEMFS.ops_table.dir.node; + node.stream_ops = MEMFS.ops_table.dir.stream; + node.contents = {}; + } else if (FS.isFile(node.mode)) { + node.node_ops = MEMFS.ops_table.file.node; + node.stream_ops = MEMFS.ops_table.file.stream; + node.usedBytes = 0; + node.contents = null; + } else if (FS.isLink(node.mode)) { + node.node_ops = MEMFS.ops_table.link.node; + node.stream_ops = MEMFS.ops_table.link.stream; + } else if (FS.isChrdev(node.mode)) { + node.node_ops = MEMFS.ops_table.chrdev.node; + node.stream_ops = MEMFS.ops_table.chrdev.stream; + } + node.atime = node.mtime = node.ctime = Date.now(); + if (parent) { + parent.contents[name] = node; + parent.atime = parent.mtime = parent.ctime = node.atime; + } + return node; + }, + getFileDataAsTypedArray(node) { + if (!node.contents) return new Uint8Array(0); + if (node.contents.subarray) + return node.contents.subarray(0, node.usedBytes); + return new Uint8Array(node.contents); + }, + expandFileStorage(node, newCapacity) { + var prevCapacity = node.contents ? node.contents.length : 0; + if (prevCapacity >= newCapacity) return; + var CAPACITY_DOUBLING_MAX = 1024 * 1024; + newCapacity = Math.max( + newCapacity, + (prevCapacity * + (prevCapacity < CAPACITY_DOUBLING_MAX ? 2 : 1.125)) >>> + 0 + ); + if (prevCapacity != 0) newCapacity = Math.max(newCapacity, 256); + var oldContents = node.contents; + node.contents = new Uint8Array(newCapacity); + if (node.usedBytes > 0) + node.contents.set(oldContents.subarray(0, node.usedBytes), 0); + }, + resizeFileStorage(node, newSize) { + if (node.usedBytes == newSize) return; + if (newSize == 0) { + node.contents = null; + node.usedBytes = 0; + } else { + var oldContents = node.contents; + node.contents = new Uint8Array(newSize); + if (oldContents) { + node.contents.set( + oldContents.subarray( + 0, + Math.min(newSize, node.usedBytes) + ) + ); + } + node.usedBytes = newSize; + } + }, + node_ops: { + getattr(node) { + var attr = {}; + attr.dev = FS.isChrdev(node.mode) ? node.id : 1; + attr.ino = node.id; + attr.mode = node.mode; + attr.nlink = 1; + attr.uid = 0; + attr.gid = 0; + attr.rdev = node.rdev; + if (FS.isDir(node.mode)) { + attr.size = 4096; + } else if (FS.isFile(node.mode)) { + attr.size = node.usedBytes; + } else if (FS.isLink(node.mode)) { + attr.size = node.link.length; + } else { + attr.size = 0; + } + attr.atime = new Date(node.atime); + attr.mtime = new Date(node.mtime); + attr.ctime = new Date(node.ctime); + attr.blksize = 4096; + attr.blocks = Math.ceil(attr.size / attr.blksize); + return attr; + }, + setattr(node, attr) { + for (const key of ['mode', 'atime', 'mtime', 'ctime']) { + if (attr[key] != null) { + node[key] = attr[key]; + } + } + if (attr.size !== undefined) { + MEMFS.resizeFileStorage(node, attr.size); + } + }, + lookup(parent, name) { + if (!MEMFS.doesNotExistError) { + MEMFS.doesNotExistError = new FS.ErrnoError(44); + MEMFS.doesNotExistError.stack = ''; + } + throw MEMFS.doesNotExistError; + }, + mknod(parent, name, mode, dev) { + return MEMFS.createNode(parent, name, mode, dev); + }, + rename(old_node, new_dir, new_name) { + var new_node; + try { + new_node = FS.lookupNode(new_dir, new_name); + } catch (e) {} + if (new_node) { + if (FS.isDir(old_node.mode)) { + for (var i in new_node.contents) { + throw new FS.ErrnoError(55); + } + } + FS.hashRemoveNode(new_node); + } + delete old_node.parent.contents[old_node.name]; + new_dir.contents[new_name] = old_node; + old_node.name = new_name; + new_dir.ctime = + new_dir.mtime = + old_node.parent.ctime = + old_node.parent.mtime = + Date.now(); + }, + unlink(parent, name) { + delete parent.contents[name]; + parent.ctime = parent.mtime = Date.now(); + }, + rmdir(parent, name) { + var node = FS.lookupNode(parent, name); + for (var i in node.contents) { + throw new FS.ErrnoError(55); + } + delete parent.contents[name]; + parent.ctime = parent.mtime = Date.now(); + }, + readdir(node) { + return ['.', '..', ...Object.keys(node.contents)]; + }, + symlink(parent, newname, oldpath) { + var node = MEMFS.createNode(parent, newname, 511 | 40960, 0); + node.link = oldpath; + return node; + }, + readlink(node) { + if (!FS.isLink(node.mode)) { + throw new FS.ErrnoError(28); + } + return node.link; + }, + }, + stream_ops: { + read(stream, buffer, offset, length, position) { + var contents = stream.node.contents; + if (position >= stream.node.usedBytes) return 0; + var size = Math.min(stream.node.usedBytes - position, length); + if (size > 8 && contents.subarray) { + buffer.set( + contents.subarray(position, position + size), + offset + ); + } else { + for (var i = 0; i < size; i++) + buffer[offset + i] = contents[position + i]; + } + return size; + }, + write(stream, buffer, offset, length, position, canOwn) { + if (buffer.buffer === HEAP8.buffer) { + canOwn = false; + } + if (!length) return 0; + var node = stream.node; + node.mtime = node.ctime = Date.now(); + if ( + buffer.subarray && + (!node.contents || node.contents.subarray) + ) { + if (canOwn) { + node.contents = buffer.subarray( + offset, + offset + length + ); + node.usedBytes = length; + return length; + } else if (node.usedBytes === 0 && position === 0) { + node.contents = buffer.slice(offset, offset + length); + node.usedBytes = length; + return length; + } else if (position + length <= node.usedBytes) { + node.contents.set( + buffer.subarray(offset, offset + length), + position + ); + return length; + } + } + MEMFS.expandFileStorage(node, position + length); + if (node.contents.subarray && buffer.subarray) { + node.contents.set( + buffer.subarray(offset, offset + length), + position + ); + } else { + for (var i = 0; i < length; i++) { + node.contents[position + i] = buffer[offset + i]; + } + } + node.usedBytes = Math.max(node.usedBytes, position + length); + return length; + }, + llseek(stream, offset, whence) { + var position = offset; + if (whence === 1) { + position += stream.position; + } else if (whence === 2) { + if (FS.isFile(stream.node.mode)) { + position += stream.node.usedBytes; + } + } + if (position < 0) { + throw new FS.ErrnoError(28); + } + return position; + }, + mmap(stream, length, position, prot, flags) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError(43); + } + var ptr; + var allocated; + var contents = stream.node.contents; + if ( + !(flags & 2) && + contents && + contents.buffer === HEAP8.buffer + ) { + allocated = false; + ptr = contents.byteOffset; + } else { + allocated = true; + ptr = mmapAlloc(length); + if (!ptr) { + throw new FS.ErrnoError(48); + } + if (contents) { + if ( + position > 0 || + position + length < contents.length + ) { + if (contents.subarray) { + contents = contents.subarray( + position, + position + length + ); + } else { + contents = Array.prototype.slice.call( + contents, + position, + position + length + ); + } + } + HEAP8.set(contents, ptr); + } + } + return { ptr, allocated }; + }, + msync(stream, buffer, offset, length, mmapFlags) { + MEMFS.stream_ops.write( + stream, + buffer, + 0, + length, + offset, + false + ); + return 0; + }, + }, + }; + var FS_modeStringToFlags = (str) => { + var flagModes = { + r: 0, + 'r+': 2, + w: 512 | 64 | 1, + 'w+': 512 | 64 | 2, + a: 1024 | 64 | 1, + 'a+': 1024 | 64 | 2, + }; + var flags = flagModes[str]; + if (typeof flags == 'undefined') { + throw new Error(`Unknown file open mode: ${str}`); + } + return flags; + }; + var FS_getMode = (canRead, canWrite) => { + var mode = 0; + if (canRead) mode |= 292 | 73; + if (canWrite) mode |= 146; + return mode; + }; + var ERRNO_CODES = { + EPERM: 63, + ENOENT: 44, + ESRCH: 71, + EINTR: 27, + EIO: 29, + ENXIO: 60, + E2BIG: 1, + ENOEXEC: 45, + EBADF: 8, + ECHILD: 12, + EAGAIN: 6, + EWOULDBLOCK: 6, + ENOMEM: 48, + EACCES: 2, + EFAULT: 21, + ENOTBLK: 105, + EBUSY: 10, + EEXIST: 20, + EXDEV: 75, + ENODEV: 43, + ENOTDIR: 54, + EISDIR: 31, + EINVAL: 28, + ENFILE: 41, + EMFILE: 33, + ENOTTY: 59, + ETXTBSY: 74, + EFBIG: 22, + ENOSPC: 51, + ESPIPE: 70, + EROFS: 69, + EMLINK: 34, + EPIPE: 64, + EDOM: 18, + ERANGE: 68, + ENOMSG: 49, + EIDRM: 24, + ECHRNG: 106, + EL2NSYNC: 156, + EL3HLT: 107, + EL3RST: 108, + ELNRNG: 109, + EUNATCH: 110, + ENOCSI: 111, + EL2HLT: 112, + EDEADLK: 16, + ENOLCK: 46, + EBADE: 113, + EBADR: 114, + EXFULL: 115, + ENOANO: 104, + EBADRQC: 103, + EBADSLT: 102, + EDEADLOCK: 16, + EBFONT: 101, + ENOSTR: 100, + ENODATA: 116, + ETIME: 117, + ENOSR: 118, + ENONET: 119, + ENOPKG: 120, + EREMOTE: 121, + ENOLINK: 47, + EADV: 122, + ESRMNT: 123, + ECOMM: 124, + EPROTO: 65, + EMULTIHOP: 36, + EDOTDOT: 125, + EBADMSG: 9, + ENOTUNIQ: 126, + EBADFD: 127, + EREMCHG: 128, + ELIBACC: 129, + ELIBBAD: 130, + ELIBSCN: 131, + ELIBMAX: 132, + ELIBEXEC: 133, + ENOSYS: 52, + ENOTEMPTY: 55, + ENAMETOOLONG: 37, + ELOOP: 32, + EOPNOTSUPP: 138, + EPFNOSUPPORT: 139, + ECONNRESET: 15, + ENOBUFS: 42, + EAFNOSUPPORT: 5, + EPROTOTYPE: 67, + ENOTSOCK: 57, + ENOPROTOOPT: 50, + ESHUTDOWN: 140, + ECONNREFUSED: 14, + EADDRINUSE: 3, + ECONNABORTED: 13, + ENETUNREACH: 40, + ENETDOWN: 38, + ETIMEDOUT: 73, + EHOSTDOWN: 142, + EHOSTUNREACH: 23, + EINPROGRESS: 26, + EALREADY: 7, + EDESTADDRREQ: 17, + EMSGSIZE: 35, + EPROTONOSUPPORT: 66, + ESOCKTNOSUPPORT: 137, + EADDRNOTAVAIL: 4, + ENETRESET: 39, + EISCONN: 30, + ENOTCONN: 53, + ETOOMANYREFS: 141, + EUSERS: 136, + EDQUOT: 19, + ESTALE: 72, + ENOTSUP: 138, + ENOMEDIUM: 148, + EILSEQ: 25, + EOVERFLOW: 61, + ECANCELED: 11, + ENOTRECOVERABLE: 56, + EOWNERDEAD: 62, + ESTRPIPE: 135, + }; + var PROXYFS = { + mount(mount) { + return PROXYFS.createNode( + null, + '/', + mount.opts.fs.lstat(mount.opts.root).mode, + 0 + ); + }, + createNode(parent, name, mode, dev) { + if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) { + throw new FS.ErrnoError(ERRNO_CODES.EINVAL); + } + var node = FS.createNode(parent, name, mode); + node.node_ops = PROXYFS.node_ops; + node.stream_ops = PROXYFS.stream_ops; + return node; + }, + realPath(node) { + var parts = []; + while (node.parent !== node) { + parts.push(node.name); + node = node.parent; + } + parts.push(node.mount.opts.root); + parts.reverse(); + return PATH.join(...parts); + }, + node_ops: { + getattr(node) { + var path = PROXYFS.realPath(node); + var stat; + try { + stat = node.mount.opts.fs.lstat(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + return { + dev: stat.dev, + ino: stat.ino, + mode: stat.mode, + nlink: stat.nlink, + uid: stat.uid, + gid: stat.gid, + rdev: stat.rdev, + size: stat.size, + atime: stat.atime, + mtime: stat.mtime, + ctime: stat.ctime, + blksize: stat.blksize, + blocks: stat.blocks, + }; + }, + setattr(node, attr) { + var path = PROXYFS.realPath(node); + try { + if (attr.mode !== undefined) { + node.mount.opts.fs.chmod(path, attr.mode); + node.mode = attr.mode; + } + if (attr.atime || attr.mtime) { + var atime = new Date(attr.atime || attr.mtime); + var mtime = new Date(attr.mtime || attr.atime); + node.mount.opts.fs.utime(path, atime, mtime); + } + if (attr.size !== undefined) { + node.mount.opts.fs.truncate(path, attr.size); + } + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + lookup(parent, name) { + try { + var path = PATH.join2(PROXYFS.realPath(parent), name); + var mode = parent.mount.opts.fs.lstat(path).mode; + var node = PROXYFS.createNode(parent, name, mode); + return node; + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + mknod(parent, name, mode, dev) { + var node = PROXYFS.createNode(parent, name, mode, dev); + var path = PROXYFS.realPath(node); + try { + if (FS.isDir(node.mode)) { + node.mount.opts.fs.mkdir(path, node.mode); + } else { + node.mount.opts.fs.writeFile(path, '', { + mode: node.mode, + }); + } + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + return node; + }, + rename(oldNode, newDir, newName) { + var oldPath = PROXYFS.realPath(oldNode); + var newPath = PATH.join2(PROXYFS.realPath(newDir), newName); + try { + oldNode.mount.opts.fs.rename(oldPath, newPath); + oldNode.name = newName; + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + unlink(parent, name) { + var path = PATH.join2(PROXYFS.realPath(parent), name); + try { + parent.mount.opts.fs.unlink(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + rmdir(parent, name) { + var path = PATH.join2(PROXYFS.realPath(parent), name); + try { + parent.mount.opts.fs.rmdir(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + readdir(node) { + var path = PROXYFS.realPath(node); + try { + return node.mount.opts.fs.readdir(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + symlink(parent, newName, oldPath) { + var newPath = PATH.join2(PROXYFS.realPath(parent), newName); + try { + parent.mount.opts.fs.symlink(oldPath, newPath); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + readlink(node) { + var path = PROXYFS.realPath(node); + try { + return node.mount.opts.fs.readlink(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + }, + stream_ops: { + open(stream) { + var path = PROXYFS.realPath(stream.node); + try { + stream.nfd = stream.node.mount.opts.fs.open( + path, + stream.flags + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + close(stream) { + try { + stream.node.mount.opts.fs.close(stream.nfd); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + read(stream, buffer, offset, length, position) { + try { + return stream.node.mount.opts.fs.read( + stream.nfd, + buffer, + offset, + length, + position + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + write(stream, buffer, offset, length, position) { + try { + return stream.node.mount.opts.fs.write( + stream.nfd, + buffer, + offset, + length, + position + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + llseek(stream, offset, whence) { + var position = offset; + if (whence === 1) { + position += stream.position; + } else if (whence === 2) { + if (FS.isFile(stream.node.mode)) { + try { + var stat = stream.node.node_ops.getattr( + stream.node + ); + position += stat.size; + } catch (e) { + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + } + } + if (position < 0) { + throw new FS.ErrnoError(ERRNO_CODES.EINVAL); + } + return position; + }, + }, + }; + var FS_createDataFile = (...args) => FS.createDataFile(...args); + var getUniqueRunDependency = (id) => id; + var FS_handledByPreloadPlugin = async (byteArray, fullname) => { + if (typeof Browser != 'undefined') Browser.init(); + for (var plugin of preloadPlugins) { + if (plugin['canHandle'](fullname)) { + return plugin['handle'](byteArray, fullname); + } + } + return byteArray; + }; + var FS_preloadFile = async ( + parent, + name, + url, + canRead, + canWrite, + dontCreateFile, + canOwn, + preFinish + ) => { + var fullname = name + ? PATH_FS.resolve(PATH.join2(parent, name)) + : parent; + var dep = getUniqueRunDependency(`cp ${fullname}`); + addRunDependency(dep); + try { + var byteArray = url; + if (typeof url == 'string') { + byteArray = await asyncLoad(url); + } + byteArray = await FS_handledByPreloadPlugin(byteArray, fullname); + preFinish?.(); + if (!dontCreateFile) { + FS_createDataFile( + parent, + name, + byteArray, + canRead, + canWrite, + canOwn + ); + } + } finally { + removeRunDependency(dep); + } + }; + var FS_createPreloadedFile = ( + parent, + name, + url, + canRead, + canWrite, + onload, + onerror, + dontCreateFile, + canOwn, + preFinish + ) => { + FS_preloadFile( + parent, + name, + url, + canRead, + canWrite, + dontCreateFile, + canOwn, + preFinish + ) + .then(onload) + .catch(onerror); + }; + var FS = { + root: null, + mounts: [], + devices: {}, + streams: [], + nextInode: 1, + nameTable: null, + currentPath: '/', + initialized: false, + ignorePermissions: true, + filesystems: null, + syncFSRequests: 0, + readFiles: {}, + ErrnoError: class { + name = 'ErrnoError'; + constructor(errno) { + this.errno = errno; + } + }, + FSStream: class { + shared = {}; + get object() { + return this.node; + } + set object(val) { + this.node = val; + } + get isRead() { + return (this.flags & 2097155) !== 1; + } + get isWrite() { + return (this.flags & 2097155) !== 0; + } + get isAppend() { + return this.flags & 1024; + } + get flags() { + return this.shared.flags; + } + set flags(val) { + this.shared.flags = val; + } + get position() { + return this.shared.position; + } + set position(val) { + this.shared.position = val; + } + }, + FSNode: class { + node_ops = {}; + stream_ops = {}; + readMode = 292 | 73; + writeMode = 146; + mounted = null; + constructor(parent, name, mode, rdev) { + if (!parent) { + parent = this; + } + this.parent = parent; + this.mount = parent.mount; + this.id = FS.nextInode++; + this.name = name; + this.mode = mode; + this.rdev = rdev; + this.atime = this.mtime = this.ctime = Date.now(); + } + get read() { + return (this.mode & this.readMode) === this.readMode; + } + set read(val) { + val + ? (this.mode |= this.readMode) + : (this.mode &= ~this.readMode); + } + get write() { + return (this.mode & this.writeMode) === this.writeMode; + } + set write(val) { + val + ? (this.mode |= this.writeMode) + : (this.mode &= ~this.writeMode); + } + get isFolder() { + return FS.isDir(this.mode); + } + get isDevice() { + return FS.isChrdev(this.mode); + } + }, + lookupPath(path, opts = {}) { + if (!path) { + throw new FS.ErrnoError(44); + } + opts.follow_mount ??= true; + if (!PATH.isAbs(path)) { + path = FS.cwd() + '/' + path; + } + linkloop: for (var nlinks = 0; nlinks < 40; nlinks++) { + var parts = path.split('/').filter((p) => !!p); + var current = FS.root; + var current_path = '/'; + for (var i = 0; i < parts.length; i++) { + var islast = i === parts.length - 1; + if (islast && opts.parent) { + break; + } + if (parts[i] === '.') { + continue; + } + if (parts[i] === '..') { + current_path = PATH.dirname(current_path); + if (FS.isRoot(current)) { + path = + current_path + + '/' + + parts.slice(i + 1).join('/'); + nlinks--; + continue linkloop; + } else { + current = current.parent; + } + continue; + } + current_path = PATH.join2(current_path, parts[i]); + try { + current = FS.lookupNode(current, parts[i]); + } catch (e) { + if (e?.errno === 44 && islast && opts.noent_okay) { + return { path: current_path }; + } + throw e; + } + if ( + FS.isMountpoint(current) && + (!islast || opts.follow_mount) + ) { + current = current.mounted.root; + } + if (FS.isLink(current.mode) && (!islast || opts.follow)) { + if (!current.node_ops.readlink) { + throw new FS.ErrnoError(52); + } + var link = current.node_ops.readlink(current); + if (!PATH.isAbs(link)) { + link = PATH.dirname(current_path) + '/' + link; + } + path = link + '/' + parts.slice(i + 1).join('/'); + continue linkloop; + } + } + return { path: current_path, node: current }; + } + throw new FS.ErrnoError(32); + }, + getPath(node) { + var path; + while (true) { + if (FS.isRoot(node)) { + var mount = node.mount.mountpoint; + if (!path) return mount; + return mount[mount.length - 1] !== '/' + ? `${mount}/${path}` + : mount + path; + } + path = path ? `${node.name}/${path}` : node.name; + node = node.parent; + } + }, + hashName(parentid, name) { + var hash = 0; + for (var i = 0; i < name.length; i++) { + hash = ((hash << 5) - hash + name.charCodeAt(i)) | 0; + } + return ((parentid + hash) >>> 0) % FS.nameTable.length; + }, + hashAddNode(node) { + var hash = FS.hashName(node.parent.id, node.name); + node.name_next = FS.nameTable[hash]; + FS.nameTable[hash] = node; + }, + hashRemoveNode(node) { + var hash = FS.hashName(node.parent.id, node.name); + if (FS.nameTable[hash] === node) { + FS.nameTable[hash] = node.name_next; + } else { + var current = FS.nameTable[hash]; + while (current) { + if (current.name_next === node) { + current.name_next = node.name_next; + break; + } + current = current.name_next; + } + } + }, + lookupNode(parent, name) { + var errCode = FS.mayLookup(parent); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + var hash = FS.hashName(parent.id, name); + for (var node = FS.nameTable[hash]; node; node = node.name_next) { + var nodeName = node.name; + if (node.parent.id === parent.id && nodeName === name) { + return node; + } + } + return FS.lookup(parent, name); + }, + createNode(parent, name, mode, rdev) { + var node = new FS.FSNode(parent, name, mode, rdev); + FS.hashAddNode(node); + return node; + }, + destroyNode(node) { + FS.hashRemoveNode(node); + }, + isRoot(node) { + return node === node.parent; + }, + isMountpoint(node) { + return !!node.mounted; + }, + isFile(mode) { + return (mode & 61440) === 32768; + }, + isDir(mode) { + return (mode & 61440) === 16384; + }, + isLink(mode) { + return (mode & 61440) === 40960; + }, + isChrdev(mode) { + return (mode & 61440) === 8192; + }, + isBlkdev(mode) { + return (mode & 61440) === 24576; + }, + isFIFO(mode) { + return (mode & 61440) === 4096; + }, + isSocket(mode) { + return (mode & 49152) === 49152; + }, + flagsToPermissionString(flag) { + var perms = ['r', 'w', 'rw'][flag & 3]; + if (flag & 512) { + perms += 'w'; + } + return perms; + }, + nodePermissions(node, perms) { + if (FS.ignorePermissions) { + return 0; + } + if (perms.includes('r') && !(node.mode & 292)) { + return 2; + } else if (perms.includes('w') && !(node.mode & 146)) { + return 2; + } else if (perms.includes('x') && !(node.mode & 73)) { + return 2; + } + return 0; + }, + mayLookup(dir) { + if (!FS.isDir(dir.mode)) return 54; + var errCode = FS.nodePermissions(dir, 'x'); + if (errCode) return errCode; + if (!dir.node_ops.lookup) return 2; + return 0; + }, + mayCreate(dir, name) { + if (!FS.isDir(dir.mode)) { + return 54; + } + try { + var node = FS.lookupNode(dir, name); + return 20; + } catch (e) {} + return FS.nodePermissions(dir, 'wx'); + }, + mayDelete(dir, name, isdir) { + var node; + try { + node = FS.lookupNode(dir, name); + } catch (e) { + return e.errno; + } + var errCode = FS.nodePermissions(dir, 'wx'); + if (errCode) { + return errCode; + } + if (isdir) { + if (!FS.isDir(node.mode)) { + return 54; + } + if (FS.isRoot(node) || FS.getPath(node) === FS.cwd()) { + return 10; + } + } else { + if (FS.isDir(node.mode)) { + return 31; + } + } + return 0; + }, + mayOpen(node, flags) { + if (!node) { + return 44; + } + if (FS.isLink(node.mode)) { + return 32; + } else if (FS.isDir(node.mode)) { + if ( + FS.flagsToPermissionString(flags) !== 'r' || + flags & (512 | 64) + ) { + return 31; + } + } + return FS.nodePermissions(node, FS.flagsToPermissionString(flags)); + }, + checkOpExists(op, err) { + if (!op) { + throw new FS.ErrnoError(err); + } + return op; + }, + MAX_OPEN_FDS: 4096, + nextfd() { + for (var fd = 0; fd <= FS.MAX_OPEN_FDS; fd++) { + if (!FS.streams[fd]) { + return fd; + } + } + throw new FS.ErrnoError(33); + }, + getStreamChecked(fd) { + var stream = FS.getStream(fd); + if (!stream) { + throw new FS.ErrnoError(8); + } + return stream; + }, + getStream: (fd) => FS.streams[fd], + createStream(stream, fd = -1) { + stream = Object.assign(new FS.FSStream(), stream); + if (fd == -1) { + fd = FS.nextfd(); + } + stream.fd = fd; + FS.streams[fd] = stream; + return stream; + }, + closeStream(fd) { + FS.streams[fd] = null; + }, + dupStream(origStream, fd = -1) { + var stream = FS.createStream(origStream, fd); + stream.stream_ops?.dup?.(stream); + return stream; + }, + doSetAttr(stream, node, attr) { + var setattr = stream?.stream_ops.setattr; + var arg = setattr ? stream : node; + setattr ??= node.node_ops.setattr; + FS.checkOpExists(setattr, 63); + setattr(arg, attr); + }, + chrdev_stream_ops: { + open(stream) { + var device = FS.getDevice(stream.node.rdev); + stream.stream_ops = device.stream_ops; + stream.stream_ops.open?.(stream); + }, + llseek() { + throw new FS.ErrnoError(70); + }, + }, + major: (dev) => dev >> 8, + minor: (dev) => dev & 255, + makedev: (ma, mi) => (ma << 8) | mi, + registerDevice(dev, ops) { + FS.devices[dev] = { stream_ops: ops }; + }, + getDevice: (dev) => FS.devices[dev], + getMounts(mount) { + var mounts = []; + var check = [mount]; + while (check.length) { + var m = check.pop(); + mounts.push(m); + check.push(...m.mounts); + } + return mounts; + }, + syncfs(populate, callback) { + if (typeof populate == 'function') { + callback = populate; + populate = false; + } + FS.syncFSRequests++; + if (FS.syncFSRequests > 1) { + err( + `warning: ${FS.syncFSRequests} FS.syncfs operations in flight at once, probably just doing extra work` + ); + } + var mounts = FS.getMounts(FS.root.mount); + var completed = 0; + function doCallback(errCode) { + FS.syncFSRequests--; + return callback(errCode); + } + function done(errCode) { + if (errCode) { + if (!done.errored) { + done.errored = true; + return doCallback(errCode); + } + return; + } + if (++completed >= mounts.length) { + doCallback(null); + } + } + for (var mount of mounts) { + if (mount.type.syncfs) { + mount.type.syncfs(mount, populate, done); + } else { + done(null); + } + } + }, + mount(type, opts, mountpoint) { + var root = mountpoint === '/'; + var pseudo = !mountpoint; + var node; + if (root && FS.root) { + throw new FS.ErrnoError(10); + } else if (!root && !pseudo) { + var lookup = FS.lookupPath(mountpoint, { follow_mount: false }); + mountpoint = lookup.path; + node = lookup.node; + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + } + var mount = { type, opts, mountpoint, mounts: [] }; + var mountRoot = type.mount(mount); + mountRoot.mount = mount; + mount.root = mountRoot; + if (root) { + FS.root = mountRoot; + } else if (node) { + node.mounted = mount; + if (node.mount) { + node.mount.mounts.push(mount); + } + } + return mountRoot; + }, + unmount(mountpoint) { + var lookup = FS.lookupPath(mountpoint, { follow_mount: false }); + if (!FS.isMountpoint(lookup.node)) { + throw new FS.ErrnoError(28); + } + var node = lookup.node; + var mount = node.mounted; + var mounts = FS.getMounts(mount); + for (var [hash, current] of Object.entries(FS.nameTable)) { + while (current) { + var next = current.name_next; + if (mounts.includes(current.mount)) { + FS.destroyNode(current); + } + current = next; + } + } + node.mounted = null; + var idx = node.mount.mounts.indexOf(mount); + node.mount.mounts.splice(idx, 1); + }, + lookup(parent, name) { + return parent.node_ops.lookup(parent, name); + }, + mknod(path, mode, dev) { + var lookup = FS.lookupPath(path, { parent: true }); + var parent = lookup.node; + var name = PATH.basename(path); + if (!name) { + throw new FS.ErrnoError(28); + } + if (name === '.' || name === '..') { + throw new FS.ErrnoError(20); + } + var errCode = FS.mayCreate(parent, name); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.mknod) { + throw new FS.ErrnoError(63); + } + return parent.node_ops.mknod(parent, name, mode, dev); + }, + statfs(path) { + return FS.statfsNode(FS.lookupPath(path, { follow: true }).node); + }, + statfsStream(stream) { + return FS.statfsNode(stream.node); + }, + statfsNode(node) { + var rtn = { + bsize: 4096, + frsize: 4096, + blocks: 1e6, + bfree: 5e5, + bavail: 5e5, + files: FS.nextInode, + ffree: FS.nextInode - 1, + fsid: 42, + flags: 2, + namelen: 255, + }; + if (node.node_ops.statfs) { + Object.assign(rtn, node.node_ops.statfs(node.mount.opts.root)); + } + return rtn; + }, + create(path, mode = 438) { + mode &= 4095; + mode |= 32768; + return FS.mknod(path, mode, 0); + }, + mkdir(path, mode = 511) { + mode &= 511 | 512; + mode |= 16384; + return FS.mknod(path, mode, 0); + }, + mkdirTree(path, mode) { + var dirs = path.split('/'); + var d = ''; + for (var dir of dirs) { + if (!dir) continue; + if (d || PATH.isAbs(path)) d += '/'; + d += dir; + try { + FS.mkdir(d, mode); + } catch (e) { + if (e.errno != 20) throw e; + } + } + }, + mkdev(path, mode, dev) { + if (typeof dev == 'undefined') { + dev = mode; + mode = 438; + } + mode |= 8192; + return FS.mknod(path, mode, dev); + }, + symlink(oldpath, newpath) { + if (!PATH_FS.resolve(oldpath)) { + throw new FS.ErrnoError(44); + } + var lookup = FS.lookupPath(newpath, { parent: true }); + var parent = lookup.node; + if (!parent) { + throw new FS.ErrnoError(44); + } + var newname = PATH.basename(newpath); + var errCode = FS.mayCreate(parent, newname); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.symlink) { + throw new FS.ErrnoError(63); + } + return parent.node_ops.symlink(parent, newname, oldpath); + }, + rename(old_path, new_path) { + var old_dirname = PATH.dirname(old_path); + var new_dirname = PATH.dirname(new_path); + var old_name = PATH.basename(old_path); + var new_name = PATH.basename(new_path); + var lookup, old_dir, new_dir; + lookup = FS.lookupPath(old_path, { parent: true }); + old_dir = lookup.node; + lookup = FS.lookupPath(new_path, { parent: true }); + new_dir = lookup.node; + if (!old_dir || !new_dir) throw new FS.ErrnoError(44); + if (old_dir.mount !== new_dir.mount) { + throw new FS.ErrnoError(75); + } + var old_node = FS.lookupNode(old_dir, old_name); + var relative = PATH_FS.relative(old_path, new_dirname); + if (relative.charAt(0) !== '.') { + throw new FS.ErrnoError(28); + } + relative = PATH_FS.relative(new_path, old_dirname); + if (relative.charAt(0) !== '.') { + throw new FS.ErrnoError(55); + } + var new_node; + try { + new_node = FS.lookupNode(new_dir, new_name); + } catch (e) {} + if (old_node === new_node) { + return; + } + var isdir = FS.isDir(old_node.mode); + var errCode = FS.mayDelete(old_dir, old_name, isdir); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + errCode = new_node + ? FS.mayDelete(new_dir, new_name, isdir) + : FS.mayCreate(new_dir, new_name); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!old_dir.node_ops.rename) { + throw new FS.ErrnoError(63); + } + if ( + FS.isMountpoint(old_node) || + (new_node && FS.isMountpoint(new_node)) + ) { + throw new FS.ErrnoError(10); + } + if (new_dir !== old_dir) { + errCode = FS.nodePermissions(old_dir, 'w'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + } + FS.hashRemoveNode(old_node); + try { + old_dir.node_ops.rename(old_node, new_dir, new_name); + old_node.parent = new_dir; + } catch (e) { + throw e; + } finally { + FS.hashAddNode(old_node); + } + }, + rmdir(path) { + var lookup = FS.lookupPath(path, { parent: true }); + var parent = lookup.node; + var name = PATH.basename(path); + var node = FS.lookupNode(parent, name); + var errCode = FS.mayDelete(parent, name, true); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.rmdir) { + throw new FS.ErrnoError(63); + } + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + parent.node_ops.rmdir(parent, name); + FS.destroyNode(node); + }, + readdir(path) { + var lookup = FS.lookupPath(path, { follow: true }); + var node = lookup.node; + var readdir = FS.checkOpExists(node.node_ops.readdir, 54); + return readdir(node); + }, + unlink(path) { + var lookup = FS.lookupPath(path, { parent: true }); + var parent = lookup.node; + if (!parent) { + throw new FS.ErrnoError(44); + } + var name = PATH.basename(path); + var node = FS.lookupNode(parent, name); + var errCode = FS.mayDelete(parent, name, false); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.unlink) { + throw new FS.ErrnoError(63); + } + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + parent.node_ops.unlink(parent, name); + FS.destroyNode(node); + }, + readlink(path) { + var lookup = FS.lookupPath(path); + var link = lookup.node; + if (!link) { + throw new FS.ErrnoError(44); + } + if (!link.node_ops.readlink) { + throw new FS.ErrnoError(28); + } + return link.node_ops.readlink(link); + }, + stat(path, dontFollow) { + var lookup = FS.lookupPath(path, { follow: !dontFollow }); + var node = lookup.node; + var getattr = FS.checkOpExists(node.node_ops.getattr, 63); + return getattr(node); + }, + fstat(fd) { + var stream = FS.getStreamChecked(fd); + var node = stream.node; + var getattr = stream.stream_ops.getattr; + var arg = getattr ? stream : node; + getattr ??= node.node_ops.getattr; + FS.checkOpExists(getattr, 63); + return getattr(arg); + }, + lstat(path) { + return FS.stat(path, true); + }, + doChmod(stream, node, mode, dontFollow) { + FS.doSetAttr(stream, node, { + mode: (mode & 4095) | (node.mode & ~4095), + ctime: Date.now(), + dontFollow, + }); + }, + chmod(path, mode, dontFollow) { + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { follow: !dontFollow }); + node = lookup.node; + } else { + node = path; + } + FS.doChmod(null, node, mode, dontFollow); + }, + lchmod(path, mode) { + FS.chmod(path, mode, true); + }, + fchmod(fd, mode) { + var stream = FS.getStreamChecked(fd); + FS.doChmod(stream, stream.node, mode, false); + }, + doChown(stream, node, dontFollow) { + FS.doSetAttr(stream, node, { timestamp: Date.now(), dontFollow }); + }, + chown(path, uid, gid, dontFollow) { + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { follow: !dontFollow }); + node = lookup.node; + } else { + node = path; + } + FS.doChown(null, node, dontFollow); + }, + lchown(path, uid, gid) { + FS.chown(path, uid, gid, true); + }, + fchown(fd, uid, gid) { + var stream = FS.getStreamChecked(fd); + FS.doChown(stream, stream.node, false); + }, + doTruncate(stream, node, len) { + if (FS.isDir(node.mode)) { + throw new FS.ErrnoError(31); + } + if (!FS.isFile(node.mode)) { + throw new FS.ErrnoError(28); + } + var errCode = FS.nodePermissions(node, 'w'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + FS.doSetAttr(stream, node, { size: len, timestamp: Date.now() }); + }, + truncate(path, len) { + if (len < 0) { + throw new FS.ErrnoError(28); + } + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { follow: true }); + node = lookup.node; + } else { + node = path; + } + FS.doTruncate(null, node, len); + }, + ftruncate(fd, len) { + var stream = FS.getStreamChecked(fd); + if (len < 0 || (stream.flags & 2097155) === 0) { + throw new FS.ErrnoError(28); + } + FS.doTruncate(stream, stream.node, len); + }, + utime(path, atime, mtime) { + var lookup = FS.lookupPath(path, { follow: true }); + var node = lookup.node; + var setattr = FS.checkOpExists(node.node_ops.setattr, 63); + setattr(node, { atime, mtime }); + }, + open(path, flags, mode = 438) { + if (path === '') { + throw new FS.ErrnoError(44); + } + flags = + typeof flags == 'string' ? FS_modeStringToFlags(flags) : flags; + if (flags & 64) { + mode = (mode & 4095) | 32768; + } else { + mode = 0; + } + var node; + var isDirPath; + if (typeof path == 'object') { + node = path; + } else { + isDirPath = path.endsWith('/'); + var lookup = FS.lookupPath(path, { + follow: !(flags & 131072), + noent_okay: true, + }); + node = lookup.node; + path = lookup.path; + } + var created = false; + if (flags & 64) { + if (node) { + if (flags & 128) { + throw new FS.ErrnoError(20); + } + } else if (isDirPath) { + throw new FS.ErrnoError(31); + } else { + node = FS.mknod(path, mode | 511, 0); + created = true; + } + } + if (!node) { + throw new FS.ErrnoError(44); + } + if (FS.isChrdev(node.mode)) { + flags &= ~512; + } + if (flags & 65536 && !FS.isDir(node.mode)) { + throw new FS.ErrnoError(54); + } + if (!created) { + var errCode = FS.mayOpen(node, flags); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + } + if (flags & 512 && !created) { + FS.truncate(node, 0); + } + flags &= ~(128 | 512 | 131072); + var stream = FS.createStream({ + node, + path: FS.getPath(node), + flags, + seekable: true, + position: 0, + stream_ops: node.stream_ops, + ungotten: [], + error: false, + }); + if (stream.stream_ops.open) { + stream.stream_ops.open(stream); + } + if (created) { + FS.chmod(node, mode & 511); + } + if (Module['logReadFiles'] && !(flags & 1)) { + if (!(path in FS.readFiles)) { + FS.readFiles[path] = 1; + } + } + return stream; + }, + close(stream) { + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if (stream.getdents) stream.getdents = null; + try { + if (stream.stream_ops.close) { + stream.stream_ops.close(stream); + } + } catch (e) { + throw e; + } finally { + FS.closeStream(stream.fd); + } + stream.fd = null; + }, + isClosed(stream) { + return stream.fd === null; + }, + llseek(stream, offset, whence) { + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if (!stream.seekable || !stream.stream_ops.llseek) { + throw new FS.ErrnoError(70); + } + if (whence != 0 && whence != 1 && whence != 2) { + throw new FS.ErrnoError(28); + } + stream.position = stream.stream_ops.llseek(stream, offset, whence); + stream.ungotten = []; + return stream.position; + }, + read(stream, buffer, offset, length, position) { + if (length < 0 || position < 0) { + throw new FS.ErrnoError(28); + } + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if ((stream.flags & 2097155) === 1) { + throw new FS.ErrnoError(8); + } + if (FS.isDir(stream.node.mode)) { + throw new FS.ErrnoError(31); + } + if (!stream.stream_ops.read) { + throw new FS.ErrnoError(28); + } + var seeking = typeof position != 'undefined'; + if (!seeking) { + position = stream.position; + } else if (!stream.seekable) { + throw new FS.ErrnoError(70); + } + var bytesRead = stream.stream_ops.read( + stream, + buffer, + offset, + length, + position + ); + if (!seeking) stream.position += bytesRead; + return bytesRead; + }, + write(stream, buffer, offset, length, position, canOwn) { + if (length < 0 || position < 0) { + throw new FS.ErrnoError(28); + } + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if ((stream.flags & 2097155) === 0) { + throw new FS.ErrnoError(8); + } + if (FS.isDir(stream.node.mode)) { + throw new FS.ErrnoError(31); + } + if (!stream.stream_ops.write) { + throw new FS.ErrnoError(28); + } + if (stream.seekable && stream.flags & 1024) { + FS.llseek(stream, 0, 2); + } + var seeking = typeof position != 'undefined'; + if (!seeking) { + position = stream.position; + } else if (!stream.seekable) { + throw new FS.ErrnoError(70); + } + var bytesWritten = stream.stream_ops.write( + stream, + buffer, + offset, + length, + position, + canOwn + ); + if (!seeking) stream.position += bytesWritten; + return bytesWritten; + }, + mmap(stream, length, position, prot, flags) { + if ( + (prot & 2) !== 0 && + (flags & 2) === 0 && + (stream.flags & 2097155) !== 2 + ) { + throw new FS.ErrnoError(2); + } + if ((stream.flags & 2097155) === 1) { + throw new FS.ErrnoError(2); + } + if (!stream.stream_ops.mmap) { + throw new FS.ErrnoError(43); + } + if (!length) { + throw new FS.ErrnoError(28); + } + return stream.stream_ops.mmap( + stream, + length, + position, + prot, + flags + ); + }, + msync(stream, buffer, offset, length, mmapFlags) { + if (!stream.stream_ops.msync) { + return 0; + } + return stream.stream_ops.msync( + stream, + buffer, + offset, + length, + mmapFlags + ); + }, + ioctl(stream, cmd, arg) { + if (!stream.stream_ops.ioctl) { + throw new FS.ErrnoError(59); + } + return stream.stream_ops.ioctl(stream, cmd, arg); + }, + readFile(path, opts = {}) { + opts.flags = opts.flags || 0; + opts.encoding = opts.encoding || 'binary'; + if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') { + abort(`Invalid encoding type "${opts.encoding}"`); + } + var stream = FS.open(path, opts.flags); + var stat = FS.stat(path); + var length = stat.size; + var buf = new Uint8Array(length); + FS.read(stream, buf, 0, length, 0); + if (opts.encoding === 'utf8') { + buf = UTF8ArrayToString(buf); + } + FS.close(stream); + return buf; + }, + writeFile(path, data, opts = {}) { + opts.flags = opts.flags || 577; + var stream = FS.open(path, opts.flags, opts.mode); + if (typeof data == 'string') { + data = new Uint8Array(intArrayFromString(data, true)); + } + if (ArrayBuffer.isView(data)) { + FS.write( + stream, + data, + 0, + data.byteLength, + undefined, + opts.canOwn + ); + } else { + abort('Unsupported data type'); + } + FS.close(stream); + }, + cwd: () => FS.currentPath, + chdir(path) { + var lookup = FS.lookupPath(path, { follow: true }); + if (lookup.node === null) { + throw new FS.ErrnoError(44); + } + if (!FS.isDir(lookup.node.mode)) { + throw new FS.ErrnoError(54); + } + var errCode = FS.nodePermissions(lookup.node, 'x'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + FS.currentPath = lookup.path; + }, + createDefaultDirectories() { + FS.mkdir('/tmp'); + FS.mkdir('/home'); + FS.mkdir('/home/web_user'); + }, + createDefaultDevices() { + FS.mkdir('/dev'); + FS.registerDevice(FS.makedev(1, 3), { + read: () => 0, + write: (stream, buffer, offset, length, pos) => length, + llseek: () => 0, + }); + FS.mkdev('/dev/null', FS.makedev(1, 3)); + TTY.register(FS.makedev(5, 0), TTY.default_tty_ops); + TTY.register(FS.makedev(6, 0), TTY.default_tty1_ops); + FS.mkdev('/dev/tty', FS.makedev(5, 0)); + FS.mkdev('/dev/tty1', FS.makedev(6, 0)); + var randomBuffer = new Uint8Array(1024), + randomLeft = 0; + var randomByte = () => { + if (randomLeft === 0) { + randomFill(randomBuffer); + randomLeft = randomBuffer.byteLength; + } + return randomBuffer[--randomLeft]; + }; + FS.createDevice('/dev', 'random', randomByte); + FS.createDevice('/dev', 'urandom', randomByte); + FS.mkdir('/dev/shm'); + FS.mkdir('/dev/shm/tmp'); + }, + createSpecialDirectories() { + FS.mkdir('/proc'); + var proc_self = FS.mkdir('/proc/self'); + FS.mkdir('/proc/self/fd'); + FS.mount( + { + mount() { + var node = FS.createNode(proc_self, 'fd', 16895, 73); + node.stream_ops = { llseek: MEMFS.stream_ops.llseek }; + node.node_ops = { + lookup(parent, name) { + var fd = +name; + var stream = FS.getStreamChecked(fd); + var ret = { + parent: null, + mount: { mountpoint: 'fake' }, + node_ops: { readlink: () => stream.path }, + id: fd + 1, + }; + ret.parent = ret; + return ret; + }, + readdir() { + return Array.from(FS.streams.entries()) + .filter(([k, v]) => v) + .map(([k, v]) => k.toString()); + }, + }; + return node; + }, + }, + {}, + '/proc/self/fd' + ); + }, + createStandardStreams(input, output, error) { + if (input) { + FS.createDevice('/dev', 'stdin', input); + } else { + FS.symlink('/dev/tty', '/dev/stdin'); + } + if (output) { + FS.createDevice('/dev', 'stdout', null, output); + } else { + FS.symlink('/dev/tty', '/dev/stdout'); + } + if (error) { + FS.createDevice('/dev', 'stderr', null, error); + } else { + FS.symlink('/dev/tty1', '/dev/stderr'); + } + var stdin = FS.open('/dev/stdin', 0); + var stdout = FS.open('/dev/stdout', 1); + var stderr = FS.open('/dev/stderr', 1); + }, + staticInit() { + FS.nameTable = new Array(4096); + FS.mount(MEMFS, {}, '/'); + FS.createDefaultDirectories(); + FS.createDefaultDevices(); + FS.createSpecialDirectories(); + FS.filesystems = { MEMFS, PROXYFS }; + }, + init(input, output, error) { + FS.initialized = true; + input ??= Module['stdin']; + output ??= Module['stdout']; + error ??= Module['stderr']; + FS.createStandardStreams(input, output, error); + }, + quit() { + FS.initialized = false; + _fflush(0); + for (var stream of FS.streams) { + if (stream) { + FS.close(stream); + } + } + }, + findObject(path, dontResolveLastLink) { + var ret = FS.analyzePath(path, dontResolveLastLink); + if (!ret.exists) { + return null; + } + return ret.object; + }, + analyzePath(path, dontResolveLastLink) { + try { + var lookup = FS.lookupPath(path, { + follow: !dontResolveLastLink, + }); + path = lookup.path; + } catch (e) {} + var ret = { + isRoot: false, + exists: false, + error: 0, + name: null, + path: null, + object: null, + parentExists: false, + parentPath: null, + parentObject: null, + }; + try { + var lookup = FS.lookupPath(path, { parent: true }); + ret.parentExists = true; + ret.parentPath = lookup.path; + ret.parentObject = lookup.node; + ret.name = PATH.basename(path); + lookup = FS.lookupPath(path, { follow: !dontResolveLastLink }); + ret.exists = true; + ret.path = lookup.path; + ret.object = lookup.node; + ret.name = lookup.node.name; + ret.isRoot = lookup.path === '/'; + } catch (e) { + ret.error = e.errno; + } + return ret; + }, + createPath(parent, path, canRead, canWrite) { + parent = typeof parent == 'string' ? parent : FS.getPath(parent); + var parts = path.split('/').reverse(); + while (parts.length) { + var part = parts.pop(); + if (!part) continue; + var current = PATH.join2(parent, part); + try { + FS.mkdir(current); + } catch (e) { + if (e.errno != 20) throw e; + } + parent = current; + } + return current; + }, + createFile(parent, name, properties, canRead, canWrite) { + var path = PATH.join2( + typeof parent == 'string' ? parent : FS.getPath(parent), + name + ); + var mode = FS_getMode(canRead, canWrite); + return FS.create(path, mode); + }, + createDataFile(parent, name, data, canRead, canWrite, canOwn) { + var path = name; + if (parent) { + parent = + typeof parent == 'string' ? parent : FS.getPath(parent); + path = name ? PATH.join2(parent, name) : parent; + } + var mode = FS_getMode(canRead, canWrite); + var node = FS.create(path, mode); + if (data) { + if (typeof data == 'string') { + var arr = new Array(data.length); + for (var i = 0, len = data.length; i < len; ++i) + arr[i] = data.charCodeAt(i); + data = arr; + } + FS.chmod(node, mode | 146); + var stream = FS.open(node, 577); + FS.write(stream, data, 0, data.length, 0, canOwn); + FS.close(stream); + FS.chmod(node, mode); + } + }, + createDevice(parent, name, input, output) { + var path = PATH.join2( + typeof parent == 'string' ? parent : FS.getPath(parent), + name + ); + var mode = FS_getMode(!!input, !!output); + FS.createDevice.major ??= 64; + var dev = FS.makedev(FS.createDevice.major++, 0); + FS.registerDevice(dev, { + open(stream) { + stream.seekable = false; + }, + close(stream) { + if (output?.buffer?.length) { + output(10); + } + }, + read(stream, buffer, offset, length, pos) { + var bytesRead = 0; + for (var i = 0; i < length; i++) { + var result; + try { + result = input(); + } catch (e) { + throw new FS.ErrnoError(29); + } + if (result === undefined && bytesRead === 0) { + throw new FS.ErrnoError(6); + } + if (result === null || result === undefined) break; + bytesRead++; + buffer[offset + i] = result; + } + if (bytesRead) { + stream.node.atime = Date.now(); + } + return bytesRead; + }, + write(stream, buffer, offset, length, pos) { + for (var i = 0; i < length; i++) { + try { + output(buffer[offset + i]); + } catch (e) { + throw new FS.ErrnoError(29); + } + } + if (length) { + stream.node.mtime = stream.node.ctime = Date.now(); + } + return i; + }, + }); + return FS.mkdev(path, mode, dev); + }, + forceLoadFile(obj) { + if (obj.isDevice || obj.isFolder || obj.link || obj.contents) + return true; + if (globalThis.XMLHttpRequest) { + abort( + 'Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.' + ); + } else { + try { + obj.contents = readBinary(obj.url); + } catch (e) { + throw new FS.ErrnoError(29); + } + } + }, + createLazyFile(parent, name, url, canRead, canWrite) { + class LazyUint8Array { + lengthKnown = false; + chunks = []; + get(idx) { + if (idx > this.length - 1 || idx < 0) { + return undefined; + } + var chunkOffset = idx % this.chunkSize; + var chunkNum = (idx / this.chunkSize) | 0; + return this.getter(chunkNum)[chunkOffset]; + } + setDataGetter(getter) { + this.getter = getter; + } + cacheLength() { + var xhr = new XMLHttpRequest(); + xhr.open('HEAD', url, false); + xhr.send(null); + if ( + !( + (xhr.status >= 200 && xhr.status < 300) || + xhr.status === 304 + ) + ) + abort( + "Couldn't load " + url + '. Status: ' + xhr.status + ); + var datalength = Number( + xhr.getResponseHeader('Content-length') + ); + var header; + var hasByteServing = + (header = xhr.getResponseHeader('Accept-Ranges')) && + header === 'bytes'; + var usesGzip = + (header = xhr.getResponseHeader('Content-Encoding')) && + header === 'gzip'; + var chunkSize = 1024 * 1024; + if (!hasByteServing) chunkSize = datalength; + var doXHR = (from, to) => { + if (from > to) + abort( + 'invalid range (' + + from + + ', ' + + to + + ') or no bytes requested!' + ); + if (to > datalength - 1) + abort( + 'only ' + + datalength + + ' bytes available! programmer error!' + ); + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, false); + if (datalength !== chunkSize) + xhr.setRequestHeader( + 'Range', + 'bytes=' + from + '-' + to + ); + xhr.responseType = 'arraybuffer'; + if (xhr.overrideMimeType) { + xhr.overrideMimeType( + 'text/plain; charset=x-user-defined' + ); + } + xhr.send(null); + if ( + !( + (xhr.status >= 200 && xhr.status < 300) || + xhr.status === 304 + ) + ) + abort( + "Couldn't load " + + url + + '. Status: ' + + xhr.status + ); + if (xhr.response !== undefined) { + return new Uint8Array(xhr.response || []); + } + return intArrayFromString(xhr.responseText || '', true); + }; + var lazyArray = this; + lazyArray.setDataGetter((chunkNum) => { + var start = chunkNum * chunkSize; + var end = (chunkNum + 1) * chunkSize - 1; + end = Math.min(end, datalength - 1); + if (typeof lazyArray.chunks[chunkNum] == 'undefined') { + lazyArray.chunks[chunkNum] = doXHR(start, end); + } + if (typeof lazyArray.chunks[chunkNum] == 'undefined') + abort('doXHR failed!'); + return lazyArray.chunks[chunkNum]; + }); + if (usesGzip || !datalength) { + chunkSize = datalength = 1; + datalength = this.getter(0).length; + chunkSize = datalength; + out( + 'LazyFiles on gzip forces download of the whole file when length is accessed' + ); + } + this._length = datalength; + this._chunkSize = chunkSize; + this.lengthKnown = true; + } + get length() { + if (!this.lengthKnown) { + this.cacheLength(); + } + return this._length; + } + get chunkSize() { + if (!this.lengthKnown) { + this.cacheLength(); + } + return this._chunkSize; + } + } + if (globalThis.XMLHttpRequest) { + if (!ENVIRONMENT_IS_WORKER) + abort( + 'Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc' + ); + var lazyArray = new LazyUint8Array(); + var properties = { isDevice: false, contents: lazyArray }; + } else { + var properties = { isDevice: false, url }; + } + var node = FS.createFile( + parent, + name, + properties, + canRead, + canWrite + ); + if (properties.contents) { + node.contents = properties.contents; + } else if (properties.url) { + node.contents = null; + node.url = properties.url; + } + Object.defineProperties(node, { + usedBytes: { + get: function () { + return this.contents.length; + }, + }, + }); + var stream_ops = {}; + for (const [key, fn] of Object.entries(node.stream_ops)) { + stream_ops[key] = (...args) => { + FS.forceLoadFile(node); + return fn(...args); + }; + } + function writeChunks(stream, buffer, offset, length, position) { + var contents = stream.node.contents; + if (position >= contents.length) return 0; + var size = Math.min(contents.length - position, length); + if (contents.slice) { + for (var i = 0; i < size; i++) { + buffer[offset + i] = contents[position + i]; + } + } else { + for (var i = 0; i < size; i++) { + buffer[offset + i] = contents.get(position + i); + } + } + return size; + } + stream_ops.read = (stream, buffer, offset, length, position) => { + FS.forceLoadFile(node); + return writeChunks(stream, buffer, offset, length, position); + }; + stream_ops.mmap = (stream, length, position, prot, flags) => { + FS.forceLoadFile(node); + var ptr = mmapAlloc(length); + if (!ptr) { + throw new FS.ErrnoError(48); + } + writeChunks(stream, HEAP8, ptr, length, position); + return { ptr, allocated: true }; + }; + node.stream_ops = stream_ops; + return node; + }, + }; + var findLibraryFS = (libName, rpath) => { + if (!runtimeInitialized) { + return undefined; + } + if (PATH.isAbs(libName)) { + try { + FS.lookupPath(libName); + return libName; + } catch (e) { + return undefined; + } + } + var rpathResolved = (rpath?.paths || []).map((p) => + replaceORIGIN(rpath?.parentLibPath, p) + ); + return withStackSave(() => { + var bufSize = 2 * 255 + 2; + var buf = stackAlloc(bufSize); + var rpathC = stringToUTF8OnStack(rpathResolved.join(':')); + var libNameC = stringToUTF8OnStack(libName); + var resLibNameC = __emscripten_find_dylib( + buf, + rpathC, + libNameC, + bufSize + ); + return resLibNameC ? UTF8ToString(resLibNameC) : undefined; + }); + }; + var registerDynCallSymbols = (exports) => { + for (var [sym, exp] of Object.entries(exports)) { + if (sym.startsWith('dynCall_')) { + var sig = sym.substring(8); + if (!dynCalls.hasOwnProperty(sig)) { + dynCalls[sig] = exp; + } + } + } + }; + function loadDynamicLibrary( + libName, + flags = { global: true, nodelete: true }, + localScope, + handle + ) { + var dso = LDSO.loadedLibsByName[libName]; + if (dso) { + if (!flags.global) { + if (localScope) { + Object.assign(localScope, dso.exports); + } + registerDynCallSymbols(dso.exports); + } else if (!dso.global) { + dso.global = true; + mergeLibSymbols(dso.exports, libName); + } + if (flags.nodelete && dso.refcount !== Infinity) { + dso.refcount = Infinity; + } + dso.refcount++; + if (handle) { + LDSO.loadedLibsByHandle[handle] = dso; + } + return flags.loadAsync ? Promise.resolve(true) : true; + } + dso = newDSO(libName, handle, 'loading'); + dso.refcount = flags.nodelete ? Infinity : 1; + dso.global = flags.global; + function loadLibData() { + if (handle) { + var data = HEAPU32[(handle + 28) >> 2]; + var dataSize = HEAPU32[(handle + 32) >> 2]; + if (data && dataSize) { + var libData = HEAP8.slice(data, data + dataSize); + return flags.loadAsync ? Promise.resolve(libData) : libData; + } + } + var f = findLibraryFS(libName, flags.rpath); + if (f) { + var libData = FS.readFile(f, { encoding: 'binary' }); + return flags.loadAsync ? Promise.resolve(libData) : libData; + } + var libFile = locateFile(libName); + if (flags.loadAsync) { + return asyncLoad(libFile); + } + if (!readBinary) { + throw new Error( + `${libFile}: file not found, and synchronous loading of external files is not available` + ); + } + return readBinary(libFile); + } + function getExports() { + var preloaded = preloadedWasm[libName]; + if (preloaded) { + return flags.loadAsync ? Promise.resolve(preloaded) : preloaded; + } + if (flags.loadAsync) { + return loadLibData().then((libData) => + loadWebAssemblyModule( + libData, + flags, + libName, + localScope, + handle + ) + ); + } + return loadWebAssemblyModule( + loadLibData(), + flags, + libName, + localScope, + handle + ); + } + function moduleLoaded(exports) { + if (dso.global) { + mergeLibSymbols(exports, libName); + } else if (localScope) { + Object.assign(localScope, exports); + registerDynCallSymbols(exports); + } + dso.exports = exports; + } + if (flags.loadAsync) { + return getExports().then((exports) => { + moduleLoaded(exports); + return true; + }); + } + moduleLoaded(getExports()); + return true; + } + var reportUndefinedSymbols = () => { + for (var [symName, entry] of Object.entries(GOT)) { + if (entry.value == -1) { + var value = resolveGlobalSymbol(symName, true).sym; + if (!value && !entry.required) { + entry.value = 0; + continue; + } + if (typeof value == 'function') { + entry.value = addFunction(value, value.sig); + } else if (typeof value == 'number') { + entry.value = value; + } else { + throw new Error( + `bad export type for '${symName}': ${typeof value} (${value})` + ); + } + } + } + }; + var loadDylibs = async () => { + if (!dynamicLibraries.length) { + reportUndefinedSymbols(); + return; + } + addRunDependency('loadDylibs'); + for (var lib of dynamicLibraries) { + await loadDynamicLibrary(lib, { + loadAsync: true, + global: true, + nodelete: true, + allowUndefined: true, + }); + } + reportUndefinedSymbols(); + removeRunDependency('loadDylibs'); + }; + var noExitRuntime = false; + var ___assert_fail = (condition, filename, line, func) => + abort( + `Assertion failed: ${UTF8ToString(condition)}, at: ` + + [ + filename ? UTF8ToString(filename) : 'unknown filename', + line, + func ? UTF8ToString(func) : 'unknown function', + ] + ); + ___assert_fail.sig = 'vppip'; + var ___asyncify_data = new WebAssembly.Global( + { value: 'i32', mutable: true }, + 0 + ); + var ___asyncify_state = new WebAssembly.Global( + { value: 'i32', mutable: true }, + 0 + ); + var ___call_sighandler = (fp, sig) => ((a1) => dynCall_vi(fp, a1))(sig); + ___call_sighandler.sig = 'vpi'; + var SOCKFS = { + websocketArgs: {}, + callbacks: {}, + on(event, callback) { + SOCKFS.callbacks[event] = callback; + }, + emit(event, param) { + SOCKFS.callbacks[event]?.(param); + }, + mount(mount) { + SOCKFS.websocketArgs = Module['websocket'] || {}; + (Module['websocket'] ??= {})['on'] = SOCKFS.on; + return FS.createNode(null, '/', 16895, 0); + }, + createSocket(family, type, protocol) { + if (family != 2) { + throw new FS.ErrnoError(5); + } + type &= ~526336; + if (type != 1 && type != 2) { + throw new FS.ErrnoError(28); + } + var streaming = type == 1; + if (streaming && protocol && protocol != 6) { + throw new FS.ErrnoError(66); + } + var sock = { + family, + type, + protocol, + server: null, + error: null, + peers: {}, + pending: [], + recv_queue: [], + sock_ops: SOCKFS.websocket_sock_ops, + }; + var name = SOCKFS.nextname(); + var node = FS.createNode(SOCKFS.root, name, 49152, 0); + node.sock = sock; + var stream = FS.createStream({ + path: name, + node, + flags: 2, + seekable: false, + stream_ops: SOCKFS.stream_ops, + }); + sock.stream = stream; + return sock; + }, + getSocket(fd) { + var stream = FS.getStream(fd); + if (!stream || !FS.isSocket(stream.node.mode)) { + return null; + } + return stream.node.sock; + }, + stream_ops: { + poll(stream) { + var sock = stream.node.sock; + return sock.sock_ops.poll(sock); + }, + ioctl(stream, request, varargs) { + var sock = stream.node.sock; + return sock.sock_ops.ioctl(sock, request, varargs); + }, + read(stream, buffer, offset, length, position) { + var sock = stream.node.sock; + var msg = sock.sock_ops.recvmsg(sock, length); + if (!msg) { + return 0; + } + buffer.set(msg.buffer, offset); + return msg.buffer.length; + }, + write(stream, buffer, offset, length, position) { + var sock = stream.node.sock; + return sock.sock_ops.sendmsg(sock, buffer, offset, length); + }, + close(stream) { + var sock = stream.node.sock; + sock.sock_ops.close(sock); + }, + }, + nextname() { + if (!SOCKFS.nextname.current) { + SOCKFS.nextname.current = 0; + } + return `socket[${SOCKFS.nextname.current++}]`; + }, + websocket_sock_ops: { + createPeer(sock, addr, port) { + var ws; + if (typeof addr == 'object') { + ws = addr; + addr = null; + port = null; + } + if (ws) { + if (ws._socket) { + addr = ws._socket.remoteAddress; + port = ws._socket.remotePort; + } else { + var result = /ws[s]?:\/\/([^:]+):(\d+)/.exec(ws.url); + if (!result) { + throw new Error( + 'WebSocket URL must be in the format ws(s)://address:port' + ); + } + addr = result[1]; + port = parseInt(result[2], 10); + } + } else { + try { + var url = 'ws://'.replace('#', '//'); + var subProtocols = 'binary'; + var opts = undefined; + if ('function' === typeof SOCKFS.websocketArgs['url']) { + url = SOCKFS.websocketArgs['url'](...arguments); + } else if ( + 'string' === typeof SOCKFS.websocketArgs['url'] + ) { + url = SOCKFS.websocketArgs['url']; + } + if (SOCKFS.websocketArgs['subprotocol']) { + subProtocols = SOCKFS.websocketArgs['subprotocol']; + } else if ( + SOCKFS.websocketArgs['subprotocol'] === null + ) { + subProtocols = 'null'; + } + if (url === 'ws://' || url === 'wss://') { + var parts = addr.split('/'); + url = + url + + parts[0] + + ':' + + port + + '/' + + parts.slice(1).join('/'); + } + if (subProtocols !== 'null') { + subProtocols = subProtocols + .replace(/^ +| +$/g, '') + .split(/ *, */); + opts = subProtocols; + } + var WebSocketConstructor; + { + WebSocketConstructor = WebSocket; + } + if (Module['websocket']['decorator']) { + WebSocketConstructor = + Module['websocket']['decorator']( + WebSocketConstructor + ); + } + ws = new WebSocketConstructor(url, opts); + ws.binaryType = 'arraybuffer'; + } catch (e) { + throw new FS.ErrnoError(23); + } + } + var peer = { addr, port, socket: ws, msg_send_queue: [] }; + SOCKFS.websocket_sock_ops.addPeer(sock, peer); + SOCKFS.websocket_sock_ops.handlePeerEvents(sock, peer); + if (sock.type === 2 && typeof sock.sport != 'undefined') { + peer.msg_send_queue.push( + new Uint8Array([ + 255, + 255, + 255, + 255, + 'p'.charCodeAt(0), + 'o'.charCodeAt(0), + 'r'.charCodeAt(0), + 't'.charCodeAt(0), + (sock.sport & 65280) >> 8, + sock.sport & 255, + ]) + ); + } + return peer; + }, + getPeer(sock, addr, port) { + return sock.peers[addr + ':' + port]; + }, + addPeer(sock, peer) { + sock.peers[peer.addr + ':' + peer.port] = peer; + }, + removePeer(sock, peer) { + delete sock.peers[peer.addr + ':' + peer.port]; + }, + handlePeerEvents(sock, peer) { + var first = true; + var handleOpen = function () { + sock.connecting = false; + SOCKFS.emit('open', sock.stream.fd); + try { + var queued = peer.msg_send_queue.shift(); + while (queued) { + peer.socket.send(queued); + queued = peer.msg_send_queue.shift(); + } + } catch (e) { + peer.socket.close(); + } + }; + function handleMessage(data) { + if (typeof data == 'string') { + var encoder = new TextEncoder(); + data = encoder.encode(data); + } else { + if (data.byteLength == 0) { + return; + } + data = new Uint8Array(data); + } + var wasfirst = first; + first = false; + if ( + wasfirst && + data.length === 10 && + data[0] === 255 && + data[1] === 255 && + data[2] === 255 && + data[3] === 255 && + data[4] === 'p'.charCodeAt(0) && + data[5] === 'o'.charCodeAt(0) && + data[6] === 'r'.charCodeAt(0) && + data[7] === 't'.charCodeAt(0) + ) { + var newport = (data[8] << 8) | data[9]; + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + peer.port = newport; + SOCKFS.websocket_sock_ops.addPeer(sock, peer); + return; + } + sock.recv_queue.push({ + addr: peer.addr, + port: peer.port, + data, + }); + SOCKFS.emit('message', sock.stream.fd); + } + if (ENVIRONMENT_IS_NODE) { + peer.socket.on('open', handleOpen); + peer.socket.on('message', function (data, isBinary) { + if (!isBinary) { + return; + } + handleMessage(new Uint8Array(data).buffer); + }); + peer.socket.on('close', function () { + SOCKFS.emit('close', sock.stream.fd); + }); + peer.socket.on('error', function (error) { + sock.error = 14; + SOCKFS.emit('error', [ + sock.stream.fd, + sock.error, + 'ECONNREFUSED: Connection refused', + ]); + }); + } else { + peer.socket.onopen = handleOpen; + peer.socket.onclose = function () { + SOCKFS.emit('close', sock.stream.fd); + }; + peer.socket.onmessage = function peer_socket_onmessage( + event + ) { + handleMessage(event.data); + }; + peer.socket.onerror = function (error) { + sock.error = 14; + SOCKFS.emit('error', [ + sock.stream.fd, + sock.error, + 'ECONNREFUSED: Connection refused', + ]); + }; + } + }, + poll(sock) { + if (sock.type === 1 && sock.server) { + return sock.pending.length ? 64 | 1 : 0; + } + var mask = 0; + var dest = + sock.type === 1 + ? SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ) + : null; + if ( + sock.recv_queue.length || + !dest || + (dest && dest.socket.readyState === dest.socket.CLOSING) || + (dest && dest.socket.readyState === dest.socket.CLOSED) + ) { + mask |= 64 | 1; + } + if ( + !dest || + (dest && dest.socket.readyState === dest.socket.OPEN) + ) { + mask |= 4; + } + if ( + (dest && dest.socket.readyState === dest.socket.CLOSING) || + (dest && dest.socket.readyState === dest.socket.CLOSED) + ) { + if (sock.connecting) { + mask |= 4; + } else { + mask |= 16; + } + } + return mask; + }, + ioctl(sock, request, arg) { + switch (request) { + case 21531: + var bytes = 0; + if (sock.recv_queue.length) { + bytes = sock.recv_queue[0].data.length; + } + HEAP32[arg >> 2] = bytes; + return 0; + case 21537: + var on = HEAP32[arg >> 2]; + if (on) { + sock.stream.flags |= 2048; + } else { + sock.stream.flags &= ~2048; + } + return 0; + default: + return 28; + } + }, + close(sock) { + if (sock.server) { + try { + sock.server.close(); + } catch (e) {} + sock.server = null; + } + for (var peer of Object.values(sock.peers)) { + try { + peer.socket.close(); + } catch (e) {} + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + } + return 0; + }, + bind(sock, addr, port) { + if ( + typeof sock.saddr != 'undefined' || + typeof sock.sport != 'undefined' + ) { + throw new FS.ErrnoError(28); + } + sock.saddr = addr; + sock.sport = port; + if (sock.type === 2) { + if (sock.server) { + sock.server.close(); + sock.server = null; + } + try { + sock.sock_ops.listen(sock, 0); + } catch (e) { + if (!(e.name === 'ErrnoError')) throw e; + if (e.errno !== 138) throw e; + } + } + }, + connect(sock, addr, port) { + if (sock.server) { + throw new FS.ErrnoError(138); + } + if ( + typeof sock.daddr != 'undefined' && + typeof sock.dport != 'undefined' + ) { + var dest = SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ); + if (dest) { + if (dest.socket.readyState === dest.socket.CONNECTING) { + throw new FS.ErrnoError(7); + } else { + throw new FS.ErrnoError(30); + } + } + } + var peer = SOCKFS.websocket_sock_ops.createPeer( + sock, + addr, + port + ); + sock.daddr = peer.addr; + sock.dport = peer.port; + sock.connecting = true; + }, + listen(sock, backlog) { + if (!ENVIRONMENT_IS_NODE) { + throw new FS.ErrnoError(138); + } + }, + accept(listensock) { + if (!listensock.server || !listensock.pending.length) { + throw new FS.ErrnoError(28); + } + var newsock = listensock.pending.shift(); + newsock.stream.flags = listensock.stream.flags; + return newsock; + }, + getname(sock, peer) { + var addr, port; + if (peer) { + if (sock.daddr === undefined || sock.dport === undefined) { + throw new FS.ErrnoError(53); + } + addr = sock.daddr; + port = sock.dport; + } else { + addr = sock.saddr || 0; + port = sock.sport || 0; + } + return { addr, port }; + }, + sendmsg(sock, buffer, offset, length, addr, port) { + if (sock.type === 2) { + if (addr === undefined || port === undefined) { + addr = sock.daddr; + port = sock.dport; + } + if (addr === undefined || port === undefined) { + throw new FS.ErrnoError(17); + } + } else { + addr = sock.daddr; + port = sock.dport; + } + var dest = SOCKFS.websocket_sock_ops.getPeer(sock, addr, port); + if (sock.type === 1) { + if ( + !dest || + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + throw new FS.ErrnoError(53); + } + } + if (ArrayBuffer.isView(buffer)) { + offset += buffer.byteOffset; + buffer = buffer.buffer; + } + var data = buffer.slice(offset, offset + length); + if (!dest || dest.socket.readyState !== dest.socket.OPEN) { + if (sock.type === 2) { + if ( + !dest || + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + dest = SOCKFS.websocket_sock_ops.createPeer( + sock, + addr, + port + ); + } + } + dest.msg_send_queue.push(data); + return length; + } + try { + dest.socket.send(data); + return length; + } catch (e) { + throw new FS.ErrnoError(28); + } + }, + recvmsg(sock, length, flags) { + if (sock.type === 1 && sock.server) { + throw new FS.ErrnoError(53); + } + var queued = sock.recv_queue.shift(); + if (!queued) { + if (sock.type === 1) { + var dest = SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ); + if (!dest) { + throw new FS.ErrnoError(53); + } + if ( + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + return null; + } + throw new FS.ErrnoError(6); + } + throw new FS.ErrnoError(6); + } + var queuedLength = queued.data.byteLength || queued.data.length; + var queuedOffset = queued.data.byteOffset || 0; + var queuedBuffer = queued.data.buffer || queued.data; + var bytesRead = Math.min(length, queuedLength); + var res = { + buffer: new Uint8Array( + queuedBuffer, + queuedOffset, + bytesRead + ), + addr: queued.addr, + port: queued.port, + }; + if (flags & 2) { + bytesRead = 0; + } + if (sock.type === 1 && bytesRead < queuedLength) { + var bytesRemaining = queuedLength - bytesRead; + queued.data = new Uint8Array( + queuedBuffer, + queuedOffset + bytesRead, + bytesRemaining + ); + sock.recv_queue.unshift(queued); + } + return res; + }, + }, + }; + var getSocketFromFD = (fd) => { + var socket = SOCKFS.getSocket(fd); + if (!socket) throw new FS.ErrnoError(8); + return socket; + }; + var inetPton4 = (str) => { + var b = str.split('.'); + for (var i = 0; i < 4; i++) { + var tmp = Number(b[i]); + if (isNaN(tmp)) return null; + b[i] = tmp; + } + return (b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24)) >>> 0; + }; + var inetPton6 = (str) => { + var words; + var w, offset, z; + var valid6regx = + /^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i; + var parts = []; + if (!valid6regx.test(str)) { + return null; + } + if (str === '::') { + return [0, 0, 0, 0, 0, 0, 0, 0]; + } + if (str.startsWith('::')) { + str = str.replace('::', 'Z:'); + } else { + str = str.replace('::', ':Z:'); + } + if (str.indexOf('.') > 0) { + str = str.replace(new RegExp('[.]', 'g'), ':'); + words = str.split(':'); + words[words.length - 4] = + Number(words[words.length - 4]) + + Number(words[words.length - 3]) * 256; + words[words.length - 3] = + Number(words[words.length - 2]) + + Number(words[words.length - 1]) * 256; + words = words.slice(0, words.length - 2); + } else { + words = str.split(':'); + } + offset = 0; + z = 0; + for (w = 0; w < words.length; w++) { + if (typeof words[w] == 'string') { + if (words[w] === 'Z') { + for (z = 0; z < 8 - words.length + 1; z++) { + parts[w + z] = 0; + } + offset = z - 1; + } else { + parts[w + offset] = _htons(parseInt(words[w], 16)); + } + } else { + parts[w + offset] = words[w]; + } + } + return [ + (parts[1] << 16) | parts[0], + (parts[3] << 16) | parts[2], + (parts[5] << 16) | parts[4], + (parts[7] << 16) | parts[6], + ]; + }; + var writeSockaddr = (sa, family, addr, port, addrlen) => { + switch (family) { + case 2: + addr = inetPton4(addr); + zeroMemory(sa, 16); + if (addrlen) { + HEAP32[addrlen >> 2] = 16; + } + HEAP16[sa >> 1] = family; + HEAP32[(sa + 4) >> 2] = addr; + HEAP16[(sa + 2) >> 1] = _htons(port); + break; + case 10: + addr = inetPton6(addr); + zeroMemory(sa, 28); + if (addrlen) { + HEAP32[addrlen >> 2] = 28; + } + HEAP32[sa >> 2] = family; + HEAP32[(sa + 8) >> 2] = addr[0]; + HEAP32[(sa + 12) >> 2] = addr[1]; + HEAP32[(sa + 16) >> 2] = addr[2]; + HEAP32[(sa + 20) >> 2] = addr[3]; + HEAP16[(sa + 2) >> 1] = _htons(port); + break; + default: + return 5; + } + return 0; + }; + var DNS = { + address_map: { id: 1, addrs: {}, names: {} }, + lookup_name(name) { + var res = inetPton4(name); + if (res !== null) { + return name; + } + res = inetPton6(name); + if (res !== null) { + return name; + } + var addr; + if (DNS.address_map.addrs[name]) { + addr = DNS.address_map.addrs[name]; + } else { + var id = DNS.address_map.id++; + addr = '172.29.' + (id & 255) + '.' + (id & 65280); + DNS.address_map.names[addr] = name; + DNS.address_map.addrs[name] = addr; + } + return addr; + }, + lookup_addr(addr) { + if (DNS.address_map.names[addr]) { + return DNS.address_map.names[addr]; + } + return null; + }, + }; + function ___syscall_accept4(fd, addr, addrlen, flags, d1, d2) { + try { + var sock = getSocketFromFD(fd); + var newsock = sock.sock_ops.accept(sock); + if (addr) { + var errno = writeSockaddr( + addr, + newsock.family, + DNS.lookup_name(newsock.daddr), + newsock.dport, + addrlen + ); + } + return newsock.stream.fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_accept4.sig = 'iippiii'; + var inetNtop4 = (addr) => + (addr & 255) + + '.' + + ((addr >> 8) & 255) + + '.' + + ((addr >> 16) & 255) + + '.' + + ((addr >> 24) & 255); + var inetNtop6 = (ints) => { + var str = ''; + var word = 0; + var longest = 0; + var lastzero = 0; + var zstart = 0; + var len = 0; + var i = 0; + var parts = [ + ints[0] & 65535, + ints[0] >> 16, + ints[1] & 65535, + ints[1] >> 16, + ints[2] & 65535, + ints[2] >> 16, + ints[3] & 65535, + ints[3] >> 16, + ]; + var hasipv4 = true; + var v4part = ''; + for (i = 0; i < 5; i++) { + if (parts[i] !== 0) { + hasipv4 = false; + break; + } + } + if (hasipv4) { + v4part = inetNtop4(parts[6] | (parts[7] << 16)); + if (parts[5] === -1) { + str = '::ffff:'; + str += v4part; + return str; + } + if (parts[5] === 0) { + str = '::'; + if (v4part === '0.0.0.0') v4part = ''; + if (v4part === '0.0.0.1') v4part = '1'; + str += v4part; + return str; + } + } + for (word = 0; word < 8; word++) { + if (parts[word] === 0) { + if (word - lastzero > 1) { + len = 0; + } + lastzero = word; + len++; + } + if (len > longest) { + longest = len; + zstart = word - longest + 1; + } + } + for (word = 0; word < 8; word++) { + if (longest > 1) { + if ( + parts[word] === 0 && + word >= zstart && + word < zstart + longest + ) { + if (word === zstart) { + str += ':'; + if (zstart === 0) str += ':'; + } + continue; + } + } + str += Number(_ntohs(parts[word] & 65535)).toString(16); + str += word < 7 ? ':' : ''; + } + return str; + }; + var readSockaddr = (sa, salen) => { + var family = HEAP16[sa >> 1]; + var port = _ntohs(HEAPU16[(sa + 2) >> 1]); + var addr; + switch (family) { + case 2: + if (salen !== 16) { + return { errno: 28 }; + } + addr = HEAP32[(sa + 4) >> 2]; + addr = inetNtop4(addr); + break; + case 10: + if (salen !== 28) { + return { errno: 28 }; + } + addr = [ + HEAP32[(sa + 8) >> 2], + HEAP32[(sa + 12) >> 2], + HEAP32[(sa + 16) >> 2], + HEAP32[(sa + 20) >> 2], + ]; + addr = inetNtop6(addr); + break; + default: + return { errno: 5 }; + } + return { family, addr, port }; + }; + var getSocketAddress = (addrp, addrlen) => { + var info = readSockaddr(addrp, addrlen); + if (info.errno) throw new FS.ErrnoError(info.errno); + info.addr = DNS.lookup_addr(info.addr) || info.addr; + return info; + }; + function ___syscall_bind(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + var info = getSocketAddress(addr, addrlen); + sock.sock_ops.bind(sock, info.addr, info.port); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_bind.sig = 'iippiii'; + var SYSCALLS = { + DEFAULT_POLLMASK: 5, + calculateAt(dirfd, path, allowEmpty) { + if (PATH.isAbs(path)) { + return path; + } + var dir; + if (dirfd === -100) { + dir = FS.cwd(); + } else { + var dirstream = SYSCALLS.getStreamFromFD(dirfd); + dir = dirstream.path; + } + if (path.length == 0) { + if (!allowEmpty) { + throw new FS.ErrnoError(44); + } + return dir; + } + return dir + '/' + path; + }, + writeStat(buf, stat) { + HEAPU32[buf >> 2] = stat.dev; + HEAPU32[(buf + 4) >> 2] = stat.mode; + HEAPU32[(buf + 8) >> 2] = stat.nlink; + HEAPU32[(buf + 12) >> 2] = stat.uid; + HEAPU32[(buf + 16) >> 2] = stat.gid; + HEAPU32[(buf + 20) >> 2] = stat.rdev; + HEAP64[(buf + 24) >> 3] = BigInt(stat.size); + HEAP32[(buf + 32) >> 2] = 4096; + HEAP32[(buf + 36) >> 2] = stat.blocks; + var atime = stat.atime.getTime(); + var mtime = stat.mtime.getTime(); + var ctime = stat.ctime.getTime(); + HEAP64[(buf + 40) >> 3] = BigInt(Math.floor(atime / 1e3)); + HEAPU32[(buf + 48) >> 2] = (atime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 56) >> 3] = BigInt(Math.floor(mtime / 1e3)); + HEAPU32[(buf + 64) >> 2] = (mtime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 72) >> 3] = BigInt(Math.floor(ctime / 1e3)); + HEAPU32[(buf + 80) >> 2] = (ctime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 88) >> 3] = BigInt(stat.ino); + return 0; + }, + writeStatFs(buf, stats) { + HEAPU32[(buf + 4) >> 2] = stats.bsize; + HEAPU32[(buf + 60) >> 2] = stats.bsize; + HEAP64[(buf + 8) >> 3] = BigInt(stats.blocks); + HEAP64[(buf + 16) >> 3] = BigInt(stats.bfree); + HEAP64[(buf + 24) >> 3] = BigInt(stats.bavail); + HEAP64[(buf + 32) >> 3] = BigInt(stats.files); + HEAP64[(buf + 40) >> 3] = BigInt(stats.ffree); + HEAPU32[(buf + 48) >> 2] = stats.fsid; + HEAPU32[(buf + 64) >> 2] = stats.flags; + HEAPU32[(buf + 56) >> 2] = stats.namelen; + }, + doMsync(addr, stream, len, flags, offset) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError(43); + } + if (flags & 2) { + return 0; + } + var buffer = HEAPU8.slice(addr, addr + len); + FS.msync(stream, buffer, offset, len, flags); + }, + getStreamFromFD(fd) { + var stream = FS.getStreamChecked(fd); + return stream; + }, + varargs: undefined, + getStr(ptr) { + var ret = UTF8ToString(ptr); + return ret; + }, + }; + function ___syscall_chdir(path) { + try { + path = SYSCALLS.getStr(path); + FS.chdir(path); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_chdir.sig = 'ip'; + function ___syscall_chmod(path, mode) { + try { + path = SYSCALLS.getStr(path); + FS.chmod(path, mode); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_chmod.sig = 'ipi'; + var allocateUTF8OnStack = (...args) => stringToUTF8OnStack(...args); + var onInits = []; + var addOnInit = (cb) => onInits.push(cb); + function _js_getpid() { + return PHPLoader.processId ?? 42; + } + function _js_wasm_trace(format, ...args) { + if (PHPLoader.trace instanceof Function) { + PHPLoader.trace(_js_getpid(), format, ...args); + } + } + var PHPWASM = { + O_APPEND: 1024, + O_NONBLOCK: 2048, + POLLHUP: 16, + SETFL_MASK: 3072, + init: function () { + if (PHPLoader.bindUserSpace) { + addOnInit(() => { + if (typeof PHPLoader.processId !== 'number') { + throw new Error( + 'PHPLoader.processId must be set before init' + ); + } + Module['userSpace'] = PHPLoader.bindUserSpace({ + pid: PHPLoader.processId, + constants: { + F_GETFL: Number('3'), + O_ACCMODE: Number('2097155'), + O_RDONLY: Number('0'), + O_WRONLY: Number('1'), + O_APPEND: Number('1024'), + O_NONBLOCK: Number('2048'), + F_SETFL: Number('4'), + F_GETLK: Number('12'), + F_SETLK: Number('13'), + F_SETLKW: Number('14'), + SEEK_SET: Number('0'), + SEEK_CUR: Number('1'), + SEEK_END: Number('2'), + F_GETFL: Number('3'), + O_ACCMODE: Number('2097155'), + O_RDONLY: Number('0'), + O_WRONLY: Number('1'), + O_APPEND: Number('1024'), + O_NONBLOCK: Number('2048'), + F_SETFL: Number('4'), + F_GETLK: Number('12'), + F_SETLK: Number('13'), + F_SETLKW: Number('14'), + SEEK_SET: Number('0'), + SEEK_CUR: Number('1'), + SEEK_END: Number('2'), + F_RDLCK: 0, + F_WRLCK: 1, + F_UNLCK: 2, + LOCK_SH: 1, + LOCK_EX: 2, + LOCK_NB: 4, + LOCK_UN: 8, + }, + errnoCodes: ERRNO_CODES, + memory: { + HEAP8: { + get(offset) { + return HEAP8[offset]; + }, + set(offset, value) { + HEAP8[offset] = value; + }, + }, + HEAPU8: { + get(offset) { + return HEAPU8[offset]; + }, + set(offset, value) { + HEAPU8[offset] = value; + }, + }, + HEAP16: { + get(offset) { + return HEAP16[offset]; + }, + set(offset, value) { + HEAP16[offset] = value; + }, + }, + HEAPU16: { + get(offset) { + return HEAPU16[offset]; + }, + set(offset, value) { + HEAPU16[offset] = value; + }, + }, + HEAP32: { + get(offset) { + return HEAP32[offset]; + }, + set(offset, value) { + HEAP32[offset] = value; + }, + }, + HEAPU32: { + get(offset) { + return HEAPU32[offset]; + }, + set(offset, value) { + HEAPU32[offset] = value; + }, + }, + HEAPF32: { + get(offset) { + return HEAPF32[offset]; + }, + set(offset, value) { + HEAPF32[offset] = value; + }, + }, + HEAP64: { + get(offset) { + return HEAP64[offset]; + }, + set(offset, value) { + HEAP64[offset] = value; + }, + }, + HEAPU64: { + get(offset) { + return HEAPU64[offset]; + }, + set(offset, value) { + HEAPU64[offset] = value; + }, + }, + HEAPF64: { + get(offset) { + return HEAPF64[offset]; + }, + set(offset, value) { + HEAPF64[offset] = value; + }, + }, + }, + wasmImports: Object.assign( + {}, + wasmImports, + typeof _builtin_fd_close === 'function' + ? { builtin_fd_close: _builtin_fd_close } + : {}, + typeof _builtin_fcntl64 === 'function' + ? { builtin_fcntl64: _builtin_fcntl64 } + : {} + ), + wasmExports, + syscalls: SYSCALLS, + FS, + PROXYFS, + NODEFS, + }); + }); + } + Module['ENV'] = Module['ENV'] || {}; + Module['ENV']['PATH'] = [ + Module['ENV']['PATH'], + '/internal/shared/bin', + ] + .filter(Boolean) + .join(':'); + FS.mkdir('/request'); + FS.mkdir('/internal'); + if (PHPLoader.nativeInternalDirPath) { + FS.mount( + FS.filesystems.NODEFS, + { root: PHPLoader.nativeInternalDirPath }, + '/internal' + ); + } + FS.mkdirTree('/internal/shared'); + FS.mkdirTree('/internal/shared/preload'); + FS.mkdirTree('/internal/shared/bin'); + const originalOnRuntimeInitialized = Module['onRuntimeInitialized']; + Module['onRuntimeInitialized'] = () => { + const { node: phpBinaryNode } = FS.lookupPath( + '/internal/shared/bin/php', + { noent_okay: true } + ); + if (!phpBinaryNode) { + FS.writeFile( + '/internal/shared/bin/php', + new TextEncoder().encode('#!/bin/sh\nphp "$@"') + ); + FS.chmod('/internal/shared/bin/php', 493); + } + originalOnRuntimeInitialized(); + }; + FS.registerDevice(FS.makedev(64, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onStdout(chunk); + return length; + }, + }); + FS.mkdev('/request/stdout', FS.makedev(64, 0)); + FS.registerDevice(FS.makedev(63, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onStderr(chunk); + return length; + }, + }); + FS.mkdev('/request/stderr', FS.makedev(63, 0)); + FS.registerDevice(FS.makedev(62, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onHeaders(chunk); + return length; + }, + }); + FS.mkdev('/request/headers', FS.makedev(62, 0)); + PHPWASM.EventEmitter = ENVIRONMENT_IS_NODE + ? require('events').EventEmitter + : class EventEmitter { + constructor() { + this.listeners = {}; + } + emit(eventName, data) { + if (this.listeners[eventName]) { + this.listeners[eventName].forEach( + (callback) => { + callback(data); + } + ); + } + } + once(eventName, callback) { + const self = this; + function removedCallback() { + callback(...arguments); + self.removeListener(eventName, removedCallback); + } + this.on(eventName, removedCallback); + } + removeAllListeners(eventName) { + if (eventName) { + delete this.listeners[eventName]; + } else { + this.listeners = {}; + } + } + removeListener(eventName, callback) { + if (this.listeners[eventName]) { + const idx = + this.listeners[eventName].indexOf(callback); + if (idx !== -1) { + this.listeners[eventName].splice(idx, 1); + } + } + } + }; + PHPWASM.processTable = {}; + PHPWASM.input_devices = {}; + const originalWrite = TTY.stream_ops.write; + TTY.stream_ops.write = function (stream, ...rest) { + const retval = originalWrite(stream, ...rest); + stream.tty.ops.fsync(stream.tty); + return retval; + }; + const originalPutChar = TTY.stream_ops.put_char; + TTY.stream_ops.put_char = function (tty, val) { + if (val === 10) tty.output.push(val); + return originalPutChar(tty, val); + }; + }, + onHeaders: function (chunk) { + if (Module['onHeaders']) { + Module['onHeaders'](chunk); + return; + } + console.log('headers', { chunk }); + }, + onStdout: function (chunk) { + if (Module['onStdout']) { + Module['onStdout'](chunk); + return; + } + if (ENVIRONMENT_IS_NODE) { + process.stdout.write(chunk); + } else { + console.log('stdout', { chunk }); + } + }, + onStderr: function (chunk) { + if (Module['onStderr']) { + Module['onStderr'](chunk); + return; + } + if (ENVIRONMENT_IS_NODE) { + process.stderr.write(chunk); + } else { + console.warn('stderr', { chunk }); + } + }, + getAllWebSockets: function (sock) { + const webSockets = new Set(); + if (sock.server) { + sock.server.clients.forEach((ws) => { + webSockets.add(ws); + }); + } + for (const peer of PHPWASM.getAllPeers(sock)) { + webSockets.add(peer.socket); + } + return Array.from(webSockets); + }, + getAllPeers: function (sock) { + const peers = new Set(); + if (sock.server) { + sock.pending + .filter((pending) => pending.peers) + .forEach((pending) => { + for (const peer of Object.values(pending.peers)) { + peers.add(peer); + } + }); + } + if (sock.peers) { + for (const peer of Object.values(sock.peers)) { + peers.add(peer); + } + } + return Array.from(peers); + }, + awaitData: function (ws) { + return PHPWASM.awaitEvent(ws, 'message'); + }, + awaitConnection: function (ws) { + if (ws.OPEN === ws.readyState) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'open'); + }, + awaitClose: function (ws) { + if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'close'); + }, + awaitError: function (ws) { + if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'error'); + }, + awaitEvent: function (ws, event) { + let resolve; + const listener = () => { + resolve(); + }; + const promise = new Promise(function (_resolve) { + resolve = _resolve; + ws.once(event, listener); + }); + const cancel = () => { + ws.removeListener(event, listener); + setTimeout(resolve); + }; + return [promise, cancel]; + }, + noop: function () {}, + spawnProcess: function (command, args, options) { + if (Module['spawnProcess']) { + const spawned = Module['spawnProcess'](command, args, { + ...options, + shell: true, + stdio: ['pipe', 'pipe', 'pipe'], + }); + if (spawned && !('then' in spawned) && 'on' in spawned) { + return spawned; + } + return Promise.resolve(spawned).then(function (spawned) { + if (!spawned || !spawned.on) { + throw new Error( + 'spawnProcess() must return an EventEmitter but returned a different type.' + ); + } + return spawned; + }); + } + const e = new Error( + 'popen(), proc_open() etc. are unsupported on this PHP instance. Call php.setSpawnHandler() ' + + 'and provide a callback to handle spawning processes, or disable a popen(), proc_open() ' + + 'and similar functions via php.ini.' + ); + e.code = 'SPAWN_UNSUPPORTED'; + throw e; + }, + shutdownSocket: function (socketd, how) { + const sock = getSocketFromFD(socketd); + const peer = Object.values(sock.peers)[0]; + if (!peer) { + return -1; + } + try { + peer.socket.close(); + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + return 0; + } catch (e) { + console.log('Socket shutdown error', e); + return -1; + } + }, + }; + function _wasm_connect(sockfd, addr, addrlen) { + if (!('Suspending' in WebAssembly)) { + var sock = getSocketFromFD(sockfd); + var info = getSocketAddress(addr, addrlen); + sock.sock_ops.connect(sock, info.addr, info.port); + return 0; + } + return Asyncify.handleSleep((wakeUp) => { + let sock; + try { + sock = getSocketFromFD(sockfd); + } catch (e) { + wakeUp(-ERRNO_CODES.EBADF); + return; + } + if (!sock) { + wakeUp(-ERRNO_CODES.EBADF); + return; + } + let info; + try { + info = getSocketAddress(addr, addrlen); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + wakeUp(-ERRNO_CODES.EFAULT); + return; + } + wakeUp(-e.errno); + return; + } + try { + sock.sock_ops.connect(sock, info.addr, info.port); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + wakeUp(-e.errno); + return; + } + const webSockets = PHPWASM.getAllWebSockets(sock); + if (!webSockets.length) { + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + const ws = webSockets[0]; + if (ws.readyState === ws.OPEN) { + wakeUp(0); + return; + } + if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) { + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + const timeout = 3e4; + let resolved = false; + const timeoutId = setTimeout(() => { + if (!resolved) { + resolved = true; + wakeUp(-ERRNO_CODES.ETIMEDOUT); + } + }, timeout); + const handleOpen = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('error', handleError); + ws.removeEventListener('close', handleClose); + wakeUp(0); + } + }; + const handleError = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('open', handleOpen); + ws.removeEventListener('close', handleClose); + wakeUp(-ERRNO_CODES.ECONNREFUSED); + } + }; + const handleClose = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('open', handleOpen); + ws.removeEventListener('error', handleError); + wakeUp(-ERRNO_CODES.ECONNREFUSED); + } + }; + ws.addEventListener('open', handleOpen); + ws.addEventListener('error', handleError); + ws.addEventListener('close', handleClose); + }); + } + function ___syscall_connect(sockfd, addr, addrlen, d1, d2, d3) { + return _wasm_connect(sockfd, addr, addrlen); + } + ___syscall_connect.sig = 'iippiii'; + function ___syscall_dup(fd) { + try { + var old = SYSCALLS.getStreamFromFD(fd); + return FS.dupStream(old).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_dup.sig = 'ii'; + function ___syscall_dup3(fd, newfd, flags) { + try { + var old = SYSCALLS.getStreamFromFD(fd); + if (old.fd === newfd) return -28; + if (newfd < 0 || newfd >= FS.MAX_OPEN_FDS) return -8; + var existing = FS.getStream(newfd); + if (existing) FS.close(existing); + return FS.dupStream(old, newfd).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_dup3.sig = 'iiii'; + function ___syscall_faccessat(dirfd, path, amode, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (amode & ~7) { + return -28; + } + var lookup = FS.lookupPath(path, { follow: true }); + var node = lookup.node; + if (!node) { + return -44; + } + var perms = ''; + if (amode & 4) perms += 'r'; + if (amode & 2) perms += 'w'; + if (amode & 1) perms += 'x'; + if (perms && FS.nodePermissions(node, perms)) { + return -2; + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_faccessat.sig = 'iipii'; + function ___syscall_fchmod(fd, mode) { + try { + FS.fchmod(fd, mode); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_fchmod.sig = 'iii'; + function ___syscall_fchown32(fd, owner, group) { + try { + FS.fchown(fd, owner, group); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_fchown32.sig = 'iiii'; + function ___syscall_fchownat(dirfd, path, owner, group, flags) { + try { + path = SYSCALLS.getStr(path); + var nofollow = flags & 256; + flags = flags & ~256; + path = SYSCALLS.calculateAt(dirfd, path); + (nofollow ? FS.lchown : FS.chown)(path, owner, group); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_fchownat.sig = 'iipiii'; + var syscallGetVarargI = () => { + var ret = HEAP32[+SYSCALLS.varargs >> 2]; + SYSCALLS.varargs += 4; + return ret; + }; + var syscallGetVarargP = syscallGetVarargI; + function ___syscall_fcntl64(fd, cmd, varargs) { + SYSCALLS.varargs = varargs; + try { + var stream = SYSCALLS.getStreamFromFD(fd); + switch (cmd) { + case 0: { + var arg = syscallGetVarargI(); + if (arg < 0) { + return -28; + } + while (FS.streams[arg]) { + arg++; + } + var newStream; + newStream = FS.dupStream(stream, arg); + return newStream.fd; + } + case 1: + case 2: + return 0; + case 3: + return stream.flags; + case 4: { + var arg = syscallGetVarargI(); + stream.flags |= arg; + return 0; + } + case 12: { + var arg = syscallGetVarargP(); + var offset = 0; + HEAP16[(arg + offset) >> 1] = 2; + return 0; + } + case 13: + case 14: + return 0; + } + return -28; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_fcntl64.sig = 'iiip'; + function ___syscall_fstat64(fd, buf) { + try { + return SYSCALLS.writeStat(buf, FS.fstat(fd)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_fstat64.sig = 'iip'; + var INT53_MAX = 9007199254740992; + var INT53_MIN = -9007199254740992; + var bigintToI53Checked = (num) => + num < INT53_MIN || num > INT53_MAX ? NaN : Number(num); + function ___syscall_ftruncate64(fd, length) { + length = bigintToI53Checked(length); + try { + if (isNaN(length)) return -61; + FS.ftruncate(fd, length); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_ftruncate64.sig = 'iij'; + function ___syscall_getcwd(buf, size) { + try { + if (size === 0) return -28; + var cwd = FS.cwd(); + var cwdLengthInBytes = lengthBytesUTF8(cwd) + 1; + if (size < cwdLengthInBytes) return -68; + stringToUTF8(cwd, buf, size); + return cwdLengthInBytes; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_getcwd.sig = 'ipp'; + function ___syscall_getdents64(fd, dirp, count) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + stream.getdents ||= FS.readdir(stream.path); + var struct_size = 280; + var pos = 0; + var off = FS.llseek(stream, 0, 1); + var startIdx = Math.floor(off / struct_size); + var endIdx = Math.min( + stream.getdents.length, + startIdx + Math.floor(count / struct_size) + ); + for (var idx = startIdx; idx < endIdx; idx++) { + var id; + var type; + var name = stream.getdents[idx]; + if (name === '.') { + id = stream.node.id; + type = 4; + } else if (name === '..') { + var lookup = FS.lookupPath(stream.path, { parent: true }); + id = lookup.node.id; + type = 4; + } else { + var child; + try { + child = FS.lookupNode(stream.node, name); + } catch (e) { + if (e?.errno === 28) { + continue; + } + throw e; + } + id = child.id; + type = FS.isChrdev(child.mode) + ? 2 + : FS.isDir(child.mode) + ? 4 + : FS.isLink(child.mode) + ? 10 + : 8; + } + HEAP64[(dirp + pos) >> 3] = BigInt(id); + HEAP64[(dirp + pos + 8) >> 3] = BigInt((idx + 1) * struct_size); + HEAP16[(dirp + pos + 16) >> 1] = 280; + HEAP8[dirp + pos + 18] = type; + stringToUTF8(name, dirp + pos + 19, 256); + pos += struct_size; + } + FS.llseek(stream, idx * struct_size, 0); + return pos; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_getdents64.sig = 'iipp'; + function ___syscall_getpeername(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + if (!sock.daddr) { + return -53; + } + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(sock.daddr), + sock.dport, + addrlen + ); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_getpeername.sig = 'iippiii'; + function ___syscall_getsockname(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(sock.saddr || '0.0.0.0'), + sock.sport, + addrlen + ); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_getsockname.sig = 'iippiii'; + function ___syscall_getsockopt(fd, level, optname, optval, optlen, d1) { + try { + var sock = getSocketFromFD(fd); + if (level === 1) { + if (optname === 4) { + HEAP32[optval >> 2] = sock.error; + HEAP32[optlen >> 2] = 4; + sock.error = null; + return 0; + } + } + return -50; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_getsockopt.sig = 'iiiippi'; + function ___syscall_ioctl(fd, op, varargs) { + SYSCALLS.varargs = varargs; + try { + var stream = SYSCALLS.getStreamFromFD(fd); + switch (op) { + case 21509: { + if (!stream.tty) return -59; + return 0; + } + case 21505: { + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tcgets) { + var termios = stream.tty.ops.ioctl_tcgets(stream); + var argp = syscallGetVarargP(); + HEAP32[argp >> 2] = termios.c_iflag || 0; + HEAP32[(argp + 4) >> 2] = termios.c_oflag || 0; + HEAP32[(argp + 8) >> 2] = termios.c_cflag || 0; + HEAP32[(argp + 12) >> 2] = termios.c_lflag || 0; + for (var i = 0; i < 32; i++) { + HEAP8[argp + i + 17] = termios.c_cc[i] || 0; + } + return 0; + } + return 0; + } + case 21510: + case 21511: + case 21512: { + if (!stream.tty) return -59; + return 0; + } + case 21506: + case 21507: + case 21508: { + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tcsets) { + var argp = syscallGetVarargP(); + var c_iflag = HEAP32[argp >> 2]; + var c_oflag = HEAP32[(argp + 4) >> 2]; + var c_cflag = HEAP32[(argp + 8) >> 2]; + var c_lflag = HEAP32[(argp + 12) >> 2]; + var c_cc = []; + for (var i = 0; i < 32; i++) { + c_cc.push(HEAP8[argp + i + 17]); + } + return stream.tty.ops.ioctl_tcsets(stream.tty, op, { + c_iflag, + c_oflag, + c_cflag, + c_lflag, + c_cc, + }); + } + return 0; + } + case 21519: { + if (!stream.tty) return -59; + var argp = syscallGetVarargP(); + HEAP32[argp >> 2] = 0; + return 0; + } + case 21520: { + if (!stream.tty) return -59; + return -28; + } + case 21537: + case 21531: { + var argp = syscallGetVarargP(); + return FS.ioctl(stream, op, argp); + } + case 21523: { + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tiocgwinsz) { + var winsize = stream.tty.ops.ioctl_tiocgwinsz( + stream.tty + ); + var argp = syscallGetVarargP(); + HEAP16[argp >> 1] = winsize[0]; + HEAP16[(argp + 2) >> 1] = winsize[1]; + } + return 0; + } + case 21524: { + if (!stream.tty) return -59; + return 0; + } + case 21515: { + if (!stream.tty) return -59; + return 0; + } + default: + return -28; + } + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_ioctl.sig = 'iiip'; + function ___syscall_listen(fd, backlog) { + try { + var sock = getSocketFromFD(fd); + sock.sock_ops.listen(sock, backlog); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_listen.sig = 'iiiiiii'; + function ___syscall_lstat64(path, buf) { + try { + path = SYSCALLS.getStr(path); + return SYSCALLS.writeStat(buf, FS.lstat(path)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_lstat64.sig = 'ipp'; + function ___syscall_mkdirat(dirfd, path, mode) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + FS.mkdir(path, mode, 0); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_mkdirat.sig = 'iipi'; + function ___syscall_newfstatat(dirfd, path, buf, flags) { + try { + path = SYSCALLS.getStr(path); + var nofollow = flags & 256; + var allowEmpty = flags & 4096; + flags = flags & ~6400; + path = SYSCALLS.calculateAt(dirfd, path, allowEmpty); + return SYSCALLS.writeStat( + buf, + nofollow ? FS.lstat(path) : FS.stat(path) + ); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_newfstatat.sig = 'iippi'; + function ___syscall_openat(dirfd, path, flags, varargs) { + SYSCALLS.varargs = varargs; + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + var mode = varargs ? syscallGetVarargI() : 0; + return FS.open(path, flags, mode).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_openat.sig = 'iipip'; + var PIPEFS = { + BUCKET_BUFFER_SIZE: 8192, + mount(mount) { + return FS.createNode(null, '/', 16384 | 511, 0); + }, + createPipe() { + var pipe = { buckets: [], refcnt: 2, timestamp: new Date() }; + pipe.buckets.push({ + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: 0, + roffset: 0, + }); + var rName = PIPEFS.nextname(); + var wName = PIPEFS.nextname(); + var rNode = FS.createNode(PIPEFS.root, rName, 4096, 0); + var wNode = FS.createNode(PIPEFS.root, wName, 4096, 0); + rNode.pipe = pipe; + wNode.pipe = pipe; + var readableStream = FS.createStream({ + path: rName, + node: rNode, + flags: 0, + seekable: false, + stream_ops: PIPEFS.stream_ops, + }); + rNode.stream = readableStream; + var writableStream = FS.createStream({ + path: wName, + node: wNode, + flags: 1, + seekable: false, + stream_ops: PIPEFS.stream_ops, + }); + wNode.stream = writableStream; + return { + readable_fd: readableStream.fd, + writable_fd: writableStream.fd, + }; + }, + stream_ops: { + getattr(stream) { + var node = stream.node; + var timestamp = node.pipe.timestamp; + return { + dev: 14, + ino: node.id, + mode: 4480, + nlink: 1, + uid: 0, + gid: 0, + rdev: 0, + size: 0, + atime: timestamp, + mtime: timestamp, + ctime: timestamp, + blksize: 4096, + blocks: 0, + }; + }, + poll(stream) { + var pipe = stream.node.pipe; + if ((stream.flags & 2097155) === 1) { + return 256 | 4; + } + for (var bucket of pipe.buckets) { + if (bucket.offset - bucket.roffset > 0) { + return 64 | 1; + } + } + return 0; + }, + dup(stream) { + stream.node.pipe.refcnt++; + }, + ioctl(stream, request, varargs) { + return 28; + }, + fsync(stream) { + return 28; + }, + read(stream, buffer, offset, length, position) { + var pipe = stream.node.pipe; + var currentLength = 0; + for (var bucket of pipe.buckets) { + currentLength += bucket.offset - bucket.roffset; + } + var data = buffer.subarray(offset, offset + length); + if (length <= 0) { + return 0; + } + if (currentLength == 0) { + if (pipe.refcnt < 2) { + return 0; + } + throw new FS.ErrnoError(6); + } + var toRead = Math.min(currentLength, length); + var totalRead = toRead; + var toRemove = 0; + for (var bucket of pipe.buckets) { + var bucketSize = bucket.offset - bucket.roffset; + if (toRead <= bucketSize) { + var tmpSlice = bucket.buffer.subarray( + bucket.roffset, + bucket.offset + ); + if (toRead < bucketSize) { + tmpSlice = tmpSlice.subarray(0, toRead); + bucket.roffset += toRead; + } else { + toRemove++; + } + data.set(tmpSlice); + break; + } else { + var tmpSlice = bucket.buffer.subarray( + bucket.roffset, + bucket.offset + ); + data.set(tmpSlice); + data = data.subarray(tmpSlice.byteLength); + toRead -= tmpSlice.byteLength; + toRemove++; + } + } + if (toRemove && toRemove == pipe.buckets.length) { + toRemove--; + pipe.buckets[toRemove].offset = 0; + pipe.buckets[toRemove].roffset = 0; + } + pipe.buckets.splice(0, toRemove); + return totalRead; + }, + write(stream, buffer, offset, length, position) { + var pipe = stream.node.pipe; + var data = buffer.subarray(offset, offset + length); + var dataLen = data.byteLength; + if (dataLen <= 0) { + return 0; + } + var currBucket = null; + if (pipe.buckets.length == 0) { + currBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: 0, + roffset: 0, + }; + pipe.buckets.push(currBucket); + } else { + currBucket = pipe.buckets[pipe.buckets.length - 1]; + } + var freeBytesInCurrBuffer = + PIPEFS.BUCKET_BUFFER_SIZE - currBucket.offset; + if (freeBytesInCurrBuffer >= dataLen) { + currBucket.buffer.set(data, currBucket.offset); + currBucket.offset += dataLen; + return dataLen; + } else if (freeBytesInCurrBuffer > 0) { + currBucket.buffer.set( + data.subarray(0, freeBytesInCurrBuffer), + currBucket.offset + ); + currBucket.offset += freeBytesInCurrBuffer; + data = data.subarray( + freeBytesInCurrBuffer, + data.byteLength + ); + } + var numBuckets = + (data.byteLength / PIPEFS.BUCKET_BUFFER_SIZE) | 0; + var remElements = data.byteLength % PIPEFS.BUCKET_BUFFER_SIZE; + for (var i = 0; i < numBuckets; i++) { + var newBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: PIPEFS.BUCKET_BUFFER_SIZE, + roffset: 0, + }; + pipe.buckets.push(newBucket); + newBucket.buffer.set( + data.subarray(0, PIPEFS.BUCKET_BUFFER_SIZE) + ); + data = data.subarray( + PIPEFS.BUCKET_BUFFER_SIZE, + data.byteLength + ); + } + if (remElements > 0) { + var newBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: data.byteLength, + roffset: 0, + }; + pipe.buckets.push(newBucket); + newBucket.buffer.set(data); + } + return dataLen; + }, + close(stream) { + var pipe = stream.node.pipe; + pipe.refcnt--; + if (pipe.refcnt === 0) { + pipe.buckets = null; + } + }, + }, + nextname() { + if (!PIPEFS.nextname.current) { + PIPEFS.nextname.current = 0; + } + return 'pipe[' + PIPEFS.nextname.current++ + ']'; + }, + }; + function ___syscall_pipe(fdPtr) { + try { + if (fdPtr == 0) { + throw new FS.ErrnoError(21); + } + var res = PIPEFS.createPipe(); + HEAP32[fdPtr >> 2] = res.readable_fd; + HEAP32[(fdPtr + 4) >> 2] = res.writable_fd; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_pipe.sig = 'ip'; + function ___syscall_readlinkat(dirfd, path, buf, bufsize) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (bufsize <= 0) return -28; + var ret = FS.readlink(path); + var len = Math.min(bufsize, lengthBytesUTF8(ret)); + var endChar = HEAP8[buf + len]; + stringToUTF8(ret, buf, bufsize + 1); + HEAP8[buf + len] = endChar; + return len; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_readlinkat.sig = 'iippp'; + function ___syscall_recvfrom(fd, buf, len, flags, addr, addrlen) { + try { + var sock = getSocketFromFD(fd); + var msg = sock.sock_ops.recvmsg( + sock, + len, + typeof flags !== 'undefined' ? flags : 0 + ); + if (!msg) return 0; + if (addr) { + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(msg.addr), + msg.port, + addrlen + ); + } + HEAPU8.set(msg.buffer, buf); + return msg.buffer.byteLength; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_recvfrom.sig = 'iippipp'; + function ___syscall_renameat(olddirfd, oldpath, newdirfd, newpath) { + try { + oldpath = SYSCALLS.getStr(oldpath); + newpath = SYSCALLS.getStr(newpath); + oldpath = SYSCALLS.calculateAt(olddirfd, oldpath); + newpath = SYSCALLS.calculateAt(newdirfd, newpath); + FS.rename(oldpath, newpath); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_renameat.sig = 'iipip'; + function ___syscall_rmdir(path) { + try { + path = SYSCALLS.getStr(path); + FS.rmdir(path); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_rmdir.sig = 'ip'; + function ___syscall_sendto(fd, message, length, flags, addr, addr_len) { + try { + var sock = getSocketFromFD(fd); + if (!addr) { + return FS.write(sock.stream, HEAP8, message, length); + } + var dest = getSocketAddress(addr, addr_len); + return sock.sock_ops.sendmsg( + sock, + HEAP8, + message, + length, + dest.addr, + dest.port + ); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_sendto.sig = 'iippipp'; + function ___syscall_socket(domain, type, protocol) { + try { + var sock = SOCKFS.createSocket(domain, type, protocol); + return sock.stream.fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_socket.sig = 'iiiiiii'; + function ___syscall_stat64(path, buf) { + try { + path = SYSCALLS.getStr(path); + return SYSCALLS.writeStat(buf, FS.stat(path)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_stat64.sig = 'ipp'; + function ___syscall_statfs64(path, size, buf) { + try { + SYSCALLS.writeStatFs(buf, FS.statfs(SYSCALLS.getStr(path))); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_statfs64.sig = 'ippp'; + function ___syscall_symlinkat(target, dirfd, linkpath) { + try { + target = SYSCALLS.getStr(target); + linkpath = SYSCALLS.getStr(linkpath); + linkpath = SYSCALLS.calculateAt(dirfd, linkpath); + FS.symlink(target, linkpath); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_symlinkat.sig = 'ipip'; + function ___syscall_unlinkat(dirfd, path, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (!flags) { + FS.unlink(path); + } else if (flags === 512) { + FS.rmdir(path); + } else { + return -28; + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_unlinkat.sig = 'iipi'; + var readI53FromI64 = (ptr) => + HEAPU32[ptr >> 2] + HEAP32[(ptr + 4) >> 2] * 4294967296; + function ___syscall_utimensat(dirfd, path, times, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path, true); + var now = Date.now(), + atime, + mtime; + if (!times) { + atime = now; + mtime = now; + } else { + var seconds = readI53FromI64(times); + var nanoseconds = HEAP32[(times + 8) >> 2]; + if (nanoseconds == 1073741823) { + atime = now; + } else if (nanoseconds == 1073741822) { + atime = null; + } else { + atime = seconds * 1e3 + nanoseconds / (1e3 * 1e3); + } + times += 16; + seconds = readI53FromI64(times); + nanoseconds = HEAP32[(times + 8) >> 2]; + if (nanoseconds == 1073741823) { + mtime = now; + } else if (nanoseconds == 1073741822) { + mtime = null; + } else { + mtime = seconds * 1e3 + nanoseconds / (1e3 * 1e3); + } + } + if ((mtime ?? atime) !== null) { + FS.utime(path, atime, mtime); + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_utimensat.sig = 'iippi'; + var __abort_js = () => abort(''); + __abort_js.sig = 'v'; + var dlSetError = (msg) => { + var sp = stackSave(); + var cmsg = stringToUTF8OnStack(msg); + ___dl_seterr(cmsg, 0); + stackRestore(sp); + }; + var dlopenInternal = (handle, jsflags) => { + var filename = UTF8ToString(handle + 36); + var flags = HEAP32[(handle + 4) >> 2]; + filename = PATH.normalize(filename); + var global = Boolean(flags & 256); + var localScope = global ? null : {}; + var combinedFlags = { + global, + nodelete: Boolean(flags & 4096), + loadAsync: jsflags.loadAsync, + }; + if (jsflags.loadAsync) { + return loadDynamicLibrary( + filename, + combinedFlags, + localScope, + handle + ); + } + try { + return loadDynamicLibrary( + filename, + combinedFlags, + localScope, + handle + ); + } catch (e) { + dlSetError(`could not load dynamic lib: ${filename}\n${e}`); + return 0; + } + }; + function __dlopen_js(handle) { + var jsflags = { loadAsync: false }; + return dlopenInternal(handle, jsflags); + } + __dlopen_js.sig = 'pp'; + var __dlsym_js = (handle, symbol, symbolIndex) => { + symbol = UTF8ToString(symbol); + var result; + var newSymIndex; + var lib = LDSO.loadedLibsByHandle[handle]; + newSymIndex = Object.keys(lib.exports).indexOf(symbol); + if (newSymIndex == -1 || lib.exports[symbol].stub) { + dlSetError( + `Tried to lookup unknown symbol "${symbol}" in dynamic lib: ${lib.name}` + ); + return 0; + } + result = lib.exports[symbol]; + if (typeof result == 'function') { + if (result.orig) { + result = result.orig; + } + var addr = getFunctionAddress(result); + if (addr) { + result = addr; + } else { + result = addFunction(result, result.sig); + HEAPU32[symbolIndex >> 2] = newSymIndex; + } + } + return result; + }; + __dlsym_js.sig = 'pppp'; + var __emscripten_lookup_name = (name) => { + var nameString = UTF8ToString(name); + return inetPton4(DNS.lookup_name(nameString)); + }; + __emscripten_lookup_name.sig = 'ip'; + var runtimeKeepaliveCounter = 0; + var __emscripten_runtime_keepalive_clear = () => { + noExitRuntime = false; + runtimeKeepaliveCounter = 0; + }; + __emscripten_runtime_keepalive_clear.sig = 'v'; + var __emscripten_throw_longjmp = () => { + throw Infinity; + }; + __emscripten_throw_longjmp.sig = 'v'; + function __gmtime_js(time, tmPtr) { + time = bigintToI53Checked(time); + var date = new Date(time * 1e3); + HEAP32[tmPtr >> 2] = date.getUTCSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getUTCMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getUTCHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getUTCDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getUTCMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getUTCFullYear() - 1900; + HEAP32[(tmPtr + 24) >> 2] = date.getUTCDay(); + var start = Date.UTC(date.getUTCFullYear(), 0, 1, 0, 0, 0, 0); + var yday = ((date.getTime() - start) / (1e3 * 60 * 60 * 24)) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + } + __gmtime_js.sig = 'vjp'; + var isLeapYear = (year) => + year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); + var MONTH_DAYS_LEAP_CUMULATIVE = [ + 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, + ]; + var MONTH_DAYS_REGULAR_CUMULATIVE = [ + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, + ]; + var ydayFromDate = (date) => { + var leap = isLeapYear(date.getFullYear()); + var monthDaysCumulative = leap + ? MONTH_DAYS_LEAP_CUMULATIVE + : MONTH_DAYS_REGULAR_CUMULATIVE; + var yday = monthDaysCumulative[date.getMonth()] + date.getDate() - 1; + return yday; + }; + function __localtime_js(time, tmPtr) { + time = bigintToI53Checked(time); + var date = new Date(time * 1e3); + HEAP32[tmPtr >> 2] = date.getSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getFullYear() - 1900; + HEAP32[(tmPtr + 24) >> 2] = date.getDay(); + var yday = ydayFromDate(date) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + HEAP32[(tmPtr + 36) >> 2] = -(date.getTimezoneOffset() * 60); + var start = new Date(date.getFullYear(), 0, 1); + var summerOffset = new Date( + date.getFullYear(), + 6, + 1 + ).getTimezoneOffset(); + var winterOffset = start.getTimezoneOffset(); + var dst = + (summerOffset != winterOffset && + date.getTimezoneOffset() == + Math.min(winterOffset, summerOffset)) | 0; + HEAP32[(tmPtr + 32) >> 2] = dst; + } + __localtime_js.sig = 'vjp'; + var __mktime_js = function (tmPtr) { + var ret = (() => { + var date = new Date( + HEAP32[(tmPtr + 20) >> 2] + 1900, + HEAP32[(tmPtr + 16) >> 2], + HEAP32[(tmPtr + 12) >> 2], + HEAP32[(tmPtr + 8) >> 2], + HEAP32[(tmPtr + 4) >> 2], + HEAP32[tmPtr >> 2], + 0 + ); + var dst = HEAP32[(tmPtr + 32) >> 2]; + var guessedOffset = date.getTimezoneOffset(); + var start = new Date(date.getFullYear(), 0, 1); + var summerOffset = new Date( + date.getFullYear(), + 6, + 1 + ).getTimezoneOffset(); + var winterOffset = start.getTimezoneOffset(); + var dstOffset = Math.min(winterOffset, summerOffset); + if (dst < 0) { + HEAP32[(tmPtr + 32) >> 2] = Number( + summerOffset != winterOffset && dstOffset == guessedOffset + ); + } else if (dst > 0 != (dstOffset == guessedOffset)) { + var nonDstOffset = Math.max(winterOffset, summerOffset); + var trueOffset = dst > 0 ? dstOffset : nonDstOffset; + date.setTime( + date.getTime() + (trueOffset - guessedOffset) * 6e4 + ); + } + HEAP32[(tmPtr + 24) >> 2] = date.getDay(); + var yday = ydayFromDate(date) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + HEAP32[tmPtr >> 2] = date.getSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getYear(); + var timeMs = date.getTime(); + if (isNaN(timeMs)) { + return -1; + } + return timeMs / 1e3; + })(); + return BigInt(ret); + }; + __mktime_js.sig = 'jp'; + function __mmap_js(len, prot, flags, fd, offset, allocated, addr) { + offset = bigintToI53Checked(offset); + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var res = FS.mmap(stream, len, offset, prot, flags); + var ptr = res.ptr; + HEAP32[allocated >> 2] = res.allocated; + HEAPU32[addr >> 2] = ptr; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + __mmap_js.sig = 'ipiiijpp'; + function __munmap_js(addr, len, prot, flags, fd, offset) { + offset = bigintToI53Checked(offset); + try { + var stream = SYSCALLS.getStreamFromFD(fd); + if (prot & 2) { + SYSCALLS.doMsync(addr, stream, len, flags, offset); + } + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + __munmap_js.sig = 'ippiiij'; + var timers = {}; + var handleException = (e) => { + if (e instanceof ExitStatus || e == 'unwind') { + return EXITSTATUS; + } + quit_(1, e); + }; + var keepRuntimeAlive = () => noExitRuntime || runtimeKeepaliveCounter > 0; + var _proc_exit = (code) => { + EXITSTATUS = code; + if (!keepRuntimeAlive()) { + Module['onExit']?.(code); + ABORT = true; + } + quit_(code, new ExitStatus(code)); + }; + _proc_exit.sig = 'vi'; + var exitJS = (status, implicit) => { + EXITSTATUS = status; + if (!keepRuntimeAlive()) { + exitRuntime(); + } + _proc_exit(status); + }; + var _exit = exitJS; + _exit.sig = 'vi'; + var maybeExit = () => { + if (runtimeExited) { + return; + } + if (!keepRuntimeAlive()) { + try { + _exit(EXITSTATUS); + } catch (e) { + handleException(e); + } + } + }; + var callUserCallback = (func) => { + if (runtimeExited || ABORT) { + return; + } + try { + func(); + maybeExit(); + } catch (e) { + handleException(e); + } + }; + var _emscripten_get_now = () => performance.now(); + _emscripten_get_now.sig = 'd'; + var __setitimer_js = (which, timeout_ms) => { + if (timers[which]) { + clearTimeout(timers[which].id); + delete timers[which]; + } + if (!timeout_ms) return 0; + var id = setTimeout(() => { + delete timers[which]; + callUserCallback(() => + __emscripten_timeout(which, _emscripten_get_now()) + ); + }, timeout_ms); + timers[which] = { id, timeout_ms }; + return 0; + }; + __setitimer_js.sig = 'iid'; + var __tzset_js = (timezone, daylight, std_name, dst_name) => { + var currentYear = new Date().getFullYear(); + var winter = new Date(currentYear, 0, 1); + var summer = new Date(currentYear, 6, 1); + var winterOffset = winter.getTimezoneOffset(); + var summerOffset = summer.getTimezoneOffset(); + var stdTimezoneOffset = Math.max(winterOffset, summerOffset); + HEAPU32[timezone >> 2] = stdTimezoneOffset * 60; + HEAP32[daylight >> 2] = Number(winterOffset != summerOffset); + var extractZone = (timezoneOffset) => { + var sign = timezoneOffset >= 0 ? '-' : '+'; + var absOffset = Math.abs(timezoneOffset); + var hours = String(Math.floor(absOffset / 60)).padStart(2, '0'); + var minutes = String(absOffset % 60).padStart(2, '0'); + return `UTC${sign}${hours}${minutes}`; + }; + var winterName = extractZone(winterOffset); + var summerName = extractZone(summerOffset); + if (summerOffset < winterOffset) { + stringToUTF8(winterName, std_name, 17); + stringToUTF8(summerName, dst_name, 17); + } else { + stringToUTF8(winterName, dst_name, 17); + stringToUTF8(summerName, std_name, 17); + } + }; + __tzset_js.sig = 'vpppp'; + var _emscripten_date_now = () => Date.now(); + _emscripten_date_now.sig = 'd'; + var nowIsMonotonic = 1; + var checkWasiClock = (clock_id) => clock_id >= 0 && clock_id <= 3; + function _clock_time_get(clk_id, ignored_precision, ptime) { + ignored_precision = bigintToI53Checked(ignored_precision); + if (!checkWasiClock(clk_id)) { + return 28; + } + var now; + if (clk_id === 0) { + now = _emscripten_date_now(); + } else if (nowIsMonotonic) { + now = _emscripten_get_now(); + } else { + return 52; + } + var nsec = Math.round(now * 1e3 * 1e3); + HEAP64[ptime >> 3] = BigInt(nsec); + return 0; + } + _clock_time_get.sig = 'iijp'; + var getHeapMax = () => 2147483648; + var _emscripten_get_heap_max = () => getHeapMax(); + _emscripten_get_heap_max.sig = 'p'; + var growMemory = (size) => { + var oldHeapSize = wasmMemory.buffer.byteLength; + var pages = ((size - oldHeapSize + 65535) / 65536) | 0; + try { + wasmMemory.grow(pages); + updateMemoryViews(); + return 1; + } catch (e) {} + }; + var _emscripten_resize_heap = (requestedSize) => { + var oldSize = HEAPU8.length; + requestedSize >>>= 0; + var maxHeapSize = getHeapMax(); + if (requestedSize > maxHeapSize) { + return false; + } + for (var cutDown = 1; cutDown <= 4; cutDown *= 2) { + var overGrownHeapSize = oldSize * (1 + 0.2 / cutDown); + overGrownHeapSize = Math.min( + overGrownHeapSize, + requestedSize + 100663296 + ); + var newSize = Math.min( + maxHeapSize, + alignMemory(Math.max(requestedSize, overGrownHeapSize), 65536) + ); + var replacement = growMemory(newSize); + if (replacement) { + return true; + } + } + return false; + }; + _emscripten_resize_heap.sig = 'ip'; + var runtimeKeepalivePush = () => { + runtimeKeepaliveCounter += 1; + }; + runtimeKeepalivePush.sig = 'v'; + var runtimeKeepalivePop = () => { + runtimeKeepaliveCounter -= 1; + }; + runtimeKeepalivePop.sig = 'v'; + var safeSetTimeout = (func, timeout) => { + runtimeKeepalivePush(); + return setTimeout(() => { + runtimeKeepalivePop(); + callUserCallback(func); + }, timeout); + }; + var _emscripten_sleep = (ms) => + Asyncify.handleSleep((wakeUp) => safeSetTimeout(wakeUp, ms)); + _emscripten_sleep.sig = 'vi'; + _emscripten_sleep.isAsync = true; + var ENV = PHPLoader.ENV || {}; + var getExecutableName = () => thisProgram || './this.program'; + var getEnvStrings = () => { + if (!getEnvStrings.strings) { + var lang = + ( + (typeof navigator == 'object' && navigator.language) || + 'C' + ).replace('-', '_') + '.UTF-8'; + var env = { + USER: 'web_user', + LOGNAME: 'web_user', + PATH: '/', + PWD: '/', + HOME: '/home/web_user', + LANG: lang, + _: getExecutableName(), + }; + for (var x in ENV) { + if (ENV[x] === undefined) delete env[x]; + else env[x] = ENV[x]; + } + var strings = []; + for (var x in env) { + strings.push(`${x}=${env[x]}`); + } + getEnvStrings.strings = strings; + } + return getEnvStrings.strings; + }; + var _environ_get = (__environ, environ_buf) => { + var bufSize = 0; + var envp = 0; + for (var string of getEnvStrings()) { + var ptr = environ_buf + bufSize; + HEAPU32[(__environ + envp) >> 2] = ptr; + bufSize += stringToUTF8(string, ptr, Infinity) + 1; + envp += 4; + } + return 0; + }; + _environ_get.sig = 'ipp'; + var _environ_sizes_get = (penviron_count, penviron_buf_size) => { + var strings = getEnvStrings(); + HEAPU32[penviron_count >> 2] = strings.length; + var bufSize = 0; + for (var string of strings) { + bufSize += lengthBytesUTF8(string) + 1; + } + HEAPU32[penviron_buf_size >> 2] = bufSize; + return 0; + }; + _environ_sizes_get.sig = 'ipp'; + function _fd_close(fd) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + FS.close(stream); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + _fd_close.sig = 'ii'; + function _fd_fdstat_get(fd, pbuf) { + try { + var rightsBase = 0; + var rightsInheriting = 0; + var flags = 0; + { + var stream = SYSCALLS.getStreamFromFD(fd); + var type = stream.tty + ? 2 + : FS.isDir(stream.mode) + ? 3 + : FS.isLink(stream.mode) + ? 7 + : 4; + } + HEAP8[pbuf] = type; + HEAP16[(pbuf + 2) >> 1] = flags; + HEAP64[(pbuf + 8) >> 3] = BigInt(rightsBase); + HEAP64[(pbuf + 16) >> 3] = BigInt(rightsInheriting); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + _fd_fdstat_get.sig = 'iip'; + var doReadv = (stream, iov, iovcnt, offset) => { + var ret = 0; + for (var i = 0; i < iovcnt; i++) { + var ptr = HEAPU32[iov >> 2]; + var len = HEAPU32[(iov + 4) >> 2]; + iov += 8; + var curr = FS.read(stream, HEAP8, ptr, len, offset); + if (curr < 0) return -1; + ret += curr; + if (curr < len) break; + if (typeof offset != 'undefined') { + offset += curr; + } + } + return ret; + }; + function _fd_read(fd, iov, iovcnt, pnum) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var num = doReadv(stream, iov, iovcnt); + HEAPU32[pnum >> 2] = num; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + _fd_read.sig = 'iippp'; + function _fd_seek(fd, offset, whence, newOffset) { + offset = bigintToI53Checked(offset); + try { + if (isNaN(offset)) return 61; + var stream = SYSCALLS.getStreamFromFD(fd); + FS.llseek(stream, offset, whence); + HEAP64[newOffset >> 3] = BigInt(stream.position); + if (stream.getdents && offset === 0 && whence === 0) + stream.getdents = null; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + _fd_seek.sig = 'iijip'; + var _fd_sync = function (fd) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + return Asyncify.handleSleep((wakeUp) => { + var mount = stream.node.mount; + if (!mount.type.syncfs) { + wakeUp(0); + return; + } + mount.type.syncfs(mount, false, (err) => { + wakeUp(err ? 29 : 0); + }); + }); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + }; + _fd_sync.sig = 'ii'; + _fd_sync.isAsync = true; + var doWritev = (stream, iov, iovcnt, offset) => { + var ret = 0; + for (var i = 0; i < iovcnt; i++) { + var ptr = HEAPU32[iov >> 2]; + var len = HEAPU32[(iov + 4) >> 2]; + iov += 8; + var curr = FS.write(stream, HEAP8, ptr, len, offset); + if (curr < 0) return -1; + ret += curr; + if (curr < len) { + break; + } + if (typeof offset != 'undefined') { + offset += curr; + } + } + return ret; + }; + function _fd_write(fd, iov, iovcnt, pnum) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var num = doWritev(stream, iov, iovcnt); + HEAPU32[pnum >> 2] = num; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + _fd_write.sig = 'iippp'; + var _getaddrinfo = (node, service, hint, out) => { + var addr = 0; + var port = 0; + var flags = 0; + var family = 0; + var type = 0; + var proto = 0; + var ai; + function allocaddrinfo(family, type, proto, canon, addr, port) { + var sa, salen, ai; + var errno; + salen = family === 10 ? 28 : 16; + addr = family === 10 ? inetNtop6(addr) : inetNtop4(addr); + sa = _malloc(salen); + errno = writeSockaddr(sa, family, addr, port); + ai = _malloc(32); + HEAP32[(ai + 4) >> 2] = family; + HEAP32[(ai + 8) >> 2] = type; + HEAP32[(ai + 12) >> 2] = proto; + HEAPU32[(ai + 24) >> 2] = canon; + HEAPU32[(ai + 20) >> 2] = sa; + if (family === 10) { + HEAP32[(ai + 16) >> 2] = 28; + } else { + HEAP32[(ai + 16) >> 2] = 16; + } + HEAP32[(ai + 28) >> 2] = 0; + return ai; + } + if (hint) { + flags = HEAP32[hint >> 2]; + family = HEAP32[(hint + 4) >> 2]; + type = HEAP32[(hint + 8) >> 2]; + proto = HEAP32[(hint + 12) >> 2]; + } + if (type && !proto) { + proto = type === 2 ? 17 : 6; + } + if (!type && proto) { + type = proto === 17 ? 2 : 1; + } + if (proto === 0) { + proto = 6; + } + if (type === 0) { + type = 1; + } + if (!node && !service) { + return -2; + } + if (flags & ~(1 | 2 | 4 | 1024 | 8 | 16 | 32)) { + return -1; + } + if (hint !== 0 && HEAP32[hint >> 2] & 2 && !node) { + return -1; + } + if (flags & 32) { + return -2; + } + if (type !== 0 && type !== 1 && type !== 2) { + return -7; + } + if (family !== 0 && family !== 2 && family !== 10) { + return -6; + } + if (service) { + service = UTF8ToString(service); + port = parseInt(service, 10); + if (isNaN(port)) { + if (flags & 1024) { + return -2; + } + return -8; + } + } + if (!node) { + if (family === 0) { + family = 2; + } + if ((flags & 1) === 0) { + if (family === 2) { + addr = _htonl(2130706433); + } else { + addr = [0, 0, 0, _htonl(1)]; + } + } + ai = allocaddrinfo(family, type, proto, null, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + } + node = UTF8ToString(node); + addr = inetPton4(node); + if (addr !== null) { + if (family === 0 || family === 2) { + family = 2; + } else if (family === 10 && flags & 8) { + addr = [0, 0, _htonl(65535), addr]; + family = 10; + } else { + return -2; + } + } else { + addr = inetPton6(node); + if (addr !== null) { + if (family === 0 || family === 10) { + family = 10; + } else { + return -2; + } + } + } + if (addr != null) { + ai = allocaddrinfo(family, type, proto, node, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + } + if (flags & 4) { + return -2; + } + node = DNS.lookup_name(node); + addr = inetPton4(node); + if (family === 0) { + family = 2; + } else if (family === 10) { + addr = [0, 0, _htonl(65535), addr]; + } + ai = allocaddrinfo(family, type, proto, null, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + }; + _getaddrinfo.sig = 'ipppp'; + var _getnameinfo = (sa, salen, node, nodelen, serv, servlen, flags) => { + var info = readSockaddr(sa, salen); + if (info.errno) { + return -6; + } + var port = info.port; + var addr = info.addr; + var overflowed = false; + if (node && nodelen) { + var lookup; + if (flags & 1 || !(lookup = DNS.lookup_addr(addr))) { + if (flags & 8) { + return -2; + } + } else { + addr = lookup; + } + var numBytesWrittenExclNull = stringToUTF8(addr, node, nodelen); + if (numBytesWrittenExclNull + 1 >= nodelen) { + overflowed = true; + } + } + if (serv && servlen) { + port = '' + port; + var numBytesWrittenExclNull = stringToUTF8(port, serv, servlen); + if (numBytesWrittenExclNull + 1 >= servlen) { + overflowed = true; + } + } + if (overflowed) { + return -12; + } + return 0; + }; + _getnameinfo.sig = 'ipipipii'; + var Protocols = { list: [], map: {} }; + var stringToAscii = (str, buffer) => { + for (var i = 0; i < str.length; ++i) { + HEAP8[buffer++] = str.charCodeAt(i); + } + HEAP8[buffer] = 0; + }; + var _setprotoent = (stayopen) => { + function allocprotoent(name, proto, aliases) { + var nameBuf = _malloc(name.length + 1); + stringToAscii(name, nameBuf); + var j = 0; + var length = aliases.length; + var aliasListBuf = _malloc((length + 1) * 4); + for (var i = 0; i < length; i++, j += 4) { + var alias = aliases[i]; + var aliasBuf = _malloc(alias.length + 1); + stringToAscii(alias, aliasBuf); + HEAPU32[(aliasListBuf + j) >> 2] = aliasBuf; + } + HEAPU32[(aliasListBuf + j) >> 2] = 0; + var pe = _malloc(12); + HEAPU32[pe >> 2] = nameBuf; + HEAPU32[(pe + 4) >> 2] = aliasListBuf; + HEAP32[(pe + 8) >> 2] = proto; + return pe; + } + var list = Protocols.list; + var map = Protocols.map; + if (list.length === 0) { + var entry = allocprotoent('tcp', 6, ['TCP']); + list.push(entry); + map['tcp'] = map['6'] = entry; + entry = allocprotoent('udp', 17, ['UDP']); + list.push(entry); + map['udp'] = map['17'] = entry; + } + _setprotoent.index = 0; + }; + _setprotoent.sig = 'vi'; + var _getprotobyname = (name) => { + name = UTF8ToString(name); + _setprotoent(true); + var result = Protocols.map[name]; + return result; + }; + _getprotobyname.sig = 'pp'; + var _getprotobynumber = (number) => { + _setprotoent(true); + var result = Protocols.map[number]; + return result; + }; + _getprotobynumber.sig = 'pi'; + function _js_open_process( + command, + argsPtr, + argsLength, + descriptorsPtr, + descriptorsLength, + cwdPtr, + cwdLength, + envPtr, + envLength + ) { + if (!command) { + ___errno_location(ERRNO_CODES.EINVAL); + return -1; + } + const cmdstr = UTF8ToString(command); + if (!cmdstr.length) { + ___errno_location(ERRNO_CODES.EINVAL); + return -1; + } + let argsArray = []; + if (argsLength) { + for (var i = 0; i < argsLength; i++) { + const charPointer = argsPtr + i * 4; + argsArray.push(UTF8ToString(HEAPU32[charPointer >> 2])); + } + } + const cwdstr = cwdPtr ? UTF8ToString(cwdPtr) : FS.cwd(); + let envObject = null; + if (envLength) { + envObject = {}; + for (var i = 0; i < envLength; i++) { + const envPointer = envPtr + i * 4; + const envEntry = UTF8ToString(HEAPU32[envPointer >> 2]); + const splitAt = envEntry.indexOf('='); + if (splitAt === -1) { + continue; + } + const key = envEntry.substring(0, splitAt); + const value = envEntry.substring(splitAt + 1); + envObject[key] = value; + } + } + var std = {}; + for (var i = 0; i < descriptorsLength; i++) { + const descriptorPtr = HEAPU32[(descriptorsPtr + i * 4) >> 2]; + std[HEAPU32[descriptorPtr >> 2]] = { + child: HEAPU32[(descriptorPtr + 4) >> 2], + parent: HEAPU32[(descriptorPtr + 8) >> 2], + }; + if (i === 0) { + HEAPU32[(descriptorPtr + 8) >> 2] = + std[HEAPU32[descriptorPtr >> 2]].parent; + HEAPU32[(descriptorPtr + 4) >> 2] = + std[HEAPU32[descriptorPtr >> 2]].child; + } + } + return Asyncify.handleAsync(async () => { + let cp; + try { + const options = {}; + if (cwdstr !== null) { + options.cwd = cwdstr; + } + if (envObject !== null) { + options.env = envObject; + } + cp = PHPWASM.spawnProcess(cmdstr, argsArray, options); + if (cp instanceof Promise) { + cp = await cp; + } + } catch (e) { + if (e.code === 'SPAWN_UNSUPPORTED') { + ___errno_location(ERRNO_CODES.ENOSYS); + return -1; + } + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) + throw e; + ___errno_location(e.code); + return -1; + } + const ProcInfo = { pid: cp.pid, exited: false }; + PHPWASM.processTable[ProcInfo.pid] = ProcInfo; + const stdinParentFd = std[0]?.parent, + stdinChildFd = std[0]?.child, + stdoutChildFd = std[1]?.child, + stdoutParentFd = std[1]?.parent, + stderrChildFd = std[2]?.child, + stderrParentFd = std[2]?.parent; + const detachPipeDataListeners = []; + cp.on('exit', function (code) { + for (const detach of detachPipeDataListeners) { + detach(); + } + for (const fd of [stdoutChildFd, stderrChildFd, stdinChildFd]) { + if (FS.streams[fd] && !FS.isClosed(FS.streams[fd])) { + FS.close(FS.streams[fd]); + } + } + ProcInfo.exitCode = code; + ProcInfo.exited = true; + }); + if (stdoutChildFd) { + const stdoutStream = SYSCALLS.getStreamFromFD(stdoutChildFd); + let stdoutAt = 0; + const onStdoutData = function (data) { + try { + stdoutStream.stream_ops.write( + stdoutStream, + data, + 0, + data.length, + stdoutAt + ); + stdoutAt += data.length; + } catch { + cp.stdout.off('data', onStdoutData); + } + }; + cp.stdout.on('data', onStdoutData); + detachPipeDataListeners.push(() => + cp.stdout.off('data', onStdoutData) + ); + } + if (stderrChildFd) { + const stderrStream = SYSCALLS.getStreamFromFD(stderrChildFd); + let stderrAt = 0; + const onStderrData = function (data) { + try { + stderrStream.stream_ops.write( + stderrStream, + data, + 0, + data.length, + stderrAt + ); + stderrAt += data.length; + } catch { + cp.stderr.off('data', onStderrData); + } + }; + cp.stderr.on('data', onStderrData); + detachPipeDataListeners.push(() => + cp.stderr.off('data', onStderrData) + ); + } + try { + await new Promise((resolve, reject) => { + let resolved = false; + cp.on('spawn', () => { + if (resolved) return; + resolved = true; + resolve(); + }); + cp.on('error', (e) => { + if (resolved) return; + resolved = true; + reject(e); + }); + cp.on('exit', function (code) { + if (resolved) return; + resolved = true; + if (code === 0) { + resolve(); + } else { + reject( + new Error(`Process exited with code ${code}`) + ); + } + }); + setTimeout(() => { + if (resolved) return; + resolved = true; + reject(new Error('Process timed out')); + }, 5e3); + }); + } catch (e) { + console.error(e); + return ProcInfo.pid; + } + if (stdinChildFd) { + let stdinStream; + try { + stdinStream = SYSCALLS.getStreamFromFD(stdinChildFd); + } catch (e) { + ___errno_location(ERRNO_CODES.EBADF); + return ProcInfo.pid; + } + if (!stdinStream?.node) { + return ProcInfo.pid; + } + const CHUNK_SIZE = 1024; + const iov = _malloc(16); + const pnum = _malloc(4); + const buffer = _malloc(CHUNK_SIZE); + HEAPU32[iov >> 2] = buffer; + HEAPU32[(iov + 4) >> 2] = CHUNK_SIZE; + function pump() { + try { + while (true) { + if (cp.killed) { + stopPumpingAndCloseStdin(); + return; + } + const result = js_fd_read( + stdinChildFd, + iov, + 1, + pnum, + false + ); + const bytesRead = HEAPU32[pnum >> 2]; + if (result === 0 && bytesRead > 0) { + const wrote = HEAPU8.subarray( + buffer, + buffer + bytesRead + ); + cp.stdin.write(wrote); + } else if (result === 0 && bytesRead === 0) { + stopPumpingAndCloseStdin(); + break; + } else if (result === ERRNO_CODES.EAGAIN) { + break; + } else { + throw new FS.ErrnoError(result); + } + } + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) { + throw e; + } + ___errno_location(e.errno); + stopPumpingAndCloseStdin(); + } + } + function stopPumpingAndCloseStdin() { + clearInterval(interval); + if (!cp.stdin.closed) { + cp.stdin.end(); + } + _wasm_free(buffer); + _wasm_free(iov); + _wasm_free(pnum); + } + const interval = setInterval(pump, 20); + pump(); + } + return ProcInfo.pid; + }); + } + var arraySum = (array, index) => { + var sum = 0; + for (var i = 0; i <= index; sum += array[i++]) {} + return sum; + }; + var MONTH_DAYS_LEAP = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + var MONTH_DAYS_REGULAR = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + var addDays = (date, days) => { + var newDate = new Date(date.getTime()); + while (days > 0) { + var leap = isLeapYear(newDate.getFullYear()); + var currentMonth = newDate.getMonth(); + var daysInCurrentMonth = ( + leap ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR + )[currentMonth]; + if (days > daysInCurrentMonth - newDate.getDate()) { + days -= daysInCurrentMonth - newDate.getDate() + 1; + newDate.setDate(1); + if (currentMonth < 11) { + newDate.setMonth(currentMonth + 1); + } else { + newDate.setMonth(0); + newDate.setFullYear(newDate.getFullYear() + 1); + } + } else { + newDate.setDate(newDate.getDate() + days); + return newDate; + } + } + return newDate; + }; + var _strptime = (buf, format, tm) => { + var pattern = UTF8ToString(format); + var SPECIAL_CHARS = '\\!@#$^&*()+=-[]/{}|:<>?,.'; + for (var i = 0, ii = SPECIAL_CHARS.length; i < ii; ++i) { + pattern = pattern.replace( + new RegExp('\\' + SPECIAL_CHARS[i], 'g'), + '\\' + SPECIAL_CHARS[i] + ); + } + var EQUIVALENT_MATCHERS = { + A: '%a', + B: '%b', + c: '%a %b %d %H:%M:%S %Y', + D: '%m\\/%d\\/%y', + e: '%d', + F: '%Y-%m-%d', + h: '%b', + R: '%H\\:%M', + r: '%I\\:%M\\:%S\\s%p', + T: '%H\\:%M\\:%S', + x: '%m\\/%d\\/(?:%y|%Y)', + X: '%H\\:%M\\:%S', + }; + var DATE_PATTERNS = { + a: '(?:Sun(?:day)?)|(?:Mon(?:day)?)|(?:Tue(?:sday)?)|(?:Wed(?:nesday)?)|(?:Thu(?:rsday)?)|(?:Fri(?:day)?)|(?:Sat(?:urday)?)', + b: '(?:Jan(?:uary)?)|(?:Feb(?:ruary)?)|(?:Mar(?:ch)?)|(?:Apr(?:il)?)|May|(?:Jun(?:e)?)|(?:Jul(?:y)?)|(?:Aug(?:ust)?)|(?:Sep(?:tember)?)|(?:Oct(?:ober)?)|(?:Nov(?:ember)?)|(?:Dec(?:ember)?)', + C: '\\d\\d', + d: '0[1-9]|[1-9](?!\\d)|1\\d|2\\d|30|31', + H: '\\d(?!\\d)|[0,1]\\d|20|21|22|23', + I: '\\d(?!\\d)|0\\d|10|11|12', + j: '00[1-9]|0?[1-9](?!\\d)|0?[1-9]\\d(?!\\d)|[1,2]\\d\\d|3[0-6]\\d', + m: '0[1-9]|[1-9](?!\\d)|10|11|12', + M: '0\\d|\\d(?!\\d)|[1-5]\\d', + n: ' ', + p: 'AM|am|PM|pm|A\\.M\\.|a\\.m\\.|P\\.M\\.|p\\.m\\.', + S: '0\\d|\\d(?!\\d)|[1-5]\\d|60', + U: '0\\d|\\d(?!\\d)|[1-4]\\d|50|51|52|53', + W: '0\\d|\\d(?!\\d)|[1-4]\\d|50|51|52|53', + w: '[0-6]', + y: '\\d\\d', + Y: '\\d\\d\\d\\d', + t: ' ', + z: 'Z|(?:[\\+\\-]\\d\\d:?(?:\\d\\d)?)', + }; + var MONTH_NUMBERS = { + JAN: 0, + FEB: 1, + MAR: 2, + APR: 3, + MAY: 4, + JUN: 5, + JUL: 6, + AUG: 7, + SEP: 8, + OCT: 9, + NOV: 10, + DEC: 11, + }; + var DAY_NUMBERS_SUN_FIRST = { + SUN: 0, + MON: 1, + TUE: 2, + WED: 3, + THU: 4, + FRI: 5, + SAT: 6, + }; + var DAY_NUMBERS_MON_FIRST = { + MON: 0, + TUE: 1, + WED: 2, + THU: 3, + FRI: 4, + SAT: 5, + SUN: 6, + }; + var capture = []; + var pattern_out = pattern + .replace(/%(.)/g, (m, c) => EQUIVALENT_MATCHERS[c] || m) + .replace(/%(.)/g, (_, c) => { + let pat = DATE_PATTERNS[c]; + if (pat) { + capture.push(c); + return `(${pat})`; + } else { + return c; + } + }) + .replace(/\s+/g, '\\s*'); + var matches = new RegExp('^' + pattern_out, 'i').exec( + UTF8ToString(buf) + ); + function initDate() { + function fixup(value, min, max) { + return typeof value != 'number' || isNaN(value) + ? min + : value >= min + ? value <= max + ? value + : max + : min; + } + return { + year: fixup(HEAP32[(tm + 20) >> 2] + 1900, 1970, 9999), + month: fixup(HEAP32[(tm + 16) >> 2], 0, 11), + day: fixup(HEAP32[(tm + 12) >> 2], 1, 31), + hour: fixup(HEAP32[(tm + 8) >> 2], 0, 23), + min: fixup(HEAP32[(tm + 4) >> 2], 0, 59), + sec: fixup(HEAP32[tm >> 2], 0, 59), + gmtoff: 0, + }; + } + if (matches) { + var date = initDate(); + var value; + var getMatch = (symbol) => { + var pos = capture.indexOf(symbol); + if (pos >= 0) { + return matches[pos + 1]; + } + return; + }; + if ((value = getMatch('S'))) { + date.sec = Number(value); + } + if ((value = getMatch('M'))) { + date.min = Number(value); + } + if ((value = getMatch('H'))) { + date.hour = Number(value); + } else if ((value = getMatch('I'))) { + var hour = Number(value); + if ((value = getMatch('p'))) { + hour += value.toUpperCase()[0] === 'P' ? 12 : 0; + } + date.hour = hour; + } + if ((value = getMatch('Y'))) { + date.year = Number(value); + } else if ((value = getMatch('y'))) { + var year = Number(value); + if ((value = getMatch('C'))) { + year += Number(value) * 100; + } else { + year += year < 69 ? 2e3 : 1900; + } + date.year = year; + } + if ((value = getMatch('m'))) { + date.month = Number(value) - 1; + } else if ((value = getMatch('b'))) { + date.month = + MONTH_NUMBERS[value.substring(0, 3).toUpperCase()] || 0; + } + if ((value = getMatch('d'))) { + date.day = Number(value); + } else if ((value = getMatch('j'))) { + var day = Number(value); + var leapYear = isLeapYear(date.year); + for (var month = 0; month < 12; ++month) { + var daysUntilMonth = arraySum( + leapYear ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR, + month - 1 + ); + if ( + day <= + daysUntilMonth + + (leapYear ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR)[ + month + ] + ) { + date.day = day - daysUntilMonth; + } + } + } else if ((value = getMatch('a'))) { + var weekDay = value.substring(0, 3).toUpperCase(); + if ((value = getMatch('U'))) { + var weekDayNumber = DAY_NUMBERS_SUN_FIRST[weekDay]; + var weekNumber = Number(value); + var janFirst = new Date(date.year, 0, 1); + var endDate; + if (janFirst.getDay() === 0) { + endDate = addDays( + janFirst, + weekDayNumber + 7 * (weekNumber - 1) + ); + } else { + endDate = addDays( + janFirst, + 7 - + janFirst.getDay() + + weekDayNumber + + 7 * (weekNumber - 1) + ); + } + date.day = endDate.getDate(); + date.month = endDate.getMonth(); + } else if ((value = getMatch('W'))) { + var weekDayNumber = DAY_NUMBERS_MON_FIRST[weekDay]; + var weekNumber = Number(value); + var janFirst = new Date(date.year, 0, 1); + var endDate; + if (janFirst.getDay() === 1) { + endDate = addDays( + janFirst, + weekDayNumber + 7 * (weekNumber - 1) + ); + } else { + endDate = addDays( + janFirst, + 7 - + janFirst.getDay() + + 1 + + weekDayNumber + + 7 * (weekNumber - 1) + ); + } + date.day = endDate.getDate(); + date.month = endDate.getMonth(); + } + } + if ((value = getMatch('z'))) { + if (value.toLowerCase() === 'z') { + date.gmtoff = 0; + } else { + var match = value.match(/^((?:\-|\+)\d\d):?(\d\d)?/); + date.gmtoff = match[1] * 3600; + if (match[2]) { + date.gmtoff += + date.gmtoff > 0 ? match[2] * 60 : -match[2] * 60; + } + } + } + var fullDate = new Date( + date.year, + date.month, + date.day, + date.hour, + date.min, + date.sec, + 0 + ); + HEAP32[tm >> 2] = fullDate.getSeconds(); + HEAP32[(tm + 4) >> 2] = fullDate.getMinutes(); + HEAP32[(tm + 8) >> 2] = fullDate.getHours(); + HEAP32[(tm + 12) >> 2] = fullDate.getDate(); + HEAP32[(tm + 16) >> 2] = fullDate.getMonth(); + HEAP32[(tm + 20) >> 2] = fullDate.getFullYear() - 1900; + HEAP32[(tm + 24) >> 2] = fullDate.getDay(); + HEAP32[(tm + 28) >> 2] = + arraySum( + isLeapYear(fullDate.getFullYear()) + ? MONTH_DAYS_LEAP + : MONTH_DAYS_REGULAR, + fullDate.getMonth() - 1 + ) + + fullDate.getDate() - + 1; + HEAP32[(tm + 32) >> 2] = 0; + HEAP32[(tm + 36) >> 2] = date.gmtoff; + return buf + lengthBytesUTF8(matches[0]); + } + return 0; + }; + _strptime.sig = 'pppp'; + function _wasm_setsockopt( + socketd, + level, + optionName, + optionValuePtr, + optionLen + ) { + const optionValue = HEAPU8[optionValuePtr]; + const SOL_SOCKET = 1; + const SO_KEEPALIVE = 9; + const SO_RCVTIMEO = 66; + const SO_SNDTIMEO = 67; + const IPPROTO_TCP = 6; + const TCP_NODELAY = 1; + const isForwardable = + (level === SOL_SOCKET && optionName === SO_KEEPALIVE) || + (level === IPPROTO_TCP && optionName === TCP_NODELAY); + const isIgnorable = + level === SOL_SOCKET && + (optionName === SO_RCVTIMEO || optionName === SO_SNDTIMEO); + if (!isForwardable && !isIgnorable) { + console.warn( + `Unsupported socket option: ${level}, ${optionName}, ${optionValue}` + ); + return -1; + } + if (isIgnorable) { + return 0; + } + const ws = PHPWASM.getAllWebSockets(socketd)[0]; + if (!ws) { + return -1; + } + ws.setSocketOpt(level, optionName, optionValuePtr); + return 0; + } + var runAndAbortIfError = (func) => { + try { + return func(); + } catch (e) { + abort(e); + } + }; + var Asyncify = { + instrumentWasmImports(imports) { + var importPattern = + /^(invoke_i|invoke_ii|invoke_iii|invoke_iiii|invoke_iiiii|invoke_iiiiii|invoke_iiiiiii|invoke_iiiiiiii|invoke_iiiiiiiii|invoke_iiiiiiiiii|invoke_v|invoke_vi|invoke_vii|invoke_viidii|invoke_viii|invoke_viiii|invoke_viiiii|invoke_viiiiii|invoke_viiiiiii|invoke_viiiiiiiii|invoke_i|invoke_ii|invoke_iii|invoke_iiii|invoke_iiiii|invoke_iiiiii|invoke_iiiiiii|invoke_iiiiiiii|invoke_iiiiiiiiii|invoke_iij|invoke_iiji|invoke_iiij|invoke_iijii|invoke_iijiji|invoke_jii|invoke_jiii|invoke_viijii|invoke_vji|js_open_process|_js_open_process|_asyncjs__js_open_process|js_popen_to_file|_js_popen_to_file|_asyncjs__js_popen_to_file|__syscall_fcntl64|___syscall_fcntl64|_asyncjs___syscall_fcntl64|js_release_file_locks|_js_release_file_locks|_async_js_release_file_locks|js_flock|_js_flock|_async_js_flock|js_fd_read|_js_fd_read|fd_close|_fd_close|_asyncjs__fd_close|close|_close|js_module_onMessage|zend_hash_str_find|_js_module_onMessage|_asyncjs__js_module_onMessage|js_waitpid|_js_waitpid|_asyncjs__js_waitpid|wasm_poll_socket|_wasm_poll_socket|_asyncjs__wasm_poll_socket|_wasm_shutdown|_asyncjs__wasm_shutdown|recv|_recv|setsockopt|_setsockopt|wasm_connect|_wasm_connect|__asyncjs__.*)$/; + for (let [x, original] of Object.entries(imports)) { + if (typeof original == 'function') { + let isAsyncifyImport = + original.isAsync || importPattern.test(x); + } + } + }, + instrumentFunction(original) { + var wrapper = (...args) => { + Asyncify.exportCallStack.push(original); + try { + return original(...args); + } finally { + if (!ABORT) { + var top = Asyncify.exportCallStack.pop(); + Asyncify.maybeStopUnwind(); + } + } + }; + Asyncify.funcWrappers.set(original, wrapper); + wrapper.orig = original; + return wrapper; + }, + instrumentWasmExports(exports) { + var ret = {}; + for (let [x, original] of Object.entries(exports)) { + if (typeof original == 'function') { + var wrapper = Asyncify.instrumentFunction(original); + ret[x] = wrapper; + } else { + ret[x] = original; + } + } + return ret; + }, + State: { Normal: 0, Unwinding: 1, Rewinding: 2, Disabled: 3 }, + state: 0, + StackSize: 4096, + currData: null, + handleSleepReturnValue: 0, + exportCallStack: [], + callstackFuncToId: new Map(), + callStackIdToFunc: new Map(), + funcWrappers: new Map(), + callStackId: 0, + asyncPromiseHandlers: null, + sleepCallbacks: [], + getCallStackId(func) { + if (!Asyncify.callstackFuncToId.has(func)) { + var id = Asyncify.callStackId++; + Asyncify.callstackFuncToId.set(func, id); + Asyncify.callStackIdToFunc.set(id, func); + } + return Asyncify.callstackFuncToId.get(func); + }, + maybeStopUnwind() { + if ( + Asyncify.currData && + Asyncify.state === Asyncify.State.Unwinding && + Asyncify.exportCallStack.length === 0 + ) { + Asyncify.state = Asyncify.State.Normal; + runtimeKeepalivePush(); + runAndAbortIfError(_asyncify_stop_unwind); + if (typeof Fibers != 'undefined') { + Fibers.trampoline(); + } + } + }, + whenDone() { + return new Promise((resolve, reject) => { + Asyncify.asyncPromiseHandlers = { resolve, reject }; + }); + }, + allocateData() { + var ptr = _malloc(12 + Asyncify.StackSize); + Asyncify.setDataHeader(ptr, ptr + 12, Asyncify.StackSize); + Asyncify.setDataRewindFunc(ptr); + return ptr; + }, + setDataHeader(ptr, stack, stackSize) { + HEAPU32[ptr >> 2] = stack; + HEAPU32[(ptr + 4) >> 2] = stack + stackSize; + }, + setDataRewindFunc(ptr) { + var bottomOfCallStack = Asyncify.exportCallStack[0]; + var rewindId = Asyncify.getCallStackId(bottomOfCallStack); + HEAP32[(ptr + 8) >> 2] = rewindId; + }, + getDataRewindFunc(ptr) { + var id = HEAP32[(ptr + 8) >> 2]; + var func = Asyncify.callStackIdToFunc.get(id); + return func; + }, + doRewind(ptr) { + var original = Asyncify.getDataRewindFunc(ptr); + var func = Asyncify.funcWrappers.get(original); + runtimeKeepalivePop(); + return func(); + }, + handleSleep(startAsync) { + if (ABORT) return; + if (Asyncify.state === Asyncify.State.Normal) { + var reachedCallback = false; + var reachedAfterCallback = false; + startAsync((handleSleepReturnValue = 0) => { + if (ABORT) return; + Asyncify.handleSleepReturnValue = handleSleepReturnValue; + reachedCallback = true; + if (!reachedAfterCallback) { + return; + } + Asyncify.state = Asyncify.State.Rewinding; + runAndAbortIfError(() => + _asyncify_start_rewind(Asyncify.currData) + ); + if (typeof MainLoop != 'undefined' && MainLoop.func) { + MainLoop.resume(); + } + var asyncWasmReturnValue, + isError = false; + try { + asyncWasmReturnValue = Asyncify.doRewind( + Asyncify.currData + ); + } catch (err) { + asyncWasmReturnValue = err; + isError = true; + } + var handled = false; + if (!Asyncify.currData) { + var asyncPromiseHandlers = + Asyncify.asyncPromiseHandlers; + if (asyncPromiseHandlers) { + Asyncify.asyncPromiseHandlers = null; + (isError + ? asyncPromiseHandlers.reject + : asyncPromiseHandlers.resolve)( + asyncWasmReturnValue + ); + handled = true; + } + } + if (isError && !handled) { + throw asyncWasmReturnValue; + } + }); + reachedAfterCallback = true; + if (!reachedCallback) { + Asyncify.state = Asyncify.State.Unwinding; + if (!Asyncify._cachedData) { + Asyncify._cachedData = Asyncify.allocateData(); + } else { + Asyncify.setDataHeader( + Asyncify._cachedData, + Asyncify._cachedData + 12, + Asyncify.StackSize + ); + Asyncify.setDataRewindFunc(Asyncify._cachedData); + } + Asyncify.currData = Asyncify._cachedData; + if (typeof MainLoop != 'undefined' && MainLoop.func) { + MainLoop.pause(); + } + runAndAbortIfError(() => + _asyncify_start_unwind(Asyncify.currData) + ); + } + } else if (Asyncify.state === Asyncify.State.Rewinding) { + Asyncify.state = Asyncify.State.Normal; + runAndAbortIfError(_asyncify_stop_rewind); + Asyncify.currData = null; + Asyncify.sleepCallbacks.forEach(callUserCallback); + } else { + abort(`invalid state: ${Asyncify.state}`); + } + return Asyncify.handleSleepReturnValue; + }, + handleAsync: (startAsync) => + Asyncify.handleSleep((wakeUp) => { + startAsync().then(wakeUp); + }), + }; + var getCFunc = (ident) => { + var func = Module['_' + ident]; + return func; + }; + var writeArrayToMemory = (array, buffer) => { + HEAP8.set(array, buffer); + }; + var ccall = (ident, returnType, argTypes, args, opts) => { + var toC = { + string: (str) => { + var ret = 0; + if (str !== null && str !== undefined && str !== 0) { + ret = stringToUTF8OnStack(str); + } + return ret; + }, + array: (arr) => { + var ret = stackAlloc(arr.length); + writeArrayToMemory(arr, ret); + return ret; + }, + }; + function convertReturnValue(ret) { + if (returnType === 'string') { + return UTF8ToString(ret); + } + if (returnType === 'boolean') return Boolean(ret); + return ret; + } + var func = getCFunc(ident); + var cArgs = []; + var stack = 0; + if (args) { + for (var i = 0; i < args.length; i++) { + var converter = toC[argTypes[i]]; + if (converter) { + if (stack === 0) stack = stackSave(); + cArgs[i] = converter(args[i]); + } else { + cArgs[i] = args[i]; + } + } + } + var previousAsync = Asyncify.currData; + var ret = func(...cArgs); + function onDone(ret) { + runtimeKeepalivePop(); + if (stack !== 0) stackRestore(stack); + return convertReturnValue(ret); + } + var asyncMode = opts?.async; + runtimeKeepalivePush(); + if (Asyncify.currData != previousAsync) { + return Asyncify.whenDone().then(onDone); + } + ret = onDone(ret); + if (asyncMode) return Promise.resolve(ret); + return ret; + }; + var FS_createPath = (...args) => FS.createPath(...args); + var FS_unlink = (...args) => FS.unlink(...args); + var FS_createLazyFile = (...args) => FS.createLazyFile(...args); + var FS_createDevice = (...args) => FS.createDevice(...args); + registerWasmPlugin(); + FS.createPreloadedFile = FS_createPreloadedFile; + FS.preloadFile = FS_preloadFile; + FS.staticInit(); + PHPWASM.init(); + { + if (Module['preloadPlugins']) preloadPlugins = Module['preloadPlugins']; + if (Module['noExitRuntime']) noExitRuntime = Module['noExitRuntime']; + if (Module['print']) out = Module['print']; + if (Module['printErr']) err = Module['printErr']; + if (Module['dynamicLibraries']) + dynamicLibraries = Module['dynamicLibraries']; + if (Module['wasmBinary']) wasmBinary = Module['wasmBinary']; + if (Module['arguments']) arguments_ = Module['arguments']; + if (Module['thisProgram']) thisProgram = Module['thisProgram']; + if (Module['quit']) quit_ = Module['quit']; + if (Module['preInit']) { + if (typeof Module['preInit'] == 'function') + Module['preInit'] = [Module['preInit']]; + while (Module['preInit'].length > 0) { + Module['preInit'].shift()(); + } + } + } + Module['wasmExports'] = wasmExports; + Module['addRunDependency'] = addRunDependency; + Module['removeRunDependency'] = removeRunDependency; + Module['ccall'] = ccall; + Module['UTF8ToString'] = UTF8ToString; + Module['stringToUTF8'] = stringToUTF8; + Module['lengthBytesUTF8'] = lengthBytesUTF8; + Module['FS_preloadFile'] = FS_preloadFile; + Module['FS_unlink'] = FS_unlink; + Module['FS_createPath'] = FS_createPath; + Module['FS_createDevice'] = FS_createDevice; + Module['FS_createDataFile'] = FS_createDataFile; + Module['FS_createLazyFile'] = FS_createLazyFile; + Module['PROXYFS'] = PROXYFS; + Module['UTF8ToString'] = UTF8ToString; + Module['lengthBytesUTF8'] = lengthBytesUTF8; + Module['stringToUTF8'] = stringToUTF8; + Module['FS'] = FS; + Module['_exit'] = _exit; + Module['_emscripten_sleep'] = _emscripten_sleep; + var ASM_CONSTS = {}; + function js_popen_to_file(command, mode, exitCodePtr) { + const returnCallback = (resolver) => Asyncify.handleSleep(resolver); + if (!command) return 1; + const cmdstr = UTF8ToString(command); + if (!cmdstr.length) return 0; + const modestr = UTF8ToString(mode); + if (!modestr.length) return 0; + if (modestr === 'w') { + console.error('popen($cmd, "w") is not implemented yet'); + } + return returnCallback(async (wakeUp) => { + let cp; + try { + cp = PHPWASM.spawnProcess(cmdstr, []); + if (cp instanceof Promise) { + cp = await cp; + } + } catch (e) { + console.error(e); + if (e.code === 'SPAWN_UNSUPPORTED') { + return 1; + } + throw e; + } + const outByteArrays = []; + cp.stdout.on('data', function (data) { + outByteArrays.push(data); + }); + const outputPath = '/tmp/popen_output'; + cp.on('exit', function (exitCode) { + const outBytes = new Uint8Array( + outByteArrays.reduce((acc, curr) => acc + curr.length, 0) + ); + let offset = 0; + for (const byteArray of outByteArrays) { + outBytes.set(byteArray, offset); + offset += byteArray.length; + } + FS.writeFile(outputPath, outBytes); + HEAPU8[exitCodePtr] = exitCode; + wakeUp(allocateUTF8OnStack(outputPath)); + }); + }); + } + js_popen_to_file.sig = 'iiii'; + function wasm_poll_socket(socketd, events, timeout) { + const returnCallback = (resolver) => Asyncify.handleSleep(resolver); + const POLLIN = 1; + const POLLPRI = 2; + const POLLOUT = 4; + const POLLERR = 8; + const POLLHUP = 16; + const POLLNVAL = 32; + return returnCallback((wakeUp) => { + const polls = []; + const stream = FS.getStream(socketd); + if (FS.isSocket(stream?.node.mode)) { + const sock = getSocketFromFD(socketd); + if (!sock) { + wakeUp(0); + return; + } + const lookingFor = new Set(); + if (events & POLLIN || events & POLLPRI) { + if (sock.server) { + for (const client of sock.pending) { + if ((client.recv_queue || []).length > 0) { + wakeUp(1); + return; + } + } + } else if ((sock.recv_queue || []).length > 0) { + wakeUp(1); + return; + } + } + const webSockets = PHPWASM.getAllWebSockets(sock); + if (!webSockets.length) { + wakeUp(0); + return; + } + for (const ws of webSockets) { + if (events & POLLIN || events & POLLPRI) { + polls.push(PHPWASM.awaitData(ws)); + lookingFor.add('POLLIN'); + } + if (events & POLLOUT) { + polls.push(PHPWASM.awaitConnection(ws)); + lookingFor.add('POLLOUT'); + } + if ( + events & POLLHUP || + events & POLLIN || + events & POLLOUT || + events & POLLERR + ) { + polls.push(PHPWASM.awaitClose(ws)); + lookingFor.add('POLLHUP'); + } + if (events & POLLERR || events & POLLNVAL) { + polls.push(PHPWASM.awaitError(ws)); + lookingFor.add('POLLERR'); + } + } + } else if (stream?.stream_ops?.poll) { + let interrupted = false; + async function poll() { + try { + while (true) { + var mask = POLLNVAL; + mask = SYSCALLS.DEFAULT_POLLMASK; + if (FS.isClosed(stream)) { + return ERRNO_CODES.EBADF; + } + if (stream.stream_ops?.poll) { + mask = stream.stream_ops.poll(stream, -1); + } + mask &= events | POLLERR | POLLHUP; + if (mask) { + return mask; + } + if (interrupted) { + return ERRNO_CODES.ETIMEDOUT; + } + await new Promise((resolve) => + setTimeout(resolve, 10) + ); + } + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) + throw e; + return -e.errno; + } + } + polls.push([ + poll(), + () => { + interrupted = true; + }, + ]); + } else { + setTimeout(function () { + wakeUp(1); + }, timeout); + return; + } + if (polls.length === 0) { + console.warn( + 'Unsupported poll event ' + + events + + ', defaulting to setTimeout().' + ); + setTimeout(function () { + wakeUp(0); + }, timeout); + return; + } + const promises = polls.map(([promise]) => promise); + const clearPolling = () => polls.forEach(([, clear]) => clear()); + let awaken = false; + let timeoutId; + Promise.race(promises).then(function (results) { + if (!awaken) { + awaken = true; + wakeUp(1); + if (timeoutId) { + clearTimeout(timeoutId); + } + clearPolling(); + } + }); + if (timeout !== -1) { + timeoutId = setTimeout(function () { + if (!awaken) { + awaken = true; + wakeUp(0); + clearPolling(); + } + }, timeout); + } + }); + } + wasm_poll_socket.sig = 'iiii'; + function js_fd_read(fd, iov, iovcnt, pnum) { + const returnCallback = (resolver) => Asyncify.handleSleep(resolver); + const pollAsync = arguments[4] === undefined ? true : !!arguments[4]; + if ( + Asyncify?.State?.Normal === undefined || + Asyncify?.state === Asyncify?.State?.Normal + ) { + var stream; + try { + stream = SYSCALLS.getStreamFromFD(fd); + HEAPU32[pnum >> 2] = doReadv(stream, iov, iovcnt); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + throw e; + } + if ( + e.errno !== ERRNO_CODES.EWOULDBLOCK && + e.errno !== ERRNO_CODES.EAGAIN + ) { + return e.errno; + } + const nonBlocking = stream.flags & PHPWASM.O_NONBLOCK; + if (nonBlocking) { + return e.errno; + } + } + } + if (false === pollAsync) { + return ERRNO_CODES.EWOULDBLOCK; + } + return returnCallback(async (wakeUp) => { + var retries = 0; + var interval = 50; + var timeout = 5e3; + var maxRetries = timeout / interval; + while (true) { + var returnCode; + var stream; + let num; + try { + stream = SYSCALLS.getStreamFromFD(fd); + num = doReadv(stream, iov, iovcnt); + returnCode = 0; + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) { + console.error(e); + throw e; + } + returnCode = e.errno; + } + if (returnCode === 0) { + HEAPU32[pnum >> 2] = num; + return wakeUp(0); + } + if ( + ++retries > maxRetries || + !stream || + FS.isClosed(stream) || + returnCode !== ERRNO_CODES.EWOULDBLOCK || + ('pipe' in stream.node && stream.node.pipe.refcnt < 2) + ) { + HEAPU32[pnum >> 2] = num; + return wakeUp(returnCode); + } + await new Promise((resolve) => setTimeout(resolve, interval)); + } + }); + } + js_fd_read.sig = 'iiiii'; + function __asyncjs__js_module_onMessage(data, response_buffer) { + return Asyncify.handleAsync(async () => { + if (Module['onMessage']) { + const dataStr = UTF8ToString(data); + return Module['onMessage'](dataStr) + .then((response) => { + const responseBytes = + typeof response === 'string' + ? new TextEncoder().encode(response) + : response; + const responseSize = responseBytes.byteLength; + const responsePtr = _malloc(responseSize + 1); + HEAPU8.set(responseBytes, responsePtr); + HEAPU8[responsePtr + responseSize] = 0; + HEAPU8[response_buffer] = responsePtr; + HEAPU8[response_buffer + 1] = responsePtr >> 8; + HEAPU8[response_buffer + 2] = responsePtr >> 16; + HEAPU8[response_buffer + 3] = responsePtr >> 24; + return responseSize; + }) + .catch((e) => { + console.error(e); + return -1; + }); + } + }); + } + __asyncjs__js_module_onMessage.sig = 'iii'; + var _calloc, + _malloc, + _free, + ___errno_location, + _wasm_sleep, + _ntohs, + _htons, + _htonl, + _wasm_read, + _fflush, + _php_pollfd_for, + _wasm_php_exec, + ___wrap_usleep, + _wasm_popen, + ___wrap_select, + _wasm_set_sapi_name, + _wasm_set_phpini_path, + _wasm_add_cli_arg, + _run_cli, + _wasm_add_SERVER_entry, + _wasm_add_ENV_entry, + _wasm_set_query_string, + _wasm_set_path_translated, + _wasm_set_skip_shebang, + _wasm_set_request_uri, + _wasm_set_request_method, + _wasm_set_request_host, + _wasm_set_content_type, + _wasm_set_request_body, + _wasm_set_content_length, + _wasm_set_cookies, + _wasm_set_request_port, + _wasm_sapi_request_shutdown, + _wasm_sapi_handle_request, + _php_wasm_init, + _wasm_free, + _wasm_trace, + _initgroups, + ___funcs_on_exit, + ___dl_seterr, + __emscripten_find_dylib, + _emscripten_builtin_memalign, + __emscripten_timeout, + _emscripten_get_sbrk_ptr, + _setThrew, + __emscripten_stack_restore, + __emscripten_stack_alloc, + _emscripten_stack_get_current, + __ZNSt3__211__call_onceERVmPvPFvS2_E, + __ZNSt3__218condition_variable10notify_allEv, + __ZNSt3__25mutex4lockEv, + __ZNSt3__25mutex6unlockEv, + dynCall_viii, + dynCall_vi, + dynCall_ii, + dynCall_iii, + dynCall_iiiii, + dynCall_viiii, + dynCall_vii, + dynCall_viiiii, + dynCall_iiii, + dynCall_iiiiii, + dynCall_v, + dynCall_i, + dynCall_iiiiiiii, + dynCall_iijii, + dynCall_iiiiiii, + dynCall_iiji, + dynCall_iiiiiiiiii, + dynCall_viiiiiiii, + dynCall_viiiiiii, + dynCall_iiiiiiiii, + dynCall_viiiiiiiii, + dynCall_viiiiii, + dynCall_ij, + dynCall_iiiij, + dynCall_vijii, + dynCall_iij, + dynCall_iiiiiij, + dynCall_iiid, + dynCall_iiij, + dynCall_dii, + dynCall_jii, + dynCall_ji, + dynCall_vid, + dynCall_vij, + dynCall_di, + dynCall_iiiiijii, + dynCall_j, + dynCall_jj, + dynCall_jiij, + dynCall_iiiiji, + dynCall_iiiijii, + dynCall_viiji, + dynCall_viijii, + dynCall_iiiiiiiiiii, + dynCall_iiiijji, + dynCall_dd, + dynCall_ddd, + dynCall_jiji, + dynCall_iidiiii, + _asyncify_start_unwind, + _asyncify_stop_unwind, + _asyncify_start_rewind, + _asyncify_stop_rewind, + memory, + __indirect_function_table, + wasmTable, + wasmMemory; + function assignWasmExports(wasmExports) { + _calloc = wasmExports['calloc']; + _malloc = + PHPLoader['malloc'] = + Module['_malloc'] = + wasmExports['malloc']; + _free = wasmExports['free']; + ___errno_location = Module['___errno_location'] = + wasmExports['__errno_location']; + _wasm_sleep = Module['_wasm_sleep'] = wasmExports['wasm_sleep']; + _ntohs = wasmExports['ntohs']; + _htons = wasmExports['htons']; + _htonl = wasmExports['htonl']; + _wasm_read = Module['_wasm_read'] = wasmExports['wasm_read']; + _fflush = wasmExports['fflush']; + _php_pollfd_for = Module['_php_pollfd_for'] = + wasmExports['php_pollfd_for']; + _wasm_php_exec = Module['_wasm_php_exec'] = + wasmExports['wasm_php_exec']; + ___wrap_usleep = Module['___wrap_usleep'] = + wasmExports['__wrap_usleep']; + _wasm_popen = Module['_wasm_popen'] = wasmExports['wasm_popen']; + ___wrap_select = Module['___wrap_select'] = + wasmExports['__wrap_select']; + _wasm_set_sapi_name = Module['_wasm_set_sapi_name'] = + wasmExports['wasm_set_sapi_name']; + _wasm_set_phpini_path = Module['_wasm_set_phpini_path'] = + wasmExports['wasm_set_phpini_path']; + _wasm_add_cli_arg = Module['_wasm_add_cli_arg'] = + wasmExports['wasm_add_cli_arg']; + _run_cli = Module['_run_cli'] = wasmExports['run_cli']; + _wasm_add_SERVER_entry = Module['_wasm_add_SERVER_entry'] = + wasmExports['wasm_add_SERVER_entry']; + _wasm_add_ENV_entry = Module['_wasm_add_ENV_entry'] = + wasmExports['wasm_add_ENV_entry']; + _wasm_set_query_string = Module['_wasm_set_query_string'] = + wasmExports['wasm_set_query_string']; + _wasm_set_path_translated = Module['_wasm_set_path_translated'] = + wasmExports['wasm_set_path_translated']; + _wasm_set_skip_shebang = Module['_wasm_set_skip_shebang'] = + wasmExports['wasm_set_skip_shebang']; + _wasm_set_request_uri = Module['_wasm_set_request_uri'] = + wasmExports['wasm_set_request_uri']; + _wasm_set_request_method = Module['_wasm_set_request_method'] = + wasmExports['wasm_set_request_method']; + _wasm_set_request_host = Module['_wasm_set_request_host'] = + wasmExports['wasm_set_request_host']; + _wasm_set_content_type = Module['_wasm_set_content_type'] = + wasmExports['wasm_set_content_type']; + _wasm_set_request_body = Module['_wasm_set_request_body'] = + wasmExports['wasm_set_request_body']; + _wasm_set_content_length = Module['_wasm_set_content_length'] = + wasmExports['wasm_set_content_length']; + _wasm_set_cookies = Module['_wasm_set_cookies'] = + wasmExports['wasm_set_cookies']; + _wasm_set_request_port = Module['_wasm_set_request_port'] = + wasmExports['wasm_set_request_port']; + _wasm_sapi_request_shutdown = Module['_wasm_sapi_request_shutdown'] = + wasmExports['wasm_sapi_request_shutdown']; + _wasm_sapi_handle_request = Module['_wasm_sapi_handle_request'] = + wasmExports['wasm_sapi_handle_request']; + _php_wasm_init = Module['_php_wasm_init'] = + wasmExports['php_wasm_init']; + _wasm_free = + PHPLoader['free'] = + Module['_wasm_free'] = + wasmExports['wasm_free']; + _wasm_trace = Module['_wasm_trace'] = wasmExports['wasm_trace']; + _initgroups = Module['_initgroups'] = wasmExports['initgroups']; + ___funcs_on_exit = wasmExports['__funcs_on_exit']; + ___dl_seterr = wasmExports['__dl_seterr']; + __emscripten_find_dylib = wasmExports['_emscripten_find_dylib']; + _emscripten_builtin_memalign = + wasmExports['emscripten_builtin_memalign']; + __emscripten_timeout = wasmExports['_emscripten_timeout']; + _emscripten_get_sbrk_ptr = wasmExports['emscripten_get_sbrk_ptr']; + _setThrew = wasmExports['setThrew']; + __emscripten_stack_restore = wasmExports['_emscripten_stack_restore']; + __emscripten_stack_alloc = wasmExports['_emscripten_stack_alloc']; + _emscripten_stack_get_current = + wasmExports['emscripten_stack_get_current']; + __ZNSt3__211__call_onceERVmPvPFvS2_E = Module[ + '__ZNSt3__211__call_onceERVmPvPFvS2_E' + ] = wasmExports['_ZNSt3__211__call_onceERVmPvPFvS2_E']; + __ZNSt3__218condition_variable10notify_allEv = Module[ + '__ZNSt3__218condition_variable10notify_allEv' + ] = wasmExports['_ZNSt3__218condition_variable10notify_allEv']; + __ZNSt3__25mutex4lockEv = Module['__ZNSt3__25mutex4lockEv'] = + wasmExports['_ZNSt3__25mutex4lockEv']; + __ZNSt3__25mutex6unlockEv = Module['__ZNSt3__25mutex6unlockEv'] = + wasmExports['_ZNSt3__25mutex6unlockEv']; + dynCall_viii = dynCalls['viii'] = wasmExports['dynCall_viii']; + dynCall_vi = dynCalls['vi'] = wasmExports['dynCall_vi']; + dynCall_ii = dynCalls['ii'] = wasmExports['dynCall_ii']; + dynCall_iii = dynCalls['iii'] = wasmExports['dynCall_iii']; + dynCall_iiiii = dynCalls['iiiii'] = wasmExports['dynCall_iiiii']; + dynCall_viiii = dynCalls['viiii'] = wasmExports['dynCall_viiii']; + dynCall_vii = dynCalls['vii'] = wasmExports['dynCall_vii']; + dynCall_viiiii = dynCalls['viiiii'] = wasmExports['dynCall_viiiii']; + dynCall_iiii = dynCalls['iiii'] = wasmExports['dynCall_iiii']; + dynCall_iiiiii = dynCalls['iiiiii'] = wasmExports['dynCall_iiiiii']; + dynCall_v = dynCalls['v'] = wasmExports['dynCall_v']; + dynCall_i = dynCalls['i'] = wasmExports['dynCall_i']; + dynCall_iiiiiiii = dynCalls['iiiiiiii'] = + wasmExports['dynCall_iiiiiiii']; + dynCall_iijii = dynCalls['iijii'] = wasmExports['dynCall_iijii']; + dynCall_iiiiiii = dynCalls['iiiiiii'] = wasmExports['dynCall_iiiiiii']; + dynCall_iiji = dynCalls['iiji'] = wasmExports['dynCall_iiji']; + dynCall_iiiiiiiiii = dynCalls['iiiiiiiiii'] = + wasmExports['dynCall_iiiiiiiiii']; + dynCall_viiiiiiii = dynCalls['viiiiiiii'] = + wasmExports['dynCall_viiiiiiii']; + dynCall_viiiiiii = dynCalls['viiiiiii'] = + wasmExports['dynCall_viiiiiii']; + dynCall_iiiiiiiii = dynCalls['iiiiiiiii'] = + wasmExports['dynCall_iiiiiiiii']; + dynCall_viiiiiiiii = dynCalls['viiiiiiiii'] = + wasmExports['dynCall_viiiiiiiii']; + dynCall_viiiiii = dynCalls['viiiiii'] = wasmExports['dynCall_viiiiii']; + dynCall_ij = dynCalls['ij'] = wasmExports['dynCall_ij']; + dynCall_iiiij = dynCalls['iiiij'] = wasmExports['dynCall_iiiij']; + dynCall_vijii = dynCalls['vijii'] = wasmExports['dynCall_vijii']; + dynCall_iij = dynCalls['iij'] = wasmExports['dynCall_iij']; + dynCall_iiiiiij = dynCalls['iiiiiij'] = wasmExports['dynCall_iiiiiij']; + dynCall_iiid = dynCalls['iiid'] = wasmExports['dynCall_iiid']; + dynCall_iiij = dynCalls['iiij'] = wasmExports['dynCall_iiij']; + dynCall_dii = dynCalls['dii'] = wasmExports['dynCall_dii']; + dynCall_jii = dynCalls['jii'] = wasmExports['dynCall_jii']; + dynCall_ji = dynCalls['ji'] = wasmExports['dynCall_ji']; + dynCall_vid = dynCalls['vid'] = wasmExports['dynCall_vid']; + dynCall_vij = dynCalls['vij'] = wasmExports['dynCall_vij']; + dynCall_di = dynCalls['di'] = wasmExports['dynCall_di']; + dynCall_iiiiijii = dynCalls['iiiiijii'] = + wasmExports['dynCall_iiiiijii']; + dynCall_j = dynCalls['j'] = wasmExports['dynCall_j']; + dynCall_jj = dynCalls['jj'] = wasmExports['dynCall_jj']; + dynCall_jiij = dynCalls['jiij'] = wasmExports['dynCall_jiij']; + dynCall_iiiiji = dynCalls['iiiiji'] = wasmExports['dynCall_iiiiji']; + dynCall_iiiijii = dynCalls['iiiijii'] = wasmExports['dynCall_iiiijii']; + dynCall_viiji = dynCalls['viiji'] = wasmExports['dynCall_viiji']; + dynCall_viijii = dynCalls['viijii'] = wasmExports['dynCall_viijii']; + dynCall_iiiiiiiiiii = dynCalls['iiiiiiiiiii'] = + wasmExports['dynCall_iiiiiiiiiii']; + dynCall_iiiijji = dynCalls['iiiijji'] = wasmExports['dynCall_iiiijji']; + dynCall_dd = dynCalls['dd'] = wasmExports['dynCall_dd']; + dynCall_ddd = dynCalls['ddd'] = wasmExports['dynCall_ddd']; + dynCall_jiji = dynCalls['jiji'] = wasmExports['dynCall_jiji']; + dynCall_iidiiii = dynCalls['iidiiii'] = wasmExports['dynCall_iidiiii']; + _asyncify_start_unwind = wasmExports['asyncify_start_unwind']; + _asyncify_stop_unwind = wasmExports['asyncify_stop_unwind']; + _asyncify_start_rewind = wasmExports['asyncify_start_rewind']; + _asyncify_stop_rewind = wasmExports['asyncify_stop_rewind']; + memory = wasmMemory = wasmExports['memory']; + __indirect_function_table = wasmTable = + wasmExports['__indirect_function_table']; + } + var ___heap_base = 5122080; + var wasmImports = { + __assert_fail: ___assert_fail, + __asyncify_data: ___asyncify_data, + __asyncify_state: ___asyncify_state, + __asyncjs__js_module_onMessage, + __call_sighandler: ___call_sighandler, + __syscall_accept4: ___syscall_accept4, + __syscall_bind: ___syscall_bind, + __syscall_chdir: ___syscall_chdir, + __syscall_chmod: ___syscall_chmod, + __syscall_connect: ___syscall_connect, + __syscall_dup: ___syscall_dup, + __syscall_dup3: ___syscall_dup3, + __syscall_faccessat: ___syscall_faccessat, + __syscall_fchmod: ___syscall_fchmod, + __syscall_fchown32: ___syscall_fchown32, + __syscall_fchownat: ___syscall_fchownat, + __syscall_fcntl64: ___syscall_fcntl64, + __syscall_fstat64: ___syscall_fstat64, + __syscall_ftruncate64: ___syscall_ftruncate64, + __syscall_getcwd: ___syscall_getcwd, + __syscall_getdents64: ___syscall_getdents64, + __syscall_getpeername: ___syscall_getpeername, + __syscall_getsockname: ___syscall_getsockname, + __syscall_getsockopt: ___syscall_getsockopt, + __syscall_ioctl: ___syscall_ioctl, + __syscall_listen: ___syscall_listen, + __syscall_lstat64: ___syscall_lstat64, + __syscall_mkdirat: ___syscall_mkdirat, + __syscall_newfstatat: ___syscall_newfstatat, + __syscall_openat: ___syscall_openat, + __syscall_pipe: ___syscall_pipe, + __syscall_readlinkat: ___syscall_readlinkat, + __syscall_recvfrom: ___syscall_recvfrom, + __syscall_renameat: ___syscall_renameat, + __syscall_rmdir: ___syscall_rmdir, + __syscall_sendto: ___syscall_sendto, + __syscall_socket: ___syscall_socket, + __syscall_stat64: ___syscall_stat64, + __syscall_statfs64: ___syscall_statfs64, + __syscall_symlinkat: ___syscall_symlinkat, + __syscall_unlinkat: ___syscall_unlinkat, + __syscall_utimensat: ___syscall_utimensat, + _abort_js: __abort_js, + _dlopen_js: __dlopen_js, + _dlsym_js: __dlsym_js, + _emscripten_lookup_name: __emscripten_lookup_name, + _emscripten_runtime_keepalive_clear: + __emscripten_runtime_keepalive_clear, + _emscripten_throw_longjmp: __emscripten_throw_longjmp, + _gmtime_js: __gmtime_js, + _localtime_js: __localtime_js, + _mktime_js: __mktime_js, + _mmap_js: __mmap_js, + _munmap_js: __munmap_js, + _setitimer_js: __setitimer_js, + _tzset_js: __tzset_js, + clock_time_get: _clock_time_get, + emscripten_date_now: _emscripten_date_now, + emscripten_get_heap_max: _emscripten_get_heap_max, + emscripten_get_now: _emscripten_get_now, + emscripten_resize_heap: _emscripten_resize_heap, + emscripten_sleep: _emscripten_sleep, + environ_get: _environ_get, + environ_sizes_get: _environ_sizes_get, + exit: _exit, + fd_close: _fd_close, + fd_fdstat_get: _fd_fdstat_get, + fd_read: _fd_read, + fd_seek: _fd_seek, + fd_sync: _fd_sync, + fd_write: _fd_write, + getaddrinfo: _getaddrinfo, + getnameinfo: _getnameinfo, + getprotobyname: _getprotobyname, + getprotobynumber: _getprotobynumber, + invoke_i, + invoke_ii, + invoke_iii, + invoke_iiii, + invoke_iiiii, + invoke_iiiiii, + invoke_iiiiiii, + invoke_iiiiiiii, + invoke_iiiiiiiii, + invoke_iiiiiiiiii, + invoke_jii, + invoke_v, + invoke_vi, + invoke_vii, + invoke_viii, + invoke_viiii, + invoke_viiiii, + invoke_viiiiiii, + js_fd_read, + js_open_process: _js_open_process, + js_popen_to_file, + js_wasm_trace: _js_wasm_trace, + proc_exit: _proc_exit, + strptime: _strptime, + wasm_poll_socket, + wasm_setsockopt: _wasm_setsockopt, + }; + function invoke_vii(index, a1, a2) { + var sp = stackSave(); + try { + dynCalls['vii'](index, a1, a2); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_vi(index, a1) { + var sp = stackSave(); + try { + dynCalls['vi'](index, a1); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_ii(index, a1) { + var sp = stackSave(); + try { + return dynCalls['ii'](index, a1); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_viii(index, a1, a2, a3) { + var sp = stackSave(); + try { + dynCalls['viii'](index, a1, a2, a3); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_iiiiiiii(index, a1, a2, a3, a4, a5, a6, a7) { + var sp = stackSave(); + try { + return dynCalls['iiiiiiii'](index, a1, a2, a3, a4, a5, a6, a7); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_v(index) { + var sp = stackSave(); + try { + dynCalls['v'](index); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_i(index) { + var sp = stackSave(); + try { + return dynCalls['i'](index); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_iiiii(index, a1, a2, a3, a4) { + var sp = stackSave(); + try { + return dynCalls['iiiii'](index, a1, a2, a3, a4); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_iiii(index, a1, a2, a3) { + var sp = stackSave(); + try { + return dynCalls['iiii'](index, a1, a2, a3); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_iiiiiii(index, a1, a2, a3, a4, a5, a6) { + var sp = stackSave(); + try { + return dynCalls['iiiiiii'](index, a1, a2, a3, a4, a5, a6); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_viiii(index, a1, a2, a3, a4) { + var sp = stackSave(); + try { + dynCalls['viiii'](index, a1, a2, a3, a4); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_iii(index, a1, a2) { + var sp = stackSave(); + try { + return dynCalls['iii'](index, a1, a2); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_viiiii(index, a1, a2, a3, a4, a5) { + var sp = stackSave(); + try { + dynCalls['viiiii'](index, a1, a2, a3, a4, a5); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_viiiiiii(index, a1, a2, a3, a4, a5, a6, a7) { + var sp = stackSave(); + try { + dynCalls['viiiiiii'](index, a1, a2, a3, a4, a5, a6, a7); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_iiiiii(index, a1, a2, a3, a4, a5) { + var sp = stackSave(); + try { + return dynCalls['iiiiii'](index, a1, a2, a3, a4, a5); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_iiiiiiiii(index, a1, a2, a3, a4, a5, a6, a7, a8) { + var sp = stackSave(); + try { + return dynCalls['iiiiiiiii'](index, a1, a2, a3, a4, a5, a6, a7, a8); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_iiiiiiiiii(index, a1, a2, a3, a4, a5, a6, a7, a8, a9) { + var sp = stackSave(); + try { + return dynCalls['iiiiiiiiii']( + index, + a1, + a2, + a3, + a4, + a5, + a6, + a7, + a8, + a9 + ); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + } + } + function invoke_jii(index, a1, a2) { + var sp = stackSave(); + try { + return dynCalls['jii'](index, a1, a2); + } catch (e) { + stackRestore(sp); + if (e !== e + 0) throw e; + _setThrew(1, 0); + return 0n; + } + } + function callMain(args = []) { + var entryFunction = resolveGlobalSymbol('main').sym; + if (!entryFunction) return; + args.unshift(thisProgram); + var argc = args.length; + var argv = stackAlloc((argc + 1) * 4); + var argv_ptr = argv; + for (var arg of args) { + HEAPU32[argv_ptr >> 2] = stringToUTF8OnStack(arg); + argv_ptr += 4; + } + HEAPU32[argv_ptr >> 2] = 0; + try { + var ret = entryFunction(argc, argv); + exitJS(ret, true); + return ret; + } catch (e) { + return handleException(e); + } + } + function run(args = arguments_) { + if (runDependencies > 0) { + dependenciesFulfilled = run; + return; + } + preRun(); + if (runDependencies > 0) { + dependenciesFulfilled = run; + return; + } + function doRun() { + Module['calledRun'] = true; + if (ABORT) return; + initRuntime(); + preMain(); + Module['onRuntimeInitialized']?.(); + var noInitialRun = Module['noInitialRun'] || true; + if (!noInitialRun) callMain(args); + postRun(); + } + if (Module['setStatus']) { + Module['setStatus']('Running...'); + setTimeout(() => { + setTimeout(() => Module['setStatus'](''), 1); + doRun(); + }, 1); + } else { + doRun(); + } + } + var wasmExports; + createWasm(); + run(); + /** + * Emscripten resolves `localhost` to a random IP address. Let's + * make it always resolve to 127.0.0.1. + */ + DNS.address_map.addrs.localhost = '127.0.0.1'; + + /** + * Debugging Asyncify errors is tricky because the stack trace is lost when the + * error is thrown. This code saves the stack trace in a global variable + * so that it can be inspected later. + */ + PHPLoader.debug = 'debug' in PHPLoader ? PHPLoader.debug : true; + if (PHPLoader.debug && typeof Asyncify !== 'undefined') { + const originalHandleSleep = Asyncify.handleSleep; + Asyncify.handleSleep = function (startAsync) { + if (!ABORT) { + Module['lastAsyncifyStackSource'] = new Error(); + } + return originalHandleSleep(startAsync); + }; + } + + /** + * Data dependencies call removeRunDependency() when they are loaded. + * The synchronous call stack then continues to run. If an error occurs + * in PHP initialization, e.g. Out Of Memory error, it will not be + * caught by any try/catch. This override propagates the failure to + * PHPLoader.onAbort() so that it can be handled. + */ + const originalRemoveRunDependency = PHPLoader['removeRunDependency']; + PHPLoader['removeRunDependency'] = function (...args) { + try { + originalRemoveRunDependency(...args); + } catch (e) { + PHPLoader['onAbort'](e); + } + }; + + if (typeof NODEFS === 'object') { + // We override NODEFS.createNode() to add an `isSharedFS` flag to all NODEFS + // nodes. This way we can tell whether file-locking is needed and possible + // for an FS node, even if wrapped with PROXYFS. + const originalNodeFsCreateNode = NODEFS.createNode; + NODEFS.createNode = function createNodeWithSharedFlag() { + const node = originalNodeFsCreateNode.apply(NODEFS, arguments); + node.isSharedFS = true; + return node; + }; + + var originalHashAddNode = FS.hashAddNode; + FS.hashAddNode = function hashAddNodeIfNotSharedFS(node) { + if (node?.isSharedFS) { + // Avoid caching shared VFS nodes so multiple instances + // can access the same underlying filesystem without + // conflicting caches. + return; + } + return originalHashAddNode.apply(FS, arguments); + }; + } + + /** + * Expose the PHP version so the PHP class can make version-specific + * adjustments to `php.ini`. + */ + PHPLoader['phpVersion'] = (() => { + const [major, minor, patch] = phpVersionString.split('.').map(Number); + return { major, minor, patch }; + })(); + + return PHPLoader; + + // Close the opening bracket from esm-prefix.js: +} diff --git a/packages/php-wasm/web-builds/5-2/build.js b/packages/php-wasm/web-builds/5-2/build.js new file mode 100644 index 00000000000..374de67e670 --- /dev/null +++ b/packages/php-wasm/web-builds/5-2/build.js @@ -0,0 +1,90 @@ +import esbuild from 'esbuild'; +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const projectRoot = path.resolve(__dirname, '..', '..', '..', '..'); +const packagePath = path.join(projectRoot, 'packages/php-wasm/web-builds/5-2'); +const distPath = path.join( + projectRoot, + 'dist/packages/php-wasm/web-builds/5-2' +); + +try { + fs.mkdirSync(distPath, { recursive: true }); +} catch (e) { + // Ignore +} + +/** + * Plugin to rewrite imports to work from the dist directory. + * Dynamic imports need to be preserved as external and paths adjusted. + */ +const externalPathPlugin = { + name: 'external-path', + setup(build) { + // Mark PHP loader files as external and rewrite their paths + build.onResolve( + { filter: /\.\.\/(?:jspi|asyncify)\/.*\.js$/ }, + (args) => { + const newPath = args.path.replace('../', './'); + return { path: newPath, external: true }; + } + ); + // Mark extension .so files as external and rewrite paths + build.onResolve( + { filter: /\.\.\/(?:jspi|asyncify)\/extensions\/.*\.so\?url$/ }, + (args) => { + const newPath = args.path.replace('../', './'); + return { path: newPath, external: true }; + } + ); + }, +}; + +async function build() { + // CommonJS build + await esbuild.build({ + entryPoints: [`${packagePath}/src/index.ts`], + supported: { 'dynamic-import': true }, + outExtension: { '.js': '.cjs' }, + outdir: distPath, + platform: 'browser', + assetNames: '[name]', + chunkNames: '[name]', + logOverride: { + 'direct-eval': 'silent', + 'commonjs-variable-in-esm': 'silent', + }, + format: 'cjs', + bundle: true, + tsconfig: `${packagePath}/tsconfig.json`, + external: ['@php-wasm/*', 'wasm-feature-detect'], + loader: { '.wasm': 'file', '.so': 'file' }, + plugins: [externalPathPlugin], + }); + + // ESM build + await esbuild.build({ + entryPoints: [`${packagePath}/src/index.ts`], + outdir: distPath, + platform: 'browser', + assetNames: '[name]', + chunkNames: '[name]', + logOverride: { + 'direct-eval': 'silent', + 'commonjs-variable-in-esm': 'silent', + }, + packages: 'external', + bundle: true, + tsconfig: `${packagePath}/tsconfig.json`, + external: ['@php-wasm/*', 'wasm-feature-detect'], + supported: { 'dynamic-import': true, 'top-level-await': true }, + format: 'esm', + loader: { '.wasm': 'file', '.so': 'file' }, + plugins: [externalPathPlugin], + }); +} +build(); diff --git a/packages/php-wasm/web-builds/5-2/jspi/5_2_17/php_5_2.wasm b/packages/php-wasm/web-builds/5-2/jspi/5_2_17/php_5_2.wasm new file mode 100755 index 00000000000..2686950c22a Binary files /dev/null and b/packages/php-wasm/web-builds/5-2/jspi/5_2_17/php_5_2.wasm differ diff --git a/packages/php-wasm/web-builds/5-2/jspi/php_5_2.js b/packages/php-wasm/web-builds/5-2/jspi/php_5_2.js new file mode 100644 index 00000000000..9534077a1f6 --- /dev/null +++ b/packages/php-wasm/web-builds/5-2/jspi/php_5_2.js @@ -0,0 +1,8235 @@ +import dependencyFilename from './5_2_17/php_5_2.wasm'; +export { dependencyFilename }; +export const dependenciesTotalSize = 9427349; +const phpVersionString = '5.2.17'; +export function init(RuntimeName, PHPLoader) { + // The rest of the code comes from the built php.js file and esm-suffix.js + var Module = typeof PHPLoader != 'undefined' ? PHPLoader : {}; + var ENVIRONMENT_IS_WEB = RuntimeName === 'WEB'; + var ENVIRONMENT_IS_WORKER = RuntimeName === 'WORKER'; + var ENVIRONMENT_IS_NODE = RuntimeName === 'NODE'; + var arguments_ = []; + var thisProgram = './this.program'; + var quit_ = (status, toThrow) => { + throw toThrow; + }; + var _scriptName = globalThis.document?.currentScript?.src; + if (ENVIRONMENT_IS_WORKER) { + _scriptName = self.location.href; + } + var scriptDirectory = ''; + function locateFile(path) { + if (Module['locateFile']) { + return Module['locateFile'](path, scriptDirectory); + } + return scriptDirectory + path; + } + var readAsync, readBinary; + if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { + try { + scriptDirectory = new URL('.', _scriptName).href; + } catch {} + { + if (ENVIRONMENT_IS_WORKER) { + readBinary = (url) => { + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, false); + xhr.responseType = 'arraybuffer'; + xhr.send(null); + return new Uint8Array(xhr.response); + }; + } + readAsync = async (url) => { + var response = await fetch(url, { credentials: 'same-origin' }); + if (response.ok) { + return response.arrayBuffer(); + } + throw new Error(response.status + ' : ' + response.url); + }; + } + } else { + } + var out = console.log.bind(console); + var err = console.error.bind(console); + var dynamicLibraries = []; + var wasmBinary; + var ABORT = false; + var EXITSTATUS; + var HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64; + var HEAP64, HEAPU64; + var runtimeInitialized = false; + var runtimeExited = false; + function updateMemoryViews() { + var b = wasmMemory.buffer; + HEAP8 = new Int8Array(b); + HEAP16 = new Int16Array(b); + Module['HEAPU8'] = HEAPU8 = new Uint8Array(b); + HEAPU16 = new Uint16Array(b); + HEAP32 = new Int32Array(b); + Module['HEAPU32'] = HEAPU32 = new Uint32Array(b); + HEAPF32 = new Float32Array(b); + HEAPF64 = new Float64Array(b); + HEAP64 = new BigInt64Array(b); + HEAPU64 = new BigUint64Array(b); + } + var __RELOC_FUNCS__ = []; + function preRun() { + if (Module['preRun']) { + if (typeof Module['preRun'] == 'function') + Module['preRun'] = [Module['preRun']]; + while (Module['preRun'].length) { + addOnPreRun(Module['preRun'].shift()); + } + } + callRuntimeCallbacks(onPreRuns); + } + function initRuntime() { + runtimeInitialized = true; + callRuntimeCallbacks(__RELOC_FUNCS__); + callRuntimeCallbacks(onInits); + if (!Module['noFSInit'] && !FS.initialized) FS.init(); + TTY.init(); + SOCKFS.root = FS.mount(SOCKFS, {}, null); + PIPEFS.root = FS.mount(PIPEFS, {}, null); + wasmExports['__wasm_call_ctors'](); + callRuntimeCallbacks(onPostCtors); + FS.ignorePermissions = false; + } + function preMain() {} + function exitRuntime() { + ___funcs_on_exit(); + FS.quit(); + TTY.shutdown(); + runtimeExited = true; + } + function postRun() { + if (Module['postRun']) { + if (typeof Module['postRun'] == 'function') + Module['postRun'] = [Module['postRun']]; + while (Module['postRun'].length) { + addOnPostRun(Module['postRun'].shift()); + } + } + callRuntimeCallbacks(onPostRuns); + } + function abort(what) { + Module['onAbort']?.(what); + what = 'Aborted(' + what + ')'; + err(what); + ABORT = true; + what += '. Build with -sASSERTIONS for more info.'; + if (runtimeInitialized) { + ___trap(); + } + var e = new WebAssembly.RuntimeError(what); + throw e; + } + var wasmBinaryFile; + function findWasmBinary() { + return locateFile(dependencyFilename); + } + function getBinarySync(file) { + if (file == wasmBinaryFile && wasmBinary) { + return new Uint8Array(wasmBinary); + } + if (readBinary) { + return readBinary(file); + } + throw 'both async and sync fetching of the wasm failed'; + } + async function getWasmBinary(binaryFile) { + if (!wasmBinary) { + try { + var response = await readAsync(binaryFile); + return new Uint8Array(response); + } catch {} + } + return getBinarySync(binaryFile); + } + async function instantiateArrayBuffer(binaryFile, imports) { + try { + var binary = await getWasmBinary(binaryFile); + var instance = await WebAssembly.instantiate(binary, imports); + return instance; + } catch (reason) { + err(`failed to asynchronously prepare wasm: ${reason}`); + abort(reason); + } + } + async function instantiateAsync(binary, binaryFile, imports) { + if (!binary) { + try { + var response = fetch(binaryFile, { + credentials: 'same-origin', + }); + var instantiationResult = + await WebAssembly.instantiateStreaming(response, imports); + return instantiationResult; + } catch (reason) { + err(`wasm streaming compile failed: ${reason}`); + err('falling back to ArrayBuffer instantiation'); + } + } + return instantiateArrayBuffer(binaryFile, imports); + } + function getWasmImports() { + Asyncify.instrumentWasmImports(wasmImports); + var imports = { + env: wasmImports, + wasi_snapshot_preview1: wasmImports, + 'GOT.mem': new Proxy(wasmImports, GOTHandler), + 'GOT.func': new Proxy(wasmImports, GOTHandler), + }; + return imports; + } + async function createWasm() { + function receiveInstance(instance, module) { + wasmExports = instance.exports; + var origExports = (wasmExports = relocateExports(wasmExports)); + wasmExports = Asyncify.instrumentWasmExports(wasmExports); + mergeLibSymbols(wasmExports, 'main'); + var metadata = getDylinkMetadata(module); + if (metadata.neededDynlibs) { + dynamicLibraries = + metadata.neededDynlibs.concat(dynamicLibraries); + } + assignWasmExports(wasmExports); + updateGOT(origExports); + Module['wasmExports'] = wasmExports; + LDSO.init(); + loadDylibs(); + updateMemoryViews(); + removeRunDependency('wasm-instantiate'); + return wasmExports; + } + addRunDependency('wasm-instantiate'); + function receiveInstantiationResult(result) { + return receiveInstance(result['instance'], result['module']); + } + var info = getWasmImports(); + if (Module['instantiateWasm']) { + return new Promise((resolve, reject) => { + Module['instantiateWasm'](info, (inst, mod) => { + resolve(receiveInstance(inst, mod)); + }); + }); + } + wasmBinaryFile ??= findWasmBinary(); + var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info); + var exports = receiveInstantiationResult(result); + return exports; + } + var asyncifyStubs = {}; + class ExitStatus { + name = 'ExitStatus'; + constructor(status) { + this.message = `Program terminated with exit(${status})`; + this.status = status; + } + } + ExitStatus = class PHPExitStatus extends Error { + constructor(status) { + super(status); + this.name = 'ExitStatus'; + this.message = 'Program terminated with exit(' + status + ')'; + this.status = status; + } + }; + var GOT = {}; + var currentModuleWeakSymbols = new Set([]); + var GOTHandler = { + get(obj, symName) { + var rtn = GOT[symName]; + if (!rtn) { + rtn = GOT[symName] = new WebAssembly.Global( + { value: 'i32', mutable: true }, + -1 + ); + } + if (!currentModuleWeakSymbols.has(symName)) { + rtn.required = true; + } + return rtn; + }, + }; + var callRuntimeCallbacks = (callbacks) => { + while (callbacks.length > 0) { + callbacks.shift()(Module); + } + }; + var onPostRuns = []; + var addOnPostRun = (cb) => onPostRuns.push(cb); + var onPreRuns = []; + var addOnPreRun = (cb) => onPreRuns.push(cb); + var runDependencies = 0; + var dependenciesFulfilled = null; + var removeRunDependency = (id) => { + runDependencies--; + Module['monitorRunDependencies']?.(runDependencies); + if (runDependencies == 0) { + if (dependenciesFulfilled) { + var callback = dependenciesFulfilled; + dependenciesFulfilled = null; + callback(); + } + } + }; + var addRunDependency = (id) => { + runDependencies++; + Module['monitorRunDependencies']?.(runDependencies); + }; + var UTF8Decoder = globalThis.TextDecoder && new TextDecoder(); + var findStringEnd = (heapOrArray, idx, maxBytesToRead, ignoreNul) => { + var maxIdx = idx + maxBytesToRead; + if (ignoreNul) return maxIdx; + while (heapOrArray[idx] && !(idx >= maxIdx)) ++idx; + return idx; + }; + var UTF8ArrayToString = ( + heapOrArray, + idx = 0, + maxBytesToRead, + ignoreNul + ) => { + var endPtr = findStringEnd(heapOrArray, idx, maxBytesToRead, ignoreNul); + if (endPtr - idx > 16 && heapOrArray.buffer && UTF8Decoder) { + return UTF8Decoder.decode(heapOrArray.subarray(idx, endPtr)); + } + var str = ''; + while (idx < endPtr) { + var u0 = heapOrArray[idx++]; + if (!(u0 & 128)) { + str += String.fromCharCode(u0); + continue; + } + var u1 = heapOrArray[idx++] & 63; + if ((u0 & 224) == 192) { + str += String.fromCharCode(((u0 & 31) << 6) | u1); + continue; + } + var u2 = heapOrArray[idx++] & 63; + if ((u0 & 240) == 224) { + u0 = ((u0 & 15) << 12) | (u1 << 6) | u2; + } else { + u0 = + ((u0 & 7) << 18) | + (u1 << 12) | + (u2 << 6) | + (heapOrArray[idx++] & 63); + } + if (u0 < 65536) { + str += String.fromCharCode(u0); + } else { + var ch = u0 - 65536; + str += String.fromCharCode( + 55296 | (ch >> 10), + 56320 | (ch & 1023) + ); + } + } + return str; + }; + var getDylinkMetadata = (binary) => { + var offset = 0; + var end = 0; + function getU8() { + return binary[offset++]; + } + function getLEB() { + var ret = 0; + var mul = 1; + while (1) { + var byte = binary[offset++]; + ret += (byte & 127) * mul; + mul *= 128; + if (!(byte & 128)) break; + } + return ret; + } + function getString() { + var len = getLEB(); + offset += len; + return UTF8ArrayToString(binary, offset - len, len); + } + function getStringList() { + var count = getLEB(); + var rtn = []; + while (count--) rtn.push(getString()); + return rtn; + } + function failIf(condition, message) { + if (condition) throw new Error(message); + } + if (binary instanceof WebAssembly.Module) { + var dylinkSection = WebAssembly.Module.customSections( + binary, + 'dylink.0' + ); + failIf(dylinkSection.length === 0, 'need dylink section'); + binary = new Uint8Array(dylinkSection[0]); + end = binary.length; + } else { + var int32View = new Uint32Array( + new Uint8Array(binary.subarray(0, 24)).buffer + ); + var magicNumberFound = int32View[0] == 1836278016; + failIf(!magicNumberFound, 'need to see wasm magic number'); + failIf(binary[8] !== 0, 'need the dylink section to be first'); + offset = 9; + var section_size = getLEB(); + end = offset + section_size; + var name = getString(); + failIf(name !== 'dylink.0'); + } + var customSection = { + neededDynlibs: [], + tlsExports: new Set(), + weakImports: new Set(), + runtimePaths: [], + }; + var WASM_DYLINK_MEM_INFO = 1; + var WASM_DYLINK_NEEDED = 2; + var WASM_DYLINK_EXPORT_INFO = 3; + var WASM_DYLINK_IMPORT_INFO = 4; + var WASM_DYLINK_RUNTIME_PATH = 5; + var WASM_SYMBOL_TLS = 256; + var WASM_SYMBOL_BINDING_MASK = 3; + var WASM_SYMBOL_BINDING_WEAK = 1; + while (offset < end) { + var subsectionType = getU8(); + var subsectionSize = getLEB(); + if (subsectionType === WASM_DYLINK_MEM_INFO) { + customSection.memorySize = getLEB(); + customSection.memoryAlign = getLEB(); + customSection.tableSize = getLEB(); + customSection.tableAlign = getLEB(); + } else if (subsectionType === WASM_DYLINK_NEEDED) { + customSection.neededDynlibs = getStringList(); + } else if (subsectionType === WASM_DYLINK_EXPORT_INFO) { + var count = getLEB(); + while (count--) { + var symname = getString(); + var flags = getLEB(); + if (flags & WASM_SYMBOL_TLS) { + customSection.tlsExports.add(symname); + } + } + } else if (subsectionType === WASM_DYLINK_IMPORT_INFO) { + var count = getLEB(); + while (count--) { + var modname = getString(); + var symname = getString(); + var flags = getLEB(); + if ( + (flags & WASM_SYMBOL_BINDING_MASK) == + WASM_SYMBOL_BINDING_WEAK + ) { + customSection.weakImports.add(symname); + } + } + } else if (subsectionType === WASM_DYLINK_RUNTIME_PATH) { + customSection.runtimePaths = getStringList(); + } else { + offset += subsectionSize; + } + } + return customSection; + }; + var newDSO = (name, handle, syms) => { + var dso = { refcount: Infinity, name, exports: syms, global: true }; + LDSO.loadedLibsByName[name] = dso; + if (handle != undefined) { + LDSO.loadedLibsByHandle[handle] = dso; + } + return dso; + }; + var LDSO = { + loadedLibsByName: {}, + loadedLibsByHandle: {}, + init() { + newDSO('__main__', 0, wasmImports); + }, + }; + var alignMemory = (size, alignment) => + Math.ceil(size / alignment) * alignment; + var getMemory = (size) => { + if (runtimeInitialized) { + return _calloc(size, 1); + } + var ret = ___heap_base; + var end = ret + alignMemory(size, 16); + ___heap_base = end; + var sbrk_ptr = _emscripten_get_sbrk_ptr(); + HEAPU32[sbrk_ptr >> 2] = end; + return ret; + }; + var isInternalSym = (symName) => + [ + 'memory', + '__memory_base', + '__table_base', + '__stack_pointer', + '__indirect_function_table', + '__cpp_exception', + '__c_longjmp', + '__wasm_apply_data_relocs', + '__dso_handle', + '__tls_size', + '__tls_align', + '__set_stack_limits', + '_emscripten_tls_init', + '__wasm_init_tls', + '__wasm_call_ctors', + '__start_em_asm', + '__stop_em_asm', + '__start_em_js', + '__stop_em_js', + ].includes(symName) || symName.startsWith('__em_js__'); + var wasmTableMirror = []; + var getWasmTableEntry = (funcPtr) => { + var func = wasmTableMirror[funcPtr]; + if (!func) { + wasmTableMirror[funcPtr] = func = wasmTable.get(funcPtr); + if (Asyncify.isAsyncExport(func)) { + wasmTableMirror[funcPtr] = func = + Asyncify.makeAsyncFunction(func); + } + } + return func; + }; + var updateTableMap = (offset, count) => { + if (functionsInTableMap) { + for (var i = offset; i < offset + count; i++) { + var item = getWasmTableEntry(i); + if (item) { + functionsInTableMap.set(item, i); + } + } + } + }; + var functionsInTableMap; + var getFunctionAddress = (func) => { + if (!functionsInTableMap) { + functionsInTableMap = new WeakMap(); + updateTableMap(0, wasmTable.length); + } + return functionsInTableMap.get(func) || 0; + }; + var freeTableIndexes = []; + var getEmptyTableSlot = () => { + if (freeTableIndexes.length) { + return freeTableIndexes.pop(); + } + return wasmTable['grow'](1); + }; + var setWasmTableEntry = (idx, func) => { + wasmTable.set(idx, func); + wasmTableMirror[idx] = wasmTable.get(idx); + }; + var uleb128EncodeWithLen = (arr) => { + const n = arr.length; + return [(n % 128) | 128, n >> 7, ...arr]; + }; + var wasmTypeCodes = { i: 127, p: 127, j: 126, f: 125, d: 124, e: 111 }; + var generateTypePack = (types) => + uleb128EncodeWithLen( + Array.from(types, (type) => { + var code = wasmTypeCodes[type]; + return code; + }) + ); + var convertJsFunctionToWasm = (func, sig) => { + var bytes = Uint8Array.of( + 0, + 97, + 115, + 109, + 1, + 0, + 0, + 0, + 1, + ...uleb128EncodeWithLen([ + 1, + 96, + ...generateTypePack(sig.slice(1)), + ...generateTypePack(sig[0] === 'v' ? '' : sig[0]), + ]), + 2, + 7, + 1, + 1, + 101, + 1, + 102, + 0, + 0, + 7, + 5, + 1, + 1, + 102, + 0, + 0 + ); + var module = new WebAssembly.Module(bytes); + var instance = new WebAssembly.Instance(module, { e: { f: func } }); + var wrappedFunc = instance.exports['f']; + return wrappedFunc; + }; + var addFunction = (func, sig) => { + var rtn = getFunctionAddress(func); + if (rtn) { + return rtn; + } + var ret = getEmptyTableSlot(); + try { + setWasmTableEntry(ret, func); + } catch (err) { + if (!(err instanceof TypeError)) { + throw err; + } + var wrapped = convertJsFunctionToWasm(func, sig); + setWasmTableEntry(ret, wrapped); + } + functionsInTableMap.set(func, ret); + return ret; + }; + var updateGOT = (exports, replace) => { + for (var symName in exports) { + if (isInternalSym(symName)) { + continue; + } + var value = exports[symName]; + var existingEntry = GOT[symName] && GOT[symName].value != -1; + if (replace || !existingEntry) { + var newValue; + if (typeof value == 'function') { + newValue = addFunction(value); + } else if (typeof value == 'number') { + newValue = value; + } else { + continue; + } + GOT[symName] ??= new WebAssembly.Global({ + value: 'i32', + mutable: true, + }); + GOT[symName].value = newValue; + } + } + }; + var isImmutableGlobal = (val) => { + if (val instanceof WebAssembly.Global) { + try { + val.value = val.value; + } catch { + return true; + } + } + return false; + }; + var relocateExports = (exports, memoryBase = 0) => { + function relocateExport(name, value) { + if (isImmutableGlobal(value)) { + return value.value + memoryBase; + } + return value; + } + var relocated = {}; + for (var e in exports) { + relocated[e] = relocateExport(e, exports[e]); + } + return relocated; + }; + var isSymbolDefined = (symName) => { + var existing = wasmImports[symName]; + if (!existing || existing.stub) { + return false; + } + if (symName in asyncifyStubs && !asyncifyStubs[symName]) { + return false; + } + return true; + }; + var resolveGlobalSymbol = (symName, direct = false) => { + var sym; + if (isSymbolDefined(symName)) { + sym = wasmImports[symName]; + } + return { sym, name: symName }; + }; + var onPostCtors = []; + var addOnPostCtor = (cb) => onPostCtors.push(cb); + var UTF8ToString = (ptr, maxBytesToRead, ignoreNul) => + ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead, ignoreNul) : ''; + var loadWebAssemblyModule = ( + binary, + flags, + libName, + localScope, + handle + ) => { + var metadata = getDylinkMetadata(binary); + function loadModule() { + var memAlign = Math.pow(2, metadata.memoryAlign); + var memoryBase = metadata.memorySize + ? alignMemory( + getMemory(metadata.memorySize + memAlign), + memAlign + ) + : 0; + var tableBase = metadata.tableSize ? wasmTable.length : 0; + if (handle) { + HEAP8[handle + 8] = 1; + HEAPU32[(handle + 12) >> 2] = memoryBase; + HEAP32[(handle + 16) >> 2] = metadata.memorySize; + HEAPU32[(handle + 20) >> 2] = tableBase; + HEAP32[(handle + 24) >> 2] = metadata.tableSize; + } + if (metadata.tableSize) { + wasmTable.grow(metadata.tableSize); + } + var moduleExports; + function resolveSymbol(sym) { + var resolved = resolveGlobalSymbol(sym).sym; + if (!resolved && localScope) { + resolved = localScope[sym]; + } + if (!resolved) { + resolved = moduleExports[sym]; + } + return resolved; + } + var proxyHandler = { + get(stubs, prop) { + switch (prop) { + case '__memory_base': + return memoryBase; + case '__table_base': + return tableBase; + } + if (prop in wasmImports && !wasmImports[prop].stub) { + var res = wasmImports[prop]; + if (res.orig) { + res = res.orig; + } + return res; + } + if (!(prop in stubs)) { + var resolved; + stubs[prop] = (...args) => { + resolved ||= resolveSymbol(prop); + return resolved(...args); + }; + } + return stubs[prop]; + }, + }; + var proxy = new Proxy({}, proxyHandler); + currentModuleWeakSymbols = metadata.weakImports; + var info = { + 'GOT.mem': new Proxy({}, GOTHandler), + 'GOT.func': new Proxy({}, GOTHandler), + env: proxy, + wasi_snapshot_preview1: proxy, + }; + function postInstantiation(module, instance) { + updateTableMap(tableBase, metadata.tableSize); + moduleExports = relocateExports(instance.exports, memoryBase); + updateGOT(moduleExports); + moduleExports = Asyncify.instrumentWasmExports(moduleExports); + if (!flags.allowUndefined) { + reportUndefinedSymbols(); + } + function addEmAsm(addr, body) { + var args = []; + for (var arity = 0; ; arity++) { + var argName = '$' + arity; + if (!body.includes(argName)) break; + args.push(argName); + } + args = args.join(','); + var func = `(${args}) => { ${body} };`; + ASM_CONSTS[start] = eval(func); + } + if ('__start_em_asm' in moduleExports) { + var start = moduleExports['__start_em_asm']; + var stop = moduleExports['__stop_em_asm']; + while (start < stop) { + var jsString = UTF8ToString(start); + addEmAsm(start, jsString); + start = HEAPU8.indexOf(0, start) + 1; + } + } + function addEmJs(name, cSig, body) { + var jsArgs = []; + cSig = cSig.slice(1, -1); + if (cSig != 'void') { + cSig = cSig.split(','); + for (var arg of cSig) { + var jsArg = arg.split(' ').pop(); + jsArgs.push(jsArg.replace('*', '')); + } + } + var func = `(${jsArgs}) => ${body};`; + moduleExports[name] = eval(func); + } + for (var name in moduleExports) { + if (name.startsWith('__em_js__')) { + var start = moduleExports[name]; + var jsString = UTF8ToString(start); + var [sig, body] = jsString.split('<::>'); + addEmJs(name.replace('__em_js__', ''), sig, body); + delete moduleExports[name]; + } + } + var applyRelocs = moduleExports['__wasm_apply_data_relocs']; + if (applyRelocs) { + if (runtimeInitialized) { + applyRelocs(); + } else { + __RELOC_FUNCS__.push(applyRelocs); + } + } + var init = moduleExports['__wasm_call_ctors']; + if (init) { + if (runtimeInitialized) { + init(); + } else { + addOnPostCtor(init); + } + } + return moduleExports; + } + if (flags.loadAsync) { + return (async () => { + var instance; + if (binary instanceof WebAssembly.Module) { + instance = new WebAssembly.Instance(binary, info); + } else { + ({ module: binary, instance } = + await WebAssembly.instantiate(binary, info)); + } + return postInstantiation(binary, instance); + })(); + } + var module = + binary instanceof WebAssembly.Module + ? binary + : new WebAssembly.Module(binary); + var instance = new WebAssembly.Instance(module, info); + return postInstantiation(module, instance); + } + flags = { + ...flags, + rpath: { parentLibPath: libName, paths: metadata.runtimePaths }, + }; + if (flags.loadAsync) { + return metadata.neededDynlibs + .reduce( + (chain, dynNeeded) => + chain.then(() => + loadDynamicLibrary(dynNeeded, flags, localScope) + ), + Promise.resolve() + ) + .then(loadModule); + } + for (var needed of metadata.neededDynlibs) { + loadDynamicLibrary(needed, flags, localScope); + } + return loadModule(); + }; + var mergeLibSymbols = (exports, libName) => { + for (var [sym, exp] of Object.entries(exports)) { + const setImport = (target) => { + if (target in asyncifyStubs) { + asyncifyStubs[target] = exp; + } + if (!isSymbolDefined(target)) { + wasmImports[target] = exp; + } + }; + setImport(sym); + const main_alias = '__main_argc_argv'; + if (sym == 'main') { + setImport(main_alias); + } + if (sym == main_alias) { + setImport('main'); + } + } + }; + var asyncLoad = async (url) => { + var arrayBuffer = await readAsync(url); + return new Uint8Array(arrayBuffer); + }; + var preloadPlugins = []; + var registerWasmPlugin = () => { + var wasmPlugin = { + promiseChainEnd: Promise.resolve(), + canHandle: (name) => + !Module['noWasmDecoding'] && name.endsWith('.so'), + handle: async (byteArray, name) => + (wasmPlugin.promiseChainEnd = wasmPlugin.promiseChainEnd.then( + async () => { + try { + var exports = await loadWebAssemblyModule( + byteArray, + { loadAsync: true, nodelete: true }, + name, + {} + ); + } catch (error) { + throw new Error( + `failed to instantiate wasm: ${name}: ${error}` + ); + } + preloadedWasm[name] = exports; + return byteArray; + } + )), + }; + preloadPlugins.push(wasmPlugin); + }; + var preloadedWasm = {}; + var PATH = { + isAbs: (path) => path.charAt(0) === '/', + splitPath: (filename) => { + var splitPathRe = + /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; + return splitPathRe.exec(filename).slice(1); + }, + normalizeArray: (parts, allowAboveRoot) => { + var up = 0; + for (var i = parts.length - 1; i >= 0; i--) { + var last = parts[i]; + if (last === '.') { + parts.splice(i, 1); + } else if (last === '..') { + parts.splice(i, 1); + up++; + } else if (up) { + parts.splice(i, 1); + up--; + } + } + if (allowAboveRoot) { + for (; up; up--) { + parts.unshift('..'); + } + } + return parts; + }, + normalize: (path) => { + var isAbsolute = PATH.isAbs(path), + trailingSlash = path.slice(-1) === '/'; + path = PATH.normalizeArray( + path.split('/').filter((p) => !!p), + !isAbsolute + ).join('/'); + if (!path && !isAbsolute) { + path = '.'; + } + if (path && trailingSlash) { + path += '/'; + } + return (isAbsolute ? '/' : '') + path; + }, + dirname: (path) => { + var result = PATH.splitPath(path), + root = result[0], + dir = result[1]; + if (!root && !dir) { + return '.'; + } + if (dir) { + dir = dir.slice(0, -1); + } + return root + dir; + }, + basename: (path) => path && path.match(/([^\/]+|\/)\/*$/)[1], + join: (...paths) => PATH.normalize(paths.join('/')), + join2: (l, r) => PATH.normalize(l + '/' + r), + }; + var replaceORIGIN = (parentLibName, rpath) => { + if (rpath.startsWith('$ORIGIN')) { + var origin = PATH.dirname(parentLibName); + return rpath.replace('$ORIGIN', origin); + } + return rpath; + }; + var stackSave = () => _emscripten_stack_get_current(); + var stackRestore = (val) => __emscripten_stack_restore(val); + var withStackSave = (f) => { + var stack = stackSave(); + var ret = f(); + stackRestore(stack); + return ret; + }; + var stackAlloc = (sz) => __emscripten_stack_alloc(sz); + var lengthBytesUTF8 = (str) => { + var len = 0; + for (var i = 0; i < str.length; ++i) { + var c = str.charCodeAt(i); + if (c <= 127) { + len++; + } else if (c <= 2047) { + len += 2; + } else if (c >= 55296 && c <= 57343) { + len += 4; + ++i; + } else { + len += 3; + } + } + return len; + }; + var stringToUTF8Array = (str, heap, outIdx, maxBytesToWrite) => { + if (!(maxBytesToWrite > 0)) return 0; + var startIdx = outIdx; + var endIdx = outIdx + maxBytesToWrite - 1; + for (var i = 0; i < str.length; ++i) { + var u = str.codePointAt(i); + if (u <= 127) { + if (outIdx >= endIdx) break; + heap[outIdx++] = u; + } else if (u <= 2047) { + if (outIdx + 1 >= endIdx) break; + heap[outIdx++] = 192 | (u >> 6); + heap[outIdx++] = 128 | (u & 63); + } else if (u <= 65535) { + if (outIdx + 2 >= endIdx) break; + heap[outIdx++] = 224 | (u >> 12); + heap[outIdx++] = 128 | ((u >> 6) & 63); + heap[outIdx++] = 128 | (u & 63); + } else { + if (outIdx + 3 >= endIdx) break; + heap[outIdx++] = 240 | (u >> 18); + heap[outIdx++] = 128 | ((u >> 12) & 63); + heap[outIdx++] = 128 | ((u >> 6) & 63); + heap[outIdx++] = 128 | (u & 63); + i++; + } + } + heap[outIdx] = 0; + return outIdx - startIdx; + }; + var stringToUTF8 = (str, outPtr, maxBytesToWrite) => + stringToUTF8Array(str, HEAPU8, outPtr, maxBytesToWrite); + var stringToUTF8OnStack = (str) => { + var size = lengthBytesUTF8(str) + 1; + var ret = stackAlloc(size); + stringToUTF8(str, ret, size); + return ret; + }; + var initRandomFill = () => (view) => crypto.getRandomValues(view); + var randomFill = (view) => { + (randomFill = initRandomFill())(view); + }; + var PATH_FS = { + resolve: (...args) => { + var resolvedPath = '', + resolvedAbsolute = false; + for (var i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) { + var path = i >= 0 ? args[i] : FS.cwd(); + if (typeof path != 'string') { + throw new TypeError( + 'Arguments to path.resolve must be strings' + ); + } else if (!path) { + return ''; + } + resolvedPath = path + '/' + resolvedPath; + resolvedAbsolute = PATH.isAbs(path); + } + resolvedPath = PATH.normalizeArray( + resolvedPath.split('/').filter((p) => !!p), + !resolvedAbsolute + ).join('/'); + return (resolvedAbsolute ? '/' : '') + resolvedPath || '.'; + }, + relative: (from, to) => { + from = PATH_FS.resolve(from).slice(1); + to = PATH_FS.resolve(to).slice(1); + function trim(arr) { + var start = 0; + for (; start < arr.length; start++) { + if (arr[start] !== '') break; + } + var end = arr.length - 1; + for (; end >= 0; end--) { + if (arr[end] !== '') break; + } + if (start > end) return []; + return arr.slice(start, end - start + 1); + } + var fromParts = trim(from.split('/')); + var toParts = trim(to.split('/')); + var length = Math.min(fromParts.length, toParts.length); + var samePartsLength = length; + for (var i = 0; i < length; i++) { + if (fromParts[i] !== toParts[i]) { + samePartsLength = i; + break; + } + } + var outputParts = []; + for (var i = samePartsLength; i < fromParts.length; i++) { + outputParts.push('..'); + } + outputParts = outputParts.concat(toParts.slice(samePartsLength)); + return outputParts.join('/'); + }, + }; + var FS_stdin_getChar_buffer = []; + var intArrayFromString = (stringy, dontAddNull, length) => { + var len = length > 0 ? length : lengthBytesUTF8(stringy) + 1; + var u8array = new Array(len); + var numBytesWritten = stringToUTF8Array( + stringy, + u8array, + 0, + u8array.length + ); + if (dontAddNull) u8array.length = numBytesWritten; + return u8array; + }; + var FS_stdin_getChar = () => { + if (!FS_stdin_getChar_buffer.length) { + var result = null; + if (globalThis.window?.prompt) { + result = window.prompt('Input: '); + if (result !== null) { + result += '\n'; + } + } else { + } + if (!result) { + return null; + } + FS_stdin_getChar_buffer = intArrayFromString(result, true); + } + return FS_stdin_getChar_buffer.shift(); + }; + var TTY = { + ttys: [], + init() {}, + shutdown() {}, + register(dev, ops) { + TTY.ttys[dev] = { input: [], output: [], ops }; + FS.registerDevice(dev, TTY.stream_ops); + }, + stream_ops: { + open(stream) { + var tty = TTY.ttys[stream.node.rdev]; + if (!tty) { + throw new FS.ErrnoError(43); + } + stream.tty = tty; + stream.seekable = false; + }, + close(stream) { + stream.tty.ops.fsync(stream.tty); + }, + fsync(stream) { + stream.tty.ops.fsync(stream.tty); + }, + read(stream, buffer, offset, length, pos) { + if (!stream.tty || !stream.tty.ops.get_char) { + throw new FS.ErrnoError(60); + } + var bytesRead = 0; + for (var i = 0; i < length; i++) { + var result; + try { + result = stream.tty.ops.get_char(stream.tty); + } catch (e) { + throw new FS.ErrnoError(29); + } + if (result === undefined && bytesRead === 0) { + throw new FS.ErrnoError(6); + } + if (result === null || result === undefined) break; + bytesRead++; + buffer[offset + i] = result; + } + if (bytesRead) { + stream.node.atime = Date.now(); + } + return bytesRead; + }, + write(stream, buffer, offset, length, pos) { + if (!stream.tty || !stream.tty.ops.put_char) { + throw new FS.ErrnoError(60); + } + try { + for (var i = 0; i < length; i++) { + stream.tty.ops.put_char(stream.tty, buffer[offset + i]); + } + } catch (e) { + throw new FS.ErrnoError(29); + } + if (length) { + stream.node.mtime = stream.node.ctime = Date.now(); + } + return i; + }, + }, + default_tty_ops: { + get_char(tty) { + return FS_stdin_getChar(); + }, + put_char(tty, val) { + if (val === null || val === 10) { + out(UTF8ArrayToString(tty.output)); + tty.output = []; + } else { + if (val != 0) tty.output.push(val); + } + }, + fsync(tty) { + if (tty.output?.length > 0) { + out(UTF8ArrayToString(tty.output)); + tty.output = []; + } + }, + ioctl_tcgets(tty) { + return { + c_iflag: 25856, + c_oflag: 5, + c_cflag: 191, + c_lflag: 35387, + c_cc: [ + 3, 28, 127, 21, 4, 0, 1, 0, 17, 19, 26, 0, 18, 15, 23, + 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ], + }; + }, + ioctl_tcsets(tty, optional_actions, data) { + return 0; + }, + ioctl_tiocgwinsz(tty) { + return [24, 80]; + }, + }, + default_tty1_ops: { + put_char(tty, val) { + if (val === null || val === 10) { + err(UTF8ArrayToString(tty.output)); + tty.output = []; + } else { + if (val != 0) tty.output.push(val); + } + }, + fsync(tty) { + if (tty.output?.length > 0) { + err(UTF8ArrayToString(tty.output)); + tty.output = []; + } + }, + }, + }; + var zeroMemory = (ptr, size) => HEAPU8.fill(0, ptr, ptr + size); + var mmapAlloc = (size) => { + size = alignMemory(size, 65536); + var ptr = _emscripten_builtin_memalign(65536, size); + if (ptr) zeroMemory(ptr, size); + return ptr; + }; + var MEMFS = { + ops_table: null, + mount(mount) { + return MEMFS.createNode(null, '/', 16895, 0); + }, + createNode(parent, name, mode, dev) { + if (FS.isBlkdev(mode) || FS.isFIFO(mode)) { + throw new FS.ErrnoError(63); + } + MEMFS.ops_table ||= { + dir: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + lookup: MEMFS.node_ops.lookup, + mknod: MEMFS.node_ops.mknod, + rename: MEMFS.node_ops.rename, + unlink: MEMFS.node_ops.unlink, + rmdir: MEMFS.node_ops.rmdir, + readdir: MEMFS.node_ops.readdir, + symlink: MEMFS.node_ops.symlink, + }, + stream: { llseek: MEMFS.stream_ops.llseek }, + }, + file: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + }, + stream: { + llseek: MEMFS.stream_ops.llseek, + read: MEMFS.stream_ops.read, + write: MEMFS.stream_ops.write, + mmap: MEMFS.stream_ops.mmap, + msync: MEMFS.stream_ops.msync, + }, + }, + link: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + readlink: MEMFS.node_ops.readlink, + }, + stream: {}, + }, + chrdev: { + node: { + getattr: MEMFS.node_ops.getattr, + setattr: MEMFS.node_ops.setattr, + }, + stream: FS.chrdev_stream_ops, + }, + }; + var node = FS.createNode(parent, name, mode, dev); + if (FS.isDir(node.mode)) { + node.node_ops = MEMFS.ops_table.dir.node; + node.stream_ops = MEMFS.ops_table.dir.stream; + node.contents = {}; + } else if (FS.isFile(node.mode)) { + node.node_ops = MEMFS.ops_table.file.node; + node.stream_ops = MEMFS.ops_table.file.stream; + node.usedBytes = 0; + node.contents = null; + } else if (FS.isLink(node.mode)) { + node.node_ops = MEMFS.ops_table.link.node; + node.stream_ops = MEMFS.ops_table.link.stream; + } else if (FS.isChrdev(node.mode)) { + node.node_ops = MEMFS.ops_table.chrdev.node; + node.stream_ops = MEMFS.ops_table.chrdev.stream; + } + node.atime = node.mtime = node.ctime = Date.now(); + if (parent) { + parent.contents[name] = node; + parent.atime = parent.mtime = parent.ctime = node.atime; + } + return node; + }, + getFileDataAsTypedArray(node) { + if (!node.contents) return new Uint8Array(0); + if (node.contents.subarray) + return node.contents.subarray(0, node.usedBytes); + return new Uint8Array(node.contents); + }, + expandFileStorage(node, newCapacity) { + var prevCapacity = node.contents ? node.contents.length : 0; + if (prevCapacity >= newCapacity) return; + var CAPACITY_DOUBLING_MAX = 1024 * 1024; + newCapacity = Math.max( + newCapacity, + (prevCapacity * + (prevCapacity < CAPACITY_DOUBLING_MAX ? 2 : 1.125)) >>> + 0 + ); + if (prevCapacity != 0) newCapacity = Math.max(newCapacity, 256); + var oldContents = node.contents; + node.contents = new Uint8Array(newCapacity); + if (node.usedBytes > 0) + node.contents.set(oldContents.subarray(0, node.usedBytes), 0); + }, + resizeFileStorage(node, newSize) { + if (node.usedBytes == newSize) return; + if (newSize == 0) { + node.contents = null; + node.usedBytes = 0; + } else { + var oldContents = node.contents; + node.contents = new Uint8Array(newSize); + if (oldContents) { + node.contents.set( + oldContents.subarray( + 0, + Math.min(newSize, node.usedBytes) + ) + ); + } + node.usedBytes = newSize; + } + }, + node_ops: { + getattr(node) { + var attr = {}; + attr.dev = FS.isChrdev(node.mode) ? node.id : 1; + attr.ino = node.id; + attr.mode = node.mode; + attr.nlink = 1; + attr.uid = 0; + attr.gid = 0; + attr.rdev = node.rdev; + if (FS.isDir(node.mode)) { + attr.size = 4096; + } else if (FS.isFile(node.mode)) { + attr.size = node.usedBytes; + } else if (FS.isLink(node.mode)) { + attr.size = node.link.length; + } else { + attr.size = 0; + } + attr.atime = new Date(node.atime); + attr.mtime = new Date(node.mtime); + attr.ctime = new Date(node.ctime); + attr.blksize = 4096; + attr.blocks = Math.ceil(attr.size / attr.blksize); + return attr; + }, + setattr(node, attr) { + for (const key of ['mode', 'atime', 'mtime', 'ctime']) { + if (attr[key] != null) { + node[key] = attr[key]; + } + } + if (attr.size !== undefined) { + MEMFS.resizeFileStorage(node, attr.size); + } + }, + lookup(parent, name) { + if (!MEMFS.doesNotExistError) { + MEMFS.doesNotExistError = new FS.ErrnoError(44); + MEMFS.doesNotExistError.stack = ''; + } + throw MEMFS.doesNotExistError; + }, + mknod(parent, name, mode, dev) { + return MEMFS.createNode(parent, name, mode, dev); + }, + rename(old_node, new_dir, new_name) { + var new_node; + try { + new_node = FS.lookupNode(new_dir, new_name); + } catch (e) {} + if (new_node) { + if (FS.isDir(old_node.mode)) { + for (var i in new_node.contents) { + throw new FS.ErrnoError(55); + } + } + FS.hashRemoveNode(new_node); + } + delete old_node.parent.contents[old_node.name]; + new_dir.contents[new_name] = old_node; + old_node.name = new_name; + new_dir.ctime = + new_dir.mtime = + old_node.parent.ctime = + old_node.parent.mtime = + Date.now(); + }, + unlink(parent, name) { + delete parent.contents[name]; + parent.ctime = parent.mtime = Date.now(); + }, + rmdir(parent, name) { + var node = FS.lookupNode(parent, name); + for (var i in node.contents) { + throw new FS.ErrnoError(55); + } + delete parent.contents[name]; + parent.ctime = parent.mtime = Date.now(); + }, + readdir(node) { + return ['.', '..', ...Object.keys(node.contents)]; + }, + symlink(parent, newname, oldpath) { + var node = MEMFS.createNode(parent, newname, 511 | 40960, 0); + node.link = oldpath; + return node; + }, + readlink(node) { + if (!FS.isLink(node.mode)) { + throw new FS.ErrnoError(28); + } + return node.link; + }, + }, + stream_ops: { + read(stream, buffer, offset, length, position) { + var contents = stream.node.contents; + if (position >= stream.node.usedBytes) return 0; + var size = Math.min(stream.node.usedBytes - position, length); + if (size > 8 && contents.subarray) { + buffer.set( + contents.subarray(position, position + size), + offset + ); + } else { + for (var i = 0; i < size; i++) + buffer[offset + i] = contents[position + i]; + } + return size; + }, + write(stream, buffer, offset, length, position, canOwn) { + if (buffer.buffer === HEAP8.buffer) { + canOwn = false; + } + if (!length) return 0; + var node = stream.node; + node.mtime = node.ctime = Date.now(); + if ( + buffer.subarray && + (!node.contents || node.contents.subarray) + ) { + if (canOwn) { + node.contents = buffer.subarray( + offset, + offset + length + ); + node.usedBytes = length; + return length; + } else if (node.usedBytes === 0 && position === 0) { + node.contents = buffer.slice(offset, offset + length); + node.usedBytes = length; + return length; + } else if (position + length <= node.usedBytes) { + node.contents.set( + buffer.subarray(offset, offset + length), + position + ); + return length; + } + } + MEMFS.expandFileStorage(node, position + length); + if (node.contents.subarray && buffer.subarray) { + node.contents.set( + buffer.subarray(offset, offset + length), + position + ); + } else { + for (var i = 0; i < length; i++) { + node.contents[position + i] = buffer[offset + i]; + } + } + node.usedBytes = Math.max(node.usedBytes, position + length); + return length; + }, + llseek(stream, offset, whence) { + var position = offset; + if (whence === 1) { + position += stream.position; + } else if (whence === 2) { + if (FS.isFile(stream.node.mode)) { + position += stream.node.usedBytes; + } + } + if (position < 0) { + throw new FS.ErrnoError(28); + } + return position; + }, + mmap(stream, length, position, prot, flags) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError(43); + } + var ptr; + var allocated; + var contents = stream.node.contents; + if ( + !(flags & 2) && + contents && + contents.buffer === HEAP8.buffer + ) { + allocated = false; + ptr = contents.byteOffset; + } else { + allocated = true; + ptr = mmapAlloc(length); + if (!ptr) { + throw new FS.ErrnoError(48); + } + if (contents) { + if ( + position > 0 || + position + length < contents.length + ) { + if (contents.subarray) { + contents = contents.subarray( + position, + position + length + ); + } else { + contents = Array.prototype.slice.call( + contents, + position, + position + length + ); + } + } + HEAP8.set(contents, ptr); + } + } + return { ptr, allocated }; + }, + msync(stream, buffer, offset, length, mmapFlags) { + MEMFS.stream_ops.write( + stream, + buffer, + 0, + length, + offset, + false + ); + return 0; + }, + }, + }; + var FS_modeStringToFlags = (str) => { + var flagModes = { + r: 0, + 'r+': 2, + w: 512 | 64 | 1, + 'w+': 512 | 64 | 2, + a: 1024 | 64 | 1, + 'a+': 1024 | 64 | 2, + }; + var flags = flagModes[str]; + if (typeof flags == 'undefined') { + throw new Error(`Unknown file open mode: ${str}`); + } + return flags; + }; + var FS_getMode = (canRead, canWrite) => { + var mode = 0; + if (canRead) mode |= 292 | 73; + if (canWrite) mode |= 146; + return mode; + }; + var ERRNO_CODES = { + EPERM: 63, + ENOENT: 44, + ESRCH: 71, + EINTR: 27, + EIO: 29, + ENXIO: 60, + E2BIG: 1, + ENOEXEC: 45, + EBADF: 8, + ECHILD: 12, + EAGAIN: 6, + EWOULDBLOCK: 6, + ENOMEM: 48, + EACCES: 2, + EFAULT: 21, + ENOTBLK: 105, + EBUSY: 10, + EEXIST: 20, + EXDEV: 75, + ENODEV: 43, + ENOTDIR: 54, + EISDIR: 31, + EINVAL: 28, + ENFILE: 41, + EMFILE: 33, + ENOTTY: 59, + ETXTBSY: 74, + EFBIG: 22, + ENOSPC: 51, + ESPIPE: 70, + EROFS: 69, + EMLINK: 34, + EPIPE: 64, + EDOM: 18, + ERANGE: 68, + ENOMSG: 49, + EIDRM: 24, + ECHRNG: 106, + EL2NSYNC: 156, + EL3HLT: 107, + EL3RST: 108, + ELNRNG: 109, + EUNATCH: 110, + ENOCSI: 111, + EL2HLT: 112, + EDEADLK: 16, + ENOLCK: 46, + EBADE: 113, + EBADR: 114, + EXFULL: 115, + ENOANO: 104, + EBADRQC: 103, + EBADSLT: 102, + EDEADLOCK: 16, + EBFONT: 101, + ENOSTR: 100, + ENODATA: 116, + ETIME: 117, + ENOSR: 118, + ENONET: 119, + ENOPKG: 120, + EREMOTE: 121, + ENOLINK: 47, + EADV: 122, + ESRMNT: 123, + ECOMM: 124, + EPROTO: 65, + EMULTIHOP: 36, + EDOTDOT: 125, + EBADMSG: 9, + ENOTUNIQ: 126, + EBADFD: 127, + EREMCHG: 128, + ELIBACC: 129, + ELIBBAD: 130, + ELIBSCN: 131, + ELIBMAX: 132, + ELIBEXEC: 133, + ENOSYS: 52, + ENOTEMPTY: 55, + ENAMETOOLONG: 37, + ELOOP: 32, + EOPNOTSUPP: 138, + EPFNOSUPPORT: 139, + ECONNRESET: 15, + ENOBUFS: 42, + EAFNOSUPPORT: 5, + EPROTOTYPE: 67, + ENOTSOCK: 57, + ENOPROTOOPT: 50, + ESHUTDOWN: 140, + ECONNREFUSED: 14, + EADDRINUSE: 3, + ECONNABORTED: 13, + ENETUNREACH: 40, + ENETDOWN: 38, + ETIMEDOUT: 73, + EHOSTDOWN: 142, + EHOSTUNREACH: 23, + EINPROGRESS: 26, + EALREADY: 7, + EDESTADDRREQ: 17, + EMSGSIZE: 35, + EPROTONOSUPPORT: 66, + ESOCKTNOSUPPORT: 137, + EADDRNOTAVAIL: 4, + ENETRESET: 39, + EISCONN: 30, + ENOTCONN: 53, + ETOOMANYREFS: 141, + EUSERS: 136, + EDQUOT: 19, + ESTALE: 72, + ENOTSUP: 138, + ENOMEDIUM: 148, + EILSEQ: 25, + EOVERFLOW: 61, + ECANCELED: 11, + ENOTRECOVERABLE: 56, + EOWNERDEAD: 62, + ESTRPIPE: 135, + }; + var PROXYFS = { + mount(mount) { + return PROXYFS.createNode( + null, + '/', + mount.opts.fs.lstat(mount.opts.root).mode, + 0 + ); + }, + createNode(parent, name, mode, dev) { + if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) { + throw new FS.ErrnoError(ERRNO_CODES.EINVAL); + } + var node = FS.createNode(parent, name, mode); + node.node_ops = PROXYFS.node_ops; + node.stream_ops = PROXYFS.stream_ops; + return node; + }, + realPath(node) { + var parts = []; + while (node.parent !== node) { + parts.push(node.name); + node = node.parent; + } + parts.push(node.mount.opts.root); + parts.reverse(); + return PATH.join(...parts); + }, + node_ops: { + getattr(node) { + var path = PROXYFS.realPath(node); + var stat; + try { + stat = node.mount.opts.fs.lstat(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + return { + dev: stat.dev, + ino: stat.ino, + mode: stat.mode, + nlink: stat.nlink, + uid: stat.uid, + gid: stat.gid, + rdev: stat.rdev, + size: stat.size, + atime: stat.atime, + mtime: stat.mtime, + ctime: stat.ctime, + blksize: stat.blksize, + blocks: stat.blocks, + }; + }, + setattr(node, attr) { + var path = PROXYFS.realPath(node); + try { + if (attr.mode !== undefined) { + node.mount.opts.fs.chmod(path, attr.mode); + node.mode = attr.mode; + } + if (attr.atime || attr.mtime) { + var atime = new Date(attr.atime || attr.mtime); + var mtime = new Date(attr.mtime || attr.atime); + node.mount.opts.fs.utime(path, atime, mtime); + } + if (attr.size !== undefined) { + node.mount.opts.fs.truncate(path, attr.size); + } + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + lookup(parent, name) { + try { + var path = PATH.join2(PROXYFS.realPath(parent), name); + var mode = parent.mount.opts.fs.lstat(path).mode; + var node = PROXYFS.createNode(parent, name, mode); + return node; + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + mknod(parent, name, mode, dev) { + var node = PROXYFS.createNode(parent, name, mode, dev); + var path = PROXYFS.realPath(node); + try { + if (FS.isDir(node.mode)) { + node.mount.opts.fs.mkdir(path, node.mode); + } else { + node.mount.opts.fs.writeFile(path, '', { + mode: node.mode, + }); + } + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + return node; + }, + rename(oldNode, newDir, newName) { + var oldPath = PROXYFS.realPath(oldNode); + var newPath = PATH.join2(PROXYFS.realPath(newDir), newName); + try { + oldNode.mount.opts.fs.rename(oldPath, newPath); + oldNode.name = newName; + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + unlink(parent, name) { + var path = PATH.join2(PROXYFS.realPath(parent), name); + try { + parent.mount.opts.fs.unlink(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + rmdir(parent, name) { + var path = PATH.join2(PROXYFS.realPath(parent), name); + try { + parent.mount.opts.fs.rmdir(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + readdir(node) { + var path = PROXYFS.realPath(node); + try { + return node.mount.opts.fs.readdir(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + symlink(parent, newName, oldPath) { + var newPath = PATH.join2(PROXYFS.realPath(parent), newName); + try { + parent.mount.opts.fs.symlink(oldPath, newPath); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + readlink(node) { + var path = PROXYFS.realPath(node); + try { + return node.mount.opts.fs.readlink(path); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + }, + stream_ops: { + open(stream) { + var path = PROXYFS.realPath(stream.node); + try { + stream.nfd = stream.node.mount.opts.fs.open( + path, + stream.flags + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + close(stream) { + try { + stream.node.mount.opts.fs.close(stream.nfd); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + read(stream, buffer, offset, length, position) { + try { + return stream.node.mount.opts.fs.read( + stream.nfd, + buffer, + offset, + length, + position + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + write(stream, buffer, offset, length, position) { + try { + return stream.node.mount.opts.fs.write( + stream.nfd, + buffer, + offset, + length, + position + ); + } catch (e) { + if (!e.code) throw e; + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + }, + llseek(stream, offset, whence) { + var position = offset; + if (whence === 1) { + position += stream.position; + } else if (whence === 2) { + if (FS.isFile(stream.node.mode)) { + try { + var stat = stream.node.node_ops.getattr( + stream.node + ); + position += stat.size; + } catch (e) { + throw new FS.ErrnoError(ERRNO_CODES[e.code]); + } + } + } + if (position < 0) { + throw new FS.ErrnoError(ERRNO_CODES.EINVAL); + } + return position; + }, + }, + }; + var FS_createDataFile = (...args) => FS.createDataFile(...args); + var getUniqueRunDependency = (id) => id; + var FS_handledByPreloadPlugin = async (byteArray, fullname) => { + if (typeof Browser != 'undefined') Browser.init(); + for (var plugin of preloadPlugins) { + if (plugin['canHandle'](fullname)) { + return plugin['handle'](byteArray, fullname); + } + } + return byteArray; + }; + var FS_preloadFile = async ( + parent, + name, + url, + canRead, + canWrite, + dontCreateFile, + canOwn, + preFinish + ) => { + var fullname = name + ? PATH_FS.resolve(PATH.join2(parent, name)) + : parent; + var dep = getUniqueRunDependency(`cp ${fullname}`); + addRunDependency(dep); + try { + var byteArray = url; + if (typeof url == 'string') { + byteArray = await asyncLoad(url); + } + byteArray = await FS_handledByPreloadPlugin(byteArray, fullname); + preFinish?.(); + if (!dontCreateFile) { + FS_createDataFile( + parent, + name, + byteArray, + canRead, + canWrite, + canOwn + ); + } + } finally { + removeRunDependency(dep); + } + }; + var FS_createPreloadedFile = ( + parent, + name, + url, + canRead, + canWrite, + onload, + onerror, + dontCreateFile, + canOwn, + preFinish + ) => { + FS_preloadFile( + parent, + name, + url, + canRead, + canWrite, + dontCreateFile, + canOwn, + preFinish + ) + .then(onload) + .catch(onerror); + }; + var FS = { + root: null, + mounts: [], + devices: {}, + streams: [], + nextInode: 1, + nameTable: null, + currentPath: '/', + initialized: false, + ignorePermissions: true, + filesystems: null, + syncFSRequests: 0, + readFiles: {}, + ErrnoError: class { + name = 'ErrnoError'; + constructor(errno) { + this.errno = errno; + } + }, + FSStream: class { + shared = {}; + get object() { + return this.node; + } + set object(val) { + this.node = val; + } + get isRead() { + return (this.flags & 2097155) !== 1; + } + get isWrite() { + return (this.flags & 2097155) !== 0; + } + get isAppend() { + return this.flags & 1024; + } + get flags() { + return this.shared.flags; + } + set flags(val) { + this.shared.flags = val; + } + get position() { + return this.shared.position; + } + set position(val) { + this.shared.position = val; + } + }, + FSNode: class { + node_ops = {}; + stream_ops = {}; + readMode = 292 | 73; + writeMode = 146; + mounted = null; + constructor(parent, name, mode, rdev) { + if (!parent) { + parent = this; + } + this.parent = parent; + this.mount = parent.mount; + this.id = FS.nextInode++; + this.name = name; + this.mode = mode; + this.rdev = rdev; + this.atime = this.mtime = this.ctime = Date.now(); + } + get read() { + return (this.mode & this.readMode) === this.readMode; + } + set read(val) { + val + ? (this.mode |= this.readMode) + : (this.mode &= ~this.readMode); + } + get write() { + return (this.mode & this.writeMode) === this.writeMode; + } + set write(val) { + val + ? (this.mode |= this.writeMode) + : (this.mode &= ~this.writeMode); + } + get isFolder() { + return FS.isDir(this.mode); + } + get isDevice() { + return FS.isChrdev(this.mode); + } + }, + lookupPath(path, opts = {}) { + if (!path) { + throw new FS.ErrnoError(44); + } + opts.follow_mount ??= true; + if (!PATH.isAbs(path)) { + path = FS.cwd() + '/' + path; + } + linkloop: for (var nlinks = 0; nlinks < 40; nlinks++) { + var parts = path.split('/').filter((p) => !!p); + var current = FS.root; + var current_path = '/'; + for (var i = 0; i < parts.length; i++) { + var islast = i === parts.length - 1; + if (islast && opts.parent) { + break; + } + if (parts[i] === '.') { + continue; + } + if (parts[i] === '..') { + current_path = PATH.dirname(current_path); + if (FS.isRoot(current)) { + path = + current_path + + '/' + + parts.slice(i + 1).join('/'); + nlinks--; + continue linkloop; + } else { + current = current.parent; + } + continue; + } + current_path = PATH.join2(current_path, parts[i]); + try { + current = FS.lookupNode(current, parts[i]); + } catch (e) { + if (e?.errno === 44 && islast && opts.noent_okay) { + return { path: current_path }; + } + throw e; + } + if ( + FS.isMountpoint(current) && + (!islast || opts.follow_mount) + ) { + current = current.mounted.root; + } + if (FS.isLink(current.mode) && (!islast || opts.follow)) { + if (!current.node_ops.readlink) { + throw new FS.ErrnoError(52); + } + var link = current.node_ops.readlink(current); + if (!PATH.isAbs(link)) { + link = PATH.dirname(current_path) + '/' + link; + } + path = link + '/' + parts.slice(i + 1).join('/'); + continue linkloop; + } + } + return { path: current_path, node: current }; + } + throw new FS.ErrnoError(32); + }, + getPath(node) { + var path; + while (true) { + if (FS.isRoot(node)) { + var mount = node.mount.mountpoint; + if (!path) return mount; + return mount[mount.length - 1] !== '/' + ? `${mount}/${path}` + : mount + path; + } + path = path ? `${node.name}/${path}` : node.name; + node = node.parent; + } + }, + hashName(parentid, name) { + var hash = 0; + for (var i = 0; i < name.length; i++) { + hash = ((hash << 5) - hash + name.charCodeAt(i)) | 0; + } + return ((parentid + hash) >>> 0) % FS.nameTable.length; + }, + hashAddNode(node) { + var hash = FS.hashName(node.parent.id, node.name); + node.name_next = FS.nameTable[hash]; + FS.nameTable[hash] = node; + }, + hashRemoveNode(node) { + var hash = FS.hashName(node.parent.id, node.name); + if (FS.nameTable[hash] === node) { + FS.nameTable[hash] = node.name_next; + } else { + var current = FS.nameTable[hash]; + while (current) { + if (current.name_next === node) { + current.name_next = node.name_next; + break; + } + current = current.name_next; + } + } + }, + lookupNode(parent, name) { + var errCode = FS.mayLookup(parent); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + var hash = FS.hashName(parent.id, name); + for (var node = FS.nameTable[hash]; node; node = node.name_next) { + var nodeName = node.name; + if (node.parent.id === parent.id && nodeName === name) { + return node; + } + } + return FS.lookup(parent, name); + }, + createNode(parent, name, mode, rdev) { + var node = new FS.FSNode(parent, name, mode, rdev); + FS.hashAddNode(node); + return node; + }, + destroyNode(node) { + FS.hashRemoveNode(node); + }, + isRoot(node) { + return node === node.parent; + }, + isMountpoint(node) { + return !!node.mounted; + }, + isFile(mode) { + return (mode & 61440) === 32768; + }, + isDir(mode) { + return (mode & 61440) === 16384; + }, + isLink(mode) { + return (mode & 61440) === 40960; + }, + isChrdev(mode) { + return (mode & 61440) === 8192; + }, + isBlkdev(mode) { + return (mode & 61440) === 24576; + }, + isFIFO(mode) { + return (mode & 61440) === 4096; + }, + isSocket(mode) { + return (mode & 49152) === 49152; + }, + flagsToPermissionString(flag) { + var perms = ['r', 'w', 'rw'][flag & 3]; + if (flag & 512) { + perms += 'w'; + } + return perms; + }, + nodePermissions(node, perms) { + if (FS.ignorePermissions) { + return 0; + } + if (perms.includes('r') && !(node.mode & 292)) { + return 2; + } else if (perms.includes('w') && !(node.mode & 146)) { + return 2; + } else if (perms.includes('x') && !(node.mode & 73)) { + return 2; + } + return 0; + }, + mayLookup(dir) { + if (!FS.isDir(dir.mode)) return 54; + var errCode = FS.nodePermissions(dir, 'x'); + if (errCode) return errCode; + if (!dir.node_ops.lookup) return 2; + return 0; + }, + mayCreate(dir, name) { + if (!FS.isDir(dir.mode)) { + return 54; + } + try { + var node = FS.lookupNode(dir, name); + return 20; + } catch (e) {} + return FS.nodePermissions(dir, 'wx'); + }, + mayDelete(dir, name, isdir) { + var node; + try { + node = FS.lookupNode(dir, name); + } catch (e) { + return e.errno; + } + var errCode = FS.nodePermissions(dir, 'wx'); + if (errCode) { + return errCode; + } + if (isdir) { + if (!FS.isDir(node.mode)) { + return 54; + } + if (FS.isRoot(node) || FS.getPath(node) === FS.cwd()) { + return 10; + } + } else { + if (FS.isDir(node.mode)) { + return 31; + } + } + return 0; + }, + mayOpen(node, flags) { + if (!node) { + return 44; + } + if (FS.isLink(node.mode)) { + return 32; + } else if (FS.isDir(node.mode)) { + if ( + FS.flagsToPermissionString(flags) !== 'r' || + flags & (512 | 64) + ) { + return 31; + } + } + return FS.nodePermissions(node, FS.flagsToPermissionString(flags)); + }, + checkOpExists(op, err) { + if (!op) { + throw new FS.ErrnoError(err); + } + return op; + }, + MAX_OPEN_FDS: 4096, + nextfd() { + for (var fd = 0; fd <= FS.MAX_OPEN_FDS; fd++) { + if (!FS.streams[fd]) { + return fd; + } + } + throw new FS.ErrnoError(33); + }, + getStreamChecked(fd) { + var stream = FS.getStream(fd); + if (!stream) { + throw new FS.ErrnoError(8); + } + return stream; + }, + getStream: (fd) => FS.streams[fd], + createStream(stream, fd = -1) { + stream = Object.assign(new FS.FSStream(), stream); + if (fd == -1) { + fd = FS.nextfd(); + } + stream.fd = fd; + FS.streams[fd] = stream; + return stream; + }, + closeStream(fd) { + FS.streams[fd] = null; + }, + dupStream(origStream, fd = -1) { + var stream = FS.createStream(origStream, fd); + stream.stream_ops?.dup?.(stream); + return stream; + }, + doSetAttr(stream, node, attr) { + var setattr = stream?.stream_ops.setattr; + var arg = setattr ? stream : node; + setattr ??= node.node_ops.setattr; + FS.checkOpExists(setattr, 63); + setattr(arg, attr); + }, + chrdev_stream_ops: { + open(stream) { + var device = FS.getDevice(stream.node.rdev); + stream.stream_ops = device.stream_ops; + stream.stream_ops.open?.(stream); + }, + llseek() { + throw new FS.ErrnoError(70); + }, + }, + major: (dev) => dev >> 8, + minor: (dev) => dev & 255, + makedev: (ma, mi) => (ma << 8) | mi, + registerDevice(dev, ops) { + FS.devices[dev] = { stream_ops: ops }; + }, + getDevice: (dev) => FS.devices[dev], + getMounts(mount) { + var mounts = []; + var check = [mount]; + while (check.length) { + var m = check.pop(); + mounts.push(m); + check.push(...m.mounts); + } + return mounts; + }, + syncfs(populate, callback) { + if (typeof populate == 'function') { + callback = populate; + populate = false; + } + FS.syncFSRequests++; + if (FS.syncFSRequests > 1) { + err( + `warning: ${FS.syncFSRequests} FS.syncfs operations in flight at once, probably just doing extra work` + ); + } + var mounts = FS.getMounts(FS.root.mount); + var completed = 0; + function doCallback(errCode) { + FS.syncFSRequests--; + return callback(errCode); + } + function done(errCode) { + if (errCode) { + if (!done.errored) { + done.errored = true; + return doCallback(errCode); + } + return; + } + if (++completed >= mounts.length) { + doCallback(null); + } + } + for (var mount of mounts) { + if (mount.type.syncfs) { + mount.type.syncfs(mount, populate, done); + } else { + done(null); + } + } + }, + mount(type, opts, mountpoint) { + var root = mountpoint === '/'; + var pseudo = !mountpoint; + var node; + if (root && FS.root) { + throw new FS.ErrnoError(10); + } else if (!root && !pseudo) { + var lookup = FS.lookupPath(mountpoint, { follow_mount: false }); + mountpoint = lookup.path; + node = lookup.node; + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + } + var mount = { type, opts, mountpoint, mounts: [] }; + var mountRoot = type.mount(mount); + mountRoot.mount = mount; + mount.root = mountRoot; + if (root) { + FS.root = mountRoot; + } else if (node) { + node.mounted = mount; + if (node.mount) { + node.mount.mounts.push(mount); + } + } + return mountRoot; + }, + unmount(mountpoint) { + var lookup = FS.lookupPath(mountpoint, { follow_mount: false }); + if (!FS.isMountpoint(lookup.node)) { + throw new FS.ErrnoError(28); + } + var node = lookup.node; + var mount = node.mounted; + var mounts = FS.getMounts(mount); + for (var [hash, current] of Object.entries(FS.nameTable)) { + while (current) { + var next = current.name_next; + if (mounts.includes(current.mount)) { + FS.destroyNode(current); + } + current = next; + } + } + node.mounted = null; + var idx = node.mount.mounts.indexOf(mount); + node.mount.mounts.splice(idx, 1); + }, + lookup(parent, name) { + return parent.node_ops.lookup(parent, name); + }, + mknod(path, mode, dev) { + var lookup = FS.lookupPath(path, { parent: true }); + var parent = lookup.node; + var name = PATH.basename(path); + if (!name) { + throw new FS.ErrnoError(28); + } + if (name === '.' || name === '..') { + throw new FS.ErrnoError(20); + } + var errCode = FS.mayCreate(parent, name); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.mknod) { + throw new FS.ErrnoError(63); + } + return parent.node_ops.mknod(parent, name, mode, dev); + }, + statfs(path) { + return FS.statfsNode(FS.lookupPath(path, { follow: true }).node); + }, + statfsStream(stream) { + return FS.statfsNode(stream.node); + }, + statfsNode(node) { + var rtn = { + bsize: 4096, + frsize: 4096, + blocks: 1e6, + bfree: 5e5, + bavail: 5e5, + files: FS.nextInode, + ffree: FS.nextInode - 1, + fsid: 42, + flags: 2, + namelen: 255, + }; + if (node.node_ops.statfs) { + Object.assign(rtn, node.node_ops.statfs(node.mount.opts.root)); + } + return rtn; + }, + create(path, mode = 438) { + mode &= 4095; + mode |= 32768; + return FS.mknod(path, mode, 0); + }, + mkdir(path, mode = 511) { + mode &= 511 | 512; + mode |= 16384; + return FS.mknod(path, mode, 0); + }, + mkdirTree(path, mode) { + var dirs = path.split('/'); + var d = ''; + for (var dir of dirs) { + if (!dir) continue; + if (d || PATH.isAbs(path)) d += '/'; + d += dir; + try { + FS.mkdir(d, mode); + } catch (e) { + if (e.errno != 20) throw e; + } + } + }, + mkdev(path, mode, dev) { + if (typeof dev == 'undefined') { + dev = mode; + mode = 438; + } + mode |= 8192; + return FS.mknod(path, mode, dev); + }, + symlink(oldpath, newpath) { + if (!PATH_FS.resolve(oldpath)) { + throw new FS.ErrnoError(44); + } + var lookup = FS.lookupPath(newpath, { parent: true }); + var parent = lookup.node; + if (!parent) { + throw new FS.ErrnoError(44); + } + var newname = PATH.basename(newpath); + var errCode = FS.mayCreate(parent, newname); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.symlink) { + throw new FS.ErrnoError(63); + } + return parent.node_ops.symlink(parent, newname, oldpath); + }, + rename(old_path, new_path) { + var old_dirname = PATH.dirname(old_path); + var new_dirname = PATH.dirname(new_path); + var old_name = PATH.basename(old_path); + var new_name = PATH.basename(new_path); + var lookup, old_dir, new_dir; + lookup = FS.lookupPath(old_path, { parent: true }); + old_dir = lookup.node; + lookup = FS.lookupPath(new_path, { parent: true }); + new_dir = lookup.node; + if (!old_dir || !new_dir) throw new FS.ErrnoError(44); + if (old_dir.mount !== new_dir.mount) { + throw new FS.ErrnoError(75); + } + var old_node = FS.lookupNode(old_dir, old_name); + var relative = PATH_FS.relative(old_path, new_dirname); + if (relative.charAt(0) !== '.') { + throw new FS.ErrnoError(28); + } + relative = PATH_FS.relative(new_path, old_dirname); + if (relative.charAt(0) !== '.') { + throw new FS.ErrnoError(55); + } + var new_node; + try { + new_node = FS.lookupNode(new_dir, new_name); + } catch (e) {} + if (old_node === new_node) { + return; + } + var isdir = FS.isDir(old_node.mode); + var errCode = FS.mayDelete(old_dir, old_name, isdir); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + errCode = new_node + ? FS.mayDelete(new_dir, new_name, isdir) + : FS.mayCreate(new_dir, new_name); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!old_dir.node_ops.rename) { + throw new FS.ErrnoError(63); + } + if ( + FS.isMountpoint(old_node) || + (new_node && FS.isMountpoint(new_node)) + ) { + throw new FS.ErrnoError(10); + } + if (new_dir !== old_dir) { + errCode = FS.nodePermissions(old_dir, 'w'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + } + FS.hashRemoveNode(old_node); + try { + old_dir.node_ops.rename(old_node, new_dir, new_name); + old_node.parent = new_dir; + } catch (e) { + throw e; + } finally { + FS.hashAddNode(old_node); + } + }, + rmdir(path) { + var lookup = FS.lookupPath(path, { parent: true }); + var parent = lookup.node; + var name = PATH.basename(path); + var node = FS.lookupNode(parent, name); + var errCode = FS.mayDelete(parent, name, true); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.rmdir) { + throw new FS.ErrnoError(63); + } + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + parent.node_ops.rmdir(parent, name); + FS.destroyNode(node); + }, + readdir(path) { + var lookup = FS.lookupPath(path, { follow: true }); + var node = lookup.node; + var readdir = FS.checkOpExists(node.node_ops.readdir, 54); + return readdir(node); + }, + unlink(path) { + var lookup = FS.lookupPath(path, { parent: true }); + var parent = lookup.node; + if (!parent) { + throw new FS.ErrnoError(44); + } + var name = PATH.basename(path); + var node = FS.lookupNode(parent, name); + var errCode = FS.mayDelete(parent, name, false); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + if (!parent.node_ops.unlink) { + throw new FS.ErrnoError(63); + } + if (FS.isMountpoint(node)) { + throw new FS.ErrnoError(10); + } + parent.node_ops.unlink(parent, name); + FS.destroyNode(node); + }, + readlink(path) { + var lookup = FS.lookupPath(path); + var link = lookup.node; + if (!link) { + throw new FS.ErrnoError(44); + } + if (!link.node_ops.readlink) { + throw new FS.ErrnoError(28); + } + return link.node_ops.readlink(link); + }, + stat(path, dontFollow) { + var lookup = FS.lookupPath(path, { follow: !dontFollow }); + var node = lookup.node; + var getattr = FS.checkOpExists(node.node_ops.getattr, 63); + return getattr(node); + }, + fstat(fd) { + var stream = FS.getStreamChecked(fd); + var node = stream.node; + var getattr = stream.stream_ops.getattr; + var arg = getattr ? stream : node; + getattr ??= node.node_ops.getattr; + FS.checkOpExists(getattr, 63); + return getattr(arg); + }, + lstat(path) { + return FS.stat(path, true); + }, + doChmod(stream, node, mode, dontFollow) { + FS.doSetAttr(stream, node, { + mode: (mode & 4095) | (node.mode & ~4095), + ctime: Date.now(), + dontFollow, + }); + }, + chmod(path, mode, dontFollow) { + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { follow: !dontFollow }); + node = lookup.node; + } else { + node = path; + } + FS.doChmod(null, node, mode, dontFollow); + }, + lchmod(path, mode) { + FS.chmod(path, mode, true); + }, + fchmod(fd, mode) { + var stream = FS.getStreamChecked(fd); + FS.doChmod(stream, stream.node, mode, false); + }, + doChown(stream, node, dontFollow) { + FS.doSetAttr(stream, node, { timestamp: Date.now(), dontFollow }); + }, + chown(path, uid, gid, dontFollow) { + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { follow: !dontFollow }); + node = lookup.node; + } else { + node = path; + } + FS.doChown(null, node, dontFollow); + }, + lchown(path, uid, gid) { + FS.chown(path, uid, gid, true); + }, + fchown(fd, uid, gid) { + var stream = FS.getStreamChecked(fd); + FS.doChown(stream, stream.node, false); + }, + doTruncate(stream, node, len) { + if (FS.isDir(node.mode)) { + throw new FS.ErrnoError(31); + } + if (!FS.isFile(node.mode)) { + throw new FS.ErrnoError(28); + } + var errCode = FS.nodePermissions(node, 'w'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + FS.doSetAttr(stream, node, { size: len, timestamp: Date.now() }); + }, + truncate(path, len) { + if (len < 0) { + throw new FS.ErrnoError(28); + } + var node; + if (typeof path == 'string') { + var lookup = FS.lookupPath(path, { follow: true }); + node = lookup.node; + } else { + node = path; + } + FS.doTruncate(null, node, len); + }, + ftruncate(fd, len) { + var stream = FS.getStreamChecked(fd); + if (len < 0 || (stream.flags & 2097155) === 0) { + throw new FS.ErrnoError(28); + } + FS.doTruncate(stream, stream.node, len); + }, + utime(path, atime, mtime) { + var lookup = FS.lookupPath(path, { follow: true }); + var node = lookup.node; + var setattr = FS.checkOpExists(node.node_ops.setattr, 63); + setattr(node, { atime, mtime }); + }, + open(path, flags, mode = 438) { + if (path === '') { + throw new FS.ErrnoError(44); + } + flags = + typeof flags == 'string' ? FS_modeStringToFlags(flags) : flags; + if (flags & 64) { + mode = (mode & 4095) | 32768; + } else { + mode = 0; + } + var node; + var isDirPath; + if (typeof path == 'object') { + node = path; + } else { + isDirPath = path.endsWith('/'); + var lookup = FS.lookupPath(path, { + follow: !(flags & 131072), + noent_okay: true, + }); + node = lookup.node; + path = lookup.path; + } + var created = false; + if (flags & 64) { + if (node) { + if (flags & 128) { + throw new FS.ErrnoError(20); + } + } else if (isDirPath) { + throw new FS.ErrnoError(31); + } else { + node = FS.mknod(path, mode | 511, 0); + created = true; + } + } + if (!node) { + throw new FS.ErrnoError(44); + } + if (FS.isChrdev(node.mode)) { + flags &= ~512; + } + if (flags & 65536 && !FS.isDir(node.mode)) { + throw new FS.ErrnoError(54); + } + if (!created) { + var errCode = FS.mayOpen(node, flags); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + } + if (flags & 512 && !created) { + FS.truncate(node, 0); + } + flags &= ~(128 | 512 | 131072); + var stream = FS.createStream({ + node, + path: FS.getPath(node), + flags, + seekable: true, + position: 0, + stream_ops: node.stream_ops, + ungotten: [], + error: false, + }); + if (stream.stream_ops.open) { + stream.stream_ops.open(stream); + } + if (created) { + FS.chmod(node, mode & 511); + } + if (Module['logReadFiles'] && !(flags & 1)) { + if (!(path in FS.readFiles)) { + FS.readFiles[path] = 1; + } + } + return stream; + }, + close(stream) { + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if (stream.getdents) stream.getdents = null; + try { + if (stream.stream_ops.close) { + stream.stream_ops.close(stream); + } + } catch (e) { + throw e; + } finally { + FS.closeStream(stream.fd); + } + stream.fd = null; + }, + isClosed(stream) { + return stream.fd === null; + }, + llseek(stream, offset, whence) { + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if (!stream.seekable || !stream.stream_ops.llseek) { + throw new FS.ErrnoError(70); + } + if (whence != 0 && whence != 1 && whence != 2) { + throw new FS.ErrnoError(28); + } + stream.position = stream.stream_ops.llseek(stream, offset, whence); + stream.ungotten = []; + return stream.position; + }, + read(stream, buffer, offset, length, position) { + if (length < 0 || position < 0) { + throw new FS.ErrnoError(28); + } + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if ((stream.flags & 2097155) === 1) { + throw new FS.ErrnoError(8); + } + if (FS.isDir(stream.node.mode)) { + throw new FS.ErrnoError(31); + } + if (!stream.stream_ops.read) { + throw new FS.ErrnoError(28); + } + var seeking = typeof position != 'undefined'; + if (!seeking) { + position = stream.position; + } else if (!stream.seekable) { + throw new FS.ErrnoError(70); + } + var bytesRead = stream.stream_ops.read( + stream, + buffer, + offset, + length, + position + ); + if (!seeking) stream.position += bytesRead; + return bytesRead; + }, + write(stream, buffer, offset, length, position, canOwn) { + if (length < 0 || position < 0) { + throw new FS.ErrnoError(28); + } + if (FS.isClosed(stream)) { + throw new FS.ErrnoError(8); + } + if ((stream.flags & 2097155) === 0) { + throw new FS.ErrnoError(8); + } + if (FS.isDir(stream.node.mode)) { + throw new FS.ErrnoError(31); + } + if (!stream.stream_ops.write) { + throw new FS.ErrnoError(28); + } + if (stream.seekable && stream.flags & 1024) { + FS.llseek(stream, 0, 2); + } + var seeking = typeof position != 'undefined'; + if (!seeking) { + position = stream.position; + } else if (!stream.seekable) { + throw new FS.ErrnoError(70); + } + var bytesWritten = stream.stream_ops.write( + stream, + buffer, + offset, + length, + position, + canOwn + ); + if (!seeking) stream.position += bytesWritten; + return bytesWritten; + }, + mmap(stream, length, position, prot, flags) { + if ( + (prot & 2) !== 0 && + (flags & 2) === 0 && + (stream.flags & 2097155) !== 2 + ) { + throw new FS.ErrnoError(2); + } + if ((stream.flags & 2097155) === 1) { + throw new FS.ErrnoError(2); + } + if (!stream.stream_ops.mmap) { + throw new FS.ErrnoError(43); + } + if (!length) { + throw new FS.ErrnoError(28); + } + return stream.stream_ops.mmap( + stream, + length, + position, + prot, + flags + ); + }, + msync(stream, buffer, offset, length, mmapFlags) { + if (!stream.stream_ops.msync) { + return 0; + } + return stream.stream_ops.msync( + stream, + buffer, + offset, + length, + mmapFlags + ); + }, + ioctl(stream, cmd, arg) { + if (!stream.stream_ops.ioctl) { + throw new FS.ErrnoError(59); + } + return stream.stream_ops.ioctl(stream, cmd, arg); + }, + readFile(path, opts = {}) { + opts.flags = opts.flags || 0; + opts.encoding = opts.encoding || 'binary'; + if (opts.encoding !== 'utf8' && opts.encoding !== 'binary') { + abort(`Invalid encoding type "${opts.encoding}"`); + } + var stream = FS.open(path, opts.flags); + var stat = FS.stat(path); + var length = stat.size; + var buf = new Uint8Array(length); + FS.read(stream, buf, 0, length, 0); + if (opts.encoding === 'utf8') { + buf = UTF8ArrayToString(buf); + } + FS.close(stream); + return buf; + }, + writeFile(path, data, opts = {}) { + opts.flags = opts.flags || 577; + var stream = FS.open(path, opts.flags, opts.mode); + if (typeof data == 'string') { + data = new Uint8Array(intArrayFromString(data, true)); + } + if (ArrayBuffer.isView(data)) { + FS.write( + stream, + data, + 0, + data.byteLength, + undefined, + opts.canOwn + ); + } else { + abort('Unsupported data type'); + } + FS.close(stream); + }, + cwd: () => FS.currentPath, + chdir(path) { + var lookup = FS.lookupPath(path, { follow: true }); + if (lookup.node === null) { + throw new FS.ErrnoError(44); + } + if (!FS.isDir(lookup.node.mode)) { + throw new FS.ErrnoError(54); + } + var errCode = FS.nodePermissions(lookup.node, 'x'); + if (errCode) { + throw new FS.ErrnoError(errCode); + } + FS.currentPath = lookup.path; + }, + createDefaultDirectories() { + FS.mkdir('/tmp'); + FS.mkdir('/home'); + FS.mkdir('/home/web_user'); + }, + createDefaultDevices() { + FS.mkdir('/dev'); + FS.registerDevice(FS.makedev(1, 3), { + read: () => 0, + write: (stream, buffer, offset, length, pos) => length, + llseek: () => 0, + }); + FS.mkdev('/dev/null', FS.makedev(1, 3)); + TTY.register(FS.makedev(5, 0), TTY.default_tty_ops); + TTY.register(FS.makedev(6, 0), TTY.default_tty1_ops); + FS.mkdev('/dev/tty', FS.makedev(5, 0)); + FS.mkdev('/dev/tty1', FS.makedev(6, 0)); + var randomBuffer = new Uint8Array(1024), + randomLeft = 0; + var randomByte = () => { + if (randomLeft === 0) { + randomFill(randomBuffer); + randomLeft = randomBuffer.byteLength; + } + return randomBuffer[--randomLeft]; + }; + FS.createDevice('/dev', 'random', randomByte); + FS.createDevice('/dev', 'urandom', randomByte); + FS.mkdir('/dev/shm'); + FS.mkdir('/dev/shm/tmp'); + }, + createSpecialDirectories() { + FS.mkdir('/proc'); + var proc_self = FS.mkdir('/proc/self'); + FS.mkdir('/proc/self/fd'); + FS.mount( + { + mount() { + var node = FS.createNode(proc_self, 'fd', 16895, 73); + node.stream_ops = { llseek: MEMFS.stream_ops.llseek }; + node.node_ops = { + lookup(parent, name) { + var fd = +name; + var stream = FS.getStreamChecked(fd); + var ret = { + parent: null, + mount: { mountpoint: 'fake' }, + node_ops: { readlink: () => stream.path }, + id: fd + 1, + }; + ret.parent = ret; + return ret; + }, + readdir() { + return Array.from(FS.streams.entries()) + .filter(([k, v]) => v) + .map(([k, v]) => k.toString()); + }, + }; + return node; + }, + }, + {}, + '/proc/self/fd' + ); + }, + createStandardStreams(input, output, error) { + if (input) { + FS.createDevice('/dev', 'stdin', input); + } else { + FS.symlink('/dev/tty', '/dev/stdin'); + } + if (output) { + FS.createDevice('/dev', 'stdout', null, output); + } else { + FS.symlink('/dev/tty', '/dev/stdout'); + } + if (error) { + FS.createDevice('/dev', 'stderr', null, error); + } else { + FS.symlink('/dev/tty1', '/dev/stderr'); + } + var stdin = FS.open('/dev/stdin', 0); + var stdout = FS.open('/dev/stdout', 1); + var stderr = FS.open('/dev/stderr', 1); + }, + staticInit() { + FS.nameTable = new Array(4096); + FS.mount(MEMFS, {}, '/'); + FS.createDefaultDirectories(); + FS.createDefaultDevices(); + FS.createSpecialDirectories(); + FS.filesystems = { MEMFS, PROXYFS }; + }, + init(input, output, error) { + FS.initialized = true; + input ??= Module['stdin']; + output ??= Module['stdout']; + error ??= Module['stderr']; + FS.createStandardStreams(input, output, error); + }, + quit() { + FS.initialized = false; + _fflush(0); + for (var stream of FS.streams) { + if (stream) { + FS.close(stream); + } + } + }, + findObject(path, dontResolveLastLink) { + var ret = FS.analyzePath(path, dontResolveLastLink); + if (!ret.exists) { + return null; + } + return ret.object; + }, + analyzePath(path, dontResolveLastLink) { + try { + var lookup = FS.lookupPath(path, { + follow: !dontResolveLastLink, + }); + path = lookup.path; + } catch (e) {} + var ret = { + isRoot: false, + exists: false, + error: 0, + name: null, + path: null, + object: null, + parentExists: false, + parentPath: null, + parentObject: null, + }; + try { + var lookup = FS.lookupPath(path, { parent: true }); + ret.parentExists = true; + ret.parentPath = lookup.path; + ret.parentObject = lookup.node; + ret.name = PATH.basename(path); + lookup = FS.lookupPath(path, { follow: !dontResolveLastLink }); + ret.exists = true; + ret.path = lookup.path; + ret.object = lookup.node; + ret.name = lookup.node.name; + ret.isRoot = lookup.path === '/'; + } catch (e) { + ret.error = e.errno; + } + return ret; + }, + createPath(parent, path, canRead, canWrite) { + parent = typeof parent == 'string' ? parent : FS.getPath(parent); + var parts = path.split('/').reverse(); + while (parts.length) { + var part = parts.pop(); + if (!part) continue; + var current = PATH.join2(parent, part); + try { + FS.mkdir(current); + } catch (e) { + if (e.errno != 20) throw e; + } + parent = current; + } + return current; + }, + createFile(parent, name, properties, canRead, canWrite) { + var path = PATH.join2( + typeof parent == 'string' ? parent : FS.getPath(parent), + name + ); + var mode = FS_getMode(canRead, canWrite); + return FS.create(path, mode); + }, + createDataFile(parent, name, data, canRead, canWrite, canOwn) { + var path = name; + if (parent) { + parent = + typeof parent == 'string' ? parent : FS.getPath(parent); + path = name ? PATH.join2(parent, name) : parent; + } + var mode = FS_getMode(canRead, canWrite); + var node = FS.create(path, mode); + if (data) { + if (typeof data == 'string') { + var arr = new Array(data.length); + for (var i = 0, len = data.length; i < len; ++i) + arr[i] = data.charCodeAt(i); + data = arr; + } + FS.chmod(node, mode | 146); + var stream = FS.open(node, 577); + FS.write(stream, data, 0, data.length, 0, canOwn); + FS.close(stream); + FS.chmod(node, mode); + } + }, + createDevice(parent, name, input, output) { + var path = PATH.join2( + typeof parent == 'string' ? parent : FS.getPath(parent), + name + ); + var mode = FS_getMode(!!input, !!output); + FS.createDevice.major ??= 64; + var dev = FS.makedev(FS.createDevice.major++, 0); + FS.registerDevice(dev, { + open(stream) { + stream.seekable = false; + }, + close(stream) { + if (output?.buffer?.length) { + output(10); + } + }, + read(stream, buffer, offset, length, pos) { + var bytesRead = 0; + for (var i = 0; i < length; i++) { + var result; + try { + result = input(); + } catch (e) { + throw new FS.ErrnoError(29); + } + if (result === undefined && bytesRead === 0) { + throw new FS.ErrnoError(6); + } + if (result === null || result === undefined) break; + bytesRead++; + buffer[offset + i] = result; + } + if (bytesRead) { + stream.node.atime = Date.now(); + } + return bytesRead; + }, + write(stream, buffer, offset, length, pos) { + for (var i = 0; i < length; i++) { + try { + output(buffer[offset + i]); + } catch (e) { + throw new FS.ErrnoError(29); + } + } + if (length) { + stream.node.mtime = stream.node.ctime = Date.now(); + } + return i; + }, + }); + return FS.mkdev(path, mode, dev); + }, + forceLoadFile(obj) { + if (obj.isDevice || obj.isFolder || obj.link || obj.contents) + return true; + if (globalThis.XMLHttpRequest) { + abort( + 'Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread.' + ); + } else { + try { + obj.contents = readBinary(obj.url); + } catch (e) { + throw new FS.ErrnoError(29); + } + } + }, + createLazyFile(parent, name, url, canRead, canWrite) { + class LazyUint8Array { + lengthKnown = false; + chunks = []; + get(idx) { + if (idx > this.length - 1 || idx < 0) { + return undefined; + } + var chunkOffset = idx % this.chunkSize; + var chunkNum = (idx / this.chunkSize) | 0; + return this.getter(chunkNum)[chunkOffset]; + } + setDataGetter(getter) { + this.getter = getter; + } + cacheLength() { + var xhr = new XMLHttpRequest(); + xhr.open('HEAD', url, false); + xhr.send(null); + if ( + !( + (xhr.status >= 200 && xhr.status < 300) || + xhr.status === 304 + ) + ) + abort( + "Couldn't load " + url + '. Status: ' + xhr.status + ); + var datalength = Number( + xhr.getResponseHeader('Content-length') + ); + var header; + var hasByteServing = + (header = xhr.getResponseHeader('Accept-Ranges')) && + header === 'bytes'; + var usesGzip = + (header = xhr.getResponseHeader('Content-Encoding')) && + header === 'gzip'; + var chunkSize = 1024 * 1024; + if (!hasByteServing) chunkSize = datalength; + var doXHR = (from, to) => { + if (from > to) + abort( + 'invalid range (' + + from + + ', ' + + to + + ') or no bytes requested!' + ); + if (to > datalength - 1) + abort( + 'only ' + + datalength + + ' bytes available! programmer error!' + ); + var xhr = new XMLHttpRequest(); + xhr.open('GET', url, false); + if (datalength !== chunkSize) + xhr.setRequestHeader( + 'Range', + 'bytes=' + from + '-' + to + ); + xhr.responseType = 'arraybuffer'; + if (xhr.overrideMimeType) { + xhr.overrideMimeType( + 'text/plain; charset=x-user-defined' + ); + } + xhr.send(null); + if ( + !( + (xhr.status >= 200 && xhr.status < 300) || + xhr.status === 304 + ) + ) + abort( + "Couldn't load " + + url + + '. Status: ' + + xhr.status + ); + if (xhr.response !== undefined) { + return new Uint8Array(xhr.response || []); + } + return intArrayFromString(xhr.responseText || '', true); + }; + var lazyArray = this; + lazyArray.setDataGetter((chunkNum) => { + var start = chunkNum * chunkSize; + var end = (chunkNum + 1) * chunkSize - 1; + end = Math.min(end, datalength - 1); + if (typeof lazyArray.chunks[chunkNum] == 'undefined') { + lazyArray.chunks[chunkNum] = doXHR(start, end); + } + if (typeof lazyArray.chunks[chunkNum] == 'undefined') + abort('doXHR failed!'); + return lazyArray.chunks[chunkNum]; + }); + if (usesGzip || !datalength) { + chunkSize = datalength = 1; + datalength = this.getter(0).length; + chunkSize = datalength; + out( + 'LazyFiles on gzip forces download of the whole file when length is accessed' + ); + } + this._length = datalength; + this._chunkSize = chunkSize; + this.lengthKnown = true; + } + get length() { + if (!this.lengthKnown) { + this.cacheLength(); + } + return this._length; + } + get chunkSize() { + if (!this.lengthKnown) { + this.cacheLength(); + } + return this._chunkSize; + } + } + if (globalThis.XMLHttpRequest) { + if (!ENVIRONMENT_IS_WORKER) + abort( + 'Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc' + ); + var lazyArray = new LazyUint8Array(); + var properties = { isDevice: false, contents: lazyArray }; + } else { + var properties = { isDevice: false, url }; + } + var node = FS.createFile( + parent, + name, + properties, + canRead, + canWrite + ); + if (properties.contents) { + node.contents = properties.contents; + } else if (properties.url) { + node.contents = null; + node.url = properties.url; + } + Object.defineProperties(node, { + usedBytes: { + get: function () { + return this.contents.length; + }, + }, + }); + var stream_ops = {}; + for (const [key, fn] of Object.entries(node.stream_ops)) { + stream_ops[key] = (...args) => { + FS.forceLoadFile(node); + return fn(...args); + }; + } + function writeChunks(stream, buffer, offset, length, position) { + var contents = stream.node.contents; + if (position >= contents.length) return 0; + var size = Math.min(contents.length - position, length); + if (contents.slice) { + for (var i = 0; i < size; i++) { + buffer[offset + i] = contents[position + i]; + } + } else { + for (var i = 0; i < size; i++) { + buffer[offset + i] = contents.get(position + i); + } + } + return size; + } + stream_ops.read = (stream, buffer, offset, length, position) => { + FS.forceLoadFile(node); + return writeChunks(stream, buffer, offset, length, position); + }; + stream_ops.mmap = (stream, length, position, prot, flags) => { + FS.forceLoadFile(node); + var ptr = mmapAlloc(length); + if (!ptr) { + throw new FS.ErrnoError(48); + } + writeChunks(stream, HEAP8, ptr, length, position); + return { ptr, allocated: true }; + }; + node.stream_ops = stream_ops; + return node; + }, + }; + var findLibraryFS = (libName, rpath) => { + if (!runtimeInitialized) { + return undefined; + } + if (PATH.isAbs(libName)) { + try { + FS.lookupPath(libName); + return libName; + } catch (e) { + return undefined; + } + } + var rpathResolved = (rpath?.paths || []).map((p) => + replaceORIGIN(rpath?.parentLibPath, p) + ); + return withStackSave(() => { + var bufSize = 2 * 255 + 2; + var buf = stackAlloc(bufSize); + var rpathC = stringToUTF8OnStack(rpathResolved.join(':')); + var libNameC = stringToUTF8OnStack(libName); + var resLibNameC = __emscripten_find_dylib( + buf, + rpathC, + libNameC, + bufSize + ); + return resLibNameC ? UTF8ToString(resLibNameC) : undefined; + }); + }; + function loadDynamicLibrary( + libName, + flags = { global: true, nodelete: true }, + localScope, + handle + ) { + var dso = LDSO.loadedLibsByName[libName]; + if (dso) { + if (!flags.global) { + if (localScope) { + Object.assign(localScope, dso.exports); + } + } else if (!dso.global) { + dso.global = true; + mergeLibSymbols(dso.exports, libName); + } + if (flags.nodelete && dso.refcount !== Infinity) { + dso.refcount = Infinity; + } + dso.refcount++; + if (handle) { + LDSO.loadedLibsByHandle[handle] = dso; + } + return flags.loadAsync ? Promise.resolve(true) : true; + } + dso = newDSO(libName, handle, 'loading'); + dso.refcount = flags.nodelete ? Infinity : 1; + dso.global = flags.global; + function loadLibData() { + if (handle) { + var data = HEAPU32[(handle + 28) >> 2]; + var dataSize = HEAPU32[(handle + 32) >> 2]; + if (data && dataSize) { + var libData = HEAP8.slice(data, data + dataSize); + return flags.loadAsync ? Promise.resolve(libData) : libData; + } + } + var f = findLibraryFS(libName, flags.rpath); + if (f) { + var libData = FS.readFile(f, { encoding: 'binary' }); + return flags.loadAsync ? Promise.resolve(libData) : libData; + } + var libFile = locateFile(libName); + if (flags.loadAsync) { + return asyncLoad(libFile); + } + if (!readBinary) { + throw new Error( + `${libFile}: file not found, and synchronous loading of external files is not available` + ); + } + return readBinary(libFile); + } + function getExports() { + var preloaded = preloadedWasm[libName]; + if (preloaded) { + return flags.loadAsync ? Promise.resolve(preloaded) : preloaded; + } + if (flags.loadAsync) { + return loadLibData().then((libData) => + loadWebAssemblyModule( + libData, + flags, + libName, + localScope, + handle + ) + ); + } + return loadWebAssemblyModule( + loadLibData(), + flags, + libName, + localScope, + handle + ); + } + function moduleLoaded(exports) { + if (dso.global) { + mergeLibSymbols(exports, libName); + } else if (localScope) { + Object.assign(localScope, exports); + } + dso.exports = exports; + } + if (flags.loadAsync) { + return getExports().then((exports) => { + moduleLoaded(exports); + return true; + }); + } + moduleLoaded(getExports()); + return true; + } + var reportUndefinedSymbols = () => { + for (var [symName, entry] of Object.entries(GOT)) { + if (entry.value == -1) { + var value = resolveGlobalSymbol(symName, true).sym; + if (!value && !entry.required) { + entry.value = 0; + continue; + } + if (typeof value == 'function') { + entry.value = addFunction(value, value.sig); + } else if (typeof value == 'number') { + entry.value = value; + } else { + throw new Error( + `bad export type for '${symName}': ${typeof value} (${value})` + ); + } + } + } + }; + var loadDylibs = async () => { + if (!dynamicLibraries.length) { + reportUndefinedSymbols(); + return; + } + addRunDependency('loadDylibs'); + for (var lib of dynamicLibraries) { + await loadDynamicLibrary(lib, { + loadAsync: true, + global: true, + nodelete: true, + allowUndefined: true, + }); + } + reportUndefinedSymbols(); + removeRunDependency('loadDylibs'); + }; + var noExitRuntime = false; + var ___assert_fail = (condition, filename, line, func) => + abort( + `Assertion failed: ${UTF8ToString(condition)}, at: ` + + [ + filename ? UTF8ToString(filename) : 'unknown filename', + line, + func ? UTF8ToString(func) : 'unknown function', + ] + ); + ___assert_fail.sig = 'vppip'; + var ___call_sighandler = (fp, sig) => getWasmTableEntry(fp)(sig); + ___call_sighandler.sig = 'vpi'; + var SOCKFS = { + websocketArgs: {}, + callbacks: {}, + on(event, callback) { + SOCKFS.callbacks[event] = callback; + }, + emit(event, param) { + SOCKFS.callbacks[event]?.(param); + }, + mount(mount) { + SOCKFS.websocketArgs = Module['websocket'] || {}; + (Module['websocket'] ??= {})['on'] = SOCKFS.on; + return FS.createNode(null, '/', 16895, 0); + }, + createSocket(family, type, protocol) { + if (family != 2) { + throw new FS.ErrnoError(5); + } + type &= ~526336; + if (type != 1 && type != 2) { + throw new FS.ErrnoError(28); + } + var streaming = type == 1; + if (streaming && protocol && protocol != 6) { + throw new FS.ErrnoError(66); + } + var sock = { + family, + type, + protocol, + server: null, + error: null, + peers: {}, + pending: [], + recv_queue: [], + sock_ops: SOCKFS.websocket_sock_ops, + }; + var name = SOCKFS.nextname(); + var node = FS.createNode(SOCKFS.root, name, 49152, 0); + node.sock = sock; + var stream = FS.createStream({ + path: name, + node, + flags: 2, + seekable: false, + stream_ops: SOCKFS.stream_ops, + }); + sock.stream = stream; + return sock; + }, + getSocket(fd) { + var stream = FS.getStream(fd); + if (!stream || !FS.isSocket(stream.node.mode)) { + return null; + } + return stream.node.sock; + }, + stream_ops: { + poll(stream) { + var sock = stream.node.sock; + return sock.sock_ops.poll(sock); + }, + ioctl(stream, request, varargs) { + var sock = stream.node.sock; + return sock.sock_ops.ioctl(sock, request, varargs); + }, + read(stream, buffer, offset, length, position) { + var sock = stream.node.sock; + var msg = sock.sock_ops.recvmsg(sock, length); + if (!msg) { + return 0; + } + buffer.set(msg.buffer, offset); + return msg.buffer.length; + }, + write(stream, buffer, offset, length, position) { + var sock = stream.node.sock; + return sock.sock_ops.sendmsg(sock, buffer, offset, length); + }, + close(stream) { + var sock = stream.node.sock; + sock.sock_ops.close(sock); + }, + }, + nextname() { + if (!SOCKFS.nextname.current) { + SOCKFS.nextname.current = 0; + } + return `socket[${SOCKFS.nextname.current++}]`; + }, + websocket_sock_ops: { + createPeer(sock, addr, port) { + var ws; + if (typeof addr == 'object') { + ws = addr; + addr = null; + port = null; + } + if (ws) { + if (ws._socket) { + addr = ws._socket.remoteAddress; + port = ws._socket.remotePort; + } else { + var result = /ws[s]?:\/\/([^:]+):(\d+)/.exec(ws.url); + if (!result) { + throw new Error( + 'WebSocket URL must be in the format ws(s)://address:port' + ); + } + addr = result[1]; + port = parseInt(result[2], 10); + } + } else { + try { + var url = 'ws://'.replace('#', '//'); + var subProtocols = 'binary'; + var opts = undefined; + if ('function' === typeof SOCKFS.websocketArgs['url']) { + url = SOCKFS.websocketArgs['url'](...arguments); + } else if ( + 'string' === typeof SOCKFS.websocketArgs['url'] + ) { + url = SOCKFS.websocketArgs['url']; + } + if (SOCKFS.websocketArgs['subprotocol']) { + subProtocols = SOCKFS.websocketArgs['subprotocol']; + } else if ( + SOCKFS.websocketArgs['subprotocol'] === null + ) { + subProtocols = 'null'; + } + if (url === 'ws://' || url === 'wss://') { + var parts = addr.split('/'); + url = + url + + parts[0] + + ':' + + port + + '/' + + parts.slice(1).join('/'); + } + if (subProtocols !== 'null') { + subProtocols = subProtocols + .replace(/^ +| +$/g, '') + .split(/ *, */); + opts = subProtocols; + } + var WebSocketConstructor; + { + WebSocketConstructor = WebSocket; + } + if (Module['websocket']['decorator']) { + WebSocketConstructor = + Module['websocket']['decorator']( + WebSocketConstructor + ); + } + ws = new WebSocketConstructor(url, opts); + ws.binaryType = 'arraybuffer'; + } catch (e) { + throw new FS.ErrnoError(23); + } + } + var peer = { addr, port, socket: ws, msg_send_queue: [] }; + SOCKFS.websocket_sock_ops.addPeer(sock, peer); + SOCKFS.websocket_sock_ops.handlePeerEvents(sock, peer); + if (sock.type === 2 && typeof sock.sport != 'undefined') { + peer.msg_send_queue.push( + new Uint8Array([ + 255, + 255, + 255, + 255, + 'p'.charCodeAt(0), + 'o'.charCodeAt(0), + 'r'.charCodeAt(0), + 't'.charCodeAt(0), + (sock.sport & 65280) >> 8, + sock.sport & 255, + ]) + ); + } + return peer; + }, + getPeer(sock, addr, port) { + return sock.peers[addr + ':' + port]; + }, + addPeer(sock, peer) { + sock.peers[peer.addr + ':' + peer.port] = peer; + }, + removePeer(sock, peer) { + delete sock.peers[peer.addr + ':' + peer.port]; + }, + handlePeerEvents(sock, peer) { + var first = true; + var handleOpen = function () { + sock.connecting = false; + SOCKFS.emit('open', sock.stream.fd); + try { + var queued = peer.msg_send_queue.shift(); + while (queued) { + peer.socket.send(queued); + queued = peer.msg_send_queue.shift(); + } + } catch (e) { + peer.socket.close(); + } + }; + function handleMessage(data) { + if (typeof data == 'string') { + var encoder = new TextEncoder(); + data = encoder.encode(data); + } else { + if (data.byteLength == 0) { + return; + } + data = new Uint8Array(data); + } + var wasfirst = first; + first = false; + if ( + wasfirst && + data.length === 10 && + data[0] === 255 && + data[1] === 255 && + data[2] === 255 && + data[3] === 255 && + data[4] === 'p'.charCodeAt(0) && + data[5] === 'o'.charCodeAt(0) && + data[6] === 'r'.charCodeAt(0) && + data[7] === 't'.charCodeAt(0) + ) { + var newport = (data[8] << 8) | data[9]; + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + peer.port = newport; + SOCKFS.websocket_sock_ops.addPeer(sock, peer); + return; + } + sock.recv_queue.push({ + addr: peer.addr, + port: peer.port, + data, + }); + SOCKFS.emit('message', sock.stream.fd); + } + if (ENVIRONMENT_IS_NODE) { + peer.socket.on('open', handleOpen); + peer.socket.on('message', function (data, isBinary) { + if (!isBinary) { + return; + } + handleMessage(new Uint8Array(data).buffer); + }); + peer.socket.on('close', function () { + SOCKFS.emit('close', sock.stream.fd); + }); + peer.socket.on('error', function (error) { + sock.error = 14; + SOCKFS.emit('error', [ + sock.stream.fd, + sock.error, + 'ECONNREFUSED: Connection refused', + ]); + }); + } else { + peer.socket.onopen = handleOpen; + peer.socket.onclose = function () { + SOCKFS.emit('close', sock.stream.fd); + }; + peer.socket.onmessage = function peer_socket_onmessage( + event + ) { + handleMessage(event.data); + }; + peer.socket.onerror = function (error) { + sock.error = 14; + SOCKFS.emit('error', [ + sock.stream.fd, + sock.error, + 'ECONNREFUSED: Connection refused', + ]); + }; + } + }, + poll(sock) { + if (sock.type === 1 && sock.server) { + return sock.pending.length ? 64 | 1 : 0; + } + var mask = 0; + var dest = + sock.type === 1 + ? SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ) + : null; + if ( + sock.recv_queue.length || + !dest || + (dest && dest.socket.readyState === dest.socket.CLOSING) || + (dest && dest.socket.readyState === dest.socket.CLOSED) + ) { + mask |= 64 | 1; + } + if ( + !dest || + (dest && dest.socket.readyState === dest.socket.OPEN) + ) { + mask |= 4; + } + if ( + (dest && dest.socket.readyState === dest.socket.CLOSING) || + (dest && dest.socket.readyState === dest.socket.CLOSED) + ) { + if (sock.connecting) { + mask |= 4; + } else { + mask |= 16; + } + } + return mask; + }, + ioctl(sock, request, arg) { + switch (request) { + case 21531: + var bytes = 0; + if (sock.recv_queue.length) { + bytes = sock.recv_queue[0].data.length; + } + HEAP32[arg >> 2] = bytes; + return 0; + case 21537: + var on = HEAP32[arg >> 2]; + if (on) { + sock.stream.flags |= 2048; + } else { + sock.stream.flags &= ~2048; + } + return 0; + default: + return 28; + } + }, + close(sock) { + if (sock.server) { + try { + sock.server.close(); + } catch (e) {} + sock.server = null; + } + for (var peer of Object.values(sock.peers)) { + try { + peer.socket.close(); + } catch (e) {} + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + } + return 0; + }, + bind(sock, addr, port) { + if ( + typeof sock.saddr != 'undefined' || + typeof sock.sport != 'undefined' + ) { + throw new FS.ErrnoError(28); + } + sock.saddr = addr; + sock.sport = port; + if (sock.type === 2) { + if (sock.server) { + sock.server.close(); + sock.server = null; + } + try { + sock.sock_ops.listen(sock, 0); + } catch (e) { + if (!(e.name === 'ErrnoError')) throw e; + if (e.errno !== 138) throw e; + } + } + }, + connect(sock, addr, port) { + if (sock.server) { + throw new FS.ErrnoError(138); + } + if ( + typeof sock.daddr != 'undefined' && + typeof sock.dport != 'undefined' + ) { + var dest = SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ); + if (dest) { + if (dest.socket.readyState === dest.socket.CONNECTING) { + throw new FS.ErrnoError(7); + } else { + throw new FS.ErrnoError(30); + } + } + } + var peer = SOCKFS.websocket_sock_ops.createPeer( + sock, + addr, + port + ); + sock.daddr = peer.addr; + sock.dport = peer.port; + sock.connecting = true; + }, + listen(sock, backlog) { + if (!ENVIRONMENT_IS_NODE) { + throw new FS.ErrnoError(138); + } + }, + accept(listensock) { + if (!listensock.server || !listensock.pending.length) { + throw new FS.ErrnoError(28); + } + var newsock = listensock.pending.shift(); + newsock.stream.flags = listensock.stream.flags; + return newsock; + }, + getname(sock, peer) { + var addr, port; + if (peer) { + if (sock.daddr === undefined || sock.dport === undefined) { + throw new FS.ErrnoError(53); + } + addr = sock.daddr; + port = sock.dport; + } else { + addr = sock.saddr || 0; + port = sock.sport || 0; + } + return { addr, port }; + }, + sendmsg(sock, buffer, offset, length, addr, port) { + if (sock.type === 2) { + if (addr === undefined || port === undefined) { + addr = sock.daddr; + port = sock.dport; + } + if (addr === undefined || port === undefined) { + throw new FS.ErrnoError(17); + } + } else { + addr = sock.daddr; + port = sock.dport; + } + var dest = SOCKFS.websocket_sock_ops.getPeer(sock, addr, port); + if (sock.type === 1) { + if ( + !dest || + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + throw new FS.ErrnoError(53); + } + } + if (ArrayBuffer.isView(buffer)) { + offset += buffer.byteOffset; + buffer = buffer.buffer; + } + var data = buffer.slice(offset, offset + length); + if (!dest || dest.socket.readyState !== dest.socket.OPEN) { + if (sock.type === 2) { + if ( + !dest || + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + dest = SOCKFS.websocket_sock_ops.createPeer( + sock, + addr, + port + ); + } + } + dest.msg_send_queue.push(data); + return length; + } + try { + dest.socket.send(data); + return length; + } catch (e) { + throw new FS.ErrnoError(28); + } + }, + recvmsg(sock, length, flags) { + if (sock.type === 1 && sock.server) { + throw new FS.ErrnoError(53); + } + var queued = sock.recv_queue.shift(); + if (!queued) { + if (sock.type === 1) { + var dest = SOCKFS.websocket_sock_ops.getPeer( + sock, + sock.daddr, + sock.dport + ); + if (!dest) { + throw new FS.ErrnoError(53); + } + if ( + dest.socket.readyState === dest.socket.CLOSING || + dest.socket.readyState === dest.socket.CLOSED + ) { + return null; + } + throw new FS.ErrnoError(6); + } + throw new FS.ErrnoError(6); + } + var queuedLength = queued.data.byteLength || queued.data.length; + var queuedOffset = queued.data.byteOffset || 0; + var queuedBuffer = queued.data.buffer || queued.data; + var bytesRead = Math.min(length, queuedLength); + var res = { + buffer: new Uint8Array( + queuedBuffer, + queuedOffset, + bytesRead + ), + addr: queued.addr, + port: queued.port, + }; + if (flags & 2) { + bytesRead = 0; + } + if (sock.type === 1 && bytesRead < queuedLength) { + var bytesRemaining = queuedLength - bytesRead; + queued.data = new Uint8Array( + queuedBuffer, + queuedOffset + bytesRead, + bytesRemaining + ); + sock.recv_queue.unshift(queued); + } + return res; + }, + }, + }; + var getSocketFromFD = (fd) => { + var socket = SOCKFS.getSocket(fd); + if (!socket) throw new FS.ErrnoError(8); + return socket; + }; + var inetPton4 = (str) => { + var b = str.split('.'); + for (var i = 0; i < 4; i++) { + var tmp = Number(b[i]); + if (isNaN(tmp)) return null; + b[i] = tmp; + } + return (b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24)) >>> 0; + }; + var inetPton6 = (str) => { + var words; + var w, offset, z; + var valid6regx = + /^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i; + var parts = []; + if (!valid6regx.test(str)) { + return null; + } + if (str === '::') { + return [0, 0, 0, 0, 0, 0, 0, 0]; + } + if (str.startsWith('::')) { + str = str.replace('::', 'Z:'); + } else { + str = str.replace('::', ':Z:'); + } + if (str.indexOf('.') > 0) { + str = str.replace(new RegExp('[.]', 'g'), ':'); + words = str.split(':'); + words[words.length - 4] = + Number(words[words.length - 4]) + + Number(words[words.length - 3]) * 256; + words[words.length - 3] = + Number(words[words.length - 2]) + + Number(words[words.length - 1]) * 256; + words = words.slice(0, words.length - 2); + } else { + words = str.split(':'); + } + offset = 0; + z = 0; + for (w = 0; w < words.length; w++) { + if (typeof words[w] == 'string') { + if (words[w] === 'Z') { + for (z = 0; z < 8 - words.length + 1; z++) { + parts[w + z] = 0; + } + offset = z - 1; + } else { + parts[w + offset] = _htons(parseInt(words[w], 16)); + } + } else { + parts[w + offset] = words[w]; + } + } + return [ + (parts[1] << 16) | parts[0], + (parts[3] << 16) | parts[2], + (parts[5] << 16) | parts[4], + (parts[7] << 16) | parts[6], + ]; + }; + var writeSockaddr = (sa, family, addr, port, addrlen) => { + switch (family) { + case 2: + addr = inetPton4(addr); + zeroMemory(sa, 16); + if (addrlen) { + HEAP32[addrlen >> 2] = 16; + } + HEAP16[sa >> 1] = family; + HEAP32[(sa + 4) >> 2] = addr; + HEAP16[(sa + 2) >> 1] = _htons(port); + break; + case 10: + addr = inetPton6(addr); + zeroMemory(sa, 28); + if (addrlen) { + HEAP32[addrlen >> 2] = 28; + } + HEAP32[sa >> 2] = family; + HEAP32[(sa + 8) >> 2] = addr[0]; + HEAP32[(sa + 12) >> 2] = addr[1]; + HEAP32[(sa + 16) >> 2] = addr[2]; + HEAP32[(sa + 20) >> 2] = addr[3]; + HEAP16[(sa + 2) >> 1] = _htons(port); + break; + default: + return 5; + } + return 0; + }; + var DNS = { + address_map: { id: 1, addrs: {}, names: {} }, + lookup_name(name) { + var res = inetPton4(name); + if (res !== null) { + return name; + } + res = inetPton6(name); + if (res !== null) { + return name; + } + var addr; + if (DNS.address_map.addrs[name]) { + addr = DNS.address_map.addrs[name]; + } else { + var id = DNS.address_map.id++; + addr = '172.29.' + (id & 255) + '.' + (id & 65280); + DNS.address_map.names[addr] = name; + DNS.address_map.addrs[name] = addr; + } + return addr; + }, + lookup_addr(addr) { + if (DNS.address_map.names[addr]) { + return DNS.address_map.names[addr]; + } + return null; + }, + }; + function ___syscall_accept4(fd, addr, addrlen, flags, d1, d2) { + try { + var sock = getSocketFromFD(fd); + var newsock = sock.sock_ops.accept(sock); + if (addr) { + var errno = writeSockaddr( + addr, + newsock.family, + DNS.lookup_name(newsock.daddr), + newsock.dport, + addrlen + ); + } + return newsock.stream.fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_accept4.sig = 'iippiii'; + var inetNtop4 = (addr) => + (addr & 255) + + '.' + + ((addr >> 8) & 255) + + '.' + + ((addr >> 16) & 255) + + '.' + + ((addr >> 24) & 255); + var inetNtop6 = (ints) => { + var str = ''; + var word = 0; + var longest = 0; + var lastzero = 0; + var zstart = 0; + var len = 0; + var i = 0; + var parts = [ + ints[0] & 65535, + ints[0] >> 16, + ints[1] & 65535, + ints[1] >> 16, + ints[2] & 65535, + ints[2] >> 16, + ints[3] & 65535, + ints[3] >> 16, + ]; + var hasipv4 = true; + var v4part = ''; + for (i = 0; i < 5; i++) { + if (parts[i] !== 0) { + hasipv4 = false; + break; + } + } + if (hasipv4) { + v4part = inetNtop4(parts[6] | (parts[7] << 16)); + if (parts[5] === -1) { + str = '::ffff:'; + str += v4part; + return str; + } + if (parts[5] === 0) { + str = '::'; + if (v4part === '0.0.0.0') v4part = ''; + if (v4part === '0.0.0.1') v4part = '1'; + str += v4part; + return str; + } + } + for (word = 0; word < 8; word++) { + if (parts[word] === 0) { + if (word - lastzero > 1) { + len = 0; + } + lastzero = word; + len++; + } + if (len > longest) { + longest = len; + zstart = word - longest + 1; + } + } + for (word = 0; word < 8; word++) { + if (longest > 1) { + if ( + parts[word] === 0 && + word >= zstart && + word < zstart + longest + ) { + if (word === zstart) { + str += ':'; + if (zstart === 0) str += ':'; + } + continue; + } + } + str += Number(_ntohs(parts[word] & 65535)).toString(16); + str += word < 7 ? ':' : ''; + } + return str; + }; + var readSockaddr = (sa, salen) => { + var family = HEAP16[sa >> 1]; + var port = _ntohs(HEAPU16[(sa + 2) >> 1]); + var addr; + switch (family) { + case 2: + if (salen !== 16) { + return { errno: 28 }; + } + addr = HEAP32[(sa + 4) >> 2]; + addr = inetNtop4(addr); + break; + case 10: + if (salen !== 28) { + return { errno: 28 }; + } + addr = [ + HEAP32[(sa + 8) >> 2], + HEAP32[(sa + 12) >> 2], + HEAP32[(sa + 16) >> 2], + HEAP32[(sa + 20) >> 2], + ]; + addr = inetNtop6(addr); + break; + default: + return { errno: 5 }; + } + return { family, addr, port }; + }; + var getSocketAddress = (addrp, addrlen) => { + var info = readSockaddr(addrp, addrlen); + if (info.errno) throw new FS.ErrnoError(info.errno); + info.addr = DNS.lookup_addr(info.addr) || info.addr; + return info; + }; + function ___syscall_bind(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + var info = getSocketAddress(addr, addrlen); + sock.sock_ops.bind(sock, info.addr, info.port); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_bind.sig = 'iippiii'; + var SYSCALLS = { + DEFAULT_POLLMASK: 5, + calculateAt(dirfd, path, allowEmpty) { + if (PATH.isAbs(path)) { + return path; + } + var dir; + if (dirfd === -100) { + dir = FS.cwd(); + } else { + var dirstream = SYSCALLS.getStreamFromFD(dirfd); + dir = dirstream.path; + } + if (path.length == 0) { + if (!allowEmpty) { + throw new FS.ErrnoError(44); + } + return dir; + } + return dir + '/' + path; + }, + writeStat(buf, stat) { + HEAPU32[buf >> 2] = stat.dev; + HEAPU32[(buf + 4) >> 2] = stat.mode; + HEAPU32[(buf + 8) >> 2] = stat.nlink; + HEAPU32[(buf + 12) >> 2] = stat.uid; + HEAPU32[(buf + 16) >> 2] = stat.gid; + HEAPU32[(buf + 20) >> 2] = stat.rdev; + HEAP64[(buf + 24) >> 3] = BigInt(stat.size); + HEAP32[(buf + 32) >> 2] = 4096; + HEAP32[(buf + 36) >> 2] = stat.blocks; + var atime = stat.atime.getTime(); + var mtime = stat.mtime.getTime(); + var ctime = stat.ctime.getTime(); + HEAP64[(buf + 40) >> 3] = BigInt(Math.floor(atime / 1e3)); + HEAPU32[(buf + 48) >> 2] = (atime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 56) >> 3] = BigInt(Math.floor(mtime / 1e3)); + HEAPU32[(buf + 64) >> 2] = (mtime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 72) >> 3] = BigInt(Math.floor(ctime / 1e3)); + HEAPU32[(buf + 80) >> 2] = (ctime % 1e3) * 1e3 * 1e3; + HEAP64[(buf + 88) >> 3] = BigInt(stat.ino); + return 0; + }, + writeStatFs(buf, stats) { + HEAPU32[(buf + 4) >> 2] = stats.bsize; + HEAPU32[(buf + 60) >> 2] = stats.bsize; + HEAP64[(buf + 8) >> 3] = BigInt(stats.blocks); + HEAP64[(buf + 16) >> 3] = BigInt(stats.bfree); + HEAP64[(buf + 24) >> 3] = BigInt(stats.bavail); + HEAP64[(buf + 32) >> 3] = BigInt(stats.files); + HEAP64[(buf + 40) >> 3] = BigInt(stats.ffree); + HEAPU32[(buf + 48) >> 2] = stats.fsid; + HEAPU32[(buf + 64) >> 2] = stats.flags; + HEAPU32[(buf + 56) >> 2] = stats.namelen; + }, + doMsync(addr, stream, len, flags, offset) { + if (!FS.isFile(stream.node.mode)) { + throw new FS.ErrnoError(43); + } + if (flags & 2) { + return 0; + } + var buffer = HEAPU8.slice(addr, addr + len); + FS.msync(stream, buffer, offset, len, flags); + }, + getStreamFromFD(fd) { + var stream = FS.getStreamChecked(fd); + return stream; + }, + varargs: undefined, + getStr(ptr) { + var ret = UTF8ToString(ptr); + return ret; + }, + }; + function ___syscall_chdir(path) { + try { + path = SYSCALLS.getStr(path); + FS.chdir(path); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_chdir.sig = 'ip'; + function ___syscall_chmod(path, mode) { + try { + path = SYSCALLS.getStr(path); + FS.chmod(path, mode); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_chmod.sig = 'ipi'; + var allocateUTF8OnStack = (...args) => stringToUTF8OnStack(...args); + var onInits = []; + var addOnInit = (cb) => onInits.push(cb); + function _js_getpid() { + return PHPLoader.processId ?? 42; + } + function _js_wasm_trace(format, ...args) { + if (PHPLoader.trace instanceof Function) { + PHPLoader.trace(_js_getpid(), format, ...args); + } + } + var PHPWASM = { + O_APPEND: 1024, + O_NONBLOCK: 2048, + POLLHUP: 16, + SETFL_MASK: 3072, + init: function () { + if (PHPLoader.bindUserSpace) { + addOnInit(() => { + if (typeof PHPLoader.processId !== 'number') { + throw new Error( + 'PHPLoader.processId must be set before init' + ); + } + Module['userSpace'] = PHPLoader.bindUserSpace({ + pid: PHPLoader.processId, + constants: { + F_GETFL: Number('3'), + O_ACCMODE: Number('2097155'), + O_RDONLY: Number('0'), + O_WRONLY: Number('1'), + O_APPEND: Number('1024'), + O_NONBLOCK: Number('2048'), + F_SETFL: Number('4'), + F_GETLK: Number('12'), + F_SETLK: Number('13'), + F_SETLKW: Number('14'), + SEEK_SET: Number('0'), + SEEK_CUR: Number('1'), + SEEK_END: Number('2'), + F_GETFL: Number('3'), + O_ACCMODE: Number('2097155'), + O_RDONLY: Number('0'), + O_WRONLY: Number('1'), + O_APPEND: Number('1024'), + O_NONBLOCK: Number('2048'), + F_SETFL: Number('4'), + F_GETLK: Number('12'), + F_SETLK: Number('13'), + F_SETLKW: Number('14'), + SEEK_SET: Number('0'), + SEEK_CUR: Number('1'), + SEEK_END: Number('2'), + F_RDLCK: 0, + F_WRLCK: 1, + F_UNLCK: 2, + LOCK_SH: 1, + LOCK_EX: 2, + LOCK_NB: 4, + LOCK_UN: 8, + }, + errnoCodes: ERRNO_CODES, + memory: { + HEAP8: { + get(offset) { + return HEAP8[offset]; + }, + set(offset, value) { + HEAP8[offset] = value; + }, + }, + HEAPU8: { + get(offset) { + return HEAPU8[offset]; + }, + set(offset, value) { + HEAPU8[offset] = value; + }, + }, + HEAP16: { + get(offset) { + return HEAP16[offset]; + }, + set(offset, value) { + HEAP16[offset] = value; + }, + }, + HEAPU16: { + get(offset) { + return HEAPU16[offset]; + }, + set(offset, value) { + HEAPU16[offset] = value; + }, + }, + HEAP32: { + get(offset) { + return HEAP32[offset]; + }, + set(offset, value) { + HEAP32[offset] = value; + }, + }, + HEAPU32: { + get(offset) { + return HEAPU32[offset]; + }, + set(offset, value) { + HEAPU32[offset] = value; + }, + }, + HEAPF32: { + get(offset) { + return HEAPF32[offset]; + }, + set(offset, value) { + HEAPF32[offset] = value; + }, + }, + HEAP64: { + get(offset) { + return HEAP64[offset]; + }, + set(offset, value) { + HEAP64[offset] = value; + }, + }, + HEAPU64: { + get(offset) { + return HEAPU64[offset]; + }, + set(offset, value) { + HEAPU64[offset] = value; + }, + }, + HEAPF64: { + get(offset) { + return HEAPF64[offset]; + }, + set(offset, value) { + HEAPF64[offset] = value; + }, + }, + }, + wasmImports: Object.assign( + {}, + wasmImports, + typeof _builtin_fd_close === 'function' + ? { builtin_fd_close: _builtin_fd_close } + : {}, + typeof _builtin_fcntl64 === 'function' + ? { builtin_fcntl64: _builtin_fcntl64 } + : {} + ), + wasmExports, + syscalls: SYSCALLS, + FS, + PROXYFS, + NODEFS, + }); + }); + } + Module['ENV'] = Module['ENV'] || {}; + Module['ENV']['PATH'] = [ + Module['ENV']['PATH'], + '/internal/shared/bin', + ] + .filter(Boolean) + .join(':'); + FS.mkdir('/request'); + FS.mkdir('/internal'); + if (PHPLoader.nativeInternalDirPath) { + FS.mount( + FS.filesystems.NODEFS, + { root: PHPLoader.nativeInternalDirPath }, + '/internal' + ); + } + FS.mkdirTree('/internal/shared'); + FS.mkdirTree('/internal/shared/preload'); + FS.mkdirTree('/internal/shared/bin'); + const originalOnRuntimeInitialized = Module['onRuntimeInitialized']; + Module['onRuntimeInitialized'] = () => { + const { node: phpBinaryNode } = FS.lookupPath( + '/internal/shared/bin/php', + { noent_okay: true } + ); + if (!phpBinaryNode) { + FS.writeFile( + '/internal/shared/bin/php', + new TextEncoder().encode('#!/bin/sh\nphp "$@"') + ); + FS.chmod('/internal/shared/bin/php', 493); + } + originalOnRuntimeInitialized(); + }; + FS.registerDevice(FS.makedev(64, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onStdout(chunk); + return length; + }, + }); + FS.mkdev('/request/stdout', FS.makedev(64, 0)); + FS.registerDevice(FS.makedev(63, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onStderr(chunk); + return length; + }, + }); + FS.mkdev('/request/stderr', FS.makedev(63, 0)); + FS.registerDevice(FS.makedev(62, 0), { + open: () => {}, + close: () => {}, + read: () => 0, + write: (stream, buffer, offset, length, pos) => { + const chunk = buffer.subarray(offset, offset + length); + PHPWASM.onHeaders(chunk); + return length; + }, + }); + FS.mkdev('/request/headers', FS.makedev(62, 0)); + PHPWASM.EventEmitter = ENVIRONMENT_IS_NODE + ? require('events').EventEmitter + : class EventEmitter { + constructor() { + this.listeners = {}; + } + emit(eventName, data) { + if (this.listeners[eventName]) { + this.listeners[eventName].forEach( + (callback) => { + callback(data); + } + ); + } + } + once(eventName, callback) { + const self = this; + function removedCallback() { + callback(...arguments); + self.removeListener(eventName, removedCallback); + } + this.on(eventName, removedCallback); + } + removeAllListeners(eventName) { + if (eventName) { + delete this.listeners[eventName]; + } else { + this.listeners = {}; + } + } + removeListener(eventName, callback) { + if (this.listeners[eventName]) { + const idx = + this.listeners[eventName].indexOf(callback); + if (idx !== -1) { + this.listeners[eventName].splice(idx, 1); + } + } + } + }; + PHPWASM.processTable = {}; + PHPWASM.input_devices = {}; + const originalWrite = TTY.stream_ops.write; + TTY.stream_ops.write = function (stream, ...rest) { + const retval = originalWrite(stream, ...rest); + stream.tty.ops.fsync(stream.tty); + return retval; + }; + const originalPutChar = TTY.stream_ops.put_char; + TTY.stream_ops.put_char = function (tty, val) { + if (val === 10) tty.output.push(val); + return originalPutChar(tty, val); + }; + }, + onHeaders: function (chunk) { + if (Module['onHeaders']) { + Module['onHeaders'](chunk); + return; + } + console.log('headers', { chunk }); + }, + onStdout: function (chunk) { + if (Module['onStdout']) { + Module['onStdout'](chunk); + return; + } + if (ENVIRONMENT_IS_NODE) { + process.stdout.write(chunk); + } else { + console.log('stdout', { chunk }); + } + }, + onStderr: function (chunk) { + if (Module['onStderr']) { + Module['onStderr'](chunk); + return; + } + if (ENVIRONMENT_IS_NODE) { + process.stderr.write(chunk); + } else { + console.warn('stderr', { chunk }); + } + }, + getAllWebSockets: function (sock) { + const webSockets = new Set(); + if (sock.server) { + sock.server.clients.forEach((ws) => { + webSockets.add(ws); + }); + } + for (const peer of PHPWASM.getAllPeers(sock)) { + webSockets.add(peer.socket); + } + return Array.from(webSockets); + }, + getAllPeers: function (sock) { + const peers = new Set(); + if (sock.server) { + sock.pending + .filter((pending) => pending.peers) + .forEach((pending) => { + for (const peer of Object.values(pending.peers)) { + peers.add(peer); + } + }); + } + if (sock.peers) { + for (const peer of Object.values(sock.peers)) { + peers.add(peer); + } + } + return Array.from(peers); + }, + awaitData: function (ws) { + return PHPWASM.awaitEvent(ws, 'message'); + }, + awaitConnection: function (ws) { + if (ws.OPEN === ws.readyState) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'open'); + }, + awaitClose: function (ws) { + if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'close'); + }, + awaitError: function (ws) { + if ([ws.CLOSING, ws.CLOSED].includes(ws.readyState)) { + return [Promise.resolve(), PHPWASM.noop]; + } + return PHPWASM.awaitEvent(ws, 'error'); + }, + awaitEvent: function (ws, event) { + let resolve; + const listener = () => { + resolve(); + }; + const promise = new Promise(function (_resolve) { + resolve = _resolve; + ws.once(event, listener); + }); + const cancel = () => { + ws.removeListener(event, listener); + setTimeout(resolve); + }; + return [promise, cancel]; + }, + noop: function () {}, + spawnProcess: function (command, args, options) { + if (Module['spawnProcess']) { + const spawned = Module['spawnProcess'](command, args, { + ...options, + shell: true, + stdio: ['pipe', 'pipe', 'pipe'], + }); + if (spawned && !('then' in spawned) && 'on' in spawned) { + return spawned; + } + return Promise.resolve(spawned).then(function (spawned) { + if (!spawned || !spawned.on) { + throw new Error( + 'spawnProcess() must return an EventEmitter but returned a different type.' + ); + } + return spawned; + }); + } + const e = new Error( + 'popen(), proc_open() etc. are unsupported on this PHP instance. Call php.setSpawnHandler() ' + + 'and provide a callback to handle spawning processes, or disable a popen(), proc_open() ' + + 'and similar functions via php.ini.' + ); + e.code = 'SPAWN_UNSUPPORTED'; + throw e; + }, + shutdownSocket: function (socketd, how) { + const sock = getSocketFromFD(socketd); + const peer = Object.values(sock.peers)[0]; + if (!peer) { + return -1; + } + try { + peer.socket.close(); + SOCKFS.websocket_sock_ops.removePeer(sock, peer); + return 0; + } catch (e) { + console.log('Socket shutdown error', e); + return -1; + } + }, + }; + function _wasm_connect(sockfd, addr, addrlen) { + if (!('Suspending' in WebAssembly)) { + var sock = getSocketFromFD(sockfd); + var info = getSocketAddress(addr, addrlen); + sock.sock_ops.connect(sock, info.addr, info.port); + return 0; + } + return Asyncify.handleSleep((wakeUp) => { + let sock; + try { + sock = getSocketFromFD(sockfd); + } catch (e) { + wakeUp(-ERRNO_CODES.EBADF); + return; + } + if (!sock) { + wakeUp(-ERRNO_CODES.EBADF); + return; + } + let info; + try { + info = getSocketAddress(addr, addrlen); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + wakeUp(-ERRNO_CODES.EFAULT); + return; + } + wakeUp(-e.errno); + return; + } + try { + sock.sock_ops.connect(sock, info.addr, info.port); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + wakeUp(-e.errno); + return; + } + const webSockets = PHPWASM.getAllWebSockets(sock); + if (!webSockets.length) { + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + const ws = webSockets[0]; + if (ws.readyState === ws.OPEN) { + wakeUp(0); + return; + } + if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED) { + wakeUp(-ERRNO_CODES.ECONNREFUSED); + return; + } + const timeout = 3e4; + let resolved = false; + const timeoutId = setTimeout(() => { + if (!resolved) { + resolved = true; + wakeUp(-ERRNO_CODES.ETIMEDOUT); + } + }, timeout); + const handleOpen = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('error', handleError); + ws.removeEventListener('close', handleClose); + wakeUp(0); + } + }; + const handleError = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('open', handleOpen); + ws.removeEventListener('close', handleClose); + wakeUp(-ERRNO_CODES.ECONNREFUSED); + } + }; + const handleClose = () => { + if (!resolved) { + resolved = true; + clearTimeout(timeoutId); + ws.removeEventListener('open', handleOpen); + ws.removeEventListener('error', handleError); + wakeUp(-ERRNO_CODES.ECONNREFUSED); + } + }; + ws.addEventListener('open', handleOpen); + ws.addEventListener('error', handleError); + ws.addEventListener('close', handleClose); + }); + } + function ___syscall_connect(sockfd, addr, addrlen, d1, d2, d3) { + return _wasm_connect(sockfd, addr, addrlen); + } + ___syscall_connect.sig = 'iippiii'; + function ___syscall_dup(fd) { + try { + var old = SYSCALLS.getStreamFromFD(fd); + return FS.dupStream(old).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_dup.sig = 'ii'; + function ___syscall_dup3(fd, newfd, flags) { + try { + var old = SYSCALLS.getStreamFromFD(fd); + if (old.fd === newfd) return -28; + if (newfd < 0 || newfd >= FS.MAX_OPEN_FDS) return -8; + var existing = FS.getStream(newfd); + if (existing) FS.close(existing); + return FS.dupStream(old, newfd).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_dup3.sig = 'iiii'; + function ___syscall_faccessat(dirfd, path, amode, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (amode & ~7) { + return -28; + } + var lookup = FS.lookupPath(path, { follow: true }); + var node = lookup.node; + if (!node) { + return -44; + } + var perms = ''; + if (amode & 4) perms += 'r'; + if (amode & 2) perms += 'w'; + if (amode & 1) perms += 'x'; + if (perms && FS.nodePermissions(node, perms)) { + return -2; + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_faccessat.sig = 'iipii'; + function ___syscall_fchmod(fd, mode) { + try { + FS.fchmod(fd, mode); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_fchmod.sig = 'iii'; + function ___syscall_fchown32(fd, owner, group) { + try { + FS.fchown(fd, owner, group); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_fchown32.sig = 'iiii'; + function ___syscall_fchownat(dirfd, path, owner, group, flags) { + try { + path = SYSCALLS.getStr(path); + var nofollow = flags & 256; + flags = flags & ~256; + path = SYSCALLS.calculateAt(dirfd, path); + (nofollow ? FS.lchown : FS.chown)(path, owner, group); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_fchownat.sig = 'iipiii'; + var syscallGetVarargI = () => { + var ret = HEAP32[+SYSCALLS.varargs >> 2]; + SYSCALLS.varargs += 4; + return ret; + }; + var syscallGetVarargP = syscallGetVarargI; + function ___syscall_fcntl64(fd, cmd, varargs) { + SYSCALLS.varargs = varargs; + try { + var stream = SYSCALLS.getStreamFromFD(fd); + switch (cmd) { + case 0: { + var arg = syscallGetVarargI(); + if (arg < 0) { + return -28; + } + while (FS.streams[arg]) { + arg++; + } + var newStream; + newStream = FS.dupStream(stream, arg); + return newStream.fd; + } + case 1: + case 2: + return 0; + case 3: + return stream.flags; + case 4: { + var arg = syscallGetVarargI(); + stream.flags |= arg; + return 0; + } + case 12: { + var arg = syscallGetVarargP(); + var offset = 0; + HEAP16[(arg + offset) >> 1] = 2; + return 0; + } + case 13: + case 14: + return 0; + } + return -28; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_fcntl64.sig = 'iiip'; + function ___syscall_fstat64(fd, buf) { + try { + return SYSCALLS.writeStat(buf, FS.fstat(fd)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_fstat64.sig = 'iip'; + var INT53_MAX = 9007199254740992; + var INT53_MIN = -9007199254740992; + var bigintToI53Checked = (num) => + num < INT53_MIN || num > INT53_MAX ? NaN : Number(num); + function ___syscall_ftruncate64(fd, length) { + length = bigintToI53Checked(length); + try { + if (isNaN(length)) return -61; + FS.ftruncate(fd, length); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_ftruncate64.sig = 'iij'; + function ___syscall_getcwd(buf, size) { + try { + if (size === 0) return -28; + var cwd = FS.cwd(); + var cwdLengthInBytes = lengthBytesUTF8(cwd) + 1; + if (size < cwdLengthInBytes) return -68; + stringToUTF8(cwd, buf, size); + return cwdLengthInBytes; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_getcwd.sig = 'ipp'; + function ___syscall_getdents64(fd, dirp, count) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + stream.getdents ||= FS.readdir(stream.path); + var struct_size = 280; + var pos = 0; + var off = FS.llseek(stream, 0, 1); + var startIdx = Math.floor(off / struct_size); + var endIdx = Math.min( + stream.getdents.length, + startIdx + Math.floor(count / struct_size) + ); + for (var idx = startIdx; idx < endIdx; idx++) { + var id; + var type; + var name = stream.getdents[idx]; + if (name === '.') { + id = stream.node.id; + type = 4; + } else if (name === '..') { + var lookup = FS.lookupPath(stream.path, { parent: true }); + id = lookup.node.id; + type = 4; + } else { + var child; + try { + child = FS.lookupNode(stream.node, name); + } catch (e) { + if (e?.errno === 28) { + continue; + } + throw e; + } + id = child.id; + type = FS.isChrdev(child.mode) + ? 2 + : FS.isDir(child.mode) + ? 4 + : FS.isLink(child.mode) + ? 10 + : 8; + } + HEAP64[(dirp + pos) >> 3] = BigInt(id); + HEAP64[(dirp + pos + 8) >> 3] = BigInt((idx + 1) * struct_size); + HEAP16[(dirp + pos + 16) >> 1] = 280; + HEAP8[dirp + pos + 18] = type; + stringToUTF8(name, dirp + pos + 19, 256); + pos += struct_size; + } + FS.llseek(stream, idx * struct_size, 0); + return pos; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_getdents64.sig = 'iipp'; + function ___syscall_getpeername(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + if (!sock.daddr) { + return -53; + } + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(sock.daddr), + sock.dport, + addrlen + ); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_getpeername.sig = 'iippiii'; + function ___syscall_getsockname(fd, addr, addrlen, d1, d2, d3) { + try { + var sock = getSocketFromFD(fd); + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(sock.saddr || '0.0.0.0'), + sock.sport, + addrlen + ); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_getsockname.sig = 'iippiii'; + function ___syscall_getsockopt(fd, level, optname, optval, optlen, d1) { + try { + var sock = getSocketFromFD(fd); + if (level === 1) { + if (optname === 4) { + HEAP32[optval >> 2] = sock.error; + HEAP32[optlen >> 2] = 4; + sock.error = null; + return 0; + } + } + return -50; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_getsockopt.sig = 'iiiippi'; + function ___syscall_ioctl(fd, op, varargs) { + SYSCALLS.varargs = varargs; + try { + var stream = SYSCALLS.getStreamFromFD(fd); + switch (op) { + case 21509: { + if (!stream.tty) return -59; + return 0; + } + case 21505: { + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tcgets) { + var termios = stream.tty.ops.ioctl_tcgets(stream); + var argp = syscallGetVarargP(); + HEAP32[argp >> 2] = termios.c_iflag || 0; + HEAP32[(argp + 4) >> 2] = termios.c_oflag || 0; + HEAP32[(argp + 8) >> 2] = termios.c_cflag || 0; + HEAP32[(argp + 12) >> 2] = termios.c_lflag || 0; + for (var i = 0; i < 32; i++) { + HEAP8[argp + i + 17] = termios.c_cc[i] || 0; + } + return 0; + } + return 0; + } + case 21510: + case 21511: + case 21512: { + if (!stream.tty) return -59; + return 0; + } + case 21506: + case 21507: + case 21508: { + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tcsets) { + var argp = syscallGetVarargP(); + var c_iflag = HEAP32[argp >> 2]; + var c_oflag = HEAP32[(argp + 4) >> 2]; + var c_cflag = HEAP32[(argp + 8) >> 2]; + var c_lflag = HEAP32[(argp + 12) >> 2]; + var c_cc = []; + for (var i = 0; i < 32; i++) { + c_cc.push(HEAP8[argp + i + 17]); + } + return stream.tty.ops.ioctl_tcsets(stream.tty, op, { + c_iflag, + c_oflag, + c_cflag, + c_lflag, + c_cc, + }); + } + return 0; + } + case 21519: { + if (!stream.tty) return -59; + var argp = syscallGetVarargP(); + HEAP32[argp >> 2] = 0; + return 0; + } + case 21520: { + if (!stream.tty) return -59; + return -28; + } + case 21537: + case 21531: { + var argp = syscallGetVarargP(); + return FS.ioctl(stream, op, argp); + } + case 21523: { + if (!stream.tty) return -59; + if (stream.tty.ops.ioctl_tiocgwinsz) { + var winsize = stream.tty.ops.ioctl_tiocgwinsz( + stream.tty + ); + var argp = syscallGetVarargP(); + HEAP16[argp >> 1] = winsize[0]; + HEAP16[(argp + 2) >> 1] = winsize[1]; + } + return 0; + } + case 21524: { + if (!stream.tty) return -59; + return 0; + } + case 21515: { + if (!stream.tty) return -59; + return 0; + } + default: + return -28; + } + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_ioctl.sig = 'iiip'; + function ___syscall_listen(fd, backlog) { + try { + var sock = getSocketFromFD(fd); + sock.sock_ops.listen(sock, backlog); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_listen.sig = 'iiiiiii'; + function ___syscall_lstat64(path, buf) { + try { + path = SYSCALLS.getStr(path); + return SYSCALLS.writeStat(buf, FS.lstat(path)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_lstat64.sig = 'ipp'; + function ___syscall_mkdirat(dirfd, path, mode) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + FS.mkdir(path, mode, 0); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_mkdirat.sig = 'iipi'; + function ___syscall_newfstatat(dirfd, path, buf, flags) { + try { + path = SYSCALLS.getStr(path); + var nofollow = flags & 256; + var allowEmpty = flags & 4096; + flags = flags & ~6400; + path = SYSCALLS.calculateAt(dirfd, path, allowEmpty); + return SYSCALLS.writeStat( + buf, + nofollow ? FS.lstat(path) : FS.stat(path) + ); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_newfstatat.sig = 'iippi'; + function ___syscall_openat(dirfd, path, flags, varargs) { + SYSCALLS.varargs = varargs; + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + var mode = varargs ? syscallGetVarargI() : 0; + return FS.open(path, flags, mode).fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_openat.sig = 'iipip'; + var PIPEFS = { + BUCKET_BUFFER_SIZE: 8192, + mount(mount) { + return FS.createNode(null, '/', 16384 | 511, 0); + }, + createPipe() { + var pipe = { buckets: [], refcnt: 2, timestamp: new Date() }; + pipe.buckets.push({ + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: 0, + roffset: 0, + }); + var rName = PIPEFS.nextname(); + var wName = PIPEFS.nextname(); + var rNode = FS.createNode(PIPEFS.root, rName, 4096, 0); + var wNode = FS.createNode(PIPEFS.root, wName, 4096, 0); + rNode.pipe = pipe; + wNode.pipe = pipe; + var readableStream = FS.createStream({ + path: rName, + node: rNode, + flags: 0, + seekable: false, + stream_ops: PIPEFS.stream_ops, + }); + rNode.stream = readableStream; + var writableStream = FS.createStream({ + path: wName, + node: wNode, + flags: 1, + seekable: false, + stream_ops: PIPEFS.stream_ops, + }); + wNode.stream = writableStream; + return { + readable_fd: readableStream.fd, + writable_fd: writableStream.fd, + }; + }, + stream_ops: { + getattr(stream) { + var node = stream.node; + var timestamp = node.pipe.timestamp; + return { + dev: 14, + ino: node.id, + mode: 4480, + nlink: 1, + uid: 0, + gid: 0, + rdev: 0, + size: 0, + atime: timestamp, + mtime: timestamp, + ctime: timestamp, + blksize: 4096, + blocks: 0, + }; + }, + poll(stream) { + var pipe = stream.node.pipe; + if ((stream.flags & 2097155) === 1) { + return 256 | 4; + } + for (var bucket of pipe.buckets) { + if (bucket.offset - bucket.roffset > 0) { + return 64 | 1; + } + } + return 0; + }, + dup(stream) { + stream.node.pipe.refcnt++; + }, + ioctl(stream, request, varargs) { + return 28; + }, + fsync(stream) { + return 28; + }, + read(stream, buffer, offset, length, position) { + var pipe = stream.node.pipe; + var currentLength = 0; + for (var bucket of pipe.buckets) { + currentLength += bucket.offset - bucket.roffset; + } + var data = buffer.subarray(offset, offset + length); + if (length <= 0) { + return 0; + } + if (currentLength == 0) { + if (pipe.refcnt < 2) { + return 0; + } + throw new FS.ErrnoError(6); + } + var toRead = Math.min(currentLength, length); + var totalRead = toRead; + var toRemove = 0; + for (var bucket of pipe.buckets) { + var bucketSize = bucket.offset - bucket.roffset; + if (toRead <= bucketSize) { + var tmpSlice = bucket.buffer.subarray( + bucket.roffset, + bucket.offset + ); + if (toRead < bucketSize) { + tmpSlice = tmpSlice.subarray(0, toRead); + bucket.roffset += toRead; + } else { + toRemove++; + } + data.set(tmpSlice); + break; + } else { + var tmpSlice = bucket.buffer.subarray( + bucket.roffset, + bucket.offset + ); + data.set(tmpSlice); + data = data.subarray(tmpSlice.byteLength); + toRead -= tmpSlice.byteLength; + toRemove++; + } + } + if (toRemove && toRemove == pipe.buckets.length) { + toRemove--; + pipe.buckets[toRemove].offset = 0; + pipe.buckets[toRemove].roffset = 0; + } + pipe.buckets.splice(0, toRemove); + return totalRead; + }, + write(stream, buffer, offset, length, position) { + var pipe = stream.node.pipe; + var data = buffer.subarray(offset, offset + length); + var dataLen = data.byteLength; + if (dataLen <= 0) { + return 0; + } + var currBucket = null; + if (pipe.buckets.length == 0) { + currBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: 0, + roffset: 0, + }; + pipe.buckets.push(currBucket); + } else { + currBucket = pipe.buckets[pipe.buckets.length - 1]; + } + var freeBytesInCurrBuffer = + PIPEFS.BUCKET_BUFFER_SIZE - currBucket.offset; + if (freeBytesInCurrBuffer >= dataLen) { + currBucket.buffer.set(data, currBucket.offset); + currBucket.offset += dataLen; + return dataLen; + } else if (freeBytesInCurrBuffer > 0) { + currBucket.buffer.set( + data.subarray(0, freeBytesInCurrBuffer), + currBucket.offset + ); + currBucket.offset += freeBytesInCurrBuffer; + data = data.subarray( + freeBytesInCurrBuffer, + data.byteLength + ); + } + var numBuckets = + (data.byteLength / PIPEFS.BUCKET_BUFFER_SIZE) | 0; + var remElements = data.byteLength % PIPEFS.BUCKET_BUFFER_SIZE; + for (var i = 0; i < numBuckets; i++) { + var newBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: PIPEFS.BUCKET_BUFFER_SIZE, + roffset: 0, + }; + pipe.buckets.push(newBucket); + newBucket.buffer.set( + data.subarray(0, PIPEFS.BUCKET_BUFFER_SIZE) + ); + data = data.subarray( + PIPEFS.BUCKET_BUFFER_SIZE, + data.byteLength + ); + } + if (remElements > 0) { + var newBucket = { + buffer: new Uint8Array(PIPEFS.BUCKET_BUFFER_SIZE), + offset: data.byteLength, + roffset: 0, + }; + pipe.buckets.push(newBucket); + newBucket.buffer.set(data); + } + return dataLen; + }, + close(stream) { + var pipe = stream.node.pipe; + pipe.refcnt--; + if (pipe.refcnt === 0) { + pipe.buckets = null; + } + }, + }, + nextname() { + if (!PIPEFS.nextname.current) { + PIPEFS.nextname.current = 0; + } + return 'pipe[' + PIPEFS.nextname.current++ + ']'; + }, + }; + function ___syscall_pipe(fdPtr) { + try { + if (fdPtr == 0) { + throw new FS.ErrnoError(21); + } + var res = PIPEFS.createPipe(); + HEAP32[fdPtr >> 2] = res.readable_fd; + HEAP32[(fdPtr + 4) >> 2] = res.writable_fd; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_pipe.sig = 'ip'; + function ___syscall_readlinkat(dirfd, path, buf, bufsize) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (bufsize <= 0) return -28; + var ret = FS.readlink(path); + var len = Math.min(bufsize, lengthBytesUTF8(ret)); + var endChar = HEAP8[buf + len]; + stringToUTF8(ret, buf, bufsize + 1); + HEAP8[buf + len] = endChar; + return len; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_readlinkat.sig = 'iippp'; + function ___syscall_recvfrom(fd, buf, len, flags, addr, addrlen) { + try { + var sock = getSocketFromFD(fd); + var msg = sock.sock_ops.recvmsg( + sock, + len, + typeof flags !== 'undefined' ? flags : 0 + ); + if (!msg) return 0; + if (addr) { + var errno = writeSockaddr( + addr, + sock.family, + DNS.lookup_name(msg.addr), + msg.port, + addrlen + ); + } + HEAPU8.set(msg.buffer, buf); + return msg.buffer.byteLength; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_recvfrom.sig = 'iippipp'; + function ___syscall_renameat(olddirfd, oldpath, newdirfd, newpath) { + try { + oldpath = SYSCALLS.getStr(oldpath); + newpath = SYSCALLS.getStr(newpath); + oldpath = SYSCALLS.calculateAt(olddirfd, oldpath); + newpath = SYSCALLS.calculateAt(newdirfd, newpath); + FS.rename(oldpath, newpath); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_renameat.sig = 'iipip'; + function ___syscall_rmdir(path) { + try { + path = SYSCALLS.getStr(path); + FS.rmdir(path); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_rmdir.sig = 'ip'; + function ___syscall_sendto(fd, message, length, flags, addr, addr_len) { + try { + var sock = getSocketFromFD(fd); + if (!addr) { + return FS.write(sock.stream, HEAP8, message, length); + } + var dest = getSocketAddress(addr, addr_len); + return sock.sock_ops.sendmsg( + sock, + HEAP8, + message, + length, + dest.addr, + dest.port + ); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_sendto.sig = 'iippipp'; + function ___syscall_socket(domain, type, protocol) { + try { + var sock = SOCKFS.createSocket(domain, type, protocol); + return sock.stream.fd; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_socket.sig = 'iiiiiii'; + function ___syscall_stat64(path, buf) { + try { + path = SYSCALLS.getStr(path); + return SYSCALLS.writeStat(buf, FS.stat(path)); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_stat64.sig = 'ipp'; + function ___syscall_statfs64(path, size, buf) { + try { + SYSCALLS.writeStatFs(buf, FS.statfs(SYSCALLS.getStr(path))); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_statfs64.sig = 'ippp'; + function ___syscall_symlinkat(target, dirfd, linkpath) { + try { + target = SYSCALLS.getStr(target); + linkpath = SYSCALLS.getStr(linkpath); + linkpath = SYSCALLS.calculateAt(dirfd, linkpath); + FS.symlink(target, linkpath); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_symlinkat.sig = 'ipip'; + function ___syscall_unlinkat(dirfd, path, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path); + if (!flags) { + FS.unlink(path); + } else if (flags === 512) { + FS.rmdir(path); + } else { + return -28; + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_unlinkat.sig = 'iipi'; + var readI53FromI64 = (ptr) => + HEAPU32[ptr >> 2] + HEAP32[(ptr + 4) >> 2] * 4294967296; + function ___syscall_utimensat(dirfd, path, times, flags) { + try { + path = SYSCALLS.getStr(path); + path = SYSCALLS.calculateAt(dirfd, path, true); + var now = Date.now(), + atime, + mtime; + if (!times) { + atime = now; + mtime = now; + } else { + var seconds = readI53FromI64(times); + var nanoseconds = HEAP32[(times + 8) >> 2]; + if (nanoseconds == 1073741823) { + atime = now; + } else if (nanoseconds == 1073741822) { + atime = null; + } else { + atime = seconds * 1e3 + nanoseconds / (1e3 * 1e3); + } + times += 16; + seconds = readI53FromI64(times); + nanoseconds = HEAP32[(times + 8) >> 2]; + if (nanoseconds == 1073741823) { + mtime = now; + } else if (nanoseconds == 1073741822) { + mtime = null; + } else { + mtime = seconds * 1e3 + nanoseconds / (1e3 * 1e3); + } + } + if ((mtime ?? atime) !== null) { + FS.utime(path, atime, mtime); + } + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + ___syscall_utimensat.sig = 'iippi'; + var __abort_js = () => abort(''); + __abort_js.sig = 'v'; + var dlSetError = (msg) => { + var sp = stackSave(); + var cmsg = stringToUTF8OnStack(msg); + ___dl_seterr(cmsg, 0); + stackRestore(sp); + }; + var dlopenInternal = (handle, jsflags) => { + var filename = UTF8ToString(handle + 36); + var flags = HEAP32[(handle + 4) >> 2]; + filename = PATH.normalize(filename); + var global = Boolean(flags & 256); + var localScope = global ? null : {}; + var combinedFlags = { + global, + nodelete: Boolean(flags & 4096), + loadAsync: jsflags.loadAsync, + }; + if (jsflags.loadAsync) { + return loadDynamicLibrary( + filename, + combinedFlags, + localScope, + handle + ); + } + try { + return loadDynamicLibrary( + filename, + combinedFlags, + localScope, + handle + ); + } catch (e) { + dlSetError(`could not load dynamic lib: ${filename}\n${e}`); + return 0; + } + }; + function __dlopen_js(handle) { + var jsflags = { loadAsync: false }; + return dlopenInternal(handle, jsflags); + } + __dlopen_js.sig = 'pp'; + var __dlsym_js = (handle, symbol, symbolIndex) => { + symbol = UTF8ToString(symbol); + var result; + var newSymIndex; + var lib = LDSO.loadedLibsByHandle[handle]; + newSymIndex = Object.keys(lib.exports).indexOf(symbol); + if (newSymIndex == -1 || lib.exports[symbol].stub) { + dlSetError( + `Tried to lookup unknown symbol "${symbol}" in dynamic lib: ${lib.name}` + ); + return 0; + } + result = lib.exports[symbol]; + if (typeof result == 'function') { + if (result.orig) { + result = result.orig; + } + var addr = getFunctionAddress(result); + if (addr) { + result = addr; + } else { + result = addFunction(result, result.sig); + HEAPU32[symbolIndex >> 2] = newSymIndex; + } + } + return result; + }; + __dlsym_js.sig = 'pppp'; + var __emscripten_lookup_name = (name) => { + var nameString = UTF8ToString(name); + return inetPton4(DNS.lookup_name(nameString)); + }; + __emscripten_lookup_name.sig = 'ip'; + var runtimeKeepaliveCounter = 0; + var __emscripten_runtime_keepalive_clear = () => { + noExitRuntime = false; + runtimeKeepaliveCounter = 0; + }; + __emscripten_runtime_keepalive_clear.sig = 'v'; + function __gmtime_js(time, tmPtr) { + time = bigintToI53Checked(time); + var date = new Date(time * 1e3); + HEAP32[tmPtr >> 2] = date.getUTCSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getUTCMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getUTCHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getUTCDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getUTCMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getUTCFullYear() - 1900; + HEAP32[(tmPtr + 24) >> 2] = date.getUTCDay(); + var start = Date.UTC(date.getUTCFullYear(), 0, 1, 0, 0, 0, 0); + var yday = ((date.getTime() - start) / (1e3 * 60 * 60 * 24)) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + } + __gmtime_js.sig = 'vjp'; + var isLeapYear = (year) => + year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); + var MONTH_DAYS_LEAP_CUMULATIVE = [ + 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, + ]; + var MONTH_DAYS_REGULAR_CUMULATIVE = [ + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, + ]; + var ydayFromDate = (date) => { + var leap = isLeapYear(date.getFullYear()); + var monthDaysCumulative = leap + ? MONTH_DAYS_LEAP_CUMULATIVE + : MONTH_DAYS_REGULAR_CUMULATIVE; + var yday = monthDaysCumulative[date.getMonth()] + date.getDate() - 1; + return yday; + }; + function __localtime_js(time, tmPtr) { + time = bigintToI53Checked(time); + var date = new Date(time * 1e3); + HEAP32[tmPtr >> 2] = date.getSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getFullYear() - 1900; + HEAP32[(tmPtr + 24) >> 2] = date.getDay(); + var yday = ydayFromDate(date) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + HEAP32[(tmPtr + 36) >> 2] = -(date.getTimezoneOffset() * 60); + var start = new Date(date.getFullYear(), 0, 1); + var summerOffset = new Date( + date.getFullYear(), + 6, + 1 + ).getTimezoneOffset(); + var winterOffset = start.getTimezoneOffset(); + var dst = + (summerOffset != winterOffset && + date.getTimezoneOffset() == + Math.min(winterOffset, summerOffset)) | 0; + HEAP32[(tmPtr + 32) >> 2] = dst; + } + __localtime_js.sig = 'vjp'; + var __mktime_js = function (tmPtr) { + var ret = (() => { + var date = new Date( + HEAP32[(tmPtr + 20) >> 2] + 1900, + HEAP32[(tmPtr + 16) >> 2], + HEAP32[(tmPtr + 12) >> 2], + HEAP32[(tmPtr + 8) >> 2], + HEAP32[(tmPtr + 4) >> 2], + HEAP32[tmPtr >> 2], + 0 + ); + var dst = HEAP32[(tmPtr + 32) >> 2]; + var guessedOffset = date.getTimezoneOffset(); + var start = new Date(date.getFullYear(), 0, 1); + var summerOffset = new Date( + date.getFullYear(), + 6, + 1 + ).getTimezoneOffset(); + var winterOffset = start.getTimezoneOffset(); + var dstOffset = Math.min(winterOffset, summerOffset); + if (dst < 0) { + HEAP32[(tmPtr + 32) >> 2] = Number( + summerOffset != winterOffset && dstOffset == guessedOffset + ); + } else if (dst > 0 != (dstOffset == guessedOffset)) { + var nonDstOffset = Math.max(winterOffset, summerOffset); + var trueOffset = dst > 0 ? dstOffset : nonDstOffset; + date.setTime( + date.getTime() + (trueOffset - guessedOffset) * 6e4 + ); + } + HEAP32[(tmPtr + 24) >> 2] = date.getDay(); + var yday = ydayFromDate(date) | 0; + HEAP32[(tmPtr + 28) >> 2] = yday; + HEAP32[tmPtr >> 2] = date.getSeconds(); + HEAP32[(tmPtr + 4) >> 2] = date.getMinutes(); + HEAP32[(tmPtr + 8) >> 2] = date.getHours(); + HEAP32[(tmPtr + 12) >> 2] = date.getDate(); + HEAP32[(tmPtr + 16) >> 2] = date.getMonth(); + HEAP32[(tmPtr + 20) >> 2] = date.getYear(); + var timeMs = date.getTime(); + if (isNaN(timeMs)) { + return -1; + } + return timeMs / 1e3; + })(); + return BigInt(ret); + }; + __mktime_js.sig = 'jp'; + function __mmap_js(len, prot, flags, fd, offset, allocated, addr) { + offset = bigintToI53Checked(offset); + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var res = FS.mmap(stream, len, offset, prot, flags); + var ptr = res.ptr; + HEAP32[allocated >> 2] = res.allocated; + HEAPU32[addr >> 2] = ptr; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + __mmap_js.sig = 'ipiiijpp'; + function __munmap_js(addr, len, prot, flags, fd, offset) { + offset = bigintToI53Checked(offset); + try { + var stream = SYSCALLS.getStreamFromFD(fd); + if (prot & 2) { + SYSCALLS.doMsync(addr, stream, len, flags, offset); + } + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return -e.errno; + } + } + __munmap_js.sig = 'ippiiij'; + var timers = {}; + var handleException = (e) => { + if (e instanceof ExitStatus || e == 'unwind') { + return EXITSTATUS; + } + quit_(1, e); + }; + var keepRuntimeAlive = () => noExitRuntime || runtimeKeepaliveCounter > 0; + var _proc_exit = (code) => { + EXITSTATUS = code; + if (!keepRuntimeAlive()) { + Module['onExit']?.(code); + ABORT = true; + } + quit_(code, new ExitStatus(code)); + }; + _proc_exit.sig = 'vi'; + var exitJS = (status, implicit) => { + EXITSTATUS = status; + if (!keepRuntimeAlive()) { + exitRuntime(); + } + _proc_exit(status); + }; + var _exit = exitJS; + _exit.sig = 'vi'; + var maybeExit = () => { + if (runtimeExited) { + return; + } + if (!keepRuntimeAlive()) { + try { + _exit(EXITSTATUS); + } catch (e) { + handleException(e); + } + } + }; + var callUserCallback = (func) => { + if (runtimeExited || ABORT) { + return; + } + try { + func(); + maybeExit(); + } catch (e) { + handleException(e); + } + }; + var _emscripten_get_now = () => performance.now(); + _emscripten_get_now.sig = 'd'; + var __setitimer_js = (which, timeout_ms) => { + if (timers[which]) { + clearTimeout(timers[which].id); + delete timers[which]; + } + if (!timeout_ms) return 0; + var id = setTimeout(() => { + delete timers[which]; + callUserCallback(() => + __emscripten_timeout(which, _emscripten_get_now()) + ); + }, timeout_ms); + timers[which] = { id, timeout_ms }; + return 0; + }; + __setitimer_js.sig = 'iid'; + var __tzset_js = (timezone, daylight, std_name, dst_name) => { + var currentYear = new Date().getFullYear(); + var winter = new Date(currentYear, 0, 1); + var summer = new Date(currentYear, 6, 1); + var winterOffset = winter.getTimezoneOffset(); + var summerOffset = summer.getTimezoneOffset(); + var stdTimezoneOffset = Math.max(winterOffset, summerOffset); + HEAPU32[timezone >> 2] = stdTimezoneOffset * 60; + HEAP32[daylight >> 2] = Number(winterOffset != summerOffset); + var extractZone = (timezoneOffset) => { + var sign = timezoneOffset >= 0 ? '-' : '+'; + var absOffset = Math.abs(timezoneOffset); + var hours = String(Math.floor(absOffset / 60)).padStart(2, '0'); + var minutes = String(absOffset % 60).padStart(2, '0'); + return `UTC${sign}${hours}${minutes}`; + }; + var winterName = extractZone(winterOffset); + var summerName = extractZone(summerOffset); + if (summerOffset < winterOffset) { + stringToUTF8(winterName, std_name, 17); + stringToUTF8(summerName, dst_name, 17); + } else { + stringToUTF8(winterName, dst_name, 17); + stringToUTF8(summerName, std_name, 17); + } + }; + __tzset_js.sig = 'vpppp'; + var _emscripten_date_now = () => Date.now(); + _emscripten_date_now.sig = 'd'; + var nowIsMonotonic = 1; + var checkWasiClock = (clock_id) => clock_id >= 0 && clock_id <= 3; + function _clock_time_get(clk_id, ignored_precision, ptime) { + ignored_precision = bigintToI53Checked(ignored_precision); + if (!checkWasiClock(clk_id)) { + return 28; + } + var now; + if (clk_id === 0) { + now = _emscripten_date_now(); + } else if (nowIsMonotonic) { + now = _emscripten_get_now(); + } else { + return 52; + } + var nsec = Math.round(now * 1e3 * 1e3); + HEAP64[ptime >> 3] = BigInt(nsec); + return 0; + } + _clock_time_get.sig = 'iijp'; + var getHeapMax = () => 2147483648; + var _emscripten_get_heap_max = () => getHeapMax(); + _emscripten_get_heap_max.sig = 'p'; + var growMemory = (size) => { + var oldHeapSize = wasmMemory.buffer.byteLength; + var pages = ((size - oldHeapSize + 65535) / 65536) | 0; + try { + wasmMemory.grow(pages); + updateMemoryViews(); + return 1; + } catch (e) {} + }; + var _emscripten_resize_heap = (requestedSize) => { + var oldSize = HEAPU8.length; + requestedSize >>>= 0; + var maxHeapSize = getHeapMax(); + if (requestedSize > maxHeapSize) { + return false; + } + for (var cutDown = 1; cutDown <= 4; cutDown *= 2) { + var overGrownHeapSize = oldSize * (1 + 0.2 / cutDown); + overGrownHeapSize = Math.min( + overGrownHeapSize, + requestedSize + 100663296 + ); + var newSize = Math.min( + maxHeapSize, + alignMemory(Math.max(requestedSize, overGrownHeapSize), 65536) + ); + var replacement = growMemory(newSize); + if (replacement) { + return true; + } + } + return false; + }; + _emscripten_resize_heap.sig = 'ip'; + var runtimeKeepalivePush = () => { + runtimeKeepaliveCounter += 1; + }; + runtimeKeepalivePush.sig = 'v'; + var runtimeKeepalivePop = () => { + runtimeKeepaliveCounter -= 1; + }; + runtimeKeepalivePop.sig = 'v'; + var safeSetTimeout = (func, timeout) => { + runtimeKeepalivePush(); + return setTimeout(() => { + runtimeKeepalivePop(); + callUserCallback(func); + }, timeout); + }; + var _emscripten_sleep = (ms) => + Asyncify.handleSleep((wakeUp) => safeSetTimeout(wakeUp, ms)); + _emscripten_sleep.sig = 'vi'; + _emscripten_sleep.isAsync = true; + var ENV = PHPLoader.ENV || {}; + var getExecutableName = () => thisProgram || './this.program'; + var getEnvStrings = () => { + if (!getEnvStrings.strings) { + var lang = + ( + (typeof navigator == 'object' && navigator.language) || + 'C' + ).replace('-', '_') + '.UTF-8'; + var env = { + USER: 'web_user', + LOGNAME: 'web_user', + PATH: '/', + PWD: '/', + HOME: '/home/web_user', + LANG: lang, + _: getExecutableName(), + }; + for (var x in ENV) { + if (ENV[x] === undefined) delete env[x]; + else env[x] = ENV[x]; + } + var strings = []; + for (var x in env) { + strings.push(`${x}=${env[x]}`); + } + getEnvStrings.strings = strings; + } + return getEnvStrings.strings; + }; + var _environ_get = (__environ, environ_buf) => { + var bufSize = 0; + var envp = 0; + for (var string of getEnvStrings()) { + var ptr = environ_buf + bufSize; + HEAPU32[(__environ + envp) >> 2] = ptr; + bufSize += stringToUTF8(string, ptr, Infinity) + 1; + envp += 4; + } + return 0; + }; + _environ_get.sig = 'ipp'; + var _environ_sizes_get = (penviron_count, penviron_buf_size) => { + var strings = getEnvStrings(); + HEAPU32[penviron_count >> 2] = strings.length; + var bufSize = 0; + for (var string of strings) { + bufSize += lengthBytesUTF8(string) + 1; + } + HEAPU32[penviron_buf_size >> 2] = bufSize; + return 0; + }; + _environ_sizes_get.sig = 'ipp'; + function _fd_close(fd) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + FS.close(stream); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + _fd_close.sig = 'ii'; + function _fd_fdstat_get(fd, pbuf) { + try { + var rightsBase = 0; + var rightsInheriting = 0; + var flags = 0; + { + var stream = SYSCALLS.getStreamFromFD(fd); + var type = stream.tty + ? 2 + : FS.isDir(stream.mode) + ? 3 + : FS.isLink(stream.mode) + ? 7 + : 4; + } + HEAP8[pbuf] = type; + HEAP16[(pbuf + 2) >> 1] = flags; + HEAP64[(pbuf + 8) >> 3] = BigInt(rightsBase); + HEAP64[(pbuf + 16) >> 3] = BigInt(rightsInheriting); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + _fd_fdstat_get.sig = 'iip'; + var doReadv = (stream, iov, iovcnt, offset) => { + var ret = 0; + for (var i = 0; i < iovcnt; i++) { + var ptr = HEAPU32[iov >> 2]; + var len = HEAPU32[(iov + 4) >> 2]; + iov += 8; + var curr = FS.read(stream, HEAP8, ptr, len, offset); + if (curr < 0) return -1; + ret += curr; + if (curr < len) break; + if (typeof offset != 'undefined') { + offset += curr; + } + } + return ret; + }; + function _fd_read(fd, iov, iovcnt, pnum) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var num = doReadv(stream, iov, iovcnt); + HEAPU32[pnum >> 2] = num; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + _fd_read.sig = 'iippp'; + function _fd_seek(fd, offset, whence, newOffset) { + offset = bigintToI53Checked(offset); + try { + if (isNaN(offset)) return 61; + var stream = SYSCALLS.getStreamFromFD(fd); + FS.llseek(stream, offset, whence); + HEAP64[newOffset >> 3] = BigInt(stream.position); + if (stream.getdents && offset === 0 && whence === 0) + stream.getdents = null; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + _fd_seek.sig = 'iijip'; + var _fd_sync = function (fd) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + return Asyncify.handleSleep((wakeUp) => { + var mount = stream.node.mount; + if (!mount.type.syncfs) { + wakeUp(0); + return; + } + mount.type.syncfs(mount, false, (err) => { + wakeUp(err ? 29 : 0); + }); + }); + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + }; + _fd_sync.sig = 'ii'; + _fd_sync.isAsync = true; + var doWritev = (stream, iov, iovcnt, offset) => { + var ret = 0; + for (var i = 0; i < iovcnt; i++) { + var ptr = HEAPU32[iov >> 2]; + var len = HEAPU32[(iov + 4) >> 2]; + iov += 8; + var curr = FS.write(stream, HEAP8, ptr, len, offset); + if (curr < 0) return -1; + ret += curr; + if (curr < len) { + break; + } + if (typeof offset != 'undefined') { + offset += curr; + } + } + return ret; + }; + function _fd_write(fd, iov, iovcnt, pnum) { + try { + var stream = SYSCALLS.getStreamFromFD(fd); + var num = doWritev(stream, iov, iovcnt); + HEAPU32[pnum >> 2] = num; + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) throw e; + return e.errno; + } + } + _fd_write.sig = 'iippp'; + var _getaddrinfo = (node, service, hint, out) => { + var addr = 0; + var port = 0; + var flags = 0; + var family = 0; + var type = 0; + var proto = 0; + var ai; + function allocaddrinfo(family, type, proto, canon, addr, port) { + var sa, salen, ai; + var errno; + salen = family === 10 ? 28 : 16; + addr = family === 10 ? inetNtop6(addr) : inetNtop4(addr); + sa = _malloc(salen); + errno = writeSockaddr(sa, family, addr, port); + ai = _malloc(32); + HEAP32[(ai + 4) >> 2] = family; + HEAP32[(ai + 8) >> 2] = type; + HEAP32[(ai + 12) >> 2] = proto; + HEAPU32[(ai + 24) >> 2] = canon; + HEAPU32[(ai + 20) >> 2] = sa; + if (family === 10) { + HEAP32[(ai + 16) >> 2] = 28; + } else { + HEAP32[(ai + 16) >> 2] = 16; + } + HEAP32[(ai + 28) >> 2] = 0; + return ai; + } + if (hint) { + flags = HEAP32[hint >> 2]; + family = HEAP32[(hint + 4) >> 2]; + type = HEAP32[(hint + 8) >> 2]; + proto = HEAP32[(hint + 12) >> 2]; + } + if (type && !proto) { + proto = type === 2 ? 17 : 6; + } + if (!type && proto) { + type = proto === 17 ? 2 : 1; + } + if (proto === 0) { + proto = 6; + } + if (type === 0) { + type = 1; + } + if (!node && !service) { + return -2; + } + if (flags & ~(1 | 2 | 4 | 1024 | 8 | 16 | 32)) { + return -1; + } + if (hint !== 0 && HEAP32[hint >> 2] & 2 && !node) { + return -1; + } + if (flags & 32) { + return -2; + } + if (type !== 0 && type !== 1 && type !== 2) { + return -7; + } + if (family !== 0 && family !== 2 && family !== 10) { + return -6; + } + if (service) { + service = UTF8ToString(service); + port = parseInt(service, 10); + if (isNaN(port)) { + if (flags & 1024) { + return -2; + } + return -8; + } + } + if (!node) { + if (family === 0) { + family = 2; + } + if ((flags & 1) === 0) { + if (family === 2) { + addr = _htonl(2130706433); + } else { + addr = [0, 0, 0, _htonl(1)]; + } + } + ai = allocaddrinfo(family, type, proto, null, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + } + node = UTF8ToString(node); + addr = inetPton4(node); + if (addr !== null) { + if (family === 0 || family === 2) { + family = 2; + } else if (family === 10 && flags & 8) { + addr = [0, 0, _htonl(65535), addr]; + family = 10; + } else { + return -2; + } + } else { + addr = inetPton6(node); + if (addr !== null) { + if (family === 0 || family === 10) { + family = 10; + } else { + return -2; + } + } + } + if (addr != null) { + ai = allocaddrinfo(family, type, proto, node, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + } + if (flags & 4) { + return -2; + } + node = DNS.lookup_name(node); + addr = inetPton4(node); + if (family === 0) { + family = 2; + } else if (family === 10) { + addr = [0, 0, _htonl(65535), addr]; + } + ai = allocaddrinfo(family, type, proto, null, addr, port); + HEAPU32[out >> 2] = ai; + return 0; + }; + _getaddrinfo.sig = 'ipppp'; + var _getnameinfo = (sa, salen, node, nodelen, serv, servlen, flags) => { + var info = readSockaddr(sa, salen); + if (info.errno) { + return -6; + } + var port = info.port; + var addr = info.addr; + var overflowed = false; + if (node && nodelen) { + var lookup; + if (flags & 1 || !(lookup = DNS.lookup_addr(addr))) { + if (flags & 8) { + return -2; + } + } else { + addr = lookup; + } + var numBytesWrittenExclNull = stringToUTF8(addr, node, nodelen); + if (numBytesWrittenExclNull + 1 >= nodelen) { + overflowed = true; + } + } + if (serv && servlen) { + port = '' + port; + var numBytesWrittenExclNull = stringToUTF8(port, serv, servlen); + if (numBytesWrittenExclNull + 1 >= servlen) { + overflowed = true; + } + } + if (overflowed) { + return -12; + } + return 0; + }; + _getnameinfo.sig = 'ipipipii'; + var Protocols = { list: [], map: {} }; + var stringToAscii = (str, buffer) => { + for (var i = 0; i < str.length; ++i) { + HEAP8[buffer++] = str.charCodeAt(i); + } + HEAP8[buffer] = 0; + }; + var _setprotoent = (stayopen) => { + function allocprotoent(name, proto, aliases) { + var nameBuf = _malloc(name.length + 1); + stringToAscii(name, nameBuf); + var j = 0; + var length = aliases.length; + var aliasListBuf = _malloc((length + 1) * 4); + for (var i = 0; i < length; i++, j += 4) { + var alias = aliases[i]; + var aliasBuf = _malloc(alias.length + 1); + stringToAscii(alias, aliasBuf); + HEAPU32[(aliasListBuf + j) >> 2] = aliasBuf; + } + HEAPU32[(aliasListBuf + j) >> 2] = 0; + var pe = _malloc(12); + HEAPU32[pe >> 2] = nameBuf; + HEAPU32[(pe + 4) >> 2] = aliasListBuf; + HEAP32[(pe + 8) >> 2] = proto; + return pe; + } + var list = Protocols.list; + var map = Protocols.map; + if (list.length === 0) { + var entry = allocprotoent('tcp', 6, ['TCP']); + list.push(entry); + map['tcp'] = map['6'] = entry; + entry = allocprotoent('udp', 17, ['UDP']); + list.push(entry); + map['udp'] = map['17'] = entry; + } + _setprotoent.index = 0; + }; + _setprotoent.sig = 'vi'; + var _getprotobyname = (name) => { + name = UTF8ToString(name); + _setprotoent(true); + var result = Protocols.map[name]; + return result; + }; + _getprotobyname.sig = 'pp'; + var _getprotobynumber = (number) => { + _setprotoent(true); + var result = Protocols.map[number]; + return result; + }; + _getprotobynumber.sig = 'pi'; + function _js_open_process( + command, + argsPtr, + argsLength, + descriptorsPtr, + descriptorsLength, + cwdPtr, + cwdLength, + envPtr, + envLength + ) { + if (!command) { + ___errno_location(ERRNO_CODES.EINVAL); + return -1; + } + const cmdstr = UTF8ToString(command); + if (!cmdstr.length) { + ___errno_location(ERRNO_CODES.EINVAL); + return -1; + } + let argsArray = []; + if (argsLength) { + for (var i = 0; i < argsLength; i++) { + const charPointer = argsPtr + i * 4; + argsArray.push(UTF8ToString(HEAPU32[charPointer >> 2])); + } + } + const cwdstr = cwdPtr ? UTF8ToString(cwdPtr) : FS.cwd(); + let envObject = null; + if (envLength) { + envObject = {}; + for (var i = 0; i < envLength; i++) { + const envPointer = envPtr + i * 4; + const envEntry = UTF8ToString(HEAPU32[envPointer >> 2]); + const splitAt = envEntry.indexOf('='); + if (splitAt === -1) { + continue; + } + const key = envEntry.substring(0, splitAt); + const value = envEntry.substring(splitAt + 1); + envObject[key] = value; + } + } + var std = {}; + for (var i = 0; i < descriptorsLength; i++) { + const descriptorPtr = HEAPU32[(descriptorsPtr + i * 4) >> 2]; + std[HEAPU32[descriptorPtr >> 2]] = { + child: HEAPU32[(descriptorPtr + 4) >> 2], + parent: HEAPU32[(descriptorPtr + 8) >> 2], + }; + if (i === 0) { + HEAPU32[(descriptorPtr + 8) >> 2] = + std[HEAPU32[descriptorPtr >> 2]].parent; + HEAPU32[(descriptorPtr + 4) >> 2] = + std[HEAPU32[descriptorPtr >> 2]].child; + } + } + return Asyncify.handleAsync(async () => { + let cp; + try { + const options = {}; + if (cwdstr !== null) { + options.cwd = cwdstr; + } + if (envObject !== null) { + options.env = envObject; + } + cp = PHPWASM.spawnProcess(cmdstr, argsArray, options); + if (cp instanceof Promise) { + cp = await cp; + } + } catch (e) { + if (e.code === 'SPAWN_UNSUPPORTED') { + ___errno_location(ERRNO_CODES.ENOSYS); + return -1; + } + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) + throw e; + ___errno_location(e.code); + return -1; + } + const ProcInfo = { pid: cp.pid, exited: false }; + PHPWASM.processTable[ProcInfo.pid] = ProcInfo; + const stdinParentFd = std[0]?.parent, + stdinChildFd = std[0]?.child, + stdoutChildFd = std[1]?.child, + stdoutParentFd = std[1]?.parent, + stderrChildFd = std[2]?.child, + stderrParentFd = std[2]?.parent; + const detachPipeDataListeners = []; + cp.on('exit', function (code) { + for (const detach of detachPipeDataListeners) { + detach(); + } + for (const fd of [stdoutChildFd, stderrChildFd, stdinChildFd]) { + if (FS.streams[fd] && !FS.isClosed(FS.streams[fd])) { + FS.close(FS.streams[fd]); + } + } + ProcInfo.exitCode = code; + ProcInfo.exited = true; + }); + if (stdoutChildFd) { + const stdoutStream = SYSCALLS.getStreamFromFD(stdoutChildFd); + let stdoutAt = 0; + const onStdoutData = function (data) { + try { + stdoutStream.stream_ops.write( + stdoutStream, + data, + 0, + data.length, + stdoutAt + ); + stdoutAt += data.length; + } catch { + cp.stdout.off('data', onStdoutData); + } + }; + cp.stdout.on('data', onStdoutData); + detachPipeDataListeners.push(() => + cp.stdout.off('data', onStdoutData) + ); + } + if (stderrChildFd) { + const stderrStream = SYSCALLS.getStreamFromFD(stderrChildFd); + let stderrAt = 0; + const onStderrData = function (data) { + try { + stderrStream.stream_ops.write( + stderrStream, + data, + 0, + data.length, + stderrAt + ); + stderrAt += data.length; + } catch { + cp.stderr.off('data', onStderrData); + } + }; + cp.stderr.on('data', onStderrData); + detachPipeDataListeners.push(() => + cp.stderr.off('data', onStderrData) + ); + } + try { + await new Promise((resolve, reject) => { + let resolved = false; + cp.on('spawn', () => { + if (resolved) return; + resolved = true; + resolve(); + }); + cp.on('error', (e) => { + if (resolved) return; + resolved = true; + reject(e); + }); + cp.on('exit', function (code) { + if (resolved) return; + resolved = true; + if (code === 0) { + resolve(); + } else { + reject( + new Error(`Process exited with code ${code}`) + ); + } + }); + setTimeout(() => { + if (resolved) return; + resolved = true; + reject(new Error('Process timed out')); + }, 5e3); + }); + } catch (e) { + console.error(e); + return ProcInfo.pid; + } + if (stdinChildFd) { + let stdinStream; + try { + stdinStream = SYSCALLS.getStreamFromFD(stdinChildFd); + } catch (e) { + ___errno_location(ERRNO_CODES.EBADF); + return ProcInfo.pid; + } + if (!stdinStream?.node) { + return ProcInfo.pid; + } + const CHUNK_SIZE = 1024; + const iov = _malloc(16); + const pnum = _malloc(4); + const buffer = _malloc(CHUNK_SIZE); + HEAPU32[iov >> 2] = buffer; + HEAPU32[(iov + 4) >> 2] = CHUNK_SIZE; + function pump() { + try { + while (true) { + if (cp.killed) { + stopPumpingAndCloseStdin(); + return; + } + const result = js_fd_read( + stdinChildFd, + iov, + 1, + pnum, + false + ); + const bytesRead = HEAPU32[pnum >> 2]; + if (result === 0 && bytesRead > 0) { + const wrote = HEAPU8.subarray( + buffer, + buffer + bytesRead + ); + cp.stdin.write(wrote); + } else if (result === 0 && bytesRead === 0) { + stopPumpingAndCloseStdin(); + break; + } else if (result === ERRNO_CODES.EAGAIN) { + break; + } else { + throw new FS.ErrnoError(result); + } + } + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) { + throw e; + } + ___errno_location(e.errno); + stopPumpingAndCloseStdin(); + } + } + function stopPumpingAndCloseStdin() { + clearInterval(interval); + if (!cp.stdin.closed) { + cp.stdin.end(); + } + _wasm_free(buffer); + _wasm_free(iov); + _wasm_free(pnum); + } + const interval = setInterval(pump, 20); + pump(); + } + return ProcInfo.pid; + }); + } + var arraySum = (array, index) => { + var sum = 0; + for (var i = 0; i <= index; sum += array[i++]) {} + return sum; + }; + var MONTH_DAYS_LEAP = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + var MONTH_DAYS_REGULAR = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + var addDays = (date, days) => { + var newDate = new Date(date.getTime()); + while (days > 0) { + var leap = isLeapYear(newDate.getFullYear()); + var currentMonth = newDate.getMonth(); + var daysInCurrentMonth = ( + leap ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR + )[currentMonth]; + if (days > daysInCurrentMonth - newDate.getDate()) { + days -= daysInCurrentMonth - newDate.getDate() + 1; + newDate.setDate(1); + if (currentMonth < 11) { + newDate.setMonth(currentMonth + 1); + } else { + newDate.setMonth(0); + newDate.setFullYear(newDate.getFullYear() + 1); + } + } else { + newDate.setDate(newDate.getDate() + days); + return newDate; + } + } + return newDate; + }; + var _strptime = (buf, format, tm) => { + var pattern = UTF8ToString(format); + var SPECIAL_CHARS = '\\!@#$^&*()+=-[]/{}|:<>?,.'; + for (var i = 0, ii = SPECIAL_CHARS.length; i < ii; ++i) { + pattern = pattern.replace( + new RegExp('\\' + SPECIAL_CHARS[i], 'g'), + '\\' + SPECIAL_CHARS[i] + ); + } + var EQUIVALENT_MATCHERS = { + A: '%a', + B: '%b', + c: '%a %b %d %H:%M:%S %Y', + D: '%m\\/%d\\/%y', + e: '%d', + F: '%Y-%m-%d', + h: '%b', + R: '%H\\:%M', + r: '%I\\:%M\\:%S\\s%p', + T: '%H\\:%M\\:%S', + x: '%m\\/%d\\/(?:%y|%Y)', + X: '%H\\:%M\\:%S', + }; + var DATE_PATTERNS = { + a: '(?:Sun(?:day)?)|(?:Mon(?:day)?)|(?:Tue(?:sday)?)|(?:Wed(?:nesday)?)|(?:Thu(?:rsday)?)|(?:Fri(?:day)?)|(?:Sat(?:urday)?)', + b: '(?:Jan(?:uary)?)|(?:Feb(?:ruary)?)|(?:Mar(?:ch)?)|(?:Apr(?:il)?)|May|(?:Jun(?:e)?)|(?:Jul(?:y)?)|(?:Aug(?:ust)?)|(?:Sep(?:tember)?)|(?:Oct(?:ober)?)|(?:Nov(?:ember)?)|(?:Dec(?:ember)?)', + C: '\\d\\d', + d: '0[1-9]|[1-9](?!\\d)|1\\d|2\\d|30|31', + H: '\\d(?!\\d)|[0,1]\\d|20|21|22|23', + I: '\\d(?!\\d)|0\\d|10|11|12', + j: '00[1-9]|0?[1-9](?!\\d)|0?[1-9]\\d(?!\\d)|[1,2]\\d\\d|3[0-6]\\d', + m: '0[1-9]|[1-9](?!\\d)|10|11|12', + M: '0\\d|\\d(?!\\d)|[1-5]\\d', + n: ' ', + p: 'AM|am|PM|pm|A\\.M\\.|a\\.m\\.|P\\.M\\.|p\\.m\\.', + S: '0\\d|\\d(?!\\d)|[1-5]\\d|60', + U: '0\\d|\\d(?!\\d)|[1-4]\\d|50|51|52|53', + W: '0\\d|\\d(?!\\d)|[1-4]\\d|50|51|52|53', + w: '[0-6]', + y: '\\d\\d', + Y: '\\d\\d\\d\\d', + t: ' ', + z: 'Z|(?:[\\+\\-]\\d\\d:?(?:\\d\\d)?)', + }; + var MONTH_NUMBERS = { + JAN: 0, + FEB: 1, + MAR: 2, + APR: 3, + MAY: 4, + JUN: 5, + JUL: 6, + AUG: 7, + SEP: 8, + OCT: 9, + NOV: 10, + DEC: 11, + }; + var DAY_NUMBERS_SUN_FIRST = { + SUN: 0, + MON: 1, + TUE: 2, + WED: 3, + THU: 4, + FRI: 5, + SAT: 6, + }; + var DAY_NUMBERS_MON_FIRST = { + MON: 0, + TUE: 1, + WED: 2, + THU: 3, + FRI: 4, + SAT: 5, + SUN: 6, + }; + var capture = []; + var pattern_out = pattern + .replace(/%(.)/g, (m, c) => EQUIVALENT_MATCHERS[c] || m) + .replace(/%(.)/g, (_, c) => { + let pat = DATE_PATTERNS[c]; + if (pat) { + capture.push(c); + return `(${pat})`; + } else { + return c; + } + }) + .replace(/\s+/g, '\\s*'); + var matches = new RegExp('^' + pattern_out, 'i').exec( + UTF8ToString(buf) + ); + function initDate() { + function fixup(value, min, max) { + return typeof value != 'number' || isNaN(value) + ? min + : value >= min + ? value <= max + ? value + : max + : min; + } + return { + year: fixup(HEAP32[(tm + 20) >> 2] + 1900, 1970, 9999), + month: fixup(HEAP32[(tm + 16) >> 2], 0, 11), + day: fixup(HEAP32[(tm + 12) >> 2], 1, 31), + hour: fixup(HEAP32[(tm + 8) >> 2], 0, 23), + min: fixup(HEAP32[(tm + 4) >> 2], 0, 59), + sec: fixup(HEAP32[tm >> 2], 0, 59), + gmtoff: 0, + }; + } + if (matches) { + var date = initDate(); + var value; + var getMatch = (symbol) => { + var pos = capture.indexOf(symbol); + if (pos >= 0) { + return matches[pos + 1]; + } + return; + }; + if ((value = getMatch('S'))) { + date.sec = Number(value); + } + if ((value = getMatch('M'))) { + date.min = Number(value); + } + if ((value = getMatch('H'))) { + date.hour = Number(value); + } else if ((value = getMatch('I'))) { + var hour = Number(value); + if ((value = getMatch('p'))) { + hour += value.toUpperCase()[0] === 'P' ? 12 : 0; + } + date.hour = hour; + } + if ((value = getMatch('Y'))) { + date.year = Number(value); + } else if ((value = getMatch('y'))) { + var year = Number(value); + if ((value = getMatch('C'))) { + year += Number(value) * 100; + } else { + year += year < 69 ? 2e3 : 1900; + } + date.year = year; + } + if ((value = getMatch('m'))) { + date.month = Number(value) - 1; + } else if ((value = getMatch('b'))) { + date.month = + MONTH_NUMBERS[value.substring(0, 3).toUpperCase()] || 0; + } + if ((value = getMatch('d'))) { + date.day = Number(value); + } else if ((value = getMatch('j'))) { + var day = Number(value); + var leapYear = isLeapYear(date.year); + for (var month = 0; month < 12; ++month) { + var daysUntilMonth = arraySum( + leapYear ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR, + month - 1 + ); + if ( + day <= + daysUntilMonth + + (leapYear ? MONTH_DAYS_LEAP : MONTH_DAYS_REGULAR)[ + month + ] + ) { + date.day = day - daysUntilMonth; + } + } + } else if ((value = getMatch('a'))) { + var weekDay = value.substring(0, 3).toUpperCase(); + if ((value = getMatch('U'))) { + var weekDayNumber = DAY_NUMBERS_SUN_FIRST[weekDay]; + var weekNumber = Number(value); + var janFirst = new Date(date.year, 0, 1); + var endDate; + if (janFirst.getDay() === 0) { + endDate = addDays( + janFirst, + weekDayNumber + 7 * (weekNumber - 1) + ); + } else { + endDate = addDays( + janFirst, + 7 - + janFirst.getDay() + + weekDayNumber + + 7 * (weekNumber - 1) + ); + } + date.day = endDate.getDate(); + date.month = endDate.getMonth(); + } else if ((value = getMatch('W'))) { + var weekDayNumber = DAY_NUMBERS_MON_FIRST[weekDay]; + var weekNumber = Number(value); + var janFirst = new Date(date.year, 0, 1); + var endDate; + if (janFirst.getDay() === 1) { + endDate = addDays( + janFirst, + weekDayNumber + 7 * (weekNumber - 1) + ); + } else { + endDate = addDays( + janFirst, + 7 - + janFirst.getDay() + + 1 + + weekDayNumber + + 7 * (weekNumber - 1) + ); + } + date.day = endDate.getDate(); + date.month = endDate.getMonth(); + } + } + if ((value = getMatch('z'))) { + if (value.toLowerCase() === 'z') { + date.gmtoff = 0; + } else { + var match = value.match(/^((?:\-|\+)\d\d):?(\d\d)?/); + date.gmtoff = match[1] * 3600; + if (match[2]) { + date.gmtoff += + date.gmtoff > 0 ? match[2] * 60 : -match[2] * 60; + } + } + } + var fullDate = new Date( + date.year, + date.month, + date.day, + date.hour, + date.min, + date.sec, + 0 + ); + HEAP32[tm >> 2] = fullDate.getSeconds(); + HEAP32[(tm + 4) >> 2] = fullDate.getMinutes(); + HEAP32[(tm + 8) >> 2] = fullDate.getHours(); + HEAP32[(tm + 12) >> 2] = fullDate.getDate(); + HEAP32[(tm + 16) >> 2] = fullDate.getMonth(); + HEAP32[(tm + 20) >> 2] = fullDate.getFullYear() - 1900; + HEAP32[(tm + 24) >> 2] = fullDate.getDay(); + HEAP32[(tm + 28) >> 2] = + arraySum( + isLeapYear(fullDate.getFullYear()) + ? MONTH_DAYS_LEAP + : MONTH_DAYS_REGULAR, + fullDate.getMonth() - 1 + ) + + fullDate.getDate() - + 1; + HEAP32[(tm + 32) >> 2] = 0; + HEAP32[(tm + 36) >> 2] = date.gmtoff; + return buf + lengthBytesUTF8(matches[0]); + } + return 0; + }; + _strptime.sig = 'pppp'; + function _wasm_setsockopt( + socketd, + level, + optionName, + optionValuePtr, + optionLen + ) { + const optionValue = HEAPU8[optionValuePtr]; + const SOL_SOCKET = 1; + const SO_KEEPALIVE = 9; + const SO_RCVTIMEO = 66; + const SO_SNDTIMEO = 67; + const IPPROTO_TCP = 6; + const TCP_NODELAY = 1; + const isForwardable = + (level === SOL_SOCKET && optionName === SO_KEEPALIVE) || + (level === IPPROTO_TCP && optionName === TCP_NODELAY); + const isIgnorable = + level === SOL_SOCKET && + (optionName === SO_RCVTIMEO || optionName === SO_SNDTIMEO); + if (!isForwardable && !isIgnorable) { + console.warn( + `Unsupported socket option: ${level}, ${optionName}, ${optionValue}` + ); + return -1; + } + if (isIgnorable) { + return 0; + } + const ws = PHPWASM.getAllWebSockets(socketd)[0]; + if (!ws) { + return -1; + } + ws.setSocketOpt(level, optionName, optionValuePtr); + return 0; + } + var Asyncify = { + instrumentWasmImports(imports) { + var importPattern = + /^(js_open_process|js_fd_read|js_waitpid|js_process_status|js_create_input_device|wasm_setsockopt|wasm_shutdown|wasm_close|wasm_recv|wasm_connect|__syscall_fcntl64|js_flock|js_release_file_locks|js_waitpid|invoke_.*|__asyncjs__.*)$/; + for (let [x, original] of Object.entries(imports)) { + if (typeof original == 'function') { + let isAsyncifyImport = + original.isAsync || importPattern.test(x); + if (isAsyncifyImport) { + imports[x] = original = new WebAssembly.Suspending( + original + ); + } + } + } + }, + instrumentFunction(original) { + var wrapper = (...args) => original(...args); + wrapper.orig = original; + return wrapper; + }, + instrumentWasmExports(exports) { + var exportPattern = + /^(php_wasm_init|wasm_sleep|wasm_read|emscripten_sleep|wasm_sapi_handle_request|wasm_sapi_request_shutdown|wasm_poll_socket|wrap_select|__wrap_select|select|php_pollfd_for|fflush|wasm_popen|wasm_read|wasm_php_exec|run_cli|wasm_recv|wasm_connect|__wasm_call_ctors|__errno_location|__funcs_on_exit|main|__main_argc_argv)$/; + Asyncify.asyncExports = new Set(); + var ret = {}; + for (let [x, original] of Object.entries(exports)) { + if (typeof original == 'function') { + let isAsyncifyExport = exportPattern.test(x); + if (isAsyncifyExport) { + Asyncify.asyncExports.add(original); + original = Asyncify.makeAsyncFunction(original); + } + var wrapper = Asyncify.instrumentFunction(original); + ret[x] = wrapper; + } else { + ret[x] = original; + } + } + return ret; + }, + asyncExports: null, + isAsyncExport(func) { + return Asyncify.asyncExports?.has(func); + }, + handleAsync: async (startAsync) => { + runtimeKeepalivePush(); + try { + return await startAsync(); + } finally { + runtimeKeepalivePop(); + } + }, + handleSleep: (startAsync) => + Asyncify.handleAsync(() => new Promise(startAsync)), + makeAsyncFunction(original) { + return WebAssembly.promising(original); + }, + }; + var getCFunc = (ident) => { + var func = Module['_' + ident]; + return func; + }; + var writeArrayToMemory = (array, buffer) => { + HEAP8.set(array, buffer); + }; + var ccall = (ident, returnType, argTypes, args, opts) => { + var toC = { + string: (str) => { + var ret = 0; + if (str !== null && str !== undefined && str !== 0) { + ret = stringToUTF8OnStack(str); + } + return ret; + }, + array: (arr) => { + var ret = stackAlloc(arr.length); + writeArrayToMemory(arr, ret); + return ret; + }, + }; + function convertReturnValue(ret) { + if (returnType === 'string') { + return UTF8ToString(ret); + } + if (returnType === 'boolean') return Boolean(ret); + return ret; + } + var func = getCFunc(ident); + var cArgs = []; + var stack = 0; + if (args) { + for (var i = 0; i < args.length; i++) { + var converter = toC[argTypes[i]]; + if (converter) { + if (stack === 0) stack = stackSave(); + cArgs[i] = converter(args[i]); + } else { + cArgs[i] = args[i]; + } + } + } + var ret = func(...cArgs); + function onDone(ret) { + if (stack !== 0) stackRestore(stack); + return convertReturnValue(ret); + } + var asyncMode = opts?.async; + if (asyncMode) return ret.then(onDone); + ret = onDone(ret); + return ret; + }; + var FS_createPath = (...args) => FS.createPath(...args); + var FS_unlink = (...args) => FS.unlink(...args); + var FS_createLazyFile = (...args) => FS.createLazyFile(...args); + var FS_createDevice = (...args) => FS.createDevice(...args); + registerWasmPlugin(); + FS.createPreloadedFile = FS_createPreloadedFile; + FS.preloadFile = FS_preloadFile; + FS.staticInit(); + PHPWASM.init(); + { + if (Module['preloadPlugins']) preloadPlugins = Module['preloadPlugins']; + if (Module['noExitRuntime']) noExitRuntime = Module['noExitRuntime']; + if (Module['print']) out = Module['print']; + if (Module['printErr']) err = Module['printErr']; + if (Module['dynamicLibraries']) + dynamicLibraries = Module['dynamicLibraries']; + if (Module['wasmBinary']) wasmBinary = Module['wasmBinary']; + if (Module['arguments']) arguments_ = Module['arguments']; + if (Module['thisProgram']) thisProgram = Module['thisProgram']; + if (Module['quit']) quit_ = Module['quit']; + if (Module['preInit']) { + if (typeof Module['preInit'] == 'function') + Module['preInit'] = [Module['preInit']]; + while (Module['preInit'].length > 0) { + Module['preInit'].shift()(); + } + } + } + Module['wasmExports'] = wasmExports; + Module['addRunDependency'] = addRunDependency; + Module['removeRunDependency'] = removeRunDependency; + Module['ccall'] = ccall; + Module['FS_preloadFile'] = FS_preloadFile; + Module['FS_unlink'] = FS_unlink; + Module['FS_createPath'] = FS_createPath; + Module['FS_createDevice'] = FS_createDevice; + Module['FS_createDataFile'] = FS_createDataFile; + Module['FS_createLazyFile'] = FS_createLazyFile; + Module['PROXYFS'] = PROXYFS; + Module['UTF8ToString'] = UTF8ToString; + Module['lengthBytesUTF8'] = lengthBytesUTF8; + Module['stringToUTF8'] = stringToUTF8; + Module['FS'] = FS; + Module['_exit'] = _exit; + Module['_emscripten_sleep'] = _emscripten_sleep; + var ASM_CONSTS = {}; + function __asyncjs__js_popen_to_file(command, mode, exitCodePtr) { + return Asyncify.handleAsync(async () => { + const returnCallback = (resolver) => new Promise(resolver); + if (!command) return 1; + const cmdstr = UTF8ToString(command); + if (!cmdstr.length) return 0; + const modestr = UTF8ToString(mode); + if (!modestr.length) return 0; + if (modestr === 'w') { + console.error('popen($cmd, "w") is not implemented yet'); + } + return returnCallback(async (wakeUp) => { + let cp; + try { + cp = PHPWASM.spawnProcess(cmdstr, []); + if (cp instanceof Promise) { + cp = await cp; + } + } catch (e) { + console.error(e); + if (e.code === 'SPAWN_UNSUPPORTED') { + return 1; + } + throw e; + } + const outByteArrays = []; + cp.stdout.on('data', function (data) { + outByteArrays.push(data); + }); + const outputPath = '/tmp/popen_output'; + cp.on('exit', function (exitCode) { + const outBytes = new Uint8Array( + outByteArrays.reduce( + (acc, curr) => acc + curr.length, + 0 + ) + ); + let offset = 0; + for (const byteArray of outByteArrays) { + outBytes.set(byteArray, offset); + offset += byteArray.length; + } + FS.writeFile(outputPath, outBytes); + HEAPU8[exitCodePtr] = exitCode; + wakeUp(allocateUTF8OnStack(outputPath)); + }); + }); + }); + } + __asyncjs__js_popen_to_file.sig = 'iiii'; + function __asyncjs__wasm_poll_socket(socketd, events, timeout) { + return Asyncify.handleAsync(async () => { + const returnCallback = (resolver) => new Promise(resolver); + const POLLIN = 1; + const POLLPRI = 2; + const POLLOUT = 4; + const POLLERR = 8; + const POLLHUP = 16; + const POLLNVAL = 32; + return returnCallback((wakeUp) => { + const polls = []; + const stream = FS.getStream(socketd); + if (FS.isSocket(stream?.node.mode)) { + const sock = getSocketFromFD(socketd); + if (!sock) { + wakeUp(0); + return; + } + const lookingFor = new Set(); + if (events & POLLIN || events & POLLPRI) { + if (sock.server) { + for (const client of sock.pending) { + if ((client.recv_queue || []).length > 0) { + wakeUp(1); + return; + } + } + } else if ((sock.recv_queue || []).length > 0) { + wakeUp(1); + return; + } + } + const webSockets = PHPWASM.getAllWebSockets(sock); + if (!webSockets.length) { + wakeUp(0); + return; + } + for (const ws of webSockets) { + if (events & POLLIN || events & POLLPRI) { + polls.push(PHPWASM.awaitData(ws)); + lookingFor.add('POLLIN'); + } + if (events & POLLOUT) { + polls.push(PHPWASM.awaitConnection(ws)); + lookingFor.add('POLLOUT'); + } + if ( + events & POLLHUP || + events & POLLIN || + events & POLLOUT || + events & POLLERR + ) { + polls.push(PHPWASM.awaitClose(ws)); + lookingFor.add('POLLHUP'); + } + if (events & POLLERR || events & POLLNVAL) { + polls.push(PHPWASM.awaitError(ws)); + lookingFor.add('POLLERR'); + } + } + } else if (stream?.stream_ops?.poll) { + let interrupted = false; + async function poll() { + try { + while (true) { + var mask = POLLNVAL; + mask = SYSCALLS.DEFAULT_POLLMASK; + if (FS.isClosed(stream)) { + return ERRNO_CODES.EBADF; + } + if (stream.stream_ops?.poll) { + mask = stream.stream_ops.poll(stream, -1); + } + mask &= events | POLLERR | POLLHUP; + if (mask) { + return mask; + } + if (interrupted) { + return ERRNO_CODES.ETIMEDOUT; + } + await new Promise((resolve) => + setTimeout(resolve, 10) + ); + } + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) + throw e; + return -e.errno; + } + } + polls.push([ + poll(), + () => { + interrupted = true; + }, + ]); + } else { + setTimeout(function () { + wakeUp(1); + }, timeout); + return; + } + if (polls.length === 0) { + console.warn( + 'Unsupported poll event ' + + events + + ', defaulting to setTimeout().' + ); + setTimeout(function () { + wakeUp(0); + }, timeout); + return; + } + const promises = polls.map(([promise]) => promise); + const clearPolling = () => + polls.forEach(([, clear]) => clear()); + let awaken = false; + let timeoutId; + Promise.race(promises).then(function (results) { + if (!awaken) { + awaken = true; + wakeUp(1); + if (timeoutId) { + clearTimeout(timeoutId); + } + clearPolling(); + } + }); + if (timeout !== -1) { + timeoutId = setTimeout(function () { + if (!awaken) { + awaken = true; + wakeUp(0); + clearPolling(); + } + }, timeout); + } + }); + }); + } + __asyncjs__wasm_poll_socket.sig = 'iiii'; + function js_fd_read(fd, iov, iovcnt, pnum) { + const returnCallback = (resolver) => new Promise(resolver); + const pollAsync = arguments[4] === undefined ? true : !!arguments[4]; + if ( + Asyncify?.State?.Normal === undefined || + Asyncify?.state === Asyncify?.State?.Normal + ) { + var stream; + try { + stream = SYSCALLS.getStreamFromFD(fd); + HEAPU32[pnum >> 2] = doReadv(stream, iov, iovcnt); + return 0; + } catch (e) { + if (typeof FS == 'undefined' || !(e.name === 'ErrnoError')) { + throw e; + } + if ( + e.errno !== ERRNO_CODES.EWOULDBLOCK && + e.errno !== ERRNO_CODES.EAGAIN + ) { + return e.errno; + } + const nonBlocking = stream.flags & PHPWASM.O_NONBLOCK; + if (nonBlocking) { + return e.errno; + } + } + } + if (false === pollAsync) { + return ERRNO_CODES.EWOULDBLOCK; + } + return returnCallback(async (wakeUp) => { + var retries = 0; + var interval = 50; + var timeout = 5e3; + var maxRetries = timeout / interval; + while (true) { + var returnCode; + var stream; + let num; + try { + stream = SYSCALLS.getStreamFromFD(fd); + num = doReadv(stream, iov, iovcnt); + returnCode = 0; + } catch (e) { + if ( + typeof FS == 'undefined' || + !(e.name === 'ErrnoError') + ) { + console.error(e); + throw e; + } + returnCode = e.errno; + } + if (returnCode === 0) { + HEAPU32[pnum >> 2] = num; + return wakeUp(0); + } + if ( + ++retries > maxRetries || + !stream || + FS.isClosed(stream) || + returnCode !== ERRNO_CODES.EWOULDBLOCK || + ('pipe' in stream.node && stream.node.pipe.refcnt < 2) + ) { + HEAPU32[pnum >> 2] = num; + return wakeUp(returnCode); + } + await new Promise((resolve) => setTimeout(resolve, interval)); + } + }); + } + js_fd_read.sig = 'iiiii'; + function __asyncjs__js_module_onMessage(data, response_buffer) { + return Asyncify.handleAsync(async () => { + if (Module['onMessage']) { + const dataStr = UTF8ToString(data); + return Module['onMessage'](dataStr) + .then((response) => { + const responseBytes = + typeof response === 'string' + ? new TextEncoder().encode(response) + : response; + const responseSize = responseBytes.byteLength; + const responsePtr = _malloc(responseSize + 1); + HEAPU8.set(responseBytes, responsePtr); + HEAPU8[responsePtr + responseSize] = 0; + HEAPU8[response_buffer] = responsePtr; + HEAPU8[response_buffer + 1] = responsePtr >> 8; + HEAPU8[response_buffer + 2] = responsePtr >> 16; + HEAPU8[response_buffer + 3] = responsePtr >> 24; + return responseSize; + }) + .catch((e) => { + console.error(e); + return -1; + }); + } + }); + } + __asyncjs__js_module_onMessage.sig = 'iii'; + var _calloc, + _malloc, + ___errno_location, + _wasm_sleep, + _ntohs, + _htons, + _htonl, + _wasm_read, + _fflush, + _php_pollfd_for, + _wasm_php_exec, + ___wrap_usleep, + _wasm_popen, + ___wrap_select, + _wasm_set_sapi_name, + _wasm_set_phpini_path, + _wasm_add_cli_arg, + _run_cli, + _wasm_add_SERVER_entry, + _wasm_add_ENV_entry, + _wasm_set_query_string, + _wasm_set_path_translated, + _wasm_set_skip_shebang, + _wasm_set_request_uri, + _wasm_set_request_method, + _wasm_set_request_host, + _wasm_set_content_type, + _wasm_set_request_body, + _wasm_set_content_length, + _wasm_set_cookies, + _wasm_set_request_port, + _wasm_sapi_request_shutdown, + _wasm_sapi_handle_request, + _php_wasm_init, + _wasm_free, + _wasm_trace, + _initgroups, + ___funcs_on_exit, + ___dl_seterr, + __emscripten_find_dylib, + _emscripten_builtin_memalign, + __emscripten_timeout, + _emscripten_get_sbrk_ptr, + ___trap, + __emscripten_stack_restore, + __emscripten_stack_alloc, + _emscripten_stack_get_current, + __ZNSt3__211__call_onceERVmPvPFvS2_E, + __ZNSt3__218condition_variable10notify_allEv, + __ZNSt3__25mutex4lockEv, + __ZNSt3__25mutex6unlockEv, + memory, + __indirect_function_table, + wasmTable, + wasmMemory; + function assignWasmExports(wasmExports) { + _calloc = wasmExports['calloc']; + _malloc = + PHPLoader['malloc'] = + Module['_malloc'] = + wasmExports['malloc']; + ___errno_location = Module['___errno_location'] = + wasmExports['__errno_location']; + _wasm_sleep = Module['_wasm_sleep'] = wasmExports['wasm_sleep']; + _ntohs = wasmExports['ntohs']; + _htons = wasmExports['htons']; + _htonl = wasmExports['htonl']; + _wasm_read = Module['_wasm_read'] = wasmExports['wasm_read']; + _fflush = wasmExports['fflush']; + _php_pollfd_for = Module['_php_pollfd_for'] = + wasmExports['php_pollfd_for']; + _wasm_php_exec = Module['_wasm_php_exec'] = + wasmExports['wasm_php_exec']; + ___wrap_usleep = Module['___wrap_usleep'] = + wasmExports['__wrap_usleep']; + _wasm_popen = Module['_wasm_popen'] = wasmExports['wasm_popen']; + ___wrap_select = Module['___wrap_select'] = + wasmExports['__wrap_select']; + _wasm_set_sapi_name = Module['_wasm_set_sapi_name'] = + wasmExports['wasm_set_sapi_name']; + _wasm_set_phpini_path = Module['_wasm_set_phpini_path'] = + wasmExports['wasm_set_phpini_path']; + _wasm_add_cli_arg = Module['_wasm_add_cli_arg'] = + wasmExports['wasm_add_cli_arg']; + _run_cli = Module['_run_cli'] = wasmExports['run_cli']; + _wasm_add_SERVER_entry = Module['_wasm_add_SERVER_entry'] = + wasmExports['wasm_add_SERVER_entry']; + _wasm_add_ENV_entry = Module['_wasm_add_ENV_entry'] = + wasmExports['wasm_add_ENV_entry']; + _wasm_set_query_string = Module['_wasm_set_query_string'] = + wasmExports['wasm_set_query_string']; + _wasm_set_path_translated = Module['_wasm_set_path_translated'] = + wasmExports['wasm_set_path_translated']; + _wasm_set_skip_shebang = Module['_wasm_set_skip_shebang'] = + wasmExports['wasm_set_skip_shebang']; + _wasm_set_request_uri = Module['_wasm_set_request_uri'] = + wasmExports['wasm_set_request_uri']; + _wasm_set_request_method = Module['_wasm_set_request_method'] = + wasmExports['wasm_set_request_method']; + _wasm_set_request_host = Module['_wasm_set_request_host'] = + wasmExports['wasm_set_request_host']; + _wasm_set_content_type = Module['_wasm_set_content_type'] = + wasmExports['wasm_set_content_type']; + _wasm_set_request_body = Module['_wasm_set_request_body'] = + wasmExports['wasm_set_request_body']; + _wasm_set_content_length = Module['_wasm_set_content_length'] = + wasmExports['wasm_set_content_length']; + _wasm_set_cookies = Module['_wasm_set_cookies'] = + wasmExports['wasm_set_cookies']; + _wasm_set_request_port = Module['_wasm_set_request_port'] = + wasmExports['wasm_set_request_port']; + _wasm_sapi_request_shutdown = Module['_wasm_sapi_request_shutdown'] = + wasmExports['wasm_sapi_request_shutdown']; + _wasm_sapi_handle_request = Module['_wasm_sapi_handle_request'] = + wasmExports['wasm_sapi_handle_request']; + _php_wasm_init = Module['_php_wasm_init'] = + wasmExports['php_wasm_init']; + _wasm_free = + PHPLoader['free'] = + Module['_wasm_free'] = + wasmExports['wasm_free']; + _wasm_trace = Module['_wasm_trace'] = wasmExports['wasm_trace']; + _initgroups = Module['_initgroups'] = wasmExports['initgroups']; + ___funcs_on_exit = wasmExports['__funcs_on_exit']; + ___dl_seterr = wasmExports['__dl_seterr']; + __emscripten_find_dylib = wasmExports['_emscripten_find_dylib']; + _emscripten_builtin_memalign = + wasmExports['emscripten_builtin_memalign']; + __emscripten_timeout = wasmExports['_emscripten_timeout']; + _emscripten_get_sbrk_ptr = wasmExports['emscripten_get_sbrk_ptr']; + ___trap = wasmExports['__trap']; + __emscripten_stack_restore = wasmExports['_emscripten_stack_restore']; + __emscripten_stack_alloc = wasmExports['_emscripten_stack_alloc']; + _emscripten_stack_get_current = + wasmExports['emscripten_stack_get_current']; + __ZNSt3__211__call_onceERVmPvPFvS2_E = Module[ + '__ZNSt3__211__call_onceERVmPvPFvS2_E' + ] = wasmExports['_ZNSt3__211__call_onceERVmPvPFvS2_E']; + __ZNSt3__218condition_variable10notify_allEv = Module[ + '__ZNSt3__218condition_variable10notify_allEv' + ] = wasmExports['_ZNSt3__218condition_variable10notify_allEv']; + __ZNSt3__25mutex4lockEv = Module['__ZNSt3__25mutex4lockEv'] = + wasmExports['_ZNSt3__25mutex4lockEv']; + __ZNSt3__25mutex6unlockEv = Module['__ZNSt3__25mutex6unlockEv'] = + wasmExports['_ZNSt3__25mutex6unlockEv']; + memory = wasmMemory = wasmExports['memory']; + __indirect_function_table = wasmTable = + wasmExports['__indirect_function_table']; + } + var ___heap_base = 5123904; + var wasmImports = { + __assert_fail: ___assert_fail, + __asyncjs__js_module_onMessage, + __asyncjs__js_popen_to_file, + __asyncjs__wasm_poll_socket, + __call_sighandler: ___call_sighandler, + __syscall_accept4: ___syscall_accept4, + __syscall_bind: ___syscall_bind, + __syscall_chdir: ___syscall_chdir, + __syscall_chmod: ___syscall_chmod, + __syscall_connect: ___syscall_connect, + __syscall_dup: ___syscall_dup, + __syscall_dup3: ___syscall_dup3, + __syscall_faccessat: ___syscall_faccessat, + __syscall_fchmod: ___syscall_fchmod, + __syscall_fchown32: ___syscall_fchown32, + __syscall_fchownat: ___syscall_fchownat, + __syscall_fcntl64: ___syscall_fcntl64, + __syscall_fstat64: ___syscall_fstat64, + __syscall_ftruncate64: ___syscall_ftruncate64, + __syscall_getcwd: ___syscall_getcwd, + __syscall_getdents64: ___syscall_getdents64, + __syscall_getpeername: ___syscall_getpeername, + __syscall_getsockname: ___syscall_getsockname, + __syscall_getsockopt: ___syscall_getsockopt, + __syscall_ioctl: ___syscall_ioctl, + __syscall_listen: ___syscall_listen, + __syscall_lstat64: ___syscall_lstat64, + __syscall_mkdirat: ___syscall_mkdirat, + __syscall_newfstatat: ___syscall_newfstatat, + __syscall_openat: ___syscall_openat, + __syscall_pipe: ___syscall_pipe, + __syscall_readlinkat: ___syscall_readlinkat, + __syscall_recvfrom: ___syscall_recvfrom, + __syscall_renameat: ___syscall_renameat, + __syscall_rmdir: ___syscall_rmdir, + __syscall_sendto: ___syscall_sendto, + __syscall_socket: ___syscall_socket, + __syscall_stat64: ___syscall_stat64, + __syscall_statfs64: ___syscall_statfs64, + __syscall_symlinkat: ___syscall_symlinkat, + __syscall_unlinkat: ___syscall_unlinkat, + __syscall_utimensat: ___syscall_utimensat, + _abort_js: __abort_js, + _dlopen_js: __dlopen_js, + _dlsym_js: __dlsym_js, + _emscripten_lookup_name: __emscripten_lookup_name, + _emscripten_runtime_keepalive_clear: + __emscripten_runtime_keepalive_clear, + _gmtime_js: __gmtime_js, + _localtime_js: __localtime_js, + _mktime_js: __mktime_js, + _mmap_js: __mmap_js, + _munmap_js: __munmap_js, + _setitimer_js: __setitimer_js, + _tzset_js: __tzset_js, + clock_time_get: _clock_time_get, + emscripten_date_now: _emscripten_date_now, + emscripten_get_heap_max: _emscripten_get_heap_max, + emscripten_get_now: _emscripten_get_now, + emscripten_resize_heap: _emscripten_resize_heap, + emscripten_sleep: _emscripten_sleep, + environ_get: _environ_get, + environ_sizes_get: _environ_sizes_get, + exit: _exit, + fd_close: _fd_close, + fd_fdstat_get: _fd_fdstat_get, + fd_read: _fd_read, + fd_seek: _fd_seek, + fd_sync: _fd_sync, + fd_write: _fd_write, + getaddrinfo: _getaddrinfo, + getnameinfo: _getnameinfo, + getprotobyname: _getprotobyname, + getprotobynumber: _getprotobynumber, + js_fd_read, + js_open_process: _js_open_process, + js_wasm_trace: _js_wasm_trace, + proc_exit: _proc_exit, + strptime: _strptime, + wasm_setsockopt: _wasm_setsockopt, + }; + async function callMain(args = []) { + var entryFunction = resolveGlobalSymbol('main').sym; + if (!entryFunction) return; + args.unshift(thisProgram); + var argc = args.length; + var argv = stackAlloc((argc + 1) * 4); + var argv_ptr = argv; + for (var arg of args) { + HEAPU32[argv_ptr >> 2] = stringToUTF8OnStack(arg); + argv_ptr += 4; + } + HEAPU32[argv_ptr >> 2] = 0; + try { + var ret = entryFunction(argc, argv); + ret = await ret; + exitJS(ret, true); + return ret; + } catch (e) { + return handleException(e); + } + } + function run(args = arguments_) { + if (runDependencies > 0) { + dependenciesFulfilled = run; + return; + } + preRun(); + if (runDependencies > 0) { + dependenciesFulfilled = run; + return; + } + async function doRun() { + Module['calledRun'] = true; + if (ABORT) return; + initRuntime(); + preMain(); + Module['onRuntimeInitialized']?.(); + var noInitialRun = Module['noInitialRun'] || true; + if (!noInitialRun) await callMain(args); + postRun(); + } + if (Module['setStatus']) { + Module['setStatus']('Running...'); + setTimeout(() => { + setTimeout(() => Module['setStatus'](''), 1); + doRun(); + }, 1); + } else { + doRun(); + } + } + var wasmExports; + createWasm(); + run(); + /** + * Emscripten resolves `localhost` to a random IP address. Let's + * make it always resolve to 127.0.0.1. + */ + DNS.address_map.addrs.localhost = '127.0.0.1'; + + /** + * Debugging Asyncify errors is tricky because the stack trace is lost when the + * error is thrown. This code saves the stack trace in a global variable + * so that it can be inspected later. + */ + PHPLoader.debug = 'debug' in PHPLoader ? PHPLoader.debug : true; + if (PHPLoader.debug && typeof Asyncify !== 'undefined') { + const originalHandleSleep = Asyncify.handleSleep; + Asyncify.handleSleep = function (startAsync) { + if (!ABORT) { + Module['lastAsyncifyStackSource'] = new Error(); + } + return originalHandleSleep(startAsync); + }; + } + + /** + * Data dependencies call removeRunDependency() when they are loaded. + * The synchronous call stack then continues to run. If an error occurs + * in PHP initialization, e.g. Out Of Memory error, it will not be + * caught by any try/catch. This override propagates the failure to + * PHPLoader.onAbort() so that it can be handled. + */ + const originalRemoveRunDependency = PHPLoader['removeRunDependency']; + PHPLoader['removeRunDependency'] = function (...args) { + try { + originalRemoveRunDependency(...args); + } catch (e) { + PHPLoader['onAbort'](e); + } + }; + + if (typeof NODEFS === 'object') { + // We override NODEFS.createNode() to add an `isSharedFS` flag to all NODEFS + // nodes. This way we can tell whether file-locking is needed and possible + // for an FS node, even if wrapped with PROXYFS. + const originalNodeFsCreateNode = NODEFS.createNode; + NODEFS.createNode = function createNodeWithSharedFlag() { + const node = originalNodeFsCreateNode.apply(NODEFS, arguments); + node.isSharedFS = true; + return node; + }; + + var originalHashAddNode = FS.hashAddNode; + FS.hashAddNode = function hashAddNodeIfNotSharedFS(node) { + if (node?.isSharedFS) { + // Avoid caching shared VFS nodes so multiple instances + // can access the same underlying filesystem without + // conflicting caches. + return; + } + return originalHashAddNode.apply(FS, arguments); + }; + } + + /** + * Expose the PHP version so the PHP class can make version-specific + * adjustments to `php.ini`. + */ + PHPLoader['phpVersion'] = (() => { + const [major, minor, patch] = phpVersionString.split('.').map(Number); + return { major, minor, patch }; + })(); + + return PHPLoader; + + // Close the opening bracket from esm-prefix.js: +} diff --git a/packages/php-wasm/web-builds/5-2/package.json b/packages/php-wasm/web-builds/5-2/package.json new file mode 100644 index 00000000000..1a9bdbd3d85 --- /dev/null +++ b/packages/php-wasm/web-builds/5-2/package.json @@ -0,0 +1,38 @@ +{ + "name": "@php-wasm/web-5-2", + "version": "3.1.19", + "description": "PHP 5.2 WebAssembly binaries for web (legacy)", + "repository": { + "type": "git", + "url": "https://github.com/WordPress/wordpress-playground" + }, + "homepage": "https://developer.wordpress.org/playground", + "author": "The WordPress contributors", + "contributors": [ + { + "name": "Adam Zielinski", + "email": "adam@adamziel.com", + "url": "https://github.com/adamziel" + } + ], + "exports": { + ".": { + "import": "./index.js", + "require": "./index.cjs" + }, + "./package.json": "./package.json" + }, + "publishConfig": { + "access": "public", + "directory": "../../../../dist/packages/php-wasm/web-builds/5-2" + }, + "type": "module", + "main": "./index.cjs", + "module": "./index.js", + "types": "index.d.ts", + "license": "GPL-2.0-or-later", + "engines": { + "node": ">=20.10.0", + "npm": ">=10.2.3" + } +} diff --git a/packages/php-wasm/web-builds/5-2/project.json b/packages/php-wasm/web-builds/5-2/project.json new file mode 100644 index 00000000000..f9641a06859 --- /dev/null +++ b/packages/php-wasm/web-builds/5-2/project.json @@ -0,0 +1,61 @@ +{ + "name": "php-wasm-web-5-2", + "$schema": "../../../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "packages/php-wasm/web-builds/5-2/src", + "projectType": "library", + "implicitDependencies": ["php-wasm-compile"], + "targets": { + "build": { + "executor": "nx:noop", + "dependsOn": ["build:copy-assets"] + }, + "build:mkdir": { + "executor": "nx:run-commands", + "options": { + "commands": ["mkdir -p dist/packages/php-wasm/web-builds/5-2"], + "parallel": false + } + }, + "build:package-json": { + "executor": "@wp-playground/nx-extensions:package-json", + "options": { + "tsConfig": "packages/php-wasm/web-builds/5-2/tsconfig.lib.json", + "outputPath": "dist/packages/php-wasm/web-builds/5-2", + "buildTarget": "php-wasm-web-5-2:build:bundle:production" + }, + "dependsOn": ["build:mkdir"] + }, + "build:bundle": { + "executor": "nx:run-commands", + "options": { + "command": "node packages/php-wasm/web-builds/5-2/build.js", + "parallel": false + }, + "dependsOn": ["build:mkdir"] + }, + "build:copy-assets": { + "executor": "nx:run-commands", + "options": { + "commands": [ + "cp -rf packages/php-wasm/web-builds/5-2/jspi dist/packages/php-wasm/web-builds/5-2/", + "cp -rf packages/php-wasm/web-builds/5-2/asyncify dist/packages/php-wasm/web-builds/5-2/" + ], + "parallel": false + }, + "dependsOn": ["build:package-json"] + }, + "publish": { + "executor": "nx:run-commands", + "options": { + "command": "node tools/scripts/publish.mjs php-wasm-web-5-2 {args.ver} {args.tag}", + "parallel": false + }, + "dependsOn": ["build"] + }, + "package-for-self-hosting": { + "executor": "@wp-playground/nx-extensions:package-for-self-hosting", + "dependsOn": ["build"] + } + }, + "tags": ["scope:php-binaries", "scope:browser-only"] +} diff --git a/packages/php-wasm/web-builds/5-2/src/index.ts b/packages/php-wasm/web-builds/5-2/src/index.ts new file mode 100644 index 00000000000..9e05b8eba6c --- /dev/null +++ b/packages/php-wasm/web-builds/5-2/src/index.ts @@ -0,0 +1,18 @@ +import type { PHPLoaderModule } from '@php-wasm/universal'; +import { jspi } from 'wasm-feature-detect'; + +export async function getPHPLoaderModule(): Promise { + if (await jspi()) { + // @ts-ignore + return await import('../jspi/php_5_2.js'); + } else { + // @ts-ignore + return await import('../asyncify/php_5_2.js'); + } +} + +export async function getIntlExtensionPath(): Promise { + throw new Error('The intl extension is not available for PHP 5.2.'); +} + +export { jspi }; diff --git a/packages/php-wasm/web-builds/5-2/tsconfig.json b/packages/php-wasm/web-builds/5-2/tsconfig.json new file mode 100644 index 00000000000..b943cbfe62a --- /dev/null +++ b/packages/php-wasm/web-builds/5-2/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "../../../../tsconfig.base.json", + "compilerOptions": { + "module": "ESNext", + "moduleResolution": "bundler", + "allowJs": true, + "checkJs": false + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + } + ] +} diff --git a/packages/php-wasm/web-builds/5-2/tsconfig.lib.json b/packages/php-wasm/web-builds/5-2/tsconfig.lib.json new file mode 100644 index 00000000000..1799417a650 --- /dev/null +++ b/packages/php-wasm/web-builds/5-2/tsconfig.lib.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../../dist/packages/php-wasm/web-builds/5-2", + "declaration": true, + "types": ["node"] + }, + "include": ["src/**/*.ts"], + "exclude": ["**/*.spec.ts", "**/*.test.ts"] +} diff --git a/packages/php-wasm/web/src/lib/get-php-loader-module.ts b/packages/php-wasm/web/src/lib/get-php-loader-module.ts index 6c32e81ec7c..87a2df53974 100644 --- a/packages/php-wasm/web/src/lib/get-php-loader-module.ts +++ b/packages/php-wasm/web/src/lib/get-php-loader-module.ts @@ -1,4 +1,4 @@ -import type { PHPLoaderModule, SupportedPHPVersion } from '@php-wasm/universal'; +import type { PHPLoaderModule, AllPHPVersion } from '@php-wasm/universal'; import { LatestSupportedPHPVersion } from '@php-wasm/universal'; /** @@ -14,7 +14,7 @@ import { LatestSupportedPHPVersion } from '@php-wasm/universal'; * @returns The PHP loader module. */ export async function getPHPLoaderModule( - version: SupportedPHPVersion = LatestSupportedPHPVersion + version: AllPHPVersion = LatestSupportedPHPVersion ): Promise { switch (version) { case '8.5': @@ -38,6 +38,9 @@ export async function getPHPLoaderModule( case '7.4': // @ts-ignore return (await import('@php-wasm/web-7-4')).getPHPLoaderModule(); + case '5.2': + // @ts-ignore + return (await import('@php-wasm/web-5-2')).getPHPLoaderModule(); } throw new Error(`Unsupported PHP version ${version}`); } diff --git a/packages/php-wasm/web/src/lib/load-runtime.ts b/packages/php-wasm/web/src/lib/load-runtime.ts index 4886e8c0bee..b6c18c6c4c5 100644 --- a/packages/php-wasm/web/src/lib/load-runtime.ts +++ b/packages/php-wasm/web/src/lib/load-runtime.ts @@ -1,9 +1,14 @@ import type { - SupportedPHPVersion, + AllPHPVersion, EmscriptenOptions, PHPLoaderModule, + SupportedPHPVersion, +} from '@php-wasm/universal'; +import { + createLegacyPhpIniPreRunStep, + isLegacyPHPVersion, + loadPHPRuntime, } from '@php-wasm/universal'; -import { loadPHPRuntime } from '@php-wasm/universal'; import { getPHPLoaderModule } from './get-php-loader-module'; import type { TCPOverFetchOptions } from './tcp-over-fetch-websocket'; import { tcpOverFetchWebsocket } from './tcp-over-fetch-websocket'; @@ -47,7 +52,7 @@ interface PHPWorkerGlobalScope extends WorkerGlobalScope { } export async function loadWebRuntime( - phpVersion: SupportedPHPVersion, + phpVersion: AllPHPVersion, loaderOptions: LoaderOptions = {} ) { /* @@ -75,8 +80,34 @@ export async function loadWebRuntime( ); } - if (loaderOptions.withIntl) { - emscriptenOptions = withIntl(phpVersion, emscriptenOptions); + const isLegacy = isLegacyPHPVersion(phpVersion); + + // For legacy PHP: pre-create php.ini via a preRun step. See + // createLegacyPhpIniPreRunStep for why this must run before the + // PHP SAPI starts. + if (isLegacy) { + const resolvedOptions = await emscriptenOptions; + // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type + const existingPreRun: Function[] = resolvedOptions['preRun'] || []; + emscriptenOptions = { + ...resolvedOptions, + ['preRun']: [...existingPreRun, createLegacyPhpIniPreRunStep()], + }; + } + + if (isLegacy && loaderOptions.withIntl) { + throw new Error( + `The intl extension is not available for legacy PHP ${phpVersion}.` + ); + } + + if (!isLegacy) { + if (loaderOptions.withIntl) { + emscriptenOptions = withIntl( + phpVersion as SupportedPHPVersion, + emscriptenOptions + ); + } } const [phpLoaderModule, options] = await Promise.all([ diff --git a/packages/playground/blueprints/public/blueprint-schema-validator.js b/packages/playground/blueprints/public/blueprint-schema-validator.js index 3b1856aa350..a89312777ef 100644 --- a/packages/playground/blueprints/public/blueprint-schema-validator.js +++ b/packages/playground/blueprints/public/blueprint-schema-validator.js @@ -160,17 +160,24 @@ const schema11 = { }, BlueprintPHPVersion: { anyOf: [ - { $ref: '#/definitions/SupportedPHPVersion' }, + { $ref: '#/definitions/AllPHPVersion' }, { type: 'string', const: '7.2' }, { type: 'string', const: '7.3' }, ], description: 'PHP versions accepted in Blueprint schema. Includes deprecated versions (7.2, 7.3) which are automatically upgraded to 7.4 during compilation.', }, + AllPHPVersion: { + anyOf: [ + { $ref: '#/definitions/SupportedPHPVersion' }, + { $ref: '#/definitions/LegacyPHPVersion' }, + ], + }, SupportedPHPVersion: { type: 'string', enum: ['8.5', '8.4', '8.3', '8.2', '8.1', '8.0', '7.4'], }, + LegacyPHPVersion: { type: 'string', enum: ['5.2'] }, ExtraLibrary: { type: 'string', const: 'wp-cli' }, PHPConstants: { type: 'object', @@ -1561,15 +1568,15 @@ const schema12 = { description: 'The Blueprint declaration, typically stored in a blueprint.json file.', }; -const schema15 = { type: 'string', const: 'wp-cli' }; -const schema16 = { +const schema17 = { type: 'string', const: 'wp-cli' }; +const schema18 = { type: 'object', additionalProperties: { type: ['string', 'boolean', 'number'] }, }; const func2 = Object.prototype.hasOwnProperty; const schema13 = { anyOf: [ - { $ref: '#/definitions/SupportedPHPVersion' }, + { $ref: '#/definitions/AllPHPVersion' }, { type: 'string', const: '7.2' }, { type: 'string', const: '7.3' }, ], @@ -1577,10 +1584,17 @@ const schema13 = { 'PHP versions accepted in Blueprint schema. Includes deprecated versions (7.2, 7.3) which are automatically upgraded to 7.4 during compilation.', }; const schema14 = { + anyOf: [ + { $ref: '#/definitions/SupportedPHPVersion' }, + { $ref: '#/definitions/LegacyPHPVersion' }, + ], +}; +const schema15 = { type: 'string', enum: ['8.5', '8.4', '8.3', '8.2', '8.1', '8.0', '7.4'], }; -function validate12( +const schema16 = { type: 'string', enum: ['5.2'] }; +function validate13( data, { instancePath = '', parentData, parentDataProperty, rootData = data } = {} ) { @@ -1619,7 +1633,7 @@ function validate12( instancePath, schemaPath: '#/definitions/SupportedPHPVersion/enum', keyword: 'enum', - params: { allowedValues: schema14.enum }, + params: { allowedValues: schema15.enum }, message: 'must be equal to one of the allowed values', }; if (vErrors === null) { @@ -1636,7 +1650,7 @@ function validate12( if (typeof data !== 'string') { const err2 = { instancePath, - schemaPath: '#/anyOf/1/type', + schemaPath: '#/definitions/LegacyPHPVersion/type', keyword: 'type', params: { type: 'string' }, message: 'must be string', @@ -1648,8 +1662,97 @@ function validate12( } errors++; } - if ('7.2' !== data) { + if (!(data === '5.2')) { const err3 = { + instancePath, + schemaPath: '#/definitions/LegacyPHPVersion/enum', + keyword: 'enum', + params: { allowedValues: schema16.enum }, + message: 'must be equal to one of the allowed values', + }; + if (vErrors === null) { + vErrors = [err3]; + } else { + vErrors.push(err3); + } + errors++; + } + var _valid0 = _errs4 === errors; + valid0 = valid0 || _valid0; + } + if (!valid0) { + const err4 = { + instancePath, + schemaPath: '#/anyOf', + keyword: 'anyOf', + params: {}, + message: 'must match a schema in anyOf', + }; + if (vErrors === null) { + vErrors = [err4]; + } else { + vErrors.push(err4); + } + errors++; + validate13.errors = vErrors; + return false; + } else { + errors = _errs0; + if (vErrors !== null) { + if (_errs0) { + vErrors.length = _errs0; + } else { + vErrors = null; + } + } + } + validate13.errors = vErrors; + return errors === 0; +} +function validate12( + data, + { instancePath = '', parentData, parentDataProperty, rootData = data } = {} +) { + let vErrors = null; + let errors = 0; + const _errs0 = errors; + let valid0 = false; + const _errs1 = errors; + if ( + !validate13(data, { + instancePath, + parentData, + parentDataProperty, + rootData, + }) + ) { + vErrors = + vErrors === null + ? validate13.errors + : vErrors.concat(validate13.errors); + errors = vErrors.length; + } + var _valid0 = _errs1 === errors; + valid0 = valid0 || _valid0; + if (!valid0) { + const _errs2 = errors; + if (typeof data !== 'string') { + const err0 = { + instancePath, + schemaPath: '#/anyOf/1/type', + keyword: 'type', + params: { type: 'string' }, + message: 'must be string', + }; + if (vErrors === null) { + vErrors = [err0]; + } else { + vErrors.push(err0); + } + errors++; + } + if ('7.2' !== data) { + const err1 = { instancePath, schemaPath: '#/anyOf/1/const', keyword: 'const', @@ -1657,18 +1760,18 @@ function validate12( message: 'must be equal to constant', }; if (vErrors === null) { - vErrors = [err3]; + vErrors = [err1]; } else { - vErrors.push(err3); + vErrors.push(err1); } errors++; } - var _valid0 = _errs4 === errors; + var _valid0 = _errs2 === errors; valid0 = valid0 || _valid0; if (!valid0) { - const _errs6 = errors; + const _errs4 = errors; if (typeof data !== 'string') { - const err4 = { + const err2 = { instancePath, schemaPath: '#/anyOf/2/type', keyword: 'type', @@ -1676,14 +1779,14 @@ function validate12( message: 'must be string', }; if (vErrors === null) { - vErrors = [err4]; + vErrors = [err2]; } else { - vErrors.push(err4); + vErrors.push(err2); } errors++; } if ('7.3' !== data) { - const err5 = { + const err3 = { instancePath, schemaPath: '#/anyOf/2/const', keyword: 'const', @@ -1691,18 +1794,18 @@ function validate12( message: 'must be equal to constant', }; if (vErrors === null) { - vErrors = [err5]; + vErrors = [err3]; } else { - vErrors.push(err5); + vErrors.push(err3); } errors++; } - var _valid0 = _errs6 === errors; + var _valid0 = _errs4 === errors; valid0 = valid0 || _valid0; } } if (!valid0) { - const err6 = { + const err4 = { instancePath, schemaPath: '#/anyOf', keyword: 'anyOf', @@ -1710,9 +1813,9 @@ function validate12( message: 'must match a schema in anyOf', }; if (vErrors === null) { - vErrors = [err6]; + vErrors = [err4]; } else { - vErrors.push(err6); + vErrors.push(err4); } errors++; validate12.errors = vErrors; @@ -1730,7 +1833,7 @@ function validate12( validate12.errors = vErrors; return errors === 0; } -const schema17 = { +const schema19 = { anyOf: [ { $ref: '#/definitions/VFSReference' }, { $ref: '#/definitions/LiteralReference' }, @@ -1741,7 +1844,7 @@ const schema17 = { { $ref: '#/definitions/ZipWrapperReference' }, ], }; -const schema18 = { +const schema20 = { type: 'object', properties: { resource: { @@ -1758,7 +1861,7 @@ const schema18 = { required: ['resource', 'path'], additionalProperties: false, }; -const schema19 = { +const schema21 = { type: 'object', properties: { resource: { @@ -1800,7 +1903,7 @@ const schema19 = { required: ['resource', 'name', 'contents'], additionalProperties: false, }; -const schema20 = { +const schema22 = { type: 'object', properties: { resource: { @@ -1817,7 +1920,7 @@ const schema20 = { required: ['resource', 'slug'], additionalProperties: false, }; -const schema21 = { +const schema23 = { type: 'object', properties: { resource: { @@ -1834,7 +1937,7 @@ const schema21 = { required: ['resource', 'slug'], additionalProperties: false, }; -const schema22 = { +const schema24 = { type: 'object', properties: { resource: { @@ -1851,7 +1954,7 @@ const schema22 = { required: ['resource', 'url'], additionalProperties: false, }; -const schema23 = { +const schema25 = { type: 'object', properties: { resource: { @@ -1867,7 +1970,7 @@ const schema23 = { required: ['resource', 'path'], additionalProperties: false, }; -const schema24 = { +const schema26 = { type: 'object', properties: { resource: { @@ -1891,14 +1994,14 @@ const schema24 = { required: ['resource', 'inner'], additionalProperties: false, }; -const wrapper0 = { validate: validate14 }; -const schema25 = { +const wrapper0 = { validate: validate16 }; +const schema27 = { anyOf: [ { $ref: '#/definitions/GitDirectoryReference' }, { $ref: '#/definitions/DirectoryLiteralReference' }, ], }; -const schema26 = { +const schema28 = { type: 'object', properties: { resource: { @@ -1931,11 +2034,11 @@ const schema26 = { required: ['resource', 'url', 'ref'], additionalProperties: false, }; -const schema27 = { +const schema29 = { type: 'string', enum: ['branch', 'tag', 'commit', 'refname'], }; -function validate17( +function validate19( data, { instancePath = '', parentData, parentDataProperty, rootData = data } = {} ) { @@ -1949,7 +2052,7 @@ function validate17( (data.url === undefined && (missing0 = 'url')) || (data.ref === undefined && (missing0 = 'ref')) ) { - validate17.errors = [ + validate19.errors = [ { instancePath, schemaPath: '#/required', @@ -1973,7 +2076,7 @@ function validate17( key0 === '.git' ) ) { - validate17.errors = [ + validate19.errors = [ { instancePath, schemaPath: '#/additionalProperties', @@ -1991,7 +2094,7 @@ function validate17( let data0 = data.resource; const _errs2 = errors; if (typeof data0 !== 'string') { - validate17.errors = [ + validate19.errors = [ { instancePath: instancePath + '/resource', schemaPath: '#/properties/resource/type', @@ -2003,7 +2106,7 @@ function validate17( return false; } if ('git:directory' !== data0) { - validate17.errors = [ + validate19.errors = [ { instancePath: instancePath + '/resource', schemaPath: '#/properties/resource/const', @@ -2022,7 +2125,7 @@ function validate17( if (data.url !== undefined) { const _errs4 = errors; if (typeof data.url !== 'string') { - validate17.errors = [ + validate19.errors = [ { instancePath: instancePath + '/url', schemaPath: '#/properties/url/type', @@ -2041,7 +2144,7 @@ function validate17( if (data.ref !== undefined) { const _errs6 = errors; if (typeof data.ref !== 'string') { - validate17.errors = [ + validate19.errors = [ { instancePath: instancePath + '/ref', schemaPath: '#/properties/ref/type', @@ -2061,7 +2164,7 @@ function validate17( let data3 = data.refType; const _errs8 = errors; if (typeof data3 !== 'string') { - validate17.errors = [ + validate19.errors = [ { instancePath: instancePath + '/refType', @@ -2082,7 +2185,7 @@ function validate17( data3 === 'refname' ) ) { - validate17.errors = [ + validate19.errors = [ { instancePath: instancePath + '/refType', @@ -2091,7 +2194,7 @@ function validate17( keyword: 'enum', params: { allowedValues: - schema27.enum, + schema29.enum, }, message: 'must be equal to one of the allowed values', @@ -2107,7 +2210,7 @@ function validate17( if (data.path !== undefined) { const _errs11 = errors; if (typeof data.path !== 'string') { - validate17.errors = [ + validate19.errors = [ { instancePath: instancePath + '/path', @@ -2131,7 +2234,7 @@ function validate17( typeof data['.git'] !== 'boolean' ) { - validate17.errors = [ + validate19.errors = [ { instancePath: instancePath + @@ -2160,7 +2263,7 @@ function validate17( } } } else { - validate17.errors = [ + validate19.errors = [ { instancePath, schemaPath: '#/type', @@ -2172,10 +2275,10 @@ function validate17( return false; } } - validate17.errors = vErrors; + validate19.errors = vErrors; return errors === 0; } -const schema28 = { +const schema30 = { type: 'object', additionalProperties: false, properties: { @@ -2189,7 +2292,7 @@ const schema28 = { }, required: ['files', 'name', 'resource'], }; -const schema29 = { +const schema31 = { type: 'object', additionalProperties: { anyOf: [ @@ -2199,8 +2302,8 @@ const schema29 = { }, properties: {}, }; -const wrapper1 = { validate: validate20 }; -function validate20( +const wrapper1 = { validate: validate22 }; +function validate22( data, { instancePath = '', parentData, parentDataProperty, rootData = data } = {} ) { @@ -2251,7 +2354,7 @@ function validate20( schemaPath: '#/additionalProperties/anyOf/1/type', keyword: 'type', params: { - type: schema29.additionalProperties.anyOf[1] + type: schema31.additionalProperties.anyOf[1] .type, }, message: 'must be object,string', @@ -2283,7 +2386,7 @@ function validate20( vErrors.push(err1); } errors++; - validate20.errors = vErrors; + validate22.errors = vErrors; return false; } else { errors = _errs3; @@ -2301,7 +2404,7 @@ function validate20( } } } else { - validate20.errors = [ + validate22.errors = [ { instancePath, schemaPath: '#/type', @@ -2313,10 +2416,10 @@ function validate20( return false; } } - validate20.errors = vErrors; + validate22.errors = vErrors; return errors === 0; } -function validate19( +function validate21( data, { instancePath = '', parentData, parentDataProperty, rootData = data } = {} ) { @@ -2330,7 +2433,7 @@ function validate19( (data.name === undefined && (missing0 = 'name')) || (data.resource === undefined && (missing0 = 'resource')) ) { - validate19.errors = [ + validate21.errors = [ { instancePath, schemaPath: '#/required', @@ -2351,7 +2454,7 @@ function validate19( key0 === 'name' ) ) { - validate19.errors = [ + validate21.errors = [ { instancePath, schemaPath: '#/additionalProperties', @@ -2369,7 +2472,7 @@ function validate19( let data0 = data.resource; const _errs2 = errors; if (typeof data0 !== 'string') { - validate19.errors = [ + validate21.errors = [ { instancePath: instancePath + '/resource', schemaPath: '#/properties/resource/type', @@ -2381,7 +2484,7 @@ function validate19( return false; } if ('literal:directory' !== data0) { - validate19.errors = [ + validate21.errors = [ { instancePath: instancePath + '/resource', schemaPath: '#/properties/resource/const', @@ -2402,7 +2505,7 @@ function validate19( if (data.files !== undefined) { const _errs4 = errors; if ( - !validate20(data.files, { + !validate22(data.files, { instancePath: instancePath + '/files', parentData: data, parentDataProperty: 'files', @@ -2411,8 +2514,8 @@ function validate19( ) { vErrors = vErrors === null - ? validate20.errors - : vErrors.concat(validate20.errors); + ? validate22.errors + : vErrors.concat(validate22.errors); errors = vErrors.length; } var valid0 = _errs4 === errors; @@ -2423,7 +2526,7 @@ function validate19( if (data.name !== undefined) { const _errs5 = errors; if (typeof data.name !== 'string') { - validate19.errors = [ + validate21.errors = [ { instancePath: instancePath + '/name', @@ -2445,7 +2548,7 @@ function validate19( } } } else { - validate19.errors = [ + validate21.errors = [ { instancePath, schemaPath: '#/type', @@ -2457,10 +2560,10 @@ function validate19( return false; } } - validate19.errors = vErrors; + validate21.errors = vErrors; return errors === 0; } -function validate16( +function validate18( data, { instancePath = '', parentData, parentDataProperty, rootData = data } = {} ) { @@ -2470,7 +2573,7 @@ function validate16( let valid0 = false; const _errs1 = errors; if ( - !validate17(data, { + !validate19(data, { instancePath, parentData, parentDataProperty, @@ -2479,8 +2582,8 @@ function validate16( ) { vErrors = vErrors === null - ? validate17.errors - : vErrors.concat(validate17.errors); + ? validate19.errors + : vErrors.concat(validate19.errors); errors = vErrors.length; } var _valid0 = _errs1 === errors; @@ -2488,7 +2591,7 @@ function validate16( if (!valid0) { const _errs2 = errors; if ( - !validate19(data, { + !validate21(data, { instancePath, parentData, parentDataProperty, @@ -2497,8 +2600,8 @@ function validate16( ) { vErrors = vErrors === null - ? validate19.errors - : vErrors.concat(validate19.errors); + ? validate21.errors + : vErrors.concat(validate21.errors); errors = vErrors.length; } var _valid0 = _errs2 === errors; @@ -2518,7 +2621,7 @@ function validate16( vErrors.push(err0); } errors++; - validate16.errors = vErrors; + validate18.errors = vErrors; return false; } else { errors = _errs0; @@ -2530,10 +2633,10 @@ function validate16( } } } - validate16.errors = vErrors; + validate18.errors = vErrors; return errors === 0; } -function validate15( +function validate17( data, { instancePath = '', parentData, parentDataProperty, rootData = data } = {} ) { @@ -2546,7 +2649,7 @@ function validate15( (data.resource === undefined && (missing0 = 'resource')) || (data.inner === undefined && (missing0 = 'inner')) ) { - validate15.errors = [ + validate17.errors = [ { instancePath, schemaPath: '#/required', @@ -2567,7 +2670,7 @@ function validate15( key0 === 'name' ) ) { - validate15.errors = [ + validate17.errors = [ { instancePath, schemaPath: '#/additionalProperties', @@ -2585,7 +2688,7 @@ function validate15( let data0 = data.resource; const _errs2 = errors; if (typeof data0 !== 'string') { - validate15.errors = [ + validate17.errors = [ { instancePath: instancePath + '/resource', schemaPath: '#/properties/resource/type', @@ -2597,7 +2700,7 @@ function validate15( return false; } if ('zip' !== data0) { - validate15.errors = [ + validate17.errors = [ { instancePath: instancePath + '/resource', schemaPath: '#/properties/resource/const', @@ -2640,7 +2743,7 @@ function validate15( if (!valid1) { const _errs7 = errors; if ( - !validate16(data1, { + !validate18(data1, { instancePath: instancePath + '/inner', parentData: data, parentDataProperty: 'inner', @@ -2649,8 +2752,8 @@ function validate15( ) { vErrors = vErrors === null - ? validate16.errors - : vErrors.concat(validate16.errors); + ? validate18.errors + : vErrors.concat(validate18.errors); errors = vErrors.length; } var _valid0 = _errs7 === errors; @@ -2670,7 +2773,7 @@ function validate15( vErrors.push(err0); } errors++; - validate15.errors = vErrors; + validate17.errors = vErrors; return false; } else { errors = _errs5; @@ -2690,7 +2793,7 @@ function validate15( if (data.name !== undefined) { const _errs8 = errors; if (typeof data.name !== 'string') { - validate15.errors = [ + validate17.errors = [ { instancePath: instancePath + '/name', @@ -2712,7 +2815,7 @@ function validate15( } } } else { - validate15.errors = [ + validate17.errors = [ { instancePath, schemaPath: '#/type', @@ -2724,10 +2827,10 @@ function validate15( return false; } } - validate15.errors = vErrors; + validate17.errors = vErrors; return errors === 0; } -function validate14( +function validate16( data, { instancePath = '', parentData, parentDataProperty, rootData = data } = {} ) { @@ -4255,7 +4358,7 @@ function validate14( if (!valid0) { const _errs73 = errors; if ( - !validate15(data, { + !validate17(data, { instancePath, parentData, parentDataProperty, @@ -4264,8 +4367,8 @@ function validate14( ) { vErrors = vErrors === null - ? validate15.errors - : vErrors.concat(validate15.errors); + ? validate17.errors + : vErrors.concat(validate17.errors); errors = vErrors.length; } var _valid0 = _errs73 === errors; @@ -4290,7 +4393,7 @@ function validate14( vErrors.push(err50); } errors++; - validate14.errors = vErrors; + validate16.errors = vErrors; return false; } else { errors = _errs0; @@ -4302,10 +4405,10 @@ function validate14( } } } - validate14.errors = vErrors; + validate16.errors = vErrors; return errors === 0; } -const schema30 = { +const schema32 = { type: 'object', discriminator: { propertyName: 'step' }, required: ['step'], @@ -5042,7 +5145,7 @@ const schema30 = { }, ], }; -const schema31 = { +const schema33 = { type: 'object', properties: { activate: { @@ -5057,7 +5160,7 @@ const schema31 = { }, additionalProperties: false, }; -const schema32 = { +const schema34 = { type: 'object', properties: { activate: { @@ -5077,7 +5180,7 @@ const schema32 = { }, additionalProperties: false, }; -const schema39 = { +const schema41 = { type: 'object', properties: { adminUsername: { type: 'string' }, @@ -5085,7 +5188,7 @@ const schema39 = { }, additionalProperties: false, }; -const schema33 = { +const schema35 = { type: 'object', properties: { method: { @@ -5182,12 +5285,12 @@ const schema33 = { required: ['url'], additionalProperties: false, }; -const schema34 = { +const schema36 = { type: 'string', enum: ['GET', 'POST', 'HEAD', 'OPTIONS', 'PATCH', 'PUT', 'DELETE'], }; -const schema35 = { type: 'object', additionalProperties: { type: 'string' } }; -function validate35( +const schema37 = { type: 'object', additionalProperties: { type: 'string' } }; +function validate37( data, { instancePath = '', parentData, parentDataProperty, rootData = data } = {} ) { @@ -5197,7 +5300,7 @@ function validate35( if (data && typeof data == 'object' && !Array.isArray(data)) { let missing0; if (data.url === undefined && (missing0 = 'url')) { - validate35.errors = [ + validate37.errors = [ { instancePath, schemaPath: '#/required', @@ -5219,7 +5322,7 @@ function validate35( key0 === 'body' ) ) { - validate35.errors = [ + validate37.errors = [ { instancePath, schemaPath: '#/additionalProperties', @@ -5237,7 +5340,7 @@ function validate35( let data0 = data.method; const _errs2 = errors; if (typeof data0 !== 'string') { - validate35.errors = [ + validate37.errors = [ { instancePath: instancePath + '/method', schemaPath: '#/definitions/HTTPMethod/type', @@ -5259,12 +5362,12 @@ function validate35( data0 === 'DELETE' ) ) { - validate35.errors = [ + validate37.errors = [ { instancePath: instancePath + '/method', schemaPath: '#/definitions/HTTPMethod/enum', keyword: 'enum', - params: { allowedValues: schema34.enum }, + params: { allowedValues: schema36.enum }, message: 'must be equal to one of the allowed values', }, @@ -5279,7 +5382,7 @@ function validate35( if (data.url !== undefined) { const _errs5 = errors; if (typeof data.url !== 'string') { - validate35.errors = [ + validate37.errors = [ { instancePath: instancePath + '/url', schemaPath: '#/properties/url/type', @@ -5310,7 +5413,7 @@ function validate35( if ( typeof data2[key1] !== 'string' ) { - validate35.errors = [ + validate37.errors = [ { instancePath: instancePath + @@ -5342,7 +5445,7 @@ function validate35( } } } else { - validate35.errors = [ + validate37.errors = [ { instancePath: instancePath + '/headers', @@ -7331,7 +7434,7 @@ function validate35( vErrors.push(err34); } errors++; - validate35.errors = vErrors; + validate37.errors = vErrors; return false; } else { errors = _errs14; @@ -7353,7 +7456,7 @@ function validate35( } } } else { - validate35.errors = [ + validate37.errors = [ { instancePath, schemaPath: '#/type', @@ -7365,10 +7468,10 @@ function validate35( return false; } } - validate35.errors = vErrors; + validate37.errors = vErrors; return errors === 0; } -const schema36 = { +const schema38 = { type: 'object', properties: { relativeUri: { @@ -7435,7 +7538,7 @@ const schema36 = { }, additionalProperties: false, }; -function validate37( +function validate39( data, { instancePath = '', parentData, parentDataProperty, rootData = data } = {} ) { @@ -7445,8 +7548,8 @@ function validate37( if (data && typeof data == 'object' && !Array.isArray(data)) { const _errs1 = errors; for (const key0 in data) { - if (!func2.call(schema36.properties, key0)) { - validate37.errors = [ + if (!func2.call(schema38.properties, key0)) { + validate39.errors = [ { instancePath, schemaPath: '#/additionalProperties', @@ -7463,7 +7566,7 @@ function validate37( if (data.relativeUri !== undefined) { const _errs2 = errors; if (typeof data.relativeUri !== 'string') { - validate37.errors = [ + validate39.errors = [ { instancePath: instancePath + '/relativeUri', schemaPath: '#/properties/relativeUri/type', @@ -7482,7 +7585,7 @@ function validate37( if (data.scriptPath !== undefined) { const _errs4 = errors; if (typeof data.scriptPath !== 'string') { - validate37.errors = [ + validate39.errors = [ { instancePath: instancePath + '/scriptPath', schemaPath: '#/properties/scriptPath/type', @@ -7501,7 +7604,7 @@ function validate37( if (data.protocol !== undefined) { const _errs6 = errors; if (typeof data.protocol !== 'string') { - validate37.errors = [ + validate39.errors = [ { instancePath: instancePath + '/protocol', @@ -7523,7 +7626,7 @@ function validate37( let data3 = data.method; const _errs8 = errors; if (typeof data3 !== 'string') { - validate37.errors = [ + validate39.errors = [ { instancePath: instancePath + '/method', @@ -7547,7 +7650,7 @@ function validate37( data3 === 'DELETE' ) ) { - validate37.errors = [ + validate39.errors = [ { instancePath: instancePath + '/method', @@ -7555,7 +7658,7 @@ function validate37( '#/definitions/HTTPMethod/enum', keyword: 'enum', params: { - allowedValues: schema34.enum, + allowedValues: schema36.enum, }, message: 'must be equal to one of the allowed values', @@ -7584,7 +7687,7 @@ function validate37( typeof data4[key1] !== 'string' ) { - validate37.errors = [ + validate39.errors = [ { instancePath: instancePath + @@ -7616,7 +7719,7 @@ function validate37( } } } else { - validate37.errors = [ + validate39.errors = [ { instancePath: instancePath + @@ -8254,7 +8357,7 @@ function validate37( vErrors.push(err12); } errors++; - validate37.errors = vErrors; + validate39.errors = vErrors; return false; } else { errors = _errs18; @@ -8287,7 +8390,7 @@ function validate37( key4 ] !== 'string' ) { - validate37.errors = + validate39.errors = [ { instancePath: @@ -8322,7 +8425,7 @@ function validate37( } } } else { - validate37.errors = [ + validate39.errors = [ { instancePath: instancePath + @@ -8363,7 +8466,7 @@ function validate37( key5 ] !== 'string' ) { - validate37.errors = + validate39.errors = [ { instancePath: @@ -8399,7 +8502,7 @@ function validate37( } } } else { - validate37.errors = [ + validate39.errors = [ { instancePath: instancePath + @@ -8428,7 +8531,7 @@ function validate37( typeof data.code !== 'string' ) { - validate37.errors = [ + validate39.errors = [ { instancePath: instancePath + @@ -8460,7 +8563,7 @@ function validate37( } } } else { - validate37.errors = [ + validate39.errors = [ { instancePath, schemaPath: '#/type', @@ -8472,10 +8575,10 @@ function validate37( return false; } } - validate37.errors = vErrors; + validate39.errors = vErrors; return errors === 0; } -function validate26( +function validate28( data, { instancePath = '', parentData, parentDataProperty, rootData = data } = {} ) { @@ -8485,7 +8588,7 @@ function validate26( if (data && typeof data == 'object' && !Array.isArray(data)) { let missing0; if (data.step === undefined && (missing0 = 'step')) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/required', @@ -8514,7 +8617,7 @@ function validate26( (data.step === undefined && (missing1 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/0/required', @@ -8540,7 +8643,7 @@ function validate26( key0 === 'pluginName' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -8579,7 +8682,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -8619,7 +8722,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -8655,7 +8758,7 @@ function validate26( typeof data0.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -8683,7 +8786,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -8710,7 +8813,7 @@ function validate26( let data3 = data.step; const _errs12 = errors; if (typeof data3 !== 'string') { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -8730,7 +8833,7 @@ function validate26( if ( 'activatePlugin' !== data3 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -8762,7 +8865,7 @@ function validate26( typeof data.pluginPath !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -8794,7 +8897,7 @@ function validate26( typeof data.pluginName !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -8824,7 +8927,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/0/type', @@ -8851,7 +8954,7 @@ function validate26( (data.themeFolderName === undefined && (missing2 = 'themeFolderName')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/1/required', @@ -8876,7 +8979,7 @@ function validate26( key2 === 'themeFolderName' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -8915,7 +9018,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -8955,7 +9058,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -8991,7 +9094,7 @@ function validate26( typeof data6.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9019,7 +9122,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9046,7 +9149,7 @@ function validate26( let data9 = data.step; const _errs28 = errors; if (typeof data9 !== 'string') { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9064,7 +9167,7 @@ function validate26( return false; } if ('activateTheme' !== data9) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9096,7 +9199,7 @@ function validate26( typeof data.themeFolderName !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9123,7 +9226,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/1/type', @@ -9152,7 +9255,7 @@ function validate26( (data.toPath === undefined && (missing3 = 'toPath')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/2/required', @@ -9178,7 +9281,7 @@ function validate26( key4 === 'toPath' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -9217,7 +9320,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9257,7 +9360,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9293,7 +9396,7 @@ function validate26( typeof data11.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9321,7 +9424,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9350,7 +9453,7 @@ function validate26( if ( typeof data14 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9368,7 +9471,7 @@ function validate26( return false; } if ('cp' !== data14) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9399,7 +9502,7 @@ function validate26( typeof data.fromPath !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9431,7 +9534,7 @@ function validate26( typeof data.toPath !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9461,7 +9564,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/2/type', @@ -9488,7 +9591,7 @@ function validate26( (data.step === undefined && (missing4 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/3/required', @@ -9515,7 +9618,7 @@ function validate26( key6 === 'virtualize' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -9554,7 +9657,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9594,7 +9697,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9630,7 +9733,7 @@ function validate26( typeof data17.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9658,7 +9761,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9687,7 +9790,7 @@ function validate26( if ( typeof data20 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9708,7 +9811,7 @@ function validate26( 'defineWpConfigConsts' !== data20 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -9755,7 +9858,7 @@ function validate26( } } } else { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9792,7 +9895,7 @@ function validate26( typeof data23 !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9819,7 +9922,7 @@ function validate26( 'define-before-run' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9831,7 +9934,7 @@ function validate26( 'enum', params: { allowedValues: - schema30 + schema32 .oneOf[3] .properties .method @@ -9859,7 +9962,7 @@ function validate26( typeof data.virtualize !== 'boolean' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -9891,7 +9994,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/3/type', @@ -9918,7 +10021,7 @@ function validate26( (data.step === undefined && (missing5 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/4/required', @@ -9943,7 +10046,7 @@ function validate26( key9 === 'siteUrl' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -9982,7 +10085,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10022,7 +10125,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10058,7 +10161,7 @@ function validate26( typeof data25.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10086,7 +10189,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10115,7 +10218,7 @@ function validate26( if ( typeof data28 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10135,7 +10238,7 @@ function validate26( if ( 'defineSiteUrl' !== data28 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10167,7 +10270,7 @@ function validate26( typeof data.siteUrl !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10194,7 +10297,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/4/type', @@ -10219,7 +10322,7 @@ function validate26( data.step === undefined && (missing6 = 'step') ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/5/required', @@ -10244,7 +10347,7 @@ function validate26( key11 === 'wpCliPath' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -10283,7 +10386,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10323,7 +10426,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10359,7 +10462,7 @@ function validate26( typeof data30.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10387,7 +10490,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10416,7 +10519,7 @@ function validate26( if ( typeof data33 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10436,7 +10539,7 @@ function validate26( if ( 'enableMultisite' !== data33 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10468,7 +10571,7 @@ function validate26( typeof data.wpCliPath !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10495,7 +10598,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/5/type', @@ -10522,7 +10625,7 @@ function validate26( (data.step === undefined && (missing7 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/6/required', @@ -10548,7 +10651,7 @@ function validate26( key13 === 'importer' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -10587,7 +10690,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10627,7 +10730,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10663,7 +10766,7 @@ function validate26( typeof data35.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10691,7 +10794,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10720,7 +10823,7 @@ function validate26( if ( typeof data38 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10738,7 +10841,7 @@ function validate26( return false; } if ('importWxr' !== data38) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -10765,7 +10868,7 @@ function validate26( if (data.file !== undefined) { const _errs108 = errors; if ( - !validate14(data.file, { + !validate16(data.file, { instancePath: instancePath + '/file', @@ -10777,9 +10880,9 @@ function validate26( ) { vErrors = vErrors === null - ? validate14.errors + ? validate16.errors : vErrors.concat( - validate14.errors + validate16.errors ); errors = vErrors.length; } @@ -10800,7 +10903,7 @@ function validate26( typeof data40 !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10827,7 +10930,7 @@ function validate26( 'default' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10839,7 +10942,7 @@ function validate26( 'enum', params: { allowedValues: - schema30 + schema32 .oneOf[6] .properties .importer @@ -10862,7 +10965,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/6/type', @@ -10887,7 +10990,7 @@ function validate26( data.step === undefined && (missing8 = 'step') ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/7/required', @@ -10912,7 +11015,7 @@ function validate26( key15 === 'themeSlug' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -10951,7 +11054,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -10991,7 +11094,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -11027,7 +11130,7 @@ function validate26( typeof data41.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -11055,7 +11158,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11084,7 +11187,7 @@ function validate26( if ( typeof data44 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11105,7 +11208,7 @@ function validate26( 'importThemeStarterContent' !== data44 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11137,7 +11240,7 @@ function validate26( typeof data.themeSlug !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11164,7 +11267,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/7/type', @@ -11191,7 +11294,7 @@ function validate26( (data.wordPressFilesZip === undefined && (missing9 = 'wordPressFilesZip')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/8/required', @@ -11217,7 +11320,7 @@ function validate26( key17 === 'pathInZip' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -11256,7 +11359,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -11296,7 +11399,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -11332,7 +11435,7 @@ function validate26( typeof data46.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -11360,7 +11463,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11389,7 +11492,7 @@ function validate26( if ( typeof data49 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11410,7 +11513,7 @@ function validate26( 'importWordPressFiles' !== data49 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11440,7 +11543,7 @@ function validate26( ) { const _errs137 = errors; if ( - !validate14( + !validate16( data.wordPressFilesZip, { instancePath: @@ -11456,9 +11559,9 @@ function validate26( ) { vErrors = vErrors === null - ? validate14.errors + ? validate16.errors : vErrors.concat( - validate14.errors + validate16.errors ); errors = vErrors.length; } @@ -11477,7 +11580,7 @@ function validate26( typeof data.pathInZip !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -11507,7 +11610,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/8/type', @@ -11534,7 +11637,7 @@ function validate26( (data.step === undefined && (missing10 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/9/required', @@ -11563,7 +11666,7 @@ function validate26( key19 === 'options' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -11602,7 +11705,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -11642,7 +11745,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -11678,7 +11781,7 @@ function validate26( typeof data52.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -11706,7 +11809,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11739,7 +11842,7 @@ function validate26( if ( typeof data55 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11764,7 +11867,7 @@ function validate26( data55 === 'error' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11774,7 +11877,7 @@ function validate26( keyword: 'enum', params: { allowedValues: - schema30 + schema32 .oneOf[9] .properties .ifAlreadyInstalled @@ -11799,7 +11902,7 @@ function validate26( typeof data56 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11820,7 +11923,7 @@ function validate26( 'installPlugin' !== data56 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -11856,7 +11959,7 @@ function validate26( let valid32 = false; const _errs156 = errors; if ( - !validate14( + !validate16( data57, { instancePath: @@ -11872,9 +11975,9 @@ function validate26( ) { vErrors = vErrors === null - ? validate14.errors + ? validate16.errors : vErrors.concat( - validate14.errors + validate16.errors ); errors = vErrors.length; @@ -11887,7 +11990,7 @@ function validate26( const _errs157 = errors; if ( - !validate16( + !validate18( data57, { instancePath: @@ -11904,9 +12007,9 @@ function validate26( vErrors = vErrors === null - ? validate16.errors + ? validate18.errors : vErrors.concat( - validate16.errors + validate18.errors ); errors = vErrors.length; @@ -11943,7 +12046,7 @@ function validate26( ); } errors++; - validate26.errors = + validate28.errors = vErrors; return false; } else { @@ -11973,7 +12076,7 @@ function validate26( const _errs158 = errors; if ( - !validate14( + !validate16( data.pluginZipFile, { instancePath: @@ -11990,9 +12093,9 @@ function validate26( vErrors = vErrors === null - ? validate14.errors + ? validate16.errors : vErrors.concat( - validate14.errors + validate16.errors ); errors = vErrors.length; @@ -12037,7 +12140,7 @@ function validate26( 'targetFolderName' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12073,7 +12176,7 @@ function validate26( typeof data59.activate !== 'boolean' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12111,7 +12214,7 @@ function validate26( typeof data59.targetFolderName !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12139,7 +12242,7 @@ function validate26( } } } else { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12173,7 +12276,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/9/type', @@ -12200,7 +12303,7 @@ function validate26( (data.themeData === undefined && (missing11 = 'themeData')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/10/required', @@ -12229,7 +12332,7 @@ function validate26( key22 === 'options' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -12268,7 +12371,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12308,7 +12411,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12344,7 +12447,7 @@ function validate26( typeof data62.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12372,7 +12475,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -12405,7 +12508,7 @@ function validate26( if ( typeof data65 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -12430,7 +12533,7 @@ function validate26( data65 === 'error' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -12440,7 +12543,7 @@ function validate26( keyword: 'enum', params: { allowedValues: - schema30 + schema32 .oneOf[10] .properties .ifAlreadyInstalled @@ -12465,7 +12568,7 @@ function validate26( typeof data66 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -12486,7 +12589,7 @@ function validate26( 'installTheme' !== data66 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -12522,7 +12625,7 @@ function validate26( let valid38 = false; const _errs183 = errors; if ( - !validate14( + !validate16( data67, { instancePath: @@ -12538,9 +12641,9 @@ function validate26( ) { vErrors = vErrors === null - ? validate14.errors + ? validate16.errors : vErrors.concat( - validate14.errors + validate16.errors ); errors = vErrors.length; @@ -12553,7 +12656,7 @@ function validate26( const _errs184 = errors; if ( - !validate16( + !validate18( data67, { instancePath: @@ -12570,9 +12673,9 @@ function validate26( vErrors = vErrors === null - ? validate16.errors + ? validate18.errors : vErrors.concat( - validate16.errors + validate18.errors ); errors = vErrors.length; @@ -12609,7 +12712,7 @@ function validate26( ); } errors++; - validate26.errors = + validate28.errors = vErrors; return false; } else { @@ -12639,7 +12742,7 @@ function validate26( const _errs185 = errors; if ( - !validate14( + !validate16( data.themeZipFile, { instancePath: @@ -12656,9 +12759,9 @@ function validate26( vErrors = vErrors === null - ? validate14.errors + ? validate16.errors : vErrors.concat( - validate14.errors + validate16.errors ); errors = vErrors.length; @@ -12705,7 +12808,7 @@ function validate26( 'targetFolderName' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12741,7 +12844,7 @@ function validate26( typeof data69.activate !== 'boolean' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12779,7 +12882,7 @@ function validate26( typeof data69.importStarterContent !== 'boolean' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12817,7 +12920,7 @@ function validate26( typeof data69.targetFolderName !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12846,7 +12949,7 @@ function validate26( } } } else { - validate26.errors = + validate28.errors = [ { instancePath: @@ -12880,7 +12983,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/10/type', @@ -12905,7 +13008,7 @@ function validate26( data.step === undefined && (missing12 = 'step') ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/11/required', @@ -12931,7 +13034,7 @@ function validate26( key25 === 'password' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -12970,7 +13073,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13010,7 +13113,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13046,7 +13149,7 @@ function validate26( typeof data73.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13074,7 +13177,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13103,7 +13206,7 @@ function validate26( if ( typeof data76 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13121,7 +13224,7 @@ function validate26( return false; } if ('login' !== data76) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13153,7 +13256,7 @@ function validate26( typeof data.username !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13185,7 +13288,7 @@ function validate26( typeof data.password !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13215,7 +13318,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/11/type', @@ -13242,7 +13345,7 @@ function validate26( (data.step === undefined && (missing13 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/12/required', @@ -13267,7 +13370,7 @@ function validate26( key27 === 'path' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -13306,7 +13409,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13346,7 +13449,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13382,7 +13485,7 @@ function validate26( typeof data79.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13410,7 +13513,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13439,7 +13542,7 @@ function validate26( if ( typeof data82 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13457,7 +13560,7 @@ function validate26( return false; } if ('mkdir' !== data82) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13487,7 +13590,7 @@ function validate26( typeof data.path !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13514,7 +13617,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/12/type', @@ -13543,7 +13646,7 @@ function validate26( (data.toPath === undefined && (missing14 = 'toPath')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/13/required', @@ -13569,7 +13672,7 @@ function validate26( key29 === 'toPath' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -13608,7 +13711,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13648,7 +13751,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13684,7 +13787,7 @@ function validate26( typeof data84.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13712,7 +13815,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13741,7 +13844,7 @@ function validate26( if ( typeof data87 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13759,7 +13862,7 @@ function validate26( return false; } if ('mv' !== data87) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13791,7 +13894,7 @@ function validate26( typeof data.fromPath !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -13823,7 +13926,7 @@ function validate26( typeof data.toPath !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13853,7 +13956,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/13/type', @@ -13878,7 +13981,7 @@ function validate26( data.step === undefined && (missing15 = 'step') ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/14/required', @@ -13902,7 +14005,7 @@ function validate26( key31 === 'step' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -13941,7 +14044,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -13981,7 +14084,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14017,7 +14120,7 @@ function validate26( typeof data90.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14045,7 +14148,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14074,7 +14177,7 @@ function validate26( if ( typeof data93 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14092,7 +14195,7 @@ function validate26( return false; } if ('resetData' !== data93) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14119,7 +14222,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/14/type', @@ -14146,7 +14249,7 @@ function validate26( (data.step === undefined && (missing16 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/15/required', @@ -14171,7 +14274,7 @@ function validate26( key33 === 'request' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -14210,7 +14313,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14250,7 +14353,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14286,7 +14389,7 @@ function validate26( typeof data94.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14314,7 +14417,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14343,7 +14446,7 @@ function validate26( if ( typeof data97 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14361,7 +14464,7 @@ function validate26( return false; } if ('request' !== data97) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14390,7 +14493,7 @@ function validate26( ) { const _errs266 = errors; if ( - !validate35( + !validate37( data.request, { instancePath: @@ -14406,9 +14509,9 @@ function validate26( ) { vErrors = vErrors === null - ? validate35.errors + ? validate37.errors : vErrors.concat( - validate35.errors + validate37.errors ); errors = vErrors.length; } @@ -14422,7 +14525,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/15/type', @@ -14449,7 +14552,7 @@ function validate26( (data.step === undefined && (missing17 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/16/required', @@ -14474,7 +14577,7 @@ function validate26( key35 === 'path' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -14513,7 +14616,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14553,7 +14656,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14589,7 +14692,7 @@ function validate26( typeof data99.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14617,7 +14720,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14646,7 +14749,7 @@ function validate26( if ( typeof data102 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14664,7 +14767,7 @@ function validate26( return false; } if ('rm' !== data102) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14694,7 +14797,7 @@ function validate26( typeof data.path !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14721,7 +14824,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/16/type', @@ -14748,7 +14851,7 @@ function validate26( (data.step === undefined && (missing18 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/17/required', @@ -14773,7 +14876,7 @@ function validate26( key37 === 'path' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -14813,7 +14916,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14853,7 +14956,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14889,7 +14992,7 @@ function validate26( typeof data104.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -14917,7 +15020,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14946,7 +15049,7 @@ function validate26( if ( typeof data107 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14964,7 +15067,7 @@ function validate26( return false; } if ('rmdir' !== data107) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -14994,7 +15097,7 @@ function validate26( typeof data.path !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -15021,7 +15124,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/17/type', @@ -15048,7 +15151,7 @@ function validate26( (data.step === undefined && (missing19 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/18/required', @@ -15073,7 +15176,7 @@ function validate26( key39 === 'code' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -15113,7 +15216,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -15153,7 +15256,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -15189,7 +15292,7 @@ function validate26( typeof data109.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -15217,7 +15320,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -15246,7 +15349,7 @@ function validate26( if ( typeof data112 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -15264,7 +15367,7 @@ function validate26( return false; } if ('runPHP' !== data112) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -15578,7 +15681,7 @@ function validate26( vErrors.push(err8); } errors++; - validate26.errors = + validate28.errors = vErrors; return false; } else { @@ -15602,7 +15705,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/18/type', @@ -15629,7 +15732,7 @@ function validate26( (data.step === undefined && (missing21 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/19/required', @@ -15654,7 +15757,7 @@ function validate26( key42 === 'options' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -15694,7 +15797,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -15734,7 +15837,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -15770,7 +15873,7 @@ function validate26( typeof data116.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -15798,7 +15901,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -15827,7 +15930,7 @@ function validate26( if ( typeof data119 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -15848,7 +15951,7 @@ function validate26( 'runPHPWithOptions' !== data119 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -15877,7 +15980,7 @@ function validate26( ) { const _errs330 = errors; if ( - !validate37( + !validate39( data.options, { instancePath: @@ -15893,9 +15996,9 @@ function validate26( ) { vErrors = vErrors === null - ? validate37.errors + ? validate39.errors : vErrors.concat( - validate37.errors + validate39.errors ); errors = vErrors.length; } @@ -15909,7 +16012,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/19/type', @@ -15936,7 +16039,7 @@ function validate26( (data.step === undefined && (missing22 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/20/required', @@ -15961,7 +16064,7 @@ function validate26( key44 === 'options' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -16001,7 +16104,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16041,7 +16144,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16077,7 +16180,7 @@ function validate26( typeof data121.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16105,7 +16208,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -16134,7 +16237,7 @@ function validate26( if ( typeof data124 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -16155,7 +16258,7 @@ function validate26( 'runWpInstallationWizard' !== data124 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -16205,7 +16308,7 @@ function validate26( 'adminPassword' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16241,7 +16344,7 @@ function validate26( typeof data125.adminUsername !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16277,7 +16380,7 @@ function validate26( typeof data125.adminPassword !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16305,7 +16408,7 @@ function validate26( } } } else { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16335,7 +16438,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/20/type', @@ -16362,7 +16465,7 @@ function validate26( (data.step === undefined && (missing23 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/21/required', @@ -16387,7 +16490,7 @@ function validate26( key47 === 'sql' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -16427,7 +16530,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16467,7 +16570,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16503,7 +16606,7 @@ function validate26( typeof data128.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16531,7 +16634,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -16560,7 +16663,7 @@ function validate26( if ( typeof data131 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -16578,7 +16681,7 @@ function validate26( return false; } if ('runSql' !== data131) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -16605,7 +16708,7 @@ function validate26( if (data.sql !== undefined) { const _errs363 = errors; if ( - !validate14(data.sql, { + !validate16(data.sql, { instancePath: instancePath + '/sql', @@ -16617,9 +16720,9 @@ function validate26( ) { vErrors = vErrors === null - ? validate14.errors + ? validate16.errors : vErrors.concat( - validate14.errors + validate16.errors ); errors = vErrors.length; } @@ -16633,7 +16736,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/21/type', @@ -16660,7 +16763,7 @@ function validate26( (data.step === undefined && (missing24 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/22/required', @@ -16685,7 +16788,7 @@ function validate26( key49 === 'options' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -16725,7 +16828,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16765,7 +16868,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16801,7 +16904,7 @@ function validate26( typeof data133.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16829,7 +16932,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -16858,7 +16961,7 @@ function validate26( if ( typeof data136 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -16878,7 +16981,7 @@ function validate26( if ( 'setSiteOptions' !== data136 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -16927,7 +17030,7 @@ function validate26( } } } else { - validate26.errors = + validate28.errors = [ { instancePath: @@ -16957,7 +17060,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/22/type', @@ -16984,7 +17087,7 @@ function validate26( (data.step === undefined && (missing25 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/23/required', @@ -17011,7 +17114,7 @@ function validate26( key52 === 'extractToPath' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -17051,7 +17154,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17091,7 +17194,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17127,7 +17230,7 @@ function validate26( typeof data139.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17155,7 +17258,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -17184,7 +17287,7 @@ function validate26( if ( typeof data142 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -17202,7 +17305,7 @@ function validate26( return false; } if ('unzip' !== data142) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -17231,7 +17334,7 @@ function validate26( ) { const _errs392 = errors; if ( - !validate14( + !validate16( data.zipFile, { instancePath: @@ -17247,9 +17350,9 @@ function validate26( ) { vErrors = vErrors === null - ? validate14.errors + ? validate16.errors : vErrors.concat( - validate14.errors + validate16.errors ); errors = vErrors.length; } @@ -17268,7 +17371,7 @@ function validate26( typeof data.zipPath !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17303,7 +17406,7 @@ function validate26( typeof data.extractToPath !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17335,7 +17438,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/23/type', @@ -17364,7 +17467,7 @@ function validate26( (data.userId === undefined && (missing26 = 'userId')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/24/required', @@ -17390,7 +17493,7 @@ function validate26( key54 === 'userId' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -17430,7 +17533,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17470,7 +17573,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17506,7 +17609,7 @@ function validate26( typeof data146.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17534,7 +17637,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -17563,7 +17666,7 @@ function validate26( if ( typeof data149 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -17583,7 +17686,7 @@ function validate26( if ( 'updateUserMeta' !== data149 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -17630,7 +17733,7 @@ function validate26( } } } else { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17672,7 +17775,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17702,7 +17805,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/24/type', @@ -17731,7 +17834,7 @@ function validate26( (data.step === undefined && (missing27 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/25/required', @@ -17757,7 +17860,7 @@ function validate26( key57 === 'data' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -17797,7 +17900,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17837,7 +17940,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17873,7 +17976,7 @@ function validate26( typeof data153.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -17901,7 +18004,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -17930,7 +18033,7 @@ function validate26( if ( typeof data156 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -17948,7 +18051,7 @@ function validate26( return false; } if ('writeFile' !== data156) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -17978,7 +18081,7 @@ function validate26( typeof data.path !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -18010,7 +18113,7 @@ function validate26( let valid92 = false; const _errs431 = errors; if ( - !validate14( + !validate16( data158, { instancePath: @@ -18026,9 +18129,9 @@ function validate26( ) { vErrors = vErrors === null - ? validate14.errors + ? validate16.errors : vErrors.concat( - validate14.errors + validate16.errors ); errors = vErrors.length; @@ -18721,7 +18824,7 @@ function validate26( ); } errors++; - validate26.errors = + validate28.errors = vErrors; return false; } else { @@ -18749,7 +18852,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/25/type', @@ -18778,7 +18881,7 @@ function validate26( (data.writeToPath === undefined && (missing30 = 'writeToPath')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/26/required', @@ -18804,7 +18907,7 @@ function validate26( key61 === 'filesTree' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -18844,7 +18947,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -18884,7 +18987,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -18920,7 +19023,7 @@ function validate26( typeof data166.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -18948,7 +19051,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -18977,7 +19080,7 @@ function validate26( if ( typeof data169 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -18995,7 +19098,7 @@ function validate26( return false; } if ('writeFiles' !== data169) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -19028,7 +19131,7 @@ function validate26( typeof data.writeToPath !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -19057,7 +19160,7 @@ function validate26( ) { const _errs466 = errors; if ( - !validate16( + !validate18( data.filesTree, { instancePath: @@ -19073,9 +19176,9 @@ function validate26( ) { vErrors = vErrors === null - ? validate16.errors + ? validate18.errors : vErrors.concat( - validate16.errors + validate18.errors ); errors = vErrors.length; @@ -19091,7 +19194,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/26/type', @@ -19118,7 +19221,7 @@ function validate26( (data.step === undefined && (missing31 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/27/required', @@ -19144,7 +19247,7 @@ function validate26( key63 === 'wpCliPath' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -19184,7 +19287,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -19224,7 +19327,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -19260,7 +19363,7 @@ function validate26( typeof data172.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -19288,7 +19391,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -19317,7 +19420,7 @@ function validate26( if ( typeof data175 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -19335,7 +19438,7 @@ function validate26( return false; } if ('wp-cli' !== data175) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -19514,7 +19617,7 @@ function validate26( vErrors.push(err25); } errors++; - validate26.errors = + validate28.errors = vErrors; return false; } else { @@ -19543,7 +19646,7 @@ function validate26( typeof data.wpCliPath !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -19573,7 +19676,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/27/type', @@ -19600,7 +19703,7 @@ function validate26( (data.step === undefined && (missing32 = 'step')) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/28/required', @@ -19625,7 +19728,7 @@ function validate26( key65 === 'language' ) ) { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: @@ -19665,7 +19768,7 @@ function validate26( 'caption' ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -19705,7 +19808,7 @@ function validate26( ) ) ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -19741,7 +19844,7 @@ function validate26( typeof data179.caption !== 'string' ) { - validate26.errors = + validate28.errors = [ { instancePath: @@ -19769,7 +19872,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -19798,7 +19901,7 @@ function validate26( if ( typeof data182 !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -19819,7 +19922,7 @@ function validate26( 'setSiteLanguage' !== data182 ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -19851,7 +19954,7 @@ function validate26( typeof data.language !== 'string' ) { - validate26.errors = [ + validate28.errors = [ { instancePath: instancePath + @@ -19878,7 +19981,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/oneOf/28/type', @@ -19891,7 +19994,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/discriminator', @@ -19907,7 +20010,7 @@ function validate26( return false; } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/discriminator', @@ -19924,7 +20027,7 @@ function validate26( } } } else { - validate26.errors = [ + validate28.errors = [ { instancePath, schemaPath: '#/type', @@ -19936,7 +20039,7 @@ function validate26( return false; } } - validate26.errors = vErrors; + validate28.errors = vErrors; return errors === 0; } function validate11( @@ -20789,7 +20892,7 @@ function validate11( keyword: 'type', params: { - type: schema16 + type: schema18 .additionalProperties .type, }, @@ -20894,7 +20997,7 @@ function validate11( const _errs53 = errors; if ( - !validate14( + !validate16( data19, { instancePath: @@ -20912,9 +21015,9 @@ function validate11( vErrors = vErrors === null - ? validate14.errors + ? validate16.errors : vErrors.concat( - validate14.errors + validate16.errors ); errors = vErrors.length; @@ -21508,7 +21611,7 @@ function validate11( const _errs76 = errors; if ( - !validate26( + !validate28( data27, { instancePath: @@ -21526,9 +21629,9 @@ function validate11( vErrors = vErrors === null - ? validate26.errors + ? validate28.errors : vErrors.concat( - validate26.errors + validate28.errors ); errors = vErrors.length; diff --git a/packages/playground/blueprints/public/blueprint-schema.json b/packages/playground/blueprints/public/blueprint-schema.json index 4079f430a01..c964b1edda5 100644 --- a/packages/playground/blueprints/public/blueprint-schema.json +++ b/packages/playground/blueprints/public/blueprint-schema.json @@ -175,7 +175,7 @@ "BlueprintPHPVersion": { "anyOf": [ { - "$ref": "#/definitions/SupportedPHPVersion" + "$ref": "#/definitions/AllPHPVersion" }, { "type": "string", @@ -188,10 +188,24 @@ ], "description": "PHP versions accepted in Blueprint schema. Includes deprecated versions (7.2, 7.3) which are automatically upgraded to 7.4 during compilation." }, + "AllPHPVersion": { + "anyOf": [ + { + "$ref": "#/definitions/SupportedPHPVersion" + }, + { + "$ref": "#/definitions/LegacyPHPVersion" + } + ] + }, "SupportedPHPVersion": { "type": "string", "enum": ["8.5", "8.4", "8.3", "8.2", "8.1", "8.0", "7.4"] }, + "LegacyPHPVersion": { + "type": "string", + "enum": ["5.2"] + }, "ExtraLibrary": { "type": "string", "const": "wp-cli" diff --git a/packages/playground/blueprints/src/lib/types.ts b/packages/playground/blueprints/src/lib/types.ts index 2088f8e257a..d7d253be054 100644 --- a/packages/playground/blueprints/src/lib/types.ts +++ b/packages/playground/blueprints/src/lib/types.ts @@ -9,7 +9,7 @@ import type { BlueprintV2, BlueprintV2Declaration, } from './v2/blueprint-v2-declaration'; -import type { SupportedPHPVersion } from '@php-wasm/universal'; +import type { AllPHPVersion } from '@php-wasm/universal'; /** * A filesystem structure containing a /blueprint.json file and any @@ -23,7 +23,7 @@ export type BlueprintDeclaration = export type Blueprint = BlueprintV1 | BlueprintV2; export interface RuntimeConfiguration { - phpVersion: SupportedPHPVersion; + phpVersion: AllPHPVersion; wpVersion: string; intl: boolean; networking: boolean; diff --git a/packages/playground/blueprints/src/lib/v1/compile.ts b/packages/playground/blueprints/src/lib/v1/compile.ts index 60b021a61e4..c2a4f9fe7eb 100644 --- a/packages/playground/blueprints/src/lib/v1/compile.ts +++ b/packages/playground/blueprints/src/lib/v1/compile.ts @@ -1,10 +1,7 @@ import { ProgressTracker } from '@php-wasm/progress'; import { Semaphore } from '@php-wasm/util'; -import type { SupportedPHPVersion, UniversalPHP } from '@php-wasm/universal'; -import { - LatestSupportedPHPVersion, - SupportedPHPVersions, -} from '@php-wasm/universal'; +import type { AllPHPVersion, UniversalPHP } from '@php-wasm/universal'; +import { AllPHPVersions, LatestSupportedPHPVersion } from '@php-wasm/universal'; import type { FileReference } from './resources'; import { isResourceReference, Resource } from './resources'; import type { Step, StepDefinition, WriteFileStep } from '../steps'; @@ -94,7 +91,7 @@ export type CompiledV1Step = (php: UniversalPHP) => Promise | void; export interface CompiledBlueprintV1 { /** The requested versions of PHP and WordPress for the blueprint */ versions: { - php: SupportedPHPVersion; + php: AllPHPVersion; wp: string; }; features: { @@ -400,7 +397,7 @@ function compileBlueprintJson( versions: { php: compileVersion( blueprint.preferredVersions?.php, - SupportedPHPVersions, + AllPHPVersions, LatestSupportedPHPVersion ), wp: blueprint.preferredVersions?.wp || 'latest', diff --git a/packages/playground/blueprints/src/lib/v1/types.ts b/packages/playground/blueprints/src/lib/v1/types.ts index 44fa8066d14..d0f247cab3f 100644 --- a/packages/playground/blueprints/src/lib/v1/types.ts +++ b/packages/playground/blueprints/src/lib/v1/types.ts @@ -1,4 +1,4 @@ -import type { SupportedPHPVersion } from '@php-wasm/universal'; +import type { AllPHPVersion } from '@php-wasm/universal'; import type { StepDefinition } from '../steps'; import type { FileReference } from './resources'; import type { StreamedFile } from '@php-wasm/stream-compression'; @@ -19,7 +19,7 @@ export type BlueprintV1 = BlueprintV1Declaration | BlueprintBundle; * Includes deprecated versions (7.2, 7.3) which are automatically * upgraded to 7.4 during compilation. */ -export type BlueprintPHPVersion = SupportedPHPVersion | '7.2' | '7.3'; +export type BlueprintPHPVersion = AllPHPVersion | '7.2' | '7.3'; /** * The Blueprint declaration, typically stored in a blueprint.json file. diff --git a/packages/playground/cli/src/blueprints-v1/blueprints-v1-handler.ts b/packages/playground/cli/src/blueprints-v1/blueprints-v1-handler.ts index b03fb70ef06..a30ca4e01b0 100644 --- a/packages/playground/cli/src/blueprints-v1/blueprints-v1-handler.ts +++ b/packages/playground/cli/src/blueprints-v1/blueprints-v1-handler.ts @@ -2,6 +2,7 @@ import { logger } from '@php-wasm/logger'; import { EmscriptenDownloadMonitor, ProgressTracker } from '@php-wasm/progress'; import { consumeAPI, + isLegacyPHPVersion, type Pooled, type UniversalPHP, } from '@php-wasm/universal'; @@ -116,7 +117,13 @@ export class BlueprintsV1Handler { sqliteIntegrationPluginZip = undefined; } else { this.cliOutput.updateProgress('Preparing SQLite database'); - sqliteIntegrationPluginZip = await fetchSqliteIntegration(); + // Use pre-patched v2.2.22 for legacy PHP (closures replaced + // with named functions, PHP 5.2 polyfills added offline). + const phpVersion = this.args.php || RecommendedPHPVersion; + const isLegacyPhp = isLegacyPHPVersion(phpVersion); + const sqliteVersion = isLegacyPhp ? 'v2.2.22-php52' : 'trunk'; + sqliteIntegrationPluginZip = + await fetchSqliteIntegration(sqliteVersion); } this.cliOutput.updateProgress('Booting WordPress'); @@ -130,6 +137,7 @@ export class BlueprintsV1Handler { playground as unknown as PlaygroundCliBlueprintV1Worker ).bootWordPress( { + phpVersion: runtimeConfiguration.phpVersion, wpVersion: runtimeConfiguration.wpVersion, siteUrl: this.siteUrl, wordpressInstallMode: diff --git a/packages/playground/cli/src/blueprints-v1/download.ts b/packages/playground/cli/src/blueprints-v1/download.ts index 93c9f424dc5..094f07345bb 100644 --- a/packages/playground/cli/src/blueprints-v1/download.ts +++ b/packages/playground/cli/src/blueprints-v1/download.ts @@ -6,7 +6,9 @@ import path, { basename } from 'path'; export const CACHE_FOLDER = path.join(os.homedir(), '.wordpress-playground'); -export async function fetchSqliteIntegration(): Promise { +export async function fetchSqliteIntegration( + version: 'trunk' | 'v2.1.16' | 'v2.2.22' | 'v2.2.22-php52' = 'trunk' +): Promise { // Production builds: the ZIP sits next to the bundled JS. const dir = typeof __dirname !== 'undefined' ? __dirname : import.meta.dirname; @@ -22,7 +24,7 @@ export async function fetchSqliteIntegration(): Promise { wpBuildsDir, 'src', 'sqlite-database-integration', - 'sqlite-database-integration-trunk.zip' + `sqlite-database-integration-${version}.zip` ); } diff --git a/packages/playground/cli/src/blueprints-v1/worker-thread-v1.ts b/packages/playground/cli/src/blueprints-v1/worker-thread-v1.ts index c28cd4a159e..a5defa6d70b 100644 --- a/packages/playground/cli/src/blueprints-v1/worker-thread-v1.ts +++ b/packages/playground/cli/src/blueprints-v1/worker-thread-v1.ts @@ -1,7 +1,7 @@ import type { FileLockManager } from '@php-wasm/universal'; import { loadNodeRuntime } from '@php-wasm/node'; import { EmscriptenDownloadMonitor } from '@php-wasm/progress'; -import type { PathAlias, SupportedPHPVersion } from '@php-wasm/universal'; +import type { AllPHPVersion, PathAlias } from '@php-wasm/universal'; import { PHPWorker, releaseApiProxy, @@ -27,6 +27,7 @@ import type { Mount } from '@php-wasm/cli-util'; export type WorkerBootWordPressOptions = { siteUrl: string; + phpVersion?: string; wpVersion?: string; wordpressInstallMode: WordPressInstallMode; wordPressZip?: ArrayBuffer; @@ -40,7 +41,7 @@ export type WorkerBootWordPressOptions = { interface WorkerBootRequestHandlerOptions { siteUrl: string; - phpVersion: SupportedPHPVersion; + phpVersion: AllPHPVersion; processId: number; trace: boolean; nativeInternalDirPath: string; @@ -102,6 +103,7 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker { this.bootedWordPress = true; const { siteUrl, + phpVersion, wordpressInstallMode, wordPressZip, sqliteIntegrationPluginZip, @@ -112,6 +114,7 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker { try { await bootWordPress(this.__internal_getRequestHandler()!, { siteUrl, + phpVersion, wordpressInstallMode, wordPressZip: wordPressZip !== undefined @@ -161,6 +164,7 @@ export class PlaygroundCliBlueprintV1Worker extends PHPWorker { try { const requestHandler = await bootRequestHandler({ siteUrl: options.siteUrl, + phpVersion: options.phpVersion, maxPhpInstances: 1, createPhpRuntime: createPhpRuntimeFactory( options, diff --git a/packages/playground/cli/src/run-cli.ts b/packages/playground/cli/src/run-cli.ts index cc1c127bed2..3424d33af84 100644 --- a/packages/playground/cli/src/run-cli.ts +++ b/packages/playground/cli/src/run-cli.ts @@ -6,7 +6,7 @@ import { type PHPRequest, type PathAlias, type RemoteAPI, - type SupportedPHPVersion, + type AllPHPVersion, } from '@php-wasm/universal'; import { PHPResponse, @@ -46,6 +46,8 @@ import type { PlaygroundCliBlueprintV2Worker } from './blueprints-v2/worker-thre import type { XdebugOptions } from '@php-wasm/node'; /* eslint-disable no-console */ import { + AllPHPVersions, + isLegacyPHPVersion, SupportedPHPVersions, FileLockManagerInMemory, } from '@php-wasm/universal'; @@ -118,7 +120,7 @@ export async function parseOptionsAndRunCLI(argsToParse: string[]) { describe: 'PHP version to use.', type: 'string', default: RecommendedPHPVersion, - choices: SupportedPHPVersions, + choices: AllPHPVersions, }, wp: { describe: 'WordPress version to use.', @@ -373,7 +375,7 @@ export async function parseOptionsAndRunCLI(argsToParse: string[]) { describe: 'PHP version to use.', type: 'string', default: RecommendedPHPVersion, - choices: SupportedPHPVersions, + choices: AllPHPVersions, }, wp: { describe: 'WordPress version to use.', @@ -662,14 +664,24 @@ export async function parseOptionsAndRunCLI(argsToParse: string[]) { const hasDebugDefine = (name: string) => { return name in define || name in defineBool || name in defineNumber; }; - if (!hasDebugDefine('WP_DEBUG')) { - define['WP_DEBUG'] = 'true'; - } - if (!hasDebugDefine('WP_DEBUG_LOG')) { - define['WP_DEBUG_LOG'] = 'true'; - } - if (!hasDebugDefine('WP_DEBUG_DISPLAY')) { - define['WP_DEBUG_DISPLAY'] = 'false'; + // Don't default WP_DEBUG* on for legacy PHP: old WordPress + // (pre-2.3) prints E_NOTICE output before headers are sent, + // which corrupts redirects and breaks the installer. The web + // worker path applies the same gate in + // @wp-playground/client/src/blueprints-v1-handler.ts. + const phpVersionForDebug = (args['php'] || + RecommendedPHPVersion) as AllPHPVersion; + const isLegacyPhpForDebug = isLegacyPHPVersion(phpVersionForDebug); + if (!isLegacyPhpForDebug) { + if (!hasDebugDefine('WP_DEBUG')) { + define['WP_DEBUG'] = 'true'; + } + if (!hasDebugDefine('WP_DEBUG_LOG')) { + define['WP_DEBUG_LOG'] = 'true'; + } + if (!hasDebugDefine('WP_DEBUG_DISPLAY')) { + define['WP_DEBUG_DISPLAY'] = 'false'; + } } const cliArgs = { @@ -736,9 +748,7 @@ export async function parseOptionsAndRunCLI(argsToParse: string[]) { currentError = currentError.cause as Error; } while (currentError instanceof Error); console.error( - '\x1b[1m' + - messageChain.join(' caused by: ') + - '\x1b[0m' + '\x1b[1m' + messageChain.join(' caused by: ') + '\x1b[0m' ); } } else { @@ -811,7 +821,7 @@ export interface RunCLIArgs { mount?: Mount[]; 'mount-before-install'?: Mount[]; outfile?: string; - php?: SupportedPHPVersion; + php?: AllPHPVersion; port?: number; 'site-url'?: string; quiet?: boolean; @@ -990,6 +1000,15 @@ export async function runCLI(args: RunCLIArgs): Promise { args.memcached = await jspi(); } + // Disable all extensions for legacy PHP versions — they're not available. + const isLegacyPhp = isLegacyPHPVersion(args.php || RecommendedPHPVersion); + if (isLegacyPhp) { + args.intl = false; + args.redis = false; + args.memcached = false; + args.xdebug = false; + } + // Setup phpMyAdmin if enabled. if (args.phpmyadmin) { if (true === args.phpmyadmin) { @@ -1098,13 +1117,16 @@ export async function runCLI(args: RunCLIArgs): Promise { vfsPath: '/', }; - const isPHP85orHigher = - SupportedPHPVersions.indexOf( - args.php || RecommendedPHPVersion - ) <= SupportedPHPVersions.indexOf('8.5'); + const phpVer = args.php || RecommendedPHPVersion; + // SupportedPHPVersions is ordered newest-first, so a + // lower index means a higher version. + const isPhp85OrHigher = + SupportedPHPVersions.includes(phpVer as any) && + SupportedPHPVersions.indexOf(phpVer as any) <= + SupportedPHPVersions.indexOf('8.5'); // And, if PHP >= 8.5, add the new Xdebug config. - if (isPHP85orHigher) { + if (isPhp85OrHigher) { await createTempDirSymlink( nativeDir.path, symlinkPath, diff --git a/packages/playground/cli/tests/run-cli.spec.ts b/packages/playground/cli/tests/run-cli.spec.ts index bd354ab3773..b4648b767b6 100644 --- a/packages/playground/cli/tests/run-cli.spec.ts +++ b/packages/playground/cli/tests/run-cli.spec.ts @@ -171,10 +171,11 @@ describe.each(blueprintVersions)( test('should set WordPress version', async () => { const { MinifiedWordPressVersionsList } = await import('@wp-playground/wordpress-builds'); - const oldestSupportedVersion = - MinifiedWordPressVersionsList[ - MinifiedWordPressVersionsList.length - 1 - ]; + // Use the oldest non-legacy version. Legacy versions + // (< 5.0) require legacy PHP and can't boot on modern PHP. + const oldestSupportedVersion = MinifiedWordPressVersionsList.filter( + (v) => parseFloat(v) >= 5 + ).pop()!; await using cliServer = await runCLI({ ...suiteCliArgs, command: 'server', @@ -339,10 +340,7 @@ describe.each(blueprintVersions)( const mounts = []; for (let i = 0; i < 5; i++) { - const hostSubDir = path.join( - hostTmpDir, - `migration-${i}` - ); + const hostSubDir = path.join(hostTmpDir, `migration-${i}`); mkdirSync(hostSubDir, { recursive: true }); const hostFilePath = path.join( hostSubDir, diff --git a/packages/playground/client/src/blueprints-v1-handler.ts b/packages/playground/client/src/blueprints-v1-handler.ts index 5b125b030db..7e5655195d1 100644 --- a/packages/playground/client/src/blueprints-v1-handler.ts +++ b/packages/playground/client/src/blueprints-v1-handler.ts @@ -87,10 +87,20 @@ export class BlueprintsV1Handler { /** * Pre-fetch WordPress update checks to speed up the initial wp-admin load. + * Skip for old WordPress versions — the functions called by prefetch + * (wp_check_php_version, wp_update_plugins, etc.) don't exist or crash + * on legacy WP, and the resulting PHP errors create noise. + * + * parseFloat extracts the major version from strings like "6.8", + * "4.9.26", etc. Non-numeric values like "nightly" or "trunk" + * produce NaN, which Number.isFinite rejects — those fall + * through to enabling prefetch (correct for dev builds). * * @see https://github.com/WordPress/wordpress-playground/pull/2295 */ - if (runtimeConfiguration.networking) { + const wpMajor = parseFloat(runtimeConfiguration.wpVersion); + const isLegacyWpVersion = Number.isFinite(wpMajor) && wpMajor < 5; + if (runtimeConfiguration.networking && !isLegacyWpVersion) { await playground.prefetchUpdateChecks(); } diff --git a/packages/playground/remote/src/lib/playground-mu-plugin/0-playground-php52.php b/packages/playground/remote/src/lib/playground-mu-plugin/0-playground-php52.php new file mode 100644 index 00000000000..84391b9ad8a --- /dev/null +++ b/packages/playground/remote/src/lib/playground-mu-plugin/0-playground-php52.php @@ -0,0 +1,86 @@ + $request) { + $responses[$id] = false; + } + return $responses; + } + public static function test($capabilities = array()) { + return true; + } + } +} +$__requests_class = class_exists('Requests') ? 'Requests' : null; +if ($__requests_class) { + call_user_func(array($__requests_class, 'add_transport'), 'Wp_Http_Dummy'); +} +add_action('requests-requests.before_request', '_pg52_set_dummy_transport', 10, 5); +add_filter('http_api_transports', '_pg52_dummy_transports'); + +function _pg52_set_dummy_transport($url, $headers, $data, $type, &$options) { + $options['transport'] = 'Wp_Http_Dummy'; +} + +function _pg52_dummy_transports() { + return array('Dummy'); +} + +// Disable WP Cron on legacy WordPress only. On PHP 5.2 the HTTP API +// is stubbed with Wp_Http_Dummy (see above), so every spawn-cron +// request would return false and WordPress would quietly retry +// forever. Short-circuit the /wp-cron.php endpoint so nothing loops. +// +// Modern 0-playground.php intentionally does NOT define this: on PHP +// 7+ the Fetch transport works, so WP Cron can run for real. Keep +// this block legacy-only. +define('DISABLE_WP_CRON', true); +if (substr($_SERVER['PHP_SELF'], -12) === '/wp-cron.php') { + header('HTTP/1.1 503 Service Unavailable'); + header('Content-Type: text/plain'); + echo 'WP Cron is temporarily disabled in the Playground.'; + exit; +} diff --git a/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php b/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php index 883e90b7b4c..0ba7499298e 100644 --- a/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php +++ b/packages/playground/remote/src/lib/playground-mu-plugin/0-playground.php @@ -1,4 +1,11 @@ $data, 'url' => $url, 'method' => $options['type'], - 'blocking' => $options['blocking'] ?? true, + 'blocking' => isset($options['blocking']) ? $options['blocking'] : true, ] ) ); diff --git a/packages/playground/remote/src/lib/playground-worker-endpoint-blueprints-v1.ts b/packages/playground/remote/src/lib/playground-worker-endpoint-blueprints-v1.ts index 90530f21015..9cdf7af1d5c 100644 --- a/packages/playground/remote/src/lib/playground-worker-endpoint-blueprints-v1.ts +++ b/packages/playground/remote/src/lib/playground-worker-endpoint-blueprints-v1.ts @@ -12,6 +12,7 @@ import { LatestSqliteDriverVersion, MinifiedWordPressVersionsList, } from '@wp-playground/wordpress-builds'; +import { isLegacyPHPVersion } from '@php-wasm/universal'; import { directoryHandleFromMountDevice } from '@wp-playground/storage'; import { bootWordPress } from '@wp-playground/wordpress'; import { createDirectoryHandleMountHandler } from '@php-wasm/web'; @@ -74,9 +75,10 @@ class PlaygroundWorkerEndpointBlueprintsV1 extends PlaygroundWorkerEndpoint { this.requestedWordPressVersion = wpVersion === 'nightly' ? 'trunk' : wpVersion; - wpVersion = MinifiedWordPressVersionsList.includes( + const isMinifiedVersion = MinifiedWordPressVersionsList.includes( this.requestedWordPressVersion - ) + ); + wpVersion = isMinifiedVersion ? this.requestedWordPressVersion : LatestMinifiedWordPressVersion; @@ -113,6 +115,32 @@ class PlaygroundWorkerEndpointBlueprintsV1 extends PlaygroundWorkerEndpoint { } ); }); + } else if ( + !isMinifiedVersion && + /^\d+\.\d+(\.\d+)?$/.test(this.requestedWordPressVersion!) + ) { + // Non-minified dotted version like "4.9" or "1.5": + // download directly from wordpress.org. Sentinel + // values like "latest" fall through to the minified- + // bundle branch below and resolve to + // LatestMinifiedWordPressVersion. + const normalizedVersion = normalizeWordPressVersion( + this.requestedWordPressVersion! + ); + const wpOrgUrl = `https://wordpress.org/wordpress-${normalizedVersion}.zip`; + const downloadUrl = corsProxyUrl + ? `${corsProxyUrl}${wpOrgUrl}` + : wpOrgUrl; + wordPressRequest = this.downloadMonitor + .monitorFetch(fetch(downloadUrl)) + .then((response) => { + if (!response.ok) { + throw new Error( + `Failed to download WordPress ${normalizedVersion} (HTTP ${response.status})` + ); + } + return response; + }); } else { const downloadUrl = maybeProxyUrl( wpDetails.url, @@ -127,8 +155,16 @@ class PlaygroundWorkerEndpointBlueprintsV1 extends PlaygroundWorkerEndpoint { } } + // Select the right SQLite version: + // - PHP 5.2: pre-patched v2.2.22 (closures replaced, PHP 5.2 + // polyfills added) + // - Everything else: whatever the caller requested + const isLegacyPhp = isLegacyPHPVersion(phpVersion); + const effectiveSqliteVersion = isLegacyPhp + ? 'v2.2.22-php52' + : sqliteDriverVersion!; const sqliteDriverModuleDetails = getSqliteDriverModuleDetails( - sqliteDriverVersion! + effectiveSqliteVersion ); this.downloadMonitor.expectAssets({ [sqliteDriverModuleDetails.url]: sqliteDriverModuleDetails.size, @@ -139,9 +175,14 @@ class PlaygroundWorkerEndpointBlueprintsV1 extends PlaygroundWorkerEndpoint { await bootWordPress(requestHandler, { siteUrl, + phpVersion, constants: shouldInstallWordPress ? { - WP_DEBUG: true, + // Disable WP_DEBUG for legacy PHP (< 7) because + // old WordPress (< 3.1) doesn't have WP_DEBUG_DISPLAY + // and shows all notices when WP_DEBUG is true, + // breaking header output and install responses. + WP_DEBUG: !isLegacyPhp, WP_DEBUG_LOG: true, WP_DEBUG_DISPLAY: false, AUTH_KEY: randomString(40), @@ -212,6 +253,21 @@ const [setApiReady, setAPIError] = exposeAPI( new PlaygroundWorkerEndpointBlueprintsV1(downloadMonitor) ); +/** + * Normalizes WordPress version strings for wordpress.org downloads. + * Versions >= 2.0 work as `.` (wordpress.org redirects + * to the latest patch). Versions < 2.0 need explicit patch versions + * because wordpress.org doesn't host `wordpress-1.x.zip` files. + */ +function normalizeWordPressVersion(version: string): string { + const legacyVersionMap: Record = { + '1.0': '1.0.2', + '1.2': '1.2.2', + '1.5': '1.5.2', + }; + return legacyVersionMap[version] ?? version; +} + function maybeProxyUrl(url: string, corsProxyUrl?: string) { if ( !corsProxyUrl || diff --git a/packages/playground/remote/src/lib/playground-worker-endpoint.ts b/packages/playground/remote/src/lib/playground-worker-endpoint.ts index 6316c9e335f..462a586dfc5 100644 --- a/packages/playground/remote/src/lib/playground-worker-endpoint.ts +++ b/packages/playground/remote/src/lib/playground-worker-endpoint.ts @@ -25,8 +25,9 @@ import transportFetch from './playground-mu-plugin/playground-includes/wp_http_f /* @ts-ignore */ import transportDummy from './playground-mu-plugin/playground-includes/wp_http_dummy.php?raw'; import { logger } from '@php-wasm/logger'; -import type { PathAlias, PHP, SupportedPHPVersion } from '@php-wasm/universal'; +import type { AllPHPVersion, PathAlias, PHP } from '@php-wasm/universal'; import { + isLegacyPHPVersion, PHPResponse, PHPWorker, isPathToSharedFS, @@ -45,6 +46,8 @@ import { wpVersionToStaticAssetsDirectory } from '@wp-playground/wordpress-build import { networkingDisabledFunctions } from './disabled-functions'; /* @ts-ignore */ import playgroundWebMuPlugin from './playground-mu-plugin/0-playground.php?raw'; +/* @ts-ignore */ +import playgroundWebMuPluginPhp52 from './playground-mu-plugin/0-playground-php52.php?raw'; import { WordPressFetchNetworkTransport } from './wordpress-fetch-network-transport'; export interface MountDescriptor { @@ -56,7 +59,7 @@ export interface MountDescriptor { export type WorkerBootOptions = { wpVersion?: string; sqliteDriverVersion?: string; - phpVersion?: SupportedPHPVersion; + phpVersion?: AllPHPVersion; sapiName?: string; scope: string; withIntl: boolean; @@ -140,7 +143,7 @@ export abstract class PlaygroundWorkerEndpoint extends PHPWorker { knownRemoteAssetPaths: Set; withIntl: boolean; withNetworking: boolean; - phpVersion: SupportedPHPVersion; + phpVersion: AllPHPVersion; pathAliases?: PathAlias[]; }) { const phpIniEntries: Record = { @@ -189,8 +192,10 @@ export abstract class PlaygroundWorkerEndpoint extends PHPWorker { } const parsedSiteUrl = new URL(siteUrl); + const isLegacyPhp = isLegacyPHPVersion(phpVersion); const requestHandler = await bootRequestHandler({ siteUrl, + phpVersion, createPhpRuntime: async () => { let wasmUrl = ''; return await loadWebRuntime(phpVersion, { @@ -235,6 +240,10 @@ export abstract class PlaygroundWorkerEndpoint extends PHPWorker { // TODO: Document that this shift is a breaking change. // Proxy the filesystem for all secondary PHP instances to // the primary one. + // proxyFileSystem auto-detects legacy PHP from the + // replica's runtime and skips the PROXYFS mmap patch + // there, so this call is the same for every PHP + // version. await proxyFileSystem( await requestHandler.getPrimaryPhp(), php, @@ -252,7 +261,11 @@ export abstract class PlaygroundWorkerEndpoint extends PHPWorker { createFiles: { '/internal/shared/ca-bundle.crt': caBundleContent, '/internal/shared/mu-plugins': { - '1-playground-web.php': playgroundWebMuPlugin, + // Legacy PHP can't parse closures at all (even with an + // early return), so use a minimal compatible stub instead. + '1-playground-web.php': isLegacyPhp + ? playgroundWebMuPluginPhp52 + : playgroundWebMuPlugin, 'playground-includes': { 'wp_http_dummy.php': transportDummy, 'wp_http_fetch.php': transportFetch, diff --git a/packages/playground/wordpress/src/boot.ts b/packages/playground/wordpress/src/boot.ts index 6fd0d5ef8ac..c266361807e 100644 --- a/packages/playground/wordpress/src/boot.ts +++ b/packages/playground/wordpress/src/boot.ts @@ -51,6 +51,11 @@ export async function bootWordPressAndRequestHandler( export interface BootRequestHandlerOptions { createPhpRuntime: (isPrimary?: boolean) => Promise; + /** + * PHP version string (e.g. '8.3', '5.2'). Used to gate + * legacy-PHP-specific behavior in the boot chain. + */ + phpVersion?: string; onPHPInstanceCreated?: PHPInstanceCreatedHook; maxPhpInstances?: number; /** @@ -141,6 +146,8 @@ export type WordPressInstallMode = | 'do-not-attempt-installing'; export interface BootWordPressOptions { + /** PHP version string (e.g. '8.3', '5.2'). */ + phpVersion?: string; /** * Mounting and Copying is handled via hooks for starters. * diff --git a/tsconfig.base.json b/tsconfig.base.json index fb0c4f419c5..78e715c1b51 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -24,6 +24,9 @@ ], "@php-wasm/logger": ["packages/php-wasm/logger/src/index.ts"], "@php-wasm/node": ["packages/php-wasm/node/src/index.ts"], + "@php-wasm/node-5-2": [ + "packages/php-wasm/node-builds/5-2/src/index.ts" + ], "@php-wasm/node-7-2": [ "packages/php-wasm/node-builds/7-2/src/index.ts" ], @@ -63,6 +66,9 @@ ], "@php-wasm/util": ["packages/php-wasm/util/src/index.ts"], "@php-wasm/web": ["packages/php-wasm/web/src/index.ts"], + "@php-wasm/web-5-2": [ + "packages/php-wasm/web-builds/5-2/src/index.ts" + ], "@php-wasm/web-7-2": [ "packages/php-wasm/web-builds/7-2/src/index.ts" ],