Exemplo n.º 1
0
Arquivo: mrb.c Projeto: XLPE/groonga
mrb_value
grn_mrb_load(grn_ctx *ctx, const char *path)
{
  grn_mrb_data *data = &(ctx->impl->mrb);
  mrb_state *mrb = data->state;
  char expanded_path[PATH_MAX];
  FILE *file;
  mrb_value result;
  struct mrb_parser_state *parser;

  if (!mrb) {
    return mrb_nil_value();
  }

  if (!grn_mrb_expand_script_path(ctx, path, expanded_path, PATH_MAX)) {
    return mrb_nil_value();
  }

  file = grn_fopen(expanded_path, "r");
  if (!file) {
    mrb_value exception;
    SERR("fopen: failed to open mruby script file: <%s>",
         expanded_path);
    exception = mrb_exc_new(mrb, E_LOAD_ERROR,
                            ctx->errbuf, strlen(ctx->errbuf));
    mrb->exc = mrb_obj_ptr(exception);
    return mrb_nil_value();
  }

  {
    char current_base_directory[PATH_MAX];
    char *last_directory;

    grn_strcpy(current_base_directory, PATH_MAX, data->base_directory);
    grn_strcpy(data->base_directory, PATH_MAX, expanded_path);
    last_directory = strrchr(data->base_directory, '/');
    if (last_directory) {
      last_directory[0] = '\0';
    }

    parser = mrb_parser_new(mrb);
    mrb_parser_set_filename(parser, expanded_path);
    parser->s = parser->send = NULL;
    parser->f = file;
    mrb_parser_parse(parser, NULL);
    fclose(file);

    {
      struct RProc *proc;
      proc = mrb_generate_code(mrb, parser);
      result = mrb_toplevel_run(mrb, proc);
    }
    mrb_parser_free(parser);

    grn_strcpy(data->base_directory, PATH_MAX, current_base_directory);
  }

  return result;
}
Exemplo n.º 2
0
Arquivo: load.c Projeto: retrage/mruby
MRB_API mrb_value
mrb_load_irep_cxt(mrb_state *mrb, const uint8_t *bin, mrbc_context *c)
{
  mrb_irep *irep = mrb_read_irep(mrb, bin);
  struct RProc *proc;

  if (!irep) {
    irep_error(mrb);
    return mrb_nil_value();
  }
  proc = mrb_proc_new(mrb, irep);
  mrb_irep_decref(mrb, irep);
  if (c && c->no_exec) return mrb_obj_value(proc);
  return mrb_toplevel_run(mrb, proc);
}
Exemplo n.º 3
0
mrb_value
mrb_load_irep_file_cxt(mrb_state *mrb, FILE* fp, mrbc_context *c)
{
  mrb_irep *irep = mrb_read_irep_file(mrb, fp);
  mrb_value val;
  struct RProc *proc;

  if (!irep) {
    irep_error(mrb);
    return mrb_nil_value();
  }
  proc = mrb_proc_new(mrb, irep);
  mrb_irep_decref(mrb, irep);
  if (c && c->no_exec) return mrb_obj_value(proc);
  val = mrb_toplevel_run(mrb, proc);
  return val;
}
Exemplo n.º 4
0
static mrb_value
f_eval(mrb_state *mrb, mrb_value self)
{
  char *s;
  mrb_int len;
  mrb_value binding = mrb_nil_value();
  char *file = NULL;
  mrb_int line = 1;
  mrb_value ret;
  struct RProc *proc;

  mrb_get_args(mrb, "s|ozi", &s, &len, &binding, &file, &line);

  proc = create_proc_from_string(mrb, s, len, binding, file, line);
  ret = mrb_toplevel_run(mrb, proc);
  if (mrb->exc) {
    mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));
  }

  return ret;
}