flush
[m6w6/ext-psi] / src / module.c
index 4e7f4ce61aa59ff0b8c2628055e38092de62abd2..aff784dbc28235c13f65583f69c58f6962638d25 100644 (file)
 
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+# include "config.h"
 #endif
 
 #include "php.h"
 #include "php_ini.h"
 #include "ext/standard/info.h"
+#include "zend_constants.h"
+#include "zend_operators.h"
 
 #include "php_psi.h"
-#include "parser.h"
 
-#include "libjit.h"
+#if HAVE_LIBJIT
+# include "libjit.h"
+# ifndef HAVE_LIBFFI
+#  define PSI_ENGINE "jit"
+# endif
+#endif
+#if HAVE_LIBFFI
+# include "libffi.h"
+# define PSI_ENGINE "ffi"
+#endif
 
 ZEND_DECLARE_MODULE_GLOBALS(psi);
 
 PHP_INI_BEGIN()
-       STD_PHP_INI_ENTRY("psi.directory", "psis", PHP_INI_ALL, OnUpdateString, directory, zend_psi_globals, psi_globals)
+       STD_PHP_INI_ENTRY("psi.engine", PSI_ENGINE, PHP_INI_SYSTEM, OnUpdateString, engine, zend_psi_globals, psi_globals)
+       STD_PHP_INI_ENTRY("psi.directory", "psi.d", PHP_INI_SYSTEM, OnUpdateString, directory, zend_psi_globals, psi_globals)
 PHP_INI_END();
 
-void psi_error(int type, const char *msg, ...)
-{
-       char buf[0x1000];
-       va_list argv;
-
-       va_start(argv, msg);
-       vslprintf(buf, 0x1000, msg, argv);
-       va_end(argv);
+static zend_object_handlers psi_object_handlers;
+static zend_class_entry *psi_class_entry;
 
-       php_error(type, buf);
-}
-
-int psi_internal_type(impl_type *type)
+zend_class_entry *psi_object_get_class_entry()
 {
-       switch (type->type) {
-       case PSI_T_BOOL:
-               return _IS_BOOL;
-       case PSI_T_INT:
-       case PSI_T_LONG:
-               return IS_LONG;
-       case PSI_T_FLOAT:
-       case PSI_T_DOUBLE:
-               return IS_DOUBLE;
-       case PSI_T_STRING:
-               return IS_STRING;
-       case PSI_T_ARRAY:
-               return IS_ARRAY;
-       default:
-               return 0;
-       }
+       return psi_class_entry;
 }
 
