Skip to content

Commit

Permalink
Added a signature operator to allow nu code an easy way to convert sy…
Browse files Browse the repository at this point in the history
…mbols to ObjC type signatures.

Updated the cblock macro in cblocks.nu to use the signature operator.
Added a test for c blocks to the TestBridge class.
  • Loading branch information
philipwhite committed Oct 16, 2011
1 parent 975bb7e commit 217c2ea
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
7 changes: 2 additions & 5 deletions nu/cblocks.nu
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
15 changes: 15 additions & 0 deletions objc/Nu.m
Expand Up @@ -8925,6 +8925,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 @@ -9057,6 +9070,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
6 changes: 3 additions & 3 deletions test/test_bridge.nu
Expand Up @@ -39,8 +39,8 @@
(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?))
(set num 3)
(assert_equal NSNotFound (num-array indexOfObjectPassingTest:equals-num?)))))
(assert_equal 1 (num-array indexOfObjectPassingTest:equals-num?)))))

0 comments on commit 217c2ea

Please sign in to comment.