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
26600ca4
Commit
26600ca4
authored
Nov 30, 2012
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #573 from skandhas/pr-add-Module-module_eval
add "Module#module_eval" and "Module#class_eval" for mruby
parents
8c24e9e2
053c104f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
1 deletion
+53
-1
src/class.c
src/class.c
+29
-1
test/t/module.rb
test/t/module.rb
+24
-0
No files found.
src/class.c
View file @
26600ca4
...
...
@@ -848,6 +848,32 @@ mrb_mod_instance_methods(mrb_state *mrb, mrb_value mod)
return
class_instance_method_list
(
mrb
,
argc
,
argv
,
c
,
0
);
}
mrb_value
mrb_yield_internal
(
mrb_state
*
mrb
,
mrb_value
b
,
int
argc
,
mrb_value
*
argv
,
mrb_value
self
,
struct
RClass
*
c
);
/* 15.2.2.4.35 */
/*
* call-seq:
* mod.class_eval {| | block } -> obj
* mod.module_eval {| | block } -> obj
*
* Evaluates block in the context of _mod_. This can
* be used to add methods to a class. <code>module_eval</code> returns
* the result of evaluating its argument.
*/
mrb_value
mrb_mod_module_eval
(
mrb_state
*
mrb
,
mrb_value
mod
)
{
mrb_value
a
,
b
;
struct
RClass
*
c
;
if
(
mrb_get_args
(
mrb
,
"|S&"
,
&
a
,
&
b
)
==
1
)
{
mrb_raise
(
mrb
,
E_NOTIMP_ERROR
,
"module_eval/class_eval with string not implemented"
);
}
c
=
mrb_class_ptr
(
mod
);
return
mrb_yield_internal
(
mrb
,
b
,
0
,
0
,
mod
,
c
);
}
mrb_value
mrb_singleton_class
(
mrb_state
*
mrb
,
mrb_value
v
)
{
...
...
@@ -1504,9 +1530,11 @@ mrb_init_class(mrb_state *mrb)
mrb_define_method
(
mrb
,
mod
,
"include"
,
mrb_mod_include
,
ARGS_ANY
());
/* 15.2.2.4.27 */
mrb_define_method
(
mrb
,
mod
,
"include?"
,
mrb_mod_include_p
,
ARGS_REQ
(
1
));
/* 15.2.2.4.28 */
mrb_define_method
(
mrb
,
mod
,
"append_features"
,
mrb_mod_append_features
,
ARGS_REQ
(
1
));
/* 15.2.2.4.10 */
mrb_define_method
(
mrb
,
mod
,
"included"
,
mrb_bob_init
,
ARGS_REQ
(
1
));
/* 15.2.2.4.29 */
mrb_define_method
(
mrb
,
mod
,
"class_eval"
,
mrb_mod_module_eval
,
ARGS_ANY
());
/* 15.2.2.4.15 */
mrb_define_method
(
mrb
,
mod
,
"included"
,
mrb_bob_init
,
ARGS_REQ
(
1
));
/* 15.2.2.4.29 */
mrb_define_method
(
mrb
,
mod
,
"included_modules"
,
mrb_mod_included_modules
,
ARGS_NONE
());
/* 15.2.2.4.30 */
mrb_define_method
(
mrb
,
mod
,
"instance_methods"
,
mrb_mod_instance_methods
,
ARGS_ANY
());
/* 15.2.2.4.33 */
mrb_define_method
(
mrb
,
mod
,
"module_eval"
,
mrb_mod_module_eval
,
ARGS_ANY
());
/* 15.2.2.4.35 */
mrb_define_method
(
mrb
,
mod
,
"to_s"
,
mrb_mod_to_s
,
ARGS_NONE
());
mrb_define_method
(
mrb
,
mod
,
"inspect"
,
mrb_mod_to_s
,
ARGS_NONE
());
...
...
test/t/module.rb
View file @
26600ca4
...
...
@@ -31,6 +31,22 @@ assert('Module#append_features', '15.2.2.4.10') do
Test4AppendFeatures2
.
const_get
(
:Const4AppendFeatures2
)
==
Test4AppendFeatures2
end
assert
(
'Module#class_eval'
,
'15.2.2.4.15'
)
do
class
Test4ClassEval
@a
=
11
@b
=
12
end
Test4ClassEval
.
class_eval
do
def
method1
end
end
r
=
Test4ClassEval
.
instance_methods
Test4ClassEval
.
class_eval
{
@a
}
==
11
and
Test4ClassEval
.
class_eval
{
@b
}
==
12
and
r
.
class
==
Array
and
r
.
include?
(
:method1
)
end
assert
(
'Module#class_variables'
,
'15.2.2.4.19'
)
do
class
Test4ClassVariables1
@@var1
=
1
...
...
@@ -151,6 +167,14 @@ assert('Module#instance_methods', '15.2.2.4.33') do
r
.
class
==
Array
and
r
.
include?
(
:method3
)
and
r
.
include?
(
:method2
)
end
assert
(
'Module#module_eval'
,
'15.2.2.4.35'
)
do
module
Test4ModuleEval
@a
=
11
@b
=
12
end
Test4ModuleEval
.
module_eval
{
@a
}
==
11
and
Test4ModuleEval
.
module_eval
{
@b
}
==
12
end
# Not ISO specified
...
...
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