Permalink
Browse files

refactor duplicated code

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@5203 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
lrz committed Jan 28, 2011
1 parent 855f737 commit 81232c27aff38ccd1630728680b7caa6adff0798
Showing with 13 additions and 73 deletions.
  1. +1 −12 class.c
  2. +4 −12 compiler.cpp
  3. +4 −18 enumerator.c
  4. +2 −12 vm.cpp
  5. +2 −19 vm_eval.c
View
13 class.c
@@ -979,17 +979,6 @@ rb_obj_singleton_methods(VALUE obj, SEL sel, int argc, VALUE *argv)
return ary;
}
-static SEL
-name_to_sel(const char *name, const int arity)
-{
- if (arity > 0) {
- char buf[100];
- snprintf(buf, sizeof buf, "%s:", name);
- return sel_registerName(buf);
- }
- return sel_registerName(name);
-}
-
static void
rb_objc_add_method(VALUE klass, const char *name, void *imp, const int arity,
const int noex, bool direct)
@@ -1001,7 +990,7 @@ rb_objc_add_method(VALUE klass, const char *name, void *imp, const int arity,
NODE *body = rb_vm_cfunc_node_from_imp((Class)klass, arity, (IMP)imp, noex);
GC_RETAIN(body);
- rb_vm_define_method((Class)klass, name_to_sel(name, arity), (IMP)imp,
+ rb_vm_define_method((Class)klass, rb_vm_name_to_sel(name, arity), (IMP)imp,
body, direct);
}
View
@@ -340,20 +340,12 @@ RoxorAOTCompiler::RoxorAOTCompiler(void)
SEL
RoxorCompiler::mid_to_sel(ID mid, int arity)
{
- SEL sel;
- const char *mid_str = rb_id2name(mid);
- char buf[100];
- if (mid_str[strlen(mid_str) - 1] != ':' && arity > 0) {
- snprintf(buf, sizeof buf, "%s:", mid_str);
+ SEL sel = rb_vm_id_to_sel(mid, arity);
+ if (rb_objc_ignored_sel(sel)) {
+ char buf[100];
+ snprintf(buf, sizeof buf, "__hidden__%s", rb_id2name(mid));
sel = sel_registerName(buf);
}
- else {
- sel = sel_registerName(mid_str);
- if (rb_objc_ignored_sel(sel)) {
- snprintf(buf, sizeof buf, "__hidden__%s", mid_str);
- sel = sel_registerName(buf);
- }
- }
return sel;
}
View
@@ -7,6 +7,8 @@
#include "macruby_internal.h"
#include "id.h"
+#include "ruby/node.h"
+#include "vm.h"
/*
* Document-class: Enumerable::Enumerator
@@ -77,15 +79,7 @@ obj_to_enum(VALUE obj, SEL sel, int argc, VALUE *argv)
}
ID meth_id = rb_to_id(meth);
- SEL enum_sel;
- if (argc > 0) {
- char buf[100];
- snprintf(buf, sizeof buf, "%s:", rb_id2name(meth_id));
- enum_sel = sel_registerName(buf);
- }
- else {
- enum_sel = sel_registerName(rb_id2name(meth_id));
- }
+ SEL enum_sel = rb_vm_id_to_sel(meth_id, argc);
return rb_enumeratorize(obj, enum_sel, argc, argv);
}
@@ -251,15 +245,7 @@ enumerator_initialize(VALUE obj, SEL sel, int argc, VALUE *argv)
--argc;
}
ID meth_id = rb_to_id(meth);
- SEL meth_sel;
- if (argc == 0) {
- meth_sel = sel_registerName(rb_id2name(meth_id));
- }
- else {
- char buf[100];
- snprintf(buf, sizeof buf, "%s:", rb_id2name(meth_id));
- meth_sel = sel_registerName(buf);
- }
+ SEL meth_sel = rb_vm_id_to_sel(meth_id, argc);
return enumerator_init(obj, recv, meth_sel, argc, argv);
}
View
14 vm.cpp
@@ -1479,19 +1479,9 @@ vm_alias_method(Class klass, Method method, ID name, bool noargs)
if (UNAVAILABLE_IMP(imp)) {
return;
}
- const char *types = method_getTypeEncoding(method);
-
- const char *name_str = rb_id2name(name);
- SEL sel;
- if (noargs) {
- sel = sel_registerName(name_str);
- }
- else {
- char tmp[100];
- snprintf(tmp, sizeof tmp, "%s:", name_str);
- sel = sel_registerName(tmp);
- }
+ const char *types = method_getTypeEncoding(method);
+ SEL sel = rb_vm_id_to_sel(name, noargs ? 0 : 1);
rb_vm_method_node_t *node = GET_CORE()->method_node_get(method);
if (node != NULL) {
GET_CORE()->add_method(klass, sel, imp, node->ruby_imp,
View
@@ -26,17 +26,8 @@ rb_call(VALUE recv, ID mid, int argc, const VALUE *argv, int scope,
sel = selAlloc;
}
else {
- const char *midstr = rb_id2name(mid);
- if (argc > 0 && midstr[strlen(midstr) - 1] != ':') {
- char buf[100];
- snprintf(buf, sizeof buf, "%s:", midstr);
- sel = sel_registerName(buf);
- }
- else {
- sel = sel_registerName(midstr);
- }
+ sel = rb_vm_id_to_sel(mid, argc);
}
-
rb_vm_block_t *block = pass_current_block ? rb_vm_current_block() : NULL;
return rb_vm_call2(block, recv, CLASS_OF(recv), sel, argc, argv);
}
@@ -280,15 +271,7 @@ VALUE
rb_block_call(VALUE obj, ID mid, int argc, VALUE *argv,
VALUE (*bl_proc) (ANYARGS), VALUE data2)
{
- SEL sel;
- if (argc == 0) {
- sel = sel_registerName(rb_id2name(mid));
- }
- else {
- char buf[100];
- snprintf(buf, sizeof buf, "%s:", rb_id2name(mid));
- sel = sel_registerName(buf);
- }
+ SEL sel = rb_vm_id_to_sel(mid, argc);
return rb_objc_block_call(obj, sel, argc, argv, bl_proc, data2);
}

0 comments on commit 81232c2

Please sign in to comment.