Skip to content
This repository
Browse code

Merge pull request #23 from chapados/forwarding-lion

Forwarding lion
  • Loading branch information...
commit 77554a2947cf56357a49791a02e38c5220c76e6e 2 parents a2efa26 + 82347bd
Tim Burks authored
5 objc/NuHandler.m
@@ -64,6 +64,11 @@ static id collect_arguments(struct handler_description *description, va_list ap)
64 64 //NSLog(@"argument is %lf", x);
65 65 [cursor setCar:get_nu_value_from_objc_value(&x, type)];
66 66 }
  67 + else if (!strcmp(type, ":")) {
  68 + SEL x = va_arg(ap, SEL);
  69 + //NSLog(@"collect_arguments: [:] (SEL) = %@", NSStringFromSelector(x));
  70 + [cursor setCar:get_nu_value_from_objc_value(&x, type)];
  71 + }
67 72 else if (!strcmp(type, "{CGRect={CGPoint=ff}{CGSize=ff}}")
68 73 || (!strcmp(type, "{CGRect=\"origin\"{CGPoint=\"x\"f\"y\"f}\"size\"{CGSize=\"width\"f\"height\"f}}"))) {
69 74 CGRect x = va_arg(ap, CGRect);
5 objc/NuObject.m
@@ -244,6 +244,10 @@ - (id) sendMessage:(id)cdr withContext:(NSMutableDictionary *)context
244 244 // Messaging null is ok.
245 245 else if (self == Nu__null) {
246 246 }
  247 + else if ( (target = [target forwardingTargetForSelector:sel]) ) {
  248 + //NSLog(@"found forwarding target: %@ for selector: %@", target, NSStringFromSelector(sel));
  249 + result = [target sendMessage:cdr withContext:context];
  250 + }
247 251 // Otherwise, call the overridable handler for unknown messages.
248 252 else {
249 253 //NSLog(@"calling handle unknown message for %@", [cdr stringValue]);
@@ -258,7 +262,6 @@ - (id) sendMessage:(id)cdr withContext:(NSMutableDictionary *)context
258 262 [result autorelease];
259 263 return result;
260 264 }
261   -
262 265 - (id) evalWithArguments:(id)cdr context:(NSMutableDictionary *)context
263 266 {
264 267 return [self sendMessage:cdr withContext:context];
78 test/test_forwarding.nu
... ... @@ -0,0 +1,78 @@
  1 +;; test_forwarding.nu
  2 +;; tests for message forwarding in Nu
  3 +;;
  4 +;; Copyright (c) 2011 Brian Chapados
  5 +
  6 +(class RealThingBase is NSObject
  7 +
  8 + (- (id)baseName is "RealThingBase"))
  9 +
  10 +
  11 +(class RealThing is RealThingBase
  12 +
  13 + (- (id)realName is "RealThing"))
  14 +
  15 +;; Standard Message forwarding:
  16 +;; override -forwardInvocation:, -respondsToSelector:, -methodSignatureForSelector:
  17 +(class MyRegularProxy is NSObject
  18 +
  19 + (- (id)initWithObject:(id)delegate is
  20 + (self init)
  21 + (set @delegate delegate)
  22 + self)
  23 +
  24 + (- (id)proxyName is "ARegularProxy")
  25 +
  26 + (- (void) forwardInvocation:(id) invocation is
  27 + (set selector (invocation selector))
  28 + (if (@delegate respondsToSelector:selector)
  29 + (then
  30 + (invocation retainArguments)
  31 + (invocation invokeWithTarget:@delegate))
  32 + (else
  33 + (self doesNotRecognizeSelector:selector))))
  34 +
  35 + (- (BOOL) respondsToSelector:(SEL) selector is
  36 + (set mySuperClass (RealThing class))
  37 + (set result (mySuperClass instancesRespondToSelector:selector))
  38 + (if (eq NO result)
  39 + (set result (@delegate respondsToSelector:selector)))
  40 + result)
  41 +
  42 + (- (id)methodSignatureForSelector:(SEL) selector is
  43 + (@delegate methodSignatureForSelector:selector)))
  44 +
  45 +;; Fast-forwarding path:
  46 +;; implement -forwardingTargetForSelector:
  47 +(class MyFastProxy is NSObject
  48 +
  49 + (- (id)initWithObject:(id)delegate is
  50 + (self init)
  51 + (set @delegate delegate)
  52 + self)
  53 +
  54 + (- (id)proxyName is "AFastForwardingProxy")
  55 +
  56 + (- (id)forwardingTargetForSelector:(SEL)selector is
  57 + (if (@delegate respondsToSelector:selector)
  58 + (then @delegate)
  59 + (else nil))))
  60 +
  61 +(class TestForwarding is NuTestCase
  62 +
  63 + (- testNormalForwarding is
  64 + (set real ((RealThing alloc) init))
  65 + (set proxy ((MyRegularProxy alloc) initWithObject:real))
  66 + (assert_equal "RealThing" (real realName))
  67 + (assert_equal "ARegularProxy" (proxy proxyName))
  68 + (assert_equal "RealThing" (proxy realName))
  69 + (assert_equal "RealThingBase" (proxy baseName)))
  70 +
  71 + (- testFastForwarding is
  72 + (set real ((RealThing alloc) init))
  73 + (set proxy ((MyFastProxy alloc) initWithObject:real))
  74 + (assert_equal "RealThing" (real realName))
  75 + (assert_equal "AFastForwardingProxy" (proxy proxyName))
  76 + (assert_equal "RealThing" (proxy realName))
  77 + (assert_equal "RealThingBase" (proxy baseName))))
  78 +

0 comments on commit 77554a2

Please sign in to comment.
Something went wrong with that request. Please try again.