GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/extensions.c
Date: 2026-07-11 23:13:58
Exec Total Coverage
Lines: 106 106 100.0%
Functions: 18 18 100.0%
Branches: 51 52 98.1%

Line Branch Exec Source
1 /*
2 Copyright 2019, 2021, 2022, 2024 2026 Joel Svensson svenssonjoel@yahoo.se
3 2022 Benjamin Vedder
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <lbm_memory.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include <eval_cps.h>
25 #include <lbm_c_interop.h>
26
27 #include "extensions.h"
28 #include "lbm_utils.h"
29
30 static lbm_uint ext_max = 0;
31 static lbm_uint next_extension_ix = 0;
32
33 lbm_extension_t *extension_table = NULL;
34
35 77 void lbm_extensions_set_next(lbm_uint i) {
36 77 next_extension_ix = i;
37 77 }
38
39 1 lbm_value lbm_extensions_default(lbm_value *args, lbm_uint argn) {
40 (void)args;
41 (void)argn;
42 1 return ENC_SYM_EERROR;
43 }
44
45 67044 bool lbm_extensions_init(lbm_extension_t *extension_storage, lbm_uint extension_storage_size) {
46
4/4
✓ Branch 0 taken 67043 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 67042 times.
67044 if (extension_storage == NULL || extension_storage_size == 0) return false;
47
48 67042 extension_table = extension_storage;
49 67042 memset(extension_table, 0, sizeof(lbm_extension_t) * extension_storage_size);
50
51
2/2
✓ Branch 0 taken 16412882 times.
✓ Branch 1 taken 67042 times.
16479924 for (lbm_uint i = 0; i < extension_storage_size; i ++) {
52 16412882 extension_storage[i].fptr = lbm_extensions_default;
53 }
54
55 67042 next_extension_ix = 0;
56 67042 ext_max = (lbm_uint)extension_storage_size;
57
58 67042 return true;
59 }
60
61 4987312853 lbm_uint lbm_get_max_extensions(void) {
62 4987312853 return ext_max;
63 }
64
65 70677 lbm_uint lbm_get_num_extensions(void) {
66 70677 return next_extension_ix;
67 }
68
69 3 extension_fptr lbm_get_extension(lbm_uint sym) {
70 3 lbm_uint ext_id = sym - EXTENSION_SYMBOLS_START;
71 3 extension_fptr res = lbm_extensions_default;
72
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (ext_id < ext_max) {
73 2 res = extension_table[ext_id].fptr;
74 }
75 3 return res;
76 }
77
78 2 bool lbm_clr_extension(lbm_uint sym_id) {
79 2 lbm_uint ext_id = SYMBOL_IX(sym_id);
80
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (ext_id >= ext_max) {
81 1 return false;
82 }
83 1 extension_table[ext_id].name = NULL;
84 1 extension_table[ext_id].fptr = lbm_extensions_default;
85 1 return true;
86 }
87
88 90 bool lbm_lookup_extension_id(char *sym_str, lbm_uint *ix) {
89
2/2
✓ Branch 0 taken 29331 times.
✓ Branch 1 taken 47 times.
29378 for (lbm_uint i = 0; i < ext_max; i ++) {
90
2/2
✓ Branch 0 taken 9852 times.
✓ Branch 1 taken 19479 times.
29331 if(extension_table[i].name) {
91
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 9809 times.
9852 if (str_eq(extension_table[i].name, sym_str)) {
92 43 *ix = i + EXTENSION_SYMBOLS_START;
93 43 return true;
94 }
95 }
96 }
97 47 return false;
98 }
99
100 // Dynamically loaded (lbm) libraries use position independent code:
101 // So a "ext-..." string could end up
102 7225244 bool lbm_add_extension(const char *sym_str, extension_fptr ext) {
103 lbm_value symbol;
104
105 // symbol_by_name loops through all symbols. It may be enough
106 // to search only the extension table, but unsure what the effect will
107 // be if adding an extension with same str-name as a built-in or special
108 // form. The extension may override built-in...
109 //
110 // Check if symbol already exists.
111
2/2
✓ Branch 0 taken 2155 times.
✓ Branch 1 taken 7223089 times.
7225244 if (lbm_get_symbol_by_name(sym_str, &symbol)) {
112
2/2
✓ Branch 0 taken 2152 times.
✓ Branch 1 taken 3 times.
2155 if (lbm_is_extension(lbm_enc_sym(symbol))) {
113 // update the extension entry.
114 2152 extension_table[SYMBOL_IX(symbol)].fptr = ext;
115 2152 return true;
116 }
117 3 return false;
118 }
119
120
2/2
✓ Branch 0 taken 7223088 times.
✓ Branch 1 taken 1 times.
7223089 if (next_extension_ix < ext_max) {
121 7223088 lbm_uint sym_ix = next_extension_ix ++;
122 7223088 extension_table[sym_ix].name = sym_str;
123 7223088 extension_table[sym_ix].fptr = ext;
124 7223088 return true;
125 }
126 1 return false;
127 }
128
129 // Helpers for extension developers:
130
131 9746 static bool lbm_is_number_all(lbm_value *args, lbm_uint argn) {
132
2/2
✓ Branch 0 taken 20611 times.
✓ Branch 1 taken 9667 times.
30278 for (lbm_uint i = 0;i < argn;i++) {
133
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 20532 times.
20611 if (!lbm_is_number(args[i])) {
134 79 return false;
135 }
136 }
137 9667 return true;
138 }
139
140 6668 bool lbm_check_number_all(lbm_value *args, lbm_uint argn) {
141
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6660 times.
6668 if (!lbm_is_number_all(args, argn)) {
142 8 lbm_set_error_reason((char*)lbm_error_str_no_number);
143 8 return false;
144 }
145 6660 return true;
146 }
147
148 350 bool lbm_check_argn(lbm_uint argn, lbm_uint n) {
149
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 264 times.
350 if (argn != n) {
150 86 lbm_set_error_reason((char*)lbm_error_str_num_args);
151 86 return false;
152 } else {
153 264 return true;
154 }
155 }
156
157 3078 bool lbm_check_argn_number(lbm_value *args, lbm_uint argn, lbm_uint n) {
158
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 3007 times.
3078 if (!lbm_is_number_all(args, argn)) {
159 71 lbm_set_error_reason((char*)lbm_error_str_no_number);
160 71 return false;
161
2/2
✓ Branch 0 taken 1270 times.
✓ Branch 1 taken 1737 times.
3007 } else if (argn != n) {
162 1270 lbm_set_error_reason((char*)lbm_error_str_num_args);
163 1270 return false;
164 } else {
165 1737 return true;
166 }
167 }
168
169 34524 lbm_value make_list(int num, ...) {
170 va_list arguments;
171 34524 va_start (arguments, num);
172 34524 lbm_value res = ENC_SYM_NIL;
173
2/2
✓ Branch 0 taken 86916 times.
✓ Branch 1 taken 34524 times.
121440 for (int i = 0; i < num; i++) {
174 86916 res = lbm_cons(va_arg(arguments, lbm_value), res);
175 }
176 34524 va_end (arguments);
177 34524 return lbm_list_destructive_reverse(res);
178 }
179
180 1154613 size_t strlen_max(const char *s, size_t maxlen) {
181 size_t i;
182
2/2
✓ Branch 0 taken 3674538 times.
✓ Branch 1 taken 87 times.
3674625 for (i = 0; i < maxlen; i ++) {
183
2/2
✓ Branch 0 taken 1154526 times.
✓ Branch 1 taken 2520012 times.
3674538 if (s[i] == 0) break;
184 }
185 1154613 return i;
186 }
187
188 44096 bool strmatch(const char *str1, const char *str2) {
189 44096 size_t len = strlen(str1);
190
191
2/2
✓ Branch 0 taken 35161 times.
✓ Branch 1 taken 8935 times.
44096 if (str2[len] != ' ') {
192 35161 return false;
193 }
194
195 8935 bool same = true;
196
2/2
✓ Branch 0 taken 42097 times.
✓ Branch 1 taken 3384 times.
45481 for (unsigned int i = 0;i < len;i++) {
197
2/2
✓ Branch 0 taken 5551 times.
✓ Branch 1 taken 36546 times.
42097 if (str1[i] != str2[i]) {
198 5551 same = false;
199 5551 break;
200 }
201 }
202
203 8935 return same;
204 }
205
206
207 273 bool dec_opt_uint(lbm_value *args, lbm_uint argn, lbm_uint idx,
208 size_t default_val, size_t *out) {
209
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 33 times.
273 if (idx >= argn) { *out = default_val; return true; }
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (!lbm_is_number(args[idx])) return false;
211 33 int32_t v = lbm_dec_as_i32(args[idx]);
212 33 *out = (v < 0) ? 0 : (size_t)v;
213 33 return true;
214 }
215
216 /* create a lbm string from a substring from a c-string
217 (and then also from other lbm strings)
218 */
219 123882 lbm_value span_to_lbm(const char *data, size_t len) {
220 lbm_value res;
221
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 123816 times.
123882 if (!lbm_create_array(&res, len + 1)) return ENC_SYM_MERROR;
222 123816 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(res);
223 123816 memcpy(arr->data, data, len);
224 123816 ((char*)(arr->data))[len] = '\0';
225 123816 return res;
226 }
227