Debian Codegraph @

src/options.c

raw symbols SCIP
1 #include "prism/options.h"
2
3 /**
4 * Set the shebang callback option on the given options struct.
5 */
6 PRISM_EXPORTED_FUNCTION void
7 pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data) {
8 options->shebang_callback = shebang_callback;
9 options->shebang_callback_data = shebang_callback_data;
10 }
11
12 /**
13 * Set the filepath option on the given options struct.
14 */
15 PRISM_EXPORTED_FUNCTION void
16 pm_options_filepath_set(pm_options_t *options, const char *filepath) {
17 pm_string_constant_init(&options->filepath, filepath, strlen(filepath));
18 }
19
20 /**
21 * Set the encoding option on the given options struct.
22 */
23 PRISM_EXPORTED_FUNCTION void
24 pm_options_encoding_set(pm_options_t *options, const char *encoding) {
25 pm_string_constant_init(&options->encoding, encoding, strlen(encoding));
26 }
27
28 /**
29 * Set the encoding_locked option on the given options struct.
30 */
31 PRISM_EXPORTED_FUNCTION void
32 pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked) {
33 options->encoding_locked = encoding_locked;
34 }
35
36 /**
37 * Set the line option on the given options struct.
38 */
39 PRISM_EXPORTED_FUNCTION void
40 pm_options_line_set(pm_options_t *options, int32_t line) {
41 options->line = line;
42 }
43
44 /**
45 * Set the frozen string literal option on the given options struct.
46 */
47 PRISM_EXPORTED_FUNCTION void
48 pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal) {
49 options->frozen_string_literal = frozen_string_literal ? PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED : PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED;
50 }
51
52 /**
53 * Sets the command line option on the given options struct.
54 */
55 PRISM_EXPORTED_FUNCTION void
56 pm_options_command_line_set(pm_options_t *options, uint8_t command_line) {
57 options->command_line = command_line;
58 }
59
60 /**
61 * Checks if the given slice represents a number.
62 */
63 static inline bool
64 is_number(const char *string, size_t length) {
65 return pm_strspn_decimal_digit((const uint8_t *) string, (ptrdiff_t) length) == length;
66 }
67
68 /**
69 * Set the version option on the given options struct by parsing the given
70 * string. If the string contains an invalid option, this returns false.
71 * Otherwise, it returns true.
72 */
73 PRISM_EXPORTED_FUNCTION bool
74 pm_options_version_set(pm_options_t *options, const char *version, size_t length) {
75 if (version == NULL) {
76 options->version = PM_OPTIONS_VERSION_LATEST;
77 return true;
78 }
79
80 if (length == 3) {
81 if (strncmp(version, "3.3", 3) == 0) {
82 options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
83 return true;
84 }
85
86 if (strncmp(version, "3.4", 3) == 0) {
87 options->version = PM_OPTIONS_VERSION_CRUBY_3_4;
88 return true;
89 }
90
91 if (strncmp(version, "3.5", 3) == 0 || strncmp(version, "4.0", 3) == 0) {
92 options->version = PM_OPTIONS_VERSION_CRUBY_4_0;
93 return true;
94 }
95
96 if (strncmp(version, "4.1", 3) == 0) {
97 options->version = PM_OPTIONS_VERSION_CRUBY_4_1;
98 return true;
99 }
100
101 return false;
102 }
103
104 if (length >= 4 && is_number(version + 4, length - 4)) {
105 if (strncmp(version, "3.3.", 4) == 0) {
106 options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
107 return true;
108 }
109
110 if (strncmp(version, "3.4.", 4) == 0) {
111 options->version = PM_OPTIONS_VERSION_CRUBY_3_4;
112 return true;
113 }
114
115 if (strncmp(version, "3.5.", 4) == 0 || strncmp(version, "4.0.", 4) == 0) {
116 options->version = PM_OPTIONS_VERSION_CRUBY_4_0;
117 return true;
118 }
119
120 if (strncmp(version, "4.1.", 4) == 0) {
121 options->version = PM_OPTIONS_VERSION_CRUBY_4_1;
122 return true;
123 }
124 }
125
126 if (length >= 6) {
127 if (strncmp(version, "latest", 7) == 0) { // 7 to compare the \0 as well
128 options->version = PM_OPTIONS_VERSION_LATEST;
129 return true;
130 }
131 }
132
133 return false;
134 }
135
136 /**
137 * Set the main script option on the given options struct.
138 */
139 PRISM_EXPORTED_FUNCTION void
140 pm_options_main_script_set(pm_options_t *options, bool main_script) {
141 options->main_script = main_script;
142 }
143
144 /**
145 * Set the partial script option on the given options struct.
146 */
147 PRISM_EXPORTED_FUNCTION void
148 pm_options_partial_script_set(pm_options_t *options, bool partial_script) {
149 options->partial_script = partial_script;
150 }
151
152 /**
153 * Set the freeze option on the given options struct.
154 */
155 PRISM_EXPORTED_FUNCTION void
156 pm_options_freeze_set(pm_options_t *options, bool freeze) {
157 options->freeze = freeze;
158 }
159
160 // For some reason, GCC analyzer thinks we're leaking allocated scopes and
161 // locals here, even though we definitely aren't. This is a false positive.
162 // Ideally we wouldn't need to suppress this.
163 #if defined(__GNUC__) && (__GNUC__ >= 10)
164 #pragma GCC diagnostic push
165 #pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
166 #endif
167
168 /**
169 * Allocate and zero out the scopes array on the given options struct.
170 */
171 PRISM_EXPORTED_FUNCTION bool
172 pm_options_scopes_init(pm_options_t *options, size_t scopes_count) {
173 options->scopes_count = scopes_count;
174 options->scopes = xcalloc(scopes_count, sizeof(pm_options_scope_t));
175 return options->scopes != NULL;
176 }
177
178 /**
179 * Return a pointer to the scope at the given index within the given options.
180 */
181 PRISM_EXPORTED_FUNCTION const pm_options_scope_t *
182 pm_options_scope_get(const pm_options_t *options, size_t index) {
183 return &options->scopes[index];
184 }
185
186 /**
187 * Create a new options scope struct. This will hold a set of locals that are in
188 * scope surrounding the code that is being parsed.
189 */
190 PRISM_EXPORTED_FUNCTION bool
191 pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) {
192 scope->locals_count = locals_count;
193 scope->locals = xcalloc(locals_count, sizeof(pm_string_t));
194 scope->forwarding = PM_OPTIONS_SCOPE_FORWARDING_NONE;
195 return scope->locals != NULL;
196 }
197
198 /**
199 * Return a pointer to the local at the given index within the given scope.
200 */
201 PRISM_EXPORTED_FUNCTION const pm_string_t *
202 pm_options_scope_local_get(const pm_options_scope_t *scope, size_t index) {
203 return &scope->locals[index];
204 }
205
206 /**
207 * Set the forwarding option on the given scope struct.
208 */
209 PRISM_EXPORTED_FUNCTION void
210 pm_options_scope_forwarding_set(pm_options_scope_t *scope, uint8_t forwarding) {
211 scope->forwarding = forwarding;
212 }
213
214 /**
215 * Free the internal memory associated with the options.
216 */
217 PRISM_EXPORTED_FUNCTION void
218 pm_options_free(pm_options_t *options) {
219 pm_string_free(&options->filepath);
220 pm_string_free(&options->encoding);
221
222 for (size_t scope_index = 0; scope_index < options->scopes_count; scope_index++) {
223 pm_options_scope_t *scope = &options->scopes[scope_index];
224
225 for (size_t local_index = 0; local_index < scope->locals_count; local_index++) {
226 pm_string_free(&scope->locals[local_index]);
227 }
228
229 xfree(scope->locals);
230 }
231
232 xfree(options->scopes);
233 }
234
235 /**
236 * Read a 32-bit unsigned integer from a pointer. This function is used to read
237 * the options that are passed into the parser from the Ruby implementation. It
238 * handles aligned and unaligned reads.
239 */
240 static uint32_t
241 pm_options_read_u32(const char *data) {
242 if (((uintptr_t) data) % sizeof(uint32_t) == 0) {
243 return *((uint32_t *) data);
244 } else {
245 uint32_t value;
246 memcpy(&value, data, sizeof(uint32_t));
247 return value;
248 }
249 }
250
251 /**
252 * Read a 32-bit signed integer from a pointer. This function is used to read
253 * the options that are passed into the parser from the Ruby implementation. It
254 * handles aligned and unaligned reads.
255 */
256 static int32_t
257 pm_options_read_s32(const char *data) {
258 if (((uintptr_t) data) % sizeof(int32_t) == 0) {
259 return *((int32_t *) data);
260 } else {
261 int32_t value;
262 memcpy(&value, data, sizeof(int32_t));
263 return value;
264 }
265 }
266
267 /**
268 * Deserialize an options struct from the given binary string. This is used to
269 * pass options to the parser from an FFI call so that consumers of the library
270 * from an FFI perspective don't have to worry about the structure of our
271 * options structs. Since the source of these calls will be from Ruby
272 * implementation internals we assume it is from a trusted source.
273 */
274 void
275 pm_options_read(pm_options_t *options, const char *data) {
276 options->line = 1; // default
277 if (data == NULL) return;
278
279 uint32_t filepath_length = pm_options_read_u32(data);
280 data += 4;
281
282 if (filepath_length > 0) {
283 pm_string_constant_init(&options->filepath, data, filepath_length);
284 data += filepath_length;
285 }
286
287 options->line = pm_options_read_s32(data);
288 data += 4;
289
290 uint32_t encoding_length = pm_options_read_u32(data);
291 data += 4;
292
293 if (encoding_length > 0) {
294 pm_string_constant_init(&options->encoding, data, encoding_length);
295 data += encoding_length;
296 }
297
298 options->frozen_string_literal = (int8_t) *data++;
299 options->command_line = (uint8_t) *data++;
300 options->version = (pm_options_version_t) *data++;
301 options->encoding_locked = ((uint8_t) *data++) > 0;
302 options->main_script = ((uint8_t) *data++) > 0;
303 options->partial_script = ((uint8_t) *data++) > 0;
304 options->freeze = ((uint8_t) *data++) > 0;
305
306 uint32_t scopes_count = pm_options_read_u32(data);
307 data += 4;
308
309 if (scopes_count > 0) {
310 if (!pm_options_scopes_init(options, scopes_count)) return;
311
312 for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
313 uint32_t locals_count = pm_options_read_u32(data);
314 data += 4;
315
316 pm_options_scope_t *scope = &options->scopes[scope_index];
317 if (!pm_options_scope_init(scope, locals_count)) {
318 pm_options_free(options);
319 return;
320 }
321
322 uint8_t forwarding = (uint8_t) *data++;
323 pm_options_scope_forwarding_set(&options->scopes[scope_index], forwarding);
324
325 for (size_t local_index = 0; local_index < locals_count; local_index++) {
326 uint32_t local_length = pm_options_read_u32(data);
327 data += 4;
328
329 pm_string_constant_init(&scope->locals[local_index], data, local_length);
330 data += local_length;
331 }
332 }
333 }
334 }
335
336 #if defined(__GNUC__) && (__GNUC__ >= 10)
337 #pragma GCC diagnostic pop
338 #endif