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
39526074
Commit
39526074
authored
Jun 01, 2018
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Instead of defining `Hash#dup`, we should define `Hash#initialize_copy`.
`Hash#clone` did not work properly; fix #4030
parent
f408143c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
41 deletions
+48
-41
src/hash.c
src/hash.c
+48
-41
No files found.
src/hash.c
View file @
39526074
...
@@ -161,6 +161,53 @@ mrb_hash_new(mrb_state *mrb)
...
@@ -161,6 +161,53 @@ mrb_hash_new(mrb_state *mrb)
static
mrb_value
mrb_hash_default
(
mrb_state
*
mrb
,
mrb_value
hash
);
static
mrb_value
mrb_hash_default
(
mrb_state
*
mrb
,
mrb_value
hash
);
static
mrb_value
hash_default
(
mrb_state
*
mrb
,
mrb_value
hash
,
mrb_value
key
);
static
mrb_value
hash_default
(
mrb_state
*
mrb
,
mrb_value
hash
,
mrb_value
key
);
static
mrb_value
mrb_hash_init_copy
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_value
orig
;
struct
RHash
*
copy
;
khash_t
(
ht
)
*
orig_h
;
mrb_value
ifnone
,
vret
;
mrb_get_args
(
mrb
,
"o"
,
&
orig
);
if
(
mrb_obj_equal
(
mrb
,
self
,
orig
))
return
self
;
if
((
mrb_type
(
self
)
!=
mrb_type
(
orig
))
||
(
mrb_obj_class
(
mrb
,
self
)
!=
mrb_obj_class
(
mrb
,
orig
)))
{
mrb_raise
(
mrb
,
E_TYPE_ERROR
,
"initialize_copy should take same class object"
);
}
orig_h
=
RHASH_TBL
(
self
);
copy
=
(
struct
RHash
*
)
mrb_obj_alloc
(
mrb
,
MRB_TT_HASH
,
mrb
->
hash_class
);
copy
->
ht
=
kh_init
(
ht
,
mrb
);
if
(
orig_h
&&
kh_size
(
orig_h
)
>
0
)
{
khash_t
(
ht
)
*
copy_h
=
copy
->
ht
;
khiter_t
k
,
copy_k
;
for
(
k
=
kh_begin
(
orig_h
);
k
!=
kh_end
(
orig_h
);
k
++
)
{
if
(
kh_exist
(
orig_h
,
k
))
{
int
ai
=
mrb_gc_arena_save
(
mrb
);
copy_k
=
kh_put
(
ht
,
mrb
,
copy_h
,
KEY
(
kh_key
(
orig_h
,
k
)));
mrb_gc_arena_restore
(
mrb
,
ai
);
kh_val
(
copy_h
,
copy_k
).
v
=
kh_val
(
orig_h
,
k
).
v
;
kh_val
(
copy_h
,
copy_k
).
n
=
kh_size
(
copy_h
)
-
1
;
}
}
}
if
(
MRB_RHASH_DEFAULT_P
(
self
))
{
copy
->
flags
|=
MRB_HASH_DEFAULT
;
}
if
(
MRB_RHASH_PROCDEFAULT_P
(
self
))
{
copy
->
flags
|=
MRB_HASH_PROC_DEFAULT
;
}
vret
=
mrb_obj_value
(
copy
);
ifnone
=
RHASH_IFNONE
(
self
);
if
(
!
mrb_nil_p
(
ifnone
))
{
mrb_iv_set
(
mrb
,
vret
,
mrb_intern_lit
(
mrb
,
"ifnone"
),
ifnone
);
}
return
vret
;
}
MRB_API
mrb_value
MRB_API
mrb_value
mrb_hash_get
(
mrb_state
*
mrb
,
mrb_value
hash
,
mrb_value
key
)
mrb_hash_get
(
mrb_state
*
mrb
,
mrb_value
hash
,
mrb_value
key
)
{
{
...
@@ -225,46 +272,6 @@ mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val)
...
@@ -225,46 +272,6 @@ mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val)
return
;
return
;
}
}
static
mrb_value
mrb_hash_dup
(
mrb_state
*
mrb
,
mrb_value
hash
)
{
struct
RHash
*
ret
;
khash_t
(
ht
)
*
h
,
*
ret_h
;
khiter_t
k
,
ret_k
;
mrb_value
ifnone
,
vret
;
h
=
RHASH_TBL
(
hash
);
ret
=
(
struct
RHash
*
)
mrb_obj_alloc
(
mrb
,
MRB_TT_HASH
,
mrb
->
hash_class
);
ret
->
ht
=
kh_init
(
ht
,
mrb
);
if
(
h
&&
kh_size
(
h
)
>
0
)
{
ret_h
=
ret
->
ht
;
for
(
k
=
kh_begin
(
h
);
k
!=
kh_end
(
h
);
k
++
)
{
if
(
kh_exist
(
h
,
k
))
{
int
ai
=
mrb_gc_arena_save
(
mrb
);
ret_k
=
kh_put
(
ht
,
mrb
,
ret_h
,
KEY
(
kh_key
(
h
,
k
)));
mrb_gc_arena_restore
(
mrb
,
ai
);
kh_val
(
ret_h
,
ret_k
).
v
=
kh_val
(
h
,
k
).
v
;
kh_val
(
ret_h
,
ret_k
).
n
=
kh_size
(
ret_h
)
-
1
;
}
}
}
if
(
MRB_RHASH_DEFAULT_P
(
hash
))
{
ret
->
flags
|=
MRB_HASH_DEFAULT
;
}
if
(
MRB_RHASH_PROCDEFAULT_P
(
hash
))
{
ret
->
flags
|=
MRB_HASH_PROC_DEFAULT
;
}
vret
=
mrb_obj_value
(
ret
);
ifnone
=
RHASH_IFNONE
(
hash
);
if
(
!
mrb_nil_p
(
ifnone
))
{
mrb_iv_set
(
mrb
,
vret
,
mrb_intern_lit
(
mrb
,
"ifnone"
),
ifnone
);
}
return
vret
;
}
MRB_API
mrb_value
MRB_API
mrb_value
mrb_check_hash_type
(
mrb_state
*
mrb
,
mrb_value
hash
)
mrb_check_hash_type
(
mrb_state
*
mrb
,
mrb_value
hash
)
{
{
...
@@ -888,6 +895,7 @@ mrb_init_hash(mrb_state *mrb)
...
@@ -888,6 +895,7 @@ mrb_init_hash(mrb_state *mrb)
mrb
->
hash_class
=
h
=
mrb_define_class
(
mrb
,
"Hash"
,
mrb
->
object_class
);
/* 15.2.13 */
mrb
->
hash_class
=
h
=
mrb_define_class
(
mrb
,
"Hash"
,
mrb
->
object_class
);
/* 15.2.13 */
MRB_SET_INSTANCE_TT
(
h
,
MRB_TT_HASH
);
MRB_SET_INSTANCE_TT
(
h
,
MRB_TT_HASH
);
mrb_define_method
(
mrb
,
h
,
"initialize_copy"
,
mrb_hash_init_copy
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
h
,
"[]"
,
mrb_hash_aget
,
MRB_ARGS_REQ
(
1
));
/* 15.2.13.4.2 */
mrb_define_method
(
mrb
,
h
,
"[]"
,
mrb_hash_aget
,
MRB_ARGS_REQ
(
1
));
/* 15.2.13.4.2 */
mrb_define_method
(
mrb
,
h
,
"[]="
,
mrb_hash_aset
,
MRB_ARGS_REQ
(
2
));
/* 15.2.13.4.3 */
mrb_define_method
(
mrb
,
h
,
"[]="
,
mrb_hash_aset
,
MRB_ARGS_REQ
(
2
));
/* 15.2.13.4.3 */
mrb_define_method
(
mrb
,
h
,
"clear"
,
mrb_hash_clear
,
MRB_ARGS_NONE
());
/* 15.2.13.4.4 */
mrb_define_method
(
mrb
,
h
,
"clear"
,
mrb_hash_clear
,
MRB_ARGS_NONE
());
/* 15.2.13.4.4 */
...
@@ -906,7 +914,6 @@ mrb_init_hash(mrb_state *mrb)
...
@@ -906,7 +914,6 @@ mrb_init_hash(mrb_state *mrb)
mrb_define_method
(
mrb
,
h
,
"length"
,
mrb_hash_size_m
,
MRB_ARGS_NONE
());
/* 15.2.13.4.20 */
mrb_define_method
(
mrb
,
h
,
"length"
,
mrb_hash_size_m
,
MRB_ARGS_NONE
());
/* 15.2.13.4.20 */
mrb_define_method
(
mrb
,
h
,
"member?"
,
mrb_hash_has_key
,
MRB_ARGS_REQ
(
1
));
/* 15.2.13.4.21 */
mrb_define_method
(
mrb
,
h
,
"member?"
,
mrb_hash_has_key
,
MRB_ARGS_REQ
(
1
));
/* 15.2.13.4.21 */
mrb_define_method
(
mrb
,
h
,
"shift"
,
mrb_hash_shift
,
MRB_ARGS_NONE
());
/* 15.2.13.4.24 */
mrb_define_method
(
mrb
,
h
,
"shift"
,
mrb_hash_shift
,
MRB_ARGS_NONE
());
/* 15.2.13.4.24 */
mrb_define_method
(
mrb
,
h
,
"dup"
,
mrb_hash_dup
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
h
,
"size"
,
mrb_hash_size_m
,
MRB_ARGS_NONE
());
/* 15.2.13.4.25 */
mrb_define_method
(
mrb
,
h
,
"size"
,
mrb_hash_size_m
,
MRB_ARGS_NONE
());
/* 15.2.13.4.25 */
mrb_define_method
(
mrb
,
h
,
"store"
,
mrb_hash_aset
,
MRB_ARGS_REQ
(
2
));
/* 15.2.13.4.26 */
mrb_define_method
(
mrb
,
h
,
"store"
,
mrb_hash_aset
,
MRB_ARGS_REQ
(
2
));
/* 15.2.13.4.26 */
mrb_define_method
(
mrb
,
h
,
"value?"
,
mrb_hash_has_value
,
MRB_ARGS_REQ
(
1
));
/* 15.2.13.4.27 */
mrb_define_method
(
mrb
,
h
,
"value?"
,
mrb_hash_has_value
,
MRB_ARGS_REQ
(
1
));
/* 15.2.13.4.27 */
...
...
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