Commit 136d9c584c
136d9c584c5289fad2918bc8758c2e99f1bba312
parent: 2af8f2b2ca
Verified · cmc
cmc <hello@cleberg.net> · 2026-11-21 19:50 UTC
Don't expand a Babel command twice to check for the JavaScriptCore fallback
Layout: unified · split
Sources/OrgPrograms/BabelRunner.swift
+3 −1
| @@ -24,8 +24,10 @@ public enum BabelRunner { |
| 24 | return paths.contains { FileManager.default.isExecutableFile(atPath: $0) } |
24 | return paths.contains { FileManager.default.isExecutableFile(atPath: $0) } |
| 25 | } |
25 | } |
| 26 | |
26 | |
| 27 | /// The words `/bin/sh` makes of `command`, after expanding `~`, variables and quotes. |
27 | /// The words `/bin/sh` makes of `command`, after expanding `~`, variables and quotes. Empty for |
| |
28 | /// a command substitution, which would otherwise run here as well as when the block runs. |
| 28 | public static func words(_ command: String) -> [String] { |
29 | public static func words(_ command: String) -> [String] { |
| |
30 | if command.contains("$(") || command.contains("`") { return [] } |
| 29 | guard command.contains(where: { "~$'\"`\\".contains($0) }) else { return command.split(whereSeparator: \.isWhitespace).map(String.init) } |
31 | guard command.contains(where: { "~$'\"`\\".contains($0) }) else { return command.split(whereSeparator: \.isWhitespace).map(String.init) } |
| 30 | let process = Process() |
32 | let process = Process() |
| 31 | process.executableURL = URL(fileURLWithPath: "/bin/sh") |
33 | process.executableURL = URL(fileURLWithPath: "/bin/sh") |
Sources/OrgPrograms/Programs.swift
+3 −1
| @@ -16,11 +16,13 @@ public struct Programs: ProgramRunning { |
| 16 | // js in JavaScriptCore when node (or `:cmd`) isn't installed. A `:cmd` with arguments runs |
16 | // js in JavaScriptCore when node (or `:cmd`) isn't installed. A `:cmd` with arguments runs |
| 17 | // as written, since JavaScriptCore can't take node's options. |
17 | // as written, since JavaScriptCore can't take node's options. |
| 18 | switch job.runner { |
18 | switch job.runner { |
| 19 | case .process(let executable, _): |
19 | case .process(let executable, _) where job.language == "js" || job.language == "javascript": |
| 20 | let words = BabelRunner.words(executable) |
20 | let words = BabelRunner.words(executable) |
| 21 | if words.count == 1, !BabelRunner.installed(words[0]), let output = try await JavaScriptBlocks.run(job, directory: fileDirectory) { return output } |
21 | if words.count == 1, !BabelRunner.installed(words[0]), let output = try await JavaScriptBlocks.run(job, directory: fileDirectory) { return output } |
| 22 | case .session(_, let executable, _): |
22 | case .session(_, let executable, _): |
| 23 | if !executable.contains(where: \.isWhitespace), !BabelRunner.installed(executable), let output = try await JavaScriptBlocks.run(job, directory: fileDirectory) { return output } |
23 | if !executable.contains(where: \.isWhitespace), !BabelRunner.installed(executable), let output = try await JavaScriptBlocks.run(job, directory: fileDirectory) { return output } |
| |
24 | case .process: |
| |
25 | break |
| 24 | case .emacs: |
26 | case .emacs: |
| 25 | // Emacs Lisp in `Elisp` when Emacs isn't installed. |
27 | // Emacs Lisp in `Elisp` when Emacs isn't installed. |
| 26 | if emacs() == nil { |
28 | if emacs() == nil { |
Tests/OrgProgramsTests/ProgramsTests.swift
+14
| @@ -282,6 +282,20 @@ struct ShellCommandTests { |
| 282 | #expect(BabelRunner.words("'a b' \"$HOME\"") == ["a b", NSHomeDirectory()]) |
282 | #expect(BabelRunner.words("'a b' \"$HOME\"") == ["a b", NSHomeDirectory()]) |
| 283 | } |
283 | } |
| 284 | |
284 | |
| |
285 | /// A command substitution in the command runs once, when the block runs, as in Org. |
| |
286 | @Test(arguments: ["js :cmd", "python :python", "ruby :ruby"]) |
| |
287 | func substitutionRunsOnce(header: String) async throws { |
| |
288 | #expect(BabelRunner.words("$(echo node)").isEmpty) |
| |
289 | #expect(BabelRunner.words("`echo node`").isEmpty) |
| |
290 | let folder = try Folder() |
| |
291 | let count = folder.url.appendingPathComponent("count").path |
| |
292 | let text = "#+begin_src \(header) \"$(echo x >> '\(count)'; echo cat)\" :results output\n1\n#+end_src\n" |
| |
293 | let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [2..<2]) |
| |
294 | guard case .success(let job) = Babel.plan(context) else { throw Babel.Failure.message("not planned") } |
| |
295 | _ = try await Programs().runBabel(job, fileDirectory: folder.url) |
| |
296 | #expect(try String(contentsOfFile: count, encoding: .utf8) == "x\n") |
| |
297 | } |
| |
298 | |
| 285 | @Test(.enabled(if: BabelRunner.installed("python3"))) |
299 | @Test(.enabled(if: BabelRunner.installed("python3"))) |
| 286 | func pythonWithOptions() async throws { |
300 | func pythonWithOptions() async throws { |
| 287 | let text = "#+begin_src python :python \"python3 -X utf8\" :results output\nimport sys\nprint(sys.flags.utf8_mode)\n#+end_src\n" |
301 | let text = "#+begin_src python :python \"python3 -X utf8\" :results output\nimport sys\nprint(sys.flags.utf8_mode)\n#+end_src\n" |
docs/manual/guide/11-code-blocks.org
+1 −1
| @@ -107,7 +107,7 @@ The [[file:../install.org][Mac App Store version]] starts no programs, so only E |
| 107 | |
107 | |
| 108 | ** JavaScript in JavaScriptCore |
108 | ** JavaScript in JavaScriptCore |
| 109 | |
109 | |
| 110 | In the Mac App Store version and on iOS, =js= and =javascript= blocks run in JavaScriptCore, the JavaScript engine built into macOS and iOS. The download version uses it when =node=, or the program in =:cmd= after =/bin/sh= expands it, isn't installed; a =:cmd= with options, such as =node --no-warnings=, never runs in JavaScriptCore. A block runs as Emacs runs it, with =node= reading it from a file as a CommonJS module, where =this= is =module.exports= and top-level =var= declarations stay in the block. Its output and result are what node gives: |
110 | In the Mac App Store version and on iOS, =js= and =javascript= blocks run in JavaScriptCore, the JavaScript engine built into macOS and iOS. The download version uses it when =node=, or the program in =:cmd= after =/bin/sh= expands it, isn't installed; a =:cmd= with options, such as =node --no-warnings=, or with a command substitution, such as =$(which node)=, never runs in JavaScriptCore. A block runs as Emacs runs it, with =node= reading it from a file as a CommonJS module, where =this= is =module.exports= and top-level =var= declarations stay in the block. Its output and result are what node gives: |
| 111 | |
111 | |
| 112 | - =console.log=, =console.info= and =console.debug= write to the output, =console.warn= and =console.error= to the error output, and =console.dir= prints with =util.inspect=. =process.stdout.write= and =process.stderr.write= write text. |
112 | - =console.log=, =console.info= and =console.debug= write to the output, =console.warn= and =console.error= to the error output, and =console.dir= prints with =util.inspect=. =process.stdout.write= and =process.stderr.write= write text. |
| 113 | - =require('util')= has =inspect=, =format= and =formatWithOptions=, which print values as Node 26 prints them. =require('process')= has =stdout= and =stderr=. |
113 | - =require('util')= has =inspect=, =format= and =formatWithOptions=, which print values as Node 26 prints them. =require('process')= has =stdout= and =stderr=. |