feat: init js-core

This commit is contained in:
Skyxim
2022-06-04 23:51:13 +08:00
parent 8ff7e180a4
commit 91b4176557
5 changed files with 117 additions and 7 deletions

32
component/js/core.go Normal file
View File

@@ -0,0 +1,32 @@
package js
import (
"github.com/dop251/goja"
"sync"
)
var JS sync.Map
var mux sync.Mutex
func NewJS(name, code string) error {
program, err := compiler(name, code)
if err != nil {
return err
}
if _, ok := JS.Load(name); !ok {
mux.Lock()
defer mux.Unlock()
if _, ok := JS.Load(name); !ok {
JS.Store(name, program)
}
}
return nil
}
func Run(name string, args map[string]any, callback func(any, error)) {
if value, ok := JS.Load(name); ok {
run(getLoop(), value.(*goja.Program), args, callback)
}
}