mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Implement LVAR section removing option in mruby-strip.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user