Skip to content

Commit 513f284

Browse files
committed
better now
1 parent ca4e002 commit 513f284

File tree

3 files changed

+95
-86
lines changed

3 files changed

+95
-86
lines changed

Dockerfile

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ WORKDIR /src/sqlite
4040
RUN emcc -Oz -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DISABLE_LFS -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_NORMALIZE -c sqlite3.c -o sqlite3.o
4141

4242
FROM build_tool as php_src
43-
ARG PHP_BRANCH=PHP-8.2.5
43+
ARG PHP_BRANCH=PHP-8.2.9
4444
RUN git clone https://github.com/php/php-src.git php-src \
4545
--branch $PHP_BRANCH \
4646
--single-branch \
@@ -99,13 +99,13 @@ RUN emcc $OPTIMIZE \
9999
-I main \
100100
-I TSRM/ \
101101
-c \
102-
/src/source/pib_eval.c \
103-
-o /src/pib_eval.o \
102+
/src/source/phpw.c \
103+
-o /src/phpw.o \
104104
-s ERROR_ON_UNDEFINED_SYMBOLS=0
105105
RUN mkdir /build && emcc $OPTIMIZE \
106106
-o /build/php-$WASM_ENVIRONMENT.mjs \
107107
--llvm-lto 2 \
108-
-s EXPORTED_FUNCTIONS='["_phpw", "_phpw_flush", "_phpw_exec", "_phpw_run", "_php_embed_init", "_php_embed_shutdown", "_zend_eval_string"]' \
108+
-s EXPORTED_FUNCTIONS='["_phpw", "_phpw_flush", "_phpw_exec", "_phpw_run", "_chdir", "_setenv", "_php_embed_init", "_php_embed_shutdown", "_zend_eval_string"]' \
109109
-s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "UTF8ToString", "lengthBytesUTF8", "FS"]' \
110110
-s ENVIRONMENT=$WASM_ENVIRONMENT \
111111
-s FORCE_FILESYSTEM=1 \
@@ -121,4 +121,4 @@ RUN mkdir /build && emcc $OPTIMIZE \
121121
-s EXPORT_NAME=createPhpModule \
122122
# -s DECLARE_ASM_MODULE_EXPORTS=0 \
123123
-lidbfs.js \
124-
/src/pib_eval.o /src/usr/lib/sqlite3.o .libs/libphp.a /src/usr/lib/libxml2.a
124+
/src/phpw.o /src/usr/lib/sqlite3.o .libs/libphp.a /src/usr/lib/libxml2.a

source/phpw.c

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "sapi/embed/php_embed.h"
2+
#include <emscripten.h>
3+
#include <stdlib.h>
4+
5+
#include "zend_globals_macros.h"
6+
#include "zend_exceptions.h"
7+
#include "zend_closures.h"
8+
9+
int main() {
10+
return 0;
11+
}
12+
13+
void phpw_flush()
14+
{
15+
// output buffers are disabled by default so we don't need to flush
16+
// fflush(stdout);
17+
fprintf(stdout, "\n");
18+
// fflush(stderr);
19+
fprintf(stderr, "\n");
20+
}
21+
22+
char *EMSCRIPTEN_KEEPALIVE phpw_exec(char *code)
23+
{
24+
php_embed_init(0, NULL);
25+
char *retVal = NULL;
26+
27+
zend_try
28+
{
29+
zval retZv;
30+
31+
zend_eval_string(code, &retZv, "php-wasm evaluate expression");
32+
33+
convert_to_string(&retZv);
34+
35+
retVal = Z_STRVAL(retZv);
36+
} zend_catch {
37+
} zend_end_try();
38+
39+
phpw_flush();
40+
php_embed_shutdown();
41+
42+
return retVal;
43+
}
44+
45+
void EMSCRIPTEN_KEEPALIVE phpw_run(char *code)
46+
{
47+
php_embed_init(0, NULL);
48+
zend_try
49+
{
50+
zend_eval_string(code, NULL, "php-wasm run script");
51+
if(EG(exception))
52+
{
53+
zend_exception_error(EG(exception), E_ERROR);
54+
}
55+
} zend_catch {
56+
/* int exit_status = EG(exit_status); */
57+
} zend_end_try();
58+
59+
phpw_flush();
60+
php_embed_shutdown();
61+
}
62+
63+
int EMBED_SHUTDOWN = 1;
64+
65+
void phpw(char *file)
66+
{
67+
if (EMBED_SHUTDOWN == 0) {
68+
php_embed_shutdown();
69+
}
70+
71+
php_embed_init(0, NULL);
72+
EMBED_SHUTDOWN = 0;
73+
zend_first_try {
74+
zend_file_handle file_handle;
75+
zend_stream_init_filename(&file_handle, file);
76+
// file_handle.primary_script = 1;
77+
78+
if (php_execute_script(&file_handle) == FAILURE) {
79+
php_printf("Failed to execute PHP script.\n");
80+
}
81+
82+
zend_destroy_file_handle(&file_handle);
83+
} zend_catch {
84+
/* int exit_status = EG(exit_status); */
85+
} zend_end_try();
86+
87+
phpw_flush();
88+
php_embed_shutdown();
89+
EMBED_SHUTDOWN = 1;
90+
}

source/pib_eval.c

-81
This file was deleted.

0 commit comments

Comments
 (0)