Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@
"ini": "4.1.2",
"octokit": "3.1.2",
"pako": "1.0.10",
"ps-man": "1.1.8",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-hook-form": "7.53.0",
"react-redux": "8.1.3",
"react-transition-group": "4.4.5",
"sha.js": "2.4.11",
"tmp-promise": "3.0.3",
"wasm-feature-detect": "1.8.0",
"xml2js": "0.6.2",
"yargs": "17.7.2"
Expand Down
18 changes: 9 additions & 9 deletions packages/php-wasm/compile/php/php_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ EM_JS(int, wasm_poll_socket, (php_socket_t socketd, int events, int timeout), {
if (stream.stream_ops?.poll) {
mask = stream.stream_ops.poll(stream, -1);
}

mask &= events | POLLERR | POLLHUP;
if (mask) {
return mask;
Expand Down Expand Up @@ -418,7 +418,7 @@ EM_JS(__wasi_errno_t, js_fd_read, (__wasi_fd_t fd, const __wasi_iovec_t *iov, si
HEAPU32[pnum >> 2] = num;
return wakeUp(returnCode);
}

// It's a blocking stream and we Blocking stream with no data available yet.
// Let's poll up to a timeout.
await new Promise(resolve => setTimeout(resolve, interval));
Expand Down Expand Up @@ -1026,8 +1026,8 @@ int main(int argc, char *argv[]);
int run_cli()
{
// See wasm_sapi_request_init() for details on why we need to redirect stdout and stderr.
stdout_replacement = redirect_stream_to_file(stdout, "/internal/stdout");
stderr_replacement = redirect_stream_to_file(stderr, "/internal/stderr");
stdout_replacement = redirect_stream_to_file(stdout, "/request/stdout");
stderr_replacement = redirect_stream_to_file(stderr, "/request/stderr");
if (stdout_replacement == -1 || stderr_replacement == -1)
{
return -1;
Expand Down Expand Up @@ -1337,7 +1337,7 @@ void wasm_set_request_port(int port)
*
* stream: The stream to redirect, e.g. stdout or stderr.
*
* path: The path to the file to redirect to, e.g. "/internal/stdout".
* path: The path to the file to redirect to, e.g. "/request/stdout".
*
* returns: The exit code: 0 on success, -1 on failure.
*/
Expand Down Expand Up @@ -1559,11 +1559,11 @@ int wasm_sapi_request_init()
// Write to files instead of stdout and stderr because Emscripten truncates null
// bytes from stdout and stderr, and null bytes are a valid output when streaming
// binary data.
// We use our custom Emscripten-defined /internal/std* devices and handle the output in JavaScript.
// We use our custom Emscripten-defined /request/std* devices and handle the output in JavaScript.
// These /internal devices are not thread-safe and should always stay in per-process MEMFS space.
// Sharing them between PHP instances may cause intertwined output.
stdout_replacement = redirect_stream_to_file(stdout, "/internal/stdout");
stderr_replacement = redirect_stream_to_file(stderr, "/internal/stderr");
stdout_replacement = redirect_stream_to_file(stdout, "/request/stdout");
stderr_replacement = redirect_stream_to_file(stderr, "/request/stderr");
if (stdout_replacement == -1 || stderr_replacement == -1)
{
return -1;
Expand Down Expand Up @@ -1821,7 +1821,7 @@ FILE *headers_file;
*/
static int wasm_sapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
{
headers_file = fopen("/internal/headers", "w");
headers_file = fopen("/request/headers", "w");
if (headers_file == NULL)
{
return FAILURE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,34 +754,37 @@ const LibraryForFileLocking = {
#if ASYNCIFY == 2
return Asyncify.handleAsync(async () => {
#endif
// We have to get the VFS path from the file descriptor
// before closing it.
const [vfsPath, vfsPathResolutionErrno] =
locking.get_vfs_path_from_fd(fd);

const fdCloseResult = _builtin_fd_close(fd);
if (fdCloseResult !== 0 || !locking.maybeLockedFds.has(fd)) {
_js_wasm_trace('fd_close(%d) result %d', fd, fdCloseResult);
return fdCloseResult;
}

try {
const [vfsPath, pathResolutionErrno] =
locking.get_vfs_path_from_fd(fd);
if (pathResolutionErrno !== 0) {
_js_wasm_trace(
'fd_close(%d) get_vfs_path_from_fd error %d',
fd,
pathResolutionErrno
);
/*
* It looks like the file may have had an associated lock,
* but since we cannot look up the path,
* there is nothing more for us to do.
*
* NOTE: This seems possible for files that are locked and
* then unlinked before close. It is an opportunity for a
* lock to be orphaned in the lock manager.
* @TODO: Explore how to ensure cleanup in this case.
*/
return fdCloseResult;
}
if (vfsPathResolutionErrno !== 0) {
_js_wasm_trace(
'fd_close(%d) get_vfs_path_from_fd error %d',
fd,
vfsPathResolutionErrno
);
/*
* It looks like the file may have had an associated lock,
* but since we cannot look up the path,
* there is nothing more for us to do.
*
* NOTE: This seems possible for files that are locked and
* then unlinked before close. It is an opportunity for a
* lock to be orphaned in the lock manager.
* @TODO: Explore how to ensure cleanup in this case.
*/
return fdCloseResult;
}

try {
const nativeFilePath =
locking.get_native_path_from_vfs_path(vfsPath);
#if ASYNCIFY == 2
Expand Down
50 changes: 35 additions & 15 deletions packages/php-wasm/compile/php/phpwasm-emscripten-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const LibraryExample = {
// Emscripten dependencies:
$PHPWASM__deps: ['$allocateUTF8OnStack'],
$PHPWASM__postset: 'PHPWASM.init();',
$PHPWASM__postset: 'PHPWASM.init(PHPLoader?.phpWasmInitOptions);',

// Functions not exposed to C but available in the generated
// JavaScript library under the PHPWASM object:
Expand All @@ -28,7 +28,7 @@ const LibraryExample = {
// emscripten_O_NDELAY |
// emscripten_O_DIRECT |
// emscripten_O_NOATIME
init: function () {
init: function (phpWasmInitOptions) {
Module['ENV'] = Module['ENV'] || {};
// Ensure a platform-level bin directory for a fallback `php` binary.
Module['ENV']['PATH'] = [
Expand All @@ -38,28 +38,48 @@ const LibraryExample = {
.filter(Boolean)
.join(':');

// The /internal directory is required by the C module. It's where the
// 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.
// 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 wp-config.php, constants, etc.
FS.mkdir('/internal');

if (phpWasmInitOptions?.nativeInternalDirPath) {
FS.mount(
FS.filesystems.NODEFS,
{ root: phpWasmInitOptions.nativeInternalDirPath },
'/internal'
);
}

// The files from the shared directory are shared between all the
// PHP processes managed by PHPProcessManager.
FS.mkdir('/internal/shared');
FS.mkdirTree('/internal/shared');

// The files from the preload directory are preloaded using the
// auto_prepend_file php.ini directive.
FS.mkdir('/internal/shared/preload');
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.mkdir('/internal/shared/bin');
FS.mkdirTree('/internal/shared/bin');
const originalOnRuntimeInitialized = Module['onRuntimeInitialized'];
Module['onRuntimeInitialized'] = () => {
// Dummy PHP binary for PHP to populate the PHP_BINARY constant.
FS.writeFile(
const { node: phpBinaryNode } = FS.lookupPath(
'/internal/shared/bin/php',
new TextEncoder().encode('#!/bin/sh\nphp "$@"')
{ noent_okay: true },
);
// It must be executable to be used by PHP.
FS.chmod('/internal/shared/bin/php', 0o755);
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', 0o755);
}
originalOnRuntimeInitialized();
};

Expand All @@ -77,7 +97,7 @@ const LibraryExample = {
return length;
},
});
FS.mkdev('/internal/stdout', FS.makedev(64, 0));
FS.mkdev('/request/stdout', FS.makedev(64, 0));

FS.registerDevice(FS.makedev(63, 0), {
open: () => {},
Expand All @@ -89,7 +109,7 @@ const LibraryExample = {
return length;
},
});
FS.mkdev('/internal/stderr', FS.makedev(63, 0));
FS.mkdev('/request/stderr', FS.makedev(63, 0));

FS.registerDevice(FS.makedev(62, 0), {
open: () => {},
Expand All @@ -101,7 +121,7 @@ const LibraryExample = {
return length;
},
});
FS.mkdev('/internal/headers', FS.makedev(62, 0));
FS.mkdev('/request/headers', FS.makedev(62, 0));

// Handle events.
PHPWASM.EventEmitter = ENVIRONMENT_IS_NODE
Expand Down
Loading
Loading