Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-class-ext/src/class.c

raw
1 #include "mruby.h"
2 #include "mruby/class.h"
3 #include "mruby/string.h"
4 #include "mruby/array.h"
5 #include "mruby/proc.h"
6 #include "mruby/variable.h"
7 #include "mruby/presym.h"
8
9 static mrb_value
10 mod_name(mrb_state *mrb, mrb_value self)
11 {
12 mrb_value name = mrb_class_path(mrb, mrb_class_ptr(self));
13 if (mrb_string_p(name)) {
14 MRB_SET_FROZEN_FLAG(mrb_basic_ptr(name));
15 }
16 return name;
17 }
18
19 static mrb_value
20 mod_singleton_class_p(mrb_state *mrb, mrb_value self)
21 {
22 return mrb_bool_value(mrb_sclass_p(self));
23 }
24
25 /*
26 * call-seq:
27 * module_exec(arg...) {|var...| block } -> obj
28 * class_exec(arg...) {|var...| block } -> obj
29 *
30 * Evaluates the given block in the context of the
31 * class/module. The method defined in the block will belong
32 * to the receiver. Any arguments passed to the method will be
33 * passed to the block. This can be used if the block needs to
34 * access instance variables.
35 *
36 * class Thing
37 * end
38 * Thing.class_exec{
39 * def hello() "Hello there!" end
40 * }
41 * puts Thing.new.hello()
42 */
43
44 static mrb_value
45 mod_module_exec(mrb_state *mrb, mrb_value self)
46 {
47 const mrb_value *argv;
48 mrb_int argc;
49 mrb_value blk;
50 struct RClass *c;
51
52 mrb_get_args(mrb, "*&!", &argv, &argc, &blk);
53
54 c = mrb_class_ptr(self);
55 if (mrb->c->ci->cci > 0) {
56 return mrb_yield_with_class(mrb, blk, argc, argv, self, c);
57 }
58 mrb_vm_ci_target_class_set(mrb->c->ci, c);
59 return mrb_yield_cont(mrb, blk, self, argc, argv);
60 }
61
62 struct subclass_args {
63 struct RClass *c;
64 mrb_value ary;
65 };
66
67 static int
68 add_subclasses(mrb_state *mrb, struct RBasic *obj, void *data)
69 {
70 struct subclass_args *args = (struct subclass_args*)data;
71 if (obj->tt == MRB_TT_CLASS) {
72 struct RClass *c = (struct RClass*)obj;
73 if (mrb_class_real(c->super) == args->c) {
74 mrb_ary_push(mrb, args->ary, mrb_obj_value(obj));
75 }
76 }
77 return MRB_EACH_OBJ_OK;
78 }
79
80 /*
81 * call-seq:
82 * subclasses -> array
83 *
84 * Returns an array of classes where the receiver is the
85 * direct superclass of the class, excluding singleton classes.
86 * The order of the returned array is not defined.
87 *
88 * class A; end
89 * class B < A; end
90 * class C < B; end
91 * class D < A; end
92 *
93 * A.subclasses #=> [D, B]
94 * B.subclasses #=> [C]
95 * C.subclasses #=> []
96 */
97 static mrb_value
98 class_subclasses(mrb_state *mrb, mrb_value self)
99 {
100 struct RClass *c;
101 mrb_value ary;
102
103 c = mrb_class_ptr(self);
104 ary = mrb_ary_new(mrb);
105
106 if (c->flags & MRB_FL_CLASS_IS_INHERITED) {
107 struct subclass_args arg = {c, ary};
108 mrb_objspace_each_objects(mrb, add_subclasses, &arg);
109 }
110 return ary;
111 }
112
113 /*
114 * call-seq:
115 * attached_object -> object
116 *
117 * Returns the object for which the receiver is the singleton class.
118 * Raises an TypeError if the class is not a singleton class.
119 *
120 * class Foo; end
121 *
122 * Foo.singleton_class.attached_object #=> Foo
123 * Foo.attached_object #=> TypeError: not a singleton class
124 * Foo.new.singleton_class.attached_object #=> #<Foo:0x000000010491a370>
125 * TrueClass.attached_object #=> TypeError: not a singleton class
126 * NilClass.attached_object #=> TypeError: not a singleton class
127 */
128 static mrb_value
129 class_attached_object(mrb_state *mrb, mrb_value self)
130 {
131 struct RClass *c;
132
133 c = mrb_class_ptr(self);
134 if (c->tt != MRB_TT_SCLASS) {
135 mrb_raise(mrb, E_TYPE_ERROR, "not a singleton class");
136 }
137 return mrb_obj_iv_get(mrb, (struct RObject*)c, MRB_SYM(__attached__));
138 }
139
140 void
141 mrb_mruby_class_ext_gem_init(mrb_state *mrb)
142 {
143 struct RClass *mod = mrb->module_class;
144
145 mrb_define_method(mrb, mod, "name", mod_name, MRB_ARGS_NONE());
146 mrb_define_method(mrb, mod, "singleton_class?", mod_singleton_class_p, MRB_ARGS_NONE());
147 mrb_define_method(mrb, mod, "module_exec", mod_module_exec, MRB_ARGS_ANY()|MRB_ARGS_BLOCK());
148 mrb_define_method(mrb, mod, "class_exec", mod_module_exec, MRB_ARGS_ANY()|MRB_ARGS_BLOCK());
149
150 struct RClass *cls = mrb->class_class;
151 mrb_define_method(mrb, cls, "subclasses", class_subclasses, MRB_ARGS_NONE());
152 mrb_define_method(mrb, cls, "attached_object", class_attached_object, MRB_ARGS_NONE());
153 }
154
155 void
156 mrb_mruby_class_ext_gem_final(mrb_state *mrb)
157 {
158 }