Skip to content

Commit

Permalink
better now
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Aug 30, 2023
1 parent ca4e002 commit 513f284
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 86 deletions.
10 changes: 5 additions & 5 deletions Dockerfile
Expand Up @@ -40,7 +40,7 @@ WORKDIR /src/sqlite
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

FROM build_tool as php_src
ARG PHP_BRANCH=PHP-8.2.5
ARG PHP_BRANCH=PHP-8.2.9
RUN git clone https://github.com/php/php-src.git php-src \
--branch $PHP_BRANCH \
--single-branch \
Expand Down Expand Up @@ -99,13 +99,13 @@ RUN emcc $OPTIMIZE \
-I main \
-I TSRM/ \
-c \
/src/source/pib_eval.c \
-o /src/pib_eval.o \
/src/source/phpw.c \
-o /src/phpw.o \
-s ERROR_ON_UNDEFINED_SYMBOLS=0
RUN mkdir /build && emcc $OPTIMIZE \
-o /build/php-$WASM_ENVIRONMENT.mjs \
--llvm-lto 2 \
-s EXPORTED_FUNCTIONS='["_phpw", "_phpw_flush", "_phpw_exec", "_phpw_run", "_php_embed_init", "_php_embed_shutdown", "_zend_eval_string"]' \
-s EXPORTED_FUNCTIONS='["_phpw", "_phpw_flush", "_phpw_exec", "_phpw_run", "_chdir", "_setenv", "_php_embed_init", "_php_embed_shutdown", "_zend_eval_string"]' \
-s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "UTF8ToString", "lengthBytesUTF8", "FS"]' \
-s ENVIRONMENT=$WASM_ENVIRONMENT \
-s FORCE_FILESYSTEM=1 \
Expand All @@ -121,4 +121,4 @@ RUN mkdir /build && emcc $OPTIMIZE \
-s EXPORT_NAME=createPhpModule \
# -s DECLARE_ASM_MODULE_EXPORTS=0 \
-lidbfs.js \
/src/pib_eval.o /src/usr/lib/sqlite3.o .libs/libphp.a /src/usr/lib/libxml2.a
/src/phpw.o /src/usr/lib/sqlite3.o .libs/libphp.a /src/usr/lib/libxml2.a
90 changes: 90 additions & 0 deletions source/phpw.c
@@ -0,0 +1,90 @@
#include "sapi/embed/php_embed.h"
#include <emscripten.h>
#include <stdlib.h>

#include "zend_globals_macros.h"
#include "zend_exceptions.h"
#include "zend_closures.h"

int main() {
return 0;
}

void phpw_flush()
{
// output buffers are disabled by default so we don't need to flush
// fflush(stdout);
fprintf(stdout, "\n");
// fflush(stderr);
fprintf(stderr, "\n");
}

char *EMSCRIPTEN_KEEPALIVE phpw_exec(char *code)
{
php_embed_init(0, NULL);
char *retVal = NULL;

zend_try
{
zval retZv;

zend_eval_string(code, &retZv, "php-wasm evaluate expression");

convert_to_string(&retZv);

retVal = Z_STRVAL(retZv);
} zend_catch {
} zend_end_try();

phpw_flush();
php_embed_shutdown();

return retVal;
}

void EMSCRIPTEN_KEEPALIVE phpw_run(char *code)
{
php_embed_init(0, NULL);
zend_try
{
zend_eval_string(code, NULL, "php-wasm run script");
if(EG(exception))
{
zend_exception_error(EG(exception), E_ERROR);
}
} zend_catch {
/* int exit_status = EG(exit_status); */
} zend_end_try();

phpw_flush();
php_embed_shutdown();
}

int EMBED_SHUTDOWN = 1;

void phpw(char *file)
{
if (EMBED_SHUTDOWN == 0) {
php_embed_shutdown();
}

php_embed_init(0, NULL);
EMBED_SHUTDOWN = 0;
zend_first_try {
zend_file_handle file_handle;
zend_stream_init_filename(&file_handle, file);
// file_handle.primary_script = 1;

if (php_execute_script(&file_handle) == FAILURE) {
php_printf("Failed to execute PHP script.\n");
}

zend_destroy_file_handle(&file_handle);
} zend_catch {
/* int exit_status = EG(exit_status); */
} zend_end_try();

phpw_flush();
php_embed_shutdown();
EMBED_SHUTDOWN = 1;
}
81 changes: 0 additions & 81 deletions source/pib_eval.c

This file was deleted.

0 comments on commit 513f284

Please sign in to comment.