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.
 
 
 
 
 

205 lines
6.2 KiB

  1. ;;; indium-structs.el --- CL structs for Indium backends -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017-2018 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-location' represents a location (most often to a file). A location
  22. ;; is a struct with a `line' and `column' slot. If a location points to a local
  23. ;; file, it also contains a `file' slot. Columns are 0-based and lines are
  24. ;; 1-based.
  25. ;;
  26. ;; `indium-frame' represents a call frame in the context of debugging.
  27. ;;
  28. ;; `indium-breakpoint' represents a breakpoint set at a location with a possible
  29. ;; breaking condition.
  30. ;;; Code:
  31. (require 'map)
  32. (require 'subr-x)
  33. (cl-defstruct
  34. (indium-location (:constructor indium-location-create)
  35. (:constructor indium-location-at-point
  36. (&aux (file buffer-file-name)
  37. (line (line-number-at-pos))
  38. (column (current-column))))
  39. (:constructor indium-location-from-alist
  40. (alist &aux
  41. (file (map-elt alist 'file))
  42. (line (map-elt alist 'line))
  43. (column (map-elt alist 'column))))
  44. (:copier nil))
  45. (line 1)
  46. (column 0)
  47. (file nil))
  48. (cl-defstruct (indium-breakpoint
  49. (:constructor indium-breakpoint-create
  50. (&key condition
  51. overlay
  52. id))
  53. (:copier nil))
  54. (id (indium-structs--next-breakpoint-id))
  55. (overlay nil)
  56. (resolved nil)
  57. (condition ""))
  58. (defun indium-breakpoint-location (brk)
  59. "Return the location of BRK."
  60. (when-let ((ov (indium-breakpoint-overlay brk))
  61. (pos (overlay-start ov))
  62. (buf (overlay-buffer ov)))
  63. (with-current-buffer buf
  64. (save-excursion
  65. (goto-char (point-min))
  66. (forward-char (1- pos))
  67. (back-to-indentation)
  68. (indium-location-at-point)))))
  69. (defun indium-breakpoint-buffer (breakpoint)
  70. "Return the buffer in which BREAKPOINT is set, or nil."
  71. (when-let ((ov (indium-breakpoint-overlay breakpoint)))
  72. (overlay-buffer ov)))
  73. (defun indium-breakpoint-unresolved-p (breakpoint)
  74. "Return non-nil if BREAKPOINT is not yet resolved in the runtime."
  75. (not (indium-breakpoint-resolved breakpoint)))
  76. (cl-defstruct (indium-frame
  77. (:constructor indium-frame-create
  78. (&key script-id
  79. function-name
  80. location
  81. scope-chain))
  82. (:constructor indium-frame-from-alist
  83. (alist &aux
  84. (id (map-elt alist 'id))
  85. (script-id (map-elt alist 'scriptId))
  86. (function-name (map-elt alist 'functionName))
  87. (location (indium-location-from-alist
  88. (map-elt alist 'location)))
  89. (scope-chain (seq-map #'indium-scope-from-alist
  90. (map-elt alist 'scopeChain)))))
  91. (:copier nil))
  92. (id "")
  93. (function-name "")
  94. (script-id "")
  95. (location nil)
  96. (scope-chain nil))
  97. (cl-defstruct (indium-scope
  98. (:constructor indium-scope-create
  99. (&key type
  100. name
  101. id))
  102. (:constructor indium-scope-from-alist
  103. (alist &aux
  104. (type (map-elt alist 'type))
  105. (name (map-elt alist 'name))
  106. (id (map-elt alist 'id))))
  107. (:copier nil))
  108. (id "")
  109. (name "")
  110. (type ""))
  111. (cl-defstruct (indium-remote-object
  112. (:constructor indium-remote-object-create
  113. (&key id
  114. type
  115. description
  116. preview))
  117. (:constructor indium-remote-object-from-alist
  118. (alist &aux
  119. (id (map-elt alist 'id))
  120. (type (map-elt alist 'type))
  121. (description (map-elt alist 'description))
  122. (preview (map-elt alist 'preview))))
  123. (:copier nil))
  124. (id nil)
  125. (type "")
  126. (description "")
  127. (preview ""))
  128. (defun indium-remote-object-error-p (obj)
  129. "Return non-nil if OBJ represents an error."
  130. (equal (indium-remote-object-type obj) "error"))
  131. (defun indium-remote-object-reference-p (obj)
  132. "Return non-nil if OBJ is a reference to a remote object."
  133. (let ((id (indium-remote-object-id obj)))
  134. (and (not (null id))
  135. (not (string-empty-p id)))))
  136. (defun indium-remote-object-function-p (obj)
  137. "Return non-nil if OBJ represents a function."
  138. (equal (indium-remote-object-type obj) "function"))
  139. (defun indium-remote-object-has-preview-p (obj)
  140. "Return non-nil if OBJ has a preview string."
  141. (let ((preview (indium-remote-object-preview obj)))
  142. (and preview (not (string-empty-p preview)))))
  143. (defun indium-remote-object-to-string (obj &optional full)
  144. "Return a short string describing OBJ.
  145. When FULL is non-nil, do not strip long descriptions and function
  146. definitions."
  147. (if (and (not full) (indium-remote-object-function-p obj))
  148. "function"
  149. (indium-remote-object-description obj)))
  150. (cl-defstruct (indium-property
  151. (:constructor indium-property-create
  152. (&key name
  153. remote-object))
  154. (:constructor indium-property-from-alist
  155. (alist &aux
  156. (name (map-elt alist 'name))
  157. (remote-object (indium-remote-object-from-alist
  158. (map-elt alist 'value)))))
  159. (:copier nil))
  160. (name "")
  161. (remote-object nil))
  162. (defun indium-property-native-p (property)
  163. "Return non-nil value if PROPERTY is native code."
  164. (string-match-p "{ \\[native code\\] }$"
  165. (or (indium-remote-object-description
  166. (indium-property-remote-object
  167. property))
  168. "")))
  169. (defvar indium-structs--breakpoint-id 0
  170. "A number that gets incremented by `indium-structs--next-breakpoint-id'.")
  171. (defun indium-structs--next-breakpoint-id ()
  172. "Return a number, different at each call."
  173. (cl-incf indium-structs--breakpoint-id)
  174. indium-structs--breakpoint-id)
  175. (provide 'indium-structs)
  176. ;;; indium-structs.el ends here