| 1 | /* |
| 2 | Copyright(C) 2014-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 | |
| 22 | #ifdef GRN_WITH_MRUBY |
| 23 | #include <mruby.h> |
| 24 | #include <mruby/class.h> |
| 25 | #include <mruby/data.h> |
| 26 | |
| 27 | #include "mrb_ctx.h" |
| 28 | #include "mrb_array.h" |
| 29 | |
| 30 | static struct mrb_data_type mrb_grn_array_type = { |
| 31 | "Groonga::Array", |
| 32 | NULL |
| 33 | }; |
| 34 | |
| 35 | static mrb_value |
| 36 | mrb_grn_array_class_create(mrb_state *mrb, mrb_value klass) |
| 37 | { |
| 38 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 39 | char *name; |
| 40 | mrb_int name_length; |
| 41 | const char *path = NULL; |
| 42 | mrb_value mrb_value_type; |
| 43 | grn_obj *value_type = NULL; |
| 44 | grn_obj *array; |
| 45 | |
| 46 | mrb_get_args(mrb, "so", &name, &name_length, &mrb_value_type); |
| 47 | if (!mrb_nil_p(mrb_value_type)) { |
| 48 | value_type = DATA_PTR(mrb_value_type); |
| 49 | } |
| 50 | |
| 51 | array = grn_table_create(ctx, |
| 52 | name, (unsigned int)name_length, |
| 53 | path, |
| 54 | GRN_TABLE_NO_KEY, |
| 55 | NULL, |
| 56 | value_type); |
| 57 | grn_mrb_ctx_check(mrb); |
| 58 | |
| 59 | return mrb_funcall(mrb, klass, "new", 1, mrb_cptr_value(mrb, array)); |
| 60 | } |
| 61 | |
| 62 | static mrb_value |
| 63 | mrb_grn_array_initialize(mrb_state *mrb, mrb_value self) |
| 64 | { |
| 65 | mrb_value mrb_array_ptr; |
| 66 | |
| 67 | mrb_get_args(mrb, "o", &mrb_array_ptr); |
| 68 | DATA_TYPE(self) = &mrb_grn_array_type; |
| 69 | DATA_PTR(self) = mrb_cptr(mrb_array_ptr); |
| 70 | return self; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | grn_mrb_array_init(grn_ctx *ctx) |
| 75 | { |
| 76 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 77 | mrb_state *mrb = data->state; |
| 78 | struct RClass *module = data->module; |
| 79 | struct RClass *table_class; |
| 80 | struct RClass *klass; |
| 81 | |
| 82 | table_class = mrb_class_get_under(mrb, module, "Table"); |
| 83 | klass = mrb_define_class_under(mrb, module, "Array", table_class); |
| 84 | MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); |
| 85 | |
| 86 | mrb_define_class_method(mrb, klass, "create", |
| 87 | mrb_grn_array_class_create, |
| 88 | MRB_ARGS_REQ(2)); |
| 89 | |
| 90 | mrb_define_method(mrb, klass, "initialize", |
| 91 | mrb_grn_array_initialize, MRB_ARGS_REQ(1)); |
| 92 | } |
| 93 | #endif |