mrubyを超漢字で動作させる
Révision | f8aac56df02f7106a87a6d8611e9cd8b5b9a4f2e (tree) |
---|---|
l'heure | 2014-07-16 05:22:04 |
Auteur | Yukihiro "Matz" Matsumoto <matz@ruby...> |
Commiter | Yukihiro "Matz" Matsumoto |
git push origin masterMerge branch 'take-cheeze-instance_eval'
@@ -173,10 +173,36 @@ f_eval(mrb_state *mrb, mrb_value self) | ||
173 | 173 | return ret; |
174 | 174 | } |
175 | 175 | |
176 | +mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self); | |
177 | + | |
178 | +static mrb_value | |
179 | +f_instance_eval(mrb_state *mrb, mrb_value self) | |
180 | +{ | |
181 | + mrb_value b; | |
182 | + mrb_int argc; mrb_value *argv; | |
183 | + | |
184 | + mrb_get_args(mrb, "*&", &argv, &argc, &b); | |
185 | + | |
186 | + if (mrb_nil_p(b)) { | |
187 | + char *s; | |
188 | + mrb_int len; | |
189 | + char *file = NULL; | |
190 | + mrb_int line = 1; | |
191 | + | |
192 | + mrb_get_args(mrb, "s|zi", &s, &len, &file, &line); | |
193 | + | |
194 | + return mrb_run(mrb, create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line), self); | |
195 | + } else { | |
196 | + mrb_get_args(mrb, "&", &b); | |
197 | + return mrb_obj_instance_eval(mrb, self); | |
198 | + } | |
199 | +} | |
200 | + | |
176 | 201 | void |
177 | 202 | mrb_mruby_eval_gem_init(mrb_state* mrb) |
178 | 203 | { |
179 | 204 | mrb_define_module_function(mrb, mrb->kernel_module, "eval", f_eval, MRB_ARGS_ARG(1, 3)); |
205 | + mrb_define_method(mrb, mrb->kernel_module, "instance_eval", f_instance_eval, MRB_ARGS_ARG(1, 2)); | |
180 | 206 | } |
181 | 207 | |
182 | 208 | void |
@@ -50,3 +50,13 @@ assert 'eval syntax error' do | ||
50 | 50 | eval 'p "test' |
51 | 51 | end |
52 | 52 | end |
53 | + | |
54 | +assert('String instance_eval') do | |
55 | + obj = Object.new | |
56 | + obj.instance_variable_set :@test, 'test' | |
57 | + assert_raise(ArgumentError) { obj.instance_eval(0) { } } | |
58 | + assert_raise(ArgumentError) { obj.instance_eval('0', 'test', 0, 'test') } | |
59 | + assert_equal(['test.rb', 10]) { obj.instance_eval('[__FILE__, __LINE__]', 'test.rb', 10)} | |
60 | + assert_equal('test') { obj.instance_eval('@test') } | |
61 | + assert_equal('test') { obj.instance_eval { @test } } | |
62 | +end |