emacs-exwm / xelb

X protocol Emacs Lisp Binding
https://elpa.gnu.org/packages/xelb.html
GNU General Public License v3.0
27 stars 5 forks source link

Use `lambda` instead of evaling s-expressions #18

Open Stebalien opened 9 months ago

Stebalien commented 9 months ago

Several types define expressions to be evaluated at runtime. E.g.:

  1. Switch statements where we have to eval the expression then find the matching case.
  2. List sizes.
  3. Struct sizes.

Instead, we should just use lambda expression so the byte-compiler can optimize them. We can likely provide some nice macros if lambdas become unwieldy. E.g., a switch could be written as:

(defclast ...
  (xcb:-struct)
  ((thing :initform
          (xcb:-switch (xcb:-fieldref 'class-id)
            ((0) pitch duration led-mask led-values global-auto-repeat click percent pad~0 auto-repeats~)
            ((1)
             pad~1 accel-num accel-denom threshold)
            ((2)
             max-symbols num-keysyms keysyms~)
            ((3)
             resolution min-value max-value)
            ((4)
             led-mask* led-values*)
            ((5)
             percent* pad~2 pitch* duration*))))

Where xcb:-switch would compile the switch down to a lambda returning the correct case to use given some obj.

medranocalvo commented 9 months ago

I wrote the comment below first, but realized now that it makes the assumption that the lambdas would be evaluated at :initform time. Would these lambdas be evaluated in xcb:marshall, kind of substituting the eval and case processing?


I think this would imply that the value is determined at construction time instead of at marshalling time. This would prevent building a message piece by piece. Say:

(let (obj (make-instance 'xcb:ConfigureWindow
                         :window xwin
                         :x x :y y :width width :height height))
  (setf (slot-value obj :value-mask)
        (logior (if x xcb:ConfigWindow:X 0)
                (if y xcb:ConfigWindow:Y 0)
                (if width xcb:ConfigWindow:Width 0)
                (if height xcb:ConfigWindow:Height 0))))

We don't do that in EXWM as far as I can see, but I think that it should be supported.

Stebalien commented 9 months ago

Would these lambdas be evaluated in xcb:marshall, kind of substituting the eval and case processing?

Yes. The goal is to have them parsed and byte-compiled along with the rest of the file. Basically, instead of storing the code as raw s-expressions, we'd store it as compiled lambdas.