IntentHandler.swift 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // IntentHandler.swift
  3. // RunCScriptCommandHandler
  4. //
  5. // Created by 邢铖 on 2022/7/6.
  6. // Copyright © 2022 xcbosa. All rights reserved.
  7. //
  8. import UIKit
  9. import Intents
  10. class IntentHandler: INExtension, RunCScriptIntentHandling {
  11. override func handler(for intent: INIntent) -> Any { self }
  12. func handle(intent: RunCScriptIntent, completion: @escaping (RunCScriptIntentResponse) -> Void) {
  13. let runner = RunnerDriver()
  14. let files = [
  15. CodeFile(fileName: "CDEnvC-Run-Script.c", content: intent.source ?? "")
  16. ]
  17. let env = CodeEnvironment(files: files, startIndex: 0)
  18. DispatchQueue.global(qos: .userInitiated).async {
  19. runner.runCode(inEnvironment: env)
  20. completion(.success(output: runner.stdout))
  21. }
  22. // while runner.processThread != nil {
  23. // usleep(20000)
  24. // }
  25. }
  26. func resolveSource(for intent: RunCScriptIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
  27. completion(.success(with: intent.source ?? ""))
  28. }
  29. }