Skip to content

Commit 1036db3

Browse files
committed
Support popen() in "w" mode
1 parent ac20149 commit 1036db3

File tree

9 files changed

+13724
-8198
lines changed

9 files changed

+13724
-8198
lines changed

packages/php-wasm/compile/Dockerfile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ RUN echo -n ' -s ASYNCIFY=1 -s ASYNCIFY_IGNORE_INDIRECT=1 ' >> /root/.emcc-php-w
601601
"invoke_viiiiiii",\n\
602602
"invoke_viiiiiiiii",\n\
603603
"js_open_process",\n\
604+
"js_popen_to_file",\n\
604605
"wasm_poll_socket",\n\
605606
"wasm_shutdown"]'; \
606607
echo -n " -s ASYNCIFY_IMPORTS=$ASYNCIFY_IMPORTS " | tr -d "\n" >> /root/.emcc-php-wasm-flags; \
@@ -697,6 +698,7 @@ RUN echo -n ' -s ASYNCIFY=1 -s ASYNCIFY_IGNORE_INDIRECT=1 ' >> /root/.emcc-php-w
697698
"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_TMPVAR_CONST_HANDLER",\
698699
"ZEND_ISSET_ISEMPTY_PROP_OBJ_SPEC_TMPVAR_HANDLER",\
699700
"cli",\
701+
"wasm_php_exec",\
700702
"wasm_sapi_handle_request",\
701703
"_call_user_function_ex",\
702704
"_call_user_function_impl",\
@@ -782,6 +784,7 @@ RUN echo -n ' -s ASYNCIFY=1 -s ASYNCIFY_IGNORE_INDIRECT=1 ' >> /root/.emcc-php-w
782784
"php_cli_server_do_event_for_each_fd_callback",\
783785
"php_cli_server_poller_poll",\
784786
"php_cli_server_recv_event_read_request",\
787+
"php_exec",\
785788
"php_execute_script",\
786789
"php_fsockopen_stream",\
787790
"php_getimagesize_from_any",\
@@ -904,6 +907,14 @@ RUN echo -n ' -s ASYNCIFY=1 -s ASYNCIFY_IGNORE_INDIRECT=1 ' >> /root/.emcc-php-w
904907
"zif_mysqli_stmt_execute",\
905908
"zif_mysqli_stmt_fetch",\
906909
"zif_preg_replace_callback",\
910+
"zif_popen",\
911+
"php_exec_ex",\
912+
"wasm_popen",\
913+
"zif_wasm_popen",\
914+
"zif_system",\
915+
"zif_exec",\
916+
"zif_passthru",\
917+
"zif_shell_exec",\
907918
"zif_proc_open",\
908919
"zif_stream_socket_client",\
909920
"zim_PDOStatement_execute",\
@@ -953,7 +964,7 @@ RUN source /root/emsdk/emsdk_env.sh && \
953964
"_wasm_set_request_port", \n\
954965
"_wasm_set_request_uri", \n\
955966
"_wasm_set_skip_shebang" '"$(cat /root/.EXPORTED_FUNCTIONS)"']'; \
956-
emcc -O3 \
967+
emcc -O3 -g2 \
957968
--js-library /root/phpwasm-emscripten-library.js \
958969
-I . \
959970
-I ext \

packages/php-wasm/compile/build-assets/phpwasm-emscripten-library.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,11 @@ const LibraryExample = {
312312
}
313313
}
314314
}
315-
console.log("ABC");
316315
PHPWASM.proc_fds[stdoutParentFd] = new EventEmitter();
317316
PHPWASM.proc_fds[stderrParentFd] = new EventEmitter();
318317

319-
console.log("DEF");
320318
const stdoutStream = SYSCALLS.getStreamFromFD(stdoutChildFd);
321319
cp.on("exit", function (data) {
322-
console.log("exit yay");
323320
PHPWASM.proc_fds[stdoutParentFd].exited = true;
324321
PHPWASM.proc_fds[stdoutParentFd].emit("data");
325322
PHPWASM.proc_fds[stderrParentFd].exited = true;
@@ -344,6 +341,7 @@ const LibraryExample = {
344341

345342
// Pass data from stdin fd to child process's stdin.
346343
if (PHPWASM.callback_pipes && procopenCallId in PHPWASM.callback_pipes) {
344+
347345
// It is a "pipe". By now it is listed in `callback_pipes`.
348346
// Let's listen to anything it outputs and pass it to the child process.
349347
PHPWASM.callback_pipes[procopenCallId].onData(function(data) {
@@ -366,7 +364,6 @@ const LibraryExample = {
366364
const buffer = Buffer.alloc(CHUNK_SIZE);
367365
let offset = 0;
368366

369-
console.log("Before")
370367
while (true) {
371368
const bytesRead = stdinStream.stream_ops.read(stdinStream, buffer, offset, CHUNK_SIZE, null);
372369
if (bytesRead === null) {
@@ -378,7 +375,6 @@ const LibraryExample = {
378375
}
379376
offset += bytesRead;
380377
}
381-
console.log("after")
382378

383379
return 0;
384380
},
@@ -600,18 +596,26 @@ const LibraryExample = {
600596
}
601597
throw e;
602598
}
603-
604-
const outBuffer = [];
599+
600+
const outByteArrays = [];
605601
cp.stdout.on("data", function (data) {
606-
outBuffer.push(data);
602+
outByteArrays.push(data);
607603
});
608604
return Asyncify.handleSleep((wakeUp) => {
609605
const outputPath = "/tmp/popen_output";
610606
cp.on("exit", function (exitCode) {
611-
FS.writeFile(
612-
outputPath,
613-
outBuffer.join("\n")
607+
// Concat outByteArrays, an array of UInt8Arrays
608+
// into a single Uint8Array.
609+
const outBytes = new Uint8Array(
610+
outByteArrays.reduce((acc, curr) => acc + curr.length, 0)
614611
);
612+
let offset = 0;
613+
for (const byteArray of outByteArrays) {
614+
outBytes.set(byteArray, offset);
615+
offset += byteArray.length;
616+
}
617+
618+
FS.writeFile(outputPath, outBytes);
615619

616620
HEAPU8[exitCodePtr] = exitCode;
617621
wakeUp(allocateUTF8OnStack(outputPath)); // 2 is SIGINT

0 commit comments

Comments
 (0)