summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-bin-strip
diff options
context:
space:
mode:
authortake_cheeze <[email protected]>2014-05-15 15:09:08 +0900
committertake_cheeze <[email protected]>2014-05-15 15:15:06 +0900
commite0064acac71a28b75797c3e3ea1b8aca1ce8a796 (patch)
treeb40492fef958e057ec4cab5a4d9d1298bc8a6995 /mrbgems/mruby-bin-strip
parent30e0dc79566430a176dd2cfab593cd3620ba69da (diff)
downloadmruby-e0064acac71a28b75797c3e3ea1b8aca1ce8a796.tar.gz
mruby-e0064acac71a28b75797c3e3ea1b8aca1ce8a796.zip
Implement `LVAR` section removing option in mruby-strip.
Diffstat (limited to 'mrbgems/mruby-bin-strip')
-rw-r--r--mrbgems/mruby-bin-strip/bintest/mruby-strip.rb21
-rw-r--r--mrbgems/mruby-bin-strip/mrbgem.rake1
-rw-r--r--mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c67
3 files changed, 83 insertions, 6 deletions
diff --git a/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb b/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb
index 17bd0e71f..770ea0638 100644
--- a/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb
+++ b/mrbgems/mruby-bin-strip/bintest/mruby-strip.rb
@@ -3,7 +3,7 @@ require 'tempfile'
assert('no files') do
o = `bin/mruby-strip 2>&1`
assert_equal 1, $?.exitstatus
- assert_equal "no files to strip\n", o
+ assert_equal "no files to strip", o.split("\n")[0]
end
assert('file not found') do
@@ -52,3 +52,22 @@ assert('check debug section') do
`bin/mruby-strip #{with_debug.path}`
assert_equal without_debug.size, with_debug.size
end
+
+assert('check lv section') do
+ script_file, with_lv, without_lv =
+ Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb')
+ script_file.write <<EOS
+a, b = 0, 1
+a += b
+p Kernel.local_variables
+EOS
+ script_file.flush
+ `bin/mrbc -o #{with_lv.path} #{script_file.path}`
+ `bin/mrbc -o #{without_lv.path} #{script_file.path}`
+
+ `bin/mruby-strip -l #{without_lv.path}`
+ assert_true without_lv.size < with_lv.size
+
+ assert_equal '[:a, :b]', `bin/mruby -b #{with_lv.path}`.chomp
+ assert_equal '[]', `bin/mruby -b #{without_lv.path}`.chomp
+end
diff --git a/mrbgems/mruby-bin-strip/mrbgem.rake b/mrbgems/mruby-bin-strip/mrbgem.rake
index 2abd25eea..7dfc5912d 100644
--- a/mrbgems/mruby-bin-strip/mrbgem.rake
+++ b/mrbgems/mruby-bin-strip/mrbgem.rake
@@ -3,4 +3,5 @@ MRuby::Gem::Specification.new('mruby-bin-strip') do |spec|
spec.author = 'mruby developers'
spec.summary = 'irep dump debug section remover command'
spec.bins = %w(mruby-strip)
+ spec.add_dependency 'mruby-proc-ext', :core =>'mruby-proc-ext'
end
diff --git a/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c b/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c
index dee3e0cd6..dca50f03f 100644
--- a/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c
+++ b/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c
@@ -1,24 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "mruby.h"
#include "mruby/irep.h"
#include "mruby/dump.h"
+struct strip_args {
+ mrb_bool lvar;
+};
+
+static void
+print_usage(const char *f)
+{
+ printf("Usage: %s [options] irepfiles\n", f);
+ printf("options:\n");
+ printf(" -l, --lvar remove LVAR section too.\n");
+}
+
+static int
+parse_args(int argc, char **argv, struct strip_args *args)
+{
+ static const struct strip_args initial_args = {0};
+ int i;
+
+ *args = initial_args;
+
+ for (i = 1; i < argc; ++i) {
+ size_t const len = strlen(argv[i]);
+ if (len >= 2 && argv[i][0] == '-') {
+ switch(argv[i][1]) {
+ case 'l':
+ args->lvar = TRUE;
+ break;
+ case '-':
+ if (strncmp((*argv) + 2, "lvar", len) == 0) {
+ args->lvar = TRUE;
+ break;
+ }
+ default:
+ return -1;
+ }
+ } else {
+ break;
+ }
+ }
+
+ return i;
+}
+
int
main(int argc, char **argv)
{
- int i, dump_result;
+ struct strip_args args;
+ int args_result, i, dump_result;
FILE **files;
mrb_irep **ireps;
mrb_state *mrb;
if (argc <= 1) {
- fprintf(stderr, "no files to strip\n");
+ printf("no files to strip\n");
+ print_usage(argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ args_result = parse_args(argc, argv, &args);
+ if (args_result < 0) {
+ print_usage(argv[0]);
return EXIT_FAILURE;
}
files = (FILE**)malloc(sizeof(FILE*) * argc);
- for (i = 1; i < argc; ++i) {
+ for (i = args_result; i < argc; ++i) {
files[i] = fopen(argv[i], "rb");
if (!files[i]) {
@@ -30,7 +82,7 @@ main(int argc, char **argv)
mrb = mrb_open();
ireps = (mrb_irep**)malloc(sizeof(mrb_irep*) * argc);
- for (i = 1; i < argc; ++i) {
+ for (i = args_result; i < argc; ++i) {
ireps[i] = mrb_read_irep_file(mrb, files[i]);
if (!ireps[i]) {
fprintf(stderr, "can't read irep file %s\n", argv[i]);
@@ -44,7 +96,12 @@ main(int argc, char **argv)
}
}
- for (i = 1; i < argc; ++i) {
+ for (i = args_result; i < argc; ++i) {
+ /* clear lv if --lvar is enabled */
+ if (args.lvar) {
+ mrb_irep_remove_lv(mrb, ireps[i]);
+ }
+
/* debug flag must be alway false */
dump_result = mrb_dump_irep_binary(mrb, ireps[i], FALSE, files[i]);
if (dump_result != MRB_DUMP_OK) {