| 1 | /* |
| 2 | Copyright(C) 2013-2018 Brazil |
| 3 | Copyright(C) 2018-2020 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 | #include <string.h> |
| 23 | |
| 24 | #ifdef GRN_WITH_MRUBY |
| 25 | # include "grn_ctx_impl_mrb.h" |
| 26 | |
| 27 | # include "grn_encoding.h" |
| 28 | # include "grn_mrb.h" |
| 29 | # include "mrb/mrb_converter.h" |
| 30 | # include "mrb/mrb_error.h" |
| 31 | # include "mrb/mrb_id.h" |
| 32 | # include "mrb/mrb_operator.h" |
| 33 | # include "mrb/mrb_command_version.h" |
| 34 | # include "mrb/mrb_ctx.h" |
| 35 | # include "mrb/mrb_logger.h" |
| 36 | # include "mrb/mrb_query_logger.h" |
| 37 | # include "mrb/mrb_void.h" |
| 38 | # include "mrb/mrb_bulk.h" |
| 39 | # include "mrb/mrb_pointer.h" |
| 40 | # include "mrb/mrb_cache.h" |
| 41 | # include "mrb/mrb_object.h" |
| 42 | # include "mrb/mrb_object_flags.h" |
| 43 | # include "mrb/mrb_database.h" |
| 44 | # include "mrb/mrb_indexable.h" |
| 45 | # include "mrb/mrb_table.h" |
| 46 | # include "mrb/mrb_table_with_key.h" |
| 47 | # include "mrb/mrb_array.h" |
| 48 | # include "mrb/mrb_hash_table.h" |
| 49 | # include "mrb/mrb_patricia_trie.h" |
| 50 | # include "mrb/mrb_double_array_trie.h" |
| 51 | # include "mrb/mrb_table_group_flags.h" |
| 52 | # include "mrb/mrb_table_group_result.h" |
| 53 | # include "mrb/mrb_table_sort_flags.h" |
| 54 | # include "mrb/mrb_table_sort_key.h" |
| 55 | # include "mrb/mrb_record.h" |
| 56 | # include "mrb/mrb_column.h" |
| 57 | # include "mrb/mrb_fixed_size_column.h" |
| 58 | # include "mrb/mrb_variable_size_column.h" |
| 59 | # include "mrb/mrb_index_column.h" |
| 60 | # include "mrb/mrb_index_cursor.h" |
| 61 | # include "mrb/mrb_type.h" |
| 62 | # include "mrb/mrb_expr.h" |
| 63 | # include "mrb/mrb_accessor.h" |
| 64 | # include "mrb/mrb_procedure.h" |
| 65 | # include "mrb/mrb_command.h" |
| 66 | # include "mrb/mrb_command_input.h" |
| 67 | # include "mrb/mrb_table_cursor.h" |
| 68 | # include "mrb/mrb_table_cursor_flags.h" |
| 69 | # include "mrb/mrb_content_type.h" |
| 70 | # include "mrb/mrb_writer.h" |
| 71 | # include "mrb/mrb_config.h" |
| 72 | # include "mrb/mrb_eval_context.h" |
| 73 | # include "mrb/mrb_thread.h" |
| 74 | # include "mrb/mrb_window_definition.h" |
| 75 | # include "mrb/mrb_window_function_executor.h" |
| 76 | # include "mrb/mrb_locale_output.h" |
| 77 | # include "mrb/mrb_output_columns.h" |
| 78 | # include "mrb/mrb_vector.h" |
| 79 | # include "mrb/mrb_uvector.h" |
| 80 | |
| 81 | # include <mruby/array.h> |
| 82 | # include <mruby/string.h> |
| 83 | # include <mruby/variable.h> |
| 84 | #endif /* GRN_WITH_MRUBY */ |
| 85 | |
| 86 | static bool grn_ctx_impl_mrb_mruby_enabled = true; |
| 87 | |
| 88 | void |
| 89 | grn_ctx_impl_mrb_init_from_env(void) |
| 90 | { |
| 91 | { |
| 92 | char grn_mruby_enabled_env[GRN_ENV_BUFFER_SIZE]; |
| 93 | grn_getenv("GRN_MRUBY_ENABLED", grn_mruby_enabled_env, GRN_ENV_BUFFER_SIZE); |
| 94 | if (grn_mruby_enabled_env[0] && strcmp(grn_mruby_enabled_env, "no") == 0) { |
| 95 | grn_ctx_impl_mrb_mruby_enabled = false; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #ifdef GRN_WITH_MRUBY |
| 101 | static mrb_value |
| 102 | mrb_kernel_load(mrb_state *mrb, mrb_value self) |
| 103 | { |
| 104 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 105 | char *utf8_path; |
| 106 | const char *path; |
| 107 | |
| 108 | mrb_get_args(mrb, "z", &utf8_path); |
| 109 | |
| 110 | path = grn_encoding_convert_to_locale_from_utf8(ctx, utf8_path, -1, NULL); |
| 111 | grn_mrb_load(ctx, path); |
| 112 | grn_encoding_converted_free(ctx, path); |
| 113 | if (mrb->exc) { |
| 114 | mrb_exc_raise(mrb, mrb_obj_value(mrb->exc)); |
| 115 | } |
| 116 | |
| 117 | grn_mrb_ctx_check(mrb); |
| 118 | |
| 119 | return mrb_true_value(); |
| 120 | } |
| 121 | |
| 122 | static mrb_value |
| 123 | mrb_groonga_init(mrb_state *mrb, mrb_value self) |
| 124 | { |
| 125 | grn_ctx *ctx = mrb->ud; |
| 126 | |
| 127 | mrb_undef_class_method(mrb, ctx->impl->mrb.module, "init"); |
| 128 | |
| 129 | mrb_define_class(mrb, "LoadError", mrb_class_get(mrb, "ScriptError")); |
| 130 | mrb_define_method(mrb, |
| 131 | mrb->kernel_module, |
| 132 | "load", |
| 133 | mrb_kernel_load, |
| 134 | MRB_ARGS_REQ(1)); |
| 135 | |
| 136 | { |
| 137 | mrb_value load_path; |
| 138 | const char *plugins_dir; |
| 139 | const char *utf8_plugins_dir; |
| 140 | const char *system_ruby_scripts_dir; |
| 141 | const char *utf8_system_ruby_scripts_dir; |
| 142 | |
| 143 | load_path = mrb_ary_new(mrb); |
| 144 | |
| 145 | plugins_dir = grn_plugin_get_system_plugins_dir(); |
| 146 | utf8_plugins_dir = |
| 147 | grn_encoding_convert_to_utf8_from_locale(ctx, plugins_dir, -1, NULL); |
| 148 | mrb_ary_push(mrb, load_path, mrb_str_new_cstr(mrb, utf8_plugins_dir)); |
| 149 | grn_encoding_converted_free(ctx, utf8_plugins_dir); |
| 150 | |
| 151 | system_ruby_scripts_dir = grn_mrb_get_system_ruby_scripts_dir(ctx); |
| 152 | utf8_system_ruby_scripts_dir = |
| 153 | grn_encoding_convert_to_utf8_from_locale(ctx, |
| 154 | system_ruby_scripts_dir, |
| 155 | -1, |
| 156 | NULL); |
| 157 | mrb_ary_push(mrb, |
| 158 | load_path, |
| 159 | mrb_str_new_cstr(mrb, utf8_system_ruby_scripts_dir)); |
| 160 | grn_encoding_converted_free(ctx, utf8_system_ruby_scripts_dir); |
| 161 | |
| 162 | mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$LOAD_PATH"), load_path); |
| 163 | } |
| 164 | |
| 165 | grn_mrb_load(ctx, "require.rb"); |
| 166 | grn_mrb_load(ctx, "initialize/pre.rb"); |
| 167 | |
| 168 | grn_mrb_converter_init(ctx); |
| 169 | grn_mrb_error_init(ctx); |
| 170 | grn_mrb_id_init(ctx); |
| 171 | grn_mrb_operator_init(ctx); |
| 172 | grn_mrb_command_version_init(ctx); |
| 173 | grn_mrb_ctx_init(ctx); |
| 174 | grn_mrb_logger_init(ctx); |
| 175 | grn_mrb_query_logger_init(ctx); |
| 176 | grn_mrb_void_init(ctx); |
| 177 | grn_mrb_bulk_init(ctx); |
| 178 | grn_mrb_pointer_init(ctx); |
| 179 | grn_mrb_cache_init(ctx); |
| 180 | grn_mrb_object_init(ctx); |
| 181 | grn_mrb_object_flags_init(ctx); |
| 182 | grn_mrb_database_init(ctx); |
| 183 | grn_mrb_indexable_init(ctx); |
| 184 | grn_mrb_table_init(ctx); |
| 185 | grn_mrb_table_with_key_init(ctx); |
| 186 | grn_mrb_array_init(ctx); |
| 187 | grn_mrb_hash_table_init(ctx); |
| 188 | grn_mrb_patricia_trie_init(ctx); |
| 189 | grn_mrb_double_array_trie_init(ctx); |
| 190 | grn_mrb_table_group_flags_init(ctx); |
| 191 | grn_mrb_table_group_result_init(ctx); |
| 192 | grn_mrb_table_sort_flags_init(ctx); |
| 193 | grn_mrb_table_sort_key_init(ctx); |
| 194 | grn_mrb_record_init(ctx); |
| 195 | grn_mrb_column_init(ctx); |
| 196 | grn_mrb_fixed_size_column_init(ctx); |
| 197 | grn_mrb_variable_size_column_init(ctx); |
| 198 | grn_mrb_index_column_init(ctx); |
| 199 | grn_mrb_index_cursor_init(ctx); |
| 200 | grn_mrb_type_init(ctx); |
| 201 | grn_mrb_expr_init(ctx); |
| 202 | grn_mrb_accessor_init(ctx); |
| 203 | grn_mrb_procedure_init(ctx); |
| 204 | grn_mrb_command_init(ctx); |
| 205 | grn_mrb_command_input_init(ctx); |
| 206 | grn_mrb_table_cursor_init(ctx); |
| 207 | grn_mrb_table_cursor_flags_init(ctx); |
| 208 | grn_mrb_content_type_init(ctx); |
| 209 | grn_mrb_writer_init(ctx); |
| 210 | grn_mrb_config_init(ctx); |
| 211 | grn_mrb_eval_context_init(ctx); |
| 212 | grn_mrb_thread_init(ctx); |
| 213 | grn_mrb_window_definition_init(ctx); |
| 214 | grn_mrb_window_function_executor_init(ctx); |
| 215 | grn_mrb_locale_output_init(ctx); |
| 216 | grn_mrb_output_columns_init(ctx); |
| 217 | grn_mrb_vector_init(ctx); |
| 218 | grn_mrb_uvector_init(ctx); |
| 219 | |
| 220 | grn_mrb_load(ctx, "initialize/post.rb"); |
| 221 | |
| 222 | return mrb_nil_value(); |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | grn_ctx_impl_mrb_init_bindings(grn_ctx *ctx) |
| 227 | { |
| 228 | mrb_state *mrb = ctx->impl->mrb.state; |
| 229 | |
| 230 | mrb->ud = ctx; |
| 231 | ctx->impl->mrb.module = mrb_define_module(mrb, "Groonga"); |
| 232 | mrb_define_const(mrb, |
| 233 | ctx->impl->mrb.module, |
| 234 | "ORDER_BY_ESTIMATED_SIZE", |
| 235 | grn_mrb_is_order_by_estimated_size_enabled() |
| 236 | ? mrb_true_value() |
| 237 | : mrb_false_value()); |
| 238 | mrb_define_class_method(mrb, |
| 239 | ctx->impl->mrb.module, |
| 240 | "init", |
| 241 | mrb_groonga_init, |
| 242 | MRB_ARGS_NONE()); |
| 243 | mrb_funcall(mrb, mrb_obj_value(ctx->impl->mrb.module), "init", 0); |
| 244 | } |
| 245 | |
| 246 | # ifndef GRN_WITH_MEMORY_DEBUG |
| 247 | static void * |
| 248 | grn_ctx_impl_mrb_allocf(mrb_state *mrb, void *ptr, size_t size, void *ud) |
| 249 | { |
| 250 | grn_ctx *ctx = ud; |
| 251 | |
| 252 | if (size == 0) { |
| 253 | if (ptr) { |
| 254 | grn_free(ctx, ptr, __FILE__, __LINE__, __FUNCTION__); |
| 255 | } |
| 256 | return NULL; |
| 257 | } else { |
| 258 | if (ptr) { |
| 259 | return grn_realloc(ctx, ptr, size, __FILE__, __LINE__, __FUNCTION__); |
| 260 | } else { |
| 261 | return grn_malloc(ctx, size, __FILE__, __LINE__, __FUNCTION__); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | # endif |
| 266 | |
| 267 | static void |
| 268 | grn_ctx_impl_mrb_init_lazy(grn_ctx *ctx) |
| 269 | { |
| 270 | if (!grn_ctx_impl_mrb_mruby_enabled) { |
| 271 | ctx->impl->mrb.state = NULL; |
| 272 | ctx->impl->mrb.base_directory[0] = '\0'; |
| 273 | ctx->impl->mrb.module = NULL; |
| 274 | ctx->impl->mrb.object_class = NULL; |
| 275 | ctx->impl->mrb.checked_procs = NULL; |
| 276 | ctx->impl->mrb.registered_plugins = NULL; |
| 277 | ctx->impl->mrb.builtin.time_class = NULL; |
| 278 | ctx->impl->mrb.groonga.operator_class = NULL; |
| 279 | } else { |
| 280 | mrb_state *mrb; |
| 281 | # ifdef GRN_WITH_MEMORY_DEBUG |
| 282 | mrb = mrb_open(); |
| 283 | # else |
| 284 | mrb = mrb_open_allocf(grn_ctx_impl_mrb_allocf, ctx); |
| 285 | # endif |
| 286 | ctx->impl->mrb.state = mrb; |
| 287 | ctx->impl->mrb.base_directory[0] = '\0'; |
| 288 | grn_ctx_impl_mrb_init_bindings(ctx); |
| 289 | if (ctx->impl->mrb.state->exc) { |
| 290 | mrb_value reason; |
| 291 | reason = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0); |
| 292 | ERR(GRN_UNKNOWN_ERROR, |
| 293 | "failed to initialize mruby: %.*s", |
| 294 | (int)RSTRING_LEN(reason), |
| 295 | RSTRING_PTR(reason)); |
| 296 | mrb_close(ctx->impl->mrb.state); |
| 297 | ctx->impl->mrb.state = NULL; |
| 298 | } else { |
| 299 | ctx->impl->mrb.checked_procs = |
| 300 | grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); |
| 301 | ctx->impl->mrb.registered_plugins = |
| 302 | grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); |
| 303 | GRN_VOID_INIT(&(ctx->impl->mrb.buffer.from)); |
| 304 | GRN_VOID_INIT(&(ctx->impl->mrb.buffer.to)); |
| 305 | ctx->impl->mrb.builtin.time_class = mrb_class_get(mrb, "Time"); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | static void |
| 311 | grn_ctx_impl_mrb_fin_real(grn_ctx *ctx) |
| 312 | { |
| 313 | if (ctx->impl->mrb.state) { |
| 314 | mrb_close(ctx->impl->mrb.state); |
| 315 | ctx->impl->mrb.state = NULL; |
| 316 | grn_hash_close(ctx, ctx->impl->mrb.checked_procs); |
| 317 | grn_hash_close(ctx, ctx->impl->mrb.registered_plugins); |
| 318 | GRN_OBJ_FIN(ctx, &(ctx->impl->mrb.buffer.from)); |
| 319 | GRN_OBJ_FIN(ctx, &(ctx->impl->mrb.buffer.to)); |
| 320 | } |
| 321 | } |
| 322 | #else /* GRN_WITH_MRUBY */ |
| 323 | static void |
| 324 | grn_ctx_impl_mrb_init_lazy(grn_ctx *ctx) |
| 325 | { |
| 326 | } |
| 327 | |
| 328 | static void |
| 329 | grn_ctx_impl_mrb_fin_real(grn_ctx *ctx) |
| 330 | { |
| 331 | } |
| 332 | #endif /* GRN_WITH_MRUBY */ |
| 333 | |
| 334 | void |
| 335 | grn_ctx_impl_mrb_init(grn_ctx *ctx) |
| 336 | { |
| 337 | ctx->impl->mrb.initialized = false; |
| 338 | } |
| 339 | |
| 340 | void |
| 341 | grn_ctx_impl_mrb_fin(grn_ctx *ctx) |
| 342 | { |
| 343 | if (!ctx->impl->mrb.initialized) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | ctx->impl->mrb.initialized = false; |
| 348 | grn_ctx_impl_mrb_fin_real(ctx); |
| 349 | } |
| 350 | |
| 351 | void |
| 352 | grn_ctx_impl_mrb_ensure_init(grn_ctx *ctx) |
| 353 | { |
| 354 | if (!ctx->impl->mrb.initialized) { |
| 355 | ctx->impl->mrb.initialized = true; |
| 356 | grn_ctx_impl_mrb_init_lazy(ctx); |
| 357 | } |
| 358 | } |