build cleanup
[m6w6/ext-psi] / src / types / assert_stmt.c
index ba86758d70254cc18c32b826d0b38e0b11cc93f3..6dbed60568c6c3c8a4d512f21fb07b288443dc23 100644 (file)
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/
 
-#include "php_psi_stdinc.h"
-
 #include "data.h"
 #include "calc.h"
 
+#include "zend_smart_str.h"
+#include "zend_exceptions.h"
+#include "ext/spl/spl_exceptions.h"
+
 struct psi_assert_stmt *psi_assert_stmt_init(enum psi_assert_kind kind, struct psi_num_exp *exp)
 {
-       struct psi_assert_stmt *stmt = calloc(1, sizeof(*stmt));
+       struct psi_assert_stmt *stmt = pecalloc(1, sizeof(*stmt), 1);
 
        stmt->kind = kind;
        stmt->exp = exp;
@@ -43,20 +45,18 @@ void psi_assert_stmt_free(struct psi_assert_stmt **stmt_ptr)
        if (*stmt_ptr) {
                struct psi_assert_stmt *stmt = *stmt_ptr;
 
+               *stmt_ptr = NULL;
                psi_num_exp_free(&stmt->exp);
-               if (stmt->token) {
-                       free(stmt->token);
-               }
+               psi_token_free(&stmt->token);
                free(stmt);
-               *stmt_ptr = NULL;
        }
 }
 
-void psi_assert_stmt_dump(int fd, struct psi_assert_stmt *stmt)
+void psi_assert_stmt_dump(struct psi_dump *dump, struct psi_assert_stmt *stmt)
 {
-       dprintf(fd, "\t%s_assert ", stmt->kind == PSI_ASSERT_PRE ? "pre" : "post");
-       psi_num_exp_dump(fd, stmt->exp);
-       dprintf(fd, ";\n");
+       PSI_DUMP(dump, "\t%s_assert ", stmt->kind == PSI_ASSERT_PRE ? "pre" : "post");
+       psi_num_exp_dump(dump, stmt->exp);
+       PSI_DUMP(dump, ";\n");
 }
 
 bool psi_assert_stmt_exec(struct psi_assert_stmt *stmt, struct psi_call_frame *frame)
@@ -68,14 +68,14 @@ bool psi_assert_stmt_exec(struct psi_assert_stmt *stmt, struct psi_call_frame *f
        return chk.u8;
 }
 
-bool psi_assert_stmts_validate(struct psi_data *data, struct psi_impl *impl)
+bool psi_assert_stmts_validate(struct psi_data *data, struct psi_validate_scope *scope)
 {
        size_t i = 0;
        struct psi_assert_stmt *ass;
 
        /* we can have multiple assert stmts */
-       while (psi_plist_get(impl->stmts.ass, i++, &ass)) {
-               if (!psi_num_exp_validate(data, ass->exp, impl, NULL, NULL, NULL, NULL)) {
+       while (psi_plist_get(scope->impl->stmts.ass, i++, &ass)) {
+               if (!psi_num_exp_validate(data, ass->exp, scope)) {
                        return false;
                }
        }
@@ -83,20 +83,19 @@ bool psi_assert_stmts_validate(struct psi_data *data, struct psi_impl *impl)
        return true;
 }
 
-char *psi_assert_stmt_message(struct psi_assert_stmt *stmt)
+void psi_assert_stmt_throw(struct psi_assert_stmt *stmt)
 {
-       /* FIXME */
-       struct stat sb;
-       char *message, template[] = "psi.assert.XXXXXX";
-       int fd = mkstemp(template);
-
-       dprintf(fd, "Failed asserting that ");
-       psi_num_exp_dump(fd, stmt->exp);
-       fstat(fd, &sb);
-       message = malloc(sb.st_size + 1);
-       lseek(fd, 0, SEEK_SET);
-       read(fd, message, sb.st_size);
-       close(fd);
-       message[sb.st_size] = '\0';
-       return message;
+       struct psi_dump dump;
+       smart_str str = {0};
+
+       dump.ctx.hn = &str;
+       dump.fun = (psi_dump_cb) smart_str_append_printf;
+
+       PSI_DUMP(&dump, "Failed asserting that ");
+       psi_num_exp_dump(&dump, stmt->exp);
+       smart_str_0(&str);
+       zend_throw_exception(stmt->kind == PSI_ASSERT_PRE
+                       ? spl_ce_InvalidArgumentException
+                       : spl_ce_UnexpectedValueException, str.s->val, 0);
+       smart_str_free(&str);
 }