gdp-wired 0.31.0 → 0.32.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.
Files changed (73) hide show
  1. package/README.md +82 -6
  2. package/dist/core/c/reader.d.ts +86 -0
  3. package/dist/core/c/reader.js +860 -0
  4. package/dist/core/cli.d.ts +66 -0
  5. package/dist/core/cli.js +582 -0
  6. package/dist/core/dsl.d.ts +4 -2
  7. package/dist/core/dsl.js +22 -12
  8. package/dist/core/effect.d.ts +6 -4
  9. package/dist/core/effect.js +48 -10
  10. package/dist/core/ex/harness.exs +177 -0
  11. package/dist/core/ex/json.exs +46 -0
  12. package/dist/core/ex/reader.exs +462 -0
  13. package/dist/core/exists.d.ts +3 -0
  14. package/dist/core/exists.js +3 -1
  15. package/dist/core/extract-c.d.ts +35 -0
  16. package/dist/core/extract-c.js +424 -0
  17. package/dist/core/extract-check.js +27 -6
  18. package/dist/core/extract-ex.d.ts +16 -0
  19. package/dist/core/extract-ex.js +33 -0
  20. package/dist/core/extract-go-check.d.ts +28 -0
  21. package/dist/core/extract-go-check.js +639 -0
  22. package/dist/core/extract-go.d.ts +231 -0
  23. package/dist/core/extract-go.js +1166 -0
  24. package/dist/core/extract-jvm.d.ts +23 -0
  25. package/dist/core/extract-jvm.js +220 -0
  26. package/dist/core/extract-print.js +17 -7
  27. package/dist/core/extract-py.d.ts +15 -0
  28. package/dist/core/extract-py.js +31 -0
  29. package/dist/core/extract-rb.d.ts +17 -0
  30. package/dist/core/extract-rb.js +31 -0
  31. package/dist/core/extract-read.d.ts +20 -0
  32. package/dist/core/extract-read.js +46 -6
  33. package/dist/core/extract-rs.d.ts +12 -0
  34. package/dist/core/extract-rs.js +306 -0
  35. package/dist/core/extract-shapes.d.ts +2 -0
  36. package/dist/core/extract-shapes.js +6 -5
  37. package/dist/core/extract-step.d.ts +806 -0
  38. package/dist/core/extract-step.js +4141 -0
  39. package/dist/core/extract-swift.d.ts +27 -0
  40. package/dist/core/extract-swift.js +484 -0
  41. package/dist/core/extract-xstate.d.ts +10 -3
  42. package/dist/core/extract-xstate.js +50 -4
  43. package/dist/core/extract.d.ts +22 -3
  44. package/dist/core/extract.js +107 -18
  45. package/dist/core/go/go.mod +3 -0
  46. package/dist/core/go/reader.go +901 -0
  47. package/dist/core/infer.js +1770 -52
  48. package/dist/core/jvm/Harness.java +361 -0
  49. package/dist/core/jvm/JavaReader.java +825 -0
  50. package/dist/core/jvm/Jvm.java +332 -0
  51. package/dist/core/jvm/KtReader.java +960 -0
  52. package/dist/core/jvm/Rt.java +10 -0
  53. package/dist/core/laws.d.ts +66 -0
  54. package/dist/core/laws.js +522 -0
  55. package/dist/core/py/harness.py +328 -0
  56. package/dist/core/py/reader.py +447 -0
  57. package/dist/core/rb/harness.rb +233 -0
  58. package/dist/core/rb/reader.rb +375 -0
  59. package/dist/core/rs/Cargo.lock +108 -0
  60. package/dist/core/rs/Cargo.toml +14 -0
  61. package/dist/core/rs/src/main.rs +681 -0
  62. package/dist/core/swift/Harness.swift +124 -0
  63. package/dist/core/swift/Main.swift +34 -0
  64. package/dist/core/swift/Package.swift +13 -0
  65. package/dist/core/swift/Sources/gdp-swiftreader/main.swift +1341 -0
  66. package/dist/kernel/cdcl.js +43 -18
  67. package/dist/kernel/epr.d.ts +35 -0
  68. package/dist/kernel/epr.js +1676 -0
  69. package/dist/kernel/prove.d.ts +44 -0
  70. package/dist/kernel/prove.js +668 -54
  71. package/dist/lean-kernel/emit.d.ts +1 -0
  72. package/dist/lean-kernel/emit.js +21 -4
  73. package/package.json +53 -6
