@effekt-lang/effekt 0.83.0 → 0.84.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/effekt +0 -0
- package/libraries/chez/common/effekt_primitives.ss +9 -0
- package/libraries/common/array.effekt +3 -1
- package/libraries/common/bench.effekt +0 -5
- package/libraries/common/bytearray.effekt +12 -6
- package/libraries/common/effekt.effekt +49 -7
- package/libraries/common/heap.effekt +100 -90
- package/libraries/common/io/console.effekt +3 -3
- package/libraries/common/io/error.effekt +4 -4
- package/libraries/common/io/filesystem.effekt +42 -32
- package/libraries/common/io/network.effekt +170 -83
- package/libraries/common/io/signal.effekt +5 -4
- package/libraries/common/io.effekt +8 -0
- package/libraries/common/process.effekt +88 -82
- package/libraries/common/ref.effekt +8 -5
- package/libraries/js/effekt_builtins.js +9 -0
- package/libraries/llvm/bytearray.c +3 -3
- package/libraries/llvm/io.c +201 -135
- package/libraries/llvm/rts.ll +231 -79
- package/libraries/llvm/types.c +3 -2
- package/licenses/eclipse public license, version 2.0 - epl-2.0.html +3 -3
- package/licenses/mit license - mit-license.html +79 -32
- package/licenses/the bsd license - bsd-license.html +79 -32
- package/licenses/the mit license - mit.html +79 -32
- package/package.json +1 -1
package/bin/effekt
CHANGED
|
Binary file
|
|
@@ -133,6 +133,15 @@
|
|
|
133
133
|
(define (int-quotient x y)
|
|
134
134
|
(if (fx= y 0) (quotient x y) (fxquotient x y)))
|
|
135
135
|
|
|
136
|
+
(define (int-remainder x y)
|
|
137
|
+
(if (fx= y 0) (remainder x y) (fxremainder x y)))
|
|
138
|
+
|
|
139
|
+
(define (int-div x y)
|
|
140
|
+
(if (fx= y 0) (div x y) (fxdiv x y)))
|
|
141
|
+
|
|
142
|
+
(define (int-mod x y)
|
|
143
|
+
(if (fx= y 0) (mod x y) (fxmod x y)))
|
|
144
|
+
|
|
136
145
|
(define (relative-timestamp)
|
|
137
146
|
(let ([t (current-time 'time-monotonic)])
|
|
138
147
|
(+ (* (time-second t) 1000000000) (time-nanosecond t))))
|
|
@@ -22,8 +22,9 @@ extern type Array[T] =
|
|
|
22
22
|
/// +--------------+------+------------+
|
|
23
23
|
/// ```
|
|
24
24
|
extern llvm """
|
|
25
|
-
define void @array_erase_fields(ptr %
|
|
25
|
+
define void @array_erase_fields(ptr %object) {
|
|
26
26
|
entry:
|
|
27
|
+
%array_pointer = getelementptr %Header, ptr %object, i64 1
|
|
27
28
|
%data_pointer = getelementptr inbounds i64, ptr %array_pointer, i64 1
|
|
28
29
|
%size = load i64, ptr %array_pointer, align 8
|
|
29
30
|
%size_eq_0 = icmp eq i64 %size, 0
|
|
@@ -39,6 +40,7 @@ loop:
|
|
|
39
40
|
%cmp = icmp ult i64 %inc, %size
|
|
40
41
|
br i1 %cmp, label %loop, label %exit
|
|
41
42
|
exit:
|
|
43
|
+
call void @free(ptr %object)
|
|
42
44
|
ret void
|
|
43
45
|
}
|
|
44
46
|
"""
|
|
@@ -25,11 +25,6 @@ extern llvm """
|
|
|
25
25
|
declare i32 @clock_gettime(i32, ptr)
|
|
26
26
|
"""
|
|
27
27
|
|
|
28
|
-
// This should not be needed when using NodeJS 16.x or newer.
|
|
29
|
-
extern jsNode """
|
|
30
|
-
const { performance } = require('node:perf_hooks');
|
|
31
|
-
"""
|
|
32
|
-
|
|
33
28
|
/// High-precision timestamp in nanoseconds that should be for measurements.
|
|
34
29
|
///
|
|
35
30
|
/// This timestamp should only be used for **relative** measurements,
|
|
@@ -32,7 +32,7 @@ extern def allocate(size: Int) at global: ByteArray =
|
|
|
32
32
|
%object_alloc = tail call noalias ptr @calloc(i64 %object_size, i64 1)
|
|
33
33
|
store i64 0, ptr %object_alloc, align 8
|
|
34
34
|
%object_data_ptr = getelementptr inbounds i8, ptr %object_alloc, i64 8
|
|
35
|
-
store ptr @
|
|
35
|
+
store ptr @bytearray_erase, ptr %object_data_ptr, align 8
|
|
36
36
|
%ret_object1 = insertvalue %Pos poison, i64 ${size}, 0
|
|
37
37
|
%ret_object2 = insertvalue %Pos %ret_object1, ptr %object_alloc, 1
|
|
38
38
|
ret %Pos %ret_object2
|
|
@@ -120,13 +120,18 @@ def build(size: Int) { index: Int => Byte }: ByteArray = {
|
|
|
120
120
|
///
|
|
121
121
|
/// O(N)
|
|
122
122
|
def copy(from: ByteArray, start: Int, to: ByteArray, offset: Int, length: Int): Unit =
|
|
123
|
-
if (
|
|
124
|
-
|| start + length > from.size || offset + length > to.size) {
|
|
125
|
-
<{ "bytearray::copy: range out of bounds" }>
|
|
126
|
-
} else {
|
|
123
|
+
if (from.inBounds(start, length) && to.inBounds(offset, length)) {
|
|
127
124
|
unsafeCopy(from, start, to, offset, length)
|
|
125
|
+
} else {
|
|
126
|
+
<{ "bytearray::copy: range out of bounds" }>
|
|
128
127
|
}
|
|
129
128
|
|
|
129
|
+
/// Whether `[offset, offset + length)` is a valid range of indices inside `arr`.
|
|
130
|
+
///
|
|
131
|
+
/// O(1)
|
|
132
|
+
def inBounds(arr: ByteArray, offset: Int, length: Int): Bool =
|
|
133
|
+
offset >= 0 && length >= 0 && offset + length <= arr.size
|
|
134
|
+
|
|
130
135
|
/// `copy` without its bounds check.
|
|
131
136
|
/// Corrupts memory if misused; prefer `copy`.
|
|
132
137
|
extern def unsafeCopy(from: ByteArray, start: Int, to: ByteArray, offset: Int, length: Int) at global: Unit =
|
|
@@ -246,7 +251,8 @@ extern js """
|
|
|
246
251
|
"""
|
|
247
252
|
|
|
248
253
|
extern llvm """
|
|
249
|
-
define void @
|
|
254
|
+
define void @bytearray_erase(ptr %object) {
|
|
255
|
+
call void @free(ptr %object)
|
|
250
256
|
ret void
|
|
251
257
|
}
|
|
252
258
|
"""
|
|
@@ -72,7 +72,7 @@ extern def readln() at async: String =
|
|
|
72
72
|
jsNode "$effekt.capture(callback => $effekt.readln(callback))"
|
|
73
73
|
llvm """
|
|
74
74
|
%result = call %Pos @c_io_readln()
|
|
75
|
-
|
|
75
|
+
musttail call tailcc void @return_Pos(%Stack %stack, %Pos %result) "no_callee_saved_registers"
|
|
76
76
|
ret void
|
|
77
77
|
"""
|
|
78
78
|
|
|
@@ -280,7 +280,7 @@ extern def infixNeq(x: Char, y: Char) at {}: Bool =
|
|
|
280
280
|
|
|
281
281
|
extern def infixEq(x: String, y: String) at {}: Bool =
|
|
282
282
|
js "${x} === ${y}"
|
|
283
|
-
chez "(
|
|
283
|
+
chez "(string=? ${x} ${y})"
|
|
284
284
|
llvm """
|
|
285
285
|
%res = call %Pos @c_bytearray_equal(%Pos ${x}, %Pos ${y})
|
|
286
286
|
ret %Pos %res
|
|
@@ -290,7 +290,7 @@ def infixNeq(x: String, y: String): Bool = not(x == y)
|
|
|
290
290
|
|
|
291
291
|
extern def infixEq(x: Bool, y: Bool) at {}: Bool =
|
|
292
292
|
js "${x} === ${y}"
|
|
293
|
-
chez "(
|
|
293
|
+
chez "(eq? ${x} ${y})"
|
|
294
294
|
llvm """
|
|
295
295
|
%slim_x = extractvalue %Pos ${x}, 0
|
|
296
296
|
%slim_y = extractvalue %Pos ${y}, 0
|
|
@@ -302,7 +302,7 @@ extern def infixEq(x: Bool, y: Bool) at {}: Bool =
|
|
|
302
302
|
|
|
303
303
|
extern def infixNeq(x: Bool, y: Bool) at {}: Bool =
|
|
304
304
|
js "${x} !== ${y}"
|
|
305
|
-
chez "(not (
|
|
305
|
+
chez "(not (eq? ${x} ${y}))"
|
|
306
306
|
llvm """
|
|
307
307
|
%slim_x = extractvalue %Pos ${x}, 0
|
|
308
308
|
%slim_y = extractvalue %Pos ${y}, 0
|
|
@@ -334,6 +334,12 @@ extern def infixNeq(x: Byte, y: Byte) at {}: Bool =
|
|
|
334
334
|
|
|
335
335
|
// Math ops
|
|
336
336
|
// ========
|
|
337
|
+
|
|
338
|
+
// Numeric representations in the backends:
|
|
339
|
+
// LLVM: int64_t (64-bit integer)
|
|
340
|
+
// JS: 64-bit floating-point (64-bit IEEE 754)
|
|
341
|
+
// Chez: 64-bit integer
|
|
342
|
+
|
|
337
343
|
extern def infixPlus(x: Int, y: Int) at {}: Int =
|
|
338
344
|
js "(${x} + ${y})"
|
|
339
345
|
chez "(fx+ ${x} ${y})"
|
|
@@ -355,11 +361,48 @@ extern def infixMinus(x: Int, y: Int) at {}: Int =
|
|
|
355
361
|
chez "(fx- ${x} ${y})"
|
|
356
362
|
llvm "%z = sub %Int ${x}, ${y} ret %Int %z"
|
|
357
363
|
|
|
358
|
-
|
|
364
|
+
/// Computes the quotient of truncated division (rounds towards zero)
|
|
365
|
+
extern def quot(x: Int, y: Int) at {}: Int =
|
|
366
|
+
js "Math.trunc(${x} / ${y})"
|
|
367
|
+
chez "(int-quotient ${x} ${y})"
|
|
368
|
+
llvm "%z = sdiv %Int ${x}, ${y} ret %Int %z"
|
|
369
|
+
|
|
370
|
+
/// Computes the remainder of truncated division (has the sign of the dividend)
|
|
371
|
+
extern def rem(x: Int, y: Int) at {}: Int =
|
|
359
372
|
js "(${x} % ${y})"
|
|
360
|
-
chez "(
|
|
373
|
+
chez "(int-remainder ${x} ${y})"
|
|
361
374
|
llvm "%z = srem %Int ${x}, ${y} ret %Int %z"
|
|
362
375
|
|
|
376
|
+
/// Computes the quotient of Euclidean division
|
|
377
|
+
extern def div(x: Int, y: Int) at {}: Int =
|
|
378
|
+
js "$effekt.div(${x}, ${y})"
|
|
379
|
+
chez "(int-div ${x} ${y})"
|
|
380
|
+
llvm """
|
|
381
|
+
%q = sdiv %Int ${x}, ${y}
|
|
382
|
+
%r = srem %Int ${x}, ${y}
|
|
383
|
+
%rNeg = ashr %Int %r, 63
|
|
384
|
+
%yNeg = ashr %Int ${y}, 63
|
|
385
|
+
%ySign = or %Int %yNeg, 1
|
|
386
|
+
%adjust = and %Int %rNeg, %ySign
|
|
387
|
+
%z = sub %Int %q, %adjust
|
|
388
|
+
ret %Int %z
|
|
389
|
+
"""
|
|
390
|
+
|
|
391
|
+
/// Computes the remainder of Euclidean division (is never negative)
|
|
392
|
+
extern def mod(x: Int, y: Int) at {}: Int =
|
|
393
|
+
js "$effekt.mod(${x}, ${y})"
|
|
394
|
+
chez "(int-mod ${x} ${y})"
|
|
395
|
+
llvm """
|
|
396
|
+
%r = srem %Int ${x}, ${y}
|
|
397
|
+
%rNeg = ashr %Int %r, 63
|
|
398
|
+
%yNeg = ashr %Int ${y}, 63
|
|
399
|
+
%yFlip = xor %Int ${y}, %yNeg
|
|
400
|
+
%yAbs = sub %Int %yFlip, %yNeg
|
|
401
|
+
%adjust = and %Int %rNeg, %yAbs
|
|
402
|
+
%z = add %Int %r, %adjust
|
|
403
|
+
ret %Int %z
|
|
404
|
+
"""
|
|
405
|
+
|
|
363
406
|
extern def infixPlus(x: Double, y: Double) at {}: Double =
|
|
364
407
|
js "(${x} + ${y})"
|
|
365
408
|
chez "(fl+ ${x} ${y})"
|
|
@@ -661,7 +704,6 @@ extern def bitwiseNot(x: Int) at {}: Int =
|
|
|
661
704
|
chez "(fxnot ${x})"
|
|
662
705
|
llvm "%z = xor %Int ${x}, -1 ret %Int %z"
|
|
663
706
|
|
|
664
|
-
|
|
665
707
|
// Byte operations
|
|
666
708
|
// ===============
|
|
667
709
|
extern def toByte(n: Int) at {}: Byte =
|
|
@@ -1,111 +1,121 @@
|
|
|
1
1
|
module heap
|
|
2
|
-
import resizable_array
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
/// `cmp` defines the ordering of elements
|
|
6
|
-
record Heap[T](rawContents: ResizableArray[T], cmp: (T, T) => Ordering at {})
|
|
3
|
+
import stream
|
|
7
4
|
|
|
8
|
-
///
|
|
9
|
-
|
|
10
|
-
Heap[T](resizableArray(), cmp)
|
|
11
|
-
|
|
12
|
-
/// Make a new Heap with the given comparison operation and initial capacity
|
|
13
|
-
def heap[T](cmp: (T,T) => Ordering at {}, capacity: Int) =
|
|
14
|
-
Heap[T](resizableArray(capacity), cmp)
|
|
5
|
+
/// A value together with its priority. Smaller weights come first.
|
|
6
|
+
record Weighted[A](value: A, weight: Double)
|
|
15
7
|
|
|
16
8
|
namespace internal {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def bubbleUp[A](heap: Heap[A], idx: Int) = {
|
|
22
|
-
val arr = heap.rawContents
|
|
23
|
-
arr.boundsCheck(idx) // idx > parent(idx), parent(parent(idx)) etc
|
|
24
|
-
|
|
25
|
-
def go(idx: Int): Unit = {
|
|
26
|
-
if (idx > 0 and (heap.cmp)(arr.get(parent(idx)), arr.get(idx)) is Greater()) {
|
|
27
|
-
arr.swap(parent(idx), idx)
|
|
28
|
-
go(parent(idx))
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
go(idx)
|
|
9
|
+
/// A pairing heap: empty, or a minimal element with its subheaps.
|
|
10
|
+
type Tree[A] {
|
|
11
|
+
Node(value: A, weight: Double, subheaps: List[Tree[A]])
|
|
12
|
+
Empty()
|
|
32
13
|
}
|
|
33
|
-
def sinkDown[A](heap: Heap[A], idx: Int) = {
|
|
34
|
-
val arr = heap.rawContents
|
|
35
14
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
val l = arr.get(left(idx))
|
|
44
|
-
val r = arr.get(right(idx))
|
|
45
|
-
if (l < v && r < v) {
|
|
46
|
-
// swap with the smaller one
|
|
47
|
-
if (l < r) {
|
|
48
|
-
arr.swap(left(idx), idx)
|
|
49
|
-
go(left(idx))
|
|
50
|
-
} else {
|
|
51
|
-
arr.swap(right(idx), idx)
|
|
52
|
-
go(right(idx))
|
|
53
|
-
}
|
|
54
|
-
} else if (l < v) {
|
|
55
|
-
arr.swap(left(idx), idx)
|
|
56
|
-
go(left(idx))
|
|
57
|
-
} else if (r < v) {
|
|
58
|
-
arr.swap(right(idx), idx)
|
|
59
|
-
go(right(idx))
|
|
60
|
-
}
|
|
61
|
-
} else if (left(idx) < arr.size) {
|
|
62
|
-
if (arr.get(left(idx)) < arr.get(idx)) {
|
|
63
|
-
arr.swap(left(idx), idx)
|
|
64
|
-
go(left(idx))
|
|
65
|
-
}
|
|
66
|
-
} // else: we are at the bottom
|
|
67
|
-
}
|
|
68
|
-
go(idx)
|
|
15
|
+
/// Merge a nonempty list of heaps, given as head and tail, using the two-pass pairing strategy.
|
|
16
|
+
///
|
|
17
|
+
/// O(log n) amortized
|
|
18
|
+
def mergePairs[A](heap: Tree[A], heaps: List[Tree[A]]): Tree[A] = heaps match {
|
|
19
|
+
case Nil() => heap
|
|
20
|
+
case Cons(heap1, Nil()) => meld(heap, heap1)
|
|
21
|
+
case Cons(heap1, Cons(heap2, rest)) => meld(meld(heap, heap1), mergePairs(heap2, rest))
|
|
69
22
|
}
|
|
70
23
|
}
|
|
71
24
|
|
|
72
|
-
///
|
|
25
|
+
/// Persistent min-heap, implemented as a pairing heap.
|
|
26
|
+
type Heap[A] = internal::Tree[A]
|
|
27
|
+
|
|
28
|
+
/// The empty heap.
|
|
73
29
|
///
|
|
74
|
-
/// O(
|
|
75
|
-
def
|
|
76
|
-
with on[OutOfBounds].panic();
|
|
77
|
-
val idx = heap.rawContents.add(value)
|
|
78
|
-
internal::bubbleUp(heap, idx)
|
|
79
|
-
}
|
|
30
|
+
/// O(1)
|
|
31
|
+
def empty[A](): Heap[A] = internal::Empty()
|
|
80
32
|
|
|
81
|
-
///
|
|
82
|
-
/// panics when heap is empty
|
|
33
|
+
/// A heap containing exactly one element.
|
|
83
34
|
///
|
|
84
35
|
/// O(1)
|
|
85
|
-
def
|
|
86
|
-
|
|
36
|
+
def single[A](value: A, weight: Double): Heap[A] = internal::Node(value, weight, Nil())
|
|
37
|
+
|
|
38
|
+
/// Check if the heap is empty.
|
|
39
|
+
///
|
|
40
|
+
/// O(1)
|
|
41
|
+
def isEmpty[A](heap: Heap[A]): Bool = heap match {
|
|
42
|
+
case internal::Empty() => true
|
|
43
|
+
case internal::Node(_, _, _) => false
|
|
87
44
|
}
|
|
88
45
|
|
|
89
|
-
///
|
|
90
|
-
/// panics when heap is empty
|
|
46
|
+
/// Merge two heaps.
|
|
91
47
|
///
|
|
92
|
-
/// O(
|
|
93
|
-
def
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
res
|
|
103
|
-
}
|
|
48
|
+
/// O(1)
|
|
49
|
+
def meld[A](heap1: Heap[A], heap2: Heap[A]): Heap[A] = (heap1, heap2) match {
|
|
50
|
+
case (internal::Empty(), heap2) => heap2
|
|
51
|
+
case (heap1, internal::Empty()) => heap1
|
|
52
|
+
case (internal::Node(x1, w1, subheaps1), internal::Node(x2, w2, subheaps2)) =>
|
|
53
|
+
if (w1 < w2) {
|
|
54
|
+
internal::Node(x1, w1, Cons(heap2, subheaps1))
|
|
55
|
+
} else {
|
|
56
|
+
internal::Node(x2, w2, Cons(heap1, subheaps2))
|
|
57
|
+
}
|
|
104
58
|
}
|
|
105
59
|
|
|
106
|
-
///
|
|
60
|
+
/// Insert a value with the given weight.
|
|
107
61
|
///
|
|
108
62
|
/// O(1)
|
|
109
|
-
def
|
|
110
|
-
heap
|
|
111
|
-
|
|
63
|
+
def insert[A](heap: Heap[A], value: A, weight: Double): Heap[A] =
|
|
64
|
+
meld(single(value, weight), heap)
|
|
65
|
+
|
|
66
|
+
/// The element with the smallest weight, without removing it.
|
|
67
|
+
/// Returns `None()` if the heap is empty.
|
|
68
|
+
///
|
|
69
|
+
/// O(1)
|
|
70
|
+
def findMinimum[A](heap: Heap[A]): Option[Weighted[A]] = heap match {
|
|
71
|
+
case internal::Empty() => None()
|
|
72
|
+
case internal::Node(x, w, _) => Some(Weighted(x, w))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// Remove the element with the smallest weight, returning it and the remaining heap.
|
|
76
|
+
/// Returns `None()` if the heap is empty.
|
|
77
|
+
///
|
|
78
|
+
/// O(log n) amortized
|
|
79
|
+
def extract[A](heap: Heap[A]): Option[(Weighted[A], Heap[A])] = heap match {
|
|
80
|
+
case internal::Empty() => None()
|
|
81
|
+
case internal::Node(x, w, Nil()) => Some((Weighted(x, w), internal::Empty()))
|
|
82
|
+
case internal::Node(x, w, Cons(heap1, heaps)) => Some((Weighted(x, w), internal::mergePairs(heap1, heaps)))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/// Insert the given value and remove the element with the smallest weight.
|
|
86
|
+
/// Returns the given value and the unchanged heap if its weight is not larger than all others.
|
|
87
|
+
///
|
|
88
|
+
/// O(log n) amortized
|
|
89
|
+
def exchange[A](heap: Heap[A], value: A, weight: Double): (Weighted[A], Heap[A]) = heap match {
|
|
90
|
+
case internal::Node(x, w, heaps) =>
|
|
91
|
+
if (w < weight) {
|
|
92
|
+
(Weighted(x, w), internal::mergePairs(single(value, weight), heaps))
|
|
93
|
+
} else {
|
|
94
|
+
(Weighted(value, weight), heap)
|
|
95
|
+
}
|
|
96
|
+
case internal::Empty() => (Weighted(value, weight), heap)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// Emit all elements in ascending order of weight.
|
|
100
|
+
///
|
|
101
|
+
/// O(n log n)
|
|
102
|
+
def each[A](heap: Heap[A]): Unit / emit[Weighted[A]] = extract(heap) match {
|
|
103
|
+
case None() => ()
|
|
104
|
+
case Some((element, rest)) =>
|
|
105
|
+
do emit(element)
|
|
106
|
+
each(rest)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/// Collect all emitted elements into a heap.
|
|
110
|
+
///
|
|
111
|
+
/// O(n)
|
|
112
|
+
def collectHeap[A] { stream: () => Unit / emit[Weighted[A]] }: Heap[A] = {
|
|
113
|
+
var heap: Heap[A] = empty()
|
|
114
|
+
try {
|
|
115
|
+
stream()
|
|
116
|
+
} with emit[Weighted[A]] { case Weighted(value, weight) =>
|
|
117
|
+
heap = insert(heap, value, weight)
|
|
118
|
+
resume(())
|
|
119
|
+
}
|
|
120
|
+
heap
|
|
121
|
+
}
|
|
@@ -23,14 +23,14 @@ def console[R] { program: () => R / Console }: R = {
|
|
|
23
23
|
namespace js {
|
|
24
24
|
|
|
25
25
|
extern jsNode """
|
|
26
|
-
const readline = require('node:readline');
|
|
26
|
+
const console$readline = require('node:readline');
|
|
27
27
|
"""
|
|
28
28
|
|
|
29
29
|
extern type JSConsole =
|
|
30
|
-
js "readline.createInterface return type"
|
|
30
|
+
js "console$readline.createInterface return type"
|
|
31
31
|
|
|
32
32
|
extern def newConsole() at io: JSConsole =
|
|
33
|
-
jsNode """readline.createInterface({
|
|
33
|
+
jsNode """console$readline.createInterface({
|
|
34
34
|
input: process.stdin,
|
|
35
35
|
output: process.stdout,
|
|
36
36
|
})"""
|
|
@@ -363,7 +363,7 @@ namespace internal {
|
|
|
363
363
|
"""
|
|
364
364
|
|
|
365
365
|
extern jsNode """
|
|
366
|
-
const
|
|
366
|
+
const error$getSystemErrorName = require('node:util').getSystemErrorName;
|
|
367
367
|
|
|
368
368
|
/**
|
|
369
369
|
* Maps the error code to a Effekt-stable (platform independent) numeric value.
|
|
@@ -376,7 +376,7 @@ extern jsNode """
|
|
|
376
376
|
* For reference, see the following Deno code which is more readable than the NodeJS implementation:
|
|
377
377
|
* https://github.com/denoland/deno/blob/8fb073d7b4ea83b57fd1bf9875681347b414bbe6/ext/node/polyfills/internal_binding/uv.ts
|
|
378
378
|
*/
|
|
379
|
-
function
|
|
379
|
+
function error$number(errno) {
|
|
380
380
|
const mapByName = {
|
|
381
381
|
EPERM: 1,
|
|
382
382
|
ENOENT: 2,
|
|
@@ -461,7 +461,7 @@ extern jsNode """
|
|
|
461
461
|
|
|
462
462
|
// When the error code is not known, we return 217 (UNKNOWN).
|
|
463
463
|
try {
|
|
464
|
-
const name = getSystemErrorName(errno);
|
|
464
|
+
const name = error$getSystemErrorName(errno);
|
|
465
465
|
return mapByName[name] ?? 217;
|
|
466
466
|
} catch {
|
|
467
467
|
return 217;
|
|
@@ -470,7 +470,7 @@ extern jsNode """
|
|
|
470
470
|
"""
|
|
471
471
|
|
|
472
472
|
extern def errorNumber(errno: Int) at {}: Int =
|
|
473
|
-
jsNode "
|
|
473
|
+
jsNode "error$number(${errno})"
|
|
474
474
|
llvm """
|
|
475
475
|
%z = call %Int @c_error_number(%Int ${errno})
|
|
476
476
|
ret %Int %z
|
|
@@ -170,11 +170,31 @@ def openForWriting(path: String): File / Exception[IOError] =
|
|
|
170
170
|
def openForAppending(path: String): File / Exception[IOError] =
|
|
171
171
|
internal::checkResult(internal::openForAppending(path))
|
|
172
172
|
|
|
173
|
-
|
|
174
|
-
|
|
173
|
+
/// Reads up to `size` bytes from `file` at `position` (-1 for the current position) into `buffer`, starting at `offset`.
|
|
174
|
+
/// Returns the number of bytes read, and 0 at end of file.
|
|
175
|
+
/// Panics unless:
|
|
176
|
+
/// - `size` is positive (a read of nothing would look like end of file), and
|
|
177
|
+
/// - `[offset, offset + size)` is a valid range inside `buffer` (like `bytearray::copy`).
|
|
178
|
+
def read(file: File, buffer: ByteArray, offset: Int, size: Int, position: Int): Int / Exception[IOError] = {
|
|
179
|
+
if (size <= 0) { panic("io/filesystem::read: size must be positive") }
|
|
180
|
+
|
|
181
|
+
if (buffer.inBounds(offset, size)) {
|
|
182
|
+
internal::checkResult(internal::read(file, buffer, offset, size, position))
|
|
183
|
+
} else {
|
|
184
|
+
panic("io/filesystem::read: window out of bounds")
|
|
185
|
+
}
|
|
186
|
+
}
|
|
175
187
|
|
|
176
|
-
|
|
177
|
-
|
|
188
|
+
/// Writes up to `size` bytes from `buffer`, starting at `offset`, to `file` at `position` (-1 for the current position).
|
|
189
|
+
/// Returns the number of bytes written, which may be fewer than `size`.
|
|
190
|
+
/// Panics unless `[offset, offset + size)` is a valid range inside `buffer` (like `bytearray::copy`).
|
|
191
|
+
def write(file: File, buffer: ByteArray, offset: Int, size: Int, position: Int): Int / Exception[IOError] = {
|
|
192
|
+
if (buffer.inBounds(offset, size)) {
|
|
193
|
+
internal::checkResult(internal::write(file, buffer, offset, size, position))
|
|
194
|
+
} else {
|
|
195
|
+
panic("io/filesystem::write: window out of bounds")
|
|
196
|
+
}
|
|
197
|
+
}
|
|
178
198
|
|
|
179
199
|
def close(file: File): Unit / Exception[IOError] = {
|
|
180
200
|
internal::checkResult(internal::close(file)); ()
|
|
@@ -186,47 +206,37 @@ def mkdir(path: String): Unit / Exception[IOError] = {
|
|
|
186
206
|
|
|
187
207
|
namespace internal {
|
|
188
208
|
|
|
189
|
-
extern js """
|
|
190
|
-
/**
|
|
191
|
-
* Nodejs file operations expect buffers, but we represent buffers as typed arrays.
|
|
192
|
-
* This function converts between the two without copying.
|
|
193
|
-
*/
|
|
194
|
-
function toBuffer(buffer) {
|
|
195
|
-
return Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength)
|
|
196
|
-
}
|
|
197
|
-
"""
|
|
198
|
-
|
|
199
209
|
extern jsNode """
|
|
200
|
-
const
|
|
210
|
+
const filesystem$node = require("fs");
|
|
201
211
|
|
|
202
|
-
function open(path, flags, callback) {
|
|
203
|
-
|
|
212
|
+
function filesystem$open(path, flags, callback) {
|
|
213
|
+
filesystem$node.open(path, flags, (err, file) => {
|
|
204
214
|
if (err) { callback(err.errno) } else { callback(file) }
|
|
205
215
|
})
|
|
206
216
|
}
|
|
207
217
|
|
|
208
|
-
function read(file, buffer, offset, size, position, callback) {
|
|
218
|
+
function filesystem$read(file, buffer, offset, size, position, callback) {
|
|
209
219
|
let positionOrNull = position === -1 ? null : position;
|
|
210
|
-
|
|
220
|
+
filesystem$node.read(file, io$toBuffer(buffer), offset, size, positionOrNull, (err, bytesRead) => {
|
|
211
221
|
if (err) { callback(err.errno) } else { callback(bytesRead) }
|
|
212
222
|
})
|
|
213
223
|
}
|
|
214
224
|
|
|
215
|
-
function write(file, buffer, offset, size, position, callback) {
|
|
225
|
+
function filesystem$write(file, buffer, offset, size, position, callback) {
|
|
216
226
|
let positionOrNull = position === -1 ? null : position;
|
|
217
|
-
|
|
227
|
+
filesystem$node.write(file, io$toBuffer(buffer), offset, size, positionOrNull, (err, bytesWritten) => {
|
|
218
228
|
if (err) { callback(err.errno) } else { callback(bytesWritten) }
|
|
219
229
|
})
|
|
220
230
|
}
|
|
221
231
|
|
|
222
|
-
function close(file, callback) {
|
|
223
|
-
|
|
232
|
+
function filesystem$close(file, callback) {
|
|
233
|
+
filesystem$node.close(file, (err) => {
|
|
224
234
|
if (err) { callback(err.errno) } else { callback(0) }
|
|
225
235
|
})
|
|
226
236
|
}
|
|
227
237
|
|
|
228
|
-
function mkdir(path, callback) {
|
|
229
|
-
|
|
238
|
+
function filesystem$mkdir(path, callback) {
|
|
239
|
+
filesystem$node.mkdir(path, (err) => {
|
|
230
240
|
if (err) { callback(err.errno) } else { callback(0) }
|
|
231
241
|
})
|
|
232
242
|
}
|
|
@@ -243,49 +253,49 @@ namespace internal {
|
|
|
243
253
|
"""
|
|
244
254
|
|
|
245
255
|
extern def openForReading(path: String) at async: Int =
|
|
246
|
-
jsNode "$effekt.capture(callback => open(${path}, 'r', callback))"
|
|
256
|
+
jsNode "$effekt.capture(callback => filesystem$open(${path}, 'r', callback))"
|
|
247
257
|
llvm """
|
|
248
258
|
notail call void @c_fs_open_reading(%Pos ${path}, %Stack %stack)
|
|
249
259
|
ret void
|
|
250
260
|
"""
|
|
251
261
|
|
|
252
262
|
extern def openForWriting(path: String) at async: Int =
|
|
253
|
-
jsNode "$effekt.capture(callback => open(${path}, 'w', callback))"
|
|
263
|
+
jsNode "$effekt.capture(callback => filesystem$open(${path}, 'w', callback))"
|
|
254
264
|
llvm """
|
|
255
265
|
notail call void @c_fs_open_writing(%Pos ${path}, %Stack %stack)
|
|
256
266
|
ret void
|
|
257
267
|
"""
|
|
258
268
|
|
|
259
269
|
extern def openForAppending(path: String) at async: Int =
|
|
260
|
-
jsNode "$effekt.capture(callback => open(${path}, 'a', callback))"
|
|
270
|
+
jsNode "$effekt.capture(callback => filesystem$open(${path}, 'a', callback))"
|
|
261
271
|
llvm """
|
|
262
272
|
notail call void @c_fs_open_appending(%Pos ${path}, %Stack %stack)
|
|
263
273
|
ret void
|
|
264
274
|
"""
|
|
265
275
|
|
|
266
276
|
extern def read(file: Int, buffer: ByteArray, offset: Int, size: Int, position: Int) at async: Int =
|
|
267
|
-
jsNode "$effekt.capture(callback => read(${file}, ${buffer}, ${offset}, ${size}, ${position}, callback))"
|
|
277
|
+
jsNode "$effekt.capture(callback => filesystem$read(${file}, ${buffer}, ${offset}, ${size}, ${position}, callback))"
|
|
268
278
|
llvm """
|
|
269
279
|
notail call void @c_fs_read(%Int ${file}, %Pos ${buffer}, %Int ${offset}, %Int ${size}, %Int ${position}, %Stack %stack)
|
|
270
280
|
ret void
|
|
271
281
|
"""
|
|
272
282
|
|
|
273
283
|
extern def write(file: Int, buffer: ByteArray, offset: Int, size: Int, position: Int) at async: Int =
|
|
274
|
-
jsNode "$effekt.capture(callback => write(${file}, ${buffer}, ${offset}, ${size}, ${position}, callback))"
|
|
284
|
+
jsNode "$effekt.capture(callback => filesystem$write(${file}, ${buffer}, ${offset}, ${size}, ${position}, callback))"
|
|
275
285
|
llvm """
|
|
276
286
|
notail call void @c_fs_write(%Int ${file}, %Pos ${buffer}, %Int ${offset}, %Int ${size}, %Int ${position}, %Stack %stack)
|
|
277
287
|
ret void
|
|
278
288
|
"""
|
|
279
289
|
|
|
280
290
|
extern def close(file: Int) at async: Int =
|
|
281
|
-
jsNode "$effekt.capture(callback => close(${file}, callback))"
|
|
291
|
+
jsNode "$effekt.capture(callback => filesystem$close(${file}, callback))"
|
|
282
292
|
llvm """
|
|
283
293
|
notail call void @c_fs_close(%Int ${file}, %Stack %stack)
|
|
284
294
|
ret void
|
|
285
295
|
"""
|
|
286
296
|
|
|
287
297
|
extern def mkdir(path: String) at async: Int =
|
|
288
|
-
jsNode "$effekt.capture(callback => mkdir(${path}, callback))"
|
|
298
|
+
jsNode "$effekt.capture(callback => filesystem$mkdir(${path}, callback))"
|
|
289
299
|
llvm """
|
|
290
300
|
notail call void @c_fs_mkdir(%Pos ${path}, %Stack %stack)
|
|
291
301
|
ret void
|