-
Notifications
You must be signed in to change notification settings - Fork 116
/
ctx_impl_mrb.c
153 lines (130 loc) · 3.87 KB
/
ctx_impl_mrb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2013-2014 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include "grn_ctx_impl_mrb.h"
#include "grn_ctx_impl.h"
#include "grn_mrb.h"
#include "mrb/mrb_error.h"
#include "mrb/mrb_id.h"
#include "mrb/mrb_operator.h"
#include "mrb/mrb_ctx.h"
#include "mrb/mrb_logger.h"
#include "mrb/mrb_void.h"
#include "mrb/mrb_bulk.h"
#include "mrb/mrb_object.h"
#include "mrb/mrb_database.h"
#include "mrb/mrb_table.h"
#include "mrb/mrb_patricia_trie.h"
#include "mrb/mrb_column.h"
#include "mrb/mrb_fixed_size_column.h"
#include "mrb/mrb_variable_size_column.h"
#include "mrb/mrb_index_column.h"
#include "mrb/mrb_expr.h"
#include "mrb/mrb_accessor.h"
#include "mrb/mrb_procedure.h"
#include "mrb/mrb_table_cursor.h"
#ifdef GRN_WITH_MRUBY
# include <mruby/array.h>
# include <mruby/variable.h>
#endif
#ifdef GRN_WITH_MRUBY
static mrb_value
mrb_kernel_load(mrb_state *mrb, mrb_value self)
{
grn_ctx *ctx = (grn_ctx *)mrb->ud;
char *path;
mrb_get_args(mrb, "z", &path);
grn_mrb_load(ctx, path);
if (mrb->exc) {
mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));
}
grn_mrb_ctx_check(mrb);
return mrb_true_value();
}
static void
grn_ctx_impl_mrb_init_bindings(grn_ctx *ctx)
{
mrb_state *mrb = ctx->impl->mrb.state;
mrb->ud = ctx;
ctx->impl->mrb.module = mrb_define_module(mrb, "Groonga");
mrb_define_class(mrb, "LoadError", mrb_class_get(mrb, "ScriptError"));
mrb_define_method(mrb, mrb->kernel_module,
"load", mrb_kernel_load, MRB_ARGS_REQ(1));
{
mrb_value load_path;
const char *system_ruby_scripts_dir;
load_path = mrb_ary_new(mrb);
system_ruby_scripts_dir = grn_mrb_get_system_ruby_scripts_dir(ctx);
mrb_ary_push(mrb, load_path,
mrb_str_new_cstr(mrb, system_ruby_scripts_dir));
mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$LOAD_PATH"), load_path);
}
grn_mrb_load(ctx, "require.rb");
grn_mrb_load(ctx, "initialize/pre.rb");
grn_mrb_error_init(ctx);
grn_mrb_id_init(ctx);
grn_mrb_operator_init(ctx);
grn_mrb_ctx_init(ctx);
grn_mrb_logger_init(ctx);
grn_mrb_void_init(ctx);
grn_mrb_bulk_init(ctx);
grn_mrb_object_init(ctx);
grn_mrb_database_init(ctx);
grn_mrb_table_init(ctx);
grn_mrb_patricia_trie_init(ctx);
grn_mrb_column_init(ctx);
grn_mrb_fixed_size_column_init(ctx);
grn_mrb_variable_size_column_init(ctx);
grn_mrb_index_column_init(ctx);
grn_mrb_expr_init(ctx);
grn_mrb_accessor_init(ctx);
grn_mrb_procedure_init(ctx);
grn_mrb_table_cursor_init(ctx);
grn_mrb_load(ctx, "initialize/post.rb");
}
void
grn_ctx_impl_mrb_init(grn_ctx *ctx)
{
const char *grn_mruby_enabled;
grn_mruby_enabled = getenv("GRN_MRUBY_ENABLED");
if (grn_mruby_enabled && strcmp(grn_mruby_enabled, "no") == 0) {
ctx->impl->mrb.state = NULL;
ctx->impl->mrb.base_directory[0] = '\0';
ctx->impl->mrb.module = NULL;
ctx->impl->mrb.object_class = NULL;
} else {
ctx->impl->mrb.state = mrb_open();
ctx->impl->mrb.base_directory[0] = '\0';
grn_ctx_impl_mrb_init_bindings(ctx);
}
}
void
grn_ctx_impl_mrb_fin(grn_ctx *ctx)
{
if (ctx->impl->mrb.state) {
mrb_close(ctx->impl->mrb.state);
ctx->impl->mrb.state = NULL;
}
}
#else
void
grn_ctx_impl_mrb_init(grn_ctx *ctx)
{
}
void
grn_ctx_impl_mrb_fin(grn_ctx *ctx)
{
}
#endif