Skip to content

Commit 778eebb

Browse files
ydnardgryski
authored andcommitted
src/{runtime, syscall}: use new package cm APIs
1 parent 8517126 commit 778eebb

File tree

3 files changed

+41
-49
lines changed

3 files changed

+41
-49
lines changed

src/runtime/runtime_tinygowasmp2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ func putchar(c byte) {
2626
if c == '\n' || putcharPosition >= putcharBufferSize {
2727
list := cm.NewList(&putcharBuffer[0], putcharPosition)
2828
result := putcharStdout.BlockingWriteAndFlush(list)
29-
if err, isErr := result.Err(); isErr {
29+
if err := result.Err(); err != nil {
3030
// TODO(ydnar): handle error case
31-
panic(err)
31+
panic(*err)
3232
}
3333
putcharPosition = 0
3434
}

src/syscall/libc_wasip2.go

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,19 @@ func readStream(stream *wasiStream, buf *byte, count uint, offset int64) int {
149149

150150
libcErrno = 0
151151
result := stream.in.BlockingRead(uint64(count))
152-
if err, isErr := result.Err(); isErr {
152+
if err := result.Err(); err != nil {
153153
if err.Closed() {
154154
libcErrno = 0
155155
return 0
156-
} else if err, ok := err.LastOperationFailed(); ok {
157-
wasiErrno = err
156+
} else if err := err.LastOperationFailed(); err != nil {
157+
wasiErrno = *err
158158
libcErrno = uintptr(EWASIERROR)
159159
}
160160
return -1
161161
}
162162

163-
list, _ := result.OK()
164163
dst := unsafe.Slice(buf, count)
164+
list := result.OK()
165165
copy(dst, list.Slice())
166166
return int(list.Len())
167167
}
@@ -189,12 +189,12 @@ func writeStream(stream *wasiStream, buf *byte, count uint, offset int64) int {
189189
len = remaining
190190
}
191191
result := stream.out.BlockingWriteAndFlush(cm.ToList(src[:len]))
192-
if err, isErr := result.Err(); isErr {
192+
if err := result.Err(); err != nil {
193193
if err.Closed() {
194194
libcErrno = 0
195195
return 0
196-
} else if err, ok := err.LastOperationFailed(); ok {
197-
wasiErrno = err
196+
} else if err := err.LastOperationFailed(); err != nil {
197+
wasiErrno = *err
198198
libcErrno = uintptr(EWASIERROR)
199199
}
200200
return -1
@@ -236,13 +236,12 @@ func pread(fd int32, buf *byte, count uint, offset int64) int {
236236
}
237237

238238
result := streams.d.Read(types.FileSize(count), types.FileSize(offset))
239-
if code, isErr := result.Err(); isErr {
240-
libcErrno = uintptr(errorCodeToErrno(code))
239+
if err := result.Err(); err != nil {
240+
libcErrno = uintptr(errorCodeToErrno(*err))
241241
return -1
242242
}
243243

244-
listAndEOF, _ := result.OK()
245-
list := listAndEOF.V0
244+
list := result.OK().V0
246245
copy(unsafe.Slice(buf, count), list.Slice())
247246

248247
// TODO(dgryski): EOF bool is ignored?
@@ -275,14 +274,13 @@ func pwrite(fd int32, buf *byte, count uint, offset int64) int {
275274
}
276275

277276
result := streams.d.Write(cm.NewList(buf, count), types.FileSize(offset))
278-
if code, isErr := result.Err(); isErr {
277+
if err := result.Err(); err != nil {
279278
// TODO(dgryski):
280-
libcErrno = uintptr(errorCodeToErrno(code))
279+
libcErrno = uintptr(errorCodeToErrno(*err))
281280
return -1
282281
}
283282

284-
size, _ := result.OK()
285-
return int(size)
283+
return int(*result.OK())
286284
}
287285

288286
// ssize_t lseek(int fd, off_t offset, int whence);
@@ -302,12 +300,11 @@ func lseek(fd int32, offset int64, whence int) int64 {
302300
stream.offset += offset
303301
case 2: // SEEK_END
304302
result := stream.d.Stat()
305-
if code, isErr := result.Err(); isErr {
306-
libcErrno = uintptr(errorCodeToErrno(code))
303+
if err := result.Err(); err != nil {
304+
libcErrno = uintptr(errorCodeToErrno(*err))
307305
return -1
308306
}
309-
stat, _ := result.OK()
310-
stream.offset = int64(stat.Size) + offset
307+
stream.offset = int64(result.OK().Size) + offset
311308
}
312309

313310
return int64(stream.offset)
@@ -443,13 +440,12 @@ func stat(pathname *byte, dst *Stat_t) int32 {
443440
dir, relPath := findPreopenForPath(path)
444441

445442
result := dir.StatAt(0, relPath)
446-
if code, isErr := result.Err(); isErr {
447-
libcErrno = uintptr(errorCodeToErrno(code))
443+
if err := result.Err(); err != nil {
444+
libcErrno = uintptr(errorCodeToErrno(*err))
448445
return -1
449446
}
450447

451-
wasiStat, _ := result.OK()
452-
setStatFromWASIStat(dst, &wasiStat)
448+
setStatFromWASIStat(dst, result.OK())
453449

454450
return 0
455451
}
@@ -475,13 +471,12 @@ func fstat(fd int32, dst *Stat_t) int32 {
475471
// }
476472

477473
result := stream.d.Stat()
478-
if code, isErr := result.Err(); isErr {
479-
libcErrno = uintptr(errorCodeToErrno(code))
474+
if err := result.Err(); err != nil {
475+
libcErrno = uintptr(errorCodeToErrno(*err))
480476
return -1
481477
}
482478

483-
wasiStat, _ := result.OK()
484-
setStatFromWASIStat(dst, &wasiStat)
479+
setStatFromWASIStat(dst, result.OK())
485480

486481
return 0
487482
}
@@ -506,18 +501,18 @@ func setStatFromWASIStat(sstat *Stat_t, wstat *types.DescriptorStat) {
506501
sstat.Blksize = 512
507502
sstat.Blocks = (sstat.Size + 511) / int64(sstat.Blksize)
508503

509-
setOptTime := func(t *Timespec, o *cm.Option[wallclock.DateTime]) {
504+
setOptTime := func(t *Timespec, o *wallclock.DateTime) {
510505
t.Sec = 0
511506
t.Nsec = 0
512-
if some, ok := o.Some(); ok {
513-
t.Sec = int32(some.Seconds)
514-
t.Nsec = int64(some.Nanoseconds)
507+
if o != nil {
508+
t.Sec = int32(o.Seconds)
509+
t.Nsec = int64(o.Nanoseconds)
515510
}
516511
}
517512

518-
setOptTime(&sstat.Atim, &wstat.DataAccessTimestamp)
519-
setOptTime(&sstat.Mtim, &wstat.DataModificationTimestamp)
520-
setOptTime(&sstat.Ctim, &wstat.StatusChangeTimestamp)
513+
setOptTime(&sstat.Atim, wstat.DataAccessTimestamp.Some())
514+
setOptTime(&sstat.Mtim, wstat.DataModificationTimestamp.Some())
515+
setOptTime(&sstat.Ctim, wstat.StatusChangeTimestamp.Some())
521516
}
522517

523518
// int lstat(const char *path, struct stat * buf);
@@ -528,13 +523,12 @@ func lstat(pathname *byte, dst *Stat_t) int32 {
528523
dir, relPath := findPreopenForPath(path)
529524

530525
result := dir.StatAt(0, relPath)
531-
if code, isErr := result.Err(); isErr {
532-
libcErrno = uintptr(errorCodeToErrno(code))
526+
if err := result.Err(); err != nil {
527+
libcErrno = uintptr(errorCodeToErrno(*err))
533528
return -1
534529
}
535530

536-
wasiStat, _ := result.OK()
537-
setStatFromWASIStat(dst, &wasiStat)
531+
setStatFromWASIStat(dst, result.OK())
538532

539533
return 0
540534
}
@@ -608,25 +602,23 @@ func open(pathname *byte, flags int32, mode uint32) int32 {
608602
}
609603

610604
result := dir.OpenAt(pflags, path, oflags, dflags)
611-
if code, isErr := result.Err(); isErr {
612-
libcErrno = uintptr(errorCodeToErrno(code))
605+
if err := result.Err(); err != nil {
606+
libcErrno = uintptr(errorCodeToErrno(*err))
613607
return -1
614608
}
615609

616-
desc, _ := result.OK()
617610
stream := wasiFile{
618-
d: desc,
611+
d: *result.OK(),
619612
oflag: flags,
620613
}
621614

622615
if flags&(O_WRONLY|O_APPEND) == (O_WRONLY | O_APPEND) {
623616
result := stream.d.Stat()
624-
if code, isErr := result.Err(); isErr {
625-
libcErrno = uintptr(errorCodeToErrno(code))
617+
if err := result.Err(); err != nil {
618+
libcErrno = uintptr(errorCodeToErrno(*err))
626619
return -1
627620
}
628-
stat, _ := result.OK()
629-
stream.offset = int64(stat.Size)
621+
stream.offset = int64(result.OK().Size)
630622
}
631623

632624
libcfd := nextLibcFd
Submodule wasm-tools-go updated 139 files

0 commit comments

Comments
 (0)