ferrovue 0.12.0 → 0.13.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 (69) hide show
  1. package/README.md +8 -8
  2. package/dist/attrs.d.ts +4 -0
  3. package/dist/attrs.js +8 -0
  4. package/dist/blocks.js +6 -17
  5. package/dist/calls.js +150 -330
  6. package/dist/children.js +64 -19
  7. package/dist/classes.js +2 -5
  8. package/dist/client.d.ts +1 -1
  9. package/dist/client.js +2 -2
  10. package/dist/component.js +76 -23
  11. package/dist/conditional.d.ts +3 -0
  12. package/dist/conditional.js +201 -0
  13. package/dist/conformance.js +4 -10
  14. package/dist/constants.js +93 -15
  15. package/dist/dynamic.js +9 -4
  16. package/dist/enums.d.ts +20 -0
  17. package/dist/enums.js +126 -13
  18. package/dist/errors.d.ts +16 -16
  19. package/dist/errors.js +16 -16
  20. package/dist/expr.js +241 -772
  21. package/dist/fallthrough.d.ts +8 -0
  22. package/dist/fallthrough.js +39 -31
  23. package/dist/functions.js +3 -1
  24. package/dist/globals.d.ts +7 -0
  25. package/dist/globals.js +223 -0
  26. package/dist/lists.d.ts +7 -2
  27. package/dist/lists.js +100 -9
  28. package/dist/logical.d.ts +3 -0
  29. package/dist/logical.js +215 -0
  30. package/dist/loops.js +7 -3
  31. package/dist/members.d.ts +5 -0
  32. package/dist/members.js +343 -0
  33. package/dist/model.d.ts +43 -1
  34. package/dist/model.js +50 -2
  35. package/dist/narrowing.d.ts +14 -4
  36. package/dist/narrowing.js +11 -8
  37. package/dist/numbers.d.ts +2 -0
  38. package/dist/numbers.js +38 -1
  39. package/dist/objects.d.ts +2 -0
  40. package/dist/objects.js +22 -0
  41. package/dist/operators.d.ts +5 -0
  42. package/dist/operators.js +186 -0
  43. package/dist/plugins/head.js +14 -24
  44. package/dist/plugins/loaders.d.ts +5 -0
  45. package/dist/plugins/loaders.js +115 -8
  46. package/dist/plugins/router-link.js +5 -12
  47. package/dist/plugins/scoped.js +7 -13
  48. package/dist/plugins/stores.js +2 -2
  49. package/dist/problems.d.ts +8 -0
  50. package/dist/problems.js +25 -0
  51. package/dist/revive.js +6 -2
  52. package/dist/rust.d.ts +1 -1
  53. package/dist/rust.js +41 -14
  54. package/dist/script.d.ts +0 -1
  55. package/dist/script.js +335 -238
  56. package/dist/slots.d.ts +2 -0
  57. package/dist/slots.js +11 -0
  58. package/dist/ssr.js +3 -2
  59. package/dist/statements.js +11 -12
  60. package/dist/strings.d.ts +25 -0
  61. package/dist/strings.js +240 -1
  62. package/dist/styles.js +12 -15
  63. package/dist/template.js +97 -11
  64. package/dist/testing-browser.js +27 -48
  65. package/dist/typescript.d.ts +7 -0
  66. package/dist/typescript.js +162 -4
  67. package/dist/vite.d.ts +0 -2
  68. package/dist/vite.js +9 -3
  69. package/package.json +1 -1
package/dist/calls.js CHANGED
@@ -1,281 +1,146 @@
1
- import { asciiTy, BOOL, fail, FLOAT, INT, isAscii, refusalAt, sameTy, STR, strTy } from "./model.js";
1
+ import { BOOL, fail } from "./model.js";
2
2
  import { ctx } from "./context.js";
3
3
  import { claim } from "./plugin.js";
