Skip to content

Instantly share code, notes, and snippets.

@jfahrenkrug
Created November 17, 2017 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfahrenkrug/2a503734a8b08d24acd5d1b49f095105 to your computer and use it in GitHub Desktop.
Save jfahrenkrug/2a503734a8b08d24acd5d1b49f095105 to your computer and use it in GitHub Desktop.
private override init() {
let jsCode = try? String.init(contentsOf: Bundle.main.url(forResource: "Sentimentalist.bundle", withExtension: "js")!)
// The Swift closure needs @convention(block) because JSContext's setObject:forKeyedSubscript: method
// expects an Objective-C compatible block in this instance.
// For more information check out https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html#//apple_ref/doc/uid/TP40014097-CH35-ID350
let nativeLog: @convention(block) (String) -> Void = { message in
NSLog("JS Log: \(message)")
}
// Create a new JavaScript context that will contain the state of our evaluated JS code.
self.context = JSContext(virtualMachine: self.vm)
// Register our native logging function in the JS context
self.context.setObject(nativeLog, forKeyedSubscript: "nativeLog" as NSString)
// Evaluate the JS code that defines the functions to be used later on.
self.context.evaluateScript(jsCode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment