Skip to content

Commit fbf7c14

Browse files
authored
Merge pull request #4045 from Earlopain/fix-freeze-offsets
Fix `Source.offsets` with `freeze: true`
2 parents 22dfec3 + 0667d23 commit fbf7c14

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

templates/ext/prism/api_node.c.erb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,18 @@ pm_source_new(const pm_parser_t *parser, rb_encoding *encoding, bool freeze) {
8282
VALUE source_string = rb_enc_str_new((const char *) start, pm_parser_end(parser) - start, encoding);
8383

8484
const pm_line_offset_list_t *line_offsets = pm_parser_line_offsets(parser);
85-
VALUE offsets = rb_str_new((const char *) line_offsets->offsets, line_offsets->size * sizeof(uint32_t));
85+
VALUE offsets;
8686

8787
if (freeze) {
88+
offsets = rb_ary_new_capa(line_offsets->size);
89+
for (size_t index = 0; index < line_offsets->size; index++) {
90+
rb_ary_push(offsets, ULONG2NUM(line_offsets->offsets[index]));
91+
}
92+
8893
rb_obj_freeze(source_string);
8994
rb_obj_freeze(offsets);
95+
} else {
96+
offsets = rb_str_new((const char *) line_offsets->offsets, line_offsets->size * sizeof(uint32_t));
9097
}
9198

9299
VALUE source = rb_funcall(rb_cPrismSource, rb_intern("for"), 3, source_string, LONG2NUM(pm_parser_start_line(parser)), offsets);

test/prism/api/freeze_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ def test_parse
88
assert_frozen(Prism.parse("1 + 2; %i{foo} + %i{bar}", freeze: true))
99
end
1010

11+
def test_offsets_usable
12+
node = Prism.parse_statement("1 + 2", freeze: true)
13+
assert_equal(1, node.start_line)
14+
end
15+
1116
def test_lex
1217
assert_frozen(Prism.lex("1 + 2; %i{foo} + %i{bar}", freeze: true))
1318
end

0 commit comments

Comments
 (0)