                 let delegate = object as? XWalkDelegate
                 if delegate?.setNativeProperty != nil {
                     let selector = Selector("setNativeProperty:value:")
-                    Invocation.call(object, selector: selector, arguments: [prop, value], thread: _thread)
+                    Invocation.call(object, selector: selector, arguments: [prop, value], thread: thread)
                 } else if mirror.hasProperty(prop) {
                     let selector = mirror.getSetter(prop)
                     if selector != Selector() {
-                        Invocation.call(object, selector: selector, arguments: [value], thread: _thread)
+                        Invocation.call(object, selector: selector, arguments: [value], thread: thread)
                     } else {
                         println("ERROR: Property '\(prop)' is readonly.")
                     }

            (object as? XWalkDelegate)?.didUnbindExtension?()
        } else if body["destroy"] != nil {
            // Destroy extension
            if _webView.URL != nil {
                evaluateJavaScript("delete \(_namespace);", completionHandler:nil)
            }
            _webView.configuration.userContentController.removeScriptMessageHandlerForName("\(_name)")
            if userScript != nil {
                _webView.configuration.userContentController.removeUserScript(userScript!)
            }
            for (_, object) in instances {
                (object as? XWalkDelegate)?.didUnbindExtension?()

import WebKit

public class XWalkChannel : NSObject, WKScriptMessageHandler {
    public var mirror: XWalkReflection!
    public let name: String
    private(set) public weak var webView: WKWebView!
    private(set) public var thread: NSThread = NSThread.mainThread()
    private(set) public var namespace: String = ""

    private var instances: [Int: AnyObject] = [:]
    private var userScript: WKUserScript?

    public init(webView: WKWebView, name: String? = nil) {
        struct seq{
            static var num: UInt32 = 0
        }

        self.webView = webView
        self.name = name ?? "\(++seq.num)"
        super.init()
        webView.configuration.userContentController.addScriptMessageHandler(self, name: "\(self.name)")
    }

    public func bind(object: AnyObject, namespace: String, thread: NSThread? = nil) {
        self.namespace = namespace
        self.thread = thread ?? webView.extensionThread
        if self.thread is XWalkThread && !self.thread.executing {
            self.thread.start()
        }

        mirror = XWalkReflection(cls: object.dynamicType)
        var script = XWalkStubGenerator(reflection: mirror).generate(name, namespace: namespace, object: object)
        let delegate = object as? XWalkDelegate
        script = delegate?.didGenerateStub?(script) ?? script

        userScript = webView.injectScript(script)
        delegate?.didBindExtension?(self, instance: 0)
        instances[0] = object
    }
