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
13 changes: 10 additions & 3 deletions ext/rbs_extension/ast_translation.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "rbs_string_bridging.h"
#include "legacy_location.h"

VALUE EMPTY_ARRAY;
VALUE EMPTY_HASH;

#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))

rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, rb_encoding *ruby_encoding) {
Expand All @@ -32,6 +35,10 @@ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t
}

VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) {
if (!rbs_hash->head) {
return EMPTY_HASH;
}

VALUE ruby_hash = rb_hash_new();

for (rbs_hash_node_t *n = rbs_hash->head; n != NULL; n = n->next) {
Expand Down Expand Up @@ -60,12 +67,12 @@ VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *so
}

VALUE rbs_location_list_to_ruby_array(rbs_translation_context_t ctx, rbs_location_list_t *list) {
VALUE ruby_array = rb_ary_new();

if (list == NULL) {
return ruby_array;
return EMPTY_ARRAY;
}

VALUE ruby_array = rb_ary_new();

for (rbs_location_list_node_t *n = list->head; n != NULL; n = n->next) {
rb_ary_push(ruby_array, rbs_loc_to_ruby_location(ctx, n->loc));
}
Expand Down
3 changes: 3 additions & 0 deletions ext/rbs_extension/ast_translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *li
VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash);
VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance);

extern VALUE EMPTY_ARRAY;
extern VALUE EMPTY_HASH;

#endif
8 changes: 6 additions & 2 deletions ext/rbs_extension/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,12 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
void rbs__init_parser(void) {
RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
rb_gc_register_mark_object(RBS_Parser);
VALUE empty_array = rb_obj_freeze(rb_ary_new());
rb_gc_register_mark_object(empty_array);

EMPTY_ARRAY = rb_obj_freeze(rb_ary_new());
rb_gc_register_mark_object(EMPTY_ARRAY);

EMPTY_HASH = rb_obj_freeze(rb_hash_new());
rb_gc_register_mark_object(EMPTY_HASH);

rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 7);
rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
Expand Down
13 changes: 10 additions & 3 deletions templates/ext/rbs_extension/ast_translation.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "rbs_string_bridging.h"
#include "legacy_location.h"

VALUE EMPTY_ARRAY;
VALUE EMPTY_HASH;

#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))

rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, rb_encoding *ruby_encoding) {
Expand All @@ -25,6 +28,10 @@ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t
}

VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) {
if (!rbs_hash->head) {
return EMPTY_HASH;
}

VALUE ruby_hash = rb_hash_new();

for (rbs_hash_node_t *n = rbs_hash->head; n != NULL; n = n->next) {
Expand Down Expand Up @@ -53,12 +60,12 @@ VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *so
}

VALUE rbs_location_list_to_ruby_array(rbs_translation_context_t ctx, rbs_location_list_t *list) {
VALUE ruby_array = rb_ary_new();

if (list == NULL) {
return ruby_array;
return EMPTY_ARRAY;
}

VALUE ruby_array = rb_ary_new();

for (rbs_location_list_node_t *n = list->head; n != NULL; n = n->next) {
rb_ary_push(ruby_array, rbs_loc_to_ruby_location(ctx, n->loc));
}
Expand Down
3 changes: 3 additions & 0 deletions templates/ext/rbs_extension/ast_translation.h.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *li
VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash);
VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance);

extern VALUE EMPTY_ARRAY;
extern VALUE EMPTY_HASH;

#endif
Loading