• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

mrubyを超漢字で動作させる


Commit MetaInfo

Révisionf8aac56df02f7106a87a6d8611e9cd8b5b9a4f2e (tree)
l'heure2014-07-16 05:22:04
AuteurYukihiro "Matz" Matsumoto <matz@ruby...>
CommiterYukihiro "Matz" Matsumoto

Message de Log

git push origin masterMerge branch 'take-cheeze-instance_eval'

Change Summary

Modification

--- a/mrbgems/mruby-eval/src/eval.c
+++ b/mrbgems/mruby-eval/src/eval.c
@@ -173,10 +173,36 @@ f_eval(mrb_state *mrb, mrb_value self)
173173 return ret;
174174 }
175175
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+
176201 void
177202 mrb_mruby_eval_gem_init(mrb_state* mrb)
178203 {
179204 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));
180206 }
181207
182208 void
--- a/mrbgems/mruby-eval/test/eval.rb
+++ b/mrbgems/mruby-eval/test/eval.rb
@@ -50,3 +50,13 @@ assert 'eval syntax error' do
5050 eval 'p "test'
5151 end
5252 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