static int compile_rb2mrb(mrb_state *mrb0, const char *code, int code_len, const char *path, FILE* tmpfp) { mrb_state *mrb = mrb_open(); mrb_value result; mrbc_context *c; int ret = -1; int debuginfo = 1; mrb_irep *irep; c = mrbc_context_new(mrb); c->no_exec = 1; if (path != NULL) { mrbc_filename(mrb, c, path); } result = mrb_load_nstring_cxt(mrb, code, code_len, c); if (mrb_undef_p(result)) { mrbc_context_free(mrb, c); mrb_close(mrb); return MRB_DUMP_GENERAL_FAILURE; } irep = mrb_proc_ptr(result)->body.irep; ret = mrb_dump_irep_binary(mrb, irep, debuginfo, tmpfp); mrbc_context_free(mrb, c); mrb_close(mrb); return ret; }
static int load_file(mrb_state *mrb, struct mrbc_args *args) { mrbc_context *c; mrb_value result; char *input = args->argv[args->idx]; FILE *infile; c = mrbc_context_new(mrb); if (args->verbose) c->dump_result = 1; c->no_exec = 1; if (input[0] == '-' && input[1] == '\0') { infile = stdin; } else if ((infile = fopen(input, "r")) == NULL) { fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, input); return EXIT_FAILURE; } mrbc_filename(mrb, c, input); args->idx++; if (args->idx < args->argc) { mrbc_partial_hook(mrb, c, partial_hook, (void*)args); } result = mrb_load_file_cxt(mrb, infile, c); if (mrb_undef_p(result) || mrb_fixnum(result) < 0) { mrbc_context_free(mrb, c); return EXIT_FAILURE; } mrbc_context_free(mrb, c); return EXIT_SUCCESS; }
static struct RProc* create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, char *file, mrb_int line) { mrbc_context *cxt; struct mrb_parser_state *p; struct RProc *proc; struct REnv *e; if (!mrb_nil_p(binding)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "Binding of eval must be nil."); } cxt = mrbc_context_new(mrb); cxt->lineno = line; if (file) { mrbc_filename(mrb, cxt, file); } cxt->capture_errors = TRUE; p = mrb_parse_nstring(mrb, s, len, cxt); /* only occur when memory ran out */ if (!p) { mrb_raise(mrb, E_RUNTIME_ERROR, "Failed to create parser state."); } if (0 < p->nerr) { /* parse error */ char buf[256]; int n; n = snprintf(buf, sizeof(buf), "line %d: %s\n", p->error_buffer[0].lineno, p->error_buffer[0].message); mrb_parser_free(p); mrbc_context_free(mrb, cxt); mrb_exc_raise(mrb, mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, n)); } proc = mrb_generate_code(mrb, p); if (proc == NULL) { /* codegen error */ mrb_parser_free(p); mrbc_context_free(mrb, cxt); mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error"); } if (mrb->c->ci[-1].proc->target_class) { proc->target_class = mrb->c->ci[-1].proc->target_class; } e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, (struct RClass*)mrb->c->ci[-1].proc->env); e->mid = mrb->c->ci[-1].mid; e->cioff = mrb->c->ci - mrb->c->cibase - 1; e->stack = mrb->c->ci->stackent; mrb->c->ci->env = e; proc->env = e; patch_irep(mrb, proc->body.irep, 0); mrb_parser_free(p); mrbc_context_free(mrb, cxt); return proc; }
static mrb_value eval_context_compile(mrb_state *mrb, mrb_value self) { char *script; mrb_int script_length; mrbc_context* compile_ctx; struct mrb_parser_state *parser; struct RProc *proc; mrb_get_args(mrb, "s", &script, &script_length); compile_ctx = mrbc_context_new(mrb); if (!compile_ctx) { mrb_raise(mrb, E_RUNTIME_ERROR, "[mruby][eval][compile] failed to allocate context"); } compile_ctx->capture_errors = TRUE; parser = mrb_parse_nstring(mrb, script, script_length, compile_ctx); if (!parser) { mrbc_context_free(mrb, compile_ctx); mrb_raise(mrb, E_RUNTIME_ERROR, "[mruby][eval][compile] failed to allocate parser"); } if (parser->nerr > 0) { struct mrb_parser_message *error = &(parser->error_buffer[0]); mrb_value new_args[1]; mrb_value exception; new_args[0] = mrb_format(mrb, "line %S:%S: %S", mrb_fixnum_value(error->lineno), mrb_fixnum_value(error->column), mrb_str_new_cstr(mrb, error->message)); exception = mrb_obj_new(mrb, E_SYNTAX_ERROR, 1, new_args); mrb_parser_free(parser); mrbc_context_free(mrb, compile_ctx); mrb_exc_raise(mrb, exception); } proc = mrb_generate_code(mrb, parser); { mrb_code *iseq = proc->body.irep->iseq; while (GET_OPCODE(*iseq) != OP_STOP) { iseq++; } *iseq = MKOP_AB(OP_RETURN, 1, OP_R_NORMAL); } mrb_parser_free(parser); mrbc_context_free(mrb, compile_ctx); return mrb_obj_value(proc); }
int main() { mrb_state *mrb; int n; FILE* f; struct RClass* cObject; mrbc_context *mrbc_ctx; struct mrb_parser_state *p; mrb = mrb_open(); mrbc_ctx = mrbc_context_new(mrb); cObject = mrb_class_obj_get(mrb, "Object"); mrb_define_method(mrb, cObject, "plus", plus, ARGS_ANY()); //mrb_define_singleton_method(mrb, mrb_top_self(mrb), "plus", plus, ARGS_REQ(2)); f = fopen("step4.rb", "r"); if(f==NULL){ _error("file not found."); } p = mrb_parse_file(mrb,f,mrbc_ctx); fclose(f); n = mrb_generate_code(mrb, p); mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value()); mrb_parser_free(p); mrbc_context_free(mrb,mrbc_ctx); mrb_close(mrb); return 0; }
static void load_rb_file(mrb_state *mrb, mrb_value filepath) { FILE *file; char *fpath = RSTRING_PTR(filepath); mrbc_context *mrbc_ctx; { FILE *fp = fopen(fpath, "r"); if (fp == NULL) { mrb_load_fail(mrb, filepath, "cannot load such file"); return; } fclose(fp); } mrbc_ctx = mrbc_context_new(mrb); file = fopen((const char*)fpath, "r"); mrbc_filename(mrb, mrbc_ctx, fpath); mrb_load_file_cxt(mrb, file, mrbc_ctx); fclose(file); mrbc_context_free(mrb, mrbc_ctx); }
static ERL_NIF_TERM eval1(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ErlNifBinary script_binary; if (!enif_inspect_binary(env, argv[0], &script_binary)){ return enif_make_badarg(env); } mrb_state *mrb; mrbc_context *cxt; mrb = mrb_open(); if (mrb == NULL) { return enif_make_atom(env, "error"); } char *script = malloc(script_binary.size+1); script[script_binary.size] = '\0'; strncpy(script, (const char *)script_binary.data, (int)script_binary.size); ERL_NIF_TERM erl_result; cxt = mrbc_context_new(mrb); struct mrb_parser_state* st = mrb_parse_string(mrb, (const char *)script, cxt); int n = mrb_generate_code(mrb, st); mrb_pool_close(st->pool); mrb_value result = mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value()); erl_result = mruby2erl(env, mrb, result); free(script); mrbc_context_free(mrb, cxt); mrb_close(mrb); enif_release_binary(&script_binary); return erl_result; }
void mrb4R_finalize(void) { if(mrb != NULL) { mrbc_context_free(mrb,mrb_cxt); mrb_close(mrb); printf("mruby finalize!!!\n"); } }
static ERL_NIF_TERM eval2(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { ErlNifBinary script_binary; if (!enif_inspect_binary(env, argv[0], &script_binary)){ return enif_make_badarg(env); } if (!enif_is_list(env, argv[1])) { enif_release_binary(&script_binary); return enif_make_badarg(env); } mrb_state *mrb; mrbc_context *cxt; mrb = mrb_open(); if (mrb == NULL) { return enif_make_atom(env, "error"); } unsigned int mrb_argv_len; enif_get_list_length(env, argv[1], &mrb_argv_len); mrb_value mrb_argv = mrb_ary_new(mrb); ERL_NIF_TERM cur; for(cur = argv[1]; !enif_is_empty_list(env, cur); ) { ERL_NIF_TERM head, tail; enif_get_list_cell(env, cur, &head, &tail); mrb_ary_push(mrb, mrb_argv, erl2mruby(env, mrb, head)); cur = tail; } mrb_define_global_const(mrb, "ARGV", mrb_argv); char *script = malloc(script_binary.size+1); strncpy(script, (const char *)script_binary.data, (int)script_binary.size); script[script_binary.size] = '\0'; cxt = mrbc_context_new(mrb); struct mrb_parser_state* st = mrb_parse_string(mrb, (const char *)script, cxt); int n = mrb_generate_code(mrb, st); mrb_pool_close(st->pool); mrb_value result = mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value()); ERL_NIF_TERM erl_result = mruby2erl(env, mrb, result); free(script); mrbc_context_free(mrb, cxt); mrb_close(mrb); enif_release_binary(&script_binary); return erl_result; }
int main(void) { mrb_value response; mrb_state *mrb = mrb_open(); mrbc_context *ctx = mrbc_context_new(mrb); // URL into instance variable "@url" of main on same ctx mrb_iv_set(mrb, mrb_top_self(mrb), mrb_intern_lit(mrb, "@url"), mrb_str_new_lit(mrb, "https://127.0.0.1:8080/index.html")); response = mrb_load_string_cxt(mrb, request, ctx); mrb_funcall(mrb, mrb_top_self(mrb), "pp", 1, response); mrbc_context_free(mrb, ctx); mrb_close(mrb); return 0; }
int webruby_internal_run_source(mrb_state* mrb, const char *s, int print_level) { mrbc_context *c = NULL; int err; if (print_level > 0) { c = mrbc_context_new(mrb); c->dump_result = TRUE; } err = check_and_print_errors(mrb, mrb_load_string_cxt(mrb, s, c), print_level); if (c) { mrbc_context_free(mrb, c); } return err; }
VALUE mruby_eval(VALUE self, VALUE prog) { int n = -1; mrb_state *mrb = mrb_open(); struct mrb_parser_state *p; mrbc_context *c = mrbc_context_new(mrb); mrb_value rc; p = mrb_parse_string(mrb, StringValueCStr(prog), c); n = mrb_generate_code(mrb, p); mrbc_context_free(mrb, c); mrb_pool_close(p->pool); if (n >= 0) { rc = mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value()); } return mruby_cvt_mr2cr(rc); }
int main() { mrb_state *mrb; int n; struct mrb_parser_state *p; mrbc_context *mrbc_ctx; mrb = mrb_open(); mrbc_ctx = mrbc_context_new(mrb); p = mrb_parse_string(mrb, "puts 'hello'; puts 1+2; p({:a=>[1,2,3], :b=>{'c'=>{'d'=>['e', {'f'=>nil}]}}})",mrbc_ctx); n = mrb_generate_code(mrb, p); mrb_parser_free(p); mrbc_context_free(mrb,mrbc_ctx); mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value()); if (mrb->exc) { mrb_p(mrb, mrb_obj_value(mrb->exc)); } mrb_close(mrb); return 0; /* returns 255 otherwise */ }
int ss_exit( ss_info* info ){ ss_mruby_info* mruby = &(info->state.mruby); if ( null != mruby->context ){ mrbc_context_free( mruby->state, mruby->context ); mruby->context = null; } if ( null != mruby->state ){ mrb_close( mruby->state ); mruby->state = null; } if ( 0 < info->state.err ){ info->exit_status = 1; } if ( info->is_verbose ){ SS_PUTS_OUT( "exit_status=%d", info->exit_status ); } return info->exit_status; }
s.run \n\ "; int main(void) { mrb_state *mrb = mrb_open(); mrbc_context *ctx = mrbc_context_new(mrb); // 8080 into instance variable "@port" of main on same ctx mrb_iv_set(mrb, mrb_top_self(mrb), mrb_intern_lit(mrb, "@port"), mrb_fixnum_value(8080)); mrb_iv_set(mrb, mrb_top_self(mrb), mrb_intern_lit(mrb, "@echo_content"), mrb_str_new_lit(mrb, "world from cb.")); mrb_load_string_cxt(mrb, config, ctx); mrb_load_string_cxt(mrb, response, ctx); mrb_load_string_cxt(mrb, run, ctx); mrbc_context_free(mrb, ctx); mrb_close(mrb); return 0; }
static mrb_value load_file(mrb_state *mrb, struct mrbc_args *args) { mrbc_context *c; mrb_value result; char *input = args->argv[args->idx]; FILE *infile; mrb_bool need_close = FALSE; c = mrbc_context_new(mrb); if (args->verbose) c->dump_result = TRUE; c->no_exec = TRUE; if (input[0] == '-' && input[1] == '\0') { infile = stdin; } else { need_close = TRUE; if ((infile = fopen(input, "r")) == NULL) { fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, input); return mrb_nil_value(); } } mrbc_filename(mrb, c, input); args->idx++; if (args->idx < args->argc) { need_close = FALSE; mrbc_partial_hook(mrb, c, partial_hook, (void*)args); } result = mrb_load_file_cxt(mrb, infile, c); if (need_close) fclose(infile); mrbc_context_free(mrb, c); if (mrb_undef_p(result)) { return mrb_nil_value(); } return result; }
int main() { mrb_state *mrb = mrb_open(); init_TestClass(mrb); char code[] = "$t = Test.new; res = $t.run; p res"; printf("Executing Ruby code from C!\n"); auto c = mrbc_context_new(mrb); auto p = mrb_parse_string(mrb, code, c); auto n = mrb_generate_code(mrb, p); // mrb_run(mrb, n, mrb_top_self(mrb)); unsigned stack_keep = 0; auto result = mrb_vm_run(mrb, n, mrb_top_self(mrb), stack_keep); if (mrb->exc) // have exception { mrb_p(mrb, mrb_obj_value(mrb->exc)); mrb->exc = 0; } else { if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))) result = mrb_any_to_s(mrb, result); mrb_p(mrb, result); } mrbc_context_free(mrb, c); mrb_close(mrb); return 0; }
int main(int argc, char **argv) { mrb_state *mrb = mrb_open(); int n = -1; int i; struct _args args; mrb_value ARGV; if (mrb == NULL) { fprintf(stderr, "Invalid mrb_state, exiting mruby"); return EXIT_FAILURE; } n = parse_args(mrb, argc, argv, &args); if (n < 0 || (args.cmdline == NULL && args.rfp == NULL)) { cleanup(mrb, &args); usage(argv[0]); return n; } ARGV = mrb_ary_new_capa(mrb, args.argc); for (i = 0; i < args.argc; i++) { mrb_ary_push(mrb, ARGV, mrb_str_new(mrb, args.argv[i], strlen(args.argv[i]))); } mrb_define_global_const(mrb, "ARGV", ARGV); if (args.mrbfile) { n = mrb_load_irep(mrb, args.rfp); if (n >= 0) { if (!args.check_syntax) { mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); if (mrb->exc) { p(mrb, mrb_obj_value(mrb->exc)); } } } } else { mrbc_context *c = mrbc_context_new(mrb); mrb_value v; if (args.verbose) c->dump_result = 1; if (args.check_syntax) c->no_exec = 1; if (args.rfp) { mrbc_filename(mrb, c, args.cmdline ? args.cmdline : "-"); v = mrb_load_file_cxt(mrb, args.rfp, c); } else { mrbc_filename(mrb, c, "-e"); v = mrb_load_string_cxt(mrb, args.cmdline, c); } mrbc_context_free(mrb, c); if (mrb->exc) { if (!mrb_undef_p(v)) { p(mrb, mrb_obj_value(mrb->exc)); } n = -1; } else if (args.check_syntax) { printf("Syntax OK\n"); } } cleanup(mrb, &args); return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE; }
int main(int argc, char **argv) { char ruby_code[1024] = { 0 }; char last_code_line[1024] = { 0 }; #ifndef ENABLE_READLINE int last_char; int char_index; #else char *home = NULL; #endif mrbc_context *cxt; struct mrb_parser_state *parser; mrb_state *mrb; mrb_value result; struct _args args; int n; mrb_bool code_block_open = FALSE; mrb_value MIRB_BIN; int ai; unsigned int stack_keep = 0; /* new interpreter instance */ mrb = mrb_open(); if (mrb == NULL) { fputs("Invalid mrb interpreter, exiting mirb\n", stderr); return EXIT_FAILURE; } mrb_define_global_const(mrb, "ARGV", mrb_ary_new_capa(mrb, 0)); n = parse_args(mrb, argc, argv, &args); if (n == EXIT_FAILURE) { cleanup(mrb, &args); usage(argv[0]); return n; } print_hint(); cxt = mrbc_context_new(mrb); cxt->capture_errors = 1; cxt->lineno = 1; mrbc_filename(mrb, cxt, "(mirb)"); if (args.verbose) cxt->dump_result = 1; MIRB_BIN= mrb_str_new(mrb, argv[0], strlen(argv[0])); mrb_define_global_const(mrb, "MIRB_BIN", MIRB_BIN); #ifdef ENABLE_REQUIRE mrb_value LOAD_PATH = mrb_gv_get(mrb, mrb_intern(mrb, "$:")); if (mrb_str_cmp(mrb, MIRB_BIN, mrb_str_new2(mrb, "mirb")) != 0) { int len = strrchr(RSTRING_PTR(MIRB_BIN), '/') - RSTRING_PTR(MIRB_BIN); mrb_value extdir = mrb_str_substr(mrb, mrb_str_dup(mrb, MIRB_BIN), 0, len); mrb_str_cat2(mrb, extdir, "/../ext"); if (mrb_obj_eq(mrb, mrb_file_exist(mrb, extdir), mrb_true_value())) { mrb_ary_push(mrb, LOAD_PATH, extdir); } } #endif /* ENABLE_REQUIRE */ ai = mrb_gc_arena_save(mrb); #ifdef ENABLE_READLINE MIRB_USING_HISTORY(); home = getenv("HOME"); #ifdef _WIN32 if (!home) home = getenv("USERPROFILE"); #endif if (home) { strcpy(history_path, home); strcat(history_path, "/"); strcat(history_path, history_file_name); MIRB_READ_HISTORY(history_path); } #endif while (TRUE) { #ifndef ENABLE_READLINE print_cmdline(code_block_open); char_index = 0; while ((last_char = getchar()) != '\n') { if (last_char == EOF) break; last_code_line[char_index++] = last_char; } if (last_char == EOF) { fputs("\n", stdout); break; } last_code_line[char_index] = '\0'; #else char* line = MIRB_READLINE(code_block_open ? "* " : "> "); if (line == NULL) { printf("\n"); break; } strncpy(last_code_line, line, sizeof(last_code_line)-1); MIRB_ADD_HISTORY(line); free(line); #endif if (code_block_open) { strcat(ruby_code, "\n"); strcat(ruby_code, last_code_line); } else { if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) { break; } strcpy(ruby_code, last_code_line); } /* parse code */ parser = mrb_parser_new(mrb); parser->s = ruby_code; parser->send = ruby_code + strlen(ruby_code); parser->lineno = cxt->lineno; mrb_parser_parse(parser, cxt); code_block_open = is_code_block_open(parser); if (code_block_open) { /* no evaluation of code */ } else { if (0 < parser->nerr) { /* syntax error */ printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message); } else { /* generate bytecode */ struct RProc *proc = mrb_generate_code(mrb, parser); if (args.verbose) { mrb_codedump_all(mrb, proc); } /* pass a proc for evaulation */ /* evaluate the bytecode */ result = mrb_context_run(mrb, proc, mrb_top_self(mrb), stack_keep); stack_keep = proc->body.irep->nlocals; /* did an exception occur? */ if (mrb->exc) { p(mrb, mrb_obj_value(mrb->exc), 0); mrb->exc = 0; } else { /* no */ if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))){ result = mrb_any_to_s(mrb,result); } p(mrb, result, 1); } } ruby_code[0] = '\0'; last_code_line[0] = '\0'; mrb_gc_arena_restore(mrb, ai); } mrb_parser_free(parser); cxt->lineno++; } mrbc_context_free(mrb, cxt); mrb_close(mrb); #ifdef ENABLE_READLINE MIRB_WRITE_HISTORY(history_path); #endif return 0; }
MRuby::~MRuby() { mrbc_context_free(mMrb, mContext); mrb_close(mMrb); }
int main(int argc, char **argv) { mrb_state *mrb = mrb_open(); int n = -1; int i; struct _args args; mrb_value ARGV; mrbc_context *c; mrb_value v; mrb_sym zero_sym; struct mrb_jmpbuf c_jmp; int ai; if (mrb == NULL) { fputs("Invalid mrb_state, exiting mruby\n", stderr); return EXIT_FAILURE; } n = parse_args(mrb, argc, argv, &args); if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) { cleanup(mrb, &args); usage(argv[0]); return n; } ai = mrb_gc_arena_save(mrb); MRB_TRY(&c_jmp) { mrb->jmp = &c_jmp; ARGV = mrb_ary_new_capa(mrb, args.argc); for (i = 0; i < args.argc; i++) { char* utf8 = mrb_utf8_from_locale(args.argv[i], -1); if (utf8) { mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, utf8)); mrb_utf8_free(utf8); } } mrb_define_global_const(mrb, "ARGV", ARGV); c = mrbc_context_new(mrb); if (args.verbose) c->dump_result = TRUE; if (args.check_syntax) c->no_exec = TRUE; /* Set $0 */ zero_sym = mrb_intern_lit(mrb, "$0"); if (args.rfp) { const char *cmdline; cmdline = args.cmdline ? args.cmdline : "-"; mrbc_filename(mrb, c, cmdline); mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, cmdline)); } else { mrbc_filename(mrb, c, "-e"); mrb_gv_set(mrb, zero_sym, mrb_str_new_lit(mrb, "-e")); } /* Load program */ if (args.mrbfile) { v = mrb_load_irep_file_cxt(mrb, args.rfp, c); } else if (args.rfp) { v = mrb_load_file_cxt(mrb, args.rfp, c); } else { char* utf8 = mrb_utf8_from_locale(args.cmdline, -1); if (!utf8) abort(); v = mrb_load_string_cxt(mrb, utf8, c); mrb_utf8_free(utf8); } mrb_gc_arena_restore(mrb, ai); mrbc_context_free(mrb, c); if (mrb->exc) { if (!mrb_undef_p(v)) { mrb_print_error(mrb); } n = -1; } else if (args.check_syntax) { printf("Syntax OK\n"); } } MRB_CATCH(&c_jmp) { /* error */ } MRB_END_EXC(&c_jmp); cleanup(mrb, &args); return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE; }
static struct RProc* create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding, const char *file, mrb_int line) { mrbc_context *cxt; struct mrb_parser_state *p; struct RProc *proc; struct REnv *e; mrb_callinfo *ci = &mrb->c->ci[-1]; /* callinfo of eval caller */ struct RClass *target_class = NULL; int bidx; if (!mrb_nil_p(binding)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "Binding of eval must be nil."); } cxt = mrbc_context_new(mrb); cxt->lineno = (short)line; mrbc_filename(mrb, cxt, file ? file : "(eval)"); cxt->capture_errors = TRUE; cxt->no_optimize = TRUE; p = mrb_parse_nstring(mrb, s, len, cxt); /* only occur when memory ran out */ if (!p) { mrb_raise(mrb, E_RUNTIME_ERROR, "Failed to create parser state."); } if (0 < p->nerr) { /* parse error */ mrb_value str; if (file) { str = mrb_format(mrb, " file %S line %S: %S", mrb_str_new_cstr(mrb, file), mrb_fixnum_value(p->error_buffer[0].lineno), mrb_str_new_cstr(mrb, p->error_buffer[0].message)); } else { str = mrb_format(mrb, " line %S: %S", mrb_fixnum_value(p->error_buffer[0].lineno), mrb_str_new_cstr(mrb, p->error_buffer[0].message)); } mrb_parser_free(p); mrbc_context_free(mrb, cxt); mrb_exc_raise(mrb, mrb_exc_new_str(mrb, E_SYNTAX_ERROR, str)); } proc = mrb_generate_code(mrb, p); if (proc == NULL) { /* codegen error */ mrb_parser_free(p); mrbc_context_free(mrb, cxt); mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error"); } target_class = MRB_PROC_TARGET_CLASS(ci->proc); if (!MRB_PROC_CFUNC_P(ci->proc)) { if (ci->env) { e = ci->env; } else { e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, (struct RClass*)target_class); e->mid = ci->mid; e->stack = ci[1].stackent; e->cxt = mrb->c; MRB_ENV_SET_STACK_LEN(e, ci->proc->body.irep->nlocals); bidx = ci->argc; if (ci->argc < 0) bidx = 2; else bidx += 1; MRB_ENV_SET_BIDX(e, bidx); ci->env = e; } proc->e.env = e; proc->flags |= MRB_PROC_ENVSET; mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e); } proc->upper = ci->proc; mrb->c->ci->target_class = target_class; patch_irep(mrb, proc->body.irep, 0, proc->body.irep); /* mrb_codedump_all(mrb, proc); */ mrb_parser_free(p); mrbc_context_free(mrb, cxt); return proc; }
int main(int argc, char **argv) { mrb_state *mrb = mrb_open(); int n = -1; struct _args args; mrb_value v; mrdb_state *mrdb; mrdb_state *mrdb_backup; mrb_debug_context* dbg_backup; debug_command *cmd; l_restart: if (mrb == NULL) { fputs("Invalid mrb_state, exiting mruby\n", stderr); return EXIT_FAILURE; } /* parse command parameters */ n = parse_args(mrb, argc, argv, &args); if (n == EXIT_FAILURE || args.rfp == NULL) { cleanup(mrb, &args); usage(argv[0]); return n; } /* initialize debugger information */ mrdb = mrdb_state_get(mrb); mrb_assert(mrdb && mrdb->dbg); mrdb->srcpath = args.srcpath; if(mrdb->dbg->xm == DBG_QUIT) { mrdb->dbg->xphase = DBG_PHASE_RESTART; } else { mrdb->dbg->xphase = DBG_PHASE_BEFORE_RUN; } mrdb->dbg->xm = DBG_INIT; mrdb->dbg->ccnt = 1; /* setup hook functions */ mrb->code_fetch_hook = mrb_code_fetch_hook; mrdb->dbg->break_hook = mrb_debug_break_hook; if (args.mrbfile) { /* .mrb */ v = mrb_load_irep_file(mrb, args.rfp); } else { /* .rb */ mrbc_context *cc = mrbc_context_new(mrb); mrbc_filename(mrb, cc, args.fname); v = mrb_load_file_cxt(mrb, args.rfp, cc); mrbc_context_free(mrb, cc); } if (mrdb->dbg->xm == DBG_QUIT && !mrb_undef_p(v) && mrb->exc) { const char *classname = mrb_obj_classname(mrb, mrb_obj_value(mrb->exc)); if (!strcmp(classname, "DebuggerExit")) { cleanup(mrb, &args); return 0; } if (!strcmp(classname, "DebuggerRestart")) { mrdb_backup = mrdb_state_get(mrb); dbg_backup = mrb_debug_context_get(mrb); mrdb_state_set(NULL); mrb_debug_context_set(NULL); cleanup(mrb, &args); mrb = mrb_open(); mrdb_state_set(mrdb_backup); mrb_debug_context_set(dbg_backup); goto l_restart; } } puts("mruby application exited."); mrdb->dbg->xphase = DBG_PHASE_AFTER_RUN; if (!mrb_undef_p(v)) { if (mrb->exc) { mrb_print_error(mrb); } else { printf(" => "); mrb_p(mrb, v); } } mrdb->dbg->prvfile = "-"; mrdb->dbg->prvline = 0; while (1) { cmd = get_and_parse_command(mrb, mrdb); mrb_assert(cmd); if (cmd->id == DBGCMD_QUIT) { break; } if( cmd->func(mrb, mrdb) == DBGST_RESTART ) goto l_restart; } cleanup(mrb, &args); return 0; }
int main(void) { char ruby_code[1024] = { 0 }; char last_code_line[1024] = { 0 }; #ifndef ENABLE_READLINE int last_char; int char_index; #endif mrbc_context *cxt; struct mrb_parser_state *parser; mrb_state *mrb; mrb_value result; int n; int code_block_open = FALSE; int ai; print_hint(); /* new interpreter instance */ mrb = mrb_open(); if (mrb == NULL) { fprintf(stderr, "Invalid mrb interpreter, exiting mirb"); return EXIT_FAILURE; } cxt = mrbc_context_new(mrb); cxt->capture_errors = 1; ai = mrb_gc_arena_save(mrb); while (TRUE) { #ifndef ENABLE_READLINE print_cmdline(code_block_open); char_index = 0; while ((last_char = getchar()) != '\n') { if (last_char == EOF) break; last_code_line[char_index++] = last_char; } if (last_char == EOF) { printf("\n"); break; } last_code_line[char_index] = '\0'; #else char* line = readline(code_block_open ? "* " : "> "); strncat(last_code_line, line, sizeof(last_code_line)-1); add_history(line); free(line); #endif if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) { if (!code_block_open) { break; } else{ /* count the quit/exit commands as strings if in a quote block */ strcat(ruby_code, "\n"); strcat(ruby_code, last_code_line); } } else { if (code_block_open) { strcat(ruby_code, "\n"); strcat(ruby_code, last_code_line); } else { strcpy(ruby_code, last_code_line); } } /* parse code */ parser = mrb_parser_new(mrb); parser->s = ruby_code; parser->send = ruby_code + strlen(ruby_code); parser->lineno = 1; mrb_parser_parse(parser, cxt); code_block_open = is_code_block_open(parser); if (code_block_open) { /* no evaluation of code */ } else { if (0 < parser->nerr) { /* syntax error */ printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message); } else { /* generate bytecode */ n = mrb_generate_code(mrb, parser); /* evaluate the bytecode */ result = mrb_run(mrb, /* pass a proc for evaulation */ mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); /* did an exception occur? */ if (mrb->exc) { p(mrb, mrb_obj_value(mrb->exc)); mrb->exc = 0; } else { /* no */ printf(" => "); p(mrb, result); } } ruby_code[0] = '\0'; last_code_line[0] = '\0'; mrb_parser_free(parser); mrb_gc_arena_restore(mrb, ai); } } mrbc_context_free(mrb, cxt); mrb_close(mrb); return 0; }
void loop() { if (Serial.available() > 0) { char_index = 0; while (true) { if (Serial.available() > 0) { incommingByte = Serial.read(); if (incommingByte == 13) { // New Line last_code_line[char_index] = '\0'; break; } else { last_code_line[char_index++] = incommingByte; Serial.write(incommingByte); } } } Serial.println(""); Serial.flush(); is_exit = false; if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) { // Exit Interactive Mode if (!code_block_open) { is_exit = true; } else { /* count the quit/exit commands as strings if in a quote block */ strcat(ruby_code, "\n"); strcat(ruby_code, last_code_line); is_exit = false; } } else { if (code_block_open) { strcat(ruby_code, "\n"); strcat(ruby_code, last_code_line); is_exit = false; } else { strcpy(ruby_code, last_code_line); is_exit = false; } } if (!is_exit) { // Not Exit or Quit! /* parse code */ parser = mrb_parser_new(mrb); parser->s = ruby_code; parser->send = ruby_code + strlen(ruby_code); parser->lineno = 1; mrb_parser_parse(parser, cxt); code_block_open = is_code_block_open(parser); if (code_block_open) { /* no evaluation of code */ } else { if (0 < parser->nerr) { /* syntax error */ Serial.write("Syntax Error: "); Serial.println(parser->error_buffer[0].message); } else { /* generate bytecode */ n = mrb_generate_code(mrb, parser); /* evaluate the bytecode */ result = mrb_run(mrb, /* pass a proc for evaulation */ mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); /* did an exception occur? */ if (mrb->exc) { /* yes */ p(mrb, mrb_obj_value(mrb->exc), 0); mrb->exc = 0; } else { /* no */ if (!mrb_respond_to(mrb,result,mrb_intern(mrb, "inspect"))){ result = mrb_any_to_s(mrb,result); } p(mrb, result, 1); } } ruby_code[0] = '\0'; last_code_line[0] = '\0'; mrb_gc_arena_restore(mrb, ai); } mrb_parser_free(parser); } else { // User Enter 'exit' or 'quit' Serial.println("quit IAS"); ruby_code[0] = '\0'; last_code_line[0] = '\0'; mrb_parser_free(parser); mrbc_context_free(mrb, cxt); mrb_close(mrb); mrb_setup_arduino(); } print_cmdline(code_block_open); } }
int main(int argc, char **argv) { mrb_state *mrb = mrb_open(); int n = -1; int i; struct _args args; mrb_value ARGV; mrbc_context *c; mrb_value v; if (mrb == NULL) { fputs("Invalid mrb_state, exiting mruby\n", stderr); return EXIT_FAILURE; } n = parse_args(mrb, argc, argv, &args); if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) { cleanup(mrb, &args); usage(argv[0]); return n; } ARGV = mrb_ary_new_capa(mrb, args.argc); for (i = 0; i < args.argc; i++) { mrb_ary_push(mrb, ARGV, mrb_str_new(mrb, args.argv[i], strlen(args.argv[i]))); } mrb_define_global_const(mrb, "ARGV", ARGV); c = mrbc_context_new(mrb); if (args.verbose) c->dump_result = TRUE; if (args.check_syntax) c->no_exec = FALSE; if (args.mrbfile) { v = mrb_load_irep_file_cxt(mrb, args.rfp, c); } else { mrb_sym zero_sym = mrb_intern_lit(mrb, "$0"); if (args.rfp) { char *cmdline; cmdline = args.cmdline ? args.cmdline : "-"; mrbc_filename(mrb, c, cmdline); mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, cmdline)); v = mrb_load_file_cxt(mrb, args.rfp, c); } else { mrbc_filename(mrb, c, "-e"); mrb_gv_set(mrb, zero_sym, mrb_str_new_lit(mrb, "-e")); v = mrb_load_string_cxt(mrb, args.cmdline, c); } } mrbc_context_free(mrb, c); if (mrb->exc) { if (!mrb_undef_p(v)) { mrb_print_error(mrb); } n = -1; } else if (args.check_syntax) { printf("Syntax OK\n"); } cleanup(mrb, &args); return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE; }
int main(int argc, char **argv) { char ruby_code[1024] = { 0 }; char last_code_line[1024] = { 0 }; #ifndef ENABLE_READLINE int last_char; int char_index; #else char *home = NULL; #endif mrbc_context *cxt; struct mrb_parser_state *parser; mrb_state *mrb; mrb_value result; struct _args args; int n; mrb_bool code_block_open = FALSE; int ai; mrb_bool first_command = TRUE; unsigned int nregs; /* new interpreter instance */ mrb = mrb_open(); if (mrb == NULL) { fputs("Invalid mrb interpreter, exiting mirb\n", stderr); return EXIT_FAILURE; } mrb_define_global_const(mrb, "ARGV", mrb_ary_new_capa(mrb, 0)); n = parse_args(mrb, argc, argv, &args); if (n == EXIT_FAILURE) { cleanup(mrb, &args); usage(argv[0]); return n; } print_hint(); cxt = mrbc_context_new(mrb); cxt->capture_errors = 1; cxt->lineno = 1; mrbc_filename(mrb, cxt, "(mirb)"); if (args.verbose) cxt->dump_result = 1; ai = mrb_gc_arena_save(mrb); #ifdef ENABLE_READLINE using_history(); home = getenv("HOME"); #ifdef _WIN32 if (!home) home = getenv("USERPROFILE"); #endif if (home) { strcpy(history_path, home); strcat(history_path, "/"); strcat(history_path, history_file_name); read_history(history_path); } #endif while (TRUE) { #ifndef ENABLE_READLINE print_cmdline(code_block_open); char_index = 0; while ((last_char = getchar()) != '\n') { if (last_char == EOF) break; last_code_line[char_index++] = last_char; } if (last_char == EOF) { fputs("\n", stdout); break; } last_code_line[char_index] = '\0'; #else char* line = readline(code_block_open ? "* " : "> "); if (line == NULL) { printf("\n"); break; } strncpy(last_code_line, line, sizeof(last_code_line)-1); add_history(line); free(line); #endif if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) { if (!code_block_open) { break; } else{ /* count the quit/exit commands as strings if in a quote block */ strcat(ruby_code, "\n"); strcat(ruby_code, last_code_line); } } else { if (code_block_open) { strcat(ruby_code, "\n"); strcat(ruby_code, last_code_line); } else { strcpy(ruby_code, last_code_line); } } /* parse code */ parser = mrb_parser_new(mrb); parser->s = ruby_code; parser->send = ruby_code + strlen(ruby_code); parser->lineno = cxt->lineno; mrb_parser_parse(parser, cxt); code_block_open = is_code_block_open(parser); if (code_block_open) { /* no evaluation of code */ } else { if (0 < parser->nerr) { /* syntax error */ printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message); } else { /* generate bytecode */ struct RProc *proc = mrb_generate_code(mrb, parser); if (args.verbose) { mrb_codedump_all(mrb, proc); } /* pass a proc for evaulation */ nregs = first_command ? 0: proc->body.irep->nregs; /* evaluate the bytecode */ result = mrb_context_run(mrb, proc, mrb_top_self(mrb), nregs); /* did an exception occur? */ if (mrb->exc) { p(mrb, mrb_obj_value(mrb->exc), 0); mrb->exc = 0; } else { /* no */ if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))){ result = mrb_any_to_s(mrb,result); } p(mrb, result, 1); } } ruby_code[0] = '\0'; last_code_line[0] = '\0'; mrb_gc_arena_restore(mrb, ai); } mrb_parser_free(parser); cxt->lineno++; first_command = FALSE; } mrbc_context_free(mrb, cxt); mrb_close(mrb); #ifdef ENABLE_READLINE write_history(history_path); #endif return 0; }