diff options
author | Matthias Andreas Benkard <matthias@benkard.de> | 2008-08-06 17:21:15 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <matthias@benkard.de> | 2008-08-06 17:21:15 +0200 |
commit | 757b74dbcbb78eee894b0bdb916923ffb2d8e99b (patch) | |
tree | 9080c7141cf71ea63df304184979e45500bb4180 /MLKRoot.m | |
parent | fd32094cf12d4894a3eb5a9512f2c74f90b61713 (diff) |
Generalise foreign value conversion.
Diffstat (limited to 'MLKRoot.m')
-rw-r--r-- | MLKRoot.m | 69 |
1 files changed, 26 insertions, 43 deletions
@@ -553,6 +553,7 @@ static id truify (BOOL value) SEL selector; NSMethodSignature *signature; int i; + MLKForeignType returnType; if (MLKFixnumP (object)) object = [MLKInteger integerWithFixnum:object]; @@ -579,55 +580,26 @@ static id truify (BOOL value) for (i = 2; i < [args count]; i++) { id argument = denullify ([args objectAtIndex:i]); - const char *type = [signature getArgumentTypeAtIndex:i]; - - if (strcmp (type, @encode(unichar)) == 0) - { - unichar arg; - if (MLKFixnumP (argument)) - arg = MLKIntWithFixnum (argument); - else if ([argument isKindOfClass:[MLKCharacter class]]) - arg = [argument unicharValue]; - else if ([argument isKindOfClass:[MLKInteger class]]) - arg = [argument intValue]; - else - [NSException raise:@"MLKInvalidArgumentError" - format:@"Don't know how to coerce %@ into type \"%s\".", - argument, type]; - [invocation setArgument:&arg atIndex:i]; - } - else - { - if (MLKFixnumP (argument)) - argument = [MLKInteger integerWithFixnum:argument]; - - [invocation setArgument:&argument atIndex:i]; - } + const char *objctype = [signature getArgumentTypeAtIndex:i]; + MLKForeignType type = MLKForeignTypeWithObjectiveCType (objctype); + ffi_type *ffi_argtype = MLKFFITypeWithForeignType (type); + void *argbuf = alloca (ffi_argtype->size); + + if (type == MLKT_INVALID) + [NSException raise:@"MLKInvalidArgumentError" + format:@"Don't know how to coerce %@ into type \"%s\".", + argument, objctype]; + + MLKSetForeignValueWithLispValue (argbuf, argument, type); + [invocation setArgument:argbuf atIndex:i]; } [invocation invoke]; -#define IF_TYPE_RETURN(TYPE, VALUE_NAME, VALUE) \ - if (strcmp ([signature methodReturnType], @encode(TYPE)) == 0) \ - { \ - TYPE VALUE_NAME; \ - [invocation getReturnValue:&VALUE_NAME]; \ - RETURN_VALUE (VALUE); \ - } + returnType = MLKForeignTypeWithObjectiveCType ([signature methodReturnType]); - if (strcmp ([signature methodReturnType], @encode(void)) == 0) - { - return [NSArray array]; - } - else IF_TYPE_RETURN (BOOL, retval, truify (retval)) - else IF_TYPE_RETURN (id, retval, retval) - else IF_TYPE_RETURN (Class, retval, retval) - else IF_TYPE_RETURN (NSException *, retval, retval) - else IF_TYPE_RETURN (int, retval, [MLKInteger integerWithInt:retval]) - else IF_TYPE_RETURN (unsigned int, retval, [MLKInteger integerWithInt:retval]) //FIXME - else IF_TYPE_RETURN (unichar, retval, [MLKCharacter characterWithUnichar:retval]) - else + if (returnType == MLKT_INVALID) { [NSException raise:@"MLKInvalidReturnTypeError" format:@"Cannot handle an Objective-C return type of \"%s\" \ @@ -635,5 +607,16 @@ as provided by method %@ of object %@", methodName, object, [signature methodReturnType]]; return nil; } + else if (returnType == MLKT_VOID) + { + return [NSArray array]; + } + else + { + ffi_type *ffi_rettype = MLKFFITypeWithForeignType (returnType); + void *returnValue = alloca (ffi_rettype->size); + [invocation getReturnValue:returnValue]; + RETURN_VALUE (MLKLispValueWithForeignValue (returnValue, returnType)); + } } @end |