prism-cli 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,161 @@
1
+ /*
2
+ ** mruby/numeric.h - Numeric, Integer, Float, Fixnum class
3
+ **
4
+ ** See Copyright Notice in mruby.h
5
+ */
6
+
7
+ #ifndef MRUBY_NUMERIC_H
8
+ #define MRUBY_NUMERIC_H
9
+
10
+ #include "common.h"
11
+
12
+ /**
13
+ * Numeric class and it's sub-classes.
14
+ *
15
+ * Integer, Float and Fixnum
16
+ */
17
+ MRB_BEGIN_DECL
18
+
19
+ #define TYPED_POSFIXABLE(f,t) ((f) <= (t)MRB_INT_MAX)
20
+ #define TYPED_NEGFIXABLE(f,t) ((f) >= (t)MRB_INT_MIN)
21
+ #define TYPED_FIXABLE(f,t) (TYPED_POSFIXABLE(f,t) && TYPED_NEGFIXABLE(f,t))
22
+ #define POSFIXABLE(f) TYPED_POSFIXABLE(f,mrb_int)
23
+ #define NEGFIXABLE(f) TYPED_NEGFIXABLE(f,mrb_int)
24
+ #define FIXABLE(f) TYPED_FIXABLE(f,mrb_int)
25
+ #ifndef MRB_WITHOUT_FLOAT
26
+ #define FIXABLE_FLOAT(f) TYPED_FIXABLE(f,double)
27
+ #endif
28
+
29
+ #ifndef MRB_WITHOUT_FLOAT
30
+ MRB_API mrb_value mrb_flo_to_fixnum(mrb_state *mrb, mrb_value val);
31
+ #endif
32
+ MRB_API mrb_value mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, mrb_int base);
33
+ /* ArgumentError if format string doesn't match /%(\.[0-9]+)?[aAeEfFgG]/ */
34
+ #ifndef MRB_WITHOUT_FLOAT
35
+ MRB_API mrb_value mrb_float_to_str(mrb_state *mrb, mrb_value x, const char *fmt);
36
+ MRB_API mrb_float mrb_to_flo(mrb_state *mrb, mrb_value x);
37
+ #endif
38
+
39
+ mrb_value mrb_fixnum_plus(mrb_state *mrb, mrb_value x, mrb_value y);
40
+ mrb_value mrb_fixnum_minus(mrb_state *mrb, mrb_value x, mrb_value y);
41
+ mrb_value mrb_fixnum_mul(mrb_state *mrb, mrb_value x, mrb_value y);
42
+ mrb_value mrb_num_div(mrb_state *mrb, mrb_value x, mrb_value y);
43
+
44
+ #ifndef __has_builtin
45
+ #define __has_builtin(x) 0
46
+ #endif
47
+
48
+ #if (defined(__GNUC__) && __GNUC__ >= 5) || \
49
+ (__has_builtin(__builtin_add_overflow) && \
50
+ __has_builtin(__builtin_sub_overflow) && \
51
+ __has_builtin(__builtin_mul_overflow))
52
+ # define MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
53
+ #endif
54
+
55
+ /*
56
+ // Clang 3.8 and 3.9 have problem compiling mruby in 32-bit mode, when MRB_INT64 is set
57
+ // because of missing __mulodi4 and similar functions in its runtime. We need to use custom
58
+ // implementation for them.
59
+ */
60
+ #ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
61
+ #if defined(__clang__) && (__clang_major__ == 3) && (__clang_minor__ >= 8) && \
62
+ defined(MRB_32BIT) && defined(MRB_INT64)
63
+ #undef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
64
+ #endif
65
+ #endif
66
+
67
+ #ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
68
+
69
+ #ifndef MRB_WORD_BOXING
70
+ # define WBCHK(x) 0
71
+ #else
72
+ # define WBCHK(x) !FIXABLE(x)
73
+ #endif
74
+
75
+ static inline mrb_bool
76
+ mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
77
+ {
78
+ return __builtin_add_overflow(augend, addend, sum) || WBCHK(*sum);
79
+ }
80
+
81
+ static inline mrb_bool
82
+ mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
83
+ {
84
+ return __builtin_sub_overflow(minuend, subtrahend, difference) || WBCHK(*difference);
85
+ }
86
+
87
+ static inline mrb_bool
88
+ mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
89
+ {
90
+ return __builtin_mul_overflow(multiplier, multiplicand, product) || WBCHK(*product);
91
+ }
92
+
93
+ #undef WBCHK
94
+
95
+ #else
96
+
97
+ #define MRB_UINT_MAKE2(n) uint ## n ## _t
98
+ #define MRB_UINT_MAKE(n) MRB_UINT_MAKE2(n)
99
+ #define mrb_uint MRB_UINT_MAKE(MRB_INT_BIT)
100
+
101
+ #define MRB_INT_OVERFLOW_MASK ((mrb_uint)1 << (MRB_INT_BIT - 1 - MRB_FIXNUM_SHIFT))
102
+
103
+ static inline mrb_bool
104
+ mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
105
+ {
106
+ mrb_uint x = (mrb_uint)augend;
107
+ mrb_uint y = (mrb_uint)addend;
108
+ mrb_uint z = (mrb_uint)(x + y);
109
+ *sum = (mrb_int)z;
110
+ return !!(((x ^ z) & (y ^ z)) & MRB_INT_OVERFLOW_MASK);
111
+ }
112
+
113
+ static inline mrb_bool
114
+ mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
115
+ {
116
+ mrb_uint x = (mrb_uint)minuend;
117
+ mrb_uint y = (mrb_uint)subtrahend;
118
+ mrb_uint z = (mrb_uint)(x - y);
119
+ *difference = (mrb_int)z;
120
+ return !!(((x ^ z) & (~y ^ z)) & MRB_INT_OVERFLOW_MASK);
121
+ }
122
+
123
+ static inline mrb_bool
124
+ mrb_int_mul_overflow(mrb_int multiplier, mrb_int multiplicand, mrb_int *product)
125
+ {
126
+ #if MRB_INT_BIT == 32
127
+ int64_t n = (int64_t)multiplier * multiplicand;
128
+ *product = (mrb_int)n;
129
+ return !FIXABLE(n);
130
+ #else
131
+ if (multiplier > 0) {
132
+ if (multiplicand > 0) {
133
+ if (multiplier > MRB_INT_MAX / multiplicand) return TRUE;
134
+ }
135
+ else {
136
+ if (multiplicand < MRB_INT_MAX / multiplier) return TRUE;
137
+ }
138
+ }
139
+ else {
140
+ if (multiplicand > 0) {
141
+ if (multiplier < MRB_INT_MAX / multiplicand) return TRUE;
142
+ }
143
+ else {
144
+ if (multiplier != 0 && multiplicand < MRB_INT_MAX / multiplier) return TRUE;
145
+ }
146
+ }
147
+ *product = multiplier * multiplicand;
148
+ return FALSE;
149
+ #endif
150
+ }
151
+
152
+ #undef MRB_INT_OVERFLOW_MASK
153
+ #undef mrb_uint
154
+ #undef MRB_UINT_MAKE
155
+ #undef MRB_UINT_MAKE2
156
+
157
+ #endif
158
+
159
+ MRB_END_DECL
160
+
161
+ #endif /* MRUBY_NUMERIC_H */
@@ -0,0 +1,44 @@
1
+ /*
2
+ ** mruby/object.h - mruby object definition
3
+ **
4
+ ** See Copyright Notice in mruby.h
5
+ */
6
+
7
+ #ifndef MRUBY_OBJECT_H
8
+ #define MRUBY_OBJECT_H
9
+
10
+ #define MRB_OBJECT_HEADER \
11
+ enum mrb_vtype tt:8;\
12
+ uint32_t color:3;\
13
+ uint32_t flags:21;\
14
+ struct RClass *c;\
15
+ struct RBasic *gcnext
16
+
17
+ #define MRB_FLAG_TEST(obj, flag) ((obj)->flags & (flag))
18
+
19
+
20
+ struct RBasic {
21
+ MRB_OBJECT_HEADER;
22
+ };
23
+ #define mrb_basic_ptr(v) ((struct RBasic*)(mrb_ptr(v)))
24
+
25
+ #define MRB_FL_OBJ_IS_FROZEN (1 << 20)
26
+ #define MRB_FROZEN_P(o) ((o)->flags & MRB_FL_OBJ_IS_FROZEN)
27
+ #define MRB_SET_FROZEN_FLAG(o) ((o)->flags |= MRB_FL_OBJ_IS_FROZEN)
28
+ #define MRB_UNSET_FROZEN_FLAG(o) ((o)->flags &= ~MRB_FL_OBJ_IS_FROZEN)
29
+
30
+ struct RObject {
31
+ MRB_OBJECT_HEADER;
32
+ struct iv_tbl *iv;
33
+ };
34
+ #define mrb_obj_ptr(v) ((struct RObject*)(mrb_ptr(v)))
35
+
36
+ #define mrb_immediate_p(x) (mrb_type(x) < MRB_TT_HAS_BASIC)
37
+ #define mrb_special_const_p(x) mrb_immediate_p(x)
38
+
39
+ struct RFiber {
40
+ MRB_OBJECT_HEADER;
41
+ struct mrb_context *cxt;
42
+ };
43
+
44
+ #endif /* MRUBY_OBJECT_H */
@@ -0,0 +1,69 @@
1
+ /*
2
+ ** mruby/opcode.h - RiteVM operation codes
3
+ **
4
+ ** See Copyright Notice in mruby.h
5
+ */
6
+
7
+ #ifndef MRUBY_OPCODE_H
8
+ #define MRUBY_OPCODE_H
9
+
10
+ enum mrb_insn {
11
+ #define OPCODE(x,_) OP_ ## x,
12
+ #include "mruby/ops.h"
13
+ #undef OPCODE
14
+ };
15
+
16
+ #define OP_L_STRICT 1
17
+ #define OP_L_CAPTURE 2
18
+ #define OP_L_METHOD OP_L_STRICT
19
+ #define OP_L_LAMBDA (OP_L_STRICT|OP_L_CAPTURE)
20
+ #define OP_L_BLOCK OP_L_CAPTURE
21
+
22
+ #define OP_R_NORMAL 0
23
+ #define OP_R_BREAK 1
24
+ #define OP_R_RETURN 2
25
+
26
+ #define PEEK_B(pc) (*(pc))
27
+ #define PEEK_S(pc) ((pc)[0]<<8|(pc)[1])
28
+ #define PEEK_W(pc) ((pc)[0]<<16|(pc)[1]<<8|(pc)[2])
29
+
30
+ #define READ_B() PEEK_B(pc++)
31
+ #define READ_S() (pc+=2, PEEK_S(pc-2))
32
+ #define READ_W() (pc+=3, PEEK_W(pc-3))
33
+
34
+ #define FETCH_Z() /* nothing */
35
+ #define FETCH_B() do {a=READ_B();} while (0)
36
+ #define FETCH_BB() do {a=READ_B(); b=READ_B();} while (0)
37
+ #define FETCH_BBB() do {a=READ_B(); b=READ_B(); c=READ_B();} while (0)
38
+ #define FETCH_BS() do {a=READ_B(); b=READ_S();} while (0)
39
+ #define FETCH_S() do {a=READ_S();} while (0)
40
+ #define FETCH_W() do {a=READ_W();} while (0)
41
+
42
+ /* with OP_EXT1 (1st 16bit) */
43
+ #define FETCH_Z_1() FETCH_Z()
44
+ #define FETCH_B_1() FETCH_S()
45
+ #define FETCH_BB_1() do {a=READ_S(); b=READ_B();} while (0)
46
+ #define FETCH_BBB_1() do {a=READ_S(); b=READ_B(); c=READ_B();} while (0)
47
+ #define FETCH_BS_1() do {a=READ_S(); b=READ_S();} while (0)
48
+ #define FETCH_S_1() FETCH_S()
49
+ #define FETCH_W_1() FETCH_W()
50
+
51
+ /* with OP_EXT2 (2nd 16bit) */
52
+ #define FETCH_Z_2() FETCH_Z()
53
+ #define FETCH_B_2() FETCH_B()
54
+ #define FETCH_BB_2() do {a=READ_B(); b=READ_S();} while (0)
55
+ #define FETCH_BBB_2() do {a=READ_B(); b=READ_S(); c=READ_B();} while (0)
56
+ #define FETCH_BS_2() FETCH_BS()
57
+ #define FETCH_S_2() FETCH_S()
58
+ #define FETCH_W_2() FETCH_W()
59
+
60
+ /* with OP_EXT3 (1st & 2nd 16bit) */
61
+ #define FETCH_Z_3() FETCH_Z()
62
+ #define FETCH_B_3() FETCH_B()
63
+ #define FETCH_BB_3() do {a=READ_S(); b=READ_S();} while (0)
64
+ #define FETCH_BBB_3() do {a=READ_S(); b=READ_S(); c=READ_B();} while (0)
65
+ #define FETCH_BS_3() do {a=READ_S(); b=READ_S();} while (0)
66
+ #define FETCH_S_3() FETCH_S()
67
+ #define FETCH_W_3() FETCH_W()
68
+
69
+ #endif /* MRUBY_OPCODE_H */
@@ -0,0 +1,117 @@
1
+ /* operand types:
2
+ + Z: no operand (Z,Z,Z,Z)
3
+ + B: 8bit (B,S,B,B)
4
+ + BB: 8+8bit (BB,SB,BS,SS)
5
+ + BBB: 8+8+8bit (BBB,SBB,BSB,SSB)
6
+ + BS: 8+16bit (BS,SS,BS,BS)
7
+ + S: 16bit (S,S,S,S)
8
+ + W: 24bit (W,W,W,W)
9
+ */
10
+
11
+ /*-----------------------------------------------------------------------
12
+ operation code operands semantics
13
+ ------------------------------------------------------------------------*/
14
+ OPCODE(NOP, Z) /* no operation */
15
+ OPCODE(MOVE, BB) /* R(a) = R(b) */
16
+ OPCODE(LOADL, BB) /* R(a) = Pool(b) */
17
+ OPCODE(LOADI, BB) /* R(a) = mrb_int(b) */
18
+ OPCODE(LOADINEG, BB) /* R(a) = mrb_int(-b) */
19
+ OPCODE(LOADI__1, B) /* R(a) = mrb_int(-1) */
20
+ OPCODE(LOADI_0, B) /* R(a) = mrb_int(0) */
21
+ OPCODE(LOADI_1, B) /* R(a) = mrb_int(1) */
22
+ OPCODE(LOADI_2, B) /* R(a) = mrb_int(2) */
23
+ OPCODE(LOADI_3, B) /* R(a) = mrb_int(3) */
24
+ OPCODE(LOADI_4, B) /* R(a) = mrb_int(4) */
25
+ OPCODE(LOADI_5, B) /* R(a) = mrb_int(5) */
26
+ OPCODE(LOADI_6, B) /* R(a) = mrb_int(6) */
27
+ OPCODE(LOADI_7, B) /* R(a) = mrb_int(7) */
28
+ OPCODE(LOADSYM, BB) /* R(a) = Syms(b) */
29
+ OPCODE(LOADNIL, B) /* R(a) = nil */
30
+ OPCODE(LOADSELF, B) /* R(a) = self */
31
+ OPCODE(LOADT, B) /* R(a) = true */
32
+ OPCODE(LOADF, B) /* R(a) = false */
33
+ OPCODE(GETGV, BB) /* R(a) = getglobal(Syms(b)) */
34
+ OPCODE(SETGV, BB) /* setglobal(Syms(b), R(a)) */
35
+ OPCODE(GETSV, BB) /* R(a) = Special[Syms(b)] */
36
+ OPCODE(SETSV, BB) /* Special[Syms(b)] = R(a) */
37
+ OPCODE(GETIV, BB) /* R(a) = ivget(Syms(b)) */
38
+ OPCODE(SETIV, BB) /* ivset(Syms(b),R(a)) */
39
+ OPCODE(GETCV, BB) /* R(a) = cvget(Syms(b)) */
40
+ OPCODE(SETCV, BB) /* cvset(Syms(b),R(a)) */
41
+ OPCODE(GETCONST, BB) /* R(a) = constget(Syms(b)) */
42
+ OPCODE(SETCONST, BB) /* constset(Syms(b),R(a)) */
43
+ OPCODE(GETMCNST, BB) /* R(a) = R(a)::Syms(b) */
44
+ OPCODE(SETMCNST, BB) /* R(a+1)::Syms(b) = R(a) */
45
+ OPCODE(GETUPVAR, BBB) /* R(a) = uvget(b,c) */
46
+ OPCODE(SETUPVAR, BBB) /* uvset(b,c,R(a)) */
47
+ OPCODE(JMP, S) /* pc=a */
48
+ OPCODE(JMPIF, BS) /* if R(b) pc=a */
49
+ OPCODE(JMPNOT, BS) /* if !R(b) pc=a */
50
+ OPCODE(JMPNIL, BS) /* if R(b)==nil pc=a */
51
+ OPCODE(ONERR, S) /* rescue_push(a) */
52
+ OPCODE(EXCEPT, B) /* R(a) = exc */
53
+ OPCODE(RESCUE, BB) /* R(b) = R(a).isa?(R(b)) */
54
+ OPCODE(POPERR, B) /* a.times{rescue_pop()} */
55
+ OPCODE(RAISE, B) /* raise(R(a)) */
56
+ OPCODE(EPUSH, B) /* ensure_push(SEQ[a]) */
57
+ OPCODE(EPOP, B) /* A.times{ensure_pop().call} */
58
+ OPCODE(SENDV, BB) /* R(a) = call(R(a),Syms(b),*R(a+1)) */
59
+ OPCODE(SENDVB, BB) /* R(a) = call(R(a),Syms(b),*R(a+1),&R(a+2)) */
60
+ OPCODE(SEND, BBB) /* R(a) = call(R(a),Syms(b),R(a+1),...,R(a+c)) */
61
+ OPCODE(SENDB, BBB) /* R(a) = call(R(a),Syms(Bx),R(a+1),...,R(a+c),&R(a+c+1)) */
62
+ OPCODE(CALL, Z) /* R(0) = self.call(frame.argc, frame.argv) */
63
+ OPCODE(SUPER, BB) /* R(a) = super(R(a+1),... ,R(a+b+1)) */
64
+ OPCODE(ARGARY, BS) /* R(a) = argument array (16=m5:r1:m5:d1:lv4) */
65
+ OPCODE(ENTER, W) /* arg setup according to flags (23=m5:o5:r1:m5:k5:d1:b1) */
66
+ OPCODE(KEY_P, BB) /* R(a) = kdict.key?(Syms(b)) # todo */
67
+ OPCODE(KEYEND, Z) /* raise unless kdict.empty? # todo */
68
+ OPCODE(KARG, BB) /* R(a) = kdict[Syms(b)]; kdict.delete(Syms(b)) # todo */
69
+ OPCODE(RETURN, B) /* return R(a) (normal) */
70
+ OPCODE(RETURN_BLK, B) /* return R(a) (in-block return) */
71
+ OPCODE(BREAK, B) /* break R(a) */
72
+ OPCODE(BLKPUSH, BS) /* R(a) = block (16=m5:r1:m5:d1:lv4) */
73
+ OPCODE(ADD, B) /* R(a) = R(a)+R(a+1) */
74
+ OPCODE(ADDI, BB) /* R(a) = R(a)+mrb_int(c) */
75
+ OPCODE(SUB, B) /* R(a) = R(a)-R(a+1) */
76
+ OPCODE(SUBI, BB) /* R(a) = R(a)-C */
77
+ OPCODE(MUL, B) /* R(a) = R(a)*R(a+1) */
78
+ OPCODE(DIV, B) /* R(a) = R(a)/R(a+1) */
79
+ OPCODE(EQ, B) /* R(a) = R(a)==R(a+1) */
80
+ OPCODE(LT, B) /* R(a) = R(a)<R(a+1) */
81
+ OPCODE(LE, B) /* R(a) = R(a)<=R(a+1) */
82
+ OPCODE(GT, B) /* R(a) = R(a)>R(a+1) */
83
+ OPCODE(GE, B) /* R(a) = R(a)>=R(a+1) */
84
+ OPCODE(ARRAY, BB) /* R(a) = ary_new(R(a),R(a+1)..R(a+b)) */
85
+ OPCODE(ARRAY2, BBB) /* R(a) = ary_new(R(b),R(b+1)..R(b+c)) */
86
+ OPCODE(ARYCAT, B) /* ary_cat(R(a),R(a+1)) */
87
+ OPCODE(ARYPUSH, B) /* ary_push(R(a),R(a+1)) */
88
+ OPCODE(ARYDUP, B) /* R(a) = ary_dup(R(a)) */
89
+ OPCODE(AREF, BBB) /* R(a) = R(b)[c] */
90
+ OPCODE(ASET, BBB) /* R(a)[c] = R(b) */
91
+ OPCODE(APOST, BBB) /* *R(a),R(a+1)..R(a+c) = R(a)[b..] */
92
+ OPCODE(INTERN, B) /* R(a) = intern(R(a)) */
93
+ OPCODE(STRING, BB) /* R(a) = str_dup(Lit(b)) */
94
+ OPCODE(STRCAT, B) /* str_cat(R(a),R(a+1)) */
95
+ OPCODE(HASH, BB) /* R(a) = hash_new(R(a),R(a+1)..R(a+b)) */
96
+ OPCODE(HASHADD, BB) /* R(a) = hash_push(R(a),R(a+1)..R(a+b)) */
97
+ OPCODE(HASHCAT, B) /* R(a) = hash_cat(R(a),R(a+1)) */
98
+ OPCODE(LAMBDA, BB) /* R(a) = lambda(SEQ[b],L_LAMBDA) */
99
+ OPCODE(BLOCK, BB) /* R(a) = lambda(SEQ[b],L_BLOCK) */
100
+ OPCODE(METHOD, BB) /* R(a) = lambda(SEQ[b],L_METHOD) */
101
+ OPCODE(RANGE_INC, B) /* R(a) = range_new(R(a),R(a+1),FALSE) */
102
+ OPCODE(RANGE_EXC, B) /* R(a) = range_new(R(a),R(a+1),TRUE) */
103
+ OPCODE(OCLASS, B) /* R(a) = ::Object */
104
+ OPCODE(CLASS, BB) /* R(a) = newclass(R(a),Syms(b),R(a+1)) */
105
+ OPCODE(MODULE, BB) /* R(a) = newmodule(R(a),Syms(b)) */
106
+ OPCODE(EXEC, BB) /* R(a) = blockexec(R(a),SEQ[b]) */
107
+ OPCODE(DEF, BB) /* R(a).newmethod(Syms(b),R(a+1)) */
108
+ OPCODE(ALIAS, BB) /* alias_method(target_class,Syms(a),Syms(b)) */
109
+ OPCODE(UNDEF, B) /* undef_method(target_class,Syms(a)) */
110
+ OPCODE(SCLASS, B) /* R(a) = R(a).singleton_class */
111
+ OPCODE(TCLASS, B) /* R(a) = target_class */
112
+ OPCODE(DEBUG, BBB) /* print a,b,c */
113
+ OPCODE(ERR, B) /* raise(LocalJumpError, Lit(a)) */
114
+ OPCODE(EXT1, Z) /* make 1st operand 16bit */
115
+ OPCODE(EXT2, Z) /* make 2nd operand 16bit */
116
+ OPCODE(EXT3, Z) /* make 1st and 2nd operands 16bit */
117
+ OPCODE(STOP, Z) /* stop VM */
@@ -0,0 +1,131 @@
1
+ /*
2
+ ** mruby/proc.h - Proc class
3
+ **
4
+ ** See Copyright Notice in mruby.h
5
+ */
6
+
7
+ #ifndef MRUBY_PROC_H
8
+ #define MRUBY_PROC_H
9
+
10
+ #include "common.h"
11
+ #include <mruby/irep.h>
12
+
13
+ /**
14
+ * Proc class
15
+ */
16
+ MRB_BEGIN_DECL
17
+
18
+ struct REnv {
19
+ MRB_OBJECT_HEADER;
20
+ mrb_value *stack;
21
+ struct mrb_context *cxt;
22
+ mrb_sym mid;
23
+ };
24
+
25
+ /* flags (21bits): 1(shared flag):10(cioff/bidx):10(stack_len) */
26
+ #define MRB_ENV_SET_STACK_LEN(e,len) ((e)->flags = (((e)->flags & ~0x3ff)|((unsigned int)(len) & 0x3ff)))
27
+ #define MRB_ENV_STACK_LEN(e) ((mrb_int)((e)->flags & 0x3ff))
28
+ #define MRB_ENV_STACK_UNSHARED (1<<20)
29
+ #define MRB_ENV_UNSHARE_STACK(e) ((e)->flags |= MRB_ENV_STACK_UNSHARED)
30
+ #define MRB_ENV_STACK_SHARED_P(e) (((e)->flags & MRB_ENV_STACK_UNSHARED) == 0)
31
+ #define MRB_ENV_BIDX(e) (((e)->flags >> 10) & 0x3ff)
32
+ #define MRB_ENV_SET_BIDX(e,idx) ((e)->flags = (((e)->flags & ~(0x3ff<<10))|((unsigned int)(idx) & 0x3ff)<<10))
33
+
34
+ void mrb_env_unshare(mrb_state*, struct REnv*);
35
+
36
+ struct RProc {
37
+ MRB_OBJECT_HEADER;
38
+ union {
39
+ mrb_irep *irep;
40
+ mrb_func_t func;
41
+ } body;
42
+ struct RProc *upper;
43
+ union {
44
+ struct RClass *target_class;
45
+ struct REnv *env;
46
+ } e;
47
+ };
48
+
49
+ /* aspec access */
50
+ #define MRB_ASPEC_REQ(a) (((a) >> 18) & 0x1f)
51
+ #define MRB_ASPEC_OPT(a) (((a) >> 13) & 0x1f)
52
+ #define MRB_ASPEC_REST(a) (((a) >> 12) & 0x1)
53
+ #define MRB_ASPEC_POST(a) (((a) >> 7) & 0x1f)
54
+ #define MRB_ASPEC_KEY(a) (((a) >> 2) & 0x1f)
55
+ #define MRB_ASPEC_KDICT(a) ((a) & (1<<1))
56
+ #define MRB_ASPEC_BLOCK(a) ((a) & 1)
57
+
58
+ #define MRB_PROC_CFUNC_FL 128
59
+ #define MRB_PROC_CFUNC_P(p) (((p)->flags & MRB_PROC_CFUNC_FL) != 0)
60
+ #define MRB_PROC_CFUNC(p) (p)->body.func
61
+ #define MRB_PROC_STRICT 256
62
+ #define MRB_PROC_STRICT_P(p) (((p)->flags & MRB_PROC_STRICT) != 0)
63
+ #define MRB_PROC_ORPHAN 512
64
+ #define MRB_PROC_ORPHAN_P(p) (((p)->flags & MRB_PROC_ORPHAN) != 0)
65
+ #define MRB_PROC_ENVSET 1024
66
+ #define MRB_PROC_ENV_P(p) (((p)->flags & MRB_PROC_ENVSET) != 0)
67
+ #define MRB_PROC_ENV(p) (MRB_PROC_ENV_P(p) ? (p)->e.env : NULL)
68
+ #define MRB_PROC_TARGET_CLASS(p) (MRB_PROC_ENV_P(p) ? (p)->e.env->c : (p)->e.target_class)
69
+ #define MRB_PROC_SET_TARGET_CLASS(p,tc) do {\
70
+ if (MRB_PROC_ENV_P(p)) {\
71
+ (p)->e.env->c = (tc);\
72
+ mrb_field_write_barrier(mrb, (struct RBasic*)(p)->e.env, (struct RBasic*)(tc));\
73
+ }\
74
+ else {\
75
+ (p)->e.target_class = (tc);\
76
+ mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)(tc));\
77
+ }\
78
+ } while (0)
79
+ #define MRB_PROC_SCOPE 2048
80
+ #define MRB_PROC_SCOPE_P(p) (((p)->flags & MRB_PROC_SCOPE) != 0)
81
+
82
+ #define mrb_proc_ptr(v) ((struct RProc*)(mrb_ptr(v)))
83
+
84
+ struct RProc *mrb_proc_new(mrb_state*, mrb_irep*);
85
+ struct RProc *mrb_closure_new(mrb_state*, mrb_irep*);
86
+ MRB_API struct RProc *mrb_proc_new_cfunc(mrb_state*, mrb_func_t);
87
+ MRB_API struct RProc *mrb_closure_new_cfunc(mrb_state *mrb, mrb_func_t func, int nlocals);
88
+ void mrb_proc_copy(struct RProc *a, struct RProc *b);
89
+
90
+ /* implementation of #send method */
91
+ mrb_value mrb_f_send(mrb_state *mrb, mrb_value self);
92
+
93
+ /* following functions are defined in mruby-proc-ext so please include it when using */
94
+ MRB_API struct RProc *mrb_proc_new_cfunc_with_env(mrb_state*, mrb_func_t, mrb_int, const mrb_value*);
95
+ MRB_API mrb_value mrb_proc_cfunc_env_get(mrb_state*, mrb_int);
96
+ /* old name */
97
+ #define mrb_cfunc_env_get(mrb, idx) mrb_proc_cfunc_env_get(mrb, idx)
98
+
99
+ #ifdef MRB_METHOD_TABLE_INLINE
100
+
101
+ #define MRB_METHOD_FUNC_FL ((uintptr_t)1)
102
+ #define MRB_METHOD_FUNC_P(m) (((uintptr_t)(m))&MRB_METHOD_FUNC_FL)
103
+ #define MRB_METHOD_FUNC(m) ((mrb_func_t)((uintptr_t)(m)&(~MRB_METHOD_FUNC_FL)))
104
+ #define MRB_METHOD_FROM_FUNC(m,fn) ((m)=(mrb_method_t)((struct RProc*)((uintptr_t)(fn)|MRB_METHOD_FUNC_FL)))
105
+ #define MRB_METHOD_FROM_PROC(m,pr) ((m)=(mrb_method_t)(struct RProc*)(pr))
106
+ #define MRB_METHOD_PROC_P(m) (!MRB_METHOD_FUNC_P(m))
107
+ #define MRB_METHOD_PROC(m) ((struct RProc*)(m))
108
+ #define MRB_METHOD_UNDEF_P(m) ((m)==0)
109
+
110
+ #else
111
+
112
+ #define MRB_METHOD_FUNC_P(m) ((m).func_p)
113
+ #define MRB_METHOD_FUNC(m) ((m).func)
114
+ #define MRB_METHOD_FROM_FUNC(m,fn) do{(m).func_p=TRUE;(m).func=(fn);}while(0)
115
+ #define MRB_METHOD_FROM_PROC(m,pr) do{(m).func_p=FALSE;(m).proc=(pr);}while(0)
116
+ #define MRB_METHOD_PROC_P(m) (!MRB_METHOD_FUNC_P(m))
117
+ #define MRB_METHOD_PROC(m) ((m).proc)
118
+ #define MRB_METHOD_UNDEF_P(m) ((m).proc==NULL)
119
+
120
+ #endif /* MRB_METHOD_TABLE_INLINE */
121
+
122
+ #define MRB_METHOD_CFUNC_P(m) (MRB_METHOD_FUNC_P(m)?TRUE:(MRB_METHOD_PROC(m)?(MRB_PROC_CFUNC_P(MRB_METHOD_PROC(m))):FALSE))
123
+ #define MRB_METHOD_CFUNC(m) (MRB_METHOD_FUNC_P(m)?MRB_METHOD_FUNC(m):((MRB_METHOD_PROC(m)&&MRB_PROC_CFUNC_P(MRB_METHOD_PROC(m)))?MRB_PROC_CFUNC(MRB_METHOD_PROC(m)):NULL))
124
+
125
+
126
+ #include <mruby/khash.h>
127
+ KHASH_DECLARE(mt, mrb_sym, mrb_method_t, TRUE)
128
+
129
+ MRB_END_DECL
130
+
131
+ #endif /* MRUBY_PROC_H */