1#include "prism/internal/options.h"
5#include "prism/internal/allocator.h"
6#include "prism/internal/char.h"
7#include "prism/internal/stringy.h"
19 if (options == NULL) abort();
28 pm_string_cleanup(&options->filepath);
29 pm_string_cleanup(&options->encoding);
31 for (
size_t scope_index = 0; scope_index < options->scopes_count; scope_index++) {
34 for (
size_t local_index = 0; local_index < scope->locals_count; local_index++) {
35 pm_string_cleanup(&scope->locals[local_index]);
38 xfree_sized(scope->locals, scope->locals_count *
sizeof(
pm_string_t));
51 pm_options_cleanup(options);
60 options->shebang_callback = shebang_callback;
61 options->shebang_callback_data = shebang_callback_data;
69 return &options->filepath;
76pm_options_filepath_set(
pm_options_t *options,
const char *filepath) {
77 pm_string_constant_init(&options->filepath, filepath, strlen(filepath));
84pm_options_encoding_set(
pm_options_t *options,
const char *encoding) {
85 pm_string_constant_init(&options->encoding, encoding, strlen(encoding));
92pm_options_encoding_locked_set(
pm_options_t *options,
bool encoding_locked) {
93 options->encoding_locked = encoding_locked;
101 options->line = line;
108pm_options_frozen_string_literal_set(
pm_options_t *options,
bool frozen_string_literal) {
116pm_options_command_line_set(
pm_options_t *options, uint8_t command_line) {
117 options->command_line = command_line;
124is_number(
const char *
string,
size_t length) {
125 return pm_strspn_decimal_digit((
const uint8_t *)
string, (ptrdiff_t) length) == length;
134pm_options_version_set(
pm_options_t *options,
const char *version,
size_t length) {
135 if (version == NULL) {
136 options->version = PM_OPTIONS_VERSION_LATEST;
141 if (strncmp(version,
"3.3", 3) == 0) {
142 options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
146 if (strncmp(version,
"3.4", 3) == 0) {
147 options->version = PM_OPTIONS_VERSION_CRUBY_3_4;
151 if (strncmp(version,
"3.5", 3) == 0 || strncmp(version,
"4.0", 3) == 0) {
152 options->version = PM_OPTIONS_VERSION_CRUBY_4_0;
156 if (strncmp(version,
"4.1", 3) == 0) {
157 options->version = PM_OPTIONS_VERSION_CRUBY_4_1;
164 if (length >= 4 && is_number(version + 4, length - 4)) {
165 if (strncmp(version,
"3.3.", 4) == 0) {
166 options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
170 if (strncmp(version,
"3.4.", 4) == 0) {
171 options->version = PM_OPTIONS_VERSION_CRUBY_3_4;
175 if (strncmp(version,
"3.5.", 4) == 0 || strncmp(version,
"4.0.", 4) == 0) {
176 options->version = PM_OPTIONS_VERSION_CRUBY_4_0;
180 if (strncmp(version,
"4.1.", 4) == 0) {
181 options->version = PM_OPTIONS_VERSION_CRUBY_4_1;
187 if (strncmp(version,
"latest", 6) == 0) {
188 options->version = PM_OPTIONS_VERSION_LATEST;
202 options->version = PM_OPTIONS_VERSION_CRUBY_3_3;
211 options->version = PM_OPTIONS_VERSION_LATEST;
219 options->main_script = main_script;
226pm_options_partial_script_set(
pm_options_t *options,
bool partial_script) {
227 options->partial_script = partial_script;
235 return options->freeze;
243 options->freeze = freeze;
251 return options->attach_comments;
258pm_options_attach_comments_set(
pm_options_t *options,
bool attach_comments) {
259 options->attach_comments = attach_comments;
266 return options->raise_error;
272void pm_options_raise_error_set(
pm_options_t *options, uint8_t raise_error) {
273 options->raise_error = raise_error;
279#if defined(__GNUC__) && (__GNUC__ >= 10)
280#pragma GCC diagnostic push
281#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
289 options->scopes_count = scopes_count;
291 return options->scopes != NULL;
300 return &options->scopes[index];
309 return &options->scopes[index];
318 scope->locals_count = locals_count;
321 if (scope->locals == NULL) abort();
330 return &scope->locals[index];
339 return &scope->locals[index];
347 scope->forwarding = forwarding;
356pm_options_read_u32(
const char *data) {
357 if (((uintptr_t) data) %
sizeof(uint32_t) == 0) {
358 return *((uint32_t *) data);
361 memcpy(&value, data,
sizeof(uint32_t));
372pm_options_read_s32(
const char *data) {
373 if (((uintptr_t) data) %
sizeof(int32_t) == 0) {
374 return *((int32_t *) data);
377 memcpy(&value, data,
sizeof(int32_t));
390pm_options_read(
pm_options_t *options,
const char *data) {
392 if (data == NULL)
return;
394 uint32_t filepath_length = pm_options_read_u32(data);
397 if (filepath_length > 0) {
398 pm_string_constant_init(&options->filepath, data, filepath_length);
399 data += filepath_length;
402 options->line = pm_options_read_s32(data);
405 uint32_t encoding_length = pm_options_read_u32(data);
408 if (encoding_length > 0) {
409 pm_string_constant_init(&options->encoding, data, encoding_length);
410 data += encoding_length;
413 options->frozen_string_literal = (int8_t) *data++;
414 options->command_line = (uint8_t) *data++;
415 options->version = (pm_options_version_t) *data++;
416 options->encoding_locked = ((uint8_t) *data++) > 0;
417 options->main_script = ((uint8_t) *data++) > 0;
418 options->partial_script = ((uint8_t) *data++) > 0;
419 options->freeze = ((uint8_t) *data++) > 0;
420 options->attach_comments = ((uint8_t) *data++) > 0;
422 uint32_t scopes_count = pm_options_read_u32(data);
425 if (scopes_count > 0) {
426 if (!pm_options_scopes_init(options, scopes_count))
return;
428 for (
size_t scope_index = 0; scope_index < scopes_count; scope_index++) {
429 uint32_t locals_count = pm_options_read_u32(data);
433 pm_options_scope_init(scope, locals_count);
435 uint8_t forwarding = (uint8_t) *data++;
436 pm_options_scope_forwarding_set(&options->scopes[scope_index], forwarding);
438 for (
size_t local_index = 0; local_index < locals_count; local_index++) {
439 uint32_t local_length = pm_options_read_u32(data);
442 pm_string_constant_init(&scope->locals[local_index], data, local_length);
443 data += local_length;
449#if defined(__GNUC__) && (__GNUC__ >= 10)
450#pragma GCC diagnostic pop
#define xcalloc
Old name of ruby_xcalloc.
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_NONE
The default value for parameters.
#define PM_OPTIONS_FROZEN_STRING_LITERAL_DISABLED
String literals should not be frozen.
#define PM_OPTIONS_FROZEN_STRING_LITERAL_ENABLED
String literals should be made frozen.
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...
A generic string type that can have various ownership semantics.