js_rt_runtime.go 920 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package ngjsvm
  2. import (
  3. "github.com/dop251/goja"
  4. "time"
  5. )
  6. type JSRtRuntime struct {
  7. isReg bool
  8. env *JSEnv
  9. }
  10. func (j *JSRtRuntime) Dispose() {
  11. }
  12. func (j *JSRtRuntime) RegisterRt(name string, env *JSEnv) (goja.Value, error) {
  13. j.env = env
  14. fmap := map[string]interface{}{
  15. "quit": j.J_quit,
  16. "sleep": j.J_sleep,
  17. "getScope": j.J_getScope,
  18. }
  19. obj := j.env.BuildObject(name, fmap)
  20. j.isReg = true
  21. return obj, nil
  22. }
  23. func (j *JSRtRuntime) IsRegistered() bool {
  24. return j.isReg
  25. }
  26. func (j *JSRtRuntime) J_quit(call goja.FunctionCall) goja.Value {
  27. if !j.isReg {
  28. return goja.Undefined()
  29. }
  30. j.env.JSCallQuit()
  31. return goja.Undefined()
  32. }
  33. func (j *JSRtRuntime) J_sleep(xtime goja.Value) {
  34. if !j.isReg {
  35. return
  36. }
  37. t := xtime.ToInteger()
  38. time.Sleep(time.Duration(t) * time.Millisecond)
  39. }
  40. func (j *JSRtRuntime) J_getScope() string {
  41. if !j.isReg {
  42. return ""
  43. }
  44. return j.env.GetCurrentScopeName()
  45. }