mbed I/F binding for mruby
Dependents: mruby_mbed_web mirb_mbed
mbed-mruby
How to use
Class
include/mruby/boxing_word.h@0:158c61bb030f, 2015-03-25 (annotated)
- Committer:
- mzta
- Date:
- Wed Mar 25 17:36:16 2015 +0000
- Revision:
- 0:158c61bb030f
mirb_mbed initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzta | 0:158c61bb030f | 1 | /* |
mzta | 0:158c61bb030f | 2 | ** mruby/boxing_word.h - word boxing mrb_value definition |
mzta | 0:158c61bb030f | 3 | ** |
mzta | 0:158c61bb030f | 4 | ** See Copyright Notice in mruby.h |
mzta | 0:158c61bb030f | 5 | */ |
mzta | 0:158c61bb030f | 6 | |
mzta | 0:158c61bb030f | 7 | #ifndef MRUBY_BOXING_WORD_H |
mzta | 0:158c61bb030f | 8 | #define MRUBY_BOXING_WORD_H |
mzta | 0:158c61bb030f | 9 | |
mzta | 0:158c61bb030f | 10 | #if defined(MRB_INT16) |
mzta | 0:158c61bb030f | 11 | # error MRB_INT16 is too small for MRB_WORD_BOXING. |
mzta | 0:158c61bb030f | 12 | #endif |
mzta | 0:158c61bb030f | 13 | |
mzta | 0:158c61bb030f | 14 | struct RFloat { |
mzta | 0:158c61bb030f | 15 | MRB_OBJECT_HEADER; |
mzta | 0:158c61bb030f | 16 | mrb_float f; |
mzta | 0:158c61bb030f | 17 | }; |
mzta | 0:158c61bb030f | 18 | |
mzta | 0:158c61bb030f | 19 | struct RCptr { |
mzta | 0:158c61bb030f | 20 | MRB_OBJECT_HEADER; |
mzta | 0:158c61bb030f | 21 | void *p; |
mzta | 0:158c61bb030f | 22 | }; |
mzta | 0:158c61bb030f | 23 | |
mzta | 0:158c61bb030f | 24 | #define MRB_FIXNUM_SHIFT 1 |
mzta | 0:158c61bb030f | 25 | #define MRB_TT_HAS_BASIC MRB_TT_FLOAT |
mzta | 0:158c61bb030f | 26 | |
mzta | 0:158c61bb030f | 27 | enum mrb_special_consts { |
mzta | 0:158c61bb030f | 28 | MRB_Qnil = 0, |
mzta | 0:158c61bb030f | 29 | MRB_Qfalse = 2, |
mzta | 0:158c61bb030f | 30 | MRB_Qtrue = 4, |
mzta | 0:158c61bb030f | 31 | MRB_Qundef = 6, |
mzta | 0:158c61bb030f | 32 | }; |
mzta | 0:158c61bb030f | 33 | |
mzta | 0:158c61bb030f | 34 | #define MRB_FIXNUM_FLAG 0x01 |
mzta | 0:158c61bb030f | 35 | #define MRB_SYMBOL_FLAG 0x0e |
mzta | 0:158c61bb030f | 36 | #define MRB_SPECIAL_SHIFT 8 |
mzta | 0:158c61bb030f | 37 | |
mzta | 0:158c61bb030f | 38 | typedef union mrb_value { |
mzta | 0:158c61bb030f | 39 | union { |
mzta | 0:158c61bb030f | 40 | void *p; |
mzta | 0:158c61bb030f | 41 | struct { |
mzta | 0:158c61bb030f | 42 | unsigned int i_flag : MRB_FIXNUM_SHIFT; |
mzta | 0:158c61bb030f | 43 | mrb_int i : (MRB_INT_BIT - MRB_FIXNUM_SHIFT); |
mzta | 0:158c61bb030f | 44 | }; |
mzta | 0:158c61bb030f | 45 | struct { |
mzta | 0:158c61bb030f | 46 | unsigned int sym_flag : MRB_SPECIAL_SHIFT; |
mzta | 0:158c61bb030f | 47 | mrb_sym sym : (sizeof(mrb_sym) * CHAR_BIT); |
mzta | 0:158c61bb030f | 48 | }; |
mzta | 0:158c61bb030f | 49 | struct RBasic *bp; |
mzta | 0:158c61bb030f | 50 | struct RFloat *fp; |
mzta | 0:158c61bb030f | 51 | struct RCptr *vp; |
mzta | 0:158c61bb030f | 52 | } value; |
mzta | 0:158c61bb030f | 53 | unsigned long w; |
mzta | 0:158c61bb030f | 54 | } mrb_value; |
mzta | 0:158c61bb030f | 55 | |
mzta | 0:158c61bb030f | 56 | MRB_API mrb_value mrb_word_boxing_cptr_value(struct mrb_state*, void*); |
mzta | 0:158c61bb030f | 57 | MRB_API mrb_value mrb_word_boxing_float_value(struct mrb_state*, mrb_float); |
mzta | 0:158c61bb030f | 58 | MRB_API mrb_value mrb_word_boxing_float_pool(struct mrb_state*, mrb_float); |
mzta | 0:158c61bb030f | 59 | |
mzta | 0:158c61bb030f | 60 | #define mrb_float_pool(mrb,f) mrb_word_boxing_float_pool(mrb,f) |
mzta | 0:158c61bb030f | 61 | |
mzta | 0:158c61bb030f | 62 | #define mrb_ptr(o) (o).value.p |
mzta | 0:158c61bb030f | 63 | #define mrb_cptr(o) (o).value.vp->p |
mzta | 0:158c61bb030f | 64 | #define mrb_float(o) (o).value.fp->f |
mzta | 0:158c61bb030f | 65 | #define mrb_fixnum(o) (o).value.i |
mzta | 0:158c61bb030f | 66 | #define mrb_symbol(o) (o).value.sym |
mzta | 0:158c61bb030f | 67 | |
mzta | 0:158c61bb030f | 68 | static inline enum mrb_vtype |
mzta | 0:158c61bb030f | 69 | mrb_type(mrb_value o) |
mzta | 0:158c61bb030f | 70 | { |
mzta | 0:158c61bb030f | 71 | switch (o.w) { |
mzta | 0:158c61bb030f | 72 | case MRB_Qfalse: |
mzta | 0:158c61bb030f | 73 | case MRB_Qnil: |
mzta | 0:158c61bb030f | 74 | return MRB_TT_FALSE; |
mzta | 0:158c61bb030f | 75 | case MRB_Qtrue: |
mzta | 0:158c61bb030f | 76 | return MRB_TT_TRUE; |
mzta | 0:158c61bb030f | 77 | case MRB_Qundef: |
mzta | 0:158c61bb030f | 78 | return MRB_TT_UNDEF; |
mzta | 0:158c61bb030f | 79 | } |
mzta | 0:158c61bb030f | 80 | if (o.value.i_flag == MRB_FIXNUM_FLAG) { |
mzta | 0:158c61bb030f | 81 | return MRB_TT_FIXNUM; |
mzta | 0:158c61bb030f | 82 | } |
mzta | 0:158c61bb030f | 83 | if (o.value.sym_flag == MRB_SYMBOL_FLAG) { |
mzta | 0:158c61bb030f | 84 | return MRB_TT_SYMBOL; |
mzta | 0:158c61bb030f | 85 | } |
mzta | 0:158c61bb030f | 86 | return o.value.bp->tt; |
mzta | 0:158c61bb030f | 87 | } |
mzta | 0:158c61bb030f | 88 | |
mzta | 0:158c61bb030f | 89 | #define mrb_bool(o) ((o).w != MRB_Qnil && (o).w != MRB_Qfalse) |
mzta | 0:158c61bb030f | 90 | #define mrb_fixnum_p(o) ((o).value.i_flag == MRB_FIXNUM_FLAG) |
mzta | 0:158c61bb030f | 91 | #define mrb_undef_p(o) ((o).w == MRB_Qundef) |
mzta | 0:158c61bb030f | 92 | #define mrb_nil_p(o) ((o).w == MRB_Qnil) |
mzta | 0:158c61bb030f | 93 | |
mzta | 0:158c61bb030f | 94 | #define BOXWORD_SET_VALUE(o, ttt, attr, v) do {\ |
mzta | 0:158c61bb030f | 95 | (o).w = 0;\ |
mzta | 0:158c61bb030f | 96 | (o).attr = (v);\ |
mzta | 0:158c61bb030f | 97 | switch (ttt) {\ |
mzta | 0:158c61bb030f | 98 | case MRB_TT_FALSE: (o).w = (v) ? MRB_Qfalse : MRB_Qnil; break;\ |
mzta | 0:158c61bb030f | 99 | case MRB_TT_TRUE: (o).w = MRB_Qtrue; break;\ |
mzta | 0:158c61bb030f | 100 | case MRB_TT_UNDEF: (o).w = MRB_Qundef; break;\ |
mzta | 0:158c61bb030f | 101 | case MRB_TT_FIXNUM: (o).value.i_flag = MRB_FIXNUM_FLAG; break;\ |
mzta | 0:158c61bb030f | 102 | case MRB_TT_SYMBOL: (o).value.sym_flag = MRB_SYMBOL_FLAG; break;\ |
mzta | 0:158c61bb030f | 103 | default: if ((o).value.bp) (o).value.bp->tt = ttt; break;\ |
mzta | 0:158c61bb030f | 104 | }\ |
mzta | 0:158c61bb030f | 105 | } while (0) |
mzta | 0:158c61bb030f | 106 | |
mzta | 0:158c61bb030f | 107 | #define SET_FLOAT_VALUE(mrb,r,v) r = mrb_word_boxing_float_value(mrb, v) |
mzta | 0:158c61bb030f | 108 | #define SET_CPTR_VALUE(mrb,r,v) r = mrb_word_boxing_cptr_value(mrb, v) |
mzta | 0:158c61bb030f | 109 | #define SET_NIL_VALUE(r) BOXWORD_SET_VALUE(r, MRB_TT_FALSE, value.i, 0) |
mzta | 0:158c61bb030f | 110 | #define SET_FALSE_VALUE(r) BOXWORD_SET_VALUE(r, MRB_TT_FALSE, value.i, 1) |
mzta | 0:158c61bb030f | 111 | #define SET_TRUE_VALUE(r) BOXWORD_SET_VALUE(r, MRB_TT_TRUE, value.i, 1) |
mzta | 0:158c61bb030f | 112 | #define SET_BOOL_VALUE(r,b) BOXWORD_SET_VALUE(r, b ? MRB_TT_TRUE : MRB_TT_FALSE, value.i, 1) |
mzta | 0:158c61bb030f | 113 | #define SET_INT_VALUE(r,n) BOXWORD_SET_VALUE(r, MRB_TT_FIXNUM, value.i, (n)) |
mzta | 0:158c61bb030f | 114 | #define SET_SYM_VALUE(r,v) BOXWORD_SET_VALUE(r, MRB_TT_SYMBOL, value.sym, (v)) |
mzta | 0:158c61bb030f | 115 | #define SET_OBJ_VALUE(r,v) BOXWORD_SET_VALUE(r, (((struct RObject*)(v))->tt), value.p, (v)) |
mzta | 0:158c61bb030f | 116 | #define SET_PROC_VALUE(r,v) BOXWORD_SET_VALUE(r, MRB_TT_PROC, value.p, v) |
mzta | 0:158c61bb030f | 117 | #define SET_UNDEF_VALUE(r) BOXWORD_SET_VALUE(r, MRB_TT_UNDEF, value.i, 0) |
mzta | 0:158c61bb030f | 118 | |
mzta | 0:158c61bb030f | 119 | #endif /* MRUBY_BOXING_WORD_H */ |
mzta | 0:158c61bb030f | 120 |