Skip to content

Commit d3632b3

Browse files
committed
Extract rbs_constant_pool_t interface
1 parent e1636a7 commit d3632b3

File tree

6 files changed

+278
-92
lines changed

6 files changed

+278
-92
lines changed

ext/rbs_extension/extconf.rb

Lines changed: 2 additions & 1 deletion
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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "rbs_extension.h"
2+
#include "rbs/util/rbs_constant_pool.h"
23

34
#define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i)))
45
#define RBS_LOC_OPTIONAL_P(loc, i) (!RBS_LOC_REQUIRED_P((loc), (i)))
@@ -168,14 +169,31 @@ static VALUE location_end_pos(VALUE self) {
168169
return INT2FIX(loc->rg.end);
169170
}
170171

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

174192
range rg;
175193
rg.start = rbs_loc_position(FIX2INT(start));
176194
rg.end = rbs_loc_position(FIX2INT(end));
177195

178-
rbs_loc_add_required_child(loc, SYM2ID(name), rg);
196+
rbs_loc_add_required_child(loc, rbs_constant_id_from_ruby_symbol(name), rg);
179197

180198
return Qnil;
181199
}
@@ -187,15 +205,15 @@ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VA
187205
rg.start = rbs_loc_position(FIX2INT(start));
188206
rg.end = rbs_loc_position(FIX2INT(end));
189207

190-
rbs_loc_add_optional_child(loc, SYM2ID(name), rg);
208+
rbs_loc_add_optional_child(loc, rbs_constant_id_from_ruby_symbol(name), rg);
191209

192210
return Qnil;
193211
}
194212

195213
static VALUE location_add_optional_no_child(VALUE self, VALUE name) {
196214
rbs_loc *loc = rbs_check_location(self);
197215

198-
rbs_loc_add_optional_child(loc, SYM2ID(name), NULL_RANGE);
216+
rbs_loc_add_optional_child(loc, rbs_constant_id_from_ruby_symbol(name), NULL_RANGE);
199217

200218
return Qnil;
201219
}
@@ -221,9 +239,9 @@ static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
221239
static VALUE location_aref(VALUE self, VALUE name) {
222240
rbs_loc *loc = rbs_check_location(self);
223241

224-
ID id = SYM2ID(name);
242+
rbs_constant_id_t id = rbs_constant_id_from_ruby_symbol(name);
225243

226-
if (loc->children != NULL) {
244+
if (loc->children != NULL && id != RBS_CONSTANT_ID_UNSET) {
227245
for (unsigned short i = 0; i < loc->children->len; i++) {
228246
if (loc->children->entries[i].name == id) {
229247
rbs_loc_range result = loc->children->entries[i].rg;
@@ -252,8 +270,8 @@ static VALUE location_optional_keys(VALUE self) {
252270

253271
for (unsigned short i = 0; i < children->len; i++) {
254272
if (RBS_LOC_OPTIONAL_P(loc, i)) {
255-
rb_ary_push(keys, ID2SYM(children->entries[i].name));
256-
273+
rbs_constant_t *key = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name);
274+
rb_ary_push(keys, rbs_constant_to_ruby_symbol(key));
257275
}
258276
}
259277

@@ -271,7 +289,8 @@ static VALUE location_required_keys(VALUE self) {
271289

272290
for (unsigned short i = 0; i < children->len; i++) {
273291
if (RBS_LOC_REQUIRED_P(loc, i)) {
274-
rb_ary_push(keys, ID2SYM(children->entries[i].name));
292+
rbs_constant_t *key = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name);
293+
rb_ary_push(keys, rbs_constant_to_ruby_symbol(key));
275294
}
276295
}
277296

ext/rbs_extension/main.c

Lines changed: 3 additions & 1 deletion
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)