1 | /*
|
---|
2 | ** mruby - An embeddable Ruby implementation
|
---|
3 | **
|
---|
4 | ** Copyright (c) mruby developers 2010-2020
|
---|
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: http://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 | #include <float.h>
|
---|
46 |
|
---|
47 | #ifdef __cplusplus
|
---|
48 | #ifndef SIZE_MAX
|
---|
49 | #ifdef __SIZE_MAX__
|
---|
50 | #define SIZE_MAX __SIZE_MAX__
|
---|
51 | #else
|
---|
52 | #define SIZE_MAX std::numeric_limits<size_t>::max()
|
---|
53 | #endif
|
---|
54 | #endif
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #ifdef MRB_DEBUG
|
---|
58 | #include <assert.h>
|
---|
59 | #define mrb_assert(p) assert(p)
|
---|
60 | #define mrb_assert_int_fit(t1,n,t2,max) assert((n)>=0 && ((sizeof(n)<=sizeof(t2))||(n<=(t1)(max))))
|
---|
61 | #else
|
---|
62 | #define mrb_assert(p) ((void)0)
|
---|
63 | #define mrb_assert_int_fit(t1,n,t2,max) ((void)0)
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L
|
---|
67 | #define mrb_static_assert(exp, str) _Static_assert(exp, str)
|
---|
68 | #else
|
---|
69 | #define mrb_static_assert(exp, str) mrb_assert(exp)
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #include "mrbconf.h"
|
---|
73 |
|
---|
74 | #include <mruby/common.h>
|
---|
75 | #include <mruby/value.h>
|
---|
76 | #include <mruby/gc.h>
|
---|
77 | #include <mruby/version.h>
|
---|
78 |
|
---|
79 | #ifndef MRB_WITHOUT_FLOAT
|
---|
80 | #ifndef FLT_EPSILON
|
---|
81 | #define FLT_EPSILON (1.19209290e-07f)
|
---|
82 | #endif
|
---|
83 | #ifndef DBL_EPSILON
|
---|
84 | #define DBL_EPSILON ((double)2.22044604925031308085e-16L)
|
---|
85 | #endif
|
---|
86 | #ifndef LDBL_EPSILON
|
---|
87 | #define LDBL_EPSILON (1.08420217248550443401e-19L)
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | #ifdef MRB_USE_FLOAT
|
---|
91 | #define MRB_FLOAT_EPSILON FLT_EPSILON
|
---|
92 | #else
|
---|
93 | #define MRB_FLOAT_EPSILON DBL_EPSILON
|
---|
94 | #endif
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * MRuby C API entry point
|
---|
99 | */
|
---|
100 | MRB_BEGIN_DECL
|
---|
101 |
|
---|
102 | typedef uint8_t mrb_code;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * \class mrb_aspec
|
---|
106 | *
|
---|
107 | * Specifies the number of arguments a function takes
|
---|
108 | *
|
---|
109 | * Example: `MRB_ARGS_REQ(2) | MRB_ARGS_OPT(1)` for a method that expects 2..3 arguments
|
---|
110 | */
|
---|
111 | typedef uint32_t mrb_aspec;
|
---|
112 |
|
---|
113 | struct mrb_irep;
|
---|
114 | struct mrb_state;
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Function pointer type of custom allocator used in @see mrb_open_allocf.
|
---|
118 | *
|
---|
119 | * The function pointing it must behave similarly as realloc except:
|
---|
120 | * - If ptr is NULL it must allocate new space.
|
---|
121 | * - If s is NULL, ptr must be freed.
|
---|
122 | *
|
---|
123 | * See @see mrb_default_allocf for the default implementation.
|
---|
124 | */
|
---|
125 | typedef void* (*mrb_allocf) (struct mrb_state *mrb, void*, size_t, void *ud);
|
---|
126 |
|
---|
127 | #ifndef MRB_FIXED_STATE_ATEXIT_STACK_SIZE
|
---|
128 | #define MRB_FIXED_STATE_ATEXIT_STACK_SIZE 5
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | typedef struct {
|
---|
132 | mrb_sym mid;
|
---|
133 | struct RProc *proc;
|
---|
134 | mrb_value *stackent;
|
---|
135 | uint16_t ridx;
|
---|
136 | uint16_t epos;
|
---|
137 | struct REnv *env;
|
---|
138 | const mrb_code *pc; /* return address */
|
---|
139 | const mrb_code *err; /* error position */
|
---|
140 | int argc;
|
---|
141 | int acc;
|
---|
142 | struct RClass *target_class;
|
---|
143 | } mrb_callinfo;
|
---|
144 |
|
---|
145 | enum mrb_fiber_state {
|
---|
146 | MRB_FIBER_CREATED = 0,
|
---|
147 | MRB_FIBER_RUNNING,
|
---|
148 | MRB_FIBER_RESUMED,
|
---|
149 | MRB_FIBER_SUSPENDED,
|
---|
150 | MRB_FIBER_TRANSFERRED,
|
---|
151 | MRB_FIBER_TERMINATED,
|
---|
152 | };
|
---|
153 |
|
---|
154 | struct mrb_context {
|
---|
155 | struct mrb_context *prev;
|
---|
156 |
|
---|
157 | mrb_value *stack; /* stack of virtual machine */
|
---|
158 | mrb_value *stbase, *stend;
|
---|
159 |
|
---|
160 | mrb_callinfo *ci;
|
---|
161 | mrb_callinfo *cibase, *ciend;
|
---|
162 |
|
---|
163 | uint16_t *rescue; /* exception handler stack */
|
---|
164 | uint16_t rsize;
|
---|
165 | struct RProc **ensure; /* ensure handler stack */
|
---|
166 | uint16_t esize, eidx;
|
---|
167 |
|
---|
168 | enum mrb_fiber_state status : 4;
|
---|
169 | mrb_bool vmexec : 1;
|
---|
170 | struct RFiber *fib;
|
---|
171 | };
|
---|
172 |
|
---|
173 | #ifdef MRB_METHOD_CACHE_SIZE
|
---|
174 | # define MRB_METHOD_CACHE
|
---|
175 | #else
|
---|
176 | /* default method cache size: 128 */
|
---|
177 | /* cache size needs to be power of 2 */
|
---|
178 | # define MRB_METHOD_CACHE_SIZE (1<<7)
|
---|
179 | #endif
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Function pointer type for a function callable by mruby.
|
---|
183 | *
|
---|
184 | * The arguments to the function are stored on the mrb_state. To get them see mrb_get_args
|
---|
185 | *
|
---|
186 | * @param mrb The mruby state
|
---|
187 | * @param self The self object
|
---|
188 | * @return [mrb_value] The function's return value
|
---|
189 | */
|
---|
190 | typedef mrb_value (*mrb_func_t)(struct mrb_state *mrb, mrb_value self);
|
---|
191 |
|
---|
192 | #ifndef MRB_METHOD_T_STRUCT
|
---|
193 | typedef uintptr_t mrb_method_t;
|
---|
194 | #else
|
---|
195 | typedef struct {
|
---|
196 | uint8_t flags;
|
---|
197 | union {
|
---|
198 | struct RProc *proc;
|
---|
199 | mrb_func_t func;
|
---|
200 | };
|
---|
201 | } mrb_method_t;
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | #ifdef MRB_METHOD_CACHE
|
---|
205 | struct mrb_cache_entry {
|
---|
206 | struct RClass *c, *c0;
|
---|
207 | mrb_sym mid;
|
---|
208 | mrb_method_t m;
|
---|
209 | };
|
---|
210 | #endif
|
---|
211 |
|
---|
212 | struct mrb_jmpbuf;
|
---|
213 |
|
---|
214 | typedef void (*mrb_atexit_func)(struct mrb_state*);
|
---|
215 |
|
---|
216 | typedef struct mrb_state {
|
---|
217 | struct mrb_jmpbuf *jmp;
|
---|
218 |
|
---|
219 | mrb_allocf allocf; /* memory allocation function */
|
---|
220 | void *allocf_ud; /* auxiliary data of allocf */
|
---|
221 |
|
---|
222 | struct mrb_context *c;
|
---|
223 | struct mrb_context *root_c;
|
---|
224 | struct iv_tbl *globals; /* global variable table */
|
---|
225 |
|
---|
226 | struct RObject *exc; /* exception */
|
---|
227 |
|
---|
228 | struct RObject *top_self;
|
---|
229 | struct RClass *object_class; /* Object class */
|
---|
230 | struct RClass *class_class;
|
---|
231 | struct RClass *module_class;
|
---|
232 | struct RClass *proc_class;
|
---|
233 | struct RClass *string_class;
|
---|
234 | struct RClass *array_class;
|
---|
235 | struct RClass *hash_class;
|
---|
236 | struct RClass *range_class;
|
---|
237 |
|
---|
238 | #ifndef MRB_WITHOUT_FLOAT
|
---|
239 | struct RClass *float_class;
|
---|
240 | #endif
|
---|
241 | struct RClass *fixnum_class;
|
---|
242 | struct RClass *true_class;
|
---|
243 | struct RClass *false_class;
|
---|
244 | struct RClass *nil_class;
|
---|
245 | struct RClass *symbol_class;
|
---|
246 | struct RClass *kernel_module;
|
---|
247 |
|
---|
248 | mrb_gc gc;
|
---|
249 |
|
---|
250 | #ifdef MRB_METHOD_CACHE
|
---|
251 | struct mrb_cache_entry cache[MRB_METHOD_CACHE_SIZE];
|
---|
252 | #endif
|
---|
253 |
|
---|
254 | mrb_sym symidx;
|
---|
255 | struct symbol_name *symtbl; /* symbol table */
|
---|
256 | mrb_sym symhash[256];
|
---|
257 | size_t symcapa;
|
---|
258 | #ifndef MRB_ENABLE_SYMBOLL_ALL
|
---|
259 | char symbuf[8]; /* buffer for small symbol names */
|
---|
260 | #endif
|
---|
261 |
|
---|
262 | #ifdef MRB_ENABLE_DEBUG_HOOK
|
---|
263 | void (*code_fetch_hook)(struct mrb_state* mrb, struct mrb_irep *irep, const mrb_code *pc, mrb_value *regs);
|
---|
264 | void (*debug_op_hook)(struct mrb_state* mrb, struct mrb_irep *irep, const mrb_code *pc, mrb_value *regs);
|
---|
265 | #endif
|
---|
266 |
|
---|
267 | #ifdef MRB_BYTECODE_DECODE_OPTION
|
---|
268 | mrb_code (*bytecode_decoder)(struct mrb_state* mrb, mrb_code code);
|
---|
269 | #endif
|
---|
270 |
|
---|
271 | struct RClass *eException_class;
|
---|
272 | struct RClass *eStandardError_class;
|
---|
273 | struct RObject *nomem_err; /* pre-allocated NoMemoryError */
|
---|
274 | struct RObject *stack_err; /* pre-allocated SysStackError */
|
---|
275 | #ifdef MRB_GC_FIXED_ARENA
|
---|
276 | struct RObject *arena_err; /* pre-allocated arena overfow error */
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | void *ud; /* auxiliary data */
|
---|
280 |
|
---|
281 | #ifdef MRB_FIXED_STATE_ATEXIT_STACK
|
---|
282 | mrb_atexit_func atexit_stack[MRB_FIXED_STATE_ATEXIT_STACK_SIZE];
|
---|
283 | #else
|
---|
284 | mrb_atexit_func *atexit_stack;
|
---|
285 | #endif
|
---|
286 | uint16_t atexit_stack_len;
|
---|
287 | uint16_t ecall_nest; /* prevent infinite recursive ecall() */
|
---|
288 | } mrb_state;
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Defines a new class.
|
---|
292 | *
|
---|
293 | * If you're creating a gem it may look something like this:
|
---|
294 | *
|
---|
295 | * !!!c
|
---|
296 | * void mrb_example_gem_init(mrb_state* mrb) {
|
---|
297 | * struct RClass *example_class;
|
---|
298 | * example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class);
|
---|
299 | * }
|
---|
300 | *
|
---|
301 | * void mrb_example_gem_final(mrb_state* mrb) {
|
---|
302 | * //free(TheAnimals);
|
---|
303 | * }
|
---|
304 | *
|
---|
305 | * @param mrb The current mruby state.
|
---|
306 | * @param name The name of the defined class.
|
---|
307 | * @param super The new class parent.
|
---|
308 | * @return [struct RClass *] Reference to the newly defined class.
|
---|
309 | * @see mrb_define_class_under
|
---|
310 | */
|
---|
311 | MRB_API struct RClass *mrb_define_class(mrb_state *mrb, const char *name, struct RClass *super);
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Defines a new module.
|
---|
315 | *
|
---|
316 | * @param mrb The current mruby state.
|
---|
317 | * @param name The name of the module.
|
---|
318 | * @return [struct RClass *] Reference to the newly defined module.
|
---|
319 | */
|
---|
320 | MRB_API struct RClass *mrb_define_module(mrb_state *mrb, const char *name);
|
---|
321 | MRB_API mrb_value mrb_singleton_class(mrb_state *mrb, mrb_value val);
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Include a module in another class or module.
|
---|
325 | * Equivalent to:
|
---|
326 | *
|
---|
327 | * module B
|
---|
328 | * include A
|
---|
329 | * end
|
---|
330 | * @param mrb The current mruby state.
|
---|
331 | * @param cla A reference to module or a class.
|
---|
332 | * @param included A reference to the module to be included.
|
---|
333 | */
|
---|
334 | MRB_API void mrb_include_module(mrb_state *mrb, struct RClass *cla, struct RClass *included);
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Prepends a module in another class or module.
|
---|
338 | *
|
---|
339 | * Equivalent to:
|
---|
340 | * module B
|
---|
341 | * prepend A
|
---|
342 | * end
|
---|
343 | * @param mrb The current mruby state.
|
---|
344 | * @param cla A reference to module or a class.
|
---|
345 | * @param prepended A reference to the module to be prepended.
|
---|
346 | */
|
---|
347 | MRB_API void mrb_prepend_module(mrb_state *mrb, struct RClass *cla, struct RClass *prepended);
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Defines a global function in ruby.
|
---|
351 | *
|
---|
352 | * If you're creating a gem it may look something like this
|
---|
353 | *
|
---|
354 | * Example:
|
---|
355 | *
|
---|
356 | * mrb_value example_method(mrb_state* mrb, mrb_value self)
|
---|
357 | * {
|
---|
358 | * puts("Executing example command!");
|
---|
359 | * return self;
|
---|
360 | * }
|
---|
361 | *
|
---|
362 | * void mrb_example_gem_init(mrb_state* mrb)
|
---|
363 | * {
|
---|
364 | * mrb_define_method(mrb, mrb->kernel_module, "example_method", example_method, MRB_ARGS_NONE());
|
---|
365 | * }
|
---|
366 | *
|
---|
367 | * @param mrb The MRuby state reference.
|
---|
368 | * @param cla The class pointer where the method will be defined.
|
---|
369 | * @param name The name of the method being defined.
|
---|
370 | * @param func The function pointer to the method definition.
|
---|
371 | * @param aspec The method parameters declaration.
|
---|
372 | */
|
---|
373 | MRB_API void mrb_define_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec);
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * Defines a class method.
|
---|
377 | *
|
---|
378 | * Example:
|
---|
379 | *
|
---|
380 | * # Ruby style
|
---|
381 | * class Foo
|
---|
382 | * def Foo.bar
|
---|
383 | * end
|
---|
384 | * end
|
---|
385 | * // C style
|
---|
386 | * mrb_value bar_method(mrb_state* mrb, mrb_value self){
|
---|
387 | * return mrb_nil_value();
|
---|
388 | * }
|
---|
389 | * void mrb_example_gem_init(mrb_state* mrb){
|
---|
390 | * struct RClass *foo;
|
---|
391 | * foo = mrb_define_class(mrb, "Foo", mrb->object_class);
|
---|
392 | * mrb_define_class_method(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
|
---|
393 | * }
|
---|
394 | * @param mrb The MRuby state reference.
|
---|
395 | * @param cla The class where the class method will be defined.
|
---|
396 | * @param name The name of the class method being defined.
|
---|
397 | * @param fun The function pointer to the class method definition.
|
---|
398 | * @param aspec The method parameters declaration.
|
---|
399 | */
|
---|
400 | MRB_API void mrb_define_class_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Defines a singleton method
|
---|
404 | *
|
---|
405 | * @see mrb_define_class_method
|
---|
406 | */
|
---|
407 | MRB_API void mrb_define_singleton_method(mrb_state *mrb, struct RObject *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * Defines a module function.
|
---|
411 | *
|
---|
412 | * Example:
|
---|
413 | *
|
---|
414 | * # Ruby style
|
---|
415 | * module Foo
|
---|
416 | * def Foo.bar
|
---|
417 | * end
|
---|
418 | * end
|
---|
419 | * // C style
|
---|
420 | * mrb_value bar_method(mrb_state* mrb, mrb_value self){
|
---|
421 | * return mrb_nil_value();
|
---|
422 | * }
|
---|
423 | * void mrb_example_gem_init(mrb_state* mrb){
|
---|
424 | * struct RClass *foo;
|
---|
425 | * foo = mrb_define_module(mrb, "Foo");
|
---|
426 | * mrb_define_module_function(mrb, foo, "bar", bar_method, MRB_ARGS_NONE());
|
---|
427 | * }
|
---|
428 | * @param mrb The MRuby state reference.
|
---|
429 | * @param cla The module where the module function will be defined.
|
---|
430 | * @param name The name of the module function being defined.
|
---|
431 | * @param fun The function pointer to the module function definition.
|
---|
432 | * @param aspec The method parameters declaration.
|
---|
433 | */
|
---|
434 | MRB_API void mrb_define_module_function(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec);
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Defines a constant.
|
---|
438 | *
|
---|
439 | * Example:
|
---|
440 | *
|
---|
441 | * # Ruby style
|
---|
442 | * class ExampleClass
|
---|
443 | * AGE = 22
|
---|
444 | * end
|
---|
445 | * // C style
|
---|
446 | * #include <stdio.h>
|
---|
447 | * #include <mruby.h>
|
---|
448 | *
|
---|
449 | * void
|
---|
450 | * mrb_example_gem_init(mrb_state* mrb){
|
---|
451 | * mrb_define_const(mrb, mrb->kernel_module, "AGE", mrb_fixnum_value(22));
|
---|
452 | * }
|
---|
453 | *
|
---|
454 | * mrb_value
|
---|
455 | * mrb_example_gem_final(mrb_state* mrb){
|
---|
456 | * }
|
---|
457 | * @param mrb The MRuby state reference.
|
---|
458 | * @param cla A class or module the constant is defined in.
|
---|
459 | * @param name The name of the constant being defined.
|
---|
460 | * @param val The value for the constant.
|
---|
461 | */
|
---|
462 | MRB_API void mrb_define_const(mrb_state* mrb, struct RClass* cla, const char *name, mrb_value val);
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * Undefines a method.
|
---|
466 | *
|
---|
467 | * Example:
|
---|
468 | *
|
---|
469 | * # Ruby style
|
---|
470 | *
|
---|
471 | * class ExampleClassA
|
---|
472 | * def example_method
|
---|
473 | * "example"
|
---|
474 | * end
|
---|
475 | * end
|
---|
476 | * ExampleClassA.new.example_method # => example
|
---|
477 | *
|
---|
478 | * class ExampleClassB < ExampleClassA
|
---|
479 | * undef_method :example_method
|
---|
480 | * end
|
---|
481 | *
|
---|
482 | * ExampleClassB.new.example_method # => undefined method 'example_method' for ExampleClassB (NoMethodError)
|
---|
483 | *
|
---|
484 | * // C style
|
---|
485 | * #include <stdio.h>
|
---|
486 | * #include <mruby.h>
|
---|
487 | *
|
---|
488 | * mrb_value
|
---|
489 | * mrb_example_method(mrb_state *mrb){
|
---|
490 | * return mrb_str_new_lit(mrb, "example");
|
---|
491 | * }
|
---|
492 | *
|
---|
493 | * void
|
---|
494 | * mrb_example_gem_init(mrb_state* mrb){
|
---|
495 | * struct RClass *example_class_a;
|
---|
496 | * struct RClass *example_class_b;
|
---|
497 | * struct RClass *example_class_c;
|
---|
498 | *
|
---|
499 | * example_class_a = mrb_define_class(mrb, "ExampleClassA", mrb->object_class);
|
---|
500 | * mrb_define_method(mrb, example_class_a, "example_method", mrb_example_method, MRB_ARGS_NONE());
|
---|
501 | * example_class_b = mrb_define_class(mrb, "ExampleClassB", example_class_a);
|
---|
502 | * example_class_c = mrb_define_class(mrb, "ExampleClassC", example_class_b);
|
---|
503 | * mrb_undef_method(mrb, example_class_c, "example_method");
|
---|
504 | * }
|
---|
505 | *
|
---|
506 | * mrb_example_gem_final(mrb_state* mrb){
|
---|
507 | * }
|
---|
508 | * @param mrb The mruby state reference.
|
---|
509 | * @param cla The class the method will be undefined from.
|
---|
510 | * @param name The name of the method to be undefined.
|
---|
511 | */
|
---|
512 | MRB_API void mrb_undef_method(mrb_state *mrb, struct RClass *cla, const char *name);
|
---|
513 | MRB_API void mrb_undef_method_id(mrb_state*, struct RClass*, mrb_sym);
|
---|
514 |
|
---|
515 | /**
|
---|
516 | * Undefine a class method.
|
---|
517 | * Example:
|
---|
518 | *
|
---|
519 | * # Ruby style
|
---|
520 | * class ExampleClass
|
---|
521 | * def self.example_method
|
---|
522 | * "example"
|
---|
523 | * end
|
---|
524 | * end
|
---|
525 | *
|
---|
526 | * ExampleClass.example_method
|
---|
527 | *
|
---|
528 | * // C style
|
---|
529 | * #include <stdio.h>
|
---|
530 | * #include <mruby.h>
|
---|
531 | *
|
---|
532 | * mrb_value
|
---|
533 | * mrb_example_method(mrb_state *mrb){
|
---|
534 | * return mrb_str_new_lit(mrb, "example");
|
---|
535 | * }
|
---|
536 | *
|
---|
537 | * void
|
---|
538 | * mrb_example_gem_init(mrb_state* mrb){
|
---|
539 | * struct RClass *example_class;
|
---|
540 | * example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
|
---|
541 | * mrb_define_class_method(mrb, example_class, "example_method", mrb_example_method, MRB_ARGS_NONE());
|
---|
542 | * mrb_undef_class_method(mrb, example_class, "example_method");
|
---|
543 | * }
|
---|
544 | *
|
---|
545 | * void
|
---|
546 | * mrb_example_gem_final(mrb_state* mrb){
|
---|
547 | * }
|
---|
548 | * @param mrb The mruby state reference.
|
---|
549 | * @param cls A class the class method will be undefined from.
|
---|
550 | * @param name The name of the class method to be undefined.
|
---|
551 | */
|
---|
552 | MRB_API void mrb_undef_class_method(mrb_state *mrb, struct RClass *cls, const char *name);
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Initialize a new object instance of c class.
|
---|
556 | *
|
---|
557 | * Example:
|
---|
558 | *
|
---|
559 | * # Ruby style
|
---|
560 | * class ExampleClass
|
---|
561 | * end
|
---|
562 | *
|
---|
563 | * p ExampleClass # => #<ExampleClass:0x9958588>
|
---|
564 | * // C style
|
---|
565 | * #include <stdio.h>
|
---|
566 | * #include <mruby.h>
|
---|
567 | *
|
---|
568 | * void
|
---|
569 | * mrb_example_gem_init(mrb_state* mrb) {
|
---|
570 | * struct RClass *example_class;
|
---|
571 | * mrb_value obj;
|
---|
572 | * example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class); # => class ExampleClass; end
|
---|
573 | * obj = mrb_obj_new(mrb, example_class, 0, NULL); # => ExampleClass.new
|
---|
574 | * mrb_p(mrb, obj); // => Kernel#p
|
---|
575 | * }
|
---|
576 | * @param mrb The current mruby state.
|
---|
577 | * @param c Reference to the class of the new object.
|
---|
578 | * @param argc Number of arguments in argv
|
---|
579 | * @param argv Array of mrb_value to initialize the object
|
---|
580 | * @return [mrb_value] The newly initialized object
|
---|
581 | */
|
---|
582 | MRB_API mrb_value mrb_obj_new(mrb_state *mrb, struct RClass *c, mrb_int argc, const mrb_value *argv);
|
---|
583 |
|
---|
584 | /** @see mrb_obj_new */
|
---|
585 | MRB_INLINE mrb_value mrb_class_new_instance(mrb_state *mrb, mrb_int argc, const mrb_value *argv, struct RClass *c)
|
---|
586 | {
|
---|
587 | return mrb_obj_new(mrb,c,argc,argv);
|
---|
588 | }
|
---|
589 |
|
---|
590 | /**
|
---|
591 | * Creates a new instance of Class, Class.
|
---|
592 | *
|
---|
593 | * Example:
|
---|
594 | *
|
---|
595 | * void
|
---|
596 | * mrb_example_gem_init(mrb_state* mrb) {
|
---|
597 | * struct RClass *example_class;
|
---|
598 | *
|
---|
599 | * mrb_value obj;
|
---|
600 | * example_class = mrb_class_new(mrb, mrb->object_class);
|
---|
601 | * obj = mrb_obj_new(mrb, example_class, 0, NULL); // => #<#<Class:0x9a945b8>:0x9a94588>
|
---|
602 | * mrb_p(mrb, obj); // => Kernel#p
|
---|
603 | * }
|
---|
604 | *
|
---|
605 | * @param mrb The current mruby state.
|
---|
606 | * @param super The super class or parent.
|
---|
607 | * @return [struct RClass *] Reference to the new class.
|
---|
608 | */
|
---|
609 | MRB_API struct RClass * mrb_class_new(mrb_state *mrb, struct RClass *super);
|
---|
610 |
|
---|
611 | /**
|
---|
612 | * Creates a new module, Module.
|
---|
613 | *
|
---|
614 | * Example:
|
---|
615 | * void
|
---|
616 | * mrb_example_gem_init(mrb_state* mrb) {
|
---|
617 | * struct RClass *example_module;
|
---|
618 | *
|
---|
619 | * example_module = mrb_module_new(mrb);
|
---|
620 | * }
|
---|
621 | *
|
---|
622 | * @param mrb The current mruby state.
|
---|
623 | * @return [struct RClass *] Reference to the new module.
|
---|
624 | */
|
---|
625 | MRB_API struct RClass * mrb_module_new(mrb_state *mrb);
|
---|
626 |
|
---|
627 | /**
|
---|
628 | * Returns an mrb_bool. True if class was defined, and false if the class was not defined.
|
---|
629 | *
|
---|
630 | * Example:
|
---|
631 | * void
|
---|
632 | * mrb_example_gem_init(mrb_state* mrb) {
|
---|
633 | * struct RClass *example_class;
|
---|
634 | * mrb_bool cd;
|
---|
635 | *
|
---|
636 | * example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
|
---|
637 | * cd = mrb_class_defined(mrb, "ExampleClass");
|
---|
638 | *
|
---|
639 | * // If mrb_class_defined returns 1 then puts "True"
|
---|
640 | * // If mrb_class_defined returns 0 then puts "False"
|
---|
641 | * if (cd == 1){
|
---|
642 | * puts("True");
|
---|
643 | * }
|
---|
644 | * else {
|
---|
645 | * puts("False");
|
---|
646 | * }
|
---|
647 | * }
|
---|
648 | *
|
---|
649 | * @param mrb The current mruby state.
|
---|
650 | * @param name A string representing the name of the class.
|
---|
651 | * @return [mrb_bool] A boolean value.
|
---|
652 | */
|
---|
653 | MRB_API mrb_bool mrb_class_defined(mrb_state *mrb, const char *name);
|
---|
654 |
|
---|
655 | /**
|
---|
656 | * Gets a class.
|
---|
657 | * @param mrb The current mruby state.
|
---|
658 | * @param name The name of the class.
|
---|
659 | * @return [struct RClass *] A reference to the class.
|
---|
660 | */
|
---|
661 | MRB_API struct RClass * mrb_class_get(mrb_state *mrb, const char *name);
|
---|
662 |
|
---|
663 | /**
|
---|
664 | * Gets a exception class.
|
---|
665 | * @param mrb The current mruby state.
|
---|
666 | * @param name The name of the class.
|
---|
667 | * @return [struct RClass *] A reference to the class.
|
---|
668 | */
|
---|
669 | MRB_API struct RClass * mrb_exc_get(mrb_state *mrb, const char *name);
|
---|
670 |
|
---|
671 | /**
|
---|
672 | * Returns an mrb_bool. True if inner class was defined, and false if the inner class was not defined.
|
---|
673 | *
|
---|
674 | * Example:
|
---|
675 | * void
|
---|
676 | * mrb_example_gem_init(mrb_state* mrb) {
|
---|
677 | * struct RClass *example_outer, *example_inner;
|
---|
678 | * mrb_bool cd;
|
---|
679 | *
|
---|
680 | * example_outer = mrb_define_module(mrb, "ExampleOuter");
|
---|
681 | *
|
---|
682 | * example_inner = mrb_define_class_under(mrb, example_outer, "ExampleInner", mrb->object_class);
|
---|
683 | * cd = mrb_class_defined_under(mrb, example_outer, "ExampleInner");
|
---|
684 | *
|
---|
685 | * // If mrb_class_defined_under returns 1 then puts "True"
|
---|
686 | * // If mrb_class_defined_under returns 0 then puts "False"
|
---|
687 | * if (cd == 1){
|
---|
688 | * puts("True");
|
---|
689 | * }
|
---|
690 | * else {
|
---|
691 | * puts("False");
|
---|
692 | * }
|
---|
693 | * }
|
---|
694 | *
|
---|
695 | * @param mrb The current mruby state.
|
---|
696 | * @param outer The name of the outer class.
|
---|
697 | * @param name A string representing the name of the inner class.
|
---|
698 | * @return [mrb_bool] A boolean value.
|
---|
699 | */
|
---|
700 | MRB_API mrb_bool mrb_class_defined_under(mrb_state *mrb, struct RClass *outer, const char *name);
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * Gets a child class.
|
---|
704 | * @param mrb The current mruby state.
|
---|
705 | * @param outer The name of the parent class.
|
---|
706 | * @param name The name of the class.
|
---|
707 | * @return [struct RClass *] A reference to the class.
|
---|
708 | */
|
---|
709 | MRB_API struct RClass * mrb_class_get_under(mrb_state *mrb, struct RClass *outer, const char *name);
|
---|
710 |
|
---|
711 | /**
|
---|
712 | * Gets a module.
|
---|
713 | * @param mrb The current mruby state.
|
---|
714 | * @param name The name of the module.
|
---|
715 | * @return [struct RClass *] A reference to the module.
|
---|
716 | */
|
---|
717 | MRB_API struct RClass * mrb_module_get(mrb_state *mrb, const char *name);
|
---|
718 |
|
---|
719 | /**
|
---|
720 | * Gets a module defined under another module.
|
---|
721 | * @param mrb The current mruby state.
|
---|
722 | * @param outer The name of the outer module.
|
---|
723 | * @param name The name of the module.
|
---|
724 | * @return [struct RClass *] A reference to the module.
|
---|
725 | */
|
---|
726 | MRB_API struct RClass * mrb_module_get_under(mrb_state *mrb, struct RClass *outer, const char *name);
|
---|
727 | /* a function to raise NotImplementedError with current method name */
|
---|
728 | MRB_API void mrb_notimplement(mrb_state*);
|
---|
729 | /* a function to be replacement of unimplemented method */
|
---|
730 | MRB_API mrb_value mrb_notimplement_m(mrb_state*, mrb_value);
|
---|
731 |
|
---|
732 | /**
|
---|
733 | * Duplicate an object.
|
---|
734 | *
|
---|
735 | * Equivalent to:
|
---|
736 | * Object#dup
|
---|
737 | * @param mrb The current mruby state.
|
---|
738 | * @param obj Object to be duplicate.
|
---|
739 | * @return [mrb_value] The newly duplicated object.
|
---|
740 | */
|
---|
741 | MRB_API mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj);
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * Returns true if obj responds to the given method. If the method was defined for that
|
---|
745 | * class it returns true, it returns false otherwise.
|
---|
746 | *
|
---|
747 | * Example:
|
---|
748 | * # Ruby style
|
---|
749 | * class ExampleClass
|
---|
750 | * def example_method
|
---|
751 | * end
|
---|
752 | * end
|
---|
753 | *
|
---|
754 | * ExampleClass.new.respond_to?(:example_method) # => true
|
---|
755 | *
|
---|
756 | * // C style
|
---|
757 | * void
|
---|
758 | * mrb_example_gem_init(mrb_state* mrb) {
|
---|
759 | * struct RClass *example_class;
|
---|
760 | * mrb_sym mid;
|
---|
761 | * mrb_bool obj_resp;
|
---|
762 | *
|
---|
763 | * example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class);
|
---|
764 | * mrb_define_method(mrb, example_class, "example_method", exampleMethod, MRB_ARGS_NONE());
|
---|
765 | * mid = mrb_intern_str(mrb, mrb_str_new_lit(mrb, "example_method" ));
|
---|
766 | * obj_resp = mrb_obj_respond_to(mrb, example_class, mid); // => 1(true in Ruby world)
|
---|
767 | *
|
---|
768 | * // If mrb_obj_respond_to returns 1 then puts "True"
|
---|
769 | * // If mrb_obj_respond_to returns 0 then puts "False"
|
---|
770 | * if (obj_resp == 1) {
|
---|
771 | * puts("True");
|
---|
772 | * }
|
---|
773 | * else if (obj_resp == 0) {
|
---|
774 | * puts("False");
|
---|
775 | * }
|
---|
776 | * }
|
---|
777 | *
|
---|
778 | * @param mrb The current mruby state.
|
---|
779 | * @param c A reference to a class.
|
---|
780 | * @param mid A symbol referencing a method id.
|
---|
781 | * @return [mrb_bool] A boolean value.
|
---|
782 | */
|
---|
783 | MRB_API mrb_bool mrb_obj_respond_to(mrb_state *mrb, struct RClass* c, mrb_sym mid);
|
---|
784 |
|
---|
785 | /**
|
---|
786 | * Defines a new class under a given module
|
---|
787 | *
|
---|
788 | * @param mrb The current mruby state.
|
---|
789 | * @param outer Reference to the module under which the new class will be defined
|
---|
790 | * @param name The name of the defined class
|
---|
791 | * @param super The new class parent
|
---|
792 | * @return [struct RClass *] Reference to the newly defined class
|
---|
793 | * @see mrb_define_class
|
---|
794 | */
|
---|
795 | MRB_API struct RClass * mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super);
|
---|
796 |
|
---|
797 | MRB_API struct RClass * mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name);
|
---|
798 |
|
---|
799 | /**
|
---|
800 | * Function requires n arguments.
|
---|
801 | *
|
---|
802 | * @param n
|
---|
803 | * The number of required arguments.
|
---|
804 | */
|
---|
805 | #define MRB_ARGS_REQ(n) ((mrb_aspec)((n)&0x1f) << 18)
|
---|
806 |
|
---|
807 | /**
|
---|
808 | * Function takes n optional arguments
|
---|
809 | *
|
---|
810 | * @param n
|
---|
811 | * The number of optional arguments.
|
---|
812 | */
|
---|
813 | #define MRB_ARGS_OPT(n) ((mrb_aspec)((n)&0x1f) << 13)
|
---|
814 |
|
---|
815 | /**
|
---|
816 | * Function takes n1 mandatory arguments and n2 optional arguments
|
---|
817 | *
|
---|
818 | * @param n1
|
---|
819 | * The number of required arguments.
|
---|
820 | * @param n2
|
---|
821 | * The number of optional arguments.
|
---|
822 | */
|
---|
823 | #define MRB_ARGS_ARG(n1,n2) (MRB_ARGS_REQ(n1)|MRB_ARGS_OPT(n2))
|
---|
824 |
|
---|
825 | /** rest argument */
|
---|
826 | #define MRB_ARGS_REST() ((mrb_aspec)(1 << 12))
|
---|
827 |
|
---|
828 | /** required arguments after rest */
|
---|
829 | #define MRB_ARGS_POST(n) ((mrb_aspec)((n)&0x1f) << 7)
|
---|
830 |
|
---|
831 | /** keyword arguments (n of keys, kdict) */
|
---|
832 | #define MRB_ARGS_KEY(n1,n2) ((mrb_aspec)((((n1)&0x1f) << 2) | ((n2)?(1<<1):0)))
|
---|
833 |
|
---|
834 | /**
|
---|
835 | * Function takes a block argument
|
---|
836 | */
|
---|
837 | #define MRB_ARGS_BLOCK() ((mrb_aspec)1)
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Function accepts any number of arguments
|
---|
841 | */
|
---|
842 | #define MRB_ARGS_ANY() MRB_ARGS_REST()
|
---|
843 |
|
---|
844 | /**
|
---|
845 | * Function accepts no arguments
|
---|
846 | */
|
---|
847 | #define MRB_ARGS_NONE() ((mrb_aspec)0)
|
---|
848 |
|
---|
849 | /**
|
---|
850 | * Format specifiers for {mrb_get_args} function
|
---|
851 | *
|
---|
852 | * Must be a C string composed of the following format specifiers:
|
---|
853 | *
|
---|
854 | * | char | Ruby type | C types | Notes |
|
---|
855 | * |:----:|----------------|-------------------|----------------------------------------------------|
|
---|
856 | * | `o` | {Object} | {mrb_value} | Could be used to retrieve any type of argument |
|
---|
857 | * | `C` | {Class}/{Module} | {mrb_value} | |
|
---|
858 | * | `S` | {String} | {mrb_value} | when `!` follows, the value may be `nil` |
|
---|
859 | * | `A` | {Array} | {mrb_value} | when `!` follows, the value may be `nil` |
|
---|
860 | * | `H` | {Hash} | {mrb_value} | when `!` follows, the value may be `nil` |
|
---|
861 | * | `s` | {String} | char *, {mrb_int} | Receive two arguments; `s!` gives (`NULL`,`0`) for `nil` |
|
---|
862 | * | `z` | {String} | char * | `NULL` terminated string; `z!` gives `NULL` for `nil` |
|
---|
863 | * | `a` | {Array} | {mrb_value} *, {mrb_int} | Receive two arguments; `a!` gives (`NULL`,`0`) for `nil` |
|
---|
864 | * | `f` | {Fixnum}/{Float} | {mrb_float} | |
|
---|
865 | * | `i` | {Fixnum}/{Float} | {mrb_int} | |
|
---|
866 | * | `b` | boolean | {mrb_bool} | |
|
---|
867 | * | `n` | {String}/{Symbol} | {mrb_sym} | |
|
---|
868 | * | `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` |
|
---|
869 | * | `I` | inline struct | void * | |
|
---|
870 | * | `&` | block | {mrb_value} | &! raises exception if no block given. |
|
---|
871 | * | `*` | rest arguments | {mrb_value} *, {mrb_int} | Receive the rest of arguments as an array; `*!` avoid copy of the stack. |
|
---|
872 | * | <code>\|</code> | optional | | After this spec following specs would be optional. |
|
---|
873 | * | `?` | optional given | {mrb_bool} | `TRUE` if preceding argument is given. Used to check optional argument is given. |
|
---|
874 | * | `:` | keyword args | {mrb_kwargs} const | Get keyword arguments. @see mrb_kwargs |
|
---|
875 | *
|
---|
876 | * @see mrb_get_args
|
---|
877 | */
|
---|
878 | typedef const char *mrb_args_format;
|
---|
879 |
|
---|
880 | /**
|
---|
881 | * Get keyword arguments by `mrb_get_args()` with `:` specifier.
|
---|
882 | *
|
---|
883 | * `mrb_kwargs::num` indicates that the number of keyword values.
|
---|
884 | *
|
---|
885 | * `mrb_kwargs::values` is an object array, and the keyword argument corresponding to the string array is assigned.
|
---|
886 | * Note that `undef` is assigned if there is no keyword argument corresponding to `mrb_kwargs::optional`.
|
---|
887 | *
|
---|
888 | * `mrb_kwargs::table` accepts a string array.
|
---|
889 | *
|
---|
890 | * `mrb_kwargs::required` indicates that the specified number of keywords starting from the beginning of the string array are required.
|
---|
891 | *
|
---|
892 | * `mrb_kwargs::rest` is the remaining keyword argument that can be accepted as `**rest` in Ruby.
|
---|
893 | * If `NULL` is specified, `ArgumentError` is raised when there is an undefined keyword.
|
---|
894 | *
|
---|
895 | * Examples:
|
---|
896 | *
|
---|
897 | * // def method(a: 1, b: 2)
|
---|
898 | *
|
---|
899 | * uint32_t kw_num = 2;
|
---|
900 | * const char *kw_names[kw_num] = { "a", "b" };
|
---|
901 | * uint32_t kw_required = 0;
|
---|
902 | * mrb_value kw_values[kw_num];
|
---|
903 | * const mrb_kwargs kwargs = { kw_num, kw_values, kw_names, kw_required, NULL };
|
---|
904 | *
|
---|
905 | * mrb_get_args(mrb, ":", &kwargs);
|
---|
906 | * if (mrb_undef_p(kw_values[0])) { kw_values[0] = mrb_fixnum_value(1); }
|
---|
907 | * if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); }
|
---|
908 | *
|
---|
909 | *
|
---|
910 | * // def method(str, x:, y: 2, z: "default string", **opts)
|
---|
911 | *
|
---|
912 | * mrb_value str, kw_rest;
|
---|
913 | * uint32_t kw_num = 3;
|
---|
914 | * const char *kw_names[kw_num] = { "x", "y", "z" };
|
---|
915 | * uint32_t kw_required = 1;
|
---|
916 | * mrb_value kw_values[kw_num];
|
---|
917 | * const mrb_kwargs kwargs = { kw_num, kw_values, kw_names, kw_required, &kw_rest };
|
---|
918 | *
|
---|
919 | * mrb_get_args(mrb, "S:", &str, &kwargs);
|
---|
920 | * // or: mrb_get_args(mrb, ":S", &kwargs, &str);
|
---|
921 | * if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); }
|
---|
922 | * if (mrb_undef_p(kw_values[2])) { kw_values[2] = mrb_str_new_cstr(mrb, "default string"); }
|
---|
923 | */
|
---|
924 | typedef struct mrb_kwargs mrb_kwargs;
|
---|
925 |
|
---|
926 | struct mrb_kwargs
|
---|
927 | {
|
---|
928 | uint32_t num;
|
---|
929 | mrb_value *values;
|
---|
930 | const char *const *table;
|
---|
931 | uint32_t required;
|
---|
932 | mrb_value *rest;
|
---|
933 | };
|
---|
934 |
|
---|
935 | /**
|
---|
936 | * Retrieve arguments from mrb_state.
|
---|
937 | *
|
---|
938 | * @param mrb The current MRuby state.
|
---|
939 | * @param format is a list of format specifiers
|
---|
940 | * @param ... The passing variadic arguments must be a pointer of retrieving type.
|
---|
941 | * @return the number of arguments retrieved.
|
---|
942 | * @see mrb_args_format
|
---|
943 | * @see mrb_kwargs
|
---|
944 | */
|
---|
945 | MRB_API mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format, ...);
|
---|
946 |
|
---|
947 | MRB_INLINE mrb_sym
|
---|
948 | mrb_get_mid(mrb_state *mrb) /* get method symbol */
|
---|
949 | {
|
---|
950 | return mrb->c->ci->mid;
|
---|
951 | }
|
---|
952 |
|
---|
953 | /**
|
---|
954 | * Retrieve number of arguments from mrb_state.
|
---|
955 | *
|
---|
956 | * Correctly handles *splat arguments.
|
---|
957 | */
|
---|
958 | MRB_API mrb_int mrb_get_argc(mrb_state *mrb);
|
---|
959 |
|
---|
960 | MRB_API mrb_value* mrb_get_argv(mrb_state *mrb);
|
---|
961 |
|
---|
962 | /* `strlen` for character string literals (use with caution or `strlen` instead)
|
---|
963 | Adjacent string literals are concatenated in C/C++ in translation phase 6.
|
---|
964 | If `lit` is not one, the compiler will report a syntax error:
|
---|
965 | MSVC: "error C2143: syntax error : missing ')' before 'string'"
|
---|
966 | GCC: "error: expected ')' before string constant"
|
---|
967 | */
|
---|
968 | #define mrb_strlen_lit(lit) (sizeof(lit "") - 1)
|
---|
969 |
|
---|
970 | /**
|
---|
971 | * Call existing ruby functions.
|
---|
972 | *
|
---|
973 | * Example:
|
---|
974 | *
|
---|
975 | * #include <stdio.h>
|
---|
976 | * #include <mruby.h>
|
---|
977 | * #include "mruby/compile.h"
|
---|
978 | *
|
---|
979 | * int
|
---|
980 | * main()
|
---|
981 | * {
|
---|
982 | * mrb_int i = 99;
|
---|
983 | * mrb_state *mrb = mrb_open();
|
---|
984 | *
|
---|
985 | * if (!mrb) { }
|
---|
986 | * FILE *fp = fopen("test.rb","r");
|
---|
987 | * mrb_value obj = mrb_load_file(mrb,fp);
|
---|
988 | * mrb_funcall(mrb, obj, "method_name", 1, mrb_fixnum_value(i));
|
---|
989 | * fclose(fp);
|
---|
990 | * mrb_close(mrb);
|
---|
991 | * }
|
---|
992 | *
|
---|
993 | * @param mrb The current mruby state.
|
---|
994 | * @param val A reference to an mruby value.
|
---|
995 | * @param name The name of the method.
|
---|
996 | * @param argc The number of arguments the method has.
|
---|
997 | * @param ... Variadic values(not type safe!).
|
---|
998 | * @return [mrb_value] mruby function value.
|
---|
999 | */
|
---|
1000 | MRB_API mrb_value mrb_funcall(mrb_state *mrb, mrb_value val, const char *name, mrb_int argc, ...);
|
---|
1001 | /**
|
---|
1002 | * Call existing ruby functions. This is basically the type safe version of mrb_funcall.
|
---|
1003 | *
|
---|
1004 | * #include <stdio.h>
|
---|
1005 | * #include <mruby.h>
|
---|
1006 | * #include "mruby/compile.h"
|
---|
1007 | * int
|
---|
1008 | * main()
|
---|
1009 | * {
|
---|
1010 | * mrb_int i = 99;
|
---|
1011 | * mrb_state *mrb = mrb_open();
|
---|
1012 | *
|
---|
1013 | * if (!mrb) { }
|
---|
1014 | * mrb_sym m_sym = mrb_intern_lit(mrb, "method_name"); // Symbol for method.
|
---|
1015 | *
|
---|
1016 | * FILE *fp = fopen("test.rb","r");
|
---|
1017 | * mrb_value obj = mrb_load_file(mrb,fp);
|
---|
1018 | * mrb_funcall_argv(mrb, obj, m_sym, 1, &obj); // Calling ruby function from test.rb.
|
---|
1019 | * fclose(fp);
|
---|
1020 | * mrb_close(mrb);
|
---|
1021 | * }
|
---|
1022 | * @param mrb The current mruby state.
|
---|
1023 | * @param val A reference to an mruby value.
|
---|
1024 | * @param name_sym The symbol representing the method.
|
---|
1025 | * @param argc The number of arguments the method has.
|
---|
1026 | * @param obj Pointer to the object.
|
---|
1027 | * @return [mrb_value] mrb_value mruby function value.
|
---|
1028 | * @see mrb_funcall
|
---|
1029 | */
|
---|
1030 | MRB_API mrb_value mrb_funcall_argv(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv);
|
---|
1031 | /**
|
---|
1032 | * Call existing ruby functions with a block.
|
---|
1033 | */
|
---|
1034 | 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);
|
---|
1035 | /**
|
---|
1036 | * Create a symbol
|
---|
1037 | *
|
---|
1038 | * Example:
|
---|
1039 | *
|
---|
1040 | * # Ruby style:
|
---|
1041 | * :pizza # => :pizza
|
---|
1042 | *
|
---|
1043 | * // C style:
|
---|
1044 | * mrb_sym m_sym = mrb_intern_lit(mrb, "pizza"); // => :pizza
|
---|
1045 | *
|
---|
1046 | * @param mrb The current mruby state.
|
---|
1047 | * @param str The string to be symbolized
|
---|
1048 | * @return [mrb_sym] mrb_sym A symbol.
|
---|
1049 | */
|
---|
1050 | MRB_API mrb_sym mrb_intern_cstr(mrb_state *mrb, const char* str);
|
---|
1051 | MRB_API mrb_sym mrb_intern(mrb_state*,const char*,size_t);
|
---|
1052 | MRB_API mrb_sym mrb_intern_static(mrb_state*,const char*,size_t);
|
---|
1053 | #define mrb_intern_lit(mrb, lit) mrb_intern_static(mrb, lit, mrb_strlen_lit(lit))
|
---|
1054 | MRB_API mrb_sym mrb_intern_str(mrb_state*,mrb_value);
|
---|
1055 | MRB_API mrb_value mrb_check_intern_cstr(mrb_state*,const char*);
|
---|
1056 | MRB_API mrb_value mrb_check_intern(mrb_state*,const char*,size_t);
|
---|
1057 | MRB_API mrb_value mrb_check_intern_str(mrb_state*,mrb_value);
|
---|
1058 | MRB_API const char *mrb_sym_name(mrb_state*,mrb_sym);
|
---|
1059 | MRB_API const char *mrb_sym_name_len(mrb_state*,mrb_sym,mrb_int*);
|
---|
1060 | MRB_API const char *mrb_sym_dump(mrb_state*,mrb_sym);
|
---|
1061 | MRB_API mrb_value mrb_sym_str(mrb_state*,mrb_sym);
|
---|
1062 | #define mrb_sym2name(mrb,sym) mrb_sym_name(mrb,sym)
|
---|
1063 | #define mrb_sym2name_len(mrb,sym,len) mrb_sym_name_len(mrb,sym,len)
|
---|
1064 | #define mrb_sym2str(mrb,sym) mrb_sym_str(mrb,sym)
|
---|
1065 |
|
---|
1066 | MRB_API void *mrb_malloc(mrb_state*, size_t); /* raise RuntimeError if no mem */
|
---|
1067 | MRB_API void *mrb_calloc(mrb_state*, size_t, size_t); /* ditto */
|
---|
1068 | MRB_API void *mrb_realloc(mrb_state*, void*, size_t); /* ditto */
|
---|
1069 | MRB_API void *mrb_realloc_simple(mrb_state*, void*, size_t); /* return NULL if no memory available */
|
---|
1070 | MRB_API void *mrb_malloc_simple(mrb_state*, size_t); /* return NULL if no memory available */
|
---|
1071 | MRB_API struct RBasic *mrb_obj_alloc(mrb_state*, enum mrb_vtype, struct RClass*);
|
---|
1072 | MRB_API void mrb_free(mrb_state*, void*);
|
---|
1073 |
|
---|
1074 | MRB_API mrb_value mrb_str_new(mrb_state *mrb, const char *p, size_t len);
|
---|
1075 |
|
---|
1076 | /**
|
---|
1077 | * Turns a C string into a Ruby string value.
|
---|
1078 | */
|
---|
1079 | MRB_API mrb_value mrb_str_new_cstr(mrb_state*, const char*);
|
---|
1080 | MRB_API mrb_value mrb_str_new_static(mrb_state *mrb, const char *p, size_t len);
|
---|
1081 | #define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit))
|
---|
1082 |
|
---|
1083 | MRB_API mrb_value mrb_obj_freeze(mrb_state*, mrb_value);
|
---|
1084 | #define mrb_str_new_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new(mrb,p,len))
|
---|
1085 | #define mrb_str_new_cstr_frozen(mrb,p) mrb_obj_freeze(mrb,mrb_str_new_cstr(mrb,p))
|
---|
1086 | #define mrb_str_new_static_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new_static(mrb,p,len))
|
---|
1087 | #define mrb_str_new_lit_frozen(mrb,lit) mrb_obj_freeze(mrb,mrb_str_new_lit(mrb,lit))
|
---|
1088 |
|
---|
1089 | #ifdef _WIN32
|
---|
1090 | MRB_API char* mrb_utf8_from_locale(const char *p, int len);
|
---|
1091 | MRB_API char* mrb_locale_from_utf8(const char *p, int len);
|
---|
1092 | #define mrb_locale_free(p) free(p)
|
---|
1093 | #define mrb_utf8_free(p) free(p)
|
---|
1094 | #else
|
---|
1095 | #define mrb_utf8_from_locale(p, l) ((char*)(p))
|
---|
1096 | #define mrb_locale_from_utf8(p, l) ((char*)(p))
|
---|
1097 | #define mrb_locale_free(p)
|
---|
1098 | #define mrb_utf8_free(p)
|
---|
1099 | #endif
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Creates new mrb_state.
|
---|
1103 | *
|
---|
1104 | * @return
|
---|
1105 | * Pointer to the newly created mrb_state.
|
---|
1106 | */
|
---|
1107 | MRB_API mrb_state* mrb_open(void);
|
---|
1108 |
|
---|
1109 | /**
|
---|
1110 | * Create new mrb_state with custom allocators.
|
---|
1111 | *
|
---|
1112 | * @param f
|
---|
1113 | * Reference to the allocation function.
|
---|
1114 | * @param ud
|
---|
1115 | * User data will be passed to custom allocator f.
|
---|
1116 | * If user data isn't required just pass NULL.
|
---|
1117 | * @return
|
---|
1118 | * Pointer to the newly created mrb_state.
|
---|
1119 | */
|
---|
1120 | MRB_API mrb_state* mrb_open_allocf(mrb_allocf f, void *ud, mrb_bool disable_gems);
|
---|
1121 |
|
---|
1122 | /**
|
---|
1123 | * Create new mrb_state with just the MRuby core
|
---|
1124 | *
|
---|
1125 | * @param f
|
---|
1126 | * Reference to the allocation function.
|
---|
1127 | * Use mrb_default_allocf for the default
|
---|
1128 | * @param ud
|
---|
1129 | * User data will be passed to custom allocator f.
|
---|
1130 | * If user data isn't required just pass NULL.
|
---|
1131 | * @return
|
---|
1132 | * Pointer to the newly created mrb_state.
|
---|
1133 | */
|
---|
1134 | MRB_API mrb_state* mrb_open_core(mrb_allocf f, void *ud);
|
---|
1135 |
|
---|
1136 | /**
|
---|
1137 | * Closes and frees a mrb_state.
|
---|
1138 | *
|
---|
1139 | * @param mrb
|
---|
1140 | * Pointer to the mrb_state to be closed.
|
---|
1141 | */
|
---|
1142 | MRB_API void mrb_close(mrb_state *mrb);
|
---|
1143 |
|
---|
1144 | /**
|
---|
1145 | * The default allocation function.
|
---|
1146 | *
|
---|
1147 | * @see mrb_allocf
|
---|
1148 | */
|
---|
1149 | MRB_API void* mrb_default_allocf(mrb_state*, void*, size_t, void*);
|
---|
1150 |
|
---|
1151 | MRB_API mrb_value mrb_top_self(mrb_state *mrb);
|
---|
1152 | MRB_API mrb_value mrb_run(mrb_state *mrb, struct RProc* proc, mrb_value self);
|
---|
1153 | MRB_API mrb_value mrb_top_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int stack_keep);
|
---|
1154 | MRB_API mrb_value mrb_vm_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int stack_keep);
|
---|
1155 | MRB_API mrb_value mrb_vm_exec(mrb_state *mrb, struct RProc *proc, const mrb_code *iseq);
|
---|
1156 | /* compatibility macros */
|
---|
1157 | #define mrb_toplevel_run_keep(m,p,k) mrb_top_run((m),(p),mrb_top_self(m),(k))
|
---|
1158 | #define mrb_toplevel_run(m,p) mrb_toplevel_run_keep((m),(p),0)
|
---|
1159 | #define mrb_context_run(m,p,s,k) mrb_vm_run((m),(p),(s),(k))
|
---|
1160 |
|
---|
1161 | MRB_API void mrb_p(mrb_state*, mrb_value);
|
---|
1162 | MRB_API mrb_int mrb_obj_id(mrb_value obj);
|
---|
1163 | MRB_API mrb_sym mrb_obj_to_sym(mrb_state *mrb, mrb_value name);
|
---|
1164 |
|
---|
1165 | MRB_API mrb_bool mrb_obj_eq(mrb_state *mrb, mrb_value a, mrb_value b);
|
---|
1166 | MRB_API mrb_bool mrb_obj_equal(mrb_state *mrb, mrb_value a, mrb_value b);
|
---|
1167 | MRB_API mrb_bool mrb_equal(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
|
---|
1168 | MRB_API mrb_value mrb_convert_to_integer(mrb_state *mrb, mrb_value val, mrb_int base);
|
---|
1169 | MRB_API mrb_value mrb_Integer(mrb_state *mrb, mrb_value val);
|
---|
1170 | #ifndef MRB_WITHOUT_FLOAT
|
---|
1171 | MRB_API mrb_value mrb_Float(mrb_state *mrb, mrb_value val);
|
---|
1172 | #endif
|
---|
1173 | MRB_API mrb_value mrb_inspect(mrb_state *mrb, mrb_value obj);
|
---|
1174 | MRB_API mrb_bool mrb_eql(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
|
---|
1175 | /* mrb_cmp(mrb, obj1, obj2): 1:0:-1; -2 for error */
|
---|
1176 | MRB_API mrb_int mrb_cmp(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
|
---|
1177 |
|
---|
1178 | MRB_INLINE int
|
---|
1179 | mrb_gc_arena_save(mrb_state *mrb)
|
---|
1180 | {
|
---|
1181 | return mrb->gc.arena_idx;
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | MRB_INLINE void
|
---|
1185 | mrb_gc_arena_restore(mrb_state *mrb, int idx)
|
---|
1186 | {
|
---|
1187 | mrb->gc.arena_idx = idx;
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | MRB_API void mrb_garbage_collect(mrb_state*);
|
---|
1191 | MRB_API void mrb_full_gc(mrb_state*);
|
---|
1192 | MRB_API void mrb_incremental_gc(mrb_state *);
|
---|
1193 | MRB_API void mrb_gc_mark(mrb_state*,struct RBasic*);
|
---|
1194 | #define mrb_gc_mark_value(mrb,val) do {\
|
---|
1195 | if (!mrb_immediate_p(val)) mrb_gc_mark((mrb), mrb_basic_ptr(val)); \
|
---|
1196 | } while (0)
|
---|
1197 | MRB_API void mrb_field_write_barrier(mrb_state *, struct RBasic*, struct RBasic*);
|
---|
1198 | #define mrb_field_write_barrier_value(mrb, obj, val) do{\
|
---|
1199 | if (!mrb_immediate_p(val)) mrb_field_write_barrier((mrb), (obj), mrb_basic_ptr(val)); \
|
---|
1200 | } while (0)
|
---|
1201 | MRB_API void mrb_write_barrier(mrb_state *, struct RBasic*);
|
---|
1202 |
|
---|
1203 | MRB_API mrb_value mrb_check_convert_type(mrb_state *mrb, mrb_value val, enum mrb_vtype type, const char *tname, const char *method);
|
---|
1204 | MRB_API mrb_value mrb_any_to_s(mrb_state *mrb, mrb_value obj);
|
---|
1205 | MRB_API const char * mrb_obj_classname(mrb_state *mrb, mrb_value obj);
|
---|
1206 | MRB_API struct RClass* mrb_obj_class(mrb_state *mrb, mrb_value obj);
|
---|
1207 | MRB_API mrb_value mrb_class_path(mrb_state *mrb, struct RClass *c);
|
---|
1208 | MRB_API mrb_value mrb_convert_type(mrb_state *mrb, mrb_value val, enum mrb_vtype type, const char *tname, const char *method);
|
---|
1209 | MRB_API mrb_bool mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c);
|
---|
1210 | MRB_API mrb_value mrb_obj_inspect(mrb_state *mrb, mrb_value self);
|
---|
1211 | MRB_API mrb_value mrb_obj_clone(mrb_state *mrb, mrb_value self);
|
---|
1212 |
|
---|
1213 | #ifndef ISPRINT
|
---|
1214 | #define ISASCII(c) ((unsigned)(c) <= 0x7f)
|
---|
1215 | #define ISPRINT(c) (((unsigned)(c) - 0x20) < 0x5f)
|
---|
1216 | #define ISSPACE(c) ((c) == ' ' || (unsigned)(c) - '\t' < 5)
|
---|
1217 | #define ISUPPER(c) (((unsigned)(c) - 'A') < 26)
|
---|
1218 | #define ISLOWER(c) (((unsigned)(c) - 'a') < 26)
|
---|
1219 | #define ISALPHA(c) ((((unsigned)(c) | 0x20) - 'a') < 26)
|
---|
1220 | #define ISDIGIT(c) (((unsigned)(c) - '0') < 10)
|
---|
1221 | #define ISXDIGIT(c) (ISDIGIT(c) || ((unsigned)(c) | 0x20) - 'a' < 6)
|
---|
1222 | #define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c))
|
---|
1223 | #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
|
---|
1224 | #define ISCNTRL(c) ((unsigned)(c) < 0x20 || (c) == 0x7f)
|
---|
1225 | #define TOUPPER(c) (ISLOWER(c) ? ((c) & 0x5f) : (c))
|
---|
1226 | #define TOLOWER(c) (ISUPPER(c) ? ((c) | 0x20) : (c))
|
---|
1227 | #endif
|
---|
1228 |
|
---|
1229 | MRB_API mrb_value mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, size_t len);
|
---|
1230 | MRB_API mrb_noreturn void mrb_exc_raise(mrb_state *mrb, mrb_value exc);
|
---|
1231 |
|
---|
1232 | MRB_API mrb_noreturn void mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg);
|
---|
1233 | MRB_API mrb_noreturn void mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...);
|
---|
1234 | MRB_API mrb_noreturn void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...);
|
---|
1235 | MRB_API mrb_noreturn void mrb_frozen_error(mrb_state *mrb, void *frozen_obj);
|
---|
1236 | MRB_API mrb_noreturn void mrb_argnum_error(mrb_state *mrb, mrb_int argc, int min, int max);
|
---|
1237 | MRB_API void mrb_warn(mrb_state *mrb, const char *fmt, ...);
|
---|
1238 | MRB_API mrb_noreturn void mrb_bug(mrb_state *mrb, const char *fmt, ...);
|
---|
1239 | MRB_API void mrb_print_backtrace(mrb_state *mrb);
|
---|
1240 | MRB_API void mrb_print_error(mrb_state *mrb);
|
---|
1241 | /* function for `raisef` formatting */
|
---|
1242 | MRB_API mrb_value mrb_vformat(mrb_state *mrb, const char *format, va_list ap);
|
---|
1243 |
|
---|
1244 | /* macros to get typical exception objects
|
---|
1245 | note:
|
---|
1246 | + those E_* macros requires mrb_state* variable named mrb.
|
---|
1247 | + exception objects obtained from those macros are local to mrb
|
---|
1248 | */
|
---|
1249 | #define E_RUNTIME_ERROR (mrb_exc_get(mrb, "RuntimeError"))
|
---|
1250 | #define E_TYPE_ERROR (mrb_exc_get(mrb, "TypeError"))
|
---|
1251 | #define E_ARGUMENT_ERROR (mrb_exc_get(mrb, "ArgumentError"))
|
---|
1252 | #define E_INDEX_ERROR (mrb_exc_get(mrb, "IndexError"))
|
---|
1253 | #define E_RANGE_ERROR (mrb_exc_get(mrb, "RangeError"))
|
---|
1254 | #define E_NAME_ERROR (mrb_exc_get(mrb, "NameError"))
|
---|
1255 | #define E_NOMETHOD_ERROR (mrb_exc_get(mrb, "NoMethodError"))
|
---|
1256 | #define E_SCRIPT_ERROR (mrb_exc_get(mrb, "ScriptError"))
|
---|
1257 | #define E_SYNTAX_ERROR (mrb_exc_get(mrb, "SyntaxError"))
|
---|
1258 | #define E_LOCALJUMP_ERROR (mrb_exc_get(mrb, "LocalJumpError"))
|
---|
1259 | #define E_REGEXP_ERROR (mrb_exc_get(mrb, "RegexpError"))
|
---|
1260 | #define E_FROZEN_ERROR (mrb_exc_get(mrb, "FrozenError"))
|
---|
1261 |
|
---|
1262 | #define E_NOTIMP_ERROR (mrb_exc_get(mrb, "NotImplementedError"))
|
---|
1263 | #ifndef MRB_WITHOUT_FLOAT
|
---|
1264 | #define E_FLOATDOMAIN_ERROR (mrb_exc_get(mrb, "FloatDomainError"))
|
---|
1265 | #endif
|
---|
1266 |
|
---|
1267 | #define E_KEY_ERROR (mrb_exc_get(mrb, "KeyError"))
|
---|
1268 |
|
---|
1269 | MRB_API mrb_value mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg);
|
---|
1270 | MRB_API mrb_value mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv);
|
---|
1271 | 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);
|
---|
1272 |
|
---|
1273 | /* continue execution to the proc */
|
---|
1274 | /* this function should always be called as the last function of a method */
|
---|
1275 | /* e.g. return mrb_yield_cont(mrb, proc, self, argc, argv); */
|
---|
1276 | mrb_value mrb_yield_cont(mrb_state *mrb, mrb_value b, mrb_value self, mrb_int argc, const mrb_value *argv);
|
---|
1277 |
|
---|
1278 | /* mrb_gc_protect() leaves the object in the arena */
|
---|
1279 | MRB_API void mrb_gc_protect(mrb_state *mrb, mrb_value obj);
|
---|
1280 | /* mrb_gc_register() keeps the object from GC. */
|
---|
1281 | MRB_API void mrb_gc_register(mrb_state *mrb, mrb_value obj);
|
---|
1282 | /* mrb_gc_unregister() removes the object from GC root. */
|
---|
1283 | MRB_API void mrb_gc_unregister(mrb_state *mrb, mrb_value obj);
|
---|
1284 |
|
---|
1285 | MRB_API mrb_value mrb_to_int(mrb_state *mrb, mrb_value val);
|
---|
1286 | #define mrb_int(mrb, val) mrb_fixnum(mrb_to_int(mrb, val))
|
---|
1287 | /* string type checking (contrary to the name, it doesn't convert) */
|
---|
1288 | MRB_API mrb_value mrb_to_str(mrb_state *mrb, mrb_value val);
|
---|
1289 | MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t);
|
---|
1290 |
|
---|
1291 | MRB_INLINE void mrb_check_frozen(mrb_state *mrb, void *o)
|
---|
1292 | {
|
---|
1293 | if (mrb_frozen_p((struct RBasic*)o)) mrb_frozen_error(mrb, o);
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | typedef enum call_type {
|
---|
1297 | CALL_PUBLIC,
|
---|
1298 | CALL_FCALL,
|
---|
1299 | CALL_VCALL,
|
---|
1300 | CALL_TYPE_MAX
|
---|
1301 | } call_type;
|
---|
1302 |
|
---|
1303 | MRB_API void mrb_define_alias(mrb_state *mrb, struct RClass *c, const char *a, const char *b);
|
---|
1304 | MRB_API const char *mrb_class_name(mrb_state *mrb, struct RClass* klass);
|
---|
1305 | MRB_API void mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val);
|
---|
1306 |
|
---|
1307 | MRB_API mrb_value mrb_attr_get(mrb_state *mrb, mrb_value obj, mrb_sym id);
|
---|
1308 |
|
---|
1309 | MRB_API mrb_bool mrb_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym mid);
|
---|
1310 | MRB_API mrb_bool mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, struct RClass* c);
|
---|
1311 | MRB_API mrb_bool mrb_func_basic_p(mrb_state *mrb, mrb_value obj, mrb_sym mid, mrb_func_t func);
|
---|
1312 |
|
---|
1313 |
|
---|
1314 | /**
|
---|
1315 | * Resume a Fiber
|
---|
1316 | *
|
---|
1317 | * Implemented in mruby-fiber
|
---|
1318 | */
|
---|
1319 | MRB_API mrb_value mrb_fiber_resume(mrb_state *mrb, mrb_value fib, mrb_int argc, const mrb_value *argv);
|
---|
1320 |
|
---|
1321 | /**
|
---|
1322 | * Yield a Fiber
|
---|
1323 | *
|
---|
1324 | * Implemented in mruby-fiber
|
---|
1325 | */
|
---|
1326 | MRB_API mrb_value mrb_fiber_yield(mrb_state *mrb, mrb_int argc, const mrb_value *argv);
|
---|
1327 |
|
---|
1328 | /**
|
---|
1329 | * Check if a Fiber is alive
|
---|
1330 | *
|
---|
1331 | * Implemented in mruby-fiber
|
---|
1332 | */
|
---|
1333 | MRB_API mrb_value mrb_fiber_alive_p(mrb_state *mrb, mrb_value fib);
|
---|
1334 |
|
---|
1335 | /**
|
---|
1336 | * FiberError reference
|
---|
1337 | *
|
---|
1338 | * Implemented in mruby-fiber
|
---|
1339 | */
|
---|
1340 | #define E_FIBER_ERROR (mrb_exc_get(mrb, "FiberError"))
|
---|
1341 | MRB_API void mrb_stack_extend(mrb_state*, mrb_int);
|
---|
1342 |
|
---|
1343 | /* memory pool implementation */
|
---|
1344 | typedef struct mrb_pool mrb_pool;
|
---|
1345 | MRB_API struct mrb_pool* mrb_pool_open(mrb_state*);
|
---|
1346 | MRB_API void mrb_pool_close(struct mrb_pool*);
|
---|
1347 | MRB_API void* mrb_pool_alloc(struct mrb_pool*, size_t);
|
---|
1348 | MRB_API void* mrb_pool_realloc(struct mrb_pool*, void*, size_t oldlen, size_t newlen);
|
---|
1349 | MRB_API mrb_bool mrb_pool_can_realloc(struct mrb_pool*, void*, size_t);
|
---|
1350 | /* temporary memory allocation, only effective while GC arena is kept */
|
---|
1351 | MRB_API void* mrb_alloca(mrb_state *mrb, size_t);
|
---|
1352 |
|
---|
1353 | MRB_API void mrb_state_atexit(mrb_state *mrb, mrb_atexit_func func);
|
---|
1354 |
|
---|
1355 | MRB_API void mrb_show_version(mrb_state *mrb);
|
---|
1356 | MRB_API void mrb_show_copyright(mrb_state *mrb);
|
---|
1357 |
|
---|
1358 | MRB_API mrb_value mrb_format(mrb_state *mrb, const char *format, ...);
|
---|
1359 |
|
---|
1360 | #if 0
|
---|
1361 | /* memcpy and memset does not work with gdb reverse-next on my box */
|
---|
1362 | /* use naive memcpy and memset instead */
|
---|
1363 | #undef memcpy
|
---|
1364 | #undef memset
|
---|
1365 | static void*
|
---|
1366 | mrbmemcpy(void *dst, const void *src, size_t n)
|
---|
1367 | {
|
---|
1368 | char *d = (char*)dst;
|
---|
1369 | const char *s = (const char*)src;
|
---|
1370 | while (n--)
|
---|
1371 | *d++ = *s++;
|
---|
1372 | return d;
|
---|
1373 | }
|
---|
1374 | #define memcpy(a,b,c) mrbmemcpy(a,b,c)
|
---|
1375 |
|
---|
1376 | static void*
|
---|
1377 | mrbmemset(void *s, int c, size_t n)
|
---|
1378 | {
|
---|
1379 | char *t = (char*)s;
|
---|
1380 | while (n--)
|
---|
1381 | *t++ = c;
|
---|
1382 | return s;
|
---|
1383 | }
|
---|
1384 | #define memset(a,b,c) mrbmemset(a,b,c)
|
---|
1385 | #endif
|
---|
1386 |
|
---|
1387 | MRB_END_DECL
|
---|
1388 |
|
---|
1389 | #endif /* MRUBY_H */
|
---|