| 1 |
/* |
| 2 |
** mruby - An embeddable Ruby implementation |
| 3 |
** |
| 4 |
** Copyright (c) mruby developers 2010-2023 |
| 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 |
struct mrb_state; |
| 159 |
|
| 160 |
/** |
| 161 |
* Function pointer type of custom allocator used in @see mrb_open_allocf. |
| 162 |
* |
| 163 |
* The function pointing it must behave similarly as realloc except: |
| 164 |
* - If ptr is NULL it must allocate new space. |
| 165 |
* - If s is NULL, ptr must be freed. |
| 166 |
* |
| 167 |
* See @see mrb_default_allocf for the default implementation. |
| 168 |
*/ |
| 169 |
typedef void* (*mrb_allocf) (struct mrb_state *mrb, void*, size_t, void *ud); |
| 170 |
|
| 171 |
#ifndef MRB_FIXED_STATE_ATEXIT_STACK_SIZE |
| 172 |
#define MRB_FIXED_STATE_ATEXIT_STACK_SIZE 5 |
| 173 |
#endif |
| 174 |
|
| 175 |
typedef struct { |
| 176 |
uint8_t n:4; /* (15=*) c=n|nk<<4 */ |
| 177 |
uint8_t nk:4; /* (15=*) */ |
| 178 |
uint8_t cci; /* called from C function */ |
| 179 |
mrb_sym mid; |
| 180 |
const struct RProc *proc; |
| 181 |
struct RProc *blk; |
| 182 |
mrb_value *stack; |
| 183 |
const mrb_code *pc; /* current address on iseq of this proc */ |
| 184 |
union { |
| 185 |
struct REnv *env; |
| 186 |
struct RClass *target_class; |
| 187 |
} u; |
| 188 |
} mrb_callinfo; |
| 189 |
|
| 190 |
enum mrb_fiber_state { |
| 191 |
MRB_FIBER_CREATED = 0, |
| 192 |
MRB_FIBER_RUNNING, |
| 193 |
MRB_FIBER_RESUMED, |
| 194 |
MRB_FIBER_SUSPENDED, |
| 195 |
MRB_FIBER_TRANSFERRED, |
| 196 |
MRB_FIBER_TERMINATED, |
| 197 |
}; |
| 198 |
|
| 199 |
struct mrb_context { |
| 200 |
struct mrb_context *prev; |
| 201 |
|
| 202 |
mrb_value *stbase, *stend; /* stack of virtual machine */ |
| 203 |
|
| 204 |
mrb_callinfo *ci; |
| 205 |
mrb_callinfo *cibase, *ciend; |
| 206 |
|
| 207 |
enum mrb_fiber_state status : 4; |
| 208 |
mrb_bool vmexec : 1; |
| 209 |
struct RFiber *fib; |
| 210 |
}; |
| 211 |
|
| 212 |
#ifdef MRB_METHOD_CACHE_SIZE |
| 213 |
# undef MRB_NO_METHOD_CACHE |
| 214 |
mrb_static_assert_powerof2(MRB_METHOD_CACHE_SIZE); |
| 215 |
#else |
| 216 |
/* default method cache size: 256 */ |
| 217 |
/* cache size needs to be power of 2 */ |
| 218 |
# define MRB_METHOD_CACHE_SIZE (1<<8) |
| 219 |
#endif |
| 220 |
|
| 221 |
/** |
| 222 |
* Function pointer type for a function callable by mruby. |
| 223 |
* |
| 224 |
* The arguments to the function are stored on the mrb_state. To get them see mrb_get_args |
| 225 |
* |
| 226 |
* @param mrb The mruby state |
| 227 |
* @param self The self object |
| 228 |
* @return [mrb_value] The function's return value |
| 229 |
*/ |
| 230 |
typedef mrb_value (*mrb_func_t)(struct mrb_state *mrb, mrb_value self); |
| 231 |
|
| 232 |
#ifndef MRB_USE_METHOD_T_STRUCT |
| 233 |
typedef uintptr_t mrb_method_t; |
| 234 |
#else |
| 235 |
typedef struct { |
| 236 |
uint8_t flags; |
| 237 |
union { |
| 238 |
struct RProc *proc; |
| 239 |
mrb_func_t func; |
| 240 |
}; |
| 241 |
} mrb_method_t; |
| 242 |
#endif |
| 243 |
|
| 244 |
#ifndef MRB_NO_METHOD_CACHE |
| 245 |
struct mrb_cache_entry { |
| 246 |
struct RClass *c, *c0; |
| 247 |
mrb_sym mid; |
| 248 |
mrb_method_t m; |
| 249 |
}; |
| 250 |
#endif |
| 251 |
|
| 252 |
struct mrb_jmpbuf; |
| 253 |
|
| 254 |
typedef void (*mrb_atexit_func)(struct mrb_state*); |
| 255 |
|
| 256 |
typedef struct mrb_state { |
| 257 |
struct mrb_jmpbuf *jmp; |
| 258 |
|
| 259 |
mrb_allocf allocf; /* memory allocation function */ |
| 260 |
void *allocf_ud; /* auxiliary data of allocf */ |
| 261 |
|
| 262 |
struct mrb_context *c; |
| 263 |
struct mrb_context *root_c; |
| 264 |
struct iv_tbl *globals; /* global variable table */ |
| 265 |
|
| 266 |
struct RObject *exc; /* exception */ |
| 267 |
|
| 268 |
struct RObject *top_self; |
| 269 |
struct RClass *object_class; /* Object class */ |
| 270 |
struct RClass *class_class; |
| 271 |
struct RClass *module_class; |
| 272 |
struct RClass *proc_class; |
| 273 |
struct RClass *string_class; |
| 274 |
struct RClass *array_class; |
| 275 |
struct RClass *hash_class; |
| 276 |
struct RClass *range_class; |
| 277 |
|
| 278 |
#ifndef MRB_NO_FLOAT |
| 279 |
struct RClass *float_class; |
| 280 |
#endif |
| 281 |
struct RClass *integer_class; |
| 282 |
struct RClass *true_class; |
| 283 |
struct RClass *false_class; |
| 284 |
struct RClass *nil_class; |
| 285 |
struct RClass *symbol_class; |
| 286 |
struct RClass *kernel_module; |
| 287 |
|
| 288 |
mrb_gc gc; |
| 289 |
|
| 290 |
#ifndef MRB_NO_METHOD_CACHE |
| 291 |
struct mrb_cache_entry cache[MRB_METHOD_CACHE_SIZE]; |
| 292 |
#endif |
| 293 |
|
| 294 |
mrb_sym symidx; |
| 295 |
const char **symtbl; |
| 296 |
uint8_t *symlink; |
| 297 |
uint8_t *symflags; |
| 298 |
mrb_sym symhash[256]; |
| 299 |
size_t symcapa; |
| 300 |
#ifndef MRB_USE_ALL_SYMBOLS |
| 301 |
char symbuf[8]; /* buffer for small symbol names */ |
| 302 |
#endif |
| 303 |
|
| 304 |
#ifdef MRB_USE_DEBUG_HOOK |
| 305 |
void (*code_fetch_hook)(struct mrb_state* mrb, const struct mrb_irep *irep, const mrb_code *pc, mrb_value *regs); |
| 306 |
void (*debug_op_hook)(struct mrb_state* mrb, const struct mrb_irep *irep, const mrb_code *pc, mrb_value *regs); |
| 307 |
#endif |
| 308 |
|
| 309 |
#ifdef MRB_BYTECODE_DECODE_OPTION |
| 310 |
mrb_code (*bytecode_decoder)(struct mrb_state* mrb, mrb_code code); |
| 311 |
#endif |
| 312 |
|
| 313 |
struct RClass *eException_class; |
| 314 |
struct RClass *eStandardError_class; |
| 315 |
struct RObject *nomem_err; /* pre-allocated NoMemoryError */ |
| 316 |
struct RObject *stack_err; /* pre-allocated SystemStackError */ |
| 317 |
#ifdef MRB_GC_FIXED_ARENA |
| 318 |
struct RObject *arena_err; /* pre-allocated arena overflow error */ |
| 319 |
#endif |
| 320 |
|
| 321 |
void *ud; /* auxiliary data */ |
| 322 |
|
| 323 |
#ifdef MRB_FIXED_STATE_ATEXIT_STACK |
| 324 |
mrb_atexit_func atexit_stack[MRB_FIXED_STATE_ATEXIT_STACK_SIZE]; |
| 325 |
#else |
| 326 |
mrb_atexit_func *atexit_stack; |
| 327 |
#endif |
| 328 |
uint16_t atexit_stack_len; |
| 329 |
} mrb_state; |
| 330 |
|
| 331 |
/** |
| 332 |
* Defines a new class. |
| 333 |
* |
| 334 |
* If you're creating a gem it may look something like this: |
| 335 |
* |
| 336 |
* !!!c |
| 337 |
* void mrb_example_gem_init(mrb_state* mrb) { |
| 338 |
* struct RClass *example_class; |
| 339 |
* example_class = mrb_define_class(mrb, "Example_Class", mrb->object_class); |
| 340 |
* } |
| 341 |
* |
| 342 |
* void mrb_example_gem_final(mrb_state* mrb) { |
| 343 |
* //free(TheAnimals); |
| 344 |
* } |
| 345 |
* |
| 346 |
* @param mrb The current mruby state. |
| 347 |
* @param name The name of the defined class. |
| 348 |
* @param super The new class parent. |
| 349 |
* @return [struct RClass *] Reference to the newly defined class. |
| 350 |
* @see mrb_define_class_under |
| 351 |
*/ |
| 352 |
MRB_API struct RClass *mrb_define_class(mrb_state *mrb, const char *name, struct RClass *super); |
| 353 |
MRB_API struct RClass *mrb_define_class_id(mrb_state *mrb, mrb_sym name, struct RClass *super); |
| 354 |
|
| 355 |
/** |
| 356 |
* Defines a new module. |
| 357 |
* |
| 358 |
* @param mrb The current mruby state. |
| 359 |
* @param name The name of the module. |
| 360 |
* @return [struct RClass *] Reference to the newly defined module. |
| 361 |
*/ |
| 362 |
MRB_API struct RClass *mrb_define_module(mrb_state *mrb, const char *name); |
| 363 |
MRB_API struct RClass *mrb_define_module_id(mrb_state *mrb, mrb_sym name); |
| 364 |
|
| 365 |
/** |
| 366 |
* Returns the singleton class of an object. |
| 367 |
* |
| 368 |
* Raises a `TypeError` exception for immediate values. |
| 369 |
*/ |
| 370 |
MRB_API mrb_value mrb_singleton_class(mrb_state *mrb, mrb_value val); |
| 371 |
|
| 372 |
/** |
| 373 |
* Returns the singleton class of an object. |
| 374 |
* |
| 375 |
* Returns `NULL` for immediate values, |
| 376 |
*/ |
| 377 |
MRB_API struct RClass *mrb_singleton_class_ptr(mrb_state *mrb, mrb_value val); |
| 378 |
|
| 379 |
/** |
| 380 |
* Include a module in another class or module. |
| 381 |
* Equivalent to: |
| 382 |
* |
| 383 |
* module B |
| 384 |
* include A |
| 385 |
* end |
| 386 |
* @param mrb The current mruby state. |
| 387 |
* @param cla A reference to module or a class. |
| 388 |
* @param included A reference to the module to be included. |
| 389 |
*/ |
| 390 |
MRB_API void mrb_include_module(mrb_state *mrb, struct RClass *cla, struct RClass *included); |
| 391 |
|
| 392 |
/** |
| 393 |
* Prepends a module in another class or module. |
| 394 |
* |
| 395 |
* Equivalent to: |
| 396 |
* module B |
| 397 |
* prepend A |
| 398 |
* end |
| 399 |
* @param mrb The current mruby state. |
| 400 |
* @param cla A reference to module or a class. |
| 401 |
* @param prepended A reference to the module to be prepended. |
| 402 |
*/ |
| 403 |
MRB_API void mrb_prepend_module(mrb_state *mrb, struct RClass *cla, struct RClass *prepended); |
| 404 |
|
| 405 |
/** |
| 406 |
* Defines a global function in ruby. |
| 407 |
* |
| 408 |
* If you're creating a gem it may look something like this |
| 409 |
* |
| 410 |
* Example: |
| 411 |
* |
| 412 |
* mrb_value example_method(mrb_state* mrb, mrb_value self) |
| 413 |
* { |
| 414 |
* puts("Executing example command!"); |
| 415 |
* return self; |
| 416 |
* } |
| 417 |
* |
| 418 |
* void mrb_example_gem_init(mrb_state* mrb) |
| 419 |
* { |
| 420 |
* mrb_define_method(mrb, mrb->kernel_module, "example_method", example_method, MRB_ARGS_NONE()); |
| 421 |
* } |
| 422 |
* |
| 423 |
* @param mrb The mruby state reference. |
| 424 |
* @param cla The class pointer where the method will be defined. |
| 425 |
* @param name The name of the method being defined. |
| 426 |
* @param func The function pointer to the method definition. |
| 427 |
* @param aspec The method parameters declaration. |
| 428 |
*/ |
| 429 |
MRB_API void mrb_define_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t func, mrb_aspec aspec); |
| 430 |
MRB_API void mrb_define_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t func, mrb_aspec aspec); |
| 431 |
|
| 432 |
/** |
| 433 |
* Defines a class method. |
| 434 |
* |
| 435 |
* Example: |
| 436 |
* |
| 437 |
* # Ruby style |
| 438 |
* class Foo |
| 439 |
* def Foo.bar |
| 440 |
* end |
| 441 |
* end |
| 442 |
* // C style |
| 443 |
* mrb_value bar_method(mrb_state* mrb, mrb_value self){ |
| 444 |
* return mrb_nil_value(); |
| 445 |
* } |
| 446 |
* void mrb_example_gem_init(mrb_state* mrb){ |
| 447 |
* struct RClass *foo; |
| 448 |
* foo = mrb_define_class(mrb, "Foo", mrb->object_class); |
| 449 |
* mrb_define_class_method(mrb, foo, "bar", bar_method, MRB_ARGS_NONE()); |
| 450 |
* } |
| 451 |
* @param mrb The mruby state reference. |
| 452 |
* @param cla The class where the class method will be defined. |
| 453 |
* @param name The name of the class method being defined. |
| 454 |
* @param fun The function pointer to the class method definition. |
| 455 |
* @param aspec The method parameters declaration. |
| 456 |
*/ |
| 457 |
MRB_API void mrb_define_class_method(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec); |
| 458 |
MRB_API void mrb_define_class_method_id(mrb_state *mrb, struct RClass *cla, mrb_sym name, mrb_func_t fun, mrb_aspec aspec); |
| 459 |
|
| 460 |
/** |
| 461 |
* Defines a singleton method |
| 462 |
* |
| 463 |
* @see mrb_define_class_method |
| 464 |
*/ |
| 465 |
MRB_API void mrb_define_singleton_method(mrb_state *mrb, struct RObject *cla, const char *name, mrb_func_t fun, mrb_aspec aspec); |
| 466 |
MRB_API void mrb_define_singleton_method_id(mrb_state *mrb, struct RObject *cla, mrb_sym name, mrb_func_t fun, mrb_aspec aspec); |
| 467 |
|
| 468 |
/** |
| 469 |
* Defines a module function. |
| 470 |
* |
| 471 |
* Example: |
| 472 |
* |
| 473 |
* # Ruby style |
| 474 |
* module Foo |
| 475 |
* def Foo.bar |
| 476 |
* end |
| 477 |
* end |
| 478 |
* // C style |
| 479 |
* mrb_value bar_method(mrb_state* mrb, mrb_value self){ |
| 480 |
* return mrb_nil_value(); |
| 481 |
* } |
| 482 |
* void mrb_example_gem_init(mrb_state* mrb){ |
| 483 |
* struct RClass *foo; |
| 484 |
* foo = mrb_define_module(mrb, "Foo"); |
| 485 |
* mrb_define_module_function(mrb, foo, "bar", bar_method, MRB_ARGS_NONE()); |
| 486 |
* } |
| 487 |
* @param mrb The mruby state reference. |
| 488 |
* @param cla The module where the module function will be defined. |
| 489 |
* @param name The name of the module function being defined. |
| 490 |
* @param fun The function pointer to the module function definition. |
| 491 |
* @param aspec The method parameters declaration. |
| 492 |
*/ |
| 493 |
MRB_API void mrb_define_module_function(mrb_state *mrb, struct RClass *cla, const char *name, mrb_func_t fun, mrb_aspec aspec); |
| 494 |
MRB_API void mrb_define_module_function_id(mrb_state *mrb, struct RClass *cla, mrb_sym name, mrb_func_t fun, mrb_aspec aspec); |
| 495 |
|
| 496 |
/** |
| 497 |
* Defines a constant. |
| 498 |
* |
| 499 |
* Example: |
| 500 |
* |
| 501 |
* # Ruby style |
| 502 |
* class ExampleClass |
| 503 |
* AGE = 22 |
| 504 |
* end |
| 505 |
* // C style |
| 506 |
* #include <stdio.h> |
| 507 |
* #include <mruby.h> |
| 508 |
* |
| 509 |
* void |
| 510 |
* mrb_example_gem_init(mrb_state* mrb){ |
| 511 |
* mrb_define_const(mrb, mrb->kernel_module, "AGE", mrb_fixnum_value(22)); |
| 512 |
* } |
| 513 |
* |
| 514 |
* mrb_value |
| 515 |
* mrb_example_gem_final(mrb_state* mrb){ |
| 516 |
* } |
| 517 |
* @param mrb The mruby state reference. |
| 518 |
* @param cla A class or module the constant is defined in. |
| 519 |
* @param name The name of the constant being defined. |
| 520 |
* @param val The value for the constant. |
| 521 |
*/ |
| 522 |
MRB_API void mrb_define_const(mrb_state* mrb, struct RClass* cla, const char *name, mrb_value val); |
| 523 |
MRB_API void mrb_define_const_id(mrb_state* mrb, struct RClass* cla, mrb_sym name, mrb_value val); |
| 524 |
|
| 525 |
/** |
| 526 |
* Undefines a method. |
| 527 |
* |
| 528 |
* Example: |
| 529 |
* |
| 530 |
* # Ruby style |
| 531 |
* |
| 532 |
* class ExampleClassA |
| 533 |
* def example_method |
| 534 |
* "example" |
| 535 |
* end |
| 536 |
* end |
| 537 |
* ExampleClassA.new.example_method # => example |
| 538 |
* |
| 539 |
* class ExampleClassB < ExampleClassA |
| 540 |
* undef_method :example_method |
| 541 |
* end |
| 542 |
* |
| 543 |
* ExampleClassB.new.example_method # => undefined method 'example_method' for ExampleClassB (NoMethodError) |
| 544 |
* |
| 545 |
* // C style |
| 546 |
* #include <stdio.h> |
| 547 |
* #include <mruby.h> |
| 548 |
* |
| 549 |
* mrb_value |
| 550 |
* mrb_example_method(mrb_state *mrb){ |
| 551 |
* return mrb_str_new_lit(mrb, "example"); |
| 552 |
* } |
| 553 |
* |
| 554 |
* void |
| 555 |
* mrb_example_gem_init(mrb_state* mrb){ |
| 556 |
* struct RClass *example_class_a; |
| 557 |
* struct RClass *example_class_b; |
| 558 |
* struct RClass *example_class_c; |
| 559 |
* |
| 560 |
* example_class_a = mrb_define_class(mrb, "ExampleClassA", mrb->object_class); |
| 561 |
* mrb_define_method(mrb, example_class_a, "example_method", mrb_example_method, MRB_ARGS_NONE()); |
| 562 |
* example_class_b = mrb_define_class(mrb, "ExampleClassB", example_class_a); |
| 563 |
* example_class_c = mrb_define_class(mrb, "ExampleClassC", example_class_b); |
| 564 |
* mrb_undef_method(mrb, example_class_c, "example_method"); |
| 565 |
* } |
| 566 |
* |
| 567 |
* mrb_example_gem_final(mrb_state* mrb){ |
| 568 |
* } |
| 569 |
* @param mrb The mruby state reference. |
| 570 |
* @param cla The class the method will be undefined from. |
| 571 |
* @param name The name of the method to be undefined. |
| 572 |
*/ |
| 573 |
MRB_API void mrb_undef_method(mrb_state *mrb, struct RClass *cla, const char *name); |
| 574 |
MRB_API void mrb_undef_method_id(mrb_state*, struct RClass*, mrb_sym); |
| 575 |
|
| 576 |
/** |
| 577 |
* Undefine a class method. |
| 578 |
* Example: |
| 579 |
* |
| 580 |
* # Ruby style |
| 581 |
* class ExampleClass |
| 582 |
* def self.example_method |
| 583 |
* "example" |
| 584 |
* end |
| 585 |
* end |
| 586 |
* |
| 587 |
* ExampleClass.example_method |
| 588 |
* |
| 589 |
* // C style |
| 590 |
* #include <stdio.h> |
| 591 |
* #include <mruby.h> |
| 592 |
* |
| 593 |
* mrb_value |
| 594 |
* mrb_example_method(mrb_state *mrb){ |
| 595 |
* return mrb_str_new_lit(mrb, "example"); |
| 596 |
* } |
| 597 |
* |
| 598 |
* void |
| 599 |
* mrb_example_gem_init(mrb_state* mrb){ |
| 600 |
* struct RClass *example_class; |
| 601 |
* example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class); |
| 602 |
* mrb_define_class_method(mrb, example_class, "example_method", mrb_example_method, MRB_ARGS_NONE()); |
| 603 |
* mrb_undef_class_method(mrb, example_class, "example_method"); |
| 604 |
* } |
| 605 |
* |
| 606 |
* void |
| 607 |
* mrb_example_gem_final(mrb_state* mrb){ |
| 608 |
* } |
| 609 |
* @param mrb The mruby state reference. |
| 610 |
* @param cls A class the class method will be undefined from. |
| 611 |
* @param name The name of the class method to be undefined. |
| 612 |
*/ |
| 613 |
MRB_API void mrb_undef_class_method(mrb_state *mrb, struct RClass *cls, const char *name); |
| 614 |
MRB_API void mrb_undef_class_method_id(mrb_state *mrb, struct RClass *cls, mrb_sym name); |
| 615 |
|
| 616 |
/** |
| 617 |
* Initialize a new object instance of c class. |
| 618 |
* |
| 619 |
* Example: |
| 620 |
* |
| 621 |
* # Ruby style |
| 622 |
* class ExampleClass |
| 623 |
* end |
| 624 |
* |
| 625 |
* p ExampleClass # => #<ExampleClass:0x9958588> |
| 626 |
* // C style |
| 627 |
* #include <stdio.h> |
| 628 |
* #include <mruby.h> |
| 629 |
* |
| 630 |
* void |
| 631 |
* mrb_example_gem_init(mrb_state* mrb) { |
| 632 |
* struct RClass *example_class; |
| 633 |
* mrb_value obj; |
| 634 |
* example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class); # => class ExampleClass; end |
| 635 |
* obj = mrb_obj_new(mrb, example_class, 0, NULL); # => ExampleClass.new |
| 636 |
* mrb_p(mrb, obj); // => Kernel#p |
| 637 |
* } |
| 638 |
* @param mrb The current mruby state. |
| 639 |
* @param c Reference to the class of the new object. |
| 640 |
* @param argc Number of arguments in argv |
| 641 |
* @param argv Array of mrb_value to initialize the object |
| 642 |
* @return [mrb_value] The newly initialized object |
| 643 |
*/ |
| 644 |
MRB_API mrb_value mrb_obj_new(mrb_state *mrb, struct RClass *c, mrb_int argc, const mrb_value *argv); |
| 645 |
|
| 646 |
/** @see mrb_obj_new */ |
| 647 |
MRB_INLINE mrb_value mrb_class_new_instance(mrb_state *mrb, mrb_int argc, const mrb_value *argv, struct RClass *c) |
| 648 |
{ |
| 649 |
return mrb_obj_new(mrb,c,argc,argv); |
| 650 |
} |
| 651 |
|
| 652 |
/** |
| 653 |
* Creates a new instance of Class, Class. |
| 654 |
* |
| 655 |
* Example: |
| 656 |
* |
| 657 |
* void |
| 658 |
* mrb_example_gem_init(mrb_state* mrb) { |
| 659 |
* struct RClass *example_class; |
| 660 |
* |
| 661 |
* mrb_value obj; |
| 662 |
* example_class = mrb_class_new(mrb, mrb->object_class); |
| 663 |
* obj = mrb_obj_new(mrb, example_class, 0, NULL); // => #<#<Class:0x9a945b8>:0x9a94588> |
| 664 |
* mrb_p(mrb, obj); // => Kernel#p |
| 665 |
* } |
| 666 |
* |
| 667 |
* @param mrb The current mruby state. |
| 668 |
* @param super The super class or parent. |
| 669 |
* @return [struct RClass *] Reference to the new class. |
| 670 |
*/ |
| 671 |
MRB_API struct RClass * mrb_class_new(mrb_state *mrb, struct RClass *super); |
| 672 |
|
| 673 |
/** |
| 674 |
* Creates a new module, Module. |
| 675 |
* |
| 676 |
* Example: |
| 677 |
* void |
| 678 |
* mrb_example_gem_init(mrb_state* mrb) { |
| 679 |
* struct RClass *example_module; |
| 680 |
* |
| 681 |
* example_module = mrb_module_new(mrb); |
| 682 |
* } |
| 683 |
* |
| 684 |
* @param mrb The current mruby state. |
| 685 |
* @return [struct RClass *] Reference to the new module. |
| 686 |
*/ |
| 687 |
MRB_API struct RClass * mrb_module_new(mrb_state *mrb); |
| 688 |
|
| 689 |
/** |
| 690 |
* Returns an mrb_bool. True if class was defined, and false if the class was not defined. |
| 691 |
* |
| 692 |
* Example: |
| 693 |
* void |
| 694 |
* mrb_example_gem_init(mrb_state* mrb) { |
| 695 |
* struct RClass *example_class; |
| 696 |
* mrb_bool cd; |
| 697 |
* |
| 698 |
* example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class); |
| 699 |
* cd = mrb_class_defined(mrb, "ExampleClass"); |
| 700 |
* |
| 701 |
* // If mrb_class_defined returns TRUE then puts "True" |
| 702 |
* // If mrb_class_defined returns FALSE then puts "False" |
| 703 |
* if (cd) { |
| 704 |
* puts("True"); |
| 705 |
* } |
| 706 |
* else { |
| 707 |
* puts("False"); |
| 708 |
* } |
| 709 |
* } |
| 710 |
* |
| 711 |
* @param mrb The current mruby state. |
| 712 |
* @param name A string representing the name of the class. |
| 713 |
* @return [mrb_bool] A boolean value. |
| 714 |
*/ |
| 715 |
MRB_API mrb_bool mrb_class_defined(mrb_state *mrb, const char *name); |
| 716 |
MRB_API mrb_bool mrb_class_defined_id(mrb_state *mrb, mrb_sym name); |
| 717 |
|
| 718 |
/** |
| 719 |
* Gets a class. |
| 720 |
* @param mrb The current mruby state. |
| 721 |
* @param name The name of the class. |
| 722 |
* @return [struct RClass *] A reference to the class. |
| 723 |
*/ |
| 724 |
MRB_API struct RClass* mrb_class_get(mrb_state *mrb, const char *name); |
| 725 |
MRB_API struct RClass* mrb_class_get_id(mrb_state *mrb, mrb_sym name); |
| 726 |
|
| 727 |
/** |
| 728 |
* Gets a exception class. |
| 729 |
* @param mrb The current mruby state. |
| 730 |
* @param name The name of the class. |
| 731 |
* @return [struct RClass *] A reference to the class. |
| 732 |
*/ |
| 733 |
MRB_API struct RClass* mrb_exc_get_id(mrb_state *mrb, mrb_sym name); |
| 734 |
#define mrb_exc_get(mrb, name) mrb_exc_get_id(mrb, mrb_intern_cstr(mrb, name)) |
| 735 |
|
| 736 |
/** |
| 737 |
* Returns an mrb_bool. True if inner class was defined, and false if the inner class was not defined. |
| 738 |
* |
| 739 |
* Example: |
| 740 |
* void |
| 741 |
* mrb_example_gem_init(mrb_state* mrb) { |
| 742 |
* struct RClass *example_outer, *example_inner; |
| 743 |
* mrb_bool cd; |
| 744 |
* |
| 745 |
* example_outer = mrb_define_module(mrb, "ExampleOuter"); |
| 746 |
* |
| 747 |
* example_inner = mrb_define_class_under(mrb, example_outer, "ExampleInner", mrb->object_class); |
| 748 |
* cd = mrb_class_defined_under(mrb, example_outer, "ExampleInner"); |
| 749 |
* |
| 750 |
* // If mrb_class_defined_under returns TRUE then puts "True" |
| 751 |
* // If mrb_class_defined_under returns FALSE then puts "False" |
| 752 |
* if (cd) { |
| 753 |
* puts("True"); |
| 754 |
* } |
| 755 |
* else { |
| 756 |
* puts("False"); |
| 757 |
* } |
| 758 |
* } |
| 759 |
* |
| 760 |
* @param mrb The current mruby state. |
| 761 |
* @param outer The name of the outer class. |
| 762 |
* @param name A string representing the name of the inner class. |
| 763 |
* @return [mrb_bool] A boolean value. |
| 764 |
*/ |
| 765 |
MRB_API mrb_bool mrb_class_defined_under(mrb_state *mrb, struct RClass *outer, const char *name); |
| 766 |
MRB_API mrb_bool mrb_class_defined_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name); |
| 767 |
|
| 768 |
/** |
| 769 |
* Gets a child class. |
| 770 |
* @param mrb The current mruby state. |
| 771 |
* @param outer The name of the parent class. |
| 772 |
* @param name The name of the class. |
| 773 |
* @return [struct RClass *] A reference to the class. |
| 774 |
*/ |
| 775 |
MRB_API struct RClass * mrb_class_get_under(mrb_state *mrb, struct RClass *outer, const char *name); |
| 776 |
MRB_API struct RClass * mrb_class_get_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name); |
| 777 |
|
| 778 |
/** |
| 779 |
* Gets a module. |
| 780 |
* @param mrb The current mruby state. |
| 781 |
* @param name The name of the module. |
| 782 |
* @return [struct RClass *] A reference to the module. |
| 783 |
*/ |
| 784 |
MRB_API struct RClass * mrb_module_get(mrb_state *mrb, const char *name); |
| 785 |
MRB_API struct RClass * mrb_module_get_id(mrb_state *mrb, mrb_sym name); |
| 786 |
|
| 787 |
/** |
| 788 |
* Gets a module defined under another module. |
| 789 |
* @param mrb The current mruby state. |
| 790 |
* @param outer The name of the outer module. |
| 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_under(mrb_state *mrb, struct RClass *outer, const char *name); |
| 795 |
MRB_API struct RClass * mrb_module_get_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name); |
| 796 |
|
| 797 |
/* a function to raise NotImplementedError with current method name */ |
| 798 |
MRB_API void mrb_notimplement(mrb_state*); |
| 799 |
/* a function to be replacement of unimplemented method */ |
| 800 |
MRB_API mrb_value mrb_notimplement_m(mrb_state*, mrb_value); |
| 801 |
|
| 802 |
/** |
| 803 |
* Duplicate an object. |
| 804 |
* |
| 805 |
* Equivalent to: |
| 806 |
* Object#dup |
| 807 |
* @param mrb The current mruby state. |
| 808 |
* @param obj Object to be duplicate. |
| 809 |
* @return [mrb_value] The newly duplicated object. |
| 810 |
*/ |
| 811 |
MRB_API mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj); |
| 812 |
|
| 813 |
/** |
| 814 |
* Returns true if obj responds to the given method. If the method was defined for that |
| 815 |
* class it returns true, it returns false otherwise. |
| 816 |
* |
| 817 |
* Example: |
| 818 |
* # Ruby style |
| 819 |
* class ExampleClass |
| 820 |
* def example_method |
| 821 |
* end |
| 822 |
* end |
| 823 |
* |
| 824 |
* ExampleClass.new.respond_to?(:example_method) # => true |
| 825 |
* |
| 826 |
* // C style |
| 827 |
* void |
| 828 |
* mrb_example_gem_init(mrb_state* mrb) { |
| 829 |
* struct RClass *example_class; |
| 830 |
* mrb_sym mid; |
| 831 |
* mrb_bool obj_resp; |
| 832 |
* |
| 833 |
* example_class = mrb_define_class(mrb, "ExampleClass", mrb->object_class); |
| 834 |
* mrb_define_method(mrb, example_class, "example_method", exampleMethod, MRB_ARGS_NONE()); |
| 835 |
* mid = mrb_intern_str(mrb, mrb_str_new_lit(mrb, "example_method" )); |
| 836 |
* obj_resp = mrb_obj_respond_to(mrb, example_class, mid); // => TRUE (true in Ruby world) |
| 837 |
* |
| 838 |
* // If mrb_obj_respond_to returns TRUE then puts "True" |
| 839 |
* // If mrb_obj_respond_to returns FALSE then puts "False" |
| 840 |
* if (obj_resp) { |
| 841 |
* puts("True"); |
| 842 |
* } |
| 843 |
* else { |
| 844 |
* puts("False"); |
| 845 |
* } |
| 846 |
* } |
| 847 |
* |
| 848 |
* @param mrb The current mruby state. |
| 849 |
* @param c A reference to a class. |
| 850 |
* @param mid A symbol referencing a method id. |
| 851 |
* @return [mrb_bool] A boolean value. |
| 852 |
*/ |
| 853 |
MRB_API mrb_bool mrb_obj_respond_to(mrb_state *mrb, struct RClass* c, mrb_sym mid); |
| 854 |
|
| 855 |
/** |
| 856 |
* Defines a new class under a given module |
| 857 |
* |
| 858 |
* @param mrb The current mruby state. |
| 859 |
* @param outer Reference to the module under which the new class will be defined |
| 860 |
* @param name The name of the defined class |
| 861 |
* @param super The new class parent |
| 862 |
* @return [struct RClass *] Reference to the newly defined class |
| 863 |
* @see mrb_define_class |
| 864 |
*/ |
| 865 |
MRB_API struct RClass* mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super); |
| 866 |
MRB_API struct RClass* mrb_define_class_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name, struct RClass *super); |
| 867 |
|
| 868 |
MRB_API struct RClass* mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name); |
| 869 |
MRB_API struct RClass* mrb_define_module_under_id(mrb_state *mrb, struct RClass *outer, mrb_sym name); |
| 870 |
|
| 871 |
/** |
| 872 |
* Function requires n arguments. |
| 873 |
* |
| 874 |
* @param n |
| 875 |
* The number of required arguments. |
| 876 |
*/ |
| 877 |
#define MRB_ARGS_REQ(n) ((mrb_aspec)((n)&0x1f) << 18) |
| 878 |
|
| 879 |
/** |
| 880 |
* Function takes n optional arguments |
| 881 |
* |
| 882 |
* @param n |
| 883 |
* The number of optional arguments. |
| 884 |
*/ |
| 885 |
#define MRB_ARGS_OPT(n) ((mrb_aspec)((n)&0x1f) << 13) |
| 886 |
|
| 887 |
/** |
| 888 |
* Function takes n1 mandatory arguments and n2 optional arguments |
| 889 |
* |
| 890 |
* @param n1 |
| 891 |
* The number of required arguments. |
| 892 |
* @param n2 |
| 893 |
* The number of optional arguments. |
| 894 |
*/ |
| 895 |
#define MRB_ARGS_ARG(n1,n2) (MRB_ARGS_REQ(n1)|MRB_ARGS_OPT(n2)) |
| 896 |
|
| 897 |
/** rest argument */ |
| 898 |
#define MRB_ARGS_REST() ((mrb_aspec)(1 << 12)) |
| 899 |
|
| 900 |
/** required arguments after rest */ |
| 901 |
#define MRB_ARGS_POST(n) ((mrb_aspec)((n)&0x1f) << 7) |
| 902 |
|
| 903 |
/** keyword arguments (n of keys, kdict) */ |
| 904 |
#define MRB_ARGS_KEY(n1,n2) ((mrb_aspec)((((n1)&0x1f) << 2) | ((n2)?(1<<1):0))) |
| 905 |
|
| 906 |
/** |
| 907 |
* Function takes a block argument |
| 908 |
*/ |
| 909 |
#define MRB_ARGS_BLOCK() ((mrb_aspec)1) |
| 910 |
|
| 911 |
/** |
| 912 |
* Function accepts any number of arguments |
| 913 |
*/ |
| 914 |
#define MRB_ARGS_ANY() MRB_ARGS_REST() |
| 915 |
|
| 916 |
/** |
| 917 |
* Function accepts no arguments |
| 918 |
*/ |
| 919 |
#define MRB_ARGS_NONE() ((mrb_aspec)0) |
| 920 |
|
| 921 |
/** |
| 922 |
* Format specifiers for {mrb_get_args} function |
| 923 |
* |
| 924 |
* Must be a C string composed of the following format specifiers: |
| 925 |
* |
| 926 |
* | char | Ruby type | C types | Notes | |
| 927 |
* |:----:|----------------|-------------------|----------------------------------------------------| |
| 928 |
* | `o` | {Object} | {mrb_value} | Could be used to retrieve any type of argument | |
| 929 |
* | `C` | {Class}/{Module} | {mrb_value} | when `!` follows, the value may be `nil` | |
| 930 |
* | `S` | {String} | {mrb_value} | when `!` follows, the value may be `nil` | |
| 931 |
* | `A` | {Array} | {mrb_value} | when `!` follows, the value may be `nil` | |
| 932 |
* | `H` | {Hash} | {mrb_value} | when `!` follows, the value may be `nil` | |
| 933 |
* | `s` | {String} | const char *, {mrb_int} | Receive two arguments; `s!` gives (`NULL`,`0`) for `nil` | |
| 934 |
* | `z` | {String} | const char * | `NULL` terminated string; `z!` gives `NULL` for `nil` | |
| 935 |
* | `a` | {Array} | const {mrb_value} *, {mrb_int} | Receive two arguments; `a!` gives (`NULL`,`0`) for `nil` | |
| 936 |
* | `c` | {Class}/{Module} | strcut RClass * | `c!` gives `NULL` for `nil` | |
| 937 |
* | `f` | {Integer}/{Float} | {mrb_float} | | |
| 938 |
* | `i` | {Integer}/{Float} | {mrb_int} | | |
| 939 |
* | `b` | boolean | {mrb_bool} | | |
| 940 |
* | `n` | {String}/{Symbol} | {mrb_sym} | | |
| 941 |
* | `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` | |
| 942 |
* | `I` | inline struct | void *, struct RClass | `I!` gives `NULL` for `nil` | |
| 943 |
* | `&` | block | {mrb_value} | &! raises exception if no block given. | |
| 944 |
* | `*` | rest arguments | const {mrb_value} *, {mrb_int} | Receive the rest of arguments as an array; `*!` avoid copy of the stack. | |
| 945 |
* | <code>\|</code> | optional | | After this spec following specs would be optional. | |
| 946 |
* | `?` | optional given | {mrb_bool} | `TRUE` if preceding argument is given. Used to check optional argument is given. | |
| 947 |
* | `:` | keyword args | {mrb_kwargs} const | Get keyword arguments. @see mrb_kwargs | |
| 948 |
* |
| 949 |
* @see mrb_get_args |
| 950 |
* |
| 951 |
* Immediately after format specifiers it can add format modifiers: |
| 952 |
* |
| 953 |
* | char | Notes | |
| 954 |
* |:----:|-----------------------------------------------------------------------------------------| |
| 955 |
* | `!` | Switch to the alternate mode; The behaviour changes depending on the format specifier | |
| 956 |
* | `+` | Request a not frozen object; However, except nil value | |
| 957 |
*/ |
| 958 |
typedef const char *mrb_args_format; |
| 959 |
|
| 960 |
/** |
| 961 |
* Get keyword arguments by `mrb_get_args()` with `:` specifier. |
| 962 |
* |
| 963 |
* `mrb_kwargs::num` indicates that the total number of keyword values. |
| 964 |
* |
| 965 |
* `mrb_kwargs::required` indicates that the specified number of keywords starting from the beginning of the `mrb_sym` array are required. |
| 966 |
* |
| 967 |
* `mrb_kwargs::table` accepts a `mrb_sym` array of C. |
| 968 |
* |
| 969 |
* `mrb_kwargs::values` is an object array of C, and the keyword argument corresponding to the `mrb_sym` array is assigned. |
| 970 |
* Note that `undef` is assigned if there is no keyword argument corresponding over `mrb_kwargs::required` to `mrb_kwargs::num`. |
| 971 |
* |
| 972 |
* `mrb_kwargs::rest` is the remaining keyword argument that can be accepted as `**rest` in Ruby. |
| 973 |
* If `NULL` is specified, `ArgumentError` is raised when there is an undefined keyword. |
| 974 |
* |
| 975 |
* Examples: |
| 976 |
* |
| 977 |
* // def method(a: 1, b: 2) |
| 978 |
* |
| 979 |
* mrb_int kw_num = 2; |
| 980 |
* mrb_int kw_required = 0; |
| 981 |
* mrb_sym kw_names[] = { mrb_intern_lit(mrb, "a"), mrb_intern_lit(mrb, "b") }; |
| 982 |
* mrb_value kw_values[kw_num]; |
| 983 |
* mrb_kwargs kwargs = { kw_num, kw_required, kw_names, kw_values, NULL }; |
| 984 |
* |
| 985 |
* mrb_get_args(mrb, ":", &kwargs); |
| 986 |
* if (mrb_undef_p(kw_values[0])) { kw_values[0] = mrb_fixnum_value(1); } |
| 987 |
* if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); } |
| 988 |
* |
| 989 |
* |
| 990 |
* // def method(str, x:, y: 2, z: "default string", **opts) |
| 991 |
* |
| 992 |
* mrb_value str, kw_rest; |
| 993 |
* uint32_t kw_num = 3; |
| 994 |
* uint32_t kw_required = 1; |
| 995 |
* // Note that `#include <mruby/presym.h>` is required beforehand because `MRB_SYM()` is used. |
| 996 |
* // If the usage of `MRB_SYM()` is not desired, replace it with `mrb_intern_lit()`. |
| 997 |
* mrb_sym kw_names[] = { MRB_SYM(x), MRB_SYM(y), MRB_SYM(z) }; |
| 998 |
* mrb_value kw_values[kw_num]; |
| 999 |
* mrb_kwargs kwargs = { kw_num, kw_required, kw_names, kw_values, &kw_rest }; |
| 1000 |
* |
| 1001 |
* mrb_get_args(mrb, "S:", &str, &kwargs); |
| 1002 |
* // or: mrb_get_args(mrb, ":S", &kwargs, &str); |
| 1003 |
* if (mrb_undef_p(kw_values[1])) { kw_values[1] = mrb_fixnum_value(2); } |
| 1004 |
* if (mrb_undef_p(kw_values[2])) { kw_values[2] = mrb_str_new_cstr(mrb, "default string"); } |
| 1005 |
*/ |
| 1006 |
typedef struct mrb_kwargs mrb_kwargs; |
| 1007 |
|
| 1008 |
struct mrb_kwargs |
| 1009 |
{ |
| 1010 |
mrb_int num; /* number of keyword arguments */ |
| 1011 |
mrb_int required; /* number of required keyword arguments */ |
| 1012 |
const mrb_sym *table; /* C array of symbols for keyword names */ |
| 1013 |
mrb_value *values; /* keyword argument values */ |
| 1014 |
mrb_value *rest; /* keyword rest (dict) */ |
| 1015 |
}; |
| 1016 |
|
| 1017 |
/** |
| 1018 |
* Retrieve arguments from mrb_state. |
| 1019 |
* |
| 1020 |
* @param mrb The current mruby state. |
| 1021 |
* @param format is a list of format specifiers |
| 1022 |
* @param ... The passing variadic arguments must be a pointer of retrieving type. |
| 1023 |
* @return the number of arguments retrieved. |
| 1024 |
* @see mrb_args_format |
| 1025 |
* @see mrb_kwargs |
| 1026 |
*/ |
| 1027 |
MRB_API mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format, ...); |
| 1028 |
|
| 1029 |
/** |
| 1030 |
* Array version of mrb_get_args() |
| 1031 |
* |
| 1032 |
* @param ptr Array of void*, in the same order as the varargs version. |
| 1033 |
*/ |
| 1034 |
MRB_API mrb_int mrb_get_args_a(mrb_state *mrb, mrb_args_format format, void** ptr); |
| 1035 |
|
| 1036 |
MRB_INLINE mrb_sym |
| 1037 |
mrb_get_mid(mrb_state *mrb) /* get method symbol */ |
| 1038 |
{ |
| 1039 |
return mrb->c->ci->mid; |
| 1040 |
} |
| 1041 |
|
| 1042 |
/** |
| 1043 |
* Retrieve number of arguments from mrb_state. |
| 1044 |
* |
| 1045 |
* Correctly handles *splat arguments. |
| 1046 |
*/ |
| 1047 |
MRB_API mrb_int mrb_get_argc(mrb_state *mrb); |
| 1048 |
|
| 1049 |
/** |
| 1050 |
* Retrieve an array of arguments from mrb_state. |
| 1051 |
* |
| 1052 |
* Correctly handles *splat arguments. |
| 1053 |
*/ |
| 1054 |
MRB_API const mrb_value *mrb_get_argv(mrb_state *mrb); |
| 1055 |
|
| 1056 |
/** |
| 1057 |
* Retrieve the first and only argument from mrb_state. |
| 1058 |
* Raises ArgumentError unless the number of arguments is exactly one. |
| 1059 |
* |
| 1060 |
* Correctly handles *splat arguments. |
| 1061 |
*/ |
| 1062 |
MRB_API mrb_value mrb_get_arg1(mrb_state *mrb); |
| 1063 |
|
| 1064 |
/** |
| 1065 |
* Check if a block argument is given from mrb_state. |
| 1066 |
*/ |
| 1067 |
MRB_API mrb_bool mrb_block_given_p(mrb_state *mrb); |
| 1068 |
|
| 1069 |
/* `strlen` for character string literals (use with caution or `strlen` instead) |
| 1070 |
Adjacent string literals are concatenated in C/C++ in translation phase 6. |
| 1071 |
If `lit` is not one, the compiler will report a syntax error: |
| 1072 |
MSVC: "error C2143: syntax error : missing ')' before 'string'" |
| 1073 |
GCC: "error: expected ')' before string constant" |
| 1074 |
*/ |
| 1075 |
#define mrb_strlen_lit(lit) (sizeof(lit "") - 1) |
| 1076 |
|
| 1077 |
/** |
| 1078 |
* Call existing ruby functions. |
| 1079 |
* |
| 1080 |
* Example: |
| 1081 |
* |
| 1082 |
* #include <stdio.h> |
| 1083 |
* #include <mruby.h> |
| 1084 |
* #include "mruby/compile.h" |
| 1085 |
* |
| 1086 |
* int |
| 1087 |
* main() |
| 1088 |
* { |
| 1089 |
* mrb_int i = 99; |
| 1090 |
* mrb_state *mrb = mrb_open(); |
| 1091 |
* |
| 1092 |
* if (!mrb) { } |
| 1093 |
* FILE *fp = fopen("test.rb","r"); |
| 1094 |
* mrb_value obj = mrb_load_file(mrb,fp); |
| 1095 |
* mrb_funcall(mrb, obj, "method_name", 1, mrb_fixnum_value(i)); |
| 1096 |
* mrb_funcall_id(mrb, obj, MRB_SYM(method_name), 1, mrb_fixnum_value(i)); |
| 1097 |
* fclose(fp); |
| 1098 |
* mrb_close(mrb); |
| 1099 |
* } |
| 1100 |
* |
| 1101 |
* @param mrb The current mruby state. |
| 1102 |
* @param val A reference to an mruby value. |
| 1103 |
* @param name The name of the method. |
| 1104 |
* @param argc The number of arguments the method has. |
| 1105 |
* @param ... Variadic values(not type safe!). |
| 1106 |
* @return [mrb_value] mruby function value. |
| 1107 |
*/ |
| 1108 |
MRB_API mrb_value mrb_funcall(mrb_state *mrb, mrb_value val, const char *name, mrb_int argc, ...); |
| 1109 |
MRB_API mrb_value mrb_funcall_id(mrb_state *mrb, mrb_value val, mrb_sym mid, mrb_int argc, ...); |
| 1110 |
/** |
| 1111 |
* Call existing ruby functions. This is basically the type safe version of mrb_funcall. |
| 1112 |
* |
| 1113 |
* #include <stdio.h> |
| 1114 |
* #include <mruby.h> |
| 1115 |
* #include "mruby/compile.h" |
| 1116 |
* int |
| 1117 |
* main() |
| 1118 |
* { |
| 1119 |
* mrb_state *mrb = mrb_open(); |
| 1120 |
* mrb_value obj = mrb_fixnum_value(1); |
| 1121 |
* |
| 1122 |
* if (!mrb) { } |
| 1123 |
* |
| 1124 |
* FILE *fp = fopen("test.rb","r"); |
| 1125 |
* mrb_value obj = mrb_load_file(mrb,fp); |
| 1126 |
* mrb_funcall_argv(mrb, obj, MRB_SYM(method_name), 1, &obj); // Calling ruby function from test.rb. |
| 1127 |
* fclose(fp); |
| 1128 |
* mrb_close(mrb); |
| 1129 |
* } |
| 1130 |
* @param mrb The current mruby state. |
| 1131 |
* @param val A reference to an mruby value. |
| 1132 |
* @param name_sym The symbol representing the method. |
| 1133 |
* @param argc The number of arguments the method has. |
| 1134 |
* @param obj Pointer to the object. |
| 1135 |
* @return [mrb_value] mrb_value mruby function value. |
| 1136 |
* @see mrb_funcall |
| 1137 |
*/ |
| 1138 |
MRB_API mrb_value mrb_funcall_argv(mrb_state *mrb, mrb_value val, mrb_sym name, mrb_int argc, const mrb_value *argv); |
| 1139 |
/** |
| 1140 |
* Call existing ruby functions with a block. |
| 1141 |
*/ |
| 1142 |
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); |
| 1143 |
/** |
| 1144 |
* Create a symbol from C string. But usually it's better to use MRB_SYM, |
| 1145 |
* MRB_OPSYM, MRB_CVSYM, MRB_IVSYM, MRB_SYM_B, MRB_SYM_Q, MRB_SYM_E macros. |
| 1146 |
* |
| 1147 |
* Example: |
| 1148 |
* |
| 1149 |
* # Ruby style: |
| 1150 |
* :pizza # => :pizza |
| 1151 |
* |
| 1152 |
* // C style: |
| 1153 |
* mrb_sym sym1 = mrb_intern_lit(mrb, "pizza"); // => :pizza |
| 1154 |
* mrb_sym sym2 = MRB_SYM(pizza); // => :pizza |
| 1155 |
* mrb_sym sym3 = MRB_SYM_Q(pizza); // => :pizza? |
| 1156 |
* |
| 1157 |
* @param mrb The current mruby state. |
| 1158 |
* @param str The string to be symbolized |
| 1159 |
* @return [mrb_sym] mrb_sym A symbol. |
| 1160 |
*/ |
| 1161 |
MRB_API mrb_sym mrb_intern_cstr(mrb_state *mrb, const char* str); |
| 1162 |
MRB_API mrb_sym mrb_intern(mrb_state*,const char*,size_t); |
| 1163 |
MRB_API mrb_sym mrb_intern_static(mrb_state*,const char*,size_t); |
| 1164 |
#define mrb_intern_lit(mrb, lit) mrb_intern_static(mrb, (lit ""), mrb_strlen_lit(lit)) |
| 1165 |
MRB_API mrb_sym mrb_intern_str(mrb_state*,mrb_value); |
| 1166 |
/* mrb_intern_check series functions returns 0 if the symbol is not defined */ |
| 1167 |
MRB_API mrb_sym mrb_intern_check_cstr(mrb_state*,const char*); |
| 1168 |
MRB_API mrb_sym mrb_intern_check(mrb_state*,const char*,size_t); |
| 1169 |
MRB_API mrb_sym mrb_intern_check_str(mrb_state*,mrb_value); |
| 1170 |
/* mrb_check_intern series functions returns nil if the symbol is not defined */ |
| 1171 |
/* otherwise returns mrb_value */ |
| 1172 |
MRB_API mrb_value mrb_check_intern_cstr(mrb_state*,const char*); |
| 1173 |
MRB_API mrb_value mrb_check_intern(mrb_state*,const char*,size_t); |
| 1174 |
MRB_API mrb_value mrb_check_intern_str(mrb_state*,mrb_value); |
| 1175 |
MRB_API const char *mrb_sym_name(mrb_state*,mrb_sym); |
| 1176 |
MRB_API const char *mrb_sym_name_len(mrb_state*,mrb_sym,mrb_int*); |
| 1177 |
MRB_API const char *mrb_sym_dump(mrb_state*,mrb_sym); |
| 1178 |
MRB_API mrb_value mrb_sym_str(mrb_state*,mrb_sym); |
| 1179 |
#define mrb_sym2name(mrb,sym) mrb_sym_name(mrb,sym) |
| 1180 |
#define mrb_sym2name_len(mrb,sym,len) mrb_sym_name_len(mrb,sym,len) |
| 1181 |
#define mrb_sym2str(mrb,sym) mrb_sym_str(mrb,sym) |
| 1182 |
|
| 1183 |
MRB_API void *mrb_malloc(mrb_state*, size_t); /* raise RuntimeError if no mem */ |
| 1184 |
MRB_API void *mrb_calloc(mrb_state*, size_t, size_t); /* ditto */ |
| 1185 |
MRB_API void *mrb_realloc(mrb_state*, void*, size_t); /* ditto */ |
| 1186 |
MRB_API void *mrb_realloc_simple(mrb_state*, void*, size_t); /* return NULL if no memory available */ |
| 1187 |
MRB_API void *mrb_malloc_simple(mrb_state*, size_t); /* return NULL if no memory available */ |
| 1188 |
MRB_API struct RBasic *mrb_obj_alloc(mrb_state*, enum mrb_vtype, struct RClass*); |
| 1189 |
MRB_API void mrb_free(mrb_state*, void*); |
| 1190 |
|
| 1191 |
/** |
| 1192 |
* Allocates a Ruby object that matches the constant literal defined in |
| 1193 |
* `enum mrb_vtype` and returns a pointer to the corresponding C type. |
| 1194 |
* |
| 1195 |
* @param mrb The current mruby state |
| 1196 |
* @param tt The constant literal of `enum mrb_vtype` |
| 1197 |
* @param klass A Class object |
| 1198 |
* @return Reference to the newly created object |
| 1199 |
*/ |
| 1200 |
#define MRB_OBJ_ALLOC(mrb, tt, klass) ((MRB_VTYPE_TYPEOF(tt)*)mrb_obj_alloc(mrb, tt, klass)) |
| 1201 |
|
| 1202 |
MRB_API mrb_value mrb_str_new(mrb_state *mrb, const char *p, mrb_int len); |
| 1203 |
|
| 1204 |
/** |
| 1205 |
* Turns a C string into a Ruby string value. |
| 1206 |
*/ |
| 1207 |
MRB_API mrb_value mrb_str_new_cstr(mrb_state*, const char*); |
| 1208 |
MRB_API mrb_value mrb_str_new_static(mrb_state *mrb, const char *p, mrb_int len); |
| 1209 |
#define mrb_str_new_lit(mrb, lit) mrb_str_new_static(mrb, (lit), mrb_strlen_lit(lit)) |
| 1210 |
|
| 1211 |
MRB_API mrb_value mrb_obj_freeze(mrb_state*, mrb_value); |
| 1212 |
#define mrb_str_new_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new(mrb,p,len)) |
| 1213 |
#define mrb_str_new_cstr_frozen(mrb,p) mrb_obj_freeze(mrb,mrb_str_new_cstr(mrb,p)) |
| 1214 |
#define mrb_str_new_static_frozen(mrb,p,len) mrb_obj_freeze(mrb,mrb_str_new_static(mrb,p,len)) |
| 1215 |
#define mrb_str_new_lit_frozen(mrb,lit) mrb_obj_freeze(mrb,mrb_str_new_lit(mrb,lit)) |
| 1216 |
|
| 1217 |
#ifdef _WIN32 |
| 1218 |
MRB_API char* mrb_utf8_from_locale(const char *p, int len); |
| 1219 |
MRB_API char* mrb_locale_from_utf8(const char *p, int len); |
| 1220 |
#define mrb_locale_free(p) free(p) |
| 1221 |
#define mrb_utf8_free(p) free(p) |
| 1222 |
#else |
| 1223 |
#define mrb_utf8_from_locale(p, l) ((char*)(p)) |
| 1224 |
#define mrb_locale_from_utf8(p, l) ((char*)(p)) |
| 1225 |
#define mrb_locale_free(p) |
| 1226 |
#define mrb_utf8_free(p) |
| 1227 |
#endif |
| 1228 |
|
| 1229 |
/** |
| 1230 |
* Creates new mrb_state. |
| 1231 |
* |
| 1232 |
* @return |
| 1233 |
* Pointer to the newly created mrb_state. |
| 1234 |
*/ |
| 1235 |
MRB_API mrb_state* mrb_open(void); |
| 1236 |
|
| 1237 |
/** |
| 1238 |
* Create new mrb_state with custom allocators. |
| 1239 |
* |
| 1240 |
* @param f |
| 1241 |
* Reference to the allocation function. |
| 1242 |
* @param ud |
| 1243 |
* User data will be passed to custom allocator f. |
| 1244 |
* If user data isn't required just pass NULL. |
| 1245 |
* @return |
| 1246 |
* Pointer to the newly created mrb_state. |
| 1247 |
*/ |
| 1248 |
MRB_API mrb_state* mrb_open_allocf(mrb_allocf f, void *ud); |
| 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_default_allocf 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(mrb_allocf f, void *ud); |
| 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 |
|
| 1272 |
/** |
| 1273 |
* The default allocation function. |
| 1274 |
* |
| 1275 |
* @see mrb_allocf |
| 1276 |
*/ |
| 1277 |
MRB_API void* mrb_default_allocf(mrb_state*, void*, size_t, void*); |
| 1278 |
|
| 1279 |
MRB_API mrb_value mrb_top_self(mrb_state *mrb); |
| 1280 |
MRB_API mrb_value mrb_top_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int stack_keep); |
| 1281 |
MRB_API mrb_value mrb_vm_run(mrb_state *mrb, const struct RProc *proc, mrb_value self, mrb_int stack_keep); |
| 1282 |
MRB_API mrb_value mrb_vm_exec(mrb_state *mrb, const struct RProc *proc, const mrb_code *iseq); |
| 1283 |
/* compatibility macros */ |
| 1284 |
#define mrb_toplevel_run_keep(m,p,k) mrb_top_run((m),(p),mrb_top_self(m),(k)) |
| 1285 |
#define mrb_toplevel_run(m,p) mrb_toplevel_run_keep((m),(p),0) |
| 1286 |
#define mrb_context_run(m,p,s,k) mrb_vm_run((m),(p),(s),(k)) |
| 1287 |
|
| 1288 |
MRB_API void mrb_p(mrb_state*, mrb_value); |
| 1289 |
MRB_API mrb_int mrb_obj_id(mrb_value obj); |
| 1290 |
MRB_API mrb_sym mrb_obj_to_sym(mrb_state *mrb, mrb_value name); |
| 1291 |
|
| 1292 |
MRB_API mrb_bool mrb_obj_eq(mrb_state *mrb, mrb_value a, mrb_value b); |
| 1293 |
MRB_API mrb_bool mrb_obj_equal(mrb_state *mrb, mrb_value a, mrb_value b); |
| 1294 |
MRB_API mrb_bool mrb_equal(mrb_state *mrb, mrb_value obj1, mrb_value obj2); |
| 1295 |
#ifndef MRB_NO_FLOAT |
| 1296 |
MRB_API mrb_value mrb_ensure_float_type(mrb_state *mrb, mrb_value val); |
| 1297 |
#define mrb_as_float(mrb, x) mrb_float(mrb_ensure_float_type(mrb, x)) |
| 1298 |
/* obsolete: use mrb_ensure_float_type() instead */ |
| 1299 |
#define mrb_to_float(mrb, val) mrb_ensure_float_type(mrb, val) |
| 1300 |
#endif |
| 1301 |
MRB_API mrb_value mrb_inspect(mrb_state *mrb, mrb_value obj); |
| 1302 |
MRB_API mrb_bool mrb_eql(mrb_state *mrb, mrb_value obj1, mrb_value obj2); |
| 1303 |
/* mrb_cmp(mrb, obj1, obj2): 1:0:-1; -2 for error */ |
| 1304 |
MRB_API mrb_int mrb_cmp(mrb_state *mrb, mrb_value obj1, mrb_value obj2); |
| 1305 |
|
| 1306 |
#define mrb_gc_arena_save(mrb) ((mrb)->gc.arena_idx) |
| 1307 |
#define mrb_gc_arena_restore(mrb, idx) ((mrb)->gc.arena_idx = (idx)) |
| 1308 |
|
| 1309 |
MRB_API void mrb_garbage_collect(mrb_state*); |
| 1310 |
MRB_API void mrb_full_gc(mrb_state*); |
| 1311 |
MRB_API void mrb_incremental_gc(mrb_state*); |
| 1312 |
MRB_API void mrb_gc_mark(mrb_state*,struct RBasic*); |
| 1313 |
#define mrb_gc_mark_value(mrb,val) do {\ |
| 1314 |
if (!mrb_immediate_p(val)) mrb_gc_mark((mrb), mrb_basic_ptr(val)); \ |
| 1315 |
} while (0) |
| 1316 |
MRB_API void mrb_field_write_barrier(mrb_state*, struct RBasic*, struct RBasic*); |
| 1317 |
#define mrb_field_write_barrier_value(mrb, obj, val) do{\ |
| 1318 |
if (!mrb_immediate_p(val)) mrb_field_write_barrier((mrb), (obj), mrb_basic_ptr(val)); \ |
| 1319 |
} while (0) |
| 1320 |
MRB_API void mrb_write_barrier(mrb_state *, struct RBasic*); |
| 1321 |
|
| 1322 |
MRB_API mrb_value mrb_type_convert(mrb_state *mrb, mrb_value val, enum mrb_vtype type, mrb_sym method); |
| 1323 |
#define mrb_convert_type(mrb, val, type, tname, method) mrb_type_convert(mrb, val, type, mrb_intern_lit(mrb, method)) |
| 1324 |
MRB_API mrb_value mrb_type_convert_check(mrb_state *mrb, mrb_value val, enum mrb_vtype type, mrb_sym method); |
| 1325 |
#define mrb_check_convert_type(mrb, val, type, tname, method) mrb_type_convert_check(mrb, val, type, mrb_intern_lit(mrb, method)) |
| 1326 |
|
| 1327 |
MRB_API mrb_value mrb_any_to_s(mrb_state *mrb, mrb_value obj); |
| 1328 |
MRB_API const char * mrb_obj_classname(mrb_state *mrb, mrb_value obj); |
| 1329 |
MRB_API struct RClass* mrb_obj_class(mrb_state *mrb, mrb_value obj); |
| 1330 |
MRB_API mrb_value mrb_class_path(mrb_state *mrb, struct RClass *c); |
| 1331 |
MRB_API mrb_bool mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c); |
| 1332 |
MRB_API mrb_value mrb_obj_inspect(mrb_state *mrb, mrb_value self); |
| 1333 |
MRB_API mrb_value mrb_obj_clone(mrb_state *mrb, mrb_value self); |
| 1334 |
|
| 1335 |
#ifndef ISPRINT |
| 1336 |
#define ISASCII(c) ((unsigned)(c) <= 0x7f) |
| 1337 |
#define ISPRINT(c) (((unsigned)(c) - 0x20) < 0x5f) |
| 1338 |
#define ISSPACE(c) ((c) == ' ' || (unsigned)(c) - '\t' < 5) |
| 1339 |
#define ISUPPER(c) (((unsigned)(c) - 'A') < 26) |
| 1340 |
#define ISLOWER(c) (((unsigned)(c) - 'a') < 26) |
| 1341 |
#define ISALPHA(c) ((((unsigned)(c) | 0x20) - 'a') < 26) |
| 1342 |
#define ISDIGIT(c) (((unsigned)(c) - '0') < 10) |
| 1343 |
#define ISXDIGIT(c) (ISDIGIT(c) || ((unsigned)(c) | 0x20) - 'a' < 6) |
| 1344 |
#define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c)) |
| 1345 |
#define ISBLANK(c) ((c) == ' ' || (c) == '\t') |
| 1346 |
#define ISCNTRL(c) ((unsigned)(c) < 0x20 || (c) == 0x7f) |
| 1347 |
#define TOUPPER(c) (ISLOWER(c) ? ((c) & 0x5f) : (c)) |
| 1348 |
#define TOLOWER(c) (ISUPPER(c) ? ((c) | 0x20) : (c)) |
| 1349 |
#endif |
| 1350 |
|
| 1351 |
MRB_API mrb_value mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, mrb_int len); |
| 1352 |
MRB_API mrb_noreturn void mrb_exc_raise(mrb_state *mrb, mrb_value exc); |
| 1353 |
|
| 1354 |
MRB_API mrb_noreturn void mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg); |
| 1355 |
MRB_API mrb_noreturn void mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...); |
| 1356 |
MRB_API mrb_noreturn void mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...); |
| 1357 |
MRB_API mrb_noreturn void mrb_frozen_error(mrb_state *mrb, void *frozen_obj); |
| 1358 |
MRB_API mrb_noreturn void mrb_argnum_error(mrb_state *mrb, mrb_int argc, int min, int max); |
| 1359 |
MRB_API void mrb_warn(mrb_state *mrb, const char *fmt, ...); |
| 1360 |
MRB_API mrb_noreturn void mrb_bug(mrb_state *mrb, const char *mesg); |
| 1361 |
MRB_API void mrb_print_backtrace(mrb_state *mrb); |
| 1362 |
MRB_API void mrb_print_error(mrb_state *mrb); |
| 1363 |
/* function for `raisef` formatting */ |
| 1364 |
MRB_API mrb_value mrb_vformat(mrb_state *mrb, const char *format, va_list ap); |
| 1365 |
|
| 1366 |
/* macros to get typical exception objects |
| 1367 |
note: |
| 1368 |
+ those E_* macros requires mrb_state* variable named mrb. |
| 1369 |
+ exception objects obtained from those macros are local to mrb |
| 1370 |
*/ |
| 1371 |
#define MRB_ERROR_SYM(sym) mrb_intern_lit(mrb, #sym) |
| 1372 |
#define E_EXCEPTION mrb->eException_class |
| 1373 |
#define E_STANDARD_ERROR mrb->eStandardError_class |
| 1374 |
#define E_RUNTIME_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(RuntimeError)) |
| 1375 |
#define E_TYPE_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(TypeError)) |
| 1376 |
#define E_ZERODIV_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(ZeroDivisionError)) |
| 1377 |
#define E_ARGUMENT_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(ArgumentError)) |
| 1378 |
#define E_INDEX_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(IndexError)) |
| 1379 |
#define E_RANGE_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(RangeError)) |
| 1380 |
#define E_NAME_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(NameError)) |
| 1381 |
#define E_NOMETHOD_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(NoMethodError)) |
| 1382 |
#define E_SCRIPT_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(ScriptError)) |
| 1383 |
#define E_SYNTAX_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(SyntaxError)) |
| 1384 |
#define E_LOCALJUMP_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(LocalJumpError)) |
| 1385 |
#define E_REGEXP_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(RegexpError)) |
| 1386 |
#define E_FROZEN_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(FrozenError)) |
| 1387 |
#define E_NOTIMP_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(NotImplementedError)) |
| 1388 |
#define E_KEY_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(KeyError)) |
| 1389 |
#ifndef MRB_NO_FLOAT |
| 1390 |
# define E_FLOATDOMAIN_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(FloatDomainError)) |
| 1391 |
#endif |
| 1392 |
|
| 1393 |
MRB_API mrb_value mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg); |
| 1394 |
MRB_API mrb_value mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv); |
| 1395 |
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); |
| 1396 |
|
| 1397 |
/* continue execution to the proc */ |
| 1398 |
/* this function should always be called as the last function of a method */ |
| 1399 |
/* e.g. return mrb_yield_cont(mrb, proc, self, argc, argv); */ |
| 1400 |
mrb_value mrb_yield_cont(mrb_state *mrb, mrb_value b, mrb_value self, mrb_int argc, const mrb_value *argv); |
| 1401 |
|
| 1402 |
/* mrb_gc_protect() leaves the object in the arena */ |
| 1403 |
MRB_API void mrb_gc_protect(mrb_state *mrb, mrb_value obj); |
| 1404 |
/* mrb_gc_register() keeps the object from GC. */ |
| 1405 |
MRB_API void mrb_gc_register(mrb_state *mrb, mrb_value obj); |
| 1406 |
/* mrb_gc_unregister() removes the object from GC root. */ |
| 1407 |
MRB_API void mrb_gc_unregister(mrb_state *mrb, mrb_value obj); |
| 1408 |
|
| 1409 |
/* type conversion/check functions */ |
| 1410 |
MRB_API mrb_value mrb_ensure_array_type(mrb_state *mrb, mrb_value self); |
| 1411 |
MRB_API mrb_value mrb_check_array_type(mrb_state *mrb, mrb_value self); |
| 1412 |
MRB_API mrb_value mrb_ensure_hash_type(mrb_state *mrb, mrb_value hash); |
| 1413 |
MRB_API mrb_value mrb_check_hash_type(mrb_state *mrb, mrb_value hash); |
| 1414 |
MRB_API mrb_value mrb_ensure_string_type(mrb_state *mrb, mrb_value str); |
| 1415 |
MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str); |
| 1416 |
/* obsolete: use mrb_ensure_string_type() instead */ |
| 1417 |
#define mrb_string_type(mrb, str) mrb_ensure_string_type(mrb,str) |
| 1418 |
#define mrb_to_str(mrb, str) mrb_ensure_string_type(mrb,str) |
| 1419 |
/* obsolete: use mrb_obj_as_string() instead */ |
| 1420 |
#define mrb_str_to_str(mrb, str) mrb_obj_as_string(mrb, str) |
| 1421 |
/* check if val is an integer (including Bigint) */ |
| 1422 |
MRB_API mrb_value mrb_ensure_integer_type(mrb_state *mrb, mrb_value val); |
| 1423 |
/* check if val fit in mrb_int */ |
| 1424 |
MRB_API mrb_value mrb_ensure_int_type(mrb_state *mrb, mrb_value val); |
| 1425 |
#define mrb_as_int(mrb, val) mrb_integer(mrb_ensure_int_type(mrb, val)) |
| 1426 |
/* obsolete: use mrb_ensure_int_type() instead */ |
| 1427 |
#define mrb_to_integer(mrb, val) mrb_ensure_int_type(mrb, val) |
| 1428 |
#define mrb_to_int(mrb, val) mrb_ensure_int_type(mrb, val) |
| 1429 |
|
| 1430 |
/* string type checking (contrary to the name, it doesn't convert) */ |
| 1431 |
MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t); |
| 1432 |
MRB_API void mrb_check_frozen(mrb_state *mrb, void *); |
| 1433 |
MRB_API void mrb_check_frozen_value(mrb_state *mrb, mrb_value v); |
| 1434 |
MRB_API void mrb_define_alias(mrb_state *mrb, struct RClass *c, const char *a, const char *b); |
| 1435 |
MRB_API void mrb_define_alias_id(mrb_state *mrb, struct RClass *c, mrb_sym a, mrb_sym b); |
| 1436 |
MRB_API const char *mrb_class_name(mrb_state *mrb, struct RClass* klass); |
| 1437 |
MRB_API void mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val); |
| 1438 |
|
| 1439 |
MRB_API mrb_value mrb_attr_get(mrb_state *mrb, mrb_value obj, mrb_sym id); |
| 1440 |
|
| 1441 |
MRB_API mrb_bool mrb_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym mid); |
| 1442 |
MRB_API mrb_bool mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, struct RClass* c); |
| 1443 |
MRB_API mrb_bool mrb_func_basic_p(mrb_state *mrb, mrb_value obj, mrb_sym mid, mrb_func_t func); |
| 1444 |
|
| 1445 |
/* obsolete function(s); will be removed */ |
| 1446 |
#define mrb_int(mrb, val) mrb_as_int(mrb, val) |
| 1447 |
|
| 1448 |
/** |
| 1449 |
* Resume a Fiber |
| 1450 |
* |
| 1451 |
* Implemented in mruby-fiber |
| 1452 |
* |
| 1453 |
* Switches to the specified fiber and executes. Like the `Fiber#resume` method. |
| 1454 |
* |
| 1455 |
* @note It can only be called before entering the mruby VM (e.g. in the `main()` function). |
| 1456 |
*/ |
| 1457 |
MRB_API mrb_value mrb_fiber_resume(mrb_state *mrb, mrb_value fib, mrb_int argc, const mrb_value *argv); |
| 1458 |
|
| 1459 |
/** |
| 1460 |
* Yield a Fiber |
| 1461 |
* |
| 1462 |
* Implemented in mruby-fiber |
| 1463 |
* |
| 1464 |
* Passes control to the caller fiber of the running fiber. Like the `Fiber.yield` method. |
| 1465 |
* |
| 1466 |
* @note This function is only available from inside a function defined as a method by, |
| 1467 |
* for example, `mrb_define_method()`. |
| 1468 |
* Also, the work following `mrb_fiber_yield()` cannot be performed, |
| 1469 |
* and the return value of `mrb_fiber_yield()` must be returned as is. |
| 1470 |
* |
| 1471 |
* return mrb_fiber_yield(mrb, argc, argv); |
| 1472 |
*/ |
| 1473 |
MRB_API mrb_value mrb_fiber_yield(mrb_state *mrb, mrb_int argc, const mrb_value *argv); |
| 1474 |
|
| 1475 |
/** |
| 1476 |
* Check if a Fiber is alive |
| 1477 |
* |
| 1478 |
* Implemented in mruby-fiber |
| 1479 |
*/ |
| 1480 |
MRB_API mrb_value mrb_fiber_alive_p(mrb_state *mrb, mrb_value fib); |
| 1481 |
|
| 1482 |
/** |
| 1483 |
* FiberError reference |
| 1484 |
* |
| 1485 |
* Implemented in mruby-fiber |
| 1486 |
*/ |
| 1487 |
#define E_FIBER_ERROR mrb_exc_get_id(mrb, MRB_ERROR_SYM(FiberError)) |
| 1488 |
MRB_API void mrb_stack_extend(mrb_state*, mrb_int); |
| 1489 |
|
| 1490 |
/* memory pool implementation */ |
| 1491 |
typedef struct mrb_pool mrb_pool; |
| 1492 |
MRB_API struct mrb_pool* mrb_pool_open(mrb_state*); |
| 1493 |
MRB_API void mrb_pool_close(struct mrb_pool*); |
| 1494 |
MRB_API void* mrb_pool_alloc(struct mrb_pool*, size_t); |
| 1495 |
MRB_API void* mrb_pool_realloc(struct mrb_pool*, void*, size_t oldlen, size_t newlen); |
| 1496 |
MRB_API mrb_bool mrb_pool_can_realloc(struct mrb_pool*, void*, size_t); |
| 1497 |
/* temporary memory allocation, only effective while GC arena is kept */ |
| 1498 |
MRB_API void* mrb_alloca(mrb_state *mrb, size_t); |
| 1499 |
|
| 1500 |
MRB_API void mrb_state_atexit(mrb_state *mrb, mrb_atexit_func func); |
| 1501 |
|
| 1502 |
MRB_API void mrb_show_version(mrb_state *mrb); |
| 1503 |
MRB_API void mrb_show_copyright(mrb_state *mrb); |
| 1504 |
|
| 1505 |
MRB_API mrb_value mrb_format(mrb_state *mrb, const char *format, ...); |
| 1506 |
|
| 1507 |
#ifdef MRB_PRESYM_SCANNING |
| 1508 |
# include <mruby/presym/scanning.h> |
| 1509 |
#endif |
| 1510 |
|
| 1511 |
#if 0 |
| 1512 |
/* memcpy and memset does not work with gdb reverse-next on my box */ |
| 1513 |
/* use naive memcpy and memset instead */ |
| 1514 |
#undef memcpy |
| 1515 |
#undef memset |
| 1516 |
static void* |
| 1517 |
mrbmemcpy(void *dst, const void *src, size_t n) |
| 1518 |
{ |
| 1519 |
char *d = (char*)dst; |
| 1520 |
const char *s = (const char*)src; |
| 1521 |
while (n--) |
| 1522 |
*d++ = *s++; |
| 1523 |
return d; |
| 1524 |
} |
| 1525 |
#define memcpy(a,b,c) mrbmemcpy(a,b,c) |
| 1526 |
|
| 1527 |
static void* |
| 1528 |
mrbmemset(void *s, int c, size_t n) |
| 1529 |
{ |
| 1530 |
char *t = (char*)s; |
| 1531 |
while (n--) |
| 1532 |
*t++ = c; |
| 1533 |
return s; |
| 1534 |
} |
| 1535 |
#define memset(a,b,c) mrbmemset(a,b,c) |
| 1536 |
#endif |
| 1537 |
|
| 1538 |
MRB_END_DECL |
| 1539 |
|
| 1540 |
#endif /* MRUBY_H */ |