portable_mruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +195 -0
- data/exe/portable-mruby +6 -0
- data/lib/portable_mruby/binary_manager.rb +225 -0
- data/lib/portable_mruby/builder.rb +97 -0
- data/lib/portable_mruby/bytecode_compiler.rb +19 -0
- data/lib/portable_mruby/c_generator.rb +94 -0
- data/lib/portable_mruby/cli.rb +136 -0
- data/lib/portable_mruby/version.rb +6 -0
- data/lib/portable_mruby.rb +15 -0
- data/vendor/mruby/bin/mrbc.com +0 -0
- data/vendor/mruby/include/mrbconf.h +230 -0
- data/vendor/mruby/include/mruby/array.h +303 -0
- data/vendor/mruby/include/mruby/boxing_nan.h +169 -0
- data/vendor/mruby/include/mruby/boxing_no.h +59 -0
- data/vendor/mruby/include/mruby/boxing_word.h +251 -0
- data/vendor/mruby/include/mruby/class.h +104 -0
- data/vendor/mruby/include/mruby/common.h +118 -0
- data/vendor/mruby/include/mruby/compile.h +185 -0
- data/vendor/mruby/include/mruby/data.h +76 -0
- data/vendor/mruby/include/mruby/debug.h +75 -0
- data/vendor/mruby/include/mruby/dump.h +159 -0
- data/vendor/mruby/include/mruby/endian.h +44 -0
- data/vendor/mruby/include/mruby/error.h +132 -0
- data/vendor/mruby/include/mruby/gc.h +72 -0
- data/vendor/mruby/include/mruby/gems/mruby-dir/include/dir_hal.h +79 -0
- data/vendor/mruby/include/mruby/gems/mruby-io/include/io_hal.h +451 -0
- data/vendor/mruby/include/mruby/gems/mruby-io/include/mruby/ext/io.h +76 -0
- data/vendor/mruby/include/mruby/gems/mruby-socket/include/socket_hal.h +83 -0
- data/vendor/mruby/include/mruby/gems/mruby-time/include/mruby/time.h +27 -0
- data/vendor/mruby/include/mruby/hash.h +234 -0
- data/vendor/mruby/include/mruby/internal.h +274 -0
- data/vendor/mruby/include/mruby/irep.h +142 -0
- data/vendor/mruby/include/mruby/istruct.h +50 -0
- data/vendor/mruby/include/mruby/khash.h +455 -0
- data/vendor/mruby/include/mruby/mempool.h +19 -0
- data/vendor/mruby/include/mruby/numeric.h +174 -0
- data/vendor/mruby/include/mruby/object.h +45 -0
- data/vendor/mruby/include/mruby/opcode.h +69 -0
- data/vendor/mruby/include/mruby/ops.h +120 -0
- data/vendor/mruby/include/mruby/presym/disable.h +72 -0
- data/vendor/mruby/include/mruby/presym/enable.h +39 -0
- data/vendor/mruby/include/mruby/presym/id.h +1423 -0
- data/vendor/mruby/include/mruby/presym/scanning.h +81 -0
- data/vendor/mruby/include/mruby/presym/table.h +2847 -0
- data/vendor/mruby/include/mruby/presym.h +41 -0
- data/vendor/mruby/include/mruby/proc.h +186 -0
- data/vendor/mruby/include/mruby/range.h +77 -0
- data/vendor/mruby/include/mruby/re.h +16 -0
- data/vendor/mruby/include/mruby/string.h +428 -0
- data/vendor/mruby/include/mruby/throw.h +57 -0
- data/vendor/mruby/include/mruby/value.h +471 -0
- data/vendor/mruby/include/mruby/variable.h +108 -0
- data/vendor/mruby/include/mruby/version.h +143 -0
- data/vendor/mruby/include/mruby.h +1614 -0
- data/vendor/mruby/lib/libmruby.a +0 -0
- metadata +102 -0
|
@@ -0,0 +1,1614 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** mruby - An embeddable Ruby implementation
|
|
3
|
+
**
|
|
4
|
+
** Copyright (c) mruby developers 2010-
|
|
5
|
+
**
|
|
6
|
+
** Permission is hereby granted, free of charge, to any person obtaining
|
|
7
|
+
** a copy of this software and associated documentation files (the
|
|
8
|
+
** "Software"), to deal in the Software without restriction, including
|
|
9
|
+
** without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
+
** distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
+
** permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
+
** the following conditions:
|
|
13
|
+
**
|
|
14
|
+
** The above copyright notice and this permission notice shall be
|
|
15
|
+
** included in all copies or substantial portions of the Software.
|
|
16
|
+
**
|
|
17
|
+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19
|
+
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
20
|
+
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
21
|
+
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
22
|
+
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
23
|
+
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24
|
+
**
|
|
25
|
+
** [ MIT license: https://www.opensource.org/licenses/mit-license.php ]
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @file mruby.h
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
#ifndef MRUBY_H
|
|
33
|
+
#define MRUBY_H
|
|
34
|
+
|
|
35
|
+
#ifdef __cplusplus
|
|
36
|
+
#define __STDC_LIMIT_MACROS
|
|
37
|
+
#define __STDC_CONSTANT_MACROS
|
|
38
|
+
#define __STDC_FORMAT_MACROS
|
|
39
|
+
#endif
|
|
40
|
+
|
|
41
|
+
#include <stdarg.h>
|
|
42
|
+
#include <stdint.h>
|
|
43
|
+
#include <stddef.h>
|
|
44
|
+
#include <limits.h>
|
|
45
|
+
|
|
46
|
+
#ifdef __cplusplus
|
|
47
|
+
#ifndef UINTPTR_MAX
|
|
48
|
+
#error Must be placed `#include <mruby.h>` before `#include <stdint.h>`
|
|
49
|
+
#endif
|
|
50
|
+
#ifndef SIZE_MAX
|
|
51
|
+
#ifdef __SIZE_MAX__
|
|
52
|
+
#define SIZE_MAX __SIZE_MAX__
|
|
53
|
+
#else
|
|
54
|
+
#define SIZE_MAX std::numeric_limits<size_t>::max()
|
|
55
|
+
#endif
|
|
56
|
+
#endif
|
|
57
|
+
#endif
|
|
58
|
+
|
|
59
|
+
#ifdef _MSC_VER
|
|
60
|
+
# define __func__ __FUNCTION__
|
|
61
|
+
#endif
|
|
62
|
+
|
|
63
|
+
#ifdef MRB_DEBUG
|
|
64
|
+
#include <assert.h>
|
|
65
|
+
#define mrb_assert(p) assert(p)
|
|
66
|
+
#define mrb_assert_int_fit(t1,n,t2,max) assert((n)>=0 && ((sizeof(n)<=sizeof(t2))||(n<=(t1)(max))))
|
|
67
|
+
#else
|
|
68
|
+
#define mrb_assert(p) ((void)0)
|
|
69
|
+
#define mrb_assert_int_fit(t1,n,t2,max) ((void)0)
|
|
70
|
+
#endif
|
|
71
|
+
|
|
72
|
+
#if (defined __cplusplus && __cplusplus >= 201703L)
|
|
73
|
+
# define mrb_static_assert(...) static_assert(__VA_ARGS__)
|
|
74
|
+
# define mrb_static_assert1(exp) static_assert(exp)
|
|
75
|
+
# define mrb_static_assert2(exp, str) static_assert(exp, str)
|
|
76
|
+
#elif (defined __cplusplus && __cplusplus >= 201103L) || \
|
|
77
|
+
(defined _MSC_VER) || \
|
|
78
|
+
(defined __GXX_EXPERIMENTAL_CXX0X__) /* for old G++/Clang++ */
|
|
79
|
+
# define mrb_static_assert2(exp, str) static_assert(exp, str)
|
|
80
|
+
#elif defined __STDC_VERSION__ && \
|
|
81
|
+
((__STDC_VERSION__ >= 201112L) || \
|
|
82
|
+
(defined __GNUC__ && __GNUC__ * 100 + __GNUC_MINOR__ >= 406))
|
|
83
|
+
# define mrb_static_assert2(exp, str) _Static_assert(exp, str)
|
|
84
|
+
#else
|
|
85
|
+
# /* alternative implementation of static_assert() */
|
|
86
|
+
# define _mrb_static_assert_cat0(a, b) a##b
|
|
87
|
+
# define _mrb_static_assert_cat(a, b) _mrb_static_assert_cat0(a, b)
|
|
88
|
+
# ifdef __COUNTER__
|
|
89
|
+
# define _mrb_static_assert_id(prefix) _mrb_static_assert_cat(prefix, __COUNTER__)
|
|
90
|
+
# else
|
|
91
|
+
# define _mrb_static_assert_id(prefix) _mrb_static_assert_cat(prefix, __LINE__)
|
|
92
|
+
# endif
|
|
93
|
+
# define mrb_static_assert2(exp, str) \
|
|
94
|
+
struct _mrb_static_assert_id(_mrb_static_assert_) { char x[(exp) ? 1 : -1]; }
|
|
95
|
+
#endif
|
|
96
|
+
|
|
97
|
+
#ifndef mrb_static_assert
|
|
98
|
+
# define mrb_static_assert1(exp) mrb_static_assert2(exp, #exp)
|
|
99
|
+
# define mrb_static_assert_expand(...) __VA_ARGS__ /* for MSVC behaviour - https://stackoverflow.com/q/5530505 */
|
|
100
|
+
# define mrb_static_assert_selector(a, b, name, ...) name
|
|
101
|
+
/**
|
|
102
|
+
* The `mrb_static_assert()` macro function takes one or two arguments.
|
|
103
|
+
*
|
|
104
|
+
* !!!c
|
|
105
|
+
* mrb_static_assert(expect_condition);
|
|
106
|
+
* mrb_static_assert(expect_condition, error_message);
|
|
107
|
+
*/
|
|
108
|
+
# define mrb_static_assert(...) \
|
|
109
|
+
mrb_static_assert_expand(mrb_static_assert_selector(__VA_ARGS__, mrb_static_assert2, mrb_static_assert1, _)(__VA_ARGS__))
|
|
110
|
+
#endif
|
|
111
|
+
|
|
112
|
+
#define mrb_static_assert_powerof2(num) mrb_static_assert((num) > 0 && (num) == ((num) & -(num)), "need power of 2 for " #num)
|
|
113
|
+
|
|
114
|
+
#include "mrbconf.h"
|
|
115
|
+
|
|
116
|
+
#include <mruby/common.h>
|
|
117
|
+
#include <mruby/value.h>
|
|
118
|
+
#include <mruby/gc.h>
|
|
119
|
+
#include <mruby/version.h>
|
|
120
|
+
|
|
121
|
+
#ifndef MRB_NO_FLOAT
|
|
122
|
+
#include <math.h>
|
|
123
|
+
#include <float.h>
|
|
124
|
+
#ifndef FLT_EPSILON
|
|
125
|
+
#define FLT_EPSILON (1.19209290e-07f)
|
|
126
|
+
#endif
|
|
127
|
+
#ifndef DBL_EPSILON
|
|
128
|
+
#define DBL_EPSILON ((double)2.22044604925031308085e-16L)
|
|
129
|
+
#endif
|
|
130
|
+
#ifndef LDBL_EPSILON
|
|
131
|
+
#define LDBL_EPSILON (1.08420217248550443401e-19L)
|
|
132
|
+
#endif
|
|
133
|
+
|
|
134
|
+
#ifdef MRB_USE_FLOAT32
|
|
135
|
+
#define MRB_FLOAT_EPSILON FLT_EPSILON
|
|
136
|
+
#else
|
|
137
|
+
#define MRB_FLOAT_EPSILON DBL_EPSILON
|
|
138
|
+
#endif
|
|
139
|
+
#endif
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* mruby C API entry point
|
|
143
|
+
*/
|
|
144
|
+
MRB_BEGIN_DECL
|
|
145
|
+
|
|
146
|
+
typedef uint8_t mrb_code;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* \class mrb_aspec
|
|
150
|
+
*
|
|
151
|
+
* Specifies the number of arguments a function takes
|
|
152
|
+
*
|
|
153
|
+
* Example: `MRB_ARGS_REQ(2) | MRB_ARGS_OPT(1)` for a method that expects 2..3 arguments
|
|
154
|
+
*/
|
|
155
|
+
typedef uint32_t mrb_aspec;
|
|
156
|
+
|
|
157
|
+
typedef struct mrb_irep mrb_irep;
|
|
158
|
+
|
|
159
|
+
struct mrb_state;
|
|
160
|
+
|
|
161
|
+
#ifndef MRB_FIXED_STATE_ATEXIT_STACK_SIZE
|
|
162
|
+
#define MRB_FIXED_STATE_ATEXIT_STACK_SIZE 5
|
|
163
|
+
#endif
|
|
164
|
+
|
|
165
|
+
typedef struct {
|
|
166
|
+
uint8_t n:4; /* (15=*) c=n|nk<<4 */
|
|
167
|
+
uint8_t nk:4; /* (15=*) */
|
|
168
|
+
uint8_t cci; /* called from C function */
|
|
169
|
+
uint8_t vis; /* 5(ZERO):1(separate module):2(method visibility) */
|
|
170
|
+
/* under 3-bit flags are copied to env, and after that, env takes precedence */
|
|
171
|
+
mrb_sym mid;
|
|
172
|
+
const struct RProc *proc;
|
|
173
|
+
struct RProc *blk;
|
|
174
|
+
mrb_value *stack;
|
|
175
|
+
const mrb_code *pc; /* current address on iseq of this proc */
|
|
176
|
+
union {
|
|
177
|
+
struct REnv *env;
|
|
178
|
+
struct RClass *target_class;
|
|
179
|
+
const void *keep_context; /* if NULL, it means that the fiber has switched; for internal use */
|
|
180
|
+
} u;
|
|
181
|
+
} mrb_callinfo;
|
|
182
|
+
|
|
183
|
+
enum mrb_fiber_state {
|
|
184
|
+
MRB_FIBER_CREATED = 0,
|
|
185
|
+
MRB_FIBER_RUNNING,
|
|
186
|
+
MRB_FIBER_RESUMED,
|
|
187
|
+
MRB_FIBER_SUSPENDED,
|
|
188
|
+
MRB_FIBER_TRANSFERRED,
|
|
189
|
+
MRB_FIBER_TERMINATED,
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/* Task context status aliases */
|
|
193
|
+
#define MRB_TASK_CREATED MRB_FIBER_CREATED
|
|
194
|
+
#define MRB_TASK_STOPPED MRB_FIBER_TERMINATED
|
|
195
|
+
|
|
196
|
+
struct mrb_context {
|
|
197
|
+
struct mrb_context *prev;
|
|
198
|
+
|
|
199
|
+
mrb_value *stbase, *stend; /* stack of virtual machine */
|
|
200
|
+
|
|
201
|
+
mrb_callinfo *ci;
|
|
202
|
+
mrb_callinfo *cibase, *ciend;
|
|
203
|
+
|
|
204
|
+
enum mrb_fiber_state status : 4;
|
|
205
|
+
mrb_bool vmexec : 1;
|
|
206
|
+
struct RFiber *fib;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
#ifdef MRB_METHOD_CACHE_SIZE
|
|
210
|
+
# undef MRB_NO_METHOD_CACHE
|
|
211
|
+
mrb_static_assert_powerof2(MRB_METHOD_CACHE_SIZE);
|
|
212
|
+
#else
|
|
213
|
+
/* default method cache size: 256 */
|
|
214
|
+
/* cache size needs to be power of 2 */
|
|
215
|
+
# define MRB_METHOD_CACHE_SIZE (1<<8)
|
|
216
|
+
#endif
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Function pointer type for a function callable by mruby.
|
|
220
|
+
*
|
|
221
|
+
* The arguments to the function are stored on the mrb_state. To get them see mrb_get_args
|
|
222
|
+
*
|
|
223
|
+
* @param mrb The mruby state
|
|
224
|
+
* @param self The self object
|
|
225
|
+
* @return [mrb_value] The function's return value
|
|
226
|
+
*/
|
|
227
|
+
typedef mrb_value (*mrb_func_t)(struct mrb_state *mrb, mrb_value self);
|
|
228
|
+
|
|
229
|
+
typedef struct {
|
|
230
|
+
uint32_t flags; /* compatible with mt keys in class.c */
|
|
231
|
+
|
|
232
|
+
union {
|
|
233
|
+
const struct RProc *proc;
|
|
234
|
+
mrb_func_t func;
|
|
235
|
+
} as;
|
|
236
|
+
} mrb_method_t;
|
|
237
|
+
|
|
238
|
+
#ifndef MRB_NO_METHOD_CACHE
|
|
239
|
+
struct mrb_cache_entry {
|
|
240
|
+
struct RClass *c, *c0;
|
|
241
|
+
/* mrb_sym mid; // mid is stored in mrb_method_t::flags */
|
|
242
|
+
mrb_method_t m;
|
|
243
|
+
};
|
|
244
|
+
#endif
|
|
245
|
+
|
|
246
|
+
struct mrb_jmpbuf;
|
|
247
|
+
|
|
248
|
+
typedef void (*mrb_atexit_func)(struct mrb_state*);
|
|
249
|
+
|
|
250
|
+
#ifdef MRB_USE_TASK_SCHEDULER
|
|
251
|
+
struct mrb_task;
|
|
252
|
+
|
|
253
|
+
typedef struct mrb_task_state {
|
|
254
|
+
struct mrb_task *queues[4]; /* Task queues (dormant, ready, waiting, suspended) */
|
|
255
|
+
volatile uint32_t tick; /* Current tick count */
|
|
256
|
+
volatile uint32_t wakeup_tick; /* Next wakeup tick */
|
|
257
|
+
volatile mrb_bool switching; /* Context switch pending flag */
|
|
258
|
+
struct mrb_task *main_task; /* Main task wrapper for root context */
|
|
259
|
+
} mrb_task_state;
|
|
260
|
+
#endif
|
|
261
|
+
|
|
262
|
+
typedef struct mrb_state {
|
|
263
|
+
struct mrb_jmpbuf *jmp;
|
|
264
|
+
|
|
265
|
+
struct mrb_context *c;
|
|
266
|
+
struct mrb_context *root_c;
|
|
267
|
+
struct iv_tbl *globals; /* global variable table */
|
|
268
|
+
|
|
269
|
+
struct RObject *exc; /* exception */
|
|
270
|
+
|
|
271
|
+
struct RObject *top_self;
|
|
272
|
+
struct RClass *object_class; /* Object class */
|
|
273
|
+
struct RClass *class_class;
|
|
274
|
+
struct RClass *module_class;
|
|
275
|
+
struct RClass *proc_class;
|
|
276
|
+
struct RClass *string_class;
|
|
277
|
+
struct RClass *array_class;
|
|
278
|
+
struct RClass *hash_class;
|
|
279
|
+
struct RClass *range_class;
|
|
280
|
+
|
|
281
|
+
#ifndef MRB_NO_FLOAT
|
|
282
|
+
struct RClass *float_class;
|
|
283
|
+
#endif
|
|
284
|
+
struct RClass *integer_class;
|
|
285
|
+
struct RClass *true_class;
|
|
286
|
+
struct RClass *false_class;
|
|
287
|
+
struct RClass *nil_class;
|
|
288
|
+
struct RClass *symbol_class;
|
|
289
|
+
struct RClass *kernel_module;
|
|
290
|
+
|
|
291
|
+
mrb_gc gc;
|
|
292
|
+
|
|
293
|
+
mrb_bool bootstrapping;
|
|
294
|
+
|
|
295
|
+
#ifndef MRB_NO_METHOD_CACHE
|
|
296
|
+
struct mrb_cache_entry cache[MRB_METHOD_CACHE_SIZE];
|
|
297
|
+
#endif
|
|
298
|
+
|
|
299
|
+
mrb_sym symidx;
|
|
300
|
+
const char **symtbl;
|
|
301
|
+
size_t symcapa;
|
|
302
|
+
struct mrb_sym_hash_table *symhash;
|
|
303
|
+
#ifndef MRB_USE_ALL_SYMBOLS
|
|
304
|
+
char symbuf[8]; /* buffer for small symbol names */
|
|
305
|
+
#endif
|
|
306
|
+
|
|
307
|
+
#ifdef MRB_USE_DEBUG_HOOK
|
|
308
|
+
void (*code_fetch_hook)(struct mrb_state* mrb, const struct mrb_irep *irep, const mrb_code *pc, mrb_value *regs);
|
|
309
|
+
void (*debug_op_hook)(struct mrb_state* mrb, const struct mrb_irep *irep, const mrb_code *pc, mrb_value *regs);
|
|
310
|
+
#endif
|
|
311
|
+
|
|
312
|
+
#ifdef MRB_BYTECODE_DECODE_OPTION
|
|
313
|
+
mrb_code (*bytecode_decoder)(struct mrb_state* mrb, mrb_code code);
|
|
314
|
+
#endif
|
|
315
|
+
|
|
316
|
+
struct RClass *eException_class;
|
|
317
|
+
struct RClass *eStandardError_class;
|
|
318
|
+
struct RObject *nomem_err; /* pre-allocated NoMemoryError */
|
|
319
|
+
struct RObject *stack_err; /* pre-allocated SystemStackError */
|
|
320
|
+
#ifdef MRB_GC_FIXED_ARENA
|
|
321
|
+
struct RObject *arena_err; /* pre-allocated arena overflow error */
|
|
322
|
+
#endif
|
|
323
|
+
|
|
324
|
+
void *ud; /* auxiliary data */
|
|
325
|
+
|
|
326
|
+
#ifdef MRB_FIXED_STATE_ATEXIT_STACK
|
|
327
|
+
mrb_atexit_func atexit_stack[MRB_FIXED_STATE_ATEXIT_STACK_SIZE];
|
|
328
|
+
#else
|
|
329
|
+
mrb_atexit_func *atexit_stack;
|
|
330
|
+
#endif
|
|
331
|
+
uint16_t atexit_stack_len;
|
|
332
|
+
|
|
333
|
+
#ifdef MRB_USE_TASK_SCHEDULER
|
|
334
|
+
mrb_task_state task; /* Task scheduler state */
|
|
335
|
+
#endif
|
|
336
|
+
} mrb_state;
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Defines a new class.
|
|
340
|
+
*
|
|
341
|
+
* If you're creating a gem it may look something like this:
|
|
342
|
+
*
|
|
343
|
+
* !!!c
|
|
344
|
+
* void mrb_example_gem_init(mrb_state* mrb) {
|
|
345
|
+
* struct RClass *example_class;
|
|
346
|
+
* example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class);
|
|
347
|
+
* }
|
|
348
|
+
*
|
|
349
|
+
* void mrb_example_gem_final(mrb_state* mrb) {
|
|
350
|
+
* //free(TheAnimals);
|
|
351
|
+
* }
|
|
352
|
+
*
|
|
353
|
+
* @param mrb The current mruby state.
|
|
354
|
+
* @param name The name of the defined class.
|
|
355
|
+
* @param super The new class parent.
|
|
356
|
+
* @return [struct RClass *] Reference to the newly defined class.
|
|
357
|
+
* @see mrb_define_class_under
|
|
358
|
+
*/
|
|
359
|
+
MRB_API struct RClass *mrb_define_class(mrb_state *mrb, const char *name, struct RClass *super);
|
|
360
|
+
MRB_API struct RClass *mrb_define_class_id(mrb_state *mrb, mrb_sym name, struct RClass *super);
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Defines a new module.
|
|
364
|
+
*
|
|
365
|
+
* @param mrb The current mruby state.
|
|
366
|
+
* @param name The name of the module.
|
|
367
|
+
* @return [struct RClass *] Reference to the newly defined module.
|
|
368
|
+
*/
|
|
369
|
+
MRB_API struct RClass *mrb_define_module(mrb_state *mrb, const char *name);
|
|
370
|
+
MRB_API struct RClass *mrb_define_module_id(mrb_state *mrb, mrb_sym name);
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Returns the singleton class of an object.
|
|
374
|
+
*
|
|
375
|
+
* Raises a `TypeError` exception for immediate values.
|
|
376
|
+
*/
|
|
377
|
+
MRB_API mrb_value mrb_singleton_class(mrb_state *mrb, mrb_value val);
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Returns the singleton class of an object.
|
|
381
|
+
*
|
|
382
|
+
* Returns `NULL` for immediate values,
|
|
383
|
+
*/
|
|
384
|
+
MRB_API struct RClass *mrb_singleton_class_ptr(mrb_state *mrb, mrb_value val);
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Include a module in another class or module.
|
|
388
|
+
* Equivalent to:
|
|
389
|
+
*
|
|
390
|
+
* module B
|
|
391
|
+
* include A
|
|
392
|
+
* end
|
|
393
|
+
* @param mrb The current mruby state.
|
|
394
|
+
* @param cla A reference to module or a class.
|
|
395
|
+
* @param included A reference to the module to be included.
|
|
396
|
+
*/
|
|
397
|
+
MRB_API void mrb_include_module(mrb_state *mrb, struct RClass *cla, struct RClass *included);
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Prepends a module in another class or module.
|
|
401
|
+
*
|
|
402
|
+
* Equivalent to:
|
|
403
|
+
* module B
|
|
404
|
+
* prepend A
|
|
405
|
+
* end
|
|
406
|
+
* @param mrb The current mruby state.
|
|
407
|
+
* @param cla A reference to module or a class.
|
|
408
|
+
* @param prepended A reference to the module to be prepended.
|
|
409
|
+
*/
|
|
410
|
+
MRB_API void mrb_prepend_module(mrb_state *mrb, struct RClass *cla, struct RClass *prepended);
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Defines a global function in Ruby.
|
|
414
|
+
*
|
|
415
|
+
* If you're creating a gem it may look something like this
|
|
416
|
+
*
|
|
417
|
+
* Example:
|
|
418
|
+
*
|
|
419
|
+
* mrb_value example_method(mrb_state* mrb, mrb_value self)
|
|
420
|
+
* {
|
|
421
|
+
* puts("Executing example command!");
|
|
422
|
+
* return self;
|
|
423
|
+
* }
|
|
424
|
+
*
|
|
425
|
+
* void mrb_example_gem_init(mrb_state* mrb)
|
|
426
|
+
* {
|
|
427
|
+
* mrb_define_method(mrb, mrb->kernel_module, "example_method", example_method, MRB_ARGS_NONE());
|
|
428
|
+
* }
|
|
429
|
+
*
|
|
430
|
+
* @param mrb The mruby state reference.
|
|
431
|
+
* @param cla The class pointer where the method will be defined.
|
|
432
|
+
* @param name The name of the method being defined.
|
|
433
|
+
* @param func The function pointer to the method definition.
|
|
434
|
+
* @param aspec The method parameters declaration.
|
|
435
|
+
*/
|
|
436
|
+
|
|
437
|
+
MRB_API void mrb_define_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec);
|
|
438
|
+
MRB_API void mrb_define_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t func, mrb_aspec aspec);
|
|
439
|
+
MRB_API void mrb_define_private_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec);
|
|
440
|
+
MRB_API void mrb_define_private_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t func, mrb_aspec aspec);
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Defines a class method.
|
|
444
|
+
*
|
|
445
|
+
* Example:
|
|
446
|
+
*
|
|
447
|
+
* # Ruby style
|
|
448
|
+
* class Foo
|
|
449
|
+
* def Foo.bar
|
|
450
|
+
* end
|
|
451
|
+
* end
|
|
452
|
+
* // C style
|
|
453
|
+
* mrb_value bar_method(mrb_state* mrb, mrb_value self){
|
|
454
|
+
* return mrb_nil_value();
|
|
455
|
+
* }
|
|
456
|
+
* void mrb_example_gem_init(mrb_state* mrb){
|
|
457
|
+
* struct RClass *foo;
|
|
458
|
+
* foo = mrb_define_class(mrb, "Foo", mrb->object_class);
|
|
459
|
+
* mrb_define_class_method(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
|
|
460
|
+
* }
|
|
461
|
+
* @param mrb The mruby state reference.
|
|
462
|
+
* @param cla The class where the class method will be defined.
|
|
463
|
+
* @param name The name of the class method being defined.
|
|
464
|
+
* @param fun The function pointer to the class method definition.
|
|
465
|
+
* @param aspec The method parameters declaration.
|
|
466
|
+
*/
|
|
467
|
+
MRB_API void mrb_define_class_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
|
|
468
|
+
MRB_API void mrb_define_class_method_id(mrb_state *mrb, struct RClass *cla, mrb_sym name, mrb_func_t fun, mrb_aspec aspec);
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Defines a singleton method
|
|
472
|
+
*
|
|
473
|
+
* @see mrb_define_class_method
|
|
474
|
+
*/
|
|
475
|
+
MRB_API void mrb_define_singleton_method(mrb_state *mrb, struct RObject *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
|
|
476
|
+
MRB_API void mrb_define_singleton_method_id(mrb_state *mrb, struct RObject *cla, mrb_sym name, mrb_func_t fun, mrb_aspec aspec);
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Defines a module function.
|
|
480
|
+
*
|
|
481
|
+
* Example:
|
|
482
|
+
*
|
|
483
|
+
* # Ruby style
|
|
484
|
+
* module Foo
|
|
485
|
+
* def Foo.bar
|
|
486
|
+
* end
|
|
487
|
+
* end
|
|
488
|
+
* // C style
|
|
489
|
+
* mrb_value bar_method(mrb_state* mrb, mrb_value self){
|
|
490
|
+
* return mrb_nil_value();
|
|
491
|
+
* }
|
|
492
|
+
* void mrb_example_gem_init(mrb_state* mrb){
|
|
493
|
+
* struct RClass *foo;
|
|
494
|
+
* foo = mrb_define_module(mrb, "Foo");
|
|
495
|
+
* mrb_define_module_function(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
|
|
496
|
+
* }
|
|
497
|
+
* @param mrb The mruby state reference.
|
|
498
|
+
* @param cla The module where the module function will be defined.
|
|
499
|
+
* @param name The name of the module function being defined.
|
|
500
|
+
* @param fun The function pointer to the module function definition.
|
|
501
|
+
* @param aspec The method parameters declaration.
|
|
502
|
+
*/
|
|
503
|
+
MRB_API void mrb_define_module_function(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
|
|
504
|
+
MRB_API void mrb_define_module_function_id(mrb_state *mrb, struct RClass *cla, mrb_sym name, mrb_func_t fun, mrb_aspec aspec);
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Defines a constant.
|
|
508
|
+
*
|
|
509
|
+
* Example:
|
|
510
|
+
*
|
|
511
|
+
* # Ruby style
|
|
512
|
+
* class ExampleClass
|
|
513
|
+
* AGE = 22
|
|
514
|
+
* end
|
|
515
|
+
* // C style
|
|
516
|
+
* #include <stdio.h>
|
|
517
|
+
* #include <mruby.h>
|
|
518
|
+
*
|
|
519
|
+
* void
|
|
520
|
+
* mrb_example_gem_init(mrb_state* mrb){
|
|
521
|
+
* mrb_define_const(mrb, mrb->kernel_module, "AGE", mrb_fixnum_value(22));
|
|
522
|
+
* }
|
|
523
|
+
*
|
|
524
|
+
* mrb_value
|
|
525
|
+
* mrb_example_gem_final(mrb_state* mrb){
|
|
526
|
+
* }
|
|
527
|
+
* @param mrb The mruby state reference.
|
|
528
|
+
* @param cla A class or module the constant is defined in.
|
|
529
|
+
* @param name The name of the constant being defined.
|
|
530
|
+
* @param val The value for the constant.
|
|
531
|
+
*/
|
|
532
|
+
MRB_API void mrb_define_const(mrb_state* mrb, struct RClass* cla, const char *name, mrb_value val);
|
|
533
|
+
MRB_API void mrb_define_const_id(mrb_state* mrb, struct RClass* cla, mrb_sym name, mrb_value val);
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Undefines a method.
|
|
537
|
+
*
|
|
538
|
+
* Example:
|
|
539
|
+
*
|
|
540
|
+
* # Ruby style
|
|
541
|
+
*
|
|
542
|
+
* class ExampleClassA
|
|
543
|
+
* def example_method
|
|
544
|
+
* "example"
|
|
545
|
+
* end
|
|
546
|
+
* end
|
|
547
|
+
* ExampleClassA.new.example_method # => example
|
|
548
|
+
*
|
|
549
|
+
* class ExampleClassB < ExampleClassA
|
|
550
|
+
* undef_method :example_method
|
|
551
|
+
* end
|
|
552
|
+
*
|
|
553
|
+
* ExampleClassB.new.example_method # => undefined method 'example_method' for ExampleClassB (NoMethodError)
|
|
554
|
+
*
|
|
555
|
+
* // C style
|
|
556
|
+
* #include <stdio.h>
|
|
557
|
+
* #include <mruby.h>
|
|
558
|
+
*
|
|
559
|
+
* mrb_value
|
|
560
|
+
* mrb_example_method(mrb_state *mrb){
|
|
561
|
+
* return mrb_str_new_lit(mrb, "example");
|
|
562
|
+
* }
|
|
563
|
+
*
|
|
564
|
+
* void
|
|
565
|
+
* mrb_example_gem_init(mrb_state* mrb){
|
|
566
|
+
* struct RClass *example_class_a;
|
|
567
|
+
* struct RClass *example_class_b;
|
|
568
|
+
* struct RClass *example_class_c;
|
|
569
|
+
*
|
|
570
|
+
* example_class_a = mrb_define_class(mrb, "ExampleClassA", mrb->object_class);
|
|
571
|
+
* mrb_define_method(mrb, example_class_a, "example_method", mrb_example_method, MRB_ARGS_NONE());
|
|
572
|
+
* example_class_b = mrb_define_class(mrb, "ExampleClassB", example_class_a);
|
|
573
|
+
* example_class_c = mrb_define_class(mrb, "ExampleClassC", example_class_b);
|
|
574
|
+
* mrb_undef_method(mrb, example_class_c, "example_method");
|
|
575
|
+
* }
|
|
576
|
+
*
|
|
577
|
+
* mrb_example_gem_final(mrb_state* mrb){
|
|
578
|
+
* }
|
|
579
|
+
* @param mrb The mruby state reference.
|
|
580
|
+
* @param cla The class the method will be undefined from.
|
|
581
|
+
* @param name The name of the method to be undefined.
|
|
582
|
+
*/
|
|
583
|
+
MRB_API void mrb_undef_method(mrb_state *mrb, struct RClass *cla, const char *name);
|
|
584
|
+
MRB_API void mrb_undef_method_id(mrb_state*, struct RClass*, mrb_sym);
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Undefine a class method.
|
|
588
|
+
* Example:
|
|
589
|
+
*
|
|
590
|
+
* # Ruby style
|
|
591
|
+
* class ExampleClass
|
|
592
|
+
* def self.example_method
|
|
593
|
+
* "example"
|
|
594
|
+
* end
|
|
595
|
+
* end
|
|
596
|
+
*
|
|
597
|
+
* ExampleClass.example_method
|
|
598
|
+
*
|
|
599
|
+
* // C style
|
|
600
|
+
* #include <stdio.h>
|
|
601
|
+
* #include <mruby.h>
|
|
602
|
+
*
|
|
603
|
+
* mrb_value
|
|
604
|
+
* mrb_example_method(mrb_state *mrb){
|
|
605
|
+
* return mrb_str_new_lit(mrb, "example");
|
|
606
|
+
* }
|
|
607
|
+
*
|
|
608
|
+
* void
|
|
609
|
+
* mrb_example_gem_init(mrb_state* mrb){
|
|
610
|
+
* struct RClass *example_class;
|
|
611
|
+
* example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
|
|
612
|
+
* mrb_define_class_method(mrb, example_class, "example_method", mrb_example_method, MRB_ARGS_NONE());
|
|
613
|
+
* mrb_undef_class_method(mrb, example_class, "example_method");
|
|
614
|
+
* }
|
|
615
|
+
*
|
|
616
|
+
* void
|
|
617
|
+
* mrb_example_gem_final(mrb_state* mrb){
|
|
618
|
+
* }
|
|
619
|
+
* @param mrb The mruby state reference.
|
|
620
|
+
* @param cls A class the class method will be undefined from.
|
|
621
|
+
* @param name The name of the class method to be undefined.
|
|
622
|
+
*/
|
|
623
|
+
MRB_API void mrb_undef_class_method(mrb_state *mrb, struct RClass *cls, const char *name);
|
|
624
|
+
MRB_API void mrb_undef_class_method_id(mrb_state *mrb, struct RClass *cls, mrb_sym name);
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Initialize a new object instance of c class.
|
|
628
|
+
*
|
|
629
|
+
* Example:
|
|
630
|
+
*
|
|
631
|
+
* # Ruby style
|
|
632
|
+
* class ExampleClass
|
|
633
|
+
* end
|
|
634
|
+
*
|
|
635
|
+
* p ExampleClass # => #<ExampleClass:0x9958588>
|
|
636
|
+
* // C style
|
|
637
|
+
* #include <stdio.h>
|
|
638
|
+
* #include <mruby.h>
|
|
639
|
+
*
|
|
640
|
+
* void
|
|
641
|
+
* mrb_example_gem_init(mrb_state* mrb) {
|
|
642
|
+
* struct RClass *example_class;
|
|
643
|
+
* mrb_value obj;
|
|
644
|
+
* example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class); # => class ExampleClass; end
|
|
645
|
+
* obj = mrb_obj_new(mrb, example_class, 0, NULL); # => ExampleClass.new
|
|
646
|
+
* mrb_p(mrb, obj); // => Kernel#p
|
|
647
|
+
* }
|
|
648
|
+
* @param mrb The current mruby state.
|
|
649
|
+
* @param c Reference to the class of the new object.
|
|
650
|
+
* @param argc Number of arguments in argv
|
|
651
|
+
* @param argv Array of mrb_value to initialize the object
|
|
652
|
+
* @return [mrb_value] The newly initialized object
|
|
653
|
+
*/
|
|
654
|
+
MRB_API mrb_value mrb_obj_new(mrb_state *mrb, struct RClass *c, mrb_int argc, const mrb_value *argv);
|
|
655
|
+
|
|
656
|
+
/** @see mrb_obj_new */
|
|
657
|
+
MRB_INLINE mrb_value mrb_class_new_instance(mrb_state *mrb, mrb_int argc, const mrb_value *argv, struct RClass *c)
|
|
658
|
+
{
|
|
659
|
+
return mrb_obj_new(mrb,c,argc,argv);
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* Creates a new instance of Class, Class.
|
|
664
|
+
*
|
|
665
|
+
* Example:
|
|
666
|
+
*
|
|
667
|
+
* void
|
|
668
|
+
* mrb_example_gem_init(mrb_state* mrb) {
|
|
669
|
+
* struct RClass *example_class;
|
|
670
|
+
*
|
|
671
|
+
* mrb_value obj;
|
|
672
|
+
* example_class = mrb_class_new(mrb, mrb->object_class);
|
|
673
|
+
* obj = mrb_obj_new(mrb, example_class, 0, NULL); // => #<#<Class:0x9a945b8>:0x9a94588>
|
|
674
|
+
* mrb_p(mrb, obj); // => Kernel#p
|
|
675
|
+
* }
|
|
676
|
+
*
|
|
677
|
+
* @param mrb The current mruby state.
|
|
678
|
+
* @param super The super class or parent.
|
|
679
|
+
* @return [struct RClass *] Reference to the new class.
|
|
680
|
+
*/
|
|
681
|
+
MRB_API struct RClass * mrb_class_new(mrb_state *mrb, struct RClass *super);
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Creates a new module, Module.
|
|
685
|
+
*
|
|
686
|
+
* Example:
|
|
687
|
+
* void
|
|
688
|
+
* mrb_example_gem_init(mrb_state* mrb) {
|
|
689
|
+
* struct RClass *example_module;
|
|
690
|
+
*
|
|
691
|
+
* example_module = mrb_module_new(mrb);
|
|
692
|
+
* }
|
|
693
|
+
*
|
|
694
|
+
* @param mrb The current mruby state.
|
|
695
|
+
* @return [struct RClass *] Reference to the new module.
|
|
696
|
+
*/
|
|
697
|
+
MRB_API struct RClass * mrb_module_new(mrb_state *mrb);
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Returns an mrb_bool. True if class was defined, and false if the class was not defined.
|
|
701
|
+
*
|
|
702
|
+
* Example:
|
|
703
|
+
* void
|
|
704
|
+
* mrb_example_gem_init(mrb_state* mrb) {
|
|
705
|
+
* struct RClass *example_class;
|
|
706
|
+
* mrb_bool cd;
|
|
707
|
+
*
|
|
708
|
+
* example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
|
|
709
|
+
* cd = mrb_class_defined(mrb, "ExampleClass");
|
|
710
|
+
*
|
|
711
|
+
* // If mrb_class_defined returns TRUE then puts "True"
|
|
712
|
+
* // If mrb_class_defined returns FALSE then puts "False"
|
|
713
|
+
* if (cd) {
|
|
714
|
+
* puts("True");
|
|
715
|
+
* }
|
|
716
|
+
* else {
|
|
717
|
+
* puts("False");
|
|
718
|
+
* }
|
|
719
|
+
* }
|
|
720
|
+
*
|
|
721
|
+
* @param mrb The current mruby state.
|
|
722
|
+
* @param name A string representing the name of the class.
|
|
723
|
+
* @return [mrb_bool] A boolean value.
|
|
724
|
+
*/
|
|
725
|
+
MRB_API mrb_bool mrb_class_defined(mrb_state *mrb, const char *name);
|
|
726
|
+
MRB_API mrb_bool mrb_class_defined_id(mrb_state *mrb, mrb_sym name);
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Gets a class.
|
|
730
|
+
* @param mrb The current mruby state.
|
|
731
|
+
* @param name The name of the class.
|
|
732
|
+
* @return [struct RClass *] A reference to the class.
|
|
733
|
+
*/
|
|
734
|
+
MRB_API struct RClass* mrb_class_get(mrb_state *mrb, const char *name);
|
|
735
|
+
MRB_API struct RClass* mrb_class_get_id(mrb_state *mrb, mrb_sym name);
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* Gets a exception class.
|
|
739
|
+
* @param mrb The current mruby state.
|
|
740
|
+
* @param name The name of the class.
|
|
741
|
+
* @return [struct RClass *] A reference to the class.
|
|
742
|
+
*/
|
|
743
|
+
MRB_API struct RClass* mrb_exc_get_id(mrb_state *mrb, mrb_sym name);
|
|
744
|
+
#define mrb_exc_get(mrb, name) mrb_exc_get_id(mrb, mrb_intern_cstr(mrb, name))
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* Returns an mrb_bool. True if inner class was defined, and false if the inner class was not defined.
|
|
748
|
+
*
|
|
749
|
+
* Example:
|
|
750
|
+
* void
|
|
751
|
+
* mrb_example_gem_init(mrb_state* mrb) {
|
|
752
|
+
* struct RClass *example_outer, *example_inner;
|
|
753
|
+
* mrb_bool cd;
|
|
754
|
+
*
|
|
755
|
+
* example_outer = mrb_define_module(mrb, "ExampleOuter");
|
|
756
|
+
*
|
|
757
|
+
* example_inner = mrb_define_class_under(mrb, example_outer, "ExampleInner", mrb->object_class);
|
|
758
|
+
* cd = mrb_class_defined_under(mrb, example_outer, "ExampleInner");
|
|
759
|
+
*
|
|
760
|
+
* // If mrb_class_defined_under returns TRUE then puts "True"
|
|
761
|
+
* // If mrb_class_defined_under returns FALSE then puts "False"
|
|
762
|
+
* if (cd) {
|
|
763
|
+
* puts("True");
|
|
764
|
+
* }
|
|
765
|
+
* else {
|
|
766
|
+
* puts("False");
|
|
767
|
+
* }
|
|
768
|
+
* }
|
|
769
|
+
*
|
|
770
|
+
* @param mrb The current mruby state.
|
|
771
|
+
* @param outer The name of the outer class.
|
|
772
|
+
* @param name A string representing the name of the inner class.
|
|
773
|
+
* @return [mrb_bool] A boolean value.
|
|
774
|
+
*/
|
|
775
|
+
MRB_API mrb_bool mrb_class_defined_under(mrb_state *mrb, struct RClass *outer, const char *name);
|
|
776
|
+
MRB_API mrb_bool mrb_class_defined_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name);
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* Gets a child class.
|
|
780
|
+
* @param mrb The current mruby state.
|
|
781
|
+
* @param outer The name of the parent class.
|
|
782
|
+
* @param name The name of the class.
|
|
783
|
+
* @return [struct RClass *] A reference to the class.
|
|
784
|
+
*/
|
|
785
|
+
MRB_API struct RClass * mrb_class_get_under(mrb_state *mrb, struct RClass *outer, const char *name);
|
|
786
|
+
MRB_API struct RClass * mrb_class_get_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name);
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* Gets a module.
|
|
790
|
+
* @param mrb The current mruby state.
|
|
791
|
+
* @param name The name of the module.
|
|
792
|
+
* @return [struct RClass *] A reference to the module.
|
|
793
|
+
*/
|
|
794
|
+
MRB_API struct RClass * mrb_module_get(mrb_state *mrb, const char *name);
|
|
795
|
+
MRB_API struct RClass * mrb_module_get_id(mrb_state *mrb, mrb_sym name);
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Gets a module defined under another module.
|
|
799
|
+
* @param mrb The current mruby state.
|
|
800
|
+
* @param outer The name of the outer module.
|
|
801
|
+
* @param name The name of the module.
|
|
802
|
+
* @return [struct RClass *] A reference to the module.
|
|
803
|
+
*/
|
|
804
|
+
MRB_API struct RClass * mrb_module_get_under(mrb_state *mrb, struct RClass *outer, const char *name);
|
|
805
|
+
MRB_API struct RClass * mrb_module_get_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name);
|
|
806
|
+
|
|
807
|
+
/* a function to raise NotImplementedError with current method name */
|
|
808
|
+
MRB_API void mrb_notimplement(mrb_state*);
|
|
809
|
+
/* a function to be replacement of unimplemented method */
|
|
810
|
+
MRB_API mrb_value mrb_notimplement_m(mrb_state*, mrb_value);
|
|
811
|
+
/* just return it self */
|
|
812
|
+
MRB_API mrb_value mrb_obj_itself(mrb_state*, mrb_value);
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* Duplicate an object.
|
|
816
|
+
*
|
|
817
|
+
* Equivalent to:
|
|
818
|
+
* Object#dup
|
|
819
|
+
* @param mrb The current mruby state.
|
|
820
|
+
* @param obj Object to be duplicate.
|
|
821
|
+
* @return [mrb_value] The newly duplicated object.
|
|
822
|
+
*/
|
|
823
|
+
MRB_API mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj);
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* Returns true if obj responds to the given method. If the method was defined for that
|
|
827
|
+
* class it returns true, it returns false otherwise.
|
|
828
|
+
*
|
|
829
|
+
* Example:
|
|
830
|
+
* # Ruby style
|
|
831
|
+
* class ExampleClass
|
|
832
|
+
* def example_method
|
|
833
|
+
* end
|
|
834
|
+
* end
|
|
835
|
+
*
|
|
836
|
+
* ExampleClass.new.respond_to?(:example_method) # => true
|
|
837
|
+
*
|
|
838
|
+
* // C style
|
|
839
|
+
* void
|
|
840
|
+
* mrb_example_gem_init(mrb_state* mrb) {
|
|
841
|
+
* struct RClass *example_class;
|
|
842
|
+
* mrb_sym mid;
|
|
843
|
+
* mrb_bool obj_resp;
|
|
844
|
+
*
|
|
845
|
+
* example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
|
|
846
|
+
* mrb_define_method(mrb, example_class, "example_method", exampleMethod, MRB_ARGS_NONE());
|
|
847
|
+
* mid = mrb_intern_str(mrb, mrb_str_new_lit(mrb, "example_method" ));
|
|
848
|
+
* obj_resp = mrb_obj_respond_to(mrb, example_class, mid); // => TRUE (true in Ruby world)
|
|
849
|
+
*
|
|
850
|
+
* // If mrb_obj_respond_to returns TRUE then puts "True"
|
|
851
|
+
* // If mrb_obj_respond_to returns FALSE then puts "False"
|
|
852
|
+
* if (obj_resp) {
|
|
853
|
+
* puts("True");
|
|
854
|
+
* }
|
|
855
|
+
* else {
|
|
856
|
+
* puts("False");
|
|
857
|
+
* }
|
|
858
|
+
* }
|
|
859
|
+
*
|
|
860
|
+
* @param mrb The current mruby state.
|
|
861
|
+
* @param c A reference to a class.
|
|
862
|
+
* @param mid A symbol referencing a method id.
|
|
863
|
+
* @return [mrb_bool] A boolean value.
|
|
864
|
+
*/
|
|
865
|
+
MRB_API mrb_bool mrb_obj_respond_to(mrb_state *mrb, struct RClass* c, mrb_sym mid);
|
|
866
|
+
|
|
867
|
+
/**
|
|
868
|
+
* Defines a new class under a given module
|
|
869
|
+
*
|
|
870
|
+
* @param mrb The current mruby state.
|
|
871
|
+
* @param outer Reference to the module under which the new class will be defined
|
|
872
|
+
* @param name The name of the defined class
|
|
873
|
+
* @param super The new class parent
|
|
874
|
+
* @return [struct RClass *] Reference to the newly defined class
|
|
875
|
+
* @see mrb_define_class
|
|
876
|
+
*/
|
|
877
|
+
MRB_API struct RClass* mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super);
|
|
878
|
+
MRB_API struct RClass* mrb_define_class_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name, struct RClass *super);
|
|
879
|
+
|
|
880
|
+
MRB_API struct RClass* mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name);
|
|
881
|
+
MRB_API struct RClass* mrb_define_module_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name);
|
|
882
|
+
|
|
883
|
+
/**
|
|
884
|
+
* Function requires n arguments.
|
|
885
|
+
*
|
|
886
|
+
* @param n
|
|
887
|
+
* The number of required arguments.
|
|
888
|
+
*/
|
|
889
|
+
#define MRB_ARGS_REQ(n) ((mrb_aspec)((n)&0x1f) << 18)
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* Function takes n optional arguments
|
|
893
|
+
*
|
|
894
|
+
* @param n
|
|
895
|
+
* The number of optional arguments.
|
|
896
|
+
*/
|
|
897
|
+
#define MRB_ARGS_OPT(n) ((mrb_aspec)((n)&0x1f) << 13)
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* Function takes n1 mandatory arguments and n2 optional arguments
|
|
901
|
+
*
|
|
902
|
+
* @param n1
|
|
903
|
+
* The number of required arguments.
|
|
904
|
+
* @param n2
|
|
905
|
+
* The number of optional arguments.
|
|
906
|
+
*/
|
|
907
|
+
#define MRB_ARGS_ARG(n1,n2) (MRB_ARGS_REQ(n1)|MRB_ARGS_OPT(n2))
|
|
908
|
+
|
|
909
|
+
/** rest argument */
|
|
910
|
+
#define MRB_ARGS_REST() ((mrb_aspec)(1 << 12))
|
|
911
|
+
|
|
912
|
+
/** required arguments after rest */
|
|
913
|
+
#define MRB_ARGS_POST(n) ((mrb_aspec)((n)&0x1f) << 7)
|
|
914
|
+
|
|
915
|
+
/** keyword arguments (n of keys, kdict) */
|
|
916
|
+
#define MRB_ARGS_KEY(n1,n2) ((mrb_aspec)((((n1)&0x1f) << 2) | ((n2)?(1<<1):0)))
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Function takes a block argument
|
|
920
|
+
*/
|
|
921
|
+
#define MRB_ARGS_BLOCK() ((mrb_aspec)1)
|
|
922
|
+
|
|
923
|
+
/**
|
|
924
|
+
* Function accepts any number of arguments
|
|
925
|
+
*/
|
|
926
|
+
#define MRB_ARGS_ANY() MRB_ARGS_REST()
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
* Function accepts no arguments
|
|
930
|
+
*/
|
|
931
|
+
#define MRB_ARGS_NONE() ((mrb_aspec)0)
|
|
932
|
+
|
|
933
|
+
/**
|
|
934
|
+
* Format specifiers for {mrb_get_args} function
|
|
935
|
+
*
|
|
936
|
+
* Must be a C string composed of the following format specifiers:
|
|
937
|
+
*
|
|
938
|
+
* | char | Ruby type | C types | Notes |
|
|
939
|
+
* |:----:|----------------|-------------------|----------------------------------------------------|
|
|
940
|
+
* | `o` | {Object} | {mrb_value} | Could be used to retrieve any type of argument |
|
|
941
|
+
* | `C` | {Class}/{Module} | {mrb_value} | when `!` follows, the value may be `nil` |
|
|
942
|
+
* | `S` | {String} | {mrb_value} | when `!` follows, the value may be `nil` |
|
|
943
|
+
* | `A` | {Array} | {mrb_value} | when `!` follows, the value may be `nil` |
|
|
944
|
+
* | `H` | {Hash} | {mrb_value} | when `!` follows, the value may be `nil` |
|
|
945
|
+
* | `s` | {String} | const char *, {mrb_int} | Receive two arguments; `s!` gives (`NULL`,`0`) for `nil` |
|
|
946
|
+
* | `z` | {String} | const char * | `NULL` terminated string; `z!` gives `NULL` for `nil` |
|
|
947
|
+
* | `a` | {Array} | const {mrb_value} *, {mrb_int} | Receive two arguments; `a!` gives (`NULL`,`0`) for `nil` |
|
|
948
|
+
* | `c` | {Class}/{Module} | strcut RClass * | `c!` gives `NULL` for `nil` |
|
|
949
|
+
* | `f` | {Integer}/{Float} | {mrb_float} | |
|
|
950
|
+
* | `i` | {Integer}/{Float} | {mrb_int} | |
|
|
951
|
+
* | `b` | boolean | {mrb_bool} | |
|
|
952
|
+
* | `n` | {String}/{Symbol} | {mrb_sym} | |
|
|
953
|
+
* | `d` | data | void *, {mrb_data_type} const | 2nd argument will be used to check data type so it won't be modified; when `!` follows, the value may be `nil` |
|
|
954
|
+
* | `I` | inline struct | void *, struct RClass | `I!` gives `NULL` for `nil` |
|
|
955
|
+
* | `&` | block | {mrb_value} | &! raises exception if no block given. |
|
|
956
|
+
* | `*` | rest arguments | const {mrb_value} *, {mrb_int} | Receive the rest of arguments as an array; `*!` avoid copy of the stack. |
|
|
957
|
+
* | `\|` | optional | | After this spec following specs would be optional. |
|
|
958
|
+
* | `?` | optional given | {mrb_bool} | `TRUE` if preceding argument is given. Used to check optional argument is given. |
|
|
959
|
+
* | `:` | keyword args | {mrb_kwargs} const | Get keyword arguments. @see mrb_kwargs |
|
|
960
|
+
*
|
|
961
|
+
* @see mrb_get_args
|
|
962
|
+
*
|
|
963
|
+
* Immediately after format specifiers it can add format modifiers:
|
|
964
|
+
*
|
|
965
|
+
* | char | Notes |
|
|
966
|
+
* |:----:|-----------------------------------------------------------------------------------------|
|
|
967
|
+
* | `!` | Switch to the alternate mode; The behaviour changes depending on the format specifier |
|
|
968
|
+
* | `+` | Request a not frozen object; However, except nil value |
|
|
969
|
+
*/
|
|
970
|
+
typedef const char *mrb_args_format;
|
|
971
|
+
|
|
972
|
+
/**
|
|
973
|
+
* Get keyword arguments by `mrb_get_args()` with `:` specifier.
|
|
974
|
+
*
|
|
975
|
+
* `mrb_kwargs::num` indicates that the total number of keyword values.
|
|
976
|
+
*
|
|
977
|
+
* `mrb_kwargs::required` indicates that the specified number of keywords starting from the beginning of the `mrb_sym` array are required.
|
|
978
|
+
*
|
|
979
|
+
* `mrb_kwargs::table` accepts a `mrb_sym` array of C.
|
|
980
|
+
*
|
|
981
|
+
* `mrb_kwargs::values` is an object array of C, and the keyword argument corresponding to the `mrb_sym` array is assigned.
|
|
982
|
+
* Note that `undef` is assigned if there is no keyword argument corresponding over `mrb_kwargs::required` to `mrb_kwargs::num`.
|
|
983
|
+
*
|
|
984
|
+
* `mrb_kwargs::rest` is the remaining keyword argument that can be accepted as `**rest` in Ruby.
|
|
985
|
+
* If `NULL` is specified, `ArgumentError` is raised when there is an undefined keyword.
|
|
986
|
+
*
|
|
987
|
+
* Examples:
|
|
988
|
+
*
|
|
989
|
+
* // def method(a: 1, b: 2)
|
|
990
|
+
*
|
|
991
|
+
* mrb_int kw_num = 2;
|
|
992
|
+
* mrb_int kw_required = 0;
|
|
993
|
+
* mrb_sym kw_names[] = { mrb_intern_lit(mrb, "a"), mrb_intern_lit(mrb, "b") };
|
|
994
|
+
* mrb_value kw_values[kw_num];
|
|
995
|
+
* mrb_kwargs kwargs = { kw_num, kw_required, kw_names, kw_values, NULL };
|
|
996
|
+
*
|
|
997
|
+
* mrb_get_args(mrb, ":", &kwargs);
|
|
998
|
+
* if (mrb_undef_p(kw_values[0])) { kw_values[0] = mrb_fixnum_value(1); }
|
|
999
|
+
* if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); }
|
|
1000
|
+
*
|
|
1001
|
+
*
|
|
1002
|
+
* // def method(str, x:, y: 2, z: "default string", **opts)
|
|
1003
|
+
*
|
|
1004
|
+
* mrb_value str, kw_rest;
|
|
1005
|
+
* uint32_t kw_num = 3;
|
|
1006
|
+
* uint32_t kw_required = 1;
|
|
1007
|
+
* // Note that `#include <mruby/presym.h>` is required beforehand because `MRB_SYM()` is used.
|
|
1008
|
+
* // If the usage of `MRB_SYM()` is not desired, replace it with `mrb_intern_lit()`.
|
|
1009
|
+
* mrb_sym kw_names[] = { MRB_SYM(x), MRB_SYM(y), MRB_SYM(z) };
|
|
1010
|
+
* mrb_value kw_values[kw_num];
|
|
1011
|
+
* mrb_kwargs kwargs = { kw_num, kw_required, kw_names, kw_values, &kw_rest };
|
|
1012
|
+
*
|
|
1013
|
+
* mrb_get_args(mrb, "S:", &str, &kwargs);
|
|
1014
|
+
* // or: mrb_get_args(mrb, ":S", &kwargs, &str);
|
|
1015
|
+
* if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); }
|
|
1016
|
+
* if (mrb_undef_p(kw_values[2])) { kw_values[2] = mrb_str_new_cstr(mrb, "default string"); }
|
|
1017
|
+
*/
|
|
1018
|
+
typedef struct mrb_kwargs mrb_kwargs;
|
|
1019
|
+
|
|
1020
|
+
struct mrb_kwargs
|
|
1021
|
+
{
|
|
1022
|
+
mrb_int num; /* number of keyword arguments */
|
|
1023
|
+
mrb_int required; /* number of required keyword arguments */
|
|
1024
|
+
const mrb_sym *table; /* C array of symbols for keyword names */
|
|
1025
|
+
mrb_value *values; /* keyword argument values */
|
|
1026
|
+
mrb_value *rest; /* keyword rest (dict) */
|
|
1027
|
+
};
|
|
1028
|
+
|
|
1029
|
+
/**
|
|
1030
|
+
* Retrieve arguments from mrb_state.
|
|
1031
|
+
*
|
|
1032
|
+
* @param mrb The current mruby state.
|
|
1033
|
+
* @param format is a list of format specifiers
|
|
1034
|
+
* @param ... The passing variadic arguments must be a pointer of retrieving type.
|
|
1035
|
+
* @return the number of arguments retrieved.
|
|
1036
|
+
* @see mrb_args_format
|
|
1037
|
+
* @see mrb_kwargs
|
|
1038
|
+
*/
|
|
1039
|
+
MRB_API mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format, ...);
|
|
1040
|
+
|
|
1041
|
+
/**
|
|
1042
|
+
* Array version of mrb_get_args()
|
|
1043
|
+
*
|
|
1044
|
+
* @param ptr Array of void*, in the same order as the varargs version.
|
|
1045
|
+
*/
|
|
1046
|
+
MRB_API mrb_int mrb_get_args_a(mrb_state *mrb, mrb_args_format format, void** ptr);
|
|
1047
|
+
|
|
1048
|
+
MRB_INLINE mrb_sym
|
|
1049
|
+
mrb_get_mid(mrb_state *mrb) /* get method symbol */
|
|
1050
|
+
{
|
|
1051
|
+
return mrb->c->ci->mid;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
/**
|
|
1055
|
+
* Retrieve number of arguments from mrb_state.
|
|
1056
|
+
*
|
|
1057
|
+
* Correctly handles *splat arguments.
|
|
1058
|
+
*/
|
|
1059
|
+
MRB_API mrb_int mrb_get_argc(mrb_state *mrb);
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* Retrieve an array of arguments from mrb_state.
|
|
1063
|
+
*
|
|
1064
|
+
* Correctly handles *splat arguments.
|
|
1065
|
+
*/
|
|
1066
|
+
MRB_API const mrb_value *mrb_get_argv(mrb_state *mrb);
|
|
1067
|
+
|
|
1068
|
+
/**
|
|
1069
|
+
* Retrieve the first and only argument from mrb_state.
|
|
1070
|
+
* Raises ArgumentError unless the number of arguments is exactly one.
|
|
1071
|
+
*
|
|
1072
|
+
* Correctly handles *splat arguments.
|
|
1073
|
+
*/
|
|
1074
|
+
MRB_API mrb_value mrb_get_arg1(mrb_state *mrb);
|
|
1075
|
+
|
|
1076
|
+
/**
|
|
1077
|
+
* Check if a block argument is given from mrb_state.
|
|
1078
|
+
*/
|
|
1079
|
+
MRB_API mrb_bool mrb_block_given_p(mrb_state *mrb);
|
|
1080
|
+
|
|
1081
|
+
/* `strlen` for character string literals (use with caution or `strlen` instead)
|
|
1082
|
+
Adjacent string literals are concatenated in C/C++ in translation phase 6.
|
|
1083
|
+
If `lit` is not one, the compiler will report a syntax error:
|
|
1084
|
+
MSVC: "error C2143: syntax error : missing ')' before 'string'"
|
|
1085
|
+
GCC: "error: expected ')' before string constant"
|
|
1086
|
+
*/
|
|
1087
|
+
#define mrb_strlen_lit(lit) (sizeof(lit "") - 1)
|
|
1088
|
+
|
|
1089
|
+
/**
|
|
1090
|
+
* Call existing Ruby functions.
|
|
1091
|
+
*
|
|
1092
|
+
* Example:
|
|
1093
|
+
*
|
|
1094
|
+
* #include <stdio.h>
|
|
1095
|
+
* #include <mruby.h>
|
|
1096
|
+
* #include <mruby/compile.h>
|
|
1097
|
+
*
|
|
1098
|
+
* int
|
|
1099
|
+
* main()
|
|
1100
|
+
* {
|
|
1101
|
+
* mrb_int i = 99;
|
|
1102
|
+
* mrb_state *mrb = mrb_open();
|
|
1103
|
+
*
|
|
1104
|
+
* if (!mrb) { }
|
|
1105
|
+
* FILE *fp = fopen("test.rb","r");
|
|
1106
|
+
* mrb_value obj = mrb_load_file(mrb,fp);
|
|
1107
|
+
* mrb_funcall(mrb, obj, "method_name", 1, mrb_fixnum_value(i));
|
|
1108
|
+
* mrb_funcall_id(mrb, obj, MRB_SYM(method_name), 1, mrb_fixnum_value(i));
|
|
1109
|
+
* fclose(fp);
|
|
1110
|
+
* mrb_close(mrb);
|
|
1111
|
+
* }
|
|
1112
|
+
*
|
|
1113
|
+
* @param mrb The current mruby state.
|
|
1114
|
+
* @param val A reference to an mruby value.
|
|
1115
|
+
* @param name The name of the method.
|
|
1116
|
+
* @param argc The number of arguments the method has.
|
|
1117
|
+
* @param ... Variadic values(not type safe!).
|
|
1118
|
+
* @return [mrb_value] mruby function value.
|
|
1119
|
+
*/
|
|
1120
|
+
MRB_API mrb_value mrb_funcall(mrb_state *mrb, mrb_value val, const char *name, mrb_int argc, ...);
|
|
1121
|
+
MRB_API mrb_value mrb_funcall_id(mrb_state *mrb, mrb_value val, mrb_sym mid, mrb_int argc, ...);
|
|
1122
|
+
/**
|
|
1123
|
+
* Call existing Ruby functions. This is basically the type safe version of mrb_funcall.
|
|
1124
|
+
*
|
|
1125
|
+
* #include <stdio.h>
|
|
1126
|
+
* #include <mruby.h>
|
|
1127
|
+
* #include <mruby/compile.h>
|
|
1128
|
+
* int
|
|
1129
|
+
* main()
|
|
1130
|
+
* {
|
|
1131
|
+
* mrb_state *mrb = mrb_open();
|
|
1132
|
+
* mrb_value obj = mrb_fixnum_value(1);
|
|
1133
|
+
*
|
|
1134
|
+
* if (!mrb) { }
|
|
1135
|
+
*
|
|
1136
|
+
* FILE *fp = fopen("test.rb","r");
|
|
1137
|
+
* mrb_value obj = mrb_load_file(mrb,fp);
|
|
1138
|
+
* mrb_funcall_argv(mrb, obj, MRB_SYM(method_name), 1, &obj); // Calling Ruby function from test.rb.
|
|
1139
|
+
* fclose(fp);
|
|
1140
|
+
* mrb_close(mrb);
|
|
1141
|
+
* }
|
|
1142
|
+
* @param mrb The current mruby state.
|
|
1143
|
+
* @param val A reference to an mruby value.
|
|
1144
|
+
* @param name_sym The symbol representing the method.
|
|
1145
|
+
* @param argc The number of arguments the method has.
|
|
1146
|
+
* @param obj Pointer to the object.
|
|
1147
|
+
* @return [mrb_value] mrb_value mruby function value.
|
|
1148
|
+
* @see mrb_funcall
|
|
1149
|
+
*/
|
|
1150
|
+
MRB_API mrb_value mrb_funcall_argv(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv);
|
|
1151
|
+
/**
|
|
1152
|
+
* Call existing Ruby functions with a block.
|
|
1153
|
+
*/
|
|
1154
|
+
MRB_API mrb_value mrb_funcall_with_block(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv, mrb_value block);
|
|
1155
|
+
/**
|
|
1156
|
+
* Create a symbol from C string. But usually it's better to
|
|
1157
|
+
* use MRB_SYM, MRB_OPSYM, MRB_CVSYM, MRB_IVSYM, MRB_GVSYM,
|
|
1158
|
+
* MRB_SYM_B, MRB_SYM_Q, MRB_SYM_E macros.
|
|
1159
|
+
*
|
|
1160
|
+
* Example:
|
|
1161
|
+
*
|
|
1162
|
+
* # Ruby style:
|
|
1163
|
+
* :pizza # => :pizza
|
|
1164
|
+
*
|
|
1165
|
+
* // C style:
|
|
1166
|
+
* mrb_sym sym1 = mrb_intern_lit(mrb, "pizza"); // => :pizza
|
|
1167
|
+
* mrb_sym sym2 = MRB_SYM(pizza); // => :pizza
|
|
1168
|
+
* mrb_sym sym3 = MRB_SYM_Q(pizza); // => :pizza?
|
|
1169
|
+
*
|
|
1170
|
+
* @param mrb The current mruby state.
|
|
1171
|
+
* @param str The string to be symbolized
|
|
1172
|
+
* @return [mrb_sym] mrb_sym A symbol.
|
|
1173
|
+
*/
|
|
1174
|
+
MRB_API mrb_sym mrb_intern_cstr(mrb_state *mrb, const char* str);
|
|
1175
|
+
MRB_API mrb_sym mrb_intern(mrb_state*,const char*,size_t);
|
|
1176
|
+
MRB_API mrb_sym mrb_intern_static(mrb_state*,const char*,size_t);
|
|
1177
|
+
#define mrb_intern_lit(mrb, lit) mrb_intern_static(mrb, (lit ""), mrb_strlen_lit(lit))
|
|
1178
|
+
MRB_API mrb_sym mrb_intern_str(mrb_state*,mrb_value);
|
|
1179
|
+
/* mrb_intern_check series functions returns 0 if the symbol is not defined */
|
|
1180
|
+
MRB_API mrb_sym mrb_intern_check_cstr(mrb_state*,const char*);
|
|
1181
|
+
MRB_API mrb_sym mrb_intern_check(mrb_state*,const char*,size_t);
|
|
1182
|
+
MRB_API mrb_sym mrb_intern_check_str(mrb_state*,mrb_value);
|
|
1183
|
+
/* mrb_check_intern series functions returns nil if the symbol is not defined */
|
|
1184
|
+
/* otherwise returns mrb_value */
|
|
1185
|
+
MRB_API mrb_value mrb_check_intern_cstr(mrb_state*,const char*);
|
|
1186
|
+
MRB_API mrb_value mrb_check_intern(mrb_state*,const char*,size_t);
|
|
1187
|
+
MRB_API mrb_value mrb_check_intern_str(mrb_state*,mrb_value);
|
|
1188
|
+
MRB_API const char *mrb_sym_name(mrb_state*,mrb_sym);
|
|
1189
|
+
MRB_API const char *mrb_sym_name_len(mrb_state*,mrb_sym,mrb_int*);
|
|
1190
|
+
MRB_API const char *mrb_sym_dump(mrb_state*,mrb_sym);
|
|
1191
|
+
MRB_API mrb_value mrb_sym_str(mrb_state*,mrb_sym);
|
|
1192
|
+
#define mrb_sym2name(mrb,sym) mrb_sym_name(mrb,sym)
|
|
1193
|
+
#define mrb_sym2name_len(mrb,sym,len) mrb_sym_name_len(mrb,sym,len)
|
|
1194
|
+
#define mrb_sym2str(mrb,sym) mrb_sym_str(mrb,sym)
|
|
1195
|
+
|
|
1196
|
+
MRB_API void *mrb_malloc(mrb_state*, size_t); /* raise RuntimeError if no mem */
|
|
1197
|
+
MRB_API void *mrb_calloc(mrb_state*, size_t, size_t); /* ditto */
|
|
1198
|
+
MRB_API void *mrb_realloc(mrb_state*, void*, size_t); /* ditto */
|
|
1199
|
+
MRB_API void *mrb_realloc_simple(mrb_state*, void*, size_t); /* return NULL if no memory available */
|
|
1200
|
+
MRB_API void *mrb_malloc_simple(mrb_state*, size_t); /* return NULL if no memory available */
|
|
1201
|
+
MRB_API struct RBasic *mrb_obj_alloc(mrb_state*, enum mrb_vtype, struct RClass*);
|
|
1202
|
+
MRB_API void mrb_free(mrb_state*, void*);
|
|
1203
|
+
|
|
1204
|
+
/**
|
|
1205
|
+
* Allocates a Ruby object that matches the constant literal defined in
|
|
1206
|
+
* `enum mrb_vtype` and returns a pointer to the corresponding C type.
|
|
1207
|
+
*
|
|
1208
|
+
* @param mrb The current mruby state
|
|
1209
|
+
* @param tt The constant literal of `enum mrb_vtype`
|
|
1210
|
+
* @param klass A Class object
|
|
1211
|
+
* @return Reference to the newly created object
|
|
1212
|
+
*/
|
|
1213
|
+
#define MRB_OBJ_ALLOC(mrb, tt, klass) ((MRB_VTYPE_TYPEOF(tt)*)mrb_obj_alloc(mrb, tt, klass))
|
|
1214
|
+
|
|
1215
|
+
MRB_API mrb_value mrb_str_new(mrb_state *mrb, const char *p, mrb_int len);
|
|
1216
|
+
|
|
1217
|
+
/**
|
|
1218
|
+
* Turns a C string into a Ruby string value.
|
|
1219
|
+
*/
|
|
1220
|
+
MRB_API mrb_value mrb_str_new_cstr(mrb_state*, const char*);
|
|
1221
|
+
MRB_API mrb_value mrb_str_new_static(mrb_state *mrb, const char *p, mrb_int len);
|
|
1222
|
+
#define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit))
|
|
1223
|
+
|
|
1224
|
+
MRB_API mrb_value mrb_obj_freeze(mrb_state*, mrb_value);
|
|
1225
|
+
#define mrb_str_new_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new(mrb,p,len))
|
|
1226
|
+
#define mrb_str_new_cstr_frozen(mrb,p) mrb_obj_freeze(mrb,mrb_str_new_cstr(mrb,p))
|
|
1227
|
+
#define mrb_str_new_static_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new_static(mrb,p,len))
|
|
1228
|
+
#define mrb_str_new_lit_frozen(mrb,lit) mrb_obj_freeze(mrb,mrb_str_new_lit(mrb,lit))
|
|
1229
|
+
|
|
1230
|
+
#ifdef _WIN32
|
|
1231
|
+
MRB_API char* mrb_utf8_from_locale(const char *p, int len);
|
|
1232
|
+
MRB_API char* mrb_locale_from_utf8(const char *p, int len);
|
|
1233
|
+
#define mrb_locale_free(p) free(p)
|
|
1234
|
+
#define mrb_utf8_free(p) free(p)
|
|
1235
|
+
#else
|
|
1236
|
+
#define mrb_utf8_from_locale(p, l) ((char*)(p))
|
|
1237
|
+
#define mrb_locale_from_utf8(p, l) ((char*)(p))
|
|
1238
|
+
#define mrb_locale_free(p)
|
|
1239
|
+
#define mrb_utf8_free(p)
|
|
1240
|
+
#endif
|
|
1241
|
+
|
|
1242
|
+
/**
|
|
1243
|
+
* Creates new mrb_state.
|
|
1244
|
+
*
|
|
1245
|
+
* @return
|
|
1246
|
+
* Pointer to the newly created mrb_state.
|
|
1247
|
+
*/
|
|
1248
|
+
MRB_API mrb_state* mrb_open(void);
|
|
1249
|
+
|
|
1250
|
+
/**
|
|
1251
|
+
* Create new mrb_state with just the mruby core
|
|
1252
|
+
*
|
|
1253
|
+
* @param f
|
|
1254
|
+
* Reference to the allocation function.
|
|
1255
|
+
* Use mrb_basic_alloc_func for the default
|
|
1256
|
+
* @param ud
|
|
1257
|
+
* User data will be passed to custom allocator f.
|
|
1258
|
+
* If user data isn't required just pass NULL.
|
|
1259
|
+
* @return
|
|
1260
|
+
* Pointer to the newly created mrb_state.
|
|
1261
|
+
*/
|
|
1262
|
+
MRB_API mrb_state* mrb_open_core(void);
|
|
1263
|
+
|
|
1264
|
+
/**
|
|
1265
|
+
* Closes and frees a mrb_state.
|
|
1266
|
+
*
|
|
1267
|
+
* @param mrb
|
|
1268
|
+
* Pointer to the mrb_state to be closed.
|
|
1269
|
+
*/
|
|
1270
|
+
MRB_API void mrb_close(mrb_state *mrb);
|
|
1271
|
+
MRB_API void mrb_method_cache_clear(mrb_state *mrb);
|
|
1272
|
+
|
|
1273
|
+
/**
|
|
1274
|
+
* Check if mrb_open() failed
|
|
1275
|
+
*
|
|
1276
|
+
* @param mrb
|
|
1277
|
+
* Pointer returned from mrb_open() or mrb_open_core().
|
|
1278
|
+
* @return
|
|
1279
|
+
* Non-zero if initialization failed, 0 if succeeded.
|
|
1280
|
+
* @note
|
|
1281
|
+
* mrb_open() may return non-NULL even on failure (with mrb->exc set).
|
|
1282
|
+
* Use this macro to check for failure:
|
|
1283
|
+
* @code
|
|
1284
|
+
* mrb_state *mrb = mrb_open();
|
|
1285
|
+
* if (MRB_OPEN_FAILURE(mrb)) {
|
|
1286
|
+
* if (mrb) {
|
|
1287
|
+
* // Inspect mrb->exc for error details
|
|
1288
|
+
* mrb_close(mrb);
|
|
1289
|
+
* }
|
|
1290
|
+
* return EXIT_FAILURE;
|
|
1291
|
+
* }
|
|
1292
|
+
* @endcode
|
|
1293
|
+
*/
|
|
1294
|
+
#define MRB_OPEN_FAILURE(mrb) (!(mrb) || (mrb)->exc)
|
|
1295
|
+
|
|
1296
|
+
/**
|
|
1297
|
+
* Check if mrb_open() succeeded
|
|
1298
|
+
*
|
|
1299
|
+
* @param mrb
|
|
1300
|
+
* Pointer returned from mrb_open() or mrb_open_core().
|
|
1301
|
+
* @return
|
|
1302
|
+
* Non-zero if initialization succeeded, 0 if failed.
|
|
1303
|
+
*/
|
|
1304
|
+
#define MRB_OPEN_SUCCESS(mrb) (!MRB_OPEN_FAILURE(mrb))
|
|
1305
|
+
|
|
1306
|
+
/**
|
|
1307
|
+
* The memory allocation function. You can redefine this function for your own allocator.
|
|
1308
|
+
*
|
|
1309
|
+
*/
|
|
1310
|
+
MRB_API void* mrb_basic_alloc_func(void*, size_t);
|
|
1311
|
+
|
|
1312
|
+
MRB_API mrb_value mrb_top_self(mrb_state *mrb);
|
|
1313
|
+
|
|
1314
|
+
/**
|
|
1315
|
+
* Enter the mruby VM and execute the proc.
|
|
1316
|
+
*
|
|
1317
|
+
* @param mrb
|
|
1318
|
+
* The current mruby state.
|
|
1319
|
+
* @param proc
|
|
1320
|
+
* An object containing `irep`.
|
|
1321
|
+
* If supplied an object containing anything other than `irep`, it will probably crash.
|
|
1322
|
+
* @param self
|
|
1323
|
+
* `self` on the execution context of `proc`.
|
|
1324
|
+
* @param stack_keep
|
|
1325
|
+
* Specifies the number of values to hold from the stack top.
|
|
1326
|
+
* Values on the stack outside this range will be initialized to `nil`.
|
|
1327
|
+
*
|
|
1328
|
+
* @note
|
|
1329
|
+
* When called from a C function defined as a method, the current stack is destroyed.
|
|
1330
|
+
* If you want to use arguments obtained by `mrb_get_args()` or other methods after `mrb_top_run()`,
|
|
1331
|
+
* you must protect them by `mrb_gc_protect()` or other ways before this function.
|
|
1332
|
+
* Or consider using `mrb_yield()` family functions.
|
|
1333
|
+
*/
|
|
1334
|
+
MRB_API mrb_value mrb_top_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int stack_keep);
|
|
1335
|
+
|
|
1336
|
+
MRB_API mrb_value mrb_vm_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int stack_keep);
|
|
1337
|
+
MRB_API mrb_value mrb_vm_exec(mrb_state *mrb, const struct RProc *proc, const mrb_code *iseq);
|
|
1338
|
+
/* compatibility macros */
|
|
1339
|
+
#define mrb_toplevel_run_keep(m,p,k) mrb_top_run((m),(p),mrb_top_self(m),(k))
|
|
1340
|
+
#define mrb_toplevel_run(m,p) mrb_toplevel_run_keep((m),(p),0)
|
|
1341
|
+
#define mrb_context_run(m,p,s,k) mrb_vm_run((m),(p),(s),(k))
|
|
1342
|
+
|
|
1343
|
+
MRB_API void mrb_p(mrb_state*, mrb_value);
|
|
1344
|
+
MRB_API mrb_int mrb_obj_id(mrb_value obj);
|
|
1345
|
+
MRB_API mrb_sym mrb_obj_to_sym(mrb_state *mrb, mrb_value name);
|
|
1346
|
+
|
|
1347
|
+
MRB_API mrb_bool mrb_obj_eq(mrb_state *mrb, mrb_value a, mrb_value b);
|
|
1348
|
+
MRB_API mrb_bool mrb_obj_equal(mrb_state *mrb, mrb_value a, mrb_value b);
|
|
1349
|
+
MRB_API mrb_bool mrb_equal(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
|
|
1350
|
+
#ifndef MRB_NO_FLOAT
|
|
1351
|
+
MRB_API mrb_value mrb_ensure_float_type(mrb_state *mrb, mrb_value val);
|
|
1352
|
+
#define mrb_as_float(mrb, x) mrb_float(mrb_ensure_float_type(mrb, x))
|
|
1353
|
+
/* obsolete: use mrb_ensure_float_type() instead */
|
|
1354
|
+
#define mrb_to_float(mrb, val) mrb_ensure_float_type(mrb, val)
|
|
1355
|
+
#endif
|
|
1356
|
+
MRB_API mrb_value mrb_inspect(mrb_state *mrb, mrb_value obj);
|
|
1357
|
+
MRB_API mrb_bool mrb_eql(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
|
|
1358
|
+
/* mrb_cmp(mrb, obj1, obj2): 1:0:-1; -2 for error */
|
|
1359
|
+
MRB_API mrb_int mrb_cmp(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
|
|
1360
|
+
|
|
1361
|
+
/* recursion detection */
|
|
1362
|
+
MRB_API mrb_bool mrb_recursive_method_p(mrb_state *mrb, mrb_sym mid, mrb_value obj1, mrb_value obj2);
|
|
1363
|
+
MRB_API mrb_bool mrb_recursive_func_p(mrb_state *mrb, mrb_sym mid, mrb_value obj1, mrb_value obj2);
|
|
1364
|
+
|
|
1365
|
+
#define MRB_RECURSIVE_P(mrb, mid, obj1, obj2) \
|
|
1366
|
+
mrb_recursive_method_p(mrb, mid, obj1, obj2)
|
|
1367
|
+
|
|
1368
|
+
#define MRB_RECURSIVE_UNARY_P(mrb, mid, obj) \
|
|
1369
|
+
mrb_recursive_method_p(mrb, mid, obj, mrb_nil_value())
|
|
1370
|
+
|
|
1371
|
+
#define MRB_RECURSIVE_BINARY_P(mrb, mid, obj1, obj2) \
|
|
1372
|
+
mrb_recursive_method_p(mrb, mid, obj1, obj2)
|
|
1373
|
+
|
|
1374
|
+
#define MRB_RECURSIVE_FUNC_P(mrb, mid, obj) \
|
|
1375
|
+
mrb_recursive_func_p(mrb, mid, obj, mrb_nil_value())
|
|
1376
|
+
|
|
1377
|
+
#define MRB_RECURSIVE_BINARY_FUNC_P(mrb, mid, obj1, obj2) \
|
|
1378
|
+
mrb_recursive_func_p(mrb, mid, obj1, obj2)
|
|
1379
|
+
|
|
1380
|
+
#define mrb_gc_arena_save(mrb) ((mrb)->gc.arena_idx)
|
|
1381
|
+
#define mrb_gc_arena_restore(mrb, idx) ((mrb)->gc.arena_idx = (idx))
|
|
1382
|
+
|
|
1383
|
+
MRB_API void mrb_garbage_collect(mrb_state*);
|
|
1384
|
+
MRB_API void mrb_full_gc(mrb_state*);
|
|
1385
|
+
MRB_API void mrb_incremental_gc(mrb_state*);
|
|
1386
|
+
MRB_API void mrb_gc_mark(mrb_state*,struct RBasic*);
|
|
1387
|
+
#define mrb_gc_mark_value(mrb,val) do {\
|
|
1388
|
+
if (!mrb_immediate_p(val)) mrb_gc_mark((mrb), mrb_basic_ptr(val)); \
|
|
1389
|
+
} while (0)
|
|
1390
|
+
MRB_API void mrb_field_write_barrier(mrb_state*, struct RBasic*, struct RBasic*);
|
|
1391
|
+
#define mrb_field_write_barrier_value(mrb, obj, val) do{\
|
|
1392
|
+
if (!mrb_immediate_p(val)) mrb_field_write_barrier((mrb), (obj), mrb_basic_ptr(val)); \
|
|
1393
|
+
} while (0)
|
|
1394
|
+
MRB_API void mrb_write_barrier(mrb_state *, struct RBasic*);
|
|
1395
|
+
|
|
1396
|
+
MRB_API mrb_value mrb_type_convert(mrb_state *mrb, mrb_value val, enum mrb_vtype type, mrb_sym method);
|
|
1397
|
+
#define mrb_convert_type(mrb, val, type, tname, method) mrb_type_convert(mrb, val, type, mrb_intern_lit(mrb, method))
|
|
1398
|
+
MRB_API mrb_value mrb_type_convert_check(mrb_state *mrb, mrb_value val, enum mrb_vtype type, mrb_sym method);
|
|
1399
|
+
#define mrb_check_convert_type(mrb, val, type, tname, method) mrb_type_convert_check(mrb, val, type, mrb_intern_lit(mrb, method))
|
|
1400
|
+
|
|
1401
|
+
MRB_API mrb_value mrb_any_to_s(mrb_state *mrb, mrb_value obj);
|
|
1402
|
+
MRB_API const char * mrb_obj_classname(mrb_state *mrb, mrb_value obj);
|
|
1403
|
+
MRB_API struct RClass* mrb_obj_class(mrb_state *mrb, mrb_value obj);
|
|
1404
|
+
MRB_API mrb_value mrb_class_path(mrb_state *mrb, struct RClass *c);
|
|
1405
|
+
MRB_API mrb_bool mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c);
|
|
1406
|
+
MRB_API mrb_value mrb_obj_inspect(mrb_state *mrb, mrb_value self);
|
|
1407
|
+
MRB_API mrb_value mrb_obj_clone(mrb_state *mrb, mrb_value self);
|
|
1408
|
+
|
|
1409
|
+
#ifndef ISPRINT
|
|
1410
|
+
#define ISASCII(c) ((unsigned)(c) <= 0x7f)
|
|
1411
|
+
#define ISPRINT(c) (((unsigned)(c) - 0x20) < 0x5f)
|
|
1412
|
+
#define ISSPACE(c) ((c) == ' ' || (unsigned)(c) - '\t' < 5)
|
|
1413
|
+
#define ISUPPER(c) (((unsigned)(c) - 'A') < 26)
|
|
1414
|
+
#define ISLOWER(c) (((unsigned)(c) - 'a') < 26)
|
|
1415
|
+
#define ISALPHA(c) ((((unsigned)(c) | 0x20) - 'a') < 26)
|
|
1416
|
+
#define ISDIGIT(c) (((unsigned)(c) - '0') < 10)
|
|
1417
|
+
#define ISXDIGIT(c) (ISDIGIT(c) || ((unsigned)(c) | 0x20) - 'a' < 6)
|
|
1418
|
+
#define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c))
|
|
1419
|
+
#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
|
|
1420
|
+
#define ISCNTRL(c) ((unsigned)(c) < 0x20 || (c) == 0x7f)
|
|
1421
|
+
#define TOUPPER(c) (ISLOWER(c) ? ((c) & 0x5f) : (c))
|
|
1422
|
+
#define TOLOWER(c) (ISUPPER(c) ? ((c) | 0x20) : (c))
|
|
1423
|
+
#endif
|
|
1424
|
+
|
|
1425
|
+
MRB_API mrb_value mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, mrb_int len);
|
|
1426
|
+
MRB_API mrb_noreturn void mrb_exc_raise(mrb_state *mrb, mrb_value exc);
|
|
1427
|
+
|
|
1428
|
+
MRB_API mrb_noreturn void mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg);
|
|
1429
|
+
MRB_API mrb_noreturn void mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...);
|
|
1430
|
+
MRB_API mrb_noreturn void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...);
|
|
1431
|
+
MRB_API mrb_noreturn void mrb_frozen_error(mrb_state *mrb, void *frozen_obj);
|
|
1432
|
+
MRB_API mrb_noreturn void mrb_argnum_error(mrb_state *mrb, mrb_int argc, int min, int max);
|
|
1433
|
+
MRB_API void mrb_warn(mrb_state *mrb, const char *fmt, ...);
|
|
1434
|
+
MRB_API mrb_noreturn void mrb_bug(mrb_state *mrb, const char *mesg);
|
|
1435
|
+
MRB_API void mrb_print_backtrace(mrb_state *mrb);
|
|
1436
|
+
MRB_API void mrb_print_error(mrb_state *mrb);
|
|
1437
|
+
/* function for `raisef` formatting */
|
|
1438
|
+
MRB_API mrb_value mrb_vformat(mrb_state *mrb, const char *format, va_list ap);
|
|
1439
|
+
|
|
1440
|
+
/* macros to get typical exception objects
|
|
1441
|
+
note:
|
|
1442
|
+
+ those E_* macros requires mrb_state* variable named mrb.
|
|
1443
|
+
+ exception objects obtained from those macros are local to mrb
|
|
1444
|
+
*/
|
|
1445
|
+
#define MRB_ERROR_SYM(sym) mrb_intern_lit(mrb, #sym)
|
|
1446
|
+
#define E_EXCEPTION mrb->eException_class
|
|
1447
|
+
#define E_STANDARD_ERROR mrb->eStandardError_class
|
|
1448
|
+
#define E_RUNTIME_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(RuntimeError))
|
|
1449
|
+
#define E_TYPE_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(TypeError))
|
|
1450
|
+
#define E_ZERODIV_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(ZeroDivisionError))
|
|
1451
|
+
#define E_ARGUMENT_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(ArgumentError))
|
|
1452
|
+
#define E_INDEX_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(IndexError))
|
|
1453
|
+
#define E_RANGE_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(RangeError))
|
|
1454
|
+
#define E_NAME_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(NameError))
|
|
1455
|
+
#define E_NOMETHOD_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(NoMethodError))
|
|
1456
|
+
#define E_SCRIPT_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(ScriptError))
|
|
1457
|
+
#define E_SYNTAX_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(SyntaxError))
|
|
1458
|
+
#define E_LOCALJUMP_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(LocalJumpError))
|
|
1459
|
+
#define E_REGEXP_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(RegexpError))
|
|
1460
|
+
#define E_FROZEN_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(FrozenError))
|
|
1461
|
+
#define E_NOTIMP_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(NotImplementedError))
|
|
1462
|
+
#define E_KEY_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(KeyError))
|
|
1463
|
+
#ifndef MRB_NO_FLOAT
|
|
1464
|
+
# define E_FLOATDOMAIN_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(FloatDomainError))
|
|
1465
|
+
#endif
|
|
1466
|
+
|
|
1467
|
+
MRB_API mrb_value mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg);
|
|
1468
|
+
MRB_API mrb_value mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv);
|
|
1469
|
+
MRB_API mrb_value mrb_yield_with_class(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv, mrb_value self, struct RClass *c);
|
|
1470
|
+
|
|
1471
|
+
/* continue execution to the proc */
|
|
1472
|
+
/* this function should always be called as the last function of a method */
|
|
1473
|
+
/* e.g. return mrb_yield_cont(mrb, proc, self, argc, argv); */
|
|
1474
|
+
mrb_value mrb_yield_cont(mrb_state *mrb, mrb_value b, mrb_value self, mrb_int argc, const mrb_value *argv);
|
|
1475
|
+
|
|
1476
|
+
/* mrb_gc_protect() leaves the object in the arena */
|
|
1477
|
+
MRB_API void mrb_gc_protect(mrb_state *mrb, mrb_value obj);
|
|
1478
|
+
/* mrb_gc_register() keeps the object from GC. */
|
|
1479
|
+
MRB_API void mrb_gc_register(mrb_state *mrb, mrb_value obj);
|
|
1480
|
+
/* mrb_gc_unregister() removes the object from GC root. */
|
|
1481
|
+
MRB_API void mrb_gc_unregister(mrb_state *mrb, mrb_value obj);
|
|
1482
|
+
|
|
1483
|
+
/* type conversion/check functions */
|
|
1484
|
+
MRB_API mrb_value mrb_ensure_array_type(mrb_state *mrb, mrb_value self);
|
|
1485
|
+
MRB_API mrb_value mrb_check_array_type(mrb_state *mrb, mrb_value self);
|
|
1486
|
+
MRB_API mrb_value mrb_ensure_hash_type(mrb_state *mrb, mrb_value hash);
|
|
1487
|
+
MRB_API mrb_value mrb_check_hash_type(mrb_state *mrb, mrb_value hash);
|
|
1488
|
+
MRB_API mrb_value mrb_ensure_string_type(mrb_state *mrb, mrb_value str);
|
|
1489
|
+
MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
|
|
1490
|
+
/* obsolete: use mrb_ensure_string_type() instead */
|
|
1491
|
+
#define mrb_string_type(mrb, str) mrb_ensure_string_type(mrb,str)
|
|
1492
|
+
#define mrb_to_str(mrb, str) mrb_ensure_string_type(mrb,str)
|
|
1493
|
+
/* obsolete: use mrb_obj_as_string() instead */
|
|
1494
|
+
#define mrb_str_to_str(mrb, str) mrb_obj_as_string(mrb, str)
|
|
1495
|
+
/* check if val is an integer (including Bigint) */
|
|
1496
|
+
MRB_API mrb_value mrb_ensure_integer_type(mrb_state *mrb, mrb_value val);
|
|
1497
|
+
/* check if val fit in mrb_int */
|
|
1498
|
+
MRB_API mrb_value mrb_ensure_int_type(mrb_state *mrb, mrb_value val);
|
|
1499
|
+
#define mrb_as_int(mrb, val) mrb_integer(mrb_ensure_int_type(mrb, val))
|
|
1500
|
+
/* obsolete: use mrb_ensure_int_type() instead */
|
|
1501
|
+
#define mrb_to_integer(mrb, val) mrb_ensure_int_type(mrb, val)
|
|
1502
|
+
#define mrb_to_int(mrb, val) mrb_ensure_int_type(mrb, val)
|
|
1503
|
+
|
|
1504
|
+
/* string type checking (contrary to the name, it doesn't convert) */
|
|
1505
|
+
MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t);
|
|
1506
|
+
MRB_API void mrb_check_frozen(mrb_state *mrb, void *);
|
|
1507
|
+
MRB_API void mrb_check_frozen_value(mrb_state *mrb, mrb_value v);
|
|
1508
|
+
MRB_API void mrb_define_alias(mrb_state *mrb, struct RClass *c, const char *a, const char *b);
|
|
1509
|
+
MRB_API void mrb_define_alias_id(mrb_state *mrb, struct RClass *c, mrb_sym a, mrb_sym b);
|
|
1510
|
+
MRB_API const char *mrb_class_name(mrb_state *mrb, struct RClass* klass);
|
|
1511
|
+
MRB_API void mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val);
|
|
1512
|
+
|
|
1513
|
+
MRB_API mrb_value mrb_attr_get(mrb_state *mrb, mrb_value obj, mrb_sym id);
|
|
1514
|
+
|
|
1515
|
+
MRB_API mrb_bool mrb_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym mid);
|
|
1516
|
+
MRB_API mrb_bool mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, const struct RClass* c);
|
|
1517
|
+
MRB_API mrb_bool mrb_func_basic_p(mrb_state *mrb, mrb_value obj, mrb_sym mid, mrb_func_t func);
|
|
1518
|
+
|
|
1519
|
+
/* obsolete function(s); will be removed */
|
|
1520
|
+
#define mrb_int(mrb, val) mrb_as_int(mrb, val)
|
|
1521
|
+
|
|
1522
|
+
/**
|
|
1523
|
+
* Create a new Fiber from proc object
|
|
1524
|
+
*
|
|
1525
|
+
* Implemented in mruby-fiber
|
|
1526
|
+
*/
|
|
1527
|
+
MRB_API mrb_value mrb_fiber_new(mrb_state *mrb, const struct RProc *proc);
|
|
1528
|
+
|
|
1529
|
+
/**
|
|
1530
|
+
* Resume a Fiber
|
|
1531
|
+
*
|
|
1532
|
+
* Implemented in mruby-fiber
|
|
1533
|
+
*
|
|
1534
|
+
* Switches to the specified fiber and executes. Like the `Fiber#resume` method.
|
|
1535
|
+
*/
|
|
1536
|
+
MRB_API mrb_value mrb_fiber_resume(mrb_state *mrb, mrb_value fib, mrb_int argc, const mrb_value *argv);
|
|
1537
|
+
|
|
1538
|
+
/**
|
|
1539
|
+
* Yield a Fiber
|
|
1540
|
+
*
|
|
1541
|
+
* Implemented in mruby-fiber
|
|
1542
|
+
*
|
|
1543
|
+
* Passes control to the caller fiber of the running fiber. Like the `Fiber.yield` method.
|
|
1544
|
+
*
|
|
1545
|
+
* @note This function is only available from inside a function defined as a method by,
|
|
1546
|
+
* for example, `mrb_define_method()`.
|
|
1547
|
+
* Also, the work following `mrb_fiber_yield()` cannot be performed,
|
|
1548
|
+
* and the return value of `mrb_fiber_yield()` must be returned as is.
|
|
1549
|
+
*
|
|
1550
|
+
* return mrb_fiber_yield(mrb, argc, argv);
|
|
1551
|
+
*/
|
|
1552
|
+
MRB_API mrb_value mrb_fiber_yield(mrb_state *mrb, mrb_int argc, const mrb_value *argv);
|
|
1553
|
+
|
|
1554
|
+
/**
|
|
1555
|
+
* Check if a Fiber is alive
|
|
1556
|
+
*
|
|
1557
|
+
* Implemented in mruby-fiber
|
|
1558
|
+
*/
|
|
1559
|
+
MRB_API mrb_value mrb_fiber_alive_p(mrb_state *mrb, mrb_value fib);
|
|
1560
|
+
|
|
1561
|
+
/**
|
|
1562
|
+
* FiberError reference
|
|
1563
|
+
*
|
|
1564
|
+
* Implemented in mruby-fiber
|
|
1565
|
+
*/
|
|
1566
|
+
#define E_FIBER_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(FiberError))
|
|
1567
|
+
MRB_API void mrb_stack_extend(mrb_state*, mrb_int);
|
|
1568
|
+
|
|
1569
|
+
/* temporary memory allocation, only effective while GC arena is kept */
|
|
1570
|
+
MRB_API void* mrb_alloca(mrb_state *mrb, size_t);
|
|
1571
|
+
|
|
1572
|
+
MRB_API void mrb_state_atexit(mrb_state *mrb, mrb_atexit_func func);
|
|
1573
|
+
|
|
1574
|
+
MRB_API void mrb_show_version(mrb_state *mrb);
|
|
1575
|
+
MRB_API void mrb_show_copyright(mrb_state *mrb);
|
|
1576
|
+
|
|
1577
|
+
MRB_API mrb_value mrb_format(mrb_state *mrb, const char *format, ...);
|
|
1578
|
+
|
|
1579
|
+
#ifdef MRB_PRESYM_SCANNING
|
|
1580
|
+
# include <mruby/presym/scanning.h>
|
|
1581
|
+
#endif
|
|
1582
|
+
|
|
1583
|
+
#if 0
|
|
1584
|
+
/* memcpy and memset does not work with gdb reverse-next on my box */
|
|
1585
|
+
/* use naive memcpy and memset instead */
|
|
1586
|
+
#undef memcpy
|
|
1587
|
+
#undef memset
|
|
1588
|
+
static void*
|
|
1589
|
+
mrbmemcpy(void *dst, const void *src, size_t n)
|
|
1590
|
+
{
|
|
1591
|
+
char *d = (char*)dst;
|
|
1592
|
+
const char *s = (const char*)src;
|
|
1593
|
+
while (n--)
|
|
1594
|
+
*d++ = *s++;
|
|
1595
|
+
return d;
|
|
1596
|
+
}
|
|
1597
|
+
#define memcpy(a,b,c) mrbmemcpy(a,b,c)
|
|
1598
|
+
|
|
1599
|
+
static void*
|
|
1600
|
+
mrbmemset(void *s, int c, size_t n)
|
|
1601
|
+
{
|
|
1602
|
+
char *t = (char*)s;
|
|
1603
|
+
while (n--)
|
|
1604
|
+
*t++ = c;
|
|
1605
|
+
return s;
|
|
1606
|
+
}
|
|
1607
|
+
#define memset(a,b,c) mrbmemset(a,b,c)
|
|
1608
|
+
#endif
|
|
1609
|
+
|
|
1610
|
+
#define mrb_int_hash_func(mrb,key) (uint32_t)((key)^((key)<<2)^((key)>>2))
|
|
1611
|
+
|
|
1612
|
+
MRB_END_DECL
|
|
1613
|
+
|
|
1614
|
+
#endif /* MRUBY_H */
|