Skip to content

Commit 68e94ad

Browse files
committed
[GR-24624] Update IDToSymbolNode to a more strict definition of isStaticSymbol.
PullRequest: truffleruby/1754
2 parents 5048be2 + 09b952c commit 68e94ad

File tree

8 files changed

+23
-19
lines changed

8 files changed

+23
-19
lines changed

src/main/c/cext/call.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ int rb_sourceline(void) {
132132
}
133133

134134
int rb_obj_method_arity(VALUE object, ID id) {
135-
return polyglot_as_i32(RUBY_CEXT_INVOKE_NO_WRAP("rb_obj_method_arity", object, id));
135+
return polyglot_as_i32(RUBY_CEXT_INVOKE_NO_WRAP("rb_obj_method_arity", object, ID2SYM(id)));
136136
}
137137

138138
int rb_obj_respond_to(VALUE object, ID id, int priv) {
139-
return polyglot_as_boolean(polyglot_invoke(RUBY_CEXT, "rb_obj_respond_to", rb_tr_unwrap(object), rb_tr_unwrap(id), priv));
139+
return polyglot_as_boolean(polyglot_invoke(RUBY_CEXT, "rb_obj_respond_to", rb_tr_unwrap(object), rb_tr_id2sym(id), priv));
140140
}
141141

142142
int rb_method_boundp(VALUE klass, ID id, int ex) {

src/main/c/cext/class.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass) {
6868
}
6969

7070
VALUE rb_cvar_defined(VALUE klass, ID id) {
71-
return RUBY_CEXT_INVOKE("rb_cvar_defined", klass, id);
71+
return RUBY_CEXT_INVOKE("rb_cvar_defined", klass, ID2SYM(id));
7272
}
7373

7474
VALUE rb_cvar_get(VALUE klass, ID id) {
75-
return RUBY_CEXT_INVOKE("rb_cvar_get", klass, id);
75+
return RUBY_CEXT_INVOKE("rb_cvar_get", klass, ID2SYM(id));
7676
}
7777

7878
void rb_cvar_set(VALUE klass, ID id, VALUE val) {
79-
RUBY_CEXT_INVOKE_NO_WRAP("rb_cvar_set", klass, id, val);
79+
RUBY_CEXT_INVOKE_NO_WRAP("rb_cvar_set", klass, ID2SYM(id), val);
8080
}
8181

