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
e5922aa8
Commit
e5922aa8
authored
Mar 25, 2014
by
take_cheeze
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement ObjectSpace.each_object .
parent
13f0f597
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
0 deletions
+59
-0
mrbgems/mruby-objectspace/src/mruby_objectspace.c
mrbgems/mruby-objectspace/src/mruby_objectspace.c
+44
-0
mrbgems/mruby-objectspace/test/objectspace.rb
mrbgems/mruby-objectspace/test/objectspace.rb
+15
-0
No files found.
mrbgems/mruby-objectspace/src/mruby_objectspace.c
View file @
e5922aa8
#include <mruby.h>
#include <mruby.h>
#include <mruby/gc.h>
#include <mruby/gc.h>
#include <mruby/hash.h>
#include <mruby/hash.h>
#include <mruby/class.h>
struct
os_count_struct
{
struct
os_count_struct
{
mrb_int
total
;
mrb_int
total
;
...
@@ -100,11 +101,54 @@ os_count_objects(mrb_state *mrb, mrb_value self)
...
@@ -100,11 +101,54 @@ os_count_objects(mrb_state *mrb, mrb_value self)
return
hash
;
return
hash
;
}
}
struct
os_each_object_data
{
mrb_value
block
;
struct
RClass
*
target_module
;
int
count
;
};
static
void
os_each_object_cb
(
mrb_state
*
mrb
,
struct
RBasic
*
obj
,
void
*
ud
)
{
struct
os_each_object_data
*
d
=
(
struct
os_each_object_data
*
)
ud
;
/* filter dead objects */
if
(
is_dead
(
mrb
,
obj
))
{
return
;
}
/* filter class kind if target module defined */
if
(
d
->
target_module
&&
!
mrb_obj_is_kind_of
(
mrb
,
mrb_obj_value
(
obj
),
d
->
target_module
))
{
return
;
}
mrb_yield
(
mrb
,
d
->
block
,
mrb_obj_value
(
obj
));
++
d
->
count
;
}
static
mrb_value
os_each_object
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_value
cls
=
mrb_nil_value
();
struct
os_each_object_data
d
;
mrb_get_args
(
mrb
,
"&|C"
,
&
d
.
block
,
&
cls
);
if
(
mrb_nil_p
(
d
.
block
))
{
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"Expected block in ObjectSpace.each_object."
);
}
d
.
target_module
=
mrb_nil_p
(
cls
)
?
NULL
:
mrb_class_ptr
(
cls
);
d
.
count
=
0
;
mrb_objspace_each_objects
(
mrb
,
os_each_object_cb
,
&
d
);
return
mrb_fixnum_value
(
d
.
count
);
}
void
void
mrb_mruby_objectspace_gem_init
(
mrb_state
*
mrb
)
mrb_mruby_objectspace_gem_init
(
mrb_state
*
mrb
)
{
{
struct
RClass
*
os
=
mrb_define_module
(
mrb
,
"ObjectSpace"
);
struct
RClass
*
os
=
mrb_define_module
(
mrb
,
"ObjectSpace"
);
mrb_define_class_method
(
mrb
,
os
,
"count_objects"
,
os_count_objects
,
MRB_ARGS_OPT
(
1
));
mrb_define_class_method
(
mrb
,
os
,
"count_objects"
,
os_count_objects
,
MRB_ARGS_OPT
(
1
));
mrb_define_class_method
(
mrb
,
os
,
"each_object"
,
os_each_object
,
MRB_ARGS_OPT
(
1
));
}
}
void
void
...
...
mrbgems/mruby-objectspace/test/objectspace.rb
View file @
e5922aa8
...
@@ -35,3 +35,18 @@ assert('ObjectSpace.count_objects') do
...
@@ -35,3 +35,18 @@ assert('ObjectSpace.count_objects') do
assert_equal
(
h
[
:MRB_TT_HASH
],
h_before
[
:MRB_TT_HASH
]
+
1000
)
assert_equal
(
h
[
:MRB_TT_HASH
],
h_before
[
:MRB_TT_HASH
]
+
1000
)
assert_equal
(
h_after
[
:MRB_TT_HASH
],
h_before
[
:MRB_TT_HASH
])
assert_equal
(
h_after
[
:MRB_TT_HASH
],
h_before
[
:MRB_TT_HASH
])
end
end
assert
(
'ObjectSpace.each_object'
)
do
objs
=
[]
objs_count
=
ObjectSpace
.
each_object
{
|
obj
|
objs
<<
obj
}
assert_equal
objs
.
length
,
objs_count
arys
=
[]
arys_count
=
ObjectSpace
.
each_object
(
Array
)
{
|
obj
|
arys
<<
obj
}
assert_equal
arys
.
length
,
arys_count
assert_true
arys
.
length
<
objs
.
length
end
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