[macruby-changes] [715] MacRuby/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Fri Nov 7 10:32:21 PST 2008
Revision: 715
http://trac.macosforge.org/projects/ruby/changeset/715
Author: lsansonetti at apple.com
Date: 2008-11-07 10:32:21 -0800 (Fri, 07 Nov 2008)
Log Message:
-----------
introducing #method_signature, contributed by Ben Stiglitz
Modified Paths:
--------------
MacRuby/trunk/gc.c
MacRuby/trunk/objc.h
MacRuby/trunk/objc.m
MacRuby/trunk/object.c
MacRuby/trunk/test/ruby/test_objc.rb
Modified: MacRuby/trunk/gc.c
===================================================================
--- MacRuby/trunk/gc.c 2008-11-06 21:59:13 UTC (rev 714)
+++ MacRuby/trunk/gc.c 2008-11-07 18:32:21 UTC (rev 715)
@@ -402,6 +402,11 @@
rb_objc_newobj(size_t size)
{
void *obj;
+
+ if (ruby_gc_stress) {
+ objc_collect(OBJC_GENERATIONAL);
+ }
+
obj = auto_zone_allocate_object(__auto_zone, size, AUTO_OBJECT_SCANNED,
0, 0);
assert(obj != NULL);
Modified: MacRuby/trunk/objc.h
===================================================================
--- MacRuby/trunk/objc.h 2008-11-06 21:59:13 UTC (rev 714)
+++ MacRuby/trunk/objc.h 2008-11-07 18:32:21 UTC (rev 715)
@@ -17,6 +17,7 @@
VALUE *argv);
void rb_objc_define_kvo_setter(VALUE klass, ID mid);
+void rb_objc_change_ruby_method_signature(VALUE mod, ID mid, VALUE sig);
static inline void
rb_objc_install_method(Class klass, SEL sel, IMP imp)
Modified: MacRuby/trunk/objc.m
===================================================================
--- MacRuby/trunk/objc.m 2008-11-06 21:59:13 UTC (rev 714)
+++ MacRuby/trunk/objc.m 2008-11-07 18:32:21 UTC (rev 715)
@@ -1949,6 +1949,29 @@
#undef forward_method_definition
}
+void
+rb_objc_change_ruby_method_signature(VALUE mod, VALUE mid, VALUE sig)
+{
+ SEL sel = sel_registerName(rb_id2name(rb_to_id(mid)));
+ char *types = StringValuePtr(sig);
+ Class c = (Class)mod;
+ Method m = class_getInstanceMethod(c, sel);
+ if (m == NULL) {
+ rb_raise(rb_eArgError,
+ "invalid method %s for given class",
+ (char *)sel);
+ }
+ IMP imp = method_getImplementation(m);
+ if (rb_objc_method_node3(imp) == NULL) {
+ rb_raise(rb_eArgError,
+ "will not replace method signature for method %s because it is a pure Objective-C method",
+ (char *)sel);
+ }
+ char **types_p = ((void *)m + sizeof(SEL));
+ free(*types_p);
+ *types_p = strdup(types);
+}
+
static inline bool
rb_objc_resourceful(VALUE obj)
{
Modified: MacRuby/trunk/object.c
===================================================================
--- MacRuby/trunk/object.c 2008-11-06 21:59:13 UTC (rev 714)
+++ MacRuby/trunk/object.c 2008-11-07 18:32:21 UTC (rev 715)
@@ -1444,6 +1444,24 @@
/*
* call-seq:
+ * class Something
+ * def setThing(thing, forIndex: i); @things[i] = thing; end
+ * method_signature :'setThing:forIndex:', 'v@:@i'
+ * end
+ *
+ * Changes the type signature stored in the Objective-C runtime for the
+ * given method.
+ *
+ */
+static VALUE
+rb_mod_method_signature(VALUE module, VALUE mid, VALUE sim)
+{
+ rb_objc_change_ruby_method_signature(module, mid, sim);
+ return Qnil;
+}
+
+/*
+ * call-seq:
* Class.new(super_class=Object) => a_class
*
* Creates a new anonymous (unnamed) class with the given superclass
@@ -2499,6 +2517,7 @@
rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
+ rb_define_private_method(rb_cModule, "method_signature", rb_mod_method_signature, 2);
rb_define_method(rb_mKernel, "nil?", rb_false, 0);
rb_define_method(rb_mKernel, "===", rb_equal, 1);
Modified: MacRuby/trunk/test/ruby/test_objc.rb
===================================================================
--- MacRuby/trunk/test/ruby/test_objc.rb 2008-11-06 21:59:13 UTC (rev 714)
+++ MacRuby/trunk/test/ruby/test_objc.rb 2008-11-07 18:32:21 UTC (rev 715)
@@ -487,4 +487,25 @@
assert_equal(true, o.isLoaded)
assert_equal(true, o.loaded?)
end
+
+ class TestChangeSignature
+ def foo; 42; end
+ def foo(x, with:y); 42; end
+ method_signature 'foo', 'v@:'
+ method_signature 'foo:with:', 'v@:ii'
+ end
+
+ def test_change_method_signature
+ o = TestChangeSignature.new
+ ms = o.methodSignatureForSelector('foo')
+ assert_equal('v', ms.methodReturnType)
+ assert_equal('@', ms.getArgumentTypeAtIndex(0))
+ assert_equal(':', ms.getArgumentTypeAtIndex(1))
+ ms = o.methodSignatureForSelector('foo:with:')
+ assert_equal('v', ms.methodReturnType)
+ assert_equal('@', ms.getArgumentTypeAtIndex(0))
+ assert_equal(':', ms.getArgumentTypeAtIndex(1))
+ assert_equal('i', ms.getArgumentTypeAtIndex(2))
+ assert_equal('i', ms.getArgumentTypeAtIndex(3))
+ end
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20081107/7ece9e4a/attachment.html>
More information about the macruby-changes
mailing list