Quick blocks do not work

I was trying to figure out how to use JavaScriptCore in swift. I run into problems, however, when I have to deal with blocks as arguments, it looks like the block starts immediately and the arguments get the return value of the block. What am I doing wrong?

Work Code Goal C:

JSContext* context = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]]; context[@"test"] = ^(NSString *string) { //code }; 

What I tried:

one

 var ctx = JSContext(virtualMachine:JSVirtualMachine()) var ctx["test"] = {(string:NSString)->() in /*code*/ } //Gives me "'JSContext' does not have a member named 'subscript'" 

2:

 var ctx = JSContext(virtualMachine:JSVirtualMachine()) let n: (string: String)->() = {string in /*code*/} ctx.setObject(n, forKeyedSubscript:"test") //Gives me "Type '(x: String) -> () does not conform to protocol 'AnyObject'" 

3:

 var ctx = JSContext(virtualMachine:JSVirtualMachine()) let n: (string: String)->() = {string in /*code*/} ctx.setObject(n as AnyObject, forKeyedSubscript:"test") //Gives me "Cannot downcast from '(string: String) -> () to non-@objc protocol type 'AnyObject'" 

Am I missing something, or is this just a bug in Swift?

Edit:

Now I also tried offers from Closing / blocking clips

 class Block<T> { let f : T init (_ f: T) { self.f = f } } 

and then

 ctx.setObject(Block<()->Void> { /*code*/ }, forKeyedSubscript: "test") 

This solution allows me to compile, but I get a runtime error:

 Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0) 
+7
block swift javascriptcore
source share
2 answers

This has something to do with how Swift shuts down. You should use @convention(block) to make it clear that the closure is an ObjC block. Use unsafeBitCast to force it

 var block : @convention(block) (NSString!) -> Void = { (string : NSString!) -> Void in println("test") } ctx.setObject(unsafeBitCast(block, AnyObject.self), forKeyedSubscript: "test") 

from REPL

 swift Welcome to Swift! Type :help for assistance. 1> import Foundation 2> var block : @convention(block)(NSString!) -> Void = {(string : NSString!) -> Void in println("test")} block: @convention(block)(NSString!) -> Void = 3> var obj: AnyObject = reinterpretCast(block) as AnyObject obj: __NSMallocBlock__ = {} // familiar block type 
+27
source share

I have a working demo at:

And here is the part that implements block registration:

 typealias ID = AnyObject! extension JSContext { func fetch(key:NSString)->JSValue { return getJSVinJSC(self, key) } func store(key:NSString, _ val:ID) { setJSVinJSC(self, key, val) } func store(key:NSString, _ blk:()->ID) { setB0JSVinJSC(self, key, blk) } func store(key:NSString, _ blk:(ID)->ID) { setB1JSVinJSC(self, key, blk) } func store(key:NSString, _ blk:(ID,ID)->ID) { setB2JSVinJSC(self, key, blk) } } 

You need a very small objc code and a title bar to make it work. See the repository for details.

+1
source share

All Articles