| 1 | /* |
| 2 | Copyright(C) 2016 Brazil |
| 3 | Copyright(C) 2019-2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "../grn_ctx_impl.h" |
| 21 | |
| 22 | #ifdef GRN_WITH_MRUBY |
| 23 | #include <mruby.h> |
| 24 | #include <mruby/proc.h> |
| 25 | #include <mruby/compile.h> |
| 26 | #include <mruby/opcode.h> |
| 27 | |
| 28 | #include "../grn_mrb.h" |
| 29 | #include "mrb_ctx.h" |
| 30 | #include "mrb_eval_context.h" |
| 31 | |
| 32 | static mrb_value |
| 33 | eval_context_compile(mrb_state *mrb, mrb_value self) |
| 34 | { |
| 35 | char *script; |
| 36 | mrb_int script_length; |
| 37 | mrbc_context* compile_ctx; |
| 38 | struct mrb_parser_state *parser; |
| 39 | struct RProc *proc; |
| 40 | |
| 41 | mrb_get_args(mrb, "s", &script, &script_length); |
| 42 | |
| 43 | compile_ctx = mrbc_context_new(mrb); |
| 44 | if (!compile_ctx) { |
| 45 | mrb_raise(mrb, E_RUNTIME_ERROR, |
| 46 | "[mruby][eval][compile] failed to allocate context"); |
| 47 | } |
| 48 | compile_ctx->capture_errors = TRUE; |
| 49 | |
| 50 | parser = mrb_parse_nstring(mrb, script, (size_t)script_length, compile_ctx); |
| 51 | if (!parser) { |
| 52 | mrbc_context_free(mrb, compile_ctx); |
| 53 | mrb_raise(mrb, E_RUNTIME_ERROR, |
| 54 | "[mruby][eval][compile] failed to allocate parser"); |
| 55 | } |
| 56 | if (parser->nerr > 0) { |
| 57 | struct mrb_parser_message *error = &(parser->error_buffer[0]); |
| 58 | mrb_value new_args[1]; |
| 59 | mrb_value exception; |
| 60 | |
| 61 | new_args[0] = mrb_format(mrb, |
| 62 | "line %S:%S: %S", |
| 63 | mrb_int_value(mrb, error->lineno), |
| 64 | mrb_int_value(mrb, error->column), |
| 65 | mrb_str_new_cstr(mrb, error->message)); |
| 66 | exception = mrb_obj_new(mrb, E_SYNTAX_ERROR, 1, new_args); |
| 67 | mrb_parser_free(parser); |
| 68 | mrbc_context_free(mrb, compile_ctx); |
| 69 | |
| 70 | mrb_exc_raise(mrb, exception); |
| 71 | } |
| 72 | |
| 73 | proc = mrb_generate_code(mrb, parser); |
| 74 | mrb_parser_free(parser); |
| 75 | mrbc_context_free(mrb, compile_ctx); |
| 76 | return mrb_obj_value(proc); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | grn_mrb_eval_context_init(grn_ctx *ctx) |
| 81 | { |
| 82 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 83 | mrb_state *mrb = data->state; |
| 84 | struct RClass *module = data->module; |
| 85 | struct RClass *klass; |
| 86 | |
| 87 | klass = mrb_define_class_under(mrb, module, "EvalContext", mrb->object_class); |
| 88 | |
| 89 | mrb_define_method(mrb, klass, "compile", eval_context_compile, |
| 90 | MRB_ARGS_REQ(1)); |
| 91 | } |
| 92 | #endif |