Commit a374769a

a374769ac794Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · ~3 modified

Description

feat: type-aware respond_to? for built-in and polymorphic types

Previously respond_to? only worked for OBJECT types (class hierarchy lookup). Now handles all types:

  • Monomorphic built-in (String, Integer, Float, Array, Hash): resolved at compile time using builtin_has_method() lookup table
  • OBJECT: compile-time class hierarchy search (unchanged)
  • Extension methods: also checked via find_ext_method()
  • POLY: generates runtime type-tag dispatch expression

Also adds builtin_has_method() which lists known methods per type, reusable for future optimizations.

60/60 tests, 39/39 benchmarks pass.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
a374769ac794
Parent
aa76e864e890

src/codegen.cmodifiedView file

@@ -172,6 +172,83 @@
172172 return SPINEL_TYPE_UNKNOWN;
173173 }
174174
175+/* Check if a built-in type has a known method (for respond_to? resolution) */
176+bool builtin_has_method(spinel_type_t kind, const char *name) {
177+ /* Universal methods available on all types */
178+ static const char *universal[] = {
179+ "nil?", "is_a?", "respond_to?", "to_s", "class",
180+ "freeze", "frozen?", "dup", "==", "!=", "equal?", NULL
181+ };
182+ for (const char **p = universal; *p; p++)
183+ if (strcmp(name, *p) == 0) return true;
184+
185+ switch (kind) {
186+ case SPINEL_TYPE_INTEGER: {
187+ static const char *m[] = {
188+ "+", "-", "*", "/", "%", "**", "<", ">", "<=", ">=",
189+ "&", "|", "^", "<<", ">>", "~", "-@",
190+ "abs", "even?", "odd?", "zero?", "positive?", "negative?",
191+ "to_i", "to_f", "chr", "succ", "itself", "clamp",
192+ "times", "upto", "downto", "divmod", NULL
193+ };
194+ for (const char **p = m; *p; p++) if (strcmp(name, *p) == 0) return true;
195+ break;
196+ }
197+ case SPINEL_TYPE_FLOAT: {
198+ static const char *m[] = {
199+ "+", "-", "*", "/", "%", "**", "<", ">", "<=", ">=", "-@",
200+ "abs", "ceil", "floor", "round", "to_i", "to_f",
201+ "zero?", "positive?", "negative?", "infinite?", "nan?", NULL
202+ };
203+ for (const char **p = m; *p; p++) if (strcmp(name, *p) == 0) return true;
204+ break;
205+ }
206+ case SPINEL_TYPE_STRING: {
207+ static const char *m[] = {
208+ "+", "*", "[]", "length", "size", "empty?", "include?",
209+ "start_with?", "end_with?", "match?", "count", "index",
210+ "upcase", "downcase", "capitalize", "reverse",
211+ "strip", "lstrip", "rstrip", "chomp",
212+ "gsub", "sub", "tr", "delete", "squeeze",
213+ "split", "chars", "bytes", "each_line", "each_char",
214+ "ljust", "rjust", "center", "slice",
215+ "to_i", "to_f", "to_sym", "ord", "hex", "oct",
216+ "getbyte", "setbyte", "bytesize", "encode",
217+ "replace", "<<", "clear", NULL
218+ };
219+ for (const char **p = m; *p; p++) if (strcmp(name, *p) == 0) return true;
220+ break;
221+ }
222+ case SPINEL_TYPE_ARRAY:
223+ case SPINEL_TYPE_FLOAT_ARRAY: {
224+ static const char *m[] = {
225+ "[]", "[]=", "push", "<<", "pop", "shift", "unshift",
226+ "length", "size", "empty?", "first", "last", "include?",
227+ "each", "each_with_index", "map", "select", "reject",
228+ "find", "any?", "all?", "none?", "count", "sum",
229+ "min", "max", "sort", "sort!", "reverse", "reverse!",
230+ "uniq", "uniq!", "flatten", "compact", "join", "dup",
231+ "insert", "reduce", "inject", "sort_by", "zip", NULL
232+ };
233+ for (const char **p = m; *p; p++) if (strcmp(name, *p) == 0) return true;
234+ break;
235+ }
236+ case SPINEL_TYPE_HASH:
237+ case SPINEL_TYPE_RB_HASH: {
238+ static const char *m[] = {
239+ "[]", "[]=", "has_key?", "key?", "delete",
240+ "keys", "values", "each", "length", "size", "empty?",
241+ "merge", "transform_values", NULL
242+ };
243+ for (const char **p = m; *p; p++) if (strcmp(name, *p) == 0) return true;
244+ break;
245+ }
246+ default:
247+ break;
248+ }
249+ return false;
250+}
251+
175252 /* Find an extension method on a built-in type */
176253 method_info_t *find_ext_method(codegen_ctx_t *ctx, spinel_type_t recv_type, const char *name) {
177254 for (int i = 0; i < ctx->ext_method_count; i++) {