eliscript 0.0.4 → 0.1.1
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 +9 -8
- package/bootstrap/compiler/emitter.eli +161 -63
- package/bootstrap/host/check-cli.mjs +0 -1
- package/bootstrap/host/check-cli.mjs.map +1 -1
- package/bootstrap/host/compiler-session.eli +2 -1
- package/bootstrap/host/eliscript-cli.mjs +2 -9
- package/bootstrap/host/eliscript-cli.mjs.map +1 -1
- package/bootstrap/host/project-cli.mjs +1 -11
- package/bootstrap/host/project-cli.mjs.map +1 -1
- package/bootstrap/host/project.mjs +2 -8
- package/bootstrap/host/project.mjs.map +1 -1
- package/browser/README.md +1 -1
- package/compiler/README.md +2 -2
- package/compiler/eliscript-emitter.el +229 -85
- package/compiler/eliscript-ir-emitter.el +64 -34
- package/compiler/eliscript-project.el +20 -5
- package/dist/backends/async.mjs +1 -90
- package/dist/backends/async.mjs.map +1 -1
- package/dist/backends/binary.mjs +1 -91
- package/dist/backends/binary.mjs.map +1 -1
- package/dist/backends/capabilities.mjs +1 -90
- package/dist/backends/capabilities.mjs.map +1 -1
- package/dist/backends/elf.mjs +1 -90
- package/dist/backends/elf.mjs.map +1 -1
- package/dist/backends/managed.mjs +1 -94
- package/dist/backends/managed.mjs.map +1 -1
- package/dist/backends/memory.mjs +1 -90
- package/dist/backends/memory.mjs.map +1 -1
- package/dist/backends/native.mjs +1 -91
- package/dist/backends/native.mjs.map +1 -1
- package/dist/backends/numeric.mjs +1 -90
- package/dist/backends/numeric.mjs.map +1 -1
- package/dist/backends/optimize.mjs +1 -90
- package/dist/backends/optimize.mjs.map +1 -1
- package/dist/backends/regexp.mjs +1 -91
- package/dist/backends/regexp.mjs.map +1 -1
- package/dist/backends/sha256.mjs +2 -96
- package/dist/backends/sha256.mjs.map +1 -1
- package/dist/backends/wasm.mjs +1 -90
- package/dist/backends/wasm.mjs.map +1 -1
- package/dist/bootstrap/analyzer.mjs +1 -95
- package/dist/bootstrap/analyzer.mjs.map +1 -1
- package/dist/bootstrap/compiler.mjs +1 -105
- package/dist/bootstrap/compiler.mjs.map +1 -1
- package/dist/bootstrap/emitter.mjs +848 -807
- package/dist/bootstrap/emitter.mjs.map +1 -1
- package/dist/bootstrap/expander.mjs +1 -96
- package/dist/bootstrap/expander.mjs.map +1 -1
- package/dist/bootstrap/formatter.mjs +1 -104
- package/dist/bootstrap/formatter.mjs.map +1 -1
- package/dist/bootstrap/ir.mjs +18 -172
- package/dist/bootstrap/ir.mjs.map +1 -1
- package/dist/bootstrap/lower.mjs +1 -92
- package/dist/bootstrap/lower.mjs.map +1 -1
- package/dist/bootstrap/project.mjs +23 -209
- package/dist/bootstrap/project.mjs.map +1 -1
- package/dist/bootstrap/reader.mjs +1 -96
- package/dist/bootstrap/reader.mjs.map +1 -1
- package/dist/bootstrap/source-map.mjs +7 -112
- package/dist/bootstrap/source-map.mjs.map +1 -1
- package/dist/bootstrap/symbol.mjs +7 -114
- package/dist/bootstrap/symbol.mjs.map +1 -1
- package/dist/bootstrap/syntax.mjs +10 -176
- package/dist/bootstrap/syntax.mjs.map +1 -1
- package/dist/bootstrap/transient-analysis.mjs +1 -93
- package/dist/bootstrap/transient-analysis.mjs.map +1 -1
- package/dist/browser/compiler.js +105 -104
- package/dist/browser/loader.js +1 -1
- package/dist/browser/loader.mjs +5 -124
- package/dist/compiler-artifact.json +1 -1
- package/dist/host/compiler-session.mjs +205 -297
- package/dist/host/compiler-session.mjs.map +1 -1
- package/dist/host/compiler-tools.mjs +1 -91
- package/dist/host/compiler-tools.mjs.map +1 -1
- package/dist/literal-sources/runtime/core/order.eli +32 -10
- package/dist/literal-sources/runtime/core/sequence.eli +69 -28
- package/dist/literal-sources/runtime/core/vector.eli +2 -4
- package/dist/literal-sources/stdlib/managed-literal-protocols.eli +5 -5
- package/dist/literal-sources/stdlib/managed-literals.eli +1 -1
- package/dist/package.json +1 -0
- package/dist/runtime/math.mjs +62 -252
- package/dist/runtime/math.mjs.map +1 -1
- package/dist/runtime/prelude.mjs +95 -0
- package/dist/runtime/prelude.mjs.macro-origins.json +1 -0
- package/dist/runtime/prelude.mjs.map +1 -0
- package/dist/runtime/unicode-case-data.mjs +1 -95
- package/dist/runtime/unicode-case-data.mjs.map +1 -1
- package/dist/runtime/unicode-case.mjs +3 -97
- package/dist/runtime/unicode-case.mjs.map +1 -1
- package/dist/stage3-release.json +1 -1
- package/editor/README.md +9 -4
- 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 +346 -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/runtime/README.md +21 -1
- package/runtime/core/protocol-impl.mjs +1 -115
- package/runtime/core/protocol-impl.mjs.map +1 -1
- package/runtime/core/text-impl.mjs +1 -90
- package/runtime/core/text-impl.mjs.map +1 -1
- package/runtime/core/vector.eli +1 -1
- package/runtime/core/vector.mjs +0 -9
- package/runtime/emacs-compat.eli +5957 -0
- package/runtime/emacs-compat.mjs +13734 -0
- package/runtime/emacs-compat.mjs.map +1 -0
- package/runtime/math.mjs +62 -252
- package/runtime/math.mjs.map +1 -1
- package/runtime/prelude.eli +4 -0
- package/runtime/prelude.mjs +95 -0
- package/runtime/prelude.mjs.map +1 -0
- package/runtime/unicode-case-data.mjs +1 -95
- package/runtime/unicode-case-data.mjs.map +1 -1
- package/runtime/unicode-case.mjs +3 -97
- package/runtime/unicode-case.mjs.map +1 -1
- package/stdlib/managed-literals.eli +1 -1
- package/stdlib/managed-runtime-vector.eli +2 -4
- package/tools/backends/compiler-build.mjs +3 -93
- package/tools/backends/compiler-cli.mjs +2 -15
- package/tools/backends/compiler-corpus-bit.mjs +2 -95
- package/tools/backends/compiler-corpus-cli-adapters.mjs +1 -97
- package/tools/backends/compiler-corpus-debugging.mjs +2 -95
- package/tools/backends/compiler-corpus-emitter.mjs +1 -97
- package/tools/backends/compiler-corpus-execution.mjs +2 -108
- package/tools/backends/compiler-corpus-fuzz.mjs +2 -95
- package/tools/backends/compiler-corpus-host-context.mjs +2 -99
- package/tools/backends/compiler-corpus-identity-host.mjs +18 -123
- package/tools/backends/compiler-corpus-identity-value-adapters.mjs +0 -9
- package/tools/backends/compiler-corpus-literal-provider.mjs +1 -91
- package/tools/backends/compiler-corpus-managed-adapters.mjs +1 -99
- package/tools/backends/compiler-corpus-managed-fixtures.mjs +2 -113
- package/tools/backends/compiler-corpus-matchers.mjs +3 -106
- package/tools/backends/compiler-corpus-original-program.mjs +1 -91
- package/tools/backends/compiler-corpus-process.mjs +1 -92
- package/tools/backends/compiler-corpus-program-driver.mjs +1 -3
- package/tools/backends/compiler-corpus-project-adapters.mjs +1 -95
- package/tools/backends/compiler-corpus-project-entry-host.mjs +2 -92
- package/tools/backends/compiler-corpus-project-transport.mjs +1 -90
- package/tools/backends/compiler-corpus-raw-fixture-observer.mjs +1 -91
- package/tools/backends/compiler-corpus-raw-language-fixtures.mjs +1 -92
- package/tools/backends/compiler-corpus-raw-math-fixture.mjs +1 -91
- package/tools/backends/compiler-corpus-raw-special-fixtures.mjs +1 -91
- package/tools/backends/compiler-corpus-reference.mjs +1 -90
- package/tools/backends/compiler-corpus-runtime-adapters.mjs +2 -110
- package/tools/backends/compiler-corpus-tools-cli.mjs +1 -95
- package/tools/backends/compiler-corpus-tools-reference.mjs +22 -163
- package/tools/backends/compiler-corpus.mjs +3 -125
- package/tools/backends/compiler-matrix.mjs +1 -92
- package/tools/backends/compiler-package-browser.mjs +1 -90
- package/tools/backends/compiler-package.mjs +3 -95
- package/tools/backends/compiler-unicode-profile-host.mjs +1 -91
- package/tools/backends/dist/bootstrap-js-stages.mjs +1 -91
- package/tools/backends/dist/bootstrap-js-stages.mjs.map +1 -1
- package/tools/backends/dist/managed-ir-inventory.mjs +1 -91
- package/tools/backends/dist/managed-ir-inventory.mjs.map +1 -1
- package/tools/backends/dist/record-grid.mjs +1 -91
- package/tools/backends/dist/record-grid.mjs.map +1 -1
- package/tools/backends/dist/release-cli.mjs +4 -94
- package/tools/backends/dist/release-cli.mjs.map +1 -1
- package/tools/backends/dist/run-artifact.mjs +1 -91
- package/tools/backends/dist/run-artifact.mjs.map +1 -1
- package/tools/backends/eliscript/compiler-corpus-bit.eli +1 -0
- package/tools/backends/managed-foreign-binding.mjs +1 -95
- package/tools/backends/managed-host-lifecycle.mjs +1 -91
- package/tools/backends/managed-host-object-provider.mjs +18 -59
- package/tools/backends/managed-host-object-realm.mjs +1 -91
- package/tools/backends/managed-host-proxy.mjs +9 -18
- package/tools/backends/managed-host-runtime.mjs +3 -105
- package/tools/backends/managed-host.mjs +1 -104
- package/tools/backends/managed-module-graph.mjs +1 -97
- package/tools/backends/managed-runtime.mjs +1 -96
- package/tools/backends/managed-source-view.mjs +13 -108
- package/tools/backends/native-managed-runtime.mjs +1 -91
- package/tools/backends/native-memory.mjs +1 -91
- package/tools/backends/stage3-targets.mjs +1 -91
- package/tools/bootstrap-runtime.el +3 -2
- package/tools/project/project.eli +1 -1
- package/tools/testing/stdlib-bit-corpus-host.mjs +1 -90
- package/tools/testing/stdlib-bit.mjs +1 -90
- package/dist/bootstrap/package.json +0 -1
- package/dist/bootstrap/runtime/math.mjs +0 -1117
- package/dist/bootstrap/runtime/math.mjs.map +0 -1
- package/dist/bootstrap/runtime/unicode-case-data.mjs +0 -10125
- package/dist/bootstrap/runtime/unicode-case-data.mjs.map +0 -1
- package/dist/bootstrap/runtime/unicode-case.mjs +0 -281
- package/dist/bootstrap/runtime/unicode-case.mjs.map +0 -1
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
;;; eliscript-compat.el --- Unified 0.1.0 entry point, definition-identity activation, and evaluator -*- lexical-binding: t; -*-
|
|
2
|
+
|
|
3
|
+
;;; Commentary:
|
|
4
|
+
|
|
5
|
+
;; Provides the umbrella `(require 'eliscript)` integration, definition-identity
|
|
6
|
+
;; extension tracking, conflict detection, static contract validation for
|
|
7
|
+
;; standard `defun`, and `eliscript-compat-eval-string` for Eliscript 0.1.0.
|
|
8
|
+
|
|
9
|
+
;;; Code:
|
|
10
|
+
|
|
11
|
+
(require 'cl-lib)
|
|
12
|
+
(require 'json)
|
|
13
|
+
(require 'eliscript-contract)
|
|
14
|
+
(require 'eliscript-data)
|
|
15
|
+
(require 'eliscript-bind)
|
|
16
|
+
(require 'eliscript-module)
|
|
17
|
+
(require 'eliscript-async)
|
|
18
|
+
(require 'eliscript-js)
|
|
19
|
+
(require 'eliscript-abi)
|
|
20
|
+
|
|
21
|
+
(defconst eliscript-compat--extension-exports
|
|
22
|
+
'((eliscript-data
|
|
23
|
+
persistent-list persistent-list-p persistent-vector persistent-vector-p
|
|
24
|
+
persistent-map persistent-map-p persistent-set persistent-set-p
|
|
25
|
+
persistent-queue persistent-queue-p persistent-sorted-map persistent-sorted-map-p
|
|
26
|
+
persistent-sorted-set persistent-sorted-set-p
|
|
27
|
+
value-get value-nth value-put value-assoc value-remove value-dissoc
|
|
28
|
+
value-conj value-count value-empty-p value-into value-equal-p value-hash
|
|
29
|
+
value-compare value-meta value-with-meta
|
|
30
|
+
cl-defstruct-persistent struct-validate struct-json-schema struct-to-json struct-from-json
|
|
31
|
+
defprotocol extend-protocol defclass-protocol reify-protocol
|
|
32
|
+
cl-defgeneric-value cl-defmethod-value
|
|
33
|
+
state-cell state-cell-p cell-value cell-update! cell-reset! cell-watch
|
|
34
|
+
delay force realized-p memoize lazy-seq lazy-map lazy-filter lazy-take lazy-range
|
|
35
|
+
map-transducer filter-transducer take-transducer seq-transduce
|
|
36
|
+
with-transient conj! assoc! dissoc!
|
|
37
|
+
result-ok result-error result-ok-p result-error-p result-value result-error-value result-bind
|
|
38
|
+
option-some option-none option-some-p option-none-p option-value
|
|
39
|
+
data-symbol data-symbol-p data-symbol-name data-symbol-namespace)
|
|
40
|
+
(eliscript-bind
|
|
41
|
+
let-bind let*-bind defun-bind lambda-bind pcase-value cond-predicate
|
|
42
|
+
defun-case lambda-case let-const let*-const
|
|
43
|
+
let-loop defun-tail lambda-tail recur trampoline
|
|
44
|
+
thread-first thread-last thread-as thread-cond-first thread-cond-last
|
|
45
|
+
thread-some-first thread-some-last
|
|
46
|
+
fn-compose fn-partial fn-juxt fn-complement)
|
|
47
|
+
(eliscript-async
|
|
48
|
+
defun-async lambda-async await future-p future-then future-cancel
|
|
49
|
+
with-async-scope with-async-timeout with-async-bindings bind-async
|
|
50
|
+
make-async-channel async-channel-p channel-put! channel-take! channel-close!
|
|
51
|
+
engine-bind engine-revoke! engine-handle-p engine-call eliscript-async-wait)
|
|
52
|
+
(eliscript-module
|
|
53
|
+
defmodule defun-lexical defvar-lexical defconst-lexical
|
|
54
|
+
export-functions export-values import-functions import-values
|
|
55
|
+
defmacro-checked macro-gensym macro-read-file)
|
|
56
|
+
(eliscript-contract
|
|
57
|
+
eliscript-contract-check-value)
|
|
58
|
+
(eliscript-js
|
|
59
|
+
js-false js-null js-undefined js-false-p js-null-p js-undefined-p js-nullish-p
|
|
60
|
+
js-import js-array js-object js-get js-get-raw js-put js-call js-call-raw
|
|
61
|
+
js-new js-expr js-divide js-remainder js-length js-substring
|
|
62
|
+
js-regexp js-regexp-p js-regexp-find js-regexp-matches js-regexp-seq js-regexp-replace)
|
|
63
|
+
(eliscript-abi
|
|
64
|
+
with-abi-memory mem-size mem-grow
|
|
65
|
+
mem-load-u8 mem-load-u16 mem-load-u32 mem-load-f64
|
|
66
|
+
mem-store-u8 mem-store-u16 mem-store-u32 mem-store-f64
|
|
67
|
+
word-add word-sub word-mul word-div word-and word-or word-xor word-shl word-shr))
|
|
68
|
+
"Exported symbols per `1 + 5 + 2' extension library.")
|
|
69
|
+
|
|
70
|
+
(defvar eliscript-compat--canonical-fn-cells (make-hash-table :test 'eq)
|
|
71
|
+
"Canonical function cells for all extension symbols.")
|
|
72
|
+
|
|
73
|
+
(dolist (group eliscript-compat--extension-exports)
|
|
74
|
+
(dolist (sym (cdr group))
|
|
75
|
+
(when (fboundp sym)
|
|
76
|
+
(puthash sym (symbol-function sym) eliscript-compat--canonical-fn-cells))))
|
|
77
|
+
|
|
78
|
+
(defvar eliscript-compat--session-user-defs (make-hash-table :test 'eq)
|
|
79
|
+
"Symbols defined by user code in the current evaluation session.")
|
|
80
|
+
|
|
81
|
+
(defvar eliscript-compat--session-active-libs nil
|
|
82
|
+
"List of extension libraries activated in the current session.")
|
|
83
|
+
|
|
84
|
+
(defvar eliscript-compat--session-touched-symbols nil
|
|
85
|
+
"Symbols created or modified during the current evaluation session.")
|
|
86
|
+
|
|
87
|
+
(defvar eliscript-compat--accelerated-functions nil
|
|
88
|
+
"Alist of (FN-SYM . ORIG-FN) modified by `eliscript-compat-accelerate-function'.")
|
|
89
|
+
|
|
90
|
+
(defconst eliscript-compat--project-directory
|
|
91
|
+
(expand-file-name
|
|
92
|
+
".."
|
|
93
|
+
(file-name-directory (or load-file-name buffer-file-name default-directory)))
|
|
94
|
+
"Root directory of the Eliscript project.")
|
|
95
|
+
|
|
96
|
+
(declare-function eliscript-worker-live-p "eliscript-worker" (worker))
|
|
97
|
+
(declare-function eliscript-worker-call-sync "eliscript-worker"
|
|
98
|
+
(worker module export arguments &rest keys))
|
|
99
|
+
(declare-function eliscript-compile-file "eliscript" (input-file &optional output-file))
|
|
100
|
+
|
|
101
|
+
(progn
|
|
102
|
+
(defun eliscript-compat-reset-state! ()
|
|
103
|
+
"Restore clean 0.1.0 evaluation state and canonical extension bindings."
|
|
104
|
+
(eliscript-contract-reset-state!)
|
|
105
|
+
(eliscript-data-reset-state!)
|
|
106
|
+
(eliscript-module-reset-state!)
|
|
107
|
+
(eliscript-async-reset-state!)
|
|
108
|
+
(dolist (pair eliscript-compat--accelerated-functions)
|
|
109
|
+
(when (symbolp (car pair))
|
|
110
|
+
(fset (car pair) (cdr pair))
|
|
111
|
+
(put (car pair) 'eliscript-compat-native-function nil)))
|
|
112
|
+
(setq eliscript-compat--accelerated-functions nil)
|
|
113
|
+
(dolist (sym eliscript-compat--session-touched-symbols)
|
|
114
|
+
(unless (gethash sym eliscript-compat--canonical-fn-cells)
|
|
115
|
+
(fmakunbound sym)
|
|
116
|
+
(makunbound sym)
|
|
117
|
+
(setplist sym nil)))
|
|
118
|
+
(maphash (lambda (sym fn) (fset sym fn))
|
|
119
|
+
eliscript-compat--canonical-fn-cells)
|
|
120
|
+
(clrhash eliscript-compat--session-user-defs)
|
|
121
|
+
(setq eliscript-compat--session-active-libs nil)
|
|
122
|
+
(setq eliscript-compat--session-touched-symbols nil)))
|
|
123
|
+
|
|
124
|
+
(defun eliscript-compat--activate-lib (lib-sym)
|
|
125
|
+
"Activate extension library LIB-SYM, checking for user symbol conflicts."
|
|
126
|
+
(if (eq lib-sym 'eliscript)
|
|
127
|
+
(dolist (sub '(eliscript-data eliscript-bind eliscript-async eliscript-module eliscript-contract))
|
|
128
|
+
(eliscript-compat--activate-lib sub))
|
|
129
|
+
(let ((exports (cdr (assq lib-sym eliscript-compat--extension-exports))))
|
|
130
|
+
(when exports
|
|
131
|
+
(dolist (sym exports)
|
|
132
|
+
(when (gethash sym eliscript-compat--session-user-defs)
|
|
133
|
+
(signal 'eliscript-compile-error
|
|
134
|
+
(list (format "ELI-M0001: extension `%S' symbol `%S' conflicts with existing user definition"
|
|
135
|
+
lib-sym sym))))
|
|
136
|
+
(let ((canon (gethash sym eliscript-compat--canonical-fn-cells)))
|
|
137
|
+
(when canon
|
|
138
|
+
(fset sym canon))))
|
|
139
|
+
(cl-pushnew lib-sym eliscript-compat--session-active-libs)))))
|
|
140
|
+
|
|
141
|
+
(defun eliscript-compat--preprocess-form (form)
|
|
142
|
+
"Pre-scan and transform FORM for definition-identity and static contracts."
|
|
143
|
+
(if (not (consp form))
|
|
144
|
+
form
|
|
145
|
+
(let ((head (car form)))
|
|
146
|
+
(cond
|
|
147
|
+
((eq head 'quote) form)
|
|
148
|
+
((eq head 'progn)
|
|
149
|
+
`(progn ,@(mapcar #'eliscript-compat--preprocess-form (cdr form))))
|
|
150
|
+
((and (eq head 'require)
|
|
151
|
+
(consp (cadr form))
|
|
152
|
+
(eq (caadr form) 'quote))
|
|
153
|
+
(let ((lib (cadadr form)))
|
|
154
|
+
(if (or (eq lib 'eliscript)
|
|
155
|
+
(assq lib eliscript-compat--extension-exports))
|
|
156
|
+
(progn
|
|
157
|
+
(eliscript-compat--activate-lib lib)
|
|
158
|
+
`',lib)
|
|
159
|
+
form)))
|
|
160
|
+
((eq head 'defun)
|
|
161
|
+
(let* ((name (nth 1 form))
|
|
162
|
+
(args (nth 2 form))
|
|
163
|
+
(raw-body (nthcdr 3 form))
|
|
164
|
+
(extracted (eliscript-contract-extract-declarations raw-body name))
|
|
165
|
+
(plist (plist-get extracted :eliscript))
|
|
166
|
+
(doc (plist-get extracted :doc))
|
|
167
|
+
(clean-body (plist-get extracted :body)))
|
|
168
|
+
(puthash name t eliscript-compat--session-user-defs)
|
|
169
|
+
(push name eliscript-compat--session-touched-symbols)
|
|
170
|
+
(when plist
|
|
171
|
+
(eliscript-contract-validate-context 'defun name plist args clean-body))
|
|
172
|
+
(when (memq 'eliscript-async eliscript-compat--session-active-libs)
|
|
173
|
+
(dolist (bform clean-body)
|
|
174
|
+
(eliscript-async--scan-forbidden-await bform nil name)))
|
|
175
|
+
(let ((wrapped (if plist
|
|
176
|
+
(eliscript-contract-wrap-body name plist args clean-body)
|
|
177
|
+
clean-body)))
|
|
178
|
+
`(defun ,name ,args
|
|
179
|
+
,@(when doc (list doc))
|
|
180
|
+
,@wrapped))))
|
|
181
|
+
((memq head '(defvar defconst defmacro cl-defun))
|
|
182
|
+
(let ((name (nth 1 form)))
|
|
183
|
+
(when (symbolp name)
|
|
184
|
+
(push name eliscript-compat--session-touched-symbols)))
|
|
185
|
+
form)
|
|
186
|
+
(t form)))))
|
|
187
|
+
|
|
188
|
+
(defun eliscript-compat--collect-defined-functions (source)
|
|
189
|
+
"Return a list of function symbol names defined at top level in SOURCE."
|
|
190
|
+
(let ((names nil))
|
|
191
|
+
(with-temp-buffer
|
|
192
|
+
(insert source)
|
|
193
|
+
(goto-char (point-min))
|
|
194
|
+
(condition-case nil
|
|
195
|
+
(while t
|
|
196
|
+
(let ((form (read (current-buffer))))
|
|
197
|
+
(when (consp form)
|
|
198
|
+
(let ((head (car form))
|
|
199
|
+
(second (nth 1 form)))
|
|
200
|
+
(cond
|
|
201
|
+
((and (memq head '(defun cl-defun defun-case defun-bind defun-tail defun-async))
|
|
202
|
+
(symbolp second))
|
|
203
|
+
(cl-pushnew (symbol-name second) names :test #'equal))
|
|
204
|
+
((eq head 'progn)
|
|
205
|
+
(dolist (sub (cdr form))
|
|
206
|
+
(when (and (consp sub)
|
|
207
|
+
(memq (car sub) '(defun cl-defun defun-case defun-bind defun-tail defun-async))
|
|
208
|
+
(symbolp (nth 1 sub)))
|
|
209
|
+
(cl-pushnew (symbol-name (nth 1 sub)) names :test #'equal)))))))))
|
|
210
|
+
(end-of-file nil)))
|
|
211
|
+
(nreverse names)))
|
|
212
|
+
|
|
213
|
+
(progn
|
|
214
|
+
(defun eliscript-compat-eval-string (source)
|
|
215
|
+
"Read, statically check, and evaluate all forms in SOURCE string."
|
|
216
|
+
(with-temp-buffer
|
|
217
|
+
(insert source)
|
|
218
|
+
(goto-char (point-min))
|
|
219
|
+
(let ((last-val nil))
|
|
220
|
+
(condition-case nil
|
|
221
|
+
(while t
|
|
222
|
+
(let* ((raw-form (read (current-buffer)))
|
|
223
|
+
(checked-form (eliscript-compat--preprocess-form raw-form)))
|
|
224
|
+
(setq last-val (eval checked-form t))))
|
|
225
|
+
(end-of-file last-val)))))
|
|
226
|
+
|
|
227
|
+
(defun eliscript-compat-eval-file (file)
|
|
228
|
+
"Read and evaluate unmodified Emacs Lisp or Eliscript FILE in 0.1.0 session."
|
|
229
|
+
(with-temp-buffer
|
|
230
|
+
(insert-file-contents file)
|
|
231
|
+
(eliscript-compat-eval-string (buffer-string))))
|
|
232
|
+
|
|
233
|
+
(defun eliscript-compat--eliscript-string (text)
|
|
234
|
+
"Return TEXT as a string literal readable by both Eliscript readers."
|
|
235
|
+
(concat "\""
|
|
236
|
+
(replace-regexp-in-string
|
|
237
|
+
"[\"\\[:cntrl:]\177]"
|
|
238
|
+
(lambda (match)
|
|
239
|
+
(pcase (aref match 0)
|
|
240
|
+
(?\" "\\\"") (?\\ "\\\\") (?\n "\\n") (?\t "\\t") (?\r "\\r")
|
|
241
|
+
(c (format "\\u%04x" c))))
|
|
242
|
+
text t t)
|
|
243
|
+
"\""))
|
|
244
|
+
|
|
245
|
+
(defun eliscript-compat--elisp-module-source (source function-names module-name)
|
|
246
|
+
"Return Eliscript module text that evaluates Elisp SOURCE in a session.
|
|
247
|
+
FUNCTION-NAMES are the top-level functions SOURCE defines; MODULE-NAME names
|
|
248
|
+
the generated module. The result is Eliscript, compiled like any other module."
|
|
249
|
+
(let ((print-length nil) (print-level nil) (print-circle nil)
|
|
250
|
+
(print-escape-newlines nil) (print-escape-nonascii nil))
|
|
251
|
+
(concat
|
|
252
|
+
"(module " (symbol-name (intern module-name)) "\n"
|
|
253
|
+
" (import \"eliscript/runtime/emacs-compat\""
|
|
254
|
+
" createCompatSession evalCompatProgram callCompatElisp invokeCompatFunction)\n"
|
|
255
|
+
" (defconst elisp-source " (eliscript-compat--eliscript-string source) ")\n"
|
|
256
|
+
" (defconst elisp-function-names (js-array"
|
|
257
|
+
(mapconcat (lambda (name) (concat " " (eliscript-compat--eliscript-string name))) function-names "")
|
|
258
|
+
"))\n"
|
|
259
|
+
" (defconst session (createCompatSession))\n"
|
|
260
|
+
" (defconst loaded (evalCompatProgram elisp-source (js-object :session session)))\n"
|
|
261
|
+
" (defun callElisp (name arguments) (callCompatElisp session name arguments))\n"
|
|
262
|
+
" (defun invokeElisp (name &rest arguments) (invokeCompatFunction session name arguments))\n"
|
|
263
|
+
" (defun elispFunctions () (js-call elisp-function-names :slice))\n"
|
|
264
|
+
" (export callElisp invokeElisp elispFunctions))\n")))
|
|
265
|
+
|
|
266
|
+
(defun eliscript-compat-compile-elisp-module (el-files out-mjs-file)
|
|
267
|
+
"Compile one or more unmodified EL-FILES into a Worker-ready OUT-MJS-FILE.
|
|
268
|
+
The intermediate Eliscript module is written next to OUT-MJS-FILE with the
|
|
269
|
+
extension .eli and compiled by the maintained Emacs Lisp Eliscript compiler.
|
|
270
|
+
OUT-MJS-FILE must be inside a project that resolves the `eliscript' package.
|
|
271
|
+
Return the absolute path of OUT-MJS-FILE."
|
|
272
|
+
(let* ((files (if (listp el-files) el-files (list el-files)))
|
|
273
|
+
(combined
|
|
274
|
+
(mapconcat
|
|
275
|
+
(lambda (f)
|
|
276
|
+
(with-temp-buffer
|
|
277
|
+
(insert-file-contents (expand-file-name f))
|
|
278
|
+
(buffer-string)))
|
|
279
|
+
files
|
|
280
|
+
"\n"))
|
|
281
|
+
(out-mjs (expand-file-name out-mjs-file))
|
|
282
|
+
(out-eli (concat (file-name-sans-extension out-mjs) ".eli"))
|
|
283
|
+
(module-name (concat "eliscript.generated."
|
|
284
|
+
(replace-regexp-in-string
|
|
285
|
+
"[^A-Za-z0-9-]" "-" (file-name-base out-mjs)))))
|
|
286
|
+
(unless (featurep 'eliscript)
|
|
287
|
+
(add-to-list 'load-path (expand-file-name "compiler" eliscript-compat--project-directory))
|
|
288
|
+
(require 'eliscript))
|
|
289
|
+
(make-directory (file-name-directory out-mjs) t)
|
|
290
|
+
(let ((coding-system-for-write 'utf-8-unix))
|
|
291
|
+
(with-temp-file out-eli
|
|
292
|
+
(insert (eliscript-compat--elisp-module-source
|
|
293
|
+
combined
|
|
294
|
+
(eliscript-compat--collect-defined-functions combined)
|
|
295
|
+
module-name))))
|
|
296
|
+
(eliscript-compile-file out-eli out-mjs)
|
|
297
|
+
out-mjs))
|
|
298
|
+
|
|
299
|
+
(defun eliscript-compat--native-function (fn-sym)
|
|
300
|
+
"Return the native Emacs Lisp definition of FN-SYM."
|
|
301
|
+
(or (get fn-sym 'eliscript-compat-native-function)
|
|
302
|
+
(symbol-function fn-sym)))
|
|
303
|
+
|
|
304
|
+
(defun eliscript-compat-offload-call (worker mjs-file fn-sym &rest args)
|
|
305
|
+
"Invoke FN-SYM with ARGS in MJS-FILE on WORKER, falling back to native Elisp.
|
|
306
|
+
Arguments and the result cross the Worker boundary as printed Emacs Lisp text
|
|
307
|
+
through the module export `callElisp'. Without a live WORKER, or when the
|
|
308
|
+
Worker call fails, the native definition of FN-SYM runs instead."
|
|
309
|
+
(if (and worker
|
|
310
|
+
(fboundp 'eliscript-worker-live-p)
|
|
311
|
+
(eliscript-worker-live-p worker)
|
|
312
|
+
(fboundp 'eliscript-worker-call-sync))
|
|
313
|
+
(condition-case nil
|
|
314
|
+
(let* ((sexp (let ((print-length nil) (print-level nil)
|
|
315
|
+
(print-circle nil) (print-escape-newlines nil))
|
|
316
|
+
(prin1-to-string args)))
|
|
317
|
+
(reply (eliscript-worker-call-sync
|
|
318
|
+
worker (expand-file-name mjs-file) "callElisp"
|
|
319
|
+
(list (symbol-name fn-sym) sexp))))
|
|
320
|
+
(unless (stringp reply)
|
|
321
|
+
(error "Invalid Eliscript Worker reply"))
|
|
322
|
+
(car (read-from-string reply)))
|
|
323
|
+
(error (apply (eliscript-compat--native-function fn-sym) args)))
|
|
324
|
+
(apply (eliscript-compat--native-function fn-sym) args)))
|
|
325
|
+
|
|
326
|
+
(defun eliscript-compat-accelerate-function (fn-sym mjs-file &optional worker-getter predicate)
|
|
327
|
+
"Install transparent Eliscript Worker acceleration on FN-SYM using MJS-FILE."
|
|
328
|
+
(let ((orig (or (get fn-sym 'eliscript-compat-native-function)
|
|
329
|
+
(symbol-function fn-sym))))
|
|
330
|
+
(unless (get fn-sym 'eliscript-compat-native-function)
|
|
331
|
+
(put fn-sym 'eliscript-compat-native-function orig)
|
|
332
|
+
(push (cons fn-sym orig) eliscript-compat--accelerated-functions))
|
|
333
|
+
(fset fn-sym
|
|
334
|
+
(lambda (&rest args)
|
|
335
|
+
(let ((worker (and worker-getter (funcall worker-getter))))
|
|
336
|
+
(if (and worker
|
|
337
|
+
(fboundp 'eliscript-worker-live-p)
|
|
338
|
+
(eliscript-worker-live-p worker)
|
|
339
|
+
(or (null predicate) (apply predicate args)))
|
|
340
|
+
(apply #'eliscript-compat-offload-call worker mjs-file fn-sym args)
|
|
341
|
+
(apply orig args)))))
|
|
342
|
+
fn-sym)))
|
|
343
|
+
|
|
344
|
+
(provide 'eliscript-compat)
|
|
345
|
+
|
|
346
|
+
;;; eliscript-compat.el ends here
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
;;; eliscript-contract.el --- Static declarations and runtime contracts for Eliscript 0.1.0 -*- lexical-binding: t; -*-
|
|
2
|
+
|
|
3
|
+
;;; Commentary:
|
|
4
|
+
|
|
5
|
+
;; Implements `(declare (eliscript ...))` parsing, static contract validation
|
|
6
|
+
;; (`:pure`, `:portable`, `:checked`, `:async`, `:destructure`, `:abi`,
|
|
7
|
+
;; `:export`, `:inputs`), runtime pre/post-condition assertions (`:params`,
|
|
8
|
+
;; `:returns`, `:requires`, `:ensures`), and Emacs `defun-declarations-alist`
|
|
9
|
+
;; integration.
|
|
10
|
+
|
|
11
|
+
;;; Code:
|
|
12
|
+
|
|
13
|
+
(require 'cl-lib)
|
|
14
|
+
|
|
15
|
+
(define-error 'eliscript-compile-error "Eliscript compile error")
|
|
16
|
+
(define-error 'eliscript-declaration-error "Eliscript declaration error" 'eliscript-compile-error)
|
|
17
|
+
(define-error 'eliscript-contract-error "Eliscript static contract error" 'eliscript-compile-error)
|
|
18
|
+
(define-error 'eliscript-runtime-contract-error "Eliscript runtime contract violation")
|
|
19
|
+
|
|
20
|
+
(defconst eliscript-contract--valid-keys
|
|
21
|
+
'(:pure :portable :checked :async :destructure
|
|
22
|
+
:params :returns :requires :ensures
|
|
23
|
+
:abi :export :inputs)
|
|
24
|
+
"Valid keys allowed inside `(declare (eliscript ...))`.")
|
|
25
|
+
|
|
26
|
+
(defconst eliscript-contract--impure-Heads
|
|
27
|
+
'(puthash remhash clrhash setcar setcdr aset fset set put
|
|
28
|
+
insert delete-region write-region message princ prin1 print
|
|
29
|
+
random current-time float-time
|
|
30
|
+
cell-update! cell-reset!
|
|
31
|
+
channel-put! channel-take! channel-close!
|
|
32
|
+
engine-call engine-revoke!
|
|
33
|
+
mem-store-u8 mem-store-u16 mem-store-u32 mem-store-f64 mem-grow)
|
|
34
|
+
"Operators forbidden inside `:pure t` functions.")
|
|
35
|
+
|
|
36
|
+
(defconst eliscript-contract--non-portable-heads
|
|
37
|
+
'(current-buffer set-buffer with-current-buffer get-buffer get-buffer-create
|
|
38
|
+
kill-buffer buffer-string buffer-substring point goto-char
|
|
39
|
+
insert delete-region selected-window selected-frame window-buffer
|
|
40
|
+
start-process make-process call-process process-send-string)
|
|
41
|
+
"Host-specific Emacs operators forbidden inside `:portable t` functions.")
|
|
42
|
+
|
|
43
|
+
(defconst eliscript-contract--abi-forbidden-alloc-heads
|
|
44
|
+
'(list cons vector make-vector make-hash-table
|
|
45
|
+
persistent-list persistent-vector persistent-map persistent-set
|
|
46
|
+
persistent-queue persistent-sorted-map persistent-sorted-set
|
|
47
|
+
concat format make-string eval funcall apply)
|
|
48
|
+
"Heap-allocating or dynamic operators forbidden inside `:abi` kernels.")
|
|
49
|
+
|
|
50
|
+
(defvar eliscript-contract--checked-functions (make-hash-table :test 'eq)
|
|
51
|
+
"Registry of functions validated with static contracts.")
|
|
52
|
+
|
|
53
|
+
(progn
|
|
54
|
+
(defun eliscript-contract-reset-state! ()
|
|
55
|
+
"Reset cached contract state for isolated test runs."
|
|
56
|
+
(clrhash eliscript-contract--checked-functions)))
|
|
57
|
+
|
|
58
|
+
(defun eliscript-contract--merge-plist (target source context-name)
|
|
59
|
+
"Merge SOURCE eliscript declaration plist into TARGET for CONTEXT-NAME."
|
|
60
|
+
(let ((cursor source)
|
|
61
|
+
(out (copy-sequence target)))
|
|
62
|
+
(while cursor
|
|
63
|
+
(unless (cdr cursor)
|
|
64
|
+
(signal 'eliscript-declaration-error
|
|
65
|
+
(list (format "ELI-D0003: odd number of elements in (declare (eliscript ...)) for %s"
|
|
66
|
+
context-name))))
|
|
67
|
+
(let ((k (car cursor))
|
|
68
|
+
(v (cadr cursor)))
|
|
69
|
+
(unless (memq k eliscript-contract--valid-keys)
|
|
70
|
+
(signal 'eliscript-declaration-error
|
|
71
|
+
(list (format "ELI-D0004: unknown eliscript declaration key %S in %s"
|
|
72
|
+
k context-name))))
|
|
73
|
+
(if (plist-member out k)
|
|
74
|
+
(let ((existing (plist-get out k)))
|
|
75
|
+
(unless (equal existing v)
|
|
76
|
+
(signal 'eliscript-declaration-error
|
|
77
|
+
(list (format "ELI-D0005: contradictory declaration for %S in %s (%S vs %S)"
|
|
78
|
+
k context-name existing v)))))
|
|
79
|
+
(setq out (plist-put out k v))))
|
|
80
|
+
(setq cursor (cddr cursor)))
|
|
81
|
+
out))
|
|
82
|
+
|
|
83
|
+
(progn
|
|
84
|
+
(defun eliscript-contract-extract-declarations (body &optional context-name)
|
|
85
|
+
"Extract docstring and `(declare ...)` forms from BODY for CONTEXT-NAME.
|
|
86
|
+
Return a plist `(:doc DOC :eliscript PLIST :declarations DECLS :body BODY)'."
|
|
87
|
+
(let ((doc nil)
|
|
88
|
+
(eli-plist nil)
|
|
89
|
+
(other-decls nil)
|
|
90
|
+
(rest body)
|
|
91
|
+
(scanning t))
|
|
92
|
+
(while (and scanning rest)
|
|
93
|
+
(let ((form (car rest)))
|
|
94
|
+
(cond
|
|
95
|
+
((and (stringp form) (null doc) (cdr rest))
|
|
96
|
+
(setq doc form)
|
|
97
|
+
(setq rest (cdr rest)))
|
|
98
|
+
((and (consp form) (eq (car form) 'declare))
|
|
99
|
+
(dolist (spec (cdr form))
|
|
100
|
+
(if (and (consp spec) (eq (car spec) 'eliscript))
|
|
101
|
+
(setq eli-plist
|
|
102
|
+
(eliscript-contract--merge-plist
|
|
103
|
+
eli-plist (cdr spec) (or context-name 'anonymous)))
|
|
104
|
+
(push spec other-decls)))
|
|
105
|
+
(setq rest (cdr rest)))
|
|
106
|
+
(t
|
|
107
|
+
(setq scanning nil)))))
|
|
108
|
+
(list :doc doc
|
|
109
|
+
:eliscript eli-plist
|
|
110
|
+
:declarations (nreverse other-decls)
|
|
111
|
+
:body rest))))
|
|
112
|
+
|
|
113
|
+
(defun eliscript-contract--extract-arg-symbols (arglist)
|
|
114
|
+
"Extract bound variable symbols from ARGLIST."
|
|
115
|
+
(let ((syms nil))
|
|
116
|
+
(dolist (item arglist)
|
|
117
|
+
(cond
|
|
118
|
+
((and (symbolp item)
|
|
119
|
+
(not (memq item '(&optional &rest &aux)))
|
|
120
|
+
(not (keywordp item)))
|
|
121
|
+
(push item syms))
|
|
122
|
+
((consp item)
|
|
123
|
+
(when (symbolp (car item))
|
|
124
|
+
(push (car item) syms)))))
|
|
125
|
+
syms))
|
|
126
|
+
|
|
127
|
+
(defun eliscript-contract--scan-pure (form local-vars context-name)
|
|
128
|
+
"Verify FORM contains no impure operations given LOCAL-VARS in CONTEXT-NAME."
|
|
129
|
+
(when (consp form)
|
|
130
|
+
(let ((head (car form)))
|
|
131
|
+
(when (memq head eliscript-contract--impure-Heads)
|
|
132
|
+
(signal 'eliscript-contract-error
|
|
133
|
+
(list (format "ELI-C0001: impure operation `%S' is forbidden in :pure function %s"
|
|
134
|
+
head context-name))))
|
|
135
|
+
(cond
|
|
136
|
+
((eq head 'quote) nil)
|
|
137
|
+
((eq head 'setq)
|
|
138
|
+
(let ((pairs (cdr form)))
|
|
139
|
+
(while pairs
|
|
140
|
+
(let ((sym (car pairs))
|
|
141
|
+
(val (cadr pairs)))
|
|
142
|
+
(unless (memq sym local-vars)
|
|
143
|
+
(signal 'eliscript-contract-error
|
|
144
|
+
(list (format "ELI-C0001: mutating non-local variable `%S' via setq is forbidden in :pure function %s"
|
|
145
|
+
sym context-name))))
|
|
146
|
+
(eliscript-contract--scan-pure val local-vars context-name))
|
|
147
|
+
(setq pairs (cddr pairs)))))
|
|
148
|
+
((memq head '(let let* let-const let*-const let-loop))
|
|
149
|
+
(let* ((has-name (and (eq head 'let-loop) (symbolp (cadr form))))
|
|
150
|
+
(bindings (if has-name (nth 2 form) (cadr form)))
|
|
151
|
+
(body (if has-name (nthcdr 3 form) (cddr form)))
|
|
152
|
+
(extended local-vars))
|
|
153
|
+
(dolist (b bindings)
|
|
154
|
+
(if (consp b)
|
|
155
|
+
(progn
|
|
156
|
+
(eliscript-contract--scan-pure (cadr b) extended context-name)
|
|
157
|
+
(when (symbolp (car b))
|
|
158
|
+
(push (car b) extended)))
|
|
159
|
+
(when (symbolp b)
|
|
160
|
+
(push b extended))))
|
|
161
|
+
(dolist (sub body)
|
|
162
|
+
(eliscript-contract--scan-pure sub extended context-name))))
|
|
163
|
+
((memq head '(lambda lambda-async lambda-tail lambda-bind))
|
|
164
|
+
(let ((ext (append (eliscript-contract--extract-arg-symbols (cadr form))
|
|
165
|
+
local-vars)))
|
|
166
|
+
(dolist (sub (cddr form))
|
|
167
|
+
(eliscript-contract--scan-pure sub ext context-name))))
|
|
168
|
+
((memq head '(dolist dotimes))
|
|
169
|
+
(let* ((spec (cadr form))
|
|
170
|
+
(var (car spec))
|
|
171
|
+
(ext (cons var local-vars)))
|
|
172
|
+
(eliscript-contract--scan-pure (cadr spec) local-vars context-name)
|
|
173
|
+
(dolist (sub (cddr form))
|
|
174
|
+
(eliscript-contract--scan-pure sub ext context-name))))
|
|
175
|
+
(t
|
|
176
|
+
(dolist (sub (cdr form))
|
|
177
|
+
(eliscript-contract--scan-pure sub local-vars context-name)))))))
|
|
178
|
+
|
|
179
|
+
(defun eliscript-contract--scan-portable (form context-name)
|
|
180
|
+
"Verify FORM contains no non-portable host calls in CONTEXT-NAME."
|
|
181
|
+
(when (consp form)
|
|
182
|
+
(let ((head (car form)))
|
|
183
|
+
(unless (eq head 'quote)
|
|
184
|
+
(when (memq head eliscript-contract--non-portable-heads)
|
|
185
|
+
(signal 'eliscript-contract-error
|
|
186
|
+
(list (format "ELI-C0002: host-specific operator `%S' is forbidden in :portable function %s"
|
|
187
|
+
head context-name))))
|
|
188
|
+
(dolist (sub (cdr form))
|
|
189
|
+
(eliscript-contract--scan-portable sub context-name))))))
|
|
190
|
+
|
|
191
|
+
(defun eliscript-contract--scan-abi (form abi-kind context-name)
|
|
192
|
+
"Verify FORM conforms to ABI-KIND in CONTEXT-NAME."
|
|
193
|
+
(cond
|
|
194
|
+
((and (eq abi-kind :numeric-f64) (or (stringp form) (vectorp form)))
|
|
195
|
+
(signal 'eliscript-contract-error
|
|
196
|
+
(list (format "ELI-B0001: heap literal `%S' is forbidden in :abi :numeric-f64 kernel %s"
|
|
197
|
+
form context-name))))
|
|
198
|
+
((consp form)
|
|
199
|
+
(let ((head (car form)))
|
|
200
|
+
(cond
|
|
201
|
+
((eq head 'quote)
|
|
202
|
+
(signal 'eliscript-contract-error
|
|
203
|
+
(list (format "ELI-B0001: quoted literal is forbidden in :abi %S kernel %s"
|
|
204
|
+
abi-kind context-name))))
|
|
205
|
+
((memq head eliscript-contract--abi-forbidden-alloc-heads)
|
|
206
|
+
(signal 'eliscript-contract-error
|
|
207
|
+
(list (format "ELI-B0001: dynamic or allocating operator `%S' is forbidden in :abi %S kernel %s"
|
|
208
|
+
head abi-kind context-name))))
|
|
209
|
+
(t
|
|
210
|
+
(dolist (sub (cdr form))
|
|
211
|
+
(eliscript-contract--scan-abi sub abi-kind context-name))))))))
|
|
212
|
+
|
|
213
|
+
(progn
|
|
214
|
+
(defun eliscript-contract-validate-context (context-kind name plist arglist body)
|
|
215
|
+
"Validate declaration PLIST for CONTEXT-KIND NAME with ARGLIST and BODY."
|
|
216
|
+
(when (memq context-kind '(defun lambda))
|
|
217
|
+
(when (plist-get plist :async)
|
|
218
|
+
(signal 'eliscript-declaration-error
|
|
219
|
+
(list (format "ELI-D0001: ordinary %s `%s' cannot declare :async t; use defun-async or lambda-async"
|
|
220
|
+
context-kind name))))
|
|
221
|
+
(when (plist-get plist :destructure)
|
|
222
|
+
(signal 'eliscript-declaration-error
|
|
223
|
+
(list (format "ELI-D0002: ordinary %s `%s' cannot declare :destructure t; use defun-bind or lambda-bind"
|
|
224
|
+
context-kind name)))))
|
|
225
|
+
(when (and (plist-get plist :inputs)
|
|
226
|
+
(not (eq context-kind 'defmacro-checked)))
|
|
227
|
+
(signal 'eliscript-declaration-error
|
|
228
|
+
(list (format "ELI-D0006: :inputs is only valid on defmacro-checked (in %s)" name))))
|
|
229
|
+
(let ((abi (plist-get plist :abi)))
|
|
230
|
+
(when abi
|
|
231
|
+
(unless (memq abi '(:numeric-f64 :memory-u32))
|
|
232
|
+
(signal 'eliscript-declaration-error
|
|
233
|
+
(list (format "ELI-B0001: unsupported :abi %S in %s (expected :numeric-f64 or :memory-u32)"
|
|
234
|
+
abi name))))
|
|
235
|
+
(dolist (form body)
|
|
236
|
+
(eliscript-contract--scan-abi form abi name))))
|
|
237
|
+
(when (plist-get plist :export)
|
|
238
|
+
(let ((exp (plist-get plist :export)))
|
|
239
|
+
(unless (and (stringp exp) (> (length exp) 0))
|
|
240
|
+
(signal 'eliscript-declaration-error
|
|
241
|
+
(list (format "ELI-D0007: :export must be a non-empty string in %s" name))))))
|
|
242
|
+
(let ((locals (eliscript-contract--extract-arg-symbols arglist)))
|
|
243
|
+
(when (plist-get plist :pure)
|
|
244
|
+
(dolist (form body)
|
|
245
|
+
(eliscript-contract--scan-pure form locals name)))
|
|
246
|
+
(when (plist-get plist :portable)
|
|
247
|
+
(dolist (form body)
|
|
248
|
+
(eliscript-contract--scan-portable form name))))
|
|
249
|
+
(when (and name (symbolp name))
|
|
250
|
+
(puthash name plist eliscript-contract--checked-functions))
|
|
251
|
+
t)
|
|
252
|
+
|
|
253
|
+
(defun eliscript-contract-check-value (value spec context-label)
|
|
254
|
+
"Check whether VALUE satisfies type or predicate SPEC in CONTEXT-LABEL."
|
|
255
|
+
(let ((ok
|
|
256
|
+
(cond
|
|
257
|
+
((eq spec :f64) (numberp value))
|
|
258
|
+
((eq spec :i32) (integerp value))
|
|
259
|
+
((eq spec :u32) (and (integerp value) (>= value 0)))
|
|
260
|
+
((eq spec :bool) (booleanp value))
|
|
261
|
+
((eq spec :string) (stringp value))
|
|
262
|
+
((eq spec :any) t)
|
|
263
|
+
((symbolp spec)
|
|
264
|
+
(if (fboundp spec)
|
|
265
|
+
(funcall spec value)
|
|
266
|
+
t))
|
|
267
|
+
((functionp spec)
|
|
268
|
+
(funcall spec value))
|
|
269
|
+
(t t))))
|
|
270
|
+
(unless ok
|
|
271
|
+
(signal 'eliscript-runtime-contract-error
|
|
272
|
+
(list (format "Contract violation in %s: expected %S, got %S"
|
|
273
|
+
context-label spec value))))
|
|
274
|
+
t))
|
|
275
|
+
|
|
276
|
+
(defun eliscript-contract-wrap-body (name plist _arglist body)
|
|
277
|
+
"Wrap BODY with runtime checks for `:params', `:requires', `:ensures'."
|
|
278
|
+
(let ((params-spec (plist-get plist :params))
|
|
279
|
+
(requires-spec (plist-get plist :requires))
|
|
280
|
+
(returns-spec (plist-get plist :returns))
|
|
281
|
+
(ensures-spec (plist-get plist :ensures)))
|
|
282
|
+
(if (and (null params-spec)
|
|
283
|
+
(null requires-spec)
|
|
284
|
+
(null returns-spec)
|
|
285
|
+
(null ensures-spec))
|
|
286
|
+
body
|
|
287
|
+
(let ((param-checks nil)
|
|
288
|
+
(req-checks nil)
|
|
289
|
+
(ens-checks nil)
|
|
290
|
+
(res-sym (make-symbol "contract-result")))
|
|
291
|
+
(dolist (item params-spec)
|
|
292
|
+
(when (and (consp item)
|
|
293
|
+
(symbolp (car item))
|
|
294
|
+
(not (memq (car item) '(&optional &rest))))
|
|
295
|
+
(let ((p-var (car item))
|
|
296
|
+
(p-type (cadr item)))
|
|
297
|
+
(push `(eliscript-contract-check-value
|
|
298
|
+
,p-var ',p-type ,(format "%s parameter %s" name p-var))
|
|
299
|
+
param-checks))))
|
|
300
|
+
(dolist (req requires-spec)
|
|
301
|
+
(push `(unless ,req
|
|
302
|
+
(signal 'eliscript-runtime-contract-error
|
|
303
|
+
(list ,(format "Precondition (:requires %S) failed in %s" req name))))
|
|
304
|
+
req-checks))
|
|
305
|
+
(dolist (ens ensures-spec)
|
|
306
|
+
(push `(unless ,ens
|
|
307
|
+
(signal 'eliscript-runtime-contract-error
|
|
308
|
+
(list ,(format "Postcondition (:ensures %S) failed in %s" ens name))))
|
|
309
|
+
ens-checks))
|
|
310
|
+
`(,@(nreverse param-checks)
|
|
311
|
+
,@(nreverse req-checks)
|
|
312
|
+
(let ((,res-sym (progn ,@body)))
|
|
313
|
+
,@(when returns-spec
|
|
314
|
+
`((eliscript-contract-check-value
|
|
315
|
+
,res-sym ',returns-spec ,(format "%s return value" name))))
|
|
316
|
+
,@(when ens-checks
|
|
317
|
+
`((let ((result ,res-sym))
|
|
318
|
+
(ignore result)
|
|
319
|
+
,@(nreverse ens-checks))))
|
|
320
|
+
,res-sym)))))))
|
|
321
|
+
|
|
322
|
+
(defun eliscript-contract--defun-declaration-handler (fn-name args &rest specs)
|
|
323
|
+
"Handle `(declare (eliscript ...))` inside standard Emacs `defun'."
|
|
324
|
+
(let ((plist (eliscript-contract--merge-plist nil specs fn-name)))
|
|
325
|
+
(eliscript-contract-validate-context 'defun fn-name plist args nil)
|
|
326
|
+
`(put ',fn-name 'eliscript-contract ',plist)))
|
|
327
|
+
|
|
328
|
+
(unless (assq 'eliscript defun-declarations-alist)
|
|
329
|
+
(push (list 'eliscript #'eliscript-contract--defun-declaration-handler)
|
|
330
|
+
defun-declarations-alist))
|
|
331
|
+
|
|
332
|
+
(provide 'eliscript-contract)
|
|
333
|
+
|
|
334
|
+
;;; eliscript-contract.el ends here
|