Commit 4916f87658
4916f8765874e48e6457690c1a7d92f2316e74c3
parent: 21d1bae1db
Verified · cmc
cmc <hello@cleberg.net> · 2026-11-21 10:30 UTC
Stop a js block org-sbe runs after five seconds
Layout: unified · split
Sources/OrgApp/JavaScriptBlocks.swift
+19 −4
| @@ -36,11 +36,15 @@ public enum JavaScriptBlocks { |
| 36 | 36 | } |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | | /// Runs a js `job` without a session on the calling thread, as `org-sbe` in a table |
| 40 | | /// formula needs; nil for other jobs. |
| 41 | | public static func runHere(_ job: BabelJob) throws -> BabelOutput? { |
| 39 | /// Runs a js `job` without a session, as `org-sbe` in a table formula needs, waiting on the |
| 40 | /// calling thread at most `timeout`; nil for other jobs. |
| 41 | public static func runHere(_ job: BabelJob, timeout: Duration = .seconds(5)) throws -> BabelOutput? { |
| 42 | 42 | guard job.language == "js" || job.language == "javascript", case .process = job.runner else { return nil } |
| 43 | | return try Engine().main(job.input, run: Run()) |
| 43 | let run = Run() |
| 44 | let thread = Thread { run.end(Result { try Engine().main(job.input, run: run) }) } |
| 45 | thread.stackSize = 8 << 20 |
| 46 | thread.start() |
| 47 | return try run.result(within: timeout) |
| 44 | 48 | } |
| 45 | 49 | |
| 46 | 50 | struct Stopped: Error, CustomStringConvertible { |
| @@ -71,6 +75,7 @@ public enum JavaScriptBlocks { |
| 71 | 75 | private var result: Result<BabelOutput, Error>? |
| 72 | 76 | private var continuation: CheckedContinuation<BabelOutput, Error>? |
| 73 | 77 | private let wake = DispatchSemaphore(value: 0) |
| 78 | private let done = DispatchSemaphore(value: 0) |
| 74 | 79 | |
| 75 | 80 | var stopped: Bool { lock.withLock { result != nil } } |
| 76 | 81 | |
| @@ -84,6 +89,16 @@ public enum JavaScriptBlocks { |
| 84 | 89 | guard first else { return } |
| 85 | 90 | waiting?.resume(with: outcome) |
| 86 | 91 | wake.signal() |
| 92 | done.signal() |
| 93 | } |
| 94 | |
| 95 | /// Blocks until the run ends, ending it when `timeout` passes first. |
| 96 | func result(within timeout: Duration) throws -> BabelOutput { |
| 97 | let (seconds, attoseconds) = timeout.components |
| 98 | if done.wait(timeout: .now() + .nanoseconds(Int(seconds) * 1_000_000_000 + Int(attoseconds / 1_000_000_000))) == .timedOut { |
| 99 | end(.failure(Stopped(description: "The block took longer than \(timeout) and was stopped."))) |
| 100 | } |
| 101 | return try lock.withLock { result! }.get() |
| 87 | 102 | } |
| 88 | 103 | |
| 89 | 104 | func wait(_ continuation: CheckedContinuation<BabelOutput, Error>) { |
Sources/OrgApp/WorkspaceModel.swift
+1 −1
| @@ -66,7 +66,7 @@ public final class WorkspaceModel { |
| 66 | 66 | } |
| 67 | 67 | #if canImport(JavaScriptCore) |
| 68 | 68 | // `org-sbe` in table formulas runs js blocks in JavaScriptCore. |
| 69 | | TableBlocks.javaScript = JavaScriptBlocks.runHere |
| 69 | TableBlocks.javaScript = { try JavaScriptBlocks.runHere($0) } |
| 70 | 70 | #endif |
| 71 | 71 | } |
| 72 | 72 | |
Tests/OrgAppTests/JavaScriptBlocksTests.swift
+11 −1
| @@ -267,10 +267,20 @@ struct JavaScriptBlocksTests { |
| 267 | 267 | await #expect(throws: JavaScriptBlocks.Stopped.self) { try await JavaScriptBlocks.run(interval, directory: folder, timeout: .milliseconds(100)) } |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | /// A block `org-sbe` runs that doesn't finish is stopped, so recalculating can't hang. |
| 271 | @Test func runHereStopsAtItsDeadline() throws { |
| 272 | let start = ContinuousClock.now |
| 273 | let interval = try Self.plan("#+begin_src js\nsetInterval(() => {}, 10)\n#+end_src\n") |
| 274 | #expect(throws: JavaScriptBlocks.Stopped.self) { try JavaScriptBlocks.runHere(interval, timeout: .milliseconds(100)) } |
| 275 | #expect(ContinuousClock.now - start < .seconds(2)) |
| 276 | let quick = try Self.plan("#+begin_src js\nreturn 6*7\n#+end_src\n") |
| 277 | #expect(try JavaScriptBlocks.runHere(quick)?.stdout.contains("42") == true) |
| 278 | } |
| 279 | |
| 270 | 280 | /// `org-sbe` on js blocks in a table formula, as Emacs runs them with node. |
| 271 | 281 | @Test(.enabled(if: oracle && nodeInstalled)) |
| 272 | 282 | func tableFormulasMatchEmacs() throws { |
| 273 | | TableBlocks.javaScript = JavaScriptBlocks.runHere |
| 283 | TableBlocks.javaScript = { try JavaScriptBlocks.runHere($0) } |
| 274 | 284 | let text = """ |
| 275 | 285 | #+NAME: sq |
| 276 | 286 | #+begin_src js :var n=2 |