Beispiel #1
0
// mruby_run for not request phase.
static int ap_mruby_run_nr(const char *mruby_code_file)
{

    int n;
    struct mrb_parser_state* p;
    FILE *mrb_file;
    mrb_state *mrb = mrb_open();
    ap_mruby_class_init(mrb);

    if ((mrb_file = fopen(mruby_code_file, "r")) == NULL) {
        ap_log_error(APLOG_MARK
            , APLOG_ERR
            , 0
            , ap_server_conf
            , "%s ERROR %s: mrb file oepn failed: %s"
            , MODULE_NAME
            , __func__
            , mruby_code_file
        );
        mrb_close(mrb);
        return -1;
    }

   ap_log_error(APLOG_MARK
       , APLOG_DEBUG
       , 0
       , ap_server_conf
       , "%s DEBUG %s: cache nothing on pid %d, compile code: %s"
       , MODULE_NAME
       , __func__
       , getpid()
       , mruby_code_file
   );

    p = mrb_parse_file(mrb, mrb_file, NULL);
    fclose(mrb_file);
    n = mrb_generate_code(mrb, p);

    mrb_pool_close(p->pool);

    ap_log_error(APLOG_MARK
        , APLOG_DEBUG
        , 0
        , ap_server_conf
        , "%s DEBUG %s: run mruby code: %s"
        , MODULE_NAME
        , __func__
        , mruby_code_file
    );

    ap_mrb_set_status_code(OK);
    mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));

    if (mrb->exc)
        ap_mrb_raise_file_error_nr(mrb, mrb_obj_value(mrb->exc), mruby_code_file);

    mrb_close(mrb);

    return APR_SUCCESS;
}
static int cpurate_from_mruby(request_rec *r)
{
    FILE *mrb_file;
    mrm_config_t *conf = ap_get_module_config(r->server->module_config, &resource_manager_module);

    mrb_state *mrb = mrb_open();
    ap_mruby_class_init(mrb);
        if ((mrb_file = fopen(conf->mruby_code, "r")) == NULL) {
            ap_log_error(APLOG_MARK
                , APLOG_ERR
                , 0
                , NULL
                , "%s ERROR %s: mrb file oepn failed: %s"
                , MODULE_NAME
                , __func__
                , conf->mruby_code
            );
        }
    struct mrb_parser_state* p = mrb_parse_file(mrb, mrb_file, NULL);
    int n = mrb_generate_code(mrb, p->tree);
    mrb_pool_close(p->pool);
    ap_mrb_push_request(r);
    mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value());

    return OK;
}
static ngx_int_t ngx_mrb_init_file(char *code_file_path, size_t len, ngx_mrb_state_t *state)
{
    FILE *mrb_file;
    mrb_state *mrb;
    struct mrb_parser_state *p;

    if ((mrb_file = fopen((char *)code_file_path, "r")) == NULL) {
        return NGX_ERROR;
    }

    mrb = mrb_open();
    ngx_mrb_class_init(mrb);

    state->ai  = mrb_gc_arena_save(mrb);
    p          = mrb_parse_file(mrb, mrb_file, NULL);
    state->mrb = mrb;
    state->n   = mrb_generate_code(mrb, p);

    ngx_cpystrn((u_char *)state->code.file, (u_char *)code_file_path, len + 1);
    state->code_type = NGX_MRB_CODE_TYPE_FILE;
    mrb_pool_close(p->pool);
    fclose(mrb_file);

    return NGX_OK;
}
Beispiel #4
0
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;
}
Beispiel #5
0
static void mod_mruby_compile_code(mrb_state *mrb, mod_mruby_code_t *c, server_rec *s)
{
    struct mrb_parser_state* p;
    FILE *mrb_file;
    TRACER;

    if (c != NULL) {
        if (c->type == MOD_MRUBY_STRING) {
            p = mrb_parse_string(mrb, c->code, NULL);
            c->irep_idx_start = mrb_generate_code(mrb, p);
            c->irep_idx_end = mrb->irep_len - 1;
            ap_log_error(APLOG_MARK
                , APLOG_DEBUG
                , 0
                , s
                , "%s DEBUG %s: mruby code string compiled: string=[%s] from irep_idx_start=[%d] to irep_idx_end=[%d]"
                , MODULE_NAME
                , __func__
                , c->code
                , c->irep_idx_start
                , c->irep_idx_end
            );
        } else if (c->type == MOD_MRUBY_FILE) {
            if ((mrb_file = fopen(c->path, "r")) == NULL) {
                ap_log_error(APLOG_MARK
                    , APLOG_ERR
                    , 0
                    , s
                    , "%s ERROR %s: mrb file oepn failed: %s"
                    , MODULE_NAME
                    , __func__
                    , c->path
                );
                return;
            }
            p = mrb_parse_file(mrb, mrb_file, NULL);
            fclose(mrb_file);
            c->irep_idx_start = mrb_generate_code(mrb, p);
            c->irep_idx_end = mrb->irep_len - 1;
            ap_log_error(APLOG_MARK
                , APLOG_DEBUG
                , 0
                , s
                , "%s DEBUG %s: mruby code file compiled: path=[%s]from irep_idx_start=[%d] to irep_idx_end=[%d]"
                , MODULE_NAME
                , __func__
                , c->path
                , c->irep_idx_start
                , c->irep_idx_end
            );
        } else {
            return;
        }
        mrb_pool_close(p->pool);
    }
}
Beispiel #6
0
int
main(int argc, char **argv)
{
  mrb_state *mrb = mrb_open();
  int n = -1;
  struct _args args;
  struct mrb_parser_state *p;

  n = parse_args(mrb, argc, argv, &args);

  if (n < 0 || args.rfp == NULL) {
    cleanup(&args);
    usage(argv[0]);
    mrb_close(mrb);
    return n;
  }

  p = mrb_parse_file(mrb, args.rfp);
  if (!p || !p->tree || p->nerr) {
    cleanup(&args);
    mrb_close(mrb);
    return -1;
  }

  if (args.verbose)
    parser_dump(mrb, p->tree, 0);

  n = mrb_generate_code(mrb, p->tree);
  mrb_pool_close(p->pool);

  if (args.verbose)
    codedump_all(mrb, n);

  if (n < 0 || args.check_syntax) {
    cleanup(&args);
    mrb_close(mrb);
    return n;
  }
  if (args.initname) {
    if (args.dump_type == DUMP_TYPE_BIN)
      n = mrb_bdump_irep(mrb, n, args.wfp, args.initname);
    else
      n = mrb_cdump_irep(mrb, n, args.wfp, args.initname);
  }
  else {
    n = mrb_dump_irep(mrb, n, args.wfp);
  }

  cleanup(&args);
  mrb_close(mrb);

  return n;
}
static ngx_int_t ngx_http_mruby_gencode_state(ngx_http_mruby_state_t *state, ngx_http_mruby_code_t *code)
{
    FILE *mrb_file;
    struct mrb_parser_state *p;

    if ((mrb_file = fopen((char *)code->code.file, "r")) == NULL) {
        return NGX_ERROR;
    }

    state->ai = mrb_gc_arena_save(state->mrb);
    p         = mrb_parse_file(state->mrb, mrb_file, NULL);
    code->n   = mrb_generate_code(state->mrb, p);

    mrb_pool_close(p->pool);
    fclose(mrb_file);

    return NGX_OK;
}
Beispiel #8
0
int exec(int argc, char* argv[])
{
    mrb_state* mrb = mrb_open();

    if(mrb == 0)
    {
        printf("cannot open mruby\n");
        return -2;
    }

    // クラスを定義する
    RClass* my_class = mrb_define_class(mrb, "MyClass", mrb->object_class);

    // メソッドを定義する
    mrb_define_method(mrb, my_class, "print_name", my_class_print_name, ARGS_NONE());

    for(int i = 0; i < argc; ++i)
    {
        FILE* fp = std::fopen(argv[i], "r");
        if(fp == 0)
        {
            printf("cannot open file \"%s\"\n", argv[i]);
            continue;
        }

        mrb_parser_state* parser       = mrb_parse_file(mrb, fp);
        int               byte_code    = mrb_generate_code(mrb, parser->tree);
        mrb_value         return_value = mrb_run( mrb,
                                                  mrb_proc_new(mrb, mrb->irep[byte_code]),
                                                  mrb_top_self(mrb)
                                                );
        if(mrb->exc) // if exception occurred ...
        {
            mrb_p(mrb, return_value);
            mrb->exc = 0;
        }

        std::fclose(fp);
    }

    mrb_close(mrb);

    return 0;
}
ngx_int_t ngx_http_mruby_shared_state_compile(ngx_http_mruby_state_t *state, ngx_http_mruby_code_t *code)
{
    FILE *mrb_file;
    struct mrb_parser_state *p;

    if (code->code_type == NGX_HTTP_MRUBY_CODE_TYPE_FILE) {
        if ((mrb_file = fopen((char *)code->code.file, "r")) == NULL) {
            return NGX_ERROR;
        }
        p = mrb_parse_file(state->mrb, mrb_file, NULL);
        fclose(mrb_file);
    } else {
        p = mrb_parse_string(state->mrb, (char *)code->code.string, NULL);
    }

    code->n = mrb_generate_code(state->mrb, p);
    mrb_pool_close(p->pool);

    return NGX_OK;
}
static ngx_int_t ngx_stream_mruby_shared_state_compile(ngx_conf_t *cf, mrb_state *mrb, ngx_mrb_code_t *code)
{
  FILE *mrb_file;
  struct mrb_parser_state *p;

  if (code->code_type == NGX_MRB_CODE_TYPE_FILE) {
    if ((mrb_file = fopen((char *)code->code.file, "r")) == NULL) {
      return NGX_ERROR;
    }

    code->ctx = mrbc_context_new(mrb);
    mrbc_filename(mrb, code->ctx, (char *)code->code.file);
    p = mrb_parse_file(mrb, mrb_file, code->ctx);
    fclose(mrb_file);
  } else {
    code->ctx = mrbc_context_new(mrb);
    mrbc_filename(mrb, code->ctx, "INLINE CODE");
    p = mrb_parse_string(mrb, (char *)code->code.string, code->ctx);
  }

  if (p == NULL) {
    return NGX_ERROR;
  }

  code->proc = mrb_generate_code(mrb, p);
  mrb_pool_close(p->pool);
  if (code->proc == NULL) {
    return NGX_ERROR;
  }

  if (code->code_type == NGX_MRB_CODE_TYPE_FILE) {
    ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, "%s NOTICE %s:%d: compile info: code->code.file=(%s)", MODULE_NAME,
                       __func__, __LINE__, code->code.file);
  } else {
    ngx_conf_log_error(NGX_LOG_NOTICE, cf, 0, "%s NOTICE %s:%d: compile info: "
                                              "code->code.string=(%s)",
                       MODULE_NAME, __func__, __LINE__, code->code.string);
  }

  return NGX_OK;
}
Beispiel #11
0
int exec(int argc, char* argv[])
{
    mrb_state* mrb = mrb_open();

    if(mrb == 0)
    {
        printf("cannot open mruby\n");
        return -2;
    }

    for(int i = 0; i < argc; ++i)
    {
        FILE* fp = std::fopen(argv[i], "r");
        if(fp == 0)
        {
            printf("cannot open file \"%s\"\n", argv[i]);
            continue;
        }

        mrb_parser_state* parser       = mrb_parse_file(mrb, fp);
        int               byte_code    = mrb_generate_code(mrb, parser->tree);
        mrb_value         return_value = mrb_run( mrb,
                                                  mrb_proc_new(mrb, mrb->irep[byte_code]),
                                                  mrb_top_self(mrb)
                                                );
        if(mrb->exc) // if exception occurred ...
        {
            mrb_p(mrb, return_value);
            mrb->exc = 0;
        }

        std::fclose(fp);
    }

    mrb_close(mrb);

    return 0;
}
mrb_value
mrb_load_file_cxt(mrb_state *mrb, FILE *f, mrbc_context *c)
{
    return load_exec(mrb, mrb_parse_file(mrb, f, c), c);
}