From: Kouhei Sutou Date: 2008-05-18T09:23:32+09:00 Subject: [ruby-dev:34734] Re: 拡張ライブラリ初期化中でのmodule_eval 須藤です。 ささださん、これはこういうものなのでしょうか? そういうことなら書き方を変えようと思っています。 In <20080322.001122.301606142902705326.kou@cozmixng.org> "[ruby-dev:34104] Re: 拡張ライブラリ初期化中でのmodule_eval" on Sat, 22 Mar 2008 00:11:16 +0900, Kouhei Sutou wrote: > In <20080319.234924.545103554540142144.kou@cozmixng.org> > "[ruby-dev:34093] 拡張ライブラリ初期化中でのmodule_eval" on Wed, 19 Mar 2008 23:49:19 +0900, > Kouhei Sutou wrote: > > > 拡張ライブラリを初期化しているとき(Init_XXXを呼んでいるとき) > > にmodule_evalを呼ぶとトップレベルで評価されている気がします。 > > > > a.c: > > #include > > > > void > > Init_a(void) > > { > > rb_funcall(rb_cArray, rb_intern("module_eval"), > > 1, rb_str_new2("def xxx; yyy; end")); > > } > > > > extconf.rb: > > require 'mkmf' > > > > create_makefile("a") > > > > 実行例: > > % ruby19 extconf.rb > > % make > > % ruby19 -v -r a -e 'xxx' > > ruby 1.9.0 (2008-03-19 revision 0) [x86_64-linux] > > (eval):1:in `xxx': undefined local variable or method `yyy' for main:Object (NameError) > > from -e:1:in `
' > > > > これで動くようにはなります。 > > > Index: eval.c > =================================================================== > --- eval.c (revision 15824) > +++ eval.c (working copy) > @@ -1855,8 +1855,8 @@ > cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); > } > > - stored_cref = (NODE *)vm_cfp_svar_get(th, cfp, 2); > - vm_cfp_svar_set(th, cfp, 2, (VALUE)vm_cref_push(th, under, NOEX_PUBLIC)); > + stored_cref = vm_set_special_cref(th, cfp->lfp, > + vm_cref_push(th, under, NOEX_PUBLIC)); > > PUSH_TAG(); > if ((state = EXEC_TAG()) == 0) { > @@ -1865,7 +1865,7 @@ > POP_TAG(); > > /* restore environment */ > - vm_cfp_svar_set(th, cfp, 2, (VALUE)stored_cref); > + vm_set_special_cref(th, cfp->lfp, stored_cref); > pcfp->self = stored_self; > > if (state) { > >