Robuuster returns in javascript.

This commit is contained in:
2026-05-28 17:44:53 +02:00
parent 36b8b5de78
commit 6ffc11d2b1
4 changed files with 47 additions and 70 deletions
+13 -21
View File
@@ -16,18 +16,10 @@
(list (verbatim rkt) (verbatim js)))))
@title{js-maker: a Syntax-Driven Racket-to-JavaScript Generator}
@author+email["Hans Dijkema" "hans@dijkewijk.nl"]
@author+email["Hans Dijkema" ""]
@defmodule[js-maker]
This module has been largely coded as an evolution to the @tt{js-transformer} setup
from @tt{racket-webview} by supervising it's evolution using AI.
It is astonishing what AI can do these days. To get the AI agent this far
on racket and testing the output by itself, I had to get @tt{racket} installed
in the coding sandbox of the AI agent.
See @hyperlink["https://racket.discourse.group/t/a-small-experiment-bootstrapping-racket-into-a-chatgpt-sandbox-at-openai/4238"]{the discourse article}
about that.
@bold{js-maker} is a small, syntax-driven JavaScript generator for writing a
practical JavaScript subset in Racket notation. It provides two macros,
@racket[js] and @racket[js/expression]. In the ordinary case the macros
@@ -70,13 +62,12 @@ function square(x) {
Inside function bodies, the last expression is returned automatically unless it
is already a statement form such as @racket[return], @racket[define],
@racket[set!], @racket[while], or @racket[for]. At the top level of a
@racket[js] form, js-maker also returns the value of a final value-producing
form, such as @racket[let], @racket[begin], or @racket[if]. This makes
@racket[js] output suitable as the body of a WebView @tt{runJavaScript}
wrapper function. A top-level @racket[with-handlers] is treated as a statement
so that a catch handler used for side effects does not prematurely return from
the surrounding wrapper. Use @racket[js/expression] when the value of a
@racket[with-handlers] form itself is needed.
@racket[js] form, however, js-maker emits statement code and does not invent an
implicit @tt{return}. This keeps the generated code valid when it is handed
directly to a WebView @tt{runJavaScript} or @tt{evaluateJavaScript} entry point,
where a top-level JavaScript @tt{return} would be an @tt{Illegal return}
syntax error. Use @racket[js/expression] when a generated JavaScript value is
needed.
For example:
@@ -95,7 +86,7 @@ emits JavaScript similar to:
{
let el = document.getElementById("test");
el.innerHTML = "<h1>Hi</h1>";
return true;
true;
}
}
}
@@ -161,14 +152,15 @@ source text.
#<<RKT
(let ([x 10]
[y 20])
(js (let ([a (inject (* x y))])
(return (* a a)))))
(js/expression
(let ([a (inject (* x y))])
(* a a))))
RKT
#<<JS
{
(() => {
let a = 200;
return (a * a);
}
})()
JS
)