Debian Codegraph @

lib/mrb/mrb_command.c

raw symbols SCIP
1 /*
2 Copyright(C) 2015 Brazil
3 Copyright(C) 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 #include <groonga/command.h>
22
23 #ifdef GRN_WITH_MRUBY
24 #include <mruby.h>
25 #include <mruby/class.h>
26 #include <mruby/data.h>
27 #include <mruby/value.h>
28 #include <mruby/string.h>
29
30 #include "mrb_ctx.h"
31 #include "mrb_command.h"
32
33 static struct mrb_data_type mrb_grn_command_type = {
34 "Groonga::Command",
35 NULL
36 };
37
38 mrb_value
39 mrb_grn_command_instantiate(grn_ctx *ctx, grn_obj *command)
40 {
41 grn_mrb_data *data = &(ctx->impl->mrb);
42 mrb_state *mrb = data->state;
43 struct RClass *module = data->module;
44 char name[GRN_TABLE_MAX_KEY_SIZE];
45 int name_size;
46 mrb_value mrb_name;
47 struct RClass *command_class;
48 struct RClass *target_command_class;
49 mrb_value mrb_target_command_class;
50 mrb_value mrb_arguments[1];
51
52 name_size = grn_obj_name(ctx, command, name, GRN_TABLE_MAX_KEY_SIZE);
53 mrb_name = mrb_str_new(mrb, name, name_size);
54
55 command_class = mrb_class_get_under(mrb, module, "Command");
56 mrb_target_command_class = mrb_funcall(mrb,
57 mrb_obj_value(command_class),
58 "find_class", 1, mrb_name);
59 if (mrb_nil_p(mrb_target_command_class)) {
60 target_command_class = command_class;
61 } else {
62 target_command_class = mrb_class_ptr(mrb_target_command_class);
63 }
64 mrb_arguments[0] = mrb_cptr_value(mrb, command);
65 return mrb_obj_new(mrb, target_command_class, 1, mrb_arguments);
66 }
67
68 static void
69 mrb_grn_command_run_wrapper(grn_ctx *ctx,
70 grn_obj *command,
71 grn_command_input *input,
72 void *user_data)
73 {
74 grn_mrb_data *data = &(ctx->impl->mrb);
75 mrb_state *mrb = data->state;
76 struct RClass *module = data->module;
77 int arena_index;
78 mrb_value mrb_command;
79 mrb_value mrb_input;
80
81 arena_index = mrb_gc_arena_save(mrb);
82 mrb_command = mrb_grn_command_instantiate(ctx, command);
83 {
84 struct RClass *command_input_class;
85 mrb_value mrb_arguments[1];
86 command_input_class = mrb_class_get_under(mrb, module, "CommandInput");
87 mrb_arguments[0] = mrb_cptr_value(mrb, input);
88 mrb_input = mrb_obj_new(mrb, command_input_class, 1, mrb_arguments);
89 }
90 mrb_funcall(mrb, mrb_command, "run_internal", 1, mrb_input);
91 if (ctx->rc == GRN_SUCCESS && mrb->exc) {
92 char name[GRN_TABLE_MAX_KEY_SIZE];
93 int name_size;
94 name_size = grn_obj_name(ctx, command, name, GRN_TABLE_MAX_KEY_SIZE);
95 if (mrb->exc == mrb->nomem_err) {
96 MERR("failed to allocate memory in mruby: <%.*s>",
97 name_size, name);
98 } else {
99 mrb_value reason;
100 reason = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
101 ERR(GRN_COMMAND_ERROR,
102 "failed to run command: <%*.s>: %.*s",
103 name_size, name,
104 (int)RSTRING_LEN(reason), RSTRING_PTR(reason));
105 }
106 }
107 mrb_gc_arena_restore(mrb, arena_index);
108 }
109
110 static mrb_value
111 mrb_grn_command_class_register(mrb_state *mrb, mrb_value klass)
112 {
113 grn_ctx *ctx = (grn_ctx *)mrb->ud;
114 mrb_value mrb_name;
115 mrb_value *mrb_arguments;
116 mrb_int n_arguments;
117
118 mrb_get_args(mrb, "Sa", &mrb_name, &mrb_arguments, &n_arguments);
119
120 {
121 grn_expr_var *vars;
122 mrb_int i;
123
124 for (i = 0; i < n_arguments; i++) {
125 mrb_arguments[i] = mrb_convert_type(mrb, mrb_arguments[i],
126 MRB_TT_STRING, "String", "to_str");
127 }
128 vars = GRN_MALLOCN(grn_expr_var, (size_t)n_arguments);
129 for (i = 0; i < n_arguments; i++) {
130 mrb_value mrb_argument = mrb_arguments[i];
131 grn_expr_var *var = &vars[i];
132 var->name = RSTRING_PTR(mrb_argument);
133 var->name_size = RSTRING_LEN(mrb_argument);
134 GRN_TEXT_INIT(&(var->value), 0);
135 }
136
137 grn_command_register(ctx,
138 RSTRING_PTR(mrb_name),
139 RSTRING_LEN(mrb_name),
140 mrb_grn_command_run_wrapper,
141 vars,
142 (unsigned int)n_arguments,
143 NULL);
144
145 for (i = 0; i < n_arguments; i++) {
146 grn_expr_var *var = &vars[i];
147 GRN_OBJ_FIN(ctx, &(var->value));
148 }
149 GRN_FREE(vars);
150 }
151
152 grn_mrb_ctx_check(mrb);
153
154 {
155 grn_mrb_data *data = &(ctx->impl->mrb);
156 struct RClass *command_class;
157 command_class = mrb_class_get_under(mrb, data->module, "Command");
158 mrb_funcall(mrb,
159 mrb_obj_value(command_class),
160 "register_class", 2, mrb_name, klass);
161 }
162
163 return mrb_nil_value();
164 }
165
166 static mrb_value
167 mrb_grn_command_initialize(mrb_state *mrb, mrb_value self)
168 {
169 mrb_value mrb_command_ptr;
170
171 mrb_get_args(mrb, "o", &mrb_command_ptr);
172 DATA_TYPE(self) = &mrb_grn_command_type;
173 DATA_PTR(self) = mrb_cptr(mrb_command_ptr);
174 return self;
175 }
176
177 void
178 grn_mrb_command_init(grn_ctx *ctx)
179 {
180 grn_mrb_data *data = &(ctx->impl->mrb);
181 mrb_state *mrb = data->state;
182 struct RClass *module = data->module;
183 struct RClass *procedure_class;
184 struct RClass *klass;
185
186 procedure_class = mrb_class_get_under(mrb, module, "Procedure");
187 klass = mrb_define_class_under(mrb, module, "Command", procedure_class);
188 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
189
190 mrb_define_class_method(mrb, klass, "register",
191 mrb_grn_command_class_register,
192 MRB_ARGS_REQ(2));
193
194 mrb_define_method(mrb, klass, "initialize",
195 mrb_grn_command_initialize, MRB_ARGS_REQ(1));
196 }
197 #endif