Permalink
Browse files

Merge pull request #23 from chapados/forwarding-lion

Forwarding lion
  • Loading branch information...
2 parents a2efa26 + 82347bd commit 77554a2947cf56357a49791a02e38c5220c76e6e Tim Burks committed Aug 25, 2011
Showing with 87 additions and 1 deletion.
  1. +5 −0 objc/NuHandler.m
  2. +4 −1 objc/NuObject.m
  3. +78 −0 test/test_forwarding.nu
View
@@ -64,6 +64,11 @@ static id collect_arguments(struct handler_description *description, va_list ap)
//NSLog(@"argument is %lf", x);
[cursor setCar:get_nu_value_from_objc_value(&x, type)];
}
+ else if (!strcmp(type, ":")) {
+ SEL x = va_arg(ap, SEL);
+ //NSLog(@"collect_arguments: [:] (SEL) = %@", NSStringFromSelector(x));
+ [cursor setCar:get_nu_value_from_objc_value(&x, type)];
+ }
else if (!strcmp(type, "{CGRect={CGPoint=ff}{CGSize=ff}}")
|| (!strcmp(type, "{CGRect=\"origin\"{CGPoint=\"x\"f\"y\"f}\"size\"{CGSize=\"width\"f\"height\"f}}"))) {
CGRect x = va_arg(ap, CGRect);
View
@@ -244,6 +244,10 @@ - (id) sendMessage:(id)cdr withContext:(NSMutableDictionary *)context
// Messaging null is ok.
else if (self == Nu__null) {
}
+ else if ( (target = [target forwardingTargetForSelector:sel]) ) {
+ //NSLog(@"found forwarding target: %@ for selector: %@", target, NSStringFromSelector(sel));
+ result = [target sendMessage:cdr withContext:context];
+ }
// Otherwise, call the overridable handler for unknown messages.
else {
//NSLog(@"calling handle unknown message for %@", [cdr stringValue]);
@@ -258,7 +262,6 @@ - (id) sendMessage:(id)cdr withContext:(NSMutableDictionary *)context
[result autorelease];
return result;
}
-
- (id) evalWithArguments:(id)cdr context:(NSMutableDictionary *)context
{
return [self sendMessage:cdr withContext:context];
@@ -0,0 +1,78 @@
+;; test_forwarding.nu
+;; tests for message forwarding in Nu
+;;
+;; Copyright (c) 2011 Brian Chapados
+
+(class RealThingBase is NSObject
+
+ (- (id)baseName is "RealThingBase"))
+
+
+(class RealThing is RealThingBase
+
+ (- (id)realName is "RealThing"))
+
+;; Standard Message forwarding:
+;; override -forwardInvocation:, -respondsToSelector:, -methodSignatureForSelector:
+(class MyRegularProxy is NSObject
+
+ (- (id)initWithObject:(id)delegate is
+ (self init)
+ (set @delegate delegate)
+ self)
+
+ (- (id)proxyName is "ARegularProxy")
+
+ (- (void) forwardInvocation:(id) invocation is
+ (set selector (invocation selector))
+ (if (@delegate respondsToSelector:selector)
+ (then
+ (invocation retainArguments)
+ (invocation invokeWithTarget:@delegate))
+ (else
+ (self doesNotRecognizeSelector:selector))))
+
+ (- (BOOL) respondsToSelector:(SEL) selector is
+ (set mySuperClass (RealThing class))
+ (set result (mySuperClass instancesRespondToSelector:selector))
+ (if (eq NO result)
+ (set result (@delegate respondsToSelector:selector)))
+ result)
+
+ (- (id)methodSignatureForSelector:(SEL) selector is
+ (@delegate methodSignatureForSelector:selector)))
+
+;; Fast-forwarding path:
+;; implement -forwardingTargetForSelector:
+(class MyFastProxy is NSObject
+
+ (- (id)initWithObject:(id)delegate is
+ (self init)
+ (set @delegate delegate)
+ self)
+
+ (- (id)proxyName is "AFastForwardingProxy")
+
+ (- (id)forwardingTargetForSelector:(SEL)selector is
+ (if (@delegate respondsToSelector:selector)
+ (then @delegate)
+ (else nil))))
+
+(class TestForwarding is NuTestCase
+
+ (- testNormalForwarding is
+ (set real ((RealThing alloc) init))
+ (set proxy ((MyRegularProxy alloc) initWithObject:real))
+ (assert_equal "RealThing" (real realName))
+ (assert_equal "ARegularProxy" (proxy proxyName))
+ (assert_equal "RealThing" (proxy realName))
+ (assert_equal "RealThingBase" (proxy baseName)))
+
+ (- testFastForwarding is
+ (set real ((RealThing alloc) init))
+ (set proxy ((MyFastProxy alloc) initWithObject:real))
+ (assert_equal "RealThing" (real realName))
+ (assert_equal "AFastForwardingProxy" (proxy proxyName))
+ (assert_equal "RealThing" (proxy realName))
+ (assert_equal "RealThingBase" (proxy baseName))))
+

0 comments on commit 77554a2

Please sign in to comment.