fix: do not use goja.Value outside of loop
arcad/edge/pipeline/head This commit looks good Details

ref #22
This commit is contained in:
wpetit 2023-12-05 21:27:43 +01:00
parent 753a6c9708
commit 59f023a7d9
2 changed files with 8 additions and 10 deletions

View File

@ -46,11 +46,11 @@ func NewPromiseProxyFrom(rt *goja.Runtime) *PromiseProxy {
return NewPromiseProxy(promise, resolve, reject) return NewPromiseProxy(promise, resolve, reject)
} }
func IsPromise(v goja.Value) (*goja.Promise, bool) { func isPromise(v any) (*goja.Promise, bool) {
if v == nil { if v == nil {
return nil, false return nil, false
} }
promise, ok := v.Export().(*goja.Promise) promise, ok := v.(*goja.Promise)
return promise, ok return promise, ok
} }

View File

@ -23,7 +23,7 @@ type Server struct {
modules []ServerModule modules []ServerModule
} }
func (s *Server) ExecFuncByName(ctx context.Context, funcName string, args ...interface{}) (any, error) { func (s *Server) ExecFuncByName(ctx context.Context, funcName string, args ...any) (any, error) {
ctx = logger.With(ctx, logger.F("function", funcName), logger.F("args", args)) ctx = logger.With(ctx, logger.F("function", funcName), logger.F("args", args))
ret, err := s.Exec(ctx, funcName, args...) ret, err := s.Exec(ctx, funcName, args...)
@ -34,9 +34,9 @@ func (s *Server) ExecFuncByName(ctx context.Context, funcName string, args ...in
return ret, nil return ret, nil
} }
func (s *Server) Exec(ctx context.Context, callableOrFuncname any, args ...interface{}) (any, error) { func (s *Server) Exec(ctx context.Context, callableOrFuncname any, args ...any) (any, error) {
type result struct { type result struct {
value goja.Value value any
err error err error
} }
@ -110,7 +110,7 @@ func (s *Server) Exec(ctx context.Context, callableOrFuncname any, args ...inter
} }
done <- result{ done <- result{
value: value, value: value.Export(),
} }
logger.Debug(ctx, "executed callable", logger.F("callable", callableOrFuncname), logger.F("duration", time.Since(start).String())) logger.Debug(ctx, "executed callable", logger.F("callable", callableOrFuncname), logger.F("duration", time.Since(start).String()))
@ -129,13 +129,11 @@ func (s *Server) Exec(ctx context.Context, callableOrFuncname any, args ...inter
return nil, errors.WithStack(result.err) return nil, errors.WithStack(result.err)
} }
value := result.value if promise, ok := isPromise(result.value); ok {
if promise, ok := IsPromise(value); ok {
return s.waitForPromise(promise), nil return s.waitForPromise(promise), nil
} }
return value.Export(), nil return result.value, nil
} }
} }