firefly-compiler 0.4.4
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/.firefly-workspace +1 -0
- package/.vscode/settings.json +5 -0
- package/LICENSE.txt +21 -0
- package/README.md +96 -0
- package/bin/firefly.mjs +2 -0
- package/compiler/.firefly/package.ff +1 -0
- package/compiler/Builder.ff +218 -0
- package/compiler/Compiler.ff +241 -0
- package/compiler/Dependencies.ff +179 -0
- package/compiler/Deriver.ff +647 -0
- package/compiler/Dictionaries.ff +205 -0
- package/compiler/Environment.ff +166 -0
- package/compiler/Inference.ff +1117 -0
- package/compiler/JsEmitter.ff +861 -0
- package/compiler/JsImporter.ff +56 -0
- package/compiler/LspHook.ff +188 -0
- package/compiler/Main.ff +237 -0
- package/compiler/Parser.ff +1383 -0
- package/compiler/Patterns.ff +111 -0
- package/compiler/Resolver.ff +620 -0
- package/compiler/Substitution.ff +178 -0
- package/compiler/Syntax.ff +299 -0
- package/compiler/Token.ff +180 -0
- package/compiler/Tokenizer.ff +278 -0
- package/compiler/Unification.ff +220 -0
- package/compiler/Wildcards.ff +50 -0
- package/compiler/Workspace.ff +88 -0
- package/core/.firefly/package.ff +2 -0
- package/core/Any.ff +30 -0
- package/core/Array.ff +249 -0
- package/core/AssetSystem.ff +61 -0
- package/core/Atomic.ff +64 -0
- package/core/Bool.ff +13 -0
- package/core/BrowserSystem.ff +14 -0
- package/core/Buffer.ff +211 -0
- package/core/BuildSystem.ff +144 -0
- package/core/Channel.ff +131 -0
- package/core/Char.ff +18 -0
- package/core/Core.ff +58 -0
- package/core/Duration.ff +15 -0
- package/core/Equal.ff +52 -0
- package/core/Error.ff +20 -0
- package/core/FileHandle.ff +41 -0
- package/core/Float.ff +41 -0
- package/core/HttpClient.ff +84 -0
- package/core/Instant.ff +9 -0
- package/core/Int.ff +61 -0
- package/core/IntMap.ff +85 -0
- package/core/JsSystem.ff +66 -0
- package/core/JsValue.ff +240 -0
- package/core/List.ff +440 -0
- package/core/Lock.ff +144 -0
- package/core/Log.ff +24 -0
- package/core/Map.ff +126 -0
- package/core/NodeSystem.ff +88 -0
- package/core/Nothing.ff +1 -0
- package/core/Option.ff +133 -0
- package/core/Ordering.ff +157 -0
- package/core/Pair.ff +55 -0
- package/core/Path.ff +393 -0
- package/core/RbMap.ff +216 -0
- package/core/Serializable.ff +173 -0
- package/core/Set.ff +38 -0
- package/core/Show.ff +43 -0
- package/core/Stack.ff +263 -0
- package/core/Stream.ff +406 -0
- package/core/String.ff +175 -0
- package/core/StringMap.ff +85 -0
- package/core/Task.ff +138 -0
- package/core/Try.ff +81 -0
- package/core/Unit.ff +3 -0
- package/experimental/random/AltGeneric.ff +44 -0
- package/experimental/random/Async.ff +68 -0
- package/experimental/random/Buffer2.ff +77 -0
- package/experimental/random/Cat.ff +12 -0
- package/experimental/random/Dictionary.ff +52 -0
- package/experimental/random/Example.ff +46 -0
- package/experimental/random/Generic.ff +102 -0
- package/experimental/random/HappyEyeballs.ff +40 -0
- package/experimental/random/HashMap.ff +72 -0
- package/experimental/random/IfElseUnit.ff +9 -0
- package/experimental/random/InputOutput.ff +23 -0
- package/experimental/random/ListVsArray.ff +45 -0
- package/experimental/random/Main.ff +44 -0
- package/experimental/random/MapTest.ff +67 -0
- package/experimental/random/OldTaskSystem.ff +210 -0
- package/experimental/random/PatternTest.ff +39 -0
- package/experimental/random/Patterns.ff +226 -0
- package/experimental/random/ReadBytesTest.ff +10 -0
- package/experimental/random/RunLength.ff +65 -0
- package/experimental/random/Scrape.ff +51 -0
- package/experimental/random/Serialization.ff +217 -0
- package/experimental/random/SerializationTest.ff +46 -0
- package/experimental/random/Serializer.ff +36 -0
- package/experimental/random/StdInOutErr.ff +4 -0
- package/experimental/random/Symbols.ff +74 -0
- package/experimental/random/Tag.ff +49 -0
- package/experimental/random/Tensor.ff +52 -0
- package/experimental/random/Try.ff +56 -0
- package/experimental/random/Tsv.ff +9 -0
- package/experimental/random/TypesAreModules.ff +87 -0
- package/experimental/random/blueprints/Blueprint.ff +52 -0
- package/experimental/random/blueprints/Main.ff +11 -0
- package/experimental/random/blueprints/Pretty.ff +58 -0
- package/experimental/random/blueprints/User.ff +64 -0
- package/experimental/random/blueprintsystem/BlueprintSystem.ff +48 -0
- package/experimental/random/blueprintsystem/Deserialize.ff +53 -0
- package/experimental/random/blueprintsystem/ReadJs.ff +13 -0
- package/experimental/random/blueprintsystem/User.ff +2 -0
- package/experimental/random/kahrs/Kahrs.ff +112 -0
- package/experimental/random/kahrs/TestKahrs.ff +22 -0
- package/experimental/random/kahrs/TestMap.ff +18 -0
- package/experimental/random/streaming/Gzip.ff +3 -0
- package/experimental/random/streaming/Main.ff +34 -0
- package/experimental/random/streaming/S3Bucket.ff +11 -0
- package/experimental/random/streaming/Tar.ff +5 -0
- package/experimental/rhymeapp/Main.ff +81 -0
- package/experimental/rhymeapp/index.html +14 -0
- package/firefly.sh +5 -0
- package/fireflysite/Main.ff +13 -0
- package/httpserver/.firefly/package.ff +1 -0
- package/httpserver/HttpServer.ff +184 -0
- package/lsp/.firefly/package.ff +1 -0
- package/lsp/CompletionHandler.ff +814 -0
- package/lsp/Handler.ff +551 -0
- package/lsp/HoverHandler.ff +82 -0
- package/lsp/LanguageServer.ff +229 -0
- package/lsp/SignatureHelpHandler.ff +55 -0
- package/lsp/SymbolHandler.ff +167 -0
- package/output/js/ff/compiler/Builder.mjs +483 -0
- package/output/js/ff/compiler/Compiler.mjs +410 -0
- package/output/js/ff/compiler/Dependencies.mjs +388 -0
- package/output/js/ff/compiler/Deriver.mjs +1166 -0
- package/output/js/ff/compiler/Dictionaries.mjs +1305 -0
- package/output/js/ff/compiler/Environment.mjs +1005 -0
- package/output/js/ff/compiler/Inference.mjs +4264 -0
- package/output/js/ff/compiler/JsEmitter.mjs +5353 -0
- package/output/js/ff/compiler/JsImporter.mjs +262 -0
- package/output/js/ff/compiler/LspHook.mjs +789 -0
- package/output/js/ff/compiler/Main.mjs +1695 -0
- package/output/js/ff/compiler/Parser.mjs +4004 -0
- package/output/js/ff/compiler/Patterns.mjs +923 -0
- package/output/js/ff/compiler/Resolver.mjs +2303 -0
- package/output/js/ff/compiler/Substitution.mjs +1146 -0
- package/output/js/ff/compiler/Syntax.mjs +12430 -0
- package/output/js/ff/compiler/Token.mjs +3092 -0
- package/output/js/ff/compiler/Tokenizer.mjs +589 -0
- package/output/js/ff/compiler/Unification.mjs +1748 -0
- package/output/js/ff/compiler/Wildcards.mjs +604 -0
- package/output/js/ff/compiler/Workspace.mjs +683 -0
- package/output/js/ff/core/Any.mjs +139 -0
- package/output/js/ff/core/Array.mjs +594 -0
- package/output/js/ff/core/AssetSystem.mjs +270 -0
- package/output/js/ff/core/Atomic.mjs +186 -0
- package/output/js/ff/core/Bool.mjs +141 -0
- package/output/js/ff/core/BrowserSystem.mjs +122 -0
- package/output/js/ff/core/Buffer.mjs +467 -0
- package/output/js/ff/core/BuildSystem.mjs +320 -0
- package/output/js/ff/core/Channel.mjs +268 -0
- package/output/js/ff/core/Char.mjs +145 -0
- package/output/js/ff/core/Core.mjs +300 -0
- package/output/js/ff/core/Duration.mjs +112 -0
- package/output/js/ff/core/Equal.mjs +175 -0
- package/output/js/ff/core/Error.mjs +138 -0
- package/output/js/ff/core/FileHandle.mjs +164 -0
- package/output/js/ff/core/Float.mjs +214 -0
- package/output/js/ff/core/HttpClient.mjs +210 -0
- package/output/js/ff/core/Instant.mjs +105 -0
- package/output/js/ff/core/Int.mjs +254 -0
- package/output/js/ff/core/IntMap.mjs +282 -0
- package/output/js/ff/core/JsSystem.mjs +234 -0
- package/output/js/ff/core/JsValue.mjs +678 -0
- package/output/js/ff/core/List.mjs +2335 -0
- package/output/js/ff/core/Lock.mjs +322 -0
- package/output/js/ff/core/Log.mjs +159 -0
- package/output/js/ff/core/Map.mjs +358 -0
- package/output/js/ff/core/NodeSystem.mjs +288 -0
- package/output/js/ff/core/Nothing.mjs +100 -0
- package/output/js/ff/core/Option.mjs +1002 -0
- package/output/js/ff/core/Ordering.mjs +734 -0
- package/output/js/ff/core/Pair.mjs +318 -0
- package/output/js/ff/core/Path.mjs +768 -0
- package/output/js/ff/core/RbMap.mjs +1936 -0
- package/output/js/ff/core/Serializable.mjs +434 -0
- package/output/js/ff/core/Set.mjs +250 -0
- package/output/js/ff/core/Show.mjs +201 -0
- package/output/js/ff/core/Stack.mjs +595 -0
- package/output/js/ff/core/Stream.mjs +1300 -0
- package/output/js/ff/core/String.mjs +433 -0
- package/output/js/ff/core/StringMap.mjs +282 -0
- package/output/js/ff/core/Task.mjs +345 -0
- package/output/js/ff/core/Try.mjs +503 -0
- package/output/js/ff/core/Unit.mjs +103 -0
- package/package.json +29 -0
- package/postgresql/.firefly/include/package-lock.json +250 -0
- package/postgresql/.firefly/include/package.json +5 -0
- package/postgresql/.firefly/include/prepare.sh +2 -0
- package/postgresql/.firefly/package.ff +3 -0
- package/postgresql/Pg.ff +530 -0
- package/unsafejs/.firefly/package.ff +1 -0
- package/unsafejs/UnsafeJs.ff +19 -0
- package/vscode/.vscode/launch.json +18 -0
- package/vscode/.vscode/tasks.json +33 -0
- package/vscode/LICENSE.txt +21 -0
- package/vscode/Prepublish.ff +15 -0
- package/vscode/README.md +17 -0
- package/vscode/client/package-lock.json +544 -0
- package/vscode/client/package.json +22 -0
- package/vscode/client/src/extension.ts +64 -0
- package/vscode/client/tsconfig.json +12 -0
- package/vscode/icons/firefly-icon.png +0 -0
- package/vscode/icons/firefly-icon.svg +10 -0
- package/vscode/icons/firefly-logo-notext.png +0 -0
- package/vscode/icons/firefly-logo.png +0 -0
- package/vscode/language-configuration.json +39 -0
- package/vscode/package-lock.json +3623 -0
- package/vscode/package.json +144 -0
- package/vscode/snippets-none.json +1 -0
- package/vscode/snippets.json +241 -0
- package/vscode/syntaxes/firefly.tmLanguage.json +294 -0
- package/vscode/tsconfig.json +20 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
data HashMap[K, V] {}
|
|
2
|
+
|
|
3
|
+
empty() { panic("magic") }
|
|
4
|
+
target js sync "return new Map()"
|
|
5
|
+
|
|
6
|
+
extend self: HashMap[K, V] {
|
|
7
|
+
|
|
8
|
+
add(key: K, value: V): Unit { panic("magic") }
|
|
9
|
+
javascript """
|
|
10
|
+
if(typeof key !== "object") {
|
|
11
|
+
self_.set(key, value)
|
|
12
|
+
} else {
|
|
13
|
+
throw new Error('Key not hashable: ' + key)
|
|
14
|
+
}
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
get(key: K): Option[V] { panic("magic") }
|
|
18
|
+
javascript """
|
|
19
|
+
if(typeof key !== "object") {
|
|
20
|
+
return self_.has(key) ? ff_core_Option.Some(self_.get(key)) : ff_core_Option.None()
|
|
21
|
+
} else {
|
|
22
|
+
throw new Error('Key not hashable: ' + key)
|
|
23
|
+
}
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
remove(key: K): Bool { panic("magic") }
|
|
27
|
+
javascript """
|
|
28
|
+
if(typeof key !== "object") {
|
|
29
|
+
return self_.delete(key)
|
|
30
|
+
} else {
|
|
31
|
+
throw new Error('Key not hashable: ' + key)
|
|
32
|
+
}
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
contains(key: K): Bool { panic("magic") }
|
|
36
|
+
javascript """
|
|
37
|
+
if(typeof key !== "object") {
|
|
38
|
+
return self_.has(key)
|
|
39
|
+
} else {
|
|
40
|
+
throw new Error('Key not hashable: ' + key)
|
|
41
|
+
}
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
each(body: (K, V) => Unit): Unit { panic("magic") }
|
|
45
|
+
javascript """
|
|
46
|
+
self_.forEach(body)
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
pairs(): List[Pair[K, V]] {
|
|
50
|
+
let list = []
|
|
51
|
+
self.each { k, v => list = [Pair(k, v), ...list] }
|
|
52
|
+
list.reverse()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
keys(): Set[K] {
|
|
56
|
+
let keys = Set.empty()
|
|
57
|
+
self.each { k, v => keys = keys.add(k, v) }
|
|
58
|
+
keys
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
values(): List[K] {
|
|
62
|
+
let list = []
|
|
63
|
+
self.each { k, v => list = [v, ...list] }
|
|
64
|
+
list.reverse()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
clear(): Unit { panic("magic") }
|
|
68
|
+
javascript """
|
|
69
|
+
self_.clear()
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
capability Writer(
|
|
2
|
+
writeBuffer: Buffer => Unit
|
|
3
|
+
)
|
|
4
|
+
|
|
5
|
+
extend self: Writer {
|
|
6
|
+
|
|
7
|
+
writeStream(stream: Stream[Buffer]): Unit {
|
|
8
|
+
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
writeText(text: String, encoding: String = "utf8"): Unit {
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
main(system: NodeSystem) {
|
|
18
|
+
let in: Stream[Buffer] = system.in
|
|
19
|
+
system.out.writeStream(in)
|
|
20
|
+
system.err.writeStream(in)
|
|
21
|
+
system.out.writeText("Hello")
|
|
22
|
+
system.err.writeText("Hello")
|
|
23
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
main(system: NodeSystem) {
|
|
2
|
+
|
|
3
|
+
function logDuration[T](label: String, body: () => T): T {
|
|
4
|
+
let started = system.time().elapsed()
|
|
5
|
+
let result = body()
|
|
6
|
+
Log.debug(label + ": " + Show.show(Duration(system.time().elapsed().seconds - started.seconds)))
|
|
7
|
+
result
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function test() {
|
|
11
|
+
let array = Array.fillBy(1000) {_}
|
|
12
|
+
let list = array.toList()
|
|
13
|
+
logDuration("array.map") {
|
|
14
|
+
mutable i = 0
|
|
15
|
+
while {i < 1000} {
|
|
16
|
+
array.map {_ + 1}
|
|
17
|
+
i += 1
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
logDuration("list.map") {
|
|
21
|
+
mutable i = 0
|
|
22
|
+
while {i < 1000} {
|
|
23
|
+
list.map {_ + 1}
|
|
24
|
+
i += 1
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
logDuration("array.sort") {
|
|
28
|
+
mutable i = 0
|
|
29
|
+
while {i < 1000} {
|
|
30
|
+
array.sort()
|
|
31
|
+
i += 1
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
logDuration("list.sort") {
|
|
35
|
+
mutable i = 0
|
|
36
|
+
while {i < 1000} {
|
|
37
|
+
list.sort()
|
|
38
|
+
i += 1
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
test()
|
|
44
|
+
|
|
45
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
data Person(
|
|
2
|
+
email: String
|
|
3
|
+
name: String
|
|
4
|
+
born: Int
|
|
5
|
+
)
|
|
6
|
+
|
|
7
|
+
main(system: NodeSystem): Unit {
|
|
8
|
+
Log.show([
|
|
9
|
+
Pair(4, 2)
|
|
10
|
+
Pair(3, 4)
|
|
11
|
+
].toMap())
|
|
12
|
+
Log.show([1, 2, 3].flatMap {x => [x, x * 2]})
|
|
13
|
+
Log.show([6, 4, 36, 8, 9, 5, [4, 2], 12, 2, 34, 4, 5, 65].toSet().toStream().toList())
|
|
14
|
+
Log.show([6, 4, 36, 8, 9, 5, 4, 2, 12, 2, 34, 4, 5, 65].toSet().toList())
|
|
15
|
+
let people = [Person("joe@example.com", "Joe", 1970), Person("jane@example.com", "Jane", 1977)].toArray()
|
|
16
|
+
let sortedPeople = people.sortBy {_.born}
|
|
17
|
+
|
|
18
|
+
Log.show(sortedPeople)
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
throw(people.grabFirst())
|
|
22
|
+
} catch {| Person p, error =>
|
|
23
|
+
Log.debug(p.email)
|
|
24
|
+
} finally {
|
|
25
|
+
Log.debug("bar")
|
|
26
|
+
} grab()
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
Log.debug("foo")
|
|
30
|
+
} finally {
|
|
31
|
+
Log.debug("bar")
|
|
32
|
+
} grab()
|
|
33
|
+
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
returnStuff(): Int {
|
|
37
|
+
try {
|
|
38
|
+
7
|
|
39
|
+
} catch {| Person p, error =>
|
|
40
|
+
p.born
|
|
41
|
+
} finally {
|
|
42
|
+
Log.debug("quux")
|
|
43
|
+
} grab()
|
|
44
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
main(system: NodeSystem): Unit {
|
|
2
|
+
testMap(system)
|
|
3
|
+
testIntMap(system)
|
|
4
|
+
testStringMap(system)
|
|
5
|
+
testStack(system)
|
|
6
|
+
testRawMap()
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
testStringMap(system: NodeSystem) {
|
|
10
|
+
let measurement = system.time().measure {
|
|
11
|
+
let map = StringMap.make()
|
|
12
|
+
mutable i = 0
|
|
13
|
+
while {i < 1000000} {
|
|
14
|
+
map.set("" + i, i)
|
|
15
|
+
i += 1
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
Log.debug("StringMap: " + measurement.second.show())
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
testIntMap(system: NodeSystem) {
|
|
22
|
+
let measurement = system.time().measure {
|
|
23
|
+
let map = IntMap.make()
|
|
24
|
+
mutable i = 0
|
|
25
|
+
while {i < 1000000} {
|
|
26
|
+
map.set(i, i)
|
|
27
|
+
i += 1
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
Log.debug("IntMap: " + measurement.second.show())
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
testMap(system: NodeSystem) {
|
|
34
|
+
let measurement = system.time().measure {
|
|
35
|
+
mutable map = [].toMap()
|
|
36
|
+
mutable i = 0
|
|
37
|
+
while {i < 1000000} {
|
|
38
|
+
map = map.add(i, i)
|
|
39
|
+
i += 1
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
Log.debug("Map: " + measurement.second.show())
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
testStack(system: NodeSystem) {
|
|
46
|
+
let measurement = system.time().measure {
|
|
47
|
+
let map = [].toStack()
|
|
48
|
+
mutable i = 0
|
|
49
|
+
while {i < 1000000} {
|
|
50
|
+
map.set(i, i)
|
|
51
|
+
i += 1
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
Log.debug("Stack: " + measurement.second.show())
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
testRawMap()
|
|
58
|
+
target js sync """
|
|
59
|
+
const start = performance.now();
|
|
60
|
+
const map = new Map();
|
|
61
|
+
let i = 0;
|
|
62
|
+
while(i < 1000000) {
|
|
63
|
+
map.set(i, i);
|
|
64
|
+
i += 1
|
|
65
|
+
}
|
|
66
|
+
console.log("RawMap: " + (performance.now() - start) + " ms")
|
|
67
|
+
"""
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
capability TaskSystem {}
|
|
2
|
+
|
|
3
|
+
extend self: TaskSystem {
|
|
4
|
+
|
|
5
|
+
start[T](task: () => T): () => T { panic("magic") }
|
|
6
|
+
javascriptasync """
|
|
7
|
+
const promise = Promise.resolve().then(() => {
|
|
8
|
+
try {
|
|
9
|
+
if(self_.controller.signal.aborted) throw self_.controller.signal.reason
|
|
10
|
+
return task_(self_.controller.signal)
|
|
11
|
+
} catch(e) {
|
|
12
|
+
if(self_.error == null) self_.error = e
|
|
13
|
+
throw e
|
|
14
|
+
} finally {
|
|
15
|
+
self_.promises.delete(promise)
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
self_.promises.add(promise)
|
|
19
|
+
return async (signal) => {
|
|
20
|
+
if(signal.aborted) throw signal.reason
|
|
21
|
+
let abort
|
|
22
|
+
let cancelPromise = new Promise((_, reject) => abort = () => reject(signal.reason))
|
|
23
|
+
signal.addEventListener('abort', abort)
|
|
24
|
+
try {
|
|
25
|
+
await Promise.race([
|
|
26
|
+
promise,
|
|
27
|
+
cancelPromise
|
|
28
|
+
])
|
|
29
|
+
} finally {
|
|
30
|
+
signal.removeEventListener('abort', abort)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
cancel(): Unit { panic("magic") }
|
|
36
|
+
javascriptasync """
|
|
37
|
+
self_.controller.abort()
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
scope[T](body: TaskSystem => T): T { panic("magic") }
|
|
41
|
+
javascriptasync """
|
|
42
|
+
if(self_.controller.signal.aborted) throw self_.controller.signal.reason
|
|
43
|
+
let controller = new AbortController()
|
|
44
|
+
let abort = () => controller.abort(self_.controller.signal.reason)
|
|
45
|
+
self_.controller.signal.addEventListener('abort', abort)
|
|
46
|
+
let result = undefined
|
|
47
|
+
let newTaskSystem = {promises: new Set(), controller: controller, error: null}
|
|
48
|
+
try {
|
|
49
|
+
result = await body_(newTaskSystem, newTaskSystem.controller.signal)
|
|
50
|
+
} finally {
|
|
51
|
+
self_.controller.signal.removeEventListener('abort', abort)
|
|
52
|
+
await Promise.allSettled(newTaskSystem.promises)
|
|
53
|
+
}
|
|
54
|
+
if(self_.error != null) throw e
|
|
55
|
+
return result
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
sleep(duration: Duration): Unit { panic("magic") }
|
|
59
|
+
javascriptasync """
|
|
60
|
+
if(self_.controller.signal.aborted) throw self_.controller.signal.reason
|
|
61
|
+
await new Promise((resolve, reject) => {
|
|
62
|
+
let abort = () => {
|
|
63
|
+
self_.controller.signal.removeEventListener('abort', abort)
|
|
64
|
+
if(timeoutId != null) clearTimeout(timeoutId)
|
|
65
|
+
reject(self_.controller.signal.reason)
|
|
66
|
+
}
|
|
67
|
+
self_.controller.signal.addEventListener('abort', abort)
|
|
68
|
+
let complete = () => {
|
|
69
|
+
self_.controller.signal.removeEventListener('abort', abort)
|
|
70
|
+
resolve()
|
|
71
|
+
}
|
|
72
|
+
let timeoutId = setTimeout(complete, duration_ * 1000);
|
|
73
|
+
})
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
yield(): Unit { panic("magic") }
|
|
77
|
+
javascriptasync """
|
|
78
|
+
if(self_.controller.signal.aborted) throw self_.controller.signal.reason
|
|
79
|
+
await Promise.resolve().then(() => {})
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
channel[T](buffer: Int = 0): Pair[() => T, T => Unit] { panic("magic") }
|
|
83
|
+
javascriptasync """
|
|
84
|
+
let readers = new Set()
|
|
85
|
+
let writers = new Set()
|
|
86
|
+
async function read(signal) {
|
|
87
|
+
if(self_.controller.signal.aborted) throw self_.controller.signal.reason
|
|
88
|
+
if(signal.aborted) throw signal.reason
|
|
89
|
+
if(writers.size != 0) {
|
|
90
|
+
let writer = writers.values().next().value
|
|
91
|
+
writers.delete(writer)
|
|
92
|
+
writer.resolve()
|
|
93
|
+
return writer.value
|
|
94
|
+
} else {
|
|
95
|
+
let reader
|
|
96
|
+
let abort
|
|
97
|
+
let promise = new Promise((resolve, reject) => {
|
|
98
|
+
reader = resolve
|
|
99
|
+
abort = () => reject(self_.controller.signal.reason || signal.reason)
|
|
100
|
+
})
|
|
101
|
+
readers.add(reader)
|
|
102
|
+
self_.controller.signal.addEventListener('abort', abort)
|
|
103
|
+
signal.addEventListener('abort', abort)
|
|
104
|
+
try {
|
|
105
|
+
return await promise
|
|
106
|
+
} finally {
|
|
107
|
+
readers.remove(reader)
|
|
108
|
+
self_.controller.signal.removeEventListener('abort', abort)
|
|
109
|
+
signal.removeEventListener('abort', abort)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function write(value, signal) {
|
|
114
|
+
if(self_.controller.signal.aborted) throw self_.controller.signal.reason
|
|
115
|
+
if(signal.aborted) throw signal.reason
|
|
116
|
+
if(readers.size != 0) {
|
|
117
|
+
let reader = readers.values().next().value
|
|
118
|
+
readers.delete(reader)
|
|
119
|
+
reader.resolve(value)
|
|
120
|
+
} else {
|
|
121
|
+
let send
|
|
122
|
+
let abort
|
|
123
|
+
let promise = new Promise((resolve, reject) => {
|
|
124
|
+
send = resolve
|
|
125
|
+
abort = () => reject(self_.controller.signal.reason || signal.reason)
|
|
126
|
+
})
|
|
127
|
+
let writer = {value, resolve: send}
|
|
128
|
+
writers.add(writer)
|
|
129
|
+
if(writers.size > buffer) {
|
|
130
|
+
self_.controller.signal.addEventListener('abort', abort)
|
|
131
|
+
signal.addEventListener('abort', abort)
|
|
132
|
+
try {
|
|
133
|
+
await promise
|
|
134
|
+
} finally {
|
|
135
|
+
writers.remove(writer)
|
|
136
|
+
self_.controller.signal.removeEventListener('abort', abort)
|
|
137
|
+
signal.removeEventListener('abort', abort)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return {first_: read, second_: write}
|
|
143
|
+
"""
|
|
144
|
+
|
|
145
|
+
race[T](tasks: List[() => T]): T { panic("magic") }
|
|
146
|
+
javascriptasync """
|
|
147
|
+
if(self_.controller.signal.aborted) throw self_.controller.signal.reason
|
|
148
|
+
let controller = new AbortController()
|
|
149
|
+
let abort = () => controller.abort(self_.controller.signal.reason)
|
|
150
|
+
self_.controller.signal.addEventListener('abort', abort)
|
|
151
|
+
let promises = []
|
|
152
|
+
try {
|
|
153
|
+
ff_core_List.List_toArray(tasks_).forEach(f =>
|
|
154
|
+
promises.push(Promise.resolve(controller.signal).then(f))
|
|
155
|
+
)
|
|
156
|
+
return await Promise.race(promises)
|
|
157
|
+
} finally {
|
|
158
|
+
self_.controller.signal.removeEventListener('abort', abort)
|
|
159
|
+
controller.abort()
|
|
160
|
+
await Promise.allSettled(promises)
|
|
161
|
+
}
|
|
162
|
+
"""
|
|
163
|
+
|
|
164
|
+
all[T](tasks: List[() => T]): List[T] { panic("magic") }
|
|
165
|
+
javascriptasync """
|
|
166
|
+
if(self_.controller.signal.aborted) throw self_.controller.signal.reason
|
|
167
|
+
let controller = new AbortController()
|
|
168
|
+
let abort = () => controller.abort(self_.controller.signal.reason)
|
|
169
|
+
self_.controller.signal.addEventListener('abort', abort)
|
|
170
|
+
let promises = []
|
|
171
|
+
try {
|
|
172
|
+
ff_core_List.List_toArray(tasks_).forEach(f =>
|
|
173
|
+
promises.push(Promise.resolve(controller.signal).then(f))
|
|
174
|
+
)
|
|
175
|
+
let array = await Promise.all(promises)
|
|
176
|
+
return ff_core_Array.Array_toList(array)
|
|
177
|
+
} finally {
|
|
178
|
+
self_.controller.signal.removeEventListener('abort', abort)
|
|
179
|
+
controller.abort()
|
|
180
|
+
await Promise.allSettled(promises)
|
|
181
|
+
}
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
both[A, B](task1: () => A, task2: () => B): Pair[A, B] { panic("magic") }
|
|
185
|
+
javascriptasync """
|
|
186
|
+
if(self_.controller.signal.aborted) throw self_.controller.signal.reason
|
|
187
|
+
let controller = new AbortController()
|
|
188
|
+
let abort = () => controller.abort(self_.controller.signal.reason)
|
|
189
|
+
self_.controller.signal.addEventListener('abort', abort)
|
|
190
|
+
let promises = []
|
|
191
|
+
try {
|
|
192
|
+
promises.push(Promise.resolve(controller.signal).then(task1_))
|
|
193
|
+
promises.push(Promise.resolve(controller.signal).then(task2_))
|
|
194
|
+
let array = await Promise.all(promises)
|
|
195
|
+
return {first_: array[0], second_: array[1]}
|
|
196
|
+
} finally {
|
|
197
|
+
self_.controller.signal.removeEventListener('abort', abort)
|
|
198
|
+
controller.abort()
|
|
199
|
+
await Promise.allSettled(promises)
|
|
200
|
+
}
|
|
201
|
+
"""
|
|
202
|
+
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
extend self: TaskSystem {
|
|
207
|
+
start[T](task: () => Unit): Task
|
|
208
|
+
channel[T](capacity: Int = 0): Channel[T]
|
|
209
|
+
sleep(duration: Duration): Unit
|
|
210
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/*test1(foo: Option[Int], bar: Option[Int]): Option[Int] {
|
|
2
|
+
| a, b => b
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
test2(foo: Option[Int], bar: Option[Int]): Option[Int] {
|
|
6
|
+
function f(baz: Option[Int], quux: Option[Int]): Option[Int] {
|
|
7
|
+
| Some(_), b => b
|
|
8
|
+
| None, _ => None
|
|
9
|
+
}
|
|
10
|
+
f(foo, bar)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
data Moo(x: Int)
|
|
14
|
+
|
|
15
|
+
extend self: Moo {
|
|
16
|
+
test3(moo: Moo): Moo {
|
|
17
|
+
| m => m
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
*/
|
|
21
|
+
data Color {
|
|
22
|
+
R
|
|
23
|
+
B
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
data RB[K, V] {
|
|
27
|
+
E
|
|
28
|
+
T(color : Color, left: RB[K, V], key: K, value: V)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test4[K, V](left: RB[K, V], key: K): RB[K, V] {
|
|
32
|
+
| T(_, _, _, _), y => E
|
|
33
|
+
| a, y => E
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
nodeMain(main: NodeSystem) {
|
|
37
|
+
//Log.debug(Moo(7).test3(Moo(8)))
|
|
38
|
+
//Log.debug(test3())
|
|
39
|
+
}
|