8282
VALUE rb_cv_get(VALUE klass, const char *name) {

src/main/c/cext/define.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ VALUE rb_define_class_under(VALUE module, const char *name, VALUE superclass) {
1515
}
1616

1717
VALUE rb_define_class_id_under(VALUE module, ID name, VALUE superclass) {
18-
return RUBY_CEXT_INVOKE("rb_define_class_under", module, name, superclass);
18+
return RUBY_CEXT_INVOKE("rb_define_class_under", module, ID2SYM(name), superclass);
1919
}
2020

2121
VALUE rb_define_module(const char *name) {
@@ -66,19 +66,19 @@ void rb_define_alias(VALUE module, const char *new_name, const char *old_name) {
6666
}
6767

6868
void rb_alias(VALUE module, ID new_name, ID old_name) {
69-
RUBY_CEXT_INVOKE_NO_WRAP("rb_alias", module, new_name, old_name);
69+
RUBY_CEXT_INVOKE_NO_WRAP("rb_alias", module, ID2SYM(new_name), ID2SYM(old_name));
7070
}
7171

7272
void rb_undef_method(VALUE module, const char *name) {
7373
rb_undef(module, rb_str_new_cstr(name));
7474
}
7575

7676
void rb_undef(VALUE module, ID name) {
77-
RUBY_CEXT_INVOKE_NO_WRAP("rb_undef", module, name);
77+
RUBY_CEXT_INVOKE_NO_WRAP("rb_undef", module, ID2SYM(name));
7878
}
7979

8080
void rb_attr(VALUE ruby_class, ID name, int read, int write, int ex) {
81-
polyglot_invoke(RUBY_CEXT, "rb_attr", rb_tr_unwrap(ruby_class), rb_tr_unwrap(name), read, write, ex);
81+
polyglot_invoke(RUBY_CEXT, "rb_attr", rb_tr_unwrap(ruby_class), rb_tr_unwrap(ID2SYM(name)), read, write, ex);
8282
}
8383

8484
void rb_define_alloc_func(VALUE ruby_class, rb_alloc_func_t alloc_function) {

src/main/c/cext/ivar.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void rb_ivar_foreach(VALUE obj, int (*func)(ANYARGS), st_data_t arg) {
4040
}
4141

4242
VALUE rb_attr_get(VALUE object, ID name) {
43-
return RUBY_CEXT_INVOKE("rb_ivar_lookup", object, name, Qnil);
43+
return RUBY_CEXT_INVOKE("rb_ivar_lookup", object, ID2SYM(name), Qnil);
4444
}
4545

4646
void rb_copy_generic_ivar(VALUE clone, VALUE obj) {

src/main/c/cext/symbol.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ ID rb_to_id(VALUE name) {
99

1010
#undef rb_intern
1111
ID rb_intern(const char *string) {
12-
return (ID) RUBY_CEXT_INVOKE("rb_intern", rb_str_new_cstr(string));
12+
return SYM2ID(RUBY_CEXT_INVOKE("rb_intern", rb_str_new_cstr(string)));
1313
}
1414

1515
ID rb_intern2(const char *string, long length) {
16-
return (ID) SYM2ID(RUBY_CEXT_INVOKE("rb_intern", rb_str_new(string, length)));
16+
return SYM2ID(RUBY_CEXT_INVOKE("rb_intern", rb_str_new(string, length)));
1717
}
1818

1919
ID rb_intern3(const char *name, long len, rb_encoding *enc) {
20-
return (ID) SYM2ID(RUBY_CEXT_INVOKE("rb_intern3", rb_str_new(name, len), rb_enc_from_encoding(enc)));
20+
return SYM2ID(RUBY_CEXT_INVOKE("rb_intern3", rb_str_new(name, len), rb_enc_from_encoding(enc)));
2121
}
2222

2323
VALUE rb_sym2str(VALUE string) {

src/main/java/org/truffleruby/cext/IDToSymbolNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package org.truffleruby.cext;
1111

1212
import static org.truffleruby.core.symbol.CoreSymbols.idToIndex;
13-
import static org.truffleruby.core.symbol.CoreSymbols.isDynamicSymbol;
1413

1514
import org.truffleruby.RubyContext;
1615
import org.truffleruby.RubyLanguage;
@@ -64,7 +63,7 @@ public static boolean isStaticSymbol(Object value) {
6463
if (!(value instanceof Long)) {
6564
return false;
6665
}
67-
return !isDynamicSymbol((long) value);
66+
return CoreSymbols.isStaticSymbol((long) value);
6867
}
6968

7069
}

src/main/java/org/truffleruby/core/symbol/CoreSymbols.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class CoreSymbols {
3636
public static final RubySymbol NEVER = createRubySymbol("never");
3737
public static final RubySymbol ON_BLOCKING = createRubySymbol("on_blocking");
3838

39+
public static final int FIRST_OP_ID = 33;
3940

4041
public static final RubySymbol BANG = CoreSymbols.createRubySymbol("!", 33);
4142
public static final RubySymbol DOUBLE_QUOTE = CoreSymbols.createRubySymbol("\"", 34);
@@ -206,8 +207,9 @@ private static long toGlobal(long id) {
206207
return id << 4 | STATIC_SYMBOL_ID | GLOBAL_SYMBOL_ID;
207208
}
208209

209-
public static boolean isDynamicSymbol(long value) {
210-
return (value & STATIC_SYMBOL_ID) == 0 && value > LAST_OP_ID;
210+
public static boolean isStaticSymbol(long value) {
211+
return (value >= FIRST_OP_ID && value <= LAST_OP_ID) ||
212+
((value & STATIC_SYMBOL_ID) == STATIC_SYMBOL_ID && (value >> 4) < STATIC_SYMBOLS_SIZE);
211213
}
212214

213215
}

tool/generate-core-symbols.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
ids_map[id.ord] = { :id => id, :name => names[id] }
5757
end
5858

59+
min_index = ids_map.keys.min
5960
offset = 128
6061
index = offset
6162

@@ -98,6 +99,7 @@
9899
public static final RubySymbol NEVER = createRubySymbol("never");
99100
public static final RubySymbol ON_BLOCKING = createRubySymbol("on_blocking");
100101
102+
public static final int FIRST_OP_ID = <%=min_index%>;
101103
<% ids_map.each do |key, value| %>
102104
public static final RubySymbol <%= value[:name] %> = CoreSymbols.createRubySymbol(<%= value[:id].inspect %>, <%= key %>);<% end %>
103105
@@ -154,8 +156,9 @@
154156
return id << 4 | STATIC_SYMBOL_ID | GLOBAL_SYMBOL_ID;
155157
}
156158
157-
public static boolean isDynamicSymbol(long value) {
158-
return (value & STATIC_SYMBOL_ID) == 0 && value > LAST_OP_ID;
159+
public static boolean isStaticSymbol(long value) {
160+
return (value >= FIRST_OP_ID && value <= LAST_OP_ID) ||
161+
((value & STATIC_SYMBOL_ID) == STATIC_SYMBOL_ID && (value >> 4) < STATIC_SYMBOLS_SIZE);
159162
}
160163
161164
}

0 commit comments

Comments
 (0)