Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

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  
    11require 'tempfile'
     2require 'open3'
     3
     4def 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
     11end
    212
    313assert('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 <<-])
    816end
    917
     
    1321  system "#{cmd('mrbc')} -g -o #{bin.path} #{script.path}"
    1422  o = `#{cmd('mruby')} -b #{bin.path}`.strip
    15   assert_equal o, '"ok"'
     23  assert_equal '"ok"', o
    1624end
    1725
     
    3038  # one liner
    3139  assert_equal '"-e"', `#{cmd('mruby')} -e #{shellquote('p $0')}`.chomp
     40end
     41
     42assert '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)])
     45end
     46
     47assert('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!
    3252end
    3353
     
    5979  assert_equal 0, $?.exitstatus
    6080end
     81
     82assert('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."])
     85end
     86
     87assert('mruby -d option') do
     88  assert_mruby("false\n", "", true, ["-e", "p $DEBUG"])
     89  assert_mruby("true\n", "", true, ["-dep $DEBUG"])
     90end
     91
     92assert('mruby -e option (no code specified)') do
     93  assert_mruby("", /\A.*: No code specified for -e\n\z/, false, %w[-e])
     94end
     95
     96assert('mruby -h option') do
     97  assert_mruby(/\AUsage: #{Regexp.escape cmd("mruby")} .*/m, "", true, %w[-h])
     98end
     99
     100assert('mruby -r option') do
     101  lib = Tempfile.new('lib.rb')
     102  lib.write <<EOS
     103class Hoge
     104  def hoge
     105    :hoge
     106  end
     107end
     108EOS
     109  lib.flush
     110
     111  script = Tempfile.new('test.rb')
     112  script.write <<EOS
     113print Hoge.new.hoge
     114EOS
     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
     121end
     122
     123assert('mruby -r option (no library specified)') do
     124  assert_mruby("", /\A.*: No library specified for -r\n\z/, false, %w[-r])
     125end
     126
     127assert('mruby -r option (file not found)') do
     128  assert_mruby("", /\A.*: Cannot open library file: .*\n\z/, false, %w[-r _no_exists_])
     129end
     130
     131assert('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)])
     135end
     136
     137assert('mruby --verbose option') do
     138  assert_mruby(/\A[^\n]*NODE.*\n:end\n\z/m, "", true, %w[--verbose -e p(:end)])
     139end
     140
     141assert('mruby --') do
     142  assert_mruby(%{["-x", "1"]\n}, "", true, %w[-e p(ARGV) -- -x 1])
     143end
     144
     145assert('mruby invalid short option') do
     146  assert_mruby("", /\A.*: invalid option -1 .*\n\z/, false, %w[-1])
     147end
     148
     149assert('mruby invalid long option') do
     150  assert_mruby("", /\A.*: invalid option --longopt .*\n\z/, false, %w[--longopt])
     151end
     152
     153assert('unhandled exception') do
     154  assert_mruby("", /\bEXCEPTION\b.*\n\z/, false, %w[-e raise("EXCEPTION")])
     155end
     156
     157assert('program file not found') do
     158  assert_mruby("", /\A.*: Cannot open program file: .*\n\z/, false, %w[_no_exists_])
     159end
     160
     161assert('codegen error') do
     162  code = "def f(#{(1..100).map{|n| "a#{n}"} * ","}); end"
     163  assert_mruby("", /\Acodegen error:.*\n\z/, false, ["-e", code])
     164end
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-bin-mruby/mrbgem.rake

    r331 r439  
    66  spec.add_dependency('mruby-compiler', :core => 'mruby-compiler')
    77  spec.add_dependency('mruby-error', :core => 'mruby-error')
     8  spec.add_test_dependency('mruby-print', :core => 'mruby-print')
    89
    910  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")
    1212  end
    1313end
  • 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
    27#include <stdlib.h>
    38#include <string.h>
    4 #include <mruby.h>
    59#include <mruby/array.h>
    610#include <mruby/compile.h>
     
    812#include <mruby/variable.h>
    913
    10 #ifdef MRB_DISABLE_STDIO
    11 static void
    12 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 #else
    20 #define p(mrb,obj) mrb_p(mrb,obj)
    21 #endif
    22 
    2314struct _args {
    2415  FILE *rfp;
    25   char* cmdline;
     16  char *cmdline;
    2617  mrb_bool fname        : 1;
    2718  mrb_bool mrbfile      : 1;
    2819  mrb_bool check_syntax : 1;
    2920  mrb_bool verbose      : 1;
     21  mrb_bool version      : 1;
     22  mrb_bool debug        : 1;
    3023  int argc;
    31   char** argv;
     24  char **argv;
     25  int libc;
     26  char **libv;
     27};
     28
     29struct options {
     30  int argc;
     31  char **argv;
     32  char *program;
     33  char *opt;
     34  char short_opt[2];
    3235};
    3336
     
    3942  "-b           load and execute RiteBinary (mrb) file",
    4043  "-c           check syntax only",
     44  "-d           set debugging flags (set $DEBUG to true)",
    4145  "-e 'command' one line of script",
     46  "-r library   load the library before executing your script",
    4247  "-v           print version number, then run in verbose mode",
    4348  "--verbose    run in verbose mode",
     
    4853  const char *const *p = usage_msg;
    4954
    50   printf("Usage: %s [switches] programfile\n", name);
     55  printf("Usage: %s [switches] [programfile] [arguments]\n", name);
    5156  while (*p)
    5257    printf("  %s\n", *p++);
    5358}
    5459
     60static void
     61options_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
     69static const char *
     70options_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
     106static const char *
     107options_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
     118static char *
     119dup_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
    55127static int
    56128parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
    57129{
    58   char **origargv = argv;
    59130  static const struct _args args_zero = { 0 };
     131  struct options opts[1];
     132  const char *opt, *item;
    60133
    61134  *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) {
    76138      args->mrbfile = TRUE;
    77       break;
    78     case 'c':
     139    }
     140    else if (strcmp(opt, "c") == 0) {
    79141      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))) {
    89148        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);
    97150        }
    98151        else {
     
    109162      }
    110163      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      }
    117192      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       else 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);
    133208      return EXIT_FAILURE;
    134209    }
    135210  }
    136211
    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");
    141221      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]);
    143223        return EXIT_FAILURE;
    144224      }
     
    163243    mrb_free(mrb, args->cmdline);
    164244  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  }
    165251  mrb_close(mrb);
    166252}
     
    179265
    180266  if (mrb == NULL) {
    181     fputs("Invalid mrb_state, exiting mruby\n", stderr);
     267    fprintf(stderr, "%s: Invalid mrb_state, exiting mruby\n", *argv);
    182268    return EXIT_FAILURE;
    183269  }
     
    186272  if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) {
    187273    cleanup(mrb, &args);
    188     usage(argv[0]);
    189274    return n;
    190275  }
     
    200285    }
    201286    mrb_define_global_const(mrb, "ARGV", ARGV);
     287    mrb_gv_set(mrb, mrb_intern_lit(mrb, "$DEBUG"), mrb_bool_value(args.debug));
    202288
    203289    c = mrbc_context_new(mrb);
     
    220306    }
    221307
     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
    222326    /* Load program */
    223327    if (args.mrbfile) {
     
    237341    mrbc_context_free(mrb, c);
    238342    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)) {
    243344        mrb_print_error(mrb);
    244345      }
    245       n = -1;
     346      n = EXIT_FAILURE;
    246347    }
    247348    else if (args.check_syntax) {
    248       printf("Syntax OK\n");
     349      puts("Syntax OK");
    249350    }
    250351  }
    251352  cleanup(mrb, &args);
    252353
    253   return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
    254 }
     354  return n;
     355}
Note: See TracChangeset for help on using the changeset viewer.