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