Support for legacy script (#1)

This commit is contained in:
CelloSerenity 2026-06-29 05:10:34 -06:00 • committed by GitHub
parent 5bdecfcafa
commit 2320badda8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 128 additions and 13 deletions

View file

@ -2,9 +2,9 @@ import Foundation
enum BundledScript {
static func universalJS() throws -> String {
static func source(for script: StikJIT.Script) throws -> String {
let bundle = Bundle(for: BundleToken.self)
guard let url = bundle.url(forResource: "universal", withExtension: "js"),
guard let url = bundle.url(forResource: script.resourceName, withExtension: "js"),
let source = try? String(contentsOf: url, encoding: .utf8),
!source.isEmpty else {
throw StikJITError.scriptUnavailable

View file

@ -29,7 +29,7 @@ final class JITSession {
self.configuration = configuration
}
func enableJIT(targetPID: Int32, progress: @escaping (String) -> Void) throws {
func enableJIT(targetPID: Int32, script: StikJIT.Script, progress: @escaping (String) -> Void) throws {
let tunnel = try makeTunnel()
defer { tunnel.free() }
@ -43,7 +43,7 @@ final class JITSession {
debug_proxy_set_ack_mode(debugProxy, 0)
if ProcessInfo.processInfo.hasTXM {
try ScriptRunner(targetPID: targetPID, debugProxy: debugProxy, progress: progress).run()
try ScriptRunner(targetPID: targetPID, debugProxy: debugProxy, script: script, progress: progress).run()
} else {
try attachWithoutScript(targetPID: targetPID, debugProxy: debugProxy, progress: progress)
}

View file

@ -8,17 +8,19 @@ final class ScriptRunner {
private let targetPID: Int32
private let debugProxy: OpaquePointer
private let script: StikJIT.Script
private let progress: (String) -> Void
private var context: JSContext?
init(targetPID: Int32, debugProxy: OpaquePointer, progress: @escaping (String) -> Void) {
init(targetPID: Int32, debugProxy: OpaquePointer, script: StikJIT.Script, progress: @escaping (String) -> Void) {
self.targetPID = targetPID
self.debugProxy = debugProxy
self.script = script
self.progress = progress
}
func run() throws {
let source = try BundledScript.universalJS()
let source = try BundledScript.source(for: script)
guard let context = JSContext() else { throw StikJITError.scriptUnavailable }
self.context = context
@ -45,7 +47,7 @@ final class ScriptRunner {
context.setObject(prepare, forKeyedSubscript: "prepare_memory_region" as NSString)
context.setObject(log, forKeyedSubscript: "log" as NSString)
progress("Running universal.js against pid \(targetPID)…")
progress("Running \(script.resourceName).js against pid \(targetPID)…")
context.evaluateScript(source)
progress("JIT script finished (region blessed, detached).")
}

View file

@ -2,6 +2,20 @@ import Foundation
public enum StikJIT {
public enum Script: Sendable {
case universal
case legacy
var resourceName: String {
switch self {
case .universal: return "universal"
case .legacy: return "legacy"
}
}
}
public struct Configuration: Sendable {
public var deviceAddress: String
@ -19,8 +33,9 @@ public enum StikJIT {
public static func enableJIT(targetPID: Int32,
pairingFile: URL,
configuration: Configuration = .default,
script: Script = .universal,
progress: @escaping (String) -> Void = { _ in }) throws {
let session = JITSession(pairingFilePath: pairingFile.path, configuration: configuration)
try session.enableJIT(targetPID: targetPID, progress: progress)
try session.enableJIT(targetPID: targetPID, script: script, progress: progress)
}
}

View file

@ -15,7 +15,7 @@ public enum StikJITError: Error, LocalizedError {
case .pairingFile(let detail):
return "Pairing file error: \(detail)."
case .scriptUnavailable:
return "The bundled universal.js could not be loaded from the StikJIT framework."
return "The bundled JIT script could not be loaded from the StikJIT framework."
case .debugProxyUnavailable:
return "Failed to establish a debugserver connection to the target process."
case .device(let code, let subCode, let message):