Skip to content

Commit 2a876d2

Browse files
committed
class.c: implement top-level public/private/protected in the core in C
1 parent 43da490 commit 2a876d2

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

mrbgems/mruby-toplevel-ext/mrblib/toplevel.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,4 @@ class <<self
44
def include(*modules)
55
Object.include(*modules)
66
end
7-
8-
def private(*methods)
9-
Object.instance_eval do
10-
private(*methods)
11-
end
12-
end
13-
def protected(*methods)
14-
Object.instance_eval do
15-
protected(*methods)
16-
end
17-
end
18-
def public(*methods)
19-
Object.instance_eval do
20-
public(*methods)
21-
end
22-
end
237
end

src/class.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,30 @@ mrb_mod_protected(mrb_state *mrb, mrb_value mod)
17121712
return mod;
17131713
}
17141714

1715+
static mrb_value
1716+
top_public(mrb_state *mrb, mrb_value self)
1717+
{
1718+
self = mrb_obj_value(mrb->object_class);
1719+
mrb_mod_visibility(mrb, self, MT_PUBLIC);
1720+
return self;
1721+
}
1722+
1723+
static mrb_value
1724+
top_private(mrb_state *mrb, mrb_value self)
1725+
{
1726+
self = mrb_obj_value(mrb->object_class);
1727+
mrb_mod_visibility(mrb, self, MT_PRIVATE);
1728+
return self;
1729+
}
1730+
1731+
static mrb_value
1732+
top_protected(mrb_state *mrb, mrb_value self)
1733+
{
1734+
self = mrb_obj_value(mrb->object_class);
1735+
mrb_mod_visibility(mrb, self, MT_PROTECTED);
1736+
return self;
1737+
}
1738+
17151739
MRB_API struct RClass*
17161740
mrb_singleton_class_ptr(mrb_state *mrb, mrb_value v)
17171741
{
@@ -3140,4 +3164,7 @@ mrb_init_class(mrb_state *mrb)
31403164
mrb_define_singleton_method_id(mrb, mrb->top_self, MRB_SYM(inspect), inspect_main, MRB_ARGS_NONE());
31413165
mrb_define_singleton_method_id(mrb, mrb->top_self, MRB_SYM(to_s), inspect_main, MRB_ARGS_NONE());
31423166
mrb_define_singleton_method_id(mrb, mrb->top_self, MRB_SYM(define_method), top_define_method, MRB_ARGS_ARG(1,1));
3167+
mrb_define_singleton_method_id(mrb, mrb->top_self, MRB_SYM(public), top_public, MRB_ARGS_ANY());
3168+
mrb_define_singleton_method_id(mrb, mrb->top_self, MRB_SYM(private), top_private, MRB_ARGS_ANY());
3169+
mrb_define_singleton_method_id(mrb, mrb->top_self, MRB_SYM(protected), top_protected, MRB_ARGS_ANY());
31433170
}

0 commit comments

Comments
 (0)