A JavaScript development environment for Emacs https://indium.readthedocs.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.1 KiB

  1. ;;; indium-structs.el --- CL structs for Indium backends -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017 Nicolas Petton
  3. ;; Author: Nicolas Petton <nicolas@petton.fr>
  4. ;; Keywords:
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This files defines all objects used in Indium as cl-structs.
  17. ;;
  18. ;; Backends should make instances of the structs defined in this file from data
  19. ;; they receive.
  20. ;;
  21. ;; `indium-script' represents a JavaScript file parsed by the runtime. Scripts
  22. ;; are structs indexed by `id' in the current Indium connection. A script contain
  23. ;; an `url' slot, and an optional `sourcemap-url' slot.
  24. ;;
  25. ;; `indium-location' represents a location (most often to a file). A location
  26. ;; is a struct with a `line' and `column' slot. If a location points to a local
  27. ;; file, it also contains a `file' slot. Columns and lines start at 0.
  28. ;;
  29. ;; `indium-frame' represents a call frame in the context of debugging.
  30. ;;; Code:
  31. (declare-function indium-script-get-file "indium-script.el")
  32. (declare-function indium-script-find-by-id "indium-script.el")
  33. (defmacro when-indium-connected (&rest body)
  34. "Evaluate BODY if there is a current Indium connection."
  35. (declare (indent 0))
  36. `(when indium-current-connection
  37. ,@body))
  38. (defmacro unless-indium-connected (&rest body)
  39. "Evalute BODY unless there is a current Indium connection."
  40. (declare (indent 0))
  41. `(unless indium-current-connection
  42. ,@body))
  43. (defvar indium-current-connection nil
  44. "Current connection to the browser tab.")
  45. (cl-defstruct indium-connection
  46. (backend nil :type symbol :read-only t)
  47. (url nil :type string :read-only t)
  48. (callbacks (make-hash-table) :type hash-table)
  49. (scripts (make-hash-table) :type hash-table)
  50. (breakpoints (make-hash-table) :type hash-table)
  51. (frames nil :type list)
  52. (current-frame nil :type indium-frame)
  53. ;; extra properties that can be added by the backend
  54. (props (make-hash-table) :type hash-table))
  55. (defun indium-current-connection-backend ()
  56. "Return the backend of the current connection if any."
  57. (when-indium-connected
  58. (indium-connection-backend indium-current-connection)))
  59. (defun indium-current-connection-url ()
  60. "Return the url of the current connection if any."
  61. (when-indium-connected
  62. (indium-connection-url indium-current-connection)))
  63. (defun indium-current-connection-callbacks ()
  64. "Return the callbacks of the current connection if any."
  65. (when-indium-connected
  66. (indium-connection-callbacks indium-current-connection)))
  67. (defun indium-current-connection-scripts ()
  68. "Return the scripts of the current connection if any."
  69. (when-indium-connected
  70. (indium-connection-scripts indium-current-connection)))
  71. (defun indium-current-connection-breakpoints ()
  72. "Return the breakpoints of the current connection if any."
  73. (when-indium-connected
  74. (indium-connection-breakpoints indium-current-connection)))
  75. (defun indium-current-connection-props ()
  76. "Return the props of the current connection if any."
  77. (when-indium-connected
  78. (indium-connection-props indium-current-connection)))
  79. (defun indium-current-connection-frames ()
  80. "Return the frames of the current connection if any."
  81. (when-indium-connected
  82. (indium-connection-frames indium-current-connection)))
  83. (cl-defmethod (setf indium-current-connection-frames) (frames)
  84. (when-indium-connected
  85. (setf (indium-connection-frames indium-current-connection) frames)))
  86. (defun indium-current-connection-current-frame ()
  87. "Return the current frame of the current connection if any."
  88. (when-indium-connected
  89. (indium-connection-current-frame indium-current-connection)))
  90. (cl-defmethod (setf indium-current-connection-current-frame) (frame)
  91. (when-indium-connected
  92. (setf (indium-connection-current-frame indium-current-connection) frame)))
  93. (cl-defstruct indium-script
  94. (id nil :type string :read-only t)
  95. (url nil :type string :read-only t)
  96. (sourcemap-url nil :type string :read-only t))
  97. (cl-defstruct
  98. (indium-location
  99. (:constructor make-indium-location-from-script-id
  100. (&key (script-id "")
  101. line
  102. column
  103. &aux (file (indium-script-get-file (indium-script-find-by-id script-id))))))
  104. (line 0 :type number :read-only t)
  105. (column 0 :type number :read-only t)
  106. (file nil :type string :read-only t))
  107. (cl-defstruct indium-frame
  108. (id nil :type string :read-only t)
  109. ;; TODO: make a scope a struct as well.
  110. (scope-chain nil :type list :read-only t)
  111. (location nil :type indium-location :read-only t)
  112. (script nil :type indium-script :read-only t)
  113. (type nil :type string :read-only t)
  114. (function-name nil :type string))
  115. (provide 'indium-structs)
  116. ;;; indium-structs.el ends here