Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recover panic to promise #290

Merged
merged 1 commit into from
Mar 30, 2023
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
3 changes: 2 additions & 1 deletion engine/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func Negate(vm *VM, goal Term, k Cont, env *Env) *Promise {
}

// Call executes goal. it succeeds if goal followed by k succeeds. A cut inside goal doesn't affect outside of Call.
func Call(vm *VM, goal Term, k Cont, env *Env) *Promise {
func Call(vm *VM, goal Term, k Cont, env *Env) (promise *Promise) {
defer ensurePromise(&promise)
switch g := env.Resolve(goal).(type) {
case Variable:
return Error(InstantiationError(env))
Expand Down
10 changes: 10 additions & 0 deletions engine/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ func TestCall(t *testing.T) {
vm.Register0(atomFail, func(_ *VM, f Cont, env *Env) *Promise {
return Bool(false)
})
vm.Register0(NewAtom("do_not_call"), func(*VM, Cont, *Env) *Promise {
panic("told you")
})
vm.Register0(NewAtom("lazy_do_not_call"), func(*VM, Cont, *Env) *Promise {
return Delay(func(context.Context) *Promise {
panic("told you")
})
})
assert.NoError(t, vm.Compile(context.Background(), `
foo.
foo(_, _).
Expand Down Expand Up @@ -49,6 +57,8 @@ f(g([a, [b, c|X]])).

{title: `cover all`, goal: atomComma.Apply(atomCut, NewAtom("f").Apply(NewAtom("g").Apply(List(NewAtom("a"), PartialList(NewVariable(), NewAtom("b"), NewAtom("c")))))), ok: true},
{title: `out of memory`, goal: NewAtom("foo").Apply(NewVariable(), NewVariable(), NewVariable(), NewVariable(), NewVariable(), NewVariable(), NewVariable(), NewVariable(), NewVariable()), err: resourceError(resourceMemory, nil), mem: 1},
{title: `panic`, goal: NewAtom("do_not_call"), err: errors.New("panic: told you")},
{title: `panic (lazy)`, goal: NewAtom("lazy_do_not_call"), err: errors.New("panic: told you")},
}

for _, tt := range tests {
Expand Down
25 changes: 19 additions & 6 deletions engine/promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"context"
"fmt"
)

var (
Expand Down Expand Up @@ -74,7 +75,7 @@ func catch(recover func(error) *Promise, k func(context.Context) *Promise) *Prom
}

// Force enforces the delayed execution and returns the result. (i.e. trampoline)
func (p *Promise) Force(ctx context.Context) (bool, error) {
func (p *Promise) Force(ctx context.Context) (ok bool, err error) {
stack := promiseStack{p}
for len(stack) > 0 {
select {
Expand Down Expand Up @@ -111,12 +112,24 @@ func (p *Promise) Force(ctx context.Context) (bool, error) {
return false, nil
}

func (p *Promise) child(ctx context.Context) *Promise {
q := p.delayed[0](ctx)
if !p.repeat {
p.delayed, p.delayed[0] = p.delayed[1:], nil
func (p *Promise) child(ctx context.Context) (promise *Promise) {
defer ensurePromise(&promise)
defer func() {
if !p.repeat {
p.delayed, p.delayed[0] = p.delayed[1:], nil
}
}()
return p.delayed[0](ctx)
}

func ensurePromise(p **Promise) {
if r := recover(); r != nil {
*p = Error(panicError(r))
}
return q
}

func panicError(r interface{}) error {
return fmt.Errorf("panic: %v", r)
}

type promiseStack []*Promise
Expand Down
4 changes: 3 additions & 1 deletion engine/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ type procedure interface {
type Cont func(*Env) *Promise

// Arrive is the entry point of the VM.
func (vm *VM) Arrive(name Atom, args []Term, k Cont, env *Env) *Promise {
func (vm *VM) Arrive(name Atom, args []Term, k Cont, env *Env) (promise *Promise) {
defer ensurePromise(&promise)

if vm.Unknown == nil {
vm.Unknown = func(Atom, []Term, *Env) {}
}
Expand Down