@effekt-lang/effekt 0.83.0 → 0.85.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 +27 -10
- package/libraries/common/dictionary.effekt +2 -2
- package/libraries/common/effekt.effekt +49 -7
- package/libraries/common/exception.effekt +3 -3
- package/libraries/common/heap.effekt +100 -90
- package/libraries/common/io/console.effekt +3 -3
- package/libraries/common/io/error.effekt +9 -4
- package/libraries/common/io/filesystem.effekt +73 -44
- package/libraries/common/io/network.effekt +277 -96
- package/libraries/common/io/signal.effekt +5 -4
- package/libraries/common/io.effekt +44 -0
- package/libraries/common/process.effekt +118 -83
- package/libraries/common/ref.effekt +8 -5
- package/libraries/common/result.effekt +17 -1
- package/libraries/common/stream.effekt +2 -2
- package/libraries/common/string.effekt +10 -2
- package/libraries/common/stringbuffer.effekt +1 -1
- package/libraries/js/effekt_builtins.js +9 -0
- package/libraries/js/effekt_runtime.js +3 -0
- package/libraries/llvm/bytearray.c +51 -3
- package/libraries/llvm/forward-declare-c.ll +2 -0
- package/libraries/llvm/io.c +495 -182
- package/libraries/llvm/rts.ll +251 -79
- package/libraries/llvm/types.c +3 -2
- package/licenses/eclipse public license, version 2.0 - epl-2.0.html +5 -5
- package/licenses/mit license - mit-license.html +87 -49
- package/licenses/the bsd license - bsd-license.html +85 -47
- package/licenses/the mit license - mit.html +88 -50
- package/package.json +1 -1
|
@@ -6,8 +6,12 @@ import io
|
|
|
6
6
|
import io/error
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
/// A file
|
|
10
|
-
|
|
9
|
+
/// A file, either open or closed.
|
|
10
|
+
/// Reading, writing or closing a closed file raises `EBADF`.
|
|
11
|
+
/// Closing is explicit, for example with `with on[IOError].finalize { close(file) }`.
|
|
12
|
+
extern type File =
|
|
13
|
+
jsNode "number"
|
|
14
|
+
llvm "i64"
|
|
11
15
|
|
|
12
16
|
|
|
13
17
|
/// Reads a file at given path as utf8 encoded string.
|
|
@@ -41,7 +45,7 @@ def writeFile(path: String, contents: String): Unit / Exception[IOError] = {
|
|
|
41
45
|
with on[IOError].finalize { close(file) }
|
|
42
46
|
|
|
43
47
|
val chunkSize = 1048576 // 1MB
|
|
44
|
-
val buffer = contents.
|
|
48
|
+
val buffer = contents.unsafeFromString
|
|
45
49
|
var offset = 0;
|
|
46
50
|
|
|
47
51
|
def go(): Unit = {
|
|
@@ -59,7 +63,7 @@ def appendFile(path: String, contents: String): Unit / Exception[IOError] = {
|
|
|
59
63
|
with on[IOError].finalize { close(file) }
|
|
60
64
|
|
|
61
65
|
val chunkSize = 1048576 // 1MB
|
|
62
|
-
val buffer = contents.
|
|
66
|
+
val buffer = contents.unsafeFromString
|
|
63
67
|
var offset = 0;
|
|
64
68
|
|
|
65
69
|
def go(): Unit = {
|
|
@@ -162,19 +166,39 @@ def filesystem[R] { program: => R / Files }: R / Exception[IOError] = // TODO mo
|
|
|
162
166
|
|
|
163
167
|
|
|
164
168
|
def openForReading(path: String): File / Exception[IOError] =
|
|
165
|
-
internal::checkResult(internal::openForReading(path))
|
|
169
|
+
internal::file(internal::checkResult(internal::openForReading(path)))
|
|
166
170
|
|
|
167
171
|
def openForWriting(path: String): File / Exception[IOError] =
|
|
168
|
-
internal::checkResult(internal::openForWriting(path))
|
|
172
|
+
internal::file(internal::checkResult(internal::openForWriting(path)))
|
|
169
173
|
|
|
170
174
|
def openForAppending(path: String): File / Exception[IOError] =
|
|
171
|
-
internal::checkResult(internal::openForAppending(path))
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
+
internal::file(internal::checkResult(internal::openForAppending(path)))
|
|
176
|
+
|
|
177
|
+
/// Reads up to `size` bytes from `file` at `position` (-1 for the current position) into `buffer`, starting at `offset`.
|
|
178
|
+
/// Returns the number of bytes read, and 0 at end of file.
|
|
179
|
+
/// Panics unless:
|
|
180
|
+
/// - `size` is positive (a read of nothing would look like end of file), and
|
|
181
|
+
/// - `[offset, offset + size)` is a valid range inside `buffer` (like `bytearray::copy`).
|
|
182
|
+
def read(file: File, buffer: ByteArray, offset: Int, size: Int, position: Int): Int / Exception[IOError] = {
|
|
183
|
+
if (size <= 0) { panic("io/filesystem::read: size must be positive") }
|
|
184
|
+
|
|
185
|
+
if (buffer.inBounds(offset, size)) {
|
|
186
|
+
internal::checkResult(internal::read(file, buffer, offset, size, position))
|
|
187
|
+
} else {
|
|
188
|
+
panic("io/filesystem::read: window out of bounds")
|
|
189
|
+
}
|
|
190
|
+
}
|
|
175
191
|
|
|
176
|
-
|
|
177
|
-
|
|
192
|
+
/// Writes up to `size` bytes from `buffer`, starting at `offset`, to `file` at `position` (-1 for the current position).
|
|
193
|
+
/// Returns the number of bytes written, which may be fewer than `size`.
|
|
194
|
+
/// Panics unless `[offset, offset + size)` is a valid range inside `buffer` (like `bytearray::copy`).
|
|
195
|
+
def write(file: File, buffer: ByteArray, offset: Int, size: Int, position: Int): Int / Exception[IOError] = {
|
|
196
|
+
if (buffer.inBounds(offset, size)) {
|
|
197
|
+
internal::checkResult(internal::write(file, buffer, offset, size, position))
|
|
198
|
+
} else {
|
|
199
|
+
panic("io/filesystem::write: window out of bounds")
|
|
200
|
+
}
|
|
201
|
+
}
|
|
178
202
|
|
|
179
203
|
def close(file: File): Unit / Exception[IOError] = {
|
|
180
204
|
internal::checkResult(internal::close(file)); ()
|
|
@@ -186,47 +210,47 @@ def mkdir(path: String): Unit / Exception[IOError] = {
|
|
|
186
210
|
|
|
187
211
|
namespace internal {
|
|
188
212
|
|
|
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
213
|
extern jsNode """
|
|
200
|
-
const
|
|
214
|
+
const filesystem$node = require("fs");
|
|
201
215
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
216
|
+
// Resumes with the handle of the opened file (see `io`), or with the (negative) error.
|
|
217
|
+
function filesystem$open(path, flags, callback) {
|
|
218
|
+
filesystem$node.open(path, flags, (err, fd) => {
|
|
219
|
+
if (err) { callback(err.errno) } else { callback(io$open(fd)) }
|
|
205
220
|
})
|
|
206
221
|
}
|
|
207
222
|
|
|
208
|
-
function read(file, buffer, offset, size, position, callback) {
|
|
223
|
+
function filesystem$read(file, buffer, offset, size, position, callback) {
|
|
224
|
+
const fd = io$get(file);
|
|
225
|
+
if (fd === undefined) { callback(error$code.EBADF); return; }
|
|
226
|
+
|
|
209
227
|
let positionOrNull = position === -1 ? null : position;
|
|
210
|
-
|
|
228
|
+
filesystem$node.read(fd, io$toBuffer(buffer), offset, size, positionOrNull, (err, bytesRead) => {
|
|
211
229
|
if (err) { callback(err.errno) } else { callback(bytesRead) }
|
|
212
230
|
})
|
|
213
231
|
}
|
|
214
232
|
|
|
215
|
-
function write(file, buffer, offset, size, position, callback) {
|
|
233
|
+
function filesystem$write(file, buffer, offset, size, position, callback) {
|
|
234
|
+
const fd = io$get(file);
|
|
235
|
+
if (fd === undefined) { callback(error$code.EBADF); return; }
|
|
236
|
+
|
|
216
237
|
let positionOrNull = position === -1 ? null : position;
|
|
217
|
-
|
|
238
|
+
filesystem$node.write(fd, io$toBuffer(buffer), offset, size, positionOrNull, (err, bytesWritten) => {
|
|
218
239
|
if (err) { callback(err.errno) } else { callback(bytesWritten) }
|
|
219
240
|
})
|
|
220
241
|
}
|
|
221
242
|
|
|
222
|
-
function close(file, callback) {
|
|
223
|
-
|
|
243
|
+
function filesystem$close(file, callback) {
|
|
244
|
+
const fd = io$close(file);
|
|
245
|
+
if (fd === undefined) { callback(error$code.EBADF); return; }
|
|
246
|
+
|
|
247
|
+
filesystem$node.close(fd, (err) => {
|
|
224
248
|
if (err) { callback(err.errno) } else { callback(0) }
|
|
225
249
|
})
|
|
226
250
|
}
|
|
227
251
|
|
|
228
|
-
function mkdir(path, callback) {
|
|
229
|
-
|
|
252
|
+
function filesystem$mkdir(path, callback) {
|
|
253
|
+
filesystem$node.mkdir(path, (err) => {
|
|
230
254
|
if (err) { callback(err.errno) } else { callback(0) }
|
|
231
255
|
})
|
|
232
256
|
}
|
|
@@ -243,49 +267,54 @@ namespace internal {
|
|
|
243
267
|
"""
|
|
244
268
|
|
|
245
269
|
extern def openForReading(path: String) at async: Int =
|
|
246
|
-
jsNode "$effekt.capture(callback => open(${path}, 'r', callback))"
|
|
270
|
+
jsNode "$effekt.capture(callback => filesystem$open(${path}, 'r', callback))"
|
|
247
271
|
llvm """
|
|
248
272
|
notail call void @c_fs_open_reading(%Pos ${path}, %Stack %stack)
|
|
249
273
|
ret void
|
|
250
274
|
"""
|
|
251
275
|
|
|
252
276
|
extern def openForWriting(path: String) at async: Int =
|
|
253
|
-
jsNode "$effekt.capture(callback => open(${path}, 'w', callback))"
|
|
277
|
+
jsNode "$effekt.capture(callback => filesystem$open(${path}, 'w', callback))"
|
|
254
278
|
llvm """
|
|
255
279
|
notail call void @c_fs_open_writing(%Pos ${path}, %Stack %stack)
|
|
256
280
|
ret void
|
|
257
281
|
"""
|
|
258
282
|
|
|
259
283
|
extern def openForAppending(path: String) at async: Int =
|
|
260
|
-
jsNode "$effekt.capture(callback => open(${path}, 'a', callback))"
|
|
284
|
+
jsNode "$effekt.capture(callback => filesystem$open(${path}, 'a', callback))"
|
|
261
285
|
llvm """
|
|
262
286
|
notail call void @c_fs_open_appending(%Pos ${path}, %Stack %stack)
|
|
263
287
|
ret void
|
|
264
288
|
"""
|
|
265
289
|
|
|
266
|
-
extern def read(file:
|
|
267
|
-
jsNode "$effekt.capture(callback => read(${file}, ${buffer}, ${offset}, ${size}, ${position}, callback))"
|
|
290
|
+
extern def read(file: File, buffer: ByteArray, offset: Int, size: Int, position: Int) at async: Int =
|
|
291
|
+
jsNode "$effekt.capture(callback => filesystem$read(${file}, ${buffer}, ${offset}, ${size}, ${position}, callback))"
|
|
268
292
|
llvm """
|
|
269
293
|
notail call void @c_fs_read(%Int ${file}, %Pos ${buffer}, %Int ${offset}, %Int ${size}, %Int ${position}, %Stack %stack)
|
|
270
294
|
ret void
|
|
271
295
|
"""
|
|
272
296
|
|
|
273
|
-
extern def write(file:
|
|
274
|
-
jsNode "$effekt.capture(callback => write(${file}, ${buffer}, ${offset}, ${size}, ${position}, callback))"
|
|
297
|
+
extern def write(file: File, buffer: ByteArray, offset: Int, size: Int, position: Int) at async: Int =
|
|
298
|
+
jsNode "$effekt.capture(callback => filesystem$write(${file}, ${buffer}, ${offset}, ${size}, ${position}, callback))"
|
|
275
299
|
llvm """
|
|
276
300
|
notail call void @c_fs_write(%Int ${file}, %Pos ${buffer}, %Int ${offset}, %Int ${size}, %Int ${position}, %Stack %stack)
|
|
277
301
|
ret void
|
|
278
302
|
"""
|
|
279
303
|
|
|
280
|
-
extern def close(file:
|
|
281
|
-
jsNode "$effekt.capture(callback => close(${file}, callback))"
|
|
304
|
+
extern def close(file: File) at async: Int =
|
|
305
|
+
jsNode "$effekt.capture(callback => filesystem$close(${file}, callback))"
|
|
282
306
|
llvm """
|
|
283
307
|
notail call void @c_fs_close(%Int ${file}, %Stack %stack)
|
|
284
308
|
ret void
|
|
285
309
|
"""
|
|
286
310
|
|
|
311
|
+
/// The file whose handle `openForReading`, `openForWriting` or `openForAppending` returned.
|
|
312
|
+
extern def file(handle: Int) at {}: File =
|
|
313
|
+
jsNode "${handle}"
|
|
314
|
+
llvm "ret %Int ${handle}"
|
|
315
|
+
|
|
287
316
|
extern def mkdir(path: String) at async: Int =
|
|
288
|
-
jsNode "$effekt.capture(callback => mkdir(${path}, callback))"
|
|
317
|
+
jsNode "$effekt.capture(callback => filesystem$mkdir(${path}, callback))"
|
|
289
318
|
llvm """
|
|
290
319
|
notail call void @c_fs_mkdir(%Pos ${path}, %Stack %stack)
|
|
291
320
|
ret void
|