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

Promise resolve and reject now return Interrupt errors #624

Merged
merged 3 commits into from
Oct 24, 2024
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: 9 additions & 5 deletions builtin_promise.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,14 +595,17 @@ func (r *Runtime) getPromise() *Object {
return ret
}

func (r *Runtime) wrapPromiseReaction(fObj *Object) func(interface{}) {
func (r *Runtime) wrapPromiseReaction(fObj *Object) func(interface{}) error {
f, _ := AssertFunction(fObj)
return func(x interface{}) {
_, _ = f(nil, r.ToValue(x))
return func(x interface{}) error {
_, err := f(nil, r.ToValue(x))
return err
}
}

// NewPromise creates and returns a Promise and resolving functions for it.
// The returned errors will be uncatchable errors, such as InterruptedError or StackOverflowError, which should be propagated upwards.
// Exceptions are handled through [PromiseRejectionTracker].
//
// WARNING: The returned values are not goroutine-safe and must not be called in parallel with VM running.
// In order to make use of this method you need an event loop such as the one in goja_nodejs (https://github.com/dop251/goja_nodejs)
Expand All @@ -617,11 +620,12 @@ func (r *Runtime) wrapPromiseReaction(fObj *Object) func(interface{}) {
// go func() {
// time.Sleep(500 * time.Millisecond) // or perform any other blocking operation
// loop.RunOnLoop(func(*goja.Runtime) { // resolve() must be called on the loop, cannot call it here
// resolve(result)
// err := resolve(result)
// // Handle uncatchable errors (e.g. by stopping the loop, panicking or setting a flag)
// })
mstoykov marked this conversation as resolved.
Show resolved Hide resolved
// }()
// }
func (r *Runtime) NewPromise() (promise *Promise, resolve func(result interface{}), reject func(reason interface{})) {
func (r *Runtime) NewPromise() (promise *Promise, resolve, reject func(reason interface{}) error) {
p := r.newPromise(r.getPromisePrototype())
resolveF, rejectF := p.createResolvingFunctions()
return p, r.wrapPromiseReaction(resolveF), r.wrapPromiseReaction(rejectF)
Expand Down
37 changes: 37 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,43 @@ func TestInterruptInWrappedFunctionExpectStackOverflowError(t *testing.T) {
}
}

func TestInterruptWithPromises(t *testing.T) {
rt := New()
rt.SetMaxCallStackSize(5)
// this test panics as otherwise goja will recover and possibly loop
rt.Set("abort", rt.ToValue(func() {
// panic("waty")
rt.Interrupt("abort this")
}))
var queue = make(chan func() error, 10)
rt.Set("myPromise", func() Value {
p, resolve, _ := rt.NewPromise()
queue <- func() error {
return resolve("some value")
}

return rt.ToValue(p)
})

_, err := rt.RunString(`
let p = myPromise()
p.then(() => { abort() });
`)
if err != nil {
t.Fatal("expected noerror but got error")
}
f := <-queue
err = f()
if err == nil {
t.Fatal("expected error but got no error")
}
t.Log(err)
var soErr *InterruptedError
if !errors.As(err, &soErr) {
t.Fatalf("Wrong error type: %T", err)
}
}

func TestRunLoopPreempt(t *testing.T) {
vm := New()
v, err := vm.RunString("(function() {for (;;) {}})")
Expand Down
Loading