Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 190 lines (166 sloc) 4.005 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"
9 #include "error.h"
10 #include "mruby/numeric.h"
514e271 @takahashim add header "mruby/data.h"
takahashim authored
11 #include "mruby/data.h"
e0d6430 @mimaki add mruby sources
mimaki authored
12
13 struct RData*
14 mrb_data_object_alloc(mrb_state *mrb, struct RClass *klass, void *ptr, const struct mrb_data_type *type)
15 {
16 struct RData *data;
17
4523bee @matz cast style consistency
matz authored
18 data = (struct RData*)mrb_obj_alloc(mrb, MRB_TT_DATA, klass);
e0d6430 @mimaki add mruby sources
mimaki authored
19 data->data = ptr;
4523bee @matz cast style consistency
matz authored
20 data->type = (struct mrb_data_type*) type;
e0d6430 @mimaki add mruby sources
mimaki authored
21
22 return data;
23 }
24
25 void *
e0460c1 @matz hash refactored
matz authored
26 mrb_get_datatype(mrb_state *mrb, mrb_value obj, const struct mrb_data_type *type)
27 {
28 if (SPECIAL_CONST_P(obj) || (mrb_type(obj) != MRB_TT_DATA)) {
29 return NULL;
30 }
31 if (DATA_TYPE(obj) != type) {
32 return NULL;
33 }
34 return DATA_PTR(obj);
35 }
36
37 void *
e0d6430 @mimaki add mruby sources
mimaki authored
38 mrb_check_datatype(mrb_state *mrb, mrb_value obj, const struct mrb_data_type *type)
39 {
40 static const char mesg[] = "wrong argument type %s (expected %s)";
41
42 if (SPECIAL_CONST_P(obj) || (mrb_type(obj) != MRB_TT_DATA)) {
43 mrb_check_type(mrb, obj, MRB_TT_DATA);
44 }
45 if (DATA_TYPE(obj) != type) {
46 const char *etype = DATA_TYPE(obj)->struct_name;
47 mrb_raise(mrb, E_TYPE_ERROR, mesg, etype, type->struct_name);
48 }
49 return DATA_PTR(obj);
50 }
51
52 mrb_value
53 mrb_lastline_get(mrb_state *mrb)
54 {
55 //mrb_value *var = mrb_svar(0);
56 //if (var) {
57 // return *var;
58 //}
59 //return mrb_nil_value();
60 mrb_value *argv;
61 int argc;
ad9e841 @matz made mrb_get_args() better (optinal args, type checks); close #173 #176
matz authored
62
e0d6430 @mimaki add mruby sources
mimaki authored
63 mrb_get_args(mrb, "*", &argv, &argc);
64 if (argc < 1) {
65 return mrb_nil_value();
66 }
67 else
68 {
69 return argv[0];
70 }
71 }
72
73 /* ------------------------------------------------ */
74 /*
75 * Calls func(obj, arg, recursive), where recursive is non-zero if the
76 * current method is called recursively on obj
77 */
78
79 mrb_value
80 mrb_exec_recursive(mrb_state *mrb, mrb_value (*func) (mrb_state *, mrb_value, mrb_value, int), mrb_value obj, void *arg)
81 {
82 // return mrb_exec_recursive(mrb, io_puts_ary, line, &out);
83 return func(mrb, obj, *(mrb_value*)arg, 0);
84 }
85
86 /*
87 * Calls func(obj, arg, recursive), where recursive is non-zero if the
88 * current method is called recursively on the ordered pair <obj, paired_obj>
89 */
90
91 mrb_sym
92 mrb_to_id(mrb_state *mrb, mrb_value name)
93 {
94 mrb_value tmp;
95 mrb_sym id;
96
97 switch (mrb_type(name)) {
98 default:
99 tmp = mrb_check_string_type(mrb, name);
100 if (mrb_nil_p(tmp)) {
101 tmp = mrb_inspect(mrb, name);
102 mrb_raise(mrb, E_TYPE_ERROR, "%s is not a symbol",
103 RSTRING_PTR(tmp));
104 }
105 name = tmp;
106 /* fall through */
107 case MRB_TT_STRING:
108 name = mrb_str_intern(mrb, name);
109 /* fall through */
110 case MRB_TT_SYMBOL:
111 return SYM2ID(name);
112 }
113 return id;
114 }
115
116 /*
117 * call-seq:
118 * proc { |...| block } -> a_proc
119 *
120 * Equivalent to <code>Proc.new</code>.
121 */
122
123 mrb_value
124 mrb_block_proc(void)
125 {
126 return mrb_nil_value();//proc_new(mrb_cProc, FALSE);
127 }
128
5b3cc4a @matz better object_id calculation
matz authored
129 static mrb_int
130 float_id(mrb_float f)
131 {
132 const char *p = (const char*)&f;
133 int len = sizeof(f);
134 mrb_int id = 0;
135
136 while (len--) {
137 id = id*65599 + *p;
138 p++;
139 }
140 id = id + (id>>5);
141
142 return id;
143 }
144
ff42f8f @matz move RDoc comments
matz authored
145 mrb_int
e0d6430 @mimaki add mruby sources
mimaki authored
146 mrb_obj_id(mrb_value obj)
147 {
5b3cc4a @matz better object_id calculation
matz authored
148 mrb_int tt = obj.tt;
149
150 #define MakeID2(p,t) (((intptr_t)(p))^(t))
151 #define MakeID(p) MakeID2(p,tt)
152
153 switch (tt) {
154 case MRB_TT_FREE:
155 case MRB_TT_UNDEF:
156 return MakeID(0); /* not define */
157 case MRB_TT_FALSE:
158 if (mrb_nil_p(obj))
159 return MakeID(1);
160 return MakeID(0);
161 case MRB_TT_TRUE:
162 return MakeID(1);
163 case MRB_TT_SYMBOL:
164 return MakeID(SYM2ID(obj));
165 case MRB_TT_FIXNUM:
166 return MakeID2(float_id((mrb_float)mrb_fixnum(obj)), MRB_TT_FLOAT);
167 case MRB_TT_FLOAT:
168 return MakeID(float_id(mrb_float(obj)));
169 case MRB_TT_STRING:
170 case MRB_TT_OBJECT:
171 case MRB_TT_CLASS:
172 case MRB_TT_MODULE:
173 case MRB_TT_ICLASS:
174 case MRB_TT_SCLASS:
175 case MRB_TT_PROC:
176 case MRB_TT_ARRAY:
177 case MRB_TT_HASH:
178 case MRB_TT_RANGE:
179 case MRB_TT_REGEX:
180 case MRB_TT_STRUCT:
181 case MRB_TT_EXCEPTION:
182 case MRB_TT_MATCH:
183 case MRB_TT_FILE:
184 case MRB_TT_DATA:
185 default:
186 return MakeID(obj.value.p);
e0d6430 @mimaki add mruby sources
mimaki authored
187 }
188 }
189
Something went wrong with that request. Please try again.