-zend_internal_arg_info *psi_internal_arginfo(impl *impl)
+void psi_error_wrapper(void *context, PSI_Token *t, int type, const char *msg, ...)
 {
-       size_t i;
-       zend_internal_arg_info *aip;
-       zend_internal_function_info *fi;
-
-       aip = calloc(impl->func->args->count + 1, sizeof(*aip));
-
-       fi = (zend_internal_function_info *) &aip[0];
-       fi->required_num_args = psi_num_min_args(impl);
-       fi->return_reference = impl->func->return_reference;
-       fi->type_hint = psi_internal_type(impl->func->return_type);
-
-       for (i = 0; i < impl->func->args->count; ++i) {
-               impl_arg *iarg = impl->func->args->args[i];
-               zend_internal_arg_info *ai = &aip[i+1];
+       va_list argv;
+       const char *fn = NULL;
+       unsigned ln = 0;
 
-               ai->name = iarg->var->name;
-               ai->type_hint = psi_internal_type(iarg->type);
-               if (iarg->var->reference) {
-                       ai->pass_by_reference = 1;
-               }
-               if (iarg->var->reference || (iarg->def && iarg->def->type == PSI_T_NULL)) {
-                       ai->allow_null = 1;
+       if (context) {
+               if (PSI_DATA(context)->flags & PSI_PARSER_SILENT) {
+                       return;
                }
        }
 
-       return aip;
+       if (t) {
+               fn = t->file;
+               ln = t->line;
+       } else if (zend_is_executing()) {
+               fn = zend_get_executed_filename();
+               ln = zend_get_executed_lineno();
+       } else if (zend_is_compiling()) {
+               fn = zend_get_compiled_filename()->val;
+               ln = zend_get_compiled_lineno();
+       }
+
+       va_start(argv, msg);
+       psi_verror(type, fn, ln, msg, argv);
+       va_end(argv);
 }
 
-size_t psi_num_min_args(impl *impl)
+void psi_error(int type, const char *fn, unsigned ln, const char *msg, ...)
 {
-       size_t i, n = impl->func->args->count;
+       va_list argv;
 
-       for (i = 0; i < impl->func->args->count; ++i) {
-               if (impl->func->args->args[i]->def) {
-                       --n;
-               }
-       }
-       return n;
+       va_start(argv, msg);
+       psi_verror(type, fn, ln, msg, argv);
+       va_end(argv);
 }
 
-void psi_to_int(impl_val *ret_val, decl_arg *func, zval *return_value)
+void psi_verror(int type, const char *fn, unsigned ln, const char *msg, va_list argv)
 {
-       switch (real_decl_type(func->type)->type) {
-       case PSI_T_FLOAT:
-       case PSI_T_DOUBLE:
-               RETVAL_DOUBLE(deref_impl_val(0, ret_val, func)->dval);
-               convert_to_long(return_value);
-               break;
-       default:
-               RETVAL_LONG(deref_impl_val(0, ret_val, func)->lval);
-       }
+       zend_error_cb(type, fn, ln, msg, argv);
 }
 
-void psi_to_string(impl_val *ret_val, decl_arg *func, zval *return_value)
+static void psi_object_free(zend_object *o)
 {
-       switch (real_decl_type(func->type)->type) {
-       case PSI_T_CHAR:
-       case PSI_T_SINT8:
-       case PSI_T_UINT8:
-               if (!func->var->pointer_level) {
-                       char chr = ret_val->lval;
-                       RETVAL_STRINGL(&chr, 1);
-               } else {
-                       ret_val = deref_impl_val(1, ret_val, func);
-                       if (ret_val->ptr) {
-                               RETVAL_STRING(ret_val->ptr);
-                       } else {
-                               RETVAL_EMPTY_STRING();
-                       }
-               }
-               break;
-       case PSI_T_FLOAT:
-       case PSI_T_DOUBLE:
-               RETVAL_DOUBLE(deref_impl_val(0, ret_val, func)->dval);
-               convert_to_string(return_value);
-               break;
-       default:
-               RETVAL_LONG(deref_impl_val(0, ret_val, func)->lval);
-               convert_to_string(return_value);
-               break;
+       psi_object *obj = PSI_OBJ(NULL, o);
+
+       if (obj->data) {
+               /* FIXME: how about registering a destructor?
+               // free(obj->data); */
+               obj->data = NULL;
        }
+       zend_object_std_dtor(o);
 }
 
-ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl)
+static zend_object *psi_object_init(zend_class_entry *ce)
 {
-       impl_arg *iarg;
-
-       if (!impl->func->args->count) {
-               return zend_parse_parameters_none();
-       }
-
-       ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl), impl->func->args->count)
-       nextarg:
-               iarg = impl->func->args->args[_i];
-               if (iarg->def) {
-                       Z_PARAM_OPTIONAL;
-               }
-               if (PSI_T_BOOL == iarg->type->type) {
-                       if (iarg->def) {
-                               iarg->val.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
-                       }
-                       Z_PARAM_BOOL(iarg->val.bval);
-               } else if (PSI_T_INT == iarg->type->type) {
-                       if (iarg->def) {
-                               iarg->val.lval = zend_atol(iarg->def->text, strlen(iarg->def->text));
-                       }
-                       Z_PARAM_LONG(iarg->val.lval);
-               } else if (PSI_T_FLOAT == iarg->type->type) {
-                       if (iarg->def) {
-                               iarg->val.dval = zend_strtod(iarg->def->text, NULL);
-                       }
-                       Z_PARAM_DOUBLE(iarg->val.dval);
-               } else if (PSI_T_STRING == iarg->type->type) {
-                       struct {char *val; size_t len;} str;
-                       if (iarg->def) {
-                               /* FIXME */
-                               str.len = strlen(iarg->def->text) - 2;
-                               str.val = &iarg->def->text[1];
-                       }
-                       Z_PARAM_STR_EX(iarg->val.str, 1, 0);
-                       if (iarg->val.str) {
-                               zend_string_addref(iarg->val.str);
-                       } else if (iarg->def) {
-                               iarg->val.str = zend_string_init(str.val, str.len, 0);
-                       }
-               } else {
-                       error_code = ZPP_ERROR_FAILURE;
-                       break;
-               }
-               iarg->_zv = _arg;
-               if (_i < _max_num_args) {
-                       goto nextarg;
-               }
-       ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
+       psi_object *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
 
-       return SUCCESS;
+       zend_object_std_init(&o->std, ce);
+       object_properties_init(&o->std, ce);
+       o->std.handlers = &psi_object_handlers;
+       return &o->std;
 }
 
