Commit 26600ca4 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #573 from skandhas/pr-add-Module-module_eval

add "Module#module_eval" and "Module#class_eval" for mruby
parents 8c24e9e2 053c104f
......@@ -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());
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment