Ruby 4.1.0dev (2026-11-16 revision 67814cf9b0f3a4bad6f7369b0c8e6fc69220de64)
options.c
1#include "prism/internal/options.h"
2
4
5#include "prism/internal/allocator.h"
6#include "prism/internal/char.h"
7#include "prism/internal/oom.h"
8#include "prism/internal/stringy.h"
9
10#include <stdlib.h>
11#include <string.h>
12
19 pm_options_t *options = xcalloc(1, sizeof(pm_options_t));
20 if (options == NULL) PRISM_OOM();
21
22 options->line = 1;
23 options->stack_budget = SIZE_MAX;
24 return options;
25}
26
30void
31pm_options_cleanup(pm_options_t *options) {
32 for (size_t scope_index = 0; scope_index < options->scopes_count; scope_index++) {
33 pm_options_scope_t *scope = &options->scopes[scope_index];
34 xfree_sized(scope->locals, scope->locals_count * sizeof(pm_string_t));
35 }
36
37 xfree_sized(options->scopes, options->scopes_count * sizeof(pm_options_scope_t));
38}
39
45void
46pm_options_free(pm_options_t *options) {
47 pm_options_cleanup(options);
48 xfree_sized(options, sizeof(pm_options_t));
49}
50
54void
55pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data) {
56 options->shebang_callback = shebang_callback;
57 options->shebang_callback_data = shebang_callback_data;
58}
59
63const pm_string_t *
64pm_options_filepath(const pm_options_t *options) {
65 return &options->filepath;
66}
67
71void
72pm_options_filepath_set(pm_options_t *options, const char *filepath) {
73 pm_string_init(&options->filepath, (const uint8_t *) filepath, strlen(filepath));
74}
75
79void
80pm_options_encoding_set(pm_options_t *options, const char *encoding) {
81 pm_string_init(&options->encoding, (const uint8_t *) encoding, strlen(encoding));
82}
83
87void
88pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked) {
89 options->encoding_locked = encoding_locked;
90}
91
95void
96pm_options_line_set(pm_options_t *options, int32_t line) {
97 options->line = line;
98}
99
103void
104pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal) {
105 options->frozen_string_literal = frozen_string_literal ? PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED : PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED;
106}
107
111void
112pm_options_command_line_set(pm_options_t *options, uint8_t command_line) {
113 options->command_line = command_line;
114}
115
119static PRISM_INLINE bool
120is_number(const char *string, size_t length) {
121 return pm_strspn_decimal_digit((const uint8_t *) string, (ptrdiff_t) length) == length;
122}
123
129bool
130pm_options_version_set(pm_options_t *options, const char *version, size_t length) {
131 if (version == NULL) {
132 options->version = PM_OPTIONS_VERSION_LATEST;
133 return true;
134 }
135
136 if (length == 3) {
137 if (memcmp(version, "3.3", 3) == 0) {
138 options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
139 return true;
140 }
141
142 if (memcmp(version, "3.4", 3) == 0) {
143 options->version = PM_OPTIONS_VERSION_CRUBY_3_4;
144 return true;
145 }
146
147 if (memcmp(version, "3.5", 3) == 0 || memcmp(version, "4.0", 3) == 0) {
148 options->version = PM_OPTIONS_VERSION_CRUBY_4_0;
149 return true;
150 }
151
152 if (memcmp(version, "4.1", 3) == 0) {
153 options->version = PM_OPTIONS_VERSION_CRUBY_4_1;
154 return true;
155 }
156
157 return false;
158 }
159
160 if (length >= 4 && is_number(version + 4, length - 4)) {
161 if (memcmp(version, "3.3.", 4) == 0) {
162 options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
163 return true;
164 }
165
166 if (memcmp(version, "3.4.", 4) == 0) {
167 options->version = PM_OPTIONS_VERSION_CRUBY_3_4;
168 return true;
169 }
170
171 if (memcmp(version, "3.5.", 4) == 0 || memcmp(version, "4.0.", 4) == 0) {
172 options->version = PM_OPTIONS_VERSION_CRUBY_4_0;
173 return true;
174 }
175
176 if (memcmp(version, "4.1.", 4) == 0) {
177 options->version = PM_OPTIONS_VERSION_CRUBY_4_1;
178 return true;
179 }
180 }
181
182 if (length == 6) {
183 if (memcmp(version, "latest", 6) == 0) {
184 options->version = PM_OPTIONS_VERSION_LATEST;
185 return true;
186 }
187 }
188
189 return false;
190}
191
196void
197pm_options_version_set_lowest(pm_options_t *options) {
198 options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
199}
200
205void
206pm_options_version_set_highest(pm_options_t *options) {
207 options->version = PM_OPTIONS_VERSION_LATEST;
208}
209
213void
214pm_options_main_script_set(pm_options_t *options, bool main_script) {
215 options->main_script = main_script;
216}
217
221void
222pm_options_partial_script_set(pm_options_t *options, bool partial_script) {
223 options->partial_script = partial_script;
224}
225
229bool
230pm_options_freeze(const pm_options_t *options) {
231 return options->freeze;
232}
233
237void
238pm_options_freeze_set(pm_options_t *options, bool freeze) {
239 options->freeze = freeze;
240}
241
245bool
246pm_options_attach_comments(const pm_options_t *options) {
247 return options->attach_comments;
248}
249
253void
254pm_options_attach_comments_set(pm_options_t *options, bool attach_comments) {
255 options->attach_comments = attach_comments;
256}
257
261uint8_t pm_options_raise_error(const pm_options_t *options) {
262 return options->raise_error;
263}
264
268void pm_options_raise_error_set(pm_options_t *options, uint8_t raise_error) {
269 options->raise_error = raise_error;
270}
271
275void
276pm_options_stack_budget_set(pm_options_t *options, size_t stack_budget) {
277 options->stack_budget = stack_budget;
278}
279
280// For some reason, GCC analyzer thinks we're leaking allocated scopes and
281// locals here, even though we definitely aren't. This is a false positive.
282// Ideally we wouldn't need to suppress this.
283#if defined(__GNUC__) && (__GNUC__ >= 10)
284#pragma GCC diagnostic push
285#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
286#endif
287
291bool
292pm_options_scopes_init(pm_options_t *options, size_t scopes_count) {
293 options->scopes_count = scopes_count;
294 options->scopes = xcalloc(scopes_count, sizeof(pm_options_scope_t));
295 return options->scopes != NULL;
296}
297
302const pm_options_scope_t *
303pm_options_scope(const pm_options_t *options, size_t index) {
304 return &options->scopes[index];
305}
306
312pm_options_scope_mut(pm_options_t *options, size_t index) {
313 return &options->scopes[index];
314}
315
320void
321pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) {
322 scope->locals_count = locals_count;
323 scope->locals = xcalloc(locals_count, sizeof(pm_string_t));
324 scope->forwarding = PM_OPTIONS_SCOPE_FORWARDING_NONE;
325 if (scope->locals == NULL) PRISM_OOM();
326}
327
332const pm_string_t *
333pm_options_scope_local(const pm_options_scope_t *scope, size_t index) {
334 return &scope->locals[index];
335}
336
342pm_options_scope_local_mut(pm_options_scope_t *scope, size_t index) {
343 return &scope->locals[index];
344}
345
349void
350pm_options_scope_forwarding_set(pm_options_scope_t *scope, uint8_t forwarding) {
351 scope->forwarding = forwarding;
352}
353
359static uint32_t
360pm_options_read_u32(const char *data) {
361 if (((uintptr_t) data) % sizeof(uint32_t) == 0) {
362 return *((uint32_t *) data);
363 } else {
364 uint32_t value;
365 memcpy(&value, data, sizeof(uint32_t));
366 return value;
367 }
368}
369
375static int32_t
376pm_options_read_s32(const char *data) {
377 if (((uintptr_t) data) % sizeof(int32_t) == 0) {
378 return *((int32_t *) data);
379 } else {
380 int32_t value;
381 memcpy(&value, data, sizeof(int32_t));
382 return value;
383 }
384}
385
393void
394pm_options_read(pm_options_t *options, const char *data) {
395 options->line = 1; // default
396 options->stack_budget = SIZE_MAX; // default
397 if (data == NULL) return;
398
399 uint32_t filepath_length = pm_options_read_u32(data);
400 data += 4;
401
402 if (filepath_length > 0) {
403 pm_string_init(&options->filepath, (const uint8_t *) data, filepath_length);
404 data += filepath_length;
405 }
406
407 options->line = pm_options_read_s32(data);
408 data += 4;
409
410 uint32_t encoding_length = pm_options_read_u32(data);
411 data += 4;
412
413 if (encoding_length > 0) {
414 pm_string_init(&options->encoding, (const uint8_t *) data, encoding_length);
415 data += encoding_length;
416 }
417
418 options->frozen_string_literal = (int8_t) *data++;
419 options->command_line = (uint8_t) *data++;
420 options->version = (pm_options_version_t) *data++;
421 options->encoding_locked = ((uint8_t) *data++) > 0;
422 options->main_script = ((uint8_t) *data++) > 0;
423 options->partial_script = ((uint8_t) *data++) > 0;
424 options->freeze = ((uint8_t) *data++) > 0;
425 options->attach_comments = ((uint8_t) *data++) > 0;
426
427 uint32_t scopes_count = pm_options_read_u32(data);
428 data += 4;
429
430 if (scopes_count > 0) {
431 if (!pm_options_scopes_init(options, scopes_count)) return;
432
433 for (size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
434 uint32_t locals_count = pm_options_read_u32(data);
435 data += 4;
436
437 pm_options_scope_t *scope = &options->scopes[scope_index];
438 pm_options_scope_init(scope, locals_count);
439
440 uint8_t forwarding = (uint8_t) *data++;
441 pm_options_scope_forwarding_set(&options->scopes[scope_index], forwarding);
442
443 for (size_t local_index = 0; local_index < locals_count; local_index++) {
444 uint32_t local_length = pm_options_read_u32(data);
445 data += 4;
446
447 pm_string_init(&scope->locals[local_index], (const uint8_t *) data, local_length);
448 data += local_length;
449 }
450 }
451 }
452}
453
454#if defined(__GNUC__) && (__GNUC__ >= 10)
455#pragma GCC diagnostic pop
456#endif
#define xcalloc
Old name of ruby_xcalloc.
Definition xmalloc.h:55
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition inline.h:12
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_NONE
The default value for parameters.
Definition options.h:49
#define PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED
String literals should not be frozen.
Definition options.h:35
#define PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED
String literals should be made frozen.
Definition options.h:46
PRISM_NODISCARD PRISM_EXPORTED_FUNCTION pm_options_t * pm_options_new(void)
Allocate a new options struct.
Definition options.c:18
void(* pm_options_shebang_callback_t)(pm_options_t *options, const uint8_t *source, size_t length, void *shebang_callback_data)
The callback called when additional switches are found in a shebang comment that need to be processed...
Definition options.h:75
A view of a sequence of bytes.
Definition stringy.h:23