Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
20a4e9ad
Commit
20a4e9ad
authored
Sep 23, 2015
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mruby-struct gem refactoring
parent
ad1087f5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
25 deletions
+24
-25
mrbgems/mruby-struct/src/struct.c
mrbgems/mruby-struct/src/struct.c
+24
-25
No files found.
mrbgems/mruby-struct/src/struct.c
View file @
20a4e9ad
...
@@ -40,9 +40,9 @@ struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id)
...
@@ -40,9 +40,9 @@ struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id)
}
}
static
mrb_value
static
mrb_value
mrb_struct_s_members
(
mrb_state
*
mrb
,
mrb_value
klass
)
struct_s_members
(
mrb_state
*
mrb
,
struct
RClass
*
klass
)
{
{
mrb_value
members
=
struct_ivar_get
(
mrb
,
klass
,
mrb_intern_lit
(
mrb
,
"__members__"
));
mrb_value
members
=
struct_ivar_get
(
mrb
,
mrb_obj_value
(
klass
)
,
mrb_intern_lit
(
mrb
,
"__members__"
));
if
(
mrb_nil_p
(
members
))
{
if
(
mrb_nil_p
(
members
))
{
mrb_raise
(
mrb
,
E_TYPE_ERROR
,
"uninitialized struct"
);
mrb_raise
(
mrb
,
E_TYPE_ERROR
,
"uninitialized struct"
);
...
@@ -54,15 +54,16 @@ mrb_struct_s_members(mrb_state *mrb, mrb_value klass)
...
@@ -54,15 +54,16 @@ mrb_struct_s_members(mrb_state *mrb, mrb_value klass)
}
}
static
mrb_value
static
mrb_value
mrb_struct_members
(
mrb_state
*
mrb
,
mrb_value
s
)
struct_members
(
mrb_state
*
mrb
,
mrb_value
s
)
{
{
mrb_value
members
=
mrb_struct_s_members
(
mrb
,
mrb_obj_value
(
mrb_obj_class
(
mrb
,
s
)));
mrb_value
members
=
struct_s_members
(
mrb
,
mrb_obj_class
(
mrb
,
s
));
if
(
!
strcmp
(
mrb_class_name
(
mrb
,
mrb_obj_class
(
mrb
,
s
)),
"Struct"
))
{
if
(
!
mrb_array_p
(
s
))
{
if
(
RSTRUCT_LEN
(
s
)
!=
RARRAY_LEN
(
members
))
{
mrb_raise
(
mrb
,
E_TYPE_ERROR
,
"corrupted struct"
);
mrb_raisef
(
mrb
,
E_TYPE_ERROR
,
}
"struct size differs (%S required %S given)"
,
if
(
RSTRUCT_LEN
(
s
)
!=
RARRAY_LEN
(
members
))
{
mrb_fixnum_value
(
RARRAY_LEN
(
members
)),
mrb_fixnum_value
(
RSTRUCT_LEN
(
s
)));
mrb_raisef
(
mrb
,
E_TYPE_ERROR
,
}
"struct size differs (%S required %S given)"
,
mrb_fixnum_value
(
RARRAY_LEN
(
members
)),
mrb_fixnum_value
(
RSTRUCT_LEN
(
s
)));
}
}
return
members
;
return
members
;
}
}
...
@@ -72,7 +73,7 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
...
@@ -72,7 +73,7 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
{
{
mrb_value
members
,
ary
;
mrb_value
members
,
ary
;
members
=
mrb_struct_s_members
(
mrb
,
klass
);
members
=
struct_s_members
(
mrb
,
mrb_class_ptr
(
klass
)
);
ary
=
mrb_ary_new_capa
(
mrb
,
RARRAY_LEN
(
members
));
ary
=
mrb_ary_new_capa
(
mrb
,
RARRAY_LEN
(
members
));
mrb_ary_replace
(
mrb
,
ary
,
members
);
mrb_ary_replace
(
mrb
,
ary
,
members
);
return
ary
;
return
ary
;
...
@@ -92,7 +93,7 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
...
@@ -92,7 +93,7 @@ mrb_struct_s_members_m(mrb_state *mrb, mrb_value klass)
*/
*/
static
mrb_value
static
mrb_value
mrb_struct_members
_m
(
mrb_state
*
mrb
,
mrb_value
obj
)
mrb_struct_members
(
mrb_state
*
mrb
,
mrb_value
obj
)
{
{
return
mrb_struct_s_members_m
(
mrb
,
mrb_obj_value
(
mrb_obj_class
(
mrb
,
obj
)));
return
mrb_struct_s_members_m
(
mrb
,
mrb_obj_value
(
mrb_obj_class
(
mrb
,
obj
)));
}
}
...
@@ -105,7 +106,7 @@ mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id)
...
@@ -105,7 +106,7 @@ mrb_struct_getmember(mrb_state *mrb, mrb_value obj, mrb_sym id)
mrb_int
i
,
len
;
mrb_int
i
,
len
;
ptr
=
RSTRUCT_PTR
(
obj
);
ptr
=
RSTRUCT_PTR
(
obj
);
members
=
mrb_
struct_members
(
mrb
,
obj
);
members
=
struct_members
(
mrb
,
obj
);
ptr_members
=
RARRAY_PTR
(
members
);
ptr_members
=
RARRAY_PTR
(
members
);
slot
=
mrb_symbol_value
(
id
);
slot
=
mrb_symbol_value
(
id
);
len
=
RARRAY_LEN
(
members
);
len
=
RARRAY_LEN
(
members
);
...
@@ -183,7 +184,7 @@ mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val)
...
@@ -183,7 +184,7 @@ mrb_struct_set(mrb_state *mrb, mrb_value obj, mrb_value val)
name
=
mrb_sym2name_len
(
mrb
,
mrb
->
c
->
ci
->
mid
,
&
slen
);
name
=
mrb_sym2name_len
(
mrb
,
mrb
->
c
->
ci
->
mid
,
&
slen
);
mid
=
mrb_intern
(
mrb
,
name
,
slen
-
1
);
/* omit last "=" */
mid
=
mrb_intern
(
mrb
,
name
,
slen
-
1
);
/* omit last "=" */
members
=
mrb_
struct_members
(
mrb
,
obj
);
members
=
struct_members
(
mrb
,
obj
);
ptr_members
=
RARRAY_PTR
(
members
);
ptr_members
=
RARRAY_PTR
(
members
);
len
=
RARRAY_LEN
(
members
);
len
=
RARRAY_LEN
(
members
);
ptr
=
RSTRUCT_PTR
(
obj
);
ptr
=
RSTRUCT_PTR
(
obj
);
...
@@ -402,7 +403,7 @@ mrb_struct_initialize_withArg(mrb_state *mrb, mrb_int argc, mrb_value *argv, mrb
...
@@ -402,7 +403,7 @@ mrb_struct_initialize_withArg(mrb_state *mrb, mrb_int argc, mrb_value *argv, mrb
}
}
static
mrb_value
static
mrb_value
mrb_struct_initialize
_m
(
mrb_state
*
mrb
,
/*int argc, mrb_value *argv,*/
mrb_value
self
)
mrb_struct_initialize
(
mrb_state
*
mrb
,
mrb_value
self
)
{
{
mrb_value
*
argv
;
mrb_value
*
argv
;
mrb_int
argc
;
mrb_int
argc
;
...
@@ -446,7 +447,7 @@ struct_aref_sym(mrb_state *mrb, mrb_value s, mrb_sym id)
...
@@ -446,7 +447,7 @@ struct_aref_sym(mrb_state *mrb, mrb_value s, mrb_sym id)
mrb_int
i
,
len
;
mrb_int
i
,
len
;
ptr
=
RSTRUCT_PTR
(
s
);
ptr
=
RSTRUCT_PTR
(
s
);
members
=
mrb_
struct_members
(
mrb
,
s
);
members
=
struct_members
(
mrb
,
s
);
ptr_members
=
RARRAY_PTR
(
members
);
ptr_members
=
RARRAY_PTR
(
members
);
len
=
RARRAY_LEN
(
members
);
len
=
RARRAY_LEN
(
members
);
for
(
i
=
0
;
i
<
len
;
i
++
)
{
for
(
i
=
0
;
i
<
len
;
i
++
)
{
...
@@ -519,7 +520,7 @@ mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
...
@@ -519,7 +520,7 @@ mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
const
mrb_value
*
ptr_members
;
const
mrb_value
*
ptr_members
;
mrb_int
i
,
len
;
mrb_int
i
,
len
;
members
=
mrb_
struct_members
(
mrb
,
s
);
members
=
struct_members
(
mrb
,
s
);
len
=
RARRAY_LEN
(
members
);
len
=
RARRAY_LEN
(
members
);
if
(
RSTRUCT_LEN
(
s
)
!=
len
)
{
if
(
RSTRUCT_LEN
(
s
)
!=
len
)
{
mrb_raisef
(
mrb
,
E_TYPE_ERROR
,
mrb_raisef
(
mrb
,
E_TYPE_ERROR
,
...
@@ -625,8 +626,7 @@ mrb_struct_equal(mrb_state *mrb, mrb_value s)
...
@@ -625,8 +626,7 @@ mrb_struct_equal(mrb_state *mrb, mrb_value s)
if
(
mrb_obj_equal
(
mrb
,
s
,
s2
))
{
if
(
mrb_obj_equal
(
mrb
,
s
,
s2
))
{
return
mrb_true_value
();
return
mrb_true_value
();
}
}
if
(
!
strcmp
(
mrb_class_name
(
mrb
,
mrb_obj_class
(
mrb
,
s
)),
"Struct"
)
||
if
(
mrb_obj_class
(
mrb
,
s
)
!=
mrb_obj_class
(
mrb
,
s2
))
{
mrb_obj_class
(
mrb
,
s
)
!=
mrb_obj_class
(
mrb
,
s2
))
{
return
mrb_false_value
();
return
mrb_false_value
();
}
}
if
(
RSTRUCT_LEN
(
s
)
!=
RSTRUCT_LEN
(
s2
))
{
if
(
RSTRUCT_LEN
(
s
)
!=
RSTRUCT_LEN
(
s2
))
{
...
@@ -663,8 +663,7 @@ mrb_struct_eql(mrb_state *mrb, mrb_value s)
...
@@ -663,8 +663,7 @@ mrb_struct_eql(mrb_state *mrb, mrb_value s)
if
(
mrb_obj_equal
(
mrb
,
s
,
s2
))
{
if
(
mrb_obj_equal
(
mrb
,
s
,
s2
))
{
return
mrb_true_value
();
return
mrb_true_value
();
}
}
if
(
strcmp
(
mrb_class_name
(
mrb
,
mrb_obj_class
(
mrb
,
s2
)),
"Struct"
)
||
if
(
mrb_obj_class
(
mrb
,
s
)
!=
mrb_obj_class
(
mrb
,
s2
))
{
mrb_obj_class
(
mrb
,
s
)
!=
mrb_obj_class
(
mrb
,
s2
))
{
return
mrb_false_value
();
return
mrb_false_value
();
}
}
if
(
RSTRUCT_LEN
(
s
)
!=
RSTRUCT_LEN
(
s2
))
{
if
(
RSTRUCT_LEN
(
s
)
!=
RSTRUCT_LEN
(
s2
))
{
...
@@ -720,7 +719,7 @@ mrb_struct_to_h(mrb_state *mrb, mrb_value self)
...
@@ -720,7 +719,7 @@ mrb_struct_to_h(mrb_state *mrb, mrb_value self)
mrb_value
members
,
ret
;
mrb_value
members
,
ret
;
mrb_int
i
;
mrb_int
i
;
members
=
mrb_struct_s_members
(
mrb
,
mrb_obj_value
(
mrb_class
(
mrb
,
self
)
));
members
=
struct_s_members
(
mrb
,
mrb_class
(
mrb
,
self
));
ret
=
mrb_hash_new_capa
(
mrb
,
RARRAY_LEN
(
members
));
ret
=
mrb_hash_new_capa
(
mrb
,
RARRAY_LEN
(
members
));
for
(
i
=
0
;
i
<
RARRAY_LEN
(
members
);
++
i
)
{
for
(
i
=
0
;
i
<
RARRAY_LEN
(
members
);
++
i
)
{
...
@@ -767,8 +766,8 @@ mrb_mruby_struct_gem_init(mrb_state* mrb)
...
@@ -767,8 +766,8 @@ mrb_mruby_struct_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
st
,
"=="
,
mrb_struct_equal
,
MRB_ARGS_REQ
(
1
));
/* 15.2.18.4.1 */
mrb_define_method
(
mrb
,
st
,
"=="
,
mrb_struct_equal
,
MRB_ARGS_REQ
(
1
));
/* 15.2.18.4.1 */
mrb_define_method
(
mrb
,
st
,
"[]"
,
mrb_struct_aref
,
MRB_ARGS_REQ
(
1
));
/* 15.2.18.4.2 */
mrb_define_method
(
mrb
,
st
,
"[]"
,
mrb_struct_aref
,
MRB_ARGS_REQ
(
1
));
/* 15.2.18.4.2 */
mrb_define_method
(
mrb
,
st
,
"[]="
,
mrb_struct_aset
,
MRB_ARGS_REQ
(
2
));
/* 15.2.18.4.3 */
mrb_define_method
(
mrb
,
st
,
"[]="
,
mrb_struct_aset
,
MRB_ARGS_REQ
(
2
));
/* 15.2.18.4.3 */
mrb_define_method
(
mrb
,
st
,
"members"
,
mrb_struct_members
_m
,
MRB_ARGS_NONE
());
/* 15.2.18.4.6 */
mrb_define_method
(
mrb
,
st
,
"members"
,
mrb_struct_members
,
MRB_ARGS_NONE
());
/* 15.2.18.4.6 */
mrb_define_method
(
mrb
,
st
,
"initialize"
,
mrb_struct_initialize
_m
,
MRB_ARGS_ANY
());
/* 15.2.18.4.8 */
mrb_define_method
(
mrb
,
st
,
"initialize"
,
mrb_struct_initialize
,
MRB_ARGS_ANY
());
/* 15.2.18.4.8 */
mrb_define_method
(
mrb
,
st
,
"initialize_copy"
,
mrb_struct_init_copy
,
MRB_ARGS_REQ
(
1
));
/* 15.2.18.4.9 */
mrb_define_method
(
mrb
,
st
,
"initialize_copy"
,
mrb_struct_init_copy
,
MRB_ARGS_REQ
(
1
));
/* 15.2.18.4.9 */
mrb_define_method
(
mrb
,
st
,
"eql?"
,
mrb_struct_eql
,
MRB_ARGS_REQ
(
1
));
/* 15.2.18.4.12(x) */
mrb_define_method
(
mrb
,
st
,
"eql?"
,
mrb_struct_eql
,
MRB_ARGS_REQ
(
1
));
/* 15.2.18.4.12(x) */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment