firefly-compiler 0.4.40 → 0.4.48

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 (109) hide show
  1. package/.hintrc +4 -4
  2. package/.vscode/settings.json +4 -4
  3. package/bin/Release.ff +136 -71
  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 +186 -186
  8. package/compiler/DependencyLock.ff +17 -17
  9. package/compiler/JsEmitter.ff +946 -946
  10. package/compiler/LspHook.ff +202 -202
  11. package/compiler/ModuleCache.ff +178 -178
  12. package/compiler/Workspace.ff +88 -88
  13. package/core/.firefly/include/package-lock.json +394 -394
  14. package/core/.firefly/include/package.json +5 -5
  15. package/core/.firefly/include/prepare.sh +1 -1
  16. package/core/.firefly/package.ff +2 -2
  17. package/core/Array.ff +265 -265
  18. package/core/Atomic.ff +64 -64
  19. package/core/Box.ff +7 -7
  20. package/core/BrowserSystem.ff +40 -40
  21. package/core/BuildSystem.ff +148 -148
  22. package/core/Crypto.ff +96 -102
  23. package/core/Equal.ff +36 -36
  24. package/core/HttpClient.ff +87 -87
  25. package/core/JsSystem.ff +69 -69
  26. package/core/Json.ff +434 -434
  27. package/core/List.ff +486 -415
  28. package/core/Lock.ff +144 -144
  29. package/core/NodeSystem.ff +189 -189
  30. package/core/Ordering.ff +161 -161
  31. package/core/Path.ff +401 -401
  32. package/core/Random.ff +134 -134
  33. package/core/RbMap.ff +216 -216
  34. package/core/Show.ff +43 -43
  35. package/core/SourceLocation.ff +68 -68
  36. package/core/Task.ff +141 -141
  37. package/experimental/benchmarks/ListGrab.ff +23 -23
  38. package/experimental/benchmarks/ListGrab.java +55 -55
  39. package/experimental/benchmarks/Pyrotek45.ff +30 -30
  40. package/experimental/benchmarks/Pyrotek45.java +64 -64
  41. package/experimental/bidirectional/Bidi.ff +88 -88
  42. package/experimental/random/Index.ff +53 -53
  43. package/experimental/random/Process.ff +120 -120
  44. package/experimental/random/Scrape.ff +51 -51
  45. package/experimental/random/Symbols.ff +73 -73
  46. package/experimental/random/Tensor.ff +52 -52
  47. package/experimental/random/Units.ff +36 -36
  48. package/experimental/s3/S3TestAuthorizationHeader.ff +38 -38
  49. package/experimental/s3/S3TestPut.ff +15 -15
  50. package/experimental/tests/TestJson.ff +26 -26
  51. package/firefly.sh +0 -0
  52. package/fireflysite/Main.ff +13 -13
  53. package/lsp/.firefly/package.ff +1 -1
  54. package/lsp/CompletionHandler.ff +808 -811
  55. package/lsp/Handler.ff +714 -714
  56. package/lsp/HoverHandler.ff +79 -79
  57. package/lsp/LanguageServer.ff +272 -272
  58. package/lsp/SignatureHelpHandler.ff +55 -55
  59. package/lsp/SymbolHandler.ff +181 -181
  60. package/lsp/TestReferences.ff +16 -16
  61. package/lsp/TestReferencesCase.ff +7 -7
  62. package/lsp/stderr.txt +1 -1
  63. package/lsp/stdin.txt +10 -10
  64. package/lsp/stdout.txt +40 -40
  65. package/lux/.firefly/package.ff +1 -1
  66. package/lux/Css.ff +648 -648
  67. package/lux/CssTest.ff +48 -48
  68. package/lux/Lux.ff +487 -487
  69. package/lux/LuxEvent.ff +116 -116
  70. package/lux/Main.ff +128 -128
  71. package/lux/Main2.ff +144 -144
  72. package/output/js/ff/compiler/Builder.mjs +43 -43
  73. package/output/js/ff/compiler/Dependencies.mjs +3 -3
  74. package/output/js/ff/core/Array.mjs +59 -59
  75. package/output/js/ff/core/Atomic.mjs +36 -36
  76. package/output/js/ff/core/BrowserSystem.mjs +11 -11
  77. package/output/js/ff/core/BuildSystem.mjs +30 -30
  78. package/output/js/ff/core/Crypto.mjs +58 -62
  79. package/output/js/ff/core/HttpClient.mjs +24 -24
  80. package/output/js/ff/core/Json.mjs +147 -147
  81. package/output/js/ff/core/List.mjs +196 -50
  82. package/output/js/ff/core/Lock.mjs +97 -97
  83. package/output/js/ff/core/NodeSystem.mjs +77 -77
  84. package/output/js/ff/core/Ordering.mjs +8 -8
  85. package/output/js/ff/core/Path.mjs +231 -231
  86. package/output/js/ff/core/Random.mjs +56 -56
  87. package/output/js/ff/core/Task.mjs +31 -31
  88. package/package.json +29 -29
  89. package/rpc/.firefly/package.ff +1 -1
  90. package/rpc/Rpc.ff +69 -69
  91. package/s3/.firefly/package.ff +1 -1
  92. package/s3/S3.ff +93 -93
  93. package/unsafejs/UnsafeJs.ff +19 -19
  94. package/vscode/LICENSE.txt +21 -21
  95. package/vscode/Prepublish.ff +15 -15
  96. package/vscode/README.md +16 -16
  97. package/vscode/client/package.json +22 -22
  98. package/vscode/client/src/extension.ts +104 -104
  99. package/vscode/icons/firefly-icon.svg +10 -10
  100. package/vscode/language-configuration.json +61 -61
  101. package/vscode/package-lock.json +3623 -3623
  102. package/vscode/package.json +160 -160
  103. package/vscode/snippets.json +241 -241
  104. package/webserver/.firefly/include/package-lock.json +16 -16
  105. package/webserver/.firefly/include/package.json +5 -5
  106. package/webserver/.firefly/package.ff +2 -2
  107. package/webserver/WebServer.ff +685 -685
  108. package/websocket/.firefly/package.ff +1 -1
  109. package/websocket/WebSocket.ff +131 -131
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 typeof self_ === 'array' ? 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 typeof self_ === 'array' ? 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 typeof self_ === 'array' ? 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 typeof self_ === 'array' ? 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
+ """