1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package ngjsvm
- import (
- "github.com/dop251/goja"
- "time"
- )
- type JSRtRuntime struct {
- isReg bool
- env *JSEnv
- }
- func (j *JSRtRuntime) Dispose() {
- }
- func (j *JSRtRuntime) RegisterRt(name string, env *JSEnv) (goja.Value, error) {
- j.env = env
- fmap := map[string]interface{}{
- "quit": j.J_quit,
- "sleep": j.J_sleep,
- "getScope": j.J_getScope,
- }
- obj := j.env.BuildObject(name, fmap)
- j.isReg = true
- return obj, nil
- }
- func (j *JSRtRuntime) IsRegistered() bool {
- return j.isReg
- }
- func (j *JSRtRuntime) J_quit(call goja.FunctionCall) goja.Value {
- if !j.isReg {
- return goja.Undefined()
- }
- j.env.JSCallQuit()
- return goja.Undefined()
- }
- func (j *JSRtRuntime) J_sleep(xtime goja.Value) {
- if !j.isReg {
- return
- }
- t := xtime.ToInteger()
- time.Sleep(time.Duration(t) * time.Millisecond)
- }
- func (j *JSRtRuntime) J_getScope() string {
- if !j.isReg {
- return ""
- }
- return j.env.GetCurrentScopeName()
- }
|