mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-14 11:23:02 +02:00
43 lines
776 B
Go
43 lines
776 B
Go
package runtime
|
|
|
|
import (
|
|
"context"
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
"github.com/MontFerret/ferret/pkg/stdlib/html/driver"
|
|
)
|
|
|
|
type Program struct {
|
|
exp core.Expression
|
|
}
|
|
|
|
func NewProgram(exp core.Expression) *Program {
|
|
return &Program{exp}
|
|
}
|
|
|
|
func (p *Program) Run(ctx context.Context, setters ...Option) ([]byte, error) {
|
|
scope, closeFn := core.NewRootScope()
|
|
|
|
defer closeFn()
|
|
|
|
opts := newOptions()
|
|
|
|
for _, setter := range setters {
|
|
setter(opts)
|
|
}
|
|
|
|
ctx = opts.withContext(ctx)
|
|
ctx = driver.WithCdpDriver(ctx, opts.cdp)
|
|
ctx = driver.WithHttpDriver(ctx)
|
|
|
|
out, err := p.exp.Exec(ctx, scope)
|
|
|
|
if err != nil {
|
|
js, _ := values.None.MarshalJSON()
|
|
|
|
return js, err
|
|
}
|
|
|
|
return out.MarshalJSON()
|
|
}
|