[macruby-changes] [453] MacRuby/branches/lrz_unstable
source_changes at macosforge.org
source_changes at macosforge.org
Tue Aug 19 02:01:27 PDT 2008
Revision: 453
http://trac.macosforge.org/projects/ruby/changeset/453
Author: lsansonetti at apple.com
Date: 2008-08-19 02:01:27 -0700 (Tue, 19 Aug 2008)
Log Message:
-----------
wip
Modified Paths:
--------------
MacRuby/branches/lrz_unstable/class.c
MacRuby/branches/lrz_unstable/compile.c
MacRuby/branches/lrz_unstable/id.c
MacRuby/branches/lrz_unstable/id.h
MacRuby/branches/lrz_unstable/include/ruby/intern.h
MacRuby/branches/lrz_unstable/include/ruby/ruby.h
MacRuby/branches/lrz_unstable/numeric.c
MacRuby/branches/lrz_unstable/objc.m
MacRuby/branches/lrz_unstable/object.c
MacRuby/branches/lrz_unstable/string.c
MacRuby/branches/lrz_unstable/variable.c
MacRuby/branches/lrz_unstable/vm_method.c
Modified: MacRuby/branches/lrz_unstable/class.c
===================================================================
--- MacRuby/branches/lrz_unstable/class.c 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/class.c 2008-08-19 09:01:27 UTC (rev 453)
@@ -62,6 +62,23 @@
}
static VALUE
+rb_objc_init(VALUE rcv)
+{
+ rb_funcall(rcv, idInitialize, 0);
+ return rcv;
+}
+
+void
+rb_define_object_special_methods(VALUE klass)
+{
+ rb_define_alloc_func(klass, rb_class_allocate_instance);
+ rb_define_singleton_method(klass, "new", rb_class_new_instance, -1);
+ rb_define_method(klass, "dup", rb_obj_dup, 0);
+ rb_define_method(klass, "init", rb_objc_init, 0);
+ rb_define_method(klass, "initialize_copy", rb_obj_init_copy, 1);
+}
+
+static VALUE
rb_objc_alloc_class(const char *name, VALUE super, VALUE flags, VALUE klass)
{
Class ocklass;
@@ -121,11 +138,8 @@
klass = rb_objc_alloc_class(name, super, T_CLASS, rb_cClass);
objc_registerClassPair((Class)klass);
- if (RCLASS_SUPER(klass) == rb_cNSObject) {
- rb_define_alloc_func(klass, rb_class_allocate_instance);
- rb_define_singleton_method(klass, "new", rb_class_new_instance, -1);
- rb_define_method(klass, "dup", rb_obj_dup, 0);
- rb_define_method(klass, "initialize_copy", rb_obj_init_copy, 1);
+ if (super == rb_cNSObject) {
+ rb_define_object_special_methods(klass);
}
if (name != NULL && rb_class_tbl != NULL)
@@ -389,10 +403,7 @@
VALUE
rb_module_new(void)
{
- VALUE mdl = rb_objc_alloc_class(NULL, 0, T_MODULE, rb_cModule);
- objc_registerClassPair((Class)mdl);
-
- return (VALUE)mdl;
+ return rb_define_module_id(0);
}
VALUE
@@ -400,9 +411,15 @@
{
VALUE mdl;
- mdl = rb_objc_alloc_class(rb_id2name(id), 0, T_MODULE, rb_cModule);
+ mdl = rb_objc_alloc_class(id == 0 ? NULL : rb_id2name(id), rb_cObject, T_MODULE, rb_cModule);
objc_registerClassPair((Class)mdl);
+ if (rb_mKernel != 0) {
+ /* because Module#initialize can accept a block */
+ extern VALUE rb_mod_initialize(VALUE);
+ rb_define_method(*(VALUE *)mdl, "initialize", rb_mod_initialize, 0);
+ }
+
return mdl;
}
@@ -461,7 +478,7 @@
Check_Type(module, T_MODULE);
- ary = rb_ivar_get(klass, idIncludedModules);
+ ary = rb_attr_get(klass, idIncludedModules);
if (ary == Qnil) {
ary = rb_ary_new();
rb_ivar_set(klass, idIncludedModules, ary);
@@ -470,7 +487,7 @@
return;
rb_ary_insert(ary, 0, module);
- ary = rb_ivar_get(module, idIncludedInClasses);
+ ary = rb_attr_get(module, idIncludedInClasses);
if (ary == Qnil) {
ary = rb_ary_new();
rb_ivar_set(module, idIncludedInClasses, ary);
@@ -678,44 +695,48 @@
len = strlen(sel_name);
- if (is_ruby_method && len >= 3 && sel_name[len - 1] == ':' && isalpha(sel_name[len - 3])) {
- assert(len + 3 < sizeof(buf));
- if (sel_name[len - 2] == '=') {
- /* skip foo=: (ruby) -> setFoo: (objc) shortcuts */
- snprintf(buf, sizeof buf, "set%s", sel_name);
- buf[4] = toupper(buf[4]);
- buf[len + 1] = ':';
- buf[len + 2] = '\0';
+ if (is_ruby_method && len > 8 && sel_name[0] == '_' && sel_name[1] == '_' && sel_name[2] == 'r' && sel_name[3] == 'b' && sel_name[4] == '_' && sel_name[len - 1] == '_' && sel_name[len - 2] == '_') {
+ /* retransform ignored selectors, __rb_%s__ -> %s */
+ assert(sizeof buf > len - 7);
+ strncpy(buf, &sel_name[5], len - 7);
+ buf[len - 7] = '\0';
+ sel_name = buf;
+ }
+ else {
+ if (is_ruby_method && len >= 3 && sel_name[len - 1] == ':' && isalpha(sel_name[len - 3])) {
+ assert(len + 3 < sizeof(buf));
+ if (sel_name[len - 2] == '=') {
+ /* skip foo=: (ruby) -> setFoo: (objc) shortcuts */
+ snprintf(buf, sizeof buf, "set%s", sel_name);
+ buf[4] = toupper(buf[4]);
+ buf[len + 1] = ':';
+ buf[len + 2] = '\0';
- method = class_getInstanceMethod((Class)mod, sel_registerName(buf));
- if (method != NULL && rb_objc_method_node3(method_getImplementation(method)) == NULL)
- continue;
- }
- else if (sel_name[len - 2] == '?') {
- /* skip foo?: (ruby) -> isFoo: (objc) shortcuts */
+ method = class_getInstanceMethod((Class)mod, sel_registerName(buf));
+ if (method != NULL && rb_objc_method_node3(method_getImplementation(method)) == NULL)
+ continue;
+ }
+ else if (sel_name[len - 2] == '?') {
+ /* skip foo?: (ruby) -> isFoo: (objc) shortcuts */
+ snprintf(buf, sizeof buf, "is%s", sel_name);
+ buf[3] = toupper(buf[3]);
+ buf[len] = ':';
+ buf[len + 1] = '\0';
- snprintf(buf, sizeof buf, "is%s", sel_name);
- buf[3] = toupper(buf[3]);
- buf[len] = ':';
- buf[len + 1] = '\0';
-
- method = class_getInstanceMethod((Class)mod, sel_registerName(buf));
- if (method != NULL && rb_objc_method_node3(method_getImplementation(method)) == NULL)
- continue;
+ method = class_getInstanceMethod((Class)mod, sel_registerName(buf));
+ if (method != NULL && rb_objc_method_node3(method_getImplementation(method)) == NULL)
+ continue;
+ }
}
+ p = strchr(sel_name, ':');
+ if (p != NULL && strchr(p + 1, ':') == NULL) {
+ /* remove trailing ':' for methods with arity 1 */
+ assert(len < sizeof(buf));
+ strncpy(buf, sel_name, len);
+ buf[len - 1] = '\0';
+ sel_name = buf;
+ }
}
- p = strchr(sel_name, ':');
- if (p != NULL && strchr(p + 1, ':') == NULL) {
- /* remove trailing ':' for methods with arity 1 */
-
- assert(len < sizeof(buf));
-
- strncpy(buf, sel_name, len);
- buf[len - 1] = '\0';
-
- sel_name = buf;
- }
-
mid = rb_intern(sel_name);
sym = ID2SYM(mid);
Modified: MacRuby/branches/lrz_unstable/compile.c
===================================================================
--- MacRuby/branches/lrz_unstable/compile.c 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/compile.c 2008-08-19 09:01:27 UTC (rev 453)
@@ -674,7 +674,7 @@
operands = (VALUE *)compile_data_alloc(iseq, sizeof(VALUE) * argc);
for (i = 0; i < argc; i++) {
VALUE v = va_arg(argv, VALUE);
- operands[i] = v;
+ GC_WB(&operands[i], v);
}
va_end(argv);
}
@@ -695,7 +695,7 @@
operands[2] = block;
operands[3] = flag;
- mcache = (struct rb_method_cache *)malloc(sizeof(struct rb_method_cache));
+ mcache = (struct rb_method_cache *)xmalloc(sizeof(struct rb_method_cache));
mcache->flags = RB_MCACHE_RCALL_FLAG;
mcache->as.rcall.klass = 0;
mcache->as.rcall.node = NULL;
@@ -716,13 +716,14 @@
}
else {
mcache->as.rcall.sel = sel_registerName(rb_sym2name(id));
- if (mcache->as.rcall.sel == sel_ignored) {
+ if (mcache->as.rcall.sel == sel_ignored
+ || mcache->as.rcall.sel == sel_zone) {
char buf[100];
snprintf(buf, sizeof buf, "__rb_%s__", rb_sym2name(id));
mcache->as.rcall.sel = sel_registerName(buf);
}
}
- operands[4] = (VALUE)mcache;
+ GC_WB(&operands[4], (VALUE)mcache);
iobj = new_insn_core(iseq, line_no, BIN(send), 5, operands);
return iobj;
}
Modified: MacRuby/branches/lrz_unstable/id.c
===================================================================
--- MacRuby/branches/lrz_unstable/id.c 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/id.c 2008-08-19 09:01:27 UTC (rev 453)
@@ -62,6 +62,7 @@
selCopy = sel_registerName("copy");
sel_ignored = sel_registerName("retain");
assert(sel_ignored == sel_registerName("release"));
+ sel_zone = sel_registerName("zone");
#endif
idAREF = rb_intern("[]");
Modified: MacRuby/branches/lrz_unstable/id.h
===================================================================
--- MacRuby/branches/lrz_unstable/id.h 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/id.h 2008-08-19 09:01:27 UTC (rev 453)
@@ -73,6 +73,7 @@
extern SEL selInit;
extern SEL selCopy;
extern SEL sel_ignored;
+extern SEL sel_zone;
extern ID idIncludedModules;
extern ID idIncludedInClasses;
#endif
Modified: MacRuby/branches/lrz_unstable/include/ruby/intern.h
===================================================================
--- MacRuby/branches/lrz_unstable/include/ruby/intern.h 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/include/ruby/intern.h 2008-08-19 09:01:27 UTC (rev 453)
@@ -150,6 +150,7 @@
#if WITH_OBJC
VALUE rb_objc_create_class(const char *name, VALUE super);
bool rb_objc_install_primitives(Class ocklass, Class ocsuper);
+void rb_define_object_special_methods(VALUE klass);
#endif
VALUE rb_class_boot(VALUE);
VALUE rb_class_new(VALUE);
Modified: MacRuby/branches/lrz_unstable/include/ruby/ruby.h
===================================================================
--- MacRuby/branches/lrz_unstable/include/ruby/ruby.h 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/include/ruby/ruby.h 2008-08-19 09:01:27 UTC (rev 453)
@@ -1134,6 +1134,20 @@
if (*(Class *)obj == (Class)rb_cFixnum) {
return LONG2FIX(RFIXNUM(obj)->value);
}
+ if (*(Class *)obj == (Class)rb_cCFNumber) {
+ /* TODO NSNumber should implement the Numeric primitive methods */
+ if (CFNumberIsFloatType((CFNumberRef)obj)) {
+ double v;
+ assert(CFNumberGetValue((CFNumberRef)obj, kCFNumberDoubleType, &v));
+ extern VALUE rb_float_new(double);
+ return rb_float_new(v);
+ }
+ else {
+ long v;
+ assert(CFNumberGetValue((CFNumberRef)obj, kCFNumberLongType, &v));
+ return LONG2FIX(v);
+ }
+ }
return (VALUE)obj;
}
#define RB2OC(obj) (rb_rval_to_ocid((VALUE)obj))
Modified: MacRuby/branches/lrz_unstable/numeric.c
===================================================================
--- MacRuby/branches/lrz_unstable/numeric.c 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/numeric.c 2008-08-19 09:01:27 UTC (rev 453)
@@ -3233,6 +3233,7 @@
rb_cCFNumber = (VALUE)objc_getClass("NSCFNumber");
rb_cNumeric = rb_define_class("Numeric", (VALUE)objc_getClass("NSNumber"));
RCLASS_SET_VERSION_FLAG(rb_cNumeric, RCLASS_IS_OBJECT_SUBCLASS);
+ rb_define_object_special_methods(rb_cNumeric);
/* overriding NSObject methods */
rb_define_method(rb_cNumeric, "class", rb_obj_class, 0);
rb_define_method(rb_cNumeric, "dup", rb_obj_dup, 0);
Modified: MacRuby/branches/lrz_unstable/objc.m
===================================================================
--- MacRuby/branches/lrz_unstable/objc.m 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/objc.m 2008-08-19 09:01:27 UTC (rev 453)
@@ -1138,6 +1138,7 @@
const char *name_str, *def_str;
SEL name_sel, def_sel;
Method method, dest_method;
+ bool redo = false;
name_str = rb_id2name(name);
def_str = rb_id2name(def);
@@ -1165,10 +1166,12 @@
}
}
+alias_method:
+
dest_method = class_getInstanceMethod((Class)klass, name_sel);
- DLOG("ALIAS", "[%s %s -> %s] direct_override=%d",
- class_getName((Class)klass), (char *)name_sel, (char *)def_sel, dest_method != NULL);
+ DLOG("ALIAS", "%c[%s %s -> %s] types=%s direct_override=%d orig_node=%p",
+ class_isMetaClass((Class)klass) ? '+' : '-', class_getName((Class)klass), (char *)name_sel, (char *)def_sel, method_getTypeEncoding(method), dest_method != NULL, rb_objc_method_node3(method_getImplementation(method)));
if (dest_method != NULL
&& dest_method != class_getInstanceMethod((Class)RCLASS_SUPER(klass), name_sel)) {
@@ -1179,6 +1182,20 @@
method_getImplementation(method),
method_getTypeEncoding(method)));
}
+
+ if (!redo && name_str[strlen(name_str) - 1] != ':') {
+ char buf[100];
+
+ snprintf(buf, sizeof buf, "%s:", def_str);
+ def_sel = sel_registerName(buf);
+ method = class_getInstanceMethod((Class)klass, def_sel);
+ if (method != NULL) {
+ snprintf(buf, sizeof buf, "%s:", name_str);
+ name_sel = sel_registerName(buf);
+ redo = true;
+ goto alias_method;
+ }
+ }
}
static VALUE
@@ -1438,7 +1455,7 @@
}
else {
sel = sel_registerName(mid_str);
- if (sel == sel_ignored) {
+ if (sel == sel_ignored || sel == sel_zone) {
assert(sizeof(buf) > mid_str_len + 7);
snprintf(buf, sizeof buf, "__rb_%s__", mid_str);
sel = sel_registerName(buf);
@@ -1446,7 +1463,7 @@
}
}
- included_in_classes = RCLASS_MODULE(mod) ? rb_ivar_get(mod, idIncludedInClasses) : Qnil;
+ included_in_classes = RCLASS_MODULE(mod) ? rb_attr_get(mod, idIncludedInClasses) : Qnil;
direct_override = false;
method = class_getInstanceMethod((Class)mod, sel);
@@ -1454,21 +1471,13 @@
if (method != NULL) {
Class klass;
- /* Do not override certain NSObject selectors. */
- if (sel == @selector(superclass)
- || sel == @selector(hash)
- || sel == @selector(zone)) {
- if (class_getInstanceMethod((Class)rb_cBasicObject, sel) == method)
- return;
- }
-
if (oc_arity + 2 != method_getNumberOfArguments(method)) {
rb_warn("cannot override Objective-C method `%s' in " \
- "class `%s' because of an arity mismatch (%d for %d)",
- (char *)method_getName(method),
- class_getName((Class)mod),
- oc_arity + 2,
- method_getNumberOfArguments(method));
+ "class `%s' because of an arity mismatch (%d for %d)",
+ (char *)method_getName(method),
+ class_getName((Class)mod),
+ oc_arity + 2,
+ method_getNumberOfArguments(method));
return;
}
types = (char *)method_getTypeEncoding(method);
Modified: MacRuby/branches/lrz_unstable/object.c
===================================================================
--- MacRuby/branches/lrz_unstable/object.c 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/object.c 2008-08-19 09:01:27 UTC (rev 453)
@@ -1381,9 +1381,7 @@
static VALUE
rb_module_s_alloc(VALUE klass)
{
- VALUE mod = rb_module_new();
-
- return mod;
+ return rb_module_new();
}
static VALUE
@@ -1415,7 +1413,7 @@
* a.meth2 #=> "bye"
*/
-static VALUE
+ VALUE
rb_mod_initialize(VALUE module)
{
extern VALUE rb_mod_module_exec(int argc, VALUE *argv, VALUE mod);
Modified: MacRuby/branches/lrz_unstable/string.c
===================================================================
--- MacRuby/branches/lrz_unstable/string.c 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/string.c 2008-08-19 09:01:27 UTC (rev 453)
@@ -5014,7 +5014,7 @@
VALUE
rb_sym_to_s(VALUE sym)
{
- return str_new3(rb_cString, sym);
+ return rb_str_new2(RSYMBOL(sym)->str);
}
@@ -5417,4 +5417,4 @@
rb_define_method(rb_cSymbol, "to_sym", sym_to_sym, 0);
install_symbol_primitives();
-}
\ No newline at end of file
+}
Modified: MacRuby/branches/lrz_unstable/variable.c
===================================================================
--- MacRuby/branches/lrz_unstable/variable.c 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/variable.c 2008-08-19 09:01:27 UTC (rev 453)
@@ -2058,7 +2058,7 @@
}
if (!recurse && klass != rb_cObject) break;
#if WITH_OBJC
- VALUE inc_mods = rb_ivar_get(tmp, idIncludedModules);
+ VALUE inc_mods = rb_attr_get(tmp, idIncludedModules);
if (inc_mods != Qnil) {
int i, count = RARRAY_LEN(inc_mods);
for (i = 0; i < count; i++) {
Modified: MacRuby/branches/lrz_unstable/vm_method.c
===================================================================
--- MacRuby/branches/lrz_unstable/vm_method.c 2008-08-19 00:49:27 UTC (rev 452)
+++ MacRuby/branches/lrz_unstable/vm_method.c 2008-08-19 09:01:27 UTC (rev 453)
@@ -320,14 +320,23 @@
const char *id_str = rb_id2name(id);
size_t slen = strlen(id_str);
- if (id_str[slen - 1] == ':') {
- return NULL;
- }
- else {
+ if (strcmp(id_str, "retain") == 0
+ || strcmp(id_str, "release") == 0
+ || strcmp(id_str, "zone") == 0) {
char buf[100];
- snprintf(buf, sizeof buf, "%s:", id_str);
+ snprintf(buf, sizeof buf, "__rb_%s__", id_str);
return rb_method_node(klass, rb_intern(buf));
}
+ else {
+ if (id_str[slen - 1] == ':') {
+ return NULL;
+ }
+ else {
+ char buf[100];
+ snprintf(buf, sizeof buf, "%s:", id_str);
+ return rb_method_node(klass, rb_intern(buf));
+ }
+ }
}
return node;
#else
@@ -451,19 +460,7 @@
origin = rb_method_node(RCLASS_SUPER(klass), name) == fbody
? RCLASS_SUPER(klass) : klass;
}
-#if 0
if (fbody == NULL) {
- char buf[512];
- ID newname;
-
- snprintf(buf, sizeof buf, "%s:", rb_id2name(name));
- newname = rb_intern(buf);
- fbody = rb_method_node(klass, newname);
- if (fbody != NULL)
- name = newname;
- }
-#endif
- if (fbody == NULL) {
rb_print_undef(klass, name, 0);
}
if (fbody->nd_noex != noex) {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080819/16fe33e4/attachment-0001.html
More information about the macruby-changes
mailing list