GCC Code Coverage Report


Directory: ../src/
File: extensions.c
Date: 2026-10-16 14:49:47
Coverage Exec Excl Total
Lines: 100.0% 106 0 106
Functions: 100.0% 18 0 18
Branches: 98.1% 51 0 52

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 29357 bool lbm_extensions_init(lbm_extension_t *extension_storage, lbm_uint extension_storage_size) {
46
4/4
✓ Branch 0 taken 29356 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 29355 times.
29357 if (extension_storage == NULL || extension_storage_size == 0) return false;
47
48 29355 extension_table = extension_storage;
49 29355 memset(extension_table, 0, sizeof(lbm_extension_t) * extension_storage_size);
50
51
2/2
✓ Branch 0 taken 9079570 times.
✓ Branch 1 taken 29355 times.
9108925 for (lbm_uint i = 0; i < extension_storage_size; i ++) {
52 9079570 extension_storage[i].fptr = lbm_extensions_default;
53 }
54
55 29355 next_extension_ix = 0;
56 29355 ext_max = (lbm_uint)extension_storage_size;
57
58 29355 return true;
59 }
60
61 4077228276 lbm_uint lbm_get_max_extensions(void) {
62 4077228276 return ext_max;
63 }
64
65 33764 lbm_uint lbm_get_num_extensions(void) {
66 33764 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 time.
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 time.
✓ Branch 1 taken 1 time.
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 42 bool lbm_lookup_extension_id(char *sym_str, lbm_uint *ix) {
89
2/2
✓ Branch 0 taken 22047 times.
✓ Branch 1 taken 23 times.
22070 for (lbm_uint i = 0; i < ext_max; i ++) {
90
2/2
✓ Branch 0 taken 5064 times.
✓ Branch 1 taken 16983 times.
22047 if(extension_table[i].name) {
91
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 5045 times.
5064 if (str_eq(extension_table[i].name, sym_str)) {
92 19 *ix = i + EXTENSION_SYMBOLS_START;
93 19 return true;
94 }
95 }
96 }
97 23 return false;
98 }
99
100 // Dynamically loaded (lbm) libraries use position independent code:
101 // So a "ext-..." string could end up
102 3395883 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 1 taken 2822 times.
✓ Branch 2 taken 3393061 times.
3395883 if (lbm_get_symbol_by_name(sym_str, &symbol)) {
112
2/2
✓ Branch 2 taken 2819 times.
✓ Branch 3 taken 3 times.
2822 if (lbm_is_extension(lbm_enc_sym(symbol))) {
113 // update the extension entry.
114 2819 extension_table[SYMBOL_IX(symbol)].fptr = ext;
115 2819 return true;
116 }
117 3 return false;
118 }
119
120
2/2
✓ Branch 0 taken 3393060 times.
✓ Branch 1 taken 1 time.
3393061 if (next_extension_ix < ext_max) {
121 3393060 lbm_uint sym_ix = next_extension_ix ++;
122 3393060 extension_table[sym_ix].name = sym_str;
123 3393060 extension_table[sym_ix].fptr = ext;
124 3393060 return true;
125 }
126 1 return false;
127 }
128
129 // Helpers for extension developers:
130
131 4322 static bool lbm_is_number_all(lbm_value *args, lbm_uint argn) {
132
2/2
✓ Branch 0 taken 8995 times.
✓ Branch 1 taken 4243 times.
13238 for (lbm_uint i = 0;i < argn;i++) {
133
2/2
✓ Branch 1 taken 79 times.
✓ Branch 2 taken 8916 times.
8995 if (!lbm_is_number(args[i])) {
134 79 return false;
135 }
136 }
137 4243 return true;
138 }
139
140 2876 bool lbm_check_number_all(lbm_value *args, lbm_uint argn) {
141
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2868 times.
2876 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 2868 return true;
146 }
147
148 206 bool lbm_check_argn(lbm_uint argn, lbm_uint n) {
149
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 168 times.
206 if (argn != n) {
150 38 lbm_set_error_reason((char*)lbm_error_str_num_args);
151 38 return false;
152 } else {
153 168 return true;
154 }
155 }
156
157 1446 bool lbm_check_argn_number(lbm_value *args, lbm_uint argn, lbm_uint n) {
158
2/2
✓ Branch 1 taken 71 times.
✓ Branch 2 taken 1375 times.
1446 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 550 times.
✓ Branch 1 taken 825 times.
1375 } else if (argn != n) {
162 550 lbm_set_error_reason((char*)lbm_error_str_num_args);
163 550 return false;
164 } else {
165 825 return true;
166 }
167 }
168
169 70462 lbm_value make_list(int num, ...) {
170 va_list arguments;
171 70462 va_start (arguments, num);
172 70462 lbm_value res = ENC_SYM_NIL;
173
2/2
✓ Branch 0 taken 180060 times.
✓ Branch 1 taken 70462 times.
250522 for (int i = 0; i < num; i++) {
174 180060 res = lbm_cons(va_arg(arguments, lbm_value), res);
175 }
176 70462 va_end (arguments);
177 70462 return lbm_list_destructive_reverse(res);
178 }
179
180 495765 size_t strlen_max(const char *s, size_t maxlen) {
181 size_t i;
182
2/2
✓ Branch 0 taken 1583514 times.
✓ Branch 1 taken 39 times.
1583553 for (i = 0; i < maxlen; i ++) {
183
2/2
✓ Branch 0 taken 495726 times.
✓ Branch 1 taken 1087788 times.
1583514 if (s[i] == 0) break;
184 }
185 495765 return i;
186 }
187
188 20985 bool strmatch(const char *str1, const char *str2) {
189 20985 size_t len = strlen(str1);
190
191
2/2
✓ Branch 0 taken 16723 times.
✓ Branch 1 taken 4262 times.
20985 if (str2[len] != ' ') {
192 16723 return false;
193 }
194
195 4262 bool same = true;
196
2/2
✓ Branch 0 taken 19746 times.
✓ Branch 1 taken 1696 times.
21442 for (unsigned int i = 0;i < len;i++) {
197
2/2
✓ Branch 0 taken 2566 times.
✓ Branch 1 taken 17180 times.
19746 if (str1[i] != str2[i]) {
198 2566 same = false;
199 2566 break;
200 }
201 }
202
203 4262 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 1 not taken.
✓ Branch 2 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 53378 lbm_value span_to_lbm(const char *data, size_t len) {
220 lbm_value res;
221
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 53354 times.
53378 if (!lbm_create_array(&res, len + 1)) return ENC_SYM_MERROR;
222 53354 lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(res);
223 53354 memcpy(arr->data, data, len);
224 53354 ((char*)(arr->data))[len] = '\0';
225 53354 return res;
226 }
227