@@ -0,0 +1,361 @@
1
+ // The JVM side of the extractor's check (core/extract-step.ts's checkStep), for Java and Kotlin alike: the class
2
+ // loaded as compiled from the code (or as the project ships it), its object made, set to a state, called with the
3
+ // rule's requests and read back by reflection, one JSON line a request over two named pipes. The clock and an answered
4
+ // call are the request's (the check's copy of the code reads them through gdp.Rt). Atomics, Optional and Kotlin's
5
+ // nullable types are read and set through what they hold; a map's cells through the map the object already has.
6
+ //
7
+ // java -cp <tools>:<the code's classes> gdp.jvm.Harness <in-fifo> <out-fifo>
8
+ package gdp.jvm;
9
+
10
+ import java.io.*;
11
+ import java.lang.reflect.*;
12
+ import java.net.URL;
13
+ import java.net.URLClassLoader;
14
+ import java.nio.charset.StandardCharsets;
15
+ import java.util.*;
16
+ import java.util.concurrent.atomic.*;
17
+ import static gdp.jvm.Jvm.*;
18
+
19
+ public final class Harness {
20
+ static Map<?, ?> S;
21
+ static Class<?> cls;
22
+ static ClassLoader loader;
23
+ static final Map<Long, Object> OBJS = new HashMap<>();
24
+ static Map<?, ?> CUR = Map.of();
25
+ // Each object's stores (an injected dependency the check gives: get, set and delete over a map that keeps what is
26
+ // set, its other methods answered by the request), and the current object's.
27
+ static final Map<Long, Map<String, Store>> STORES = new HashMap<>();
28
+ static Map<String, Store> ST = new HashMap<>();
29
+ static final class Store { final Map<String, Object> data = new LinkedHashMap<>(); Method get; }
30
+ static List<?> list(Object o) { return o instanceof List<?> l ? l : List.of(); }
31
+ // An injected dependency of an interface type, given as a store.
32
+ static Object store(String name, Class<?> c) {
33
+ if (!c.isInterface()) throw new IllegalArgumentException(name + ": a store of a class (the check gives one of an interface)");
34
+ Store st = ST.computeIfAbsent(name, k -> new Store());
35
+ Map<?, ?> d = (Map<?, ?>) ((Map<?, ?>) S.get("stores")).get(name);
36
+ // (the executor's defaults where nothing names them)
37
+ List<?> gets = d != null && d.get("get") != null ? list(d.get("get")) : List.of("get", "async_get_cache", "get_cache"), sets = d != null && d.get("set") != null ? list(d.get("set")) : List.of("set", "async_set_cache", "set_cache"), dels = d != null && d.get("delete") != null ? list(d.get("delete")) : List.of("delete", "delete_cache", "async_delete_cache");
38
+ for (Method gm : c.getMethods()) if (gets.contains(gm.getName()) && gm.getParameterCount() == 1) st.get = gm;
39
+ return Proxy.newProxyInstance(loader, new Class<?>[]{c}, (o, m, a) -> {
40
+ String n = m.getName();
41
+ if (n.equals("toString") && m.getParameterCount() == 0) return "store " + name;
42
+ if (n.equals("hashCode") && m.getParameterCount() == 0) return System.identityHashCode(o);
43
+ if (n.equals("equals") && m.getParameterCount() == 1) return o == a[0];
44
+ if (gets.contains(n) && a != null && a.length >= 1) return conv(st.data.get(String.valueOf(a[0] instanceof Enum<?> e ? e.name() : a[0])), m.getReturnType(), m.getGenericReturnType());
45
+ if (sets.contains(n) && a != null && a.length >= 2) { st.data.put(String.valueOf(a[0] instanceof Enum<?> e ? e.name() : a[0]), a[1]); return m.getReturnType() == void.class ? null : conv(null, m.getReturnType(), m.getGenericReturnType()); }
46
+ if (dels.contains(n) && a != null && a.length >= 1) { st.data.remove(String.valueOf(a[0])); return m.getReturnType() == void.class || m.getReturnType().isPrimitive() ? conv(null, m.getReturnType(), null) : null; }
47
+ for (Object a0 : list(S.get("answers"))) {
48
+ Map<?, ?> an = (Map<?, ?>) a0, pt = (Map<?, ?>) an.get("patch");
49
+ if (pt != null && name.equals(pt.get("store")) && n.equals(list(pt.get("path")).get(list(pt.get("path")).size() - 1))) { Object v = CUR.get(an.get("key")); return v == null || ((Map<?, ?>) v).containsKey("none") ? conv(null, m.getReturnType(), m.getGenericReturnType()) : conv(((Map<?, ?>) v).get("in"), m.getReturnType(), m.getGenericReturnType()); }
50
+ }
51
+ // (a method the rule leaves out runs as nothing, as the rule takes it; any other fails: the check reports it)
52
+ if (d == null || !list(d.get("skip")).contains(n)) throw new UnsupportedOperationException(name + "." + n + ": a method of the injected dependency the rule doesn't read");
53
+ return m.getReturnType() == void.class ? null : m.getReturnType().isPrimitive() ? conv(null, m.getReturnType(), null) : m.getReturnType() == Optional.class ? Optional.empty() : null;
54
+ });
55
+ }
56
+ static String tplMatch(String tpl, String key) {
57
+ int i = tpl.indexOf("{}");
58
+ if (i < 0) return key.equals(tpl) ? "" : null;
59
+ String pre = tpl.substring(0, i), post = tpl.substring(i + 2);
60
+ return key.startsWith(pre) && key.endsWith(post) && key.length() >= pre.length() + post.length() ? key.substring(pre.length(), key.length() - post.length()) : null;
61
+ }
62
+
63
+ public static void main(String[] argv) throws Exception {
64
+ BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(argv[0]), StandardCharsets.UTF_8));
65
+ Writer out = new OutputStreamWriter(new FileOutputStream(argv[1]), StandardCharsets.UTF_8);
66
+ gdp.Rt.ANSWER = (k, c) -> { Object a = CUR.get(k); return a == null || ((Map<?, ?>) a).containsKey("none") ? null : conv(((Map<?, ?>) a).get("in"), c, null); };
67
+ for (String line; (line = in.readLine()) != null; ) {
68
+ Map<?, ?> req = (Map<?, ?>) parse(line);
69
+ Object res;
70
+ try { res = handle(req); } catch (Throwable e) { res = m("error", cut(e.toString(), 500)); }
71
+ out.write(json(res)); out.write('\n'); out.flush();
72
+ }
73
+ }
74
+ static String cut(String s, int n) { return s.length() <= n ? s : s.substring(0, n); }
75
+ static String err(Throwable e) { Throwable c = e instanceof InvocationTargetException i && i.getCause() != null ? i.getCause() : e; return cut(c.getClass().getSimpleName() + ": " + c.getMessage(), 300); }
76
+ static long now(Map<?, ?> req) { Object n = req.get("now"); return n == null ? 0 : ((Number) n).longValue(); }
77
+
78
+ static Object handle(Map<?, ?> req) throws Exception {
79
+ String op = (String) req.get("op");
80
+ if (op.equals("new") || op.equals("at")) gdp.Rt.NOW = now(req);
81
+ switch (op) {
82
+ case "hello": {
83
+ S = req;
84
+ List<File> cp = new ArrayList<>();
85
+ for (Object f : (List<?>) req.get("cp")) cp.add(new File((String) f));
86
+ URL[] urls = new URL[cp.size()];
87
+ for (int i = 0; i < urls.length; i++) urls[i] = cp.get(i).toURI().toURL();
88
+ loader = new URLClassLoader(urls, Harness.class.getClassLoader());
89
+ cls = Class.forName((String) req.get("jcls"), false, loader);
90
+ return m("ok", true);
91
+ }
92
+ case "new": try { return m("id", make((List<?>) req.get("a"))); } catch (Throwable e) { return m("err", err(e)); }
93
+ case "at":
94
+ try { long i = make((List<?>) req.get("a")); setState(OBJS.get(i), (Map<?, ?>) req.get("state")); return m("id", i); }
95
+ catch (Throwable e) { return m("bad", err(e)); }
96
+ case "get": { long id = ((Number) req.get("id")).longValue(); ST = STORES.get(id); return m("state", stateOf(OBJS.get(id))); }
97
+ case "call": return call(req);
98
+ case "coverage": return m("hit", List.of());
99
+ default: return m("error", "no op " + op);
100
+ }
101
+ }
102
+
103
+ // --- classes and members --------------------------------------------------------------------------------------------
104
+ static Class<?> type(String n) throws ClassNotFoundException {
105
+ return switch (n) { case "int" -> int.class; case "long" -> long.class; case "short" -> short.class; case "byte" -> byte.class; case "char" -> char.class; case "boolean" -> boolean.class; case "float" -> float.class; case "double" -> double.class; default -> Class.forName(n, false, loader); };
106
+ }
107
+ static Class<?>[] types(List<?> ns) throws ClassNotFoundException { Class<?>[] out = new Class<?>[ns.size()]; for (int i = 0; i < out.length; i++) out[i] = type((String) ns.get(i)); return out; }
108
+ static Field field(Class<?> c, String n) throws NoSuchFieldException {
109
+ for (Class<?> k = c; k != null; k = k.getSuperclass()) try { Field f = k.getDeclaredField(n); f.setAccessible(true); return f; } catch (NoSuchFieldException e) { /* the superclass's */ }
110
+ throw new NoSuchFieldException(n);
111
+ }
112
+ // A method by its JVM name (Kotlin's internal ones are name$module) and parameter types, in the class or a superclass.
113
+ static Method method(Map<?, ?> sig) throws Exception {
114
+ String n = (String) sig.get("name");
115
+ Class<?>[] ps = types((List<?>) sig.get("params"));
116
+ for (Class<?> k = cls; k != null; k = k.getSuperclass())
117
+ for (Method m : k.getDeclaredMethods()) if ((m.getName().equals(n) || m.getName().startsWith(n + "$")) && !m.isSynthetic() && Arrays.equals(m.getParameterTypes(), ps)) { m.setAccessible(true); return m; }
118
+ throw new NoSuchMethodException(n + Arrays.toString(ps));
119
+ }
120
+
121
+ // --- values ---------------------------------------------------------------------------------------------------------
122
+ static Object conv(Object v, Class<?> c, Type g) {
123
+ if (c == Optional.class) return v == null ? Optional.empty() : Optional.of(conv(v, g instanceof ParameterizedType pt ? raw(pt.getActualTypeArguments()[0]) : Object.class, null));
124
+ if (v == null) return c.isPrimitive() ? conv(0L, c, null) : null;
125
+ if (c == int.class || c == Integer.class) return (int) fit(v, Integer.MIN_VALUE, Integer.MAX_VALUE);
126
+ if (c == long.class || c == Long.class) return num(v).longValue();
127
+ if (c == short.class || c == Short.class) return (short) fit(v, Short.MIN_VALUE, Short.MAX_VALUE);
128
+ if (c == byte.class || c == Byte.class) return (byte) fit(v, Byte.MIN_VALUE, Byte.MAX_VALUE);
129
+ if (c == char.class || c == Character.class) return (char) fit(v, 0, 65535);
130
+ if (c == double.class || c == Double.class) { double d = num(v).doubleValue(); if (Math.abs(d) > 9.007199254740992E15) throw new IllegalArgumentException("a double past 2^53 (the subset's floats are whole and exact)"); return d; }
131
+ if (c == float.class || c == Float.class) { double d = num(v).doubleValue(); if (d < -16777216 || d > 16777215) throw new IllegalArgumentException("a float outside [-2^24, 2^24) (the subset's floats are whole and exact)"); return (float) d; }
132
+ if (c == boolean.class || c == Boolean.class) return v;
133
+ if (c.isEnum()) return enumOf(c, (String) v);
134
+ if (c == String.class || c == CharSequence.class) return String.valueOf(v);
135
+ if (c == Object.class) return v;
136
+ throw new IllegalArgumentException("a " + c.getSimpleName() + " from " + v);
137
+ }
138
+ // An integer within its type (one outside it isn't given: the rule refuses it too).
139
+ static long fit(Object v, long lo, long hi) { long n = num(v).longValue(); if (n < lo || n > hi) throw new IllegalArgumentException(n + " is outside the type's range"); return n; }
140
+ @SuppressWarnings({"unchecked", "rawtypes"}) static Object enumOf(Class<?> c, String n) { return Enum.valueOf((Class) c, n); }
141
+ static Number num(Object v) { return v instanceof Number n ? n : v instanceof String s ? (Number) Long.parseLong(s) : (Number) 0L; }
142
+ static Class<?> raw(Type t) { return t instanceof Class<?> c ? c : t instanceof ParameterizedType p ? raw(p.getRawType()) : Object.class; }
143
+ // An argument from its spec: an input, None, an inert value (an interface's proxy that does nothing), a record.
144
+ static Object build(Object spec0, Class<?> c, Type g) throws Exception {
145
+ Map<?, ?> spec = (Map<?, ?>) spec0;
146
+ if (spec.containsKey("in")) return conv(spec.get("in"), c, g);
147
+ if (spec.containsKey("none")) return c == Optional.class ? Optional.empty() : c.isPrimitive() ? conv(0L, c, null) : null;
148
+ if (spec.containsKey("inert")) return inert(c);
149
+ if (spec.containsKey("store")) return store((String) spec.get("store"), c);
150
+ if (spec.containsKey("rec")) return record(c, (Map<?, ?>) spec.get("f"));
151
+ throw new IllegalArgumentException("an argument spec " + json(spec));
152
+ }
153
+ static Object inert(Class<?> c) {
154
+ if (c.isPrimitive()) return conv(0L, c, null);
155
+ if (!c.isInterface()) return null;
156
+ return Proxy.newProxyInstance(loader, new Class<?>[]{c}, (o, m, a) -> m.getName().equals("toString") ? "inert" : m.getName().equals("hashCode") ? 0 : m.getName().equals("equals") ? o == a[0] : m.getReturnType().isPrimitive() && m.getReturnType() != void.class ? conv(0L, m.getReturnType(), null) : null);
157
+ }
158
+ // A record of the code's own: a Java record by its canonical constructor, another class made without one and its
159
+ // fields set (every other field its zero).
160
+ static Object record(Class<?> c, Map<?, ?> f) throws Exception {
161
+ if (c.isRecord()) {
162
+ RecordComponent[] rc = c.getRecordComponents();
163
+ Object[] args = new Object[rc.length];
164
+ Class<?>[] ts = new Class<?>[rc.length];
165
+ for (int i = 0; i < rc.length; i++) { ts[i] = rc[i].getType(); Object s = f.get(rc[i].getName()); args[i] = s == null ? conv(null, ts[i], null) : s instanceof Map<?, ?> ? build(s, ts[i], rc[i].getGenericType()) : conv(s, ts[i], rc[i].getGenericType()); }
166
+ Constructor<?> k = c.getDeclaredConstructor(ts); k.setAccessible(true); return k.newInstance(args);
167
+ }
168
+ Field uf = Class.forName("sun.misc.Unsafe").getDeclaredField("theUnsafe"); uf.setAccessible(true);
169
+ Object u = uf.get(null);
170
+ Object o = u.getClass().getMethod("allocateInstance", Class.class).invoke(u, c);
171
+ for (Map.Entry<?, ?> e : f.entrySet()) { Field fd = field(c, (String) e.getKey()); fd.set(o, e.getValue() instanceof Map<?, ?> ? build(e.getValue(), fd.getType(), fd.getGenericType()) : conv(e.getValue(), fd.getType(), fd.getGenericType())); }
172
+ return o;
173
+ }
174
+ // A value as the check reads it: a number as its decimal text, a string or an enum's name, a boolean, null.
175
+ static String enc(Object v) {
176
+ if (v instanceof Double || v instanceof Float) { double d = ((Number) v).doubleValue(); return d == Math.floor(d) && Math.abs(d) < 9.2e18 ? Long.toString((long) d) : "f:" + d; }
177
+ if (v instanceof Number n) return Long.toString(n.longValue());
178
+ if (v instanceof Character ch) return Integer.toString(ch);
179
+ return "?:" + (v == null ? "null" : v.getClass().getSimpleName());
180
+ }
181
+ static Object tag(Object v) {
182
+ if (v instanceof Optional<?> o) return o.isPresent() ? tag(o.get()) : null;
183
+ if (v == null || v instanceof Boolean) return v;
184
+ if (v instanceof Number || v instanceof Character) return m("n", enc(v));
185
+ if (v instanceof String s) return m("s", s);
186
+ if (v instanceof Enum<?> e) return m("s", e.name());
187
+ return m("o", v.getClass().getSimpleName());
188
+ }
189
+
190
+ // --- the state ------------------------------------------------------------------------------------------------------
191
+ // A leaf's holder (the object whose field it is) and its field.
192
+ static Object holder(Object o, List<?> path) throws Exception { for (int i = 0; i < path.size() - 1; i++) o = field(o.getClass(), (String) path.get(i)).get(o); return o; }
193
+ // What a field holds: an atomic's value, an Optional's (null where empty).
194
+ static Object read(Object h, String f) throws Exception {
195
+ Object v = field(h.getClass(), f).get(h);
196
+ if (v instanceof AtomicInteger a) return a.get();
197
+ if (v instanceof AtomicLong a) return a.get();
198
+ if (v instanceof AtomicBoolean a) return a.get();
199
+ if (v instanceof AtomicReference<?> a) return a.get();
200
+ if (v instanceof Optional<?> o) return o.orElse(null);
201
+ return v;
202
+ }
203
+ @SuppressWarnings("unchecked") static void write(Object h, String f, Object v, Map<?, ?> leaf) throws Exception {
204
+ Field fd = field(h.getClass(), f);
205
+ Object cur = fd.get(h);
206
+ Type g = fd.getGenericType();
207
+ if (cur instanceof AtomicInteger a) { a.set((int) fit(v, Integer.MIN_VALUE, Integer.MAX_VALUE)); return; }
208
+ if (cur instanceof AtomicLong a) { a.set(num(v).longValue()); return; }
209
+ if (cur instanceof AtomicBoolean a) { a.set((Boolean) v); return; }
210
+ if (cur instanceof AtomicReference<?> a) { ((AtomicReference<Object>) a).set(v == null ? null : conv(v, g instanceof ParameterizedType pt ? raw(pt.getActualTypeArguments()[0]) : Object.class, null)); return; }
211
+ fd.set(h, conv(v, fd.getType(), g));
212
+ }
213
+ static Map<String, Object> stateOf(Object obj) throws Exception {
214
+ Map<String, Object> s = new LinkedHashMap<>();
215
+ for (Object l0 : (List<?>) S.get("leaves")) {
216
+ Map<?, ?> l = (Map<?, ?>) l0;
217
+ String k = (String) l.get("kind");
218
+ if (k.equals("frame")) continue;
219
+ if (l.get("store") != null) {
220
+ Store st = ST.get(l.get("store"));
221
+ Map<String, Object> cells = new LinkedHashMap<>();
222
+ if (st != null) for (Map.Entry<String, Object> e : st.data.entrySet()) { String h = tplMatch((String) l.get("tpl"), e.getKey()); if (h != null) cells.put(h, m("value", e.getValue() instanceof Boolean ? e.getValue() : enc(e.getValue()))); }
223
+ s.put((String) l.get("name"), cells);
224
+ continue;
225
+ }
226
+ List<?> path = (List<?>) l.get("path");
227
+ Object h = holder(obj, path), v = read(h, (String) path.get(path.size() - 1));
228
+ switch (k) {
229
+ case "none" -> s.put((String) l.get("name"), v == null);
230
+ case "num" -> s.put((String) l.get("name"), v == null ? "0" : enc(v));
231
+ case "str", "choice" -> s.put((String) l.get("name"), v == null ? "" : v instanceof Enum<?> e ? e.name() : v.toString());
232
+ case "bool" -> s.put((String) l.get("name"), v == null ? false : v);
233
+ case "map" -> {
234
+ Map<String, Object> cells = new LinkedHashMap<>();
235
+ List<?> fs = (List<?>) l.get("fields");
236
+ for (Map.Entry<?, ?> e : ((Map<?, ?>) v).entrySet()) {
237
+ Object x = e.getValue();
238
+ Map<String, Object> c = new LinkedHashMap<>();
239
+ if (fs == null) c.put("value", x instanceof Boolean ? x : enc(x));
240
+ else for (Object f0 : fs) { String f = (String) ((Map<?, ?>) f0).get("name"); Object y = read(x, f); c.put(f, y instanceof Boolean ? y : y instanceof Enum<?> en ? en.name() : enc(y)); }
241
+ cells.put(e.getKey() instanceof Enum<?> en ? en.name() : String.valueOf(e.getKey()), c);
242
+ }
243
+ s.put((String) l.get("name"), cells);
244
+ }
245
+ default -> { }
246
+ }
247
+ }
248
+ // (a store's key no template names: the code keeps state the rule has no place for)
249
+ for (Map.Entry<String, Store> e : ST.entrySet()) for (String key : e.getValue().data.keySet()) {
250
+ boolean named = false;
251
+ for (Object l0 : (List<?>) S.get("leaves")) { Map<?, ?> l = (Map<?, ?>) l0; if (e.getKey().equals(l.get("store")) && tplMatch((String) l.get("tpl"), key) != null) named = true; }
252
+ if (!named) throw new IllegalStateException("the store " + e.getKey() + " holds " + key + ", a key no template of the rule names");
253
+ }
254
+ return s;
255
+ }
256
+ @SuppressWarnings("unchecked") static void setState(Object obj, Map<?, ?> st) throws Exception {
257
+ List<?> leaves = (List<?>) S.get("leaves");
258
+ for (Object l0 : leaves) {
259
+ Map<?, ?> l = (Map<?, ?>) l0;
260
+ String k = (String) l.get("kind");
261
+ if (k.equals("frame") || k.equals("none")) continue;
262
+ if (l.get("store") != null) {
263
+ Store sto = ST.computeIfAbsent((String) l.get("store"), x -> new Store());
264
+ String tpl = (String) l.get("tpl");
265
+ sto.data.keySet().removeIf(key -> tplMatch(tpl, key) != null);
266
+ for (Map.Entry<?, ?> e : ((Map<?, ?>) st.get(l.get("name"))).entrySet()) { Object v0 = ((Map<?, ?>) e.getValue()).get("value"); Object v1 = v0 instanceof String vs ? (Object) Long.parseLong(vs) : v0; if (sto.get != null) conv(v1, sto.get.getReturnType(), sto.get.getGenericReturnType()); sto.data.put(tpl.contains("{}") ? tpl.replace("{}", (String) e.getKey()) : tpl, v1); }
267
+ continue;
268
+ }
269
+ List<?> path = (List<?>) l.get("path");
270
+ Object h = holder(obj, path), v = st.get(l.get("name"));
271
+ String f = (String) path.get(path.size() - 1);
272
+ if (k.equals("map")) {
273
+ Field fd = field(h.getClass(), f);
274
+ Map<Object, Object> mp = (Map<Object, Object>) fd.get(h);
275
+ Type[] ka = fd.getGenericType() instanceof ParameterizedType pt ? pt.getActualTypeArguments() : new Type[]{Object.class, Object.class};
276
+ mp.clear();
277
+ List<?> fs = (List<?>) l.get("fields");
278
+ for (Map.Entry<?, ?> e : ((Map<?, ?>) v).entrySet()) {
279
+ Object key = conv("num".equals(l.get("key")) ? (Object) Long.parseLong((String) e.getKey()) : e.getKey(), raw(ka[0]), null);
280
+ Map<?, ?> c = (Map<?, ?>) e.getValue();
281
+ mp.put(key, fs == null ? conv(c.get("value"), raw(ka[1]), null) : record(raw(ka[1]), c));
282
+ }
283
+ continue;
284
+ }
285
+ write(h, f, v, l);
286
+ }
287
+ for (Object l0 : leaves) {
288
+ Map<?, ?> l = (Map<?, ?>) l0;
289
+ if (!"none".equals(l.get("kind")) || !Boolean.TRUE.equals(st.get(l.get("name")))) continue;
290
+ List<?> path = (List<?>) l.get("path");
291
+ Object h = holder(obj, path);
292
+ String f = (String) path.get(path.size() - 1);
293
+ Field fd = field(h.getClass(), f);
294
+ if (fd.get(h) instanceof AtomicReference<?> a) ((AtomicReference<Object>) a).set(null);
295
+ else fd.set(h, fd.getType() == Optional.class ? Optional.empty() : null);
296
+ }
297
+ }
298
+ // The fields the rule leaves out, as they are (each must be as it was after a call).
299
+ static Map<String, String> frames(Object obj) {
300
+ Map<String, String> out = new LinkedHashMap<>();
301
+ for (Object l0 : (List<?>) S.get("leaves")) {
302
+ Map<?, ?> l = (Map<?, ?>) l0;
303
+ if (!"frame".equals(l.get("kind")) || l.get("cls") != null) continue;
304
+ List<?> path = (List<?>) l.get("path");
305
+ try {
306
+ Object h = holder(obj, path), v = field(h.getClass(), (String) path.get(path.size() - 1)).get(h);
307
+ String txt = v == null ? "null" : v.getClass().isArray() ? Arrays.deepToString(new Object[]{v}) : v instanceof Collection<?> || v instanceof Map<?, ?> || v instanceof Number || v instanceof String || v instanceof Boolean ? v.toString() : v.getClass().getName();
308
+ out.put(String.join(".", path.stream().map(String::valueOf).toList()), System.identityHashCode(v) + ":" + txt);
309
+ } catch (Exception e) { /* unread */ }
310
+ }
311
+ return out;
312
+ }
313
+
314
+ // --- requests -------------------------------------------------------------------------------------------------------
315
+ static long make(List<?> args) throws Exception {
316
+ ST = new HashMap<>();
317
+ Map<?, ?> ctor = (Map<?, ?>) S.get("jctor");
318
+ Class<?>[] ts = types((List<?>) ctor.get("params"));
319
+ Constructor<?> k = cls.getDeclaredConstructor(ts);
320
+ k.setAccessible(true);
321
+ Type[] gs = k.getGenericParameterTypes();
322
+ Object[] a = new Object[ts.length];
323
+ for (int i = 0; i < ts.length; i++) a[i] = i < args.size() ? build(((List<?>) args.get(i)).get(1), ts[i], i < gs.length ? gs[i] : ts[i]) : inert(ts[i]);
324
+ Object o = k.newInstance(a);
325
+ // (a field the framework injects: set to the store the check gives, after the constructor)
326
+ for (Object f : list(S.get("inject"))) { Field fd = field(cls, (String) f); fd.set(o, store((String) f, fd.getType())); }
327
+ long id = OBJS.size() + 1;
328
+ OBJS.put(id, o);
329
+ STORES.put(id, ST);
330
+ return id;
331
+ }
332
+ static Object call(Map<?, ?> req) throws Exception {
333
+ long id = ((Number) req.get("id")).longValue();
334
+ Object obj = OBJS.get(id);
335
+ ST = STORES.get(id);
336
+ gdp.Rt.NOW = now(req);
337
+ CUR = req.get("answers") == null ? Map.of() : (Map<?, ?>) req.get("answers");
338
+ String key = (String) req.get("m");
339
+ Map<?, ?> sig = (Map<?, ?>) ((Map<?, ?>) S.get("methods")).get(key);
340
+ Method m = method(sig);
341
+ Class<?>[] ts = m.getParameterTypes();
342
+ Type[] gs = m.getGenericParameterTypes();
343
+ List<?> specs = (List<?>) req.get("a");
344
+ Object[] a = new Object[ts.length];
345
+ try { for (int i = 0; i < ts.length; i++) a[i] = i < specs.size() ? build(((List<?>) specs.get(i)).get(1), ts[i], gs[i]) : inert(ts[i]); }
346
+ catch (Exception e) { return m("bad", err(e)); }
347
+ Map<String, String> before = frames(obj);
348
+ Map<String, Object> snap = Boolean.TRUE.equals(S.get("discard")) ? stateOf(obj) : null;
349
+ Object out = null;
350
+ String err = null;
351
+ try { out = m.invoke(Modifier.isStatic(m.getModifiers()) ? null : obj, a); } catch (InvocationTargetException e) { err = err(e); }
352
+ if (err != null && snap != null) setState(obj, snap);
353
+ // A method returning the object's next value (an immutable class's step): the object is that value.
354
+ if (err == null && ((List<?>) S.get("reducers")).contains(key) && cls.isInstance(out)) { OBJS.put(id, out); obj = out; out = null; }
355
+ Map<String, String> after = frames(obj);
356
+ for (Map.Entry<String, String> e : before.entrySet()) if (!e.getValue().equals(after.get(e.getKey())) && !((List<?>) S.get("reducers")).contains(key)) return m("frame", e.getKey());
357
+ Map<String, Object> res = m("out", tag(out));
358
+ if (err != null) res.put("err", err);
359
+ return res;
360
+ }
361
+ }