-
Notifications
You must be signed in to change notification settings - Fork 0
/
zorba.cc
295 lines (226 loc) · 7.65 KB
/
zorba.cc
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* zorba-ruby/zorba.cc
*
* Copyright 2010 James Pharaoh <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <cstdarg>
#include <exception>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <ruby.h>
#include <zorba/dynamic_context.h>
#include <zorba/external_function.h>
#include <zorba/external_module.h>
#include <zorba/options.h>
#include <zorba/singleton_item_sequence.h>
#include <zorba/store_manager.h>
#include <zorba/uri_resolvers.h>
#include <zorba/xquery.h>
#include <zorba/zorba.h>
#define ZR_REAL(type, name) \
type * name; \
Data_Get_Struct (name##_ruby, type, name)
#define ZR_SHADOW(type, name) \
VALUE name##Shadow_ruby = rb_iv_get (name##_ruby, "@shadow"); \
type * name; \
Data_Get_Struct (name##Shadow_ruby, type, name)
#define ZR_REAL_OPT(type, name) \
type * name; \
if (name##_ruby == Qnil) name = NULL; \
else Data_Get_Struct (name##_ruby, type, name)
#define ZR_SHADOW_OPT(type, name) \
VALUE name##Shadow_ruby; \
type * name; \
if (name##_ruby = Qnil) { \
name##Shadow_ruby = Qnil; \
name = NULL; \
} else { \
name##Shadow_ruby = rb_iv_get (name##_ruby, "@shadow"); \
Data_Get_Struct (name##Shadow_ruby, type, name); \
}
#define ZR_DEBUG(...) \
fprintf (stderr, __VA_ARGS__)
using namespace std;
VALUE cStore;
VALUE cXmlDataManager;
VALUE currentException = Qnil;
#define STUBS_PART
#include "parts.h"
#undef STUBS_PART
class RubyException : public exception {
};
const char * zr_map_method_name (const char * name) {
static char * pointer = NULL;
if (pointer) free (pointer);
pointer = NULL;
int len = strlen (name);
pointer = (char *) malloc (len + 1);
strcpy (pointer, name);
if (strcmp (name, "new_new") == 0) {
pointer [3] = 0;
return pointer;
}
if (len > 3 && strcmp (name + len - 3, "_eq") == 0) {
pointer [len - 3] = '=';
pointer [len - 2] = 0;
return pointer;
}
return pointer;
}
VALUE zr_funcall_nest (VALUE args) {
return rb_apply (
rb_ary_entry (args, 0),
rb_ary_entry (args, 1),
rb_ary_entry (args, 2));
}
VALUE zr_funcall (VALUE self, VALUE name, int argc, ...) {
va_list ap;
int i;
VALUE args1 = rb_ary_new2 (argc);
va_start (ap, argc);
for (int i = 0; i < argc; i++) {
VALUE arg = va_arg (ap, VALUE);
rb_ary_store (args1, i, arg);
}
va_end (ap);
VALUE args2 =
rb_ary_new3 (3,
self,
name,
args1);
int result;
VALUE ret = rb_protect (&zr_funcall_nest, args2, &result);
if (result) {
currentException = rb_gv_get ("$!");
cout << "excep: " << currentException << endl;
throw new RubyException ();
}
return ret;
}
#define INTERFACE_PART
#include "parts.h"
#undef INTERFACE_PART
// -------------------- ruby class and module globals
#define ZR_CLASS(name, parent) \
VALUE c##name;
#define ZR_CLASS_CLASS(under, name, parent) \
VALUE c##name;
#define ZR_CLASS_MODULE(under, name) \
VALUE m##name;
#define ZR_CLASS_EXCEP(under, name, parent) \
VALUE e##name;
#define RUBY_PART
#include "parts.h"
#undef RUBY_PART
// -------------------- implementation
#define IMPLEMENTATION_PART
#include "parts.h"
#undef IMPLEMENTATION_PART
// -------------------- wrap ruby methods
#define ZR_WRAP_INVOKE_VAR_C(name, self, argc, argv) \
return name (argc, argv, self)
#define ZR_WRAP_INVOKE_0(name, self, argc, argv) \
if (argc != 0) rb_raise (rb_eArgError, "Expected 0 arguments, got %d", argc); \
return name (self)
#define ZR_WRAP_INVOKE_1(name, self, argc, argv) \
if (argc != 1) rb_raise (rb_eArgError, "Expected 1 arguments, got %d", argc); \
return name (self, argv[0])
#define ZR_WRAP_INVOKE_2(name, self, argc, argv) \
if (argc != 2) rb_raise (rb_eArgError, "Expected 2 arguments, got %d", argc); \
return name (self, argv[0], argv[1])
#define ZR_WRAP_INVOKE_3(name, self, argc, argv) \
if (argc != 3) rb_raise (rb_eArgError, "Expected 3 arguments, got %d", argc); \
return name (self, argv[0], argv[1], argv[2])
#define ZR_WRAP_INVOKE_4(name, self, argc, argv) \
if (argc != 4) rb_raise (rb_eArgError, "Expected 4 arguments, got %d", argc); \
return name (self, argv[0], argv[1], argv[2], argv[3])
#define ZR_WRAP_INVOKE_5(name, self, argc, argv) \
if (argc != 5) rb_raise (rb_eArgError, "Expected 5 arguments, got %d", argc); \
return name (self, argv[0], argv[1], argv[2], argv[3], argv[4])
#define ZR_WRAP_INVOKE_6(name, self, argc, argv) \
if (argc != 6) rb_raise (rb_eArgError, "Expected 6 arguments, got %d", argc); \
return name (self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5])
#define ZR_WRAP_INVOKE_7(name, self, argc, argv) \
if (argc != 7) rb_raise (rb_eArgError, "Expected 7 arguments, got %d", argc); \
return name (self, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6])
#define ZR_CLASS_METHOD(klass, name, args) \
VALUE klass##_##name##_wrap (int argc, VALUE *argv, VALUE self) { \
try { \
ZR_WRAP_INVOKE_##args (klass::name, self, argc, argv); \
} \
catch (zorba::ZorbaException & e) { zr_raise (e); } \
catch (RubyException & e) { zr_raise (e); } \
}
#define ZR_CLASS_SINGLETON_METHOD(klass, name, args) \
VALUE klass##_##name##_wrap (int argc, VALUE *argv, VALUE self) { \
try { \
ZR_WRAP_INVOKE_##args (klass::name, self, argc, argv); \
} \
catch (zorba::ZorbaException & e) { zr_raise (e); } \
catch (RubyException & e) { zr_raise (e); } \
}
#define ZR_MODULE_SINGLETON_METHOD(module, name, args) \
VALUE module##_##name##_wrap (int argc, VALUE *argv, VALUE self) { \
try { \
ZR_WRAP_INVOKE_##args (module::name, self, argc, argv); \
} \
catch (zorba::ZorbaException & e) { zr_raise (e); } \
catch (RubyException & e) { zr_raise (e); } \
}
#define RUBY_PART
#include "parts.h"
#undef RUBY_PART
#undef ZR_CLASS_INVOKE_VAR_C
#undef ZR_CLASS_INVOKE_0
#undef ZR_CLASS_INVOKE_1
#undef ZR_CLASS_INVOKE_2
#undef ZR_CLASS_INVOKE_3
#undef ZR_CLASS_INVOKE_4
#undef ZR_CLASS_INVOKE_5
#undef ZR_CLASS_INVOKE_6
#undef ZR_CLASS_INVOKE_7
// --------------------
extern "C"
void Init_zorba () {
#define INIT_PART
#include "parts.h"
#undef INIT_PART
// -------------------- initialise classes, methods etc
#define ZR_CLASS(name, parent) \
c##name = rb_define_class (#name, parent);
#define ZR_CLASS_CLASS(under, name, parent) \
c##name = rb_define_class_under (c##under, #name, parent);
#define ZR_CLASS_CONST(klass, name, value) \
rb_define_const (c##klass, #name, value);
#define ZR_CLASS_EXCEP(under, name, parent) \
e##name = rb_define_class_under (c##under, #name, parent);
#define ZR_CLASS_METHOD(klass, name, args) \
rb_define_method (c##klass, zr_map_method_name (#name), RUBY_METHOD_FUNC (klass##_##name##_wrap), -1);
#define ZR_CLASS_MODULE(under, name) \
m##name = rb_define_module_under (c##under, #name);
#define ZR_CLASS_SINGLETON_METHOD(klass, name, args) \
rb_define_singleton_method (c##klass, zr_map_method_name (#name), RUBY_METHOD_FUNC (klass##_##name##_wrap), -1);
#define ZR_MODULE_SINGLETON_METHOD(module, name, args) \
rb_define_singleton_method (m##module, zr_map_method_name (#name), RUBY_METHOD_FUNC (module##_##name##_wrap), -1);
#define RUBY_PART
#include "parts.h"
#undef RUBY_PART
// --------------------
cStore = rb_define_class_under (cZorba, "Store", rb_cObject);
cXmlDataManager = rb_define_class_under (cZorba, "XmlDataManager", rb_cObject);
}