diff options
| author | Arun Isaac | 2025-11-11 17:55:44 +0000 |
|---|---|---|
| committer | Arun Isaac | 2025-11-16 22:43:00 +0000 |
| commit | a39bc5776c9a93492ef0cbafb44b354d78492e72 (patch) | |
| tree | c98d386af28c6fe08a012e9c24f176d82d86f1bd | |
| parent | 872ffd5f5c18c895c50c341abeb899d53d0d7020 (diff) | |
| download | ravanan-a39bc5776c9a93492ef0cbafb44b354d78492e72.tar.gz ravanan-a39bc5776c9a93492ef0cbafb44b354d78492e72.tar.lz ravanan-a39bc5776c9a93492ef0cbafb44b354d78492e72.zip | |
javascript: Refer to parameter context variables in G-expression.
| -rw-r--r-- | ravanan/javascript.scm | 34 | ||||
| -rw-r--r-- | tests/javascript.scm | 23 |
2 files changed, 37 insertions, 20 deletions
diff --git a/ravanan/javascript.scm b/ravanan/javascript.scm index d57a02d..fa63fff 100644 --- a/ravanan/javascript.scm +++ b/ravanan/javascript.scm @@ -1,5 +1,5 @@ ;;; ravanan --- High-reproducibility CWL runner powered by Guix -;;; Copyright © 2024 Arun Isaac <arunisaac@systemreboot.net> +;;; Copyright © 2024–2025 Arun Isaac <arunisaac@systemreboot.net> ;;; ;;; This file is part of ravanan. ;;; @@ -122,25 +122,27 @@ keys @code{\"inputs\"}, @code{\"self\"} and @code{\"runtime\"}. (define (evaluate-using-node expression context expression-lib) "This function is the same as @code{evaluate-parameter-reference-1} but uses the node javascript engine." - (define (set-variable name value) - (string-append name " = " (scm->json-string value) ";")) - - (define preamble - (string-join (append expression-lib - (filter-map (match-lambda - (((and (or "inputs" "self" "runtime") - name) - . value) - (set-variable name value)) - (_ #f)) - (or context - (list)))))) + (define (context-value name) + (scm->json-string (assoc-ref context name))) (if context ;; Evaluate immediately. - (evaluate-javascript %node expression preamble) + (evaluate-javascript %node + expression + (string-append (string-join expression-lib) + "inputs = " (context-value "inputs") ";" + "self = " (context-value "self") ";" + "runtime = " (context-value "runtime") ";")) ;; Compile to a G-expression that evaluates expression. - #~(evaluate-javascript #$%worker-node #$expression #$preamble))) + #~(evaluate-javascript #$%worker-node + #$expression + ;; Context variables are only fully available at + ;; runtime. So, defer their reference to the + ;; G-expression. + (string-append #$(string-join expression-lib) + "inputs = " (scm->json-string inputs) ";" + "self = " (scm->json-string self) ";" + "runtime = " (scm->json-string runtime) ";")))) (define (tokenize-parameter-references str) "Split @var{str} into tokens of parameter reference and literal strings." diff --git a/tests/javascript.scm b/tests/javascript.scm index 75936c0..58d2639 100644 --- a/tests/javascript.scm +++ b/tests/javascript.scm @@ -1,5 +1,5 @@ ;;; ravanan --- High-reproducibility CWL runner powered by Guix -;;; Copyright © 2024 Arun Isaac <arunisaac@systemreboot.net> +;;; Copyright © 2024–2025 Arun Isaac <arunisaac@systemreboot.net> ;;; ;;; This file is part of ravanan. ;;; @@ -119,7 +119,12 @@ (evaluate-parameter-reference "foo$(inputs.vector)$(inputs.object)"))) (test-equal "evaluate parameter reference with node (without context)" - '(evaluate-javascript (*approximate*) "(inputs.n + 1)" "") + '(evaluate-javascript (*approximate*) + "(inputs.n + 1)" + (string-append "" + "inputs = " (scm->json-string inputs) ";" + "self = " (scm->json-string self) ";" + "runtime = " (scm->json-string runtime) ";")) (gexp->sexp-rec (evaluate-parameter-reference "$(inputs.n + 1)"))) @@ -129,7 +134,12 @@ (if (string? token) token (scm->json-string (canonicalize-json token)))) (list (json-ref runtime "cores") "foo" - (evaluate-javascript (*approximate*) "(inputs.threads*2)" "") + (evaluate-javascript (*approximate*) + "(inputs.threads*2)" + (string-append "" + "inputs = " (scm->json-string inputs) ";" + "self = " (scm->json-string self) ";" + "runtime = " (scm->json-string runtime) ";")) (json-ref inputs "output_filename"))) "") (gexp->sexp-rec @@ -142,7 +152,12 @@ (list "foo" (json-ref inputs "vector") (json-ref inputs "object") - (evaluate-javascript (*approximate*) "(inputs.object.foo*20)" ""))) + (evaluate-javascript (*approximate*) + "(inputs.object.foo*20)" + (string-append "" + "inputs = " (scm->json-string inputs) ";" + "self = " (scm->json-string self) ";" + "runtime = " (scm->json-string runtime) ";")))) "") (gexp->sexp-rec (evaluate-parameter-reference "foo$(inputs.vector)$(inputs.object)$(inputs.object.foo*20)"))) |
