debug_vm.go 988 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package vm
  2. import (
  3. "fmt"
  4. "io"
  5. "github.com/goccy/go-json/internal/encoder"
  6. )
  7. func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) ([]byte, error) {
  8. defer func() {
  9. var code *encoder.Opcode
  10. if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
  11. code = codeSet.EscapeKeyCode
  12. } else {
  13. code = codeSet.NoescapeKeyCode
  14. }
  15. if wc := ctx.Option.DebugDOTOut; wc != nil {
  16. _, _ = io.WriteString(wc, code.DumpDOT())
  17. wc.Close()
  18. ctx.Option.DebugDOTOut = nil
  19. }
  20. if err := recover(); err != nil {
  21. w := ctx.Option.DebugOut
  22. fmt.Fprintln(w, "=============[DEBUG]===============")
  23. fmt.Fprintln(w, "* [TYPE]")
  24. fmt.Fprintln(w, codeSet.Type)
  25. fmt.Fprintf(w, "\n")
  26. fmt.Fprintln(w, "* [ALL OPCODE]")
  27. fmt.Fprintln(w, code.Dump())
  28. fmt.Fprintf(w, "\n")
  29. fmt.Fprintln(w, "* [CONTEXT]")
  30. fmt.Fprintf(w, "%+v\n", ctx)
  31. fmt.Fprintln(w, "===================================")
  32. panic(err)
  33. }
  34. }()
  35. return Run(ctx, b, codeSet)
  36. }