summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2018-12-13 21:49:05 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-06-23 01:46:38 +0900
commit93843830198ba436e2ea21a60a11758d47cf521b (patch)
tree0ad4617c10a4474f8a1357006b76bd66ee04f4fb /object.c
parent5084233b88cc48a74b58690423df1129668d5706 (diff)
Module#constant_source_location [Feature #10771]
Diffstat (limited to 'object.c')
-rw-r--r--object.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/object.c b/object.c
index 17964bb5c8..9536cd36f2 100644
--- a/object.c
+++ b/object.c
@@ -2720,6 +2720,105 @@ rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
return Qtrue;
}
+static VALUE
+rb_mod_const_source_location(int argc, VALUE *argv, VALUE mod)
+{
+ VALUE name, recur, loc = Qnil;
+ rb_encoding *enc;
+ const char *pbeg, *p, *path, *pend;
+ ID id;
+
+ rb_check_arity(argc, 1, 2);
+ name = argv[0];
+ recur = (argc == 1) ? Qtrue : argv[1];
+
+ if (SYMBOL_P(name)) {
+ if (!rb_is_const_sym(name)) goto wrong_name;
+ id = rb_check_id(&name);
+ if (!id) return Qnil;
+ return RTEST(recur) ? rb_const_source_location(mod, id) : rb_const_source_location_at(mod, id);
+ }
+
+ path = StringValuePtr(name);
+ enc = rb_enc_get(name);
+
+ if (!rb_enc_asciicompat(enc)) {
+ rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
+ }
+
+ pbeg = p = path;
+ pend = path + RSTRING_LEN(name);
+
+ if (p >= pend || !*p) {
+ wrong_name:
+ rb_name_err_raise(wrong_constant_name, mod, name);
+ }
+
+ if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
+ mod = rb_cObject;
+ p += 2;
+ pbeg = p;
+ }
+
+ while (p < pend) {
+ VALUE part;
+ long len, beglen;
+
+ while (p < pend && *p != ':') p++;
+
+ if (pbeg == p) goto wrong_name;
+
+ id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
+ beglen = pbeg-path;
+
+ if (p < pend && p[0] == ':') {
+ if (p + 2 >= pend || p[1] != ':') goto wrong_name;
+ p += 2;
+ pbeg = p;
+ }
+
+ if (!id) {
+ part = rb_str_subseq(name, beglen, len);
+ OBJ_FREEZE(part);
+ if (!rb_is_const_name(part)) {
+ name = part;
+ goto wrong_name;
+ }
+ else {
+ return Qnil;
+ }
+ }
+ if (!rb_is_const_id(id)) {
+ name = ID2SYM(id);
+ goto wrong_name;
+ }
+ if (p < pend) {
+ if (RTEST(recur)) {
+ mod = rb_const_get(mod, id);
+ }
+ else {
+ mod = rb_const_get_at(mod, id);
+ }
+ if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
+ rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
+ QUOTE(name));
+ }
+ }
+ else {
+ if (RTEST(recur)) {
+ loc = rb_const_source_location(mod, id);
+ }
+ else {
+ loc = rb_const_source_location_at(mod, id);
+ }
+ break;
+ }
+ recur = Qfalse;
+ }
+
+ return loc;
+}
+
/*
* call-seq:
* obj.instance_variable_get(symbol) -> obj
@@ -4249,6 +4348,7 @@ InitVM_Object(void)
rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
+ rb_define_method(rb_cModule, "const_source_location", rb_mod_const_source_location, -1);
rb_define_private_method(rb_cModule, "remove_const",
rb_mod_remove_const, 1); /* in variable.c */
rb_define_method(rb_cModule, "const_missing",