4
- import { atom, bare, binary, condition, operand, receiver, strArg, UNARY } from "./parens.js";
5
- import { computedListMethod, isBoolean, items, listMethod, objectCall, presentLiteral, spreadExtreme } from "./lists.js";
6
- import { formatted, lonely, loneOf, meet, stringMethod, stringOf, stringsEqual, writesAscii } from "./strings.js";
7
- import { asF64, asWritten, intFromF64, isCast, isNumber, numberVal, power } from "./numbers.js";
4
+ import { atom, bare } from "./parens.js";
5
+ import { isBoolean, listCall, listMethod, presentLiteral } from "./lists.js";
6
+ import { meet, noArgStringMethod, regexTest, stringMethod, stringsEqual } from "./strings.js";
7
+ import { numberMethod } from "./numbers.js";
8
8
  import { boolOf, truthy } from "./narrowing.js";
9
- import { coerce, describeTy, expr, refuseClientOnly, refuseUnavailable } from "./expr.js";
9
+ import { coerce, expr, refuseClientOnly, refuseUnavailable } from "./expr.js";
10
10
  import { callFunction } from "./functions.js";
11
11
  import { constOf, mapCall } from "./constants.js";
12
12
  import { refuseInPlace } from "./objects.js";
13
- const DIGITS = new Map([
14
- ["toFixed", ["FV0705", 0, "number of digits"]],
15
- ["toPrecision", ["FV0716", 1, "precision"]],
16
- ["toExponential", ["FV0717", 0, "number of digits"]],
17
- ]);
13
+ import { globalCall, mathCall, staticCall } from "./globals.js";
18
14
  export function call(s, n) {
19
- const comp = s.comp;
15
+ const callee = n.callee;
16
+ if (callee.type === "Identifier")
17
+ return namedCall(s, n);
18
+ if (callee.type === "MemberExpression" && !callee.computed)
19
+ return memberCall(s, n);
20
+ return fail(s.comp, "FV0603", "this call is not supported", n);
21
+ }
22
+ function namedCall(s, n) {
20
23
  const callee = n.callee;
21
24
  const args = n.arguments;
22
- if (callee.type === "Identifier") {
23
- switch (callee.name) {
24
- case "_ssrLooseEqual": {
25
- const a = expr(s, args[0]);
26
- const b = expr(s, args[1]);
27
- if (a.ty.k === "str" && b.ty.k === "str") {
28
- meet(comp, a, b, "`v-model`", n, "equal");
29
- return boolOf(stringsEqual(a, b));
30
- }
31
- return fail(comp, "FV0413", "`v-model` comparison between these types", n);
32
- }
33
- case "_ssrIncludeBooleanAttr": {
34
- const a = expr(s, args[0]);
35
- if (a.konst !== undefined)
36
- return a;
37
- if (a.ty.k === "str")
38
- return { code: "true", ty: BOOL, konst: true };
39
- if (a.ty.k === "opt" && a.ty.of.k === "str")
40
- return { code: `${atom(a.code)}.is_some()`, ty: BOOL };
41
- return boolOf(truthy(a));
42
- }
43
- case "_ssrLooseContain":
44
- return fail(comp, "FV0414", "`v-model` over an array", n);
45
- }
46
- const own = claim((p) => p.call?.(s, n));
47
- if (own)
48
- return own;
49
- const helper = s.helpers.get(callee.name);
50
- if (helper)
51
- return helperCall(s, helper, args, n);
52
- const fn = s.locals.has(callee.name) ? undefined : s.functions.get(callee.name);
53
- if (fn)
54
- return callFunction(s, callee.fvName ?? callee.name, fn, args, n);
55
- if (isBoolean(s, callee) && args.length === 1)
56
- return boolOf(truthy(expr(s, args[0])));
57
- const global = !s.locals.has(callee.name) && !s.setup.has(callee.name);
58
- if (callee.name === "String" && global) {
59
- if (args.length !== 1)
60
- return fail(comp, "FV0723", "`String()` takes one value", n);
61
- const a = expr(s, args[0]);
62
- if (a.ty.k === "str")
63
- return a;
64
- if (isNumber(a.ty))
65
- return { code: `&*fv::core::Js(${bare(asWritten(a).code)}).to_string()`, ty: asciiTy() };
66
- if (a.ty.k === "bool")
67
- return { code: `if ${condition(a.code)} { "true" } else { "false" }`, ty: asciiTy() };
68
- return formatted(comp, [stringOf(comp, a, args[0], "`String()`", "FV0723")], n, "`String()`");
69
- }
70
- if ((callee.name === "encodeURIComponent" || callee.name === "encodeURI") && global) {
71
- const name = `\`${callee.name}()\``;
72
- if (args.length !== 1)
73
- return fail(comp, "FV0723", `${name} takes one value`, n);
74
- const a = expr(s, args[0]);
75
- if (a.lone)
76
- fail(comp, "FV0706", `${lonely(name)}; it throws a \`URIError\` on a half`, n);
77
- const text = a.ty.k === "str" ? a : formatted(comp, [stringOf(comp, a, args[0], name, "FV0723")], n, name);
78
- return { code: `&*fv::core::js_${callee.name === "encodeURI" ? "encode_uri" : "encode_uri_component"}(${strArg(text.code)})`, ty: STR };
79
- }
80
- if (callee.name === "Number" && args.length === 1) {
81
- const a = expr(s, args[0]);
82
- if (a.ty.k === "str")
83
- return { code: `fv::strings::js_number(${strArg(a.code)})`, ty: FLOAT };
84
- if (isNumber(a.ty))
85
- return a;
86
- if (a.ty.k === "bool")
87
- return { code: `i64::from(${bare(a.code)})`, ty: INT };
88
- return fail(comp, "FV0701", "`Number()` takes a string, a number or a boolean that is present", n);
89
- }
90
- if ((callee.name === "parseInt" && (args.length === 1 || args.length === 2)) || (callee.name === "parseFloat" && args.length === 1)) {
91
- const a = expr(s, args[0]);
92
- if (a.ty.k !== "str")
93
- fail(comp, "FV0702", `\`${callee.name}()\` takes a string`, args[0]);
94
- if (callee.name === "parseFloat")
95
- return { code: `fv::strings::js_parse_float(${strArg(a.code)})`, ty: FLOAT };
96
- const radix = args[1];
97
- if (radix && !(radix.type === "NumericLiteral" && (radix.value === 10 || radix.value === 16))) {
98
- fail(comp, "FV0703", "`parseInt()` takes a radix of 10 or 16, written as a literal", radix);
99
- }
100
- return { code: `fv::strings::js_parse_int(${strArg(a.code)}, ${radix ? radix.value : 0})`, ty: FLOAT };
101
- }
102
- if ((callee.name === "isNaN" || callee.name === "isFinite") && global)
103
- return numberTest(s, callee.name, args, n, true);
104
- if (global && BUILTINS.has(callee.name))
105
- return builtin(s, `${callee.name}()`, n);
106
- return refuseUnavailable(s, callee.name, n, true);
25
+ const rendered = renderHelperCall(s, callee.name, args, n);
26
+ if (rendered)
27
+ return rendered;
28
+ const own = claim((p) => p.call?.(s, n));
29
+ if (own)
30
+ return own;
31
+ const mine = ownCall(s, callee, args, n);
32
+ if (mine)
33
+ return mine;
34
+ if (isBoolean(s, callee) && args.length === 1)
35
+ return boolOf(truthy(expr(s, args[0])));
36
+ return globalNamedCall(s, callee.name, args, n);
37
+ }
38
+ function globalNamedCall(s, name, args, n) {
39
+ const global = !s.locals.has(name) && !s.setup.has(name);
40
+ const known = globalCall(s, name, args, n, global);
41
+ if (known)
42
+ return known;
43
+ if (global && BUILTINS.has(name))
44
+ return builtin(s, `${name}()`, n);
45
+ return refuseUnavailable(s, name, n, true);
46
+ }
47
+ function renderHelperCall(s, name, args, n) {
48
+ switch (name) {
49
+ case "_ssrLooseEqual":
50
+ return looseEqual(s, args, n);
51
+ case "_ssrIncludeBooleanAttr":
52
+ return includeBooleanAttr(s, args);
53
+ case "_ssrLooseContain":
54
+ return fail(s.comp, "FV0414", "`v-model` over an array", n);
55
+ }
56
+ return null;
57
+ }
58
+ function looseEqual(s, args, n) {
59
+ const a = expr(s, args[0]);
60
+ const b = expr(s, args[1]);
61
+ if (a.ty.k === "str" && b.ty.k === "str") {
62
+ meet(s.comp, a, b, "`v-model`", n, "equal");
63
+ return boolOf(stringsEqual(a, b));
107
64
  }
108
- if (callee.type === "MemberExpression" && !callee.computed) {
109
- const method = callee.property.name;
110
- if (callee.object.type === "Identifier" && callee.object.name === "Array" && method === "isArray") {
111
- const a = expr(s, args[0]);
112
- const own = claim((p) => p.values?.isArray?.(a));
113
- if (own)
114
- return own;
115
- const is = a.ty.k === "list";
116
- return { code: String(is), ty: BOOL, konst: is };
117
- }
118
- if (callee.object.type === "Identifier" && callee.object.name === "Object")
119
- return objectCall(s, method, args, n);
120
- if (callee.object.type === "Identifier" && callee.object.name === "Number" && NUMBER_TESTS.has(method))
121
- return numberTest(s, method, args, n, false);
122
- if (callee.object.type === "Identifier" && callee.object.name === "JSON" && method === "stringify" && args.length === 1) {
123
- const v = expr(s, args[0]);
124
- if (v.ty.k === "bool")
125
- return { code: `if ${condition(v.code)} { "true" } else { "false" }`, ty: STR };
126
- return { code: `&*${json(s, v, args[0])}`, ty: STR };
127
- }
128
- if (callee.object.type === "Identifier" && callee.object.name === "JSON" && method === "stringify") {
129
- return fail(comp, "FV0633", "`JSON.stringify()` with a replacer or an indentation, which the server does not write: give it one value, `JSON.stringify(x)`", n);
130
- }
131
- const own = claim((p) => p.call?.(s, n));
132
- if (own)
133
- return own;
134
- if (callee.object.type === "Identifier" && (callee.object.name === "$setup" || callee.object.name === "_ctx")) {
135
- const helper = s.helpers.get(method);
136
- if (helper)
137
- return helperCall(s, helper, args, n);
138
- const fn = s.functions.get(method);
139
- if (fn)
140
- return callFunction(s, method, fn, args, n);
141
- if (s.clientOnly.has(method))
142
- return refuseClientOnly(s, method, n, true);
143
- if (!s.setup.has(method) && !(callee.object.name === "_ctx" && comp.props.fields.some((f) => f.js === method)))
144
- return refuseUnavailable(s, method, n, true);
145
- }
146
- if (callee.object.type === "Identifier" && callee.object.name === "Math") {
147
- if ((method === "max" || method === "min") && args.some((a) => a?.type === "SpreadElement"))
148
- return spreadExtreme(s, method, args);
149
- if (method === "pow" && args.length === 2)
150
- return power(s, args[0], args[1], n, "`Math.pow()`");
151
- if (ROUNDED_BY_EACH.has(method)) {
152
- return fail(comp, "FV0719", `\`Math.${method}()\` is computed by each JavaScript engine's own routine, which need not round its result as another engine or Rust does, so the page could show digits the browser's render does not: compute it in Rust and pass it as a prop`, n);
153
- }
154
- const vals = args.map((a) => expr(s, a));
155
- if (vals.length && vals.every((v) => isNumber(v.ty))) {
156
- const ints = vals.every((v) => v.ty.k === "int");
157
- if ((method === "max" || method === "min") && vals.length >= 2) {
158
- if (ints && !vals.some(isCast))
159
- return { code: vals.slice(1).reduce((acc, v) => `${atom(acc)}.${method}(${bare(v.code)})`, vals[0].code), ty: INT };
160
- const extreme = vals.slice(1).reduce((acc, v) => `fv::core::js_${method}(${bare(acc)}, ${bare(asF64(v))})`, asF64(vals[0]));
161
- if (ints)
162
- return intFromF64(extreme, vals.find(isCast)?.at ?? refusalAt(comp, n));
163
- return { code: extreme, ty: FLOAT };
164
- }
165
- if (vals.length === 1) {
166
- const x = vals[0];
167
- if (method === "abs")
168
- return ints ? intFromF64(`${receiver(asF64(x))}.abs()`, refusalAt(comp, n)) : { code: `${receiver(x.code)}.abs()`, ty: FLOAT };
169
- if (method === "round")
170
- return { code: `fv::core::js_round(${bare(asF64(x))})`, ty: FLOAT };
171
- if (method === "floor" || method === "ceil" || method === "trunc")
172
- return { code: `${receiver(asF64(x))}.${method}()`, ty: FLOAT };
173
- if (method === "sqrt")
174
- return x.num !== undefined ? numberVal(Math.sqrt(x.num), false) : { code: `${receiver(asF64(x))}.sqrt()`, ty: FLOAT };
175
- if (method === "sign") {
176
- if (x.num !== undefined)
177
- return numberVal(Math.sign(x.num), ints);
178
- const sign = `fv::core::js_sign(${bare(asF64(x))})`;
179
- return ints ? intFromF64(sign, null) : { code: sign, ty: FLOAT };
180
- }
181
- }
182
- }
183
- return fail(comp, "FV0704", `\`Math.${method}()\` is supported on numbers as \`max\`, \`min\`, \`abs\`, \`round\`, \`floor\`, \`ceil\`, \`trunc\`, \`sqrt\`, \`sign\` and \`pow\``, n);
184
- }
185
- if (callee.object.type === "Identifier" && BUILTINS.has(callee.object.name) && !s.locals.has(callee.object.name) && !s.setup.has(callee.object.name)) {
186
- return builtin(s, `${callee.object.name}.${method}()`, n);
187
- }
188
- if ((method === "sort" || method === "reverse") && callee.object.type === "ArrayExpression") {
189
- const copy = expr(s, callee.object);
190
- if (copy.ty.k === "list")
191
- return listMethod(s, copy, method === "sort" ? "toSorted" : "toReversed", args, n);
192
- }
193
- const map = callee.object.type === "Identifier" || callee.object.type === "MemberExpression" ? constOf(s, callee.object) : null;
194
- if (map?.sym.k === "const" && map.sym.c.k === "map")
195
- return mapCall(s, map, method, args, n);
196
- const literal = method === "filter" && args.length === 1 && isBoolean(s, args[0]) && callee.object.type === "ArrayExpression";
197
- const target = (literal ? presentLiteral(s, callee.object) : null) ?? expr(s, callee.object);
198
- const typed = claim((p) => p.method?.(s, target, method, args, n));
199
- if (typed)
200
- return typed;
201
- if (target.ty.k === "str") {
202
- const lone = loneOf(target);
203
- const same = strTy(isAscii(target.ty));
204
- switch (args.length === 0 ? method : "") {
205
- case "trim":
206
- return { code: `fv::strings::js_trim(${strArg(target.code)})`, ty: same, ...lone };
207
- case "trimStart":
208
- return { code: `fv::strings::js_trim_start(${strArg(target.code)})`, ty: same, ...lone };
209
- case "trimEnd":
210
- case "trimRight":
211
- return { code: `fv::strings::js_trim_end(${strArg(target.code)})`, ty: same, ...lone };
212
- case "trimLeft":
213
- return { code: `fv::strings::js_trim_start(${strArg(target.code)})`, ty: same, ...lone };
214
- case "toUpperCase":
215
- return { code: `&*${receiver(target.code)}.to_uppercase()`, ty: same, ...lone };
216
- case "toLowerCase":
217
- return { code: `&*${receiver(target.code)}.to_lowercase()`, ty: same, ...lone };
218
- case "toString":
219
- return target;
220
- }
221
- const m = stringMethod(s, target, method, args, n);
222
- if (m)
223
- return m;
224
- }
225
- const digits = isNumber(target.ty) && args.length <= 1 ? DIGITS.get(method) : undefined;
226
- if (digits) {
227
- const [code, low, what] = digits;
228
- const a = args[0];
229
- const given = a === undefined || (a.type === "Identifier" && a.name === "undefined") ? undefined : a.type === "NumericLiteral" ? Math.trunc(a.value) : NaN;
230
- if (given !== undefined && !(given >= low && given <= 100))
231
- fail(comp, code, `\`.${method}()\` takes a literal ${what}, from ${low} to 100`, n);
232
- const x = bare(asF64(target));
233
- if (method === "toFixed")
234
- return { code: `&*fv::core::js_to_fixed(${x}, ${given ?? 0})`, ty: asciiTy() };
235
- if (method === "toExponential")
236
- return { code: `&*fv::core::js_to_exponential(${x}, ${given === undefined ? "None" : `Some(${given})`})`, ty: asciiTy() };
237
- if (given === undefined)
238
- return { code: `&*fv::core::Js(${bare(asWritten(target).code)}).to_string()`, ty: asciiTy() };
239
- return { code: `&*fv::core::js_to_precision(${x}, ${given})`, ty: asciiTy() };
240
- }
241
- if (isNumber(target.ty) && method === "toString" && args.length === 0) {
242
- return { code: `&*fv::core::Js(${bare(asWritten(target).code)}).to_string()`, ty: asciiTy() };
243
- }
244
- if (target.owned)
245
- refuseInPlace(comp, `\`.${method}()\``, n);
246
- if (target.ty.k === "list") {
247
- const inPlace = target.iter === undefined && !target.lone && (target.ty.of.k === "str" || target.ty.of.k === "int");
248
- const listed = listMethod(s, target, method, args, n) ?? (inPlace ? null : computedListMethod(s, target, method, args, n));
249
- if (listed)
250
- return listed;
251
- }
252
- if (target.ty.k === "list" && (target.ty.of.k === "str" || target.ty.of.k === "int")) {
253
- const of = target.ty.of;
254
- if (method === "includes" && args.length === 1) {
255
- const v = expr(s, args[0]);
256
- if (of.k === "int" && v.ty.k === "float")
257
- return { code: `${atom(target.code)}.iter().any(|v| ${binary("*v as f64", "==", v.code)})`, ty: BOOL };
258
- if (!sameTy(v.ty, of))
259
- fail(comp, "FV0801", "`.includes()` looks for a value of the list's own type", args[0]);
260
- if (of.k === "int")
261
- return { code: `${atom(target.code)}.contains(&${operand(v.code, UNARY)})`, ty: BOOL };
262
- const test = v.code === '""' ? "v.is_empty()" : binary("&**v", "==", v.code);
263
- return { code: `${atom(target.code)}.iter().any(|v| ${test})`, ty: BOOL };
264
- }
265
- if (method === "join" && args.length <= 1) {
266
- const sep = args.length ? expr(s, args[0]) : { code: '","', ty: STR };
267
- if (sep.ty.k !== "str")
268
- fail(comp, "FV0802", "`.join()` takes a string", args[0]);
269
- const each = of.k === "str" ? `${atom(target.code)}.iter().map(|v| &**v)` : `${atom(target.code)}.iter().map(|v| fv::core::Js(*v).to_string())`;
270
- return { code: `&*${each}.collect::<Vec<_>>().join(${strArg(sep.code)})`, ty: strTy(writesAscii(target.ty) && isAscii(sep.ty)), ...loneOf(sep) };
271
- }
272
- }
273
- return fail(comp, "FV0602", `\`.${method}()\` is not supported`, n);
65
+ return fail(s.comp, "FV0413", "`v-model` comparison between these types", n);
66
+ }
67
+ function includeBooleanAttr(s, args) {
68
+ const a = expr(s, args[0]);
69
+ if (a.konst !== undefined)
70
+ return a;
71
+ if (a.ty.k === "str")
72
+ return { code: "true", ty: BOOL, konst: true };
73
+ if (a.ty.k === "opt" && a.ty.of.k === "str")
74
+ return { code: `${atom(a.code)}.is_some()`, ty: BOOL };
75
+ return boolOf(truthy(a));
76
+ }
77
+ function memberCall(s, n) {
78
+ const object = n.callee.object;
79
+ const method = n.callee.property.name;
80
+ const args = n.arguments;
81
+ const named = object.type === "Identifier" ? namedObjectCall(s, object.name, method, args, n) : claim((p) => p.call?.(s, n));
82
+ return named ?? methodCall(s, object, method, args, n);
83
+ }
84
+ function namedObjectCall(s, name, method, args, n) {
85
+ const known = staticCall(s, name, method, args, n);
86
+ if (known)
87
+ return known;
88
+ const own = claim((p) => p.call?.(s, n));
89
+ if (own)
90
+ return own;
91
+ const mine = name === "$setup" || name === "_ctx" ? setupCall(s, name, method, args, n) : null;
92
+ if (mine)
93
+ return mine;
94
+ return globalObjectCall(s, name, method, args, n);
95
+ }
96
+ function globalObjectCall(s, name, method, args, n) {
97
+ if (name === "Math")
98
+ return mathCall(s, method, args, n);
99
+ if (BUILTINS.has(name) && !s.locals.has(name) && !s.setup.has(name))
100
+ return builtin(s, `${name}.${method}()`, n);
101
+ return null;
102
+ }
103
+ function methodCall(s, object, method, args, n) {
104
+ const fixed = literalReceiverCall(s, object, method, args, n);
105
+ if (fixed)
106
+ return fixed;
107
+ const literal = method === "filter" && args.length === 1 && isBoolean(s, args[0]) && object.type === "ArrayExpression";
108
+ const target = (literal ? presentLiteral(s, object) : null) ?? expr(s, object);
109
+ return valueMethod(s, target, method, args, n);
110
+ }
111
+ function literalReceiverCall(s, object, method, args, n) {
112
+ const copied = sortedCopy(s, object, method, args, n);
113
+ if (copied)
114
+ return copied;
115
+ if (object.type === "RegExpLiteral" && method === "test")
116
+ return regexTest(s, object, args, n);
117
+ const map = object.type === "Identifier" || object.type === "MemberExpression" ? constOf(s, object) : null;
118
+ if (map?.sym.k === "const" && map.sym.c.k === "map")
119
+ return mapCall(s, map, method, args, n);
120
+ return null;
121
+ }
122
+ function sortedCopy(s, object, method, args, n) {
123
+ if ((method !== "sort" && method !== "reverse") || object.type !== "ArrayExpression")
124
+ return null;
125
+ const copy = expr(s, object);
126
+ return copy.ty.k === "list" ? listMethod(s, copy, method === "sort" ? "toSorted" : "toReversed", args, n) : null;
127
+ }
128
+ function valueMethod(s, target, method, args, n) {
129
+ const typed = claim((p) => p.method?.(s, target, method, args, n));
130
+ if (typed)
131
+ return typed;
132
+ if (target.ty.k === "str") {
133
+ const m = (args.length === 0 ? noArgStringMethod(target, method) : null) ?? stringMethod(s, target, method, args, n);
134
+ if (m)
135
+ return m;
274
136
  }
275
- return fail(comp, "FV0603", "this call is not supported", n);
137
+ const numbered = numberMethod(s, target, method, args, n);
138
+ if (numbered)
139
+ return numbered;
140
+ if (target.owned)
141
+ refuseInPlace(s.comp, `\`.${method}()\``, n);
142
+ return listCall(s, target, method, args, n) ?? fail(s.comp, "FV0602", `\`.${method}()\` is not supported`, n);
276
143
  }
277
- const NUMBER_TESTS = new Set(["isNaN", "isFinite", "isInteger", "isSafeInteger"]);
278
- const ROUNDED_BY_EACH = new Set("acos acosh asin asinh atan atan2 atanh cbrt cos cosh exp expm1 hypot log log10 log1p log2 sin sinh tan tanh".split(" "));
279
144
  /** The globals of JavaScript a template may name, which the server translates only some of. */
280
145
  export const BUILTINS = new Set("Array BigInt Boolean JSON Map Math Number Object RegExp Set String Symbol console decodeURI decodeURIComponent encodeURI encodeURIComponent isFinite isNaN parseFloat parseInt".split(" "));
281
146
  const OF_BUILTINS = {
@@ -299,74 +164,29 @@ export function builtin(s, what, n) {
299
164
  const of = OF_BUILTINS[name];
300
165
  return fail(s.comp, "FV0633", `\`${what}\` is not supported: ${of ? `of \`${name}\`, the server translates ${of}` : `the server does not translate \`${name}\``}`, n);
301
166
  }
302
- function floatTest(method, x) {
303
- switch (method) {
304
- case "isNaN":
305
- return `${receiver(x)}.is_nan()`;
306
- case "isFinite":
307
- return `${receiver(x)}.is_finite()`;
308
- case "isInteger":
309
- return `${receiver(x)}.fract() == 0.0`;
310
- default:
311
- return /^[\w.]+$/.test(x) ? `${x}.fract() == 0.0 && ${x}.abs() <= 9_007_199_254_740_991.0` : `matches!(${bare(x)}, v if v.fract() == 0.0 && v.abs() <= 9_007_199_254_740_991.0)`;
312
- }
313
- }
314
- function intTest(method, x) {
315
- if (method === "isSafeInteger")
316
- return `(-9_007_199_254_740_991..=9_007_199_254_740_991).contains(&${operand(x, UNARY)})`;
317
- return String(method !== "isNaN");
318
- }
319
- function numberTest(s, method, args, n, global) {
320
- const name = global ? `\`${method}()\`` : `\`Number.${method}()\``;
321
- if (args.length !== 1)
322
- return fail(s.comp, "FV0718", `${name} takes one number`, n);
323
- const v = expr(s, args[0]);
324
- if (isNumber(v.ty)) {
325
- if (v.num !== undefined) {
326
- const konst = Number[method](v.num);
327
- return { code: String(konst), ty: BOOL, konst };
328
- }
329
- return boolOf(v.ty.k === "int" ? intTest(method, v.code) : floatTest(method, v.code));
330
- }
331
- if (!global && v.ty.k === "opt" && isNumber(v.ty.of)) {
332
- const each = v.ty.of.k === "int" ? intTest(method, "v") : floatTest(method, "v");
333
- if (each === "false")
334
- return { code: "false", ty: BOOL, konst: false };
335
- return boolOf(each === "true" ? `${atom(v.code)}.is_some()` : `${atom(v.code)}.is_some_and(|v| ${each})`);
336
- }
337
- if (global) {
338
- return fail(s.comp, "FV0718", `${name} converts its argument as \`Number()\` does, which the server does for a number only: write \`Number.${method}(Number(x))\` for a string, or pass a present number`, args[0]);
339
- }
340
- return fail(s.comp, "FV0718", `${name} of ${describeTy(v.ty)}, for which it is \`false\` whatever it holds, since it does not convert its argument: pass a number, present or optional`, args[0]);
341
- }
342
167
  export function isObjectCall(n, method) {
343
168
  return (n?.type === "CallExpression" && n.callee.type === "MemberExpression" && !n.callee.computed &&
344
169
  n.callee.object.type === "Identifier" && n.callee.object.name === "Object" && n.callee.property.name === method);
345
170
  }
346
- function json(s, v, n) {
347
- if (v.lone)
348
- fail(s.comp, "FV0706", lonely("`JSON.stringify()`"), n);
349
- const one = (code, ty) => {
350
- switch (ty.k) {
351
- case "str":
352
- return `fv::strings::js_json_string(${code === "v" ? "&v" : strArg(code)})`;
353
- case "int":
354
- return `fv::core::Js(${bare(code)}).to_string()`;
355
- case "float":
356
- return `fv::strings::js_json_number(${bare(code)})`;
357
- case "bool":
358
- return `(if ${condition(code)} { "true" } else { "false" }).to_owned()`;
359
- default:
360
- return claim((p) => p.values?.json?.({ code, ty })) ?? fail(s.comp, "FV0707", "`JSON.stringify()` of a string, a number, a boolean, a date or a list of those, present", n);
361
- }
362
- };
363
- if (v.ty.k === "list") {
364
- const each = one("v", v.ty.of);
365
- const f = /^([\w:]+)\(v\)$/.exec(each)?.[1];
366
- return `format!("[{}]", ${items(v)}.map(${f ?? `|v| ${each}`}).collect::<Vec<_>>().join(","))`;
367
- }
368
- const w = asWritten(v);
369
- return one(w.code, w.ty);
171
+ function ownCall(s, callee, args, n) {
172
+ const helper = s.helpers.get(callee.name);
173
+ if (helper)
174
+ return helperCall(s, helper, args, n);
175
+ const fn = s.locals.has(callee.name) ? undefined : s.functions.get(callee.name);
176
+ return fn ? callFunction(s, callee.fvName ?? callee.name, fn, args, n) : null;
177
+ }
178
+ function setupCall(s, of, method, args, n) {
179
+ const helper = s.helpers.get(method);
180
+ if (helper)
181
+ return helperCall(s, helper, args, n);
182
+ const fn = s.functions.get(method);
183
+ if (fn)
184
+ return callFunction(s, method, fn, args, n);
185
+ if (s.clientOnly.has(method))
186
+ return refuseClientOnly(s, method, n, true);
187
+ if (!s.setup.has(method) && !(of === "_ctx" && s.comp.props.fields.some((f) => f.js === method)))
188
+ return refuseUnavailable(s, method, n, true);
189
+ return null;
370
190
  }
371
191
  export function helperCall(s, name, args, n) {
372
192
  const h = ctx.helpers[name];