@slim-lang/core 1.2.11 → 1.2.13
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 +522 -14
- package/package.json +2 -6
- package/packages/slim/server/main.slim +433 -422
- package/run-dev-slim.js +10 -5
- package/run-slim.js +10 -20
- package/src/bin/api/github_get.js +7 -12
- package/src/bin/api/github_req.js +54 -241
- package/src/bin/api/lock.js +2 -1
- package/src/bin/api/spm.js +20 -91
- package/src/bin/cli.js +73 -102
- package/src/bin/config.default.json +11 -11
- package/src/bin/helpers.js +2 -7
- package/src/bin/spm.js +3 -7
- package/src/checker.js +1705 -388
- package/src/compile.js +131 -100
- package/src/external/classErrors.js +5 -6
- package/src/external/client.js +0 -2
- package/src/external/core.js +355 -237
- package/src/external/defaults.js +12 -4
- package/src/external/dom.js +663 -0
- package/src/external/helpers.js +87 -58
- package/src/external/reactive.js +601 -0
- package/src/external/router.js +117 -0
- package/src/external/slim-globals.d.ts +43 -4
- package/src/external/typeSyntax.js +258 -0
- package/src/external/types.js +1 -1
- package/src/format.js +183 -12
- package/src/handlers/errorHandler.js +9 -36
- package/src/jsdoc.js +36 -16
- package/src/modulePaths.js +15 -5
- package/src/reactivity.js +774 -0
- package/src/repl.js +1 -1
- package/src/suggest.js +60 -0
- package/src/syntax/babel.js +46 -0
- package/src/syntax/index.js +39 -0
- package/src/syntax/lower.js +666 -0
- package/src/syntax/markup.js +195 -0
- package/src/syntax/plugin.js +903 -0
- package/src/syntax/types.js +106 -0
- package/src/test-runner.js +5 -5
- package/src/transform.js +209 -244
- package/packages/slim/helpers/string.js +0 -43
- package/packages/slim/types/hash.js +0 -11
- package/src/handlers/parser/components.js +0 -295
- package/src/handlers/parserHandler.js +0 -789
- package/src/lexer.js +0 -174
- package/src/parser.js +0 -803
- package/src/sourcemap.js +0 -0
package/src/external/core.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
// Portable core runtime with no Node or DOM dependencies.
|
|
2
1
|
|
|
3
2
|
import {
|
|
4
3
|
StructPassedError, StructError, StructExpectError, ArgumentDeclarationTypeError,
|
|
5
|
-
EnumError
|
|
4
|
+
EnumError, formatError, resolveErrorLocation
|
|
6
5
|
} from "./classErrors.js"
|
|
7
6
|
import {
|
|
8
7
|
SlimVariableType, SlimVariableTypes, Struct, Component, Enum, EnumValue,
|
|
9
8
|
isSameType
|
|
10
9
|
} from "./types.js"
|
|
11
|
-
import {
|
|
10
|
+
import { ANY, readType, formatType, substituteType } from "./typeSyntax.js"
|
|
11
|
+
import { REACTIVE_GLOBALS, Signal, __computed__, batch } from "./reactive.js"
|
|
12
|
+
|
|
13
|
+
export { effect, watch, untrack, batch, onCleanup, resource, createContext, provide, inject, catchError } from "./reactive.js"
|
|
12
14
|
|
|
13
15
|
const __structs__ = {}
|
|
14
16
|
const __enums__ = {}
|
|
@@ -74,53 +76,51 @@ export function __handle_sync_error__(err) {
|
|
|
74
76
|
finalizeError(err)
|
|
75
77
|
}
|
|
76
78
|
|
|
77
|
-
function
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (Type.isTyped(arg) && "scheme" in arg) {
|
|
83
|
-
return arg.scheme
|
|
84
|
-
}
|
|
85
|
-
return arg
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
export const log = (...args) => {
|
|
89
|
-
const processed = logProcessed(args)
|
|
90
|
-
console.log(...processed)
|
|
91
|
-
}
|
|
92
|
-
export const warn = (...args) => {
|
|
93
|
-
const processed = logProcessed(args)
|
|
94
|
-
console.warn(...processed)
|
|
95
|
-
}
|
|
96
|
-
export const error = (...args) => {
|
|
97
|
-
const processed = logProcessed(args)
|
|
98
|
-
console.error(...processed)
|
|
99
|
-
}
|
|
100
|
-
export const info = (...args) => {
|
|
101
|
-
const processed = logProcessed(args)
|
|
102
|
-
console.info(...processed)
|
|
103
|
-
}
|
|
104
|
-
export const debug = (...args) => {
|
|
105
|
-
console.log(...args)
|
|
79
|
+
function unwrap(arg) {
|
|
80
|
+
if (!Type.isTyped(arg)) return arg
|
|
81
|
+
if ("value" in arg) return arg.value
|
|
82
|
+
if ("scheme" in arg) return arg.scheme
|
|
83
|
+
return arg
|
|
106
84
|
}
|
|
107
85
|
|
|
86
|
+
export const log = (...args) => console.log(...args.map(unwrap))
|
|
87
|
+
export const warn = (...args) => console.warn(...args.map(unwrap))
|
|
88
|
+
export const error = (...args) => console.error(...args.map(unwrap))
|
|
89
|
+
export const info = (...args) => console.info(...args.map(unwrap))
|
|
90
|
+
export const debug = (...args) => console.log(...args)
|
|
91
|
+
|
|
108
92
|
export const PI = Math.PI
|
|
109
93
|
|
|
110
94
|
const __test_stats__ = { passed: 0, failed: 0 }
|
|
111
95
|
|
|
112
|
-
|
|
96
|
+
const testGroups = []
|
|
97
|
+
let testQueue = Promise.resolve()
|
|
98
|
+
|
|
99
|
+
export function describe(name, fn) {
|
|
100
|
+
testGroups.push(name)
|
|
113
101
|
try {
|
|
114
102
|
fn()
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
} catch (err) {
|
|
118
|
-
__test_stats__.failed++
|
|
119
|
-
if (typeof process !== "undefined") process.exitCode = 1
|
|
120
|
-
console.log(` ✗ ${name}\n ${err?.message ?? err}`)
|
|
103
|
+
} finally {
|
|
104
|
+
testGroups.pop()
|
|
121
105
|
}
|
|
122
106
|
}
|
|
123
107
|
|
|
108
|
+
export function test(name, fn) {
|
|
109
|
+
const title = [...testGroups, name].join(" › ")
|
|
110
|
+
|
|
111
|
+
testQueue = testQueue.then(async () => {
|
|
112
|
+
try {
|
|
113
|
+
await fn()
|
|
114
|
+
__test_stats__.passed++
|
|
115
|
+
console.log(` ✓ ${title}`)
|
|
116
|
+
} catch (err) {
|
|
117
|
+
__test_stats__.failed++
|
|
118
|
+
if (typeof process !== "undefined") process.exitCode = 1
|
|
119
|
+
console.log(` ✗ ${title}\n ${err?.message ?? err}`)
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
|
|
124
124
|
export function assert(condition, message = "assertion failed") {
|
|
125
125
|
if (!condition) throw new Error(message)
|
|
126
126
|
}
|
|
@@ -152,21 +152,14 @@ export class Type {
|
|
|
152
152
|
return type(obj) == "object"
|
|
153
153
|
}
|
|
154
154
|
static isTyped(obj) {
|
|
155
|
-
|
|
156
|
-
return true
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
return false
|
|
160
|
-
}
|
|
155
|
+
return obj instanceof SlimVariableType || obj instanceof EnumValue || obj instanceof Struct
|
|
161
156
|
}
|
|
162
157
|
static isAnyArray(obj) {
|
|
163
|
-
|
|
164
|
-
else if (type(obj).endsWith("[]")) return true
|
|
165
|
-
else return false
|
|
158
|
+
return Array.isArray(obj)
|
|
166
159
|
}
|
|
167
160
|
|
|
168
|
-
static isRegistredCustom(
|
|
169
|
-
return Object.
|
|
161
|
+
static isRegistredCustom(name) {
|
|
162
|
+
return Object.hasOwn(__custom_types__, name)
|
|
170
163
|
}
|
|
171
164
|
|
|
172
165
|
static Customs = __custom_types__
|
|
@@ -177,57 +170,39 @@ export class Debug {
|
|
|
177
170
|
}
|
|
178
171
|
}
|
|
179
172
|
|
|
180
|
-
|
|
181
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
|
182
|
-
const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/
|
|
173
|
+
const ARRAY_KINDS = ["undefined", "string", "int", "float", "object", "enum", "struct", "array"]
|
|
183
174
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
175
|
+
function arrayType(items) {
|
|
176
|
+
if (items.length === 0) return "null[]"
|
|
177
|
+
|
|
178
|
+
const kinds = new Set(items.map(item => type(item)))
|
|
179
|
+
if (kinds.size === 1) {
|
|
180
|
+
const [kind] = kinds
|
|
181
|
+
if (kind === "undefined") return "null[]"
|
|
182
|
+
if (ARRAY_KINDS.includes(kind)) return `${kind}[]`
|
|
192
183
|
}
|
|
184
|
+
if ([...kinds].every(kind => kind === "int" || kind === "float" || kind === "NaN")) return "number[]"
|
|
185
|
+
return "array"
|
|
186
|
+
}
|
|
193
187
|
|
|
188
|
+
export function type(obj) {
|
|
194
189
|
if (obj === null) return "null"
|
|
195
190
|
if (obj === undefined) return "undefined"
|
|
196
191
|
if (Number.isNaN(obj)) return "NaN"
|
|
197
192
|
|
|
198
|
-
|
|
199
|
-
if (
|
|
200
|
-
if (obj && obj.nodeType === 11) return "fragment"
|
|
193
|
+
if (typeof obj === "object" && obj.nodeType === 1) return "element"
|
|
194
|
+
if (typeof obj === "object" && obj.nodeType === 11) return "fragment"
|
|
201
195
|
|
|
202
|
-
if(typeof obj
|
|
196
|
+
if (typeof obj === "function" && obj.__type__ === true) return "type"
|
|
197
|
+
if (obj instanceof SlimVariableType) return obj.kind
|
|
203
198
|
|
|
204
|
-
if (typeof obj
|
|
205
|
-
|
|
199
|
+
if (typeof obj === "object" && !Array.isArray(obj)) {
|
|
200
|
+
if (obj.type === Struct) return "struct"
|
|
201
|
+
if (obj.type instanceof Enum || obj instanceof EnumValue) return "enum"
|
|
206
202
|
}
|
|
207
203
|
|
|
208
|
-
if (typeof obj
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
if (typeof obj == "object" && !Array.isArray(obj) && "type" in obj && obj.type instanceof Enum) {
|
|
212
|
-
return "enum"
|
|
213
|
-
}
|
|
214
|
-
if (typeof obj == "object" && !Array.isArray(obj) && obj instanceof EnumValue) {
|
|
215
|
-
return "enum"
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (typeof obj === "function" && obj.__component__ == true) return "component"
|
|
219
|
-
|
|
220
|
-
if (isArray(obj) && isTypedArray(obj, "undefined")) return "null[]"
|
|
221
|
-
if (isArray(obj) && isTypedArray(obj, "string")) return "string[]"
|
|
222
|
-
if (isArray(obj) && isTypedArray(obj, "int")) return "int[]"
|
|
223
|
-
if (isArray(obj) && isTypedArray(obj, "float")) return "float[]"
|
|
224
|
-
if (isArray(obj) && isTypedArray(obj, "object")) return "object[]"
|
|
225
|
-
if (isArray(obj) && isTypedOfArray(obj, "number")) return "number[]"
|
|
226
|
-
if (isArray(obj) && isTypedArray(obj, "enum")) return "enum[]"
|
|
227
|
-
if (isArray(obj) && isTypedArray(obj, "struct")) return "struct[]"
|
|
228
|
-
if (isArray(obj) && isTypedArray(obj, "array")) return "array[]"
|
|
229
|
-
|
|
230
|
-
if (isArray(obj)) return "array"
|
|
204
|
+
if (typeof obj === "function" && obj.__component__ === true) return "component"
|
|
205
|
+
if (Array.isArray(obj)) return arrayType(obj)
|
|
231
206
|
|
|
232
207
|
if (obj instanceof Map) return "map"
|
|
233
208
|
if (obj instanceof Set) return "set"
|
|
@@ -277,20 +252,12 @@ export function __type_label__(specification) {
|
|
|
277
252
|
return refs.map(referenceLabel).join(" | ")
|
|
278
253
|
}
|
|
279
254
|
|
|
280
|
-
function
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
const name = reference.label.replace(/(?:\[\])+$/, "").split("::")[0]
|
|
285
|
-
return __custom_types__[name] ?? __structs__[name] ?? __enums__[name]
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
if (typeof reference === "string") {
|
|
289
|
-
const name = reference.replace(/(?:\[\])+$/, "").split("::")[0]
|
|
290
|
-
return __custom_types__[name] ?? __structs__[name] ?? __enums__[name]
|
|
291
|
-
}
|
|
255
|
+
function lookupType(name) {
|
|
256
|
+
return __custom_types__[name] ?? __structs__[name] ?? __enums__[name]
|
|
257
|
+
}
|
|
292
258
|
|
|
293
|
-
|
|
259
|
+
function resolveReference(reference) {
|
|
260
|
+
return typeof reference?.resolve === "function" ? reference.resolve() : undefined
|
|
294
261
|
}
|
|
295
262
|
|
|
296
263
|
function isClass(value) {
|
|
@@ -308,111 +275,70 @@ function matchesDefinition(definition, value) {
|
|
|
308
275
|
return isSameType(definition, value)
|
|
309
276
|
}
|
|
310
277
|
|
|
311
|
-
function matchesResolvedType(resolved,
|
|
278
|
+
function matchesResolvedType(resolved, value, typeArgs = null) {
|
|
312
279
|
if (resolved === undefined || resolved === null) return false
|
|
313
|
-
|
|
314
|
-
if (resolved
|
|
315
|
-
return __typed__(value, resolved, "errorResult").success
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
if (resolved?.type instanceof Enum) return resolved.has(value)
|
|
280
|
+
if (resolved.type === Struct) return __typed__(value, resolved, "errorResult", typeArgs).success
|
|
281
|
+
if (resolved.type instanceof Enum) return resolved.has(value)
|
|
319
282
|
if (resolved instanceof EnumValue) return resolved === value
|
|
320
|
-
|
|
321
|
-
if (typeof resolved === "function" && resolved.__type__ === true) {
|
|
322
|
-
return resolved(value) === true
|
|
323
|
-
}
|
|
324
|
-
|
|
283
|
+
if (typeof resolved === "function" && resolved.__type__ === true) return resolved(value) === true
|
|
325
284
|
return matchesDefinition(resolved, value)
|
|
326
285
|
}
|
|
327
286
|
|
|
328
287
|
const BUILTIN_CLASSES = { Map, Set, Date, Promise, RegExp, Error }
|
|
329
288
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
for (const char of text) {
|
|
337
|
-
if (char === "<" || char === "[" || char === "(") depth++
|
|
338
|
-
else if (char === ">" || char === "]" || char === ")") depth--
|
|
339
|
-
else if (char === "," && depth === 0) { parts.push(current.trim()); current = ""; continue }
|
|
340
|
-
current += char
|
|
341
|
-
}
|
|
342
|
-
if (current.trim()) parts.push(current.trim())
|
|
343
|
-
return parts
|
|
289
|
+
function matchesNamed(name, resolved, value) {
|
|
290
|
+
if (resolved !== undefined) return matchesResolvedType(resolved, value)
|
|
291
|
+
if (name === "number" || name === "float") return typeof value === "number" && !Number.isNaN(value)
|
|
292
|
+
if (name === "array") return Array.isArray(value)
|
|
293
|
+
if (name in BUILTIN_CLASSES) return value instanceof BUILTIN_CLASSES[name]
|
|
294
|
+
return type(value) === name
|
|
344
295
|
}
|
|
345
296
|
|
|
346
|
-
function
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
297
|
+
function matchesNode(node, value, resolved) {
|
|
298
|
+
switch (node.kind) {
|
|
299
|
+
case "any":
|
|
300
|
+
return true
|
|
301
|
+
case "union":
|
|
302
|
+
return node.types.some(part => matchesNode(part, value))
|
|
303
|
+
case "intersection":
|
|
304
|
+
return node.types.every(part => matchesNode(part, value))
|
|
305
|
+
case "literal":
|
|
306
|
+
return value === node.value
|
|
307
|
+
case "tuple":
|
|
308
|
+
return Array.isArray(value) && value.length === node.items.length &&
|
|
309
|
+
node.items.every((part, index) => matchesNode(part, value[index]))
|
|
310
|
+
case "array":
|
|
311
|
+
if (!Array.isArray(value)) return false
|
|
312
|
+
if (node.element.kind === "named" && node.element.name === "null") return value.every(item => item == null)
|
|
313
|
+
return value.every(item => matchesNode(node.element, item, resolved))
|
|
314
|
+
case "generic": {
|
|
315
|
+
const [first = ANY, second = ANY] = node.args
|
|
316
|
+
if (node.name === "Set") return value instanceof Set && [...value].every(item => matchesNode(first, item))
|
|
317
|
+
if (node.name === "Map") {
|
|
318
|
+
return value instanceof Map && [...value].every(([key, entry]) =>
|
|
319
|
+
matchesNode(first, key) && matchesNode(second, entry))
|
|
320
|
+
}
|
|
321
|
+
if (node.name === "Promise") return value instanceof Promise
|
|
366
322
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
if (container === "Set") {
|
|
371
|
-
return value instanceof Set && [...value].every(item => matchesLabel(first, item))
|
|
323
|
+
const definition = resolved ?? lookupType(node.name)
|
|
324
|
+
if (definition?.type === Struct) return matchesResolvedType(definition, value, node.args)
|
|
325
|
+
return matchesNamed(node.name, definition, value)
|
|
372
326
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
327
|
+
case "function":
|
|
328
|
+
return typeof value === "function"
|
|
329
|
+
case "enumMember": {
|
|
330
|
+
const definition = resolved ?? __enums__[node.name]
|
|
331
|
+
return !!definition && definition[node.member] === value
|
|
377
332
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
return matchesNamedType(container, resolved, value)
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
if (label.endsWith("[]")) {
|
|
384
|
-
if (!Array.isArray(value)) return false
|
|
385
|
-
|
|
386
|
-
const elementLabel = label.slice(0, -2)
|
|
387
|
-
if (value.length === 0) return type(value) === label || elementLabel === "any"
|
|
388
|
-
|
|
389
|
-
if (resolved !== undefined) {
|
|
390
|
-
return value.every(item => matchesResolvedType(resolved, elementLabel, item))
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
return type(value) === label
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
const enumSeparator = label.indexOf("::")
|
|
397
|
-
if (enumSeparator !== -1) {
|
|
398
|
-
const enumName = label.slice(0, enumSeparator)
|
|
399
|
-
const member = label.slice(enumSeparator + 2)
|
|
400
|
-
const enumDef = resolved ?? __enums__[enumName]
|
|
401
|
-
return !!enumDef && enumDef[member] === value
|
|
333
|
+
default:
|
|
334
|
+
return matchesNamed(node.name, resolved ?? lookupType(node.name), value)
|
|
402
335
|
}
|
|
403
|
-
|
|
404
|
-
if (resolved !== undefined) return matchesResolvedType(resolved, label, value)
|
|
405
|
-
if (label === "number") return type(value) === "int" || type(value) === "float"
|
|
406
|
-
|
|
407
|
-
if (label in BUILTIN_CLASSES) return value instanceof BUILTIN_CLASSES[label]
|
|
408
|
-
|
|
409
|
-
return type(value) === label
|
|
410
336
|
}
|
|
411
337
|
|
|
412
338
|
function matchesReference(reference, value) {
|
|
413
339
|
const label = typeof reference === "string" ? reference : reference?.label
|
|
414
340
|
if (!label) return false
|
|
415
|
-
return
|
|
341
|
+
return matchesNode(readType(label), value, resolveReference(reference))
|
|
416
342
|
}
|
|
417
343
|
|
|
418
344
|
export function __type_matches__(specification, value) {
|
|
@@ -423,6 +349,14 @@ export function __type_matches__(specification, value) {
|
|
|
423
349
|
return references.some(reference => matchesReference(reference, value))
|
|
424
350
|
}
|
|
425
351
|
|
|
352
|
+
function substituteTypeParams(label, params, args) {
|
|
353
|
+
const bound = new Map(params.map((param, index) => {
|
|
354
|
+
const arg = args?.[index] ?? ANY
|
|
355
|
+
return [param, typeof arg === "string" ? readType(arg) : arg]
|
|
356
|
+
}))
|
|
357
|
+
return substituteType(readType(label), bound)
|
|
358
|
+
}
|
|
359
|
+
|
|
426
360
|
function previewValue(value) {
|
|
427
361
|
try {
|
|
428
362
|
if (typeof value === "string") return JSON.stringify(value)
|
|
@@ -476,6 +410,31 @@ function getStaticFieldType(owner, field) {
|
|
|
476
410
|
return typedStaticFields.get(owner)?.get(field)
|
|
477
411
|
}
|
|
478
412
|
|
|
413
|
+
const isIndex = property => typeof property === "string" && /^\d+$/.test(property)
|
|
414
|
+
|
|
415
|
+
function propertyCheck(specification) {
|
|
416
|
+
if (!Array.isArray(specification) || specification.length !== 1) return null
|
|
417
|
+
|
|
418
|
+
const reference = specification[0]
|
|
419
|
+
const node = readType(referenceLabel(reference))
|
|
420
|
+
const resolved = resolveReference(reference)
|
|
421
|
+
|
|
422
|
+
if (node.kind === "array") {
|
|
423
|
+
return (target, property) =>
|
|
424
|
+
!isIndex(property) || !(property in target) || matchesNode(node.element, target[property], resolved)
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const definition = node.kind === "named" ? resolved ?? lookupType(node.name) : null
|
|
428
|
+
if (definition?.type !== Struct || definition.typeParams.length > 0) return null
|
|
429
|
+
|
|
430
|
+
return (target, property) => {
|
|
431
|
+
const field = definition.scheme[property]
|
|
432
|
+
if (!field) return false
|
|
433
|
+
if (!(property in target)) return field.optional
|
|
434
|
+
return __type_matches__(field.specification, target[property])
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
479
438
|
function guardTypedValue(value, specification, varName) {
|
|
480
439
|
if (!value || typeof value !== "object" || !__type_matches__(specification, value)) return value
|
|
481
440
|
if (__type_label__(specification) === "any") return value
|
|
@@ -488,17 +447,24 @@ function guardTypedValue(value, specification, varName) {
|
|
|
488
447
|
if (descriptor) Reflect.defineProperty(target, property, descriptor)
|
|
489
448
|
else Reflect.deleteProperty(target, property)
|
|
490
449
|
}
|
|
450
|
+
const quick = propertyCheck(specification)
|
|
491
451
|
const check = (target, property, descriptor) => {
|
|
492
|
-
if (__type_matches__(specification, target)) return
|
|
452
|
+
if (quick ? quick(target, property) : __type_matches__(specification, target)) return
|
|
493
453
|
restore(target, property, descriptor)
|
|
494
454
|
typeError(varName, __type_label__(specification), target, "mutated")
|
|
495
455
|
}
|
|
496
456
|
const proxy = new Proxy(value, {
|
|
497
457
|
set(target, property, next) {
|
|
498
458
|
const descriptor = Object.getOwnPropertyDescriptor(target, property)
|
|
459
|
+
const length = Array.isArray(target) ? target.length : -1
|
|
499
460
|
const succeeded = Reflect.set(target, property, next, target)
|
|
500
461
|
if (!succeeded) return false
|
|
501
|
-
|
|
462
|
+
try {
|
|
463
|
+
check(target, property, descriptor)
|
|
464
|
+
} catch (error) {
|
|
465
|
+
if (length !== -1) target.length = length
|
|
466
|
+
throw error
|
|
467
|
+
}
|
|
502
468
|
return true
|
|
503
469
|
},
|
|
504
470
|
deleteProperty(target, property) {
|
|
@@ -524,13 +490,19 @@ function guardTypedValue(value, specification, varName) {
|
|
|
524
490
|
}
|
|
525
491
|
|
|
526
492
|
|
|
527
|
-
export function __def_struct__(
|
|
493
|
+
export function __def_struct__(
|
|
494
|
+
name, schema, specifications = {}, defaults = {}, extendsName = null, methods = {},
|
|
495
|
+
typeParams = null, readonly = null
|
|
496
|
+
) {
|
|
497
|
+
if (__RESERVED_DEFINES__.has(name)) throw new StructError(`Name "${name}" is reserved`)
|
|
498
|
+
|
|
528
499
|
const parent = extendsName ? __structs__[extendsName] : null
|
|
529
500
|
if (extendsName && !parent) throw new StructError(`Struct "${name}" extends unknown struct "${extendsName}"`)
|
|
530
501
|
|
|
531
502
|
const schemeArray = parent ? { ...parent.scheme } : {}
|
|
532
503
|
const mergedDefaults = parent ? { ...parent.__defaults__, ...defaults } : defaults
|
|
533
|
-
const mergedMethods = parent ? { ...parent.__methods__, ...methods } : methods
|
|
504
|
+
const mergedMethods = parent ? { ...parent.__methods__, ...(methods ?? {}) } : methods ?? {}
|
|
505
|
+
const readonlyFields = new Set([...(parent?.readonly ?? []), ...(readonly ?? [])])
|
|
534
506
|
|
|
535
507
|
Object.keys(schema).forEach(item => {
|
|
536
508
|
let fieldName = item
|
|
@@ -551,27 +523,40 @@ export function __def_struct__(name, schema, specifications = {}, defaults = {},
|
|
|
551
523
|
type: Struct,
|
|
552
524
|
name,
|
|
553
525
|
scheme: schemeArray,
|
|
526
|
+
typeParams: typeParams ?? [],
|
|
527
|
+
readonly: [...readonlyFields],
|
|
554
528
|
__defaults__: mergedDefaults,
|
|
555
529
|
__methods__: mergedMethods,
|
|
556
|
-
verify: object => __typed__(object, definition),
|
|
557
|
-
verifySafe: object => __typed__(object, definition, "errorResult"),
|
|
530
|
+
verify: (object, typeArgs = null) => __typed__(object, definition, "default", typeArgs),
|
|
531
|
+
verifySafe: (object, typeArgs = null) => __typed__(object, definition, "errorResult", typeArgs),
|
|
558
532
|
new: (values = {}) => {
|
|
559
533
|
const filled = {}
|
|
560
534
|
for (const key of Object.keys(mergedDefaults)) {
|
|
561
535
|
if (!(key in values)) filled[key] = mergedDefaults[key]()
|
|
562
536
|
}
|
|
563
537
|
const data = __typed__({ ...filled, ...values }, definition)
|
|
564
|
-
|
|
538
|
+
for (const [field, { type: label }] of Object.entries(schemeArray)) {
|
|
539
|
+
if (field in data) data[field] = toStruct(data[field], readType(label))
|
|
540
|
+
}
|
|
541
|
+
const instance = Object.keys(mergedMethods).length ? Object.assign(Object.create(mergedMethods), data) : data
|
|
542
|
+
|
|
543
|
+
for (const field of readonlyFields) {
|
|
544
|
+
if (field in instance) {
|
|
545
|
+
Object.defineProperty(instance, field, { value: instance[field], enumerable: true, writable: false, configurable: false })
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return instance
|
|
565
549
|
}
|
|
566
550
|
}
|
|
567
551
|
|
|
568
552
|
__structs__[name] = definition
|
|
569
|
-
if (__RESERVED_DEFINES__.has(name)) throw new StructError(`Name "${name}" is reserved`)
|
|
570
553
|
globalThis[name] = definition
|
|
571
554
|
return definition
|
|
572
555
|
}
|
|
573
556
|
|
|
574
557
|
export function __def_enum__(name, schema) {
|
|
558
|
+
if (__RESERVED_DEFINES__.has(name)) throw new EnumError(`Name "${name}" is reserved`)
|
|
559
|
+
|
|
575
560
|
const values = { ...schema }
|
|
576
561
|
const definition = {
|
|
577
562
|
type: new Enum(name),
|
|
@@ -581,7 +566,13 @@ export function __def_enum__(name, schema) {
|
|
|
581
566
|
keys: () => Object.keys(values),
|
|
582
567
|
has: value => Object.values(values).some(item =>
|
|
583
568
|
value === item || (value instanceof EnumValue && value.value === item)
|
|
584
|
-
)
|
|
569
|
+
),
|
|
570
|
+
from: value => {
|
|
571
|
+
const raw = value instanceof EnumValue ? value.value : value
|
|
572
|
+
const key = Object.keys(values).find(key => values[key] === raw)
|
|
573
|
+
if (key === undefined) throw new EnumError(`${previewValue(value)} is not a value of enum "${name}"`)
|
|
574
|
+
return definition[key]
|
|
575
|
+
}
|
|
585
576
|
}
|
|
586
577
|
|
|
587
578
|
Object.keys(values).forEach(key => {
|
|
@@ -589,7 +580,6 @@ export function __def_enum__(name, schema) {
|
|
|
589
580
|
})
|
|
590
581
|
|
|
591
582
|
__enums__[name] = definition
|
|
592
|
-
if (__RESERVED_DEFINES__.has(name)) throw new EnumError(`Name "${name}" is reserved`)
|
|
593
583
|
globalThis[name] = definition
|
|
594
584
|
return definition
|
|
595
585
|
}
|
|
@@ -607,6 +597,36 @@ export function __typed_variable__(value, specification, bindingId, varName = bi
|
|
|
607
597
|
return guardTypedValue(value, specification, varName)
|
|
608
598
|
}
|
|
609
599
|
|
|
600
|
+
const isPlainObject = value =>
|
|
601
|
+
value !== null && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype
|
|
602
|
+
|
|
603
|
+
function structNamed(type) {
|
|
604
|
+
if (type.kind === "union") {
|
|
605
|
+
const found = type.types.map(structNamed).filter(Boolean)
|
|
606
|
+
return found.length === 1 ? found[0] : null
|
|
607
|
+
}
|
|
608
|
+
if (type.kind !== "named" && type.kind !== "generic") return null
|
|
609
|
+
const definition = lookupType(type.name)
|
|
610
|
+
return definition?.type === Struct ? definition : null
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function toStruct(value, type) {
|
|
614
|
+
if (Array.isArray(value) && type.kind === "array" && structNamed(type.element)) {
|
|
615
|
+
return value.map(item => toStruct(item, type.element))
|
|
616
|
+
}
|
|
617
|
+
if (!isPlainObject(value)) return value
|
|
618
|
+
const definition = structNamed(type)
|
|
619
|
+
return definition ? definition.new(value) : value
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
export function __match_type__(value, label) {
|
|
623
|
+
return matchesNode(readType(label), value)
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
export function __as_struct__(value, label) {
|
|
627
|
+
return toStruct(value, readType(label))
|
|
628
|
+
}
|
|
629
|
+
|
|
610
630
|
export function __typed_return__(value, specification, fnName) {
|
|
611
631
|
if (RELEASE) return value
|
|
612
632
|
|
|
@@ -702,7 +722,7 @@ __type_def__("negative", value => typeof value === "number" && value < 0)
|
|
|
702
722
|
__type_def__("natural", value => Number.isInteger(value) && value >= 0)
|
|
703
723
|
__type_def__("nonempty", value => (typeof value === "string" || Array.isArray(value)) && value.length > 0)
|
|
704
724
|
|
|
705
|
-
export function __typed__(value, structName, returnMethod = "default") {
|
|
725
|
+
export function __typed__(value, structName, returnMethod = "default", typeArgs = null) {
|
|
706
726
|
const name = typeof structName === "string" ? structName : structName?.name
|
|
707
727
|
const structDef = typeof structName === "string" ? __structs__[structName] : structName
|
|
708
728
|
if (!structDef) throw new StructError(`Unknown struct "${structName}"`)
|
|
@@ -736,14 +756,23 @@ export function __typed__(value, structName, returnMethod = "default") {
|
|
|
736
756
|
continue
|
|
737
757
|
}
|
|
738
758
|
|
|
739
|
-
|
|
759
|
+
const params = structDef.typeParams ?? []
|
|
760
|
+
const expected = params.length
|
|
761
|
+
? substituteTypeParams(__type_label__(definition.specification), params, typeArgs)
|
|
762
|
+
: null
|
|
763
|
+
const fits = expected === null
|
|
764
|
+
? __type_matches__(definition.specification, value[field])
|
|
765
|
+
: matchesNode(expected, value[field])
|
|
766
|
+
|
|
767
|
+
if (!fits) {
|
|
768
|
+
const label = expected ? formatType(expected) : __type_label__(definition.specification)
|
|
740
769
|
return fail(new StructError(
|
|
741
|
-
`"${name}.${field}" expected ${
|
|
770
|
+
`"${name}.${field}" expected ${label}, got ${type(value[field])}: ${previewValue(value[field])}`
|
|
742
771
|
), {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
772
|
+
name,
|
|
773
|
+
field,
|
|
774
|
+
expected: label,
|
|
775
|
+
got: type(value[field]),
|
|
747
776
|
fieldName: `${name}.${field}`
|
|
748
777
|
})
|
|
749
778
|
}
|
|
@@ -765,26 +794,13 @@ export function __sizeof__(value) {
|
|
|
765
794
|
}
|
|
766
795
|
|
|
767
796
|
export function __is_empty__(obj) {
|
|
768
|
-
|
|
769
|
-
return true
|
|
770
|
-
}
|
|
771
|
-
else {
|
|
772
|
-
return false
|
|
773
|
-
}
|
|
797
|
+
return __sizeof__(obj) === 0
|
|
774
798
|
}
|
|
775
799
|
|
|
776
800
|
export function __copyof__(obj) {
|
|
777
|
-
if (
|
|
778
|
-
|
|
779
|
-
}
|
|
780
|
-
else if (Type.isAnyArray(obj)) {
|
|
781
|
-
const arrCopy = []
|
|
782
|
-
obj.forEach(item => arrCopy.push(item))
|
|
783
|
-
return arrCopy
|
|
784
|
-
}
|
|
785
|
-
else {
|
|
786
|
-
throw TypeError(`Copy target must be object or array, not ${type(obj)}`)
|
|
787
|
-
}
|
|
801
|
+
if (Array.isArray(obj)) return [...obj]
|
|
802
|
+
if (Type.isObj(obj)) return { ...obj }
|
|
803
|
+
throw new TypeError(`Copy target must be object or array, not ${type(obj)}`)
|
|
788
804
|
}
|
|
789
805
|
|
|
790
806
|
export function __intdiv__(a, b) {
|
|
@@ -820,42 +836,144 @@ export function __lock_object__(obj) {
|
|
|
820
836
|
}
|
|
821
837
|
|
|
822
838
|
const wrapped = new Proxy(obj, handler)
|
|
823
|
-
|
|
824
839
|
IMMUTABLE.set(obj, wrapped)
|
|
825
840
|
|
|
826
|
-
const
|
|
841
|
+
for (const key of Reflect.ownKeys(obj)) {
|
|
842
|
+
const descriptor = Object.getOwnPropertyDescriptor(obj, key)
|
|
843
|
+
const value = descriptor.value
|
|
844
|
+
if (!value || typeof value !== "object") continue
|
|
827
845
|
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
if (value && typeof value === "object") {
|
|
832
|
-
obj[key] = __lock_object__(value)
|
|
833
|
-
}
|
|
846
|
+
const locked = __lock_object__(value)
|
|
847
|
+
if (descriptor.writable) obj[key] = locked
|
|
834
848
|
}
|
|
835
849
|
|
|
836
|
-
Object.freeze(obj)
|
|
850
|
+
if (!ArrayBuffer.isView(obj)) Object.freeze(obj)
|
|
837
851
|
|
|
838
852
|
return wrapped
|
|
839
853
|
}
|
|
840
854
|
|
|
855
|
+
|
|
856
|
+
const FORM_NAMES = new Set(["errors", "valid", "values", "submit", "reset", "submitting", "touched"])
|
|
857
|
+
const isBlank = value => value === undefined || value === null || value === ""
|
|
858
|
+
|
|
859
|
+
function emptyValue(label) {
|
|
860
|
+
if (label === "bool") return false
|
|
861
|
+
return ""
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
export function createForm(definition, { initial = {}, messages = {} } = {}) {
|
|
865
|
+
if (!definition?.scheme) throw new TypeError("createForm takes a struct: createForm(Signup)")
|
|
866
|
+
const scheme = definition.scheme
|
|
867
|
+
const fields = Object.keys(scheme)
|
|
868
|
+
for (const field of fields) {
|
|
869
|
+
if (FORM_NAMES.has(field)) throw new TypeError(`createForm: the field "${field}" of "${definition.name}" has the name of a form property, rename it`)
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
const startValue = field => field in initial ? initial[field]
|
|
873
|
+
: field in definition.__defaults__ ? definition.__defaults__[field]()
|
|
874
|
+
: emptyValue(scheme[field].type)
|
|
875
|
+
const values = Object.fromEntries(fields.map(field => [field, new Signal(startValue(field))]))
|
|
876
|
+
const touched = Object.fromEntries(fields.map(field => [field, new Signal(false)]))
|
|
877
|
+
const submitted = new Signal(false)
|
|
878
|
+
const submitting = new Signal(false)
|
|
879
|
+
|
|
880
|
+
const coerce = (field, value) => {
|
|
881
|
+
const { specification } = scheme[field]
|
|
882
|
+
if (typeof value !== "string" || value.trim() === "" || __type_matches__(specification, value)) return value
|
|
883
|
+
const number = Number(value)
|
|
884
|
+
return !Number.isNaN(number) && __type_matches__(specification, number) ? number : value
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
const problems = Object.fromEntries(fields.map(field => [field, __computed__(() => {
|
|
888
|
+
const { specification, optional } = scheme[field]
|
|
889
|
+
const value = values[field].v
|
|
890
|
+
if (isBlank(value)) return optional ? null : messages[field] ?? "required"
|
|
891
|
+
if (__type_matches__(specification, value)) return null
|
|
892
|
+
return messages[field] ?? `expected ${__type_label__(specification)}`
|
|
893
|
+
})]))
|
|
894
|
+
|
|
895
|
+
const errors = {}
|
|
896
|
+
for (const field of fields) {
|
|
897
|
+
Object.defineProperty(errors, field, {
|
|
898
|
+
enumerable: true,
|
|
899
|
+
get: () => touched[field].v || submitted.v ? problems[field].v : null
|
|
900
|
+
})
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
const valid = __computed__(() => fields.every(field => problems[field].v === null))
|
|
904
|
+
const snapshot = () => {
|
|
905
|
+
const data = {}
|
|
906
|
+
for (const field of fields) {
|
|
907
|
+
const value = values[field].peek()
|
|
908
|
+
if (!(isBlank(value) && scheme[field].optional)) data[field] = value
|
|
909
|
+
}
|
|
910
|
+
return data
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
const form = {
|
|
914
|
+
errors,
|
|
915
|
+
get valid() { return valid.v },
|
|
916
|
+
get values() { return snapshot() },
|
|
917
|
+
get submitting() { return submitting.v },
|
|
918
|
+
touched: field => touched[field]?.v ?? false,
|
|
919
|
+
submit(handler) {
|
|
920
|
+
return event => {
|
|
921
|
+
event?.preventDefault?.()
|
|
922
|
+
submitted.v = true
|
|
923
|
+
if (!valid.peek()) return
|
|
924
|
+
const result = handler(definition.new(snapshot()))
|
|
925
|
+
if (result && typeof result.then === "function") {
|
|
926
|
+
submitting.v = true
|
|
927
|
+
return result.finally(() => { submitting.v = false })
|
|
928
|
+
}
|
|
929
|
+
return result
|
|
930
|
+
}
|
|
931
|
+
},
|
|
932
|
+
reset(next = {}) {
|
|
933
|
+
batch(() => {
|
|
934
|
+
for (const field of fields) {
|
|
935
|
+
values[field].v = field in next ? next[field] : startValue(field)
|
|
936
|
+
touched[field].v = false
|
|
937
|
+
}
|
|
938
|
+
submitted.v = false
|
|
939
|
+
})
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
for (const field of fields) {
|
|
943
|
+
Object.defineProperty(form, field, {
|
|
944
|
+
enumerable: true,
|
|
945
|
+
get: () => values[field].v,
|
|
946
|
+
set: value => batch(() => {
|
|
947
|
+
values[field].v = coerce(field, value)
|
|
948
|
+
touched[field].v = true
|
|
949
|
+
})
|
|
950
|
+
})
|
|
951
|
+
}
|
|
952
|
+
return form
|
|
953
|
+
}
|
|
954
|
+
|
|
841
955
|
Object.assign(globalThis, {
|
|
842
956
|
log, warn, error, info, debug,
|
|
843
957
|
|
|
958
|
+
createForm,
|
|
959
|
+
|
|
844
960
|
type,
|
|
845
961
|
|
|
846
962
|
Component, Type, Struct,
|
|
847
963
|
|
|
848
|
-
onError, test, assert, assertEqual,
|
|
964
|
+
onError, test, describe, assert, assertEqual,
|
|
849
965
|
|
|
850
966
|
__def_struct__, __def_enum__, __typed__, __handle_async_error__,
|
|
851
967
|
__handle_sync_error__, __sizeof__, __is_empty__, __lock_object__,
|
|
852
|
-
__typed_variable__, __typed_variable_check__, __typed_pattern__, __typed_return__,
|
|
853
|
-
__intdiv__, __copyof__, __match_eq__,
|
|
968
|
+
__typed_variable__, __typed_variable_check__, __typed_pattern__, __typed_return__, __as_struct__,
|
|
969
|
+
__intdiv__, __copyof__, __match_eq__, __match_type__,
|
|
854
970
|
__typed_parameter__, __typed_static_field__, __typed_static_field_check__,
|
|
855
971
|
__type_def__, __argument_typed__,
|
|
856
972
|
__type_ref__, __type_spec__, __type_spec_all__, __type_matches__, __type_label__,
|
|
857
973
|
|
|
858
974
|
StructError, StructPassedError, StructExpectError, ArgumentDeclarationTypeError,
|
|
859
975
|
|
|
860
|
-
PI
|
|
976
|
+
PI,
|
|
977
|
+
|
|
978
|
+
...REACTIVE_GLOBALS
|
|
861
979
|
})
|