Skip to content

Commit 4ee0431

Browse files
committed
Extract rbs_constant_pool_t interface
1 parent e1636a7 commit 4ee0431

File tree

7 files changed

+283
-97
lines changed

7 files changed

+283
-97
lines changed

ext/rbs_extension/extconf.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
$INCFLAGS << " -I$(srcdir)/../../include"
55

66
$VPATH << "$(srcdir)/../../src"
7+
$VPATH << "$(srcdir)/../../src/util"
78
$VPATH << "$(srcdir)/ext/rbs_extension"
89

910
root_dir = File.expand_path('../../../', __FILE__)
10-
$srcs = Dir.glob("#{root_dir}/src/*.c") +
11+
$srcs = Dir.glob("#{root_dir}/src/**/*.c") +
1112
Dir.glob("#{root_dir}/ext/rbs_extension/*.c")
1213

1314
append_cflags ['-std=gnu99']

ext/rbs_extension/location.c

+28-10
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ static void check_children_cap(rbs_loc *loc) {
5656
}
5757
}
5858

59-
void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r) {
59+
void rbs_loc_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r) {
6060
rbs_loc_add_optional_child(loc, name, r);
6161

6262
unsigned short last_index = loc->children->len - 1;
6363
loc->children->required_p |= 1 << last_index;
6464
}
6565

66-
void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r) {
66+
void rbs_loc_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r) {
6767
check_children_cap(loc);
6868

6969
unsigned short i = loc->children->len++;
@@ -168,14 +168,31 @@ static VALUE location_end_pos(VALUE self) {
168168
return INT2FIX(loc->rg.end);
169169
}
170170

171+
static rbs_constant_id_t rbs_constant_id_from_ruby_symbol(VALUE symbol) {
172+
const char *name = rb_id2name(SYM2ID(symbol));
173+
174+
if (name == NULL) {
175+
// This happens if `symbol` was a Dynamic symbol (as opposed to a "static symbol" which has
176+
// been interned to get a an ID).
177+
return RBS_CONSTANT_ID_UNSET;
178+
}
179+
180+
return rbs_constant_pool_find(RBS_GLOBAL_CONSTANT_POOL, (const uint8_t *) name, strlen(name));
181+
}
182+
183+
static VALUE rbs_constant_to_ruby_symbol(rbs_constant_t *constant) {
184+
// Casts back the symbol that was looked up by `pm_constant_pool_id_to_constant()`.
185+
return (VALUE) constant;
186+
}
187+
171188
static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) {
172189
rbs_loc *loc = rbs_check_location(self);
173190

174191
range rg;
175192
rg.start = rbs_loc_position(FIX2INT(start));
176193
rg.end = rbs_loc_position(FIX2INT(end));
177194

178-
rbs_loc_add_required_child(loc, SYM2ID(name), rg);
195+
rbs_loc_add_required_child(loc, rbs_constant_id_from_ruby_symbol(name), rg);
179196

180197
return Qnil;
181198
}
@@ -187,15 +204,15 @@ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VA
187204
rg.start = rbs_loc_position(FIX2INT(start));
188205
rg.end = rbs_loc_position(FIX2INT(end));
189206

190-
rbs_loc_add_optional_child(loc, SYM2ID(name), rg);
207+
rbs_loc_add_optional_child(loc, rbs_constant_id_from_ruby_symbol(name), rg);
191208

192209
return Qnil;
193210
}
194211

195212
static VALUE location_add_optional_no_child(VALUE self, VALUE name) {
196213
rbs_loc *loc = rbs_check_location(self);
197214

198-
rbs_loc_add_optional_child(loc, SYM2ID(name), NULL_RANGE);
215+
rbs_loc_add_optional_child(loc, rbs_constant_id_from_ruby_symbol(name), NULL_RANGE);
199216

200217
return Qnil;
201218
}
@@ -221,9 +238,9 @@ static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
221238
static VALUE location_aref(VALUE self, VALUE name) {
222239
rbs_loc *loc = rbs_check_location(self);
223240

224-
ID id = SYM2ID(name);
241+
rbs_constant_id_t id = rbs_constant_id_from_ruby_symbol(name);
225242

226-
if (loc->children != NULL) {
243+
if (loc->children != NULL && id != RBS_CONSTANT_ID_UNSET) {
227244
for (unsigned short i = 0; i < loc->children->len; i++) {
228245
if (loc->children->entries[i].name == id) {
229246
rbs_loc_range result = loc->children->entries[i].rg;
@@ -252,8 +269,8 @@ static VALUE location_optional_keys(VALUE self) {
252269

253270
for (unsigned short i = 0; i < children->len; i++) {
254271
if (RBS_LOC_OPTIONAL_P(loc, i)) {
255-
rb_ary_push(keys, ID2SYM(children->entries[i].name));
256-
272+
rbs_constant_t *key = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name);
273+
rb_ary_push(keys, rbs_constant_to_ruby_symbol(key));
257274
}
258275
}
259276

@@ -271,7 +288,8 @@ static VALUE location_required_keys(VALUE self) {
271288

272289
for (unsigned short i = 0; i < children->len; i++) {
273290
if (RBS_LOC_REQUIRED_P(loc, i)) {
274-
rb_ary_push(keys, ID2SYM(children->entries[i].name));
291+
rbs_constant_t *key = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name);
292+
rb_ary_push(keys, rbs_constant_to_ruby_symbol(key));
275293
}
276294
}
277295

ext/rbs_extension/location.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "ruby.h"
55
#include "lexer.h"
6+
#include "rbs/util/rbs_constant_pool.h"
67

78
/**
89
* RBS::Location class
@@ -15,7 +16,7 @@ typedef struct {
1516
} rbs_loc_range;
1617

1718
typedef struct {
18-
ID name;
19+
rbs_constant_id_t name;
1920
rbs_loc_range rg;
2021
} rbs_loc_entry;
2122

@@ -58,14 +59,14 @@ void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap);
5859
*
5960
* Allocate memory for children with rbs_loc_alloc_children before calling this function.
6061
* */
61-
void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r);
62+
void rbs_loc_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r);
6263

6364
/**
6465
* Add an optional child range with given name.
6566
*
6667
* Allocate memory for children with rbs_loc_alloc_children before calling this function.
6768
* */
68-
void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r);
69+
void rbs_loc_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r);
6970

7071
/**
7172
* Returns RBS::Location object with start/end positions.

ext/rbs_extension/main.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "rbs_extension.h"
2+
#include "rbs/util/rbs_constant_pool.h"
23

34
void
45
Init_rbs_extension(void)
56
{
67
#ifdef HAVE_RB_EXT_RACTOR_SAFE
78
rb_ext_ractor_safe(true);
8-
#endif
9+
#endif
910
rbs__init_constants();
1011
rbs__init_location();
1112
rbs__init_parser();
13+
rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, 0);
1214
}

0 commit comments

Comments
 (0)