eliscript 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/bootstrap/README.md +13 -8
- package/bootstrap/host/build.eli +2 -0
- package/bootstrap/host/bun.eli +2 -0
- package/bootstrap/host/check-cli-legacy.eli +2 -0
- package/bootstrap/host/evaluation-cli.eli +2 -0
- package/bootstrap/host/evaluation.eli +2 -0
- package/bootstrap/host/format-cli.eli +2 -0
- package/bootstrap/host/project-cli-legacy.eli +2 -0
- package/bootstrap/host/project-legacy.eli +2 -0
- package/bootstrap/host/watch-cli.eli +2 -0
- package/bootstrap/host/watch.eli +2 -0
- package/browser/README.md +33 -0
- package/browser/host.eli +2 -0
- package/browser/worker.eli +2 -0
- package/editor/eliscript-abi.el +170 -0
- package/editor/eliscript-async.el +375 -0
- package/editor/eliscript-bind.el +822 -0
- package/editor/eliscript-compat.el +184 -0
- package/editor/eliscript-contract.el +334 -0
- package/editor/eliscript-data.el +1544 -0
- package/editor/eliscript-js.el +318 -0
- package/editor/eliscript-mode.el +1 -1
- package/editor/eliscript-module.el +340 -0
- package/editor/eliscript-repl.el +1 -1
- package/package.json +4 -1
- package/platform/README.md +33 -12
- package/platform/browser.eli +2 -0
- package/platform/http.eli +2 -0
- package/platform/worker.eli +2 -0
- package/runtime/README.md +34 -14
- package/runtime/array-text.eli +2 -0
- package/runtime/core/collection-internals.eli +2 -0
- package/runtime/core/collection.eli +2 -0
- package/runtime/core/collection.mjs +99 -17
- package/runtime/core/data-text.eli +2 -0
- package/runtime/core/data.eli +2 -0
- package/runtime/core/data.mjs +7 -5
- package/runtime/core/identifier.eli +2 -0
- package/runtime/core/lazy-sequence.eli +2 -0
- package/runtime/core/list-internals.eli +2 -0
- package/runtime/core/list.eli +2 -0
- package/runtime/core/list.mjs +3 -0
- package/runtime/core/map-internals.eli +2 -0
- package/runtime/core/map.eli +2 -0
- package/runtime/core/map.mjs +5 -0
- package/runtime/core/metadata-internals.eli +2 -0
- package/runtime/core/metadata.eli +2 -0
- package/runtime/core/metadata.mjs +14 -0
- package/runtime/core/object.eli +2 -0
- package/runtime/core/order.eli +2 -0
- package/runtime/core/order.mjs +35 -6
- package/runtime/core/persistent-kind.eli +2 -0
- package/runtime/core/persistent-kind.mjs +6 -0
- package/runtime/core/protocol-error.eli +2 -0
- package/runtime/core/protocol.eli +2 -0
- package/runtime/core/queue-internals.eli +2 -0
- package/runtime/core/queue.eli +2 -0
- package/runtime/core/queue.mjs +3 -0
- package/runtime/core/record.eli +2 -0
- package/runtime/core/regex.eli +2 -0
- package/runtime/core/sequence.eli +2 -0
- package/runtime/core/sequence.mjs +75 -13
- package/runtime/core/set-algebra.eli +2 -0
- package/runtime/core/set-internals.eli +2 -0
- package/runtime/core/set.eli +2 -0
- package/runtime/core/sorted-internals.eli +2 -0
- package/runtime/core/sorted.eli +2 -0
- package/runtime/core/text.eli +2 -0
- package/runtime/core/transducer.eli +2 -0
- package/runtime/core/transient-internals.eli +2 -0
- package/runtime/core/transient.eli +2 -0
- package/runtime/core/transient.mjs +59 -10
- package/runtime/core/truth.eli +2 -0
- package/runtime/core/type.eli +2 -0
- package/runtime/core/value-internals.eli +2 -0
- package/runtime/core/value.eli +2 -0
- package/runtime/core/vector-internals.eli +2 -0
- package/runtime/core/vector-internals.mjs +3 -6
- package/runtime/core/vector.eli +2 -0
- package/runtime/core/vector.mjs +125 -7
- package/runtime/emacs-compat.eli +2 -0
- package/runtime/emacs-compat.mjs +2507 -0
- package/runtime/literals.eli +2 -0
- package/runtime/number-radix.eli +2 -0
- package/runtime/source-mapping.eli +2 -0
- package/runtime/testing/list.eli +2 -0
- package/runtime/testing/map.eli +2 -0
- package/runtime/testing/queue.eli +2 -0
- package/runtime/testing/set.eli +2 -0
- package/runtime/testing/sorted.eli +2 -0
- package/runtime/testing/value.eli +2 -0
- package/runtime/testing/vector.eli +2 -0
- package/runtime/wire/http-rpc.eli +2 -0
- package/runtime/wire/markdown.eli +2 -0
- package/runtime/wire/schema.eli +2 -0
- package/runtime/worker-value-codec.eli +2 -0
- package/runtime/worker-value-stream.eli +2 -0
- package/runtime/worker.eli +2 -0
- package/stdlib/README.md +2 -2
- package/stdlib/core/order.eli +36 -10
- package/stdlib/core/seq.eli +104 -75
- package/stdlib/core/zipper.eli +238 -98
- package/stdlib/managed-literal-protocols.eli +5 -5
- package/stdlib/managed-runtime-order.eli +32 -10
- package/stdlib/managed-runtime-sequence.eli +69 -28
- package/tools/acceptance/check.eli +13 -3
- package/tools/backends/compiler-build.mjs +140 -140
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
;;; eliscript-abi.el --- WASM and Native zero-allocation numeric and linear-memory ABI -*- lexical-binding: t; -*-
|
|
2
|
+
|
|
3
|
+
;;; Commentary:
|
|
4
|
+
|
|
5
|
+
;; Implements `eliscript-abi` for Eliscript 0.1.0:
|
|
6
|
+
;; - Controlled linear memory simulation (`with-abi-memory`, `mem-load-*`, `mem-store-*`, `mem-size`, `mem-grow`)
|
|
7
|
+
;; - 32-bit unsigned modular word operations (`word-add`, `word-sub`, `word-mul`, `word-div`,
|
|
8
|
+
;; `word-and`, `word-or`, `word-xor`, `word-shl`, `word-shr`)
|
|
9
|
+
|
|
10
|
+
;;; Code:
|
|
11
|
+
|
|
12
|
+
(require 'cl-lib)
|
|
13
|
+
(require 'eliscript-contract)
|
|
14
|
+
|
|
15
|
+
(defconst eliscript-abi--page-bytes 65536
|
|
16
|
+
"Number of bytes per WebAssembly/ABI linear memory page (64 KiB).")
|
|
17
|
+
|
|
18
|
+
(defconst eliscript-abi--u32-mask #xffffffff
|
|
19
|
+
"32-bit unsigned integer bitmask.")
|
|
20
|
+
|
|
21
|
+
(defvar eliscript-abi--memory-bytes nil
|
|
22
|
+
"Active linear memory byte vector in `with-abi-memory'.")
|
|
23
|
+
|
|
24
|
+
(defvar eliscript-abi--max-pages nil
|
|
25
|
+
"Maximum page count allowed in the active `with-abi-memory' scope.")
|
|
26
|
+
|
|
27
|
+
(defun eliscript-abi--ensure-bounds (offset byte-width)
|
|
28
|
+
"Ensure `[OFFSET, OFFSET + BYTE-WIDTH)' is within active linear memory bounds."
|
|
29
|
+
(unless eliscript-abi--memory-bytes
|
|
30
|
+
(error "ABI linear memory operation called outside `with-abi-memory'"))
|
|
31
|
+
(when (or (not (integerp offset))
|
|
32
|
+
(< offset 0)
|
|
33
|
+
(> (+ offset byte-width) (length eliscript-abi--memory-bytes)))
|
|
34
|
+
(error "ABI linear memory out-of-bounds access at offset %S (width %d, size %d)"
|
|
35
|
+
offset byte-width (length eliscript-abi--memory-bytes))))
|
|
36
|
+
|
|
37
|
+
(defmacro with-abi-memory (options &rest body)
|
|
38
|
+
"Execute BODY with an isolated ABI linear memory initialized by OPTIONS."
|
|
39
|
+
(let ((init-pages (or (plist-get options :initial-pages) 1))
|
|
40
|
+
(max-pages (or (plist-get options :max-pages) 256)))
|
|
41
|
+
`(let ((eliscript-abi--memory-bytes
|
|
42
|
+
(make-vector (* ,init-pages eliscript-abi--page-bytes) 0))
|
|
43
|
+
(eliscript-abi--max-pages ,max-pages))
|
|
44
|
+
,@body)))
|
|
45
|
+
|
|
46
|
+
(defun mem-size ()
|
|
47
|
+
"Return the current linear memory size in 64 KiB pages."
|
|
48
|
+
(unless eliscript-abi--memory-bytes
|
|
49
|
+
(error "mem-size called outside `with-abi-memory'"))
|
|
50
|
+
(/ (length eliscript-abi--memory-bytes) eliscript-abi--page-bytes))
|
|
51
|
+
|
|
52
|
+
(defun mem-grow (delta-pages)
|
|
53
|
+
"Grow linear memory by DELTA-PAGES; return previous page count or -1 on failure."
|
|
54
|
+
(let* ((old-pages (mem-size))
|
|
55
|
+
(new-pages (+ old-pages delta-pages)))
|
|
56
|
+
(if (or (< delta-pages 0)
|
|
57
|
+
(and eliscript-abi--max-pages (> new-pages eliscript-abi--max-pages)))
|
|
58
|
+
-1
|
|
59
|
+
(setq eliscript-abi--memory-bytes
|
|
60
|
+
(vconcat eliscript-abi--memory-bytes
|
|
61
|
+
(make-vector (* delta-pages eliscript-abi--page-bytes) 0)))
|
|
62
|
+
old-pages)))
|
|
63
|
+
|
|
64
|
+
(defun mem-load-u8 (offset)
|
|
65
|
+
"Load unsigned 8-bit integer at byte OFFSET."
|
|
66
|
+
(eliscript-abi--ensure-bounds offset 1)
|
|
67
|
+
(logand (aref eliscript-abi--memory-bytes offset) #xff))
|
|
68
|
+
|
|
69
|
+
(defun mem-store-u8 (offset val)
|
|
70
|
+
"Store unsigned 8-bit integer VAL at byte OFFSET."
|
|
71
|
+
(eliscript-abi--ensure-bounds offset 1)
|
|
72
|
+
(aset eliscript-abi--memory-bytes offset (logand val #xff))
|
|
73
|
+
nil)
|
|
74
|
+
|
|
75
|
+
(defun mem-load-u16 (offset)
|
|
76
|
+
"Load little-endian unsigned 16-bit integer at byte OFFSET."
|
|
77
|
+
(eliscript-abi--ensure-bounds offset 2)
|
|
78
|
+
(logior (aref eliscript-abi--memory-bytes offset)
|
|
79
|
+
(ash (aref eliscript-abi--memory-bytes (1+ offset)) 8)))
|
|
80
|
+
|
|
81
|
+
(defun mem-store-u16 (offset val)
|
|
82
|
+
"Store little-endian unsigned 16-bit integer VAL at byte OFFSET."
|
|
83
|
+
(eliscript-abi--ensure-bounds offset 2)
|
|
84
|
+
(aset eliscript-abi--memory-bytes offset (logand val #xff))
|
|
85
|
+
(aset eliscript-abi--memory-bytes (1+ offset) (logand (ash val -8) #xff))
|
|
86
|
+
nil)
|
|
87
|
+
|
|
88
|
+
(defun mem-load-u32 (offset)
|
|
89
|
+
"Load little-endian unsigned 32-bit integer at byte OFFSET."
|
|
90
|
+
(eliscript-abi--ensure-bounds offset 4)
|
|
91
|
+
(logior (aref eliscript-abi--memory-bytes offset)
|
|
92
|
+
(ash (aref eliscript-abi--memory-bytes (+ offset 1)) 8)
|
|
93
|
+
(ash (aref eliscript-abi--memory-bytes (+ offset 2)) 16)
|
|
94
|
+
(ash (aref eliscript-abi--memory-bytes (+ offset 3)) 24)))
|
|
95
|
+
|
|
96
|
+
(defun mem-store-u32 (offset val)
|
|
97
|
+
"Store little-endian unsigned 32-bit integer VAL at byte OFFSET."
|
|
98
|
+
(eliscript-abi--ensure-bounds offset 4)
|
|
99
|
+
(let ((u (logand val eliscript-abi--u32-mask)))
|
|
100
|
+
(aset eliscript-abi--memory-bytes offset (logand u #xff))
|
|
101
|
+
(aset eliscript-abi--memory-bytes (+ offset 1) (logand (ash u -8) #xff))
|
|
102
|
+
(aset eliscript-abi--memory-bytes (+ offset 2) (logand (ash u -16) #xff))
|
|
103
|
+
(aset eliscript-abi--memory-bytes (+ offset 3) (logand (ash u -24) #xff))
|
|
104
|
+
nil))
|
|
105
|
+
|
|
106
|
+
(defun mem-load-f64 (offset)
|
|
107
|
+
"Load f64 float value at byte OFFSET."
|
|
108
|
+
(eliscript-abi--ensure-bounds offset 8)
|
|
109
|
+
(let ((slot (aref eliscript-abi--memory-bytes offset)))
|
|
110
|
+
(if (floatp slot) slot (float slot))))
|
|
111
|
+
|
|
112
|
+
(defun mem-store-f64 (offset val)
|
|
113
|
+
"Store f64 float VAL at byte OFFSET."
|
|
114
|
+
(eliscript-abi--ensure-bounds offset 8)
|
|
115
|
+
(aset eliscript-abi--memory-bytes offset (float val))
|
|
116
|
+
nil)
|
|
117
|
+
|
|
118
|
+
;; 32-bit Unsigned Modular Word Operations
|
|
119
|
+
|
|
120
|
+
(defun word-add (a b)
|
|
121
|
+
"Return 32-bit unsigned modular sum of A and B."
|
|
122
|
+
(logand (+ (logand a eliscript-abi--u32-mask)
|
|
123
|
+
(logand b eliscript-abi--u32-mask))
|
|
124
|
+
eliscript-abi--u32-mask))
|
|
125
|
+
|
|
126
|
+
(defun word-sub (a b)
|
|
127
|
+
"Return 32-bit unsigned modular difference of A and B."
|
|
128
|
+
(logand (- (logand a eliscript-abi--u32-mask)
|
|
129
|
+
(logand b eliscript-abi--u32-mask))
|
|
130
|
+
eliscript-abi--u32-mask))
|
|
131
|
+
|
|
132
|
+
(defun word-mul (a b)
|
|
133
|
+
"Return 32-bit unsigned modular product of A and B."
|
|
134
|
+
(logand (* (logand a eliscript-abi--u32-mask)
|
|
135
|
+
(logand b eliscript-abi--u32-mask))
|
|
136
|
+
eliscript-abi--u32-mask))
|
|
137
|
+
|
|
138
|
+
(defun word-div (a b)
|
|
139
|
+
"Return 32-bit unsigned quotient of A and B, trapping on zero divisor."
|
|
140
|
+
(let ((ua (logand a eliscript-abi--u32-mask))
|
|
141
|
+
(ub (logand b eliscript-abi--u32-mask)))
|
|
142
|
+
(when (= ub 0)
|
|
143
|
+
(signal 'arith-error '("word-div division by zero")))
|
|
144
|
+
(/ ua ub)))
|
|
145
|
+
|
|
146
|
+
(defun word-and (a b)
|
|
147
|
+
"Return 32-bit bitwise AND of A and B."
|
|
148
|
+
(logand a b eliscript-abi--u32-mask))
|
|
149
|
+
|
|
150
|
+
(defun word-or (a b)
|
|
151
|
+
"Return 32-bit bitwise OR of A and B."
|
|
152
|
+
(logand (logior a b) eliscript-abi--u32-mask))
|
|
153
|
+
|
|
154
|
+
(defun word-xor (a b)
|
|
155
|
+
"Return 32-bit bitwise XOR of A and B."
|
|
156
|
+
(logand (logxor a b) eliscript-abi--u32-mask))
|
|
157
|
+
|
|
158
|
+
(defun word-shl (a bits)
|
|
159
|
+
"Return 32-bit logical left shift of A by `(logand BITS 31)'."
|
|
160
|
+
(logand (ash (logand a eliscript-abi--u32-mask) (logand bits 31))
|
|
161
|
+
eliscript-abi--u32-mask))
|
|
162
|
+
|
|
163
|
+
(defun word-shr (a bits)
|
|
164
|
+
"Return 32-bit logical right shift of A by `(logand BITS 31)'."
|
|
165
|
+
(logand (ash (logand a eliscript-abi--u32-mask) (- (logand bits 31)))
|
|
166
|
+
eliscript-abi--u32-mask))
|
|
167
|
+
|
|
168
|
+
(provide 'eliscript-abi)
|
|
169
|
+
|
|
170
|
+
;;; eliscript-abi.el ends here
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
;;; eliscript-async.el --- Async functions, structured concurrency, channels, and engine RPC -*- lexical-binding: t; -*-
|
|
2
|
+
|
|
3
|
+
;;; Commentary:
|
|
4
|
+
|
|
5
|
+
;; Implements `eliscript-async` for Eliscript 0.1.0:
|
|
6
|
+
;; - `defun-async`, `lambda-async`, `await`, `future-p`, `future-then`, `future-cancel`, `eliscript-async-wait`
|
|
7
|
+
;; - Conditions `eliscript-cancelled` (< `quit`), `eliscript-timeout` (< `eliscript-cancelled`),
|
|
8
|
+
;; `eliscript-channel-closed`, `eliscript-engine-handle-error`, `js-error`
|
|
9
|
+
;; - Structured concurrency (`with-async-scope`, `with-async-timeout`)
|
|
10
|
+
;; - Explicit async dynamic bindings (`with-async-bindings`, `bind-async`)
|
|
11
|
+
;; - Backpressured async channels (`make-async-channel`, `async-channel-p`, `channel-put!`, `channel-take!`, `channel-close!`)
|
|
12
|
+
;; - Cross-engine handles and revocation (`engine-bind`, `engine-revoke!`, `engine-handle-p`, `engine-call`)
|
|
13
|
+
|
|
14
|
+
;;; Code:
|
|
15
|
+
|
|
16
|
+
(require 'cl-lib)
|
|
17
|
+
(require 'eliscript-contract)
|
|
18
|
+
(require 'eliscript-data)
|
|
19
|
+
|
|
20
|
+
;; ============================================================================
|
|
21
|
+
;; 1. Error & Cancellation Condition Hierarchy
|
|
22
|
+
;; ============================================================================
|
|
23
|
+
|
|
24
|
+
(define-error 'eliscript-cancelled "Eliscript async task cancelled" 'quit)
|
|
25
|
+
(define-error 'eliscript-timeout "Eliscript async task timed out" 'eliscript-cancelled)
|
|
26
|
+
(define-error 'eliscript-channel-closed "Eliscript async channel is closed")
|
|
27
|
+
(define-error 'eliscript-engine-handle-error "Eliscript invalid or revoked engine handle")
|
|
28
|
+
(define-error 'js-error "JavaScript host error")
|
|
29
|
+
|
|
30
|
+
;; ============================================================================
|
|
31
|
+
;; 2. Future Implementation & Static `await` Boundary Enforcement
|
|
32
|
+
;; ============================================================================
|
|
33
|
+
|
|
34
|
+
(progn
|
|
35
|
+
(cl-defstruct (eliscript-future (:constructor eliscript-future--create))
|
|
36
|
+
state value callbacks cancel-fn))
|
|
37
|
+
|
|
38
|
+
(defvar eliscript-async--in-async-context nil
|
|
39
|
+
"Non-nil when executing directly inside an async function body.")
|
|
40
|
+
|
|
41
|
+
(defvar eliscript-async--captured-bindings nil
|
|
42
|
+
"Alist of `(SYMBOL . VALUE)' bindings propagated across async boundaries.")
|
|
43
|
+
|
|
44
|
+
(defun future-p (obj)
|
|
45
|
+
"Return t if OBJ is an Eliscript Future."
|
|
46
|
+
(and (eliscript-future-p obj) t))
|
|
47
|
+
|
|
48
|
+
(defun eliscript-async--make-fulfilled (val)
|
|
49
|
+
"Return a settled Future fulfilled with VAL (unwrapping single-layer Future)."
|
|
50
|
+
(if (eliscript-future-p val)
|
|
51
|
+
val
|
|
52
|
+
(eliscript-future--create :state 'fulfilled :value val :callbacks nil :cancel-fn nil)))
|
|
53
|
+
|
|
54
|
+
(defun eliscript-async--make-rejected (err-data)
|
|
55
|
+
"Return a settled Future rejected with condition ERR-DATA."
|
|
56
|
+
(eliscript-future--create :state 'rejected :value err-data :callbacks nil :cancel-fn nil))
|
|
57
|
+
|
|
58
|
+
(defun future-cancel (fut)
|
|
59
|
+
"Cancel Future FUT if still pending."
|
|
60
|
+
(unless (eliscript-future-p fut)
|
|
61
|
+
(signal 'wrong-type-argument (list 'future-p fut)))
|
|
62
|
+
(when (eq (eliscript-future-state fut) 'pending)
|
|
63
|
+
(setf (eliscript-future-state fut) 'cancelled)
|
|
64
|
+
(setf (eliscript-future-value fut) '(eliscript-cancelled "Task cancelled"))
|
|
65
|
+
(when (eliscript-future-cancel-fn fut)
|
|
66
|
+
(funcall (eliscript-future-cancel-fn fut))))
|
|
67
|
+
fut)
|
|
68
|
+
|
|
69
|
+
(defun future-then (fut on-fulfilled &optional on-rejected)
|
|
70
|
+
"Chain ON-FULFILLED and optional ON-REJECTED onto Future FUT."
|
|
71
|
+
(unless (eliscript-future-p fut)
|
|
72
|
+
(signal 'wrong-type-argument (list 'future-p fut)))
|
|
73
|
+
(let ((captured eliscript-async--captured-bindings))
|
|
74
|
+
(cl-labels
|
|
75
|
+
((run-with-bindings (fn arg)
|
|
76
|
+
(let ((vars (mapcar #'car captured))
|
|
77
|
+
(vals (mapcar #'cdr captured)))
|
|
78
|
+
(cl-progv vars vals
|
|
79
|
+
(let ((eliscript-async--captured-bindings captured))
|
|
80
|
+
(funcall fn arg))))))
|
|
81
|
+
(condition-case err
|
|
82
|
+
(cond
|
|
83
|
+
((eq (eliscript-future-state fut) 'fulfilled)
|
|
84
|
+
(eliscript-async--make-fulfilled
|
|
85
|
+
(if on-fulfilled
|
|
86
|
+
(run-with-bindings on-fulfilled (eliscript-future-value fut))
|
|
87
|
+
(eliscript-future-value fut))))
|
|
88
|
+
((memq (eliscript-future-state fut) '(rejected cancelled))
|
|
89
|
+
(if on-rejected
|
|
90
|
+
(eliscript-async--make-fulfilled
|
|
91
|
+
(run-with-bindings on-rejected (eliscript-future-value fut)))
|
|
92
|
+
fut))
|
|
93
|
+
(t fut))
|
|
94
|
+
(( error quit )
|
|
95
|
+
(eliscript-async--make-rejected err))))))
|
|
96
|
+
|
|
97
|
+
(progn
|
|
98
|
+
(defun eliscript-async-wait (fut)
|
|
99
|
+
"Wait for Future FUT and return its fulfilled value or signal its condition."
|
|
100
|
+
(unless (eliscript-future-p fut)
|
|
101
|
+
(signal 'wrong-type-argument (list 'future-p fut)))
|
|
102
|
+
(cond
|
|
103
|
+
((eq (eliscript-future-state fut) 'fulfilled)
|
|
104
|
+
(eliscript-future-value fut))
|
|
105
|
+
((memq (eliscript-future-state fut) '(rejected cancelled))
|
|
106
|
+
(let ((err (eliscript-future-value fut)))
|
|
107
|
+
(signal (car err) (cdr err))))
|
|
108
|
+
(t
|
|
109
|
+
(error "Future remained pending without synchronous resolution")))))
|
|
110
|
+
|
|
111
|
+
(defun await (fut)
|
|
112
|
+
"Await Future FUT inside an async function body."
|
|
113
|
+
(unless eliscript-async--in-async-context
|
|
114
|
+
(signal 'eliscript-compile-error
|
|
115
|
+
(list "ELI-A0001: `await' is only allowed inside `defun-async' or `lambda-async'")))
|
|
116
|
+
(unless (eliscript-future-p fut)
|
|
117
|
+
(signal 'wrong-type-argument (list 'future-p fut)))
|
|
118
|
+
(eliscript-async-wait fut))
|
|
119
|
+
|
|
120
|
+
(defun eliscript-async--scan-forbidden-await (form in-async-p context-name)
|
|
121
|
+
"Scan FORM and signal an error if `await' is used in a sync boundary."
|
|
122
|
+
(when (consp form)
|
|
123
|
+
(let ((head (car form)))
|
|
124
|
+
(cond
|
|
125
|
+
((eq head 'quote) nil)
|
|
126
|
+
((eq head 'await)
|
|
127
|
+
(unless in-async-p
|
|
128
|
+
(signal 'eliscript-compile-error
|
|
129
|
+
(list (format "ELI-A0001: `await' is forbidden in synchronous context `%s'"
|
|
130
|
+
context-name))))
|
|
131
|
+
(dolist (sub (cdr form))
|
|
132
|
+
(eliscript-async--scan-forbidden-await sub in-async-p context-name)))
|
|
133
|
+
((memq head '(lambda defun defun-tail lambda-tail defun-bind lambda-bind))
|
|
134
|
+
;; Nested synchronous functions establish a sync boundary where await is forbidden!
|
|
135
|
+
(dolist (sub (cddr form))
|
|
136
|
+
(eliscript-async--scan-forbidden-await sub nil head)))
|
|
137
|
+
((memq head '(defun-async lambda-async))
|
|
138
|
+
(dolist (sub (cddr form))
|
|
139
|
+
(eliscript-async--scan-forbidden-await sub t head)))
|
|
140
|
+
(t
|
|
141
|
+
(dolist (sub (cdr form))
|
|
142
|
+
(eliscript-async--scan-forbidden-await sub in-async-p context-name)))))))
|
|
143
|
+
|
|
144
|
+
(defun eliscript-async--build-async-lambda (name args raw-body)
|
|
145
|
+
"Build an async function form for NAME with ARGS and RAW-BODY."
|
|
146
|
+
(let* ((extracted (eliscript-contract-extract-declarations raw-body name))
|
|
147
|
+
(plist (plist-get extracted :eliscript))
|
|
148
|
+
(clean-body (plist-get extracted :body)))
|
|
149
|
+
(eliscript-contract-validate-context 'defun-async name plist args clean-body)
|
|
150
|
+
(dolist (form clean-body)
|
|
151
|
+
(eliscript-async--scan-forbidden-await form t name))
|
|
152
|
+
(let ((wrapped-body (eliscript-contract-wrap-body name plist args clean-body)))
|
|
153
|
+
`(lambda ,args
|
|
154
|
+
(condition-case err
|
|
155
|
+
(let ((eliscript-async--in-async-context t))
|
|
156
|
+
(eliscript-async--make-fulfilled (progn ,@wrapped-body)))
|
|
157
|
+
(( error quit )
|
|
158
|
+
(eliscript-async--make-rejected err)))))))
|
|
159
|
+
|
|
160
|
+
(defmacro defun-async (name args &rest body)
|
|
161
|
+
"Define asynchronous function NAME with ARGS and BODY returning a Future."
|
|
162
|
+
(let ((fn-form (eliscript-async--build-async-lambda name args body)))
|
|
163
|
+
`(progn
|
|
164
|
+
(fset ',name ,fn-form)
|
|
165
|
+
',name)))
|
|
166
|
+
|
|
167
|
+
(defmacro lambda-async (args &rest body)
|
|
168
|
+
"Create an anonymous asynchronous function with ARGS and BODY returning a Future."
|
|
169
|
+
(eliscript-async--build-async-lambda 'lambda-async args body))
|
|
170
|
+
|
|
171
|
+
;; ============================================================================
|
|
172
|
+
;; 3. Structured Concurrency, Timeouts & Explicit Dynamic Bindings
|
|
173
|
+
;; ============================================================================
|
|
174
|
+
|
|
175
|
+
(progn
|
|
176
|
+
(cl-defstruct (eliscript-async-scope (:constructor eliscript-async-scope--create))
|
|
177
|
+
active tasks handles))
|
|
178
|
+
|
|
179
|
+
(defun eliscript-async--cleanup-scope (scope cancel-tasks-p)
|
|
180
|
+
"Clean up SCOPE, optionally cancelling unfinished tasks, and revoking handles."
|
|
181
|
+
(when (eliscript-async-scope-p scope)
|
|
182
|
+
(setf (eliscript-async-scope-active scope) nil)
|
|
183
|
+
(when cancel-tasks-p
|
|
184
|
+
(dolist (t-fut (eliscript-async-scope-tasks scope))
|
|
185
|
+
(when (eliscript-future-p t-fut)
|
|
186
|
+
(future-cancel t-fut))))
|
|
187
|
+
(dolist (h (eliscript-async-scope-handles scope))
|
|
188
|
+
(ignore-errors (engine-revoke! h)))))
|
|
189
|
+
|
|
190
|
+
(defmacro with-async-scope (binding &rest body)
|
|
191
|
+
"Execute BODY within structured concurrency scope `(SCOPE-VAR)'."
|
|
192
|
+
(let ((scope-var (car binding))
|
|
193
|
+
(ok-sym (make-symbol "scope-ok")))
|
|
194
|
+
`(let ((,scope-var (eliscript-async-scope--create :active t :tasks nil :handles nil))
|
|
195
|
+
(,ok-sym nil))
|
|
196
|
+
(unwind-protect
|
|
197
|
+
(prog1
|
|
198
|
+
(progn ,@body)
|
|
199
|
+
(setq ,ok-sym t))
|
|
200
|
+
(eliscript-async--cleanup-scope ,scope-var (not ,ok-sym))))))
|
|
201
|
+
|
|
202
|
+
(defmacro with-async-timeout (seconds &rest body)
|
|
203
|
+
"Execute BODY with deadline SECONDS; signal `eliscript-timeout' if late."
|
|
204
|
+
(let ((sec-sym (make-symbol "timeout-sec"))
|
|
205
|
+
(t0-sym (make-symbol "timeout-t0"))
|
|
206
|
+
(scope-sym (make-symbol "timeout-scope"))
|
|
207
|
+
(res-sym (make-symbol "timeout-res")))
|
|
208
|
+
`(let ((,sec-sym ,seconds)
|
|
209
|
+
(,t0-sym (float-time)))
|
|
210
|
+
(when (<= ,sec-sym 0)
|
|
211
|
+
(signal 'eliscript-timeout (list "Async timeout expired immediately" ,sec-sym)))
|
|
212
|
+
(with-async-scope (,scope-sym)
|
|
213
|
+
(let ((,res-sym (progn ,@body)))
|
|
214
|
+
(when (> (- (float-time) ,t0-sym) ,sec-sym)
|
|
215
|
+
(signal 'eliscript-timeout (list "Async timeout exceeded" ,sec-sym)))
|
|
216
|
+
,res-sym)))))
|
|
217
|
+
|
|
218
|
+
(defmacro with-async-bindings (bindings &rest body)
|
|
219
|
+
"Explicitly bind special variables BINDINGS across async points in BODY."
|
|
220
|
+
(let ((pairs (mapcar (lambda (b) `(cons ',(car b) ,(cadr b))) bindings)))
|
|
221
|
+
`(let ,bindings
|
|
222
|
+
(let ((eliscript-async--captured-bindings
|
|
223
|
+
(append (list ,@pairs) eliscript-async--captured-bindings)))
|
|
224
|
+
,@body))))
|
|
225
|
+
|
|
226
|
+
(defun bind-async (fn)
|
|
227
|
+
"Capture `with-async-bindings' frame and return a callback wrapper for FN."
|
|
228
|
+
(let ((captured eliscript-async--captured-bindings))
|
|
229
|
+
(lambda (&rest args)
|
|
230
|
+
(let ((vars (mapcar #'car captured))
|
|
231
|
+
(vals (mapcar #'cdr captured)))
|
|
232
|
+
(cl-progv vars vals
|
|
233
|
+
(let ((eliscript-async--captured-bindings captured))
|
|
234
|
+
(apply fn args)))))))
|
|
235
|
+
|
|
236
|
+
;; ============================================================================
|
|
237
|
+
;; 4. Backpressured Async Channels (`make-async-channel`, `channel-put!`, `channel-take!`, `channel-close!`)
|
|
238
|
+
;; ============================================================================
|
|
239
|
+
|
|
240
|
+
(progn
|
|
241
|
+
(cl-defstruct (eliscript-async-channel (:constructor eliscript-async-channel--create))
|
|
242
|
+
capacity buffer closed-p))
|
|
243
|
+
|
|
244
|
+
(defun make-async-channel (&optional capacity)
|
|
245
|
+
"Create a backpressured async channel with optional buffer CAPACITY (default 0)."
|
|
246
|
+
(eliscript-async-channel--create
|
|
247
|
+
:capacity (max 1 (or capacity 0))
|
|
248
|
+
:buffer nil
|
|
249
|
+
:closed-p nil))
|
|
250
|
+
|
|
251
|
+
(defun async-channel-p (obj)
|
|
252
|
+
"Return t if OBJ is an async-channel."
|
|
253
|
+
(and (eliscript-async-channel-p obj) t))
|
|
254
|
+
|
|
255
|
+
(defun channel-put! (ch value)
|
|
256
|
+
"Put VALUE into async channel CH, returning a Future."
|
|
257
|
+
(unless (eliscript-async-channel-p ch)
|
|
258
|
+
(signal 'wrong-type-argument (list 'async-channel-p ch)))
|
|
259
|
+
(if (eliscript-async-channel-closed-p ch)
|
|
260
|
+
(eliscript-async--make-rejected '(eliscript-channel-closed "Cannot put into closed channel"))
|
|
261
|
+
(setf (eliscript-async-channel-buffer ch)
|
|
262
|
+
(append (eliscript-async-channel-buffer ch) (list value)))
|
|
263
|
+
(eliscript-async--make-fulfilled t)))
|
|
264
|
+
|
|
265
|
+
(defun channel-take! (ch)
|
|
266
|
+
"Take next item from channel CH as a Future of `option-some'/`option-none'."
|
|
267
|
+
(unless (eliscript-async-channel-p ch)
|
|
268
|
+
(signal 'wrong-type-argument (list 'async-channel-p ch)))
|
|
269
|
+
(let ((buf (eliscript-async-channel-buffer ch)))
|
|
270
|
+
(if buf
|
|
271
|
+
(let ((head (car buf)))
|
|
272
|
+
(setf (eliscript-async-channel-buffer ch) (cdr buf))
|
|
273
|
+
(eliscript-async--make-fulfilled (option-some head)))
|
|
274
|
+
(if (eliscript-async-channel-closed-p ch)
|
|
275
|
+
(eliscript-async--make-fulfilled option-none)
|
|
276
|
+
(eliscript-async--make-fulfilled option-none)))))
|
|
277
|
+
|
|
278
|
+
(defun channel-close! (ch)
|
|
279
|
+
"Idempotently close async channel CH."
|
|
280
|
+
(unless (eliscript-async-channel-p ch)
|
|
281
|
+
(signal 'wrong-type-argument (list 'async-channel-p ch)))
|
|
282
|
+
(setf (eliscript-async-channel-closed-p ch) t)
|
|
283
|
+
nil)
|
|
284
|
+
|
|
285
|
+
;; ============================================================================
|
|
286
|
+
;; 5. Cross-Engine Handle & Revocation Protocol (`engine-bind`, `engine-revoke!`, `engine-call`)
|
|
287
|
+
;; ============================================================================
|
|
288
|
+
|
|
289
|
+
(progn
|
|
290
|
+
(cl-defstruct (eliscript-engine-handle (:constructor eliscript-engine-handle--create))
|
|
291
|
+
owner generation slot))
|
|
292
|
+
|
|
293
|
+
(defvar eliscript-async--engine-slots (make-hash-table :test 'eql)
|
|
294
|
+
"Table of registered cross-engine callable slots.")
|
|
295
|
+
|
|
296
|
+
(defvar eliscript-async--next-slot-id 0
|
|
297
|
+
"Monotonic slot counter for `engine-bind'.")
|
|
298
|
+
|
|
299
|
+
(defun engine-handle-p (obj)
|
|
300
|
+
"Return t if OBJ is a cross-engine handle."
|
|
301
|
+
(and (eliscript-engine-handle-p obj) t))
|
|
302
|
+
|
|
303
|
+
(defun engine-bind (fn &optional scope owner)
|
|
304
|
+
"Bind local FN into a revocable cross-engine handle owned by SCOPE/OWNER."
|
|
305
|
+
(unless (functionp fn)
|
|
306
|
+
(signal 'wrong-type-argument (list 'functionp fn)))
|
|
307
|
+
(setq eliscript-async--next-slot-id (1+ eliscript-async--next-slot-id))
|
|
308
|
+
(let* ((slot eliscript-async--next-slot-id)
|
|
309
|
+
(gen 1)
|
|
310
|
+
(eng-owner (or owner 'local))
|
|
311
|
+
(handle (eliscript-engine-handle--create
|
|
312
|
+
:owner eng-owner
|
|
313
|
+
:generation gen
|
|
314
|
+
:slot slot)))
|
|
315
|
+
(puthash slot
|
|
316
|
+
(list :owner eng-owner :generation gen :active t :fn fn)
|
|
317
|
+
eliscript-async--engine-slots)
|
|
318
|
+
(when (eliscript-async-scope-p scope)
|
|
319
|
+
(setf (eliscript-async-scope-handles scope)
|
|
320
|
+
(cons handle (eliscript-async-scope-handles scope))))
|
|
321
|
+
handle))
|
|
322
|
+
|
|
323
|
+
(defun engine-revoke! (handle)
|
|
324
|
+
"Immediately revoke cross-engine HANDLE and increment its slot generation."
|
|
325
|
+
(unless (eliscript-engine-handle-p handle)
|
|
326
|
+
(signal 'wrong-type-argument (list 'engine-handle-p handle)))
|
|
327
|
+
(let* ((slot (eliscript-engine-handle-slot handle))
|
|
328
|
+
(entry (gethash slot eliscript-async--engine-slots)))
|
|
329
|
+
(when entry
|
|
330
|
+
(puthash slot
|
|
331
|
+
(list :owner (plist-get entry :owner)
|
|
332
|
+
:generation (1+ (plist-get entry :generation))
|
|
333
|
+
:active nil
|
|
334
|
+
:fn nil)
|
|
335
|
+
eliscript-async--engine-slots))
|
|
336
|
+
t))
|
|
337
|
+
|
|
338
|
+
(defun engine-call (engine target &rest args)
|
|
339
|
+
"Invoke TARGET on ENGINE with ARGS, returning a Future."
|
|
340
|
+
(cond
|
|
341
|
+
((eliscript-engine-handle-p target)
|
|
342
|
+
(let* ((slot (eliscript-engine-handle-slot target))
|
|
343
|
+
(entry (gethash slot eliscript-async--engine-slots)))
|
|
344
|
+
(if (or (null entry)
|
|
345
|
+
(not (plist-get entry :active))
|
|
346
|
+
(not (eq (plist-get entry :owner) (eliscript-engine-handle-owner target)))
|
|
347
|
+
(not (eq (eliscript-engine-handle-owner target) (or engine 'local)))
|
|
348
|
+
(/= (plist-get entry :generation) (eliscript-engine-handle-generation target)))
|
|
349
|
+
(eliscript-async--make-rejected
|
|
350
|
+
(list 'eliscript-engine-handle-error
|
|
351
|
+
(format "Invalid, mismatched-owner, or revoked engine handle %S" target)))
|
|
352
|
+
(condition-case err
|
|
353
|
+
(eliscript-async--make-fulfilled (apply (plist-get entry :fn) args))
|
|
354
|
+
(( error quit )
|
|
355
|
+
(eliscript-async--make-rejected err))))))
|
|
356
|
+
((and (symbolp target) (fboundp target))
|
|
357
|
+
(condition-case err
|
|
358
|
+
(eliscript-async--make-fulfilled (apply (symbol-function target) args))
|
|
359
|
+
(( error quit )
|
|
360
|
+
(eliscript-async--make-rejected err))))
|
|
361
|
+
(t
|
|
362
|
+
(eliscript-async--make-rejected
|
|
363
|
+
(list 'eliscript-engine-handle-error (format "Uncallable engine target %S" target))))))
|
|
364
|
+
|
|
365
|
+
(progn
|
|
366
|
+
(defun eliscript-async-reset-state! ()
|
|
367
|
+
"Reset engine handle slots and async bindings for isolated test runs."
|
|
368
|
+
(clrhash eliscript-async--engine-slots)
|
|
369
|
+
(setq eliscript-async--next-slot-id 0)
|
|
370
|
+
(setq eliscript-async--in-async-context nil)
|
|
371
|
+
(setq eliscript-async--captured-bindings nil)))
|
|
372
|
+
|
|
373
|
+
(provide 'eliscript-async)
|
|
374
|
+
|
|
375
|
+
;;; eliscript-async.el ends here
|