Isolate top-level local variables by file scope; fix
MODIFY
lib/mruby/gem.rb
lib/mruby/gem.rb
@@ -171,6 +171,7 @@ module MRuby
f.puts %Q[]
f.puts %Q[void GENERATED_TMP_mrb_#{funcname}_gem_init(mrb_state *mrb) {]
f.puts %Q[ int ai = mrb_gc_arena_save(mrb);]
+ f.puts %Q[ struct REnv *e;] unless rbfiles.empty?
f.puts %Q[ mrb_#{funcname}_gem_init(mrb);] if objs != [objfile("#{build_dir}/gem_init")]
unless rbfiles.empty?
f.puts %Q[ mrb_load_irep(mrb, gem_mrblib_irep_#{funcname});]
@@ -179,6 +180,9 @@ module MRuby
f.puts %Q[ mrb_close(mrb);]
f.puts %Q[ exit(EXIT_FAILURE);]
f.puts %Q[ }]
+ f.puts %Q[ e = mrb->c->cibase->env;]
+ f.puts %Q[ mrb->c->cibase->env = NULL;]
+ f.puts %Q[ mrb_env_unshare(mrb, e);]
end
f.puts %Q[ mrb_gc_arena_restore(mrb, ai);]
f.puts %Q[}]
@@ -205,6 +209,7 @@ module MRuby
f.puts %Q[#include <stdlib.h>] unless rbfiles.empty?
f.puts %Q[#include <mruby.h>]
f.puts %Q[#include <mruby/irep.h>] unless rbfiles.empty?
+ f.puts %Q[#include <mruby/proc.h>] unless rbfiles.empty?
end
def print_gem_test_header(f)

MODIFY
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
@@ -493,6 +493,7 @@ main(int argc, char **argv)
/* 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]);
@@ -501,6 +502,9 @@ 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);
}

MODIFY
mrbgems/mruby-bin-mruby/tools/mruby/mruby.c
mrbgems/mruby-bin-mruby/tools/mruby/mruby.c
#include <mruby/compile.h>
#include <mruby/dump.h>
#include <mruby/variable.h>
+#include <mruby/proc.h>
struct _args {
FILE *rfp;
@@ -303,6 +304,7 @@ main(int argc, char **argv)
/* Load libraries */
for (i = 0; i < args.libc; i++) {
+ struct REnv *e;
FILE *lfp = fopen(args.libv[i], args.mrbfile ? "rb" : "r");
if (lfp == NULL) {
fprintf(stderr, "%s: Cannot open library file: %s\n", *argv, args.libv[i]);
@@ -317,6 +319,9 @@ main(int argc, char **argv)
v = mrb_load_file_cxt(mrb, lfp, c);
}
fclose(lfp);
+ e = mrb->c->cibase->env;
+ mrb->c->cibase->env = NULL;
+ mrb_env_unshare(mrb, e);
mrbc_cleanup_local_variables(mrb, c);
}

Isolate top-level local variables by file scope; fix #4931