diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2020-09-10 18:12:38 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-09-10 18:12:38 +0900 |
| commit | c27e45193177b6e4d09d526a248af63c0961035a (patch) | |
| tree | 863c5082109295c880311b2abe1eefc4c748dc4d /mrbgems/mruby-bin-mirb | |
| parent | c345fe432171cc2b90b614a0b44305b218090de6 (diff) | |
| parent | 9840e5352b2abf96235068f0e9d08f14b1d333cf (diff) | |
| download | mruby-c27e45193177b6e4d09d526a248af63c0961035a.tar.gz mruby-c27e45193177b6e4d09d526a248af63c0961035a.zip | |
Merge pull request #4933 from dearblue/variables
Fix take over file scope variables with `mruby` and `mirb` command
Diffstat (limited to 'mrbgems/mruby-bin-mirb')
| -rw-r--r-- | mrbgems/mruby-bin-mirb/bintest/mirb.rb | 17 | ||||
| -rw-r--r-- | mrbgems/mruby-bin-mirb/tools/mirb/mirb.c | 13 |
2 files changed, 26 insertions, 4 deletions
diff --git a/mrbgems/mruby-bin-mirb/bintest/mirb.rb b/mrbgems/mruby-bin-mirb/bintest/mirb.rb index 0058896f1..3a03726bb 100644 --- a/mrbgems/mruby-bin-mirb/bintest/mirb.rb +++ b/mrbgems/mruby-bin-mirb/bintest/mirb.rb @@ -32,3 +32,20 @@ EOS o, _ = Open3.capture2("bin/mirb -r #{lib.path}", :stdin_data => "Hoge.new.hoge\n") assert_true o.include?('=> :hoge') end + +assert('top level local variables are in file scope') do + lib = Tempfile.new('lib.rb') + lib.write <<-TESTLIB +a = 1 +A = -> { a } + TESTLIB + lib.flush + + o, _ = Open3.capture2("bin/mirb -r #{lib.path}", :stdin_data => <<-TESTCODE) +a +a = 5 +A.call + TESTCODE + + assert_kind_of Integer, o =~ /\bundefined method 'a' \(NoMethodError\).*=> 5\b.*=> 1\b/m +end diff --git a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c index a0eeaf459..51ba3fd78 100644 --- a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c +++ b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c @@ -495,12 +495,9 @@ main(int argc, char **argv) cxt = mrbc_context_new(mrb); -#ifndef DISABLE_MIRB_UNDERSCORE - decl_lv_underscore(mrb, cxt); -#endif - /* Load libraries */ for (i = 0; i < args.libc; i++) { + struct REnv *e; FILE *lfp = fopen(args.libv[i], "r"); if (lfp == NULL) { printf("Cannot open library file. (%s)\n", args.libv[i]); @@ -509,8 +506,16 @@ main(int argc, char **argv) } mrb_load_file_cxt(mrb, lfp, cxt); fclose(lfp); + e = mrb->c->cibase->env; + mrb->c->cibase->env = NULL; + mrb_env_unshare(mrb, e); + mrbc_cleanup_local_variables(mrb, cxt); } +#ifndef DISABLE_MIRB_UNDERSCORE + decl_lv_underscore(mrb, cxt); +#endif + cxt->capture_errors = TRUE; cxt->lineno = 1; mrbc_filename(mrb, cxt, "(mirb)"); |