-impl_val *psi_do_let(decl_arg *darg)
-{
-       impl_val *arg_val = &darg->let->out;
-       impl_arg *iarg = darg->let->arg;
+ZEND_BEGIN_ARG_INFO_EX(ai_psi_dump, 0, 0, 0)
+       ZEND_ARG_INFO(0, stream)
+ZEND_END_ARG_INFO();
+static PHP_FUNCTION(psi_dump) {
+       php_stream *s;
+       zval *r = NULL;
+       int fd = STDOUT_FILENO;
 
-       if (!iarg) {
-               /* let foo = NULL */
-               memset(arg_val, 0, sizeof(*arg_val));
-               return arg_val;
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &r)) {
+               return;
        }
-       switch (darg->let->val->func->type) {
-       case PSI_T_BOOLVAL:
-               if (iarg->type->type == PSI_T_BOOL) {
-                       arg_val->cval = iarg->val.cval;
-               } else {
-                       arg_val->cval = zend_is_true(iarg->_zv);
-               }
-               break;
-       case PSI_T_INTVAL:
-               if (iarg->type->type == PSI_T_INT) {
-                       arg_val->lval = iarg->val.lval;
-               } else {
-                       arg_val->lval = zval_get_long(iarg->_zv);
-               }
-               break;
-       case PSI_T_STRVAL:
-               if (iarg->type->type == PSI_T_STRING) {
-                       arg_val->ptr = estrdup(iarg->val.str->val);
-                       darg->let->mem = arg_val->ptr;
-                       zend_string_release(iarg->val.str);
-               } else {
-                       zend_string *zs = zval_get_string(iarg->_zv);
-                       arg_val->ptr = estrdup(zs->val);
-                       darg->let->mem = arg_val->ptr;
-                       zend_string_release(zs);
-               }
-               break;
-       case PSI_T_STRLEN:
-               if (iarg->type->type == PSI_T_STRING) {
-                       arg_val->lval = iarg->val.str->len;
-                       zend_string_release(iarg->val.str);
-               } else {
-                       zend_string *zs = zval_get_string(iarg->_zv);
-                       arg_val->lval = zs->len;
-                       zend_string_release(zs);
+       if (r) {
+               php_stream_from_zval(s, r);
+
+               if (SUCCESS != php_stream_cast(s, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void **)&fd, 1)) {
+                       RETURN_FALSE;
                }
-               break;
-       EMPTY_SWITCH_DEFAULT_CASE();
        }
-
-       return arg_val;
+       PSI_ContextDump(&PSI_G(context), fd);
 }
 
-void psi_do_set(zval *return_value, set_func *func, decl_vars *vars)
-{
-       impl_val *val = (impl_val *) &vars->vars[0]->arg->let->ptr;
-
-       ZVAL_DEREF(return_value);
-       zval_dtor(return_value);
+ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate, 0, 0, 1)
+       ZEND_ARG_INFO(0, file)
+ZEND_END_ARG_INFO();
+static PHP_FUNCTION(psi_validate) {
+       zend_string *file;
+       PSI_Parser P;
 
-       switch (func->type) {
-       case PSI_T_TO_STRING:
-               psi_to_string(val, vars->vars[0]->arg, return_value);
-               break;
-       EMPTY_SWITCH_DEFAULT_CASE();
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file)) {
+               return;
        }
-}
 
