Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 64 additions & 47 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,26 @@ options_version_set_nearest(pm_options_t *options) {
}
}

struct build_options_data {
pm_options_t *options;
VALUE keywords;
VALUE filepath;
};

/**
* An iterator function that is called for each key-value in the keywords hash.
*/
static int
build_options_i(VALUE key, VALUE value, VALUE argument) {
pm_options_t *options = (pm_options_t *) argument;
struct build_options_data *data = (struct build_options_data *) argument;
pm_options_t *options = data->options;
ID key_id = SYM2ID(key);

if (key_id == rb_id_option_filepath) {
if (!NIL_P(value)) {
const char *filepath = check_string(value);
pm_options_filepath_set(options, filepath, RSTRING_LEN(value));
check_string(value);
/* A positional filepath takes precedence over the keyword. */
if (NIL_P(data->filepath)) data->filepath = value;
}
} else if (key_id == rb_id_option_encoding) {
if (!NIL_P(value)) {
Expand Down Expand Up @@ -342,62 +350,50 @@ build_options_i(VALUE key, VALUE value, VALUE argument) {
}

/**
* We need a struct here to pass through rb_protect and it has to be a single
* value. Because the sizeof(VALUE) == sizeof(void *), we're going to pass this
* through as an opaque pointer and cast it on both sides.
*/
struct build_options_data {
pm_options_t *options;
VALUE keywords;
};

/**
* Build the set of options from the given keywords. Note that this can raise a
* Ruby error if the options are not valid.
* Build the options and return the Ruby string that owns the filepath bytes.
*/
static VALUE
build_options(VALUE argument) {
struct build_options_data *data = (struct build_options_data *) argument;
rb_hash_foreach(data->keywords, build_options_i, (VALUE) data->options);
return Qnil;
if (!NIL_P(data->keywords)) rb_hash_foreach(data->keywords, build_options_i, argument);
if (NIL_P(data->filepath)) return Qnil;

/*
* Option conversions can change the string. Take its bytes once they have
* finished, and keep them stable across any callbacks during parsing.
*/
VALUE filepath = rb_str_new_frozen(data->filepath);
pm_options_filepath_set(data->options, RSTRING_PTR(filepath), RSTRING_LEN(filepath));
return filepath;
}

/**
* Extract the options from the given keyword arguments.
*/
static void
static VALUE
extract_options(pm_options_t *options, VALUE filepath, VALUE keywords) {
pm_options_line_set(options, 1); /* default */
options_version_set_nearest(options); /* default */

if (!NIL_P(keywords)) {
struct build_options_data data = { .options = options, .keywords = keywords };
struct build_options_data *argument = &data;
if (NIL_P(keywords) && NIL_P(filepath)) return Qnil;

int state = 0;
rb_protect(build_options, (VALUE) argument, &state);
struct build_options_data data = { .options = options, .keywords = keywords, .filepath = filepath };
int state = 0;

if (state != 0) {
pm_options_free(options);
rb_jump_tag(state);
}
VALUE snapshot = rb_protect(build_options, (VALUE) &data, &state);
if (state != 0) {
pm_options_free(options);
rb_jump_tag(state);
}

if (!NIL_P(filepath)) {
if (!RB_TYPE_P(filepath, T_STRING)) {
pm_options_free(options);
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected String)", rb_obj_class(filepath));
}

pm_options_filepath_set(options, RSTRING_PTR(filepath), RSTRING_LEN(filepath));
}
return snapshot;
}

/**
* Read options for methods that look like (source, **options).
*/
static VALUE
string_options(int argc, VALUE *argv, pm_options_t *options) {
string_options(int argc, VALUE *argv, pm_options_t *options, VALUE *filepath) {
VALUE string;
VALUE keywords;
rb_scan_args(argc, argv, "1:", &string, &keywords);
Expand All @@ -407,7 +403,7 @@ string_options(int argc, VALUE *argv, pm_options_t *options) {
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected String)", rb_obj_class(string));
}

extract_options(options, Qnil, keywords);
*filepath = extract_options(options, Qnil, keywords);
return string;
}

Expand All @@ -427,8 +423,7 @@ file_options(int argc, VALUE *argv, pm_options_t *options, VALUE *encoded_filepa
rb_jump_tag(state);
}

*encoded_filepath = rb_str_encode_ospath(filepath);
extract_options(options, *encoded_filepath, keywords);
*encoded_filepath = extract_options(options, rb_str_encode_ospath(filepath), keywords);

const char *source = (const char *) pm_string_source(pm_options_filepath(options));
pm_source_init_result_t result;
Expand Down Expand Up @@ -565,7 +560,8 @@ dump_input(const uint8_t *input, size_t input_length, const pm_options_t *option
static VALUE
dump(int argc, VALUE *argv, VALUE self) {
pm_options_t *options = pm_options_new();
VALUE string = string_options(argc, argv, options);
VALUE filepath;
VALUE string = string_options(argc, argv, options, &filepath);

const uint8_t *source = (const uint8_t *) RSTRING_PTR(string);
size_t length = RSTRING_LEN(string);
Expand All @@ -587,6 +583,7 @@ dump(int argc, VALUE *argv, VALUE self) {
#endif

pm_options_free(options);
RB_GC_GUARD(filepath);
return result_get(result);
}

Expand All @@ -608,6 +605,7 @@ dump_file(int argc, VALUE *argv, VALUE self) {
result_t result = dump_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
pm_source_free(src);
pm_options_free(options);
RB_GC_GUARD(encoded_filepath);

return result_get(result);
}
Expand Down Expand Up @@ -1039,10 +1037,12 @@ parse_lex_input(const uint8_t *input, size_t input_length, const pm_options_t *o
static VALUE
lex(int argc, VALUE *argv, VALUE self) {
pm_options_t *options = pm_options_new();
VALUE string = string_options(argc, argv, options);
VALUE filepath;
VALUE string = string_options(argc, argv, options, &filepath);

result_t result = parse_lex_input((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL, false);
pm_options_free(options);
RB_GC_GUARD(filepath);

return result_get(result);
}
Expand All @@ -1065,6 +1065,7 @@ lex_file(int argc, VALUE *argv, VALUE self) {
result_t result = parse_lex_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath), false);
pm_source_free(src);
pm_options_free(options);
RB_GC_GUARD(encoded_filepath);

return result_get(result);
}
Expand Down Expand Up @@ -1173,7 +1174,8 @@ parse_input(const uint8_t *input, size_t input_length, const pm_options_t *optio
static VALUE
parse(int argc, VALUE *argv, VALUE self) {
pm_options_t *options = pm_options_new();
VALUE string = string_options(argc, argv, options);
VALUE filepath;
VALUE string = string_options(argc, argv, options, &filepath);

const uint8_t *source = (const uint8_t *) RSTRING_PTR(string);
size_t length = RSTRING_LEN(string);
Expand All @@ -1195,6 +1197,7 @@ parse(int argc, VALUE *argv, VALUE self) {
#endif

pm_options_free(options);
RB_GC_GUARD(filepath);
return result_get(result);
}

Expand All @@ -1216,6 +1219,7 @@ parse_file(int argc, VALUE *argv, VALUE self) {
result_t result = parse_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
pm_source_free(src);
pm_options_free(options);
RB_GC_GUARD(encoded_filepath);

return result_get(result);
}
Expand Down Expand Up @@ -1249,10 +1253,12 @@ profile_input(const uint8_t *input, size_t input_length, const pm_options_t *opt
static VALUE
profile(int argc, VALUE *argv, VALUE self) {
pm_options_t *options = pm_options_new();
VALUE string = string_options(argc, argv, options);
VALUE filepath;
VALUE string = string_options(argc, argv, options, &filepath);

result_t result = profile_input((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL);
pm_options_free(options);
RB_GC_GUARD(filepath);
result_get(result);

return Qnil;
Expand All @@ -1277,6 +1283,7 @@ profile_file(int argc, VALUE *argv, VALUE self) {
result_t result = profile_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
pm_source_free(src);
pm_options_free(options);
RB_GC_GUARD(encoded_filepath);
result_get(result);

return Qnil;
Expand Down Expand Up @@ -1364,7 +1371,7 @@ parse_stream(int argc, VALUE *argv, VALUE self) {
rb_scan_args(argc, argv, "1:", &stream, &keywords);

pm_options_t *options = pm_options_new();
extract_options(options, Qnil, keywords);
VALUE filepath = extract_options(options, Qnil, keywords);

pm_source_t *src = pm_source_stream_new((void *) stream, parse_stream_fgets, parse_stream_eof);
pm_arena_t *arena = pm_arena_new();
Expand Down Expand Up @@ -1393,6 +1400,7 @@ parse_stream(int argc, VALUE *argv, VALUE self) {
pm_parser_free(parser);
pm_arena_free(arena);
pm_options_free(options);
RB_GC_GUARD(filepath);

return result_get(result);
}
Expand Down Expand Up @@ -1439,10 +1447,12 @@ parse_input_comments(const uint8_t *input, size_t input_length, const pm_options
static VALUE
parse_comments(int argc, VALUE *argv, VALUE self) {
pm_options_t *options = pm_options_new();
VALUE string = string_options(argc, argv, options);
VALUE filepath;
VALUE string = string_options(argc, argv, options, &filepath);

result_t result = parse_input_comments((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL);
pm_options_free(options);
RB_GC_GUARD(filepath);

return result_get(result);
}
Expand All @@ -1465,6 +1475,7 @@ parse_file_comments(int argc, VALUE *argv, VALUE self) {
result_t result = parse_input_comments(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
pm_source_free(src);
pm_options_free(options);
RB_GC_GUARD(encoded_filepath);

return result_get(result);
}
Expand All @@ -1487,10 +1498,12 @@ parse_file_comments(int argc, VALUE *argv, VALUE self) {
static VALUE
parse_lex(int argc, VALUE *argv, VALUE self) {
pm_options_t *options = pm_options_new();
VALUE string = string_options(argc, argv, options);
VALUE filepath;
VALUE string = string_options(argc, argv, options, &filepath);

result_t result = parse_lex_input((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL, true);
pm_options_free(options);
RB_GC_GUARD(filepath);

return result_get(result);
}
Expand Down Expand Up @@ -1520,6 +1533,7 @@ parse_lex_file(int argc, VALUE *argv, VALUE self) {
result_t result = parse_lex_input(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath), true);
pm_source_free(src);
pm_options_free(options);
RB_GC_GUARD(encoded_filepath);

return result_get(result);
}
Expand Down Expand Up @@ -1555,10 +1569,12 @@ parse_input_success_p(const uint8_t *input, size_t input_length, const pm_option
static VALUE
parse_success_p(int argc, VALUE *argv, VALUE self) {
pm_options_t *options = pm_options_new();
VALUE string = string_options(argc, argv, options);
VALUE filepath;
VALUE string = string_options(argc, argv, options, &filepath);

result_t result = parse_input_success_p((const uint8_t *) RSTRING_PTR(string), RSTRING_LEN(string), options, NULL);
pm_options_free(options);
RB_GC_GUARD(filepath);

return result_get(result);
}
Expand Down Expand Up @@ -1594,6 +1610,7 @@ parse_file_success_p(int argc, VALUE *argv, VALUE self) {
result_t result = parse_input_success_p(pm_source_source(src), pm_source_length(src), options, rb_enc_get(encoded_filepath));
pm_source_free(src);
pm_options_free(options);
RB_GC_GUARD(encoded_filepath);

return result_get(result);
}
Expand Down
Loading
Loading