first commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
require 'tempfile'
|
||||
|
||||
assert('no files') do
|
||||
o = `#{cmd('mruby-strip')} 2>&1`
|
||||
assert_equal 1, $?.exitstatus
|
||||
assert_equal "no files to strip", o.split("\n")[0]
|
||||
end
|
||||
|
||||
assert('file not found') do
|
||||
o = `#{cmd('mruby-strip')} not_found.mrb 2>&1`
|
||||
assert_equal 1, $?.exitstatus
|
||||
assert_equal "can't open file for reading not_found.mrb\n", o
|
||||
end
|
||||
|
||||
assert('not irep file') do
|
||||
t = Tempfile.new('script.rb')
|
||||
t.write 'p test\n'
|
||||
t.flush
|
||||
o = `#{cmd('mruby-strip')} #{t.path} 2>&1`
|
||||
assert_equal 1, $?.exitstatus
|
||||
assert_equal "can't read irep file #{t.path}\n", o
|
||||
end
|
||||
|
||||
assert('success') do
|
||||
script_file, compiled1, compiled2 =
|
||||
Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb')
|
||||
script_file.write "p 'test'\n"
|
||||
script_file.flush
|
||||
`#{cmd('mrbc')} -g -o #{compiled1.path} #{script_file.path}`
|
||||
`#{cmd('mrbc')} -g -o #{compiled2.path} #{script_file.path}`
|
||||
|
||||
o = `#{cmd('mruby-strip')} #{compiled1.path}`
|
||||
assert_equal 0, $?.exitstatus
|
||||
assert_equal "", o
|
||||
assert_equal `#{cmd('mruby')} #{script_file.path}`, `#{cmd('mruby')} -b #{compiled1.path}`
|
||||
|
||||
o = `#{cmd('mruby-strip')} #{compiled1.path} #{compiled2.path}`
|
||||
assert_equal 0, $?.exitstatus
|
||||
assert_equal "", o
|
||||
end
|
||||
|
||||
assert('check debug section') do
|
||||
script_file, with_debug, without_debug =
|
||||
Tempfile.new('script.rb'), Tempfile.new('c1.mrb'), Tempfile.new('c2.mrb')
|
||||
script_file.write "p 'test'\n"
|
||||
script_file.flush
|
||||
`#{cmd('mrbc')} -o #{without_debug.path} #{script_file.path}`
|
||||
`#{cmd('mrbc')} -g -o #{with_debug.path} #{script_file.path}`
|
||||
|
||||
assert_true with_debug.size >= without_debug.size
|
||||
|
||||
`#{cmd('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
|
||||
`#{cmd('mrbc')} -o #{with_lv.path} #{script_file.path}`
|
||||
`#{cmd('mrbc')} -o #{without_lv.path} #{script_file.path}`
|
||||
|
||||
`#{cmd('mruby-strip')} -l #{without_lv.path}`
|
||||
assert_true without_lv.size < with_lv.size
|
||||
#
|
||||
# assert_equal '[:a, :b]', `#{cmd('mruby')} -b #{with_lv.path}`.chomp
|
||||
# assert_equal '[]', `#{cmd('mruby')} -b #{without_lv.path}`.chomp
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
MRuby::Gem::Specification.new('mruby-bin-strip') do |spec|
|
||||
spec.license = 'MIT'
|
||||
spec.author = 'mruby developers'
|
||||
spec.summary = 'irep dump debug section remover command'
|
||||
spec.bins = %w(mruby-strip)
|
||||
end
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
#include <mruby.h>
|
||||
|
||||
#ifdef MRB_DISABLE_STDIO
|
||||
# error mruby-bin-strip conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb'
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <mruby/irep.h>
|
||||
#include <mruby/dump.h>
|
||||
|
||||
struct strip_args {
|
||||
int argc_start;
|
||||
int argc;
|
||||
char **argv;
|
||||
mrb_bool lvar;
|
||||
};
|
||||
|
||||
static void
|
||||
print_usage(const char *f)
|
||||
{
|
||||
printf("Usage: %s [switches] irepfiles\n", f);
|
||||
printf("switches:\n");
|
||||
printf(" -l, --lvar remove LVAR section too.\n");
|
||||
}
|
||||
|
||||
static int
|
||||
parse_args(int argc, char **argv, struct strip_args *args)
|
||||
{
|
||||
int i;
|
||||
|
||||
args->argc_start = 0;
|
||||
args->argc = argc;
|
||||
args->argv = argv;
|
||||
args->lvar = FALSE;
|
||||
|
||||
for (i = 1; i < argc; ++i) {
|
||||
const size_t 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;
|
||||
}
|
||||
}
|
||||
|
||||
args->argc_start = i;
|
||||
return i;
|
||||
}
|
||||
|
||||
static int
|
||||
strip(mrb_state *mrb, struct strip_args *args)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = args->argc_start; i < args->argc; ++i) {
|
||||
char *filename;
|
||||
FILE *rfile;
|
||||
mrb_irep *irep;
|
||||
FILE *wfile;
|
||||
int dump_result;
|
||||
|
||||
filename = args->argv[i];
|
||||
rfile = fopen(filename, "rb");
|
||||
if (rfile == NULL) {
|
||||
fprintf(stderr, "can't open file for reading %s\n", filename);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
irep = mrb_read_irep_file(mrb, rfile);
|
||||
fclose(rfile);
|
||||
if (irep == NULL) {
|
||||
fprintf(stderr, "can't read irep file %s\n", filename);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* clear lv if --lvar is enabled */
|
||||
if (args->lvar) {
|
||||
mrb_irep_remove_lv(mrb, irep);
|
||||
}
|
||||
|
||||
wfile = fopen(filename, "wb");
|
||||
if (wfile == NULL) {
|
||||
fprintf(stderr, "can't open file for writing %s\n", filename);
|
||||
mrb_irep_decref(mrb, irep);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* debug flag must always be false */
|
||||
dump_result = mrb_dump_irep_binary(mrb, irep, FALSE, wfile);
|
||||
|
||||
fclose(wfile);
|
||||
mrb_irep_decref(mrb, irep);
|
||||
|
||||
if (dump_result != MRB_DUMP_OK) {
|
||||
fprintf(stderr, "error occurred during dumping %s\n", filename);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
struct strip_args args;
|
||||
int args_result;
|
||||
mrb_state *mrb;
|
||||
int ret;
|
||||
|
||||
if (argc <= 1) {
|
||||
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;
|
||||
}
|
||||
mrb = mrb_open_core(mrb_default_allocf, NULL);
|
||||
if (mrb == NULL) {
|
||||
fputs("Invalid mrb_state, exiting mruby-strip\n", stderr);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
ret = strip(mrb, &args);
|
||||
|
||||
mrb_close(mrb);
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user