Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-bin-mruby
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 3 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-bin-mruby/bintest/mruby.rb
r331 r439 1 1 require 'tempfile' 2 require 'open3' 3 4 def assert_mruby(exp_out, exp_err, exp_success, args) 5 out, err, stat = Open3.capture3(cmd("mruby"), *args) 6 assert "assert_mruby" do 7 assert_operator(exp_out, :===, out, "standard output") 8 assert_operator(exp_err, :===, err, "standard error") 9 assert_equal(exp_success, stat.success?, "exit success?") 10 end 11 end 2 12 3 13 assert('regression for #1564') do 4 o = `#{cmd('mruby')} -e #{shellquote('<<')} 2>&1` 5 assert_include o, "-e:1:2: syntax error" 6 o = `#{cmd('mruby')} -e #{shellquote('<<-')} 2>&1` 7 assert_include o, "-e:1:3: syntax error" 14 assert_mruby("", /\A-e:1:2: syntax error, .*\n\z/, false, %w[-e <<]) 15 assert_mruby("", /\A-e:1:3: syntax error, .*\n\z/, false, %w[-e <<-]) 8 16 end 9 17 … … 13 21 system "#{cmd('mrbc')} -g -o #{bin.path} #{script.path}" 14 22 o = `#{cmd('mruby')} -b #{bin.path}`.strip 15 assert_equal o, '"ok"'23 assert_equal '"ok"', o 16 24 end 17 25 … … 30 38 # one liner 31 39 assert_equal '"-e"', `#{cmd('mruby')} -e #{shellquote('p $0')}`.chomp 40 end 41 42 assert 'ARGV value' do 43 assert_mruby(%{["ab", "cde"]\n}, "", true, %w[-e p(ARGV) ab cde]) 44 assert_mruby("[]\n", "", true, %w[-e p(ARGV)]) 45 end 46 47 assert('float literal') do 48 script, bin = Tempfile.new('test.rb'), Tempfile.new('test.mrb') 49 File.write script.path, 'p [3.21, 2e308.infinite?, -2e308.infinite?]' 50 system "#{cmd('mrbc')} -g -o #{bin.path} #{script.path}" 51 assert_equal "[3.21, 1, -1]", `#{cmd('mruby')} -b #{bin.path}`.chomp! 32 52 end 33 53 … … 59 79 assert_equal 0, $?.exitstatus 60 80 end 81 82 assert('mruby -c option') do 83 assert_mruby("Syntax OK\n", "", true, ["-c", "-e", "p 1"]) 84 assert_mruby("", /\A-e:1:7: syntax error, .*\n\z/, false, ["-c", "-e", "p 1; 1."]) 85 end 86 87 assert('mruby -d option') do 88 assert_mruby("false\n", "", true, ["-e", "p $DEBUG"]) 89 assert_mruby("true\n", "", true, ["-dep $DEBUG"]) 90 end 91 92 assert('mruby -e option (no code specified)') do 93 assert_mruby("", /\A.*: No code specified for -e\n\z/, false, %w[-e]) 94 end 95 96 assert('mruby -h option') do 97 assert_mruby(/\AUsage: #{Regexp.escape cmd("mruby")} .*/m, "", true, %w[-h]) 98 end 99 100 assert('mruby -r option') do 101 lib = Tempfile.new('lib.rb') 102 lib.write <<EOS 103 class Hoge 104 def hoge 105 :hoge 106 end 107 end 108 EOS 109 lib.flush 110 111 script = Tempfile.new('test.rb') 112 script.write <<EOS 113 print Hoge.new.hoge 114 EOS 115 script.flush 116 assert_equal 'hoge', `#{cmd('mruby')} -r #{lib.path} #{script.path}` 117 assert_equal 0, $?.exitstatus 118 119 assert_equal 'hogeClass', `#{cmd('mruby')} -r #{lib.path} -r #{script.path} -e #{shellquote('print Hoge.class')}` 120 assert_equal 0, $?.exitstatus 121 end 122 123 assert('mruby -r option (no library specified)') do 124 assert_mruby("", /\A.*: No library specified for -r\n\z/, false, %w[-r]) 125 end 126 127 assert('mruby -r option (file not found)') do 128 assert_mruby("", /\A.*: Cannot open library file: .*\n\z/, false, %w[-r _no_exists_]) 129 end 130 131 assert('mruby -v option') do 132 ver_re = '\Amruby \d+\.\d+\.\d+ \(\d+-\d+-\d+\)\n' 133 assert_mruby(/#{ver_re}\z/, "", true, %w[-v]) 134 assert_mruby(/#{ver_re}^[^\n]*NODE.*\n:end\n\z/m, "", true, %w[-v -e p(:end)]) 135 end 136 137 assert('mruby --verbose option') do 138 assert_mruby(/\A[^\n]*NODE.*\n:end\n\z/m, "", true, %w[--verbose -e p(:end)]) 139 end 140 141 assert('mruby --') do 142 assert_mruby(%{["-x", "1"]\n}, "", true, %w[-e p(ARGV) -- -x 1]) 143 end 144 145 assert('mruby invalid short option') do 146 assert_mruby("", /\A.*: invalid option -1 .*\n\z/, false, %w[-1]) 147 end 148 149 assert('mruby invalid long option') do 150 assert_mruby("", /\A.*: invalid option --longopt .*\n\z/, false, %w[--longopt]) 151 end 152 153 assert('unhandled exception') do 154 assert_mruby("", /\bEXCEPTION\b.*\n\z/, false, %w[-e raise("EXCEPTION")]) 155 end 156 157 assert('program file not found') do 158 assert_mruby("", /\A.*: Cannot open program file: .*\n\z/, false, %w[_no_exists_]) 159 end 160 161 assert('codegen error') do 162 code = "def f(#{(1..100).map{|n| "a#{n}"} * ","}); end" 163 assert_mruby("", /\Acodegen error:.*\n\z/, false, ["-e", code]) 164 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-bin-mruby/mrbgem.rake
r331 r439 6 6 spec.add_dependency('mruby-compiler', :core => 'mruby-compiler') 7 7 spec.add_dependency('mruby-error', :core => 'mruby-error') 8 spec.add_test_dependency('mruby-print', :core => 'mruby-print') 8 9 9 10 if build.cxx_exception_enabled? 10 @objs << build.compile_as_cxx("#{spec.dir}/tools/mruby/mruby.c", "#{spec.build_dir}/tools/mruby/mruby.cxx") 11 @objs.delete_if { |v| v == objfile("#{spec.build_dir}/tools/mruby/mruby") } 11 build.compile_as_cxx("#{spec.dir}/tools/mruby/mruby.c", "#{spec.build_dir}/tools/mruby/mruby.cxx") 12 12 end 13 13 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c
r331 r439 1 #include <stdio.h> 1 #include <mruby.h> 2 3 #ifdef MRB_DISABLE_STDIO 4 # error mruby-bin-mruby conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb' 5 #endif 6 2 7 #include <stdlib.h> 3 8 #include <string.h> 4 #include <mruby.h>5 9 #include <mruby/array.h> 6 10 #include <mruby/compile.h> … … 8 12 #include <mruby/variable.h> 9 13 10 #ifdef MRB_DISABLE_STDIO11 static void12 p(mrb_state *mrb, mrb_value obj)13 {14 mrb_value val = mrb_inspect(mrb, obj);15 16 fwrite(RSTRING_PTR(val), RSTRING_LEN(val), 1, stdout);17 putc('\n', stdout);18 }19 #else20 #define p(mrb,obj) mrb_p(mrb,obj)21 #endif22 23 14 struct _args { 24 15 FILE *rfp; 25 char *cmdline;16 char *cmdline; 26 17 mrb_bool fname : 1; 27 18 mrb_bool mrbfile : 1; 28 19 mrb_bool check_syntax : 1; 29 20 mrb_bool verbose : 1; 21 mrb_bool version : 1; 22 mrb_bool debug : 1; 30 23 int argc; 31 char** argv; 24 char **argv; 25 int libc; 26 char **libv; 27 }; 28 29 struct options { 30 int argc; 31 char **argv; 32 char *program; 33 char *opt; 34 char short_opt[2]; 32 35 }; 33 36 … … 39 42 "-b load and execute RiteBinary (mrb) file", 40 43 "-c check syntax only", 44 "-d set debugging flags (set $DEBUG to true)", 41 45 "-e 'command' one line of script", 46 "-r library load the library before executing your script", 42 47 "-v print version number, then run in verbose mode", 43 48 "--verbose run in verbose mode", … … 48 53 const char *const *p = usage_msg; 49 54 50 printf("Usage: %s [switches] programfile\n", name);55 printf("Usage: %s [switches] [programfile] [arguments]\n", name); 51 56 while (*p) 52 57 printf(" %s\n", *p++); 53 58 } 54 59 60 static void 61 options_init(struct options *opts, int argc, char **argv) 62 { 63 opts->argc = argc; 64 opts->argv = argv; 65 opts->program = *argv; 66 *opts->short_opt = 0; 67 } 68 69 static const char * 70 options_opt(struct options *opts) 71 { 72 /* concatenated short options (e.g. `-cv`) */ 73 if (*opts->short_opt && *++opts->opt) { 74 short_opt: 75 opts->short_opt[0] = *opts->opt; 76 opts->short_opt[1] = 0; 77 return opts->short_opt; 78 } 79 80 while (++opts->argv, --opts->argc) { 81 opts->opt = *opts->argv; 82 83 /* empty || not start with `-` || `-` */ 84 if (!opts->opt[0] || opts->opt[0] != '-' || !opts->opt[1]) return NULL; 85 86 if (opts->opt[1] == '-') { 87 /* `--` */ 88 if (!opts->opt[2]) { 89 ++opts->argv, --opts->argc; 90 return NULL; 91 } 92 /* long option */ 93 opts->opt += 2; 94 *opts->short_opt = 0; 95 return opts->opt; 96 } 97 else { 98 /* short option */ 99 ++opts->opt; 100 goto short_opt; 101 } 102 } 103 return NULL; 104 } 105 106 static const char * 107 options_arg(struct options *opts) 108 { 109 if (*opts->short_opt && opts->opt[1]) { 110 /* concatenated short option and option argument (e.g. `-rLIBRARY`) */ 111 *opts->short_opt = 0; 112 return opts->opt + 1; 113 } 114 --opts->argc, ++opts->argv; 115 return opts->argc ? *opts->argv : NULL; 116 } 117 118 static char * 119 dup_arg_item(mrb_state *mrb, const char *item) 120 { 121 size_t buflen = strlen(item) + 1; 122 char *buf = (char*)mrb_malloc(mrb, buflen); 123 memcpy(buf, item, buflen); 124 return buf; 125 } 126 55 127 static int 56 128 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) 57 129 { 58 char **origargv = argv;59 130 static const struct _args args_zero = { 0 }; 131 struct options opts[1]; 132 const char *opt, *item; 60 133 61 134 *args = args_zero; 62 63 for (argc--,argv++; argc > 0; argc--,argv++) { 64 char *item; 65 if (argv[0][0] != '-') break; 66 67 if (strlen(*argv) <= 1) { 68 argc--; argv++; 69 args->rfp = stdin; 70 break; 71 } 72 73 item = argv[0] + 1; 74 switch (*item++) { 75 case 'b': 135 options_init(opts, argc, argv); 136 while ((opt = options_opt(opts))) { 137 if (strcmp(opt, "b") == 0) { 76 138 args->mrbfile = TRUE; 77 break;78 case 'c':139 } 140 else if (strcmp(opt, "c") == 0) { 79 141 args->check_syntax = TRUE; 80 break; 81 case 'e': 82 if (item[0]) { 83 goto append_cmdline; 84 } 85 else if (argc > 1) { 86 argc--; argv++; 87 item = argv[0]; 88 append_cmdline: 142 } 143 else if (strcmp(opt, "d") == 0) { 144 args->debug = TRUE; 145 } 146 else if (strcmp(opt, "e") == 0) { 147 if ((item = options_arg(opts))) { 89 148 if (!args->cmdline) { 90 size_t buflen; 91 char *buf; 92 93 buflen = strlen(item) + 1; 94 buf = (char *)mrb_malloc(mrb, buflen); 95 memcpy(buf, item, buflen); 96 args->cmdline = buf; 149 args->cmdline = dup_arg_item(mrb, item); 97 150 } 98 151 else { … … 109 162 } 110 163 else { 111 printf("%s: No code specified for -e\n", *origargv); 112 return EXIT_SUCCESS; 113 } 114 break; 115 case 'v': 116 if (!args->verbose) mrb_show_version(mrb); 164 fprintf(stderr, "%s: No code specified for -e\n", opts->program); 165 return EXIT_FAILURE; 166 } 167 } 168 else if (strcmp(opt, "h") == 0) { 169 usage(opts->program); 170 exit(EXIT_SUCCESS); 171 } 172 else if (strcmp(opt, "r") == 0) { 173 if ((item = options_arg(opts))) { 174 if (args->libc == 0) { 175 args->libv = (char**)mrb_malloc(mrb, sizeof(char*)); 176 } 177 else { 178 args->libv = (char**)mrb_realloc(mrb, args->libv, sizeof(char*) * (args->libc + 1)); 179 } 180 args->libv[args->libc++] = dup_arg_item(mrb, item); 181 } 182 else { 183 fprintf(stderr, "%s: No library specified for -r\n", opts->program); 184 return EXIT_FAILURE; 185 } 186 } 187 else if (strcmp(opt, "v") == 0) { 188 if (!args->verbose) { 189 mrb_show_version(mrb); 190 args->version = TRUE; 191 } 117 192 args->verbose = TRUE; 118 break;119 case '-':120 if (strcmp((*argv) + 2, "version") == 0) {121 mrb_show_version(mrb);122 exit(EXIT_SUCCESS);123 }124 else if (strcmp((*argv) + 2, "verbose") == 0) {125 args->verbose = TRUE;126 break;127 }128 e lse if (strcmp((*argv) + 2, "copyright") == 0) {129 mrb_show_copyright(mrb);130 exit(EXIT_SUCCESS);131 }132 default:193 } 194 else if (strcmp(opt, "version") == 0) { 195 mrb_show_version(mrb); 196 exit(EXIT_SUCCESS); 197 } 198 else if (strcmp(opt, "verbose") == 0) { 199 args->verbose = TRUE; 200 } 201 else if (strcmp(opt, "copyright") == 0) { 202 mrb_show_copyright(mrb); 203 exit(EXIT_SUCCESS); 204 } 205 else { 206 fprintf(stderr, "%s: invalid option %s%s (-h will show valid options)\n", 207 opts->program, opt[1] ? "--" : "-", opt); 133 208 return EXIT_FAILURE; 134 209 } 135 210 } 136 211 137 if (args->rfp == NULL && args->cmdline == NULL) { 138 if (*argv == NULL) args->rfp = stdin; 139 else { 140 args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r"); 212 argc = opts->argc; argv = opts->argv; 213 if (args->cmdline == NULL) { 214 if (*argv == NULL) { 215 if (args->version) exit(EXIT_SUCCESS); 216 args->rfp = stdin; 217 } 218 else { 219 args->rfp = strcmp(argv[0], "-") == 0 ? 220 stdin : fopen(argv[0], args->mrbfile ? "rb" : "r"); 141 221 if (args->rfp == NULL) { 142 printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);222 fprintf(stderr, "%s: Cannot open program file: %s\n", opts->program, argv[0]); 143 223 return EXIT_FAILURE; 144 224 } … … 163 243 mrb_free(mrb, args->cmdline); 164 244 mrb_free(mrb, args->argv); 245 if (args->libc) { 246 while (args->libc--) { 247 mrb_free(mrb, args->libv[args->libc]); 248 } 249 mrb_free(mrb, args->libv); 250 } 165 251 mrb_close(mrb); 166 252 } … … 179 265 180 266 if (mrb == NULL) { 181 fp uts("Invalid mrb_state, exiting mruby\n", stderr);267 fprintf(stderr, "%s: Invalid mrb_state, exiting mruby\n", *argv); 182 268 return EXIT_FAILURE; 183 269 } … … 186 272 if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) { 187 273 cleanup(mrb, &args); 188 usage(argv[0]);189 274 return n; 190 275 } … … 200 285 } 201 286 mrb_define_global_const(mrb, "ARGV", ARGV); 287 mrb_gv_set(mrb, mrb_intern_lit(mrb, "$DEBUG"), mrb_bool_value(args.debug)); 202 288 203 289 c = mrbc_context_new(mrb); … … 220 306 } 221 307 308 /* Load libraries */ 309 for (i = 0; i < args.libc; i++) { 310 FILE *lfp = fopen(args.libv[i], args.mrbfile ? "rb" : "r"); 311 if (lfp == NULL) { 312 fprintf(stderr, "%s: Cannot open library file: %s\n", *argv, args.libv[i]); 313 mrbc_context_free(mrb, c); 314 cleanup(mrb, &args); 315 return EXIT_FAILURE; 316 } 317 if (args.mrbfile) { 318 v = mrb_load_irep_file_cxt(mrb, lfp, c); 319 } 320 else { 321 v = mrb_load_file_cxt(mrb, lfp, c); 322 } 323 fclose(lfp); 324 } 325 222 326 /* Load program */ 223 327 if (args.mrbfile) { … … 237 341 mrbc_context_free(mrb, c); 238 342 if (mrb->exc) { 239 if (mrb_undef_p(v)) { 240 mrb_p(mrb, mrb_obj_value(mrb->exc)); 241 } 242 else { 343 if (!mrb_undef_p(v)) { 243 344 mrb_print_error(mrb); 244 345 } 245 n = -1;346 n = EXIT_FAILURE; 246 347 } 247 348 else if (args.check_syntax) { 248 p rintf("Syntax OK\n");349 puts("Syntax OK"); 249 350 } 250 351 } 251 352 cleanup(mrb, &args); 252 353 253 return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE;254 } 354 return n; 355 }
Note:
See TracChangeset
for help on using the changeset viewer.