Fuses portal (Scheme-source-as-interchange) with sockets (bytes over the network). Wire protocol: one S-expression per connection. Same server + client .lsp runs byte-identically in Python, C, and asm. New primitives in all three impls: - read-from-string — parse one sexp from a string Asm gets two more: - symbol->string — standard R7RS, was missing - eval — evaluate a Scheme value in the global env (Python + C had it as a special form; asm exposes it as a builtin) examples/rpc-server.lsp (port 9080): - Whitelisted dispatch: ping / add / mul / fib / echo - Never calls eval on client input; safe by construction - Uses heap-snapshot/restore for O(1) memory on asm - ~90 lines, portable examples/rpc-client.lsp: - Sends one request, reads one response, displays both - 45 lines, portable examples/repl-server.lsp (port 9081): - DANGER: full remote eval. Any Scheme form accepted and evaluated in the server's global env. Persistent across connections. - Deliberately does NOT use heap-snapshot — remote (define x ...) lives in the global env above any snapshot point; rewinding would invalidate the new binding. The ulimit -v 512 MB safety net (documented in CLAUDE.md) ensures an escaped process can't crash the machine. - ~70 lines, portable. Demonstrates what "the language IS the interchange format" gets you at the limit: a single socket and a single primitive (eval) carry a full-powered REPL. Verified 3×3 server×client matrix: all 9 combinations green. All 132 asm + 571 py + 189 shared + 83 c tests still pass. One quirk discovered and worked around: in asm, a closure captures its env chain by pointer at define time. Forward-referenced names in mutually-recursive toplevel defines can fail under specific heap-restore patterns — see the leaf-first ordering note in rpc-server.lsp.
87 lines
3.3 KiB
Text
87 lines
3.3 KiB
Text
;;; repl-server.lsp — REMOTE SCHEME REPL OVER TCP.
|
|
;;;
|
|
;;; DANGER: this accepts ANY S-expression from the network and evaluates
|
|
;;; it in the global environment. Anyone who can reach the TCP port can
|
|
;;; run arbitrary Scheme code in this process — read files, open sockets,
|
|
;;; shell out via system calls if any are exposed, leak the env, etc.
|
|
;;;
|
|
;;; Run on localhost only. Do not expose publicly. This exists to show
|
|
;;; what the "language IS the interchange" thesis gets you when taken
|
|
;;; to its logical end: a single socket carries a full-powered REPL
|
|
;;; because both sides already have a reader and an evaluator.
|
|
;;;
|
|
;;; Usage:
|
|
;;; python3 uncommonlisp.py --fast examples/repl-server.lsp
|
|
;;; ./c/uncommonlisp examples/repl-server.lsp
|
|
;;; ./asm/uncommonlisp < examples/repl-server.lsp
|
|
;;;
|
|
;;; Then from a client:
|
|
;;; (define x 42) ; server mutates its global env
|
|
;;; (* x 10) ; => 420
|
|
;;; (map car '((1 a) (2 b))) ; => (1 2)
|
|
;;;
|
|
;;; Each connection = one request + one response. Persistent sessions
|
|
;;; across connections because all defines land in the shared global env.
|
|
|
|
(define *port* 9081)
|
|
(define *max-requests* 10000)
|
|
|
|
;; Portable serializer — same as rpc-server.lsp pattern.
|
|
;; Avoids open-output-string and `guard` (neither exists in asm).
|
|
|
|
(define (atom->string v)
|
|
(cond
|
|
((number? v) (number->string v))
|
|
((symbol? v) (symbol->string v))
|
|
((null? v) "()")
|
|
((pair? v) (string-append "(" (list->string v) ")"))
|
|
((string? v) (string-append "\"" v "\""))
|
|
(else "#<unknown>")))
|
|
|
|
(define (list->string lst)
|
|
(cond
|
|
((null? lst) "")
|
|
((null? (cdr lst)) (atom->string (car lst)))
|
|
(else (string-append (atom->string (car lst)) " " (list->string (cdr lst))))))
|
|
|
|
(define (response->string v)
|
|
(cond
|
|
((number? v) (string-append (number->string v) "\n"))
|
|
((symbol? v) (string-append (symbol->string v) "\n"))
|
|
((null? v) "()\n")
|
|
((pair? v) (string-append "(" (list->string v) ")\n"))
|
|
((string? v) (string-append "\"" v "\"\n"))
|
|
(else "#<unknown>\n")))
|
|
|
|
(define (handle-request raw)
|
|
(let ((form (read-from-string raw)))
|
|
(if (eqv? form #f)
|
|
"(error \"empty or malformed input\")\n"
|
|
(response->string (eval form)))))
|
|
|
|
(define server (tcp-listen *port*))
|
|
|
|
;; Intentionally does NOT use heap-snapshot. A remote `(define x ...)`
|
|
;; adds a new binding to the global env chain — heap cells allocated
|
|
;; AFTER any snapshot point. Rewinding would invalidate those bindings.
|
|
;; The asm heap grows with each new top-level define; the ulimit -v
|
|
;; safety net kills the process if it escapes. Each request still
|
|
;; produces garbage (tcp-recv buffer, intermediate strings) that stays
|
|
;; forever — acceptable for a demo, at ~100 bytes per request plus
|
|
;; whatever `define` binds.
|
|
|
|
(define (server-loop n)
|
|
(if (>= n *max-requests*)
|
|
(begin (display "request cap reached, exiting\n") (tcp-close server))
|
|
(begin
|
|
(let ((client (tcp-accept server)))
|
|
(let ((req (tcp-recv client 8192)))
|
|
(if (and req (> (string-length req) 0))
|
|
(tcp-send client (handle-request req))
|
|
#f))
|
|
(tcp-close client))
|
|
(server-loop (+ n 1)))))
|
|
|
|
(display "repl-server on :") (display *port*)
|
|
(display " — DANGER: full remote eval") (newline)
|
|
(server-loop 0)
|