Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 223 lines (194 sloc) 4.878 kb
8354436 @mimaki add file header
mimaki authored
1 /*
4ec6d41 rm whitespace
roco authored
2 ** etc.c -
3 **
8354436 @mimaki add file header
mimaki authored
4 ** See Copyright Notice in mruby.h
5 */
6
e0d6430 @mimaki add mruby sources
mimaki authored
7 #include "mruby.h"
8 #include "mruby/string.h"
514e271 @takahashim add header "mruby/data.h"
takahashim authored
9 #include "mruby/data.h"
b225e4c @matz redesign mruby/data.h API; use DATA_PTR() for raw data pointer, DATA_GET...
matz authored
10 #include "mruby/class.h"
d4a510f @take-cheeze Add mrb_regexp_p to check whether mrb_value is Regexp.
take-cheeze authored
11 #include "mruby/re.h"
30e0dc7 @take-cheeze Support `LVAR` section removing.
take-cheeze authored
12 #include "mruby/irep.h"
e0d6430 @mimaki add mruby sources
mimaki authored
13
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
14 MRB_API struct RData*
e4231a0 @monaka Add typedef to structures that have mrb_ prefix. Use typedef-ed type ins...
monaka authored
15 mrb_data_object_alloc(mrb_state *mrb, struct RClass *klass, void *ptr, const mrb_data_type *type)
e0d6430 @mimaki add mruby sources
mimaki authored
16 {
17 struct RData *data;
18
4523bee @matz cast style consistency
matz authored
19 data = (struct RData*)mrb_obj_alloc(mrb, MRB_TT_DATA, klass);
e0d6430 @mimaki add mruby sources
mimaki authored
20 data->data = ptr;
d90f52f @cremno etc.c: small cleanup (delete unused functions)
cremno authored
21 data->type = type;
e0d6430 @mimaki add mruby sources
mimaki authored
22
23 return data;
24 }
25
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
26 MRB_API void
75f5535 @matz rename DATA API: mrb_get_datatype -> mrb_data_get_ptr; mrb_check_datatyp...
matz authored
27 mrb_data_check_type(mrb_state *mrb, mrb_value obj, const mrb_data_type *type)
e0460c1 @matz hash refactored
matz authored
28 {
6aa6e75 @matz rename obsolete mrb_special_const_p to mrb_immediate_p
matz authored
29 if (mrb_immediate_p(obj) || (mrb_type(obj) != MRB_TT_DATA)) {
75f5535 @matz rename DATA API: mrb_get_datatype -> mrb_data_get_ptr; mrb_check_datatyp...
matz authored
30 mrb_check_type(mrb, obj, MRB_TT_DATA);
e0460c1 @matz hash refactored
matz authored
31 }
32 if (DATA_TYPE(obj) != type) {
75f5535 @matz rename DATA API: mrb_get_datatype -> mrb_data_get_ptr; mrb_check_datatyp...
matz authored
33 const mrb_data_type *t2 = DATA_TYPE(obj);
34
35 if (t2) {
36 mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %S (expected %S)",
37 mrb_str_new_cstr(mrb, t2->struct_name), mrb_str_new_cstr(mrb, type->struct_name));
38 }
b225e4c @matz redesign mruby/data.h API; use DATA_PTR() for raw data pointer, DATA_GET...
matz authored
39 else {
40 struct RClass *c = mrb_class(mrb, obj);
75f5535 @matz rename DATA API: mrb_get_datatype -> mrb_data_get_ptr; mrb_check_datatyp...
matz authored
41
b225e4c @matz redesign mruby/data.h API; use DATA_PTR() for raw data pointer, DATA_GET...
matz authored
42 mrb_raisef(mrb, E_TYPE_ERROR, "uninitialized %S (expected %S)",
43 mrb_obj_value(c), mrb_str_new_cstr(mrb, type->struct_name));
44 }
45 }
e0460c1 @matz hash refactored
matz authored
46 }
47
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
48 MRB_API void*
b225e4c @matz redesign mruby/data.h API; use DATA_PTR() for raw data pointer, DATA_GET...
matz authored
49 mrb_data_check_get_ptr(mrb_state *mrb, mrb_value obj, const mrb_data_type *type)
e0d6430 @mimaki add mruby sources
mimaki authored
50 {
6aa6e75 @matz rename obsolete mrb_special_const_p to mrb_immediate_p
matz authored
51 if (mrb_immediate_p(obj) || (mrb_type(obj) != MRB_TT_DATA)) {
75f5535 @matz rename DATA API: mrb_get_datatype -> mrb_data_get_ptr; mrb_check_datatyp...
matz authored
52 return NULL;
e0d6430 @mimaki add mruby sources
mimaki authored
53 }
54 if (DATA_TYPE(obj) != type) {
75f5535 @matz rename DATA API: mrb_get_datatype -> mrb_data_get_ptr; mrb_check_datatyp...
matz authored
55 return NULL;
e0d6430 @mimaki add mruby sources
mimaki authored
56 }
57 return DATA_PTR(obj);
58 }
59
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
60 MRB_API void*
b225e4c @matz redesign mruby/data.h API; use DATA_PTR() for raw data pointer, DATA_GET...
matz authored
61 mrb_data_get_ptr(mrb_state *mrb, mrb_value obj, const mrb_data_type *type)
62 {
63 mrb_data_check_type(mrb, obj, type);
64 return DATA_PTR(obj);
65 }
66
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
67 MRB_API mrb_sym
02f843d @matz rename mrb_to_id to mrb_obj_to_sym since symbol is nothing to do with id...
matz authored
68 mrb_obj_to_sym(mrb_state *mrb, mrb_value name)
e0d6430 @mimaki add mruby sources
mimaki authored
69 {
70 mrb_value tmp;
71 mrb_sym id;
72
73 switch (mrb_type(name)) {
74 default:
75 tmp = mrb_check_string_type(mrb, name);
76 if (mrb_nil_p(tmp)) {
77 tmp = mrb_inspect(mrb, name);
18b2683 @matz use new mrb_format API from mrb_raisef; its only format specifier is "%S...
matz authored
78 mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a symbol", tmp);
e0d6430 @mimaki add mruby sources
mimaki authored
79 }
80 name = tmp;
81 /* fall through */
82 case MRB_TT_STRING:
83 name = mrb_str_intern(mrb, name);
84 /* fall through */
85 case MRB_TT_SYMBOL:
d90f52f @cremno etc.c: small cleanup (delete unused functions)
cremno authored
86 id = mrb_symbol(name);
e0d6430 @mimaki add mruby sources
mimaki authored
87 }
88 return id;
89 }
90
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
91 MRB_API mrb_int
62505d5 @matz calc hash value directly for strings, symbols and numbers
matz authored
92 mrb_float_id(mrb_float f)
5b3cc4a @matz better object_id calculation
matz authored
93 {
94 const char *p = (const char*)&f;
95 int len = sizeof(f);
96 mrb_int id = 0;
97
98 while (len--) {
99 id = id*65599 + *p;
100 p++;
101 }
102 id = id + (id>>5);
103
104 return id;
105 }
106
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
107 MRB_API mrb_int
e0d6430 @mimaki add mruby sources
mimaki authored
108 mrb_obj_id(mrb_value obj)
109 {
7d02df3 @matz NaN boxing
matz authored
110 mrb_int tt = mrb_type(obj);
5b3cc4a @matz better object_id calculation
matz authored
111
48e82d7 @matz cast intptr_t to mrb_int to pacify compiler warning; close #2148
matz authored
112 #define MakeID2(p,t) (mrb_int)(((intptr_t)(p))^(t))
5b3cc4a @matz better object_id calculation
matz authored
113 #define MakeID(p) MakeID2(p,tt)
114
115 switch (tt) {
d94469a @kkkkkt changed to call check_cv_name_str in check_cv_name_sym and adjust indent
kkkkkt authored
116 case MRB_TT_FREE:
117 case MRB_TT_UNDEF:
5b3cc4a @matz better object_id calculation
matz authored
118 return MakeID(0); /* not define */
d94469a @kkkkkt changed to call check_cv_name_str in check_cv_name_sym and adjust indent
kkkkkt authored
119 case MRB_TT_FALSE:
5b3cc4a @matz better object_id calculation
matz authored
120 if (mrb_nil_p(obj))
121 return MakeID(1);
122 return MakeID(0);
d94469a @kkkkkt changed to call check_cv_name_str in check_cv_name_sym and adjust indent
kkkkkt authored
123 case MRB_TT_TRUE:
5b3cc4a @matz better object_id calculation
matz authored
124 return MakeID(1);
d94469a @kkkkkt changed to call check_cv_name_str in check_cv_name_sym and adjust indent
kkkkkt authored
125 case MRB_TT_SYMBOL:
e429905 @matz replace SYM2ID() by mrb_symbol()
matz authored
126 return MakeID(mrb_symbol(obj));
d94469a @kkkkkt changed to call check_cv_name_str in check_cv_name_sym and adjust indent
kkkkkt authored
127 case MRB_TT_FIXNUM:
62505d5 @matz calc hash value directly for strings, symbols and numbers
matz authored
128 return MakeID2(mrb_float_id((mrb_float)mrb_fixnum(obj)), MRB_TT_FLOAT);
d94469a @kkkkkt changed to call check_cv_name_str in check_cv_name_sym and adjust indent
kkkkkt authored
129 case MRB_TT_FLOAT:
62505d5 @matz calc hash value directly for strings, symbols and numbers
matz authored
130 return MakeID(mrb_float_id(mrb_float(obj)));
d94469a @kkkkkt changed to call check_cv_name_str in check_cv_name_sym and adjust indent
kkkkkt authored
131 case MRB_TT_STRING:
132 case MRB_TT_OBJECT:
133 case MRB_TT_CLASS:
134 case MRB_TT_MODULE:
135 case MRB_TT_ICLASS:
136 case MRB_TT_SCLASS:
137 case MRB_TT_PROC:
138 case MRB_TT_ARRAY:
139 case MRB_TT_HASH:
140 case MRB_TT_RANGE:
141 case MRB_TT_EXCEPTION:
142 case MRB_TT_FILE:
143 case MRB_TT_DATA:
5b3cc4a @matz better object_id calculation
matz authored
144 default:
40e6cdc @matz rename mrb_value_p() to mrb_ptr() since _p means predicate in mruby sour...
matz authored
145 return MakeID(mrb_ptr(obj));
e0d6430 @mimaki add mruby sources
mimaki authored
146 }
147 }
148
e720782 @kimushu Add MRB_WORD_BOXING mode (represent mrb_value as a word)
kimushu authored
149 #ifdef MRB_WORD_BOXING
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
150 MRB_API mrb_value
61bba9a @matz rename word boxing functions
matz authored
151 mrb_word_boxing_float_value(mrb_state *mrb, mrb_float f)
e720782 @kimushu Add MRB_WORD_BOXING mode (represent mrb_value as a word)
kimushu authored
152 {
153 mrb_value v;
154
155 v.value.p = mrb_obj_alloc(mrb, MRB_TT_FLOAT, mrb->float_class);
156 v.value.fp->f = f;
157 return v;
158 }
159
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
160 MRB_API mrb_value
61bba9a @matz rename word boxing functions
matz authored
161 mrb_word_boxing_float_pool(mrb_state *mrb, mrb_float f)
9ac3869 @matz float objects in pool are objects when MRB_WORD_BOXING is set
matz authored
162 {
163 struct RFloat *nf = (struct RFloat *)mrb_malloc(mrb, sizeof(struct RFloat));
164 nf->tt = MRB_TT_FLOAT;
165 nf->c = mrb->float_class;
166 nf->f = f;
167 return mrb_obj_value(nf);
168 }
169
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
170 MRB_API mrb_value
61bba9a @matz rename word boxing functions
matz authored
171 mrb_word_boxing_cptr_value(mrb_state *mrb, void *p)
e720782 @kimushu Add MRB_WORD_BOXING mode (represent mrb_value as a word)
kimushu authored
172 {
173 mrb_value v;
174
3995e7f @matz rename voidp to cptr
matz authored
175 v.value.p = mrb_obj_alloc(mrb, MRB_TT_CPTR, mrb->object_class);
e720782 @kimushu Add MRB_WORD_BOXING mode (represent mrb_value as a word)
kimushu authored
176 v.value.vp->p = p;
177 return v;
178 }
179 #endif /* MRB_WORD_BOXING */
180
206f89e @matz add MRB_API modifiers to mruby API functions
matz authored
181 MRB_API mrb_bool
d4a510f @take-cheeze Add mrb_regexp_p to check whether mrb_value is Regexp.
take-cheeze authored
182 mrb_regexp_p(mrb_state *mrb, mrb_value v)
183 {
184 return mrb_class_defined(mrb, REGEXP_CLASS) && mrb_obj_is_kind_of(mrb, v, mrb_class_get(mrb, REGEXP_CLASS));
185 }
b6e2721 @cremno MSVC: add simple (v)snprintf implementation
cremno authored
186
187 #if defined _MSC_VER && _MSC_VER < 1900
188
189 #ifndef va_copy
190 static void
191 mrb_msvc_va_copy(va_list *dest, va_list src)
192 {
193 *dest = src;
194 }
d95f77c @bggd Fix a typo in etc.c
bggd authored
195 #define va_copy(dest, src) mrb_msvc_va_copy(&(dest), src)
b6e2721 @cremno MSVC: add simple (v)snprintf implementation
cremno authored
196 #endif
197
198 MRB_API int
199 mrb_msvc_vsnprintf(char *s, size_t n, const char *format, va_list arg)
200 {
201 int cnt;
202 va_list argcp;
203 va_copy(argcp, arg);
204 if (n == 0 || (cnt = _vsnprintf_s(s, n, _TRUNCATE, format, argcp)) < 0) {
205 cnt = _vscprintf(format, arg);
206 }
207 va_end(argcp);
208 return cnt;
209 }
210
211 MRB_API int
212 mrb_msvc_snprintf(char *s, size_t n, const char *format, ...)
213 {
214 va_list arg;
215 int ret;
216 va_start(arg, format);
217 ret = mrb_msvc_vsnprintf(s, n, format, arg);
218 va_end(arg);
219 return ret;
220 }
221
222 #endif /* defined _MSC_VER && _MSC_VER < 1900 */
Something went wrong with that request. Please try again.