Skip to content

Commit

Permalink
Merge branch 'amalgamated' of https://github.com/philipwhite/nu into …
Browse files Browse the repository at this point in the history
…amalgamated
  • Loading branch information
timburks committed Nov 27, 2011
2 parents 41865a8 + 217c2ea commit 30e5c09
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
7 changes: 2 additions & 5 deletions nu/cblocks.nu
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
(try ((NuBridgedBlock class))
(catch (execption) (throw* "NuException" "This build of Nu does not support C blocks.")))

(function __get_type_signature (identifier)
(((NuBridgedFunction functionWithName:"signature_for_identifier" signature:"@@@") identifier
(NuSymbolTable sharedSymbolTable))))
(set __sig (__get_type_signature (list ret)))
(set __sig (signature (list ret)))
(set __blockparams ())
(set __paramlist params)
(until (eq __paramlist nil)
Expand All @@ -42,7 +39,7 @@
(set __param (car (cdr __paramlist)))
(set __paramlist (cdr (cdr __paramlist)))
(set __sig (__sig stringByAppendingString:
(__get_type_signature __type)))
(signature __type)))
(set __blockparams (append __blockparams (list __param))))
;(puts "Signature: #{__sig}")
;(puts "Block params: #{__blockparams}")
Expand Down
45 changes: 18 additions & 27 deletions objc/Nu.m
Original file line number Diff line number Diff line change
Expand Up @@ -1730,26 +1730,9 @@ static id nu_calling_objc_method_handler(id target, Method m, NSMutableArray *ar

// dynamically construct the method call

//the method_***** functions seems to count c blocks twice, i.e. they separate
//the @ and ?. Using an NSMethodSignature seems to be an easy way around it.
//However, it appears to have some flaws as it causes 'nuke test' to fail
#define USE_SIG 1

#if USE_SIG
NSMethodSignature *sig = [target methodSignatureForSelector:s];
NSUInteger argument_count = [sig numberOfArguments];
BOOL zeroArguments = NO;
if (argument_count == 0)
{
// - [NSMethodSignature numberOfArguments] returns 0 if there are no arguments, but we expect 2 (cmd and self).
// If we get zero, we use method_getNumberOfArguments() here, and method_getArgumentType() below.
// This works around Apple's bug in the method_*** functions, but allows 'nuke test' to pass
argument_count = method_getNumberOfArguments(m);
zeroArguments = YES;
}
#else

int argument_count = method_getNumberOfArguments(m);
#endif

if ( [args count] != argument_count-2) {

raise_argc_exception(s, argument_count-2, [args count]);
Expand All @@ -1764,15 +1747,8 @@ static id nu_calling_objc_method_handler(id target, Method m, NSMutableArray *ar
int *argument_needs_retained = (int *) malloc (argument_count * sizeof(int));
int i;
for (i = 0; i < argument_count; i++) {
#if USE_SIG
if (zeroArguments) {
method_getArgumentType(m, i, &arg_type_buffer[0], BUFSIZE);
} else {
strncpy(&arg_type_buffer[0], [sig getArgumentTypeAtIndex:i], BUFSIZE);
}
#else

method_getArgumentType(m, i, &arg_type_buffer[0], BUFSIZE);
#endif

argument_types[i] = ffi_type_for_objc_type(&arg_type_buffer[0]);
argument_values[i] = value_buffer_for_objc_type(&arg_type_buffer[0]);
Expand Down Expand Up @@ -8966,6 +8942,19 @@ - (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context

@end

@interface Nu_signature_operator : NuOperator {}
@end

@implementation Nu_signature_operator

// signature operator; basically gives access to the static signature_for_identifier function from within Nu code
- (id) callWithArguments:(id)cdr context:(NSMutableDictionary *)context
{
return signature_for_identifier( [[cdr car] evalWithContext:context],[NuSymbolTable sharedSymbolTable]);
}

@end

#define install(name, class) [(NuSymbol *) [symbolTable symbolWithString:name] setValue:[[[class alloc] init] autorelease]]

void load_builtins(NuSymbolTable *symbolTable);
Expand Down Expand Up @@ -9098,6 +9087,8 @@ void load_builtins(NuSymbolTable *symbolTable)
install(@"?", Nu_help_operator);
install(@"version", Nu_version_operator);

install(@"signature", Nu_signature_operator);

// set some commonly-used globals
[(NuSymbol *) [symbolTable symbolWithString:@"NSUTF8StringEncoding"]
setValue:[NSNumber numberWithInt:NSUTF8StringEncoding]];
Expand Down
12 changes: 11 additions & 1 deletion test/test_bridge.nu
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,15 @@
(assert_equal (list 1 2 3 4) (NuTestHelper getCGRectFromProxy:structHelper))
(assert_equal (list 1 2) (NuTestHelper getCGPointFromProxy:structHelper))
(assert_equal (list 3 4) (NuTestHelper getCGSizeFromProxy:structHelper))
(assert_equal (list 5 6) (NuTestHelper getNSRangeFromProxy:structHelper))))
(assert_equal (list 5 6) (NuTestHelper getNSRangeFromProxy:structHelper)))
(- (id) testBlocks is
(load "cblocks")
(let ((num nil)
(num-array (array 1 2)))
(set equals-num? (cblock BOOL ((id) obj (unsigned long) idx (void*) stop)
(if (== obj num) YES (else NO))))
(set num 1)
(assert_equal 0 (num-array indexOfObjectPassingTest:equals-num?))
(set num 2)
(assert_equal 1 (num-array indexOfObjectPassingTest:equals-num?)))))

0 comments on commit 30e5c09

Please sign in to comment.