Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions asm_386.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
TEXT ·waserror(SB),NOSPLIT|NOFRAME,$0
NO_LOCAL_POINTERS
MOVL cp+0(FP), DI
MOVL SP, Checkpoint_sp(DI)
MOVL DX, Checkpoint_ctxt(DI)
MOVL SP, regs_sp(DI)
MOVL DX, regs_ctxt(DI)
MOVL (SP), AX
MOVL AX, Checkpoint_pc(DI)
MOVL AX, regs_pc(DI)
MOVL (TLS), CX
MOVL (g_stack+stack_hi)(CX), AX
MOVL AX, Checkpoint_probe(DI)
MOVL AX, regs_probe(DI)
MOVB $0, ret+4(FP)
RET

TEXT ·raise(SB),NOSPLIT|NOFRAME,$0
NO_LOCAL_POINTERS
MOVL cp+0(FP), DI
MOVL Checkpoint_sp(DI), SP
MOVL Checkpoint_ctxt(DI), DX
MOVL Checkpoint_pc(DI), AX
MOVL regs_sp(DI), SP
MOVL regs_ctxt(DI), DX
MOVL regs_pc(DI), AX
MOVL AX, (SP)
MOVB $1, ret+4(FP)
RET
Expand Down
18 changes: 9 additions & 9 deletions asm_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
TEXT ·waserror(SB),NOSPLIT|NOFRAME,$0
NO_LOCAL_POINTERS
MOVQ cp+0(FP), DI
MOVQ SP, Checkpoint_sp(DI)
MOVQ BP, Checkpoint_bp(DI)
MOVQ DX, Checkpoint_ctxt(DI)
MOVQ SP, regs_sp(DI)
MOVQ BP, regs_bp(DI)
MOVQ DX, regs_ctxt(DI)
MOVQ (SP), AX
MOVQ AX, Checkpoint_pc(DI)
MOVQ AX, regs_pc(DI)
MOVQ (g_stack+stack_hi)(g), AX // g.stack.hi -> AX
MOVQ AX, Checkpoint_probe(DI)
MOVQ AX, regs_probe(DI)
MOVB $0, ret+8(FP)
RET

TEXT ·raise(SB),NOSPLIT|NOFRAME,$0
NO_LOCAL_POINTERS
MOVQ cp+0(FP), DI
MOVQ Checkpoint_sp(DI), SP
MOVQ Checkpoint_bp(DI), BP
MOVQ Checkpoint_ctxt(DI), DX
MOVQ Checkpoint_pc(DI), AX
MOVQ regs_sp(DI), SP
MOVQ regs_bp(DI), BP
MOVQ regs_ctxt(DI), DX
MOVQ regs_pc(DI), AX
MOVQ AX, (SP)
MOVB $1, ret+8(FP)
RET
Expand Down
18 changes: 9 additions & 9 deletions asm_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ TEXT ·waserror(SB),NOSPLIT|NOFRAME,$0
NO_LOCAL_POINTERS
MOVD cp+0(FP), R2
MOVD RSP, R0
MOVD R0, Checkpoint_sp(R2)
MOVD R29, Checkpoint_bp(R2)
MOVD R26, Checkpoint_ctxt(R2)
MOVD LR, Checkpoint_pc(R2)
MOVD R0, regs_sp(R2)
MOVD R29, regs_bp(R2)
MOVD R26, regs_ctxt(R2)
MOVD LR, regs_pc(R2)
MOVD (g_stack+stack_hi)(g), R0 // g.stack.hi -> R0
MOVD R0, Checkpoint_probe(R2)
MOVD R0, regs_probe(R2)
MOVD $0, R0
MOVD R0, ret+8(FP)
RET

TEXT ·raise(SB),NOSPLIT|NOFRAME,$0
NO_LOCAL_POINTERS
MOVD cp+0(FP), R2
MOVD Checkpoint_sp(R2), R0
MOVD regs_sp(R2), R0
MOVD R0, RSP
MOVD Checkpoint_bp(R2), R29
MOVD Checkpoint_ctxt(R2), R26
MOVD Checkpoint_pc(R2), LR
MOVD regs_bp(R2), R29
MOVD regs_ctxt(R2), R26
MOVD regs_pc(R2), LR
MOVD $1, R0
MOVD R0, ret+8(FP)
RET
Expand Down
10 changes: 10 additions & 0 deletions require.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build ignore

