Skip to content

Commit

Permalink
Avoid using mrb_closure_new()
Browse files Browse the repository at this point in the history
Workarounds are no longer required, by mruby/mruby@9ee7edc .
  • Loading branch information
dearblue committed Dec 15, 2023
1 parent e186a1f commit 9bef16a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/http/ngx_http_mruby_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ mrb_value ngx_mrb_start_fiber(ngx_http_request_t *r, mrb_state *mrb, struct RPro
ctx = ngx_mrb_http_get_module_ctx(mrb, r);
ctx->async_handler_result = result;

handler_proc = mrb_closure_new(mrb, rproc->body.irep);
handler_proc = rproc;
handler_proc->upper = NULL;
handler_proc->e.target_class = mrb->object_class;
fiber_proc = (mrb_value *)ngx_palloc(r->pool, sizeof(mrb_value));
*fiber_proc = mrb_fiber_new(mrb, rproc);
if (mrb->exc) {
Expand Down
30 changes: 12 additions & 18 deletions src/http/ngx_http_mruby_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "ngx_http_mruby_init.h"
#include "ngx_http_mruby_request.h"

#include <mruby/proc.h>
#include <mruby/string.h>

#define ON 1
Expand Down Expand Up @@ -910,7 +911,7 @@ ngx_int_t ngx_mrb_run(ngx_http_request_t *r, ngx_mrb_state_t *state, ngx_mrb_cod
static ngx_int_t ngx_http_mruby_state_reinit_from_file(ngx_mrb_state_t *state, ngx_mrb_code_t *code)
{
FILE *mrb_file;
struct mrb_parser_state *p;
mrb_value proc;

if ((mrb_file = fopen((char *)code->code.file, "r")) == NULL) {
return NGX_ERROR;
Expand All @@ -919,18 +920,15 @@ static ngx_int_t ngx_http_mruby_state_reinit_from_file(ngx_mrb_state_t *state, n
NGX_MRUBY_CODE_MRBC_CONTEXT_FREE(state->mrb, code);
code->ctx = mrbc_context_new(state->mrb);
mrbc_filename(state->mrb, code->ctx, (char *)code->code.file);
p = mrb_parse_file(state->mrb, mrb_file, code->ctx);
code->ctx->no_exec = TRUE;
proc = mrb_load_file_cxt(state->mrb, mrb_file, code->ctx);
fclose(mrb_file);

if (p == NULL || (0 < p->nerr)) {
if (!mrb_proc_p(proc)) {
return NGX_ERROR;
}

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

return NGX_OK;
}
Expand Down Expand Up @@ -994,10 +992,11 @@ static ngx_int_t ngx_http_mruby_shared_state_init(ngx_mrb_state_t *state)
static ngx_int_t ngx_http_mruby_shared_state_compile(ngx_conf_t *cf, ngx_mrb_state_t *state, ngx_mrb_code_t *code)
{
FILE *mrb_file;
struct mrb_parser_state *p;
mrb_value proc;

NGX_MRUBY_CODE_MRBC_CONTEXT_FREE(state->mrb, code);
code->ctx = mrbc_context_new(state->mrb);
code->ctx->no_exec = TRUE;
#ifdef NGX_MRUBY_IREP_DEBUG
code->ctx->dump_result = TRUE;
#endif
Expand All @@ -1007,23 +1006,18 @@ static ngx_int_t ngx_http_mruby_shared_state_compile(ngx_conf_t *cf, ngx_mrb_sta
return NGX_ERROR;
}
mrbc_filename(state->mrb, code->ctx, (char *)code->code.file);
p = mrb_parse_file(state->mrb, mrb_file, code->ctx);
proc = mrb_load_file_cxt(state->mrb, mrb_file, code->ctx);
fclose(mrb_file);
} else {
mrbc_filename(state->mrb, code->ctx, "INLINE CODE");
p = mrb_parse_string(state->mrb, (char *)code->code.string, code->ctx);
}

if (p == NULL || (0 < p->nerr)) {
return NGX_ERROR;
proc = mrb_load_string_cxt(state->mrb, (char *)code->code.string, code->ctx);
}

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

code->proc = mrb_proc_ptr(proc);
#ifdef NGX_MRUBY_IREP_DEBUG
/* mrb_codedump_all() is not declared in mruby headers. So just follows the mruby way. See mruby/src/load.c. */
void mrb_codedump_all(mrb_state *, struct RProc *);
Expand Down
4 changes: 3 additions & 1 deletion src/stream/ngx_stream_mruby_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ mrb_value ngx_stream_mrb_start_fiber(ngx_stream_session_t *s, mrb_state *mrb, st
ctx = ngx_stream_mrb_get_module_ctx(mrb, s);
ctx->async_handler_result = result;

handler_proc = mrb_closure_new(mrb, rproc->body.irep);
handler_proc = rproc;
handler_proc->upper = NULL;
handler_proc->e.target_class = mrb->object_class;
fiber_proc = (mrb_value *)ngx_palloc(s->connection->pool, sizeof(mrb_value));
*fiber_proc = mrb_fiber_new(mrb, rproc);
if (mrb->exc) {
Expand Down
17 changes: 8 additions & 9 deletions src/stream/ngx_stream_mruby_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "ngx_stream_mruby_init.h"

#include <mruby/proc.h>
#include "mruby/string.h"

#define NGX_MRUBY_CODE_MRBC_CONTEXT_FREE(mrb, code) \
Expand Down Expand Up @@ -361,7 +362,7 @@ static ngx_mrb_code_t *ngx_stream_mruby_mrb_code_from_string(ngx_pool_t *pool, n
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;
mrb_value proc;

if (code->code_type == NGX_MRB_CODE_TYPE_FILE) {
if ((mrb_file = fopen((char *)code->code.file, "r")) == NULL) {
Expand All @@ -370,24 +371,22 @@ static ngx_int_t ngx_stream_mruby_shared_state_compile(ngx_conf_t *cf, mrb_state
NGX_MRUBY_CODE_MRBC_CONTEXT_FREE(mrb, code);
code->ctx = mrbc_context_new(mrb);
mrbc_filename(mrb, code->ctx, (char *)code->code.file);
p = mrb_parse_file(mrb, mrb_file, code->ctx);
code->ctx->no_exec = TRUE;
proc = mrb_load_file_cxt(mrb, mrb_file, code->ctx);
fclose(mrb_file);
} else {
NGX_MRUBY_CODE_MRBC_CONTEXT_FREE(mrb, code);
code->ctx = mrbc_context_new(mrb);
mrbc_filename(mrb, code->ctx, "INLINE CODE");
p = mrb_parse_string(mrb, (char *)code->code.string, code->ctx);
code->ctx->no_exec = TRUE;
proc = mrb_load_string_cxt(mrb, (char *)code->code.string, code->ctx);
}

if (p == NULL || (0 < p->nerr)) {
if (!mrb_proc_p(proc)) {
return NGX_ERROR;
}

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

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,
Expand Down

0 comments on commit 9bef16a

Please sign in to comment.