firefly-compiler 0.4.79 → 0.4.80

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 (158) hide show
  1. package/.hintrc +4 -4
  2. package/.vscode/settings.json +4 -4
  3. package/bin/Release.ff +153 -153
  4. package/bin/firefly.mjs +1 -1
  5. package/compiler/Builder.ff +257 -257
  6. package/compiler/Compiler.ff +227 -227
  7. package/compiler/Dependencies.ff +187 -187
  8. package/compiler/DependencyLock.ff +17 -17
  9. package/compiler/Inference.ff +2 -1
  10. package/compiler/JsEmitter.ff +940 -946
  11. package/compiler/LspHook.ff +202 -202
  12. package/compiler/Main.ff +3 -3
  13. package/compiler/ModuleCache.ff +178 -178
  14. package/compiler/Tokenizer.ff +1 -1
  15. package/compiler/Unification.ff +1 -1
  16. package/compiler/Workspace.ff +88 -88
  17. package/core/.firefly/include/package-lock.json +564 -564
  18. package/core/.firefly/include/package.json +5 -5
  19. package/core/.firefly/include/prepare.sh +1 -1
  20. package/core/.firefly/package.ff +2 -2
  21. package/core/Array.ff +265 -265
  22. package/core/Atomic.ff +64 -64
  23. package/core/Box.ff +7 -7
  24. package/core/BrowserSystem.ff +40 -40
  25. package/core/BuildSystem.ff +148 -148
  26. package/core/Crypto.ff +96 -96
  27. package/core/Equal.ff +36 -36
  28. package/core/Float.ff +25 -0
  29. package/core/HttpClient.ff +148 -148
  30. package/core/JsSystem.ff +69 -69
  31. package/core/Json.ff +434 -434
  32. package/core/List.ff +486 -486
  33. package/core/Lock.ff +144 -144
  34. package/core/NodeSystem.ff +216 -216
  35. package/core/Ordering.ff +161 -161
  36. package/core/Path.ff +401 -401
  37. package/core/Random.ff +134 -134
  38. package/core/RbMap.ff +216 -216
  39. package/core/Show.ff +43 -43
  40. package/core/SourceLocation.ff +68 -68
  41. package/core/Stream.ff +9 -9
  42. package/core/Task.ff +141 -141
  43. package/core/Try.ff +25 -4
  44. package/experimental/benchmarks/ListGrab.ff +23 -23
  45. package/experimental/benchmarks/ListGrab.java +55 -55
  46. package/experimental/benchmarks/Pyrotek45.ff +30 -30
  47. package/experimental/benchmarks/Pyrotek45.java +64 -64
  48. package/experimental/bidirectional/Bidi.ff +88 -88
  49. package/experimental/random/Index.ff +53 -53
  50. package/experimental/random/Process.ff +120 -120
  51. package/experimental/random/Scrape.ff +51 -51
  52. package/experimental/random/Symbols.ff +73 -73
  53. package/experimental/random/Tensor.ff +52 -52
  54. package/experimental/random/Units.ff +36 -36
  55. package/experimental/s3/S3TestAuthorizationHeader.ff +39 -39
  56. package/experimental/s3/S3TestPut.ff +16 -16
  57. package/experimental/tests/TestJson.ff +26 -26
  58. package/firefly.sh +0 -0
  59. package/fireflysite/.firefly/package.ff +4 -4
  60. package/fireflysite/CommunityOverview.ff +20 -20
  61. package/fireflysite/CountingButtonDemo.ff +58 -58
  62. package/fireflysite/DocumentParser.ff +331 -217
  63. package/fireflysite/ExamplesOverview.ff +40 -40
  64. package/fireflysite/FrontPage.ff +344 -360
  65. package/fireflysite/{GuideIntroduction.ff → GettingStarted.ff} +45 -52
  66. package/fireflysite/Guide.ff +443 -411
  67. package/fireflysite/Main.ff +141 -137
  68. package/fireflysite/MatchingPasswordsDemo.ff +82 -82
  69. package/fireflysite/PackagesOverview.ff +49 -49
  70. package/fireflysite/PostgresqlDemo.ff +34 -34
  71. package/fireflysite/ReferenceAll.ff +19 -0
  72. package/fireflysite/ReferenceIntroduction.ff +11 -0
  73. package/fireflysite/Styles.ff +567 -495
  74. package/fireflysite/Test.ff +38 -0
  75. package/fireflysite/assets/markdown/reference/BaseTypes.md +209 -0
  76. package/fireflysite/assets/markdown/reference/FunctionsAndMethods.md +208 -0
  77. package/fireflysite/assets/markdown/reference/ModulesAndPackages.md +168 -0
  78. package/fireflysite/assets/markdown/reference/PatternMatching.md +224 -0
  79. package/fireflysite/assets/markdown/reference/StatementsAndExpressions.md +86 -0
  80. package/fireflysite/assets/markdown/reference/TraitsAndInstances.md +100 -0
  81. package/fireflysite/assets/markdown/reference/UserDefinedTypes.md +184 -0
  82. package/fireflysite/assets/markdown/{ControlFlow.md → scratch/ControlFlow.md} +136 -136
  83. package/fireflysite/assets/markdown/scratch/Toc.md +41 -0
  84. package/lsp/.firefly/package.ff +1 -1
  85. package/lsp/CompletionHandler.ff +828 -828
  86. package/lsp/Handler.ff +714 -714
  87. package/lsp/HoverHandler.ff +79 -79
  88. package/lsp/LanguageServer.ff +272 -272
  89. package/lsp/SignatureHelpHandler.ff +55 -55
  90. package/lsp/SymbolHandler.ff +181 -181
  91. package/lsp/TestReferences.ff +17 -17
  92. package/lsp/TestReferencesCase.ff +7 -7
  93. package/lsp/stderr.txt +1 -1
  94. package/lsp/stdout.txt +34 -34
  95. package/lux/.firefly/package.ff +1 -1
  96. package/lux/Css.ff +648 -648
  97. package/lux/CssTest.ff +48 -48
  98. package/lux/Lux.ff +487 -487
  99. package/lux/LuxEvent.ff +116 -116
  100. package/lux/Main.ff +123 -123
  101. package/lux/Main2.ff +143 -143
  102. package/output/js/ff/compiler/Builder.mjs +47 -47
  103. package/output/js/ff/compiler/Dependencies.mjs +3 -3
  104. package/output/js/ff/compiler/Inference.mjs +2 -2
  105. package/output/js/ff/compiler/JsEmitter.mjs +18 -72
  106. package/output/js/ff/compiler/Main.mjs +4 -4
  107. package/output/js/ff/compiler/ModuleCache.mjs +4 -4
  108. package/output/js/ff/core/Array.mjs +59 -59
  109. package/output/js/ff/core/Atomic.mjs +36 -36
  110. package/output/js/ff/core/BrowserSystem.mjs +11 -11
  111. package/output/js/ff/core/BuildSystem.mjs +30 -30
  112. package/output/js/ff/core/Crypto.mjs +40 -40
  113. package/output/js/ff/core/Float.mjs +50 -0
  114. package/output/js/ff/core/HttpClient.mjs +56 -56
  115. package/output/js/ff/core/Json.mjs +147 -147
  116. package/output/js/ff/core/List.mjs +50 -50
  117. package/output/js/ff/core/Lock.mjs +97 -97
  118. package/output/js/ff/core/NodeSystem.mjs +87 -87
  119. package/output/js/ff/core/Ordering.mjs +8 -8
  120. package/output/js/ff/core/Path.mjs +231 -231
  121. package/output/js/ff/core/Random.mjs +56 -56
  122. package/output/js/ff/core/Task.mjs +39 -39
  123. package/output/js/ff/core/Try.mjs +98 -4
  124. package/package.json +1 -1
  125. package/postgresql/Pg.ff +1 -1
  126. package/rpc/.firefly/package.ff +1 -1
  127. package/rpc/Rpc.ff +70 -70
  128. package/s3/.firefly/package.ff +1 -1
  129. package/s3/S3.ff +94 -94
  130. package/unsafejs/UnsafeJs.ff +19 -19
  131. package/vscode/LICENSE.txt +21 -21
  132. package/vscode/Prepublish.ff +15 -15
  133. package/vscode/README.md +16 -16
  134. package/vscode/client/package.json +22 -22
  135. package/vscode/client/src/extension.ts +104 -104
  136. package/vscode/icons/firefly-icon.svg +10 -10
  137. package/vscode/language-configuration.json +61 -61
  138. package/vscode/package-lock.json +3623 -3623
  139. package/vscode/package.json +1 -1
  140. package/vscode/snippets.json +241 -241
  141. package/vscode/syntaxes/firefly-markdown-injection.json +45 -45
  142. package/webserver/.firefly/include/package-lock.json +22 -22
  143. package/webserver/.firefly/include/package.json +5 -5
  144. package/webserver/.firefly/package.ff +2 -2
  145. package/webserver/WebServer.ff +685 -685
  146. package/websocket/.firefly/package.ff +1 -1
  147. package/websocket/WebSocket.ff +131 -131
  148. package/fireflysite/GuideAll.ff +0 -21
  149. package/fireflysite/GuideBaseTypes.ff +0 -168
  150. package/fireflysite/GuideControlFlow.ff +0 -212
  151. package/fireflysite/assets/markdown/Example.md +0 -78
  152. /package/fireflysite/assets/{NotoSansMono-Regular.ttf → font/NotoSansMono-Regular.ttf} +0 -0
  153. /package/fireflysite/assets/{NunitoSans-VariableFont_YTLC,opsz,wdth,wght.ttf → font/NunitoSans-VariableFont_YTLC,opsz,wdth,wght.ttf} +0 -0
  154. /package/fireflysite/assets/{autocomplete-small.png → image/autocomplete-small.png} +0 -0
  155. /package/fireflysite/assets/{autocomplete.png → image/autocomplete.png} +0 -0
  156. /package/fireflysite/assets/{edit-time-error.png → image/edit-time-error.png} +0 -0
  157. /package/fireflysite/assets/{firefly-logo-notext.png → image/firefly-logo-notext.png} +0 -0
  158. /package/fireflysite/assets/{firefly-logo-yellow.png → image/firefly-logo-yellow.png} +0 -0
