示例#1
0
VALUE test_iseq(VALUE arg)
{
	VALUE v;
	// rb_require("rubygems");
	// rb_protect( (VALUE (*)(VALUE))rb_require, (VALUE) "iseq", &error);
	// perror("require 'iseq'");
	// rb_class_new_instance(argc, argv, klass);
	p(s("OK"));
	ruby_show_version();
	VALUE iseq=eval("$iseq = RubyVM::InstructionSequence.compile('puts 1+4')");
	VALUE rbc=eval("$rbc = $iseq.to_a");
	// VALUE rbc=eval("Marshal.load(File.read('out.dump'))");

	p(rbc);
	v=rb_funcall(iseq,id("eval"),0);// YAY!!!
	// p(rb_cISeq);
	pf("-----\n");
	// iseq=rb_iseq_load(rbc,0, Qnil);
	iseq=(VALUE)rb_iseq_load(rbc,rb_cISeq, Qnil);
	// iseq.h:VALUE rb_iseq_load(VALUE data, VALUE parent, VALUE opt);
	pf("+++++\n");
	pf("%lu\n\n",iseq);
	if(iseq)
		p(iseq);
	pf("///////\n");
	// rb_call(VALUE recv, ID mid, int argc, const VALUE *argv, call_type scope)
	v=rb_funcall(iseq,id("eval"),0);
	pf("-----");
	 // eval("puts 'hellou';arr = Marshal.load(File.read('out.dump'));puts arr;puts 'ok1';require 'iseq';puts ISeq;puts 'ok';x=ISeq.load arr;puts x;");
	return v;
}
示例#2
0
/* load the interpreter and the script */
void startup_myruby(void)
{
  int rc;

  RUBY_INIT_STACK;
  ruby_init();
  ruby_init_loadpath();
  /* dumps the version info to stdout */
  ruby_show_version();
  /* set working dir for includes */
  rb_eval_string_protect("$: << '.'", &rc); /* NOTRANSLATE */
#if USE_RUBYVERSION < 19
#else
  rb_enc_find_index("encdb"); /* NOTRANSLATE */
#endif

  myruby_loaded = 0;

  //define that callback below
  rb_define_global_function("iroffer_input", cie_inputline, 0); /* NOTRANSLATE */
  rb_define_global_function("iroffer_privmsg", cie_privmsg, 2); /* NOTRANSLATE */
  Init_IrofferEvent();
  load_script(gdata.ruby_script);
}
示例#3
0
static int
parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
{
  char *infile = NULL;
  char *outfile = NULL;
  char **origargv = argv;

  memset(args, 0, sizeof(*args));
  args->ext = RITEBIN_EXT;

  for (argc--,argv++; argc > 0; argc--,argv++) {
    if (**argv == '-') {
      if (strlen(*argv) <= 1)
        return -1;

      switch ((*argv)[1]) {
      case 'o':
        outfile = get_outfilename((*argv) + 2, "");
        break;
      case 'B':
      case 'C':
        args->ext = C_EXT;
        args->initname = (*argv) + 2;
        if (*args->initname == '\0') {
          printf("%s: Function name is not specified.\n", *origargv);
          return -2;
        }
        args->dump_type = ((*argv)[1] == 'B') ? DUMP_TYPE_BIN : DUMP_TYPE_CODE;
        break;
      case 'c':
        args->check_syntax = 1;
        break;
      case 'v':
        ruby_show_version(mrb);
        args->verbose = 1;
        break;
      case '-':
        if (strcmp((*argv) + 2, "version") == 0) {
          ruby_show_version(mrb);
        }
        else if (strcmp((*argv) + 2, "verbose") == 0) {
          args->verbose = 1;
          break;
        }
        else if (strcmp((*argv) + 2, "copyright") == 0) {
          ruby_show_copyright(mrb);
        }
        else return -3;
        return 0;
      }
    }
    else if (args->rfp == NULL) {
      infile = *argv;
      if ((args->rfp = fopen(infile, "r")) == NULL) {
        printf("%s: Cannot open program file. (%s)\n", *origargv, infile);
        return 0;
      }
    }
  }

  if (infile == NULL)
    return -4;
  if (args->check_syntax)
    return 0;

  if (outfile == NULL)
    outfile = get_outfilename(infile, args->ext);

  if ((args->wfp = fopen(outfile, "wb")) == NULL) {
    printf("%s: Cannot open output file. (%s)\n", *origargv, outfile);
    return 0;
  }

  return 0;
}
示例#4
0
文件: ruby.c 项目: Netfart/rhodes
static int
proc_options(int argc, char **argv, struct cmdline_options *opt, int envopt)
{
    int n, argc0 = argc;
    const char *s;

    if (argc == 0)
	return 0;

    for (argc--, argv++; argc > 0; argc--, argv++) {
	const char *const arg = argv[0];
	if (arg[0] != '-' || !arg[1])
	    break;

	s = arg + 1;
      reswitch:
	switch (*s) {
	  case 'a':
	    if (envopt) goto noenvopt;
	    opt->do_split = Qtrue;
	    s++;
	    goto reswitch;

	  case 'p':
	    if (envopt) goto noenvopt;
	    opt->do_print = Qtrue;
	    /* through */
	  case 'n':
	    if (envopt) goto noenvopt;
	    opt->do_loop = Qtrue;
	    s++;
	    goto reswitch;

	  case 'd':
	    ruby_debug = Qtrue;
	    ruby_verbose = Qtrue;
	    s++;
	    goto reswitch;

	  case 'y':
	    if (envopt) goto noenvopt;
	    opt->yydebug = 1;
	    s++;
	    goto reswitch;

	  case 'v':
	    if (opt->verbose) {
		s++;
		goto reswitch;
	    }
	    ruby_show_version();
	    opt->verbose = 1;
	  case 'w':
	    ruby_verbose = Qtrue;
	    s++;
	    goto reswitch;

	  case 'W':
	    {
		int numlen;
		int v = 2;	/* -W as -W2 */

		if (*++s) {
		    v = scan_oct(s, 1, &numlen);
		    if (numlen == 0)
			v = 1;
		    s += numlen;
		}
		switch (v) {
		  case 0:
		    ruby_verbose = Qnil;
		    break;
		  case 1:
		    ruby_verbose = Qfalse;
		    break;
		  default:
		    ruby_verbose = Qtrue;
		    break;
		}
	    }
	    goto reswitch;

	  case 'c':
	    if (envopt) goto noenvopt;
	    opt->do_check = Qtrue;
	    s++;
	    goto reswitch;

	  case 's':
	    if (envopt) goto noenvopt;
	    forbid_setid("-s");
	    opt->sflag = 1;
	    s++;
	    goto reswitch;

	  case 'h':
	    if (envopt) goto noenvopt;
	    usage(origarg.argv[0]);
	    rb_exit(EXIT_SUCCESS);
	    break;

	  case 'l':
	    if (envopt) goto noenvopt;
	    opt->do_line = Qtrue;
	    rb_output_rs = rb_rs;
	    s++;
	    goto reswitch;

	  case 'S':
	    if (envopt) goto noenvopt;
	    forbid_setid("-S");
	    opt->do_search = Qtrue;
	    s++;
	    goto reswitch;

	  case 'e':
	    if (envopt) goto noenvopt;
	    forbid_setid("-e");
	    if (!*++s) {
		s = argv[1];
		argc--, argv++;
	    }
	    if (!s) {
		rb_raise(rb_eRuntimeError, "no code specified for -e");
	    }
	    if (!opt->e_script) {
		opt->e_script = rb_str_new(0, 0);
		if (opt->script == 0)
		    opt->script = "-e";
	    }
	    rb_str_cat2(opt->e_script, s);
	    rb_str_cat2(opt->e_script, "\n");
	    break;

	  case 'r':
	    forbid_setid("-r");
	    if (*++s) {
		add_modules(opt, s);
	    }
	    else if (argv[1]) {
		add_modules(opt, argv[1]);
		argc--, argv++;
	    }
	    break;

	  case 'i':
	    if (envopt) goto noenvopt;
	    forbid_setid("-i");
	    ruby_set_inplace_mode(s + 1);
	    break;

	  case 'x':
	    if (envopt) goto noenvopt;
	    opt->xflag = Qtrue;
	    s++;
	    if (*s && chdir(s) < 0) {
		rb_fatal("Can't chdir to %s", s);
	    }
	    break;

	  case 'C':
	  case 'X':
	    if (envopt) goto noenvopt;
	    s++;
	    if (!*s) {
		s = argv[1];
		argc--, argv++;
	    }
	    if (!s || !*s) {
		rb_fatal("Can't chdir");
	    }
	    if (chdir(s) < 0) {
		rb_fatal("Can't chdir to %s", s);
	    }
	    break;

	  case 'F':
	    if (envopt) goto noenvopt;
	    if (*++s) {
		rb_fs = rb_reg_new(s, strlen(s), 0);
	    }
	    break;

	  case 'E':
	    if (!*++s && (!--argc || !(s = *++argv))) {
		rb_raise(rb_eRuntimeError, "missing argument for -E");
	    }
	    goto encoding;

	  case 'U':
	    set_internal_encoding_once(opt, "UTF-8", 0);
	    ++s;
	    goto reswitch;

	  case 'K':
	    if (*++s) {
		const char *enc_name = 0;
		switch (*s) {
		  case 'E': case 'e':
		    enc_name = "EUC-JP";
		    break;
		  case 'S': case 's':
		    enc_name = "Windows-31J";
		    break;
		  case 'U': case 'u':
		    enc_name = "UTF-8";
		    break;
		  case 'N': case 'n': case 'A': case 'a':
		    enc_name = "ASCII-8BIT";
		    break;
		}
		if (enc_name) {
		    opt->src.enc.name = rb_str_new2(enc_name);
		    if (!opt->ext.enc.name)
			opt->ext.enc.name = opt->src.enc.name;
		}
		s++;
	    }
	    goto reswitch;

	  case 'T':
	    {
		int numlen;
		int v = 1;

		if (*++s) {
		    v = scan_oct(s, 2, &numlen);
		    if (numlen == 0)
			v = 1;
		    s += numlen;
		}
		if (v > opt->safe_level) opt->safe_level = v;
	    }
	    goto reswitch;

	  case 'I':
	    forbid_setid("-I");
	    if (*++s)
		ruby_incpush_expand(s);
	    else if (argv[1]) {
		ruby_incpush_expand(argv[1]);
		argc--, argv++;
	    }
	    break;
    //RHO
	  case 'R':
        forbid_setid("-R");
	      if (*++s)
	    	  opt->rootPath = rb_str_new(s, strlen(s));//ruby_incpush_expand(s);
	      else if (argv[1]) {
    		  opt->rootPath = rb_str_new(argv[1],strlen(s));//ruby_incpush_expand(argv[1]);

		    argc--, argv++;
	    }
	    break;
    //RHO
	  case '0':
	    if (envopt) goto noenvopt;
	    {
		int numlen;
		int v;
		char c;

		v = scan_oct(s, 4, &numlen);
		s += numlen;
		if (v > 0377)
		    rb_rs = Qnil;
		else if (v == 0 && numlen >= 2) {
		    rb_rs = rb_str_new2("\n\n");
		}
		else {
		    c = v & 0xff;
		    rb_rs = rb_str_new(&c, 1);
		}
	    }
	    goto reswitch;

	  case '-':
	    if (!s[1] || (s[1] == '\r' && !s[2])) {
		argc--, argv++;
		goto switch_end;
	    }
	    s++;

#	define is_option_end(c, allow_hyphen) \
	    (!(c) || (allow_hyphen && (c) == '-') || (c) == '=')
#	define check_envopt(name, allow_envopt) \
	    ((allow_envopt || !envopt) ? (void)0 : \
	     rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --" name))
#	define need_argument(name, s) \
	    ((*s++ ? !*s : (!--argc || !(s = *++argv))) ?		\
	     rb_raise(rb_eRuntimeError, "missing argument for --" name) \
	     : (void)0)
#	define is_option_with_arg(name, allow_hyphen, allow_envopt) \
	    (strncmp(name, s, n = sizeof(name) - 1) == 0 && is_option_end(s[n], allow_hyphen) ? \
	     (check_envopt(name, allow_envopt), s += n, need_argument(name, s), 1) : 0)

	    if (strcmp("copyright", s) == 0) {
		if (envopt) goto noenvopt_long;
		opt->copyright = 1;
	    }
	    else if (strcmp("debug", s) == 0) {
		ruby_debug = Qtrue;
                ruby_verbose = Qtrue;
            }
	    else if (is_option_with_arg("enable", Qtrue, Qtrue)) {
		ruby_each_words(s, enable_option, &opt->disable);
	    }
	    else if (is_option_with_arg("disable", Qtrue, Qtrue)) {
		ruby_each_words(s, disable_option, &opt->disable);
	    }
	    else if (is_option_with_arg("encoding", Qfalse, Qtrue)) {
		char *p;
	      encoding:
		do {
#	define set_encoding_part(type) \
		    if (!(p = strchr(s, ':'))) { \
			set_##type##_encoding_once(opt, s, 0); \
			break; \
		    } \
		    else if (p > s) { \
			set_##type##_encoding_once(opt, s, p-s); \
		    }
		    set_encoding_part(external);
		    if (!*(s = ++p)) break;
		    set_encoding_part(internal);
		    if (!*(s = ++p)) break;
		    rb_raise(rb_eRuntimeError, "extra argument for %s: %s",
			     (arg[1] == '-' ? "--encoding" : "-E"), s);
#	undef set_encoding_part
		} while (0);
	    }
	    else if (is_option_with_arg("internal-encoding", Qfalse, Qtrue)) {
		set_internal_encoding_once(opt, s, 0);
	    }
	    else if (is_option_with_arg("external-encoding", Qfalse, Qtrue)) {
		set_external_encoding_once(opt, s, 0);
	    }
	    else if (strcmp("version", s) == 0) {
		if (envopt) goto noenvopt_long;
		opt->version = 1;
	    }
	    else if (strcmp("verbose", s) == 0) {
		opt->verbose = 1;
		ruby_verbose = Qtrue;
	    }
	    else if (strcmp("yydebug", s) == 0) {
		if (envopt) goto noenvopt_long;
		opt->yydebug = 1;
	    }
	    else if (is_option_with_arg("dump", Qfalse, Qfalse)) {
		ruby_each_words(s, dump_option, &opt->dump);
	    }
	    else if (strcmp("help", s) == 0) {
		if (envopt) goto noenvopt_long;
		usage(origarg.argv[0]);
		rb_exit(EXIT_SUCCESS);
	    }
	    else {
		rb_raise(rb_eRuntimeError,
			 "invalid option --%s  (-h will show valid options)", s);
	    }
	    break;

	  case '\r':
	    if (!s[1])
		break;

	  default:
	    {
		if (ISPRINT(*s)) {
                    rb_raise(rb_eRuntimeError,
			"invalid option -%c  (-h will show valid options)",
                        (int)(unsigned char)*s);
		}
		else {
                    rb_raise(rb_eRuntimeError,
			"invalid option -\\x%02X  (-h will show valid options)",
                        (int)(unsigned char)*s);
		}
	    }
	    goto switch_end;

	  noenvopt:
	    /* "EIdvwWrKU" only */
	    rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: -%c", *s);
	    break;

	  noenvopt_long:
	    rb_raise(rb_eRuntimeError, "invalid switch in RUBYOPT: --%s", s);
	    break;

	  case 0:
	    break;
#	undef is_option_end
#	undef check_envopt
#	undef need_argument
#	undef is_option_with_arg
	}
    }

  switch_end:
    return argc0 - argc;
}