package main

import "github.com/lufia/try"

func needInlines() {
try.Handle()
try.HandleFor[error]()
}
19 changes: 17 additions & 2 deletions require.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
#!/bin/bash
# require.sh - check whether Handle and HandleFor can be inlined

# func Handle must be inlined.
go build -gcflags=-m 2>&1 | grep 'can inline'
go build -gcflags=-m require.go 2>&1 |
awk '
/inlining call to / && /try\.Handle$/ {
handle = 1
}
/inlining call to / && /try\.HandleFor\[/ {
handlefor = 1
}
END {
if(handle <= 0)
print "cannot not inline Handle" >"/dev/stderr"
if(handlefor <= 0)
print "cannot not inline HandleFor[E]" >"/dev/stderr"
exit(handle + handlefor == 2 ? 0 : 1)
}
'
90 changes: 59 additions & 31 deletions try.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,50 @@ package try
import (
"errors"
"fmt"
"unsafe"
)

// Checkpoint represents the fallback point.
type Checkpoint struct {
type Checkpoint[E error] struct {
regs
err E
handlers []func(err E) E
}

type regs struct {
sp uintptr
bp uintptr
ctxt uintptr
pc uintptr
probe uintptr // BP of Handle's parent

err error
handlers []func(err error) error
}

// Option configures [Check], [Check1] and [Check2].
type Option func(*Checkpoint)
type Option[E error] func(*Checkpoint[E])

func applyOpts(cp *Checkpoint, opts ...Option) {
func applyOpts[E error](cp *Checkpoint[E], opts ...Option[E]) {
for _, opt := range opts {
opt(cp)
}
}

func WithHandler(f func(err error) error) Option {
return func(cp *Checkpoint) {
func WithHandler[E error](f func(err E) E) Option[E] {
return func(cp *Checkpoint[E]) {
cp.handlers = append(cp.handlers, f)
}
}

func WithDescription(format string, args ...any) Option {
func WithDescription(format string, args ...any) Option[error] {
prefix := fmt.Sprintf(format, args...)
return func(cp *Checkpoint) {
return func(cp *Checkpoint[error]) {
cp.handlers = append(cp.handlers, func(err error) error {
return fmt.Errorf("%s: %w", prefix, err)
})
}
}

func WithIgnore(errs ...error) Option {
return func(cp *Checkpoint) {
func WithIgnore(errs ...error) Option[error] {
return func(cp *Checkpoint[error]) {
cp.handlers = append(cp.handlers, func(err error) error {
for _, e := range errs {
if errors.Is(err, e) {
Expand All @@ -55,26 +59,50 @@ func WithIgnore(errs ...error) Option {
}
}

func waserror(cp *Checkpoint) bool
func raise(cp *Checkpoint) bool
func waserror(cp uintptr) bool
func raise(cp uintptr) bool
func stkhi() uintptr

// Handle creates a fallback point.
func Handle() (*Checkpoint, error) {
var cp Checkpoint
if waserror(&cp) {
func Handle() (*Checkpoint[error], error) {
var cp Checkpoint[error]
if waserror(uintptr(unsafe.Pointer(&cp))) {
return nil, cp.err
}
return &cp, nil
}

func (cp *Checkpoint) raise(skip int, err error) {
if err == nil {
// HandleFor creates a fallback point for the type argument E.
func HandleFor[E error]() (*Checkpoint[E], E) {
var cp Checkpoint[E]
if waserror(uintptr(unsafe.Pointer(&cp))) {
return nil, cp.err
}
var zero E
return &cp, zero
}

func isZero[T any](v T) bool {
n := unsafe.Sizeof(v)
if n == 0 {
return true
}
b := unsafe.Slice((*byte)(unsafe.Pointer(&v)), n)
for _, n := range b {
if n != 0 {
return false
}
}
return true
}

func (cp *Checkpoint[E]) raise(skip int, err E) {
if isZero(err) {
return
}
for _, f := range cp.handlers {
err = f(err)
if err == nil {
if isZero(err) {
return
}
}
Expand All @@ -86,51 +114,51 @@ func (cp *Checkpoint) raise(skip int, err error) {
cp.sp += d
cp.bp += d
cp.ctxt += d
raise(cp)
raise(uintptr(unsafe.Pointer(cp)))
panic("do not reach here")
}

// Rewind rewinds current execution point to cp.
//
// Deprecated: use goto statement instead.
func (cp *Checkpoint) Rewind(err error) {
func (cp *Checkpoint[E]) Rewind(err E) {
cp.raise(1, err)
}

type RewinderFunc func(*Checkpoint, ...Option)
type RewinderFunc[E error] func(*Checkpoint[E], ...Option[E])

// Check checks whether err is not nil.
// If err is nil, it does nothing.
// Otherwise it rewinds to the fallback point s, then [Handle] returns err.
//
// Check should be called on the same stack to [Handle].
func Check(err error) RewinderFunc {
return func(cp *Checkpoint, opts ...Option) {
func Check[E error](err E) RewinderFunc[E] {
return func(cp *Checkpoint[E], opts ...Option[E]) {
applyOpts(cp, opts...)
cp.raise(1, err)
}
}

type RewinderFunc1[T any] func(*Checkpoint, ...Option) T
type RewinderFunc1[T any, E error] func(*Checkpoint[E], ...Option[E]) T

// Check1 checks whether err is not nil.
// If err is nil, it returns v.
// Otherwise it rewinds to the fallback point s, then [Handle] returns err.
//
// Check1 should be called on the same stack to [Handle].
func Check1[T any](v T, err error) RewinderFunc1[T] {
return func(cp *Checkpoint, opts ...Option) T {
func Check1[T any, E error](v T, err E) RewinderFunc1[T, E] {
return func(cp *Checkpoint[E], opts ...Option[E]) T {
applyOpts(cp, opts...)
cp.raise(1, err)
return v
}
}

type RewinderFunc2[T1, T2 any] func(*Checkpoint, ...Option) (T1, T2)
type RewinderFunc2[T1, T2 any, E error] func(*Checkpoint[E], ...Option[E]) (T1, T2)

// Check2 is a variant of [Check1].
func Check2[T1, T2 any](v1 T1, v2 T2, err error) RewinderFunc2[T1, T2] {
return func(cp *Checkpoint, opts ...Option) (T1, T2) {
func Check2[T1, T2 any, E error](v1 T1, v2 T2, err E) RewinderFunc2[T1, T2, E] {
return func(cp *Checkpoint[E], opts ...Option[E]) (T1, T2) {
applyOpts(cp, opts...)
cp.raise(1, err)
return v1, v2
Expand Down
22 changes: 22 additions & 0 deletions try_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import (
"github.com/m-mizutani/gt"
)

func TestIsZero(t *testing.T) {
type (
Z struct{}
U struct{ N byte }
)
gt.Bool(t, isZero(Z{})).True()
gt.Bool(t, isZero(U{N: 1})).False()
gt.Bool(t, isZero(U{})).True()
gt.Bool(t, isZero[*int](nil)).True()
gt.Bool(t, isZero(new(int))).False()
}

func TestBP(t *testing.T) {
p1 := stkhi()
p2 := wrapstkhi()
Expand Down Expand Up @@ -49,6 +61,16 @@ func TestCheckpointRewind(t *testing.T) {
cp2.Rewind(e)
}

func TestHandleFor(t *testing.T) {
e := errors.New("rewinded")
cp, err := HandleFor[error]()
if err != nil {
gt.Error(t, err).Is(e)
return
}
Check(e)(cp)
}

func TestCheckpointCheck(t *testing.T) {
raised := false
cp, err := Handle()
Expand Down
Loading