package/core/Json.ff CHANGED
@@ -1,434 +1,434 @@
1
- data Json {}
2
-
3
- read(json: String): Option[Json]
4
- target js sync """
5
- try {
6
- return ff_core_Option.Some(JSON.parse(json_));
7
- } catch(e) {
8
- return ff_core_Option.None();
9
- }
10
- """
11
-
12
- string(json: String): Json
13
- target js sync """
14
- return json_;
15
- """
16
-
17
- int(json: Int): Json
18
- target js sync """
19
- return json_;
20
- """
21
-
22
- float(json: Float): Json
23
- target js sync """
24
- return json_;
25
- """
26
-
27
- bool(json: Bool): Json
28
- target js sync """
29
- return json_;
30
- """
31
-
32
- null(): Json
33
- target js sync """
34
- return null;
35
- """
36
-
37
- array(json: List[Json]): Json
38
- target js sync """
39
- return json_;
40
- """
41
-
42
- object(): Json
43
- target js sync """
44
- return {};
45
- """
46
-
47
- fields(body: ((String, Json) => Unit) => Unit): Json
48
- target js sync """
49
- const result = {};
50
- body_((k, v) => {result[k] = v});
51
- return result;
52
- """
53
- target js async """
54
- const result = {};
55
- await body_((k, v) => {result[k] = v}, $task);
56
- return result;
57
- """
58
-
59
- extend self: Json {
60
-
61
- write(indentation: Option[String] = None): String
62
- target js sync """
63
- return JSON.stringify(self_, null, indentation_.value_);
64
- """
65
-
66
- with[T: JsonLike](field: String, value: T): Json {
67
- internalWith(self, field, toJson(value))
68
- }
69
-
70
- merge(that: Json): Json
71
- target js sync """
72
- if(typeof self_ !== 'object' || self_ === null || Array.isArray(self_)) {
73
- throw new Error('Not an object: ' + JSON.stringify(self_));
74
- }
75
- if(typeof that_ !== 'object' || that_ === null || Array.isArray(that_)) {
76
- throw new Error('Not an object: ' + JSON.stringify(that_));
77
- }
78
- return {...self_, ...that_};
79
- """
80
-
81
- grabString(): String
82
- target js sync """
83
- if(typeof self_ !== 'string') throw new Error('Not a string: ' + JSON.stringify(self_));
84
- return self_;
85
- """
86
-
87
- grabInt(): Int
88
- target js sync """
89
- if(!Number.isSafeInteger(self_)) throw new Error('Not an int: ' + JSON.stringify(self_));
90
- return Math.trunc(self_);
91
- """
92
-
93
- grabFloat(): Float
94
- target js sync """
95
- if(typeof self_ !== 'number') throw new Error('Not a float: ' + JSON.stringify(self_));
96
- return self_;
97
- """
98
-
99
- grabBool(): Bool
100
- target js sync """
101
- if(self_ === true) return true;
102
- if(self_ === false) return false;
103
- throw new Error('Not a bool: ' + JSON.stringify(self_));
104
- """
105
-
106
- grabArray(): List[Json]
107
- target js sync """
108
- if(!Array.isArray(self_)) throw new Error('Not an array: ' + JSON.stringify(self_));
109
- return self_;
110
- """
111
-
112
- grabMap(): Map[String, Json] {
113
- mutable map = Map.new()
114
- self.each {key, value =>
115
- map = map.add(key, value)
116
- }
117
- map
118
- }
119
-
120
- getString(): Option[String] {
121
- if(self.isString()) {self.grabString()}
122
- }
123
-
124
- getInt(): Option[Int] {
125
- if(self.isInt()) {self.grabInt()}
126
- }
127
-
128
- getFloat(): Option[Float] {
129
- if(self.isFloat()) {self.grabFloat()}
130
- }
131
-
132
- getBool(): Option[Bool] {
133
- if(self.isBool()) {self.grabBool()}
134
- }
135
-
136
- getArray(): Option[List[Json]] {
137
- if(self.isArray()) {self.grabArray()}
138
- }
139
-
140
- getMap(): Option[Map[String, Json]] {
141
- if(self.isObject()) {self.grabMap()}
142
- }
143
-
144
- isString(): Bool
145
- target js sync """
146
- return typeof self_ === 'string';
147
- """
148
-
149
- isInt(): Bool
150
- target js sync """
151
- return Number.isSafeInteger(self_);
152
- """
153
-
154
- isFloat(): Bool
155
- target js sync """
156
- return typeof self_ === 'number';
157
- """
158
-
159
- isBool(): Bool
160
- target js sync """
161
- return typeof self_ === 'boolean';
162
- """
163
-
164
- isArray(): Bool
165
- target js sync """
166
- return Array.isArray(self_);
167
- """
168
-
169
- isObject(): Bool
170
- target js sync """
171
- return typeof self_ === 'object' && self_ !== null && !Array.isArray(self_);
172
- """
173
-
174
- isNull(): Bool
175
- target js sync """
176
- return self_ === null;
177
- """
178
-
179
- field(key: String): Json
180
- target js sync """
181
- return typeof self_ === 'object' && self_ !== null && !Array.isArray(self_) && Object.hasOwn(self_, key_)
182
- ? self_[key_] : null;
183
- """
184
-
185
- index(key: Int): Json
186
- target js sync """
187
- return Array.isArray(self_) ? self_[key] ?? null : null;
188
- """
189
-
190
- hasField(key: String): Bool
191
- target js sync """
192
- return typeof self_ === 'object' && self_ !== null && !Array.isArray(self_) && Object.hasOwn(self_, key_);
193
- """
194
-
195
- getField(key: String): Option[Json]
196
- target js sync """
197
- return typeof self_ === 'object' && self_ !== null && !Array.isArray(self_) && Object.hasOwn(self_, key_)
198
- ? ff_core_Option.Some(self_[key_]) : ff_core_Option.None();
199
- """
200
-
201
- getIndex(key: Int): Option[Json]
202
- target js sync """
203
- return Array.isArray(self_) ? ff_core_Option.Some(self_[key_] ?? null) : ff_core_Option.None();
204
- """
205
-
206
- getFields(): Option[List[String]]
207
- target js sync """
208
- return typeof self_ === 'object' && self_ !== null && !Array.isArray(self_)
209
- ? ff_core_Option.Some(Object.keys(self_)) : ff_core_Option.None();
210
- """
211
-
212
- grabField(key: String): Json {
213
- self.getField(key).else {
214
- throw(GrabException())
215
- }
216
- }
217
-
218
- grabIndex(key: Int): Json {
219
- self.getIndex(key).else {
220
- throw(GrabException())
221
- }
222
- }
223
-
224
- grabFields(): List[String] {
225
- self.getFields().else {
226
- throw(GrabException())
227
- }
228
- }
229
-
230
- map[T](body: (String, Json) => T): List[T] {
231
- let array = Array.new()
232
- self.each {field, value => array.push(body(field, value))}
233
- array.drain()
234
- }
235
-
236
- flatMap[T](body: (String, Json) => List[T]): List[T] {
237
- let array = Array.new()
238
- self.each {field, value => array.pushList(body(field, value))}
239
- array.drain()
240
- }
241
-
242
- each(body: (String, Json) => Unit) {
243
- if(!self.isObject()) {throw(GrabException())}
244
- internalEach(self, body)
245
- }
246
-
247
- eachWhile(body: (String, Json) => Bool) {
248
- if(!self.isObject()) {throw(GrabException())}
249
- internalEachWhile(self, body)
250
- }
251
-
252
- }
253
-
254
- internalWith(self: Json, field: String, value: Json): Json
255
- target js sync """
256
- if(typeof self_ !== 'object' || self_ === null || Array.isArray(self_)) {
257
- throw new Error('Not an object: ' + JSON.stringify(self_));
258
- }
259
- return {...self_, [field_]: value_};
260
- """
261
-
262
- internalEach(self: Json, body: (String, Json) => Unit): Unit
263
- target js sync "for(const [key, value] of Object.entries(self_)) body_(key, value)"
264
- target js async "for(const [key, value] of Object.entries(self_)) await body_(key, value, $task)"
265
-
266
- internalEachWhile(self: Json, body: (String, Json) => Bool): Unit
267
- target js sync "for(const [key, value] of Object.entries(self_)) if(!body_(key, value)) break"
268
- target js async "for(const [key, value] of Object.entries(self_)) if(!await body_(key, value, $task)) break"
269
-
270
- trait T: JsonLike {
271
- toJson(value: T): Json
272
- fromJson(json: Json): Option[T]
273
- }
274
-
275
- instance Json: JsonLike {
276
- toJson(value: Json): Json {value}
277
- fromJson(json: Json): Option[Json] {Some(json)}
278
- }
279
-
280
- instance String: JsonLike {
281
- toJson(value: String): Json {string(value)}
282
- fromJson(json: Json): Option[String] {_.getString()}
283
- }
284
-
285
- instance Int: JsonLike {
286
- toJson(value: Int): Json {int(value)}
287
- fromJson(json: Json): Option[Int] {_.getInt()}
288
- }
289
-
290
- instance Float: JsonLike {
291
- toJson(value: Float): Json {float(value)}
292
- fromJson(json: Json): Option[Float] {_.getFloat()}
293
- }
294
-
295
- instance Bool: JsonLike {
296
- toJson(value: Bool): Json {bool(value)}
297
- fromJson(json: Json): Option[Bool] {_.getBool()}
298
- }
299
-
300
- instance List[T: JsonLike]: JsonLike {
301
- toJson(value: List[T]): Json {array(value.map(toJson))}
302
- fromJson(json: Json): Option[List[T]] {
303
- json.getArray().flatMap {array =>
304
- mutable convertible = True
305
- let result = Array.new()
306
- array.eachWhile {item =>
307
- fromJson(item).{
308
- | None => convertible = False
309
- | Some(value) => result.push(value)
310
- }
311
- convertible
312
- }
313
- if(convertible) {result.drain()}
314
- }
315
- }
316
- }
317
-
318
- instance StringMap[T: JsonLike]: JsonLike {
319
- toJson(value: StringMap[T]): Json {
320
- Json.fields {setField =>
321
- value.each {key, value => setField(key, toJson(value))}
322
- }
323
- }
324
- fromJson(json: Json): Option[StringMap[T]] {
325
- if(json.isObject()) {
326
- let map = StringMap.new()
327
- mutable convertible = True
328
- json.eachWhile {key, value =>
329
- fromJson(value).{
330
- | None => convertible = False
331
- | Some(v) => map.set(key, v)
332
- }
333
- convertible
334
- }
335
- if(convertible) {map}
336
- }.flatten()
337
- }
338
- }
339
-
340
-
341
- instance Json: HasAnyTag {
342
- anyTag(): AnyTag[Json] {Any.internalAnyTag("ff:core/Json.Json[]")}
343
- }
344
-
345
- instance Json: Show {
346
- show(value: Json): String {value.write(Some(" "))}
347
- }
348
-
349
- instance Json: Equal {
350
- equals(a: Json, b: Json): Bool {
351
- internalEquals(a, b)
352
- }
353
- }
354
-
355
- instance Json: Order {
356
- compare(a: Json, b: Json): Ordering {
357
- Ordering.fromInt(internalCompare(a, b))
358
- }
359
- }
360
-
361
- instance Json: Serializable {
362
- serializeUsing(serialization: Serialization, value: Json): Unit {
363
- Serializable.serializeUsing(serialization, value.write())
364
- }
365
- deserializeUsing(serialization: Serialization): Json {
366
- Json.read(Serializable.deserializeUsing[String](serialization)).grab()
367
- }
368
- }
369
-
370
-
371
- internalEquals(a: Json, b: Json): Bool
372
- target js sync """
373
- if(a_ === b_) {
374
- return true;
375
- } else if(Array.isArray(a_) || Array.isArray(b_)) {
376
- if(!Array.isArray(a_) || !Array.isArray(b_)) return false;
377
- if(a_.length !== b_.length) return false;
378
- for(let i = 0; i < a_.length; i++) {
379
- if(!internalEquals_(a_[i], b_[i])) return false;
380
- }
381
- return true;
382
- } else if(typeof a_ === 'object' && typeof b_ === 'object' && a_ !== null && b_ !== null) {
383
- const aKeys = Object.keys(a_);
384
- const bKeys = Object.keys(b_);
385
- if(aKeys.length !== bKeys.length) return false;
386
- for(const key of aKeys) {
387
- if(!Object.hasOwn(b_, key) || !internalEquals_(a_[key], b_[key])) return false;
388
- }
389
- return true;
390
- } else {
391
- return false;
392
- }
393
- """
394
-
395
- internalCompare(a: Json, b: Json): Int
396
- target js sync """
397
- if(a_ === b_) {
398
- return 0;
399
- } else if (a_ === null || b_ === null) {
400
- return a_ === null ? -1 : 1;
401
- } else if (typeof a_ === 'boolean' || typeof b_ === 'boolean') {
402
- if(typeof b_ !== 'boolean') return -1;
403
- if(typeof a_ !== 'boolean') return 1;
404
- return a_ < b_ ? -1 : 1;
405
- } else if (typeof a_ === 'number' || typeof b_ === 'number') {
406
- if(typeof b_ !== 'number') return -1;
407
- if(typeof a_ !== 'number') return 1;
408
- if(isNaN(a_)) return isNaN(b_) ? 0 : -1;
409
- if(isNaN(b_)) return 1;
410
- return a_ < b_ ? -1 : 1;
411
- } else if (typeof a_ === 'string' || typeof b_ === 'string') {
412
- if(typeof b_ !== 'string') return -1;
413
- if(typeof a_ !== 'string') return 1;
414
- return a_.localeCompare(b_, 'en');
415
- } else if(Array.isArray(a_) || Array.isArray(b_)) {
416
- if(!Array.isArray(a_) || !Array.isArray(b_)) return a_ < b_ ? -1 : 1;
417
- const length = Math.min(a_.length, b_.length);
418
- for(let i = 0; i < length; i++) {
419
- const cmp = internalCompare_(a_[i], b_[i]);
420
- if(cmp !== 0) return cmp;
421
- }
422
- return a_.length - b_.length;
423
- } else {
424
- const aKeys = Object.keys(a_).sort();
425
- const bKeys = Object.keys(b_).sort();
426
- const keyResult = internalCompare_(aKeys, bKeys);
427
- if(keyResult !== 0) return keyResult;
428
- for(const key of aKeys) {
429
- const result = internalCompare_(a_[key], b_[key]);
430
- if(result !== 0) return result;
431
- }
432
- return 0;
433
- }
434
- """
1
+ data Json {}
2
+
3
+ read(json: String): Option[Json]
4
+ target js sync """
5
+ try {
6
+ return ff_core_Option.Some(JSON.parse(json_));
7
+ } catch(e) {
8
+ return ff_core_Option.None();
9
+ }
10
+ """
11
+
12
+ string(json: String): Json
13
+ target js sync """
14
+ return json_;
15
+ """
16
+
17
+ int(json: Int): Json
18
+ target js sync """
19
+ return json_;
20
+ """
21
+
22
+ float(json: Float): Json
23
+ target js sync """
24
+ return json_;
25
+ """
26
+
27
+ bool(json: Bool): Json
28
+ target js sync """
29
+ return json_;
30
+ """
31
+
32
+ null(): Json
33
+ target js sync """
34
+ return null;
35
+ """
36
+
37
+ array(json: List[Json]): Json
38
+ target js sync """
39
+ return json_;
40
+ """
41
+
42
+ object(): Json
43
+ target js sync """
44
+ return {};
45
+ """
46
+
47
+ fields(body: ((String, Json) => Unit) => Unit): Json
48
+ target js sync """
49
+ const result = {};
50
+ body_((k, v) => {result[k] = v});
51
+ return result;
52
+ """
53
+ target js async """
54
+ const result = {};
55
+ await body_((k, v) => {result[k] = v}, $task);
56
+ return result;
57
+ """
58
+
59
+ extend self: Json {
60
+
61
+ write(indentation: Option[String] = None): String
62
+ target js sync """
63
+ return JSON.stringify(self_, null, indentation_.value_);
64
+ """
65
+
66
+ with[T: JsonLike](field: String, value: T): Json {
67
+ internalWith(self, field, toJson(value))
68
+ }
69
+
70
+ merge(that: Json): Json
71
+ target js sync """
72
+ if(typeof self_ !== 'object' || self_ === null || Array.isArray(self_)) {
73
+ throw new Error('Not an object: ' + JSON.stringify(self_));
74
+ }
75
+ if(typeof that_ !== 'object' || that_ === null || Array.isArray(that_)) {
76
+ throw new Error('Not an object: ' + JSON.stringify(that_));
77
+ }
78
+ return {...self_, ...that_};
79
+ """
80
+
81
+ grabString(): String
82
+ target js sync """
83
+ if(typeof self_ !== 'string') throw new Error('Not a string: ' + JSON.stringify(self_));
84
+ return self_;
85
+ """
86
+
87
+ grabInt(): Int
88
+ target js sync """
89
+ if(!Number.isSafeInteger(self_)) throw new Error('Not an int: ' + JSON.stringify(self_));
90
+ return Math.trunc(self_);
91
+ """
92
+
93
+ grabFloat(): Float
94
+ target js sync """
95
+ if(typeof self_ !== 'number') throw new Error('Not a float: ' + JSON.stringify(self_));
96
+ return self_;
97
+ """
98
+
99
+ grabBool(): Bool
100
+ target js sync """
101
+ if(self_ === true) return true;
102
+ if(self_ === false) return false;
103
+ throw new Error('Not a bool: ' + JSON.stringify(self_));
104
+ """
105
+
106
+ grabArray(): List[Json]
107
+ target js sync """
108
+ if(!Array.isArray(self_)) throw new Error('Not an array: ' + JSON.stringify(self_));
109
+ return self_;
110
+ """
111
+
112
+ grabMap(): Map[String, Json] {
113
+ mutable map = Map.new()
114
+ self.each {key, value =>
115
+ map = map.add(key, value)
116
+ }
117
+ map
118
+ }
119
+
120
+ getString(): Option[String] {
121
+ if(self.isString()) {self.grabString()}
122
+ }
123
+
124
+ getInt(): Option[Int] {
125
+ if(self.isInt()) {self.grabInt()}
126
+ }
127
+
128
+ getFloat(): Option[Float] {
129
+ if(self.isFloat()) {self.grabFloat()}
130
+ }
131
+
132
+ getBool(): Option[Bool] {
133
+ if(self.isBool()) {self.grabBool()}
134
+ }
135
+
136
+ getArray(): Option[List[Json]] {
137
+ if(self.isArray()) {self.grabArray()}
138
+ }
139
+
140
+ getMap(): Option[Map[String, Json]] {
141
+ if(self.isObject()) {self.grabMap()}
142
+ }
143
+
144
+ isString(): Bool
145
+ target js sync """
146
+ return typeof self_ === 'string';
147
+ """
148
+
149
+ isInt(): Bool
150
+ target js sync """
151
+ return Number.isSafeInteger(self_);
152
+ """
153
+
154
+ isFloat(): Bool
155
+ target js sync """
156
+ return typeof self_ === 'number';
157
+ """
158
+
159
+ isBool(): Bool
160
+ target js sync """
161
+ return typeof self_ === 'boolean';
162
+ """
163
+
164
+ isArray(): Bool
165
+ target js sync """
166
+ return Array.isArray(self_);
167
+ """
168
+
169
+ isObject(): Bool
170
+ target js sync """
171
+ return typeof self_ === 'object' && self_ !== null && !Array.isArray(self_);
172
+ """
173
+
174
+ isNull(): Bool
175
+ target js sync """
176
+ return self_ === null;
177
+ """
178
+
179
+ field(key: String): Json
180
+ target js sync """
181
+ return typeof self_ === 'object' && self_ !== null && !Array.isArray(self_) && Object.hasOwn(self_, key_)
182
+ ? self_[key_] : null;
183
+ """
184
+
185
+ index(key: Int): Json
186
+ target js sync """
187
+ return Array.isArray(self_) ? self_[key] ?? null : null;
188
+ """
189
+
190
+ hasField(key: String): Bool
191
+ target js sync """
192
+ return typeof self_ === 'object' && self_ !== null && !Array.isArray(self_) && Object.hasOwn(self_, key_);
193
+ """
194
+
195
+ getField(key: String): Option[Json]
196
+ target js sync """
197
+ return typeof self_ === 'object' && self_ !== null && !Array.isArray(self_) && Object.hasOwn(self_, key_)
198
+ ? ff_core_Option.Some(self_[key_]) : ff_core_Option.None();
199
+ """
200
+
201
+ getIndex(key: Int): Option[Json]
202
+ target js sync """
203
+ return Array.isArray(self_) ? ff_core_Option.Some(self_[key_] ?? null) : ff_core_Option.None();
204
+ """
205
+
206
+ getFields(): Option[List[String]]
207
+ target js sync """
208
+ return typeof self_ === 'object' && self_ !== null && !Array.isArray(self_)
209
+ ? ff_core_Option.Some(Object.keys(self_)) : ff_core_Option.None();
210
+ """
211
+
212
+ grabField(key: String): Json {
213
+ self.getField(key).else {
214
+ throw(GrabException())
215
+ }
216
+ }
217
+
218
+ grabIndex(key: Int): Json {
219
+ self.getIndex(key).else {
220
+ throw(GrabException())
221
+ }
222
+ }
223
+
224
+ grabFields(): List[String] {
225
+ self.getFields().else {
226
+ throw(GrabException())
227
+ }
228
+ }
229
+
230
+ map[T](body: (String, Json) => T): List[T] {
231
+ let array = Array.new()
232
+ self.each {field, value => array.push(body(field, value))}
233
+ array.drain()
234
+ }
235
+
236
+ flatMap[T](body: (String, Json) => List[T]): List[T] {
237
+ let array = Array.new()
238
+ self.each {field, value => array.pushList(body(field, value))}
239
+ array.drain()
240
+ }
241
+
242
+ each(body: (String, Json) => Unit) {
243
+ if(!self.isObject()) {throw(GrabException())}
244
+ internalEach(self, body)
245
+ }
246
+
247
+ eachWhile(body: (String, Json) => Bool) {
248
+ if(!self.isObject()) {throw(GrabException())}
249
+ internalEachWhile(self, body)
250
+ }
251
+
252
+ }
253
+
254
+ internalWith(self: Json, field: String, value: Json): Json
255
+ target js sync """
256
+ if(typeof self_ !== 'object' || self_ === null || Array.isArray(self_)) {
257
+ throw new Error('Not an object: ' + JSON.stringify(self_));
258
+ }
259
+ return {...self_, [field_]: value_};
260
+ """
261
+
262
+ internalEach(self: Json, body: (String, Json) => Unit): Unit
263
+ target js sync "for(const [key, value] of Object.entries(self_)) body_(key, value)"
264
+ target js async "for(const [key, value] of Object.entries(self_)) await body_(key, value, $task)"
265
+
266
+ internalEachWhile(self: Json, body: (String, Json) => Bool): Unit
267
+ target js sync "for(const [key, value] of Object.entries(self_)) if(!body_(key, value)) break"
268
+ target js async "for(const [key, value] of Object.entries(self_)) if(!await body_(key, value, $task)) break"
269
+
270
+ trait T: JsonLike {
271
+ toJson(value: T): Json
272
+ fromJson(json: Json): Option[T]
273
+ }
274
+
275
+ instance Json: JsonLike {
276
+ toJson(value: Json): Json {value}
277
+ fromJson(json: Json): Option[Json] {Some(json)}
278
+ }
279
+
280
+ instance String: JsonLike {
281
+ toJson(value: String): Json {string(value)}
282
+ fromJson(json: Json): Option[String] {_.getString()}
283
+ }
284
+
285
+ instance Int: JsonLike {
286
+ toJson(value: Int): Json {int(value)}
287
+ fromJson(json: Json): Option[Int] {_.getInt()}
288
+ }
289
+
290
+ instance Float: JsonLike {
291
+ toJson(value: Float): Json {float(value)}
292
+ fromJson(json: Json): Option[Float] {_.getFloat()}
293
+ }
294
+
295
+ instance Bool: JsonLike {
296
+ toJson(value: Bool): Json {bool(value)}
297
+ fromJson(json: Json): Option[Bool] {_.getBool()}
298
+ }
299
+
300
+ instance List[T: JsonLike]: JsonLike {
301
+ toJson(value: List[T]): Json {array(value.map(toJson))}
302
+ fromJson(json: Json): Option[List[T]] {
303
+ json.getArray().flatMap {array =>
304
+ mutable convertible = True
305
+ let result = Array.new()
306
+ array.eachWhile {item =>
307
+ fromJson(item).{
308
+ | None => convertible = False
309
+ | Some(value) => result.push(value)
310
+ }
311
+ convertible
312
+ }
313
+ if(convertible) {result.drain()}
314
+ }
315
+ }
316
+ }
317
+
318
+ instance StringMap[T: JsonLike]: JsonLike {
319
+ toJson(value: StringMap[T]): Json {
320
+ Json.fields {setField =>
321
+ value.each {key, value => setField(key, toJson(value))}
322
+ }
323
+ }
324
+ fromJson(json: Json): Option[StringMap[T]] {
325
+ if(json.isObject()) {
326
+ let map = StringMap.new()
327
+ mutable convertible = True
328
+ json.eachWhile {key, value =>
329
+ fromJson(value).{
330
+ | None => convertible = False
331
+ | Some(v) => map.set(key, v)
332
+ }
333
+ convertible
334
+ }
335
+ if(convertible) {map}
336
+ }.flatten()
337
+ }
338
+ }
339
+
340
+
341
+ instance Json: HasAnyTag {
342
+ anyTag(): AnyTag[Json] {Any.internalAnyTag("ff:core/Json.Json[]")}
343
+ }
344
+
345
+ instance Json: Show {
346
+ show(value: Json): String {value.write(Some(" "))}
347
+ }
348
+
349
+ instance Json: Equal {
350
+ equals(a: Json, b: Json): Bool {
351
+ internalEquals(a, b)
352
+ }
353
+ }
354
+
355
+ instance Json: Order {
356
+ compare(a: Json, b: Json): Ordering {
357
+ Ordering.fromInt(internalCompare(a, b))
358
+ }
359
+ }
360
+
361
+ instance Json: Serializable {
362
+ serializeUsing(serialization: Serialization, value: Json): Unit {
363
+ Serializable.serializeUsing(serialization, value.write())
364
+ }
365
+ deserializeUsing(serialization: Serialization): Json {
366
+ Json.read(Serializable.deserializeUsing[String](serialization)).grab()
367
+ }
368
+ }
369
+
370
+
371
+ internalEquals(a: Json, b: Json): Bool
372
+ target js sync """
373
+ if(a_ === b_) {
374
+ return true;
375
+ } else if(Array.isArray(a_) || Array.isArray(b_)) {
376
+ if(!Array.isArray(a_) || !Array.isArray(b_)) return false;
377
+ if(a_.length !== b_.length) return false;
378
+ for(let i = 0; i < a_.length; i++) {
379
+ if(!internalEquals_(a_[i], b_[i])) return false;
380
+ }
381
+ return true;
382
+ } else if(typeof a_ === 'object' && typeof b_ === 'object' && a_ !== null && b_ !== null) {
383
+ const aKeys = Object.keys(a_);
384
+ const bKeys = Object.keys(b_);
385
+ if(aKeys.length !== bKeys.length) return false;
386
+ for(const key of aKeys) {
387
+ if(!Object.hasOwn(b_, key) || !internalEquals_(a_[key], b_[key])) return false;
388
+ }
389
+ return true;
390
+ } else {
391
+ return false;
392
+ }
393
+ """
394
+
395
+ internalCompare(a: Json, b: Json): Int
396
+ target js sync """
397
+ if(a_ === b_) {
398
+ return 0;
399
+ } else if (a_ === null || b_ === null) {
400
+ return a_ === null ? -1 : 1;
401
+ } else if (typeof a_ === 'boolean' || typeof b_ === 'boolean') {
402
+ if(typeof b_ !== 'boolean') return -1;
403
+ if(typeof a_ !== 'boolean') return 1;
404
+ return a_ < b_ ? -1 : 1;
405
+ } else if (typeof a_ === 'number' || typeof b_ === 'number') {
406
+ if(typeof b_ !== 'number') return -1;
407
+ if(typeof a_ !== 'number') return 1;
408
+ if(isNaN(a_)) return isNaN(b_) ? 0 : -1;
409
+ if(isNaN(b_)) return 1;
410
+ return a_ < b_ ? -1 : 1;
411
+ } else if (typeof a_ === 'string' || typeof b_ === 'string') {
412
+ if(typeof b_ !== 'string') return -1;
413
+ if(typeof a_ !== 'string') return 1;
414
+ return a_.localeCompare(b_, 'en');
415
+ } else if(Array.isArray(a_) || Array.isArray(b_)) {
416
+ if(!Array.isArray(a_) || !Array.isArray(b_)) return a_ < b_ ? -1 : 1;
417
+ const length = Math.min(a_.length, b_.length);
418
+ for(let i = 0; i < length; i++) {
419
+ const cmp = internalCompare_(a_[i], b_[i]);
420
+ if(cmp !== 0) return cmp;
421
+ }
422
+ return a_.length - b_.length;
423
+ } else {
424
+ const aKeys = Object.keys(a_).sort();
425
+ const bKeys = Object.keys(b_).sort();
426
+ const keyResult = internalCompare_(aKeys, bKeys);
427
+ if(keyResult !== 0) return keyResult;
428
+ for(const key of aKeys) {
429
+ const result = internalCompare_(a_[key], b_[key]);
430
+ if(result !== 0) return result;
431
+ }
432
+ return 0;
433
+ }
434
+ """