Я пытаюсь передать параметры в JSContext, как в Android, который мы можем передать.
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine scriptEngine = factory.getEngineByName("javascript");
Bindings vars = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
vars.put("renderPage", renderPage);
ниже - Мой код.
var bufferString : String = String()
jsContext = JSContext()
if let jsSourcePath = Bundle.main.path(forResource: "parser", ofType: "txt") {
do {
let jsSourceContents = try String(contentsOfFile: jsSourcePath)
var myTempDictionary : Dictionary = Dictionary<String,AnyObject>()
myTempDictionary.updateValue(renderPage as AnyObject, forKey: "rendePage")
myTempDictionary.updateValue(parseResult as AnyObject, forKey: "parseResult")
myTempDictionary.updateValue(logFireRuleException, forKey: "logFireRuleException")
myTempDictionary.updateValue(separatorRenderLogic as AnyObject, forKey: "sepratorRenderLogic")
myTempDictionary.updateValue(isHaveBeep! as AnyObject, forKey: "isHaveBeep")
myTempDictionary.updateValue(listButton as AnyObject, forKey: "listButton")
myTempDictionary.updateValue(listField as AnyObject, forKey: "listField")
myTempDictionary.updateValue(isList as AnyObject, forKey: "isList")
myTempDictionary.updateValue(isPageList as AnyObject, forKey: "isPageList")
myTempDictionary.updateValue(isInputSerial as AnyObject, forKey: "isInputSerial")
myTempDictionary.updateValue(LogFireParser.textUsers as AnyObject, forKey: "textUsers")
myTempDictionary.updateValue(LogFireParser.textPasswords as AnyObject, forKey: "textPasswords")
myTempDictionary.updateValue(cursorY as AnyObject, forKey: "cursorY")
myTempDictionary.updateValue(LogFireParser.SUBTITLE_SEPARATOR as AnyObject, forKey: "SUBTITLE_SEPRATOR")
let lines = jsSourceContents.components(separatedBy: .newlines)
for line in lines
{
bufferString.append(line + "\n")
}
print(bufferString)
jsContext.virtualMachine.setValuesForKeys(myTempDictionary)
self.jsContext.exceptionHandler = { context, exception in
if let exc = exception {
print("JS Exception:", exc.toString())
}
}
let myJSResult = self.jsContext.evaluateScript(bufferString)
let testFunction = jsContext.objectForKeyedSubscript("result")
let result = testFunction?.call(withArguments: [myTempDictionary])
//let result = testFunction?.
print(result)
}
catch {
print(error.localizedDescription)
}
}
Здесь, в tempDictionary, я пытался передать параметры и на самом деле в Javascript нет функции, это простой Javascript файл, который возвращает словарь после оценки, он работает в Android правильно, я столкнулся с проблемой в iOS.