|
| 1 | +/** |
| 2 | + * Copyright (c) 2025 The PHP Foundation |
| 3 | + * Copyright (c) 2023-2024 Antoine Bluchet |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | + * copy of this software and associated documentation files (the "Software"), |
| 7 | + * to deal in the Software without restriction, including without limitation |
| 8 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | + * Software is furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | + * DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | +#include "sapi/embed/php_embed.h" |
| 24 | +#include <emscripten.h> |
| 25 | +#include <stdlib.h> |
| 26 | + |
| 27 | +#include "zend_globals_macros.h" |
| 28 | +#include "zend_exceptions.h" |
| 29 | +#include "zend_closures.h" |
| 30 | + |
| 31 | +int main() { |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +void phpw_flush() |
| 36 | +{ |
| 37 | + fprintf(stdout, "\n"); |
| 38 | + fprintf(stderr, "\n"); |
| 39 | +} |
| 40 | + |
| 41 | +char *EMSCRIPTEN_KEEPALIVE phpw_exec(char *code) |
| 42 | +{ |
| 43 | + setenv("USE_ZEND_ALLOC", "0", 1); |
| 44 | + php_embed_init(0, NULL); |
| 45 | + char *retVal = NULL; |
| 46 | + |
| 47 | + zend_try |
| 48 | + { |
| 49 | + zval ret_zv; |
| 50 | + |
| 51 | + zend_eval_string(code, &ret_zv, "expression"); |
| 52 | + convert_to_string(&ret_zv); |
| 53 | + |
| 54 | + retVal = Z_STRVAL(ret_zv); |
| 55 | + } zend_catch { |
| 56 | + } zend_end_try(); |
| 57 | + |
| 58 | + phpw_flush(); |
| 59 | + php_embed_shutdown(); |
| 60 | + |
| 61 | + return retVal; |
| 62 | +} |
| 63 | + |
| 64 | +void EMSCRIPTEN_KEEPALIVE phpw_run(char *code) |
| 65 | +{ |
| 66 | + setenv("USE_ZEND_ALLOC", "0", 1); |
| 67 | + php_embed_init(0, NULL); |
| 68 | + |
| 69 | + zend_try |
| 70 | + { |
| 71 | + zend_eval_string(code, NULL, "script"); |
| 72 | + if (EG(exception)) { |
| 73 | + zend_exception_error(EG(exception), E_ERROR); |
| 74 | + } |
| 75 | + } zend_catch { |
| 76 | + /* int exit_status = EG(exit_status); */ |
| 77 | + } zend_end_try(); |
| 78 | + |
| 79 | + phpw_flush(); |
| 80 | + php_embed_shutdown(); |
| 81 | +} |
| 82 | + |
| 83 | +int EMBED_SHUTDOWN = 1; |
| 84 | + |
| 85 | +void phpw(char *file) |
| 86 | +{ |
| 87 | + setenv("USE_ZEND_ALLOC", "0", 1); |
| 88 | + if (EMBED_SHUTDOWN == 0) { |
| 89 | + php_embed_shutdown(); |
| 90 | + } |
| 91 | + |
| 92 | + php_embed_init(0, NULL); |
| 93 | + EMBED_SHUTDOWN = 0; |
| 94 | + zend_first_try { |
| 95 | + zend_file_handle file_handle; |
| 96 | + zend_stream_init_filename(&file_handle, file); |
| 97 | + // file_handle.primary_script = 1; |
| 98 | + |
| 99 | + if (!php_execute_script(&file_handle)) { |
| 100 | + php_printf("Failed to execute PHP script.\n"); |
| 101 | + } |
| 102 | + |
| 103 | + zend_destroy_file_handle(&file_handle); |
| 104 | + } zend_catch { |
| 105 | + /* int exit_status = EG(exit_status); */ |
| 106 | + } zend_end_try(); |
| 107 | + |
| 108 | + phpw_flush(); |
| 109 | + php_embed_shutdown(); |
| 110 | + EMBED_SHUTDOWN = 1; |
| 111 | +} |
0 commit comments