I'm running into some weird issues with JavaScriptCore. Whenever I add a method to my class that has a parameter, it doesn't seem to be exported (or is exported with a weird name). For example:
import Cocoa
import JavaScriptCore
@objc
public protocol MyObjectExport: JSExport {
var property: Int { get set }
init()
func test() -> String
func doNothing(num: Int)
func squared(num: Int) -> Int
func sum(a:Int, _ b: Int) -> Int
}
@objc
public class MyObject: NSObject, MyObjectExport {
public var property: Int {
get {
return 5
}
set {
print("New value \(newValue)")
}
}
public required override init() {
}
public func test() -> String {
return "Tested"
}
public func doNothing(num: Int) {
}
public func squared(num: Int) -> Int {
return num * num
}
public func sum(a: Int, _ b: Int) -> Int {
return a + b
}
}
class ViewController: NSViewController {
override func viewDidLoad() {
let context = JSContext()!
context.exceptionHandler = { context, exc in
print("Exception \(exc)")
}
context.setObject(MyObject.self, forKeyedSubscript: NSString(string: "MyObject"))
context.evaluateScript("var obj = new MyObject()")
print(context.evaluateScript("obj"))
print(context.evaluateScript("obj.test()"))
print(context.evaluateScript("obj.doNothing(5)"))
print(context.evaluateScript("obj.squared(5)"))
print(context.evaluateScript("obj.sum(5,5)"))
}
}
Note that this has to be tested in a macOS app. JavaScriptCore acts even weirder in a playground.
When this is ran, I get the output
<TestProject.MyObject: 0x608000001180>
Tested
Exception Optional(TypeError: obj.doNothing is not a function. (In 'obj.doNothing(5)', 'obj.doNothing' is undefined))
undefined
Exception Optional(TypeError: obj.squared is not a function. (In 'obj.squared(5)', 'obj.squared' is undefined))
undefined
Exception Optional(TypeError: obj.sum is not a function. (In 'obj.sum(5,5)', 'obj.sum' is undefined))
undefined
As you can see, the initiator works and so does the method test. However, all other methods with parameters do not seem to be exported or they are exported under some other method name I can not find.
Any help would be much appreciated.