参数未从 class 导出的 JavaScriptCore 方法

JavaScriptCore methods with parameters not being exported from class

我 运行 遇到了 JavaScriptCore 的一些奇怪问题。每当我向我的 class 添加一个带有参数的方法时,它似乎并没有被导出(或者以一个奇怪的名称导出)。例如:

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)"))

    }
}

请注意,这必须在 macOS 应用程序中进行测试。 JavaScriptCore 在操场上表现得更奇怪。

当这是运行时,我得到输出

<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

如您所见,启动器有效,方法 test 也有效。但是,所有其他带参数的方法似乎都没有导出,或者它们以我找不到的其他方法名称导出。

如有任何帮助,我们将不胜感激。

从Swift3开始,第一个参数不再默认未命名。

您不能从 Swift 代码中调用 doNothingsquaredsum

doNothing(5)
squared(5)
sum(5,5)

您必须包含参数名称:

doNothing(num: 5)
squared(num: 5)
sum(a: 5,5)    //argument name not required for 'b' because it is '_'

这些方法获得 Objective-C 选择器 doNothingWithNum:squaredWithNum:sumWithA::。根据 the documentation for JSExport:

,这些导出到 JavaScript 的规则如下

When exporting a selector that takes one or more arguments, JavaScriptCore generates a corresponding function name using the following conversion:

  • All colons are removed from the selector.

  • Any lowercase letter that had followed a colon is capitalized.

所以doNothingsquaredsum被称为doNothingWithNum()squaredWithNum()sumWithA()。您必须更改 JavaScript 以调用具有这些名称的方法:

print(context.evaluateScript("obj.doNothingWithNum(5)"))
print(context.evaluateScript("obj.squaredWithNum(5)"))
print(context.evaluateScript("obj.sumWithA(5,5)"))

或者更改您的 class 和协议定义以删除参数名称:

@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
    }
}

Swift 3

/// 创建协议,继承JSExport

@objc protocol MYPJSExport: JSExport {
    var sum: Int {get set}

    func doNothing()

    func squared(_ num: Int) -> Int
    func add(_ a: Int, _ b: Int) -> Int

    func add(num: Int) -> Int
    func add(num1: Int, num2: Int) -> Int
    func add(num1: Int, _ num2: Int) -> Int
//    func add(_ num1: Int, num2: Int) -> Int //the first external parameter is omitted,JS can't call
}

//注意:@objc不能丢

@objc class MYPObject: NSObject, MYPJSExport {
    var sum: Int = 0 {
        willSet{
            print("newValue: \(newValue)  |CurrentThread: \(Thread.current)")
        }
        didSet{
            print("oldValue: \(oldValue)  |CurrentThread: \(Thread.current)")
        }
    }

    func add(_ a: Int, _ b: Int) -> Int {
        return a + b
    }
    func doNothing(){
        print("doNothing--")
    }
    func squared(_ num: Int) -> Int {
        return num * num
    }

    func add(num: Int) -> Int {
        return num + 10
    }
    func add(num1: Int, num2: Int) -> Int {
        return num1 + num2
    }
    func add(num1: Int, _ num2: Int) -> Int {
        return num1 * num2
    }


//    func add(_ num1: Int, num2: Int) -> Int {
//        return (num1 + num2) * 2
//    }
}

JS 在 ViewController.

上调用 Swift 方法
class ViewController: UIViewController {
let obj: MYPObject = MYPObject()
let context:JSContext = JSContext()

override func viewDidLoad() {
    super.viewDidLoad()

    context.exceptionHandler = { (context, exception) in
        guard let exce = exception else { return }
        context!.exception = exce
        print("JS exception: \(exce)")
    }
    let block: @convention(block) () -> () = {
        print("++++++Begin Log++++++")
        let args = JSContext.currentArguments()
        for jsVal in args! {
            print(jsVal)
        }
        print("---End Log------")
    }
    context.setObject(block, forKeyedSubscript: NSString(string: "log"))
    context.setObject(obj, forKeyedSubscript: NSString(string: "Swiftobj"))


    print(context.evaluateScript("log(Swiftobj.doNothing(5))"))
    print(context.evaluateScript("log(Swiftobj.squared(5))"))
    print(context.evaluateScript("log(Swiftobj.add(5,5))"))
    print(context.evaluateScript("log(Swiftobj.addWithNum(5))"))
    print(context.evaluateScript("log(Swiftobj.addWithNum1Num2(10,10))"))
    print(context.evaluateScript("log(Swiftobj.addWithNum1(10,10))"))
//        print(context.evaluateScript("log(Swiftobj.addWithNum2(10,10))")) // 'Swiftobj.addWithNum2' is undefined

    context.evaluateScript("Swiftobj.sum = Swiftobj.add(2,3)")
    print(context.evaluateScript("log(Swiftobj.sum)"))
    print("obj.sum: \(obj.sum)")
}

}