[270] | 1 | /*
|
---|
| 2 | * This file comes from mruby. (renamed from tools/mruby/mruby.c)
|
---|
| 3 | * This file is distributed under ruby's license. (not under TOPPERS license)
|
---|
| 4 | * see https://github.com/mruby/mruby
|
---|
| 5 | * A little modification is done to to call initializer registering TECS components.
|
---|
| 6 | *
|
---|
| 7 | * ãã®ãã¡ã¤ã«ã¯ãmruby ã®ãã®ã§ãã
|
---|
| 8 | * mruby ã®ã©ã¤ã»ã³ã¹ã®ãã¨ã§ãé
|
---|
| 9 | å¸ããã¾ãã(TOPPERS ã©ã¤ã»ã³ã¹ã§ã¯ããã¾ãã)
|
---|
| 10 | * åç
|
---|
| 11 | §ï¼ https://github.com/mruby/mruby
|
---|
| 12 | * TECS ã³ã³ãã¼ãã³ããç»é²ããåæååãå¼ã¶ãããå°ãã®ä¿®æ£ããªããã¦ãã¾ãã
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | #include "nMruby_tMrubyProc_tecsgen.h"
|
---|
| 16 |
|
---|
| 17 | #include "mruby.h"
|
---|
| 18 | #include "mruby/proc.h"
|
---|
| 19 | #include "mruby/array.h"
|
---|
| 20 | #include "mruby/string.h"
|
---|
| 21 | #include "mruby/compile.h"
|
---|
| 22 | #include "mruby/dump.h"
|
---|
| 23 | #include "mruby/variable.h"
|
---|
| 24 | #include <stdio.h>
|
---|
| 25 | #include <string.h>
|
---|
| 26 |
|
---|
| 27 | #ifndef ENABLE_STDIO
|
---|
| 28 | static void
|
---|
| 29 | p(mrb_state *mrb, mrb_value obj)
|
---|
| 30 | {
|
---|
| 31 | obj = mrb_funcall(mrb, obj, "inspect", 0);
|
---|
| 32 | fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout);
|
---|
| 33 | putc('\n', stdout);
|
---|
| 34 | }
|
---|
| 35 | #else
|
---|
| 36 | #define p(mrb,obj) mrb_p(mrb,obj)
|
---|
| 37 | #endif
|
---|
| 38 |
|
---|
| 39 | void mrb_show_version(mrb_state *);
|
---|
| 40 | void mrb_show_copyright(mrb_state *);
|
---|
| 41 |
|
---|
| 42 | struct _args {
|
---|
| 43 | FILE *rfp;
|
---|
| 44 | char* cmdline;
|
---|
| 45 | int fname : 1;
|
---|
| 46 | int mrbfile : 1;
|
---|
| 47 | int check_syntax : 1;
|
---|
| 48 | int verbose : 1;
|
---|
| 49 | int argc;
|
---|
| 50 | char** argv;
|
---|
| 51 | };
|
---|
| 52 |
|
---|
| 53 | static void
|
---|
| 54 | usage(const char *name)
|
---|
| 55 | {
|
---|
| 56 | static const char *const usage_msg[] = {
|
---|
| 57 | "switches:",
|
---|
| 58 | "-b load and execute RiteBinary (mrb) file",
|
---|
| 59 | "-c check syntax only",
|
---|
| 60 | "-e 'command' one line of script",
|
---|
| 61 | "-v print version number, then run in verbose mode",
|
---|
| 62 | "--verbose run in verbose mode",
|
---|
| 63 | "--version print the version",
|
---|
| 64 | "--copyright print the copyright",
|
---|
| 65 | NULL
|
---|
| 66 | };
|
---|
| 67 | const char *const *p = usage_msg;
|
---|
| 68 |
|
---|
| 69 | printf("Usage: %s [switches] programfile\n", name);
|
---|
| 70 | while(*p)
|
---|
| 71 | printf(" %s\n", *p++);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | static int
|
---|
| 75 | parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
|
---|
| 76 | {
|
---|
| 77 | char **origargv = argv;
|
---|
| 78 | static const struct _args args_zero = { 0 };
|
---|
| 79 |
|
---|
| 80 | *args = args_zero;
|
---|
| 81 |
|
---|
| 82 | for (argc--,argv++; argc > 0; argc--,argv++) {
|
---|
| 83 | char *item;
|
---|
| 84 | if (argv[0][0] != '-') break;
|
---|
| 85 |
|
---|
| 86 | if (strlen(*argv) <= 1) {
|
---|
| 87 | argc--; argv++;
|
---|
| 88 | args->rfp = stdin;
|
---|
| 89 | break;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | item = argv[0] + 1;
|
---|
| 93 | switch (*item++) {
|
---|
| 94 | case 'b':
|
---|
| 95 | args->mrbfile = 1;
|
---|
| 96 | break;
|
---|
| 97 | case 'c':
|
---|
| 98 | args->check_syntax = 1;
|
---|
| 99 | break;
|
---|
| 100 | case 'e':
|
---|
| 101 | if (item[0]) {
|
---|
| 102 | goto append_cmdline;
|
---|
| 103 | }
|
---|
| 104 | else if (argc > 1) {
|
---|
| 105 | argc--; argv++;
|
---|
| 106 | item = argv[0];
|
---|
| 107 | append_cmdline:
|
---|
| 108 | if (!args->cmdline) {
|
---|
| 109 | char *buf;
|
---|
| 110 |
|
---|
| 111 | buf = (char *)mrb_malloc(mrb, strlen(item)+1);
|
---|
| 112 | strcpy(buf, item);
|
---|
| 113 | args->cmdline = buf;
|
---|
| 114 | }
|
---|
| 115 | else {
|
---|
| 116 | args->cmdline = (char *)mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(item)+2);
|
---|
| 117 | strcat(args->cmdline, "\n");
|
---|
| 118 | strcat(args->cmdline, item);
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | else {
|
---|
| 122 | printf("%s: No code specified for -e\n", *origargv);
|
---|
| 123 | return 0;
|
---|
| 124 | }
|
---|
| 125 | break;
|
---|
| 126 | case 'v':
|
---|
| 127 | mrb_show_version(mrb);
|
---|
| 128 | args->verbose = 1;
|
---|
| 129 | break;
|
---|
| 130 | case '-':
|
---|
| 131 | if (strcmp((*argv) + 2, "version") == 0) {
|
---|
| 132 | mrb_show_version(mrb);
|
---|
| 133 | exit(0);
|
---|
| 134 | }
|
---|
| 135 | else if (strcmp((*argv) + 2, "verbose") == 0) {
|
---|
| 136 | args->verbose = 1;
|
---|
| 137 | break;
|
---|
| 138 | }
|
---|
| 139 | else if (strcmp((*argv) + 2, "copyright") == 0) {
|
---|
| 140 | mrb_show_copyright(mrb);
|
---|
| 141 | exit(0);
|
---|
| 142 | }
|
---|
| 143 | else return -3;
|
---|
| 144 | return 0;
|
---|
| 145 | default:
|
---|
| 146 | return -4;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | if (args->rfp == NULL && args->cmdline == NULL) {
|
---|
| 151 | if (*argv == NULL) args->rfp = stdin;
|
---|
| 152 | else {
|
---|
| 153 | args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r");
|
---|
| 154 | if (args->rfp == NULL) {
|
---|
| 155 | printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);
|
---|
| 156 | return 0;
|
---|
| 157 | }
|
---|
| 158 | args->fname = 1;
|
---|
| 159 | args->cmdline = argv[0];
|
---|
| 160 | argc--; argv++;
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
|
---|
| 164 | memcpy(args->argv, argv, (argc+1) * sizeof(char*));
|
---|
| 165 | args->argc = argc;
|
---|
| 166 |
|
---|
| 167 | return 0;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | static void
|
---|
| 171 | cleanup(mrb_state *mrb, struct _args *args)
|
---|
| 172 | {
|
---|
| 173 | if (args->rfp && args->rfp != stdin)
|
---|
| 174 | fclose(args->rfp);
|
---|
| 175 | if (args->cmdline && !args->fname)
|
---|
| 176 | mrb_free(mrb, args->cmdline);
|
---|
| 177 | if (args->argv)
|
---|
| 178 | mrb_free(mrb, args->argv);
|
---|
| 179 | mrb_close(mrb);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | static void
|
---|
| 183 | showcallinfo(mrb_state *mrb)
|
---|
| 184 | {
|
---|
| 185 | mrb_callinfo *ci;
|
---|
| 186 | mrb_int ciidx;
|
---|
| 187 | const char *filename, *method, *sep;
|
---|
| 188 | int i, line;
|
---|
| 189 |
|
---|
| 190 | printf("trace:\n");
|
---|
| 191 | ciidx = mrb_fixnum(mrb_obj_iv_get(mrb, mrb->exc, mrb_intern(mrb, "ciidx")));
|
---|
| 192 | if (ciidx >= mrb->ciend - mrb->cibase)
|
---|
| 193 | ciidx = 10; /* ciidx is broken... */
|
---|
| 194 |
|
---|
| 195 | for (i = ciidx; i >= 0; i--) {
|
---|
| 196 | ci = &mrb->cibase[i];
|
---|
| 197 | filename = "(unknown)";
|
---|
| 198 | line = -1;
|
---|
| 199 |
|
---|
| 200 | if (MRB_PROC_CFUNC_P(ci->proc)) {
|
---|
| 201 | continue;
|
---|
| 202 | }
|
---|
| 203 | else {
|
---|
| 204 | mrb_irep *irep = ci->proc->body.irep;
|
---|
| 205 | if (irep->filename != NULL)
|
---|
| 206 | filename = irep->filename;
|
---|
| 207 | if (irep->lines != NULL && i+1 <= ciidx) {
|
---|
| 208 | mrb_code *pc = mrb->cibase[i+1].pc;
|
---|
| 209 | if (irep->iseq <= pc && pc < irep->iseq + irep->ilen) {
|
---|
| 210 | line = irep->lines[pc - irep->iseq - 1];
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | if (ci->target_class == ci->proc->target_class)
|
---|
| 215 | sep = ".";
|
---|
| 216 | else
|
---|
| 217 | sep = "#";
|
---|
| 218 |
|
---|
| 219 | method = mrb_sym2name(mrb, ci->mid);
|
---|
| 220 | printf("\t[%d] %s:%d%s%s%s%s\n",
|
---|
| 221 | i, filename, line,
|
---|
| 222 | method ? ":in " : "",
|
---|
| 223 | method ? mrb_class_name(mrb, ci->proc->target_class) : "",
|
---|
| 224 | method ? sep : "",
|
---|
| 225 | method ? method : "");
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | int
|
---|
| 230 | eMain_main( CELLIDX idx, int argc, const char **argv)
|
---|
| 231 | {
|
---|
| 232 | CELLCB *p_cellcb = GET_CELLCB( idx );
|
---|
| 233 | mrb_state *mrb = mrb_open();
|
---|
| 234 | int n = -1;
|
---|
| 235 | int i;
|
---|
| 236 | struct _args args;
|
---|
| 237 | mrb_value ARGV;
|
---|
| 238 |
|
---|
| 239 | cInit_initializeBridge( mrb );
|
---|
| 240 |
|
---|
| 241 | if (mrb == NULL) {
|
---|
| 242 | fprintf(stderr, "Invalid mrb_state, exiting mruby\n");
|
---|
| 243 | return EXIT_FAILURE;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | n = parse_args(mrb, argc, (char **)argv, &args);
|
---|
| 247 | if (n < 0 || (args.cmdline == NULL && args.rfp == NULL)) {
|
---|
| 248 | cleanup(mrb, &args);
|
---|
| 249 | usage(argv[0]);
|
---|
| 250 | return n;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | ARGV = mrb_ary_new_capa(mrb, args.argc);
|
---|
| 254 | for (i = 0; i < args.argc; i++) {
|
---|
| 255 | mrb_ary_push(mrb, ARGV, mrb_str_new(mrb, args.argv[i], strlen(args.argv[i])));
|
---|
| 256 | }
|
---|
| 257 | mrb_define_global_const(mrb, "ARGV", ARGV);
|
---|
| 258 |
|
---|
| 259 | if (args.mrbfile) {
|
---|
| 260 | n = mrb_load_irep(mrb, args.rfp);
|
---|
| 261 | if (n < 0) {
|
---|
| 262 | fprintf(stderr, "failed to load mrb file: %s\n", args.cmdline);
|
---|
| 263 | }
|
---|
| 264 | else if (!args.check_syntax) {
|
---|
| 265 | mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
|
---|
| 266 | n = 0;
|
---|
| 267 | if (mrb->exc) {
|
---|
| 268 | showcallinfo(mrb);
|
---|
| 269 | p(mrb, mrb_obj_value(mrb->exc));
|
---|
| 270 | n = -1;
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | else {
|
---|
| 275 | mrbc_context *c = mrbc_context_new(mrb);
|
---|
| 276 | mrb_value v;
|
---|
| 277 |
|
---|
| 278 | if (args.verbose)
|
---|
| 279 | c->dump_result = 1;
|
---|
| 280 | if (args.check_syntax)
|
---|
| 281 | c->no_exec = 1;
|
---|
| 282 |
|
---|
| 283 | if (args.rfp) {
|
---|
| 284 | mrbc_filename(mrb, c, args.cmdline ? args.cmdline : "-");
|
---|
| 285 | v = mrb_load_file_cxt(mrb, args.rfp, c);
|
---|
| 286 | }
|
---|
| 287 | else {
|
---|
| 288 | mrbc_filename(mrb, c, "-e");
|
---|
| 289 | v = mrb_load_string_cxt(mrb, args.cmdline, c);
|
---|
| 290 | }
|
---|
| 291 | mrbc_context_free(mrb, c);
|
---|
| 292 | if (mrb->exc) {
|
---|
| 293 | if (!mrb_undef_p(v)) {
|
---|
| 294 | showcallinfo(mrb);
|
---|
| 295 | p(mrb, mrb_obj_value(mrb->exc));
|
---|
| 296 | }
|
---|
| 297 | n = -1;
|
---|
| 298 | }
|
---|
| 299 | else if (args.check_syntax) {
|
---|
| 300 | printf("Syntax OK\n");
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
| 303 | cleanup(mrb, &args);
|
---|
| 304 |
|
---|
| 305 | return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
---|
| 306 | }
|
---|