Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-object-ext / src / object.c
1 #include <mruby.h>
2 #include <mruby/array.h>
3 #include <mruby/class.h>
4 #include <mruby/proc.h>
5
6 /*
7  *  call-seq:
8  *     nil.to_a    -> []
9  *
10  *  Always returns an empty array.
11  */
12
13 static mrb_value
14 nil_to_a(mrb_state *mrb, mrb_value obj)
15 {
16   return mrb_ary_new(mrb);
17 }
18
19 #ifndef MRB_WITHOUT_FLOAT
20 /*
21  *  call-seq:
22  *     nil.to_f    -> 0.0
23  *
24  *  Always returns zero.
25  */
26
27 static mrb_value
28 nil_to_f(mrb_state *mrb, mrb_value obj)
29 {
30   return mrb_float_value(mrb, 0.0);
31 }
32 #endif
33
34 /*
35  *  call-seq:
36  *     nil.to_i    -> 0
37  *
38  *  Always returns zero.
39  */
40
41 static mrb_value
42 nil_to_i(mrb_state *mrb, mrb_value obj)
43 {
44   return mrb_fixnum_value(0);
45 }
46
47 /*
48  *  call-seq:
49  *     obj.instance_exec(arg...) {|var...| block }                       -> obj
50  *
51  *  Executes the given block within the context of the receiver
52  *  (_obj_). In order to set the context, the variable +self+ is set
53  *  to _obj_ while the code is executing, giving the code access to
54  *  _obj_'s instance variables.  Arguments are passed as block parameters.
55  *
56  *     class KlassWithSecret
57  *       def initialize
58  *         @secret = 99
59  *       end
60  *     end
61  *     k = KlassWithSecret.new
62  *     k.instance_exec(5) {|x| @secret+x }   #=> 104
63  */
64
65 static mrb_value
66 mrb_obj_instance_exec(mrb_state *mrb, mrb_value self)
67 {
68   const mrb_value *argv;
69   mrb_int argc;
70   mrb_value blk;
71   struct RClass *c;
72
73   mrb_get_args(mrb, "*&", &argv, &argc, &blk);
74
75   if (mrb_nil_p(blk)) {
76     mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
77   }
78
79   switch (mrb_type(self)) {
80   case MRB_TT_SYMBOL:
81   case MRB_TT_FIXNUM:
82 #ifndef MRB_WITHOUT_FLOAT
83   case MRB_TT_FLOAT:
84 #endif
85     c = NULL;
86     break;
87   default:
88     c = mrb_class_ptr(mrb_singleton_class(mrb, self));
89     break;
90   }
91   mrb->c->ci->target_class = c;
92   return mrb_yield_cont(mrb, blk, self, argc, argv);
93 }
94
95 void
96 mrb_mruby_object_ext_gem_init(mrb_state* mrb)
97 {
98   struct RClass * n = mrb->nil_class;
99
100   mrb_define_method(mrb, n, "to_a", nil_to_a,       MRB_ARGS_NONE());
101 #ifndef MRB_WITHOUT_FLOAT
102   mrb_define_method(mrb, n, "to_f", nil_to_f,       MRB_ARGS_NONE());
103 #endif
104   mrb_define_method(mrb, n, "to_i", nil_to_i,       MRB_ARGS_NONE());
105
106   mrb_define_method(mrb, mrb->kernel_module, "instance_exec", mrb_obj_instance_exec, MRB_ARGS_ANY() | MRB_ARGS_BLOCK());
107 }
108
109 void
110 mrb_mruby_object_ext_gem_final(mrb_state* mrb)
111 {
112 }