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