Files
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

112 lines
1.9 KiB
Go

package jsenv
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/dop251/goja"
)
const (
// placeholder for functions in js env
exportFuncDescriptor = "_expFunc_"
)
type (
Vm struct {
g *goja.Runtime
t Transformer
}
)
func New(t Transformer) Vm {
return Vm{
g: goja.New(),
t: t,
}
}
// New creates a goja internal type
func (ss Vm) New(i interface{}) goja.Value {
return ss.g.ToValue(i)
}
// Register the value in vm
func (ss Vm) Register(key string, i interface{}) error {
return ss.g.Set(key, i)
}
// Fetch value from vm
func (ss Vm) Fetch(key string) goja.Value {
return ss.g.Get(key)
}
// RegisterFunction registers the function to the vm and returns the
// function that can be used in go
func (ss Vm) RegisterFunction(s string, wrapperFn ...func() string) (f *Fn, err error) {
if len(wrapperFn) > 0 {
for _, wfn := range wrapperFn {
s = fmt.Sprintf(wfn(), s)
}
} else {
s = fmt.Sprintf("function (input) { %s }", s)
}
desc := ss.funcDescriptor(s)
run := fmt.Sprintf("var %s=%s;", desc, s)
err = ss.Eval(run)
if err != nil {
return
}
internalF := ss.Fetch(desc)
if internalF == nil {
err = fmt.Errorf("could not fetch registered value")
return
}
fnn, ok := goja.AssertFunction(internalF)
if !ok {
err = fmt.Errorf("could not assert function")
return
}
return &Fn{
f: fnn,
}, nil
}
// Eval transforms the input js to the specified
// version and evals in vm
func (ss Vm) Eval(p string) (err error) {
tr, err := ss.t.Transform(p)
if err != nil {
return
}
_, err = ss.g.RunString(string(tr))
return
}
// Compile is used only when parsing the
// input evaluation without actually running it
func (ss Vm) Compile(p string) (err error) {
_, err = goja.Parse("", p)
return
}
func (ss Vm) genID(s string) string {
h := md5.Sum([]byte(s))
return hex.EncodeToString(h[:])
}
func (ss Vm) funcDescriptor(s string) string {
return fmt.Sprintf("%s%s", exportFuncDescriptor, ss.genID(s))
}