-void psi_do_return(impl *impl, impl_val *ret_val, zval *return_value)
-{
-       switch (impl->stmts->ret.list[0]->func->type) {
-       case PSI_T_TO_STRING:
-               psi_to_string(ret_val, impl->decl->func, return_value);
-               break;
-       case PSI_T_TO_INT:
-               psi_to_int(ret_val, impl->decl->func, return_value);
-               break;
-       EMPTY_SWITCH_DEFAULT_CASE();
+       if (!PSI_ParserInit(&P, file->val, psi_error_wrapper, 0)) {
+               RETURN_FALSE;
        }
-}
-
-void psi_do_free(free_stmt *fre)
-{
-       size_t i;
 
-       for (i = 0; i < fre->vars->count; ++i) {
-               decl_var *dvar = fre->vars->vars[i];
-
-               if (dvar->arg && dvar->arg->let->out.ptr) {
-                       free(dvar->arg->let->out.ptr);
-                       dvar->arg->let->out.ptr = NULL;
+       while (0 < PSI_ParserScan(&P)) {
+               PSI_ParserParse(&P, PSI_TokenAlloc(&P));
+               if (P.num == PSI_T_EOF) {
+                       break;
                }
        }
+       PSI_ParserParse(&P, NULL);
+
+       if (0 == PSI_ContextValidateData(NULL, PSI_DATA(&P)) && !P.errors) {
+               RETVAL_TRUE;
+       } else {
+               RETVAL_FALSE;
+       }
+       PSI_ParserDtor(&P);
 }
 
-void psi_do_clean(impl *impl)
+static PHP_MINIT_FUNCTION(psi)
 {
-       size_t i;
+       PSI_ContextOps *ops = NULL;
+       zend_class_entry ce = {0};
+       unsigned flags = psi_check_env("PSI_DEBUG") ? PSI_PARSER_DEBUG : (
+                       psi_check_env("PSI_SILENT") ? PSI_PARSER_SILENT : 0);
 
-       for (i = 0; i < impl->func->args->count; ++i ) {
-               impl_arg *iarg = impl->func->args->args[i];
+       REGISTER_INI_ENTRIES();
 
-               switch (iarg->type->type) {
-               case PSI_T_STRING:
-                       if (iarg->val.str) {
-                               zend_string_release(iarg->val.str);
-                       }
-                       break;
-               }
-       }
+       INIT_NS_CLASS_ENTRY(ce, "psi", "object", NULL);
+       psi_class_entry = zend_register_internal_class_ex(&ce, NULL);
+       psi_class_entry->create_object = psi_object_init;
 
-       for (i = 0; i < impl->decl->args->count; ++i) {
-               decl_arg *darg = impl->decl->args->args[i];
+       memcpy(&psi_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+       psi_object_handlers.offset = XtOffsetOf(psi_object, std);
+       psi_object_handlers.free_obj = psi_object_free;
+       psi_object_handlers.clone_obj = NULL;
 
-               if (darg->let && darg->let->mem) {
-                       efree(darg->let->mem);
-                       darg->let->mem = NULL;
-               }
-       }
-}
+#ifdef HAVE_LIBJIT
+       if (!strcasecmp(PSI_G(engine), "jit")) {
+               ops = PSI_Libjit();
+       } else
+#endif
+#ifdef HAVE_LIBFFI
+               ops = PSI_Libffi();
+#endif
 
-PHP_MINIT_FUNCTION(psi)
-{
-       REGISTER_INI_ENTRIES();
+       if (!ops) {
+               php_error(E_WARNING, "No PSI engine found");
+               return FAILURE;
+       }
 
-       PSI_ContextInit(&PSI_G(context), PSI_Libjit(), psi_error);
+       PSI_ContextInit(&PSI_G(context), ops, psi_error_wrapper, flags);
        PSI_ContextBuild(&PSI_G(context), PSI_G(directory));
 
+       if (psi_check_env("PSI_DUMP")) {
+               PSI_ContextDump(&PSI_G(context), STDOUT_FILENO);
+       }
+
        return SUCCESS;
 }
-PHP_MSHUTDOWN_FUNCTION(psi)
+
+static PHP_MSHUTDOWN_FUNCTION(psi)
 {
        PSI_ContextDtor(&PSI_G(context));
 
@@ -329,28 +204,15 @@ PHP_MSHUTDOWN_FUNCTION(psi)
        return SUCCESS;
 }
 
-/* Remove if there's nothing to do at request start */
-/* {{{ PHP_RINIT_FUNCTION
- */
-PHP_RINIT_FUNCTION(psi)
-{
 #if defined(COMPILE_DL_PSI) && defined(ZTS)
-       ZEND_TSRMLS_CACHE_UPDATE();
-#endif
-       return SUCCESS;
-}
-/* }}} */
-
-/* Remove if there's nothing to do at request end */
-/* {{{ PHP_RSHUTDOWN_FUNCTION
- */
-PHP_RSHUTDOWN_FUNCTION(psi)
+static PHP_RINIT_FUNCTION(psi)
 {
+       ZEND_TSRMLS_CACHE_UPDATE();
        return SUCCESS;
 }
-/* }}} */
+#endif
 
-PHP_MINFO_FUNCTION(psi)
+static PHP_MINFO_FUNCTION(psi)
 {
        php_info_print_table_start();
        php_info_print_table_header(2, "psi support", "enabled");
@@ -358,7 +220,10 @@ PHP_MINFO_FUNCTION(psi)
 
        DISPLAY_INI_ENTRIES();
 }
-const zend_function_entry psi_functions[] = {
+
+static const zend_function_entry psi_functions[] = {
+       PHP_FE(psi_dump, ai_psi_dump)
+       PHP_FE(psi_validate, ai_psi_validate)
        PHP_FE_END
 };
 
@@ -368,8 +233,12 @@ zend_module_entry psi_module_entry = {
        psi_functions,
        PHP_MINIT(psi),
        PHP_MSHUTDOWN(psi),
+#if defined(COMPILE_DL_PSI) && defined(ZTS)
        PHP_RINIT(psi),         /* Replace with NULL if there's nothing to do at request start */
-       PHP_RSHUTDOWN(psi),     /* Replace with NULL if there's nothing to do at request end */
+#else
+       NULL,
+#endif
+       NULL,
        PHP_MINFO(psi),
        PHP_PSI_VERSION,
        STANDARD_MODULE_PROPERTIES