Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blueprints: Explore switching to the PHP implementation #1051

Closed
wants to merge 11 commits into from
Closed
5 changes: 3 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"file-saver": "^2.0.5",
"follow-redirects": "1.15.2",
"fs-extra": "11.1.1",
"node-forge": "1.3.1",
"octokit": "3.1.1",
"octokit-plugin-create-pull-request": "5.1.1",
"react": "^18.2.0",
Expand Down
1 change: 1 addition & 0 deletions packages/php-wasm/compile/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const platformDefaults = {
PHP_VERSION: '8.0.24',
WITH_LIBZIP: 'yes',
WITH_SQLITE: 'yes',
WITH_WS_NETWORKING_PROXY: 'yes',
},
['web-light']: {},
['web-kitchen-sink']: {
Expand Down
20 changes: 17 additions & 3 deletions packages/php-wasm/compile/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,17 @@ RUN echo -n ' -s ASYNCIFY=1 -s ASYNCIFY_IGNORE_INDIRECT=1 ' >> /root/.emcc-php-w
"dynCall_viiiii",\
"dynCall_viiiiiii",\
"dynCall_viiiiiiii",'; \
export ASYNCIFY_ONLY=$'"__fwritex",\
export ASYNCIFY_ONLY=$'"zend_generator_iterator_move_forward",\
"zend_fe_fetch_object_helper_SPEC",\
"ZEND_FE_FETCH_R_SPEC_VAR_HANDLER",\
"rc_dtor_func",\
"zend_generator_resume",\
"zend_call_method_if_exists",\
"zend_generator_iterator_rewind",\
"php_userstreamop_read",\
"zend_fe_reset_iterator",\
"ZEND_FE_RESET_R_SPEC_VAR_HANDLER",\
"__fwritex",\
"read",\
"zif_sleep",\
"zif_stream_get_contents",\
Expand Down Expand Up @@ -532,6 +542,9 @@ RUN echo -n ' -s ASYNCIFY=1 -s ASYNCIFY_IGNORE_INDIRECT=1 ' >> /root/.emcc-php-w
"_php_stream_open_wrapper_ex",\
"_php_stream_read",\
"_php_stream_set_option",\
"_php_stream_mmap_range",\
"_php_stream_copy_to_stream_ex",\
"zif_stream_copy_to_stream",\
"_php_stream_write",\
"_php_stream_xport_create",\
"do_cli",\
Expand Down Expand Up @@ -783,6 +796,7 @@ RUN set -euxo pipefail; \
"_wasm_set_phpini_path", \n\
"_wasm_set_phpini_entries", \n\
"_wasm_add_SERVER_entry", \n\
"_wasm_add_ENV_entry", \n\
"_wasm_read", \n\
"_wasm_sapi_handle_request", \n\
"_wasm_set_content_length", \n\
Expand All @@ -797,11 +811,11 @@ RUN set -euxo pipefail; \
"_wasm_set_request_port", \n\
"_wasm_set_request_uri", \n\
"_wasm_set_skip_shebang" '"$(cat /root/.EXPORTED_FUNCTIONS)"']'; \
export OPTIMIZATION_FLAGS="-O3"; \
export OPTIMIZATION_FLAGS="-O0 "; \
if [ "${WITH_SOURCEMAPS}" = "yes" ]; then \
export OPTIMIZATION_FLAGS="-O0"; \
fi; \
emcc $OPTIMIZATION_FLAGS \
emcc $OPTIMIZATION_FLAGS -g2 \
--js-library /root/phpwasm-emscripten-library.js \
-I . \
-I ext \
Expand Down
53 changes: 51 additions & 2 deletions packages/php-wasm/compile/php/php_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ EMSCRIPTEN_KEEPALIVE FILE *wasm_popen(const char *cmd, const char *mode)
NULL,
0,
descv,
3
3,
"",
0,
0,
0
);
// }}}

Expand Down Expand Up @@ -555,6 +559,7 @@ typedef struct
*php_code;

struct wasm_array_entry *server_array_entries;
struct wasm_array_entry *env_array_entries;

int content_length,
request_port,
Expand Down Expand Up @@ -588,6 +593,21 @@ void wasm_init_server_context();
static char *int_to_string(int i);
static int EMSCRIPTEN_KEEPALIVE run_php(char *code);

static char *wasm_sapi_getenv(const char *name, size_t name_len)
{
wasm_array_entry_t *current_entry = wasm_server_context->env_array_entries;
while (current_entry != NULL)
{
if (strncmp(current_entry->key, name, name_len) == 0)
{
return current_entry->value;
}
current_entry = current_entry->next;
}
return NULL;
}


SAPI_API sapi_module_struct php_wasm_sapi_module = {
"wasm", /* name */
"PHP WASM SAPI", /* pretty name */
Expand All @@ -601,7 +621,7 @@ SAPI_API sapi_module_struct php_wasm_sapi_module = {
wasm_sapi_ub_write, /* unbuffered write */
wasm_sapi_flush, /* flush */
NULL, /* get uid */
NULL, /* getenv */
wasm_sapi_getenv, /* getenv */

php_error, /* error handler */

Expand Down Expand Up @@ -661,6 +681,7 @@ void wasm_init_server_context()
wasm_server_context->execution_mode = MODE_EXECUTE_SCRIPT;
wasm_server_context->skip_shebang = 0;
wasm_server_context->server_array_entries = NULL;
wasm_server_context->env_array_entries = NULL;
}

void wasm_destroy_server_context()
Expand Down Expand Up @@ -712,6 +733,17 @@ void wasm_destroy_server_context()
free(current_entry);
current_entry = next_entry;
}

// Free wasm_server_context->env_array_entries
wasm_array_entry_t *current_env_entry = wasm_server_context->env_array_entries;
while (current_env_entry != NULL)
{
wasm_array_entry_t *next_entry = current_env_entry->next;
free(current_env_entry->key);
free(current_env_entry->value);
free(current_env_entry);
current_env_entry = next_entry;
}
}

/**
Expand Down Expand Up @@ -740,6 +772,23 @@ void wasm_add_SERVER_entry(char *key, char *value)
}
}

/**
* Function: wasm_add_ENV_entry
* ----------------------------
* Exposes a new entry to the getenv() function.
*
* key: the key of the entry
* value: the value of the entry
*/
void wasm_add_ENV_entry(char *key, char *value)
{
wasm_array_entry_t *entry = (wasm_array_entry_t *)malloc(sizeof(wasm_array_entry_t));
entry->key = strdup(key);
entry->value = strdup(value);
entry->next = wasm_server_context->env_array_entries;
wasm_server_context->env_array_entries = entry;
}

/**
* Function: wasm_set_query_string
* ----------------------------
Expand Down
Loading