Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-class-ext / src / class.c
1 #include "mruby.h"
2 #include "mruby/class.h"
3 #include "mruby/string.h"
4
5 static mrb_value
6 mrb_mod_name(mrb_state *mrb, mrb_value self)
7 {
8   mrb_value name = mrb_class_path(mrb, mrb_class_ptr(self));
9   return mrb_nil_p(name)? name : mrb_str_dup(mrb, name);
10 }
11
12 static mrb_value
13 mrb_mod_singleton_class_p(mrb_state *mrb, mrb_value self)
14 {
15   return mrb_bool_value(mrb_type(self) == MRB_TT_SCLASS);
16 }
17
18 /*
19  *  call-seq:
20  *     module_exec(arg...) {|var...| block } -> obj
21  *     class_exec(arg...) {|var...| block } -> obj
22  *
23  * Evaluates the given block in the context of the
24  * class/module. The method defined in the block will belong
25  * to the receiver. Any arguments passed to the method will be
26  * passed to the block. This can be used if the block needs to
27  * access instance variables.
28  *
29  *     class Thing
30  *     end
31  *     Thing.class_exec{
32  *       def hello() "Hello there!" end
33  *     }
34  *     puts Thing.new.hello()
35  */
36
37 static mrb_value
38 mrb_mod_module_exec(mrb_state *mrb, mrb_value self)
39 {
40   const mrb_value *argv;
41   mrb_int argc;
42   mrb_value blk;
43
44   mrb_get_args(mrb, "*&", &argv, &argc, &blk);
45
46   if (mrb_nil_p(blk)) {
47     mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
48   }
49
50   mrb->c->ci->target_class = mrb_class_ptr(self);
51   return mrb_yield_cont(mrb, blk, self, argc, argv);
52 }
53
54 void
55 mrb_mruby_class_ext_gem_init(mrb_state *mrb)
56 {
57   struct RClass *mod = mrb->module_class;
58
59   mrb_define_method(mrb, mod, "name", mrb_mod_name, MRB_ARGS_NONE());
60   mrb_define_method(mrb, mod, "singleton_class?", mrb_mod_singleton_class_p, MRB_ARGS_NONE());
61   mrb_define_method(mrb, mod, "module_exec", mrb_mod_module_exec, MRB_ARGS_ANY()|MRB_ARGS_BLOCK());
62   mrb_define_method(mrb, mod, "class_exec", mrb_mod_module_exec, MRB_ARGS_ANY()|MRB_ARGS_BLOCK());
63 }
64
65 void
66 mrb_mruby_class_ext_gem_final(mrb_state *mrb)
67 {
68 }