rucy 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.doc/ext/rucy/class.cpp +48 -56
- data/.doc/ext/rucy/exception.cpp +1 -1
- data/.doc/ext/rucy/function.cpp +14 -2
- data/.doc/ext/rucy/struct.cpp +2 -20
- data/.doc/ext/rucy/tester.cpp +7 -19
- data/.doc/ext/rucy/value.cpp +23 -1
- data/{README → README.md} +0 -0
- data/Rakefile +7 -5
- data/VERSION +1 -1
- data/bin/rucy2rdoc +8 -5
- data/ext/rucy/class.cpp +78 -87
- data/ext/rucy/class.h +12 -6
- data/ext/rucy/exception.cpp +17 -17
- data/ext/rucy/extconf.rb +11 -52
- data/ext/rucy/function.cpp +25 -12
- data/ext/rucy/struct.cpp +8 -26
- data/ext/rucy/tester.cpp +11 -24
- data/ext/rucy/value.cpp +32 -9
- data/include/rucy/class.h +5 -3
- data/include/rucy/exception.h +17 -71
- data/include/rucy/extension.h.erb +489 -0
- data/include/rucy/function.h.erb +20 -34
- data/include/rucy/module.h.erb +20 -14
- data/include/rucy/ruby.h +23 -0
- data/include/rucy/rucy.h +7 -66
- data/include/rucy/symbol.h +11 -11
- data/include/rucy/value.h.erb +98 -176
- data/include/rucy.h +9 -1
- data/lib/rucy/module.rb +11 -7
- data/rucy.gemspec +3 -4
- data/src/class.cpp +34 -6
- data/src/exception.cpp +69 -54
- data/src/extension.cpp +59 -0
- data/src/function.cpp.erb +17 -25
- data/src/module.cpp.erb +25 -16
- data/src/rucy.cpp +15 -25
- data/src/symbol.cpp +18 -17
- data/src/value.cpp.erb +374 -175
- data/task/doc.rake +5 -0
- data/test/helper.rb +6 -2
- data/test/test_class.rb +27 -20
- data/test/test_function.rb +6 -0
- data/test/test_value.rb +4 -0
- metadata +29 -39
- data/.gitignore +0 -22
- data/ChangeLog +0 -13
- data/include/rucy/defs.h.erb +0 -76
- data/include/rucy/extension.h +0 -206
@@ -0,0 +1,489 @@
|
|
1
|
+
// -*- c++ -*-
|
2
|
+
#pragma once
|
3
|
+
#ifndef __RUCY_EXTENSION_H__
|
4
|
+
#define __RUCY_EXTENSION_H__
|
5
|
+
|
6
|
+
|
7
|
+
#include <typeinfo>
|
8
|
+
#include <boost/dynamic_bitset.hpp>
|
9
|
+
#include <xot/ref.h>
|
10
|
+
#include <xot/string.h>
|
11
|
+
#include <rucy/rucy.h>
|
12
|
+
#include <rucy/value.h>
|
13
|
+
#include <rucy/exception.h>
|
14
|
+
|
15
|
+
|
16
|
+
#define RUCY_DECLARE_VALUE_FROM(native_class) \
|
17
|
+
namespace Rucy \
|
18
|
+
{ \
|
19
|
+
Value value (const native_class& obj); \
|
20
|
+
Value value (const native_class* obj); \
|
21
|
+
}
|
22
|
+
|
23
|
+
#define RUCY_DECLARE_VALUE_TO(native_class) \
|
24
|
+
namespace Rucy \
|
25
|
+
{ \
|
26
|
+
template <> native_class* value_to<native_class*> (Value value, bool); \
|
27
|
+
template <> native_class& value_to<native_class&> (Value value, bool convert); \
|
28
|
+
template <> native_class value_to<native_class> (Value value, bool convert); \
|
29
|
+
}
|
30
|
+
|
31
|
+
#define RUCY_DECLARE_WRAPPER_VALUE_FROM(wrapped_class) \
|
32
|
+
namespace Rucy \
|
33
|
+
{ \
|
34
|
+
Value value (wrapped_class* obj); \
|
35
|
+
Value value (wrapped_class* obj, Value klass); \
|
36
|
+
}
|
37
|
+
|
38
|
+
#define RUCY_DECLARE_WRAPPER_VALUE_TO(wrapped_class) \
|
39
|
+
namespace Rucy \
|
40
|
+
{ \
|
41
|
+
template <> wrapped_class* value_to<wrapped_class*> (Value value, bool convert); \
|
42
|
+
}
|
43
|
+
|
44
|
+
#define RUCY_DEFINE_VALUE_FROM(native_class, ruby_class) \
|
45
|
+
namespace Rucy \
|
46
|
+
{ \
|
47
|
+
Value \
|
48
|
+
value (const native_class& obj) \
|
49
|
+
{ \
|
50
|
+
return new_type(ruby_class, new native_class(obj)); \
|
51
|
+
} \
|
52
|
+
Value \
|
53
|
+
value (const native_class* obj) \
|
54
|
+
{ \
|
55
|
+
return obj ? value(*obj) : nil(); \
|
56
|
+
} \
|
57
|
+
}
|
58
|
+
|
59
|
+
#define RUCY_DEFINE_VALUE_TO(native_class, ruby_class) \
|
60
|
+
namespace Rucy \
|
61
|
+
{ \
|
62
|
+
template <> native_class* \
|
63
|
+
value_to<native_class*> (Value value, bool) \
|
64
|
+
{ \
|
65
|
+
return get_type_ptr<native_class>(value, ruby_class); \
|
66
|
+
} \
|
67
|
+
template <> native_class& \
|
68
|
+
value_to<native_class&> (Value value, bool convert) \
|
69
|
+
{ \
|
70
|
+
native_class* obj = value_to<native_class*>(value, convert); \
|
71
|
+
if (!obj) \
|
72
|
+
rucy_error(__FILE__, __LINE__, "failed to convert from/to %s.", #native_class); \
|
73
|
+
return *obj; \
|
74
|
+
} \
|
75
|
+
}
|
76
|
+
|
77
|
+
#define RUCY_DEFINE_WRAPPER_VALUE_FROM(wrapped_class, ruby_class) \
|
78
|
+
namespace Rucy \
|
79
|
+
{ \
|
80
|
+
Value \
|
81
|
+
value (wrapped_class* obj) \
|
82
|
+
{ \
|
83
|
+
return value(obj, ruby_class); \
|
84
|
+
} \
|
85
|
+
Value \
|
86
|
+
value (wrapped_class* obj, Value klass) \
|
87
|
+
{ \
|
88
|
+
if (!obj) return nil(); \
|
89
|
+
ClassWrapper<wrapped_class>* p = dynamic_cast<ClassWrapper<wrapped_class>*>(obj); \
|
90
|
+
if (!p) return new_ref(klass, obj); \
|
91
|
+
if (p->value.is_nil()) p->value = new_wrapper(klass, obj); \
|
92
|
+
return p->value; \
|
93
|
+
} \
|
94
|
+
}
|
95
|
+
|
96
|
+
#define RUCY_DEFINE_WRAPPER_VALUE_TO(wrapped_class, ruby_class) \
|
97
|
+
namespace Rucy \
|
98
|
+
{ \
|
99
|
+
template <> wrapped_class* \
|
100
|
+
value_to<wrapped_class*> (Value value, bool convert) \
|
101
|
+
{ \
|
102
|
+
return get_type_ptr<wrapped_class>(value, ruby_class); \
|
103
|
+
} \
|
104
|
+
}
|
105
|
+
|
106
|
+
#define RUCY_DECLARE_VALUE_FROM_TO(native_class) \
|
107
|
+
RUCY_DECLARE_VALUE_FROM(native_class) \
|
108
|
+
RUCY_DECLARE_VALUE_TO(native_class)
|
109
|
+
|
110
|
+
#define RUCY_DEFINE_VALUE_FROM_TO(native_class, ruby_class) \
|
111
|
+
RUCY_DEFINE_VALUE_FROM(native_class, ruby_class) \
|
112
|
+
RUCY_DEFINE_VALUE_TO(native_class, ruby_class)
|
113
|
+
|
114
|
+
#define RUCY_VALUE_FROM_TO(native_class, ruby_class) \
|
115
|
+
RUCY_DECLARE_VALUE_FROM_TO(native_class) \
|
116
|
+
RUCY_DEFINE_VALUE_FROM_TO(native_class, ruby_class)
|
117
|
+
|
118
|
+
#define RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(wrapped_class) \
|
119
|
+
RUCY_DECLARE_WRAPPER_VALUE_FROM(wrapped_class) \
|
120
|
+
RUCY_DECLARE_WRAPPER_VALUE_TO(wrapped_class)
|
121
|
+
|
122
|
+
#define RUCY_DEFINE_WRAPPER_VALUE_FROM_TO(wrapped_class, ruby_class) \
|
123
|
+
RUCY_DEFINE_WRAPPER_VALUE_FROM(wrapped_class, ruby_class) \
|
124
|
+
RUCY_DEFINE_WRAPPER_VALUE_TO(wrapped_class, ruby_class)
|
125
|
+
|
126
|
+
#define RUCY_WRAPPER_VALUE_FROM_TO(wrapped_class, ruby_class) \
|
127
|
+
RUCY_DECLARE_WRAPPER_VALUE_FROM_TO(wrapped_class) \
|
128
|
+
RUCY_DEFINE_WRAPPER_VALUE_FROM_TO(wrapped_class, ruby_class)
|
129
|
+
|
130
|
+
|
131
|
+
#define RUCY_OVERRIDE_BEGIN(wrapper_class) \
|
132
|
+
typedef wrapper_class RucyWrapper; \
|
133
|
+
typedef RucyWrapper Super; \
|
134
|
+
enum { OVERRIDE_ID_FIRST = RucyWrapper::OVERRIDE_ID_LAST - 1,
|
135
|
+
|
136
|
+
#define RUCY_OVERRIDE_END \
|
137
|
+
OVERRIDE_ID_LAST };
|
138
|
+
|
139
|
+
#define RUCY_OVERRIDE_ID(name) \
|
140
|
+
OID_##name,
|
141
|
+
|
142
|
+
#define RUCY_IS_OVERRIDDEN(name, ruby_class) \
|
143
|
+
this->is_overridden(ruby_class, name, OID_##name)
|
144
|
+
|
145
|
+
|
146
|
+
#define RUCY_TRY \
|
147
|
+
RubyValue RUCY__rubyexception__ = nil(); \
|
148
|
+
int RUCY__rubyjumptag__ = 0; \
|
149
|
+
\
|
150
|
+
goto RUCY__ruby_try_start__; \
|
151
|
+
\
|
152
|
+
RUCY__ruby_jump_tag__: \
|
153
|
+
if (RUCY__rubyjumptag__) rb_jump_tag(RUCY__rubyjumptag__); \
|
154
|
+
RUCY_THROW(rb_exc_new2(Rucy::native_error_class(), "Bad jump tag.")); \
|
155
|
+
\
|
156
|
+
RUCY__ruby_raise_exception__: \
|
157
|
+
rb_exc_raise(RUCY__rubyexception__); \
|
158
|
+
\
|
159
|
+
RUCY__ruby_try_start__: \
|
160
|
+
try \
|
161
|
+
{
|
162
|
+
|
163
|
+
#define RUCY_CATCH \
|
164
|
+
} \
|
165
|
+
catch (const Rucy::RubyJumpTag& e) \
|
166
|
+
{ \
|
167
|
+
RUCY__rubyjumptag__ = e.tag; \
|
168
|
+
goto RUCY__ruby_jump_tag__; \
|
169
|
+
} \
|
170
|
+
catch (const Rucy::RubyException& e) \
|
171
|
+
{ \
|
172
|
+
RUCY_THROW(e.value()); \
|
173
|
+
} \
|
174
|
+
catch (const std::invalid_argument& e) \
|
175
|
+
{ \
|
176
|
+
RUCY_THROW(rb_exc_new2(rb_eArgError, e.what())); \
|
177
|
+
} \
|
178
|
+
catch (const std::exception& e) \
|
179
|
+
{ \
|
180
|
+
Xot::String text = e.what(), type = typeid(e).name(); \
|
181
|
+
if (!type.empty()) \
|
182
|
+
{ \
|
183
|
+
if (!text.empty()) text += " "; \
|
184
|
+
text += "[" + type + "]"; \
|
185
|
+
} \
|
186
|
+
RUCY_THROW(rb_exc_new2(Rucy::native_error_class(), text.c_str())); \
|
187
|
+
} \
|
188
|
+
catch (const std::string& s) \
|
189
|
+
{ \
|
190
|
+
RUCY_THROW(rb_exc_new2(Rucy::native_error_class(), s.c_str())); \
|
191
|
+
} \
|
192
|
+
catch (const char* s) \
|
193
|
+
{ \
|
194
|
+
RUCY_THROW(rb_exc_new2(Rucy::native_error_class(), s)); \
|
195
|
+
} \
|
196
|
+
catch (...) \
|
197
|
+
{ \
|
198
|
+
RUCY_THROW(rb_exc_new2( \
|
199
|
+
Rucy::native_error_class(), "Unknown C++ exception was thrown.")); \
|
200
|
+
}
|
201
|
+
|
202
|
+
#define RUCY_THROW(exception) \
|
203
|
+
RUCY__rubyexception__ = (exception); \
|
204
|
+
goto RUCY__ruby_raise_exception__
|
205
|
+
|
206
|
+
|
207
|
+
#define RUCY_DEF_ALLOC(name, klass) \
|
208
|
+
RubyValue name (Value klass) \
|
209
|
+
{ \
|
210
|
+
RUCY_TRY
|
211
|
+
|
212
|
+
#define RUCY_DEFN(name) \
|
213
|
+
RubyValue name (int argc, const Value* argv, Value self) \
|
214
|
+
{ \
|
215
|
+
RUCY_TRY
|
216
|
+
|
217
|
+
% NTIMES.each do |n|
|
218
|
+
#define RUCY_DEF<%= n %>(name<%= params(n) {|i| ", v#{i}"} %>) \
|
219
|
+
RubyValue name (Value self<%= params(n) {|i| ", Value v#{i}"} %>) \
|
220
|
+
{ \
|
221
|
+
RUCY_TRY
|
222
|
+
% end
|
223
|
+
|
224
|
+
#define RUCY_DEF_clear_override_flags(name, wrapped_class, ruby_class) \
|
225
|
+
RUCY_DEF0(name) \
|
226
|
+
{ \
|
227
|
+
RUCY_CHECK_OBJ(wrapped_class, ruby_class, self); \
|
228
|
+
ClassWrapper<wrapped_class>* obj = \
|
229
|
+
dynamic_cast<ClassWrapper<wrapped_class>*>(to<wrapped_class*>(self)); \
|
230
|
+
if (obj) obj->clear_override_flags(); \
|
231
|
+
} \
|
232
|
+
RUCY_END
|
233
|
+
|
234
|
+
#define RUCY_END \
|
235
|
+
RUCY_CATCH \
|
236
|
+
return nil(); \
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
#define RUCY_CHECK_OBJ(native_class, ruby_class, obj) \
|
241
|
+
do \
|
242
|
+
{ \
|
243
|
+
native_class* p = Rucy::get_type_ptr<native_class>(obj, ruby_class); \
|
244
|
+
if (!p) Rucy::invalid_object_error(__FILE__, __LINE__); \
|
245
|
+
} \
|
246
|
+
while(0)
|
247
|
+
|
248
|
+
#define RUCY_CHECK_OBJECT(native_class, ruby_class, obj) \
|
249
|
+
do \
|
250
|
+
{ \
|
251
|
+
native_class* p = Rucy::get_type_ptr<native_class>(obj, ruby_class); \
|
252
|
+
if (!p || !*p) Rucy::invalid_object_error(__FILE__, __LINE__); \
|
253
|
+
} \
|
254
|
+
while(0)
|
255
|
+
|
256
|
+
#define RUCY_WRAPPER_CALL(wrapped_class, obj, fun) \
|
257
|
+
(dynamic_cast<ClassWrapper<wrapped_class>*>(obj) \
|
258
|
+
? reinterpret_cast<ClassWrapper<wrapped_class>*>(obj)->RucyWrapped::fun \
|
259
|
+
: (obj)->fun)
|
260
|
+
|
261
|
+
|
262
|
+
namespace Rucy
|
263
|
+
{
|
264
|
+
|
265
|
+
|
266
|
+
void check_class (Value obj, Value klass);
|
267
|
+
|
268
|
+
void check_arg_count (
|
269
|
+
const char* file, int line,
|
270
|
+
const char* method, int nargs, int nargs_expected_n0,
|
271
|
+
int n1 = -1, int n2 = -1, int n3 = -1, int n4 = -1, int n5 = -1,
|
272
|
+
int n6 = -1, int n7 = -1, int n8 = -1, int n9 = -1, int n10 = -1);
|
273
|
+
|
274
|
+
|
275
|
+
template <typename T>
|
276
|
+
class ClassWrapper : public T
|
277
|
+
{
|
278
|
+
|
279
|
+
typedef ClassWrapper This;
|
280
|
+
|
281
|
+
public:
|
282
|
+
|
283
|
+
typedef T RucyWrapped;
|
284
|
+
|
285
|
+
GlobalValue value;
|
286
|
+
|
287
|
+
ClassWrapper ()
|
288
|
+
: value(nil(), true)
|
289
|
+
{
|
290
|
+
}
|
291
|
+
|
292
|
+
virtual void retain (void* by_ruby_value = NULL) const
|
293
|
+
{
|
294
|
+
if (!by_ruby_value)
|
295
|
+
{
|
296
|
+
refc_set_aux(refc_aux() + 1);
|
297
|
+
if (refc_aux() == 1) value.gc(false);
|
298
|
+
}
|
299
|
+
|
300
|
+
RucyWrapped::retain();
|
301
|
+
}
|
302
|
+
|
303
|
+
virtual void release (void* by_ruby_value = NULL) const
|
304
|
+
{
|
305
|
+
if (!by_ruby_value && refc_aux() > 0)
|
306
|
+
{
|
307
|
+
refc_set_aux(refc_aux() - 1);
|
308
|
+
if (refc_aux() == 0) value.gc(true);
|
309
|
+
}
|
310
|
+
|
311
|
+
RucyWrapped::release();
|
312
|
+
}
|
313
|
+
|
314
|
+
virtual void clear_override_flags ()
|
315
|
+
{
|
316
|
+
override_flags.reset();
|
317
|
+
}
|
318
|
+
|
319
|
+
virtual bool is_overridden (const Value& klass, const Symbol& name, uint id) const
|
320
|
+
{
|
321
|
+
if (id <= OVERRIDE_ID_UNKNOWN)
|
322
|
+
return false;
|
323
|
+
|
324
|
+
bool checked = false, overridden = false;
|
325
|
+
get_override_flag(&checked, &overridden, id);
|
326
|
+
if (checked) return overridden;
|
327
|
+
|
328
|
+
overridden = check_overridden(klass, name);
|
329
|
+
if (!set_override_flag(id, true, overridden))
|
330
|
+
return false;
|
331
|
+
|
332
|
+
return overridden;
|
333
|
+
}
|
334
|
+
|
335
|
+
protected:
|
336
|
+
|
337
|
+
enum
|
338
|
+
{
|
339
|
+
|
340
|
+
OVERRIDE_ID_UNKNOWN = 0,
|
341
|
+
|
342
|
+
OVERRIDE_ID_LAST,
|
343
|
+
|
344
|
+
OVERRIDE_ID_MAX = 256
|
345
|
+
|
346
|
+
};
|
347
|
+
|
348
|
+
virtual ushort refc_aux () const
|
349
|
+
{
|
350
|
+
return RucyWrapped::refc_aux();
|
351
|
+
}
|
352
|
+
|
353
|
+
virtual void refc_set_aux (ushort aux) const
|
354
|
+
{
|
355
|
+
RucyWrapped::refc_set_aux(aux);
|
356
|
+
}
|
357
|
+
|
358
|
+
private:
|
359
|
+
|
360
|
+
mutable boost::dynamic_bitset<> override_flags;
|
361
|
+
|
362
|
+
bool check_overridden (const Value& klass, const Symbol& name) const
|
363
|
+
{
|
364
|
+
RUCY_SYM(method);
|
365
|
+
RUCY_SYM(owner);
|
366
|
+
return value.call(method, name.value()).call(owner) != klass;
|
367
|
+
}
|
368
|
+
|
369
|
+
void get_override_flag (bool* checked, bool* overridden, uint id) const
|
370
|
+
{
|
371
|
+
assert(checked || overridden);
|
372
|
+
|
373
|
+
int checked_pos = id2index(id);
|
374
|
+
int overridden_pos = checked_pos + 1;
|
375
|
+
bool valid = 0 <= checked_pos && overridden_pos < (int) override_flags.size();
|
376
|
+
if (checked) *checked = valid ? override_flags[checked_pos] : false;
|
377
|
+
if (overridden) *overridden = valid ? override_flags[overridden_pos] : false;
|
378
|
+
}
|
379
|
+
|
380
|
+
bool set_override_flag (uint id, bool checked, bool overridden) const
|
381
|
+
{
|
382
|
+
assert(id < OVERRIDE_ID_MAX);
|
383
|
+
|
384
|
+
int checked_pos = id2index(id);
|
385
|
+
if (checked_pos < 0) return true;
|
386
|
+
|
387
|
+
int overridden_pos = checked_pos + 1;
|
388
|
+
if (overridden_pos >= (int) override_flags.size())
|
389
|
+
override_flags.resize(overridden_pos + 1);
|
390
|
+
|
391
|
+
override_flags[checked_pos] = checked;
|
392
|
+
override_flags[overridden_pos] = overridden;
|
393
|
+
return true;
|
394
|
+
}
|
395
|
+
|
396
|
+
int id2index (uint id) const
|
397
|
+
{
|
398
|
+
return (id - 1) * 2;
|
399
|
+
}
|
400
|
+
|
401
|
+
};// ClassWrapper
|
402
|
+
|
403
|
+
|
404
|
+
template <typename T> inline void mark_type (void* p)
|
405
|
+
{
|
406
|
+
if (p) ((T*) p)->mark();
|
407
|
+
}
|
408
|
+
|
409
|
+
template <typename T> inline void delete_type (void* p)
|
410
|
+
{
|
411
|
+
delete (T*) p;
|
412
|
+
}
|
413
|
+
|
414
|
+
template <typename T> inline void release_ref (void* p)
|
415
|
+
{
|
416
|
+
if (p) ((T*) p)->release((void*) true);// 'true' means by-ruby-value.
|
417
|
+
}
|
418
|
+
|
419
|
+
template <typename T> inline void release_wrapper (void* p)
|
420
|
+
{
|
421
|
+
if (p) ((T*) p)->Xot::template RefCountable<>::release();
|
422
|
+
}
|
423
|
+
|
424
|
+
|
425
|
+
template <typename T> inline Value new_type (
|
426
|
+
Value klass, T* ptr,
|
427
|
+
RUBY_DATA_FUNC mark = NULL,
|
428
|
+
RUBY_DATA_FUNC free = delete_type<T>)
|
429
|
+
{
|
430
|
+
if (!ptr) return nil();
|
431
|
+
return Data_Wrap_Struct(klass, mark, free, ptr);
|
432
|
+
}
|
433
|
+
|
434
|
+
template <typename T> inline Value new_type (
|
435
|
+
Value klass,
|
436
|
+
RUBY_DATA_FUNC mark = NULL,
|
437
|
+
RUBY_DATA_FUNC free = delete_type<T>)
|
438
|
+
{
|
439
|
+
return new_type(klass, new T, mark, free);
|
440
|
+
}
|
441
|
+
|
442
|
+
template <typename T> inline Value new_ref (
|
443
|
+
Value klass, T* ptr,
|
444
|
+
RUBY_DATA_FUNC mark = NULL,
|
445
|
+
RUBY_DATA_FUNC free = release_ref<T>)
|
446
|
+
{
|
447
|
+
if (ptr) ptr->retain((void*) true);// 'true' means by-ruby-value
|
448
|
+
return new_type(klass, ptr, mark, free);
|
449
|
+
}
|
450
|
+
|
451
|
+
template <typename T> inline Value new_ref (
|
452
|
+
Value klass,
|
453
|
+
RUBY_DATA_FUNC mark = NULL,
|
454
|
+
RUBY_DATA_FUNC free = release_ref<T>)
|
455
|
+
{
|
456
|
+
return new_ref(klass, new T, mark, free);
|
457
|
+
}
|
458
|
+
|
459
|
+
template <typename T> inline Value new_wrapper (
|
460
|
+
Value klass, T* ptr,
|
461
|
+
RUBY_DATA_FUNC mark = NULL,
|
462
|
+
RUBY_DATA_FUNC free = release_wrapper<T>)
|
463
|
+
{
|
464
|
+
if (ptr) ptr->Xot::template RefCountable<>::retain();
|
465
|
+
return new_type(klass, ptr, mark, free);
|
466
|
+
}
|
467
|
+
|
468
|
+
template <typename T> inline Value new_wrapper (
|
469
|
+
Value klass,
|
470
|
+
RUBY_DATA_FUNC mark = NULL,
|
471
|
+
RUBY_DATA_FUNC free = release_wrapper<T>)
|
472
|
+
{
|
473
|
+
return new_wrapper(klass, new T, mark, free);
|
474
|
+
}
|
475
|
+
|
476
|
+
|
477
|
+
template <typename T> inline T* get_type_ptr (Value obj, Value klass = nil())
|
478
|
+
{
|
479
|
+
if (!klass.is_nil()) check_class(obj, klass);
|
480
|
+
T* p = NULL;
|
481
|
+
Data_Get_Struct(obj.value(), T, p);
|
482
|
+
return p;
|
483
|
+
}
|
484
|
+
|
485
|
+
|
486
|
+
}// Rucy
|
487
|
+
|
488
|
+
|
489
|
+
#endif//EOH
|
data/include/rucy/function.h.erb
CHANGED
@@ -4,8 +4,6 @@
|
|
4
4
|
#define __RUCY_FUNCTION_H__
|
5
5
|
|
6
6
|
|
7
|
-
#include <ruby.h>
|
8
|
-
#include <rucy/defs.h>
|
9
7
|
#include <rucy/value.h>
|
10
8
|
|
11
9
|
|
@@ -13,58 +11,46 @@ namespace Rucy
|
|
13
11
|
{
|
14
12
|
|
15
13
|
|
16
|
-
|
14
|
+
typedef RubyValue (*RubyFunctionN) (int argc, const Value* argv, Value self);
|
17
15
|
% NTIMES.each do |n|
|
18
|
-
|
16
|
+
|
17
|
+
typedef RubyValue (*RubyFunction<%= n %>) (Value self<%= params(n) {|i| ", Value v#{i}"} %>);
|
19
18
|
% end
|
20
19
|
|
20
|
+
|
21
21
|
Value call (Symbol name, int argc, const Value* argv);
|
22
22
|
% NTIMES.each do |n|
|
23
|
+
|
23
24
|
Value call (Symbol name<%= params(n) {|i| ", Value v#{i}"} %>);
|
24
25
|
% end
|
25
26
|
|
26
|
-
Value eval (const char* str);
|
27
27
|
|
28
|
+
Value eval (const char* format, ...);
|
28
29
|
|
29
|
-
% NTIMES.each do |n|
|
30
|
-
template <typename F<%= params(n) {|i| ", typename P#{i}"} %>> class Fun<%= n %>
|
31
|
-
{
|
32
|
-
public:
|
33
|
-
Fun<%= n %> (F f<%= params(n) {|i| ", P#{i} p#{i}"} %>) : f(f)<%= params(n) {|i| ", p#{i}(p#{i})"} %> {}
|
34
|
-
static VALUE function (Fun<%= n %>* fun) {return (*fun->f)(<%= params(n, ", ") {|i| "fun->p#{i}"} %>);}
|
35
|
-
private:
|
36
|
-
F f; <%= params(n, " ") {|i| "P#{i} p#{i};"} %>
|
37
|
-
};// Fun<%= n %>
|
38
|
-
% end
|
39
30
|
|
40
|
-
Value call_protect (
|
31
|
+
Value call_protect (RubyValue (*fun)(RubyValue), RubyValue arg);
|
41
32
|
|
42
33
|
% NTIMES.each do |n|
|
43
|
-
template <typename F<%= params(n) {|i| ", typename
|
44
|
-
|
34
|
+
template <typename F<%= params(n) {|i| ", typename V#{i}"} %>>
|
35
|
+
struct ProtectFunction<%= n %>
|
45
36
|
{
|
46
|
-
|
47
|
-
|
48
|
-
return
|
49
|
-
}
|
37
|
+
F f; <%= params(n, ' ') {|i| "V#{i}* v#{i};"} %>
|
38
|
+
ProtectFunction<%= n %> (F f<%= params(n) {|i| ", V#{i}& v#{i}"} %>) : f(f)<%= params(n) {|i| ", v#{i}(&v#{i})"} %> {}
|
39
|
+
static RubyValue apply (ProtectFunction<%= n %>* fun) {return (*fun->f)(<%= params(n, ', ') {|i| "*fun->v#{i}"} %>);}
|
40
|
+
};
|
41
|
+
|
50
42
|
% end
|
51
43
|
|
52
44
|
% NTIMES.each do |n|
|
53
|
-
|
54
|
-
|
55
|
-
% end
|
56
|
-
inline Value protect (const char* method<%= params(n) {|i| ", P#{i} p#{i}"} %>)
|
45
|
+
template <typename F<%= params(n) {|i| ", typename V#{i}"} %>>
|
46
|
+
inline Value protect (F f<%= params(n) {|i| ", V#{i} v#{i}"} %>)
|
57
47
|
{
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
return protect(rb_eval_string, stringf(
|
62
|
-
"%s(<%= params(n, ", ") {|i| "%s"} %>)", method,
|
63
|
-
<%= params(n, ", ") {|i| "to_str(p#{i}).c_str()"} %>).c_str());
|
64
|
-
% end
|
48
|
+
typedef ProtectFunction<%= n %><F<%= params(n) {|i| ", V#{i}"} %>> Fun;
|
49
|
+
Fun fun(f<%= params(n) {|i| ", v#{i}"} %>);
|
50
|
+
return call_protect((RubyValue(*)(RubyValue)) Fun::apply, (RubyValue) &fun);
|
65
51
|
}
|
66
|
-
% end
|
67
52
|
|
53
|
+
% end
|
68
54
|
|
69
55
|
}// Rucy
|
70
56
|
|
data/include/rucy/module.h.erb
CHANGED
@@ -4,9 +4,8 @@
|
|
4
4
|
#define __RUCY_MODULE_H__
|
5
5
|
|
6
6
|
|
7
|
-
#include <ruby.h>
|
8
|
-
#include <rucy/defs.h>
|
9
7
|
#include <rucy/value.h>
|
8
|
+
#include <rucy/function.h>
|
10
9
|
|
11
10
|
|
12
11
|
namespace Rucy
|
@@ -23,23 +22,23 @@ namespace Rucy
|
|
23
22
|
|
24
23
|
public:
|
25
24
|
|
26
|
-
Module (
|
25
|
+
Module (RubyValue v = nil());
|
27
26
|
|
28
27
|
Module define_module (const char* name);
|
29
28
|
|
30
|
-
Class define_class (const char* name, Value super =
|
29
|
+
Class define_class (const char* name, Value super = nil());
|
31
30
|
|
32
|
-
|
31
|
+
void define_const (const char* name, Value val);
|
33
32
|
|
34
|
-
|
33
|
+
void define_attr (const char* name, bool read = true, bool write = true);
|
35
34
|
|
36
|
-
|
35
|
+
void define_alias (const char* new_, const char* old);
|
37
36
|
|
38
|
-
|
37
|
+
void undef_method (const char* name);
|
39
38
|
|
40
|
-
|
39
|
+
void include_module (Value module);
|
41
40
|
|
42
|
-
|
41
|
+
void extend_module (Value module);
|
43
42
|
|
44
43
|
% [
|
45
44
|
% 'define_function',
|
@@ -47,21 +46,28 @@ namespace Rucy
|
|
47
46
|
% 'define_private_method',
|
48
47
|
% 'define_singleton_method',
|
49
48
|
% ].each do |op|
|
50
|
-
|
49
|
+
void <%= op %> (const char* name, RubyFunctionN fun);
|
50
|
+
|
51
51
|
% NTIMES.each do |n|
|
52
|
-
|
52
|
+
void <%= op %> (const char* name, RubyFunction<%= n %> fun);
|
53
|
+
|
53
54
|
% end
|
54
55
|
% end
|
55
|
-
|
56
56
|
};// Module
|
57
57
|
|
58
58
|
|
59
59
|
Module define_module (const char* name);
|
60
60
|
|
61
|
-
Class define_class (const char* name, Value super =
|
61
|
+
Class define_class (const char* name, Value super = nil());
|
62
62
|
|
63
63
|
void define_const (const char* name, Value val);
|
64
64
|
|
65
|
+
void define_function (const char* name, RubyFunctionN fun);
|
66
|
+
% NTIMES.each do |n|
|
67
|
+
|
68
|
+
void define_function (const char* name, RubyFunction<%= n %> fun);
|
69
|
+
% end
|
70
|
+
|
65
71
|
|
66
72
|
}// Rucy
|
67
73
|
|
data/include/rucy/ruby.h
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
// -*- c++ -*-
|
2
|
+
#pragma once
|
3
|
+
#ifndef __RUCY_RUBY_H__
|
4
|
+
#define __RUCY_RUBY_H__
|
5
|
+
|
6
|
+
|
7
|
+
#include <ruby.h>
|
8
|
+
#include <ruby/intern.h>
|
9
|
+
|
10
|
+
|
11
|
+
namespace Rucy
|
12
|
+
{
|
13
|
+
|
14
|
+
|
15
|
+
typedef VALUE RubyValue;
|
16
|
+
typedef int RubyValueType;
|
17
|
+
typedef ID RubySymbol;
|
18
|
+
|
19
|
+
|
20
|
+
}// Rucy
|
21
|
+
|
22
|
+
|
23
|
+
#endif//EOH
|