ba86758d70254cc18c32b826d0b38e0b11cc93f3
[m6w6/ext-psi] / src / types / assert_stmt.c
1 /*******************************************************************************
2 Copyright (c) 2017, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27
28 #include "data.h"
29 #include "calc.h"
30
31 struct psi_assert_stmt *psi_assert_stmt_init(enum psi_assert_kind kind, struct psi_num_exp *exp)
32 {
33 struct psi_assert_stmt *stmt = calloc(1, sizeof(*stmt));
34
35 stmt->kind = kind;
36 stmt->exp = exp;
37
38 return stmt;
39 }
40
41 void psi_assert_stmt_free(struct psi_assert_stmt **stmt_ptr)
42 {
43 if (*stmt_ptr) {
44 struct psi_assert_stmt *stmt = *stmt_ptr;
45
46 psi_num_exp_free(&stmt->exp);
47 if (stmt->token) {
48 free(stmt->token);
49 }
50 free(stmt);
51 *stmt_ptr = NULL;
52 }
53 }
54
55 void psi_assert_stmt_dump(int fd, struct psi_assert_stmt *stmt)
56 {
57 dprintf(fd, "\t%s_assert ", stmt->kind == PSI_ASSERT_PRE ? "pre" : "post");
58 psi_num_exp_dump(fd, stmt->exp);
59 dprintf(fd, ";\n");
60 }
61
62 bool psi_assert_stmt_exec(struct psi_assert_stmt *stmt, struct psi_call_frame *frame)
63 {
64 impl_val res, chk;
65 token_t res_type = psi_num_exp_exec(stmt->exp, &res, frame, NULL);
66
67 psi_calc_cast(res_type, &res, PSI_T_UINT8, &chk);
68 return chk.u8;
69 }
70
71 bool psi_assert_stmts_validate(struct psi_data *data, struct psi_impl *impl)
72 {
73 size_t i = 0;
74 struct psi_assert_stmt *ass;
75
76 /* we can have multiple assert stmts */
77 while (psi_plist_get(impl->stmts.ass, i++, &ass)) {
78 if (!psi_num_exp_validate(data, ass->exp, impl, NULL, NULL, NULL, NULL)) {
79 return false;
80 }
81 }
82
83 return true;
84 }
85
86 char *psi_assert_stmt_message(struct psi_assert_stmt *stmt)
87 {
88 /* FIXME */
89 struct stat sb;
90 char *message, template[] = "psi.assert.XXXXXX";
91 int fd = mkstemp(template);
92
93 dprintf(fd, "Failed asserting that ");
94 psi_num_exp_dump(fd, stmt->exp);
95 fstat(fd, &sb);
96 message = malloc(sb.st_size + 1);
97 lseek(fd, 0, SEEK_SET);
98 read(fd, message, sb.st_size);
99 close(fd);
100 message[sb.st_size] = '\0';
101 return message;
102 }